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/Statistic.h"
104 #include "llvm/ADT/StringExtras.h"
105 #include "llvm/ADT/StringMap.h"
106 #include "llvm/ADT/StringRef.h"
107 #include "llvm/ADT/Triple.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/MemoryBuffer.h"
119 #include "llvm/Support/Path.h"
120 #include "llvm/Support/SaveAndRestore.h"
121 #include "llvm/Support/Timer.h"
122 #include "llvm/Support/VersionTuple.h"
123 #include "llvm/Support/raw_ostream.h"
124 #include <algorithm>
125 #include <cassert>
126 #include <cstddef>
127 #include <cstdint>
128 #include <cstdio>
129 #include <ctime>
130 #include <iterator>
131 #include <limits>
132 #include <map>
133 #include <memory>
134 #include <string>
135 #include <system_error>
136 #include <tuple>
137 #include <utility>
138 #include <vector>
139 
140 using namespace clang;
141 using namespace clang::serialization;
142 using namespace clang::serialization::reader;
143 using llvm::BitstreamCursor;
144 using llvm::RoundingMode;
145 
146 #define DEBUG_TYPE "modules"
147 
148 ALWAYS_ENABLED_STATISTIC(NumTryLoadModule, "Number of times tried to load a "
149                                            "module. Includes failed attempts "
150                                            "and using cached results.");
151 ALWAYS_ENABLED_STATISTIC(NumReadASTCore,
152                          "Number of ReadASTCore() invocations. Includes only "
153                          "actual AST core parsing and ignores cached results.");
154 
155 //===----------------------------------------------------------------------===//
156 // ChainedASTReaderListener implementation
157 //===----------------------------------------------------------------------===//
158 
159 bool
160 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
161   return First->ReadFullVersionInformation(FullVersion) ||
162          Second->ReadFullVersionInformation(FullVersion);
163 }
164 
165 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
166   First->ReadModuleName(ModuleName);
167   Second->ReadModuleName(ModuleName);
168 }
169 
170 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
171   First->ReadModuleMapFile(ModuleMapPath);
172   Second->ReadModuleMapFile(ModuleMapPath);
173 }
174 
175 bool
176 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
177                                               bool Complain,
178                                               bool AllowCompatibleDifferences) {
179   return First->ReadLanguageOptions(LangOpts, Complain,
180                                     AllowCompatibleDifferences) ||
181          Second->ReadLanguageOptions(LangOpts, Complain,
182                                      AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadTargetOptions(
186     const TargetOptions &TargetOpts, bool Complain,
187     bool AllowCompatibleDifferences) {
188   return First->ReadTargetOptions(TargetOpts, Complain,
189                                   AllowCompatibleDifferences) ||
190          Second->ReadTargetOptions(TargetOpts, Complain,
191                                    AllowCompatibleDifferences);
192 }
193 
194 bool ChainedASTReaderListener::ReadDiagnosticOptions(
195     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
196   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
197          Second->ReadDiagnosticOptions(DiagOpts, Complain);
198 }
199 
200 bool
201 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
202                                                 bool Complain) {
203   return First->ReadFileSystemOptions(FSOpts, Complain) ||
204          Second->ReadFileSystemOptions(FSOpts, Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
208     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
209     bool Complain) {
210   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
211                                         Complain) ||
212          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
213                                          Complain);
214 }
215 
216 bool ChainedASTReaderListener::ReadPreprocessorOptions(
217     const PreprocessorOptions &PPOpts, bool Complain,
218     std::string &SuggestedPredefines) {
219   return First->ReadPreprocessorOptions(PPOpts, Complain,
220                                         SuggestedPredefines) ||
221          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
222 }
223 
224 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
225                                            unsigned Value) {
226   First->ReadCounter(M, Value);
227   Second->ReadCounter(M, Value);
228 }
229 
230 bool ChainedASTReaderListener::needsInputFileVisitation() {
231   return First->needsInputFileVisitation() ||
232          Second->needsInputFileVisitation();
233 }
234 
235 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
236   return First->needsSystemInputFileVisitation() ||
237   Second->needsSystemInputFileVisitation();
238 }
239 
240 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
241                                                ModuleKind Kind) {
242   First->visitModuleFile(Filename, Kind);
243   Second->visitModuleFile(Filename, Kind);
244 }
245 
246 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
247                                               bool isSystem,
248                                               bool isOverridden,
249                                               bool isExplicitModule) {
250   bool Continue = false;
251   if (First->needsInputFileVisitation() &&
252       (!isSystem || First->needsSystemInputFileVisitation()))
253     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
254                                       isExplicitModule);
255   if (Second->needsInputFileVisitation() &&
256       (!isSystem || Second->needsSystemInputFileVisitation()))
257     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
258                                        isExplicitModule);
259   return Continue;
260 }
261 
262 void ChainedASTReaderListener::readModuleFileExtension(
263        const ModuleFileExtensionMetadata &Metadata) {
264   First->readModuleFileExtension(Metadata);
265   Second->readModuleFileExtension(Metadata);
266 }
267 
268 //===----------------------------------------------------------------------===//
269 // PCH validator implementation
270 //===----------------------------------------------------------------------===//
271 
272 ASTReaderListener::~ASTReaderListener() = default;
273 
274 /// Compare the given set of language options against an existing set of
275 /// language options.
276 ///
277 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
278 /// \param AllowCompatibleDifferences If true, differences between compatible
279 ///        language options will be permitted.
280 ///
281 /// \returns true if the languagae options mis-match, false otherwise.
282 static bool checkLanguageOptions(const LangOptions &LangOpts,
283                                  const LangOptions &ExistingLangOpts,
284                                  DiagnosticsEngine *Diags,
285                                  bool AllowCompatibleDifferences = true) {
286 #define LANGOPT(Name, Bits, Default, Description)                 \
287   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
288     if (Diags)                                                    \
289       Diags->Report(diag::err_pch_langopt_mismatch)               \
290         << Description << LangOpts.Name << ExistingLangOpts.Name; \
291     return true;                                                  \
292   }
293 
294 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
295   if (ExistingLangOpts.Name != LangOpts.Name) {           \
296     if (Diags)                                            \
297       Diags->Report(diag::err_pch_langopt_value_mismatch) \
298         << Description;                                   \
299     return true;                                          \
300   }
301 
302 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
303   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
304     if (Diags)                                                 \
305       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
306         << Description;                                        \
307     return true;                                               \
308   }
309 
310 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
311   if (!AllowCompatibleDifferences)                            \
312     LANGOPT(Name, Bits, Default, Description)
313 
314 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
315   if (!AllowCompatibleDifferences)                                 \
316     ENUM_LANGOPT(Name, Bits, Default, Description)
317 
318 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
319   if (!AllowCompatibleDifferences)                                 \
320     VALUE_LANGOPT(Name, Bits, Default, Description)
321 
322 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
323 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
324 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
325 #include "clang/Basic/LangOptions.def"
326 
327   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
328     if (Diags)
329       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
330     return true;
331   }
332 
333   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
334     if (Diags)
335       Diags->Report(diag::err_pch_langopt_value_mismatch)
336       << "target Objective-C runtime";
337     return true;
338   }
339 
340   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
341       LangOpts.CommentOpts.BlockCommandNames) {
342     if (Diags)
343       Diags->Report(diag::err_pch_langopt_value_mismatch)
344         << "block command names";
345     return true;
346   }
347 
348   // Sanitizer feature mismatches are treated as compatible differences. If
349   // compatible differences aren't allowed, we still only want to check for
350   // mismatches of non-modular sanitizers (the only ones which can affect AST
351   // generation).
352   if (!AllowCompatibleDifferences) {
353     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
354     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
355     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
356     ExistingSanitizers.clear(ModularSanitizers);
357     ImportedSanitizers.clear(ModularSanitizers);
358     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
359       const std::string Flag = "-fsanitize=";
360       if (Diags) {
361 #define SANITIZER(NAME, ID)                                                    \
362   {                                                                            \
363     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
364     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
365     if (InExistingModule != InImportedModule)                                  \
366       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
367           << InExistingModule << (Flag + NAME);                                \
368   }
369 #include "clang/Basic/Sanitizers.def"
370       }
371       return true;
372     }
373   }
374 
375   return false;
376 }
377 
378 /// Compare the given set of target options against an existing set of
379 /// target options.
380 ///
381 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
382 ///
383 /// \returns true if the target options mis-match, false otherwise.
384 static bool checkTargetOptions(const TargetOptions &TargetOpts,
385                                const TargetOptions &ExistingTargetOpts,
386                                DiagnosticsEngine *Diags,
387                                bool AllowCompatibleDifferences = true) {
388 #define CHECK_TARGET_OPT(Field, Name)                             \
389   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
390     if (Diags)                                                    \
391       Diags->Report(diag::err_pch_targetopt_mismatch)             \
392         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
393     return true;                                                  \
394   }
395 
396   // The triple and ABI must match exactly.
397   CHECK_TARGET_OPT(Triple, "target");
398   CHECK_TARGET_OPT(ABI, "target ABI");
399 
400   // We can tolerate different CPUs in many cases, notably when one CPU
401   // supports a strict superset of another. When allowing compatible
402   // differences skip this check.
403   if (!AllowCompatibleDifferences) {
404     CHECK_TARGET_OPT(CPU, "target CPU");
405     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
406   }
407 
408 #undef CHECK_TARGET_OPT
409 
410   // Compare feature sets.
411   SmallVector<StringRef, 4> ExistingFeatures(
412                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
413                                              ExistingTargetOpts.FeaturesAsWritten.end());
414   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
415                                          TargetOpts.FeaturesAsWritten.end());
416   llvm::sort(ExistingFeatures);
417   llvm::sort(ReadFeatures);
418 
419   // We compute the set difference in both directions explicitly so that we can
420   // diagnose the differences differently.
421   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
422   std::set_difference(
423       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
424       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
425   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
426                       ExistingFeatures.begin(), ExistingFeatures.end(),
427                       std::back_inserter(UnmatchedReadFeatures));
428 
429   // If we are allowing compatible differences and the read feature set is
430   // a strict subset of the existing feature set, there is nothing to diagnose.
431   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
432     return false;
433 
434   if (Diags) {
435     for (StringRef Feature : UnmatchedReadFeatures)
436       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
437           << /* is-existing-feature */ false << Feature;
438     for (StringRef Feature : UnmatchedExistingFeatures)
439       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
440           << /* is-existing-feature */ true << Feature;
441   }
442 
443   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
444 }
445 
446 bool
447 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
448                                   bool Complain,
449                                   bool AllowCompatibleDifferences) {
450   const LangOptions &ExistingLangOpts = PP.getLangOpts();
451   return checkLanguageOptions(LangOpts, ExistingLangOpts,
452                               Complain ? &Reader.Diags : nullptr,
453                               AllowCompatibleDifferences);
454 }
455 
456 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
457                                      bool Complain,
458                                      bool AllowCompatibleDifferences) {
459   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
460   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
461                             Complain ? &Reader.Diags : nullptr,
462                             AllowCompatibleDifferences);
463 }
464 
465 namespace {
466 
467 using MacroDefinitionsMap =
468     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
469 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
470 
471 } // namespace
472 
473 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
474                                          DiagnosticsEngine &Diags,
475                                          bool Complain) {
476   using Level = DiagnosticsEngine::Level;
477 
478   // Check current mappings for new -Werror mappings, and the stored mappings
479   // for cases that were explicitly mapped to *not* be errors that are now
480   // errors because of options like -Werror.
481   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
482 
483   for (DiagnosticsEngine *MappingSource : MappingSources) {
484     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
485       diag::kind DiagID = DiagIDMappingPair.first;
486       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
487       if (CurLevel < DiagnosticsEngine::Error)
488         continue; // not significant
489       Level StoredLevel =
490           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
491       if (StoredLevel < DiagnosticsEngine::Error) {
492         if (Complain)
493           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
494               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
495         return true;
496       }
497     }
498   }
499 
500   return false;
501 }
502 
503 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
504   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
505   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
506     return true;
507   return Ext >= diag::Severity::Error;
508 }
509 
510 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
511                                     DiagnosticsEngine &Diags,
512                                     bool IsSystem, bool Complain) {
513   // Top-level options
514   if (IsSystem) {
515     if (Diags.getSuppressSystemWarnings())
516       return false;
517     // If -Wsystem-headers was not enabled before, be conservative
518     if (StoredDiags.getSuppressSystemWarnings()) {
519       if (Complain)
520         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
521       return true;
522     }
523   }
524 
525   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
526     if (Complain)
527       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
528     return true;
529   }
530 
531   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
532       !StoredDiags.getEnableAllWarnings()) {
533     if (Complain)
534       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
535     return true;
536   }
537 
538   if (isExtHandlingFromDiagsError(Diags) &&
539       !isExtHandlingFromDiagsError(StoredDiags)) {
540     if (Complain)
541       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
542     return true;
543   }
544 
545   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
546 }
547 
548 /// Return the top import module if it is implicit, nullptr otherwise.
549 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
550                                           Preprocessor &PP) {
551   // If the original import came from a file explicitly generated by the user,
552   // don't check the diagnostic mappings.
553   // FIXME: currently this is approximated by checking whether this is not a
554   // module import of an implicitly-loaded module file.
555   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
556   // the transitive closure of its imports, since unrelated modules cannot be
557   // imported until after this module finishes validation.
558   ModuleFile *TopImport = &*ModuleMgr.rbegin();
559   while (!TopImport->ImportedBy.empty())
560     TopImport = TopImport->ImportedBy[0];
561   if (TopImport->Kind != MK_ImplicitModule)
562     return nullptr;
563 
564   StringRef ModuleName = TopImport->ModuleName;
565   assert(!ModuleName.empty() && "diagnostic options read before module name");
566 
567   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
568   assert(M && "missing module");
569   return M;
570 }
571 
572 bool PCHValidator::ReadDiagnosticOptions(
573     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
574   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
575   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
576   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
577       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
578   // This should never fail, because we would have processed these options
579   // before writing them to an ASTFile.
580   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
581 
582   ModuleManager &ModuleMgr = Reader.getModuleManager();
583   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
584 
585   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
586   if (!TopM)
587     return false;
588 
589   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
590   // contains the union of their flags.
591   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
592                                  Complain);
593 }
594 
595 /// Collect the macro definitions provided by the given preprocessor
596 /// options.
597 static void
598 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
599                         MacroDefinitionsMap &Macros,
600                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
601   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
602     StringRef Macro = PPOpts.Macros[I].first;
603     bool IsUndef = PPOpts.Macros[I].second;
604 
605     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
606     StringRef MacroName = MacroPair.first;
607     StringRef MacroBody = MacroPair.second;
608 
609     // For an #undef'd macro, we only care about the name.
610     if (IsUndef) {
611       if (MacroNames && !Macros.count(MacroName))
612         MacroNames->push_back(MacroName);
613 
614       Macros[MacroName] = std::make_pair("", true);
615       continue;
616     }
617 
618     // For a #define'd macro, figure out the actual definition.
619     if (MacroName.size() == Macro.size())
620       MacroBody = "1";
621     else {
622       // Note: GCC drops anything following an end-of-line character.
623       StringRef::size_type End = MacroBody.find_first_of("\n\r");
624       MacroBody = MacroBody.substr(0, End);
625     }
626 
627     if (MacroNames && !Macros.count(MacroName))
628       MacroNames->push_back(MacroName);
629     Macros[MacroName] = std::make_pair(MacroBody, false);
630   }
631 }
632 
633 /// Check the preprocessor options deserialized from the control block
634 /// against the preprocessor options in an existing preprocessor.
635 ///
636 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
637 /// \param Validate If true, validate preprocessor options. If false, allow
638 ///        macros defined by \p ExistingPPOpts to override those defined by
639 ///        \p PPOpts in SuggestedPredefines.
640 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
641                                      const PreprocessorOptions &ExistingPPOpts,
642                                      DiagnosticsEngine *Diags,
643                                      FileManager &FileMgr,
644                                      std::string &SuggestedPredefines,
645                                      const LangOptions &LangOpts,
646                                      bool Validate = true) {
647   // Check macro definitions.
648   MacroDefinitionsMap ASTFileMacros;
649   collectMacroDefinitions(PPOpts, ASTFileMacros);
650   MacroDefinitionsMap ExistingMacros;
651   SmallVector<StringRef, 4> ExistingMacroNames;
652   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
653 
654   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
655     // Dig out the macro definition in the existing preprocessor options.
656     StringRef MacroName = ExistingMacroNames[I];
657     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
658 
659     // Check whether we know anything about this macro name or not.
660     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
661         ASTFileMacros.find(MacroName);
662     if (!Validate || Known == ASTFileMacros.end()) {
663       // FIXME: Check whether this identifier was referenced anywhere in the
664       // AST file. If so, we should reject the AST file. Unfortunately, this
665       // information isn't in the control block. What shall we do about it?
666 
667       if (Existing.second) {
668         SuggestedPredefines += "#undef ";
669         SuggestedPredefines += MacroName.str();
670         SuggestedPredefines += '\n';
671       } else {
672         SuggestedPredefines += "#define ";
673         SuggestedPredefines += MacroName.str();
674         SuggestedPredefines += ' ';
675         SuggestedPredefines += Existing.first.str();
676         SuggestedPredefines += '\n';
677       }
678       continue;
679     }
680 
681     // If the macro was defined in one but undef'd in the other, we have a
682     // conflict.
683     if (Existing.second != Known->second.second) {
684       if (Diags) {
685         Diags->Report(diag::err_pch_macro_def_undef)
686           << MacroName << Known->second.second;
687       }
688       return true;
689     }
690 
691     // If the macro was #undef'd in both, or if the macro bodies are identical,
692     // it's fine.
693     if (Existing.second || Existing.first == Known->second.first)
694       continue;
695 
696     // The macro bodies differ; complain.
697     if (Diags) {
698       Diags->Report(diag::err_pch_macro_def_conflict)
699         << MacroName << Known->second.first << Existing.first;
700     }
701     return true;
702   }
703 
704   // Check whether we're using predefines.
705   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
706     if (Diags) {
707       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
708     }
709     return true;
710   }
711 
712   // Detailed record is important since it is used for the module cache hash.
713   if (LangOpts.Modules &&
714       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
715     if (Diags) {
716       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
717     }
718     return true;
719   }
720 
721   // Compute the #include and #include_macros lines we need.
722   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
723     StringRef File = ExistingPPOpts.Includes[I];
724 
725     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
726         !ExistingPPOpts.PCHThroughHeader.empty()) {
727       // In case the through header is an include, we must add all the includes
728       // to the predefines so the start point can be determined.
729       SuggestedPredefines += "#include \"";
730       SuggestedPredefines += File;
731       SuggestedPredefines += "\"\n";
732       continue;
733     }
734 
735     if (File == ExistingPPOpts.ImplicitPCHInclude)
736       continue;
737 
738     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
739           != PPOpts.Includes.end())
740       continue;
741 
742     SuggestedPredefines += "#include \"";
743     SuggestedPredefines += File;
744     SuggestedPredefines += "\"\n";
745   }
746 
747   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
748     StringRef File = ExistingPPOpts.MacroIncludes[I];
749     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
750                   File)
751         != PPOpts.MacroIncludes.end())
752       continue;
753 
754     SuggestedPredefines += "#__include_macros \"";
755     SuggestedPredefines += File;
756     SuggestedPredefines += "\"\n##\n";
757   }
758 
759   return false;
760 }
761 
762 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
763                                            bool Complain,
764                                            std::string &SuggestedPredefines) {
765   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
766 
767   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
768                                   Complain? &Reader.Diags : nullptr,
769                                   PP.getFileManager(),
770                                   SuggestedPredefines,
771                                   PP.getLangOpts());
772 }
773 
774 bool SimpleASTReaderListener::ReadPreprocessorOptions(
775                                   const PreprocessorOptions &PPOpts,
776                                   bool Complain,
777                                   std::string &SuggestedPredefines) {
778   return checkPreprocessorOptions(PPOpts,
779                                   PP.getPreprocessorOpts(),
780                                   nullptr,
781                                   PP.getFileManager(),
782                                   SuggestedPredefines,
783                                   PP.getLangOpts(),
784                                   false);
785 }
786 
787 /// Check the header search options deserialized from the control block
788 /// against the header search options in an existing preprocessor.
789 ///
790 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
791 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792                                      StringRef SpecificModuleCachePath,
793                                      StringRef ExistingModuleCachePath,
794                                      DiagnosticsEngine *Diags,
795                                      const LangOptions &LangOpts) {
796   if (LangOpts.Modules) {
797     if (SpecificModuleCachePath != ExistingModuleCachePath) {
798       if (Diags)
799         Diags->Report(diag::err_pch_modulecache_mismatch)
800           << SpecificModuleCachePath << ExistingModuleCachePath;
801       return true;
802     }
803   }
804 
805   return false;
806 }
807 
808 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
809                                            StringRef SpecificModuleCachePath,
810                                            bool Complain) {
811   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
812                                   PP.getHeaderSearchInfo().getModuleCachePath(),
813                                   Complain ? &Reader.Diags : nullptr,
814                                   PP.getLangOpts());
815 }
816 
817 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
818   PP.setCounterValue(Value);
819 }
820 
821 //===----------------------------------------------------------------------===//
822 // AST reader implementation
823 //===----------------------------------------------------------------------===//
824 
825 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
826                                            bool TakeOwnership) {
827   DeserializationListener = Listener;
828   OwnsDeserializationListener = TakeOwnership;
829 }
830 
831 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
832   return serialization::ComputeHash(Sel);
833 }
834 
835 std::pair<unsigned, unsigned>
836 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
837   using namespace llvm::support;
838 
839   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
840   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
841   return std::make_pair(KeyLen, DataLen);
842 }
843 
844 ASTSelectorLookupTrait::internal_key_type
845 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
846   using namespace llvm::support;
847 
848   SelectorTable &SelTable = Reader.getContext().Selectors;
849   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
850   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
851       F, endian::readNext<uint32_t, little, unaligned>(d));
852   if (N == 0)
853     return SelTable.getNullarySelector(FirstII);
854   else if (N == 1)
855     return SelTable.getUnarySelector(FirstII);
856 
857   SmallVector<IdentifierInfo *, 16> Args;
858   Args.push_back(FirstII);
859   for (unsigned I = 1; I != N; ++I)
860     Args.push_back(Reader.getLocalIdentifier(
861         F, endian::readNext<uint32_t, little, unaligned>(d)));
862 
863   return SelTable.getSelector(N, Args.data());
864 }
865 
866 ASTSelectorLookupTrait::data_type
867 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
868                                  unsigned DataLen) {
869   using namespace llvm::support;
870 
871   data_type Result;
872 
873   Result.ID = Reader.getGlobalSelectorID(
874       F, endian::readNext<uint32_t, little, unaligned>(d));
875   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
876   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
877   Result.InstanceBits = FullInstanceBits & 0x3;
878   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
879   Result.FactoryBits = FullFactoryBits & 0x3;
880   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
881   unsigned NumInstanceMethods = FullInstanceBits >> 3;
882   unsigned NumFactoryMethods = FullFactoryBits >> 3;
883 
884   // Load instance methods
885   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
886     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
887             F, endian::readNext<uint32_t, little, unaligned>(d)))
888       Result.Instance.push_back(Method);
889   }
890 
891   // Load factory methods
892   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
893     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
894             F, endian::readNext<uint32_t, little, unaligned>(d)))
895       Result.Factory.push_back(Method);
896   }
897 
898   return Result;
899 }
900 
901 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
902   return llvm::djbHash(a);
903 }
904 
905 std::pair<unsigned, unsigned>
906 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
907   using namespace llvm::support;
908 
909   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
910   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
911   return std::make_pair(KeyLen, DataLen);
912 }
913 
914 ASTIdentifierLookupTraitBase::internal_key_type
915 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
916   assert(n >= 2 && d[n-1] == '\0');
917   return StringRef((const char*) d, n-1);
918 }
919 
920 /// Whether the given identifier is "interesting".
921 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
922                                     bool IsModule) {
923   return II.hadMacroDefinition() || II.isPoisoned() ||
924          (!IsModule && II.getObjCOrBuiltinID()) ||
925          II.hasRevertedTokenIDToIdentifier() ||
926          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
927           II.getFETokenInfo());
928 }
929 
930 static bool readBit(unsigned &Bits) {
931   bool Value = Bits & 0x1;
932   Bits >>= 1;
933   return Value;
934 }
935 
936 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
937   using namespace llvm::support;
938 
939   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
940   return Reader.getGlobalIdentifierID(F, RawID >> 1);
941 }
942 
943 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
944   if (!II.isFromAST()) {
945     II.setIsFromAST();
946     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
947     if (isInterestingIdentifier(Reader, II, IsModule))
948       II.setChangedSinceDeserialization();
949   }
950 }
951 
952 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
953                                                    const unsigned char* d,
954                                                    unsigned DataLen) {
955   using namespace llvm::support;
956 
957   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
958   bool IsInteresting = RawID & 0x01;
959 
960   // Wipe out the "is interesting" bit.
961   RawID = RawID >> 1;
962 
963   // Build the IdentifierInfo and link the identifier ID with it.
964   IdentifierInfo *II = KnownII;
965   if (!II) {
966     II = &Reader.getIdentifierTable().getOwn(k);
967     KnownII = II;
968   }
969   markIdentifierFromAST(Reader, *II);
970   Reader.markIdentifierUpToDate(II);
971 
972   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
973   if (!IsInteresting) {
974     // For uninteresting identifiers, there's nothing else to do. Just notify
975     // the reader that we've finished loading this identifier.
976     Reader.SetIdentifierInfo(ID, II);
977     return II;
978   }
979 
980   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
981   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
982   bool CPlusPlusOperatorKeyword = readBit(Bits);
983   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
984   bool Poisoned = readBit(Bits);
985   bool ExtensionToken = readBit(Bits);
986   bool HadMacroDefinition = readBit(Bits);
987 
988   assert(Bits == 0 && "Extra bits in the identifier?");
989   DataLen -= 8;
990 
991   // Set or check the various bits in the IdentifierInfo structure.
992   // Token IDs are read-only.
993   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
994     II->revertTokenIDToIdentifier();
995   if (!F.isModule())
996     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
997   assert(II->isExtensionToken() == ExtensionToken &&
998          "Incorrect extension token flag");
999   (void)ExtensionToken;
1000   if (Poisoned)
1001     II->setIsPoisoned(true);
1002   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1003          "Incorrect C++ operator keyword flag");
1004   (void)CPlusPlusOperatorKeyword;
1005 
1006   // If this identifier is a macro, deserialize the macro
1007   // definition.
1008   if (HadMacroDefinition) {
1009     uint32_t MacroDirectivesOffset =
1010         endian::readNext<uint32_t, little, unaligned>(d);
1011     DataLen -= 4;
1012 
1013     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1014   }
1015 
1016   Reader.SetIdentifierInfo(ID, II);
1017 
1018   // Read all of the declarations visible at global scope with this
1019   // name.
1020   if (DataLen > 0) {
1021     SmallVector<uint32_t, 4> DeclIDs;
1022     for (; DataLen > 0; DataLen -= 4)
1023       DeclIDs.push_back(Reader.getGlobalDeclID(
1024           F, endian::readNext<uint32_t, little, unaligned>(d)));
1025     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1026   }
1027 
1028   return II;
1029 }
1030 
1031 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1032     : Kind(Name.getNameKind()) {
1033   switch (Kind) {
1034   case DeclarationName::Identifier:
1035     Data = (uint64_t)Name.getAsIdentifierInfo();
1036     break;
1037   case DeclarationName::ObjCZeroArgSelector:
1038   case DeclarationName::ObjCOneArgSelector:
1039   case DeclarationName::ObjCMultiArgSelector:
1040     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1041     break;
1042   case DeclarationName::CXXOperatorName:
1043     Data = Name.getCXXOverloadedOperator();
1044     break;
1045   case DeclarationName::CXXLiteralOperatorName:
1046     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1047     break;
1048   case DeclarationName::CXXDeductionGuideName:
1049     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1050                ->getDeclName().getAsIdentifierInfo();
1051     break;
1052   case DeclarationName::CXXConstructorName:
1053   case DeclarationName::CXXDestructorName:
1054   case DeclarationName::CXXConversionFunctionName:
1055   case DeclarationName::CXXUsingDirective:
1056     Data = 0;
1057     break;
1058   }
1059 }
1060 
1061 unsigned DeclarationNameKey::getHash() const {
1062   llvm::FoldingSetNodeID ID;
1063   ID.AddInteger(Kind);
1064 
1065   switch (Kind) {
1066   case DeclarationName::Identifier:
1067   case DeclarationName::CXXLiteralOperatorName:
1068   case DeclarationName::CXXDeductionGuideName:
1069     ID.AddString(((IdentifierInfo*)Data)->getName());
1070     break;
1071   case DeclarationName::ObjCZeroArgSelector:
1072   case DeclarationName::ObjCOneArgSelector:
1073   case DeclarationName::ObjCMultiArgSelector:
1074     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1075     break;
1076   case DeclarationName::CXXOperatorName:
1077     ID.AddInteger((OverloadedOperatorKind)Data);
1078     break;
1079   case DeclarationName::CXXConstructorName:
1080   case DeclarationName::CXXDestructorName:
1081   case DeclarationName::CXXConversionFunctionName:
1082   case DeclarationName::CXXUsingDirective:
1083     break;
1084   }
1085 
1086   return ID.ComputeHash();
1087 }
1088 
1089 ModuleFile *
1090 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1091   using namespace llvm::support;
1092 
1093   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1094   return Reader.getLocalModuleFile(F, ModuleFileID);
1095 }
1096 
1097 std::pair<unsigned, unsigned>
1098 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1099   using namespace llvm::support;
1100 
1101   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1102   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1103   return std::make_pair(KeyLen, DataLen);
1104 }
1105 
1106 ASTDeclContextNameLookupTrait::internal_key_type
1107 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1108   using namespace llvm::support;
1109 
1110   auto Kind = (DeclarationName::NameKind)*d++;
1111   uint64_t Data;
1112   switch (Kind) {
1113   case DeclarationName::Identifier:
1114   case DeclarationName::CXXLiteralOperatorName:
1115   case DeclarationName::CXXDeductionGuideName:
1116     Data = (uint64_t)Reader.getLocalIdentifier(
1117         F, endian::readNext<uint32_t, little, unaligned>(d));
1118     break;
1119   case DeclarationName::ObjCZeroArgSelector:
1120   case DeclarationName::ObjCOneArgSelector:
1121   case DeclarationName::ObjCMultiArgSelector:
1122     Data =
1123         (uint64_t)Reader.getLocalSelector(
1124                              F, endian::readNext<uint32_t, little, unaligned>(
1125                                     d)).getAsOpaquePtr();
1126     break;
1127   case DeclarationName::CXXOperatorName:
1128     Data = *d++; // OverloadedOperatorKind
1129     break;
1130   case DeclarationName::CXXConstructorName:
1131   case DeclarationName::CXXDestructorName:
1132   case DeclarationName::CXXConversionFunctionName:
1133   case DeclarationName::CXXUsingDirective:
1134     Data = 0;
1135     break;
1136   }
1137 
1138   return DeclarationNameKey(Kind, Data);
1139 }
1140 
1141 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1142                                                  const unsigned char *d,
1143                                                  unsigned DataLen,
1144                                                  data_type_builder &Val) {
1145   using namespace llvm::support;
1146 
1147   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1148     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1149     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1150   }
1151 }
1152 
1153 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1154                                               BitstreamCursor &Cursor,
1155                                               uint64_t Offset,
1156                                               DeclContext *DC) {
1157   assert(Offset != 0);
1158 
1159   SavedStreamPosition SavedPosition(Cursor);
1160   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1161     Error(std::move(Err));
1162     return true;
1163   }
1164 
1165   RecordData Record;
1166   StringRef Blob;
1167   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1168   if (!MaybeCode) {
1169     Error(MaybeCode.takeError());
1170     return true;
1171   }
1172   unsigned Code = MaybeCode.get();
1173 
1174   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1175   if (!MaybeRecCode) {
1176     Error(MaybeRecCode.takeError());
1177     return true;
1178   }
1179   unsigned RecCode = MaybeRecCode.get();
1180   if (RecCode != DECL_CONTEXT_LEXICAL) {
1181     Error("Expected lexical block");
1182     return true;
1183   }
1184 
1185   assert(!isa<TranslationUnitDecl>(DC) &&
1186          "expected a TU_UPDATE_LEXICAL record for TU");
1187   // If we are handling a C++ class template instantiation, we can see multiple
1188   // lexical updates for the same record. It's important that we select only one
1189   // of them, so that field numbering works properly. Just pick the first one we
1190   // see.
1191   auto &Lex = LexicalDecls[DC];
1192   if (!Lex.first) {
1193     Lex = std::make_pair(
1194         &M, llvm::makeArrayRef(
1195                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1196                     Blob.data()),
1197                 Blob.size() / 4));
1198   }
1199   DC->setHasExternalLexicalStorage(true);
1200   return false;
1201 }
1202 
1203 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1204                                               BitstreamCursor &Cursor,
1205                                               uint64_t Offset,
1206                                               DeclID ID) {
1207   assert(Offset != 0);
1208 
1209   SavedStreamPosition SavedPosition(Cursor);
1210   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1211     Error(std::move(Err));
1212     return true;
1213   }
1214 
1215   RecordData Record;
1216   StringRef Blob;
1217   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1218   if (!MaybeCode) {
1219     Error(MaybeCode.takeError());
1220     return true;
1221   }
1222   unsigned Code = MaybeCode.get();
1223 
1224   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1225   if (!MaybeRecCode) {
1226     Error(MaybeRecCode.takeError());
1227     return true;
1228   }
1229   unsigned RecCode = MaybeRecCode.get();
1230   if (RecCode != DECL_CONTEXT_VISIBLE) {
1231     Error("Expected visible lookup table block");
1232     return true;
1233   }
1234 
1235   // We can't safely determine the primary context yet, so delay attaching the
1236   // lookup table until we're done with recursive deserialization.
1237   auto *Data = (const unsigned char*)Blob.data();
1238   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1239   return false;
1240 }
1241 
1242 void ASTReader::Error(StringRef Msg) const {
1243   Error(diag::err_fe_pch_malformed, Msg);
1244   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1245       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1246     Diag(diag::note_module_cache_path)
1247       << PP.getHeaderSearchInfo().getModuleCachePath();
1248   }
1249 }
1250 
1251 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1252                       StringRef Arg3) const {
1253   if (Diags.isDiagnosticInFlight())
1254     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1255   else
1256     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1257 }
1258 
1259 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1260                       unsigned Select) const {
1261   if (!Diags.isDiagnosticInFlight())
1262     Diag(DiagID) << Arg1 << Arg2 << Select;
1263 }
1264 
1265 void ASTReader::Error(llvm::Error &&Err) const {
1266   Error(toString(std::move(Err)));
1267 }
1268 
1269 //===----------------------------------------------------------------------===//
1270 // Source Manager Deserialization
1271 //===----------------------------------------------------------------------===//
1272 
1273 /// Read the line table in the source manager block.
1274 /// \returns true if there was an error.
1275 bool ASTReader::ParseLineTable(ModuleFile &F,
1276                                const RecordData &Record) {
1277   unsigned Idx = 0;
1278   LineTableInfo &LineTable = SourceMgr.getLineTable();
1279 
1280   // Parse the file names
1281   std::map<int, int> FileIDs;
1282   FileIDs[-1] = -1; // For unspecified filenames.
1283   for (unsigned I = 0; Record[Idx]; ++I) {
1284     // Extract the file name
1285     auto Filename = ReadPath(F, Record, Idx);
1286     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1287   }
1288   ++Idx;
1289 
1290   // Parse the line entries
1291   std::vector<LineEntry> Entries;
1292   while (Idx < Record.size()) {
1293     int FID = Record[Idx++];
1294     assert(FID >= 0 && "Serialized line entries for non-local file.");
1295     // Remap FileID from 1-based old view.
1296     FID += F.SLocEntryBaseID - 1;
1297 
1298     // Extract the line entries
1299     unsigned NumEntries = Record[Idx++];
1300     assert(NumEntries && "no line entries for file ID");
1301     Entries.clear();
1302     Entries.reserve(NumEntries);
1303     for (unsigned I = 0; I != NumEntries; ++I) {
1304       unsigned FileOffset = Record[Idx++];
1305       unsigned LineNo = Record[Idx++];
1306       int FilenameID = FileIDs[Record[Idx++]];
1307       SrcMgr::CharacteristicKind FileKind
1308         = (SrcMgr::CharacteristicKind)Record[Idx++];
1309       unsigned IncludeOffset = Record[Idx++];
1310       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1311                                        FileKind, IncludeOffset));
1312     }
1313     LineTable.AddEntry(FileID::get(FID), Entries);
1314   }
1315 
1316   return false;
1317 }
1318 
1319 /// Read a source manager block
1320 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1321   using namespace SrcMgr;
1322 
1323   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1324 
1325   // Set the source-location entry cursor to the current position in
1326   // the stream. This cursor will be used to read the contents of the
1327   // source manager block initially, and then lazily read
1328   // source-location entries as needed.
1329   SLocEntryCursor = F.Stream;
1330 
1331   // The stream itself is going to skip over the source manager block.
1332   if (llvm::Error Err = F.Stream.SkipBlock()) {
1333     Error(std::move(Err));
1334     return true;
1335   }
1336 
1337   // Enter the source manager block.
1338   if (llvm::Error Err =
1339           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1340     Error(std::move(Err));
1341     return true;
1342   }
1343   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1344 
1345   RecordData Record;
1346   while (true) {
1347     Expected<llvm::BitstreamEntry> MaybeE =
1348         SLocEntryCursor.advanceSkippingSubblocks();
1349     if (!MaybeE) {
1350       Error(MaybeE.takeError());
1351       return true;
1352     }
1353     llvm::BitstreamEntry E = MaybeE.get();
1354 
1355     switch (E.Kind) {
1356     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1357     case llvm::BitstreamEntry::Error:
1358       Error("malformed block record in AST file");
1359       return true;
1360     case llvm::BitstreamEntry::EndBlock:
1361       return false;
1362     case llvm::BitstreamEntry::Record:
1363       // The interesting case.
1364       break;
1365     }
1366 
1367     // Read a record.
1368     Record.clear();
1369     StringRef Blob;
1370     Expected<unsigned> MaybeRecord =
1371         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1372     if (!MaybeRecord) {
1373       Error(MaybeRecord.takeError());
1374       return true;
1375     }
1376     switch (MaybeRecord.get()) {
1377     default:  // Default behavior: ignore.
1378       break;
1379 
1380     case SM_SLOC_FILE_ENTRY:
1381     case SM_SLOC_BUFFER_ENTRY:
1382     case SM_SLOC_EXPANSION_ENTRY:
1383       // Once we hit one of the source location entries, we're done.
1384       return false;
1385     }
1386   }
1387 }
1388 
1389 /// If a header file is not found at the path that we expect it to be
1390 /// and the PCH file was moved from its original location, try to resolve the
1391 /// file by assuming that header+PCH were moved together and the header is in
1392 /// the same place relative to the PCH.
1393 static std::string
1394 resolveFileRelativeToOriginalDir(const std::string &Filename,
1395                                  const std::string &OriginalDir,
1396                                  const std::string &CurrDir) {
1397   assert(OriginalDir != CurrDir &&
1398          "No point trying to resolve the file if the PCH dir didn't change");
1399 
1400   using namespace llvm::sys;
1401 
1402   SmallString<128> filePath(Filename);
1403   fs::make_absolute(filePath);
1404   assert(path::is_absolute(OriginalDir));
1405   SmallString<128> currPCHPath(CurrDir);
1406 
1407   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1408                        fileDirE = path::end(path::parent_path(filePath));
1409   path::const_iterator origDirI = path::begin(OriginalDir),
1410                        origDirE = path::end(OriginalDir);
1411   // Skip the common path components from filePath and OriginalDir.
1412   while (fileDirI != fileDirE && origDirI != origDirE &&
1413          *fileDirI == *origDirI) {
1414     ++fileDirI;
1415     ++origDirI;
1416   }
1417   for (; origDirI != origDirE; ++origDirI)
1418     path::append(currPCHPath, "..");
1419   path::append(currPCHPath, fileDirI, fileDirE);
1420   path::append(currPCHPath, path::filename(Filename));
1421   return std::string(currPCHPath.str());
1422 }
1423 
1424 bool ASTReader::ReadSLocEntry(int ID) {
1425   if (ID == 0)
1426     return false;
1427 
1428   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1429     Error("source location entry ID out-of-range for AST file");
1430     return true;
1431   }
1432 
1433   // Local helper to read the (possibly-compressed) buffer data following the
1434   // entry record.
1435   auto ReadBuffer = [this](
1436       BitstreamCursor &SLocEntryCursor,
1437       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1438     RecordData Record;
1439     StringRef Blob;
1440     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1441     if (!MaybeCode) {
1442       Error(MaybeCode.takeError());
1443       return nullptr;
1444     }
1445     unsigned Code = MaybeCode.get();
1446 
1447     Expected<unsigned> MaybeRecCode =
1448         SLocEntryCursor.readRecord(Code, Record, &Blob);
1449     if (!MaybeRecCode) {
1450       Error(MaybeRecCode.takeError());
1451       return nullptr;
1452     }
1453     unsigned RecCode = MaybeRecCode.get();
1454 
1455     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1456       if (!llvm::zlib::isAvailable()) {
1457         Error("zlib is not available");
1458         return nullptr;
1459       }
1460       SmallString<0> Uncompressed;
1461       if (llvm::Error E =
1462               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1463         Error("could not decompress embedded file contents: " +
1464               llvm::toString(std::move(E)));
1465         return nullptr;
1466       }
1467       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1468     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1469       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1470     } else {
1471       Error("AST record has invalid code");
1472       return nullptr;
1473     }
1474   };
1475 
1476   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1477   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1478           F->SLocEntryOffsetsBase +
1479           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1480     Error(std::move(Err));
1481     return true;
1482   }
1483 
1484   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1485   unsigned BaseOffset = F->SLocEntryBaseOffset;
1486 
1487   ++NumSLocEntriesRead;
1488   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1489   if (!MaybeEntry) {
1490     Error(MaybeEntry.takeError());
1491     return true;
1492   }
1493   llvm::BitstreamEntry Entry = MaybeEntry.get();
1494 
1495   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1496     Error("incorrectly-formatted source location entry in AST file");
1497     return true;
1498   }
1499 
1500   RecordData Record;
1501   StringRef Blob;
1502   Expected<unsigned> MaybeSLOC =
1503       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1504   if (!MaybeSLOC) {
1505     Error(MaybeSLOC.takeError());
1506     return true;
1507   }
1508   switch (MaybeSLOC.get()) {
1509   default:
1510     Error("incorrectly-formatted source location entry in AST file");
1511     return true;
1512 
1513   case SM_SLOC_FILE_ENTRY: {
1514     // We will detect whether a file changed and return 'Failure' for it, but
1515     // we will also try to fail gracefully by setting up the SLocEntry.
1516     unsigned InputID = Record[4];
1517     InputFile IF = getInputFile(*F, InputID);
1518     const FileEntry *File = IF.getFile();
1519     bool OverriddenBuffer = IF.isOverridden();
1520 
1521     // Note that we only check if a File was returned. If it was out-of-date
1522     // we have complained but we will continue creating a FileID to recover
1523     // gracefully.
1524     if (!File)
1525       return true;
1526 
1527     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1528     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1529       // This is the module's main file.
1530       IncludeLoc = getImportLocation(F);
1531     }
1532     SrcMgr::CharacteristicKind
1533       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1534     // FIXME: The FileID should be created from the FileEntryRef.
1535     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1536                                         ID, BaseOffset + Record[0]);
1537     SrcMgr::FileInfo &FileInfo =
1538           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1539     FileInfo.NumCreatedFIDs = Record[5];
1540     if (Record[3])
1541       FileInfo.setHasLineDirectives();
1542 
1543     unsigned NumFileDecls = Record[7];
1544     if (NumFileDecls && ContextObj) {
1545       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1546       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1547       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1548                                                              NumFileDecls));
1549     }
1550 
1551     const SrcMgr::ContentCache *ContentCache
1552       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1553     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1554         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1555         !ContentCache->getRawBuffer()) {
1556       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1557       if (!Buffer)
1558         return true;
1559       SourceMgr.overrideFileContents(File, std::move(Buffer));
1560     }
1561 
1562     break;
1563   }
1564 
1565   case SM_SLOC_BUFFER_ENTRY: {
1566     const char *Name = Blob.data();
1567     unsigned Offset = Record[0];
1568     SrcMgr::CharacteristicKind
1569       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1570     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1571     if (IncludeLoc.isInvalid() && F->isModule()) {
1572       IncludeLoc = getImportLocation(F);
1573     }
1574 
1575     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1576     if (!Buffer)
1577       return true;
1578     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1579                            BaseOffset + Offset, IncludeLoc);
1580     break;
1581   }
1582 
1583   case SM_SLOC_EXPANSION_ENTRY: {
1584     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1585     SourceMgr.createExpansionLoc(SpellingLoc,
1586                                      ReadSourceLocation(*F, Record[2]),
1587                                      ReadSourceLocation(*F, Record[3]),
1588                                      Record[5],
1589                                      Record[4],
1590                                      ID,
1591                                      BaseOffset + Record[0]);
1592     break;
1593   }
1594   }
1595 
1596   return false;
1597 }
1598 
1599 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1600   if (ID == 0)
1601     return std::make_pair(SourceLocation(), "");
1602 
1603   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1604     Error("source location entry ID out-of-range for AST file");
1605     return std::make_pair(SourceLocation(), "");
1606   }
1607 
1608   // Find which module file this entry lands in.
1609   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1610   if (!M->isModule())
1611     return std::make_pair(SourceLocation(), "");
1612 
1613   // FIXME: Can we map this down to a particular submodule? That would be
1614   // ideal.
1615   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1616 }
1617 
1618 /// Find the location where the module F is imported.
1619 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1620   if (F->ImportLoc.isValid())
1621     return F->ImportLoc;
1622 
1623   // Otherwise we have a PCH. It's considered to be "imported" at the first
1624   // location of its includer.
1625   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1626     // Main file is the importer.
1627     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1628     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1629   }
1630   return F->ImportedBy[0]->FirstLoc;
1631 }
1632 
1633 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1634 /// the abbreviations that are at the top of the block and then leave the cursor
1635 /// pointing into the block.
1636 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1637                                  uint64_t *StartOfBlockOffset) {
1638   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1639     // FIXME this drops errors on the floor.
1640     consumeError(std::move(Err));
1641     return true;
1642   }
1643 
1644   if (StartOfBlockOffset)
1645     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1646 
1647   while (true) {
1648     uint64_t Offset = Cursor.GetCurrentBitNo();
1649     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1650     if (!MaybeCode) {
1651       // FIXME this drops errors on the floor.
1652       consumeError(MaybeCode.takeError());
1653       return true;
1654     }
1655     unsigned Code = MaybeCode.get();
1656 
1657     // We expect all abbrevs to be at the start of the block.
1658     if (Code != llvm::bitc::DEFINE_ABBREV) {
1659       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1660         // FIXME this drops errors on the floor.
1661         consumeError(std::move(Err));
1662         return true;
1663       }
1664       return false;
1665     }
1666     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1667       // FIXME this drops errors on the floor.
1668       consumeError(std::move(Err));
1669       return true;
1670     }
1671   }
1672 }
1673 
1674 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1675                            unsigned &Idx) {
1676   Token Tok;
1677   Tok.startToken();
1678   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1679   Tok.setLength(Record[Idx++]);
1680   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1681     Tok.setIdentifierInfo(II);
1682   Tok.setKind((tok::TokenKind)Record[Idx++]);
1683   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1684   return Tok;
1685 }
1686 
1687 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1688   BitstreamCursor &Stream = F.MacroCursor;
1689 
1690   // Keep track of where we are in the stream, then jump back there
1691   // after reading this macro.
1692   SavedStreamPosition SavedPosition(Stream);
1693 
1694   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1695     // FIXME this drops errors on the floor.
1696     consumeError(std::move(Err));
1697     return nullptr;
1698   }
1699   RecordData Record;
1700   SmallVector<IdentifierInfo*, 16> MacroParams;
1701   MacroInfo *Macro = nullptr;
1702 
1703   while (true) {
1704     // Advance to the next record, but if we get to the end of the block, don't
1705     // pop it (removing all the abbreviations from the cursor) since we want to
1706     // be able to reseek within the block and read entries.
1707     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1708     Expected<llvm::BitstreamEntry> MaybeEntry =
1709         Stream.advanceSkippingSubblocks(Flags);
1710     if (!MaybeEntry) {
1711       Error(MaybeEntry.takeError());
1712       return Macro;
1713     }
1714     llvm::BitstreamEntry Entry = MaybeEntry.get();
1715 
1716     switch (Entry.Kind) {
1717     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1718     case llvm::BitstreamEntry::Error:
1719       Error("malformed block record in AST file");
1720       return Macro;
1721     case llvm::BitstreamEntry::EndBlock:
1722       return Macro;
1723     case llvm::BitstreamEntry::Record:
1724       // The interesting case.
1725       break;
1726     }
1727 
1728     // Read a record.
1729     Record.clear();
1730     PreprocessorRecordTypes RecType;
1731     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1732       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1733     else {
1734       Error(MaybeRecType.takeError());
1735       return Macro;
1736     }
1737     switch (RecType) {
1738     case PP_MODULE_MACRO:
1739     case PP_MACRO_DIRECTIVE_HISTORY:
1740       return Macro;
1741 
1742     case PP_MACRO_OBJECT_LIKE:
1743     case PP_MACRO_FUNCTION_LIKE: {
1744       // If we already have a macro, that means that we've hit the end
1745       // of the definition of the macro we were looking for. We're
1746       // done.
1747       if (Macro)
1748         return Macro;
1749 
1750       unsigned NextIndex = 1; // Skip identifier ID.
1751       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1752       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1753       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1754       MI->setIsUsed(Record[NextIndex++]);
1755       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1756 
1757       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1758         // Decode function-like macro info.
1759         bool isC99VarArgs = Record[NextIndex++];
1760         bool isGNUVarArgs = Record[NextIndex++];
1761         bool hasCommaPasting = Record[NextIndex++];
1762         MacroParams.clear();
1763         unsigned NumArgs = Record[NextIndex++];
1764         for (unsigned i = 0; i != NumArgs; ++i)
1765           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1766 
1767         // Install function-like macro info.
1768         MI->setIsFunctionLike();
1769         if (isC99VarArgs) MI->setIsC99Varargs();
1770         if (isGNUVarArgs) MI->setIsGNUVarargs();
1771         if (hasCommaPasting) MI->setHasCommaPasting();
1772         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1773       }
1774 
1775       // Remember that we saw this macro last so that we add the tokens that
1776       // form its body to it.
1777       Macro = MI;
1778 
1779       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1780           Record[NextIndex]) {
1781         // We have a macro definition. Register the association
1782         PreprocessedEntityID
1783             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1784         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1785         PreprocessingRecord::PPEntityID PPID =
1786             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1787         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1788             PPRec.getPreprocessedEntity(PPID));
1789         if (PPDef)
1790           PPRec.RegisterMacroDefinition(Macro, PPDef);
1791       }
1792 
1793       ++NumMacrosRead;
1794       break;
1795     }
1796 
1797     case PP_TOKEN: {
1798       // If we see a TOKEN before a PP_MACRO_*, then the file is
1799       // erroneous, just pretend we didn't see this.
1800       if (!Macro) break;
1801 
1802       unsigned Idx = 0;
1803       Token Tok = ReadToken(F, Record, Idx);
1804       Macro->AddTokenToBody(Tok);
1805       break;
1806     }
1807     }
1808   }
1809 }
1810 
1811 PreprocessedEntityID
1812 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1813                                          unsigned LocalID) const {
1814   if (!M.ModuleOffsetMap.empty())
1815     ReadModuleOffsetMap(M);
1816 
1817   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1818     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1819   assert(I != M.PreprocessedEntityRemap.end()
1820          && "Invalid index into preprocessed entity index remap");
1821 
1822   return LocalID + I->second;
1823 }
1824 
1825 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1826   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1827 }
1828 
1829 HeaderFileInfoTrait::internal_key_type
1830 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1831   internal_key_type ikey = {FE->getSize(),
1832                             M.HasTimestamps ? FE->getModificationTime() : 0,
1833                             FE->getName(), /*Imported*/ false};
1834   return ikey;
1835 }
1836 
1837 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1838   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1839     return false;
1840 
1841   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1842     return true;
1843 
1844   // Determine whether the actual files are equivalent.
1845   FileManager &FileMgr = Reader.getFileManager();
1846   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1847     if (!Key.Imported) {
1848       if (auto File = FileMgr.getFile(Key.Filename))
1849         return *File;
1850       return nullptr;
1851     }
1852 
1853     std::string Resolved = std::string(Key.Filename);
1854     Reader.ResolveImportedPath(M, Resolved);
1855     if (auto File = FileMgr.getFile(Resolved))
1856       return *File;
1857     return nullptr;
1858   };
1859 
1860   const FileEntry *FEA = GetFile(a);
1861   const FileEntry *FEB = GetFile(b);
1862   return FEA && FEA == FEB;
1863 }
1864 
1865 std::pair<unsigned, unsigned>
1866 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1867   using namespace llvm::support;
1868 
1869   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1870   unsigned DataLen = (unsigned) *d++;
1871   return std::make_pair(KeyLen, DataLen);
1872 }
1873 
1874 HeaderFileInfoTrait::internal_key_type
1875 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1876   using namespace llvm::support;
1877 
1878   internal_key_type ikey;
1879   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1880   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1881   ikey.Filename = (const char *)d;
1882   ikey.Imported = true;
1883   return ikey;
1884 }
1885 
1886 HeaderFileInfoTrait::data_type
1887 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1888                               unsigned DataLen) {
1889   using namespace llvm::support;
1890 
1891   const unsigned char *End = d + DataLen;
1892   HeaderFileInfo HFI;
1893   unsigned Flags = *d++;
1894   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1895   HFI.isImport |= (Flags >> 5) & 0x01;
1896   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1897   HFI.DirInfo = (Flags >> 1) & 0x07;
1898   HFI.IndexHeaderMapHeader = Flags & 0x01;
1899   // FIXME: Find a better way to handle this. Maybe just store a
1900   // "has been included" flag?
1901   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1902                              HFI.NumIncludes);
1903   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1904       M, endian::readNext<uint32_t, little, unaligned>(d));
1905   if (unsigned FrameworkOffset =
1906           endian::readNext<uint32_t, little, unaligned>(d)) {
1907     // The framework offset is 1 greater than the actual offset,
1908     // since 0 is used as an indicator for "no framework name".
1909     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1910     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1911   }
1912 
1913   assert((End - d) % 4 == 0 &&
1914          "Wrong data length in HeaderFileInfo deserialization");
1915   while (d != End) {
1916     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1917     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1918     LocalSMID >>= 2;
1919 
1920     // This header is part of a module. Associate it with the module to enable
1921     // implicit module import.
1922     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1923     Module *Mod = Reader.getSubmodule(GlobalSMID);
1924     FileManager &FileMgr = Reader.getFileManager();
1925     ModuleMap &ModMap =
1926         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1927 
1928     std::string Filename = std::string(key.Filename);
1929     if (key.Imported)
1930       Reader.ResolveImportedPath(M, Filename);
1931     // FIXME: This is not always the right filename-as-written, but we're not
1932     // going to use this information to rebuild the module, so it doesn't make
1933     // a lot of difference.
1934     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1935     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1936     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1937   }
1938 
1939   // This HeaderFileInfo was externally loaded.
1940   HFI.External = true;
1941   HFI.IsValid = true;
1942   return HFI;
1943 }
1944 
1945 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1946                                 uint32_t MacroDirectivesOffset) {
1947   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1948   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1949 }
1950 
1951 void ASTReader::ReadDefinedMacros() {
1952   // Note that we are loading defined macros.
1953   Deserializing Macros(this);
1954 
1955   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1956     BitstreamCursor &MacroCursor = I.MacroCursor;
1957 
1958     // If there was no preprocessor block, skip this file.
1959     if (MacroCursor.getBitcodeBytes().empty())
1960       continue;
1961 
1962     BitstreamCursor Cursor = MacroCursor;
1963     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1964       Error(std::move(Err));
1965       return;
1966     }
1967 
1968     RecordData Record;
1969     while (true) {
1970       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1971       if (!MaybeE) {
1972         Error(MaybeE.takeError());
1973         return;
1974       }
1975       llvm::BitstreamEntry E = MaybeE.get();
1976 
1977       switch (E.Kind) {
1978       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1979       case llvm::BitstreamEntry::Error:
1980         Error("malformed block record in AST file");
1981         return;
1982       case llvm::BitstreamEntry::EndBlock:
1983         goto NextCursor;
1984 
1985       case llvm::BitstreamEntry::Record: {
1986         Record.clear();
1987         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1988         if (!MaybeRecord) {
1989           Error(MaybeRecord.takeError());
1990           return;
1991         }
1992         switch (MaybeRecord.get()) {
1993         default:  // Default behavior: ignore.
1994           break;
1995 
1996         case PP_MACRO_OBJECT_LIKE:
1997         case PP_MACRO_FUNCTION_LIKE: {
1998           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1999           if (II->isOutOfDate())
2000             updateOutOfDateIdentifier(*II);
2001           break;
2002         }
2003 
2004         case PP_TOKEN:
2005           // Ignore tokens.
2006           break;
2007         }
2008         break;
2009       }
2010       }
2011     }
2012     NextCursor:  ;
2013   }
2014 }
2015 
2016 namespace {
2017 
2018   /// Visitor class used to look up identifirs in an AST file.
2019   class IdentifierLookupVisitor {
2020     StringRef Name;
2021     unsigned NameHash;
2022     unsigned PriorGeneration;
2023     unsigned &NumIdentifierLookups;
2024     unsigned &NumIdentifierLookupHits;
2025     IdentifierInfo *Found = nullptr;
2026 
2027   public:
2028     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2029                             unsigned &NumIdentifierLookups,
2030                             unsigned &NumIdentifierLookupHits)
2031       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2032         PriorGeneration(PriorGeneration),
2033         NumIdentifierLookups(NumIdentifierLookups),
2034         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2035 
2036     bool operator()(ModuleFile &M) {
2037       // If we've already searched this module file, skip it now.
2038       if (M.Generation <= PriorGeneration)
2039         return true;
2040 
2041       ASTIdentifierLookupTable *IdTable
2042         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2043       if (!IdTable)
2044         return false;
2045 
2046       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2047                                      Found);
2048       ++NumIdentifierLookups;
2049       ASTIdentifierLookupTable::iterator Pos =
2050           IdTable->find_hashed(Name, NameHash, &Trait);
2051       if (Pos == IdTable->end())
2052         return false;
2053 
2054       // Dereferencing the iterator has the effect of building the
2055       // IdentifierInfo node and populating it with the various
2056       // declarations it needs.
2057       ++NumIdentifierLookupHits;
2058       Found = *Pos;
2059       return true;
2060     }
2061 
2062     // Retrieve the identifier info found within the module
2063     // files.
2064     IdentifierInfo *getIdentifierInfo() const { return Found; }
2065   };
2066 
2067 } // namespace
2068 
2069 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2070   // Note that we are loading an identifier.
2071   Deserializing AnIdentifier(this);
2072 
2073   unsigned PriorGeneration = 0;
2074   if (getContext().getLangOpts().Modules)
2075     PriorGeneration = IdentifierGeneration[&II];
2076 
2077   // If there is a global index, look there first to determine which modules
2078   // provably do not have any results for this identifier.
2079   GlobalModuleIndex::HitSet Hits;
2080   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2081   if (!loadGlobalIndex()) {
2082     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2083       HitsPtr = &Hits;
2084     }
2085   }
2086 
2087   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2088                                   NumIdentifierLookups,
2089                                   NumIdentifierLookupHits);
2090   ModuleMgr.visit(Visitor, HitsPtr);
2091   markIdentifierUpToDate(&II);
2092 }
2093 
2094 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2095   if (!II)
2096     return;
2097 
2098   II->setOutOfDate(false);
2099 
2100   // Update the generation for this identifier.
2101   if (getContext().getLangOpts().Modules)
2102     IdentifierGeneration[II] = getGeneration();
2103 }
2104 
2105 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2106                                     const PendingMacroInfo &PMInfo) {
2107   ModuleFile &M = *PMInfo.M;
2108 
2109   BitstreamCursor &Cursor = M.MacroCursor;
2110   SavedStreamPosition SavedPosition(Cursor);
2111   if (llvm::Error Err =
2112           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2113     Error(std::move(Err));
2114     return;
2115   }
2116 
2117   struct ModuleMacroRecord {
2118     SubmoduleID SubModID;
2119     MacroInfo *MI;
2120     SmallVector<SubmoduleID, 8> Overrides;
2121   };
2122   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2123 
2124   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2125   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2126   // macro histroy.
2127   RecordData Record;
2128   while (true) {
2129     Expected<llvm::BitstreamEntry> MaybeEntry =
2130         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2131     if (!MaybeEntry) {
2132       Error(MaybeEntry.takeError());
2133       return;
2134     }
2135     llvm::BitstreamEntry Entry = MaybeEntry.get();
2136 
2137     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2138       Error("malformed block record in AST file");
2139       return;
2140     }
2141 
2142     Record.clear();
2143     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2144     if (!MaybePP) {
2145       Error(MaybePP.takeError());
2146       return;
2147     }
2148     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2149     case PP_MACRO_DIRECTIVE_HISTORY:
2150       break;
2151 
2152     case PP_MODULE_MACRO: {
2153       ModuleMacros.push_back(ModuleMacroRecord());
2154       auto &Info = ModuleMacros.back();
2155       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2156       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2157       for (int I = 2, N = Record.size(); I != N; ++I)
2158         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2159       continue;
2160     }
2161 
2162     default:
2163       Error("malformed block record in AST file");
2164       return;
2165     }
2166 
2167     // We found the macro directive history; that's the last record
2168     // for this macro.
2169     break;
2170   }
2171 
2172   // Module macros are listed in reverse dependency order.
2173   {
2174     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2175     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2176     for (auto &MMR : ModuleMacros) {
2177       Overrides.clear();
2178       for (unsigned ModID : MMR.Overrides) {
2179         Module *Mod = getSubmodule(ModID);
2180         auto *Macro = PP.getModuleMacro(Mod, II);
2181         assert(Macro && "missing definition for overridden macro");
2182         Overrides.push_back(Macro);
2183       }
2184 
2185       bool Inserted = false;
2186       Module *Owner = getSubmodule(MMR.SubModID);
2187       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2188     }
2189   }
2190 
2191   // Don't read the directive history for a module; we don't have anywhere
2192   // to put it.
2193   if (M.isModule())
2194     return;
2195 
2196   // Deserialize the macro directives history in reverse source-order.
2197   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2198   unsigned Idx = 0, N = Record.size();
2199   while (Idx < N) {
2200     MacroDirective *MD = nullptr;
2201     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2202     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2203     switch (K) {
2204     case MacroDirective::MD_Define: {
2205       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2206       MD = PP.AllocateDefMacroDirective(MI, Loc);
2207       break;
2208     }
2209     case MacroDirective::MD_Undefine:
2210       MD = PP.AllocateUndefMacroDirective(Loc);
2211       break;
2212     case MacroDirective::MD_Visibility:
2213       bool isPublic = Record[Idx++];
2214       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2215       break;
2216     }
2217 
2218     if (!Latest)
2219       Latest = MD;
2220     if (Earliest)
2221       Earliest->setPrevious(MD);
2222     Earliest = MD;
2223   }
2224 
2225   if (Latest)
2226     PP.setLoadedMacroDirective(II, Earliest, Latest);
2227 }
2228 
2229 ASTReader::InputFileInfo
2230 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2231   // Go find this input file.
2232   BitstreamCursor &Cursor = F.InputFilesCursor;
2233   SavedStreamPosition SavedPosition(Cursor);
2234   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2235     // FIXME this drops errors on the floor.
2236     consumeError(std::move(Err));
2237   }
2238 
2239   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2240   if (!MaybeCode) {
2241     // FIXME this drops errors on the floor.
2242     consumeError(MaybeCode.takeError());
2243   }
2244   unsigned Code = MaybeCode.get();
2245   RecordData Record;
2246   StringRef Blob;
2247 
2248   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2249     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2250            "invalid record type for input file");
2251   else {
2252     // FIXME this drops errors on the floor.
2253     consumeError(Maybe.takeError());
2254   }
2255 
2256   assert(Record[0] == ID && "Bogus stored ID or offset");
2257   InputFileInfo R;
2258   R.StoredSize = static_cast<off_t>(Record[1]);
2259   R.StoredTime = static_cast<time_t>(Record[2]);
2260   R.Overridden = static_cast<bool>(Record[3]);
2261   R.Transient = static_cast<bool>(Record[4]);
2262   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2263   R.Filename = std::string(Blob);
2264   ResolveImportedPath(F, R.Filename);
2265 
2266   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2267   if (!MaybeEntry) // FIXME this drops errors on the floor.
2268     consumeError(MaybeEntry.takeError());
2269   llvm::BitstreamEntry Entry = MaybeEntry.get();
2270   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2271          "expected record type for input file hash");
2272 
2273   Record.clear();
2274   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2275     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2276            "invalid record type for input file hash");
2277   else {
2278     // FIXME this drops errors on the floor.
2279     consumeError(Maybe.takeError());
2280   }
2281   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2282                   static_cast<uint64_t>(Record[0]);
2283   return R;
2284 }
2285 
2286 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2287 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2288   // If this ID is bogus, just return an empty input file.
2289   if (ID == 0 || ID > F.InputFilesLoaded.size())
2290     return InputFile();
2291 
2292   // If we've already loaded this input file, return it.
2293   if (F.InputFilesLoaded[ID-1].getFile())
2294     return F.InputFilesLoaded[ID-1];
2295 
2296   if (F.InputFilesLoaded[ID-1].isNotFound())
2297     return InputFile();
2298 
2299   // Go find this input file.
2300   BitstreamCursor &Cursor = F.InputFilesCursor;
2301   SavedStreamPosition SavedPosition(Cursor);
2302   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2303     // FIXME this drops errors on the floor.
2304     consumeError(std::move(Err));
2305   }
2306 
2307   InputFileInfo FI = readInputFileInfo(F, ID);
2308   off_t StoredSize = FI.StoredSize;
2309   time_t StoredTime = FI.StoredTime;
2310   bool Overridden = FI.Overridden;
2311   bool Transient = FI.Transient;
2312   StringRef Filename = FI.Filename;
2313   uint64_t StoredContentHash = FI.ContentHash;
2314 
2315   const FileEntry *File = nullptr;
2316   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2317     File = *FE;
2318 
2319   // If we didn't find the file, resolve it relative to the
2320   // original directory from which this AST file was created.
2321   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2322       F.OriginalDir != F.BaseDirectory) {
2323     std::string Resolved = resolveFileRelativeToOriginalDir(
2324         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2325     if (!Resolved.empty())
2326       if (auto FE = FileMgr.getFile(Resolved))
2327         File = *FE;
2328   }
2329 
2330   // For an overridden file, create a virtual file with the stored
2331   // size/timestamp.
2332   if ((Overridden || Transient) && File == nullptr)
2333     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2334 
2335   if (File == nullptr) {
2336     if (Complain) {
2337       std::string ErrorStr = "could not find file '";
2338       ErrorStr += Filename;
2339       ErrorStr += "' referenced by AST file '";
2340       ErrorStr += F.FileName;
2341       ErrorStr += "'";
2342       Error(ErrorStr);
2343     }
2344     // Record that we didn't find the file.
2345     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2346     return InputFile();
2347   }
2348 
2349   // Check if there was a request to override the contents of the file
2350   // that was part of the precompiled header. Overriding such a file
2351   // can lead to problems when lexing using the source locations from the
2352   // PCH.
2353   SourceManager &SM = getSourceManager();
2354   // FIXME: Reject if the overrides are different.
2355   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2356     if (Complain)
2357       Error(diag::err_fe_pch_file_overridden, Filename);
2358 
2359     // After emitting the diagnostic, bypass the overriding file to recover
2360     // (this creates a separate FileEntry).
2361     File = SM.bypassFileContentsOverride(*File);
2362     if (!File) {
2363       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2364       return InputFile();
2365     }
2366   }
2367 
2368   enum ModificationType {
2369     Size,
2370     ModTime,
2371     Content,
2372     None,
2373   };
2374   auto HasInputFileChanged = [&]() {
2375     if (StoredSize != File->getSize())
2376       return ModificationType::Size;
2377     if (!DisableValidation && StoredTime &&
2378         StoredTime != File->getModificationTime()) {
2379       // In case the modification time changes but not the content,
2380       // accept the cached file as legit.
2381       if (ValidateASTInputFilesContent &&
2382           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2383         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2384         if (!MemBuffOrError) {
2385           if (!Complain)
2386             return ModificationType::ModTime;
2387           std::string ErrorStr = "could not get buffer for file '";
2388           ErrorStr += File->getName();
2389           ErrorStr += "'";
2390           Error(ErrorStr);
2391           return ModificationType::ModTime;
2392         }
2393 
2394         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2395         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2396           return ModificationType::None;
2397         return ModificationType::Content;
2398       }
2399       return ModificationType::ModTime;
2400     }
2401     return ModificationType::None;
2402   };
2403 
2404   bool IsOutOfDate = false;
2405   auto FileChange = HasInputFileChanged();
2406   // For an overridden file, there is nothing to validate.
2407   if (!Overridden && FileChange != ModificationType::None) {
2408     if (Complain) {
2409       // Build a list of the PCH imports that got us here (in reverse).
2410       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2411       while (!ImportStack.back()->ImportedBy.empty())
2412         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2413 
2414       // The top-level PCH is stale.
2415       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2416       unsigned DiagnosticKind =
2417           moduleKindForDiagnostic(ImportStack.back()->Kind);
2418       if (DiagnosticKind == 0)
2419         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2420               (unsigned)FileChange);
2421       else if (DiagnosticKind == 1)
2422         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2423               (unsigned)FileChange);
2424       else
2425         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2426               (unsigned)FileChange);
2427 
2428       // Print the import stack.
2429       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2430         Diag(diag::note_pch_required_by)
2431           << Filename << ImportStack[0]->FileName;
2432         for (unsigned I = 1; I < ImportStack.size(); ++I)
2433           Diag(diag::note_pch_required_by)
2434             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2435       }
2436 
2437       if (!Diags.isDiagnosticInFlight())
2438         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2439     }
2440 
2441     IsOutOfDate = true;
2442   }
2443   // FIXME: If the file is overridden and we've already opened it,
2444   // issue an error (or split it into a separate FileEntry).
2445 
2446   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2447 
2448   // Note that we've loaded this input file.
2449   F.InputFilesLoaded[ID-1] = IF;
2450   return IF;
2451 }
2452 
2453 /// If we are loading a relocatable PCH or module file, and the filename
2454 /// is not an absolute path, add the system or module root to the beginning of
2455 /// the file name.
2456 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2457   // Resolve relative to the base directory, if we have one.
2458   if (!M.BaseDirectory.empty())
2459     return ResolveImportedPath(Filename, M.BaseDirectory);
2460 }
2461 
2462 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2463   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2464     return;
2465 
2466   SmallString<128> Buffer;
2467   llvm::sys::path::append(Buffer, Prefix, Filename);
2468   Filename.assign(Buffer.begin(), Buffer.end());
2469 }
2470 
2471 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2472   switch (ARR) {
2473   case ASTReader::Failure: return true;
2474   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2475   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2476   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2477   case ASTReader::ConfigurationMismatch:
2478     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2479   case ASTReader::HadErrors: return true;
2480   case ASTReader::Success: return false;
2481   }
2482 
2483   llvm_unreachable("unknown ASTReadResult");
2484 }
2485 
2486 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2487     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2488     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2489     std::string &SuggestedPredefines) {
2490   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2491     // FIXME this drops errors on the floor.
2492     consumeError(std::move(Err));
2493     return Failure;
2494   }
2495 
2496   // Read all of the records in the options block.
2497   RecordData Record;
2498   ASTReadResult Result = Success;
2499   while (true) {
2500     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2501     if (!MaybeEntry) {
2502       // FIXME this drops errors on the floor.
2503       consumeError(MaybeEntry.takeError());
2504       return Failure;
2505     }
2506     llvm::BitstreamEntry Entry = MaybeEntry.get();
2507 
2508     switch (Entry.Kind) {
2509     case llvm::BitstreamEntry::Error:
2510     case llvm::BitstreamEntry::SubBlock:
2511       return Failure;
2512 
2513     case llvm::BitstreamEntry::EndBlock:
2514       return Result;
2515 
2516     case llvm::BitstreamEntry::Record:
2517       // The interesting case.
2518       break;
2519     }
2520 
2521     // Read and process a record.
2522     Record.clear();
2523     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2524     if (!MaybeRecordType) {
2525       // FIXME this drops errors on the floor.
2526       consumeError(MaybeRecordType.takeError());
2527       return Failure;
2528     }
2529     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2530     case LANGUAGE_OPTIONS: {
2531       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2532       if (ParseLanguageOptions(Record, Complain, Listener,
2533                                AllowCompatibleConfigurationMismatch))
2534         Result = ConfigurationMismatch;
2535       break;
2536     }
2537 
2538     case TARGET_OPTIONS: {
2539       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2540       if (ParseTargetOptions(Record, Complain, Listener,
2541                              AllowCompatibleConfigurationMismatch))
2542         Result = ConfigurationMismatch;
2543       break;
2544     }
2545 
2546     case FILE_SYSTEM_OPTIONS: {
2547       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2548       if (!AllowCompatibleConfigurationMismatch &&
2549           ParseFileSystemOptions(Record, Complain, Listener))
2550         Result = ConfigurationMismatch;
2551       break;
2552     }
2553 
2554     case HEADER_SEARCH_OPTIONS: {
2555       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2556       if (!AllowCompatibleConfigurationMismatch &&
2557           ParseHeaderSearchOptions(Record, Complain, Listener))
2558         Result = ConfigurationMismatch;
2559       break;
2560     }
2561 
2562     case PREPROCESSOR_OPTIONS:
2563       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2564       if (!AllowCompatibleConfigurationMismatch &&
2565           ParsePreprocessorOptions(Record, Complain, Listener,
2566                                    SuggestedPredefines))
2567         Result = ConfigurationMismatch;
2568       break;
2569     }
2570   }
2571 }
2572 
2573 ASTReader::ASTReadResult
2574 ASTReader::ReadControlBlock(ModuleFile &F,
2575                             SmallVectorImpl<ImportedModule> &Loaded,
2576                             const ModuleFile *ImportedBy,
2577                             unsigned ClientLoadCapabilities) {
2578   BitstreamCursor &Stream = F.Stream;
2579 
2580   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2581     Error(std::move(Err));
2582     return Failure;
2583   }
2584 
2585   // Lambda to read the unhashed control block the first time it's called.
2586   //
2587   // For PCM files, the unhashed control block cannot be read until after the
2588   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2589   // need to look ahead before reading the IMPORTS record.  For consistency,
2590   // this block is always read somehow (see BitstreamEntry::EndBlock).
2591   bool HasReadUnhashedControlBlock = false;
2592   auto readUnhashedControlBlockOnce = [&]() {
2593     if (!HasReadUnhashedControlBlock) {
2594       HasReadUnhashedControlBlock = true;
2595       if (ASTReadResult Result =
2596               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2597         return Result;
2598     }
2599     return Success;
2600   };
2601 
2602   // Read all of the records and blocks in the control block.
2603   RecordData Record;
2604   unsigned NumInputs = 0;
2605   unsigned NumUserInputs = 0;
2606   StringRef BaseDirectoryAsWritten;
2607   while (true) {
2608     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2609     if (!MaybeEntry) {
2610       Error(MaybeEntry.takeError());
2611       return Failure;
2612     }
2613     llvm::BitstreamEntry Entry = MaybeEntry.get();
2614 
2615     switch (Entry.Kind) {
2616     case llvm::BitstreamEntry::Error:
2617       Error("malformed block record in AST file");
2618       return Failure;
2619     case llvm::BitstreamEntry::EndBlock: {
2620       // Validate the module before returning.  This call catches an AST with
2621       // no module name and no imports.
2622       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2623         return Result;
2624 
2625       // Validate input files.
2626       const HeaderSearchOptions &HSOpts =
2627           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2628 
2629       // All user input files reside at the index range [0, NumUserInputs), and
2630       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2631       // loaded module files, ignore missing inputs.
2632       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2633           F.Kind != MK_PrebuiltModule) {
2634         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2635 
2636         // If we are reading a module, we will create a verification timestamp,
2637         // so we verify all input files.  Otherwise, verify only user input
2638         // files.
2639 
2640         unsigned N = NumUserInputs;
2641         if (ValidateSystemInputs ||
2642             (HSOpts.ModulesValidateOncePerBuildSession &&
2643              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2644              F.Kind == MK_ImplicitModule))
2645           N = NumInputs;
2646 
2647         for (unsigned I = 0; I < N; ++I) {
2648           InputFile IF = getInputFile(F, I+1, Complain);
2649           if (!IF.getFile() || IF.isOutOfDate())
2650             return OutOfDate;
2651         }
2652       }
2653 
2654       if (Listener)
2655         Listener->visitModuleFile(F.FileName, F.Kind);
2656 
2657       if (Listener && Listener->needsInputFileVisitation()) {
2658         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2659                                                                 : NumUserInputs;
2660         for (unsigned I = 0; I < N; ++I) {
2661           bool IsSystem = I >= NumUserInputs;
2662           InputFileInfo FI = readInputFileInfo(F, I+1);
2663           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2664                                    F.Kind == MK_ExplicitModule ||
2665                                    F.Kind == MK_PrebuiltModule);
2666         }
2667       }
2668 
2669       return Success;
2670     }
2671 
2672     case llvm::BitstreamEntry::SubBlock:
2673       switch (Entry.ID) {
2674       case INPUT_FILES_BLOCK_ID:
2675         F.InputFilesCursor = Stream;
2676         if (llvm::Error Err = Stream.SkipBlock()) {
2677           Error(std::move(Err));
2678           return Failure;
2679         }
2680         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2681           Error("malformed block record in AST file");
2682           return Failure;
2683         }
2684         continue;
2685 
2686       case OPTIONS_BLOCK_ID:
2687         // If we're reading the first module for this group, check its options
2688         // are compatible with ours. For modules it imports, no further checking
2689         // is required, because we checked them when we built it.
2690         if (Listener && !ImportedBy) {
2691           // Should we allow the configuration of the module file to differ from
2692           // the configuration of the current translation unit in a compatible
2693           // way?
2694           //
2695           // FIXME: Allow this for files explicitly specified with -include-pch.
2696           bool AllowCompatibleConfigurationMismatch =
2697               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2698 
2699           ASTReadResult Result =
2700               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2701                                AllowCompatibleConfigurationMismatch, *Listener,
2702                                SuggestedPredefines);
2703           if (Result == Failure) {
2704             Error("malformed block record in AST file");
2705             return Result;
2706           }
2707 
2708           if (DisableValidation ||
2709               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2710             Result = Success;
2711 
2712           // If we can't load the module, exit early since we likely
2713           // will rebuild the module anyway. The stream may be in the
2714           // middle of a block.
2715           if (Result != Success)
2716             return Result;
2717         } else if (llvm::Error Err = Stream.SkipBlock()) {
2718           Error(std::move(Err));
2719           return Failure;
2720         }
2721         continue;
2722 
2723       default:
2724         if (llvm::Error Err = Stream.SkipBlock()) {
2725           Error(std::move(Err));
2726           return Failure;
2727         }
2728         continue;
2729       }
2730 
2731     case llvm::BitstreamEntry::Record:
2732       // The interesting case.
2733       break;
2734     }
2735 
2736     // Read and process a record.
2737     Record.clear();
2738     StringRef Blob;
2739     Expected<unsigned> MaybeRecordType =
2740         Stream.readRecord(Entry.ID, Record, &Blob);
2741     if (!MaybeRecordType) {
2742       Error(MaybeRecordType.takeError());
2743       return Failure;
2744     }
2745     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2746     case METADATA: {
2747       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2748         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2749           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2750                                         : diag::err_pch_version_too_new);
2751         return VersionMismatch;
2752       }
2753 
2754       bool hasErrors = Record[6];
2755       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2756         Diag(diag::err_pch_with_compiler_errors);
2757         return HadErrors;
2758       }
2759       if (hasErrors) {
2760         Diags.ErrorOccurred = true;
2761         Diags.UncompilableErrorOccurred = true;
2762         Diags.UnrecoverableErrorOccurred = true;
2763       }
2764 
2765       F.RelocatablePCH = Record[4];
2766       // Relative paths in a relocatable PCH are relative to our sysroot.
2767       if (F.RelocatablePCH)
2768         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2769 
2770       F.HasTimestamps = Record[5];
2771 
2772       const std::string &CurBranch = getClangFullRepositoryVersion();
2773       StringRef ASTBranch = Blob;
2774       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2775         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2776           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2777         return VersionMismatch;
2778       }
2779       break;
2780     }
2781 
2782     case IMPORTS: {
2783       // Validate the AST before processing any imports (otherwise, untangling
2784       // them can be error-prone and expensive).  A module will have a name and
2785       // will already have been validated, but this catches the PCH case.
2786       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2787         return Result;
2788 
2789       // Load each of the imported PCH files.
2790       unsigned Idx = 0, N = Record.size();
2791       while (Idx < N) {
2792         // Read information about the AST file.
2793         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2794         // The import location will be the local one for now; we will adjust
2795         // all import locations of module imports after the global source
2796         // location info are setup, in ReadAST.
2797         SourceLocation ImportLoc =
2798             ReadUntranslatedSourceLocation(Record[Idx++]);
2799         off_t StoredSize = (off_t)Record[Idx++];
2800         time_t StoredModTime = (time_t)Record[Idx++];
2801         auto FirstSignatureByte = Record.begin() + Idx;
2802         ASTFileSignature StoredSignature = ASTFileSignature::create(
2803             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2804         Idx += ASTFileSignature::size;
2805 
2806         std::string ImportedName = ReadString(Record, Idx);
2807         std::string ImportedFile;
2808 
2809         // For prebuilt and explicit modules first consult the file map for
2810         // an override. Note that here we don't search prebuilt module
2811         // directories, only the explicit name to file mappings. Also, we will
2812         // still verify the size/signature making sure it is essentially the
2813         // same file but perhaps in a different location.
2814         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2815           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2816             ImportedName, /*FileMapOnly*/ true);
2817 
2818         if (ImportedFile.empty())
2819           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2820           // ModuleCache as when writing.
2821           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2822         else
2823           SkipPath(Record, Idx);
2824 
2825         // If our client can't cope with us being out of date, we can't cope with
2826         // our dependency being missing.
2827         unsigned Capabilities = ClientLoadCapabilities;
2828         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2829           Capabilities &= ~ARR_Missing;
2830 
2831         // Load the AST file.
2832         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2833                                   Loaded, StoredSize, StoredModTime,
2834                                   StoredSignature, Capabilities);
2835 
2836         // If we diagnosed a problem, produce a backtrace.
2837         if (isDiagnosedResult(Result, Capabilities))
2838           Diag(diag::note_module_file_imported_by)
2839               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2840 
2841         switch (Result) {
2842         case Failure: return Failure;
2843           // If we have to ignore the dependency, we'll have to ignore this too.
2844         case Missing:
2845         case OutOfDate: return OutOfDate;
2846         case VersionMismatch: return VersionMismatch;
2847         case ConfigurationMismatch: return ConfigurationMismatch;
2848         case HadErrors: return HadErrors;
2849         case Success: break;
2850         }
2851       }
2852       break;
2853     }
2854 
2855     case ORIGINAL_FILE:
2856       F.OriginalSourceFileID = FileID::get(Record[0]);
2857       F.ActualOriginalSourceFileName = std::string(Blob);
2858       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2859       ResolveImportedPath(F, F.OriginalSourceFileName);
2860       break;
2861 
2862     case ORIGINAL_FILE_ID:
2863       F.OriginalSourceFileID = FileID::get(Record[0]);
2864       break;
2865 
2866     case ORIGINAL_PCH_DIR:
2867       F.OriginalDir = std::string(Blob);
2868       break;
2869 
2870     case MODULE_NAME:
2871       F.ModuleName = std::string(Blob);
2872       Diag(diag::remark_module_import)
2873           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2874           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2875       if (Listener)
2876         Listener->ReadModuleName(F.ModuleName);
2877 
2878       // Validate the AST as soon as we have a name so we can exit early on
2879       // failure.
2880       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2881         return Result;
2882 
2883       break;
2884 
2885     case MODULE_DIRECTORY: {
2886       // Save the BaseDirectory as written in the PCM for computing the module
2887       // filename for the ModuleCache.
2888       BaseDirectoryAsWritten = Blob;
2889       assert(!F.ModuleName.empty() &&
2890              "MODULE_DIRECTORY found before MODULE_NAME");
2891       // If we've already loaded a module map file covering this module, we may
2892       // have a better path for it (relative to the current build).
2893       Module *M = PP.getHeaderSearchInfo().lookupModule(
2894           F.ModuleName, /*AllowSearch*/ true,
2895           /*AllowExtraModuleMapSearch*/ true);
2896       if (M && M->Directory) {
2897         // If we're implicitly loading a module, the base directory can't
2898         // change between the build and use.
2899         // Don't emit module relocation error if we have -fno-validate-pch
2900         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2901             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2902           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2903           if (!BuildDir || *BuildDir != M->Directory) {
2904             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2905               Diag(diag::err_imported_module_relocated)
2906                   << F.ModuleName << Blob << M->Directory->getName();
2907             return OutOfDate;
2908           }
2909         }
2910         F.BaseDirectory = std::string(M->Directory->getName());
2911       } else {
2912         F.BaseDirectory = std::string(Blob);
2913       }
2914       break;
2915     }
2916 
2917     case MODULE_MAP_FILE:
2918       if (ASTReadResult Result =
2919               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2920         return Result;
2921       break;
2922 
2923     case INPUT_FILE_OFFSETS:
2924       NumInputs = Record[0];
2925       NumUserInputs = Record[1];
2926       F.InputFileOffsets =
2927           (const llvm::support::unaligned_uint64_t *)Blob.data();
2928       F.InputFilesLoaded.resize(NumInputs);
2929       F.NumUserInputFiles = NumUserInputs;
2930       break;
2931     }
2932   }
2933 }
2934 
2935 ASTReader::ASTReadResult
2936 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2937   BitstreamCursor &Stream = F.Stream;
2938 
2939   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2940     Error(std::move(Err));
2941     return Failure;
2942   }
2943   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2944 
2945   // Read all of the records and blocks for the AST file.
2946   RecordData Record;
2947   while (true) {
2948     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2949     if (!MaybeEntry) {
2950       Error(MaybeEntry.takeError());
2951       return Failure;
2952     }
2953     llvm::BitstreamEntry Entry = MaybeEntry.get();
2954 
2955     switch (Entry.Kind) {
2956     case llvm::BitstreamEntry::Error:
2957       Error("error at end of module block in AST file");
2958       return Failure;
2959     case llvm::BitstreamEntry::EndBlock:
2960       // Outside of C++, we do not store a lookup map for the translation unit.
2961       // Instead, mark it as needing a lookup map to be built if this module
2962       // contains any declarations lexically within it (which it always does!).
2963       // This usually has no cost, since we very rarely need the lookup map for
2964       // the translation unit outside C++.
2965       if (ASTContext *Ctx = ContextObj) {
2966         DeclContext *DC = Ctx->getTranslationUnitDecl();
2967         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2968           DC->setMustBuildLookupTable();
2969       }
2970 
2971       return Success;
2972     case llvm::BitstreamEntry::SubBlock:
2973       switch (Entry.ID) {
2974       case DECLTYPES_BLOCK_ID:
2975         // We lazily load the decls block, but we want to set up the
2976         // DeclsCursor cursor to point into it.  Clone our current bitcode
2977         // cursor to it, enter the block and read the abbrevs in that block.
2978         // With the main cursor, we just skip over it.
2979         F.DeclsCursor = Stream;
2980         if (llvm::Error Err = Stream.SkipBlock()) {
2981           Error(std::move(Err));
2982           return Failure;
2983         }
2984         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2985                              &F.DeclsBlockStartOffset)) {
2986           Error("malformed block record in AST file");
2987           return Failure;
2988         }
2989         break;
2990 
2991       case PREPROCESSOR_BLOCK_ID:
2992         F.MacroCursor = Stream;
2993         if (!PP.getExternalSource())
2994           PP.setExternalSource(this);
2995 
2996         if (llvm::Error Err = Stream.SkipBlock()) {
2997           Error(std::move(Err));
2998           return Failure;
2999         }
3000         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3001           Error("malformed block record in AST file");
3002           return Failure;
3003         }
3004         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3005         break;
3006 
3007       case PREPROCESSOR_DETAIL_BLOCK_ID:
3008         F.PreprocessorDetailCursor = Stream;
3009 
3010         if (llvm::Error Err = Stream.SkipBlock()) {
3011           Error(std::move(Err));
3012           return Failure;
3013         }
3014         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3015                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3016           Error("malformed preprocessor detail record in AST file");
3017           return Failure;
3018         }
3019         F.PreprocessorDetailStartOffset
3020         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3021 
3022         if (!PP.getPreprocessingRecord())
3023           PP.createPreprocessingRecord();
3024         if (!PP.getPreprocessingRecord()->getExternalSource())
3025           PP.getPreprocessingRecord()->SetExternalSource(*this);
3026         break;
3027 
3028       case SOURCE_MANAGER_BLOCK_ID:
3029         if (ReadSourceManagerBlock(F))
3030           return Failure;
3031         break;
3032 
3033       case SUBMODULE_BLOCK_ID:
3034         if (ASTReadResult Result =
3035                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3036           return Result;
3037         break;
3038 
3039       case COMMENTS_BLOCK_ID: {
3040         BitstreamCursor C = Stream;
3041 
3042         if (llvm::Error Err = Stream.SkipBlock()) {
3043           Error(std::move(Err));
3044           return Failure;
3045         }
3046         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3047           Error("malformed comments block in AST file");
3048           return Failure;
3049         }
3050         CommentsCursors.push_back(std::make_pair(C, &F));
3051         break;
3052       }
3053 
3054       default:
3055         if (llvm::Error Err = Stream.SkipBlock()) {
3056           Error(std::move(Err));
3057           return Failure;
3058         }
3059         break;
3060       }
3061       continue;
3062 
3063     case llvm::BitstreamEntry::Record:
3064       // The interesting case.
3065       break;
3066     }
3067 
3068     // Read and process a record.
3069     Record.clear();
3070     StringRef Blob;
3071     Expected<unsigned> MaybeRecordType =
3072         Stream.readRecord(Entry.ID, Record, &Blob);
3073     if (!MaybeRecordType) {
3074       Error(MaybeRecordType.takeError());
3075       return Failure;
3076     }
3077     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3078 
3079     // If we're not loading an AST context, we don't care about most records.
3080     if (!ContextObj) {
3081       switch (RecordType) {
3082       case IDENTIFIER_TABLE:
3083       case IDENTIFIER_OFFSET:
3084       case INTERESTING_IDENTIFIERS:
3085       case STATISTICS:
3086       case PP_CONDITIONAL_STACK:
3087       case PP_COUNTER_VALUE:
3088       case SOURCE_LOCATION_OFFSETS:
3089       case MODULE_OFFSET_MAP:
3090       case SOURCE_MANAGER_LINE_TABLE:
3091       case SOURCE_LOCATION_PRELOADS:
3092       case PPD_ENTITIES_OFFSETS:
3093       case HEADER_SEARCH_TABLE:
3094       case IMPORTED_MODULES:
3095       case MACRO_OFFSET:
3096         break;
3097       default:
3098         continue;
3099       }
3100     }
3101 
3102     switch (RecordType) {
3103     default:  // Default behavior: ignore.
3104       break;
3105 
3106     case TYPE_OFFSET: {
3107       if (F.LocalNumTypes != 0) {
3108         Error("duplicate TYPE_OFFSET record in AST file");
3109         return Failure;
3110       }
3111       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3112       F.LocalNumTypes = Record[0];
3113       unsigned LocalBaseTypeIndex = Record[1];
3114       F.BaseTypeIndex = getTotalNumTypes();
3115 
3116       if (F.LocalNumTypes > 0) {
3117         // Introduce the global -> local mapping for types within this module.
3118         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3119 
3120         // Introduce the local -> global mapping for types within this module.
3121         F.TypeRemap.insertOrReplace(
3122           std::make_pair(LocalBaseTypeIndex,
3123                          F.BaseTypeIndex - LocalBaseTypeIndex));
3124 
3125         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3126       }
3127       break;
3128     }
3129 
3130     case DECL_OFFSET: {
3131       if (F.LocalNumDecls != 0) {
3132         Error("duplicate DECL_OFFSET record in AST file");
3133         return Failure;
3134       }
3135       F.DeclOffsets = (const DeclOffset *)Blob.data();
3136       F.LocalNumDecls = Record[0];
3137       unsigned LocalBaseDeclID = Record[1];
3138       F.BaseDeclID = getTotalNumDecls();
3139 
3140       if (F.LocalNumDecls > 0) {
3141         // Introduce the global -> local mapping for declarations within this
3142         // module.
3143         GlobalDeclMap.insert(
3144           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3145 
3146         // Introduce the local -> global mapping for declarations within this
3147         // module.
3148         F.DeclRemap.insertOrReplace(
3149           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3150 
3151         // Introduce the global -> local mapping for declarations within this
3152         // module.
3153         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3154 
3155         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3156       }
3157       break;
3158     }
3159 
3160     case TU_UPDATE_LEXICAL: {
3161       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3162       LexicalContents Contents(
3163           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3164               Blob.data()),
3165           static_cast<unsigned int>(Blob.size() / 4));
3166       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3167       TU->setHasExternalLexicalStorage(true);
3168       break;
3169     }
3170 
3171     case UPDATE_VISIBLE: {
3172       unsigned Idx = 0;
3173       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3174       auto *Data = (const unsigned char*)Blob.data();
3175       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3176       // If we've already loaded the decl, perform the updates when we finish
3177       // loading this block.
3178       if (Decl *D = GetExistingDecl(ID))
3179         PendingUpdateRecords.push_back(
3180             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3181       break;
3182     }
3183 
3184     case IDENTIFIER_TABLE:
3185       F.IdentifierTableData = Blob.data();
3186       if (Record[0]) {
3187         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3188             (const unsigned char *)F.IdentifierTableData + Record[0],
3189             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3190             (const unsigned char *)F.IdentifierTableData,
3191             ASTIdentifierLookupTrait(*this, F));
3192 
3193         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3194       }
3195       break;
3196 
3197     case IDENTIFIER_OFFSET: {
3198       if (F.LocalNumIdentifiers != 0) {
3199         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3200         return Failure;
3201       }
3202       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3203       F.LocalNumIdentifiers = Record[0];
3204       unsigned LocalBaseIdentifierID = Record[1];
3205       F.BaseIdentifierID = getTotalNumIdentifiers();
3206 
3207       if (F.LocalNumIdentifiers > 0) {
3208         // Introduce the global -> local mapping for identifiers within this
3209         // module.
3210         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3211                                                   &F));
3212 
3213         // Introduce the local -> global mapping for identifiers within this
3214         // module.
3215         F.IdentifierRemap.insertOrReplace(
3216           std::make_pair(LocalBaseIdentifierID,
3217                          F.BaseIdentifierID - LocalBaseIdentifierID));
3218 
3219         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3220                                  + F.LocalNumIdentifiers);
3221       }
3222       break;
3223     }
3224 
3225     case INTERESTING_IDENTIFIERS:
3226       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3227       break;
3228 
3229     case EAGERLY_DESERIALIZED_DECLS:
3230       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3231       // about "interesting" decls (for instance, if we're building a module).
3232       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3233         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3234       break;
3235 
3236     case MODULAR_CODEGEN_DECLS:
3237       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3238       // them (ie: if we're not codegenerating this module).
3239       if (F.Kind == MK_MainFile ||
3240           getContext().getLangOpts().BuildingPCHWithObjectFile)
3241         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3242           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3243       break;
3244 
3245     case SPECIAL_TYPES:
3246       if (SpecialTypes.empty()) {
3247         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3248           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3249         break;
3250       }
3251 
3252       if (SpecialTypes.size() != Record.size()) {
3253         Error("invalid special-types record");
3254         return Failure;
3255       }
3256 
3257       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3258         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3259         if (!SpecialTypes[I])
3260           SpecialTypes[I] = ID;
3261         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3262         // merge step?
3263       }
3264       break;
3265 
3266     case STATISTICS:
3267       TotalNumStatements += Record[0];
3268       TotalNumMacros += Record[1];
3269       TotalLexicalDeclContexts += Record[2];
3270       TotalVisibleDeclContexts += Record[3];
3271       break;
3272 
3273     case UNUSED_FILESCOPED_DECLS:
3274       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3275         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3276       break;
3277 
3278     case DELEGATING_CTORS:
3279       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3280         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3281       break;
3282 
3283     case WEAK_UNDECLARED_IDENTIFIERS:
3284       if (Record.size() % 4 != 0) {
3285         Error("invalid weak identifiers record");
3286         return Failure;
3287       }
3288 
3289       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3290       // files. This isn't the way to do it :)
3291       WeakUndeclaredIdentifiers.clear();
3292 
3293       // Translate the weak, undeclared identifiers into global IDs.
3294       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3295         WeakUndeclaredIdentifiers.push_back(
3296           getGlobalIdentifierID(F, Record[I++]));
3297         WeakUndeclaredIdentifiers.push_back(
3298           getGlobalIdentifierID(F, Record[I++]));
3299         WeakUndeclaredIdentifiers.push_back(
3300           ReadSourceLocation(F, Record, I).getRawEncoding());
3301         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3302       }
3303       break;
3304 
3305     case SELECTOR_OFFSETS: {
3306       F.SelectorOffsets = (const uint32_t *)Blob.data();
3307       F.LocalNumSelectors = Record[0];
3308       unsigned LocalBaseSelectorID = Record[1];
3309       F.BaseSelectorID = getTotalNumSelectors();
3310 
3311       if (F.LocalNumSelectors > 0) {
3312         // Introduce the global -> local mapping for selectors within this
3313         // module.
3314         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3315 
3316         // Introduce the local -> global mapping for selectors within this
3317         // module.
3318         F.SelectorRemap.insertOrReplace(
3319           std::make_pair(LocalBaseSelectorID,
3320                          F.BaseSelectorID - LocalBaseSelectorID));
3321 
3322         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3323       }
3324       break;
3325     }
3326 
3327     case METHOD_POOL:
3328       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3329       if (Record[0])
3330         F.SelectorLookupTable
3331           = ASTSelectorLookupTable::Create(
3332                         F.SelectorLookupTableData + Record[0],
3333                         F.SelectorLookupTableData,
3334                         ASTSelectorLookupTrait(*this, F));
3335       TotalNumMethodPoolEntries += Record[1];
3336       break;
3337 
3338     case REFERENCED_SELECTOR_POOL:
3339       if (!Record.empty()) {
3340         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3341           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3342                                                                 Record[Idx++]));
3343           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3344                                               getRawEncoding());
3345         }
3346       }
3347       break;
3348 
3349     case PP_CONDITIONAL_STACK:
3350       if (!Record.empty()) {
3351         unsigned Idx = 0, End = Record.size() - 1;
3352         bool ReachedEOFWhileSkipping = Record[Idx++];
3353         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3354         if (ReachedEOFWhileSkipping) {
3355           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3356           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3357           bool FoundNonSkipPortion = Record[Idx++];
3358           bool FoundElse = Record[Idx++];
3359           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3360           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3361                            FoundElse, ElseLoc);
3362         }
3363         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3364         while (Idx < End) {
3365           auto Loc = ReadSourceLocation(F, Record, Idx);
3366           bool WasSkipping = Record[Idx++];
3367           bool FoundNonSkip = Record[Idx++];
3368           bool FoundElse = Record[Idx++];
3369           ConditionalStack.push_back(
3370               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3371         }
3372         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3373       }
3374       break;
3375 
3376     case PP_COUNTER_VALUE:
3377       if (!Record.empty() && Listener)
3378         Listener->ReadCounter(F, Record[0]);
3379       break;
3380 
3381     case FILE_SORTED_DECLS:
3382       F.FileSortedDecls = (const DeclID *)Blob.data();
3383       F.NumFileSortedDecls = Record[0];
3384       break;
3385 
3386     case SOURCE_LOCATION_OFFSETS: {
3387       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3388       F.LocalNumSLocEntries = Record[0];
3389       unsigned SLocSpaceSize = Record[1];
3390       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3391       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3392           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3393                                               SLocSpaceSize);
3394       if (!F.SLocEntryBaseID) {
3395         Error("ran out of source locations");
3396         break;
3397       }
3398       // Make our entry in the range map. BaseID is negative and growing, so
3399       // we invert it. Because we invert it, though, we need the other end of
3400       // the range.
3401       unsigned RangeStart =
3402           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3403       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3404       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3405 
3406       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3407       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3408       GlobalSLocOffsetMap.insert(
3409           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3410                            - SLocSpaceSize,&F));
3411 
3412       // Initialize the remapping table.
3413       // Invalid stays invalid.
3414       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3415       // This module. Base was 2 when being compiled.
3416       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3417                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3418 
3419       TotalNumSLocEntries += F.LocalNumSLocEntries;
3420       break;
3421     }
3422 
3423     case MODULE_OFFSET_MAP:
3424       F.ModuleOffsetMap = Blob;
3425       break;
3426 
3427     case SOURCE_MANAGER_LINE_TABLE:
3428       if (ParseLineTable(F, Record)) {
3429         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3430         return Failure;
3431       }
3432       break;
3433 
3434     case SOURCE_LOCATION_PRELOADS: {
3435       // Need to transform from the local view (1-based IDs) to the global view,
3436       // which is based off F.SLocEntryBaseID.
3437       if (!F.PreloadSLocEntries.empty()) {
3438         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3439         return Failure;
3440       }
3441 
3442       F.PreloadSLocEntries.swap(Record);
3443       break;
3444     }
3445 
3446     case EXT_VECTOR_DECLS:
3447       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3448         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3449       break;
3450 
3451     case VTABLE_USES:
3452       if (Record.size() % 3 != 0) {
3453         Error("Invalid VTABLE_USES record");
3454         return Failure;
3455       }
3456 
3457       // Later tables overwrite earlier ones.
3458       // FIXME: Modules will have some trouble with this. This is clearly not
3459       // the right way to do this.
3460       VTableUses.clear();
3461 
3462       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3463         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3464         VTableUses.push_back(
3465           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3466         VTableUses.push_back(Record[Idx++]);
3467       }
3468       break;
3469 
3470     case PENDING_IMPLICIT_INSTANTIATIONS:
3471       if (PendingInstantiations.size() % 2 != 0) {
3472         Error("Invalid existing PendingInstantiations");
3473         return Failure;
3474       }
3475 
3476       if (Record.size() % 2 != 0) {
3477         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3478         return Failure;
3479       }
3480 
3481       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3482         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3483         PendingInstantiations.push_back(
3484           ReadSourceLocation(F, Record, I).getRawEncoding());
3485       }
3486       break;
3487 
3488     case SEMA_DECL_REFS:
3489       if (Record.size() != 3) {
3490         Error("Invalid SEMA_DECL_REFS block");
3491         return Failure;
3492       }
3493       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3494         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3495       break;
3496 
3497     case PPD_ENTITIES_OFFSETS: {
3498       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3499       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3500       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3501 
3502       unsigned LocalBasePreprocessedEntityID = Record[0];
3503 
3504       unsigned StartingID;
3505       if (!PP.getPreprocessingRecord())
3506         PP.createPreprocessingRecord();
3507       if (!PP.getPreprocessingRecord()->getExternalSource())
3508         PP.getPreprocessingRecord()->SetExternalSource(*this);
3509       StartingID
3510         = PP.getPreprocessingRecord()
3511             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3512       F.BasePreprocessedEntityID = StartingID;
3513 
3514       if (F.NumPreprocessedEntities > 0) {
3515         // Introduce the global -> local mapping for preprocessed entities in
3516         // this module.
3517         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3518 
3519         // Introduce the local -> global mapping for preprocessed entities in
3520         // this module.
3521         F.PreprocessedEntityRemap.insertOrReplace(
3522           std::make_pair(LocalBasePreprocessedEntityID,
3523             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3524       }
3525 
3526       break;
3527     }
3528 
3529     case PPD_SKIPPED_RANGES: {
3530       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3531       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3532       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3533 
3534       if (!PP.getPreprocessingRecord())
3535         PP.createPreprocessingRecord();
3536       if (!PP.getPreprocessingRecord()->getExternalSource())
3537         PP.getPreprocessingRecord()->SetExternalSource(*this);
3538       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3539           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3540 
3541       if (F.NumPreprocessedSkippedRanges > 0)
3542         GlobalSkippedRangeMap.insert(
3543             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3544       break;
3545     }
3546 
3547     case DECL_UPDATE_OFFSETS:
3548       if (Record.size() % 2 != 0) {
3549         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3550         return Failure;
3551       }
3552       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3553         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3554         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3555 
3556         // If we've already loaded the decl, perform the updates when we finish
3557         // loading this block.
3558         if (Decl *D = GetExistingDecl(ID))
3559           PendingUpdateRecords.push_back(
3560               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3561       }
3562       break;
3563 
3564     case OBJC_CATEGORIES_MAP:
3565       if (F.LocalNumObjCCategoriesInMap != 0) {
3566         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3567         return Failure;
3568       }
3569 
3570       F.LocalNumObjCCategoriesInMap = Record[0];
3571       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3572       break;
3573 
3574     case OBJC_CATEGORIES:
3575       F.ObjCCategories.swap(Record);
3576       break;
3577 
3578     case CUDA_SPECIAL_DECL_REFS:
3579       // Later tables overwrite earlier ones.
3580       // FIXME: Modules will have trouble with this.
3581       CUDASpecialDeclRefs.clear();
3582       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3583         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3584       break;
3585 
3586     case HEADER_SEARCH_TABLE:
3587       F.HeaderFileInfoTableData = Blob.data();
3588       F.LocalNumHeaderFileInfos = Record[1];
3589       if (Record[0]) {
3590         F.HeaderFileInfoTable
3591           = HeaderFileInfoLookupTable::Create(
3592                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3593                    (const unsigned char *)F.HeaderFileInfoTableData,
3594                    HeaderFileInfoTrait(*this, F,
3595                                        &PP.getHeaderSearchInfo(),
3596                                        Blob.data() + Record[2]));
3597 
3598         PP.getHeaderSearchInfo().SetExternalSource(this);
3599         if (!PP.getHeaderSearchInfo().getExternalLookup())
3600           PP.getHeaderSearchInfo().SetExternalLookup(this);
3601       }
3602       break;
3603 
3604     case FP_PRAGMA_OPTIONS:
3605       // Later tables overwrite earlier ones.
3606       FPPragmaOptions.swap(Record);
3607       break;
3608 
3609     case OPENCL_EXTENSIONS:
3610       for (unsigned I = 0, E = Record.size(); I != E; ) {
3611         auto Name = ReadString(Record, I);
3612         auto &Opt = OpenCLExtensions.OptMap[Name];
3613         Opt.Supported = Record[I++] != 0;
3614         Opt.Enabled = Record[I++] != 0;
3615         Opt.Avail = Record[I++];
3616         Opt.Core = Record[I++];
3617       }
3618       break;
3619 
3620     case OPENCL_EXTENSION_TYPES:
3621       for (unsigned I = 0, E = Record.size(); I != E;) {
3622         auto TypeID = static_cast<::TypeID>(Record[I++]);
3623         auto *Type = GetType(TypeID).getTypePtr();
3624         auto NumExt = static_cast<unsigned>(Record[I++]);
3625         for (unsigned II = 0; II != NumExt; ++II) {
3626           auto Ext = ReadString(Record, I);
3627           OpenCLTypeExtMap[Type].insert(Ext);
3628         }
3629       }
3630       break;
3631 
3632     case OPENCL_EXTENSION_DECLS:
3633       for (unsigned I = 0, E = Record.size(); I != E;) {
3634         auto DeclID = static_cast<::DeclID>(Record[I++]);
3635         auto *Decl = GetDecl(DeclID);
3636         auto NumExt = static_cast<unsigned>(Record[I++]);
3637         for (unsigned II = 0; II != NumExt; ++II) {
3638           auto Ext = ReadString(Record, I);
3639           OpenCLDeclExtMap[Decl].insert(Ext);
3640         }
3641       }
3642       break;
3643 
3644     case TENTATIVE_DEFINITIONS:
3645       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3646         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3647       break;
3648 
3649     case KNOWN_NAMESPACES:
3650       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3651         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3652       break;
3653 
3654     case UNDEFINED_BUT_USED:
3655       if (UndefinedButUsed.size() % 2 != 0) {
3656         Error("Invalid existing UndefinedButUsed");
3657         return Failure;
3658       }
3659 
3660       if (Record.size() % 2 != 0) {
3661         Error("invalid undefined-but-used record");
3662         return Failure;
3663       }
3664       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3665         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3666         UndefinedButUsed.push_back(
3667             ReadSourceLocation(F, Record, I).getRawEncoding());
3668       }
3669       break;
3670 
3671     case DELETE_EXPRS_TO_ANALYZE:
3672       for (unsigned I = 0, N = Record.size(); I != N;) {
3673         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3674         const uint64_t Count = Record[I++];
3675         DelayedDeleteExprs.push_back(Count);
3676         for (uint64_t C = 0; C < Count; ++C) {
3677           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3678           bool IsArrayForm = Record[I++] == 1;
3679           DelayedDeleteExprs.push_back(IsArrayForm);
3680         }
3681       }
3682       break;
3683 
3684     case IMPORTED_MODULES:
3685       if (!F.isModule()) {
3686         // If we aren't loading a module (which has its own exports), make
3687         // all of the imported modules visible.
3688         // FIXME: Deal with macros-only imports.
3689         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3690           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3691           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3692           if (GlobalID) {
3693             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3694             if (DeserializationListener)
3695               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3696           }
3697         }
3698       }
3699       break;
3700 
3701     case MACRO_OFFSET: {
3702       if (F.LocalNumMacros != 0) {
3703         Error("duplicate MACRO_OFFSET record in AST file");
3704         return Failure;
3705       }
3706       F.MacroOffsets = (const uint32_t *)Blob.data();
3707       F.LocalNumMacros = Record[0];
3708       unsigned LocalBaseMacroID = Record[1];
3709       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3710       F.BaseMacroID = getTotalNumMacros();
3711 
3712       if (F.LocalNumMacros > 0) {
3713         // Introduce the global -> local mapping for macros within this module.
3714         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3715 
3716         // Introduce the local -> global mapping for macros within this module.
3717         F.MacroRemap.insertOrReplace(
3718           std::make_pair(LocalBaseMacroID,
3719                          F.BaseMacroID - LocalBaseMacroID));
3720 
3721         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3722       }
3723       break;
3724     }
3725 
3726     case LATE_PARSED_TEMPLATE:
3727       LateParsedTemplates.emplace_back(
3728           std::piecewise_construct, std::forward_as_tuple(&F),
3729           std::forward_as_tuple(Record.begin(), Record.end()));
3730       break;
3731 
3732     case OPTIMIZE_PRAGMA_OPTIONS:
3733       if (Record.size() != 1) {
3734         Error("invalid pragma optimize record");
3735         return Failure;
3736       }
3737       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3738       break;
3739 
3740     case MSSTRUCT_PRAGMA_OPTIONS:
3741       if (Record.size() != 1) {
3742         Error("invalid pragma ms_struct record");
3743         return Failure;
3744       }
3745       PragmaMSStructState = Record[0];
3746       break;
3747 
3748     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3749       if (Record.size() != 2) {
3750         Error("invalid pragma ms_struct record");
3751         return Failure;
3752       }
3753       PragmaMSPointersToMembersState = Record[0];
3754       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3755       break;
3756 
3757     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3758       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3759         UnusedLocalTypedefNameCandidates.push_back(
3760             getGlobalDeclID(F, Record[I]));
3761       break;
3762 
3763     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3764       if (Record.size() != 1) {
3765         Error("invalid cuda pragma options record");
3766         return Failure;
3767       }
3768       ForceCUDAHostDeviceDepth = Record[0];
3769       break;
3770 
3771     case PACK_PRAGMA_OPTIONS: {
3772       if (Record.size() < 3) {
3773         Error("invalid pragma pack record");
3774         return Failure;
3775       }
3776       PragmaPackCurrentValue = Record[0];
3777       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3778       unsigned NumStackEntries = Record[2];
3779       unsigned Idx = 3;
3780       // Reset the stack when importing a new module.
3781       PragmaPackStack.clear();
3782       for (unsigned I = 0; I < NumStackEntries; ++I) {
3783         PragmaPackStackEntry Entry;
3784         Entry.Value = Record[Idx++];
3785         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3786         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3787         PragmaPackStrings.push_back(ReadString(Record, Idx));
3788         Entry.SlotLabel = PragmaPackStrings.back();
3789         PragmaPackStack.push_back(Entry);
3790       }
3791       break;
3792     }
3793 
3794     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3795       if (Record.size() < 3) {
3796         Error("invalid pragma pack record");
3797         return Failure;
3798       }
3799       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3800       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3801       unsigned NumStackEntries = Record[2];
3802       unsigned Idx = 3;
3803       // Reset the stack when importing a new module.
3804       FpPragmaStack.clear();
3805       for (unsigned I = 0; I < NumStackEntries; ++I) {
3806         FpPragmaStackEntry Entry;
3807         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3808         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3809         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3810         FpPragmaStrings.push_back(ReadString(Record, Idx));
3811         Entry.SlotLabel = FpPragmaStrings.back();
3812         FpPragmaStack.push_back(Entry);
3813       }
3814       break;
3815     }
3816 
3817     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3818       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3819         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3820       break;
3821     }
3822   }
3823 }
3824 
3825 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3826   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3827 
3828   // Additional remapping information.
3829   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3830   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3831   F.ModuleOffsetMap = StringRef();
3832 
3833   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3834   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3835     F.SLocRemap.insert(std::make_pair(0U, 0));
3836     F.SLocRemap.insert(std::make_pair(2U, 1));
3837   }
3838 
3839   // Continuous range maps we may be updating in our module.
3840   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3841   RemapBuilder SLocRemap(F.SLocRemap);
3842   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3843   RemapBuilder MacroRemap(F.MacroRemap);
3844   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3845   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3846   RemapBuilder SelectorRemap(F.SelectorRemap);
3847   RemapBuilder DeclRemap(F.DeclRemap);
3848   RemapBuilder TypeRemap(F.TypeRemap);
3849 
3850   while (Data < DataEnd) {
3851     // FIXME: Looking up dependency modules by filename is horrible. Let's
3852     // start fixing this with prebuilt, explicit and implicit modules and see
3853     // how it goes...
3854     using namespace llvm::support;
3855     ModuleKind Kind = static_cast<ModuleKind>(
3856       endian::readNext<uint8_t, little, unaligned>(Data));
3857     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3858     StringRef Name = StringRef((const char*)Data, Len);
3859     Data += Len;
3860     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3861                               Kind == MK_ImplicitModule
3862                           ? ModuleMgr.lookupByModuleName(Name)
3863                           : ModuleMgr.lookupByFileName(Name));
3864     if (!OM) {
3865       std::string Msg =
3866           "SourceLocation remap refers to unknown module, cannot find ";
3867       Msg.append(std::string(Name));
3868       Error(Msg);
3869       return;
3870     }
3871 
3872     uint32_t SLocOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t IdentifierIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t MacroIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t PreprocessedEntityIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t SubmoduleIDOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882     uint32_t SelectorIDOffset =
3883         endian::readNext<uint32_t, little, unaligned>(Data);
3884     uint32_t DeclIDOffset =
3885         endian::readNext<uint32_t, little, unaligned>(Data);
3886     uint32_t TypeIndexOffset =
3887         endian::readNext<uint32_t, little, unaligned>(Data);
3888 
3889     uint32_t None = std::numeric_limits<uint32_t>::max();
3890 
3891     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3892                          RemapBuilder &Remap) {
3893       if (Offset != None)
3894         Remap.insert(std::make_pair(Offset,
3895                                     static_cast<int>(BaseOffset - Offset)));
3896     };
3897     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3898     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3899     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3900     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3901               PreprocessedEntityRemap);
3902     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3903     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3904     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3905     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3906 
3907     // Global -> local mappings.
3908     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3909   }
3910 }
3911 
3912 ASTReader::ASTReadResult
3913 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3914                                   const ModuleFile *ImportedBy,
3915                                   unsigned ClientLoadCapabilities) {
3916   unsigned Idx = 0;
3917   F.ModuleMapPath = ReadPath(F, Record, Idx);
3918 
3919   // Try to resolve ModuleName in the current header search context and
3920   // verify that it is found in the same module map file as we saved. If the
3921   // top-level AST file is a main file, skip this check because there is no
3922   // usable header search context.
3923   assert(!F.ModuleName.empty() &&
3924          "MODULE_NAME should come before MODULE_MAP_FILE");
3925   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3926     // An implicitly-loaded module file should have its module listed in some
3927     // module map file that we've already loaded.
3928     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3929     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3930     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3931     // Don't emit module relocation error if we have -fno-validate-pch
3932     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3933       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3934         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3935           // This module was defined by an imported (explicit) module.
3936           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3937                                                << ASTFE->getName();
3938         } else {
3939           // This module was built with a different module map.
3940           Diag(diag::err_imported_module_not_found)
3941               << F.ModuleName << F.FileName
3942               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3943               << !ImportedBy;
3944           // In case it was imported by a PCH, there's a chance the user is
3945           // just missing to include the search path to the directory containing
3946           // the modulemap.
3947           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3948             Diag(diag::note_imported_by_pch_module_not_found)
3949                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3950         }
3951       }
3952       return OutOfDate;
3953     }
3954 
3955     assert(M && M->Name == F.ModuleName && "found module with different name");
3956 
3957     // Check the primary module map file.
3958     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3959     if (!StoredModMap || *StoredModMap != ModMap) {
3960       assert(ModMap && "found module is missing module map file");
3961       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3962              "top-level import should be verified");
3963       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3964       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3965         Diag(diag::err_imported_module_modmap_changed)
3966             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3967             << ModMap->getName() << F.ModuleMapPath << NotImported;
3968       return OutOfDate;
3969     }
3970 
3971     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3972     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3973       // FIXME: we should use input files rather than storing names.
3974       std::string Filename = ReadPath(F, Record, Idx);
3975       auto F = FileMgr.getFile(Filename, false, false);
3976       if (!F) {
3977         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3978           Error("could not find file '" + Filename +"' referenced by AST file");
3979         return OutOfDate;
3980       }
3981       AdditionalStoredMaps.insert(*F);
3982     }
3983 
3984     // Check any additional module map files (e.g. module.private.modulemap)
3985     // that are not in the pcm.
3986     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3987       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3988         // Remove files that match
3989         // Note: SmallPtrSet::erase is really remove
3990         if (!AdditionalStoredMaps.erase(ModMap)) {
3991           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3992             Diag(diag::err_module_different_modmap)
3993               << F.ModuleName << /*new*/0 << ModMap->getName();
3994           return OutOfDate;
3995         }
3996       }
3997     }
3998 
3999     // Check any additional module map files that are in the pcm, but not
4000     // found in header search. Cases that match are already removed.
4001     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4002       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4003         Diag(diag::err_module_different_modmap)
4004           << F.ModuleName << /*not new*/1 << ModMap->getName();
4005       return OutOfDate;
4006     }
4007   }
4008 
4009   if (Listener)
4010     Listener->ReadModuleMapFile(F.ModuleMapPath);
4011   return Success;
4012 }
4013 
4014 /// Move the given method to the back of the global list of methods.
4015 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4016   // Find the entry for this selector in the method pool.
4017   Sema::GlobalMethodPool::iterator Known
4018     = S.MethodPool.find(Method->getSelector());
4019   if (Known == S.MethodPool.end())
4020     return;
4021 
4022   // Retrieve the appropriate method list.
4023   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4024                                                     : Known->second.second;
4025   bool Found = false;
4026   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4027     if (!Found) {
4028       if (List->getMethod() == Method) {
4029         Found = true;
4030       } else {
4031         // Keep searching.
4032         continue;
4033       }
4034     }
4035 
4036     if (List->getNext())
4037       List->setMethod(List->getNext()->getMethod());
4038     else
4039       List->setMethod(Method);
4040   }
4041 }
4042 
4043 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4044   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4045   for (Decl *D : Names) {
4046     bool wasHidden = !D->isUnconditionallyVisible();
4047     D->setVisibleDespiteOwningModule();
4048 
4049     if (wasHidden && SemaObj) {
4050       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4051         moveMethodToBackOfGlobalList(*SemaObj, Method);
4052       }
4053     }
4054   }
4055 }
4056 
4057 void ASTReader::makeModuleVisible(Module *Mod,
4058                                   Module::NameVisibilityKind NameVisibility,
4059                                   SourceLocation ImportLoc) {
4060   llvm::SmallPtrSet<Module *, 4> Visited;
4061   SmallVector<Module *, 4> Stack;
4062   Stack.push_back(Mod);
4063   while (!Stack.empty()) {
4064     Mod = Stack.pop_back_val();
4065 
4066     if (NameVisibility <= Mod->NameVisibility) {
4067       // This module already has this level of visibility (or greater), so
4068       // there is nothing more to do.
4069       continue;
4070     }
4071 
4072     if (Mod->isUnimportable()) {
4073       // Modules that aren't importable cannot be made visible.
4074       continue;
4075     }
4076 
4077     // Update the module's name visibility.
4078     Mod->NameVisibility = NameVisibility;
4079 
4080     // If we've already deserialized any names from this module,
4081     // mark them as visible.
4082     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4083     if (Hidden != HiddenNamesMap.end()) {
4084       auto HiddenNames = std::move(*Hidden);
4085       HiddenNamesMap.erase(Hidden);
4086       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4087       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4088              "making names visible added hidden names");
4089     }
4090 
4091     // Push any exported modules onto the stack to be marked as visible.
4092     SmallVector<Module *, 16> Exports;
4093     Mod->getExportedModules(Exports);
4094     for (SmallVectorImpl<Module *>::iterator
4095            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4096       Module *Exported = *I;
4097       if (Visited.insert(Exported).second)
4098         Stack.push_back(Exported);
4099     }
4100   }
4101 }
4102 
4103 /// We've merged the definition \p MergedDef into the existing definition
4104 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4105 /// visible.
4106 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4107                                           NamedDecl *MergedDef) {
4108   if (!Def->isUnconditionallyVisible()) {
4109     // If MergedDef is visible or becomes visible, make the definition visible.
4110     if (MergedDef->isUnconditionallyVisible())
4111       Def->setVisibleDespiteOwningModule();
4112     else {
4113       getContext().mergeDefinitionIntoModule(
4114           Def, MergedDef->getImportedOwningModule(),
4115           /*NotifyListeners*/ false);
4116       PendingMergedDefinitionsToDeduplicate.insert(Def);
4117     }
4118   }
4119 }
4120 
4121 bool ASTReader::loadGlobalIndex() {
4122   if (GlobalIndex)
4123     return false;
4124 
4125   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4126       !PP.getLangOpts().Modules)
4127     return true;
4128 
4129   // Try to load the global index.
4130   TriedLoadingGlobalIndex = true;
4131   StringRef ModuleCachePath
4132     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4133   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4134       GlobalModuleIndex::readIndex(ModuleCachePath);
4135   if (llvm::Error Err = std::move(Result.second)) {
4136     assert(!Result.first);
4137     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4138     return true;
4139   }
4140 
4141   GlobalIndex.reset(Result.first);
4142   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4143   return false;
4144 }
4145 
4146 bool ASTReader::isGlobalIndexUnavailable() const {
4147   return PP.getLangOpts().Modules && UseGlobalIndex &&
4148          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4149 }
4150 
4151 static void updateModuleTimestamp(ModuleFile &MF) {
4152   // Overwrite the timestamp file contents so that file's mtime changes.
4153   std::string TimestampFilename = MF.getTimestampFilename();
4154   std::error_code EC;
4155   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4156   if (EC)
4157     return;
4158   OS << "Timestamp file\n";
4159   OS.close();
4160   OS.clear_error(); // Avoid triggering a fatal error.
4161 }
4162 
4163 /// Given a cursor at the start of an AST file, scan ahead and drop the
4164 /// cursor into the start of the given block ID, returning false on success and
4165 /// true on failure.
4166 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4167   while (true) {
4168     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4169     if (!MaybeEntry) {
4170       // FIXME this drops errors on the floor.
4171       consumeError(MaybeEntry.takeError());
4172       return true;
4173     }
4174     llvm::BitstreamEntry Entry = MaybeEntry.get();
4175 
4176     switch (Entry.Kind) {
4177     case llvm::BitstreamEntry::Error:
4178     case llvm::BitstreamEntry::EndBlock:
4179       return true;
4180 
4181     case llvm::BitstreamEntry::Record:
4182       // Ignore top-level records.
4183       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4184         break;
4185       else {
4186         // FIXME this drops errors on the floor.
4187         consumeError(Skipped.takeError());
4188         return true;
4189       }
4190 
4191     case llvm::BitstreamEntry::SubBlock:
4192       if (Entry.ID == BlockID) {
4193         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4194           // FIXME this drops the error on the floor.
4195           consumeError(std::move(Err));
4196           return true;
4197         }
4198         // Found it!
4199         return false;
4200       }
4201 
4202       if (llvm::Error Err = Cursor.SkipBlock()) {
4203         // FIXME this drops the error on the floor.
4204         consumeError(std::move(Err));
4205         return true;
4206       }
4207     }
4208   }
4209 }
4210 
4211 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4212                                             ModuleKind Type,
4213                                             SourceLocation ImportLoc,
4214                                             unsigned ClientLoadCapabilities,
4215                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4216   ++NumTryLoadModule;
4217   llvm::SaveAndRestore<SourceLocation>
4218     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4219 
4220   // Defer any pending actions until we get to the end of reading the AST file.
4221   Deserializing AnASTFile(this);
4222 
4223   // Bump the generation number.
4224   unsigned PreviousGeneration = 0;
4225   if (ContextObj)
4226     PreviousGeneration = incrementGeneration(*ContextObj);
4227 
4228   unsigned NumModules = ModuleMgr.size();
4229   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4230     assert(ReadResult && "expected to return error");
4231     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4232                             PP.getLangOpts().Modules
4233                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4234                                 : nullptr);
4235 
4236     // If we find that any modules are unusable, the global index is going
4237     // to be out-of-date. Just remove it.
4238     GlobalIndex.reset();
4239     ModuleMgr.setGlobalIndex(nullptr);
4240     return ReadResult;
4241   };
4242 
4243   SmallVector<ImportedModule, 4> Loaded;
4244   switch (ASTReadResult ReadResult =
4245               ReadASTCore(FileName, Type, ImportLoc,
4246                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4247                           ASTFileSignature(), ClientLoadCapabilities)) {
4248   case Failure:
4249   case Missing:
4250   case OutOfDate:
4251   case VersionMismatch:
4252   case ConfigurationMismatch:
4253   case HadErrors:
4254     return removeModulesAndReturn(ReadResult);
4255   case Success:
4256     break;
4257   }
4258 
4259   // Here comes stuff that we only do once the entire chain is loaded.
4260 
4261   // Load the AST blocks of all of the modules that we loaded.  We can still
4262   // hit errors parsing the ASTs at this point.
4263   for (ImportedModule &M : Loaded) {
4264     ModuleFile &F = *M.Mod;
4265 
4266     // Read the AST block.
4267     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4268       return removeModulesAndReturn(Result);
4269 
4270     // The AST block should always have a definition for the main module.
4271     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4272       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4273       return removeModulesAndReturn(Failure);
4274     }
4275 
4276     // Read the extension blocks.
4277     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4278       if (ASTReadResult Result = ReadExtensionBlock(F))
4279         return removeModulesAndReturn(Result);
4280     }
4281 
4282     // Once read, set the ModuleFile bit base offset and update the size in
4283     // bits of all files we've seen.
4284     F.GlobalBitOffset = TotalModulesSizeInBits;
4285     TotalModulesSizeInBits += F.SizeInBits;
4286     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4287   }
4288 
4289   // Preload source locations and interesting indentifiers.
4290   for (ImportedModule &M : Loaded) {
4291     ModuleFile &F = *M.Mod;
4292 
4293     // Preload SLocEntries.
4294     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4295       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4296       // Load it through the SourceManager and don't call ReadSLocEntry()
4297       // directly because the entry may have already been loaded in which case
4298       // calling ReadSLocEntry() directly would trigger an assertion in
4299       // SourceManager.
4300       SourceMgr.getLoadedSLocEntryByID(Index);
4301     }
4302 
4303     // Map the original source file ID into the ID space of the current
4304     // compilation.
4305     if (F.OriginalSourceFileID.isValid()) {
4306       F.OriginalSourceFileID = FileID::get(
4307           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4308     }
4309 
4310     // Preload all the pending interesting identifiers by marking them out of
4311     // date.
4312     for (auto Offset : F.PreloadIdentifierOffsets) {
4313       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4314           F.IdentifierTableData + Offset);
4315 
4316       ASTIdentifierLookupTrait Trait(*this, F);
4317       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4318       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4319       auto &II = PP.getIdentifierTable().getOwn(Key);
4320       II.setOutOfDate(true);
4321 
4322       // Mark this identifier as being from an AST file so that we can track
4323       // whether we need to serialize it.
4324       markIdentifierFromAST(*this, II);
4325 
4326       // Associate the ID with the identifier so that the writer can reuse it.
4327       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4328       SetIdentifierInfo(ID, &II);
4329     }
4330   }
4331 
4332   // Setup the import locations and notify the module manager that we've
4333   // committed to these module files.
4334   for (ImportedModule &M : Loaded) {
4335     ModuleFile &F = *M.Mod;
4336 
4337     ModuleMgr.moduleFileAccepted(&F);
4338 
4339     // Set the import location.
4340     F.DirectImportLoc = ImportLoc;
4341     // FIXME: We assume that locations from PCH / preamble do not need
4342     // any translation.
4343     if (!M.ImportedBy)
4344       F.ImportLoc = M.ImportLoc;
4345     else
4346       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4347   }
4348 
4349   if (!PP.getLangOpts().CPlusPlus ||
4350       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4351        Type != MK_PrebuiltModule)) {
4352     // Mark all of the identifiers in the identifier table as being out of date,
4353     // so that various accessors know to check the loaded modules when the
4354     // identifier is used.
4355     //
4356     // For C++ modules, we don't need information on many identifiers (just
4357     // those that provide macros or are poisoned), so we mark all of
4358     // the interesting ones via PreloadIdentifierOffsets.
4359     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4360                                 IdEnd = PP.getIdentifierTable().end();
4361          Id != IdEnd; ++Id)
4362       Id->second->setOutOfDate(true);
4363   }
4364   // Mark selectors as out of date.
4365   for (auto Sel : SelectorGeneration)
4366     SelectorOutOfDate[Sel.first] = true;
4367 
4368   // Resolve any unresolved module exports.
4369   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4370     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4371     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4372     Module *ResolvedMod = getSubmodule(GlobalID);
4373 
4374     switch (Unresolved.Kind) {
4375     case UnresolvedModuleRef::Conflict:
4376       if (ResolvedMod) {
4377         Module::Conflict Conflict;
4378         Conflict.Other = ResolvedMod;
4379         Conflict.Message = Unresolved.String.str();
4380         Unresolved.Mod->Conflicts.push_back(Conflict);
4381       }
4382       continue;
4383 
4384     case UnresolvedModuleRef::Import:
4385       if (ResolvedMod)
4386         Unresolved.Mod->Imports.insert(ResolvedMod);
4387       continue;
4388 
4389     case UnresolvedModuleRef::Export:
4390       if (ResolvedMod || Unresolved.IsWildcard)
4391         Unresolved.Mod->Exports.push_back(
4392           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4393       continue;
4394     }
4395   }
4396   UnresolvedModuleRefs.clear();
4397 
4398   if (Imported)
4399     Imported->append(ImportedModules.begin(),
4400                      ImportedModules.end());
4401 
4402   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4403   // Might be unnecessary as use declarations are only used to build the
4404   // module itself.
4405 
4406   if (ContextObj)
4407     InitializeContext();
4408 
4409   if (SemaObj)
4410     UpdateSema();
4411 
4412   if (DeserializationListener)
4413     DeserializationListener->ReaderInitialized(this);
4414 
4415   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4416   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4417     // If this AST file is a precompiled preamble, then set the
4418     // preamble file ID of the source manager to the file source file
4419     // from which the preamble was built.
4420     if (Type == MK_Preamble) {
4421       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4422     } else if (Type == MK_MainFile) {
4423       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4424     }
4425   }
4426 
4427   // For any Objective-C class definitions we have already loaded, make sure
4428   // that we load any additional categories.
4429   if (ContextObj) {
4430     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4431       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4432                          ObjCClassesLoaded[I],
4433                          PreviousGeneration);
4434     }
4435   }
4436 
4437   if (PP.getHeaderSearchInfo()
4438           .getHeaderSearchOpts()
4439           .ModulesValidateOncePerBuildSession) {
4440     // Now we are certain that the module and all modules it depends on are
4441     // up to date.  Create or update timestamp files for modules that are
4442     // located in the module cache (not for PCH files that could be anywhere
4443     // in the filesystem).
4444     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4445       ImportedModule &M = Loaded[I];
4446       if (M.Mod->Kind == MK_ImplicitModule) {
4447         updateModuleTimestamp(*M.Mod);
4448       }
4449     }
4450   }
4451 
4452   return Success;
4453 }
4454 
4455 static ASTFileSignature readASTFileSignature(StringRef PCH);
4456 
4457 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4458 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4459   // FIXME checking magic headers is done in other places such as
4460   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4461   // always done the same. Unify it all with a helper.
4462   if (!Stream.canSkipToPos(4))
4463     return llvm::createStringError(std::errc::illegal_byte_sequence,
4464                                    "file too small to contain AST file magic");
4465   for (unsigned C : {'C', 'P', 'C', 'H'})
4466     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4467       if (Res.get() != C)
4468         return llvm::createStringError(
4469             std::errc::illegal_byte_sequence,
4470             "file doesn't start with AST file magic");
4471     } else
4472       return Res.takeError();
4473   return llvm::Error::success();
4474 }
4475 
4476 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4477   switch (Kind) {
4478   case MK_PCH:
4479     return 0; // PCH
4480   case MK_ImplicitModule:
4481   case MK_ExplicitModule:
4482   case MK_PrebuiltModule:
4483     return 1; // module
4484   case MK_MainFile:
4485   case MK_Preamble:
4486     return 2; // main source file
4487   }
4488   llvm_unreachable("unknown module kind");
4489 }
4490 
4491 ASTReader::ASTReadResult
4492 ASTReader::ReadASTCore(StringRef FileName,
4493                        ModuleKind Type,
4494                        SourceLocation ImportLoc,
4495                        ModuleFile *ImportedBy,
4496                        SmallVectorImpl<ImportedModule> &Loaded,
4497                        off_t ExpectedSize, time_t ExpectedModTime,
4498                        ASTFileSignature ExpectedSignature,
4499                        unsigned ClientLoadCapabilities) {
4500   ModuleFile *M;
4501   std::string ErrorStr;
4502   ModuleManager::AddModuleResult AddResult
4503     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4504                           getGeneration(), ExpectedSize, ExpectedModTime,
4505                           ExpectedSignature, readASTFileSignature,
4506                           M, ErrorStr);
4507 
4508   switch (AddResult) {
4509   case ModuleManager::AlreadyLoaded:
4510     Diag(diag::remark_module_import)
4511         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4512         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4513     return Success;
4514 
4515   case ModuleManager::NewlyLoaded:
4516     // Load module file below.
4517     break;
4518 
4519   case ModuleManager::Missing:
4520     // The module file was missing; if the client can handle that, return
4521     // it.
4522     if (ClientLoadCapabilities & ARR_Missing)
4523       return Missing;
4524 
4525     // Otherwise, return an error.
4526     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4527                                           << FileName << !ErrorStr.empty()
4528                                           << ErrorStr;
4529     return Failure;
4530 
4531   case ModuleManager::OutOfDate:
4532     // We couldn't load the module file because it is out-of-date. If the
4533     // client can handle out-of-date, return it.
4534     if (ClientLoadCapabilities & ARR_OutOfDate)
4535       return OutOfDate;
4536 
4537     // Otherwise, return an error.
4538     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4539                                             << FileName << !ErrorStr.empty()
4540                                             << ErrorStr;
4541     return Failure;
4542   }
4543 
4544   assert(M && "Missing module file");
4545 
4546   bool ShouldFinalizePCM = false;
4547   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4548     auto &MC = getModuleManager().getModuleCache();
4549     if (ShouldFinalizePCM)
4550       MC.finalizePCM(FileName);
4551     else
4552       MC.tryToDropPCM(FileName);
4553   });
4554   ModuleFile &F = *M;
4555   BitstreamCursor &Stream = F.Stream;
4556   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4557   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4558 
4559   // Sniff for the signature.
4560   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4561     Diag(diag::err_module_file_invalid)
4562         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4563     return Failure;
4564   }
4565 
4566   ++NumReadASTCore;
4567   // This is used for compatibility with older PCH formats.
4568   bool HaveReadControlBlock = false;
4569   while (true) {
4570     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4571     if (!MaybeEntry) {
4572       Error(MaybeEntry.takeError());
4573       return Failure;
4574     }
4575     llvm::BitstreamEntry Entry = MaybeEntry.get();
4576 
4577     switch (Entry.Kind) {
4578     case llvm::BitstreamEntry::Error:
4579     case llvm::BitstreamEntry::Record:
4580     case llvm::BitstreamEntry::EndBlock:
4581       Error("invalid record at top-level of AST file");
4582       return Failure;
4583 
4584     case llvm::BitstreamEntry::SubBlock:
4585       break;
4586     }
4587 
4588     switch (Entry.ID) {
4589     case CONTROL_BLOCK_ID:
4590       HaveReadControlBlock = true;
4591       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4592       case Success:
4593         // Check that we didn't try to load a non-module AST file as a module.
4594         //
4595         // FIXME: Should we also perform the converse check? Loading a module as
4596         // a PCH file sort of works, but it's a bit wonky.
4597         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4598              Type == MK_PrebuiltModule) &&
4599             F.ModuleName.empty()) {
4600           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4601           if (Result != OutOfDate ||
4602               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4603             Diag(diag::err_module_file_not_module) << FileName;
4604           return Result;
4605         }
4606         break;
4607 
4608       case Failure: return Failure;
4609       case Missing: return Missing;
4610       case OutOfDate: return OutOfDate;
4611       case VersionMismatch: return VersionMismatch;
4612       case ConfigurationMismatch: return ConfigurationMismatch;
4613       case HadErrors: return HadErrors;
4614       }
4615       break;
4616 
4617     case AST_BLOCK_ID:
4618       if (!HaveReadControlBlock) {
4619         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4620           Diag(diag::err_pch_version_too_old);
4621         return VersionMismatch;
4622       }
4623 
4624       // Record that we've loaded this module.
4625       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4626       ShouldFinalizePCM = true;
4627       return Success;
4628 
4629     case UNHASHED_CONTROL_BLOCK_ID:
4630       // This block is handled using look-ahead during ReadControlBlock.  We
4631       // shouldn't get here!
4632       Error("malformed block record in AST file");
4633       return Failure;
4634 
4635     default:
4636       if (llvm::Error Err = Stream.SkipBlock()) {
4637         Error(std::move(Err));
4638         return Failure;
4639       }
4640       break;
4641     }
4642   }
4643 
4644   llvm_unreachable("unexpected break; expected return");
4645 }
4646 
4647 ASTReader::ASTReadResult
4648 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4649                                     unsigned ClientLoadCapabilities) {
4650   const HeaderSearchOptions &HSOpts =
4651       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4652   bool AllowCompatibleConfigurationMismatch =
4653       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4654 
4655   ASTReadResult Result = readUnhashedControlBlockImpl(
4656       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4657       Listener.get(),
4658       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4659 
4660   // If F was directly imported by another module, it's implicitly validated by
4661   // the importing module.
4662   if (DisableValidation || WasImportedBy ||
4663       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4664     return Success;
4665 
4666   if (Result == Failure) {
4667     Error("malformed block record in AST file");
4668     return Failure;
4669   }
4670 
4671   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4672     // If this module has already been finalized in the ModuleCache, we're stuck
4673     // with it; we can only load a single version of each module.
4674     //
4675     // This can happen when a module is imported in two contexts: in one, as a
4676     // user module; in another, as a system module (due to an import from
4677     // another module marked with the [system] flag).  It usually indicates a
4678     // bug in the module map: this module should also be marked with [system].
4679     //
4680     // If -Wno-system-headers (the default), and the first import is as a
4681     // system module, then validation will fail during the as-user import,
4682     // since -Werror flags won't have been validated.  However, it's reasonable
4683     // to treat this consistently as a system module.
4684     //
4685     // If -Wsystem-headers, the PCM on disk was built with
4686     // -Wno-system-headers, and the first import is as a user module, then
4687     // validation will fail during the as-system import since the PCM on disk
4688     // doesn't guarantee that -Werror was respected.  However, the -Werror
4689     // flags were checked during the initial as-user import.
4690     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4691       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4692       return Success;
4693     }
4694   }
4695 
4696   return Result;
4697 }
4698 
4699 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4700     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4701     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4702     bool ValidateDiagnosticOptions) {
4703   // Initialize a stream.
4704   BitstreamCursor Stream(StreamData);
4705 
4706   // Sniff for the signature.
4707   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4708     // FIXME this drops the error on the floor.
4709     consumeError(std::move(Err));
4710     return Failure;
4711   }
4712 
4713   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4714   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4715     return Failure;
4716 
4717   // Read all of the records in the options block.
4718   RecordData Record;
4719   ASTReadResult Result = Success;
4720   while (true) {
4721     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4722     if (!MaybeEntry) {
4723       // FIXME this drops the error on the floor.
4724       consumeError(MaybeEntry.takeError());
4725       return Failure;
4726     }
4727     llvm::BitstreamEntry Entry = MaybeEntry.get();
4728 
4729     switch (Entry.Kind) {
4730     case llvm::BitstreamEntry::Error:
4731     case llvm::BitstreamEntry::SubBlock:
4732       return Failure;
4733 
4734     case llvm::BitstreamEntry::EndBlock:
4735       return Result;
4736 
4737     case llvm::BitstreamEntry::Record:
4738       // The interesting case.
4739       break;
4740     }
4741 
4742     // Read and process a record.
4743     Record.clear();
4744     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4745     if (!MaybeRecordType) {
4746       // FIXME this drops the error.
4747       return Failure;
4748     }
4749     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4750     case SIGNATURE:
4751       if (F)
4752         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4753       break;
4754     case AST_BLOCK_HASH:
4755       if (F)
4756         F->ASTBlockHash =
4757             ASTFileSignature::create(Record.begin(), Record.end());
4758       break;
4759     case DIAGNOSTIC_OPTIONS: {
4760       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4761       if (Listener && ValidateDiagnosticOptions &&
4762           !AllowCompatibleConfigurationMismatch &&
4763           ParseDiagnosticOptions(Record, Complain, *Listener))
4764         Result = OutOfDate; // Don't return early.  Read the signature.
4765       break;
4766     }
4767     case DIAG_PRAGMA_MAPPINGS:
4768       if (!F)
4769         break;
4770       if (F->PragmaDiagMappings.empty())
4771         F->PragmaDiagMappings.swap(Record);
4772       else
4773         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4774                                      Record.begin(), Record.end());
4775       break;
4776     }
4777   }
4778 }
4779 
4780 /// Parse a record and blob containing module file extension metadata.
4781 static bool parseModuleFileExtensionMetadata(
4782               const SmallVectorImpl<uint64_t> &Record,
4783               StringRef Blob,
4784               ModuleFileExtensionMetadata &Metadata) {
4785   if (Record.size() < 4) return true;
4786 
4787   Metadata.MajorVersion = Record[0];
4788   Metadata.MinorVersion = Record[1];
4789 
4790   unsigned BlockNameLen = Record[2];
4791   unsigned UserInfoLen = Record[3];
4792 
4793   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4794 
4795   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4796   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4797                                   Blob.data() + BlockNameLen + UserInfoLen);
4798   return false;
4799 }
4800 
4801 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4802   BitstreamCursor &Stream = F.Stream;
4803 
4804   RecordData Record;
4805   while (true) {
4806     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4807     if (!MaybeEntry) {
4808       Error(MaybeEntry.takeError());
4809       return Failure;
4810     }
4811     llvm::BitstreamEntry Entry = MaybeEntry.get();
4812 
4813     switch (Entry.Kind) {
4814     case llvm::BitstreamEntry::SubBlock:
4815       if (llvm::Error Err = Stream.SkipBlock()) {
4816         Error(std::move(Err));
4817         return Failure;
4818       }
4819       continue;
4820 
4821     case llvm::BitstreamEntry::EndBlock:
4822       return Success;
4823 
4824     case llvm::BitstreamEntry::Error:
4825       return HadErrors;
4826 
4827     case llvm::BitstreamEntry::Record:
4828       break;
4829     }
4830 
4831     Record.clear();
4832     StringRef Blob;
4833     Expected<unsigned> MaybeRecCode =
4834         Stream.readRecord(Entry.ID, Record, &Blob);
4835     if (!MaybeRecCode) {
4836       Error(MaybeRecCode.takeError());
4837       return Failure;
4838     }
4839     switch (MaybeRecCode.get()) {
4840     case EXTENSION_METADATA: {
4841       ModuleFileExtensionMetadata Metadata;
4842       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4843         Error("malformed EXTENSION_METADATA in AST file");
4844         return Failure;
4845       }
4846 
4847       // Find a module file extension with this block name.
4848       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4849       if (Known == ModuleFileExtensions.end()) break;
4850 
4851       // Form a reader.
4852       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4853                                                              F, Stream)) {
4854         F.ExtensionReaders.push_back(std::move(Reader));
4855       }
4856 
4857       break;
4858     }
4859     }
4860   }
4861 
4862   return Success;
4863 }
4864 
4865 void ASTReader::InitializeContext() {
4866   assert(ContextObj && "no context to initialize");
4867   ASTContext &Context = *ContextObj;
4868 
4869   // If there's a listener, notify them that we "read" the translation unit.
4870   if (DeserializationListener)
4871     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4872                                       Context.getTranslationUnitDecl());
4873 
4874   // FIXME: Find a better way to deal with collisions between these
4875   // built-in types. Right now, we just ignore the problem.
4876 
4877   // Load the special types.
4878   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4879     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4880       if (!Context.CFConstantStringTypeDecl)
4881         Context.setCFConstantStringType(GetType(String));
4882     }
4883 
4884     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4885       QualType FileType = GetType(File);
4886       if (FileType.isNull()) {
4887         Error("FILE type is NULL");
4888         return;
4889       }
4890 
4891       if (!Context.FILEDecl) {
4892         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4893           Context.setFILEDecl(Typedef->getDecl());
4894         else {
4895           const TagType *Tag = FileType->getAs<TagType>();
4896           if (!Tag) {
4897             Error("Invalid FILE type in AST file");
4898             return;
4899           }
4900           Context.setFILEDecl(Tag->getDecl());
4901         }
4902       }
4903     }
4904 
4905     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4906       QualType Jmp_bufType = GetType(Jmp_buf);
4907       if (Jmp_bufType.isNull()) {
4908         Error("jmp_buf type is NULL");
4909         return;
4910       }
4911 
4912       if (!Context.jmp_bufDecl) {
4913         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4914           Context.setjmp_bufDecl(Typedef->getDecl());
4915         else {
4916           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4917           if (!Tag) {
4918             Error("Invalid jmp_buf type in AST file");
4919             return;
4920           }
4921           Context.setjmp_bufDecl(Tag->getDecl());
4922         }
4923       }
4924     }
4925 
4926     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4927       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4928       if (Sigjmp_bufType.isNull()) {
4929         Error("sigjmp_buf type is NULL");
4930         return;
4931       }
4932 
4933       if (!Context.sigjmp_bufDecl) {
4934         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4935           Context.setsigjmp_bufDecl(Typedef->getDecl());
4936         else {
4937           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4938           assert(Tag && "Invalid sigjmp_buf type in AST file");
4939           Context.setsigjmp_bufDecl(Tag->getDecl());
4940         }
4941       }
4942     }
4943 
4944     if (unsigned ObjCIdRedef
4945           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4946       if (Context.ObjCIdRedefinitionType.isNull())
4947         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4948     }
4949 
4950     if (unsigned ObjCClassRedef
4951           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4952       if (Context.ObjCClassRedefinitionType.isNull())
4953         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4954     }
4955 
4956     if (unsigned ObjCSelRedef
4957           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4958       if (Context.ObjCSelRedefinitionType.isNull())
4959         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4960     }
4961 
4962     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4963       QualType Ucontext_tType = GetType(Ucontext_t);
4964       if (Ucontext_tType.isNull()) {
4965         Error("ucontext_t type is NULL");
4966         return;
4967       }
4968 
4969       if (!Context.ucontext_tDecl) {
4970         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4971           Context.setucontext_tDecl(Typedef->getDecl());
4972         else {
4973           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4974           assert(Tag && "Invalid ucontext_t type in AST file");
4975           Context.setucontext_tDecl(Tag->getDecl());
4976         }
4977       }
4978     }
4979   }
4980 
4981   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4982 
4983   // If there were any CUDA special declarations, deserialize them.
4984   if (!CUDASpecialDeclRefs.empty()) {
4985     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4986     Context.setcudaConfigureCallDecl(
4987                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4988   }
4989 
4990   // Re-export any modules that were imported by a non-module AST file.
4991   // FIXME: This does not make macro-only imports visible again.
4992   for (auto &Import : ImportedModules) {
4993     if (Module *Imported = getSubmodule(Import.ID)) {
4994       makeModuleVisible(Imported, Module::AllVisible,
4995                         /*ImportLoc=*/Import.ImportLoc);
4996       if (Import.ImportLoc.isValid())
4997         PP.makeModuleVisible(Imported, Import.ImportLoc);
4998       // This updates visibility for Preprocessor only. For Sema, which can be
4999       // nullptr here, we do the same later, in UpdateSema().
5000     }
5001   }
5002 }
5003 
5004 void ASTReader::finalizeForWriting() {
5005   // Nothing to do for now.
5006 }
5007 
5008 /// Reads and return the signature record from \p PCH's control block, or
5009 /// else returns 0.
5010 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5011   BitstreamCursor Stream(PCH);
5012   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5013     // FIXME this drops the error on the floor.
5014     consumeError(std::move(Err));
5015     return ASTFileSignature();
5016   }
5017 
5018   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5019   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5020     return ASTFileSignature();
5021 
5022   // Scan for SIGNATURE inside the diagnostic options block.
5023   ASTReader::RecordData Record;
5024   while (true) {
5025     Expected<llvm::BitstreamEntry> MaybeEntry =
5026         Stream.advanceSkippingSubblocks();
5027     if (!MaybeEntry) {
5028       // FIXME this drops the error on the floor.
5029       consumeError(MaybeEntry.takeError());
5030       return ASTFileSignature();
5031     }
5032     llvm::BitstreamEntry Entry = MaybeEntry.get();
5033 
5034     if (Entry.Kind != llvm::BitstreamEntry::Record)
5035       return ASTFileSignature();
5036 
5037     Record.clear();
5038     StringRef Blob;
5039     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5040     if (!MaybeRecord) {
5041       // FIXME this drops the error on the floor.
5042       consumeError(MaybeRecord.takeError());
5043       return ASTFileSignature();
5044     }
5045     if (SIGNATURE == MaybeRecord.get())
5046       return ASTFileSignature::create(Record.begin(),
5047                                       Record.begin() + ASTFileSignature::size);
5048   }
5049 }
5050 
5051 /// Retrieve the name of the original source file name
5052 /// directly from the AST file, without actually loading the AST
5053 /// file.
5054 std::string ASTReader::getOriginalSourceFile(
5055     const std::string &ASTFileName, FileManager &FileMgr,
5056     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5057   // Open the AST file.
5058   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5059   if (!Buffer) {
5060     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5061         << ASTFileName << Buffer.getError().message();
5062     return std::string();
5063   }
5064 
5065   // Initialize the stream
5066   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5067 
5068   // Sniff for the signature.
5069   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5070     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5071     return std::string();
5072   }
5073 
5074   // Scan for the CONTROL_BLOCK_ID block.
5075   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5076     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5077     return std::string();
5078   }
5079 
5080   // Scan for ORIGINAL_FILE inside the control block.
5081   RecordData Record;
5082   while (true) {
5083     Expected<llvm::BitstreamEntry> MaybeEntry =
5084         Stream.advanceSkippingSubblocks();
5085     if (!MaybeEntry) {
5086       // FIXME this drops errors on the floor.
5087       consumeError(MaybeEntry.takeError());
5088       return std::string();
5089     }
5090     llvm::BitstreamEntry Entry = MaybeEntry.get();
5091 
5092     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5093       return std::string();
5094 
5095     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5096       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5097       return std::string();
5098     }
5099 
5100     Record.clear();
5101     StringRef Blob;
5102     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5103     if (!MaybeRecord) {
5104       // FIXME this drops the errors on the floor.
5105       consumeError(MaybeRecord.takeError());
5106       return std::string();
5107     }
5108     if (ORIGINAL_FILE == MaybeRecord.get())
5109       return Blob.str();
5110   }
5111 }
5112 
5113 namespace {
5114 
5115   class SimplePCHValidator : public ASTReaderListener {
5116     const LangOptions &ExistingLangOpts;
5117     const TargetOptions &ExistingTargetOpts;
5118     const PreprocessorOptions &ExistingPPOpts;
5119     std::string ExistingModuleCachePath;
5120     FileManager &FileMgr;
5121 
5122   public:
5123     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5124                        const TargetOptions &ExistingTargetOpts,
5125                        const PreprocessorOptions &ExistingPPOpts,
5126                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5127         : ExistingLangOpts(ExistingLangOpts),
5128           ExistingTargetOpts(ExistingTargetOpts),
5129           ExistingPPOpts(ExistingPPOpts),
5130           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5131 
5132     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5133                              bool AllowCompatibleDifferences) override {
5134       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5135                                   AllowCompatibleDifferences);
5136     }
5137 
5138     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5139                            bool AllowCompatibleDifferences) override {
5140       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5141                                 AllowCompatibleDifferences);
5142     }
5143 
5144     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5145                                  StringRef SpecificModuleCachePath,
5146                                  bool Complain) override {
5147       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5148                                       ExistingModuleCachePath,
5149                                       nullptr, ExistingLangOpts);
5150     }
5151 
5152     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5153                                  bool Complain,
5154                                  std::string &SuggestedPredefines) override {
5155       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5156                                       SuggestedPredefines, ExistingLangOpts);
5157     }
5158   };
5159 
5160 } // namespace
5161 
5162 bool ASTReader::readASTFileControlBlock(
5163     StringRef Filename, FileManager &FileMgr,
5164     const PCHContainerReader &PCHContainerRdr,
5165     bool FindModuleFileExtensions,
5166     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5167   // Open the AST file.
5168   // FIXME: This allows use of the VFS; we do not allow use of the
5169   // VFS when actually loading a module.
5170   auto Buffer = FileMgr.getBufferForFile(Filename);
5171   if (!Buffer) {
5172     return true;
5173   }
5174 
5175   // Initialize the stream
5176   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5177   BitstreamCursor Stream(Bytes);
5178 
5179   // Sniff for the signature.
5180   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5181     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5182     return true;
5183   }
5184 
5185   // Scan for the CONTROL_BLOCK_ID block.
5186   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5187     return true;
5188 
5189   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5190   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5191   bool NeedsImports = Listener.needsImportVisitation();
5192   BitstreamCursor InputFilesCursor;
5193 
5194   RecordData Record;
5195   std::string ModuleDir;
5196   bool DoneWithControlBlock = false;
5197   while (!DoneWithControlBlock) {
5198     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5199     if (!MaybeEntry) {
5200       // FIXME this drops the error on the floor.
5201       consumeError(MaybeEntry.takeError());
5202       return true;
5203     }
5204     llvm::BitstreamEntry Entry = MaybeEntry.get();
5205 
5206     switch (Entry.Kind) {
5207     case llvm::BitstreamEntry::SubBlock: {
5208       switch (Entry.ID) {
5209       case OPTIONS_BLOCK_ID: {
5210         std::string IgnoredSuggestedPredefines;
5211         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5212                              /*AllowCompatibleConfigurationMismatch*/ false,
5213                              Listener, IgnoredSuggestedPredefines) != Success)
5214           return true;
5215         break;
5216       }
5217 
5218       case INPUT_FILES_BLOCK_ID:
5219         InputFilesCursor = Stream;
5220         if (llvm::Error Err = Stream.SkipBlock()) {
5221           // FIXME this drops the error on the floor.
5222           consumeError(std::move(Err));
5223           return true;
5224         }
5225         if (NeedsInputFiles &&
5226             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5227           return true;
5228         break;
5229 
5230       default:
5231         if (llvm::Error Err = Stream.SkipBlock()) {
5232           // FIXME this drops the error on the floor.
5233           consumeError(std::move(Err));
5234           return true;
5235         }
5236         break;
5237       }
5238 
5239       continue;
5240     }
5241 
5242     case llvm::BitstreamEntry::EndBlock:
5243       DoneWithControlBlock = true;
5244       break;
5245 
5246     case llvm::BitstreamEntry::Error:
5247       return true;
5248 
5249     case llvm::BitstreamEntry::Record:
5250       break;
5251     }
5252 
5253     if (DoneWithControlBlock) break;
5254 
5255     Record.clear();
5256     StringRef Blob;
5257     Expected<unsigned> MaybeRecCode =
5258         Stream.readRecord(Entry.ID, Record, &Blob);
5259     if (!MaybeRecCode) {
5260       // FIXME this drops the error.
5261       return Failure;
5262     }
5263     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5264     case METADATA:
5265       if (Record[0] != VERSION_MAJOR)
5266         return true;
5267       if (Listener.ReadFullVersionInformation(Blob))
5268         return true;
5269       break;
5270     case MODULE_NAME:
5271       Listener.ReadModuleName(Blob);
5272       break;
5273     case MODULE_DIRECTORY:
5274       ModuleDir = std::string(Blob);
5275       break;
5276     case MODULE_MAP_FILE: {
5277       unsigned Idx = 0;
5278       auto Path = ReadString(Record, Idx);
5279       ResolveImportedPath(Path, ModuleDir);
5280       Listener.ReadModuleMapFile(Path);
5281       break;
5282     }
5283     case INPUT_FILE_OFFSETS: {
5284       if (!NeedsInputFiles)
5285         break;
5286 
5287       unsigned NumInputFiles = Record[0];
5288       unsigned NumUserFiles = Record[1];
5289       const llvm::support::unaligned_uint64_t *InputFileOffs =
5290           (const llvm::support::unaligned_uint64_t *)Blob.data();
5291       for (unsigned I = 0; I != NumInputFiles; ++I) {
5292         // Go find this input file.
5293         bool isSystemFile = I >= NumUserFiles;
5294 
5295         if (isSystemFile && !NeedsSystemInputFiles)
5296           break; // the rest are system input files
5297 
5298         BitstreamCursor &Cursor = InputFilesCursor;
5299         SavedStreamPosition SavedPosition(Cursor);
5300         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5301           // FIXME this drops errors on the floor.
5302           consumeError(std::move(Err));
5303         }
5304 
5305         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5306         if (!MaybeCode) {
5307           // FIXME this drops errors on the floor.
5308           consumeError(MaybeCode.takeError());
5309         }
5310         unsigned Code = MaybeCode.get();
5311 
5312         RecordData Record;
5313         StringRef Blob;
5314         bool shouldContinue = false;
5315         Expected<unsigned> MaybeRecordType =
5316             Cursor.readRecord(Code, Record, &Blob);
5317         if (!MaybeRecordType) {
5318           // FIXME this drops errors on the floor.
5319           consumeError(MaybeRecordType.takeError());
5320         }
5321         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5322         case INPUT_FILE_HASH:
5323           break;
5324         case INPUT_FILE:
5325           bool Overridden = static_cast<bool>(Record[3]);
5326           std::string Filename = std::string(Blob);
5327           ResolveImportedPath(Filename, ModuleDir);
5328           shouldContinue = Listener.visitInputFile(
5329               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5330           break;
5331         }
5332         if (!shouldContinue)
5333           break;
5334       }
5335       break;
5336     }
5337 
5338     case IMPORTS: {
5339       if (!NeedsImports)
5340         break;
5341 
5342       unsigned Idx = 0, N = Record.size();
5343       while (Idx < N) {
5344         // Read information about the AST file.
5345         Idx +=
5346             1 + 1 + 1 + 1 +
5347             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5348         std::string ModuleName = ReadString(Record, Idx);
5349         std::string Filename = ReadString(Record, Idx);
5350         ResolveImportedPath(Filename, ModuleDir);
5351         Listener.visitImport(ModuleName, Filename);
5352       }
5353       break;
5354     }
5355 
5356     default:
5357       // No other validation to perform.
5358       break;
5359     }
5360   }
5361 
5362   // Look for module file extension blocks, if requested.
5363   if (FindModuleFileExtensions) {
5364     BitstreamCursor SavedStream = Stream;
5365     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5366       bool DoneWithExtensionBlock = false;
5367       while (!DoneWithExtensionBlock) {
5368         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5369         if (!MaybeEntry) {
5370           // FIXME this drops the error.
5371           return true;
5372         }
5373         llvm::BitstreamEntry Entry = MaybeEntry.get();
5374 
5375         switch (Entry.Kind) {
5376         case llvm::BitstreamEntry::SubBlock:
5377           if (llvm::Error Err = Stream.SkipBlock()) {
5378             // FIXME this drops the error on the floor.
5379             consumeError(std::move(Err));
5380             return true;
5381           }
5382           continue;
5383 
5384         case llvm::BitstreamEntry::EndBlock:
5385           DoneWithExtensionBlock = true;
5386           continue;
5387 
5388         case llvm::BitstreamEntry::Error:
5389           return true;
5390 
5391         case llvm::BitstreamEntry::Record:
5392           break;
5393         }
5394 
5395        Record.clear();
5396        StringRef Blob;
5397        Expected<unsigned> MaybeRecCode =
5398            Stream.readRecord(Entry.ID, Record, &Blob);
5399        if (!MaybeRecCode) {
5400          // FIXME this drops the error.
5401          return true;
5402        }
5403        switch (MaybeRecCode.get()) {
5404        case EXTENSION_METADATA: {
5405          ModuleFileExtensionMetadata Metadata;
5406          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5407            return true;
5408 
5409          Listener.readModuleFileExtension(Metadata);
5410          break;
5411        }
5412        }
5413       }
5414     }
5415     Stream = SavedStream;
5416   }
5417 
5418   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5419   if (readUnhashedControlBlockImpl(
5420           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5421           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5422           ValidateDiagnosticOptions) != Success)
5423     return true;
5424 
5425   return false;
5426 }
5427 
5428 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5429                                     const PCHContainerReader &PCHContainerRdr,
5430                                     const LangOptions &LangOpts,
5431                                     const TargetOptions &TargetOpts,
5432                                     const PreprocessorOptions &PPOpts,
5433                                     StringRef ExistingModuleCachePath) {
5434   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5435                                ExistingModuleCachePath, FileMgr);
5436   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5437                                   /*FindModuleFileExtensions=*/false,
5438                                   validator,
5439                                   /*ValidateDiagnosticOptions=*/true);
5440 }
5441 
5442 ASTReader::ASTReadResult
5443 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5444   // Enter the submodule block.
5445   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5446     Error(std::move(Err));
5447     return Failure;
5448   }
5449 
5450   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5451   bool First = true;
5452   Module *CurrentModule = nullptr;
5453   RecordData Record;
5454   while (true) {
5455     Expected<llvm::BitstreamEntry> MaybeEntry =
5456         F.Stream.advanceSkippingSubblocks();
5457     if (!MaybeEntry) {
5458       Error(MaybeEntry.takeError());
5459       return Failure;
5460     }
5461     llvm::BitstreamEntry Entry = MaybeEntry.get();
5462 
5463     switch (Entry.Kind) {
5464     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5465     case llvm::BitstreamEntry::Error:
5466       Error("malformed block record in AST file");
5467       return Failure;
5468     case llvm::BitstreamEntry::EndBlock:
5469       return Success;
5470     case llvm::BitstreamEntry::Record:
5471       // The interesting case.
5472       break;
5473     }
5474 
5475     // Read a record.
5476     StringRef Blob;
5477     Record.clear();
5478     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5479     if (!MaybeKind) {
5480       Error(MaybeKind.takeError());
5481       return Failure;
5482     }
5483     unsigned Kind = MaybeKind.get();
5484 
5485     if ((Kind == SUBMODULE_METADATA) != First) {
5486       Error("submodule metadata record should be at beginning of block");
5487       return Failure;
5488     }
5489     First = false;
5490 
5491     // Submodule information is only valid if we have a current module.
5492     // FIXME: Should we error on these cases?
5493     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5494         Kind != SUBMODULE_DEFINITION)
5495       continue;
5496 
5497     switch (Kind) {
5498     default:  // Default behavior: ignore.
5499       break;
5500 
5501     case SUBMODULE_DEFINITION: {
5502       if (Record.size() < 12) {
5503         Error("malformed module definition");
5504         return Failure;
5505       }
5506 
5507       StringRef Name = Blob;
5508       unsigned Idx = 0;
5509       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5510       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5511       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5512       bool IsFramework = Record[Idx++];
5513       bool IsExplicit = Record[Idx++];
5514       bool IsSystem = Record[Idx++];
5515       bool IsExternC = Record[Idx++];
5516       bool InferSubmodules = Record[Idx++];
5517       bool InferExplicitSubmodules = Record[Idx++];
5518       bool InferExportWildcard = Record[Idx++];
5519       bool ConfigMacrosExhaustive = Record[Idx++];
5520       bool ModuleMapIsPrivate = Record[Idx++];
5521 
5522       Module *ParentModule = nullptr;
5523       if (Parent)
5524         ParentModule = getSubmodule(Parent);
5525 
5526       // Retrieve this (sub)module from the module map, creating it if
5527       // necessary.
5528       CurrentModule =
5529           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5530               .first;
5531 
5532       // FIXME: set the definition loc for CurrentModule, or call
5533       // ModMap.setInferredModuleAllowedBy()
5534 
5535       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5536       if (GlobalIndex >= SubmodulesLoaded.size() ||
5537           SubmodulesLoaded[GlobalIndex]) {
5538         Error("too many submodules");
5539         return Failure;
5540       }
5541 
5542       if (!ParentModule) {
5543         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5544           // Don't emit module relocation error if we have -fno-validate-pch
5545           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5546               CurFile != F.File) {
5547             Error(diag::err_module_file_conflict,
5548                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5549                   F.File->getName());
5550             return Failure;
5551           }
5552         }
5553 
5554         F.DidReadTopLevelSubmodule = true;
5555         CurrentModule->setASTFile(F.File);
5556         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5557       }
5558 
5559       CurrentModule->Kind = Kind;
5560       CurrentModule->Signature = F.Signature;
5561       CurrentModule->IsFromModuleFile = true;
5562       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5563       CurrentModule->IsExternC = IsExternC;
5564       CurrentModule->InferSubmodules = InferSubmodules;
5565       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5566       CurrentModule->InferExportWildcard = InferExportWildcard;
5567       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5568       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5569       if (DeserializationListener)
5570         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5571 
5572       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5573 
5574       // Clear out data that will be replaced by what is in the module file.
5575       CurrentModule->LinkLibraries.clear();
5576       CurrentModule->ConfigMacros.clear();
5577       CurrentModule->UnresolvedConflicts.clear();
5578       CurrentModule->Conflicts.clear();
5579 
5580       // The module is available unless it's missing a requirement; relevant
5581       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5582       // Missing headers that were present when the module was built do not
5583       // make it unavailable -- if we got this far, this must be an explicitly
5584       // imported module file.
5585       CurrentModule->Requirements.clear();
5586       CurrentModule->MissingHeaders.clear();
5587       CurrentModule->IsUnimportable =
5588           ParentModule && ParentModule->IsUnimportable;
5589       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5590       break;
5591     }
5592 
5593     case SUBMODULE_UMBRELLA_HEADER: {
5594       std::string Filename = std::string(Blob);
5595       ResolveImportedPath(F, Filename);
5596       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5597         if (!CurrentModule->getUmbrellaHeader())
5598           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5599         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5600           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5601             Error("mismatched umbrella headers in submodule");
5602           return OutOfDate;
5603         }
5604       }
5605       break;
5606     }
5607 
5608     case SUBMODULE_HEADER:
5609     case SUBMODULE_EXCLUDED_HEADER:
5610     case SUBMODULE_PRIVATE_HEADER:
5611       // We lazily associate headers with their modules via the HeaderInfo table.
5612       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5613       // of complete filenames or remove it entirely.
5614       break;
5615 
5616     case SUBMODULE_TEXTUAL_HEADER:
5617     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5618       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5619       // them here.
5620       break;
5621 
5622     case SUBMODULE_TOPHEADER:
5623       CurrentModule->addTopHeaderFilename(Blob);
5624       break;
5625 
5626     case SUBMODULE_UMBRELLA_DIR: {
5627       std::string Dirname = std::string(Blob);
5628       ResolveImportedPath(F, Dirname);
5629       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5630         if (!CurrentModule->getUmbrellaDir())
5631           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5632         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5633           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5634             Error("mismatched umbrella directories in submodule");
5635           return OutOfDate;
5636         }
5637       }
5638       break;
5639     }
5640 
5641     case SUBMODULE_METADATA: {
5642       F.BaseSubmoduleID = getTotalNumSubmodules();
5643       F.LocalNumSubmodules = Record[0];
5644       unsigned LocalBaseSubmoduleID = Record[1];
5645       if (F.LocalNumSubmodules > 0) {
5646         // Introduce the global -> local mapping for submodules within this
5647         // module.
5648         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5649 
5650         // Introduce the local -> global mapping for submodules within this
5651         // module.
5652         F.SubmoduleRemap.insertOrReplace(
5653           std::make_pair(LocalBaseSubmoduleID,
5654                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5655 
5656         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5657       }
5658       break;
5659     }
5660 
5661     case SUBMODULE_IMPORTS:
5662       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5663         UnresolvedModuleRef Unresolved;
5664         Unresolved.File = &F;
5665         Unresolved.Mod = CurrentModule;
5666         Unresolved.ID = Record[Idx];
5667         Unresolved.Kind = UnresolvedModuleRef::Import;
5668         Unresolved.IsWildcard = false;
5669         UnresolvedModuleRefs.push_back(Unresolved);
5670       }
5671       break;
5672 
5673     case SUBMODULE_EXPORTS:
5674       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5675         UnresolvedModuleRef Unresolved;
5676         Unresolved.File = &F;
5677         Unresolved.Mod = CurrentModule;
5678         Unresolved.ID = Record[Idx];
5679         Unresolved.Kind = UnresolvedModuleRef::Export;
5680         Unresolved.IsWildcard = Record[Idx + 1];
5681         UnresolvedModuleRefs.push_back(Unresolved);
5682       }
5683 
5684       // Once we've loaded the set of exports, there's no reason to keep
5685       // the parsed, unresolved exports around.
5686       CurrentModule->UnresolvedExports.clear();
5687       break;
5688 
5689     case SUBMODULE_REQUIRES:
5690       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5691                                     PP.getTargetInfo());
5692       break;
5693 
5694     case SUBMODULE_LINK_LIBRARY:
5695       ModMap.resolveLinkAsDependencies(CurrentModule);
5696       CurrentModule->LinkLibraries.push_back(
5697           Module::LinkLibrary(std::string(Blob), Record[0]));
5698       break;
5699 
5700     case SUBMODULE_CONFIG_MACRO:
5701       CurrentModule->ConfigMacros.push_back(Blob.str());
5702       break;
5703 
5704     case SUBMODULE_CONFLICT: {
5705       UnresolvedModuleRef Unresolved;
5706       Unresolved.File = &F;
5707       Unresolved.Mod = CurrentModule;
5708       Unresolved.ID = Record[0];
5709       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5710       Unresolved.IsWildcard = false;
5711       Unresolved.String = Blob;
5712       UnresolvedModuleRefs.push_back(Unresolved);
5713       break;
5714     }
5715 
5716     case SUBMODULE_INITIALIZERS: {
5717       if (!ContextObj)
5718         break;
5719       SmallVector<uint32_t, 16> Inits;
5720       for (auto &ID : Record)
5721         Inits.push_back(getGlobalDeclID(F, ID));
5722       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5723       break;
5724     }
5725 
5726     case SUBMODULE_EXPORT_AS:
5727       CurrentModule->ExportAsModule = Blob.str();
5728       ModMap.addLinkAsDependency(CurrentModule);
5729       break;
5730     }
5731   }
5732 }
5733 
5734 /// Parse the record that corresponds to a LangOptions data
5735 /// structure.
5736 ///
5737 /// This routine parses the language options from the AST file and then gives
5738 /// them to the AST listener if one is set.
5739 ///
5740 /// \returns true if the listener deems the file unacceptable, false otherwise.
5741 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5742                                      bool Complain,
5743                                      ASTReaderListener &Listener,
5744                                      bool AllowCompatibleDifferences) {
5745   LangOptions LangOpts;
5746   unsigned Idx = 0;
5747 #define LANGOPT(Name, Bits, Default, Description) \
5748   LangOpts.Name = Record[Idx++];
5749 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5750   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5751 #include "clang/Basic/LangOptions.def"
5752 #define SANITIZER(NAME, ID)                                                    \
5753   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5754 #include "clang/Basic/Sanitizers.def"
5755 
5756   for (unsigned N = Record[Idx++]; N; --N)
5757     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5758 
5759   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5760   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5761   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5762 
5763   LangOpts.CurrentModule = ReadString(Record, Idx);
5764 
5765   // Comment options.
5766   for (unsigned N = Record[Idx++]; N; --N) {
5767     LangOpts.CommentOpts.BlockCommandNames.push_back(
5768       ReadString(Record, Idx));
5769   }
5770   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5771 
5772   // OpenMP offloading options.
5773   for (unsigned N = Record[Idx++]; N; --N) {
5774     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5775   }
5776 
5777   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5778 
5779   return Listener.ReadLanguageOptions(LangOpts, Complain,
5780                                       AllowCompatibleDifferences);
5781 }
5782 
5783 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5784                                    ASTReaderListener &Listener,
5785                                    bool AllowCompatibleDifferences) {
5786   unsigned Idx = 0;
5787   TargetOptions TargetOpts;
5788   TargetOpts.Triple = ReadString(Record, Idx);
5789   TargetOpts.CPU = ReadString(Record, Idx);
5790   TargetOpts.TuneCPU = ReadString(Record, Idx);
5791   TargetOpts.ABI = ReadString(Record, Idx);
5792   for (unsigned N = Record[Idx++]; N; --N) {
5793     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5794   }
5795   for (unsigned N = Record[Idx++]; N; --N) {
5796     TargetOpts.Features.push_back(ReadString(Record, Idx));
5797   }
5798 
5799   return Listener.ReadTargetOptions(TargetOpts, Complain,
5800                                     AllowCompatibleDifferences);
5801 }
5802 
5803 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5804                                        ASTReaderListener &Listener) {
5805   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5806   unsigned Idx = 0;
5807 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5808 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5809   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5810 #include "clang/Basic/DiagnosticOptions.def"
5811 
5812   for (unsigned N = Record[Idx++]; N; --N)
5813     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5814   for (unsigned N = Record[Idx++]; N; --N)
5815     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5816 
5817   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5818 }
5819 
5820 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5821                                        ASTReaderListener &Listener) {
5822   FileSystemOptions FSOpts;
5823   unsigned Idx = 0;
5824   FSOpts.WorkingDir = ReadString(Record, Idx);
5825   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5826 }
5827 
5828 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5829                                          bool Complain,
5830                                          ASTReaderListener &Listener) {
5831   HeaderSearchOptions HSOpts;
5832   unsigned Idx = 0;
5833   HSOpts.Sysroot = ReadString(Record, Idx);
5834 
5835   // Include entries.
5836   for (unsigned N = Record[Idx++]; N; --N) {
5837     std::string Path = ReadString(Record, Idx);
5838     frontend::IncludeDirGroup Group
5839       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5840     bool IsFramework = Record[Idx++];
5841     bool IgnoreSysRoot = Record[Idx++];
5842     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5843                                     IgnoreSysRoot);
5844   }
5845 
5846   // System header prefixes.
5847   for (unsigned N = Record[Idx++]; N; --N) {
5848     std::string Prefix = ReadString(Record, Idx);
5849     bool IsSystemHeader = Record[Idx++];
5850     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5851   }
5852 
5853   HSOpts.ResourceDir = ReadString(Record, Idx);
5854   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5855   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5856   HSOpts.DisableModuleHash = Record[Idx++];
5857   HSOpts.ImplicitModuleMaps = Record[Idx++];
5858   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5859   HSOpts.UseBuiltinIncludes = Record[Idx++];
5860   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5861   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5862   HSOpts.UseLibcxx = Record[Idx++];
5863   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5864 
5865   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5866                                           Complain);
5867 }
5868 
5869 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5870                                          bool Complain,
5871                                          ASTReaderListener &Listener,
5872                                          std::string &SuggestedPredefines) {
5873   PreprocessorOptions PPOpts;
5874   unsigned Idx = 0;
5875 
5876   // Macro definitions/undefs
5877   for (unsigned N = Record[Idx++]; N; --N) {
5878     std::string Macro = ReadString(Record, Idx);
5879     bool IsUndef = Record[Idx++];
5880     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5881   }
5882 
5883   // Includes
5884   for (unsigned N = Record[Idx++]; N; --N) {
5885     PPOpts.Includes.push_back(ReadString(Record, Idx));
5886   }
5887 
5888   // Macro Includes
5889   for (unsigned N = Record[Idx++]; N; --N) {
5890     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5891   }
5892 
5893   PPOpts.UsePredefines = Record[Idx++];
5894   PPOpts.DetailedRecord = Record[Idx++];
5895   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5896   PPOpts.ObjCXXARCStandardLibrary =
5897     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5898   SuggestedPredefines.clear();
5899   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5900                                           SuggestedPredefines);
5901 }
5902 
5903 std::pair<ModuleFile *, unsigned>
5904 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5905   GlobalPreprocessedEntityMapType::iterator
5906   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5907   assert(I != GlobalPreprocessedEntityMap.end() &&
5908          "Corrupted global preprocessed entity map");
5909   ModuleFile *M = I->second;
5910   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5911   return std::make_pair(M, LocalIndex);
5912 }
5913 
5914 llvm::iterator_range<PreprocessingRecord::iterator>
5915 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5916   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5917     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5918                                              Mod.NumPreprocessedEntities);
5919 
5920   return llvm::make_range(PreprocessingRecord::iterator(),
5921                           PreprocessingRecord::iterator());
5922 }
5923 
5924 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5925 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5926   return llvm::make_range(
5927       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5928       ModuleDeclIterator(this, &Mod,
5929                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5930 }
5931 
5932 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5933   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5934   assert(I != GlobalSkippedRangeMap.end() &&
5935     "Corrupted global skipped range map");
5936   ModuleFile *M = I->second;
5937   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5938   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5939   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5940   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5941                     TranslateSourceLocation(*M, RawRange.getEnd()));
5942   assert(Range.isValid());
5943   return Range;
5944 }
5945 
5946 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5947   PreprocessedEntityID PPID = Index+1;
5948   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5949   ModuleFile &M = *PPInfo.first;
5950   unsigned LocalIndex = PPInfo.second;
5951   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5952 
5953   if (!PP.getPreprocessingRecord()) {
5954     Error("no preprocessing record");
5955     return nullptr;
5956   }
5957 
5958   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5959   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5960           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5961     Error(std::move(Err));
5962     return nullptr;
5963   }
5964 
5965   Expected<llvm::BitstreamEntry> MaybeEntry =
5966       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5967   if (!MaybeEntry) {
5968     Error(MaybeEntry.takeError());
5969     return nullptr;
5970   }
5971   llvm::BitstreamEntry Entry = MaybeEntry.get();
5972 
5973   if (Entry.Kind != llvm::BitstreamEntry::Record)
5974     return nullptr;
5975 
5976   // Read the record.
5977   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5978                     TranslateSourceLocation(M, PPOffs.getEnd()));
5979   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5980   StringRef Blob;
5981   RecordData Record;
5982   Expected<unsigned> MaybeRecType =
5983       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5984   if (!MaybeRecType) {
5985     Error(MaybeRecType.takeError());
5986     return nullptr;
5987   }
5988   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5989   case PPD_MACRO_EXPANSION: {
5990     bool isBuiltin = Record[0];
5991     IdentifierInfo *Name = nullptr;
5992     MacroDefinitionRecord *Def = nullptr;
5993     if (isBuiltin)
5994       Name = getLocalIdentifier(M, Record[1]);
5995     else {
5996       PreprocessedEntityID GlobalID =
5997           getGlobalPreprocessedEntityID(M, Record[1]);
5998       Def = cast<MacroDefinitionRecord>(
5999           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6000     }
6001 
6002     MacroExpansion *ME;
6003     if (isBuiltin)
6004       ME = new (PPRec) MacroExpansion(Name, Range);
6005     else
6006       ME = new (PPRec) MacroExpansion(Def, Range);
6007 
6008     return ME;
6009   }
6010 
6011   case PPD_MACRO_DEFINITION: {
6012     // Decode the identifier info and then check again; if the macro is
6013     // still defined and associated with the identifier,
6014     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6015     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6016 
6017     if (DeserializationListener)
6018       DeserializationListener->MacroDefinitionRead(PPID, MD);
6019 
6020     return MD;
6021   }
6022 
6023   case PPD_INCLUSION_DIRECTIVE: {
6024     const char *FullFileNameStart = Blob.data() + Record[0];
6025     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6026     const FileEntry *File = nullptr;
6027     if (!FullFileName.empty())
6028       if (auto FE = PP.getFileManager().getFile(FullFileName))
6029         File = *FE;
6030 
6031     // FIXME: Stable encoding
6032     InclusionDirective::InclusionKind Kind
6033       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6034     InclusionDirective *ID
6035       = new (PPRec) InclusionDirective(PPRec, Kind,
6036                                        StringRef(Blob.data(), Record[0]),
6037                                        Record[1], Record[3],
6038                                        File,
6039                                        Range);
6040     return ID;
6041   }
6042   }
6043 
6044   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6045 }
6046 
6047 /// Find the next module that contains entities and return the ID
6048 /// of the first entry.
6049 ///
6050 /// \param SLocMapI points at a chunk of a module that contains no
6051 /// preprocessed entities or the entities it contains are not the ones we are
6052 /// looking for.
6053 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6054                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6055   ++SLocMapI;
6056   for (GlobalSLocOffsetMapType::const_iterator
6057          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6058     ModuleFile &M = *SLocMapI->second;
6059     if (M.NumPreprocessedEntities)
6060       return M.BasePreprocessedEntityID;
6061   }
6062 
6063   return getTotalNumPreprocessedEntities();
6064 }
6065 
6066 namespace {
6067 
6068 struct PPEntityComp {
6069   const ASTReader &Reader;
6070   ModuleFile &M;
6071 
6072   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6073 
6074   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6075     SourceLocation LHS = getLoc(L);
6076     SourceLocation RHS = getLoc(R);
6077     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6078   }
6079 
6080   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6081     SourceLocation LHS = getLoc(L);
6082     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6083   }
6084 
6085   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6086     SourceLocation RHS = getLoc(R);
6087     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6088   }
6089 
6090   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6091     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6092   }
6093 };
6094 
6095 } // namespace
6096 
6097 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6098                                                        bool EndsAfter) const {
6099   if (SourceMgr.isLocalSourceLocation(Loc))
6100     return getTotalNumPreprocessedEntities();
6101 
6102   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6103       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6104   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6105          "Corrupted global sloc offset map");
6106 
6107   if (SLocMapI->second->NumPreprocessedEntities == 0)
6108     return findNextPreprocessedEntity(SLocMapI);
6109 
6110   ModuleFile &M = *SLocMapI->second;
6111 
6112   using pp_iterator = const PPEntityOffset *;
6113 
6114   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6115   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6116 
6117   size_t Count = M.NumPreprocessedEntities;
6118   size_t Half;
6119   pp_iterator First = pp_begin;
6120   pp_iterator PPI;
6121 
6122   if (EndsAfter) {
6123     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6124                            PPEntityComp(*this, M));
6125   } else {
6126     // Do a binary search manually instead of using std::lower_bound because
6127     // The end locations of entities may be unordered (when a macro expansion
6128     // is inside another macro argument), but for this case it is not important
6129     // whether we get the first macro expansion or its containing macro.
6130     while (Count > 0) {
6131       Half = Count / 2;
6132       PPI = First;
6133       std::advance(PPI, Half);
6134       if (SourceMgr.isBeforeInTranslationUnit(
6135               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6136         First = PPI;
6137         ++First;
6138         Count = Count - Half - 1;
6139       } else
6140         Count = Half;
6141     }
6142   }
6143 
6144   if (PPI == pp_end)
6145     return findNextPreprocessedEntity(SLocMapI);
6146 
6147   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6148 }
6149 
6150 /// Returns a pair of [Begin, End) indices of preallocated
6151 /// preprocessed entities that \arg Range encompasses.
6152 std::pair<unsigned, unsigned>
6153     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6154   if (Range.isInvalid())
6155     return std::make_pair(0,0);
6156   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6157 
6158   PreprocessedEntityID BeginID =
6159       findPreprocessedEntity(Range.getBegin(), false);
6160   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6161   return std::make_pair(BeginID, EndID);
6162 }
6163 
6164 /// Optionally returns true or false if the preallocated preprocessed
6165 /// entity with index \arg Index came from file \arg FID.
6166 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6167                                                              FileID FID) {
6168   if (FID.isInvalid())
6169     return false;
6170 
6171   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6172   ModuleFile &M = *PPInfo.first;
6173   unsigned LocalIndex = PPInfo.second;
6174   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6175 
6176   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6177   if (Loc.isInvalid())
6178     return false;
6179 
6180   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6181     return true;
6182   else
6183     return false;
6184 }
6185 
6186 namespace {
6187 
6188   /// Visitor used to search for information about a header file.
6189   class HeaderFileInfoVisitor {
6190     const FileEntry *FE;
6191     Optional<HeaderFileInfo> HFI;
6192 
6193   public:
6194     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6195 
6196     bool operator()(ModuleFile &M) {
6197       HeaderFileInfoLookupTable *Table
6198         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6199       if (!Table)
6200         return false;
6201 
6202       // Look in the on-disk hash table for an entry for this file name.
6203       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6204       if (Pos == Table->end())
6205         return false;
6206 
6207       HFI = *Pos;
6208       return true;
6209     }
6210 
6211     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6212   };
6213 
6214 } // namespace
6215 
6216 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6217   HeaderFileInfoVisitor Visitor(FE);
6218   ModuleMgr.visit(Visitor);
6219   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6220     return *HFI;
6221 
6222   return HeaderFileInfo();
6223 }
6224 
6225 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6226   using DiagState = DiagnosticsEngine::DiagState;
6227   SmallVector<DiagState *, 32> DiagStates;
6228 
6229   for (ModuleFile &F : ModuleMgr) {
6230     unsigned Idx = 0;
6231     auto &Record = F.PragmaDiagMappings;
6232     if (Record.empty())
6233       continue;
6234 
6235     DiagStates.clear();
6236 
6237     auto ReadDiagState =
6238         [&](const DiagState &BasedOn, SourceLocation Loc,
6239             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6240       unsigned BackrefID = Record[Idx++];
6241       if (BackrefID != 0)
6242         return DiagStates[BackrefID - 1];
6243 
6244       // A new DiagState was created here.
6245       Diag.DiagStates.push_back(BasedOn);
6246       DiagState *NewState = &Diag.DiagStates.back();
6247       DiagStates.push_back(NewState);
6248       unsigned Size = Record[Idx++];
6249       assert(Idx + Size * 2 <= Record.size() &&
6250              "Invalid data, not enough diag/map pairs");
6251       while (Size--) {
6252         unsigned DiagID = Record[Idx++];
6253         DiagnosticMapping NewMapping =
6254             DiagnosticMapping::deserialize(Record[Idx++]);
6255         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6256           continue;
6257 
6258         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6259 
6260         // If this mapping was specified as a warning but the severity was
6261         // upgraded due to diagnostic settings, simulate the current diagnostic
6262         // settings (and use a warning).
6263         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6264           NewMapping.setSeverity(diag::Severity::Warning);
6265           NewMapping.setUpgradedFromWarning(false);
6266         }
6267 
6268         Mapping = NewMapping;
6269       }
6270       return NewState;
6271     };
6272 
6273     // Read the first state.
6274     DiagState *FirstState;
6275     if (F.Kind == MK_ImplicitModule) {
6276       // Implicitly-built modules are reused with different diagnostic
6277       // settings.  Use the initial diagnostic state from Diag to simulate this
6278       // compilation's diagnostic settings.
6279       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6280       DiagStates.push_back(FirstState);
6281 
6282       // Skip the initial diagnostic state from the serialized module.
6283       assert(Record[1] == 0 &&
6284              "Invalid data, unexpected backref in initial state");
6285       Idx = 3 + Record[2] * 2;
6286       assert(Idx < Record.size() &&
6287              "Invalid data, not enough state change pairs in initial state");
6288     } else if (F.isModule()) {
6289       // For an explicit module, preserve the flags from the module build
6290       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6291       // -Wblah flags.
6292       unsigned Flags = Record[Idx++];
6293       DiagState Initial;
6294       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6295       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6296       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6297       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6298       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6299       Initial.ExtBehavior = (diag::Severity)Flags;
6300       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6301 
6302       assert(F.OriginalSourceFileID.isValid());
6303 
6304       // Set up the root buffer of the module to start with the initial
6305       // diagnostic state of the module itself, to cover files that contain no
6306       // explicit transitions (for which we did not serialize anything).
6307       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6308           .StateTransitions.push_back({FirstState, 0});
6309     } else {
6310       // For prefix ASTs, start with whatever the user configured on the
6311       // command line.
6312       Idx++; // Skip flags.
6313       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6314                                  SourceLocation(), false);
6315     }
6316 
6317     // Read the state transitions.
6318     unsigned NumLocations = Record[Idx++];
6319     while (NumLocations--) {
6320       assert(Idx < Record.size() &&
6321              "Invalid data, missing pragma diagnostic states");
6322       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6323       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6324       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6325       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6326       unsigned Transitions = Record[Idx++];
6327 
6328       // Note that we don't need to set up Parent/ParentOffset here, because
6329       // we won't be changing the diagnostic state within imported FileIDs
6330       // (other than perhaps appending to the main source file, which has no
6331       // parent).
6332       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6333       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6334       for (unsigned I = 0; I != Transitions; ++I) {
6335         unsigned Offset = Record[Idx++];
6336         auto *State =
6337             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6338         F.StateTransitions.push_back({State, Offset});
6339       }
6340     }
6341 
6342     // Read the final state.
6343     assert(Idx < Record.size() &&
6344            "Invalid data, missing final pragma diagnostic state");
6345     SourceLocation CurStateLoc =
6346         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6347     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6348 
6349     if (!F.isModule()) {
6350       Diag.DiagStatesByLoc.CurDiagState = CurState;
6351       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6352 
6353       // Preserve the property that the imaginary root file describes the
6354       // current state.
6355       FileID NullFile;
6356       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6357       if (T.empty())
6358         T.push_back({CurState, 0});
6359       else
6360         T[0].State = CurState;
6361     }
6362 
6363     // Don't try to read these mappings again.
6364     Record.clear();
6365   }
6366 }
6367 
6368 /// Get the correct cursor and offset for loading a type.
6369 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6370   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6371   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6372   ModuleFile *M = I->second;
6373   return RecordLocation(
6374       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6375              M->DeclsBlockStartOffset);
6376 }
6377 
6378 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6379   switch (code) {
6380 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6381   case TYPE_##CODE_ID: return Type::CLASS_ID;
6382 #include "clang/Serialization/TypeBitCodes.def"
6383   default: return llvm::None;
6384   }
6385 }
6386 
6387 /// Read and return the type with the given index..
6388 ///
6389 /// The index is the type ID, shifted and minus the number of predefs. This
6390 /// routine actually reads the record corresponding to the type at the given
6391 /// location. It is a helper routine for GetType, which deals with reading type
6392 /// IDs.
6393 QualType ASTReader::readTypeRecord(unsigned Index) {
6394   assert(ContextObj && "reading type with no AST context");
6395   ASTContext &Context = *ContextObj;
6396   RecordLocation Loc = TypeCursorForIndex(Index);
6397   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6398 
6399   // Keep track of where we are in the stream, then jump back there
6400   // after reading this type.
6401   SavedStreamPosition SavedPosition(DeclsCursor);
6402 
6403   ReadingKindTracker ReadingKind(Read_Type, *this);
6404 
6405   // Note that we are loading a type record.
6406   Deserializing AType(this);
6407 
6408   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6409     Error(std::move(Err));
6410     return QualType();
6411   }
6412   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6413   if (!RawCode) {
6414     Error(RawCode.takeError());
6415     return QualType();
6416   }
6417 
6418   ASTRecordReader Record(*this, *Loc.F);
6419   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6420   if (!Code) {
6421     Error(Code.takeError());
6422     return QualType();
6423   }
6424   if (Code.get() == TYPE_EXT_QUAL) {
6425     QualType baseType = Record.readQualType();
6426     Qualifiers quals = Record.readQualifiers();
6427     return Context.getQualifiedType(baseType, quals);
6428   }
6429 
6430   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6431   if (!maybeClass) {
6432     Error("Unexpected code for type");
6433     return QualType();
6434   }
6435 
6436   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6437   return TypeReader.read(*maybeClass);
6438 }
6439 
6440 namespace clang {
6441 
6442 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6443   ASTRecordReader &Reader;
6444 
6445   SourceLocation readSourceLocation() {
6446     return Reader.readSourceLocation();
6447   }
6448 
6449   TypeSourceInfo *GetTypeSourceInfo() {
6450     return Reader.readTypeSourceInfo();
6451   }
6452 
6453   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6454     return Reader.readNestedNameSpecifierLoc();
6455   }
6456 
6457   Attr *ReadAttr() {
6458     return Reader.readAttr();
6459   }
6460 
6461 public:
6462   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6463 
6464   // We want compile-time assurance that we've enumerated all of
6465   // these, so unfortunately we have to declare them first, then
6466   // define them out-of-line.
6467 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6468 #define TYPELOC(CLASS, PARENT) \
6469   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6470 #include "clang/AST/TypeLocNodes.def"
6471 
6472   void VisitFunctionTypeLoc(FunctionTypeLoc);
6473   void VisitArrayTypeLoc(ArrayTypeLoc);
6474 };
6475 
6476 } // namespace clang
6477 
6478 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6479   // nothing to do
6480 }
6481 
6482 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6483   TL.setBuiltinLoc(readSourceLocation());
6484   if (TL.needsExtraLocalData()) {
6485     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6486     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6487     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6488     TL.setModeAttr(Reader.readInt());
6489   }
6490 }
6491 
6492 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6493   TL.setNameLoc(readSourceLocation());
6494 }
6495 
6496 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6497   TL.setStarLoc(readSourceLocation());
6498 }
6499 
6500 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6501   // nothing to do
6502 }
6503 
6504 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6505   // nothing to do
6506 }
6507 
6508 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6509   TL.setExpansionLoc(readSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6513   TL.setCaretLoc(readSourceLocation());
6514 }
6515 
6516 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6517   TL.setAmpLoc(readSourceLocation());
6518 }
6519 
6520 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6521   TL.setAmpAmpLoc(readSourceLocation());
6522 }
6523 
6524 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6525   TL.setStarLoc(readSourceLocation());
6526   TL.setClassTInfo(GetTypeSourceInfo());
6527 }
6528 
6529 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6530   TL.setLBracketLoc(readSourceLocation());
6531   TL.setRBracketLoc(readSourceLocation());
6532   if (Reader.readBool())
6533     TL.setSizeExpr(Reader.readExpr());
6534   else
6535     TL.setSizeExpr(nullptr);
6536 }
6537 
6538 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6539   VisitArrayTypeLoc(TL);
6540 }
6541 
6542 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6543   VisitArrayTypeLoc(TL);
6544 }
6545 
6546 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6547   VisitArrayTypeLoc(TL);
6548 }
6549 
6550 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6551                                             DependentSizedArrayTypeLoc TL) {
6552   VisitArrayTypeLoc(TL);
6553 }
6554 
6555 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6556     DependentAddressSpaceTypeLoc TL) {
6557 
6558     TL.setAttrNameLoc(readSourceLocation());
6559     TL.setAttrOperandParensRange(Reader.readSourceRange());
6560     TL.setAttrExprOperand(Reader.readExpr());
6561 }
6562 
6563 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6564                                         DependentSizedExtVectorTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6569   TL.setNameLoc(readSourceLocation());
6570 }
6571 
6572 void TypeLocReader::VisitDependentVectorTypeLoc(
6573     DependentVectorTypeLoc TL) {
6574   TL.setNameLoc(readSourceLocation());
6575 }
6576 
6577 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6578   TL.setNameLoc(readSourceLocation());
6579 }
6580 
6581 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6582   TL.setAttrNameLoc(readSourceLocation());
6583   TL.setAttrOperandParensRange(Reader.readSourceRange());
6584   TL.setAttrRowOperand(Reader.readExpr());
6585   TL.setAttrColumnOperand(Reader.readExpr());
6586 }
6587 
6588 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6589     DependentSizedMatrixTypeLoc TL) {
6590   TL.setAttrNameLoc(readSourceLocation());
6591   TL.setAttrOperandParensRange(Reader.readSourceRange());
6592   TL.setAttrRowOperand(Reader.readExpr());
6593   TL.setAttrColumnOperand(Reader.readExpr());
6594 }
6595 
6596 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6597   TL.setLocalRangeBegin(readSourceLocation());
6598   TL.setLParenLoc(readSourceLocation());
6599   TL.setRParenLoc(readSourceLocation());
6600   TL.setExceptionSpecRange(Reader.readSourceRange());
6601   TL.setLocalRangeEnd(readSourceLocation());
6602   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6603     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6604   }
6605 }
6606 
6607 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6608   VisitFunctionTypeLoc(TL);
6609 }
6610 
6611 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6612   VisitFunctionTypeLoc(TL);
6613 }
6614 
6615 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6616   TL.setNameLoc(readSourceLocation());
6617 }
6618 
6619 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6620   TL.setNameLoc(readSourceLocation());
6621 }
6622 
6623 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6624   TL.setTypeofLoc(readSourceLocation());
6625   TL.setLParenLoc(readSourceLocation());
6626   TL.setRParenLoc(readSourceLocation());
6627 }
6628 
6629 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6630   TL.setTypeofLoc(readSourceLocation());
6631   TL.setLParenLoc(readSourceLocation());
6632   TL.setRParenLoc(readSourceLocation());
6633   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6634 }
6635 
6636 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6637   TL.setNameLoc(readSourceLocation());
6638 }
6639 
6640 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6641   TL.setKWLoc(readSourceLocation());
6642   TL.setLParenLoc(readSourceLocation());
6643   TL.setRParenLoc(readSourceLocation());
6644   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6645 }
6646 
6647 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6648   TL.setNameLoc(readSourceLocation());
6649   if (Reader.readBool()) {
6650     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6651     TL.setTemplateKWLoc(readSourceLocation());
6652     TL.setConceptNameLoc(readSourceLocation());
6653     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6654     TL.setLAngleLoc(readSourceLocation());
6655     TL.setRAngleLoc(readSourceLocation());
6656     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6657       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6658                               TL.getTypePtr()->getArg(i).getKind()));
6659   }
6660 }
6661 
6662 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6663     DeducedTemplateSpecializationTypeLoc TL) {
6664   TL.setTemplateNameLoc(readSourceLocation());
6665 }
6666 
6667 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6668   TL.setNameLoc(readSourceLocation());
6669 }
6670 
6671 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6672   TL.setNameLoc(readSourceLocation());
6673 }
6674 
6675 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6676   TL.setAttr(ReadAttr());
6677 }
6678 
6679 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6680   TL.setNameLoc(readSourceLocation());
6681 }
6682 
6683 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6684                                             SubstTemplateTypeParmTypeLoc TL) {
6685   TL.setNameLoc(readSourceLocation());
6686 }
6687 
6688 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6689                                           SubstTemplateTypeParmPackTypeLoc TL) {
6690   TL.setNameLoc(readSourceLocation());
6691 }
6692 
6693 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6694                                            TemplateSpecializationTypeLoc TL) {
6695   TL.setTemplateKeywordLoc(readSourceLocation());
6696   TL.setTemplateNameLoc(readSourceLocation());
6697   TL.setLAngleLoc(readSourceLocation());
6698   TL.setRAngleLoc(readSourceLocation());
6699   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6700     TL.setArgLocInfo(
6701         i,
6702         Reader.readTemplateArgumentLocInfo(
6703           TL.getTypePtr()->getArg(i).getKind()));
6704 }
6705 
6706 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6707   TL.setLParenLoc(readSourceLocation());
6708   TL.setRParenLoc(readSourceLocation());
6709 }
6710 
6711 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6712   TL.setElaboratedKeywordLoc(readSourceLocation());
6713   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6714 }
6715 
6716 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6717   TL.setNameLoc(readSourceLocation());
6718 }
6719 
6720 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6721   TL.setElaboratedKeywordLoc(readSourceLocation());
6722   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6723   TL.setNameLoc(readSourceLocation());
6724 }
6725 
6726 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6727        DependentTemplateSpecializationTypeLoc TL) {
6728   TL.setElaboratedKeywordLoc(readSourceLocation());
6729   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6730   TL.setTemplateKeywordLoc(readSourceLocation());
6731   TL.setTemplateNameLoc(readSourceLocation());
6732   TL.setLAngleLoc(readSourceLocation());
6733   TL.setRAngleLoc(readSourceLocation());
6734   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6735     TL.setArgLocInfo(
6736         I,
6737         Reader.readTemplateArgumentLocInfo(
6738             TL.getTypePtr()->getArg(I).getKind()));
6739 }
6740 
6741 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6742   TL.setEllipsisLoc(readSourceLocation());
6743 }
6744 
6745 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6746   TL.setNameLoc(readSourceLocation());
6747 }
6748 
6749 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6750   if (TL.getNumProtocols()) {
6751     TL.setProtocolLAngleLoc(readSourceLocation());
6752     TL.setProtocolRAngleLoc(readSourceLocation());
6753   }
6754   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6755     TL.setProtocolLoc(i, readSourceLocation());
6756 }
6757 
6758 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6759   TL.setHasBaseTypeAsWritten(Reader.readBool());
6760   TL.setTypeArgsLAngleLoc(readSourceLocation());
6761   TL.setTypeArgsRAngleLoc(readSourceLocation());
6762   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6763     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6764   TL.setProtocolLAngleLoc(readSourceLocation());
6765   TL.setProtocolRAngleLoc(readSourceLocation());
6766   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6767     TL.setProtocolLoc(i, readSourceLocation());
6768 }
6769 
6770 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6771   TL.setStarLoc(readSourceLocation());
6772 }
6773 
6774 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6775   TL.setKWLoc(readSourceLocation());
6776   TL.setLParenLoc(readSourceLocation());
6777   TL.setRParenLoc(readSourceLocation());
6778 }
6779 
6780 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6781   TL.setKWLoc(readSourceLocation());
6782 }
6783 
6784 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6785   TL.setNameLoc(readSourceLocation());
6786 }
6787 void TypeLocReader::VisitDependentExtIntTypeLoc(
6788     clang::DependentExtIntTypeLoc TL) {
6789   TL.setNameLoc(readSourceLocation());
6790 }
6791 
6792 
6793 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6794   TypeLocReader TLR(*this);
6795   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6796     TLR.Visit(TL);
6797 }
6798 
6799 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6800   QualType InfoTy = readType();
6801   if (InfoTy.isNull())
6802     return nullptr;
6803 
6804   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6805   readTypeLoc(TInfo->getTypeLoc());
6806   return TInfo;
6807 }
6808 
6809 QualType ASTReader::GetType(TypeID ID) {
6810   assert(ContextObj && "reading type with no AST context");
6811   ASTContext &Context = *ContextObj;
6812 
6813   unsigned FastQuals = ID & Qualifiers::FastMask;
6814   unsigned Index = ID >> Qualifiers::FastWidth;
6815 
6816   if (Index < NUM_PREDEF_TYPE_IDS) {
6817     QualType T;
6818     switch ((PredefinedTypeIDs)Index) {
6819     case PREDEF_TYPE_NULL_ID:
6820       return QualType();
6821     case PREDEF_TYPE_VOID_ID:
6822       T = Context.VoidTy;
6823       break;
6824     case PREDEF_TYPE_BOOL_ID:
6825       T = Context.BoolTy;
6826       break;
6827     case PREDEF_TYPE_CHAR_U_ID:
6828     case PREDEF_TYPE_CHAR_S_ID:
6829       // FIXME: Check that the signedness of CharTy is correct!
6830       T = Context.CharTy;
6831       break;
6832     case PREDEF_TYPE_UCHAR_ID:
6833       T = Context.UnsignedCharTy;
6834       break;
6835     case PREDEF_TYPE_USHORT_ID:
6836       T = Context.UnsignedShortTy;
6837       break;
6838     case PREDEF_TYPE_UINT_ID:
6839       T = Context.UnsignedIntTy;
6840       break;
6841     case PREDEF_TYPE_ULONG_ID:
6842       T = Context.UnsignedLongTy;
6843       break;
6844     case PREDEF_TYPE_ULONGLONG_ID:
6845       T = Context.UnsignedLongLongTy;
6846       break;
6847     case PREDEF_TYPE_UINT128_ID:
6848       T = Context.UnsignedInt128Ty;
6849       break;
6850     case PREDEF_TYPE_SCHAR_ID:
6851       T = Context.SignedCharTy;
6852       break;
6853     case PREDEF_TYPE_WCHAR_ID:
6854       T = Context.WCharTy;
6855       break;
6856     case PREDEF_TYPE_SHORT_ID:
6857       T = Context.ShortTy;
6858       break;
6859     case PREDEF_TYPE_INT_ID:
6860       T = Context.IntTy;
6861       break;
6862     case PREDEF_TYPE_LONG_ID:
6863       T = Context.LongTy;
6864       break;
6865     case PREDEF_TYPE_LONGLONG_ID:
6866       T = Context.LongLongTy;
6867       break;
6868     case PREDEF_TYPE_INT128_ID:
6869       T = Context.Int128Ty;
6870       break;
6871     case PREDEF_TYPE_BFLOAT16_ID:
6872       T = Context.BFloat16Ty;
6873       break;
6874     case PREDEF_TYPE_HALF_ID:
6875       T = Context.HalfTy;
6876       break;
6877     case PREDEF_TYPE_FLOAT_ID:
6878       T = Context.FloatTy;
6879       break;
6880     case PREDEF_TYPE_DOUBLE_ID:
6881       T = Context.DoubleTy;
6882       break;
6883     case PREDEF_TYPE_LONGDOUBLE_ID:
6884       T = Context.LongDoubleTy;
6885       break;
6886     case PREDEF_TYPE_SHORT_ACCUM_ID:
6887       T = Context.ShortAccumTy;
6888       break;
6889     case PREDEF_TYPE_ACCUM_ID:
6890       T = Context.AccumTy;
6891       break;
6892     case PREDEF_TYPE_LONG_ACCUM_ID:
6893       T = Context.LongAccumTy;
6894       break;
6895     case PREDEF_TYPE_USHORT_ACCUM_ID:
6896       T = Context.UnsignedShortAccumTy;
6897       break;
6898     case PREDEF_TYPE_UACCUM_ID:
6899       T = Context.UnsignedAccumTy;
6900       break;
6901     case PREDEF_TYPE_ULONG_ACCUM_ID:
6902       T = Context.UnsignedLongAccumTy;
6903       break;
6904     case PREDEF_TYPE_SHORT_FRACT_ID:
6905       T = Context.ShortFractTy;
6906       break;
6907     case PREDEF_TYPE_FRACT_ID:
6908       T = Context.FractTy;
6909       break;
6910     case PREDEF_TYPE_LONG_FRACT_ID:
6911       T = Context.LongFractTy;
6912       break;
6913     case PREDEF_TYPE_USHORT_FRACT_ID:
6914       T = Context.UnsignedShortFractTy;
6915       break;
6916     case PREDEF_TYPE_UFRACT_ID:
6917       T = Context.UnsignedFractTy;
6918       break;
6919     case PREDEF_TYPE_ULONG_FRACT_ID:
6920       T = Context.UnsignedLongFractTy;
6921       break;
6922     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6923       T = Context.SatShortAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_ACCUM_ID:
6926       T = Context.SatAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6929       T = Context.SatLongAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6932       T = Context.SatUnsignedShortAccumTy;
6933       break;
6934     case PREDEF_TYPE_SAT_UACCUM_ID:
6935       T = Context.SatUnsignedAccumTy;
6936       break;
6937     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6938       T = Context.SatUnsignedLongAccumTy;
6939       break;
6940     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6941       T = Context.SatShortFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_FRACT_ID:
6944       T = Context.SatFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6947       T = Context.SatLongFractTy;
6948       break;
6949     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6950       T = Context.SatUnsignedShortFractTy;
6951       break;
6952     case PREDEF_TYPE_SAT_UFRACT_ID:
6953       T = Context.SatUnsignedFractTy;
6954       break;
6955     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6956       T = Context.SatUnsignedLongFractTy;
6957       break;
6958     case PREDEF_TYPE_FLOAT16_ID:
6959       T = Context.Float16Ty;
6960       break;
6961     case PREDEF_TYPE_FLOAT128_ID:
6962       T = Context.Float128Ty;
6963       break;
6964     case PREDEF_TYPE_OVERLOAD_ID:
6965       T = Context.OverloadTy;
6966       break;
6967     case PREDEF_TYPE_BOUND_MEMBER:
6968       T = Context.BoundMemberTy;
6969       break;
6970     case PREDEF_TYPE_PSEUDO_OBJECT:
6971       T = Context.PseudoObjectTy;
6972       break;
6973     case PREDEF_TYPE_DEPENDENT_ID:
6974       T = Context.DependentTy;
6975       break;
6976     case PREDEF_TYPE_UNKNOWN_ANY:
6977       T = Context.UnknownAnyTy;
6978       break;
6979     case PREDEF_TYPE_NULLPTR_ID:
6980       T = Context.NullPtrTy;
6981       break;
6982     case PREDEF_TYPE_CHAR8_ID:
6983       T = Context.Char8Ty;
6984       break;
6985     case PREDEF_TYPE_CHAR16_ID:
6986       T = Context.Char16Ty;
6987       break;
6988     case PREDEF_TYPE_CHAR32_ID:
6989       T = Context.Char32Ty;
6990       break;
6991     case PREDEF_TYPE_OBJC_ID:
6992       T = Context.ObjCBuiltinIdTy;
6993       break;
6994     case PREDEF_TYPE_OBJC_CLASS:
6995       T = Context.ObjCBuiltinClassTy;
6996       break;
6997     case PREDEF_TYPE_OBJC_SEL:
6998       T = Context.ObjCBuiltinSelTy;
6999       break;
7000 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7001     case PREDEF_TYPE_##Id##_ID: \
7002       T = Context.SingletonId; \
7003       break;
7004 #include "clang/Basic/OpenCLImageTypes.def"
7005 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7006     case PREDEF_TYPE_##Id##_ID: \
7007       T = Context.Id##Ty; \
7008       break;
7009 #include "clang/Basic/OpenCLExtensionTypes.def"
7010     case PREDEF_TYPE_SAMPLER_ID:
7011       T = Context.OCLSamplerTy;
7012       break;
7013     case PREDEF_TYPE_EVENT_ID:
7014       T = Context.OCLEventTy;
7015       break;
7016     case PREDEF_TYPE_CLK_EVENT_ID:
7017       T = Context.OCLClkEventTy;
7018       break;
7019     case PREDEF_TYPE_QUEUE_ID:
7020       T = Context.OCLQueueTy;
7021       break;
7022     case PREDEF_TYPE_RESERVE_ID_ID:
7023       T = Context.OCLReserveIDTy;
7024       break;
7025     case PREDEF_TYPE_AUTO_DEDUCT:
7026       T = Context.getAutoDeductType();
7027       break;
7028     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7029       T = Context.getAutoRRefDeductType();
7030       break;
7031     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7032       T = Context.ARCUnbridgedCastTy;
7033       break;
7034     case PREDEF_TYPE_BUILTIN_FN:
7035       T = Context.BuiltinFnTy;
7036       break;
7037     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7038       T = Context.IncompleteMatrixIdxTy;
7039       break;
7040     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7041       T = Context.OMPArraySectionTy;
7042       break;
7043     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7044       T = Context.OMPArraySectionTy;
7045       break;
7046     case PREDEF_TYPE_OMP_ITERATOR:
7047       T = Context.OMPIteratorTy;
7048       break;
7049 #define SVE_TYPE(Name, Id, SingletonId) \
7050     case PREDEF_TYPE_##Id##_ID: \
7051       T = Context.SingletonId; \
7052       break;
7053 #include "clang/Basic/AArch64SVEACLETypes.def"
7054     }
7055 
7056     assert(!T.isNull() && "Unknown predefined type");
7057     return T.withFastQualifiers(FastQuals);
7058   }
7059 
7060   Index -= NUM_PREDEF_TYPE_IDS;
7061   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7062   if (TypesLoaded[Index].isNull()) {
7063     TypesLoaded[Index] = readTypeRecord(Index);
7064     if (TypesLoaded[Index].isNull())
7065       return QualType();
7066 
7067     TypesLoaded[Index]->setFromAST();
7068     if (DeserializationListener)
7069       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7070                                         TypesLoaded[Index]);
7071   }
7072 
7073   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7074 }
7075 
7076 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7077   return GetType(getGlobalTypeID(F, LocalID));
7078 }
7079 
7080 serialization::TypeID
7081 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7082   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7083   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7084 
7085   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7086     return LocalID;
7087 
7088   if (!F.ModuleOffsetMap.empty())
7089     ReadModuleOffsetMap(F);
7090 
7091   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7092     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7093   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7094 
7095   unsigned GlobalIndex = LocalIndex + I->second;
7096   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7097 }
7098 
7099 TemplateArgumentLocInfo
7100 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7101   switch (Kind) {
7102   case TemplateArgument::Expression:
7103     return readExpr();
7104   case TemplateArgument::Type:
7105     return readTypeSourceInfo();
7106   case TemplateArgument::Template: {
7107     NestedNameSpecifierLoc QualifierLoc =
7108       readNestedNameSpecifierLoc();
7109     SourceLocation TemplateNameLoc = readSourceLocation();
7110     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7111                                    TemplateNameLoc, SourceLocation());
7112   }
7113   case TemplateArgument::TemplateExpansion: {
7114     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7115     SourceLocation TemplateNameLoc = readSourceLocation();
7116     SourceLocation EllipsisLoc = readSourceLocation();
7117     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7118                                    TemplateNameLoc, EllipsisLoc);
7119   }
7120   case TemplateArgument::Null:
7121   case TemplateArgument::Integral:
7122   case TemplateArgument::Declaration:
7123   case TemplateArgument::NullPtr:
7124   case TemplateArgument::Pack:
7125     // FIXME: Is this right?
7126     return TemplateArgumentLocInfo();
7127   }
7128   llvm_unreachable("unexpected template argument loc");
7129 }
7130 
7131 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7132   TemplateArgument Arg = readTemplateArgument();
7133 
7134   if (Arg.getKind() == TemplateArgument::Expression) {
7135     if (readBool()) // bool InfoHasSameExpr.
7136       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7137   }
7138   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7139 }
7140 
7141 const ASTTemplateArgumentListInfo *
7142 ASTRecordReader::readASTTemplateArgumentListInfo() {
7143   SourceLocation LAngleLoc = readSourceLocation();
7144   SourceLocation RAngleLoc = readSourceLocation();
7145   unsigned NumArgsAsWritten = readInt();
7146   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7147   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7148     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7149   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7150 }
7151 
7152 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7153   return GetDecl(ID);
7154 }
7155 
7156 void ASTReader::CompleteRedeclChain(const Decl *D) {
7157   if (NumCurrentElementsDeserializing) {
7158     // We arrange to not care about the complete redeclaration chain while we're
7159     // deserializing. Just remember that the AST has marked this one as complete
7160     // but that it's not actually complete yet, so we know we still need to
7161     // complete it later.
7162     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7163     return;
7164   }
7165 
7166   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7167 
7168   // If this is a named declaration, complete it by looking it up
7169   // within its context.
7170   //
7171   // FIXME: Merging a function definition should merge
7172   // all mergeable entities within it.
7173   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7174       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7175     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7176       if (!getContext().getLangOpts().CPlusPlus &&
7177           isa<TranslationUnitDecl>(DC)) {
7178         // Outside of C++, we don't have a lookup table for the TU, so update
7179         // the identifier instead. (For C++ modules, we don't store decls
7180         // in the serialized identifier table, so we do the lookup in the TU.)
7181         auto *II = Name.getAsIdentifierInfo();
7182         assert(II && "non-identifier name in C?");
7183         if (II->isOutOfDate())
7184           updateOutOfDateIdentifier(*II);
7185       } else
7186         DC->lookup(Name);
7187     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7188       // Find all declarations of this kind from the relevant context.
7189       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7190         auto *DC = cast<DeclContext>(DCDecl);
7191         SmallVector<Decl*, 8> Decls;
7192         FindExternalLexicalDecls(
7193             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7194       }
7195     }
7196   }
7197 
7198   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7199     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7200   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7201     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7202   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7203     if (auto *Template = FD->getPrimaryTemplate())
7204       Template->LoadLazySpecializations();
7205   }
7206 }
7207 
7208 CXXCtorInitializer **
7209 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7210   RecordLocation Loc = getLocalBitOffset(Offset);
7211   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7212   SavedStreamPosition SavedPosition(Cursor);
7213   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7214     Error(std::move(Err));
7215     return nullptr;
7216   }
7217   ReadingKindTracker ReadingKind(Read_Decl, *this);
7218 
7219   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7220   if (!MaybeCode) {
7221     Error(MaybeCode.takeError());
7222     return nullptr;
7223   }
7224   unsigned Code = MaybeCode.get();
7225 
7226   ASTRecordReader Record(*this, *Loc.F);
7227   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7228   if (!MaybeRecCode) {
7229     Error(MaybeRecCode.takeError());
7230     return nullptr;
7231   }
7232   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7233     Error("malformed AST file: missing C++ ctor initializers");
7234     return nullptr;
7235   }
7236 
7237   return Record.readCXXCtorInitializers();
7238 }
7239 
7240 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7241   assert(ContextObj && "reading base specifiers with no AST context");
7242   ASTContext &Context = *ContextObj;
7243 
7244   RecordLocation Loc = getLocalBitOffset(Offset);
7245   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7246   SavedStreamPosition SavedPosition(Cursor);
7247   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7248     Error(std::move(Err));
7249     return nullptr;
7250   }
7251   ReadingKindTracker ReadingKind(Read_Decl, *this);
7252 
7253   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7254   if (!MaybeCode) {
7255     Error(MaybeCode.takeError());
7256     return nullptr;
7257   }
7258   unsigned Code = MaybeCode.get();
7259 
7260   ASTRecordReader Record(*this, *Loc.F);
7261   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7262   if (!MaybeRecCode) {
7263     Error(MaybeCode.takeError());
7264     return nullptr;
7265   }
7266   unsigned RecCode = MaybeRecCode.get();
7267 
7268   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7269     Error("malformed AST file: missing C++ base specifiers");
7270     return nullptr;
7271   }
7272 
7273   unsigned NumBases = Record.readInt();
7274   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7275   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7276   for (unsigned I = 0; I != NumBases; ++I)
7277     Bases[I] = Record.readCXXBaseSpecifier();
7278   return Bases;
7279 }
7280 
7281 serialization::DeclID
7282 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7283   if (LocalID < NUM_PREDEF_DECL_IDS)
7284     return LocalID;
7285 
7286   if (!F.ModuleOffsetMap.empty())
7287     ReadModuleOffsetMap(F);
7288 
7289   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7290     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7291   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7292 
7293   return LocalID + I->second;
7294 }
7295 
7296 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7297                                    ModuleFile &M) const {
7298   // Predefined decls aren't from any module.
7299   if (ID < NUM_PREDEF_DECL_IDS)
7300     return false;
7301 
7302   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7303          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7304 }
7305 
7306 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7307   if (!D->isFromASTFile())
7308     return nullptr;
7309   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7310   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7311   return I->second;
7312 }
7313 
7314 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7315   if (ID < NUM_PREDEF_DECL_IDS)
7316     return SourceLocation();
7317 
7318   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7319 
7320   if (Index > DeclsLoaded.size()) {
7321     Error("declaration ID out-of-range for AST file");
7322     return SourceLocation();
7323   }
7324 
7325   if (Decl *D = DeclsLoaded[Index])
7326     return D->getLocation();
7327 
7328   SourceLocation Loc;
7329   DeclCursorForID(ID, Loc);
7330   return Loc;
7331 }
7332 
7333 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7334   switch (ID) {
7335   case PREDEF_DECL_NULL_ID:
7336     return nullptr;
7337 
7338   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7339     return Context.getTranslationUnitDecl();
7340 
7341   case PREDEF_DECL_OBJC_ID_ID:
7342     return Context.getObjCIdDecl();
7343 
7344   case PREDEF_DECL_OBJC_SEL_ID:
7345     return Context.getObjCSelDecl();
7346 
7347   case PREDEF_DECL_OBJC_CLASS_ID:
7348     return Context.getObjCClassDecl();
7349 
7350   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7351     return Context.getObjCProtocolDecl();
7352 
7353   case PREDEF_DECL_INT_128_ID:
7354     return Context.getInt128Decl();
7355 
7356   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7357     return Context.getUInt128Decl();
7358 
7359   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7360     return Context.getObjCInstanceTypeDecl();
7361 
7362   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7363     return Context.getBuiltinVaListDecl();
7364 
7365   case PREDEF_DECL_VA_LIST_TAG:
7366     return Context.getVaListTagDecl();
7367 
7368   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7369     return Context.getBuiltinMSVaListDecl();
7370 
7371   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7372     return Context.getMSGuidTagDecl();
7373 
7374   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7375     return Context.getExternCContextDecl();
7376 
7377   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7378     return Context.getMakeIntegerSeqDecl();
7379 
7380   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7381     return Context.getCFConstantStringDecl();
7382 
7383   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7384     return Context.getCFConstantStringTagDecl();
7385 
7386   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7387     return Context.getTypePackElementDecl();
7388   }
7389   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7390 }
7391 
7392 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7393   assert(ContextObj && "reading decl with no AST context");
7394   if (ID < NUM_PREDEF_DECL_IDS) {
7395     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7396     if (D) {
7397       // Track that we have merged the declaration with ID \p ID into the
7398       // pre-existing predefined declaration \p D.
7399       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7400       if (Merged.empty())
7401         Merged.push_back(ID);
7402     }
7403     return D;
7404   }
7405 
7406   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7407 
7408   if (Index >= DeclsLoaded.size()) {
7409     assert(0 && "declaration ID out-of-range for AST file");
7410     Error("declaration ID out-of-range for AST file");
7411     return nullptr;
7412   }
7413 
7414   return DeclsLoaded[Index];
7415 }
7416 
7417 Decl *ASTReader::GetDecl(DeclID ID) {
7418   if (ID < NUM_PREDEF_DECL_IDS)
7419     return GetExistingDecl(ID);
7420 
7421   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7422 
7423   if (Index >= DeclsLoaded.size()) {
7424     assert(0 && "declaration ID out-of-range for AST file");
7425     Error("declaration ID out-of-range for AST file");
7426     return nullptr;
7427   }
7428 
7429   if (!DeclsLoaded[Index]) {
7430     ReadDeclRecord(ID);
7431     if (DeserializationListener)
7432       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7433   }
7434 
7435   return DeclsLoaded[Index];
7436 }
7437 
7438 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7439                                                   DeclID GlobalID) {
7440   if (GlobalID < NUM_PREDEF_DECL_IDS)
7441     return GlobalID;
7442 
7443   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7444   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7445   ModuleFile *Owner = I->second;
7446 
7447   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7448     = M.GlobalToLocalDeclIDs.find(Owner);
7449   if (Pos == M.GlobalToLocalDeclIDs.end())
7450     return 0;
7451 
7452   return GlobalID - Owner->BaseDeclID + Pos->second;
7453 }
7454 
7455 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7456                                             const RecordData &Record,
7457                                             unsigned &Idx) {
7458   if (Idx >= Record.size()) {
7459     Error("Corrupted AST file");
7460     return 0;
7461   }
7462 
7463   return getGlobalDeclID(F, Record[Idx++]);
7464 }
7465 
7466 /// Resolve the offset of a statement into a statement.
7467 ///
7468 /// This operation will read a new statement from the external
7469 /// source each time it is called, and is meant to be used via a
7470 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7471 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7472   // Switch case IDs are per Decl.
7473   ClearSwitchCaseIDs();
7474 
7475   // Offset here is a global offset across the entire chain.
7476   RecordLocation Loc = getLocalBitOffset(Offset);
7477   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7478     Error(std::move(Err));
7479     return nullptr;
7480   }
7481   assert(NumCurrentElementsDeserializing == 0 &&
7482          "should not be called while already deserializing");
7483   Deserializing D(this);
7484   return ReadStmtFromStream(*Loc.F);
7485 }
7486 
7487 void ASTReader::FindExternalLexicalDecls(
7488     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7489     SmallVectorImpl<Decl *> &Decls) {
7490   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7491 
7492   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7493     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7494     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7495       auto K = (Decl::Kind)+LexicalDecls[I];
7496       if (!IsKindWeWant(K))
7497         continue;
7498 
7499       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7500 
7501       // Don't add predefined declarations to the lexical context more
7502       // than once.
7503       if (ID < NUM_PREDEF_DECL_IDS) {
7504         if (PredefsVisited[ID])
7505           continue;
7506 
7507         PredefsVisited[ID] = true;
7508       }
7509 
7510       if (Decl *D = GetLocalDecl(*M, ID)) {
7511         assert(D->getKind() == K && "wrong kind for lexical decl");
7512         if (!DC->isDeclInLexicalTraversal(D))
7513           Decls.push_back(D);
7514       }
7515     }
7516   };
7517 
7518   if (isa<TranslationUnitDecl>(DC)) {
7519     for (auto Lexical : TULexicalDecls)
7520       Visit(Lexical.first, Lexical.second);
7521   } else {
7522     auto I = LexicalDecls.find(DC);
7523     if (I != LexicalDecls.end())
7524       Visit(I->second.first, I->second.second);
7525   }
7526 
7527   ++NumLexicalDeclContextsRead;
7528 }
7529 
7530 namespace {
7531 
7532 class DeclIDComp {
7533   ASTReader &Reader;
7534   ModuleFile &Mod;
7535 
7536 public:
7537   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7538 
7539   bool operator()(LocalDeclID L, LocalDeclID R) const {
7540     SourceLocation LHS = getLocation(L);
7541     SourceLocation RHS = getLocation(R);
7542     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7543   }
7544 
7545   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7546     SourceLocation RHS = getLocation(R);
7547     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7548   }
7549 
7550   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7551     SourceLocation LHS = getLocation(L);
7552     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7553   }
7554 
7555   SourceLocation getLocation(LocalDeclID ID) const {
7556     return Reader.getSourceManager().getFileLoc(
7557             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7558   }
7559 };
7560 
7561 } // namespace
7562 
7563 void ASTReader::FindFileRegionDecls(FileID File,
7564                                     unsigned Offset, unsigned Length,
7565                                     SmallVectorImpl<Decl *> &Decls) {
7566   SourceManager &SM = getSourceManager();
7567 
7568   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7569   if (I == FileDeclIDs.end())
7570     return;
7571 
7572   FileDeclsInfo &DInfo = I->second;
7573   if (DInfo.Decls.empty())
7574     return;
7575 
7576   SourceLocation
7577     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7578   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7579 
7580   DeclIDComp DIDComp(*this, *DInfo.Mod);
7581   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7582       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7583   if (BeginIt != DInfo.Decls.begin())
7584     --BeginIt;
7585 
7586   // If we are pointing at a top-level decl inside an objc container, we need
7587   // to backtrack until we find it otherwise we will fail to report that the
7588   // region overlaps with an objc container.
7589   while (BeginIt != DInfo.Decls.begin() &&
7590          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7591              ->isTopLevelDeclInObjCContainer())
7592     --BeginIt;
7593 
7594   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7595       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7596   if (EndIt != DInfo.Decls.end())
7597     ++EndIt;
7598 
7599   for (ArrayRef<serialization::LocalDeclID>::iterator
7600          DIt = BeginIt; DIt != EndIt; ++DIt)
7601     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7602 }
7603 
7604 bool
7605 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7606                                           DeclarationName Name) {
7607   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7608          "DeclContext has no visible decls in storage");
7609   if (!Name)
7610     return false;
7611 
7612   auto It = Lookups.find(DC);
7613   if (It == Lookups.end())
7614     return false;
7615 
7616   Deserializing LookupResults(this);
7617 
7618   // Load the list of declarations.
7619   SmallVector<NamedDecl *, 64> Decls;
7620   for (DeclID ID : It->second.Table.find(Name)) {
7621     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7622     if (ND->getDeclName() == Name)
7623       Decls.push_back(ND);
7624   }
7625 
7626   ++NumVisibleDeclContextsRead;
7627   SetExternalVisibleDeclsForName(DC, Name, Decls);
7628   return !Decls.empty();
7629 }
7630 
7631 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7632   if (!DC->hasExternalVisibleStorage())
7633     return;
7634 
7635   auto It = Lookups.find(DC);
7636   assert(It != Lookups.end() &&
7637          "have external visible storage but no lookup tables");
7638 
7639   DeclsMap Decls;
7640 
7641   for (DeclID ID : It->second.Table.findAll()) {
7642     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7643     Decls[ND->getDeclName()].push_back(ND);
7644   }
7645 
7646   ++NumVisibleDeclContextsRead;
7647 
7648   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7649     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7650   }
7651   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7652 }
7653 
7654 const serialization::reader::DeclContextLookupTable *
7655 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7656   auto I = Lookups.find(Primary);
7657   return I == Lookups.end() ? nullptr : &I->second;
7658 }
7659 
7660 /// Under non-PCH compilation the consumer receives the objc methods
7661 /// before receiving the implementation, and codegen depends on this.
7662 /// We simulate this by deserializing and passing to consumer the methods of the
7663 /// implementation before passing the deserialized implementation decl.
7664 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7665                                        ASTConsumer *Consumer) {
7666   assert(ImplD && Consumer);
7667 
7668   for (auto *I : ImplD->methods())
7669     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7670 
7671   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7672 }
7673 
7674 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7675   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7676     PassObjCImplDeclToConsumer(ImplD, Consumer);
7677   else
7678     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7679 }
7680 
7681 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7682   this->Consumer = Consumer;
7683 
7684   if (Consumer)
7685     PassInterestingDeclsToConsumer();
7686 
7687   if (DeserializationListener)
7688     DeserializationListener->ReaderInitialized(this);
7689 }
7690 
7691 void ASTReader::PrintStats() {
7692   std::fprintf(stderr, "*** AST File Statistics:\n");
7693 
7694   unsigned NumTypesLoaded
7695     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7696                                       QualType());
7697   unsigned NumDeclsLoaded
7698     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7699                                       (Decl *)nullptr);
7700   unsigned NumIdentifiersLoaded
7701     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7702                                             IdentifiersLoaded.end(),
7703                                             (IdentifierInfo *)nullptr);
7704   unsigned NumMacrosLoaded
7705     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7706                                        MacrosLoaded.end(),
7707                                        (MacroInfo *)nullptr);
7708   unsigned NumSelectorsLoaded
7709     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7710                                           SelectorsLoaded.end(),
7711                                           Selector());
7712 
7713   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7714     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7715                  NumSLocEntriesRead, TotalNumSLocEntries,
7716                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7717   if (!TypesLoaded.empty())
7718     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7719                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7720                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7721   if (!DeclsLoaded.empty())
7722     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7723                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7724                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7725   if (!IdentifiersLoaded.empty())
7726     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7727                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7728                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7729   if (!MacrosLoaded.empty())
7730     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7731                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7732                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7733   if (!SelectorsLoaded.empty())
7734     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7735                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7736                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7737   if (TotalNumStatements)
7738     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7739                  NumStatementsRead, TotalNumStatements,
7740                  ((float)NumStatementsRead/TotalNumStatements * 100));
7741   if (TotalNumMacros)
7742     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7743                  NumMacrosRead, TotalNumMacros,
7744                  ((float)NumMacrosRead/TotalNumMacros * 100));
7745   if (TotalLexicalDeclContexts)
7746     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7747                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7748                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7749                   * 100));
7750   if (TotalVisibleDeclContexts)
7751     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7752                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7753                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7754                   * 100));
7755   if (TotalNumMethodPoolEntries)
7756     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7757                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7758                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7759                   * 100));
7760   if (NumMethodPoolLookups)
7761     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7762                  NumMethodPoolHits, NumMethodPoolLookups,
7763                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7764   if (NumMethodPoolTableLookups)
7765     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7766                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7767                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7768                   * 100.0));
7769   if (NumIdentifierLookupHits)
7770     std::fprintf(stderr,
7771                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7772                  NumIdentifierLookupHits, NumIdentifierLookups,
7773                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7774 
7775   if (GlobalIndex) {
7776     std::fprintf(stderr, "\n");
7777     GlobalIndex->printStats();
7778   }
7779 
7780   std::fprintf(stderr, "\n");
7781   dump();
7782   std::fprintf(stderr, "\n");
7783 }
7784 
7785 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7786 LLVM_DUMP_METHOD static void
7787 dumpModuleIDMap(StringRef Name,
7788                 const ContinuousRangeMap<Key, ModuleFile *,
7789                                          InitialCapacity> &Map) {
7790   if (Map.begin() == Map.end())
7791     return;
7792 
7793   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7794 
7795   llvm::errs() << Name << ":\n";
7796   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7797        I != IEnd; ++I) {
7798     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7799       << "\n";
7800   }
7801 }
7802 
7803 LLVM_DUMP_METHOD void ASTReader::dump() {
7804   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7805   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7806   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7807   dumpModuleIDMap("Global type map", GlobalTypeMap);
7808   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7809   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7810   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7811   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7812   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7813   dumpModuleIDMap("Global preprocessed entity map",
7814                   GlobalPreprocessedEntityMap);
7815 
7816   llvm::errs() << "\n*** PCH/Modules Loaded:";
7817   for (ModuleFile &M : ModuleMgr)
7818     M.dump();
7819 }
7820 
7821 /// Return the amount of memory used by memory buffers, breaking down
7822 /// by heap-backed versus mmap'ed memory.
7823 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7824   for (ModuleFile &I : ModuleMgr) {
7825     if (llvm::MemoryBuffer *buf = I.Buffer) {
7826       size_t bytes = buf->getBufferSize();
7827       switch (buf->getBufferKind()) {
7828         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7829           sizes.malloc_bytes += bytes;
7830           break;
7831         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7832           sizes.mmap_bytes += bytes;
7833           break;
7834       }
7835     }
7836   }
7837 }
7838 
7839 void ASTReader::InitializeSema(Sema &S) {
7840   SemaObj = &S;
7841   S.addExternalSource(this);
7842 
7843   // Makes sure any declarations that were deserialized "too early"
7844   // still get added to the identifier's declaration chains.
7845   for (uint64_t ID : PreloadedDeclIDs) {
7846     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7847     pushExternalDeclIntoScope(D, D->getDeclName());
7848   }
7849   PreloadedDeclIDs.clear();
7850 
7851   // FIXME: What happens if these are changed by a module import?
7852   if (!FPPragmaOptions.empty()) {
7853     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7854     FPOptionsOverride NewOverrides =
7855         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7856     SemaObj->CurFPFeatures =
7857         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7858   }
7859 
7860   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7861   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7862   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7863 
7864   UpdateSema();
7865 }
7866 
7867 void ASTReader::UpdateSema() {
7868   assert(SemaObj && "no Sema to update");
7869 
7870   // Load the offsets of the declarations that Sema references.
7871   // They will be lazily deserialized when needed.
7872   if (!SemaDeclRefs.empty()) {
7873     assert(SemaDeclRefs.size() % 3 == 0);
7874     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7875       if (!SemaObj->StdNamespace)
7876         SemaObj->StdNamespace = SemaDeclRefs[I];
7877       if (!SemaObj->StdBadAlloc)
7878         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7879       if (!SemaObj->StdAlignValT)
7880         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7881     }
7882     SemaDeclRefs.clear();
7883   }
7884 
7885   // Update the state of pragmas. Use the same API as if we had encountered the
7886   // pragma in the source.
7887   if(OptimizeOffPragmaLocation.isValid())
7888     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7889   if (PragmaMSStructState != -1)
7890     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7891   if (PointersToMembersPragmaLocation.isValid()) {
7892     SemaObj->ActOnPragmaMSPointersToMembers(
7893         (LangOptions::PragmaMSPointersToMembersKind)
7894             PragmaMSPointersToMembersState,
7895         PointersToMembersPragmaLocation);
7896   }
7897   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7898 
7899   if (PragmaPackCurrentValue) {
7900     // The bottom of the stack might have a default value. It must be adjusted
7901     // to the current value to ensure that the packing state is preserved after
7902     // popping entries that were included/imported from a PCH/module.
7903     bool DropFirst = false;
7904     if (!PragmaPackStack.empty() &&
7905         PragmaPackStack.front().Location.isInvalid()) {
7906       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7907              "Expected a default alignment value");
7908       SemaObj->PackStack.Stack.emplace_back(
7909           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7910           SemaObj->PackStack.CurrentPragmaLocation,
7911           PragmaPackStack.front().PushLocation);
7912       DropFirst = true;
7913     }
7914     for (const auto &Entry :
7915          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7916       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7917                                             Entry.Location, Entry.PushLocation);
7918     if (PragmaPackCurrentLocation.isInvalid()) {
7919       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7920              "Expected a default alignment value");
7921       // Keep the current values.
7922     } else {
7923       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7924       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7925     }
7926   }
7927   if (FpPragmaCurrentValue) {
7928     // The bottom of the stack might have a default value. It must be adjusted
7929     // to the current value to ensure that fp-pragma state is preserved after
7930     // popping entries that were included/imported from a PCH/module.
7931     bool DropFirst = false;
7932     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7933       assert(FpPragmaStack.front().Value ==
7934                  SemaObj->FpPragmaStack.DefaultValue &&
7935              "Expected a default pragma float_control value");
7936       SemaObj->FpPragmaStack.Stack.emplace_back(
7937           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7938           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7939           FpPragmaStack.front().PushLocation);
7940       DropFirst = true;
7941     }
7942     for (const auto &Entry :
7943          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7944       SemaObj->FpPragmaStack.Stack.emplace_back(
7945           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7946     if (FpPragmaCurrentLocation.isInvalid()) {
7947       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7948              "Expected a default pragma float_control value");
7949       // Keep the current values.
7950     } else {
7951       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7952       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7953     }
7954   }
7955 
7956   // For non-modular AST files, restore visiblity of modules.
7957   for (auto &Import : ImportedModules) {
7958     if (Import.ImportLoc.isInvalid())
7959       continue;
7960     if (Module *Imported = getSubmodule(Import.ID)) {
7961       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7962     }
7963   }
7964 }
7965 
7966 IdentifierInfo *ASTReader::get(StringRef Name) {
7967   // Note that we are loading an identifier.
7968   Deserializing AnIdentifier(this);
7969 
7970   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7971                                   NumIdentifierLookups,
7972                                   NumIdentifierLookupHits);
7973 
7974   // We don't need to do identifier table lookups in C++ modules (we preload
7975   // all interesting declarations, and don't need to use the scope for name
7976   // lookups). Perform the lookup in PCH files, though, since we don't build
7977   // a complete initial identifier table if we're carrying on from a PCH.
7978   if (PP.getLangOpts().CPlusPlus) {
7979     for (auto F : ModuleMgr.pch_modules())
7980       if (Visitor(*F))
7981         break;
7982   } else {
7983     // If there is a global index, look there first to determine which modules
7984     // provably do not have any results for this identifier.
7985     GlobalModuleIndex::HitSet Hits;
7986     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7987     if (!loadGlobalIndex()) {
7988       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7989         HitsPtr = &Hits;
7990       }
7991     }
7992 
7993     ModuleMgr.visit(Visitor, HitsPtr);
7994   }
7995 
7996   IdentifierInfo *II = Visitor.getIdentifierInfo();
7997   markIdentifierUpToDate(II);
7998   return II;
7999 }
8000 
8001 namespace clang {
8002 
8003   /// An identifier-lookup iterator that enumerates all of the
8004   /// identifiers stored within a set of AST files.
8005   class ASTIdentifierIterator : public IdentifierIterator {
8006     /// The AST reader whose identifiers are being enumerated.
8007     const ASTReader &Reader;
8008 
8009     /// The current index into the chain of AST files stored in
8010     /// the AST reader.
8011     unsigned Index;
8012 
8013     /// The current position within the identifier lookup table
8014     /// of the current AST file.
8015     ASTIdentifierLookupTable::key_iterator Current;
8016 
8017     /// The end position within the identifier lookup table of
8018     /// the current AST file.
8019     ASTIdentifierLookupTable::key_iterator End;
8020 
8021     /// Whether to skip any modules in the ASTReader.
8022     bool SkipModules;
8023 
8024   public:
8025     explicit ASTIdentifierIterator(const ASTReader &Reader,
8026                                    bool SkipModules = false);
8027 
8028     StringRef Next() override;
8029   };
8030 
8031 } // namespace clang
8032 
8033 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8034                                              bool SkipModules)
8035     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8036 }
8037 
8038 StringRef ASTIdentifierIterator::Next() {
8039   while (Current == End) {
8040     // If we have exhausted all of our AST files, we're done.
8041     if (Index == 0)
8042       return StringRef();
8043 
8044     --Index;
8045     ModuleFile &F = Reader.ModuleMgr[Index];
8046     if (SkipModules && F.isModule())
8047       continue;
8048 
8049     ASTIdentifierLookupTable *IdTable =
8050         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8051     Current = IdTable->key_begin();
8052     End = IdTable->key_end();
8053   }
8054 
8055   // We have any identifiers remaining in the current AST file; return
8056   // the next one.
8057   StringRef Result = *Current;
8058   ++Current;
8059   return Result;
8060 }
8061 
8062 namespace {
8063 
8064 /// A utility for appending two IdentifierIterators.
8065 class ChainedIdentifierIterator : public IdentifierIterator {
8066   std::unique_ptr<IdentifierIterator> Current;
8067   std::unique_ptr<IdentifierIterator> Queued;
8068 
8069 public:
8070   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8071                             std::unique_ptr<IdentifierIterator> Second)
8072       : Current(std::move(First)), Queued(std::move(Second)) {}
8073 
8074   StringRef Next() override {
8075     if (!Current)
8076       return StringRef();
8077 
8078     StringRef result = Current->Next();
8079     if (!result.empty())
8080       return result;
8081 
8082     // Try the queued iterator, which may itself be empty.
8083     Current.reset();
8084     std::swap(Current, Queued);
8085     return Next();
8086   }
8087 };
8088 
8089 } // namespace
8090 
8091 IdentifierIterator *ASTReader::getIdentifiers() {
8092   if (!loadGlobalIndex()) {
8093     std::unique_ptr<IdentifierIterator> ReaderIter(
8094         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8095     std::unique_ptr<IdentifierIterator> ModulesIter(
8096         GlobalIndex->createIdentifierIterator());
8097     return new ChainedIdentifierIterator(std::move(ReaderIter),
8098                                          std::move(ModulesIter));
8099   }
8100 
8101   return new ASTIdentifierIterator(*this);
8102 }
8103 
8104 namespace clang {
8105 namespace serialization {
8106 
8107   class ReadMethodPoolVisitor {
8108     ASTReader &Reader;
8109     Selector Sel;
8110     unsigned PriorGeneration;
8111     unsigned InstanceBits = 0;
8112     unsigned FactoryBits = 0;
8113     bool InstanceHasMoreThanOneDecl = false;
8114     bool FactoryHasMoreThanOneDecl = false;
8115     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8116     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8117 
8118   public:
8119     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8120                           unsigned PriorGeneration)
8121         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8122 
8123     bool operator()(ModuleFile &M) {
8124       if (!M.SelectorLookupTable)
8125         return false;
8126 
8127       // If we've already searched this module file, skip it now.
8128       if (M.Generation <= PriorGeneration)
8129         return true;
8130 
8131       ++Reader.NumMethodPoolTableLookups;
8132       ASTSelectorLookupTable *PoolTable
8133         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8134       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8135       if (Pos == PoolTable->end())
8136         return false;
8137 
8138       ++Reader.NumMethodPoolTableHits;
8139       ++Reader.NumSelectorsRead;
8140       // FIXME: Not quite happy with the statistics here. We probably should
8141       // disable this tracking when called via LoadSelector.
8142       // Also, should entries without methods count as misses?
8143       ++Reader.NumMethodPoolEntriesRead;
8144       ASTSelectorLookupTrait::data_type Data = *Pos;
8145       if (Reader.DeserializationListener)
8146         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8147 
8148       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8149       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8150       InstanceBits = Data.InstanceBits;
8151       FactoryBits = Data.FactoryBits;
8152       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8153       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8154       return true;
8155     }
8156 
8157     /// Retrieve the instance methods found by this visitor.
8158     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8159       return InstanceMethods;
8160     }
8161 
8162     /// Retrieve the instance methods found by this visitor.
8163     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8164       return FactoryMethods;
8165     }
8166 
8167     unsigned getInstanceBits() const { return InstanceBits; }
8168     unsigned getFactoryBits() const { return FactoryBits; }
8169 
8170     bool instanceHasMoreThanOneDecl() const {
8171       return InstanceHasMoreThanOneDecl;
8172     }
8173 
8174     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8175   };
8176 
8177 } // namespace serialization
8178 } // namespace clang
8179 
8180 /// Add the given set of methods to the method list.
8181 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8182                              ObjCMethodList &List) {
8183   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8184     S.addMethodToGlobalList(&List, Methods[I]);
8185   }
8186 }
8187 
8188 void ASTReader::ReadMethodPool(Selector Sel) {
8189   // Get the selector generation and update it to the current generation.
8190   unsigned &Generation = SelectorGeneration[Sel];
8191   unsigned PriorGeneration = Generation;
8192   Generation = getGeneration();
8193   SelectorOutOfDate[Sel] = false;
8194 
8195   // Search for methods defined with this selector.
8196   ++NumMethodPoolLookups;
8197   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8198   ModuleMgr.visit(Visitor);
8199 
8200   if (Visitor.getInstanceMethods().empty() &&
8201       Visitor.getFactoryMethods().empty())
8202     return;
8203 
8204   ++NumMethodPoolHits;
8205 
8206   if (!getSema())
8207     return;
8208 
8209   Sema &S = *getSema();
8210   Sema::GlobalMethodPool::iterator Pos
8211     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8212 
8213   Pos->second.first.setBits(Visitor.getInstanceBits());
8214   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8215   Pos->second.second.setBits(Visitor.getFactoryBits());
8216   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8217 
8218   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8219   // when building a module we keep every method individually and may need to
8220   // update hasMoreThanOneDecl as we add the methods.
8221   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8222   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8223 }
8224 
8225 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8226   if (SelectorOutOfDate[Sel])
8227     ReadMethodPool(Sel);
8228 }
8229 
8230 void ASTReader::ReadKnownNamespaces(
8231                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8232   Namespaces.clear();
8233 
8234   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8235     if (NamespaceDecl *Namespace
8236                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8237       Namespaces.push_back(Namespace);
8238   }
8239 }
8240 
8241 void ASTReader::ReadUndefinedButUsed(
8242     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8243   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8244     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8245     SourceLocation Loc =
8246         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8247     Undefined.insert(std::make_pair(D, Loc));
8248   }
8249 }
8250 
8251 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8252     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8253                                                      Exprs) {
8254   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8255     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8256     uint64_t Count = DelayedDeleteExprs[Idx++];
8257     for (uint64_t C = 0; C < Count; ++C) {
8258       SourceLocation DeleteLoc =
8259           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8260       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8261       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8262     }
8263   }
8264 }
8265 
8266 void ASTReader::ReadTentativeDefinitions(
8267                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8268   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8269     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8270     if (Var)
8271       TentativeDefs.push_back(Var);
8272   }
8273   TentativeDefinitions.clear();
8274 }
8275 
8276 void ASTReader::ReadUnusedFileScopedDecls(
8277                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8278   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8279     DeclaratorDecl *D
8280       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8281     if (D)
8282       Decls.push_back(D);
8283   }
8284   UnusedFileScopedDecls.clear();
8285 }
8286 
8287 void ASTReader::ReadDelegatingConstructors(
8288                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8289   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8290     CXXConstructorDecl *D
8291       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8292     if (D)
8293       Decls.push_back(D);
8294   }
8295   DelegatingCtorDecls.clear();
8296 }
8297 
8298 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8299   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8300     TypedefNameDecl *D
8301       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8302     if (D)
8303       Decls.push_back(D);
8304   }
8305   ExtVectorDecls.clear();
8306 }
8307 
8308 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8309     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8310   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8311        ++I) {
8312     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8313         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8314     if (D)
8315       Decls.insert(D);
8316   }
8317   UnusedLocalTypedefNameCandidates.clear();
8318 }
8319 
8320 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8321     llvm::SmallVector<Decl *, 4> &Decls) {
8322   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8323        ++I) {
8324     auto *D = dyn_cast_or_null<Decl>(
8325         GetDecl(DeclsToCheckForDeferredDiags[I]));
8326     if (D)
8327       Decls.push_back(D);
8328   }
8329   DeclsToCheckForDeferredDiags.clear();
8330 }
8331 
8332 
8333 void ASTReader::ReadReferencedSelectors(
8334        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8335   if (ReferencedSelectorsData.empty())
8336     return;
8337 
8338   // If there are @selector references added them to its pool. This is for
8339   // implementation of -Wselector.
8340   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8341   unsigned I = 0;
8342   while (I < DataSize) {
8343     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8344     SourceLocation SelLoc
8345       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8346     Sels.push_back(std::make_pair(Sel, SelLoc));
8347   }
8348   ReferencedSelectorsData.clear();
8349 }
8350 
8351 void ASTReader::ReadWeakUndeclaredIdentifiers(
8352        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8353   if (WeakUndeclaredIdentifiers.empty())
8354     return;
8355 
8356   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8357     IdentifierInfo *WeakId
8358       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8359     IdentifierInfo *AliasId
8360       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8361     SourceLocation Loc
8362       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8363     bool Used = WeakUndeclaredIdentifiers[I++];
8364     WeakInfo WI(AliasId, Loc);
8365     WI.setUsed(Used);
8366     WeakIDs.push_back(std::make_pair(WeakId, WI));
8367   }
8368   WeakUndeclaredIdentifiers.clear();
8369 }
8370 
8371 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8372   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8373     ExternalVTableUse VT;
8374     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8375     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8376     VT.DefinitionRequired = VTableUses[Idx++];
8377     VTables.push_back(VT);
8378   }
8379 
8380   VTableUses.clear();
8381 }
8382 
8383 void ASTReader::ReadPendingInstantiations(
8384        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8385   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8386     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8387     SourceLocation Loc
8388       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8389 
8390     Pending.push_back(std::make_pair(D, Loc));
8391   }
8392   PendingInstantiations.clear();
8393 }
8394 
8395 void ASTReader::ReadLateParsedTemplates(
8396     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8397         &LPTMap) {
8398   for (auto &LPT : LateParsedTemplates) {
8399     ModuleFile *FMod = LPT.first;
8400     RecordDataImpl &LateParsed = LPT.second;
8401     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8402          /* In loop */) {
8403       FunctionDecl *FD =
8404           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8405 
8406       auto LT = std::make_unique<LateParsedTemplate>();
8407       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8408 
8409       ModuleFile *F = getOwningModuleFile(LT->D);
8410       assert(F && "No module");
8411 
8412       unsigned TokN = LateParsed[Idx++];
8413       LT->Toks.reserve(TokN);
8414       for (unsigned T = 0; T < TokN; ++T)
8415         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8416 
8417       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8418     }
8419   }
8420 }
8421 
8422 void ASTReader::LoadSelector(Selector Sel) {
8423   // It would be complicated to avoid reading the methods anyway. So don't.
8424   ReadMethodPool(Sel);
8425 }
8426 
8427 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8428   assert(ID && "Non-zero identifier ID required");
8429   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8430   IdentifiersLoaded[ID - 1] = II;
8431   if (DeserializationListener)
8432     DeserializationListener->IdentifierRead(ID, II);
8433 }
8434 
8435 /// Set the globally-visible declarations associated with the given
8436 /// identifier.
8437 ///
8438 /// If the AST reader is currently in a state where the given declaration IDs
8439 /// cannot safely be resolved, they are queued until it is safe to resolve
8440 /// them.
8441 ///
8442 /// \param II an IdentifierInfo that refers to one or more globally-visible
8443 /// declarations.
8444 ///
8445 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8446 /// visible at global scope.
8447 ///
8448 /// \param Decls if non-null, this vector will be populated with the set of
8449 /// deserialized declarations. These declarations will not be pushed into
8450 /// scope.
8451 void
8452 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8453                               const SmallVectorImpl<uint32_t> &DeclIDs,
8454                                    SmallVectorImpl<Decl *> *Decls) {
8455   if (NumCurrentElementsDeserializing && !Decls) {
8456     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8457     return;
8458   }
8459 
8460   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8461     if (!SemaObj) {
8462       // Queue this declaration so that it will be added to the
8463       // translation unit scope and identifier's declaration chain
8464       // once a Sema object is known.
8465       PreloadedDeclIDs.push_back(DeclIDs[I]);
8466       continue;
8467     }
8468 
8469     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8470 
8471     // If we're simply supposed to record the declarations, do so now.
8472     if (Decls) {
8473       Decls->push_back(D);
8474       continue;
8475     }
8476 
8477     // Introduce this declaration into the translation-unit scope
8478     // and add it to the declaration chain for this identifier, so
8479     // that (unqualified) name lookup will find it.
8480     pushExternalDeclIntoScope(D, II);
8481   }
8482 }
8483 
8484 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8485   if (ID == 0)
8486     return nullptr;
8487 
8488   if (IdentifiersLoaded.empty()) {
8489     Error("no identifier table in AST file");
8490     return nullptr;
8491   }
8492 
8493   ID -= 1;
8494   if (!IdentifiersLoaded[ID]) {
8495     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8496     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8497     ModuleFile *M = I->second;
8498     unsigned Index = ID - M->BaseIdentifierID;
8499     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8500 
8501     // All of the strings in the AST file are preceded by a 16-bit length.
8502     // Extract that 16-bit length to avoid having to execute strlen().
8503     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8504     //  unsigned integers.  This is important to avoid integer overflow when
8505     //  we cast them to 'unsigned'.
8506     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8507     unsigned StrLen = (((unsigned) StrLenPtr[0])
8508                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8509     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8510     IdentifiersLoaded[ID] = &II;
8511     markIdentifierFromAST(*this,  II);
8512     if (DeserializationListener)
8513       DeserializationListener->IdentifierRead(ID + 1, &II);
8514   }
8515 
8516   return IdentifiersLoaded[ID];
8517 }
8518 
8519 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8520   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8521 }
8522 
8523 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8524   if (LocalID < NUM_PREDEF_IDENT_IDS)
8525     return LocalID;
8526 
8527   if (!M.ModuleOffsetMap.empty())
8528     ReadModuleOffsetMap(M);
8529 
8530   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8531     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8532   assert(I != M.IdentifierRemap.end()
8533          && "Invalid index into identifier index remap");
8534 
8535   return LocalID + I->second;
8536 }
8537 
8538 MacroInfo *ASTReader::getMacro(MacroID ID) {
8539   if (ID == 0)
8540     return nullptr;
8541 
8542   if (MacrosLoaded.empty()) {
8543     Error("no macro table in AST file");
8544     return nullptr;
8545   }
8546 
8547   ID -= NUM_PREDEF_MACRO_IDS;
8548   if (!MacrosLoaded[ID]) {
8549     GlobalMacroMapType::iterator I
8550       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8551     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8552     ModuleFile *M = I->second;
8553     unsigned Index = ID - M->BaseMacroID;
8554     MacrosLoaded[ID] =
8555         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8556 
8557     if (DeserializationListener)
8558       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8559                                          MacrosLoaded[ID]);
8560   }
8561 
8562   return MacrosLoaded[ID];
8563 }
8564 
8565 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8566   if (LocalID < NUM_PREDEF_MACRO_IDS)
8567     return LocalID;
8568 
8569   if (!M.ModuleOffsetMap.empty())
8570     ReadModuleOffsetMap(M);
8571 
8572   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8573     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8574   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8575 
8576   return LocalID + I->second;
8577 }
8578 
8579 serialization::SubmoduleID
8580 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8581   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8582     return LocalID;
8583 
8584   if (!M.ModuleOffsetMap.empty())
8585     ReadModuleOffsetMap(M);
8586 
8587   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8588     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8589   assert(I != M.SubmoduleRemap.end()
8590          && "Invalid index into submodule index remap");
8591 
8592   return LocalID + I->second;
8593 }
8594 
8595 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8596   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8597     assert(GlobalID == 0 && "Unhandled global submodule ID");
8598     return nullptr;
8599   }
8600 
8601   if (GlobalID > SubmodulesLoaded.size()) {
8602     Error("submodule ID out of range in AST file");
8603     return nullptr;
8604   }
8605 
8606   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8607 }
8608 
8609 Module *ASTReader::getModule(unsigned ID) {
8610   return getSubmodule(ID);
8611 }
8612 
8613 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8614   if (ID & 1) {
8615     // It's a module, look it up by submodule ID.
8616     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8617     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8618   } else {
8619     // It's a prefix (preamble, PCH, ...). Look it up by index.
8620     unsigned IndexFromEnd = ID >> 1;
8621     assert(IndexFromEnd && "got reference to unknown module file");
8622     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8623   }
8624 }
8625 
8626 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8627   if (!F)
8628     return 1;
8629 
8630   // For a file representing a module, use the submodule ID of the top-level
8631   // module as the file ID. For any other kind of file, the number of such
8632   // files loaded beforehand will be the same on reload.
8633   // FIXME: Is this true even if we have an explicit module file and a PCH?
8634   if (F->isModule())
8635     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8636 
8637   auto PCHModules = getModuleManager().pch_modules();
8638   auto I = llvm::find(PCHModules, F);
8639   assert(I != PCHModules.end() && "emitting reference to unknown file");
8640   return (I - PCHModules.end()) << 1;
8641 }
8642 
8643 llvm::Optional<ASTSourceDescriptor>
8644 ASTReader::getSourceDescriptor(unsigned ID) {
8645   if (Module *M = getSubmodule(ID))
8646     return ASTSourceDescriptor(*M);
8647 
8648   // If there is only a single PCH, return it instead.
8649   // Chained PCH are not supported.
8650   const auto &PCHChain = ModuleMgr.pch_modules();
8651   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8652     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8653     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8654     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8655     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8656                                MF.Signature);
8657   }
8658   return None;
8659 }
8660 
8661 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8662   auto I = DefinitionSource.find(FD);
8663   if (I == DefinitionSource.end())
8664     return EK_ReplyHazy;
8665   return I->second ? EK_Never : EK_Always;
8666 }
8667 
8668 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8669   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8670 }
8671 
8672 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8673   if (ID == 0)
8674     return Selector();
8675 
8676   if (ID > SelectorsLoaded.size()) {
8677     Error("selector ID out of range in AST file");
8678     return Selector();
8679   }
8680 
8681   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8682     // Load this selector from the selector table.
8683     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8684     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8685     ModuleFile &M = *I->second;
8686     ASTSelectorLookupTrait Trait(*this, M);
8687     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8688     SelectorsLoaded[ID - 1] =
8689       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8690     if (DeserializationListener)
8691       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8692   }
8693 
8694   return SelectorsLoaded[ID - 1];
8695 }
8696 
8697 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8698   return DecodeSelector(ID);
8699 }
8700 
8701 uint32_t ASTReader::GetNumExternalSelectors() {
8702   // ID 0 (the null selector) is considered an external selector.
8703   return getTotalNumSelectors() + 1;
8704 }
8705 
8706 serialization::SelectorID
8707 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8708   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8709     return LocalID;
8710 
8711   if (!M.ModuleOffsetMap.empty())
8712     ReadModuleOffsetMap(M);
8713 
8714   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8715     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8716   assert(I != M.SelectorRemap.end()
8717          && "Invalid index into selector index remap");
8718 
8719   return LocalID + I->second;
8720 }
8721 
8722 DeclarationNameLoc
8723 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8724   DeclarationNameLoc DNLoc;
8725   switch (Name.getNameKind()) {
8726   case DeclarationName::CXXConstructorName:
8727   case DeclarationName::CXXDestructorName:
8728   case DeclarationName::CXXConversionFunctionName:
8729     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8730     break;
8731 
8732   case DeclarationName::CXXOperatorName:
8733     DNLoc.CXXOperatorName.BeginOpNameLoc
8734       = readSourceLocation().getRawEncoding();
8735     DNLoc.CXXOperatorName.EndOpNameLoc
8736       = readSourceLocation().getRawEncoding();
8737     break;
8738 
8739   case DeclarationName::CXXLiteralOperatorName:
8740     DNLoc.CXXLiteralOperatorName.OpNameLoc
8741       = readSourceLocation().getRawEncoding();
8742     break;
8743 
8744   case DeclarationName::Identifier:
8745   case DeclarationName::ObjCZeroArgSelector:
8746   case DeclarationName::ObjCOneArgSelector:
8747   case DeclarationName::ObjCMultiArgSelector:
8748   case DeclarationName::CXXUsingDirective:
8749   case DeclarationName::CXXDeductionGuideName:
8750     break;
8751   }
8752   return DNLoc;
8753 }
8754 
8755 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8756   DeclarationNameInfo NameInfo;
8757   NameInfo.setName(readDeclarationName());
8758   NameInfo.setLoc(readSourceLocation());
8759   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8760   return NameInfo;
8761 }
8762 
8763 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8764   Info.QualifierLoc = readNestedNameSpecifierLoc();
8765   unsigned NumTPLists = readInt();
8766   Info.NumTemplParamLists = NumTPLists;
8767   if (NumTPLists) {
8768     Info.TemplParamLists =
8769         new (getContext()) TemplateParameterList *[NumTPLists];
8770     for (unsigned i = 0; i != NumTPLists; ++i)
8771       Info.TemplParamLists[i] = readTemplateParameterList();
8772   }
8773 }
8774 
8775 TemplateParameterList *
8776 ASTRecordReader::readTemplateParameterList() {
8777   SourceLocation TemplateLoc = readSourceLocation();
8778   SourceLocation LAngleLoc = readSourceLocation();
8779   SourceLocation RAngleLoc = readSourceLocation();
8780 
8781   unsigned NumParams = readInt();
8782   SmallVector<NamedDecl *, 16> Params;
8783   Params.reserve(NumParams);
8784   while (NumParams--)
8785     Params.push_back(readDeclAs<NamedDecl>());
8786 
8787   bool HasRequiresClause = readBool();
8788   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8789 
8790   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8791       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8792   return TemplateParams;
8793 }
8794 
8795 void ASTRecordReader::readTemplateArgumentList(
8796                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8797                         bool Canonicalize) {
8798   unsigned NumTemplateArgs = readInt();
8799   TemplArgs.reserve(NumTemplateArgs);
8800   while (NumTemplateArgs--)
8801     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8802 }
8803 
8804 /// Read a UnresolvedSet structure.
8805 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8806   unsigned NumDecls = readInt();
8807   Set.reserve(getContext(), NumDecls);
8808   while (NumDecls--) {
8809     DeclID ID = readDeclID();
8810     AccessSpecifier AS = (AccessSpecifier) readInt();
8811     Set.addLazyDecl(getContext(), ID, AS);
8812   }
8813 }
8814 
8815 CXXBaseSpecifier
8816 ASTRecordReader::readCXXBaseSpecifier() {
8817   bool isVirtual = readBool();
8818   bool isBaseOfClass = readBool();
8819   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8820   bool inheritConstructors = readBool();
8821   TypeSourceInfo *TInfo = readTypeSourceInfo();
8822   SourceRange Range = readSourceRange();
8823   SourceLocation EllipsisLoc = readSourceLocation();
8824   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8825                           EllipsisLoc);
8826   Result.setInheritConstructors(inheritConstructors);
8827   return Result;
8828 }
8829 
8830 CXXCtorInitializer **
8831 ASTRecordReader::readCXXCtorInitializers() {
8832   ASTContext &Context = getContext();
8833   unsigned NumInitializers = readInt();
8834   assert(NumInitializers && "wrote ctor initializers but have no inits");
8835   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8836   for (unsigned i = 0; i != NumInitializers; ++i) {
8837     TypeSourceInfo *TInfo = nullptr;
8838     bool IsBaseVirtual = false;
8839     FieldDecl *Member = nullptr;
8840     IndirectFieldDecl *IndirectMember = nullptr;
8841 
8842     CtorInitializerType Type = (CtorInitializerType) readInt();
8843     switch (Type) {
8844     case CTOR_INITIALIZER_BASE:
8845       TInfo = readTypeSourceInfo();
8846       IsBaseVirtual = readBool();
8847       break;
8848 
8849     case CTOR_INITIALIZER_DELEGATING:
8850       TInfo = readTypeSourceInfo();
8851       break;
8852 
8853      case CTOR_INITIALIZER_MEMBER:
8854       Member = readDeclAs<FieldDecl>();
8855       break;
8856 
8857      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8858       IndirectMember = readDeclAs<IndirectFieldDecl>();
8859       break;
8860     }
8861 
8862     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8863     Expr *Init = readExpr();
8864     SourceLocation LParenLoc = readSourceLocation();
8865     SourceLocation RParenLoc = readSourceLocation();
8866 
8867     CXXCtorInitializer *BOMInit;
8868     if (Type == CTOR_INITIALIZER_BASE)
8869       BOMInit = new (Context)
8870           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8871                              RParenLoc, MemberOrEllipsisLoc);
8872     else if (Type == CTOR_INITIALIZER_DELEGATING)
8873       BOMInit = new (Context)
8874           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8875     else if (Member)
8876       BOMInit = new (Context)
8877           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8878                              Init, RParenLoc);
8879     else
8880       BOMInit = new (Context)
8881           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8882                              LParenLoc, Init, RParenLoc);
8883 
8884     if (/*IsWritten*/readBool()) {
8885       unsigned SourceOrder = readInt();
8886       BOMInit->setSourceOrder(SourceOrder);
8887     }
8888 
8889     CtorInitializers[i] = BOMInit;
8890   }
8891 
8892   return CtorInitializers;
8893 }
8894 
8895 NestedNameSpecifierLoc
8896 ASTRecordReader::readNestedNameSpecifierLoc() {
8897   ASTContext &Context = getContext();
8898   unsigned N = readInt();
8899   NestedNameSpecifierLocBuilder Builder;
8900   for (unsigned I = 0; I != N; ++I) {
8901     auto Kind = readNestedNameSpecifierKind();
8902     switch (Kind) {
8903     case NestedNameSpecifier::Identifier: {
8904       IdentifierInfo *II = readIdentifier();
8905       SourceRange Range = readSourceRange();
8906       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8907       break;
8908     }
8909 
8910     case NestedNameSpecifier::Namespace: {
8911       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8912       SourceRange Range = readSourceRange();
8913       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8914       break;
8915     }
8916 
8917     case NestedNameSpecifier::NamespaceAlias: {
8918       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8919       SourceRange Range = readSourceRange();
8920       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8921       break;
8922     }
8923 
8924     case NestedNameSpecifier::TypeSpec:
8925     case NestedNameSpecifier::TypeSpecWithTemplate: {
8926       bool Template = readBool();
8927       TypeSourceInfo *T = readTypeSourceInfo();
8928       if (!T)
8929         return NestedNameSpecifierLoc();
8930       SourceLocation ColonColonLoc = readSourceLocation();
8931 
8932       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8933       Builder.Extend(Context,
8934                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8935                      T->getTypeLoc(), ColonColonLoc);
8936       break;
8937     }
8938 
8939     case NestedNameSpecifier::Global: {
8940       SourceLocation ColonColonLoc = readSourceLocation();
8941       Builder.MakeGlobal(Context, ColonColonLoc);
8942       break;
8943     }
8944 
8945     case NestedNameSpecifier::Super: {
8946       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8947       SourceRange Range = readSourceRange();
8948       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8949       break;
8950     }
8951     }
8952   }
8953 
8954   return Builder.getWithLocInContext(Context);
8955 }
8956 
8957 SourceRange
8958 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8959                            unsigned &Idx) {
8960   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8961   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8962   return SourceRange(beg, end);
8963 }
8964 
8965 static llvm::FixedPointSemantics
8966 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8967                         unsigned &Idx) {
8968   unsigned Width = Record[Idx++];
8969   unsigned Scale = Record[Idx++];
8970   uint64_t Tmp = Record[Idx++];
8971   bool IsSigned = Tmp & 0x1;
8972   bool IsSaturated = Tmp & 0x2;
8973   bool HasUnsignedPadding = Tmp & 0x4;
8974   return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8975                                    HasUnsignedPadding);
8976 }
8977 
8978 static const llvm::fltSemantics &
8979 readAPFloatSemantics(ASTRecordReader &reader) {
8980   return llvm::APFloatBase::EnumToSemantics(
8981     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8982 }
8983 
8984 APValue ASTRecordReader::readAPValue() {
8985   unsigned Kind = readInt();
8986   switch ((APValue::ValueKind) Kind) {
8987   case APValue::None:
8988     return APValue();
8989   case APValue::Indeterminate:
8990     return APValue::IndeterminateValue();
8991   case APValue::Int:
8992     return APValue(readAPSInt());
8993   case APValue::Float: {
8994     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8995     return APValue(readAPFloat(FloatSema));
8996   }
8997   case APValue::FixedPoint: {
8998     llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8999     return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
9000   }
9001   case APValue::ComplexInt: {
9002     llvm::APSInt First = readAPSInt();
9003     return APValue(std::move(First), readAPSInt());
9004   }
9005   case APValue::ComplexFloat: {
9006     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
9007     llvm::APFloat First = readAPFloat(FloatSema1);
9008     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
9009     return APValue(std::move(First), readAPFloat(FloatSema2));
9010   }
9011   case APValue::LValue:
9012   case APValue::Vector:
9013   case APValue::Array:
9014   case APValue::Struct:
9015   case APValue::Union:
9016   case APValue::MemberPointer:
9017   case APValue::AddrLabelDiff:
9018     // TODO : Handle all these APValue::ValueKind.
9019     return APValue();
9020   }
9021   llvm_unreachable("Invalid APValue::ValueKind");
9022 }
9023 
9024 /// Read a floating-point value
9025 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9026   return llvm::APFloat(Sem, readAPInt());
9027 }
9028 
9029 // Read a string
9030 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9031   unsigned Len = Record[Idx++];
9032   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9033   Idx += Len;
9034   return Result;
9035 }
9036 
9037 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9038                                 unsigned &Idx) {
9039   std::string Filename = ReadString(Record, Idx);
9040   ResolveImportedPath(F, Filename);
9041   return Filename;
9042 }
9043 
9044 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9045                                 const RecordData &Record, unsigned &Idx) {
9046   std::string Filename = ReadString(Record, Idx);
9047   if (!BaseDirectory.empty())
9048     ResolveImportedPath(Filename, BaseDirectory);
9049   return Filename;
9050 }
9051 
9052 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9053                                          unsigned &Idx) {
9054   unsigned Major = Record[Idx++];
9055   unsigned Minor = Record[Idx++];
9056   unsigned Subminor = Record[Idx++];
9057   if (Minor == 0)
9058     return VersionTuple(Major);
9059   if (Subminor == 0)
9060     return VersionTuple(Major, Minor - 1);
9061   return VersionTuple(Major, Minor - 1, Subminor - 1);
9062 }
9063 
9064 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9065                                           const RecordData &Record,
9066                                           unsigned &Idx) {
9067   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9068   return CXXTemporary::Create(getContext(), Decl);
9069 }
9070 
9071 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9072   return Diag(CurrentImportLoc, DiagID);
9073 }
9074 
9075 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9076   return Diags.Report(Loc, DiagID);
9077 }
9078 
9079 /// Retrieve the identifier table associated with the
9080 /// preprocessor.
9081 IdentifierTable &ASTReader::getIdentifierTable() {
9082   return PP.getIdentifierTable();
9083 }
9084 
9085 /// Record that the given ID maps to the given switch-case
9086 /// statement.
9087 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9088   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9089          "Already have a SwitchCase with this ID");
9090   (*CurrSwitchCaseStmts)[ID] = SC;
9091 }
9092 
9093 /// Retrieve the switch-case statement with the given ID.
9094 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9095   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9096   return (*CurrSwitchCaseStmts)[ID];
9097 }
9098 
9099 void ASTReader::ClearSwitchCaseIDs() {
9100   CurrSwitchCaseStmts->clear();
9101 }
9102 
9103 void ASTReader::ReadComments() {
9104   ASTContext &Context = getContext();
9105   std::vector<RawComment *> Comments;
9106   for (SmallVectorImpl<std::pair<BitstreamCursor,
9107                                  serialization::ModuleFile *>>::iterator
9108        I = CommentsCursors.begin(),
9109        E = CommentsCursors.end();
9110        I != E; ++I) {
9111     Comments.clear();
9112     BitstreamCursor &Cursor = I->first;
9113     serialization::ModuleFile &F = *I->second;
9114     SavedStreamPosition SavedPosition(Cursor);
9115 
9116     RecordData Record;
9117     while (true) {
9118       Expected<llvm::BitstreamEntry> MaybeEntry =
9119           Cursor.advanceSkippingSubblocks(
9120               BitstreamCursor::AF_DontPopBlockAtEnd);
9121       if (!MaybeEntry) {
9122         Error(MaybeEntry.takeError());
9123         return;
9124       }
9125       llvm::BitstreamEntry Entry = MaybeEntry.get();
9126 
9127       switch (Entry.Kind) {
9128       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9129       case llvm::BitstreamEntry::Error:
9130         Error("malformed block record in AST file");
9131         return;
9132       case llvm::BitstreamEntry::EndBlock:
9133         goto NextCursor;
9134       case llvm::BitstreamEntry::Record:
9135         // The interesting case.
9136         break;
9137       }
9138 
9139       // Read a record.
9140       Record.clear();
9141       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9142       if (!MaybeComment) {
9143         Error(MaybeComment.takeError());
9144         return;
9145       }
9146       switch ((CommentRecordTypes)MaybeComment.get()) {
9147       case COMMENTS_RAW_COMMENT: {
9148         unsigned Idx = 0;
9149         SourceRange SR = ReadSourceRange(F, Record, Idx);
9150         RawComment::CommentKind Kind =
9151             (RawComment::CommentKind) Record[Idx++];
9152         bool IsTrailingComment = Record[Idx++];
9153         bool IsAlmostTrailingComment = Record[Idx++];
9154         Comments.push_back(new (Context) RawComment(
9155             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9156         break;
9157       }
9158       }
9159     }
9160   NextCursor:
9161     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9162         FileToOffsetToComment;
9163     for (RawComment *C : Comments) {
9164       SourceLocation CommentLoc = C->getBeginLoc();
9165       if (CommentLoc.isValid()) {
9166         std::pair<FileID, unsigned> Loc =
9167             SourceMgr.getDecomposedLoc(CommentLoc);
9168         if (Loc.first.isValid())
9169           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9170       }
9171     }
9172   }
9173 }
9174 
9175 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9176                                 bool IncludeSystem, bool Complain,
9177                     llvm::function_ref<void(const serialization::InputFile &IF,
9178                                             bool isSystem)> Visitor) {
9179   unsigned NumUserInputs = MF.NumUserInputFiles;
9180   unsigned NumInputs = MF.InputFilesLoaded.size();
9181   assert(NumUserInputs <= NumInputs);
9182   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9183   for (unsigned I = 0; I < N; ++I) {
9184     bool IsSystem = I >= NumUserInputs;
9185     InputFile IF = getInputFile(MF, I+1, Complain);
9186     Visitor(IF, IsSystem);
9187   }
9188 }
9189 
9190 void ASTReader::visitTopLevelModuleMaps(
9191     serialization::ModuleFile &MF,
9192     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9193   unsigned NumInputs = MF.InputFilesLoaded.size();
9194   for (unsigned I = 0; I < NumInputs; ++I) {
9195     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9196     if (IFI.TopLevelModuleMap)
9197       // FIXME: This unnecessarily re-reads the InputFileInfo.
9198       if (auto *FE = getInputFile(MF, I + 1).getFile())
9199         Visitor(FE);
9200   }
9201 }
9202 
9203 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9204   // If we know the owning module, use it.
9205   if (Module *M = D->getImportedOwningModule())
9206     return M->getFullModuleName();
9207 
9208   // Otherwise, use the name of the top-level module the decl is within.
9209   if (ModuleFile *M = getOwningModuleFile(D))
9210     return M->ModuleName;
9211 
9212   // Not from a module.
9213   return {};
9214 }
9215 
9216 void ASTReader::finishPendingActions() {
9217   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9218          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9219          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9220          !PendingUpdateRecords.empty()) {
9221     // If any identifiers with corresponding top-level declarations have
9222     // been loaded, load those declarations now.
9223     using TopLevelDeclsMap =
9224         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9225     TopLevelDeclsMap TopLevelDecls;
9226 
9227     while (!PendingIdentifierInfos.empty()) {
9228       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9229       SmallVector<uint32_t, 4> DeclIDs =
9230           std::move(PendingIdentifierInfos.back().second);
9231       PendingIdentifierInfos.pop_back();
9232 
9233       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9234     }
9235 
9236     // Load each function type that we deferred loading because it was a
9237     // deduced type that might refer to a local type declared within itself.
9238     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9239       auto *FD = PendingFunctionTypes[I].first;
9240       FD->setType(GetType(PendingFunctionTypes[I].second));
9241 
9242       // If we gave a function a deduced return type, remember that we need to
9243       // propagate that along the redeclaration chain.
9244       auto *DT = FD->getReturnType()->getContainedDeducedType();
9245       if (DT && DT->isDeduced())
9246         PendingDeducedTypeUpdates.insert(
9247             {FD->getCanonicalDecl(), FD->getReturnType()});
9248     }
9249     PendingFunctionTypes.clear();
9250 
9251     // For each decl chain that we wanted to complete while deserializing, mark
9252     // it as "still needs to be completed".
9253     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9254       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9255     }
9256     PendingIncompleteDeclChains.clear();
9257 
9258     // Load pending declaration chains.
9259     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9260       loadPendingDeclChain(PendingDeclChains[I].first,
9261                            PendingDeclChains[I].second);
9262     PendingDeclChains.clear();
9263 
9264     // Make the most recent of the top-level declarations visible.
9265     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9266            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9267       IdentifierInfo *II = TLD->first;
9268       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9269         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9270       }
9271     }
9272 
9273     // Load any pending macro definitions.
9274     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9275       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9276       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9277       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9278       // Initialize the macro history from chained-PCHs ahead of module imports.
9279       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9280            ++IDIdx) {
9281         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9282         if (!Info.M->isModule())
9283           resolvePendingMacro(II, Info);
9284       }
9285       // Handle module imports.
9286       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9287            ++IDIdx) {
9288         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9289         if (Info.M->isModule())
9290           resolvePendingMacro(II, Info);
9291       }
9292     }
9293     PendingMacroIDs.clear();
9294 
9295     // Wire up the DeclContexts for Decls that we delayed setting until
9296     // recursive loading is completed.
9297     while (!PendingDeclContextInfos.empty()) {
9298       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9299       PendingDeclContextInfos.pop_front();
9300       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9301       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9302       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9303     }
9304 
9305     // Perform any pending declaration updates.
9306     while (!PendingUpdateRecords.empty()) {
9307       auto Update = PendingUpdateRecords.pop_back_val();
9308       ReadingKindTracker ReadingKind(Read_Decl, *this);
9309       loadDeclUpdateRecords(Update);
9310     }
9311   }
9312 
9313   // At this point, all update records for loaded decls are in place, so any
9314   // fake class definitions should have become real.
9315   assert(PendingFakeDefinitionData.empty() &&
9316          "faked up a class definition but never saw the real one");
9317 
9318   // If we deserialized any C++ or Objective-C class definitions, any
9319   // Objective-C protocol definitions, or any redeclarable templates, make sure
9320   // that all redeclarations point to the definitions. Note that this can only
9321   // happen now, after the redeclaration chains have been fully wired.
9322   for (Decl *D : PendingDefinitions) {
9323     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9324       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9325         // Make sure that the TagType points at the definition.
9326         const_cast<TagType*>(TagT)->decl = TD;
9327       }
9328 
9329       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9330         for (auto *R = getMostRecentExistingDecl(RD); R;
9331              R = R->getPreviousDecl()) {
9332           assert((R == D) ==
9333                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9334                  "declaration thinks it's the definition but it isn't");
9335           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9336         }
9337       }
9338 
9339       continue;
9340     }
9341 
9342     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9343       // Make sure that the ObjCInterfaceType points at the definition.
9344       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9345         ->Decl = ID;
9346 
9347       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9348         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9349 
9350       continue;
9351     }
9352 
9353     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9354       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9355         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9356 
9357       continue;
9358     }
9359 
9360     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9361     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9362       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9363   }
9364   PendingDefinitions.clear();
9365 
9366   // Load the bodies of any functions or methods we've encountered. We do
9367   // this now (delayed) so that we can be sure that the declaration chains
9368   // have been fully wired up (hasBody relies on this).
9369   // FIXME: We shouldn't require complete redeclaration chains here.
9370   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9371                                PBEnd = PendingBodies.end();
9372        PB != PBEnd; ++PB) {
9373     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9374       // For a function defined inline within a class template, force the
9375       // canonical definition to be the one inside the canonical definition of
9376       // the template. This ensures that we instantiate from a correct view
9377       // of the template.
9378       //
9379       // Sadly we can't do this more generally: we can't be sure that all
9380       // copies of an arbitrary class definition will have the same members
9381       // defined (eg, some member functions may not be instantiated, and some
9382       // special members may or may not have been implicitly defined).
9383       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9384         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9385           continue;
9386 
9387       // FIXME: Check for =delete/=default?
9388       // FIXME: Complain about ODR violations here?
9389       const FunctionDecl *Defn = nullptr;
9390       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9391         FD->setLazyBody(PB->second);
9392       } else {
9393         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9394         mergeDefinitionVisibility(NonConstDefn, FD);
9395 
9396         if (!FD->isLateTemplateParsed() &&
9397             !NonConstDefn->isLateTemplateParsed() &&
9398             FD->getODRHash() != NonConstDefn->getODRHash()) {
9399           if (!isa<CXXMethodDecl>(FD)) {
9400             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9401           } else if (FD->getLexicalParent()->isFileContext() &&
9402                      NonConstDefn->getLexicalParent()->isFileContext()) {
9403             // Only diagnose out-of-line method definitions.  If they are
9404             // in class definitions, then an error will be generated when
9405             // processing the class bodies.
9406             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9407           }
9408         }
9409       }
9410       continue;
9411     }
9412 
9413     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9414     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9415       MD->setLazyBody(PB->second);
9416   }
9417   PendingBodies.clear();
9418 
9419   // Do some cleanup.
9420   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9421     getContext().deduplicateMergedDefinitonsFor(ND);
9422   PendingMergedDefinitionsToDeduplicate.clear();
9423 }
9424 
9425 void ASTReader::diagnoseOdrViolations() {
9426   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9427       PendingFunctionOdrMergeFailures.empty() &&
9428       PendingEnumOdrMergeFailures.empty())
9429     return;
9430 
9431   // Trigger the import of the full definition of each class that had any
9432   // odr-merging problems, so we can produce better diagnostics for them.
9433   // These updates may in turn find and diagnose some ODR failures, so take
9434   // ownership of the set first.
9435   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9436   PendingOdrMergeFailures.clear();
9437   for (auto &Merge : OdrMergeFailures) {
9438     Merge.first->buildLookup();
9439     Merge.first->decls_begin();
9440     Merge.first->bases_begin();
9441     Merge.first->vbases_begin();
9442     for (auto &RecordPair : Merge.second) {
9443       auto *RD = RecordPair.first;
9444       RD->decls_begin();
9445       RD->bases_begin();
9446       RD->vbases_begin();
9447     }
9448   }
9449 
9450   // Trigger the import of functions.
9451   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9452   PendingFunctionOdrMergeFailures.clear();
9453   for (auto &Merge : FunctionOdrMergeFailures) {
9454     Merge.first->buildLookup();
9455     Merge.first->decls_begin();
9456     Merge.first->getBody();
9457     for (auto &FD : Merge.second) {
9458       FD->buildLookup();
9459       FD->decls_begin();
9460       FD->getBody();
9461     }
9462   }
9463 
9464   // Trigger the import of enums.
9465   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9466   PendingEnumOdrMergeFailures.clear();
9467   for (auto &Merge : EnumOdrMergeFailures) {
9468     Merge.first->decls_begin();
9469     for (auto &Enum : Merge.second) {
9470       Enum->decls_begin();
9471     }
9472   }
9473 
9474   // For each declaration from a merged context, check that the canonical
9475   // definition of that context also contains a declaration of the same
9476   // entity.
9477   //
9478   // Caution: this loop does things that might invalidate iterators into
9479   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9480   while (!PendingOdrMergeChecks.empty()) {
9481     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9482 
9483     // FIXME: Skip over implicit declarations for now. This matters for things
9484     // like implicitly-declared special member functions. This isn't entirely
9485     // correct; we can end up with multiple unmerged declarations of the same
9486     // implicit entity.
9487     if (D->isImplicit())
9488       continue;
9489 
9490     DeclContext *CanonDef = D->getDeclContext();
9491 
9492     bool Found = false;
9493     const Decl *DCanon = D->getCanonicalDecl();
9494 
9495     for (auto RI : D->redecls()) {
9496       if (RI->getLexicalDeclContext() == CanonDef) {
9497         Found = true;
9498         break;
9499       }
9500     }
9501     if (Found)
9502       continue;
9503 
9504     // Quick check failed, time to do the slow thing. Note, we can't just
9505     // look up the name of D in CanonDef here, because the member that is
9506     // in CanonDef might not be found by name lookup (it might have been
9507     // replaced by a more recent declaration in the lookup table), and we
9508     // can't necessarily find it in the redeclaration chain because it might
9509     // be merely mergeable, not redeclarable.
9510     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9511     for (auto *CanonMember : CanonDef->decls()) {
9512       if (CanonMember->getCanonicalDecl() == DCanon) {
9513         // This can happen if the declaration is merely mergeable and not
9514         // actually redeclarable (we looked for redeclarations earlier).
9515         //
9516         // FIXME: We should be able to detect this more efficiently, without
9517         // pulling in all of the members of CanonDef.
9518         Found = true;
9519         break;
9520       }
9521       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9522         if (ND->getDeclName() == D->getDeclName())
9523           Candidates.push_back(ND);
9524     }
9525 
9526     if (!Found) {
9527       // The AST doesn't like TagDecls becoming invalid after they've been
9528       // completed. We only really need to mark FieldDecls as invalid here.
9529       if (!isa<TagDecl>(D))
9530         D->setInvalidDecl();
9531 
9532       // Ensure we don't accidentally recursively enter deserialization while
9533       // we're producing our diagnostic.
9534       Deserializing RecursionGuard(this);
9535 
9536       std::string CanonDefModule =
9537           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9538       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9539         << D << getOwningModuleNameForDiagnostic(D)
9540         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9541 
9542       if (Candidates.empty())
9543         Diag(cast<Decl>(CanonDef)->getLocation(),
9544              diag::note_module_odr_violation_no_possible_decls) << D;
9545       else {
9546         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9547           Diag(Candidates[I]->getLocation(),
9548                diag::note_module_odr_violation_possible_decl)
9549             << Candidates[I];
9550       }
9551 
9552       DiagnosedOdrMergeFailures.insert(CanonDef);
9553     }
9554   }
9555 
9556   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9557       EnumOdrMergeFailures.empty())
9558     return;
9559 
9560   // Ensure we don't accidentally recursively enter deserialization while
9561   // we're producing our diagnostics.
9562   Deserializing RecursionGuard(this);
9563 
9564   // Common code for hashing helpers.
9565   ODRHash Hash;
9566   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9567     Hash.clear();
9568     Hash.AddQualType(Ty);
9569     return Hash.CalculateHash();
9570   };
9571 
9572   auto ComputeODRHash = [&Hash](const Stmt *S) {
9573     assert(S);
9574     Hash.clear();
9575     Hash.AddStmt(S);
9576     return Hash.CalculateHash();
9577   };
9578 
9579   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9580     assert(D);
9581     Hash.clear();
9582     Hash.AddSubDecl(D);
9583     return Hash.CalculateHash();
9584   };
9585 
9586   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9587     Hash.clear();
9588     Hash.AddTemplateArgument(TA);
9589     return Hash.CalculateHash();
9590   };
9591 
9592   auto ComputeTemplateParameterListODRHash =
9593       [&Hash](const TemplateParameterList *TPL) {
9594         assert(TPL);
9595         Hash.clear();
9596         Hash.AddTemplateParameterList(TPL);
9597         return Hash.CalculateHash();
9598       };
9599 
9600   // Used with err_module_odr_violation_mismatch_decl and
9601   // note_module_odr_violation_mismatch_decl
9602   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9603   enum ODRMismatchDecl {
9604     EndOfClass,
9605     PublicSpecifer,
9606     PrivateSpecifer,
9607     ProtectedSpecifer,
9608     StaticAssert,
9609     Field,
9610     CXXMethod,
9611     TypeAlias,
9612     TypeDef,
9613     Var,
9614     Friend,
9615     FunctionTemplate,
9616     Other
9617   };
9618 
9619   // Used with err_module_odr_violation_mismatch_decl_diff and
9620   // note_module_odr_violation_mismatch_decl_diff
9621   enum ODRMismatchDeclDifference {
9622     StaticAssertCondition,
9623     StaticAssertMessage,
9624     StaticAssertOnlyMessage,
9625     FieldName,
9626     FieldTypeName,
9627     FieldSingleBitField,
9628     FieldDifferentWidthBitField,
9629     FieldSingleMutable,
9630     FieldSingleInitializer,
9631     FieldDifferentInitializers,
9632     MethodName,
9633     MethodDeleted,
9634     MethodDefaulted,
9635     MethodVirtual,
9636     MethodStatic,
9637     MethodVolatile,
9638     MethodConst,
9639     MethodInline,
9640     MethodNumberParameters,
9641     MethodParameterType,
9642     MethodParameterName,
9643     MethodParameterSingleDefaultArgument,
9644     MethodParameterDifferentDefaultArgument,
9645     MethodNoTemplateArguments,
9646     MethodDifferentNumberTemplateArguments,
9647     MethodDifferentTemplateArgument,
9648     MethodSingleBody,
9649     MethodDifferentBody,
9650     TypedefName,
9651     TypedefType,
9652     VarName,
9653     VarType,
9654     VarSingleInitializer,
9655     VarDifferentInitializer,
9656     VarConstexpr,
9657     FriendTypeFunction,
9658     FriendType,
9659     FriendFunction,
9660     FunctionTemplateDifferentNumberParameters,
9661     FunctionTemplateParameterDifferentKind,
9662     FunctionTemplateParameterName,
9663     FunctionTemplateParameterSingleDefaultArgument,
9664     FunctionTemplateParameterDifferentDefaultArgument,
9665     FunctionTemplateParameterDifferentType,
9666     FunctionTemplatePackParameter,
9667   };
9668 
9669   // These lambdas have the common portions of the ODR diagnostics.  This
9670   // has the same return as Diag(), so addition parameters can be passed
9671   // in with operator<<
9672   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9673                                  SourceLocation Loc, SourceRange Range,
9674                                  ODRMismatchDeclDifference DiffType) {
9675     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9676            << FirstRecord << FirstModule.empty() << FirstModule << Range
9677            << DiffType;
9678   };
9679   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9680                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9681     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9682            << SecondModule << Range << DiffType;
9683   };
9684 
9685   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9686                        &ComputeQualTypeODRHash, &ComputeODRHash](
9687                           NamedDecl *FirstRecord, StringRef FirstModule,
9688                           StringRef SecondModule, FieldDecl *FirstField,
9689                           FieldDecl *SecondField) {
9690     IdentifierInfo *FirstII = FirstField->getIdentifier();
9691     IdentifierInfo *SecondII = SecondField->getIdentifier();
9692     if (FirstII->getName() != SecondII->getName()) {
9693       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9694                        FirstField->getSourceRange(), FieldName)
9695           << FirstII;
9696       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9697                       SecondField->getSourceRange(), FieldName)
9698           << SecondII;
9699 
9700       return true;
9701     }
9702 
9703     assert(getContext().hasSameType(FirstField->getType(),
9704                                     SecondField->getType()));
9705 
9706     QualType FirstType = FirstField->getType();
9707     QualType SecondType = SecondField->getType();
9708     if (ComputeQualTypeODRHash(FirstType) !=
9709         ComputeQualTypeODRHash(SecondType)) {
9710       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9711                        FirstField->getSourceRange(), FieldTypeName)
9712           << FirstII << FirstType;
9713       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9714                       SecondField->getSourceRange(), FieldTypeName)
9715           << SecondII << SecondType;
9716 
9717       return true;
9718     }
9719 
9720     const bool IsFirstBitField = FirstField->isBitField();
9721     const bool IsSecondBitField = SecondField->isBitField();
9722     if (IsFirstBitField != IsSecondBitField) {
9723       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9724                        FirstField->getSourceRange(), FieldSingleBitField)
9725           << FirstII << IsFirstBitField;
9726       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9727                       SecondField->getSourceRange(), FieldSingleBitField)
9728           << SecondII << IsSecondBitField;
9729       return true;
9730     }
9731 
9732     if (IsFirstBitField && IsSecondBitField) {
9733       unsigned FirstBitWidthHash =
9734           ComputeODRHash(FirstField->getBitWidth());
9735       unsigned SecondBitWidthHash =
9736           ComputeODRHash(SecondField->getBitWidth());
9737       if (FirstBitWidthHash != SecondBitWidthHash) {
9738         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9739                          FirstField->getSourceRange(),
9740                          FieldDifferentWidthBitField)
9741             << FirstII << FirstField->getBitWidth()->getSourceRange();
9742         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9743                         SecondField->getSourceRange(),
9744                         FieldDifferentWidthBitField)
9745             << SecondII << SecondField->getBitWidth()->getSourceRange();
9746         return true;
9747       }
9748     }
9749 
9750     if (!PP.getLangOpts().CPlusPlus)
9751       return false;
9752 
9753     const bool IsFirstMutable = FirstField->isMutable();
9754     const bool IsSecondMutable = SecondField->isMutable();
9755     if (IsFirstMutable != IsSecondMutable) {
9756       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9757                        FirstField->getSourceRange(), FieldSingleMutable)
9758           << FirstII << IsFirstMutable;
9759       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9760                       SecondField->getSourceRange(), FieldSingleMutable)
9761           << SecondII << IsSecondMutable;
9762       return true;
9763     }
9764 
9765     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9766     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9767     if ((!FirstInitializer && SecondInitializer) ||
9768         (FirstInitializer && !SecondInitializer)) {
9769       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9770                        FirstField->getSourceRange(), FieldSingleInitializer)
9771           << FirstII << (FirstInitializer != nullptr);
9772       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9773                       SecondField->getSourceRange(), FieldSingleInitializer)
9774           << SecondII << (SecondInitializer != nullptr);
9775       return true;
9776     }
9777 
9778     if (FirstInitializer && SecondInitializer) {
9779       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9780       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9781       if (FirstInitHash != SecondInitHash) {
9782         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9783                          FirstField->getSourceRange(),
9784                          FieldDifferentInitializers)
9785             << FirstII << FirstInitializer->getSourceRange();
9786         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9787                         SecondField->getSourceRange(),
9788                         FieldDifferentInitializers)
9789             << SecondII << SecondInitializer->getSourceRange();
9790         return true;
9791       }
9792     }
9793 
9794     return false;
9795   };
9796 
9797   auto ODRDiagTypeDefOrAlias =
9798       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9799           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9800           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9801           bool IsTypeAlias) {
9802         auto FirstName = FirstTD->getDeclName();
9803         auto SecondName = SecondTD->getDeclName();
9804         if (FirstName != SecondName) {
9805           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9806                            FirstTD->getSourceRange(), TypedefName)
9807               << IsTypeAlias << FirstName;
9808           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9809                           SecondTD->getSourceRange(), TypedefName)
9810               << IsTypeAlias << SecondName;
9811           return true;
9812         }
9813 
9814         QualType FirstType = FirstTD->getUnderlyingType();
9815         QualType SecondType = SecondTD->getUnderlyingType();
9816         if (ComputeQualTypeODRHash(FirstType) !=
9817             ComputeQualTypeODRHash(SecondType)) {
9818           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9819                            FirstTD->getSourceRange(), TypedefType)
9820               << IsTypeAlias << FirstName << FirstType;
9821           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9822                           SecondTD->getSourceRange(), TypedefType)
9823               << IsTypeAlias << SecondName << SecondType;
9824           return true;
9825         }
9826 
9827         return false;
9828   };
9829 
9830   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9831                      &ComputeQualTypeODRHash, &ComputeODRHash,
9832                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9833                            StringRef SecondModule, VarDecl *FirstVD,
9834                            VarDecl *SecondVD) {
9835     auto FirstName = FirstVD->getDeclName();
9836     auto SecondName = SecondVD->getDeclName();
9837     if (FirstName != SecondName) {
9838       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9839                        FirstVD->getSourceRange(), VarName)
9840           << FirstName;
9841       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9842                       SecondVD->getSourceRange(), VarName)
9843           << SecondName;
9844       return true;
9845     }
9846 
9847     QualType FirstType = FirstVD->getType();
9848     QualType SecondType = SecondVD->getType();
9849     if (ComputeQualTypeODRHash(FirstType) !=
9850         ComputeQualTypeODRHash(SecondType)) {
9851       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9852                        FirstVD->getSourceRange(), VarType)
9853           << FirstName << FirstType;
9854       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9855                       SecondVD->getSourceRange(), VarType)
9856           << SecondName << SecondType;
9857       return true;
9858     }
9859 
9860     if (!PP.getLangOpts().CPlusPlus)
9861       return false;
9862 
9863     const Expr *FirstInit = FirstVD->getInit();
9864     const Expr *SecondInit = SecondVD->getInit();
9865     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9866       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9867                        FirstVD->getSourceRange(), VarSingleInitializer)
9868           << FirstName << (FirstInit == nullptr)
9869           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9870       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9871                       SecondVD->getSourceRange(), VarSingleInitializer)
9872           << SecondName << (SecondInit == nullptr)
9873           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9874       return true;
9875     }
9876 
9877     if (FirstInit && SecondInit &&
9878         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9879       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9880                        FirstVD->getSourceRange(), VarDifferentInitializer)
9881           << FirstName << FirstInit->getSourceRange();
9882       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9883                       SecondVD->getSourceRange(), VarDifferentInitializer)
9884           << SecondName << SecondInit->getSourceRange();
9885       return true;
9886     }
9887 
9888     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9889     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9890     if (FirstIsConstexpr != SecondIsConstexpr) {
9891       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9892                        FirstVD->getSourceRange(), VarConstexpr)
9893           << FirstName << FirstIsConstexpr;
9894       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9895                       SecondVD->getSourceRange(), VarConstexpr)
9896           << SecondName << SecondIsConstexpr;
9897       return true;
9898     }
9899     return false;
9900   };
9901 
9902   auto DifferenceSelector = [](Decl *D) {
9903     assert(D && "valid Decl required");
9904     switch (D->getKind()) {
9905     default:
9906       return Other;
9907     case Decl::AccessSpec:
9908       switch (D->getAccess()) {
9909       case AS_public:
9910         return PublicSpecifer;
9911       case AS_private:
9912         return PrivateSpecifer;
9913       case AS_protected:
9914         return ProtectedSpecifer;
9915       case AS_none:
9916         break;
9917       }
9918       llvm_unreachable("Invalid access specifier");
9919     case Decl::StaticAssert:
9920       return StaticAssert;
9921     case Decl::Field:
9922       return Field;
9923     case Decl::CXXMethod:
9924     case Decl::CXXConstructor:
9925     case Decl::CXXDestructor:
9926       return CXXMethod;
9927     case Decl::TypeAlias:
9928       return TypeAlias;
9929     case Decl::Typedef:
9930       return TypeDef;
9931     case Decl::Var:
9932       return Var;
9933     case Decl::Friend:
9934       return Friend;
9935     case Decl::FunctionTemplate:
9936       return FunctionTemplate;
9937     }
9938   };
9939 
9940   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9941   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9942                                                  RecordDecl *Record,
9943                                                  const DeclContext *DC) {
9944     for (auto *D : Record->decls()) {
9945       if (!ODRHash::isDeclToBeProcessed(D, DC))
9946         continue;
9947       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9948     }
9949   };
9950 
9951   struct DiffResult {
9952     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9953     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9954   };
9955 
9956   // If there is a diagnoseable difference, FirstDiffType and
9957   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9958   // filled in if not EndOfClass.
9959   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9960                                              DeclHashes &SecondHashes) {
9961     DiffResult DR;
9962     auto FirstIt = FirstHashes.begin();
9963     auto SecondIt = SecondHashes.begin();
9964     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9965       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9966           FirstIt->second == SecondIt->second) {
9967         ++FirstIt;
9968         ++SecondIt;
9969         continue;
9970       }
9971 
9972       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9973       DR.SecondDecl =
9974           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9975 
9976       DR.FirstDiffType =
9977           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9978       DR.SecondDiffType =
9979           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9980       return DR;
9981     }
9982     return DR;
9983   };
9984 
9985   // Use this to diagnose that an unexpected Decl was encountered
9986   // or no difference was detected. This causes a generic error
9987   // message to be emitted.
9988   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9989                                       StringRef FirstModule,
9990                                       NamedDecl *SecondRecord,
9991                                       StringRef SecondModule) {
9992     Diag(FirstRecord->getLocation(),
9993          diag::err_module_odr_violation_different_definitions)
9994         << FirstRecord << FirstModule.empty() << FirstModule;
9995 
9996     if (DR.FirstDecl) {
9997       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9998           << FirstRecord << DR.FirstDecl->getSourceRange();
9999     }
10000 
10001     Diag(SecondRecord->getLocation(),
10002          diag::note_module_odr_violation_different_definitions)
10003         << SecondModule;
10004 
10005     if (DR.SecondDecl) {
10006       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
10007           << DR.SecondDecl->getSourceRange();
10008     }
10009   };
10010 
10011   auto DiagnoseODRMismatch =
10012       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10013              NamedDecl *SecondRecord, StringRef SecondModule) {
10014         SourceLocation FirstLoc;
10015         SourceRange FirstRange;
10016         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10017         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10018           FirstLoc = FirstTag->getBraceRange().getEnd();
10019         } else {
10020           FirstLoc = DR.FirstDecl->getLocation();
10021           FirstRange = DR.FirstDecl->getSourceRange();
10022         }
10023         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10024             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10025             << DR.FirstDiffType;
10026 
10027         SourceLocation SecondLoc;
10028         SourceRange SecondRange;
10029         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10030         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10031           SecondLoc = SecondTag->getBraceRange().getEnd();
10032         } else {
10033           SecondLoc = DR.SecondDecl->getLocation();
10034           SecondRange = DR.SecondDecl->getSourceRange();
10035         }
10036         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10037             << SecondModule << SecondRange << DR.SecondDiffType;
10038       };
10039 
10040   // Issue any pending ODR-failure diagnostics.
10041   for (auto &Merge : OdrMergeFailures) {
10042     // If we've already pointed out a specific problem with this class, don't
10043     // bother issuing a general "something's different" diagnostic.
10044     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10045       continue;
10046 
10047     bool Diagnosed = false;
10048     CXXRecordDecl *FirstRecord = Merge.first;
10049     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10050     for (auto &RecordPair : Merge.second) {
10051       CXXRecordDecl *SecondRecord = RecordPair.first;
10052       // Multiple different declarations got merged together; tell the user
10053       // where they came from.
10054       if (FirstRecord == SecondRecord)
10055         continue;
10056 
10057       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10058 
10059       auto *FirstDD = FirstRecord->DefinitionData;
10060       auto *SecondDD = RecordPair.second;
10061 
10062       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10063 
10064       // Diagnostics from DefinitionData are emitted here.
10065       if (FirstDD != SecondDD) {
10066         enum ODRDefinitionDataDifference {
10067           NumBases,
10068           NumVBases,
10069           BaseType,
10070           BaseVirtual,
10071           BaseAccess,
10072         };
10073         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10074                                  this](SourceLocation Loc, SourceRange Range,
10075                                        ODRDefinitionDataDifference DiffType) {
10076           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10077                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10078                  << DiffType;
10079         };
10080         auto ODRDiagBaseNote = [&SecondModule,
10081                                 this](SourceLocation Loc, SourceRange Range,
10082                                       ODRDefinitionDataDifference DiffType) {
10083           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10084                  << SecondModule << Range << DiffType;
10085         };
10086 
10087         unsigned FirstNumBases = FirstDD->NumBases;
10088         unsigned FirstNumVBases = FirstDD->NumVBases;
10089         unsigned SecondNumBases = SecondDD->NumBases;
10090         unsigned SecondNumVBases = SecondDD->NumVBases;
10091 
10092         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10093           unsigned NumBases = DD->NumBases;
10094           if (NumBases == 0) return SourceRange();
10095           auto bases = DD->bases();
10096           return SourceRange(bases[0].getBeginLoc(),
10097                              bases[NumBases - 1].getEndLoc());
10098         };
10099 
10100         if (FirstNumBases != SecondNumBases) {
10101           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10102                            NumBases)
10103               << FirstNumBases;
10104           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10105                           NumBases)
10106               << SecondNumBases;
10107           Diagnosed = true;
10108           break;
10109         }
10110 
10111         if (FirstNumVBases != SecondNumVBases) {
10112           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10113                            NumVBases)
10114               << FirstNumVBases;
10115           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10116                           NumVBases)
10117               << SecondNumVBases;
10118           Diagnosed = true;
10119           break;
10120         }
10121 
10122         auto FirstBases = FirstDD->bases();
10123         auto SecondBases = SecondDD->bases();
10124         unsigned i = 0;
10125         for (i = 0; i < FirstNumBases; ++i) {
10126           auto FirstBase = FirstBases[i];
10127           auto SecondBase = SecondBases[i];
10128           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10129               ComputeQualTypeODRHash(SecondBase.getType())) {
10130             ODRDiagBaseError(FirstRecord->getLocation(),
10131                              FirstBase.getSourceRange(), BaseType)
10132                 << (i + 1) << FirstBase.getType();
10133             ODRDiagBaseNote(SecondRecord->getLocation(),
10134                             SecondBase.getSourceRange(), BaseType)
10135                 << (i + 1) << SecondBase.getType();
10136             break;
10137           }
10138 
10139           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10140             ODRDiagBaseError(FirstRecord->getLocation(),
10141                              FirstBase.getSourceRange(), BaseVirtual)
10142                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10143             ODRDiagBaseNote(SecondRecord->getLocation(),
10144                             SecondBase.getSourceRange(), BaseVirtual)
10145                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10146             break;
10147           }
10148 
10149           if (FirstBase.getAccessSpecifierAsWritten() !=
10150               SecondBase.getAccessSpecifierAsWritten()) {
10151             ODRDiagBaseError(FirstRecord->getLocation(),
10152                              FirstBase.getSourceRange(), BaseAccess)
10153                 << (i + 1) << FirstBase.getType()
10154                 << (int)FirstBase.getAccessSpecifierAsWritten();
10155             ODRDiagBaseNote(SecondRecord->getLocation(),
10156                             SecondBase.getSourceRange(), BaseAccess)
10157                 << (i + 1) << SecondBase.getType()
10158                 << (int)SecondBase.getAccessSpecifierAsWritten();
10159             break;
10160           }
10161         }
10162 
10163         if (i != FirstNumBases) {
10164           Diagnosed = true;
10165           break;
10166         }
10167       }
10168 
10169       const ClassTemplateDecl *FirstTemplate =
10170           FirstRecord->getDescribedClassTemplate();
10171       const ClassTemplateDecl *SecondTemplate =
10172           SecondRecord->getDescribedClassTemplate();
10173 
10174       assert(!FirstTemplate == !SecondTemplate &&
10175              "Both pointers should be null or non-null");
10176 
10177       enum ODRTemplateDifference {
10178         ParamEmptyName,
10179         ParamName,
10180         ParamSingleDefaultArgument,
10181         ParamDifferentDefaultArgument,
10182       };
10183 
10184       if (FirstTemplate && SecondTemplate) {
10185         DeclHashes FirstTemplateHashes;
10186         DeclHashes SecondTemplateHashes;
10187 
10188         auto PopulateTemplateParameterHashs =
10189             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10190                                      const ClassTemplateDecl *TD) {
10191               for (auto *D : TD->getTemplateParameters()->asArray()) {
10192                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10193               }
10194             };
10195 
10196         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10197         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10198 
10199         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10200                "Number of template parameters should be equal.");
10201 
10202         auto FirstIt = FirstTemplateHashes.begin();
10203         auto FirstEnd = FirstTemplateHashes.end();
10204         auto SecondIt = SecondTemplateHashes.begin();
10205         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10206           if (FirstIt->second == SecondIt->second)
10207             continue;
10208 
10209           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10210                                           SourceLocation Loc, SourceRange Range,
10211                                           ODRTemplateDifference DiffType) {
10212             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10213                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10214                    << DiffType;
10215           };
10216           auto ODRDiagTemplateNote = [&SecondModule, this](
10217                                          SourceLocation Loc, SourceRange Range,
10218                                          ODRTemplateDifference DiffType) {
10219             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10220                    << SecondModule << Range << DiffType;
10221           };
10222 
10223           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10224           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10225 
10226           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10227                  "Parameter Decl's should be the same kind.");
10228 
10229           DeclarationName FirstName = FirstDecl->getDeclName();
10230           DeclarationName SecondName = SecondDecl->getDeclName();
10231 
10232           if (FirstName != SecondName) {
10233             const bool FirstNameEmpty =
10234                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10235             const bool SecondNameEmpty =
10236                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10237             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10238                    "Both template parameters cannot be unnamed.");
10239             ODRDiagTemplateError(FirstDecl->getLocation(),
10240                                  FirstDecl->getSourceRange(),
10241                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10242                 << FirstName;
10243             ODRDiagTemplateNote(SecondDecl->getLocation(),
10244                                 SecondDecl->getSourceRange(),
10245                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10246                 << SecondName;
10247             break;
10248           }
10249 
10250           switch (FirstDecl->getKind()) {
10251           default:
10252             llvm_unreachable("Invalid template parameter type.");
10253           case Decl::TemplateTypeParm: {
10254             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10255             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10256             const bool HasFirstDefaultArgument =
10257                 FirstParam->hasDefaultArgument() &&
10258                 !FirstParam->defaultArgumentWasInherited();
10259             const bool HasSecondDefaultArgument =
10260                 SecondParam->hasDefaultArgument() &&
10261                 !SecondParam->defaultArgumentWasInherited();
10262 
10263             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10264               ODRDiagTemplateError(FirstDecl->getLocation(),
10265                                    FirstDecl->getSourceRange(),
10266                                    ParamSingleDefaultArgument)
10267                   << HasFirstDefaultArgument;
10268               ODRDiagTemplateNote(SecondDecl->getLocation(),
10269                                   SecondDecl->getSourceRange(),
10270                                   ParamSingleDefaultArgument)
10271                   << HasSecondDefaultArgument;
10272               break;
10273             }
10274 
10275             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10276                    "Expecting default arguments.");
10277 
10278             ODRDiagTemplateError(FirstDecl->getLocation(),
10279                                  FirstDecl->getSourceRange(),
10280                                  ParamDifferentDefaultArgument);
10281             ODRDiagTemplateNote(SecondDecl->getLocation(),
10282                                 SecondDecl->getSourceRange(),
10283                                 ParamDifferentDefaultArgument);
10284 
10285             break;
10286           }
10287           case Decl::NonTypeTemplateParm: {
10288             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10289             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10290             const bool HasFirstDefaultArgument =
10291                 FirstParam->hasDefaultArgument() &&
10292                 !FirstParam->defaultArgumentWasInherited();
10293             const bool HasSecondDefaultArgument =
10294                 SecondParam->hasDefaultArgument() &&
10295                 !SecondParam->defaultArgumentWasInherited();
10296 
10297             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10298               ODRDiagTemplateError(FirstDecl->getLocation(),
10299                                    FirstDecl->getSourceRange(),
10300                                    ParamSingleDefaultArgument)
10301                   << HasFirstDefaultArgument;
10302               ODRDiagTemplateNote(SecondDecl->getLocation(),
10303                                   SecondDecl->getSourceRange(),
10304                                   ParamSingleDefaultArgument)
10305                   << HasSecondDefaultArgument;
10306               break;
10307             }
10308 
10309             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10310                    "Expecting default arguments.");
10311 
10312             ODRDiagTemplateError(FirstDecl->getLocation(),
10313                                  FirstDecl->getSourceRange(),
10314                                  ParamDifferentDefaultArgument);
10315             ODRDiagTemplateNote(SecondDecl->getLocation(),
10316                                 SecondDecl->getSourceRange(),
10317                                 ParamDifferentDefaultArgument);
10318 
10319             break;
10320           }
10321           case Decl::TemplateTemplateParm: {
10322             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10323             const auto *SecondParam =
10324                 cast<TemplateTemplateParmDecl>(SecondDecl);
10325             const bool HasFirstDefaultArgument =
10326                 FirstParam->hasDefaultArgument() &&
10327                 !FirstParam->defaultArgumentWasInherited();
10328             const bool HasSecondDefaultArgument =
10329                 SecondParam->hasDefaultArgument() &&
10330                 !SecondParam->defaultArgumentWasInherited();
10331 
10332             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10333               ODRDiagTemplateError(FirstDecl->getLocation(),
10334                                    FirstDecl->getSourceRange(),
10335                                    ParamSingleDefaultArgument)
10336                   << HasFirstDefaultArgument;
10337               ODRDiagTemplateNote(SecondDecl->getLocation(),
10338                                   SecondDecl->getSourceRange(),
10339                                   ParamSingleDefaultArgument)
10340                   << HasSecondDefaultArgument;
10341               break;
10342             }
10343 
10344             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10345                    "Expecting default arguments.");
10346 
10347             ODRDiagTemplateError(FirstDecl->getLocation(),
10348                                  FirstDecl->getSourceRange(),
10349                                  ParamDifferentDefaultArgument);
10350             ODRDiagTemplateNote(SecondDecl->getLocation(),
10351                                 SecondDecl->getSourceRange(),
10352                                 ParamDifferentDefaultArgument);
10353 
10354             break;
10355           }
10356           }
10357 
10358           break;
10359         }
10360 
10361         if (FirstIt != FirstEnd) {
10362           Diagnosed = true;
10363           break;
10364         }
10365       }
10366 
10367       DeclHashes FirstHashes;
10368       DeclHashes SecondHashes;
10369       const DeclContext *DC = FirstRecord;
10370       PopulateHashes(FirstHashes, FirstRecord, DC);
10371       PopulateHashes(SecondHashes, SecondRecord, DC);
10372 
10373       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10374       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10375       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10376       Decl *FirstDecl = DR.FirstDecl;
10377       Decl *SecondDecl = DR.SecondDecl;
10378 
10379       if (FirstDiffType == Other || SecondDiffType == Other) {
10380         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10381                               SecondModule);
10382         Diagnosed = true;
10383         break;
10384       }
10385 
10386       if (FirstDiffType != SecondDiffType) {
10387         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10388                             SecondModule);
10389         Diagnosed = true;
10390         break;
10391       }
10392 
10393       assert(FirstDiffType == SecondDiffType);
10394 
10395       switch (FirstDiffType) {
10396       case Other:
10397       case EndOfClass:
10398       case PublicSpecifer:
10399       case PrivateSpecifer:
10400       case ProtectedSpecifer:
10401         llvm_unreachable("Invalid diff type");
10402 
10403       case StaticAssert: {
10404         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10405         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10406 
10407         Expr *FirstExpr = FirstSA->getAssertExpr();
10408         Expr *SecondExpr = SecondSA->getAssertExpr();
10409         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10410         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10411         if (FirstODRHash != SecondODRHash) {
10412           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10413                            FirstExpr->getSourceRange(), StaticAssertCondition);
10414           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10415                           SecondExpr->getSourceRange(), StaticAssertCondition);
10416           Diagnosed = true;
10417           break;
10418         }
10419 
10420         StringLiteral *FirstStr = FirstSA->getMessage();
10421         StringLiteral *SecondStr = SecondSA->getMessage();
10422         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10423         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10424           SourceLocation FirstLoc, SecondLoc;
10425           SourceRange FirstRange, SecondRange;
10426           if (FirstStr) {
10427             FirstLoc = FirstStr->getBeginLoc();
10428             FirstRange = FirstStr->getSourceRange();
10429           } else {
10430             FirstLoc = FirstSA->getBeginLoc();
10431             FirstRange = FirstSA->getSourceRange();
10432           }
10433           if (SecondStr) {
10434             SecondLoc = SecondStr->getBeginLoc();
10435             SecondRange = SecondStr->getSourceRange();
10436           } else {
10437             SecondLoc = SecondSA->getBeginLoc();
10438             SecondRange = SecondSA->getSourceRange();
10439           }
10440           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10441                            StaticAssertOnlyMessage)
10442               << (FirstStr == nullptr);
10443           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10444                           StaticAssertOnlyMessage)
10445               << (SecondStr == nullptr);
10446           Diagnosed = true;
10447           break;
10448         }
10449 
10450         if (FirstStr && SecondStr &&
10451             FirstStr->getString() != SecondStr->getString()) {
10452           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10453                            FirstStr->getSourceRange(), StaticAssertMessage);
10454           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10455                           SecondStr->getSourceRange(), StaticAssertMessage);
10456           Diagnosed = true;
10457           break;
10458         }
10459         break;
10460       }
10461       case Field: {
10462         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10463                                  cast<FieldDecl>(FirstDecl),
10464                                  cast<FieldDecl>(SecondDecl));
10465         break;
10466       }
10467       case CXXMethod: {
10468         enum {
10469           DiagMethod,
10470           DiagConstructor,
10471           DiagDestructor,
10472         } FirstMethodType,
10473             SecondMethodType;
10474         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10475           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10476           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10477           return DiagMethod;
10478         };
10479         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10480         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10481         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10482         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10483         auto FirstName = FirstMethod->getDeclName();
10484         auto SecondName = SecondMethod->getDeclName();
10485         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10486           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10487                            FirstMethod->getSourceRange(), MethodName)
10488               << FirstMethodType << FirstName;
10489           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10490                           SecondMethod->getSourceRange(), MethodName)
10491               << SecondMethodType << SecondName;
10492 
10493           Diagnosed = true;
10494           break;
10495         }
10496 
10497         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10498         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10499         if (FirstDeleted != SecondDeleted) {
10500           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10501                            FirstMethod->getSourceRange(), MethodDeleted)
10502               << FirstMethodType << FirstName << FirstDeleted;
10503 
10504           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10505                           SecondMethod->getSourceRange(), MethodDeleted)
10506               << SecondMethodType << SecondName << SecondDeleted;
10507           Diagnosed = true;
10508           break;
10509         }
10510 
10511         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10512         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10513         if (FirstDefaulted != SecondDefaulted) {
10514           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10515                            FirstMethod->getSourceRange(), MethodDefaulted)
10516               << FirstMethodType << FirstName << FirstDefaulted;
10517 
10518           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10519                           SecondMethod->getSourceRange(), MethodDefaulted)
10520               << SecondMethodType << SecondName << SecondDefaulted;
10521           Diagnosed = true;
10522           break;
10523         }
10524 
10525         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10526         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10527         const bool FirstPure = FirstMethod->isPure();
10528         const bool SecondPure = SecondMethod->isPure();
10529         if ((FirstVirtual || SecondVirtual) &&
10530             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10531           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10532                            FirstMethod->getSourceRange(), MethodVirtual)
10533               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10534           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10535                           SecondMethod->getSourceRange(), MethodVirtual)
10536               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10537           Diagnosed = true;
10538           break;
10539         }
10540 
10541         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10542         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10543         // class needs to be checked instead.
10544         const auto FirstStorage = FirstMethod->getStorageClass();
10545         const auto SecondStorage = SecondMethod->getStorageClass();
10546         const bool FirstStatic = FirstStorage == SC_Static;
10547         const bool SecondStatic = SecondStorage == SC_Static;
10548         if (FirstStatic != SecondStatic) {
10549           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10550                            FirstMethod->getSourceRange(), MethodStatic)
10551               << FirstMethodType << FirstName << FirstStatic;
10552           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10553                           SecondMethod->getSourceRange(), MethodStatic)
10554               << SecondMethodType << SecondName << SecondStatic;
10555           Diagnosed = true;
10556           break;
10557         }
10558 
10559         const bool FirstVolatile = FirstMethod->isVolatile();
10560         const bool SecondVolatile = SecondMethod->isVolatile();
10561         if (FirstVolatile != SecondVolatile) {
10562           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10563                            FirstMethod->getSourceRange(), MethodVolatile)
10564               << FirstMethodType << FirstName << FirstVolatile;
10565           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10566                           SecondMethod->getSourceRange(), MethodVolatile)
10567               << SecondMethodType << SecondName << SecondVolatile;
10568           Diagnosed = true;
10569           break;
10570         }
10571 
10572         const bool FirstConst = FirstMethod->isConst();
10573         const bool SecondConst = SecondMethod->isConst();
10574         if (FirstConst != SecondConst) {
10575           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10576                            FirstMethod->getSourceRange(), MethodConst)
10577               << FirstMethodType << FirstName << FirstConst;
10578           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10579                           SecondMethod->getSourceRange(), MethodConst)
10580               << SecondMethodType << SecondName << SecondConst;
10581           Diagnosed = true;
10582           break;
10583         }
10584 
10585         const bool FirstInline = FirstMethod->isInlineSpecified();
10586         const bool SecondInline = SecondMethod->isInlineSpecified();
10587         if (FirstInline != SecondInline) {
10588           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10589                            FirstMethod->getSourceRange(), MethodInline)
10590               << FirstMethodType << FirstName << FirstInline;
10591           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10592                           SecondMethod->getSourceRange(), MethodInline)
10593               << SecondMethodType << SecondName << SecondInline;
10594           Diagnosed = true;
10595           break;
10596         }
10597 
10598         const unsigned FirstNumParameters = FirstMethod->param_size();
10599         const unsigned SecondNumParameters = SecondMethod->param_size();
10600         if (FirstNumParameters != SecondNumParameters) {
10601           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10602                            FirstMethod->getSourceRange(),
10603                            MethodNumberParameters)
10604               << FirstMethodType << FirstName << FirstNumParameters;
10605           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10606                           SecondMethod->getSourceRange(),
10607                           MethodNumberParameters)
10608               << SecondMethodType << SecondName << SecondNumParameters;
10609           Diagnosed = true;
10610           break;
10611         }
10612 
10613         // Need this status boolean to know when break out of the switch.
10614         bool ParameterMismatch = false;
10615         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10616           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10617           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10618 
10619           QualType FirstParamType = FirstParam->getType();
10620           QualType SecondParamType = SecondParam->getType();
10621           if (FirstParamType != SecondParamType &&
10622               ComputeQualTypeODRHash(FirstParamType) !=
10623                   ComputeQualTypeODRHash(SecondParamType)) {
10624             if (const DecayedType *ParamDecayedType =
10625                     FirstParamType->getAs<DecayedType>()) {
10626               ODRDiagDeclError(
10627                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10628                   FirstMethod->getSourceRange(), MethodParameterType)
10629                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10630                   << true << ParamDecayedType->getOriginalType();
10631             } else {
10632               ODRDiagDeclError(
10633                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10634                   FirstMethod->getSourceRange(), MethodParameterType)
10635                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10636                   << false;
10637             }
10638 
10639             if (const DecayedType *ParamDecayedType =
10640                     SecondParamType->getAs<DecayedType>()) {
10641               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10642                               SecondMethod->getSourceRange(),
10643                               MethodParameterType)
10644                   << SecondMethodType << SecondName << (I + 1)
10645                   << SecondParamType << true
10646                   << ParamDecayedType->getOriginalType();
10647             } else {
10648               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10649                               SecondMethod->getSourceRange(),
10650                               MethodParameterType)
10651                   << SecondMethodType << SecondName << (I + 1)
10652                   << SecondParamType << false;
10653             }
10654             ParameterMismatch = true;
10655             break;
10656           }
10657 
10658           DeclarationName FirstParamName = FirstParam->getDeclName();
10659           DeclarationName SecondParamName = SecondParam->getDeclName();
10660           if (FirstParamName != SecondParamName) {
10661             ODRDiagDeclError(FirstRecord, FirstModule,
10662                              FirstMethod->getLocation(),
10663                              FirstMethod->getSourceRange(), MethodParameterName)
10664                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10665             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10666                             SecondMethod->getSourceRange(), MethodParameterName)
10667                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10668             ParameterMismatch = true;
10669             break;
10670           }
10671 
10672           const Expr *FirstInit = FirstParam->getInit();
10673           const Expr *SecondInit = SecondParam->getInit();
10674           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10675             ODRDiagDeclError(FirstRecord, FirstModule,
10676                              FirstMethod->getLocation(),
10677                              FirstMethod->getSourceRange(),
10678                              MethodParameterSingleDefaultArgument)
10679                 << FirstMethodType << FirstName << (I + 1)
10680                 << (FirstInit == nullptr)
10681                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10682             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10683                             SecondMethod->getSourceRange(),
10684                             MethodParameterSingleDefaultArgument)
10685                 << SecondMethodType << SecondName << (I + 1)
10686                 << (SecondInit == nullptr)
10687                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10688             ParameterMismatch = true;
10689             break;
10690           }
10691 
10692           if (FirstInit && SecondInit &&
10693               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10694             ODRDiagDeclError(FirstRecord, FirstModule,
10695                              FirstMethod->getLocation(),
10696                              FirstMethod->getSourceRange(),
10697                              MethodParameterDifferentDefaultArgument)
10698                 << FirstMethodType << FirstName << (I + 1)
10699                 << FirstInit->getSourceRange();
10700             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10701                             SecondMethod->getSourceRange(),
10702                             MethodParameterDifferentDefaultArgument)
10703                 << SecondMethodType << SecondName << (I + 1)
10704                 << SecondInit->getSourceRange();
10705             ParameterMismatch = true;
10706             break;
10707 
10708           }
10709         }
10710 
10711         if (ParameterMismatch) {
10712           Diagnosed = true;
10713           break;
10714         }
10715 
10716         const auto *FirstTemplateArgs =
10717             FirstMethod->getTemplateSpecializationArgs();
10718         const auto *SecondTemplateArgs =
10719             SecondMethod->getTemplateSpecializationArgs();
10720 
10721         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10722             (!FirstTemplateArgs && SecondTemplateArgs)) {
10723           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10724                            FirstMethod->getSourceRange(),
10725                            MethodNoTemplateArguments)
10726               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10727           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10728                           SecondMethod->getSourceRange(),
10729                           MethodNoTemplateArguments)
10730               << SecondMethodType << SecondName
10731               << (SecondTemplateArgs != nullptr);
10732 
10733           Diagnosed = true;
10734           break;
10735         }
10736 
10737         if (FirstTemplateArgs && SecondTemplateArgs) {
10738           // Remove pack expansions from argument list.
10739           auto ExpandTemplateArgumentList =
10740               [](const TemplateArgumentList *TAL) {
10741                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10742                 for (const TemplateArgument &TA : TAL->asArray()) {
10743                   if (TA.getKind() != TemplateArgument::Pack) {
10744                     ExpandedList.push_back(&TA);
10745                     continue;
10746                   }
10747                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10748                     ExpandedList.push_back(&PackTA);
10749                   }
10750                 }
10751                 return ExpandedList;
10752               };
10753           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10754               ExpandTemplateArgumentList(FirstTemplateArgs);
10755           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10756               ExpandTemplateArgumentList(SecondTemplateArgs);
10757 
10758           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10759             ODRDiagDeclError(FirstRecord, FirstModule,
10760                              FirstMethod->getLocation(),
10761                              FirstMethod->getSourceRange(),
10762                              MethodDifferentNumberTemplateArguments)
10763                 << FirstMethodType << FirstName
10764                 << (unsigned)FirstExpandedList.size();
10765             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10766                             SecondMethod->getSourceRange(),
10767                             MethodDifferentNumberTemplateArguments)
10768                 << SecondMethodType << SecondName
10769                 << (unsigned)SecondExpandedList.size();
10770 
10771             Diagnosed = true;
10772             break;
10773           }
10774 
10775           bool TemplateArgumentMismatch = false;
10776           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10777             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10778                                    &SecondTA = *SecondExpandedList[i];
10779             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10780                 ComputeTemplateArgumentODRHash(SecondTA)) {
10781               continue;
10782             }
10783 
10784             ODRDiagDeclError(
10785                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10786                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10787                 << FirstMethodType << FirstName << FirstTA << i + 1;
10788             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10789                             SecondMethod->getSourceRange(),
10790                             MethodDifferentTemplateArgument)
10791                 << SecondMethodType << SecondName << SecondTA << i + 1;
10792 
10793             TemplateArgumentMismatch = true;
10794             break;
10795           }
10796 
10797           if (TemplateArgumentMismatch) {
10798             Diagnosed = true;
10799             break;
10800           }
10801         }
10802 
10803         // Compute the hash of the method as if it has no body.
10804         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10805           Hash.clear();
10806           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10807           return Hash.CalculateHash();
10808         };
10809 
10810         // Compare the hash generated to the hash stored.  A difference means
10811         // that a body was present in the original source.  Due to merging,
10812         // the stardard way of detecting a body will not work.
10813         const bool HasFirstBody =
10814             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10815         const bool HasSecondBody =
10816             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10817 
10818         if (HasFirstBody != HasSecondBody) {
10819           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10820                            FirstMethod->getSourceRange(), MethodSingleBody)
10821               << FirstMethodType << FirstName << HasFirstBody;
10822           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10823                           SecondMethod->getSourceRange(), MethodSingleBody)
10824               << SecondMethodType << SecondName << HasSecondBody;
10825           Diagnosed = true;
10826           break;
10827         }
10828 
10829         if (HasFirstBody && HasSecondBody) {
10830           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10831                            FirstMethod->getSourceRange(), MethodDifferentBody)
10832               << FirstMethodType << FirstName;
10833           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10834                           SecondMethod->getSourceRange(), MethodDifferentBody)
10835               << SecondMethodType << SecondName;
10836           Diagnosed = true;
10837           break;
10838         }
10839 
10840         break;
10841       }
10842       case TypeAlias:
10843       case TypeDef: {
10844         Diagnosed = ODRDiagTypeDefOrAlias(
10845             FirstRecord, FirstModule, SecondModule,
10846             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10847             FirstDiffType == TypeAlias);
10848         break;
10849       }
10850       case Var: {
10851         Diagnosed =
10852             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10853                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10854         break;
10855       }
10856       case Friend: {
10857         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10858         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10859 
10860         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10861         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10862 
10863         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10864         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10865 
10866         if (FirstND && SecondND) {
10867           ODRDiagDeclError(FirstRecord, FirstModule,
10868                            FirstFriend->getFriendLoc(),
10869                            FirstFriend->getSourceRange(), FriendFunction)
10870               << FirstND;
10871           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10872                           SecondFriend->getSourceRange(), FriendFunction)
10873               << SecondND;
10874 
10875           Diagnosed = true;
10876           break;
10877         }
10878 
10879         if (FirstTSI && SecondTSI) {
10880           QualType FirstFriendType = FirstTSI->getType();
10881           QualType SecondFriendType = SecondTSI->getType();
10882           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10883                  ComputeQualTypeODRHash(SecondFriendType));
10884           ODRDiagDeclError(FirstRecord, FirstModule,
10885                            FirstFriend->getFriendLoc(),
10886                            FirstFriend->getSourceRange(), FriendType)
10887               << FirstFriendType;
10888           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10889                           SecondFriend->getSourceRange(), FriendType)
10890               << SecondFriendType;
10891           Diagnosed = true;
10892           break;
10893         }
10894 
10895         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10896                          FirstFriend->getSourceRange(), FriendTypeFunction)
10897             << (FirstTSI == nullptr);
10898         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10899                         SecondFriend->getSourceRange(), FriendTypeFunction)
10900             << (SecondTSI == nullptr);
10901 
10902         Diagnosed = true;
10903         break;
10904       }
10905       case FunctionTemplate: {
10906         FunctionTemplateDecl *FirstTemplate =
10907             cast<FunctionTemplateDecl>(FirstDecl);
10908         FunctionTemplateDecl *SecondTemplate =
10909             cast<FunctionTemplateDecl>(SecondDecl);
10910 
10911         TemplateParameterList *FirstTPL =
10912             FirstTemplate->getTemplateParameters();
10913         TemplateParameterList *SecondTPL =
10914             SecondTemplate->getTemplateParameters();
10915 
10916         if (FirstTPL->size() != SecondTPL->size()) {
10917           ODRDiagDeclError(FirstRecord, FirstModule,
10918                            FirstTemplate->getLocation(),
10919                            FirstTemplate->getSourceRange(),
10920                            FunctionTemplateDifferentNumberParameters)
10921               << FirstTemplate << FirstTPL->size();
10922           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10923                           SecondTemplate->getSourceRange(),
10924                           FunctionTemplateDifferentNumberParameters)
10925               << SecondTemplate << SecondTPL->size();
10926 
10927           Diagnosed = true;
10928           break;
10929         }
10930 
10931         bool ParameterMismatch = false;
10932         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10933           NamedDecl *FirstParam = FirstTPL->getParam(i);
10934           NamedDecl *SecondParam = SecondTPL->getParam(i);
10935 
10936           if (FirstParam->getKind() != SecondParam->getKind()) {
10937             enum {
10938               TemplateTypeParameter,
10939               NonTypeTemplateParameter,
10940               TemplateTemplateParameter,
10941             };
10942             auto GetParamType = [](NamedDecl *D) {
10943               switch (D->getKind()) {
10944                 default:
10945                   llvm_unreachable("Unexpected template parameter type");
10946                 case Decl::TemplateTypeParm:
10947                   return TemplateTypeParameter;
10948                 case Decl::NonTypeTemplateParm:
10949                   return NonTypeTemplateParameter;
10950                 case Decl::TemplateTemplateParm:
10951                   return TemplateTemplateParameter;
10952               }
10953             };
10954 
10955             ODRDiagDeclError(FirstRecord, FirstModule,
10956                              FirstTemplate->getLocation(),
10957                              FirstTemplate->getSourceRange(),
10958                              FunctionTemplateParameterDifferentKind)
10959                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10960             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10961                             SecondTemplate->getSourceRange(),
10962                             FunctionTemplateParameterDifferentKind)
10963                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10964 
10965             ParameterMismatch = true;
10966             break;
10967           }
10968 
10969           if (FirstParam->getName() != SecondParam->getName()) {
10970             ODRDiagDeclError(
10971                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10972                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10973                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10974                 << FirstParam;
10975             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10976                             SecondTemplate->getSourceRange(),
10977                             FunctionTemplateParameterName)
10978                 << SecondTemplate << (i + 1)
10979                 << (bool)SecondParam->getIdentifier() << SecondParam;
10980             ParameterMismatch = true;
10981             break;
10982           }
10983 
10984           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10985               isa<TemplateTypeParmDecl>(SecondParam)) {
10986             TemplateTypeParmDecl *FirstTTPD =
10987                 cast<TemplateTypeParmDecl>(FirstParam);
10988             TemplateTypeParmDecl *SecondTTPD =
10989                 cast<TemplateTypeParmDecl>(SecondParam);
10990             bool HasFirstDefaultArgument =
10991                 FirstTTPD->hasDefaultArgument() &&
10992                 !FirstTTPD->defaultArgumentWasInherited();
10993             bool HasSecondDefaultArgument =
10994                 SecondTTPD->hasDefaultArgument() &&
10995                 !SecondTTPD->defaultArgumentWasInherited();
10996             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10997               ODRDiagDeclError(FirstRecord, FirstModule,
10998                                FirstTemplate->getLocation(),
10999                                FirstTemplate->getSourceRange(),
11000                                FunctionTemplateParameterSingleDefaultArgument)
11001                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11002               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11003                               SecondTemplate->getSourceRange(),
11004                               FunctionTemplateParameterSingleDefaultArgument)
11005                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11006               ParameterMismatch = true;
11007               break;
11008             }
11009 
11010             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11011               QualType FirstType = FirstTTPD->getDefaultArgument();
11012               QualType SecondType = SecondTTPD->getDefaultArgument();
11013               if (ComputeQualTypeODRHash(FirstType) !=
11014                   ComputeQualTypeODRHash(SecondType)) {
11015                 ODRDiagDeclError(
11016                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11017                     FirstTemplate->getSourceRange(),
11018                     FunctionTemplateParameterDifferentDefaultArgument)
11019                     << FirstTemplate << (i + 1) << FirstType;
11020                 ODRDiagDeclNote(
11021                     SecondModule, SecondTemplate->getLocation(),
11022                     SecondTemplate->getSourceRange(),
11023                     FunctionTemplateParameterDifferentDefaultArgument)
11024                     << SecondTemplate << (i + 1) << SecondType;
11025                 ParameterMismatch = true;
11026                 break;
11027               }
11028             }
11029 
11030             if (FirstTTPD->isParameterPack() !=
11031                 SecondTTPD->isParameterPack()) {
11032               ODRDiagDeclError(FirstRecord, FirstModule,
11033                                FirstTemplate->getLocation(),
11034                                FirstTemplate->getSourceRange(),
11035                                FunctionTemplatePackParameter)
11036                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11037               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11038                               SecondTemplate->getSourceRange(),
11039                               FunctionTemplatePackParameter)
11040                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11041               ParameterMismatch = true;
11042               break;
11043             }
11044           }
11045 
11046           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11047               isa<TemplateTemplateParmDecl>(SecondParam)) {
11048             TemplateTemplateParmDecl *FirstTTPD =
11049                 cast<TemplateTemplateParmDecl>(FirstParam);
11050             TemplateTemplateParmDecl *SecondTTPD =
11051                 cast<TemplateTemplateParmDecl>(SecondParam);
11052 
11053             TemplateParameterList *FirstTPL =
11054                 FirstTTPD->getTemplateParameters();
11055             TemplateParameterList *SecondTPL =
11056                 SecondTTPD->getTemplateParameters();
11057 
11058             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11059                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11060               ODRDiagDeclError(FirstRecord, FirstModule,
11061                                FirstTemplate->getLocation(),
11062                                FirstTemplate->getSourceRange(),
11063                                FunctionTemplateParameterDifferentType)
11064                   << FirstTemplate << (i + 1);
11065               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11066                               SecondTemplate->getSourceRange(),
11067                               FunctionTemplateParameterDifferentType)
11068                   << SecondTemplate << (i + 1);
11069               ParameterMismatch = true;
11070               break;
11071             }
11072 
11073             bool HasFirstDefaultArgument =
11074                 FirstTTPD->hasDefaultArgument() &&
11075                 !FirstTTPD->defaultArgumentWasInherited();
11076             bool HasSecondDefaultArgument =
11077                 SecondTTPD->hasDefaultArgument() &&
11078                 !SecondTTPD->defaultArgumentWasInherited();
11079             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11080               ODRDiagDeclError(FirstRecord, FirstModule,
11081                                FirstTemplate->getLocation(),
11082                                FirstTemplate->getSourceRange(),
11083                                FunctionTemplateParameterSingleDefaultArgument)
11084                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11085               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11086                               SecondTemplate->getSourceRange(),
11087                               FunctionTemplateParameterSingleDefaultArgument)
11088                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11089               ParameterMismatch = true;
11090               break;
11091             }
11092 
11093             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11094               TemplateArgument FirstTA =
11095                   FirstTTPD->getDefaultArgument().getArgument();
11096               TemplateArgument SecondTA =
11097                   SecondTTPD->getDefaultArgument().getArgument();
11098               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11099                   ComputeTemplateArgumentODRHash(SecondTA)) {
11100                 ODRDiagDeclError(
11101                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11102                     FirstTemplate->getSourceRange(),
11103                     FunctionTemplateParameterDifferentDefaultArgument)
11104                     << FirstTemplate << (i + 1) << FirstTA;
11105                 ODRDiagDeclNote(
11106                     SecondModule, SecondTemplate->getLocation(),
11107                     SecondTemplate->getSourceRange(),
11108                     FunctionTemplateParameterDifferentDefaultArgument)
11109                     << SecondTemplate << (i + 1) << SecondTA;
11110                 ParameterMismatch = true;
11111                 break;
11112               }
11113             }
11114 
11115             if (FirstTTPD->isParameterPack() !=
11116                 SecondTTPD->isParameterPack()) {
11117               ODRDiagDeclError(FirstRecord, FirstModule,
11118                                FirstTemplate->getLocation(),
11119                                FirstTemplate->getSourceRange(),
11120                                FunctionTemplatePackParameter)
11121                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11122               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11123                               SecondTemplate->getSourceRange(),
11124                               FunctionTemplatePackParameter)
11125                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11126               ParameterMismatch = true;
11127               break;
11128             }
11129           }
11130 
11131           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11132               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11133             NonTypeTemplateParmDecl *FirstNTTPD =
11134                 cast<NonTypeTemplateParmDecl>(FirstParam);
11135             NonTypeTemplateParmDecl *SecondNTTPD =
11136                 cast<NonTypeTemplateParmDecl>(SecondParam);
11137 
11138             QualType FirstType = FirstNTTPD->getType();
11139             QualType SecondType = SecondNTTPD->getType();
11140             if (ComputeQualTypeODRHash(FirstType) !=
11141                 ComputeQualTypeODRHash(SecondType)) {
11142               ODRDiagDeclError(FirstRecord, FirstModule,
11143                                FirstTemplate->getLocation(),
11144                                FirstTemplate->getSourceRange(),
11145                                FunctionTemplateParameterDifferentType)
11146                   << FirstTemplate << (i + 1);
11147               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11148                               SecondTemplate->getSourceRange(),
11149                               FunctionTemplateParameterDifferentType)
11150                   << SecondTemplate << (i + 1);
11151               ParameterMismatch = true;
11152               break;
11153             }
11154 
11155             bool HasFirstDefaultArgument =
11156                 FirstNTTPD->hasDefaultArgument() &&
11157                 !FirstNTTPD->defaultArgumentWasInherited();
11158             bool HasSecondDefaultArgument =
11159                 SecondNTTPD->hasDefaultArgument() &&
11160                 !SecondNTTPD->defaultArgumentWasInherited();
11161             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11162               ODRDiagDeclError(FirstRecord, FirstModule,
11163                                FirstTemplate->getLocation(),
11164                                FirstTemplate->getSourceRange(),
11165                                FunctionTemplateParameterSingleDefaultArgument)
11166                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11167               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11168                               SecondTemplate->getSourceRange(),
11169                               FunctionTemplateParameterSingleDefaultArgument)
11170                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11171               ParameterMismatch = true;
11172               break;
11173             }
11174 
11175             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11176               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11177               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11178               if (ComputeODRHash(FirstDefaultArgument) !=
11179                   ComputeODRHash(SecondDefaultArgument)) {
11180                 ODRDiagDeclError(
11181                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11182                     FirstTemplate->getSourceRange(),
11183                     FunctionTemplateParameterDifferentDefaultArgument)
11184                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11185                 ODRDiagDeclNote(
11186                     SecondModule, SecondTemplate->getLocation(),
11187                     SecondTemplate->getSourceRange(),
11188                     FunctionTemplateParameterDifferentDefaultArgument)
11189                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11190                 ParameterMismatch = true;
11191                 break;
11192               }
11193             }
11194 
11195             if (FirstNTTPD->isParameterPack() !=
11196                 SecondNTTPD->isParameterPack()) {
11197               ODRDiagDeclError(FirstRecord, FirstModule,
11198                                FirstTemplate->getLocation(),
11199                                FirstTemplate->getSourceRange(),
11200                                FunctionTemplatePackParameter)
11201                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11202               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11203                               SecondTemplate->getSourceRange(),
11204                               FunctionTemplatePackParameter)
11205                   << SecondTemplate << (i + 1)
11206                   << SecondNTTPD->isParameterPack();
11207               ParameterMismatch = true;
11208               break;
11209             }
11210           }
11211         }
11212 
11213         if (ParameterMismatch) {
11214           Diagnosed = true;
11215           break;
11216         }
11217 
11218         break;
11219       }
11220       }
11221 
11222       if (Diagnosed)
11223         continue;
11224 
11225       Diag(FirstDecl->getLocation(),
11226            diag::err_module_odr_violation_mismatch_decl_unknown)
11227           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11228           << FirstDecl->getSourceRange();
11229       Diag(SecondDecl->getLocation(),
11230            diag::note_module_odr_violation_mismatch_decl_unknown)
11231           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11232       Diagnosed = true;
11233     }
11234 
11235     if (!Diagnosed) {
11236       // All definitions are updates to the same declaration. This happens if a
11237       // module instantiates the declaration of a class template specialization
11238       // and two or more other modules instantiate its definition.
11239       //
11240       // FIXME: Indicate which modules had instantiations of this definition.
11241       // FIXME: How can this even happen?
11242       Diag(Merge.first->getLocation(),
11243            diag::err_module_odr_violation_different_instantiations)
11244         << Merge.first;
11245     }
11246   }
11247 
11248   // Issue ODR failures diagnostics for functions.
11249   for (auto &Merge : FunctionOdrMergeFailures) {
11250     enum ODRFunctionDifference {
11251       ReturnType,
11252       ParameterName,
11253       ParameterType,
11254       ParameterSingleDefaultArgument,
11255       ParameterDifferentDefaultArgument,
11256       FunctionBody,
11257     };
11258 
11259     FunctionDecl *FirstFunction = Merge.first;
11260     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11261 
11262     bool Diagnosed = false;
11263     for (auto &SecondFunction : Merge.second) {
11264 
11265       if (FirstFunction == SecondFunction)
11266         continue;
11267 
11268       std::string SecondModule =
11269           getOwningModuleNameForDiagnostic(SecondFunction);
11270 
11271       auto ODRDiagError = [FirstFunction, &FirstModule,
11272                            this](SourceLocation Loc, SourceRange Range,
11273                                  ODRFunctionDifference DiffType) {
11274         return Diag(Loc, diag::err_module_odr_violation_function)
11275                << FirstFunction << FirstModule.empty() << FirstModule << Range
11276                << DiffType;
11277       };
11278       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11279                                                SourceRange Range,
11280                                                ODRFunctionDifference DiffType) {
11281         return Diag(Loc, diag::note_module_odr_violation_function)
11282                << SecondModule << Range << DiffType;
11283       };
11284 
11285       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11286           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11287         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11288                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11289             << FirstFunction->getReturnType();
11290         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11291                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11292             << SecondFunction->getReturnType();
11293         Diagnosed = true;
11294         break;
11295       }
11296 
11297       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11298              "Merged functions with different number of parameters");
11299 
11300       auto ParamSize = FirstFunction->param_size();
11301       bool ParameterMismatch = false;
11302       for (unsigned I = 0; I < ParamSize; ++I) {
11303         auto *FirstParam = FirstFunction->getParamDecl(I);
11304         auto *SecondParam = SecondFunction->getParamDecl(I);
11305 
11306         assert(getContext().hasSameType(FirstParam->getType(),
11307                                       SecondParam->getType()) &&
11308                "Merged function has different parameter types.");
11309 
11310         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11311           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11312                        ParameterName)
11313               << I + 1 << FirstParam->getDeclName();
11314           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11315                       ParameterName)
11316               << I + 1 << SecondParam->getDeclName();
11317           ParameterMismatch = true;
11318           break;
11319         };
11320 
11321         QualType FirstParamType = FirstParam->getType();
11322         QualType SecondParamType = SecondParam->getType();
11323         if (FirstParamType != SecondParamType &&
11324             ComputeQualTypeODRHash(FirstParamType) !=
11325                 ComputeQualTypeODRHash(SecondParamType)) {
11326           if (const DecayedType *ParamDecayedType =
11327                   FirstParamType->getAs<DecayedType>()) {
11328             ODRDiagError(FirstParam->getLocation(),
11329                          FirstParam->getSourceRange(), ParameterType)
11330                 << (I + 1) << FirstParamType << true
11331                 << ParamDecayedType->getOriginalType();
11332           } else {
11333             ODRDiagError(FirstParam->getLocation(),
11334                          FirstParam->getSourceRange(), ParameterType)
11335                 << (I + 1) << FirstParamType << false;
11336           }
11337 
11338           if (const DecayedType *ParamDecayedType =
11339                   SecondParamType->getAs<DecayedType>()) {
11340             ODRDiagNote(SecondParam->getLocation(),
11341                         SecondParam->getSourceRange(), ParameterType)
11342                 << (I + 1) << SecondParamType << true
11343                 << ParamDecayedType->getOriginalType();
11344           } else {
11345             ODRDiagNote(SecondParam->getLocation(),
11346                         SecondParam->getSourceRange(), ParameterType)
11347                 << (I + 1) << SecondParamType << false;
11348           }
11349           ParameterMismatch = true;
11350           break;
11351         }
11352 
11353         const Expr *FirstInit = FirstParam->getInit();
11354         const Expr *SecondInit = SecondParam->getInit();
11355         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11356           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11357                        ParameterSingleDefaultArgument)
11358               << (I + 1) << (FirstInit == nullptr)
11359               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11360           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11361                       ParameterSingleDefaultArgument)
11362               << (I + 1) << (SecondInit == nullptr)
11363               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11364           ParameterMismatch = true;
11365           break;
11366         }
11367 
11368         if (FirstInit && SecondInit &&
11369             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11370           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11371                        ParameterDifferentDefaultArgument)
11372               << (I + 1) << FirstInit->getSourceRange();
11373           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11374                       ParameterDifferentDefaultArgument)
11375               << (I + 1) << SecondInit->getSourceRange();
11376           ParameterMismatch = true;
11377           break;
11378         }
11379 
11380         assert(ComputeSubDeclODRHash(FirstParam) ==
11381                    ComputeSubDeclODRHash(SecondParam) &&
11382                "Undiagnosed parameter difference.");
11383       }
11384 
11385       if (ParameterMismatch) {
11386         Diagnosed = true;
11387         break;
11388       }
11389 
11390       // If no error has been generated before now, assume the problem is in
11391       // the body and generate a message.
11392       ODRDiagError(FirstFunction->getLocation(),
11393                    FirstFunction->getSourceRange(), FunctionBody);
11394       ODRDiagNote(SecondFunction->getLocation(),
11395                   SecondFunction->getSourceRange(), FunctionBody);
11396       Diagnosed = true;
11397       break;
11398     }
11399     (void)Diagnosed;
11400     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11401   }
11402 
11403   // Issue ODR failures diagnostics for enums.
11404   for (auto &Merge : EnumOdrMergeFailures) {
11405     enum ODREnumDifference {
11406       SingleScopedEnum,
11407       EnumTagKeywordMismatch,
11408       SingleSpecifiedType,
11409       DifferentSpecifiedTypes,
11410       DifferentNumberEnumConstants,
11411       EnumConstantName,
11412       EnumConstantSingleInitilizer,
11413       EnumConstantDifferentInitilizer,
11414     };
11415 
11416     // If we've already pointed out a specific problem with this enum, don't
11417     // bother issuing a general "something's different" diagnostic.
11418     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11419       continue;
11420 
11421     EnumDecl *FirstEnum = Merge.first;
11422     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11423 
11424     using DeclHashes =
11425         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11426     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11427                               DeclHashes &Hashes, EnumDecl *Enum) {
11428       for (auto *D : Enum->decls()) {
11429         // Due to decl merging, the first EnumDecl is the parent of
11430         // Decls in both records.
11431         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11432           continue;
11433         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11434         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11435                             ComputeSubDeclODRHash(D));
11436       }
11437     };
11438     DeclHashes FirstHashes;
11439     PopulateHashes(FirstHashes, FirstEnum);
11440     bool Diagnosed = false;
11441     for (auto &SecondEnum : Merge.second) {
11442 
11443       if (FirstEnum == SecondEnum)
11444         continue;
11445 
11446       std::string SecondModule =
11447           getOwningModuleNameForDiagnostic(SecondEnum);
11448 
11449       auto ODRDiagError = [FirstEnum, &FirstModule,
11450                            this](SourceLocation Loc, SourceRange Range,
11451                                  ODREnumDifference DiffType) {
11452         return Diag(Loc, diag::err_module_odr_violation_enum)
11453                << FirstEnum << FirstModule.empty() << FirstModule << Range
11454                << DiffType;
11455       };
11456       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11457                                                SourceRange Range,
11458                                                ODREnumDifference DiffType) {
11459         return Diag(Loc, diag::note_module_odr_violation_enum)
11460                << SecondModule << Range << DiffType;
11461       };
11462 
11463       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11464         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11465                      SingleScopedEnum)
11466             << FirstEnum->isScoped();
11467         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11468                     SingleScopedEnum)
11469             << SecondEnum->isScoped();
11470         Diagnosed = true;
11471         continue;
11472       }
11473 
11474       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11475         if (FirstEnum->isScopedUsingClassTag() !=
11476             SecondEnum->isScopedUsingClassTag()) {
11477           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11478                        EnumTagKeywordMismatch)
11479               << FirstEnum->isScopedUsingClassTag();
11480           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11481                       EnumTagKeywordMismatch)
11482               << SecondEnum->isScopedUsingClassTag();
11483           Diagnosed = true;
11484           continue;
11485         }
11486       }
11487 
11488       QualType FirstUnderlyingType =
11489           FirstEnum->getIntegerTypeSourceInfo()
11490               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11491               : QualType();
11492       QualType SecondUnderlyingType =
11493           SecondEnum->getIntegerTypeSourceInfo()
11494               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11495               : QualType();
11496       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11497           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11498                        SingleSpecifiedType)
11499               << !FirstUnderlyingType.isNull();
11500           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11501                       SingleSpecifiedType)
11502               << !SecondUnderlyingType.isNull();
11503           Diagnosed = true;
11504           continue;
11505       }
11506 
11507       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11508         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11509             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11510           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11511                        DifferentSpecifiedTypes)
11512               << FirstUnderlyingType;
11513           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11514                       DifferentSpecifiedTypes)
11515               << SecondUnderlyingType;
11516           Diagnosed = true;
11517           continue;
11518         }
11519       }
11520 
11521       DeclHashes SecondHashes;
11522       PopulateHashes(SecondHashes, SecondEnum);
11523 
11524       if (FirstHashes.size() != SecondHashes.size()) {
11525         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11526                      DifferentNumberEnumConstants)
11527             << (int)FirstHashes.size();
11528         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11529                     DifferentNumberEnumConstants)
11530             << (int)SecondHashes.size();
11531         Diagnosed = true;
11532         continue;
11533       }
11534 
11535       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11536         if (FirstHashes[I].second == SecondHashes[I].second)
11537           continue;
11538         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11539         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11540 
11541         if (FirstEnumConstant->getDeclName() !=
11542             SecondEnumConstant->getDeclName()) {
11543 
11544           ODRDiagError(FirstEnumConstant->getLocation(),
11545                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11546               << I + 1 << FirstEnumConstant;
11547           ODRDiagNote(SecondEnumConstant->getLocation(),
11548                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11549               << I + 1 << SecondEnumConstant;
11550           Diagnosed = true;
11551           break;
11552         }
11553 
11554         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11555         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11556         if (!FirstInit && !SecondInit)
11557           continue;
11558 
11559         if (!FirstInit || !SecondInit) {
11560           ODRDiagError(FirstEnumConstant->getLocation(),
11561                        FirstEnumConstant->getSourceRange(),
11562                        EnumConstantSingleInitilizer)
11563               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11564           ODRDiagNote(SecondEnumConstant->getLocation(),
11565                       SecondEnumConstant->getSourceRange(),
11566                       EnumConstantSingleInitilizer)
11567               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11568           Diagnosed = true;
11569           break;
11570         }
11571 
11572         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11573           ODRDiagError(FirstEnumConstant->getLocation(),
11574                        FirstEnumConstant->getSourceRange(),
11575                        EnumConstantDifferentInitilizer)
11576               << I + 1 << FirstEnumConstant;
11577           ODRDiagNote(SecondEnumConstant->getLocation(),
11578                       SecondEnumConstant->getSourceRange(),
11579                       EnumConstantDifferentInitilizer)
11580               << I + 1 << SecondEnumConstant;
11581           Diagnosed = true;
11582           break;
11583         }
11584       }
11585     }
11586 
11587     (void)Diagnosed;
11588     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11589   }
11590 }
11591 
11592 void ASTReader::StartedDeserializing() {
11593   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11594     ReadTimer->startTimer();
11595 }
11596 
11597 void ASTReader::FinishedDeserializing() {
11598   assert(NumCurrentElementsDeserializing &&
11599          "FinishedDeserializing not paired with StartedDeserializing");
11600   if (NumCurrentElementsDeserializing == 1) {
11601     // We decrease NumCurrentElementsDeserializing only after pending actions
11602     // are finished, to avoid recursively re-calling finishPendingActions().
11603     finishPendingActions();
11604   }
11605   --NumCurrentElementsDeserializing;
11606 
11607   if (NumCurrentElementsDeserializing == 0) {
11608     // Propagate exception specification and deduced type updates along
11609     // redeclaration chains.
11610     //
11611     // We do this now rather than in finishPendingActions because we want to
11612     // be able to walk the complete redeclaration chains of the updated decls.
11613     while (!PendingExceptionSpecUpdates.empty() ||
11614            !PendingDeducedTypeUpdates.empty()) {
11615       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11616       PendingExceptionSpecUpdates.clear();
11617       for (auto Update : ESUpdates) {
11618         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11619         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11620         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11621         if (auto *Listener = getContext().getASTMutationListener())
11622           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11623         for (auto *Redecl : Update.second->redecls())
11624           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11625       }
11626 
11627       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11628       PendingDeducedTypeUpdates.clear();
11629       for (auto Update : DTUpdates) {
11630         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11631         // FIXME: If the return type is already deduced, check that it matches.
11632         getContext().adjustDeducedFunctionResultType(Update.first,
11633                                                      Update.second);
11634       }
11635     }
11636 
11637     if (ReadTimer)
11638       ReadTimer->stopTimer();
11639 
11640     diagnoseOdrViolations();
11641 
11642     // We are not in recursive loading, so it's safe to pass the "interesting"
11643     // decls to the consumer.
11644     if (Consumer)
11645       PassInterestingDeclsToConsumer();
11646   }
11647 }
11648 
11649 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11650   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11651     // Remove any fake results before adding any real ones.
11652     auto It = PendingFakeLookupResults.find(II);
11653     if (It != PendingFakeLookupResults.end()) {
11654       for (auto *ND : It->second)
11655         SemaObj->IdResolver.RemoveDecl(ND);
11656       // FIXME: this works around module+PCH performance issue.
11657       // Rather than erase the result from the map, which is O(n), just clear
11658       // the vector of NamedDecls.
11659       It->second.clear();
11660     }
11661   }
11662 
11663   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11664     SemaObj->TUScope->AddDecl(D);
11665   } else if (SemaObj->TUScope) {
11666     // Adding the decl to IdResolver may have failed because it was already in
11667     // (even though it was not added in scope). If it is already in, make sure
11668     // it gets in the scope as well.
11669     if (std::find(SemaObj->IdResolver.begin(Name),
11670                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11671       SemaObj->TUScope->AddDecl(D);
11672   }
11673 }
11674 
11675 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11676                      ASTContext *Context,
11677                      const PCHContainerReader &PCHContainerRdr,
11678                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11679                      StringRef isysroot, bool DisableValidation,
11680                      bool AllowASTWithCompilerErrors,
11681                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11682                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11683                      std::unique_ptr<llvm::Timer> ReadTimer)
11684     : Listener(DisableValidation
11685                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11686                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11687       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11688       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11689       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11690                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11691       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11692       DisableValidation(DisableValidation),
11693       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11694       AllowConfigurationMismatch(AllowConfigurationMismatch),
11695       ValidateSystemInputs(ValidateSystemInputs),
11696       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11697       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11698   SourceMgr.setExternalSLocEntrySource(this);
11699 
11700   for (const auto &Ext : Extensions) {
11701     auto BlockName = Ext->getExtensionMetadata().BlockName;
11702     auto Known = ModuleFileExtensions.find(BlockName);
11703     if (Known != ModuleFileExtensions.end()) {
11704       Diags.Report(diag::warn_duplicate_module_file_extension)
11705         << BlockName;
11706       continue;
11707     }
11708 
11709     ModuleFileExtensions.insert({BlockName, Ext});
11710   }
11711 }
11712 
11713 ASTReader::~ASTReader() {
11714   if (OwnsDeserializationListener)
11715     delete DeserializationListener;
11716 }
11717 
11718 IdentifierResolver &ASTReader::getIdResolver() {
11719   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11720 }
11721 
11722 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11723                                                unsigned AbbrevID) {
11724   Idx = 0;
11725   Record.clear();
11726   return Cursor.readRecord(AbbrevID, Record);
11727 }
11728 //===----------------------------------------------------------------------===//
11729 //// OMPClauseReader implementation
11730 ////===----------------------------------------------------------------------===//
11731 
11732 // This has to be in namespace clang because it's friended by all
11733 // of the OMP clauses.
11734 namespace clang {
11735 
11736 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11737   ASTRecordReader &Record;
11738   ASTContext &Context;
11739 
11740 public:
11741   OMPClauseReader(ASTRecordReader &Record)
11742       : Record(Record), Context(Record.getContext()) {}
11743 
11744 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11745 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11746   OMPClause *readClause();
11747   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11748   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11749 };
11750 
11751 } // end namespace clang
11752 
11753 OMPClause *ASTRecordReader::readOMPClause() {
11754   return OMPClauseReader(*this).readClause();
11755 }
11756 
11757 OMPClause *OMPClauseReader::readClause() {
11758   OMPClause *C = nullptr;
11759   switch (llvm::omp::Clause(Record.readInt())) {
11760   case llvm::omp::OMPC_if:
11761     C = new (Context) OMPIfClause();
11762     break;
11763   case llvm::omp::OMPC_final:
11764     C = new (Context) OMPFinalClause();
11765     break;
11766   case llvm::omp::OMPC_num_threads:
11767     C = new (Context) OMPNumThreadsClause();
11768     break;
11769   case llvm::omp::OMPC_safelen:
11770     C = new (Context) OMPSafelenClause();
11771     break;
11772   case llvm::omp::OMPC_simdlen:
11773     C = new (Context) OMPSimdlenClause();
11774     break;
11775   case llvm::omp::OMPC_allocator:
11776     C = new (Context) OMPAllocatorClause();
11777     break;
11778   case llvm::omp::OMPC_collapse:
11779     C = new (Context) OMPCollapseClause();
11780     break;
11781   case llvm::omp::OMPC_default:
11782     C = new (Context) OMPDefaultClause();
11783     break;
11784   case llvm::omp::OMPC_proc_bind:
11785     C = new (Context) OMPProcBindClause();
11786     break;
11787   case llvm::omp::OMPC_schedule:
11788     C = new (Context) OMPScheduleClause();
11789     break;
11790   case llvm::omp::OMPC_ordered:
11791     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11792     break;
11793   case llvm::omp::OMPC_nowait:
11794     C = new (Context) OMPNowaitClause();
11795     break;
11796   case llvm::omp::OMPC_untied:
11797     C = new (Context) OMPUntiedClause();
11798     break;
11799   case llvm::omp::OMPC_mergeable:
11800     C = new (Context) OMPMergeableClause();
11801     break;
11802   case llvm::omp::OMPC_read:
11803     C = new (Context) OMPReadClause();
11804     break;
11805   case llvm::omp::OMPC_write:
11806     C = new (Context) OMPWriteClause();
11807     break;
11808   case llvm::omp::OMPC_update:
11809     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11810     break;
11811   case llvm::omp::OMPC_capture:
11812     C = new (Context) OMPCaptureClause();
11813     break;
11814   case llvm::omp::OMPC_seq_cst:
11815     C = new (Context) OMPSeqCstClause();
11816     break;
11817   case llvm::omp::OMPC_acq_rel:
11818     C = new (Context) OMPAcqRelClause();
11819     break;
11820   case llvm::omp::OMPC_acquire:
11821     C = new (Context) OMPAcquireClause();
11822     break;
11823   case llvm::omp::OMPC_release:
11824     C = new (Context) OMPReleaseClause();
11825     break;
11826   case llvm::omp::OMPC_relaxed:
11827     C = new (Context) OMPRelaxedClause();
11828     break;
11829   case llvm::omp::OMPC_threads:
11830     C = new (Context) OMPThreadsClause();
11831     break;
11832   case llvm::omp::OMPC_simd:
11833     C = new (Context) OMPSIMDClause();
11834     break;
11835   case llvm::omp::OMPC_nogroup:
11836     C = new (Context) OMPNogroupClause();
11837     break;
11838   case llvm::omp::OMPC_unified_address:
11839     C = new (Context) OMPUnifiedAddressClause();
11840     break;
11841   case llvm::omp::OMPC_unified_shared_memory:
11842     C = new (Context) OMPUnifiedSharedMemoryClause();
11843     break;
11844   case llvm::omp::OMPC_reverse_offload:
11845     C = new (Context) OMPReverseOffloadClause();
11846     break;
11847   case llvm::omp::OMPC_dynamic_allocators:
11848     C = new (Context) OMPDynamicAllocatorsClause();
11849     break;
11850   case llvm::omp::OMPC_atomic_default_mem_order:
11851     C = new (Context) OMPAtomicDefaultMemOrderClause();
11852     break;
11853  case llvm::omp::OMPC_private:
11854     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11855     break;
11856   case llvm::omp::OMPC_firstprivate:
11857     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11858     break;
11859   case llvm::omp::OMPC_lastprivate:
11860     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11861     break;
11862   case llvm::omp::OMPC_shared:
11863     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11864     break;
11865   case llvm::omp::OMPC_reduction: {
11866     unsigned N = Record.readInt();
11867     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11868     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11869     break;
11870   }
11871   case llvm::omp::OMPC_task_reduction:
11872     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11873     break;
11874   case llvm::omp::OMPC_in_reduction:
11875     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11876     break;
11877   case llvm::omp::OMPC_linear:
11878     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11879     break;
11880   case llvm::omp::OMPC_aligned:
11881     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11882     break;
11883   case llvm::omp::OMPC_copyin:
11884     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11885     break;
11886   case llvm::omp::OMPC_copyprivate:
11887     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11888     break;
11889   case llvm::omp::OMPC_flush:
11890     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11891     break;
11892   case llvm::omp::OMPC_depobj:
11893     C = OMPDepobjClause::CreateEmpty(Context);
11894     break;
11895   case llvm::omp::OMPC_depend: {
11896     unsigned NumVars = Record.readInt();
11897     unsigned NumLoops = Record.readInt();
11898     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11899     break;
11900   }
11901   case llvm::omp::OMPC_device:
11902     C = new (Context) OMPDeviceClause();
11903     break;
11904   case llvm::omp::OMPC_map: {
11905     OMPMappableExprListSizeTy Sizes;
11906     Sizes.NumVars = Record.readInt();
11907     Sizes.NumUniqueDeclarations = Record.readInt();
11908     Sizes.NumComponentLists = Record.readInt();
11909     Sizes.NumComponents = Record.readInt();
11910     C = OMPMapClause::CreateEmpty(Context, Sizes);
11911     break;
11912   }
11913   case llvm::omp::OMPC_num_teams:
11914     C = new (Context) OMPNumTeamsClause();
11915     break;
11916   case llvm::omp::OMPC_thread_limit:
11917     C = new (Context) OMPThreadLimitClause();
11918     break;
11919   case llvm::omp::OMPC_priority:
11920     C = new (Context) OMPPriorityClause();
11921     break;
11922   case llvm::omp::OMPC_grainsize:
11923     C = new (Context) OMPGrainsizeClause();
11924     break;
11925   case llvm::omp::OMPC_num_tasks:
11926     C = new (Context) OMPNumTasksClause();
11927     break;
11928   case llvm::omp::OMPC_hint:
11929     C = new (Context) OMPHintClause();
11930     break;
11931   case llvm::omp::OMPC_dist_schedule:
11932     C = new (Context) OMPDistScheduleClause();
11933     break;
11934   case llvm::omp::OMPC_defaultmap:
11935     C = new (Context) OMPDefaultmapClause();
11936     break;
11937   case llvm::omp::OMPC_to: {
11938     OMPMappableExprListSizeTy Sizes;
11939     Sizes.NumVars = Record.readInt();
11940     Sizes.NumUniqueDeclarations = Record.readInt();
11941     Sizes.NumComponentLists = Record.readInt();
11942     Sizes.NumComponents = Record.readInt();
11943     C = OMPToClause::CreateEmpty(Context, Sizes);
11944     break;
11945   }
11946   case llvm::omp::OMPC_from: {
11947     OMPMappableExprListSizeTy Sizes;
11948     Sizes.NumVars = Record.readInt();
11949     Sizes.NumUniqueDeclarations = Record.readInt();
11950     Sizes.NumComponentLists = Record.readInt();
11951     Sizes.NumComponents = Record.readInt();
11952     C = OMPFromClause::CreateEmpty(Context, Sizes);
11953     break;
11954   }
11955   case llvm::omp::OMPC_use_device_ptr: {
11956     OMPMappableExprListSizeTy Sizes;
11957     Sizes.NumVars = Record.readInt();
11958     Sizes.NumUniqueDeclarations = Record.readInt();
11959     Sizes.NumComponentLists = Record.readInt();
11960     Sizes.NumComponents = Record.readInt();
11961     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11962     break;
11963   }
11964   case llvm::omp::OMPC_use_device_addr: {
11965     OMPMappableExprListSizeTy Sizes;
11966     Sizes.NumVars = Record.readInt();
11967     Sizes.NumUniqueDeclarations = Record.readInt();
11968     Sizes.NumComponentLists = Record.readInt();
11969     Sizes.NumComponents = Record.readInt();
11970     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11971     break;
11972   }
11973   case llvm::omp::OMPC_is_device_ptr: {
11974     OMPMappableExprListSizeTy Sizes;
11975     Sizes.NumVars = Record.readInt();
11976     Sizes.NumUniqueDeclarations = Record.readInt();
11977     Sizes.NumComponentLists = Record.readInt();
11978     Sizes.NumComponents = Record.readInt();
11979     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11980     break;
11981   }
11982   case llvm::omp::OMPC_allocate:
11983     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11984     break;
11985   case llvm::omp::OMPC_nontemporal:
11986     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11987     break;
11988   case llvm::omp::OMPC_inclusive:
11989     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11990     break;
11991   case llvm::omp::OMPC_exclusive:
11992     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11993     break;
11994   case llvm::omp::OMPC_order:
11995     C = new (Context) OMPOrderClause();
11996     break;
11997   case llvm::omp::OMPC_destroy:
11998     C = new (Context) OMPDestroyClause();
11999     break;
12000   case llvm::omp::OMPC_detach:
12001     C = new (Context) OMPDetachClause();
12002     break;
12003   case llvm::omp::OMPC_uses_allocators:
12004     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
12005     break;
12006   case llvm::omp::OMPC_affinity:
12007     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
12008     break;
12009 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
12010   case llvm::omp::Enum:                                                        \
12011     break;
12012 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12013   default:
12014     break;
12015   }
12016   assert(C && "Unknown OMPClause type");
12017 
12018   Visit(C);
12019   C->setLocStart(Record.readSourceLocation());
12020   C->setLocEnd(Record.readSourceLocation());
12021 
12022   return C;
12023 }
12024 
12025 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12026   C->setPreInitStmt(Record.readSubStmt(),
12027                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12028 }
12029 
12030 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12031   VisitOMPClauseWithPreInit(C);
12032   C->setPostUpdateExpr(Record.readSubExpr());
12033 }
12034 
12035 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12036   VisitOMPClauseWithPreInit(C);
12037   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12038   C->setNameModifierLoc(Record.readSourceLocation());
12039   C->setColonLoc(Record.readSourceLocation());
12040   C->setCondition(Record.readSubExpr());
12041   C->setLParenLoc(Record.readSourceLocation());
12042 }
12043 
12044 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12045   VisitOMPClauseWithPreInit(C);
12046   C->setCondition(Record.readSubExpr());
12047   C->setLParenLoc(Record.readSourceLocation());
12048 }
12049 
12050 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12051   VisitOMPClauseWithPreInit(C);
12052   C->setNumThreads(Record.readSubExpr());
12053   C->setLParenLoc(Record.readSourceLocation());
12054 }
12055 
12056 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12057   C->setSafelen(Record.readSubExpr());
12058   C->setLParenLoc(Record.readSourceLocation());
12059 }
12060 
12061 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12062   C->setSimdlen(Record.readSubExpr());
12063   C->setLParenLoc(Record.readSourceLocation());
12064 }
12065 
12066 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12067   C->setAllocator(Record.readExpr());
12068   C->setLParenLoc(Record.readSourceLocation());
12069 }
12070 
12071 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12072   C->setNumForLoops(Record.readSubExpr());
12073   C->setLParenLoc(Record.readSourceLocation());
12074 }
12075 
12076 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12077   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12078   C->setLParenLoc(Record.readSourceLocation());
12079   C->setDefaultKindKwLoc(Record.readSourceLocation());
12080 }
12081 
12082 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12083   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12084   C->setLParenLoc(Record.readSourceLocation());
12085   C->setProcBindKindKwLoc(Record.readSourceLocation());
12086 }
12087 
12088 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12089   VisitOMPClauseWithPreInit(C);
12090   C->setScheduleKind(
12091        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12092   C->setFirstScheduleModifier(
12093       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12094   C->setSecondScheduleModifier(
12095       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12096   C->setChunkSize(Record.readSubExpr());
12097   C->setLParenLoc(Record.readSourceLocation());
12098   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12099   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12100   C->setScheduleKindLoc(Record.readSourceLocation());
12101   C->setCommaLoc(Record.readSourceLocation());
12102 }
12103 
12104 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12105   C->setNumForLoops(Record.readSubExpr());
12106   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12107     C->setLoopNumIterations(I, Record.readSubExpr());
12108   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12109     C->setLoopCounter(I, Record.readSubExpr());
12110   C->setLParenLoc(Record.readSourceLocation());
12111 }
12112 
12113 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12114   C->setEventHandler(Record.readSubExpr());
12115   C->setLParenLoc(Record.readSourceLocation());
12116 }
12117 
12118 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12119 
12120 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12121 
12122 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12123 
12124 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12125 
12126 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12127 
12128 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12129   if (C->isExtended()) {
12130     C->setLParenLoc(Record.readSourceLocation());
12131     C->setArgumentLoc(Record.readSourceLocation());
12132     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12133   }
12134 }
12135 
12136 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12137 
12138 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12139 
12140 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12141 
12142 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12143 
12144 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12145 
12146 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12147 
12148 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12149 
12150 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12151 
12152 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12153 
12154 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12155 
12156 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12157 
12158 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12159     OMPUnifiedSharedMemoryClause *) {}
12160 
12161 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12162 
12163 void
12164 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12165 }
12166 
12167 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12168     OMPAtomicDefaultMemOrderClause *C) {
12169   C->setAtomicDefaultMemOrderKind(
12170       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12171   C->setLParenLoc(Record.readSourceLocation());
12172   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12173 }
12174 
12175 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12176   C->setLParenLoc(Record.readSourceLocation());
12177   unsigned NumVars = C->varlist_size();
12178   SmallVector<Expr *, 16> Vars;
12179   Vars.reserve(NumVars);
12180   for (unsigned i = 0; i != NumVars; ++i)
12181     Vars.push_back(Record.readSubExpr());
12182   C->setVarRefs(Vars);
12183   Vars.clear();
12184   for (unsigned i = 0; i != NumVars; ++i)
12185     Vars.push_back(Record.readSubExpr());
12186   C->setPrivateCopies(Vars);
12187 }
12188 
12189 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12190   VisitOMPClauseWithPreInit(C);
12191   C->setLParenLoc(Record.readSourceLocation());
12192   unsigned NumVars = C->varlist_size();
12193   SmallVector<Expr *, 16> Vars;
12194   Vars.reserve(NumVars);
12195   for (unsigned i = 0; i != NumVars; ++i)
12196     Vars.push_back(Record.readSubExpr());
12197   C->setVarRefs(Vars);
12198   Vars.clear();
12199   for (unsigned i = 0; i != NumVars; ++i)
12200     Vars.push_back(Record.readSubExpr());
12201   C->setPrivateCopies(Vars);
12202   Vars.clear();
12203   for (unsigned i = 0; i != NumVars; ++i)
12204     Vars.push_back(Record.readSubExpr());
12205   C->setInits(Vars);
12206 }
12207 
12208 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12209   VisitOMPClauseWithPostUpdate(C);
12210   C->setLParenLoc(Record.readSourceLocation());
12211   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12212   C->setKindLoc(Record.readSourceLocation());
12213   C->setColonLoc(Record.readSourceLocation());
12214   unsigned NumVars = C->varlist_size();
12215   SmallVector<Expr *, 16> Vars;
12216   Vars.reserve(NumVars);
12217   for (unsigned i = 0; i != NumVars; ++i)
12218     Vars.push_back(Record.readSubExpr());
12219   C->setVarRefs(Vars);
12220   Vars.clear();
12221   for (unsigned i = 0; i != NumVars; ++i)
12222     Vars.push_back(Record.readSubExpr());
12223   C->setPrivateCopies(Vars);
12224   Vars.clear();
12225   for (unsigned i = 0; i != NumVars; ++i)
12226     Vars.push_back(Record.readSubExpr());
12227   C->setSourceExprs(Vars);
12228   Vars.clear();
12229   for (unsigned i = 0; i != NumVars; ++i)
12230     Vars.push_back(Record.readSubExpr());
12231   C->setDestinationExprs(Vars);
12232   Vars.clear();
12233   for (unsigned i = 0; i != NumVars; ++i)
12234     Vars.push_back(Record.readSubExpr());
12235   C->setAssignmentOps(Vars);
12236 }
12237 
12238 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12239   C->setLParenLoc(Record.readSourceLocation());
12240   unsigned NumVars = C->varlist_size();
12241   SmallVector<Expr *, 16> Vars;
12242   Vars.reserve(NumVars);
12243   for (unsigned i = 0; i != NumVars; ++i)
12244     Vars.push_back(Record.readSubExpr());
12245   C->setVarRefs(Vars);
12246 }
12247 
12248 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12249   VisitOMPClauseWithPostUpdate(C);
12250   C->setLParenLoc(Record.readSourceLocation());
12251   C->setModifierLoc(Record.readSourceLocation());
12252   C->setColonLoc(Record.readSourceLocation());
12253   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12254   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12255   C->setQualifierLoc(NNSL);
12256   C->setNameInfo(DNI);
12257 
12258   unsigned NumVars = C->varlist_size();
12259   SmallVector<Expr *, 16> Vars;
12260   Vars.reserve(NumVars);
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setVarRefs(Vars);
12264   Vars.clear();
12265   for (unsigned i = 0; i != NumVars; ++i)
12266     Vars.push_back(Record.readSubExpr());
12267   C->setPrivates(Vars);
12268   Vars.clear();
12269   for (unsigned i = 0; i != NumVars; ++i)
12270     Vars.push_back(Record.readSubExpr());
12271   C->setLHSExprs(Vars);
12272   Vars.clear();
12273   for (unsigned i = 0; i != NumVars; ++i)
12274     Vars.push_back(Record.readSubExpr());
12275   C->setRHSExprs(Vars);
12276   Vars.clear();
12277   for (unsigned i = 0; i != NumVars; ++i)
12278     Vars.push_back(Record.readSubExpr());
12279   C->setReductionOps(Vars);
12280   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12281     Vars.clear();
12282     for (unsigned i = 0; i != NumVars; ++i)
12283       Vars.push_back(Record.readSubExpr());
12284     C->setInscanCopyOps(Vars);
12285     Vars.clear();
12286     for (unsigned i = 0; i != NumVars; ++i)
12287       Vars.push_back(Record.readSubExpr());
12288     C->setInscanCopyArrayTemps(Vars);
12289     Vars.clear();
12290     for (unsigned i = 0; i != NumVars; ++i)
12291       Vars.push_back(Record.readSubExpr());
12292     C->setInscanCopyArrayElems(Vars);
12293   }
12294 }
12295 
12296 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12297   VisitOMPClauseWithPostUpdate(C);
12298   C->setLParenLoc(Record.readSourceLocation());
12299   C->setColonLoc(Record.readSourceLocation());
12300   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12301   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12302   C->setQualifierLoc(NNSL);
12303   C->setNameInfo(DNI);
12304 
12305   unsigned NumVars = C->varlist_size();
12306   SmallVector<Expr *, 16> Vars;
12307   Vars.reserve(NumVars);
12308   for (unsigned I = 0; I != NumVars; ++I)
12309     Vars.push_back(Record.readSubExpr());
12310   C->setVarRefs(Vars);
12311   Vars.clear();
12312   for (unsigned I = 0; I != NumVars; ++I)
12313     Vars.push_back(Record.readSubExpr());
12314   C->setPrivates(Vars);
12315   Vars.clear();
12316   for (unsigned I = 0; I != NumVars; ++I)
12317     Vars.push_back(Record.readSubExpr());
12318   C->setLHSExprs(Vars);
12319   Vars.clear();
12320   for (unsigned I = 0; I != NumVars; ++I)
12321     Vars.push_back(Record.readSubExpr());
12322   C->setRHSExprs(Vars);
12323   Vars.clear();
12324   for (unsigned I = 0; I != NumVars; ++I)
12325     Vars.push_back(Record.readSubExpr());
12326   C->setReductionOps(Vars);
12327 }
12328 
12329 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12330   VisitOMPClauseWithPostUpdate(C);
12331   C->setLParenLoc(Record.readSourceLocation());
12332   C->setColonLoc(Record.readSourceLocation());
12333   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12334   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12335   C->setQualifierLoc(NNSL);
12336   C->setNameInfo(DNI);
12337 
12338   unsigned NumVars = C->varlist_size();
12339   SmallVector<Expr *, 16> Vars;
12340   Vars.reserve(NumVars);
12341   for (unsigned I = 0; I != NumVars; ++I)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setVarRefs(Vars);
12344   Vars.clear();
12345   for (unsigned I = 0; I != NumVars; ++I)
12346     Vars.push_back(Record.readSubExpr());
12347   C->setPrivates(Vars);
12348   Vars.clear();
12349   for (unsigned I = 0; I != NumVars; ++I)
12350     Vars.push_back(Record.readSubExpr());
12351   C->setLHSExprs(Vars);
12352   Vars.clear();
12353   for (unsigned I = 0; I != NumVars; ++I)
12354     Vars.push_back(Record.readSubExpr());
12355   C->setRHSExprs(Vars);
12356   Vars.clear();
12357   for (unsigned I = 0; I != NumVars; ++I)
12358     Vars.push_back(Record.readSubExpr());
12359   C->setReductionOps(Vars);
12360   Vars.clear();
12361   for (unsigned I = 0; I != NumVars; ++I)
12362     Vars.push_back(Record.readSubExpr());
12363   C->setTaskgroupDescriptors(Vars);
12364 }
12365 
12366 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12367   VisitOMPClauseWithPostUpdate(C);
12368   C->setLParenLoc(Record.readSourceLocation());
12369   C->setColonLoc(Record.readSourceLocation());
12370   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12371   C->setModifierLoc(Record.readSourceLocation());
12372   unsigned NumVars = C->varlist_size();
12373   SmallVector<Expr *, 16> Vars;
12374   Vars.reserve(NumVars);
12375   for (unsigned i = 0; i != NumVars; ++i)
12376     Vars.push_back(Record.readSubExpr());
12377   C->setVarRefs(Vars);
12378   Vars.clear();
12379   for (unsigned i = 0; i != NumVars; ++i)
12380     Vars.push_back(Record.readSubExpr());
12381   C->setPrivates(Vars);
12382   Vars.clear();
12383   for (unsigned i = 0; i != NumVars; ++i)
12384     Vars.push_back(Record.readSubExpr());
12385   C->setInits(Vars);
12386   Vars.clear();
12387   for (unsigned i = 0; i != NumVars; ++i)
12388     Vars.push_back(Record.readSubExpr());
12389   C->setUpdates(Vars);
12390   Vars.clear();
12391   for (unsigned i = 0; i != NumVars; ++i)
12392     Vars.push_back(Record.readSubExpr());
12393   C->setFinals(Vars);
12394   C->setStep(Record.readSubExpr());
12395   C->setCalcStep(Record.readSubExpr());
12396   Vars.clear();
12397   for (unsigned I = 0; I != NumVars + 1; ++I)
12398     Vars.push_back(Record.readSubExpr());
12399   C->setUsedExprs(Vars);
12400 }
12401 
12402 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12403   C->setLParenLoc(Record.readSourceLocation());
12404   C->setColonLoc(Record.readSourceLocation());
12405   unsigned NumVars = C->varlist_size();
12406   SmallVector<Expr *, 16> Vars;
12407   Vars.reserve(NumVars);
12408   for (unsigned i = 0; i != NumVars; ++i)
12409     Vars.push_back(Record.readSubExpr());
12410   C->setVarRefs(Vars);
12411   C->setAlignment(Record.readSubExpr());
12412 }
12413 
12414 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12415   C->setLParenLoc(Record.readSourceLocation());
12416   unsigned NumVars = C->varlist_size();
12417   SmallVector<Expr *, 16> Exprs;
12418   Exprs.reserve(NumVars);
12419   for (unsigned i = 0; i != NumVars; ++i)
12420     Exprs.push_back(Record.readSubExpr());
12421   C->setVarRefs(Exprs);
12422   Exprs.clear();
12423   for (unsigned i = 0; i != NumVars; ++i)
12424     Exprs.push_back(Record.readSubExpr());
12425   C->setSourceExprs(Exprs);
12426   Exprs.clear();
12427   for (unsigned i = 0; i != NumVars; ++i)
12428     Exprs.push_back(Record.readSubExpr());
12429   C->setDestinationExprs(Exprs);
12430   Exprs.clear();
12431   for (unsigned i = 0; i != NumVars; ++i)
12432     Exprs.push_back(Record.readSubExpr());
12433   C->setAssignmentOps(Exprs);
12434 }
12435 
12436 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12437   C->setLParenLoc(Record.readSourceLocation());
12438   unsigned NumVars = C->varlist_size();
12439   SmallVector<Expr *, 16> Exprs;
12440   Exprs.reserve(NumVars);
12441   for (unsigned i = 0; i != NumVars; ++i)
12442     Exprs.push_back(Record.readSubExpr());
12443   C->setVarRefs(Exprs);
12444   Exprs.clear();
12445   for (unsigned i = 0; i != NumVars; ++i)
12446     Exprs.push_back(Record.readSubExpr());
12447   C->setSourceExprs(Exprs);
12448   Exprs.clear();
12449   for (unsigned i = 0; i != NumVars; ++i)
12450     Exprs.push_back(Record.readSubExpr());
12451   C->setDestinationExprs(Exprs);
12452   Exprs.clear();
12453   for (unsigned i = 0; i != NumVars; ++i)
12454     Exprs.push_back(Record.readSubExpr());
12455   C->setAssignmentOps(Exprs);
12456 }
12457 
12458 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12459   C->setLParenLoc(Record.readSourceLocation());
12460   unsigned NumVars = C->varlist_size();
12461   SmallVector<Expr *, 16> Vars;
12462   Vars.reserve(NumVars);
12463   for (unsigned i = 0; i != NumVars; ++i)
12464     Vars.push_back(Record.readSubExpr());
12465   C->setVarRefs(Vars);
12466 }
12467 
12468 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12469   C->setDepobj(Record.readSubExpr());
12470   C->setLParenLoc(Record.readSourceLocation());
12471 }
12472 
12473 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12474   C->setLParenLoc(Record.readSourceLocation());
12475   C->setModifier(Record.readSubExpr());
12476   C->setDependencyKind(
12477       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12478   C->setDependencyLoc(Record.readSourceLocation());
12479   C->setColonLoc(Record.readSourceLocation());
12480   unsigned NumVars = C->varlist_size();
12481   SmallVector<Expr *, 16> Vars;
12482   Vars.reserve(NumVars);
12483   for (unsigned I = 0; I != NumVars; ++I)
12484     Vars.push_back(Record.readSubExpr());
12485   C->setVarRefs(Vars);
12486   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12487     C->setLoopData(I, Record.readSubExpr());
12488 }
12489 
12490 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12491   VisitOMPClauseWithPreInit(C);
12492   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12493   C->setDevice(Record.readSubExpr());
12494   C->setModifierLoc(Record.readSourceLocation());
12495   C->setLParenLoc(Record.readSourceLocation());
12496 }
12497 
12498 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12499   C->setLParenLoc(Record.readSourceLocation());
12500   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12501     C->setMapTypeModifier(
12502         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12503     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12504   }
12505   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12506   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12507   C->setMapType(
12508      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12509   C->setMapLoc(Record.readSourceLocation());
12510   C->setColonLoc(Record.readSourceLocation());
12511   auto NumVars = C->varlist_size();
12512   auto UniqueDecls = C->getUniqueDeclarationsNum();
12513   auto TotalLists = C->getTotalComponentListNum();
12514   auto TotalComponents = C->getTotalComponentsNum();
12515 
12516   SmallVector<Expr *, 16> Vars;
12517   Vars.reserve(NumVars);
12518   for (unsigned i = 0; i != NumVars; ++i)
12519     Vars.push_back(Record.readExpr());
12520   C->setVarRefs(Vars);
12521 
12522   SmallVector<Expr *, 16> UDMappers;
12523   UDMappers.reserve(NumVars);
12524   for (unsigned I = 0; I < NumVars; ++I)
12525     UDMappers.push_back(Record.readExpr());
12526   C->setUDMapperRefs(UDMappers);
12527 
12528   SmallVector<ValueDecl *, 16> Decls;
12529   Decls.reserve(UniqueDecls);
12530   for (unsigned i = 0; i < UniqueDecls; ++i)
12531     Decls.push_back(Record.readDeclAs<ValueDecl>());
12532   C->setUniqueDecls(Decls);
12533 
12534   SmallVector<unsigned, 16> ListsPerDecl;
12535   ListsPerDecl.reserve(UniqueDecls);
12536   for (unsigned i = 0; i < UniqueDecls; ++i)
12537     ListsPerDecl.push_back(Record.readInt());
12538   C->setDeclNumLists(ListsPerDecl);
12539 
12540   SmallVector<unsigned, 32> ListSizes;
12541   ListSizes.reserve(TotalLists);
12542   for (unsigned i = 0; i < TotalLists; ++i)
12543     ListSizes.push_back(Record.readInt());
12544   C->setComponentListSizes(ListSizes);
12545 
12546   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12547   Components.reserve(TotalComponents);
12548   for (unsigned i = 0; i < TotalComponents; ++i) {
12549     Expr *AssociatedExpr = Record.readExpr();
12550     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12551     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12552         AssociatedExpr, AssociatedDecl));
12553   }
12554   C->setComponents(Components, ListSizes);
12555 }
12556 
12557 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12558   C->setLParenLoc(Record.readSourceLocation());
12559   C->setColonLoc(Record.readSourceLocation());
12560   C->setAllocator(Record.readSubExpr());
12561   unsigned NumVars = C->varlist_size();
12562   SmallVector<Expr *, 16> Vars;
12563   Vars.reserve(NumVars);
12564   for (unsigned i = 0; i != NumVars; ++i)
12565     Vars.push_back(Record.readSubExpr());
12566   C->setVarRefs(Vars);
12567 }
12568 
12569 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12570   VisitOMPClauseWithPreInit(C);
12571   C->setNumTeams(Record.readSubExpr());
12572   C->setLParenLoc(Record.readSourceLocation());
12573 }
12574 
12575 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12576   VisitOMPClauseWithPreInit(C);
12577   C->setThreadLimit(Record.readSubExpr());
12578   C->setLParenLoc(Record.readSourceLocation());
12579 }
12580 
12581 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12582   VisitOMPClauseWithPreInit(C);
12583   C->setPriority(Record.readSubExpr());
12584   C->setLParenLoc(Record.readSourceLocation());
12585 }
12586 
12587 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12588   VisitOMPClauseWithPreInit(C);
12589   C->setGrainsize(Record.readSubExpr());
12590   C->setLParenLoc(Record.readSourceLocation());
12591 }
12592 
12593 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12594   VisitOMPClauseWithPreInit(C);
12595   C->setNumTasks(Record.readSubExpr());
12596   C->setLParenLoc(Record.readSourceLocation());
12597 }
12598 
12599 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12600   C->setHint(Record.readSubExpr());
12601   C->setLParenLoc(Record.readSourceLocation());
12602 }
12603 
12604 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12605   VisitOMPClauseWithPreInit(C);
12606   C->setDistScheduleKind(
12607       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12608   C->setChunkSize(Record.readSubExpr());
12609   C->setLParenLoc(Record.readSourceLocation());
12610   C->setDistScheduleKindLoc(Record.readSourceLocation());
12611   C->setCommaLoc(Record.readSourceLocation());
12612 }
12613 
12614 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12615   C->setDefaultmapKind(
12616        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12617   C->setDefaultmapModifier(
12618       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12619   C->setLParenLoc(Record.readSourceLocation());
12620   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12621   C->setDefaultmapKindLoc(Record.readSourceLocation());
12622 }
12623 
12624 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12625   C->setLParenLoc(Record.readSourceLocation());
12626   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12627     C->setMotionModifier(
12628         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12629     C->setMotionModifierLoc(I, Record.readSourceLocation());
12630   }
12631   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12632   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12633   C->setColonLoc(Record.readSourceLocation());
12634   auto NumVars = C->varlist_size();
12635   auto UniqueDecls = C->getUniqueDeclarationsNum();
12636   auto TotalLists = C->getTotalComponentListNum();
12637   auto TotalComponents = C->getTotalComponentsNum();
12638 
12639   SmallVector<Expr *, 16> Vars;
12640   Vars.reserve(NumVars);
12641   for (unsigned i = 0; i != NumVars; ++i)
12642     Vars.push_back(Record.readSubExpr());
12643   C->setVarRefs(Vars);
12644 
12645   SmallVector<Expr *, 16> UDMappers;
12646   UDMappers.reserve(NumVars);
12647   for (unsigned I = 0; I < NumVars; ++I)
12648     UDMappers.push_back(Record.readSubExpr());
12649   C->setUDMapperRefs(UDMappers);
12650 
12651   SmallVector<ValueDecl *, 16> Decls;
12652   Decls.reserve(UniqueDecls);
12653   for (unsigned i = 0; i < UniqueDecls; ++i)
12654     Decls.push_back(Record.readDeclAs<ValueDecl>());
12655   C->setUniqueDecls(Decls);
12656 
12657   SmallVector<unsigned, 16> ListsPerDecl;
12658   ListsPerDecl.reserve(UniqueDecls);
12659   for (unsigned i = 0; i < UniqueDecls; ++i)
12660     ListsPerDecl.push_back(Record.readInt());
12661   C->setDeclNumLists(ListsPerDecl);
12662 
12663   SmallVector<unsigned, 32> ListSizes;
12664   ListSizes.reserve(TotalLists);
12665   for (unsigned i = 0; i < TotalLists; ++i)
12666     ListSizes.push_back(Record.readInt());
12667   C->setComponentListSizes(ListSizes);
12668 
12669   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12670   Components.reserve(TotalComponents);
12671   for (unsigned i = 0; i < TotalComponents; ++i) {
12672     Expr *AssociatedExpr = Record.readSubExpr();
12673     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12674     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12675         AssociatedExpr, AssociatedDecl));
12676   }
12677   C->setComponents(Components, ListSizes);
12678 }
12679 
12680 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12681   C->setLParenLoc(Record.readSourceLocation());
12682   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12683     C->setMotionModifier(
12684         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12685     C->setMotionModifierLoc(I, Record.readSourceLocation());
12686   }
12687   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12688   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12689   C->setColonLoc(Record.readSourceLocation());
12690   auto NumVars = C->varlist_size();
12691   auto UniqueDecls = C->getUniqueDeclarationsNum();
12692   auto TotalLists = C->getTotalComponentListNum();
12693   auto TotalComponents = C->getTotalComponentsNum();
12694 
12695   SmallVector<Expr *, 16> Vars;
12696   Vars.reserve(NumVars);
12697   for (unsigned i = 0; i != NumVars; ++i)
12698     Vars.push_back(Record.readSubExpr());
12699   C->setVarRefs(Vars);
12700 
12701   SmallVector<Expr *, 16> UDMappers;
12702   UDMappers.reserve(NumVars);
12703   for (unsigned I = 0; I < NumVars; ++I)
12704     UDMappers.push_back(Record.readSubExpr());
12705   C->setUDMapperRefs(UDMappers);
12706 
12707   SmallVector<ValueDecl *, 16> Decls;
12708   Decls.reserve(UniqueDecls);
12709   for (unsigned i = 0; i < UniqueDecls; ++i)
12710     Decls.push_back(Record.readDeclAs<ValueDecl>());
12711   C->setUniqueDecls(Decls);
12712 
12713   SmallVector<unsigned, 16> ListsPerDecl;
12714   ListsPerDecl.reserve(UniqueDecls);
12715   for (unsigned i = 0; i < UniqueDecls; ++i)
12716     ListsPerDecl.push_back(Record.readInt());
12717   C->setDeclNumLists(ListsPerDecl);
12718 
12719   SmallVector<unsigned, 32> ListSizes;
12720   ListSizes.reserve(TotalLists);
12721   for (unsigned i = 0; i < TotalLists; ++i)
12722     ListSizes.push_back(Record.readInt());
12723   C->setComponentListSizes(ListSizes);
12724 
12725   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12726   Components.reserve(TotalComponents);
12727   for (unsigned i = 0; i < TotalComponents; ++i) {
12728     Expr *AssociatedExpr = Record.readSubExpr();
12729     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12730     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12731         AssociatedExpr, AssociatedDecl));
12732   }
12733   C->setComponents(Components, ListSizes);
12734 }
12735 
12736 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12737   C->setLParenLoc(Record.readSourceLocation());
12738   auto NumVars = C->varlist_size();
12739   auto UniqueDecls = C->getUniqueDeclarationsNum();
12740   auto TotalLists = C->getTotalComponentListNum();
12741   auto TotalComponents = C->getTotalComponentsNum();
12742 
12743   SmallVector<Expr *, 16> Vars;
12744   Vars.reserve(NumVars);
12745   for (unsigned i = 0; i != NumVars; ++i)
12746     Vars.push_back(Record.readSubExpr());
12747   C->setVarRefs(Vars);
12748   Vars.clear();
12749   for (unsigned i = 0; i != NumVars; ++i)
12750     Vars.push_back(Record.readSubExpr());
12751   C->setPrivateCopies(Vars);
12752   Vars.clear();
12753   for (unsigned i = 0; i != NumVars; ++i)
12754     Vars.push_back(Record.readSubExpr());
12755   C->setInits(Vars);
12756 
12757   SmallVector<ValueDecl *, 16> Decls;
12758   Decls.reserve(UniqueDecls);
12759   for (unsigned i = 0; i < UniqueDecls; ++i)
12760     Decls.push_back(Record.readDeclAs<ValueDecl>());
12761   C->setUniqueDecls(Decls);
12762 
12763   SmallVector<unsigned, 16> ListsPerDecl;
12764   ListsPerDecl.reserve(UniqueDecls);
12765   for (unsigned i = 0; i < UniqueDecls; ++i)
12766     ListsPerDecl.push_back(Record.readInt());
12767   C->setDeclNumLists(ListsPerDecl);
12768 
12769   SmallVector<unsigned, 32> ListSizes;
12770   ListSizes.reserve(TotalLists);
12771   for (unsigned i = 0; i < TotalLists; ++i)
12772     ListSizes.push_back(Record.readInt());
12773   C->setComponentListSizes(ListSizes);
12774 
12775   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12776   Components.reserve(TotalComponents);
12777   for (unsigned i = 0; i < TotalComponents; ++i) {
12778     Expr *AssociatedExpr = Record.readSubExpr();
12779     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12780     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12781         AssociatedExpr, AssociatedDecl));
12782   }
12783   C->setComponents(Components, ListSizes);
12784 }
12785 
12786 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12787   C->setLParenLoc(Record.readSourceLocation());
12788   auto NumVars = C->varlist_size();
12789   auto UniqueDecls = C->getUniqueDeclarationsNum();
12790   auto TotalLists = C->getTotalComponentListNum();
12791   auto TotalComponents = C->getTotalComponentsNum();
12792 
12793   SmallVector<Expr *, 16> Vars;
12794   Vars.reserve(NumVars);
12795   for (unsigned i = 0; i != NumVars; ++i)
12796     Vars.push_back(Record.readSubExpr());
12797   C->setVarRefs(Vars);
12798 
12799   SmallVector<ValueDecl *, 16> Decls;
12800   Decls.reserve(UniqueDecls);
12801   for (unsigned i = 0; i < UniqueDecls; ++i)
12802     Decls.push_back(Record.readDeclAs<ValueDecl>());
12803   C->setUniqueDecls(Decls);
12804 
12805   SmallVector<unsigned, 16> ListsPerDecl;
12806   ListsPerDecl.reserve(UniqueDecls);
12807   for (unsigned i = 0; i < UniqueDecls; ++i)
12808     ListsPerDecl.push_back(Record.readInt());
12809   C->setDeclNumLists(ListsPerDecl);
12810 
12811   SmallVector<unsigned, 32> ListSizes;
12812   ListSizes.reserve(TotalLists);
12813   for (unsigned i = 0; i < TotalLists; ++i)
12814     ListSizes.push_back(Record.readInt());
12815   C->setComponentListSizes(ListSizes);
12816 
12817   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12818   Components.reserve(TotalComponents);
12819   for (unsigned i = 0; i < TotalComponents; ++i) {
12820     Expr *AssociatedExpr = Record.readSubExpr();
12821     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12822     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12823         AssociatedExpr, AssociatedDecl));
12824   }
12825   C->setComponents(Components, ListSizes);
12826 }
12827 
12828 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12829   C->setLParenLoc(Record.readSourceLocation());
12830   auto NumVars = C->varlist_size();
12831   auto UniqueDecls = C->getUniqueDeclarationsNum();
12832   auto TotalLists = C->getTotalComponentListNum();
12833   auto TotalComponents = C->getTotalComponentsNum();
12834 
12835   SmallVector<Expr *, 16> Vars;
12836   Vars.reserve(NumVars);
12837   for (unsigned i = 0; i != NumVars; ++i)
12838     Vars.push_back(Record.readSubExpr());
12839   C->setVarRefs(Vars);
12840   Vars.clear();
12841 
12842   SmallVector<ValueDecl *, 16> Decls;
12843   Decls.reserve(UniqueDecls);
12844   for (unsigned i = 0; i < UniqueDecls; ++i)
12845     Decls.push_back(Record.readDeclAs<ValueDecl>());
12846   C->setUniqueDecls(Decls);
12847 
12848   SmallVector<unsigned, 16> ListsPerDecl;
12849   ListsPerDecl.reserve(UniqueDecls);
12850   for (unsigned i = 0; i < UniqueDecls; ++i)
12851     ListsPerDecl.push_back(Record.readInt());
12852   C->setDeclNumLists(ListsPerDecl);
12853 
12854   SmallVector<unsigned, 32> ListSizes;
12855   ListSizes.reserve(TotalLists);
12856   for (unsigned i = 0; i < TotalLists; ++i)
12857     ListSizes.push_back(Record.readInt());
12858   C->setComponentListSizes(ListSizes);
12859 
12860   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12861   Components.reserve(TotalComponents);
12862   for (unsigned i = 0; i < TotalComponents; ++i) {
12863     Expr *AssociatedExpr = Record.readSubExpr();
12864     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12865     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12866         AssociatedExpr, AssociatedDecl));
12867   }
12868   C->setComponents(Components, ListSizes);
12869 }
12870 
12871 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12872   C->setLParenLoc(Record.readSourceLocation());
12873   unsigned NumVars = C->varlist_size();
12874   SmallVector<Expr *, 16> Vars;
12875   Vars.reserve(NumVars);
12876   for (unsigned i = 0; i != NumVars; ++i)
12877     Vars.push_back(Record.readSubExpr());
12878   C->setVarRefs(Vars);
12879   Vars.clear();
12880   Vars.reserve(NumVars);
12881   for (unsigned i = 0; i != NumVars; ++i)
12882     Vars.push_back(Record.readSubExpr());
12883   C->setPrivateRefs(Vars);
12884 }
12885 
12886 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12887   C->setLParenLoc(Record.readSourceLocation());
12888   unsigned NumVars = C->varlist_size();
12889   SmallVector<Expr *, 16> Vars;
12890   Vars.reserve(NumVars);
12891   for (unsigned i = 0; i != NumVars; ++i)
12892     Vars.push_back(Record.readSubExpr());
12893   C->setVarRefs(Vars);
12894 }
12895 
12896 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12897   C->setLParenLoc(Record.readSourceLocation());
12898   unsigned NumVars = C->varlist_size();
12899   SmallVector<Expr *, 16> Vars;
12900   Vars.reserve(NumVars);
12901   for (unsigned i = 0; i != NumVars; ++i)
12902     Vars.push_back(Record.readSubExpr());
12903   C->setVarRefs(Vars);
12904 }
12905 
12906 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12907   C->setLParenLoc(Record.readSourceLocation());
12908   unsigned NumOfAllocators = C->getNumberOfAllocators();
12909   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12910   Data.reserve(NumOfAllocators);
12911   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12912     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12913     D.Allocator = Record.readSubExpr();
12914     D.AllocatorTraits = Record.readSubExpr();
12915     D.LParenLoc = Record.readSourceLocation();
12916     D.RParenLoc = Record.readSourceLocation();
12917   }
12918   C->setAllocatorsData(Data);
12919 }
12920 
12921 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12922   C->setLParenLoc(Record.readSourceLocation());
12923   C->setModifier(Record.readSubExpr());
12924   C->setColonLoc(Record.readSourceLocation());
12925   unsigned NumOfLocators = C->varlist_size();
12926   SmallVector<Expr *, 4> Locators;
12927   Locators.reserve(NumOfLocators);
12928   for (unsigned I = 0; I != NumOfLocators; ++I)
12929     Locators.push_back(Record.readSubExpr());
12930   C->setVarRefs(Locators);
12931 }
12932 
12933 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12934   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12935   C->setLParenLoc(Record.readSourceLocation());
12936   C->setKindKwLoc(Record.readSourceLocation());
12937 }
12938 
12939 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12940   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12941   TI.Sets.resize(readUInt32());
12942   for (auto &Set : TI.Sets) {
12943     Set.Kind = readEnum<llvm::omp::TraitSet>();
12944     Set.Selectors.resize(readUInt32());
12945     for (auto &Selector : Set.Selectors) {
12946       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12947       Selector.ScoreOrCondition = nullptr;
12948       if (readBool())
12949         Selector.ScoreOrCondition = readExprRef();
12950       Selector.Properties.resize(readUInt32());
12951       for (auto &Property : Selector.Properties)
12952         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12953     }
12954   }
12955   return &TI;
12956 }
12957 
12958 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12959   if (!Data)
12960     return;
12961   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12962     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12963     skipInts(3);
12964   }
12965   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12966   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12967     Clauses[I] = readOMPClause();
12968   Data->setClauses(Clauses);
12969   if (Data->hasAssociatedStmt())
12970     Data->setAssociatedStmt(readStmt());
12971   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12972     Data->getChildren()[I] = readStmt();
12973 }
12974