1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/LEB128.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 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
559   assert(M && "missing module");
560   return M;
561 }
562 
563 bool PCHValidator::ReadDiagnosticOptions(
564     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
565   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
566   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
567   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
568       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
569   // This should never fail, because we would have processed these options
570   // before writing them to an ASTFile.
571   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
572 
573   ModuleManager &ModuleMgr = Reader.getModuleManager();
574   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
575 
576   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
577   if (!TopM)
578     return false;
579 
580   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
581   // contains the union of their flags.
582   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
583                                  Complain);
584 }
585 
586 /// Collect the macro definitions provided by the given preprocessor
587 /// options.
588 static void
589 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
590                         MacroDefinitionsMap &Macros,
591                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
592   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
593     StringRef Macro = PPOpts.Macros[I].first;
594     bool IsUndef = PPOpts.Macros[I].second;
595 
596     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
597     StringRef MacroName = MacroPair.first;
598     StringRef MacroBody = MacroPair.second;
599 
600     // For an #undef'd macro, we only care about the name.
601     if (IsUndef) {
602       if (MacroNames && !Macros.count(MacroName))
603         MacroNames->push_back(MacroName);
604 
605       Macros[MacroName] = std::make_pair("", true);
606       continue;
607     }
608 
609     // For a #define'd macro, figure out the actual definition.
610     if (MacroName.size() == Macro.size())
611       MacroBody = "1";
612     else {
613       // Note: GCC drops anything following an end-of-line character.
614       StringRef::size_type End = MacroBody.find_first_of("\n\r");
615       MacroBody = MacroBody.substr(0, End);
616     }
617 
618     if (MacroNames && !Macros.count(MacroName))
619       MacroNames->push_back(MacroName);
620     Macros[MacroName] = std::make_pair(MacroBody, false);
621   }
622 }
623 
624 /// Check the preprocessor options deserialized from the control block
625 /// against the preprocessor options in an existing preprocessor.
626 ///
627 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
628 /// \param Validate If true, validate preprocessor options. If false, allow
629 ///        macros defined by \p ExistingPPOpts to override those defined by
630 ///        \p PPOpts in SuggestedPredefines.
631 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
632                                      const PreprocessorOptions &ExistingPPOpts,
633                                      DiagnosticsEngine *Diags,
634                                      FileManager &FileMgr,
635                                      std::string &SuggestedPredefines,
636                                      const LangOptions &LangOpts,
637                                      bool Validate = true) {
638   // Check macro definitions.
639   MacroDefinitionsMap ASTFileMacros;
640   collectMacroDefinitions(PPOpts, ASTFileMacros);
641   MacroDefinitionsMap ExistingMacros;
642   SmallVector<StringRef, 4> ExistingMacroNames;
643   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
644 
645   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
646     // Dig out the macro definition in the existing preprocessor options.
647     StringRef MacroName = ExistingMacroNames[I];
648     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
649 
650     // Check whether we know anything about this macro name or not.
651     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
652         ASTFileMacros.find(MacroName);
653     if (!Validate || Known == ASTFileMacros.end()) {
654       // FIXME: Check whether this identifier was referenced anywhere in the
655       // AST file. If so, we should reject the AST file. Unfortunately, this
656       // information isn't in the control block. What shall we do about it?
657 
658       if (Existing.second) {
659         SuggestedPredefines += "#undef ";
660         SuggestedPredefines += MacroName.str();
661         SuggestedPredefines += '\n';
662       } else {
663         SuggestedPredefines += "#define ";
664         SuggestedPredefines += MacroName.str();
665         SuggestedPredefines += ' ';
666         SuggestedPredefines += Existing.first.str();
667         SuggestedPredefines += '\n';
668       }
669       continue;
670     }
671 
672     // If the macro was defined in one but undef'd in the other, we have a
673     // conflict.
674     if (Existing.second != Known->second.second) {
675       if (Diags) {
676         Diags->Report(diag::err_pch_macro_def_undef)
677           << MacroName << Known->second.second;
678       }
679       return true;
680     }
681 
682     // If the macro was #undef'd in both, or if the macro bodies are identical,
683     // it's fine.
684     if (Existing.second || Existing.first == Known->second.first)
685       continue;
686 
687     // The macro bodies differ; complain.
688     if (Diags) {
689       Diags->Report(diag::err_pch_macro_def_conflict)
690         << MacroName << Known->second.first << Existing.first;
691     }
692     return true;
693   }
694 
695   // Check whether we're using predefines.
696   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
697     if (Diags) {
698       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
699     }
700     return true;
701   }
702 
703   // Detailed record is important since it is used for the module cache hash.
704   if (LangOpts.Modules &&
705       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
706     if (Diags) {
707       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
708     }
709     return true;
710   }
711 
712   // Compute the #include and #include_macros lines we need.
713   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
714     StringRef File = ExistingPPOpts.Includes[I];
715 
716     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
717         !ExistingPPOpts.PCHThroughHeader.empty()) {
718       // In case the through header is an include, we must add all the includes
719       // to the predefines so the start point can be determined.
720       SuggestedPredefines += "#include \"";
721       SuggestedPredefines += File;
722       SuggestedPredefines += "\"\n";
723       continue;
724     }
725 
726     if (File == ExistingPPOpts.ImplicitPCHInclude)
727       continue;
728 
729     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
730           != PPOpts.Includes.end())
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
741                   File)
742         != PPOpts.MacroIncludes.end())
743       continue;
744 
745     SuggestedPredefines += "#__include_macros \"";
746     SuggestedPredefines += File;
747     SuggestedPredefines += "\"\n##\n";
748   }
749 
750   return false;
751 }
752 
753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
754                                            bool Complain,
755                                            std::string &SuggestedPredefines) {
756   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
757 
758   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
759                                   Complain? &Reader.Diags : nullptr,
760                                   PP.getFileManager(),
761                                   SuggestedPredefines,
762                                   PP.getLangOpts());
763 }
764 
765 bool SimpleASTReaderListener::ReadPreprocessorOptions(
766                                   const PreprocessorOptions &PPOpts,
767                                   bool Complain,
768                                   std::string &SuggestedPredefines) {
769   return checkPreprocessorOptions(PPOpts,
770                                   PP.getPreprocessorOpts(),
771                                   nullptr,
772                                   PP.getFileManager(),
773                                   SuggestedPredefines,
774                                   PP.getLangOpts(),
775                                   false);
776 }
777 
778 /// Check the header search options deserialized from the control block
779 /// against the header search options in an existing preprocessor.
780 ///
781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
783                                      StringRef SpecificModuleCachePath,
784                                      StringRef ExistingModuleCachePath,
785                                      DiagnosticsEngine *Diags,
786                                      const LangOptions &LangOpts,
787                                      const PreprocessorOptions &PPOpts) {
788   if (LangOpts.Modules) {
789     if (SpecificModuleCachePath != ExistingModuleCachePath &&
790         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
791       if (Diags)
792         Diags->Report(diag::err_pch_modulecache_mismatch)
793           << SpecificModuleCachePath << ExistingModuleCachePath;
794       return true;
795     }
796   }
797 
798   return false;
799 }
800 
801 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
802                                            StringRef SpecificModuleCachePath,
803                                            bool Complain) {
804   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
805                                   PP.getHeaderSearchInfo().getModuleCachePath(),
806                                   Complain ? &Reader.Diags : nullptr,
807                                   PP.getLangOpts(), PP.getPreprocessorOpts());
808 }
809 
810 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
811   PP.setCounterValue(Value);
812 }
813 
814 //===----------------------------------------------------------------------===//
815 // AST reader implementation
816 //===----------------------------------------------------------------------===//
817 
818 static uint64_t readULEB(const unsigned char *&P) {
819   unsigned Length = 0;
820   const char *Error = nullptr;
821 
822   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
823   if (Error)
824     llvm::report_fatal_error(Error);
825   P += Length;
826   return Val;
827 }
828 
829 /// Read ULEB-encoded key length and data length.
830 static std::pair<unsigned, unsigned>
831 readULEBKeyDataLength(const unsigned char *&P) {
832   unsigned KeyLen = readULEB(P);
833   if ((unsigned)KeyLen != KeyLen)
834     llvm::report_fatal_error("key too large");
835 
836   unsigned DataLen = readULEB(P);
837   if ((unsigned)DataLen != DataLen)
838     llvm::report_fatal_error("data too large");
839 
840   return std::make_pair(KeyLen, DataLen);
841 }
842 
843 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
844                                            bool TakeOwnership) {
845   DeserializationListener = Listener;
846   OwnsDeserializationListener = TakeOwnership;
847 }
848 
849 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
850   return serialization::ComputeHash(Sel);
851 }
852 
853 std::pair<unsigned, unsigned>
854 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
855   return readULEBKeyDataLength(d);
856 }
857 
858 ASTSelectorLookupTrait::internal_key_type
859 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
860   using namespace llvm::support;
861 
862   SelectorTable &SelTable = Reader.getContext().Selectors;
863   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
864   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
865       F, endian::readNext<uint32_t, little, unaligned>(d));
866   if (N == 0)
867     return SelTable.getNullarySelector(FirstII);
868   else if (N == 1)
869     return SelTable.getUnarySelector(FirstII);
870 
871   SmallVector<IdentifierInfo *, 16> Args;
872   Args.push_back(FirstII);
873   for (unsigned I = 1; I != N; ++I)
874     Args.push_back(Reader.getLocalIdentifier(
875         F, endian::readNext<uint32_t, little, unaligned>(d)));
876 
877   return SelTable.getSelector(N, Args.data());
878 }
879 
880 ASTSelectorLookupTrait::data_type
881 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
882                                  unsigned DataLen) {
883   using namespace llvm::support;
884 
885   data_type Result;
886 
887   Result.ID = Reader.getGlobalSelectorID(
888       F, endian::readNext<uint32_t, little, unaligned>(d));
889   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
890   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
891   Result.InstanceBits = FullInstanceBits & 0x3;
892   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
893   Result.FactoryBits = FullFactoryBits & 0x3;
894   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
895   unsigned NumInstanceMethods = FullInstanceBits >> 3;
896   unsigned NumFactoryMethods = FullFactoryBits >> 3;
897 
898   // Load instance methods
899   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
900     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
901             F, endian::readNext<uint32_t, little, unaligned>(d)))
902       Result.Instance.push_back(Method);
903   }
904 
905   // Load factory methods
906   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
907     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
908             F, endian::readNext<uint32_t, little, unaligned>(d)))
909       Result.Factory.push_back(Method);
910   }
911 
912   return Result;
913 }
914 
915 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
916   return llvm::djbHash(a);
917 }
918 
919 std::pair<unsigned, unsigned>
920 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
921   return readULEBKeyDataLength(d);
922 }
923 
924 ASTIdentifierLookupTraitBase::internal_key_type
925 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
926   assert(n >= 2 && d[n-1] == '\0');
927   return StringRef((const char*) d, n-1);
928 }
929 
930 /// Whether the given identifier is "interesting".
931 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
932                                     bool IsModule) {
933   return II.hadMacroDefinition() || II.isPoisoned() ||
934          (!IsModule && II.getObjCOrBuiltinID()) ||
935          II.hasRevertedTokenIDToIdentifier() ||
936          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
937           II.getFETokenInfo());
938 }
939 
940 static bool readBit(unsigned &Bits) {
941   bool Value = Bits & 0x1;
942   Bits >>= 1;
943   return Value;
944 }
945 
946 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
947   using namespace llvm::support;
948 
949   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
950   return Reader.getGlobalIdentifierID(F, RawID >> 1);
951 }
952 
953 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
954   if (!II.isFromAST()) {
955     II.setIsFromAST();
956     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
957     if (isInterestingIdentifier(Reader, II, IsModule))
958       II.setChangedSinceDeserialization();
959   }
960 }
961 
962 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
963                                                    const unsigned char* d,
964                                                    unsigned DataLen) {
965   using namespace llvm::support;
966 
967   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
968   bool IsInteresting = RawID & 0x01;
969 
970   // Wipe out the "is interesting" bit.
971   RawID = RawID >> 1;
972 
973   // Build the IdentifierInfo and link the identifier ID with it.
974   IdentifierInfo *II = KnownII;
975   if (!II) {
976     II = &Reader.getIdentifierTable().getOwn(k);
977     KnownII = II;
978   }
979   markIdentifierFromAST(Reader, *II);
980   Reader.markIdentifierUpToDate(II);
981 
982   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
983   if (!IsInteresting) {
984     // For uninteresting identifiers, there's nothing else to do. Just notify
985     // the reader that we've finished loading this identifier.
986     Reader.SetIdentifierInfo(ID, II);
987     return II;
988   }
989 
990   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
991   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
992   bool CPlusPlusOperatorKeyword = readBit(Bits);
993   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
994   bool Poisoned = readBit(Bits);
995   bool ExtensionToken = readBit(Bits);
996   bool HadMacroDefinition = readBit(Bits);
997 
998   assert(Bits == 0 && "Extra bits in the identifier?");
999   DataLen -= 8;
1000 
1001   // Set or check the various bits in the IdentifierInfo structure.
1002   // Token IDs are read-only.
1003   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1004     II->revertTokenIDToIdentifier();
1005   if (!F.isModule())
1006     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1007   assert(II->isExtensionToken() == ExtensionToken &&
1008          "Incorrect extension token flag");
1009   (void)ExtensionToken;
1010   if (Poisoned)
1011     II->setIsPoisoned(true);
1012   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1013          "Incorrect C++ operator keyword flag");
1014   (void)CPlusPlusOperatorKeyword;
1015 
1016   // If this identifier is a macro, deserialize the macro
1017   // definition.
1018   if (HadMacroDefinition) {
1019     uint32_t MacroDirectivesOffset =
1020         endian::readNext<uint32_t, little, unaligned>(d);
1021     DataLen -= 4;
1022 
1023     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1024   }
1025 
1026   Reader.SetIdentifierInfo(ID, II);
1027 
1028   // Read all of the declarations visible at global scope with this
1029   // name.
1030   if (DataLen > 0) {
1031     SmallVector<uint32_t, 4> DeclIDs;
1032     for (; DataLen > 0; DataLen -= 4)
1033       DeclIDs.push_back(Reader.getGlobalDeclID(
1034           F, endian::readNext<uint32_t, little, unaligned>(d)));
1035     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1036   }
1037 
1038   return II;
1039 }
1040 
1041 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1042     : Kind(Name.getNameKind()) {
1043   switch (Kind) {
1044   case DeclarationName::Identifier:
1045     Data = (uint64_t)Name.getAsIdentifierInfo();
1046     break;
1047   case DeclarationName::ObjCZeroArgSelector:
1048   case DeclarationName::ObjCOneArgSelector:
1049   case DeclarationName::ObjCMultiArgSelector:
1050     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1051     break;
1052   case DeclarationName::CXXOperatorName:
1053     Data = Name.getCXXOverloadedOperator();
1054     break;
1055   case DeclarationName::CXXLiteralOperatorName:
1056     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1057     break;
1058   case DeclarationName::CXXDeductionGuideName:
1059     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1060                ->getDeclName().getAsIdentifierInfo();
1061     break;
1062   case DeclarationName::CXXConstructorName:
1063   case DeclarationName::CXXDestructorName:
1064   case DeclarationName::CXXConversionFunctionName:
1065   case DeclarationName::CXXUsingDirective:
1066     Data = 0;
1067     break;
1068   }
1069 }
1070 
1071 unsigned DeclarationNameKey::getHash() const {
1072   llvm::FoldingSetNodeID ID;
1073   ID.AddInteger(Kind);
1074 
1075   switch (Kind) {
1076   case DeclarationName::Identifier:
1077   case DeclarationName::CXXLiteralOperatorName:
1078   case DeclarationName::CXXDeductionGuideName:
1079     ID.AddString(((IdentifierInfo*)Data)->getName());
1080     break;
1081   case DeclarationName::ObjCZeroArgSelector:
1082   case DeclarationName::ObjCOneArgSelector:
1083   case DeclarationName::ObjCMultiArgSelector:
1084     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1085     break;
1086   case DeclarationName::CXXOperatorName:
1087     ID.AddInteger((OverloadedOperatorKind)Data);
1088     break;
1089   case DeclarationName::CXXConstructorName:
1090   case DeclarationName::CXXDestructorName:
1091   case DeclarationName::CXXConversionFunctionName:
1092   case DeclarationName::CXXUsingDirective:
1093     break;
1094   }
1095 
1096   return ID.ComputeHash();
1097 }
1098 
1099 ModuleFile *
1100 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1101   using namespace llvm::support;
1102 
1103   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1104   return Reader.getLocalModuleFile(F, ModuleFileID);
1105 }
1106 
1107 std::pair<unsigned, unsigned>
1108 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1109   return readULEBKeyDataLength(d);
1110 }
1111 
1112 ASTDeclContextNameLookupTrait::internal_key_type
1113 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1114   using namespace llvm::support;
1115 
1116   auto Kind = (DeclarationName::NameKind)*d++;
1117   uint64_t Data;
1118   switch (Kind) {
1119   case DeclarationName::Identifier:
1120   case DeclarationName::CXXLiteralOperatorName:
1121   case DeclarationName::CXXDeductionGuideName:
1122     Data = (uint64_t)Reader.getLocalIdentifier(
1123         F, endian::readNext<uint32_t, little, unaligned>(d));
1124     break;
1125   case DeclarationName::ObjCZeroArgSelector:
1126   case DeclarationName::ObjCOneArgSelector:
1127   case DeclarationName::ObjCMultiArgSelector:
1128     Data =
1129         (uint64_t)Reader.getLocalSelector(
1130                              F, endian::readNext<uint32_t, little, unaligned>(
1131                                     d)).getAsOpaquePtr();
1132     break;
1133   case DeclarationName::CXXOperatorName:
1134     Data = *d++; // OverloadedOperatorKind
1135     break;
1136   case DeclarationName::CXXConstructorName:
1137   case DeclarationName::CXXDestructorName:
1138   case DeclarationName::CXXConversionFunctionName:
1139   case DeclarationName::CXXUsingDirective:
1140     Data = 0;
1141     break;
1142   }
1143 
1144   return DeclarationNameKey(Kind, Data);
1145 }
1146 
1147 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1148                                                  const unsigned char *d,
1149                                                  unsigned DataLen,
1150                                                  data_type_builder &Val) {
1151   using namespace llvm::support;
1152 
1153   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1154     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1155     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1156   }
1157 }
1158 
1159 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1160                                               BitstreamCursor &Cursor,
1161                                               uint64_t Offset,
1162                                               DeclContext *DC) {
1163   assert(Offset != 0);
1164 
1165   SavedStreamPosition SavedPosition(Cursor);
1166   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1167     Error(std::move(Err));
1168     return true;
1169   }
1170 
1171   RecordData Record;
1172   StringRef Blob;
1173   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1174   if (!MaybeCode) {
1175     Error(MaybeCode.takeError());
1176     return true;
1177   }
1178   unsigned Code = MaybeCode.get();
1179 
1180   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1181   if (!MaybeRecCode) {
1182     Error(MaybeRecCode.takeError());
1183     return true;
1184   }
1185   unsigned RecCode = MaybeRecCode.get();
1186   if (RecCode != DECL_CONTEXT_LEXICAL) {
1187     Error("Expected lexical block");
1188     return true;
1189   }
1190 
1191   assert(!isa<TranslationUnitDecl>(DC) &&
1192          "expected a TU_UPDATE_LEXICAL record for TU");
1193   // If we are handling a C++ class template instantiation, we can see multiple
1194   // lexical updates for the same record. It's important that we select only one
1195   // of them, so that field numbering works properly. Just pick the first one we
1196   // see.
1197   auto &Lex = LexicalDecls[DC];
1198   if (!Lex.first) {
1199     Lex = std::make_pair(
1200         &M, llvm::makeArrayRef(
1201                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1202                     Blob.data()),
1203                 Blob.size() / 4));
1204   }
1205   DC->setHasExternalLexicalStorage(true);
1206   return false;
1207 }
1208 
1209 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1210                                               BitstreamCursor &Cursor,
1211                                               uint64_t Offset,
1212                                               DeclID ID) {
1213   assert(Offset != 0);
1214 
1215   SavedStreamPosition SavedPosition(Cursor);
1216   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1217     Error(std::move(Err));
1218     return true;
1219   }
1220 
1221   RecordData Record;
1222   StringRef Blob;
1223   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1224   if (!MaybeCode) {
1225     Error(MaybeCode.takeError());
1226     return true;
1227   }
1228   unsigned Code = MaybeCode.get();
1229 
1230   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1231   if (!MaybeRecCode) {
1232     Error(MaybeRecCode.takeError());
1233     return true;
1234   }
1235   unsigned RecCode = MaybeRecCode.get();
1236   if (RecCode != DECL_CONTEXT_VISIBLE) {
1237     Error("Expected visible lookup table block");
1238     return true;
1239   }
1240 
1241   // We can't safely determine the primary context yet, so delay attaching the
1242   // lookup table until we're done with recursive deserialization.
1243   auto *Data = (const unsigned char*)Blob.data();
1244   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1245   return false;
1246 }
1247 
1248 void ASTReader::Error(StringRef Msg) const {
1249   Error(diag::err_fe_pch_malformed, Msg);
1250   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1251       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1252     Diag(diag::note_module_cache_path)
1253       << PP.getHeaderSearchInfo().getModuleCachePath();
1254   }
1255 }
1256 
1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1258                       StringRef Arg3) const {
1259   if (Diags.isDiagnosticInFlight())
1260     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1261   else
1262     Diag(DiagID) << Arg1 << Arg2 << Arg3;
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     Optional<FileEntryRef> 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     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1535                                         BaseOffset + Record[0]);
1536     SrcMgr::FileInfo &FileInfo =
1537           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1538     FileInfo.NumCreatedFIDs = Record[5];
1539     if (Record[3])
1540       FileInfo.setHasLineDirectives();
1541 
1542     unsigned NumFileDecls = Record[7];
1543     if (NumFileDecls && ContextObj) {
1544       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1545       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1546       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1547                                                              NumFileDecls));
1548     }
1549 
1550     const SrcMgr::ContentCache &ContentCache =
1551         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1552     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1553         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1554         !ContentCache.getBufferIfLoaded()) {
1555       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1556       if (!Buffer)
1557         return true;
1558       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1559     }
1560 
1561     break;
1562   }
1563 
1564   case SM_SLOC_BUFFER_ENTRY: {
1565     const char *Name = Blob.data();
1566     unsigned Offset = Record[0];
1567     SrcMgr::CharacteristicKind
1568       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1569     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1570     if (IncludeLoc.isInvalid() && F->isModule()) {
1571       IncludeLoc = getImportLocation(F);
1572     }
1573 
1574     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1575     if (!Buffer)
1576       return true;
1577     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1578                            BaseOffset + Offset, IncludeLoc);
1579     break;
1580   }
1581 
1582   case SM_SLOC_EXPANSION_ENTRY: {
1583     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1584     SourceMgr.createExpansionLoc(SpellingLoc,
1585                                      ReadSourceLocation(*F, Record[2]),
1586                                      ReadSourceLocation(*F, Record[3]),
1587                                      Record[5],
1588                                      Record[4],
1589                                      ID,
1590                                      BaseOffset + Record[0]);
1591     break;
1592   }
1593   }
1594 
1595   return false;
1596 }
1597 
1598 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1599   if (ID == 0)
1600     return std::make_pair(SourceLocation(), "");
1601 
1602   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1603     Error("source location entry ID out-of-range for AST file");
1604     return std::make_pair(SourceLocation(), "");
1605   }
1606 
1607   // Find which module file this entry lands in.
1608   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1609   if (!M->isModule())
1610     return std::make_pair(SourceLocation(), "");
1611 
1612   // FIXME: Can we map this down to a particular submodule? That would be
1613   // ideal.
1614   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1615 }
1616 
1617 /// Find the location where the module F is imported.
1618 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1619   if (F->ImportLoc.isValid())
1620     return F->ImportLoc;
1621 
1622   // Otherwise we have a PCH. It's considered to be "imported" at the first
1623   // location of its includer.
1624   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1625     // Main file is the importer.
1626     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1627     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1628   }
1629   return F->ImportedBy[0]->FirstLoc;
1630 }
1631 
1632 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1633 /// the abbreviations that are at the top of the block and then leave the cursor
1634 /// pointing into the block.
1635 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1636                                  uint64_t *StartOfBlockOffset) {
1637   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1638     // FIXME this drops errors on the floor.
1639     consumeError(std::move(Err));
1640     return true;
1641   }
1642 
1643   if (StartOfBlockOffset)
1644     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1645 
1646   while (true) {
1647     uint64_t Offset = Cursor.GetCurrentBitNo();
1648     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1649     if (!MaybeCode) {
1650       // FIXME this drops errors on the floor.
1651       consumeError(MaybeCode.takeError());
1652       return true;
1653     }
1654     unsigned Code = MaybeCode.get();
1655 
1656     // We expect all abbrevs to be at the start of the block.
1657     if (Code != llvm::bitc::DEFINE_ABBREV) {
1658       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1659         // FIXME this drops errors on the floor.
1660         consumeError(std::move(Err));
1661         return true;
1662       }
1663       return false;
1664     }
1665     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1666       // FIXME this drops errors on the floor.
1667       consumeError(std::move(Err));
1668       return true;
1669     }
1670   }
1671 }
1672 
1673 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1674                            unsigned &Idx) {
1675   Token Tok;
1676   Tok.startToken();
1677   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1678   Tok.setLength(Record[Idx++]);
1679   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1680     Tok.setIdentifierInfo(II);
1681   Tok.setKind((tok::TokenKind)Record[Idx++]);
1682   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1683   return Tok;
1684 }
1685 
1686 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1687   BitstreamCursor &Stream = F.MacroCursor;
1688 
1689   // Keep track of where we are in the stream, then jump back there
1690   // after reading this macro.
1691   SavedStreamPosition SavedPosition(Stream);
1692 
1693   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1694     // FIXME this drops errors on the floor.
1695     consumeError(std::move(Err));
1696     return nullptr;
1697   }
1698   RecordData Record;
1699   SmallVector<IdentifierInfo*, 16> MacroParams;
1700   MacroInfo *Macro = nullptr;
1701 
1702   while (true) {
1703     // Advance to the next record, but if we get to the end of the block, don't
1704     // pop it (removing all the abbreviations from the cursor) since we want to
1705     // be able to reseek within the block and read entries.
1706     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1707     Expected<llvm::BitstreamEntry> MaybeEntry =
1708         Stream.advanceSkippingSubblocks(Flags);
1709     if (!MaybeEntry) {
1710       Error(MaybeEntry.takeError());
1711       return Macro;
1712     }
1713     llvm::BitstreamEntry Entry = MaybeEntry.get();
1714 
1715     switch (Entry.Kind) {
1716     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1717     case llvm::BitstreamEntry::Error:
1718       Error("malformed block record in AST file");
1719       return Macro;
1720     case llvm::BitstreamEntry::EndBlock:
1721       return Macro;
1722     case llvm::BitstreamEntry::Record:
1723       // The interesting case.
1724       break;
1725     }
1726 
1727     // Read a record.
1728     Record.clear();
1729     PreprocessorRecordTypes RecType;
1730     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1731       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1732     else {
1733       Error(MaybeRecType.takeError());
1734       return Macro;
1735     }
1736     switch (RecType) {
1737     case PP_MODULE_MACRO:
1738     case PP_MACRO_DIRECTIVE_HISTORY:
1739       return Macro;
1740 
1741     case PP_MACRO_OBJECT_LIKE:
1742     case PP_MACRO_FUNCTION_LIKE: {
1743       // If we already have a macro, that means that we've hit the end
1744       // of the definition of the macro we were looking for. We're
1745       // done.
1746       if (Macro)
1747         return Macro;
1748 
1749       unsigned NextIndex = 1; // Skip identifier ID.
1750       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1751       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1752       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1753       MI->setIsUsed(Record[NextIndex++]);
1754       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1755 
1756       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1757         // Decode function-like macro info.
1758         bool isC99VarArgs = Record[NextIndex++];
1759         bool isGNUVarArgs = Record[NextIndex++];
1760         bool hasCommaPasting = Record[NextIndex++];
1761         MacroParams.clear();
1762         unsigned NumArgs = Record[NextIndex++];
1763         for (unsigned i = 0; i != NumArgs; ++i)
1764           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1765 
1766         // Install function-like macro info.
1767         MI->setIsFunctionLike();
1768         if (isC99VarArgs) MI->setIsC99Varargs();
1769         if (isGNUVarArgs) MI->setIsGNUVarargs();
1770         if (hasCommaPasting) MI->setHasCommaPasting();
1771         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1772       }
1773 
1774       // Remember that we saw this macro last so that we add the tokens that
1775       // form its body to it.
1776       Macro = MI;
1777 
1778       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1779           Record[NextIndex]) {
1780         // We have a macro definition. Register the association
1781         PreprocessedEntityID
1782             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1783         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1784         PreprocessingRecord::PPEntityID PPID =
1785             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1786         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1787             PPRec.getPreprocessedEntity(PPID));
1788         if (PPDef)
1789           PPRec.RegisterMacroDefinition(Macro, PPDef);
1790       }
1791 
1792       ++NumMacrosRead;
1793       break;
1794     }
1795 
1796     case PP_TOKEN: {
1797       // If we see a TOKEN before a PP_MACRO_*, then the file is
1798       // erroneous, just pretend we didn't see this.
1799       if (!Macro) break;
1800 
1801       unsigned Idx = 0;
1802       Token Tok = ReadToken(F, Record, Idx);
1803       Macro->AddTokenToBody(Tok);
1804       break;
1805     }
1806     }
1807   }
1808 }
1809 
1810 PreprocessedEntityID
1811 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1812                                          unsigned LocalID) const {
1813   if (!M.ModuleOffsetMap.empty())
1814     ReadModuleOffsetMap(M);
1815 
1816   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1817     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1818   assert(I != M.PreprocessedEntityRemap.end()
1819          && "Invalid index into preprocessed entity index remap");
1820 
1821   return LocalID + I->second;
1822 }
1823 
1824 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1825   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1826 }
1827 
1828 HeaderFileInfoTrait::internal_key_type
1829 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1830   internal_key_type ikey = {FE->getSize(),
1831                             M.HasTimestamps ? FE->getModificationTime() : 0,
1832                             FE->getName(), /*Imported*/ false};
1833   return ikey;
1834 }
1835 
1836 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1837   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1838     return false;
1839 
1840   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1841     return true;
1842 
1843   // Determine whether the actual files are equivalent.
1844   FileManager &FileMgr = Reader.getFileManager();
1845   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1846     if (!Key.Imported) {
1847       if (auto File = FileMgr.getFile(Key.Filename))
1848         return *File;
1849       return nullptr;
1850     }
1851 
1852     std::string Resolved = std::string(Key.Filename);
1853     Reader.ResolveImportedPath(M, Resolved);
1854     if (auto File = FileMgr.getFile(Resolved))
1855       return *File;
1856     return nullptr;
1857   };
1858 
1859   const FileEntry *FEA = GetFile(a);
1860   const FileEntry *FEB = GetFile(b);
1861   return FEA && FEA == FEB;
1862 }
1863 
1864 std::pair<unsigned, unsigned>
1865 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1866   return readULEBKeyDataLength(d);
1867 }
1868 
1869 HeaderFileInfoTrait::internal_key_type
1870 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1871   using namespace llvm::support;
1872 
1873   internal_key_type ikey;
1874   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1875   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1876   ikey.Filename = (const char *)d;
1877   ikey.Imported = true;
1878   return ikey;
1879 }
1880 
1881 HeaderFileInfoTrait::data_type
1882 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1883                               unsigned DataLen) {
1884   using namespace llvm::support;
1885 
1886   const unsigned char *End = d + DataLen;
1887   HeaderFileInfo HFI;
1888   unsigned Flags = *d++;
1889   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1890   HFI.isImport |= (Flags >> 5) & 0x01;
1891   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1892   HFI.DirInfo = (Flags >> 1) & 0x07;
1893   HFI.IndexHeaderMapHeader = Flags & 0x01;
1894   // FIXME: Find a better way to handle this. Maybe just store a
1895   // "has been included" flag?
1896   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1897                              HFI.NumIncludes);
1898   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1899       M, endian::readNext<uint32_t, little, unaligned>(d));
1900   if (unsigned FrameworkOffset =
1901           endian::readNext<uint32_t, little, unaligned>(d)) {
1902     // The framework offset is 1 greater than the actual offset,
1903     // since 0 is used as an indicator for "no framework name".
1904     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1905     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1906   }
1907 
1908   assert((End - d) % 4 == 0 &&
1909          "Wrong data length in HeaderFileInfo deserialization");
1910   while (d != End) {
1911     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1912     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1913     LocalSMID >>= 2;
1914 
1915     // This header is part of a module. Associate it with the module to enable
1916     // implicit module import.
1917     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1918     Module *Mod = Reader.getSubmodule(GlobalSMID);
1919     FileManager &FileMgr = Reader.getFileManager();
1920     ModuleMap &ModMap =
1921         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1922 
1923     std::string Filename = std::string(key.Filename);
1924     if (key.Imported)
1925       Reader.ResolveImportedPath(M, Filename);
1926     // FIXME: NameAsWritten
1927     Module::Header H = {std::string(key.Filename), "",
1928                         *FileMgr.getFile(Filename)};
1929     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1930     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1931   }
1932 
1933   // This HeaderFileInfo was externally loaded.
1934   HFI.External = true;
1935   HFI.IsValid = true;
1936   return HFI;
1937 }
1938 
1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1940                                 uint32_t MacroDirectivesOffset) {
1941   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1942   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1943 }
1944 
1945 void ASTReader::ReadDefinedMacros() {
1946   // Note that we are loading defined macros.
1947   Deserializing Macros(this);
1948 
1949   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1950     BitstreamCursor &MacroCursor = I.MacroCursor;
1951 
1952     // If there was no preprocessor block, skip this file.
1953     if (MacroCursor.getBitcodeBytes().empty())
1954       continue;
1955 
1956     BitstreamCursor Cursor = MacroCursor;
1957     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1958       Error(std::move(Err));
1959       return;
1960     }
1961 
1962     RecordData Record;
1963     while (true) {
1964       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1965       if (!MaybeE) {
1966         Error(MaybeE.takeError());
1967         return;
1968       }
1969       llvm::BitstreamEntry E = MaybeE.get();
1970 
1971       switch (E.Kind) {
1972       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1973       case llvm::BitstreamEntry::Error:
1974         Error("malformed block record in AST file");
1975         return;
1976       case llvm::BitstreamEntry::EndBlock:
1977         goto NextCursor;
1978 
1979       case llvm::BitstreamEntry::Record: {
1980         Record.clear();
1981         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1982         if (!MaybeRecord) {
1983           Error(MaybeRecord.takeError());
1984           return;
1985         }
1986         switch (MaybeRecord.get()) {
1987         default:  // Default behavior: ignore.
1988           break;
1989 
1990         case PP_MACRO_OBJECT_LIKE:
1991         case PP_MACRO_FUNCTION_LIKE: {
1992           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1993           if (II->isOutOfDate())
1994             updateOutOfDateIdentifier(*II);
1995           break;
1996         }
1997 
1998         case PP_TOKEN:
1999           // Ignore tokens.
2000           break;
2001         }
2002         break;
2003       }
2004       }
2005     }
2006     NextCursor:  ;
2007   }
2008 }
2009 
2010 namespace {
2011 
2012   /// Visitor class used to look up identifirs in an AST file.
2013   class IdentifierLookupVisitor {
2014     StringRef Name;
2015     unsigned NameHash;
2016     unsigned PriorGeneration;
2017     unsigned &NumIdentifierLookups;
2018     unsigned &NumIdentifierLookupHits;
2019     IdentifierInfo *Found = nullptr;
2020 
2021   public:
2022     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2023                             unsigned &NumIdentifierLookups,
2024                             unsigned &NumIdentifierLookupHits)
2025       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2026         PriorGeneration(PriorGeneration),
2027         NumIdentifierLookups(NumIdentifierLookups),
2028         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2029 
2030     bool operator()(ModuleFile &M) {
2031       // If we've already searched this module file, skip it now.
2032       if (M.Generation <= PriorGeneration)
2033         return true;
2034 
2035       ASTIdentifierLookupTable *IdTable
2036         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2037       if (!IdTable)
2038         return false;
2039 
2040       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2041                                      Found);
2042       ++NumIdentifierLookups;
2043       ASTIdentifierLookupTable::iterator Pos =
2044           IdTable->find_hashed(Name, NameHash, &Trait);
2045       if (Pos == IdTable->end())
2046         return false;
2047 
2048       // Dereferencing the iterator has the effect of building the
2049       // IdentifierInfo node and populating it with the various
2050       // declarations it needs.
2051       ++NumIdentifierLookupHits;
2052       Found = *Pos;
2053       return true;
2054     }
2055 
2056     // Retrieve the identifier info found within the module
2057     // files.
2058     IdentifierInfo *getIdentifierInfo() const { return Found; }
2059   };
2060 
2061 } // namespace
2062 
2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2064   // Note that we are loading an identifier.
2065   Deserializing AnIdentifier(this);
2066 
2067   unsigned PriorGeneration = 0;
2068   if (getContext().getLangOpts().Modules)
2069     PriorGeneration = IdentifierGeneration[&II];
2070 
2071   // If there is a global index, look there first to determine which modules
2072   // provably do not have any results for this identifier.
2073   GlobalModuleIndex::HitSet Hits;
2074   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2075   if (!loadGlobalIndex()) {
2076     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2077       HitsPtr = &Hits;
2078     }
2079   }
2080 
2081   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2082                                   NumIdentifierLookups,
2083                                   NumIdentifierLookupHits);
2084   ModuleMgr.visit(Visitor, HitsPtr);
2085   markIdentifierUpToDate(&II);
2086 }
2087 
2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2089   if (!II)
2090     return;
2091 
2092   II->setOutOfDate(false);
2093 
2094   // Update the generation for this identifier.
2095   if (getContext().getLangOpts().Modules)
2096     IdentifierGeneration[II] = getGeneration();
2097 }
2098 
2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2100                                     const PendingMacroInfo &PMInfo) {
2101   ModuleFile &M = *PMInfo.M;
2102 
2103   BitstreamCursor &Cursor = M.MacroCursor;
2104   SavedStreamPosition SavedPosition(Cursor);
2105   if (llvm::Error Err =
2106           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2107     Error(std::move(Err));
2108     return;
2109   }
2110 
2111   struct ModuleMacroRecord {
2112     SubmoduleID SubModID;
2113     MacroInfo *MI;
2114     SmallVector<SubmoduleID, 8> Overrides;
2115   };
2116   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2117 
2118   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2119   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2120   // macro histroy.
2121   RecordData Record;
2122   while (true) {
2123     Expected<llvm::BitstreamEntry> MaybeEntry =
2124         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2125     if (!MaybeEntry) {
2126       Error(MaybeEntry.takeError());
2127       return;
2128     }
2129     llvm::BitstreamEntry Entry = MaybeEntry.get();
2130 
2131     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2132       Error("malformed block record in AST file");
2133       return;
2134     }
2135 
2136     Record.clear();
2137     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2138     if (!MaybePP) {
2139       Error(MaybePP.takeError());
2140       return;
2141     }
2142     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2143     case PP_MACRO_DIRECTIVE_HISTORY:
2144       break;
2145 
2146     case PP_MODULE_MACRO: {
2147       ModuleMacros.push_back(ModuleMacroRecord());
2148       auto &Info = ModuleMacros.back();
2149       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2150       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2151       for (int I = 2, N = Record.size(); I != N; ++I)
2152         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2153       continue;
2154     }
2155 
2156     default:
2157       Error("malformed block record in AST file");
2158       return;
2159     }
2160 
2161     // We found the macro directive history; that's the last record
2162     // for this macro.
2163     break;
2164   }
2165 
2166   // Module macros are listed in reverse dependency order.
2167   {
2168     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2169     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2170     for (auto &MMR : ModuleMacros) {
2171       Overrides.clear();
2172       for (unsigned ModID : MMR.Overrides) {
2173         Module *Mod = getSubmodule(ModID);
2174         auto *Macro = PP.getModuleMacro(Mod, II);
2175         assert(Macro && "missing definition for overridden macro");
2176         Overrides.push_back(Macro);
2177       }
2178 
2179       bool Inserted = false;
2180       Module *Owner = getSubmodule(MMR.SubModID);
2181       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2182     }
2183   }
2184 
2185   // Don't read the directive history for a module; we don't have anywhere
2186   // to put it.
2187   if (M.isModule())
2188     return;
2189 
2190   // Deserialize the macro directives history in reverse source-order.
2191   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2192   unsigned Idx = 0, N = Record.size();
2193   while (Idx < N) {
2194     MacroDirective *MD = nullptr;
2195     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2196     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2197     switch (K) {
2198     case MacroDirective::MD_Define: {
2199       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2200       MD = PP.AllocateDefMacroDirective(MI, Loc);
2201       break;
2202     }
2203     case MacroDirective::MD_Undefine:
2204       MD = PP.AllocateUndefMacroDirective(Loc);
2205       break;
2206     case MacroDirective::MD_Visibility:
2207       bool isPublic = Record[Idx++];
2208       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2209       break;
2210     }
2211 
2212     if (!Latest)
2213       Latest = MD;
2214     if (Earliest)
2215       Earliest->setPrevious(MD);
2216     Earliest = MD;
2217   }
2218 
2219   if (Latest)
2220     PP.setLoadedMacroDirective(II, Earliest, Latest);
2221 }
2222 
2223 bool ASTReader::shouldDisableValidationForFile(
2224     const serialization::ModuleFile &M) const {
2225   if (DisableValidationKind == DisableValidationForModuleKind::None)
2226     return false;
2227 
2228   // If a PCH is loaded and validation is disabled for PCH then disable
2229   // validation for the PCH and the modules it loads.
2230   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2231 
2232   switch (K) {
2233   case MK_MainFile:
2234   case MK_Preamble:
2235   case MK_PCH:
2236     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2237   case MK_ImplicitModule:
2238   case MK_ExplicitModule:
2239   case MK_PrebuiltModule:
2240     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2241   }
2242 
2243   return false;
2244 }
2245 
2246 ASTReader::InputFileInfo
2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2248   // Go find this input file.
2249   BitstreamCursor &Cursor = F.InputFilesCursor;
2250   SavedStreamPosition SavedPosition(Cursor);
2251   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2252     // FIXME this drops errors on the floor.
2253     consumeError(std::move(Err));
2254   }
2255 
2256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2257   if (!MaybeCode) {
2258     // FIXME this drops errors on the floor.
2259     consumeError(MaybeCode.takeError());
2260   }
2261   unsigned Code = MaybeCode.get();
2262   RecordData Record;
2263   StringRef Blob;
2264 
2265   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2266     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2267            "invalid record type for input file");
2268   else {
2269     // FIXME this drops errors on the floor.
2270     consumeError(Maybe.takeError());
2271   }
2272 
2273   assert(Record[0] == ID && "Bogus stored ID or offset");
2274   InputFileInfo R;
2275   R.StoredSize = static_cast<off_t>(Record[1]);
2276   R.StoredTime = static_cast<time_t>(Record[2]);
2277   R.Overridden = static_cast<bool>(Record[3]);
2278   R.Transient = static_cast<bool>(Record[4]);
2279   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2280   R.Filename = std::string(Blob);
2281   ResolveImportedPath(F, R.Filename);
2282 
2283   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2284   if (!MaybeEntry) // FIXME this drops errors on the floor.
2285     consumeError(MaybeEntry.takeError());
2286   llvm::BitstreamEntry Entry = MaybeEntry.get();
2287   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2288          "expected record type for input file hash");
2289 
2290   Record.clear();
2291   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2292     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2293            "invalid record type for input file hash");
2294   else {
2295     // FIXME this drops errors on the floor.
2296     consumeError(Maybe.takeError());
2297   }
2298   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2299                   static_cast<uint64_t>(Record[0]);
2300   return R;
2301 }
2302 
2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2305   // If this ID is bogus, just return an empty input file.
2306   if (ID == 0 || ID > F.InputFilesLoaded.size())
2307     return InputFile();
2308 
2309   // If we've already loaded this input file, return it.
2310   if (F.InputFilesLoaded[ID-1].getFile())
2311     return F.InputFilesLoaded[ID-1];
2312 
2313   if (F.InputFilesLoaded[ID-1].isNotFound())
2314     return InputFile();
2315 
2316   // Go find this input file.
2317   BitstreamCursor &Cursor = F.InputFilesCursor;
2318   SavedStreamPosition SavedPosition(Cursor);
2319   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2320     // FIXME this drops errors on the floor.
2321     consumeError(std::move(Err));
2322   }
2323 
2324   InputFileInfo FI = readInputFileInfo(F, ID);
2325   off_t StoredSize = FI.StoredSize;
2326   time_t StoredTime = FI.StoredTime;
2327   bool Overridden = FI.Overridden;
2328   bool Transient = FI.Transient;
2329   StringRef Filename = FI.Filename;
2330   uint64_t StoredContentHash = FI.ContentHash;
2331 
2332   OptionalFileEntryRefDegradesToFileEntryPtr File =
2333       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2334 
2335   // If we didn't find the file, resolve it relative to the
2336   // original directory from which this AST file was created.
2337   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2338       F.OriginalDir != F.BaseDirectory) {
2339     std::string Resolved = resolveFileRelativeToOriginalDir(
2340         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2341     if (!Resolved.empty())
2342       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2343   }
2344 
2345   // For an overridden file, create a virtual file with the stored
2346   // size/timestamp.
2347   if ((Overridden || Transient) && !File)
2348     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2349 
2350   if (!File) {
2351     if (Complain) {
2352       std::string ErrorStr = "could not find file '";
2353       ErrorStr += Filename;
2354       ErrorStr += "' referenced by AST file '";
2355       ErrorStr += F.FileName;
2356       ErrorStr += "'";
2357       Error(ErrorStr);
2358     }
2359     // Record that we didn't find the file.
2360     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2361     return InputFile();
2362   }
2363 
2364   // Check if there was a request to override the contents of the file
2365   // that was part of the precompiled header. Overriding such a file
2366   // can lead to problems when lexing using the source locations from the
2367   // PCH.
2368   SourceManager &SM = getSourceManager();
2369   // FIXME: Reject if the overrides are different.
2370   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2371     if (Complain)
2372       Error(diag::err_fe_pch_file_overridden, Filename);
2373 
2374     // After emitting the diagnostic, bypass the overriding file to recover
2375     // (this creates a separate FileEntry).
2376     File = SM.bypassFileContentsOverride(*File);
2377     if (!File) {
2378       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2379       return InputFile();
2380     }
2381   }
2382 
2383   enum ModificationType {
2384     Size,
2385     ModTime,
2386     Content,
2387     None,
2388   };
2389   auto HasInputFileChanged = [&]() {
2390     if (StoredSize != File->getSize())
2391       return ModificationType::Size;
2392     if (!shouldDisableValidationForFile(F) && StoredTime &&
2393         StoredTime != File->getModificationTime()) {
2394       // In case the modification time changes but not the content,
2395       // accept the cached file as legit.
2396       if (ValidateASTInputFilesContent &&
2397           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2398         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2399         if (!MemBuffOrError) {
2400           if (!Complain)
2401             return ModificationType::ModTime;
2402           std::string ErrorStr = "could not get buffer for file '";
2403           ErrorStr += File->getName();
2404           ErrorStr += "'";
2405           Error(ErrorStr);
2406           return ModificationType::ModTime;
2407         }
2408 
2409         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2410         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2411           return ModificationType::None;
2412         return ModificationType::Content;
2413       }
2414       return ModificationType::ModTime;
2415     }
2416     return ModificationType::None;
2417   };
2418 
2419   bool IsOutOfDate = false;
2420   auto FileChange = HasInputFileChanged();
2421   // For an overridden file, there is nothing to validate.
2422   if (!Overridden && FileChange != ModificationType::None) {
2423     if (Complain && !Diags.isDiagnosticInFlight()) {
2424       // Build a list of the PCH imports that got us here (in reverse).
2425       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2426       while (!ImportStack.back()->ImportedBy.empty())
2427         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2428 
2429       // The top-level PCH is stale.
2430       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2431       Diag(diag::err_fe_ast_file_modified)
2432           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2433           << TopLevelPCHName << FileChange;
2434 
2435       // Print the import stack.
2436       if (ImportStack.size() > 1) {
2437         Diag(diag::note_pch_required_by)
2438           << Filename << ImportStack[0]->FileName;
2439         for (unsigned I = 1; I < ImportStack.size(); ++I)
2440           Diag(diag::note_pch_required_by)
2441             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2442       }
2443 
2444       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2445     }
2446 
2447     IsOutOfDate = true;
2448   }
2449   // FIXME: If the file is overridden and we've already opened it,
2450   // issue an error (or split it into a separate FileEntry).
2451 
2452   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2453 
2454   // Note that we've loaded this input file.
2455   F.InputFilesLoaded[ID-1] = IF;
2456   return IF;
2457 }
2458 
2459 /// If we are loading a relocatable PCH or module file, and the filename
2460 /// is not an absolute path, add the system or module root to the beginning of
2461 /// the file name.
2462 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2463   // Resolve relative to the base directory, if we have one.
2464   if (!M.BaseDirectory.empty())
2465     return ResolveImportedPath(Filename, M.BaseDirectory);
2466 }
2467 
2468 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2469   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2470     return;
2471 
2472   SmallString<128> Buffer;
2473   llvm::sys::path::append(Buffer, Prefix, Filename);
2474   Filename.assign(Buffer.begin(), Buffer.end());
2475 }
2476 
2477 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2478   switch (ARR) {
2479   case ASTReader::Failure: return true;
2480   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2481   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2482   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2483   case ASTReader::ConfigurationMismatch:
2484     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2485   case ASTReader::HadErrors: return true;
2486   case ASTReader::Success: return false;
2487   }
2488 
2489   llvm_unreachable("unknown ASTReadResult");
2490 }
2491 
2492 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2493     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2494     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2495     std::string &SuggestedPredefines) {
2496   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2497     // FIXME this drops errors on the floor.
2498     consumeError(std::move(Err));
2499     return Failure;
2500   }
2501 
2502   // Read all of the records in the options block.
2503   RecordData Record;
2504   ASTReadResult Result = Success;
2505   while (true) {
2506     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2507     if (!MaybeEntry) {
2508       // FIXME this drops errors on the floor.
2509       consumeError(MaybeEntry.takeError());
2510       return Failure;
2511     }
2512     llvm::BitstreamEntry Entry = MaybeEntry.get();
2513 
2514     switch (Entry.Kind) {
2515     case llvm::BitstreamEntry::Error:
2516     case llvm::BitstreamEntry::SubBlock:
2517       return Failure;
2518 
2519     case llvm::BitstreamEntry::EndBlock:
2520       return Result;
2521 
2522     case llvm::BitstreamEntry::Record:
2523       // The interesting case.
2524       break;
2525     }
2526 
2527     // Read and process a record.
2528     Record.clear();
2529     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2530     if (!MaybeRecordType) {
2531       // FIXME this drops errors on the floor.
2532       consumeError(MaybeRecordType.takeError());
2533       return Failure;
2534     }
2535     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2536     case LANGUAGE_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (ParseLanguageOptions(Record, Complain, Listener,
2539                                AllowCompatibleConfigurationMismatch))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case TARGET_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (ParseTargetOptions(Record, Complain, Listener,
2547                              AllowCompatibleConfigurationMismatch))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case FILE_SYSTEM_OPTIONS: {
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParseFileSystemOptions(Record, Complain, Listener))
2556         Result = ConfigurationMismatch;
2557       break;
2558     }
2559 
2560     case HEADER_SEARCH_OPTIONS: {
2561       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562       if (!AllowCompatibleConfigurationMismatch &&
2563           ParseHeaderSearchOptions(Record, Complain, Listener))
2564         Result = ConfigurationMismatch;
2565       break;
2566     }
2567 
2568     case PREPROCESSOR_OPTIONS:
2569       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2570       if (!AllowCompatibleConfigurationMismatch &&
2571           ParsePreprocessorOptions(Record, Complain, Listener,
2572                                    SuggestedPredefines))
2573         Result = ConfigurationMismatch;
2574       break;
2575     }
2576   }
2577 }
2578 
2579 ASTReader::ASTReadResult
2580 ASTReader::ReadControlBlock(ModuleFile &F,
2581                             SmallVectorImpl<ImportedModule> &Loaded,
2582                             const ModuleFile *ImportedBy,
2583                             unsigned ClientLoadCapabilities) {
2584   BitstreamCursor &Stream = F.Stream;
2585 
2586   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2587     Error(std::move(Err));
2588     return Failure;
2589   }
2590 
2591   // Lambda to read the unhashed control block the first time it's called.
2592   //
2593   // For PCM files, the unhashed control block cannot be read until after the
2594   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2595   // need to look ahead before reading the IMPORTS record.  For consistency,
2596   // this block is always read somehow (see BitstreamEntry::EndBlock).
2597   bool HasReadUnhashedControlBlock = false;
2598   auto readUnhashedControlBlockOnce = [&]() {
2599     if (!HasReadUnhashedControlBlock) {
2600       HasReadUnhashedControlBlock = true;
2601       if (ASTReadResult Result =
2602               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2603         return Result;
2604     }
2605     return Success;
2606   };
2607 
2608   bool DisableValidation = shouldDisableValidationForFile(F);
2609 
2610   // Read all of the records and blocks in the control block.
2611   RecordData Record;
2612   unsigned NumInputs = 0;
2613   unsigned NumUserInputs = 0;
2614   StringRef BaseDirectoryAsWritten;
2615   while (true) {
2616     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2617     if (!MaybeEntry) {
2618       Error(MaybeEntry.takeError());
2619       return Failure;
2620     }
2621     llvm::BitstreamEntry Entry = MaybeEntry.get();
2622 
2623     switch (Entry.Kind) {
2624     case llvm::BitstreamEntry::Error:
2625       Error("malformed block record in AST file");
2626       return Failure;
2627     case llvm::BitstreamEntry::EndBlock: {
2628       // Validate the module before returning.  This call catches an AST with
2629       // no module name and no imports.
2630       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2631         return Result;
2632 
2633       // Validate input files.
2634       const HeaderSearchOptions &HSOpts =
2635           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2636 
2637       // All user input files reside at the index range [0, NumUserInputs), and
2638       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2639       // loaded module files, ignore missing inputs.
2640       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2641           F.Kind != MK_PrebuiltModule) {
2642         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2643 
2644         // If we are reading a module, we will create a verification timestamp,
2645         // so we verify all input files.  Otherwise, verify only user input
2646         // files.
2647 
2648         unsigned N = NumUserInputs;
2649         if (ValidateSystemInputs ||
2650             (HSOpts.ModulesValidateOncePerBuildSession &&
2651              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2652              F.Kind == MK_ImplicitModule))
2653           N = NumInputs;
2654 
2655         for (unsigned I = 0; I < N; ++I) {
2656           InputFile IF = getInputFile(F, I+1, Complain);
2657           if (!IF.getFile() || IF.isOutOfDate())
2658             return OutOfDate;
2659         }
2660       }
2661 
2662       if (Listener)
2663         Listener->visitModuleFile(F.FileName, F.Kind);
2664 
2665       if (Listener && Listener->needsInputFileVisitation()) {
2666         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2667                                                                 : NumUserInputs;
2668         for (unsigned I = 0; I < N; ++I) {
2669           bool IsSystem = I >= NumUserInputs;
2670           InputFileInfo FI = readInputFileInfo(F, I+1);
2671           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2672                                    F.Kind == MK_ExplicitModule ||
2673                                    F.Kind == MK_PrebuiltModule);
2674         }
2675       }
2676 
2677       return Success;
2678     }
2679 
2680     case llvm::BitstreamEntry::SubBlock:
2681       switch (Entry.ID) {
2682       case INPUT_FILES_BLOCK_ID:
2683         F.InputFilesCursor = Stream;
2684         if (llvm::Error Err = Stream.SkipBlock()) {
2685           Error(std::move(Err));
2686           return Failure;
2687         }
2688         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2689           Error("malformed block record in AST file");
2690           return Failure;
2691         }
2692         continue;
2693 
2694       case OPTIONS_BLOCK_ID:
2695         // If we're reading the first module for this group, check its options
2696         // are compatible with ours. For modules it imports, no further checking
2697         // is required, because we checked them when we built it.
2698         if (Listener && !ImportedBy) {
2699           // Should we allow the configuration of the module file to differ from
2700           // the configuration of the current translation unit in a compatible
2701           // way?
2702           //
2703           // FIXME: Allow this for files explicitly specified with -include-pch.
2704           bool AllowCompatibleConfigurationMismatch =
2705               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2706 
2707           ASTReadResult Result =
2708               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2709                                AllowCompatibleConfigurationMismatch, *Listener,
2710                                SuggestedPredefines);
2711           if (Result == Failure) {
2712             Error("malformed block record in AST file");
2713             return Result;
2714           }
2715 
2716           if (DisableValidation ||
2717               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2718             Result = Success;
2719 
2720           // If we can't load the module, exit early since we likely
2721           // will rebuild the module anyway. The stream may be in the
2722           // middle of a block.
2723           if (Result != Success)
2724             return Result;
2725         } else if (llvm::Error Err = Stream.SkipBlock()) {
2726           Error(std::move(Err));
2727           return Failure;
2728         }
2729         continue;
2730 
2731       default:
2732         if (llvm::Error Err = Stream.SkipBlock()) {
2733           Error(std::move(Err));
2734           return Failure;
2735         }
2736         continue;
2737       }
2738 
2739     case llvm::BitstreamEntry::Record:
2740       // The interesting case.
2741       break;
2742     }
2743 
2744     // Read and process a record.
2745     Record.clear();
2746     StringRef Blob;
2747     Expected<unsigned> MaybeRecordType =
2748         Stream.readRecord(Entry.ID, Record, &Blob);
2749     if (!MaybeRecordType) {
2750       Error(MaybeRecordType.takeError());
2751       return Failure;
2752     }
2753     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2754     case METADATA: {
2755       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2756         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2757           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2758                                         : diag::err_pch_version_too_new);
2759         return VersionMismatch;
2760       }
2761 
2762       bool hasErrors = Record[6];
2763       if (hasErrors && !DisableValidation) {
2764         // If requested by the caller and the module hasn't already been read
2765         // or compiled, mark modules on error as out-of-date.
2766         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2767             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2768           return OutOfDate;
2769 
2770         if (!AllowASTWithCompilerErrors) {
2771           Diag(diag::err_pch_with_compiler_errors);
2772           return HadErrors;
2773         }
2774       }
2775       if (hasErrors) {
2776         Diags.ErrorOccurred = true;
2777         Diags.UncompilableErrorOccurred = true;
2778         Diags.UnrecoverableErrorOccurred = true;
2779       }
2780 
2781       F.RelocatablePCH = Record[4];
2782       // Relative paths in a relocatable PCH are relative to our sysroot.
2783       if (F.RelocatablePCH)
2784         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2785 
2786       F.HasTimestamps = Record[5];
2787 
2788       const std::string &CurBranch = getClangFullRepositoryVersion();
2789       StringRef ASTBranch = Blob;
2790       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2791         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2792           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2793         return VersionMismatch;
2794       }
2795       break;
2796     }
2797 
2798     case IMPORTS: {
2799       // Validate the AST before processing any imports (otherwise, untangling
2800       // them can be error-prone and expensive).  A module will have a name and
2801       // will already have been validated, but this catches the PCH case.
2802       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2803         return Result;
2804 
2805       // Load each of the imported PCH files.
2806       unsigned Idx = 0, N = Record.size();
2807       while (Idx < N) {
2808         // Read information about the AST file.
2809         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2810         // The import location will be the local one for now; we will adjust
2811         // all import locations of module imports after the global source
2812         // location info are setup, in ReadAST.
2813         SourceLocation ImportLoc =
2814             ReadUntranslatedSourceLocation(Record[Idx++]);
2815         off_t StoredSize = (off_t)Record[Idx++];
2816         time_t StoredModTime = (time_t)Record[Idx++];
2817         auto FirstSignatureByte = Record.begin() + Idx;
2818         ASTFileSignature StoredSignature = ASTFileSignature::create(
2819             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2820         Idx += ASTFileSignature::size;
2821 
2822         std::string ImportedName = ReadString(Record, Idx);
2823         std::string ImportedFile;
2824 
2825         // For prebuilt and explicit modules first consult the file map for
2826         // an override. Note that here we don't search prebuilt module
2827         // directories, only the explicit name to file mappings. Also, we will
2828         // still verify the size/signature making sure it is essentially the
2829         // same file but perhaps in a different location.
2830         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2831           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2832             ImportedName, /*FileMapOnly*/ true);
2833 
2834         if (ImportedFile.empty())
2835           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2836           // ModuleCache as when writing.
2837           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2838         else
2839           SkipPath(Record, Idx);
2840 
2841         // If our client can't cope with us being out of date, we can't cope with
2842         // our dependency being missing.
2843         unsigned Capabilities = ClientLoadCapabilities;
2844         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2845           Capabilities &= ~ARR_Missing;
2846 
2847         // Load the AST file.
2848         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2849                                   Loaded, StoredSize, StoredModTime,
2850                                   StoredSignature, Capabilities);
2851 
2852         // If we diagnosed a problem, produce a backtrace.
2853         bool recompilingFinalized =
2854             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2855             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2856         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2857           Diag(diag::note_module_file_imported_by)
2858               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2859         if (recompilingFinalized)
2860           Diag(diag::note_module_file_conflict);
2861 
2862         switch (Result) {
2863         case Failure: return Failure;
2864           // If we have to ignore the dependency, we'll have to ignore this too.
2865         case Missing:
2866         case OutOfDate: return OutOfDate;
2867         case VersionMismatch: return VersionMismatch;
2868         case ConfigurationMismatch: return ConfigurationMismatch;
2869         case HadErrors: return HadErrors;
2870         case Success: break;
2871         }
2872       }
2873       break;
2874     }
2875 
2876     case ORIGINAL_FILE:
2877       F.OriginalSourceFileID = FileID::get(Record[0]);
2878       F.ActualOriginalSourceFileName = std::string(Blob);
2879       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2880       ResolveImportedPath(F, F.OriginalSourceFileName);
2881       break;
2882 
2883     case ORIGINAL_FILE_ID:
2884       F.OriginalSourceFileID = FileID::get(Record[0]);
2885       break;
2886 
2887     case ORIGINAL_PCH_DIR:
2888       F.OriginalDir = std::string(Blob);
2889       break;
2890 
2891     case MODULE_NAME:
2892       F.ModuleName = std::string(Blob);
2893       Diag(diag::remark_module_import)
2894           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2895           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2896       if (Listener)
2897         Listener->ReadModuleName(F.ModuleName);
2898 
2899       // Validate the AST as soon as we have a name so we can exit early on
2900       // failure.
2901       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2902         return Result;
2903 
2904       break;
2905 
2906     case MODULE_DIRECTORY: {
2907       // Save the BaseDirectory as written in the PCM for computing the module
2908       // filename for the ModuleCache.
2909       BaseDirectoryAsWritten = Blob;
2910       assert(!F.ModuleName.empty() &&
2911              "MODULE_DIRECTORY found before MODULE_NAME");
2912       // If we've already loaded a module map file covering this module, we may
2913       // have a better path for it (relative to the current build).
2914       Module *M = PP.getHeaderSearchInfo().lookupModule(
2915           F.ModuleName, /*AllowSearch*/ true,
2916           /*AllowExtraModuleMapSearch*/ true);
2917       if (M && M->Directory) {
2918         // If we're implicitly loading a module, the base directory can't
2919         // change between the build and use.
2920         // Don't emit module relocation error if we have -fno-validate-pch
2921         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2922                   DisableValidationForModuleKind::Module) &&
2923             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2924           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2925           if (!BuildDir || *BuildDir != M->Directory) {
2926             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2927               Diag(diag::err_imported_module_relocated)
2928                   << F.ModuleName << Blob << M->Directory->getName();
2929             return OutOfDate;
2930           }
2931         }
2932         F.BaseDirectory = std::string(M->Directory->getName());
2933       } else {
2934         F.BaseDirectory = std::string(Blob);
2935       }
2936       break;
2937     }
2938 
2939     case MODULE_MAP_FILE:
2940       if (ASTReadResult Result =
2941               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2942         return Result;
2943       break;
2944 
2945     case INPUT_FILE_OFFSETS:
2946       NumInputs = Record[0];
2947       NumUserInputs = Record[1];
2948       F.InputFileOffsets =
2949           (const llvm::support::unaligned_uint64_t *)Blob.data();
2950       F.InputFilesLoaded.resize(NumInputs);
2951       F.NumUserInputFiles = NumUserInputs;
2952       break;
2953     }
2954   }
2955 }
2956 
2957 ASTReader::ASTReadResult
2958 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2959   BitstreamCursor &Stream = F.Stream;
2960 
2961   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2962     Error(std::move(Err));
2963     return Failure;
2964   }
2965   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2966 
2967   // Read all of the records and blocks for the AST file.
2968   RecordData Record;
2969   while (true) {
2970     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2971     if (!MaybeEntry) {
2972       Error(MaybeEntry.takeError());
2973       return Failure;
2974     }
2975     llvm::BitstreamEntry Entry = MaybeEntry.get();
2976 
2977     switch (Entry.Kind) {
2978     case llvm::BitstreamEntry::Error:
2979       Error("error at end of module block in AST file");
2980       return Failure;
2981     case llvm::BitstreamEntry::EndBlock:
2982       // Outside of C++, we do not store a lookup map for the translation unit.
2983       // Instead, mark it as needing a lookup map to be built if this module
2984       // contains any declarations lexically within it (which it always does!).
2985       // This usually has no cost, since we very rarely need the lookup map for
2986       // the translation unit outside C++.
2987       if (ASTContext *Ctx = ContextObj) {
2988         DeclContext *DC = Ctx->getTranslationUnitDecl();
2989         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2990           DC->setMustBuildLookupTable();
2991       }
2992 
2993       return Success;
2994     case llvm::BitstreamEntry::SubBlock:
2995       switch (Entry.ID) {
2996       case DECLTYPES_BLOCK_ID:
2997         // We lazily load the decls block, but we want to set up the
2998         // DeclsCursor cursor to point into it.  Clone our current bitcode
2999         // cursor to it, enter the block and read the abbrevs in that block.
3000         // With the main cursor, we just skip over it.
3001         F.DeclsCursor = Stream;
3002         if (llvm::Error Err = Stream.SkipBlock()) {
3003           Error(std::move(Err));
3004           return Failure;
3005         }
3006         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
3007                              &F.DeclsBlockStartOffset)) {
3008           Error("malformed block record in AST file");
3009           return Failure;
3010         }
3011         break;
3012 
3013       case PREPROCESSOR_BLOCK_ID:
3014         F.MacroCursor = Stream;
3015         if (!PP.getExternalSource())
3016           PP.setExternalSource(this);
3017 
3018         if (llvm::Error Err = Stream.SkipBlock()) {
3019           Error(std::move(Err));
3020           return Failure;
3021         }
3022         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3023           Error("malformed block record in AST file");
3024           return Failure;
3025         }
3026         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3027         break;
3028 
3029       case PREPROCESSOR_DETAIL_BLOCK_ID:
3030         F.PreprocessorDetailCursor = Stream;
3031 
3032         if (llvm::Error Err = Stream.SkipBlock()) {
3033           Error(std::move(Err));
3034           return Failure;
3035         }
3036         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3037                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3038           Error("malformed preprocessor detail record in AST file");
3039           return Failure;
3040         }
3041         F.PreprocessorDetailStartOffset
3042         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3043 
3044         if (!PP.getPreprocessingRecord())
3045           PP.createPreprocessingRecord();
3046         if (!PP.getPreprocessingRecord()->getExternalSource())
3047           PP.getPreprocessingRecord()->SetExternalSource(*this);
3048         break;
3049 
3050       case SOURCE_MANAGER_BLOCK_ID:
3051         if (ReadSourceManagerBlock(F))
3052           return Failure;
3053         break;
3054 
3055       case SUBMODULE_BLOCK_ID:
3056         if (ASTReadResult Result =
3057                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3058           return Result;
3059         break;
3060 
3061       case COMMENTS_BLOCK_ID: {
3062         BitstreamCursor C = Stream;
3063 
3064         if (llvm::Error Err = Stream.SkipBlock()) {
3065           Error(std::move(Err));
3066           return Failure;
3067         }
3068         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3069           Error("malformed comments block in AST file");
3070           return Failure;
3071         }
3072         CommentsCursors.push_back(std::make_pair(C, &F));
3073         break;
3074       }
3075 
3076       default:
3077         if (llvm::Error Err = Stream.SkipBlock()) {
3078           Error(std::move(Err));
3079           return Failure;
3080         }
3081         break;
3082       }
3083       continue;
3084 
3085     case llvm::BitstreamEntry::Record:
3086       // The interesting case.
3087       break;
3088     }
3089 
3090     // Read and process a record.
3091     Record.clear();
3092     StringRef Blob;
3093     Expected<unsigned> MaybeRecordType =
3094         Stream.readRecord(Entry.ID, Record, &Blob);
3095     if (!MaybeRecordType) {
3096       Error(MaybeRecordType.takeError());
3097       return Failure;
3098     }
3099     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3100 
3101     // If we're not loading an AST context, we don't care about most records.
3102     if (!ContextObj) {
3103       switch (RecordType) {
3104       case IDENTIFIER_TABLE:
3105       case IDENTIFIER_OFFSET:
3106       case INTERESTING_IDENTIFIERS:
3107       case STATISTICS:
3108       case PP_CONDITIONAL_STACK:
3109       case PP_COUNTER_VALUE:
3110       case SOURCE_LOCATION_OFFSETS:
3111       case MODULE_OFFSET_MAP:
3112       case SOURCE_MANAGER_LINE_TABLE:
3113       case SOURCE_LOCATION_PRELOADS:
3114       case PPD_ENTITIES_OFFSETS:
3115       case HEADER_SEARCH_TABLE:
3116       case IMPORTED_MODULES:
3117       case MACRO_OFFSET:
3118         break;
3119       default:
3120         continue;
3121       }
3122     }
3123 
3124     switch (RecordType) {
3125     default:  // Default behavior: ignore.
3126       break;
3127 
3128     case TYPE_OFFSET: {
3129       if (F.LocalNumTypes != 0) {
3130         Error("duplicate TYPE_OFFSET record in AST file");
3131         return Failure;
3132       }
3133       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3134       F.LocalNumTypes = Record[0];
3135       unsigned LocalBaseTypeIndex = Record[1];
3136       F.BaseTypeIndex = getTotalNumTypes();
3137 
3138       if (F.LocalNumTypes > 0) {
3139         // Introduce the global -> local mapping for types within this module.
3140         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3141 
3142         // Introduce the local -> global mapping for types within this module.
3143         F.TypeRemap.insertOrReplace(
3144           std::make_pair(LocalBaseTypeIndex,
3145                          F.BaseTypeIndex - LocalBaseTypeIndex));
3146 
3147         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3148       }
3149       break;
3150     }
3151 
3152     case DECL_OFFSET: {
3153       if (F.LocalNumDecls != 0) {
3154         Error("duplicate DECL_OFFSET record in AST file");
3155         return Failure;
3156       }
3157       F.DeclOffsets = (const DeclOffset *)Blob.data();
3158       F.LocalNumDecls = Record[0];
3159       unsigned LocalBaseDeclID = Record[1];
3160       F.BaseDeclID = getTotalNumDecls();
3161 
3162       if (F.LocalNumDecls > 0) {
3163         // Introduce the global -> local mapping for declarations within this
3164         // module.
3165         GlobalDeclMap.insert(
3166           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3167 
3168         // Introduce the local -> global mapping for declarations within this
3169         // module.
3170         F.DeclRemap.insertOrReplace(
3171           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3172 
3173         // Introduce the global -> local mapping for declarations within this
3174         // module.
3175         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3176 
3177         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3178       }
3179       break;
3180     }
3181 
3182     case TU_UPDATE_LEXICAL: {
3183       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3184       LexicalContents Contents(
3185           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3186               Blob.data()),
3187           static_cast<unsigned int>(Blob.size() / 4));
3188       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3189       TU->setHasExternalLexicalStorage(true);
3190       break;
3191     }
3192 
3193     case UPDATE_VISIBLE: {
3194       unsigned Idx = 0;
3195       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3196       auto *Data = (const unsigned char*)Blob.data();
3197       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3198       // If we've already loaded the decl, perform the updates when we finish
3199       // loading this block.
3200       if (Decl *D = GetExistingDecl(ID))
3201         PendingUpdateRecords.push_back(
3202             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3203       break;
3204     }
3205 
3206     case IDENTIFIER_TABLE:
3207       F.IdentifierTableData =
3208           reinterpret_cast<const unsigned char *>(Blob.data());
3209       if (Record[0]) {
3210         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3211             F.IdentifierTableData + Record[0],
3212             F.IdentifierTableData + sizeof(uint32_t),
3213             F.IdentifierTableData,
3214             ASTIdentifierLookupTrait(*this, F));
3215 
3216         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3217       }
3218       break;
3219 
3220     case IDENTIFIER_OFFSET: {
3221       if (F.LocalNumIdentifiers != 0) {
3222         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3223         return Failure;
3224       }
3225       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3226       F.LocalNumIdentifiers = Record[0];
3227       unsigned LocalBaseIdentifierID = Record[1];
3228       F.BaseIdentifierID = getTotalNumIdentifiers();
3229 
3230       if (F.LocalNumIdentifiers > 0) {
3231         // Introduce the global -> local mapping for identifiers within this
3232         // module.
3233         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3234                                                   &F));
3235 
3236         // Introduce the local -> global mapping for identifiers within this
3237         // module.
3238         F.IdentifierRemap.insertOrReplace(
3239           std::make_pair(LocalBaseIdentifierID,
3240                          F.BaseIdentifierID - LocalBaseIdentifierID));
3241 
3242         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3243                                  + F.LocalNumIdentifiers);
3244       }
3245       break;
3246     }
3247 
3248     case INTERESTING_IDENTIFIERS:
3249       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3250       break;
3251 
3252     case EAGERLY_DESERIALIZED_DECLS:
3253       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3254       // about "interesting" decls (for instance, if we're building a module).
3255       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3256         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3257       break;
3258 
3259     case MODULAR_CODEGEN_DECLS:
3260       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3261       // them (ie: if we're not codegenerating this module).
3262       if (F.Kind == MK_MainFile ||
3263           getContext().getLangOpts().BuildingPCHWithObjectFile)
3264         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3266       break;
3267 
3268     case SPECIAL_TYPES:
3269       if (SpecialTypes.empty()) {
3270         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3271           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3272         break;
3273       }
3274 
3275       if (SpecialTypes.size() != Record.size()) {
3276         Error("invalid special-types record");
3277         return Failure;
3278       }
3279 
3280       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3281         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3282         if (!SpecialTypes[I])
3283           SpecialTypes[I] = ID;
3284         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3285         // merge step?
3286       }
3287       break;
3288 
3289     case STATISTICS:
3290       TotalNumStatements += Record[0];
3291       TotalNumMacros += Record[1];
3292       TotalLexicalDeclContexts += Record[2];
3293       TotalVisibleDeclContexts += Record[3];
3294       break;
3295 
3296     case UNUSED_FILESCOPED_DECLS:
3297       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3298         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3299       break;
3300 
3301     case DELEGATING_CTORS:
3302       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3303         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3304       break;
3305 
3306     case WEAK_UNDECLARED_IDENTIFIERS:
3307       if (Record.size() % 4 != 0) {
3308         Error("invalid weak identifiers record");
3309         return Failure;
3310       }
3311 
3312       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3313       // files. This isn't the way to do it :)
3314       WeakUndeclaredIdentifiers.clear();
3315 
3316       // Translate the weak, undeclared identifiers into global IDs.
3317       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3318         WeakUndeclaredIdentifiers.push_back(
3319           getGlobalIdentifierID(F, Record[I++]));
3320         WeakUndeclaredIdentifiers.push_back(
3321           getGlobalIdentifierID(F, Record[I++]));
3322         WeakUndeclaredIdentifiers.push_back(
3323           ReadSourceLocation(F, Record, I).getRawEncoding());
3324         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3325       }
3326       break;
3327 
3328     case SELECTOR_OFFSETS: {
3329       F.SelectorOffsets = (const uint32_t *)Blob.data();
3330       F.LocalNumSelectors = Record[0];
3331       unsigned LocalBaseSelectorID = Record[1];
3332       F.BaseSelectorID = getTotalNumSelectors();
3333 
3334       if (F.LocalNumSelectors > 0) {
3335         // Introduce the global -> local mapping for selectors within this
3336         // module.
3337         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3338 
3339         // Introduce the local -> global mapping for selectors within this
3340         // module.
3341         F.SelectorRemap.insertOrReplace(
3342           std::make_pair(LocalBaseSelectorID,
3343                          F.BaseSelectorID - LocalBaseSelectorID));
3344 
3345         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3346       }
3347       break;
3348     }
3349 
3350     case METHOD_POOL:
3351       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3352       if (Record[0])
3353         F.SelectorLookupTable
3354           = ASTSelectorLookupTable::Create(
3355                         F.SelectorLookupTableData + Record[0],
3356                         F.SelectorLookupTableData,
3357                         ASTSelectorLookupTrait(*this, F));
3358       TotalNumMethodPoolEntries += Record[1];
3359       break;
3360 
3361     case REFERENCED_SELECTOR_POOL:
3362       if (!Record.empty()) {
3363         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3364           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3365                                                                 Record[Idx++]));
3366           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3367                                               getRawEncoding());
3368         }
3369       }
3370       break;
3371 
3372     case PP_CONDITIONAL_STACK:
3373       if (!Record.empty()) {
3374         unsigned Idx = 0, End = Record.size() - 1;
3375         bool ReachedEOFWhileSkipping = Record[Idx++];
3376         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3377         if (ReachedEOFWhileSkipping) {
3378           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3379           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3380           bool FoundNonSkipPortion = Record[Idx++];
3381           bool FoundElse = Record[Idx++];
3382           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3383           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3384                            FoundElse, ElseLoc);
3385         }
3386         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3387         while (Idx < End) {
3388           auto Loc = ReadSourceLocation(F, Record, Idx);
3389           bool WasSkipping = Record[Idx++];
3390           bool FoundNonSkip = Record[Idx++];
3391           bool FoundElse = Record[Idx++];
3392           ConditionalStack.push_back(
3393               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3394         }
3395         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3396       }
3397       break;
3398 
3399     case PP_COUNTER_VALUE:
3400       if (!Record.empty() && Listener)
3401         Listener->ReadCounter(F, Record[0]);
3402       break;
3403 
3404     case FILE_SORTED_DECLS:
3405       F.FileSortedDecls = (const DeclID *)Blob.data();
3406       F.NumFileSortedDecls = Record[0];
3407       break;
3408 
3409     case SOURCE_LOCATION_OFFSETS: {
3410       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3411       F.LocalNumSLocEntries = Record[0];
3412       unsigned SLocSpaceSize = Record[1];
3413       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3414       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3415           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3416                                               SLocSpaceSize);
3417       if (!F.SLocEntryBaseID) {
3418         Error("ran out of source locations");
3419         break;
3420       }
3421       // Make our entry in the range map. BaseID is negative and growing, so
3422       // we invert it. Because we invert it, though, we need the other end of
3423       // the range.
3424       unsigned RangeStart =
3425           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3426       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3427       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3428 
3429       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3430       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3431       GlobalSLocOffsetMap.insert(
3432           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3433                            - SLocSpaceSize,&F));
3434 
3435       // Initialize the remapping table.
3436       // Invalid stays invalid.
3437       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3438       // This module. Base was 2 when being compiled.
3439       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3440                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3441 
3442       TotalNumSLocEntries += F.LocalNumSLocEntries;
3443       break;
3444     }
3445 
3446     case MODULE_OFFSET_MAP:
3447       F.ModuleOffsetMap = Blob;
3448       break;
3449 
3450     case SOURCE_MANAGER_LINE_TABLE:
3451       if (ParseLineTable(F, Record)) {
3452         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3453         return Failure;
3454       }
3455       break;
3456 
3457     case SOURCE_LOCATION_PRELOADS: {
3458       // Need to transform from the local view (1-based IDs) to the global view,
3459       // which is based off F.SLocEntryBaseID.
3460       if (!F.PreloadSLocEntries.empty()) {
3461         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3462         return Failure;
3463       }
3464 
3465       F.PreloadSLocEntries.swap(Record);
3466       break;
3467     }
3468 
3469     case EXT_VECTOR_DECLS:
3470       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3471         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3472       break;
3473 
3474     case VTABLE_USES:
3475       if (Record.size() % 3 != 0) {
3476         Error("Invalid VTABLE_USES record");
3477         return Failure;
3478       }
3479 
3480       // Later tables overwrite earlier ones.
3481       // FIXME: Modules will have some trouble with this. This is clearly not
3482       // the right way to do this.
3483       VTableUses.clear();
3484 
3485       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3486         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3487         VTableUses.push_back(
3488           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3489         VTableUses.push_back(Record[Idx++]);
3490       }
3491       break;
3492 
3493     case PENDING_IMPLICIT_INSTANTIATIONS:
3494       if (PendingInstantiations.size() % 2 != 0) {
3495         Error("Invalid existing PendingInstantiations");
3496         return Failure;
3497       }
3498 
3499       if (Record.size() % 2 != 0) {
3500         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3501         return Failure;
3502       }
3503 
3504       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3505         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3506         PendingInstantiations.push_back(
3507           ReadSourceLocation(F, Record, I).getRawEncoding());
3508       }
3509       break;
3510 
3511     case SEMA_DECL_REFS:
3512       if (Record.size() != 3) {
3513         Error("Invalid SEMA_DECL_REFS block");
3514         return Failure;
3515       }
3516       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3517         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3518       break;
3519 
3520     case PPD_ENTITIES_OFFSETS: {
3521       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3522       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3523       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3524 
3525       unsigned LocalBasePreprocessedEntityID = Record[0];
3526 
3527       unsigned StartingID;
3528       if (!PP.getPreprocessingRecord())
3529         PP.createPreprocessingRecord();
3530       if (!PP.getPreprocessingRecord()->getExternalSource())
3531         PP.getPreprocessingRecord()->SetExternalSource(*this);
3532       StartingID
3533         = PP.getPreprocessingRecord()
3534             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3535       F.BasePreprocessedEntityID = StartingID;
3536 
3537       if (F.NumPreprocessedEntities > 0) {
3538         // Introduce the global -> local mapping for preprocessed entities in
3539         // this module.
3540         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3541 
3542         // Introduce the local -> global mapping for preprocessed entities in
3543         // this module.
3544         F.PreprocessedEntityRemap.insertOrReplace(
3545           std::make_pair(LocalBasePreprocessedEntityID,
3546             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3547       }
3548 
3549       break;
3550     }
3551 
3552     case PPD_SKIPPED_RANGES: {
3553       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3554       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3555       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3556 
3557       if (!PP.getPreprocessingRecord())
3558         PP.createPreprocessingRecord();
3559       if (!PP.getPreprocessingRecord()->getExternalSource())
3560         PP.getPreprocessingRecord()->SetExternalSource(*this);
3561       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3562           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3563 
3564       if (F.NumPreprocessedSkippedRanges > 0)
3565         GlobalSkippedRangeMap.insert(
3566             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3567       break;
3568     }
3569 
3570     case DECL_UPDATE_OFFSETS:
3571       if (Record.size() % 2 != 0) {
3572         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3573         return Failure;
3574       }
3575       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3576         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3577         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3578 
3579         // If we've already loaded the decl, perform the updates when we finish
3580         // loading this block.
3581         if (Decl *D = GetExistingDecl(ID))
3582           PendingUpdateRecords.push_back(
3583               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3584       }
3585       break;
3586 
3587     case OBJC_CATEGORIES_MAP:
3588       if (F.LocalNumObjCCategoriesInMap != 0) {
3589         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3590         return Failure;
3591       }
3592 
3593       F.LocalNumObjCCategoriesInMap = Record[0];
3594       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3595       break;
3596 
3597     case OBJC_CATEGORIES:
3598       F.ObjCCategories.swap(Record);
3599       break;
3600 
3601     case CUDA_SPECIAL_DECL_REFS:
3602       // Later tables overwrite earlier ones.
3603       // FIXME: Modules will have trouble with this.
3604       CUDASpecialDeclRefs.clear();
3605       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3606         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3607       break;
3608 
3609     case HEADER_SEARCH_TABLE:
3610       F.HeaderFileInfoTableData = Blob.data();
3611       F.LocalNumHeaderFileInfos = Record[1];
3612       if (Record[0]) {
3613         F.HeaderFileInfoTable
3614           = HeaderFileInfoLookupTable::Create(
3615                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3616                    (const unsigned char *)F.HeaderFileInfoTableData,
3617                    HeaderFileInfoTrait(*this, F,
3618                                        &PP.getHeaderSearchInfo(),
3619                                        Blob.data() + Record[2]));
3620 
3621         PP.getHeaderSearchInfo().SetExternalSource(this);
3622         if (!PP.getHeaderSearchInfo().getExternalLookup())
3623           PP.getHeaderSearchInfo().SetExternalLookup(this);
3624       }
3625       break;
3626 
3627     case FP_PRAGMA_OPTIONS:
3628       // Later tables overwrite earlier ones.
3629       FPPragmaOptions.swap(Record);
3630       break;
3631 
3632     case OPENCL_EXTENSIONS:
3633       for (unsigned I = 0, E = Record.size(); I != E; ) {
3634         auto Name = ReadString(Record, I);
3635         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3636         OptInfo.Supported = Record[I++] != 0;
3637         OptInfo.Enabled = Record[I++] != 0;
3638         OptInfo.WithPragma = Record[I++] != 0;
3639         OptInfo.Avail = Record[I++];
3640         OptInfo.Core = Record[I++];
3641         OptInfo.Opt = Record[I++];
3642       }
3643       break;
3644 
3645     case TENTATIVE_DEFINITIONS:
3646       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3647         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3648       break;
3649 
3650     case KNOWN_NAMESPACES:
3651       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3652         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3653       break;
3654 
3655     case UNDEFINED_BUT_USED:
3656       if (UndefinedButUsed.size() % 2 != 0) {
3657         Error("Invalid existing UndefinedButUsed");
3658         return Failure;
3659       }
3660 
3661       if (Record.size() % 2 != 0) {
3662         Error("invalid undefined-but-used record");
3663         return Failure;
3664       }
3665       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3666         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3667         UndefinedButUsed.push_back(
3668             ReadSourceLocation(F, Record, I).getRawEncoding());
3669       }
3670       break;
3671 
3672     case DELETE_EXPRS_TO_ANALYZE:
3673       for (unsigned I = 0, N = Record.size(); I != N;) {
3674         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3675         const uint64_t Count = Record[I++];
3676         DelayedDeleteExprs.push_back(Count);
3677         for (uint64_t C = 0; C < Count; ++C) {
3678           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3679           bool IsArrayForm = Record[I++] == 1;
3680           DelayedDeleteExprs.push_back(IsArrayForm);
3681         }
3682       }
3683       break;
3684 
3685     case IMPORTED_MODULES:
3686       if (!F.isModule()) {
3687         // If we aren't loading a module (which has its own exports), make
3688         // all of the imported modules visible.
3689         // FIXME: Deal with macros-only imports.
3690         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3691           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3692           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3693           if (GlobalID) {
3694             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3695             if (DeserializationListener)
3696               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3697           }
3698         }
3699       }
3700       break;
3701 
3702     case MACRO_OFFSET: {
3703       if (F.LocalNumMacros != 0) {
3704         Error("duplicate MACRO_OFFSET record in AST file");
3705         return Failure;
3706       }
3707       F.MacroOffsets = (const uint32_t *)Blob.data();
3708       F.LocalNumMacros = Record[0];
3709       unsigned LocalBaseMacroID = Record[1];
3710       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3711       F.BaseMacroID = getTotalNumMacros();
3712 
3713       if (F.LocalNumMacros > 0) {
3714         // Introduce the global -> local mapping for macros within this module.
3715         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3716 
3717         // Introduce the local -> global mapping for macros within this module.
3718         F.MacroRemap.insertOrReplace(
3719           std::make_pair(LocalBaseMacroID,
3720                          F.BaseMacroID - LocalBaseMacroID));
3721 
3722         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3723       }
3724       break;
3725     }
3726 
3727     case LATE_PARSED_TEMPLATE:
3728       LateParsedTemplates.emplace_back(
3729           std::piecewise_construct, std::forward_as_tuple(&F),
3730           std::forward_as_tuple(Record.begin(), Record.end()));
3731       break;
3732 
3733     case OPTIMIZE_PRAGMA_OPTIONS:
3734       if (Record.size() != 1) {
3735         Error("invalid pragma optimize record");
3736         return Failure;
3737       }
3738       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3739       break;
3740 
3741     case MSSTRUCT_PRAGMA_OPTIONS:
3742       if (Record.size() != 1) {
3743         Error("invalid pragma ms_struct record");
3744         return Failure;
3745       }
3746       PragmaMSStructState = Record[0];
3747       break;
3748 
3749     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3750       if (Record.size() != 2) {
3751         Error("invalid pragma ms_struct record");
3752         return Failure;
3753       }
3754       PragmaMSPointersToMembersState = Record[0];
3755       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3756       break;
3757 
3758     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3759       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3760         UnusedLocalTypedefNameCandidates.push_back(
3761             getGlobalDeclID(F, Record[I]));
3762       break;
3763 
3764     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3765       if (Record.size() != 1) {
3766         Error("invalid cuda pragma options record");
3767         return Failure;
3768       }
3769       ForceCUDAHostDeviceDepth = Record[0];
3770       break;
3771 
3772     case ALIGN_PACK_PRAGMA_OPTIONS: {
3773       if (Record.size() < 3) {
3774         Error("invalid pragma pack record");
3775         return Failure;
3776       }
3777       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3778       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3779       unsigned NumStackEntries = Record[2];
3780       unsigned Idx = 3;
3781       // Reset the stack when importing a new module.
3782       PragmaAlignPackStack.clear();
3783       for (unsigned I = 0; I < NumStackEntries; ++I) {
3784         PragmaAlignPackStackEntry Entry;
3785         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3786         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3787         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3788         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3789         Entry.SlotLabel = PragmaAlignPackStrings.back();
3790         PragmaAlignPackStack.push_back(Entry);
3791       }
3792       break;
3793     }
3794 
3795     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3796       if (Record.size() < 3) {
3797         Error("invalid pragma pack record");
3798         return Failure;
3799       }
3800       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3801       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3802       unsigned NumStackEntries = Record[2];
3803       unsigned Idx = 3;
3804       // Reset the stack when importing a new module.
3805       FpPragmaStack.clear();
3806       for (unsigned I = 0; I < NumStackEntries; ++I) {
3807         FpPragmaStackEntry Entry;
3808         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3809         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3810         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3811         FpPragmaStrings.push_back(ReadString(Record, Idx));
3812         Entry.SlotLabel = FpPragmaStrings.back();
3813         FpPragmaStack.push_back(Entry);
3814       }
3815       break;
3816     }
3817 
3818     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3819       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3820         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3821       break;
3822     }
3823   }
3824 }
3825 
3826 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3827   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3828 
3829   // Additional remapping information.
3830   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3831   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3832   F.ModuleOffsetMap = StringRef();
3833 
3834   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3835   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3836     F.SLocRemap.insert(std::make_pair(0U, 0));
3837     F.SLocRemap.insert(std::make_pair(2U, 1));
3838   }
3839 
3840   // Continuous range maps we may be updating in our module.
3841   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3842   RemapBuilder SLocRemap(F.SLocRemap);
3843   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3844   RemapBuilder MacroRemap(F.MacroRemap);
3845   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3846   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3847   RemapBuilder SelectorRemap(F.SelectorRemap);
3848   RemapBuilder DeclRemap(F.DeclRemap);
3849   RemapBuilder TypeRemap(F.TypeRemap);
3850 
3851   while (Data < DataEnd) {
3852     // FIXME: Looking up dependency modules by filename is horrible. Let's
3853     // start fixing this with prebuilt, explicit and implicit modules and see
3854     // how it goes...
3855     using namespace llvm::support;
3856     ModuleKind Kind = static_cast<ModuleKind>(
3857       endian::readNext<uint8_t, little, unaligned>(Data));
3858     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3859     StringRef Name = StringRef((const char*)Data, Len);
3860     Data += Len;
3861     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3862                               Kind == MK_ImplicitModule
3863                           ? ModuleMgr.lookupByModuleName(Name)
3864                           : ModuleMgr.lookupByFileName(Name));
3865     if (!OM) {
3866       std::string Msg =
3867           "SourceLocation remap refers to unknown module, cannot find ";
3868       Msg.append(std::string(Name));
3869       Error(Msg);
3870       return;
3871     }
3872 
3873     uint32_t SLocOffset =
3874         endian::readNext<uint32_t, little, unaligned>(Data);
3875     uint32_t IdentifierIDOffset =
3876         endian::readNext<uint32_t, little, unaligned>(Data);
3877     uint32_t MacroIDOffset =
3878         endian::readNext<uint32_t, little, unaligned>(Data);
3879     uint32_t PreprocessedEntityIDOffset =
3880         endian::readNext<uint32_t, little, unaligned>(Data);
3881     uint32_t SubmoduleIDOffset =
3882         endian::readNext<uint32_t, little, unaligned>(Data);
3883     uint32_t SelectorIDOffset =
3884         endian::readNext<uint32_t, little, unaligned>(Data);
3885     uint32_t DeclIDOffset =
3886         endian::readNext<uint32_t, little, unaligned>(Data);
3887     uint32_t TypeIndexOffset =
3888         endian::readNext<uint32_t, little, unaligned>(Data);
3889 
3890     uint32_t None = std::numeric_limits<uint32_t>::max();
3891 
3892     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3893                          RemapBuilder &Remap) {
3894       if (Offset != None)
3895         Remap.insert(std::make_pair(Offset,
3896                                     static_cast<int>(BaseOffset - Offset)));
3897     };
3898     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3899     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3900     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3901     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3902               PreprocessedEntityRemap);
3903     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3904     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3905     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3906     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3907 
3908     // Global -> local mappings.
3909     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3910   }
3911 }
3912 
3913 ASTReader::ASTReadResult
3914 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3915                                   const ModuleFile *ImportedBy,
3916                                   unsigned ClientLoadCapabilities) {
3917   unsigned Idx = 0;
3918   F.ModuleMapPath = ReadPath(F, Record, Idx);
3919 
3920   // Try to resolve ModuleName in the current header search context and
3921   // verify that it is found in the same module map file as we saved. If the
3922   // top-level AST file is a main file, skip this check because there is no
3923   // usable header search context.
3924   assert(!F.ModuleName.empty() &&
3925          "MODULE_NAME should come before MODULE_MAP_FILE");
3926   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3927     // An implicitly-loaded module file should have its module listed in some
3928     // module map file that we've already loaded.
3929     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3930     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3931     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3932     // Don't emit module relocation error if we have -fno-validate-pch
3933     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3934               DisableValidationForModuleKind::Module) &&
3935         !ModMap) {
3936       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3937         if (auto ASTFE = M ? M->getASTFile() : None) {
3938           // This module was defined by an imported (explicit) module.
3939           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3940                                                << ASTFE->getName();
3941         } else {
3942           // This module was built with a different module map.
3943           Diag(diag::err_imported_module_not_found)
3944               << F.ModuleName << F.FileName
3945               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3946               << !ImportedBy;
3947           // In case it was imported by a PCH, there's a chance the user is
3948           // just missing to include the search path to the directory containing
3949           // the modulemap.
3950           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3951             Diag(diag::note_imported_by_pch_module_not_found)
3952                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3953         }
3954       }
3955       return OutOfDate;
3956     }
3957 
3958     assert(M && M->Name == F.ModuleName && "found module with different name");
3959 
3960     // Check the primary module map file.
3961     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3962     if (!StoredModMap || *StoredModMap != ModMap) {
3963       assert(ModMap && "found module is missing module map file");
3964       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3965              "top-level import should be verified");
3966       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3967       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3968         Diag(diag::err_imported_module_modmap_changed)
3969             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3970             << ModMap->getName() << F.ModuleMapPath << NotImported;
3971       return OutOfDate;
3972     }
3973 
3974     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3975     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3976       // FIXME: we should use input files rather than storing names.
3977       std::string Filename = ReadPath(F, Record, Idx);
3978       auto SF = FileMgr.getFile(Filename, false, false);
3979       if (!SF) {
3980         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3981           Error("could not find file '" + Filename +"' referenced by AST file");
3982         return OutOfDate;
3983       }
3984       AdditionalStoredMaps.insert(*SF);
3985     }
3986 
3987     // Check any additional module map files (e.g. module.private.modulemap)
3988     // that are not in the pcm.
3989     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3990       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3991         // Remove files that match
3992         // Note: SmallPtrSet::erase is really remove
3993         if (!AdditionalStoredMaps.erase(ModMap)) {
3994           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3995             Diag(diag::err_module_different_modmap)
3996               << F.ModuleName << /*new*/0 << ModMap->getName();
3997           return OutOfDate;
3998         }
3999       }
4000     }
4001 
4002     // Check any additional module map files that are in the pcm, but not
4003     // found in header search. Cases that match are already removed.
4004     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4005       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4006         Diag(diag::err_module_different_modmap)
4007           << F.ModuleName << /*not new*/1 << ModMap->getName();
4008       return OutOfDate;
4009     }
4010   }
4011 
4012   if (Listener)
4013     Listener->ReadModuleMapFile(F.ModuleMapPath);
4014   return Success;
4015 }
4016 
4017 /// Move the given method to the back of the global list of methods.
4018 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4019   // Find the entry for this selector in the method pool.
4020   Sema::GlobalMethodPool::iterator Known
4021     = S.MethodPool.find(Method->getSelector());
4022   if (Known == S.MethodPool.end())
4023     return;
4024 
4025   // Retrieve the appropriate method list.
4026   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4027                                                     : Known->second.second;
4028   bool Found = false;
4029   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4030     if (!Found) {
4031       if (List->getMethod() == Method) {
4032         Found = true;
4033       } else {
4034         // Keep searching.
4035         continue;
4036       }
4037     }
4038 
4039     if (List->getNext())
4040       List->setMethod(List->getNext()->getMethod());
4041     else
4042       List->setMethod(Method);
4043   }
4044 }
4045 
4046 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4047   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4048   for (Decl *D : Names) {
4049     bool wasHidden = !D->isUnconditionallyVisible();
4050     D->setVisibleDespiteOwningModule();
4051 
4052     if (wasHidden && SemaObj) {
4053       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4054         moveMethodToBackOfGlobalList(*SemaObj, Method);
4055       }
4056     }
4057   }
4058 }
4059 
4060 void ASTReader::makeModuleVisible(Module *Mod,
4061                                   Module::NameVisibilityKind NameVisibility,
4062                                   SourceLocation ImportLoc) {
4063   llvm::SmallPtrSet<Module *, 4> Visited;
4064   SmallVector<Module *, 4> Stack;
4065   Stack.push_back(Mod);
4066   while (!Stack.empty()) {
4067     Mod = Stack.pop_back_val();
4068 
4069     if (NameVisibility <= Mod->NameVisibility) {
4070       // This module already has this level of visibility (or greater), so
4071       // there is nothing more to do.
4072       continue;
4073     }
4074 
4075     if (Mod->isUnimportable()) {
4076       // Modules that aren't importable cannot be made visible.
4077       continue;
4078     }
4079 
4080     // Update the module's name visibility.
4081     Mod->NameVisibility = NameVisibility;
4082 
4083     // If we've already deserialized any names from this module,
4084     // mark them as visible.
4085     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4086     if (Hidden != HiddenNamesMap.end()) {
4087       auto HiddenNames = std::move(*Hidden);
4088       HiddenNamesMap.erase(Hidden);
4089       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4090       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4091              "making names visible added hidden names");
4092     }
4093 
4094     // Push any exported modules onto the stack to be marked as visible.
4095     SmallVector<Module *, 16> Exports;
4096     Mod->getExportedModules(Exports);
4097     for (SmallVectorImpl<Module *>::iterator
4098            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4099       Module *Exported = *I;
4100       if (Visited.insert(Exported).second)
4101         Stack.push_back(Exported);
4102     }
4103   }
4104 }
4105 
4106 /// We've merged the definition \p MergedDef into the existing definition
4107 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4108 /// visible.
4109 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4110                                           NamedDecl *MergedDef) {
4111   if (!Def->isUnconditionallyVisible()) {
4112     // If MergedDef is visible or becomes visible, make the definition visible.
4113     if (MergedDef->isUnconditionallyVisible())
4114       Def->setVisibleDespiteOwningModule();
4115     else {
4116       getContext().mergeDefinitionIntoModule(
4117           Def, MergedDef->getImportedOwningModule(),
4118           /*NotifyListeners*/ false);
4119       PendingMergedDefinitionsToDeduplicate.insert(Def);
4120     }
4121   }
4122 }
4123 
4124 bool ASTReader::loadGlobalIndex() {
4125   if (GlobalIndex)
4126     return false;
4127 
4128   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4129       !PP.getLangOpts().Modules)
4130     return true;
4131 
4132   // Try to load the global index.
4133   TriedLoadingGlobalIndex = true;
4134   StringRef ModuleCachePath
4135     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4136   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4137       GlobalModuleIndex::readIndex(ModuleCachePath);
4138   if (llvm::Error Err = std::move(Result.second)) {
4139     assert(!Result.first);
4140     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4141     return true;
4142   }
4143 
4144   GlobalIndex.reset(Result.first);
4145   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4146   return false;
4147 }
4148 
4149 bool ASTReader::isGlobalIndexUnavailable() const {
4150   return PP.getLangOpts().Modules && UseGlobalIndex &&
4151          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4152 }
4153 
4154 static void updateModuleTimestamp(ModuleFile &MF) {
4155   // Overwrite the timestamp file contents so that file's mtime changes.
4156   std::string TimestampFilename = MF.getTimestampFilename();
4157   std::error_code EC;
4158   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4159                           llvm::sys::fs::OF_TextWithCRLF);
4160   if (EC)
4161     return;
4162   OS << "Timestamp file\n";
4163   OS.close();
4164   OS.clear_error(); // Avoid triggering a fatal error.
4165 }
4166 
4167 /// Given a cursor at the start of an AST file, scan ahead and drop the
4168 /// cursor into the start of the given block ID, returning false on success and
4169 /// true on failure.
4170 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4171   while (true) {
4172     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4173     if (!MaybeEntry) {
4174       // FIXME this drops errors on the floor.
4175       consumeError(MaybeEntry.takeError());
4176       return true;
4177     }
4178     llvm::BitstreamEntry Entry = MaybeEntry.get();
4179 
4180     switch (Entry.Kind) {
4181     case llvm::BitstreamEntry::Error:
4182     case llvm::BitstreamEntry::EndBlock:
4183       return true;
4184 
4185     case llvm::BitstreamEntry::Record:
4186       // Ignore top-level records.
4187       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4188         break;
4189       else {
4190         // FIXME this drops errors on the floor.
4191         consumeError(Skipped.takeError());
4192         return true;
4193       }
4194 
4195     case llvm::BitstreamEntry::SubBlock:
4196       if (Entry.ID == BlockID) {
4197         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4198           // FIXME this drops the error on the floor.
4199           consumeError(std::move(Err));
4200           return true;
4201         }
4202         // Found it!
4203         return false;
4204       }
4205 
4206       if (llvm::Error Err = Cursor.SkipBlock()) {
4207         // FIXME this drops the error on the floor.
4208         consumeError(std::move(Err));
4209         return true;
4210       }
4211     }
4212   }
4213 }
4214 
4215 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4216                                             ModuleKind Type,
4217                                             SourceLocation ImportLoc,
4218                                             unsigned ClientLoadCapabilities,
4219                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4220   llvm::SaveAndRestore<SourceLocation>
4221     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4222   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4223       CurrentDeserializingModuleKind, Type);
4224 
4225   // Defer any pending actions until we get to the end of reading the AST file.
4226   Deserializing AnASTFile(this);
4227 
4228   // Bump the generation number.
4229   unsigned PreviousGeneration = 0;
4230   if (ContextObj)
4231     PreviousGeneration = incrementGeneration(*ContextObj);
4232 
4233   unsigned NumModules = ModuleMgr.size();
4234   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4235     assert(ReadResult && "expected to return error");
4236     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4237                             PP.getLangOpts().Modules
4238                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4239                                 : nullptr);
4240 
4241     // If we find that any modules are unusable, the global index is going
4242     // to be out-of-date. Just remove it.
4243     GlobalIndex.reset();
4244     ModuleMgr.setGlobalIndex(nullptr);
4245     return ReadResult;
4246   };
4247 
4248   SmallVector<ImportedModule, 4> Loaded;
4249   switch (ASTReadResult ReadResult =
4250               ReadASTCore(FileName, Type, ImportLoc,
4251                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4252                           ASTFileSignature(), ClientLoadCapabilities)) {
4253   case Failure:
4254   case Missing:
4255   case OutOfDate:
4256   case VersionMismatch:
4257   case ConfigurationMismatch:
4258   case HadErrors:
4259     return removeModulesAndReturn(ReadResult);
4260   case Success:
4261     break;
4262   }
4263 
4264   // Here comes stuff that we only do once the entire chain is loaded.
4265 
4266   // Load the AST blocks of all of the modules that we loaded.  We can still
4267   // hit errors parsing the ASTs at this point.
4268   for (ImportedModule &M : Loaded) {
4269     ModuleFile &F = *M.Mod;
4270 
4271     // Read the AST block.
4272     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4273       return removeModulesAndReturn(Result);
4274 
4275     // The AST block should always have a definition for the main module.
4276     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4277       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4278       return removeModulesAndReturn(Failure);
4279     }
4280 
4281     // Read the extension blocks.
4282     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4283       if (ASTReadResult Result = ReadExtensionBlock(F))
4284         return removeModulesAndReturn(Result);
4285     }
4286 
4287     // Once read, set the ModuleFile bit base offset and update the size in
4288     // bits of all files we've seen.
4289     F.GlobalBitOffset = TotalModulesSizeInBits;
4290     TotalModulesSizeInBits += F.SizeInBits;
4291     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4292   }
4293 
4294   // Preload source locations and interesting indentifiers.
4295   for (ImportedModule &M : Loaded) {
4296     ModuleFile &F = *M.Mod;
4297 
4298     // Preload SLocEntries.
4299     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4300       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4301       // Load it through the SourceManager and don't call ReadSLocEntry()
4302       // directly because the entry may have already been loaded in which case
4303       // calling ReadSLocEntry() directly would trigger an assertion in
4304       // SourceManager.
4305       SourceMgr.getLoadedSLocEntryByID(Index);
4306     }
4307 
4308     // Map the original source file ID into the ID space of the current
4309     // compilation.
4310     if (F.OriginalSourceFileID.isValid()) {
4311       F.OriginalSourceFileID = FileID::get(
4312           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4313     }
4314 
4315     // Preload all the pending interesting identifiers by marking them out of
4316     // date.
4317     for (auto Offset : F.PreloadIdentifierOffsets) {
4318       const unsigned char *Data = F.IdentifierTableData + Offset;
4319 
4320       ASTIdentifierLookupTrait Trait(*this, F);
4321       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4322       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4323       auto &II = PP.getIdentifierTable().getOwn(Key);
4324       II.setOutOfDate(true);
4325 
4326       // Mark this identifier as being from an AST file so that we can track
4327       // whether we need to serialize it.
4328       markIdentifierFromAST(*this, II);
4329 
4330       // Associate the ID with the identifier so that the writer can reuse it.
4331       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4332       SetIdentifierInfo(ID, &II);
4333     }
4334   }
4335 
4336   // Setup the import locations and notify the module manager that we've
4337   // committed to these module files.
4338   for (ImportedModule &M : Loaded) {
4339     ModuleFile &F = *M.Mod;
4340 
4341     ModuleMgr.moduleFileAccepted(&F);
4342 
4343     // Set the import location.
4344     F.DirectImportLoc = ImportLoc;
4345     // FIXME: We assume that locations from PCH / preamble do not need
4346     // any translation.
4347     if (!M.ImportedBy)
4348       F.ImportLoc = M.ImportLoc;
4349     else
4350       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4351   }
4352 
4353   if (!PP.getLangOpts().CPlusPlus ||
4354       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4355        Type != MK_PrebuiltModule)) {
4356     // Mark all of the identifiers in the identifier table as being out of date,
4357     // so that various accessors know to check the loaded modules when the
4358     // identifier is used.
4359     //
4360     // For C++ modules, we don't need information on many identifiers (just
4361     // those that provide macros or are poisoned), so we mark all of
4362     // the interesting ones via PreloadIdentifierOffsets.
4363     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4364                                 IdEnd = PP.getIdentifierTable().end();
4365          Id != IdEnd; ++Id)
4366       Id->second->setOutOfDate(true);
4367   }
4368   // Mark selectors as out of date.
4369   for (auto Sel : SelectorGeneration)
4370     SelectorOutOfDate[Sel.first] = true;
4371 
4372   // Resolve any unresolved module exports.
4373   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4374     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4375     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4376     Module *ResolvedMod = getSubmodule(GlobalID);
4377 
4378     switch (Unresolved.Kind) {
4379     case UnresolvedModuleRef::Conflict:
4380       if (ResolvedMod) {
4381         Module::Conflict Conflict;
4382         Conflict.Other = ResolvedMod;
4383         Conflict.Message = Unresolved.String.str();
4384         Unresolved.Mod->Conflicts.push_back(Conflict);
4385       }
4386       continue;
4387 
4388     case UnresolvedModuleRef::Import:
4389       if (ResolvedMod)
4390         Unresolved.Mod->Imports.insert(ResolvedMod);
4391       continue;
4392 
4393     case UnresolvedModuleRef::Export:
4394       if (ResolvedMod || Unresolved.IsWildcard)
4395         Unresolved.Mod->Exports.push_back(
4396           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4397       continue;
4398     }
4399   }
4400   UnresolvedModuleRefs.clear();
4401 
4402   if (Imported)
4403     Imported->append(ImportedModules.begin(),
4404                      ImportedModules.end());
4405 
4406   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4407   // Might be unnecessary as use declarations are only used to build the
4408   // module itself.
4409 
4410   if (ContextObj)
4411     InitializeContext();
4412 
4413   if (SemaObj)
4414     UpdateSema();
4415 
4416   if (DeserializationListener)
4417     DeserializationListener->ReaderInitialized(this);
4418 
4419   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4420   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4421     // If this AST file is a precompiled preamble, then set the
4422     // preamble file ID of the source manager to the file source file
4423     // from which the preamble was built.
4424     if (Type == MK_Preamble) {
4425       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4426     } else if (Type == MK_MainFile) {
4427       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4428     }
4429   }
4430 
4431   // For any Objective-C class definitions we have already loaded, make sure
4432   // that we load any additional categories.
4433   if (ContextObj) {
4434     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4435       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4436                          ObjCClassesLoaded[I],
4437                          PreviousGeneration);
4438     }
4439   }
4440 
4441   if (PP.getHeaderSearchInfo()
4442           .getHeaderSearchOpts()
4443           .ModulesValidateOncePerBuildSession) {
4444     // Now we are certain that the module and all modules it depends on are
4445     // up to date.  Create or update timestamp files for modules that are
4446     // located in the module cache (not for PCH files that could be anywhere
4447     // in the filesystem).
4448     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4449       ImportedModule &M = Loaded[I];
4450       if (M.Mod->Kind == MK_ImplicitModule) {
4451         updateModuleTimestamp(*M.Mod);
4452       }
4453     }
4454   }
4455 
4456   return Success;
4457 }
4458 
4459 static ASTFileSignature readASTFileSignature(StringRef PCH);
4460 
4461 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4462 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4463   // FIXME checking magic headers is done in other places such as
4464   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4465   // always done the same. Unify it all with a helper.
4466   if (!Stream.canSkipToPos(4))
4467     return llvm::createStringError(std::errc::illegal_byte_sequence,
4468                                    "file too small to contain AST file magic");
4469   for (unsigned C : {'C', 'P', 'C', 'H'})
4470     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4471       if (Res.get() != C)
4472         return llvm::createStringError(
4473             std::errc::illegal_byte_sequence,
4474             "file doesn't start with AST file magic");
4475     } else
4476       return Res.takeError();
4477   return llvm::Error::success();
4478 }
4479 
4480 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4481   switch (Kind) {
4482   case MK_PCH:
4483     return 0; // PCH
4484   case MK_ImplicitModule:
4485   case MK_ExplicitModule:
4486   case MK_PrebuiltModule:
4487     return 1; // module
4488   case MK_MainFile:
4489   case MK_Preamble:
4490     return 2; // main source file
4491   }
4492   llvm_unreachable("unknown module kind");
4493 }
4494 
4495 ASTReader::ASTReadResult
4496 ASTReader::ReadASTCore(StringRef FileName,
4497                        ModuleKind Type,
4498                        SourceLocation ImportLoc,
4499                        ModuleFile *ImportedBy,
4500                        SmallVectorImpl<ImportedModule> &Loaded,
4501                        off_t ExpectedSize, time_t ExpectedModTime,
4502                        ASTFileSignature ExpectedSignature,
4503                        unsigned ClientLoadCapabilities) {
4504   ModuleFile *M;
4505   std::string ErrorStr;
4506   ModuleManager::AddModuleResult AddResult
4507     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4508                           getGeneration(), ExpectedSize, ExpectedModTime,
4509                           ExpectedSignature, readASTFileSignature,
4510                           M, ErrorStr);
4511 
4512   switch (AddResult) {
4513   case ModuleManager::AlreadyLoaded:
4514     Diag(diag::remark_module_import)
4515         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4516         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4517     return Success;
4518 
4519   case ModuleManager::NewlyLoaded:
4520     // Load module file below.
4521     break;
4522 
4523   case ModuleManager::Missing:
4524     // The module file was missing; if the client can handle that, return
4525     // it.
4526     if (ClientLoadCapabilities & ARR_Missing)
4527       return Missing;
4528 
4529     // Otherwise, return an error.
4530     Diag(diag::err_ast_file_not_found)
4531         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4532         << ErrorStr;
4533     return Failure;
4534 
4535   case ModuleManager::OutOfDate:
4536     // We couldn't load the module file because it is out-of-date. If the
4537     // client can handle out-of-date, return it.
4538     if (ClientLoadCapabilities & ARR_OutOfDate)
4539       return OutOfDate;
4540 
4541     // Otherwise, return an error.
4542     Diag(diag::err_ast_file_out_of_date)
4543         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4544         << ErrorStr;
4545     return Failure;
4546   }
4547 
4548   assert(M && "Missing module file");
4549 
4550   bool ShouldFinalizePCM = false;
4551   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4552     auto &MC = getModuleManager().getModuleCache();
4553     if (ShouldFinalizePCM)
4554       MC.finalizePCM(FileName);
4555     else
4556       MC.tryToDropPCM(FileName);
4557   });
4558   ModuleFile &F = *M;
4559   BitstreamCursor &Stream = F.Stream;
4560   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4561   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4562 
4563   // Sniff for the signature.
4564   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4565     Diag(diag::err_ast_file_invalid)
4566         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4567     return Failure;
4568   }
4569 
4570   // This is used for compatibility with older PCH formats.
4571   bool HaveReadControlBlock = false;
4572   while (true) {
4573     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4574     if (!MaybeEntry) {
4575       Error(MaybeEntry.takeError());
4576       return Failure;
4577     }
4578     llvm::BitstreamEntry Entry = MaybeEntry.get();
4579 
4580     switch (Entry.Kind) {
4581     case llvm::BitstreamEntry::Error:
4582     case llvm::BitstreamEntry::Record:
4583     case llvm::BitstreamEntry::EndBlock:
4584       Error("invalid record at top-level of AST file");
4585       return Failure;
4586 
4587     case llvm::BitstreamEntry::SubBlock:
4588       break;
4589     }
4590 
4591     switch (Entry.ID) {
4592     case CONTROL_BLOCK_ID:
4593       HaveReadControlBlock = true;
4594       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4595       case Success:
4596         // Check that we didn't try to load a non-module AST file as a module.
4597         //
4598         // FIXME: Should we also perform the converse check? Loading a module as
4599         // a PCH file sort of works, but it's a bit wonky.
4600         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4601              Type == MK_PrebuiltModule) &&
4602             F.ModuleName.empty()) {
4603           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4604           if (Result != OutOfDate ||
4605               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4606             Diag(diag::err_module_file_not_module) << FileName;
4607           return Result;
4608         }
4609         break;
4610 
4611       case Failure: return Failure;
4612       case Missing: return Missing;
4613       case OutOfDate: return OutOfDate;
4614       case VersionMismatch: return VersionMismatch;
4615       case ConfigurationMismatch: return ConfigurationMismatch;
4616       case HadErrors: return HadErrors;
4617       }
4618       break;
4619 
4620     case AST_BLOCK_ID:
4621       if (!HaveReadControlBlock) {
4622         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4623           Diag(diag::err_pch_version_too_old);
4624         return VersionMismatch;
4625       }
4626 
4627       // Record that we've loaded this module.
4628       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4629       ShouldFinalizePCM = true;
4630       return Success;
4631 
4632     case UNHASHED_CONTROL_BLOCK_ID:
4633       // This block is handled using look-ahead during ReadControlBlock.  We
4634       // shouldn't get here!
4635       Error("malformed block record in AST file");
4636       return Failure;
4637 
4638     default:
4639       if (llvm::Error Err = Stream.SkipBlock()) {
4640         Error(std::move(Err));
4641         return Failure;
4642       }
4643       break;
4644     }
4645   }
4646 
4647   llvm_unreachable("unexpected break; expected return");
4648 }
4649 
4650 ASTReader::ASTReadResult
4651 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4652                                     unsigned ClientLoadCapabilities) {
4653   const HeaderSearchOptions &HSOpts =
4654       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4655   bool AllowCompatibleConfigurationMismatch =
4656       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4657   bool DisableValidation = shouldDisableValidationForFile(F);
4658 
4659   ASTReadResult Result = readUnhashedControlBlockImpl(
4660       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4661       Listener.get(),
4662       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4663 
4664   // If F was directly imported by another module, it's implicitly validated by
4665   // the importing module.
4666   if (DisableValidation || WasImportedBy ||
4667       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4668     return Success;
4669 
4670   if (Result == Failure) {
4671     Error("malformed block record in AST file");
4672     return Failure;
4673   }
4674 
4675   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4676     // If this module has already been finalized in the ModuleCache, we're stuck
4677     // with it; we can only load a single version of each module.
4678     //
4679     // This can happen when a module is imported in two contexts: in one, as a
4680     // user module; in another, as a system module (due to an import from
4681     // another module marked with the [system] flag).  It usually indicates a
4682     // bug in the module map: this module should also be marked with [system].
4683     //
4684     // If -Wno-system-headers (the default), and the first import is as a
4685     // system module, then validation will fail during the as-user import,
4686     // since -Werror flags won't have been validated.  However, it's reasonable
4687     // to treat this consistently as a system module.
4688     //
4689     // If -Wsystem-headers, the PCM on disk was built with
4690     // -Wno-system-headers, and the first import is as a user module, then
4691     // validation will fail during the as-system import since the PCM on disk
4692     // doesn't guarantee that -Werror was respected.  However, the -Werror
4693     // flags were checked during the initial as-user import.
4694     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4695       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4696       return Success;
4697     }
4698   }
4699 
4700   return Result;
4701 }
4702 
4703 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4704     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4705     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4706     bool ValidateDiagnosticOptions) {
4707   // Initialize a stream.
4708   BitstreamCursor Stream(StreamData);
4709 
4710   // Sniff for the signature.
4711   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4712     // FIXME this drops the error on the floor.
4713     consumeError(std::move(Err));
4714     return Failure;
4715   }
4716 
4717   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4718   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4719     return Failure;
4720 
4721   // Read all of the records in the options block.
4722   RecordData Record;
4723   ASTReadResult Result = Success;
4724   while (true) {
4725     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4726     if (!MaybeEntry) {
4727       // FIXME this drops the error on the floor.
4728       consumeError(MaybeEntry.takeError());
4729       return Failure;
4730     }
4731     llvm::BitstreamEntry Entry = MaybeEntry.get();
4732 
4733     switch (Entry.Kind) {
4734     case llvm::BitstreamEntry::Error:
4735     case llvm::BitstreamEntry::SubBlock:
4736       return Failure;
4737 
4738     case llvm::BitstreamEntry::EndBlock:
4739       return Result;
4740 
4741     case llvm::BitstreamEntry::Record:
4742       // The interesting case.
4743       break;
4744     }
4745 
4746     // Read and process a record.
4747     Record.clear();
4748     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4749     if (!MaybeRecordType) {
4750       // FIXME this drops the error.
4751       return Failure;
4752     }
4753     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4754     case SIGNATURE:
4755       if (F)
4756         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4757       break;
4758     case AST_BLOCK_HASH:
4759       if (F)
4760         F->ASTBlockHash =
4761             ASTFileSignature::create(Record.begin(), Record.end());
4762       break;
4763     case DIAGNOSTIC_OPTIONS: {
4764       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4765       if (Listener && ValidateDiagnosticOptions &&
4766           !AllowCompatibleConfigurationMismatch &&
4767           ParseDiagnosticOptions(Record, Complain, *Listener))
4768         Result = OutOfDate; // Don't return early.  Read the signature.
4769       break;
4770     }
4771     case DIAG_PRAGMA_MAPPINGS:
4772       if (!F)
4773         break;
4774       if (F->PragmaDiagMappings.empty())
4775         F->PragmaDiagMappings.swap(Record);
4776       else
4777         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4778                                      Record.begin(), Record.end());
4779       break;
4780     }
4781   }
4782 }
4783 
4784 /// Parse a record and blob containing module file extension metadata.
4785 static bool parseModuleFileExtensionMetadata(
4786               const SmallVectorImpl<uint64_t> &Record,
4787               StringRef Blob,
4788               ModuleFileExtensionMetadata &Metadata) {
4789   if (Record.size() < 4) return true;
4790 
4791   Metadata.MajorVersion = Record[0];
4792   Metadata.MinorVersion = Record[1];
4793 
4794   unsigned BlockNameLen = Record[2];
4795   unsigned UserInfoLen = Record[3];
4796 
4797   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4798 
4799   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4800   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4801                                   Blob.data() + BlockNameLen + UserInfoLen);
4802   return false;
4803 }
4804 
4805 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4806   BitstreamCursor &Stream = F.Stream;
4807 
4808   RecordData Record;
4809   while (true) {
4810     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4811     if (!MaybeEntry) {
4812       Error(MaybeEntry.takeError());
4813       return Failure;
4814     }
4815     llvm::BitstreamEntry Entry = MaybeEntry.get();
4816 
4817     switch (Entry.Kind) {
4818     case llvm::BitstreamEntry::SubBlock:
4819       if (llvm::Error Err = Stream.SkipBlock()) {
4820         Error(std::move(Err));
4821         return Failure;
4822       }
4823       continue;
4824 
4825     case llvm::BitstreamEntry::EndBlock:
4826       return Success;
4827 
4828     case llvm::BitstreamEntry::Error:
4829       return HadErrors;
4830 
4831     case llvm::BitstreamEntry::Record:
4832       break;
4833     }
4834 
4835     Record.clear();
4836     StringRef Blob;
4837     Expected<unsigned> MaybeRecCode =
4838         Stream.readRecord(Entry.ID, Record, &Blob);
4839     if (!MaybeRecCode) {
4840       Error(MaybeRecCode.takeError());
4841       return Failure;
4842     }
4843     switch (MaybeRecCode.get()) {
4844     case EXTENSION_METADATA: {
4845       ModuleFileExtensionMetadata Metadata;
4846       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4847         Error("malformed EXTENSION_METADATA in AST file");
4848         return Failure;
4849       }
4850 
4851       // Find a module file extension with this block name.
4852       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4853       if (Known == ModuleFileExtensions.end()) break;
4854 
4855       // Form a reader.
4856       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4857                                                              F, Stream)) {
4858         F.ExtensionReaders.push_back(std::move(Reader));
4859       }
4860 
4861       break;
4862     }
4863     }
4864   }
4865 
4866   return Success;
4867 }
4868 
4869 void ASTReader::InitializeContext() {
4870   assert(ContextObj && "no context to initialize");
4871   ASTContext &Context = *ContextObj;
4872 
4873   // If there's a listener, notify them that we "read" the translation unit.
4874   if (DeserializationListener)
4875     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4876                                       Context.getTranslationUnitDecl());
4877 
4878   // FIXME: Find a better way to deal with collisions between these
4879   // built-in types. Right now, we just ignore the problem.
4880 
4881   // Load the special types.
4882   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4883     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4884       if (!Context.CFConstantStringTypeDecl)
4885         Context.setCFConstantStringType(GetType(String));
4886     }
4887 
4888     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4889       QualType FileType = GetType(File);
4890       if (FileType.isNull()) {
4891         Error("FILE type is NULL");
4892         return;
4893       }
4894 
4895       if (!Context.FILEDecl) {
4896         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4897           Context.setFILEDecl(Typedef->getDecl());
4898         else {
4899           const TagType *Tag = FileType->getAs<TagType>();
4900           if (!Tag) {
4901             Error("Invalid FILE type in AST file");
4902             return;
4903           }
4904           Context.setFILEDecl(Tag->getDecl());
4905         }
4906       }
4907     }
4908 
4909     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4910       QualType Jmp_bufType = GetType(Jmp_buf);
4911       if (Jmp_bufType.isNull()) {
4912         Error("jmp_buf type is NULL");
4913         return;
4914       }
4915 
4916       if (!Context.jmp_bufDecl) {
4917         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4918           Context.setjmp_bufDecl(Typedef->getDecl());
4919         else {
4920           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4921           if (!Tag) {
4922             Error("Invalid jmp_buf type in AST file");
4923             return;
4924           }
4925           Context.setjmp_bufDecl(Tag->getDecl());
4926         }
4927       }
4928     }
4929 
4930     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4931       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4932       if (Sigjmp_bufType.isNull()) {
4933         Error("sigjmp_buf type is NULL");
4934         return;
4935       }
4936 
4937       if (!Context.sigjmp_bufDecl) {
4938         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4939           Context.setsigjmp_bufDecl(Typedef->getDecl());
4940         else {
4941           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4942           assert(Tag && "Invalid sigjmp_buf type in AST file");
4943           Context.setsigjmp_bufDecl(Tag->getDecl());
4944         }
4945       }
4946     }
4947 
4948     if (unsigned ObjCIdRedef
4949           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4950       if (Context.ObjCIdRedefinitionType.isNull())
4951         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4952     }
4953 
4954     if (unsigned ObjCClassRedef
4955           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4956       if (Context.ObjCClassRedefinitionType.isNull())
4957         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4958     }
4959 
4960     if (unsigned ObjCSelRedef
4961           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4962       if (Context.ObjCSelRedefinitionType.isNull())
4963         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4964     }
4965 
4966     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4967       QualType Ucontext_tType = GetType(Ucontext_t);
4968       if (Ucontext_tType.isNull()) {
4969         Error("ucontext_t type is NULL");
4970         return;
4971       }
4972 
4973       if (!Context.ucontext_tDecl) {
4974         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4975           Context.setucontext_tDecl(Typedef->getDecl());
4976         else {
4977           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4978           assert(Tag && "Invalid ucontext_t type in AST file");
4979           Context.setucontext_tDecl(Tag->getDecl());
4980         }
4981       }
4982     }
4983   }
4984 
4985   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4986 
4987   // If there were any CUDA special declarations, deserialize them.
4988   if (!CUDASpecialDeclRefs.empty()) {
4989     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4990     Context.setcudaConfigureCallDecl(
4991                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4992   }
4993 
4994   // Re-export any modules that were imported by a non-module AST file.
4995   // FIXME: This does not make macro-only imports visible again.
4996   for (auto &Import : ImportedModules) {
4997     if (Module *Imported = getSubmodule(Import.ID)) {
4998       makeModuleVisible(Imported, Module::AllVisible,
4999                         /*ImportLoc=*/Import.ImportLoc);
5000       if (Import.ImportLoc.isValid())
5001         PP.makeModuleVisible(Imported, Import.ImportLoc);
5002       // This updates visibility for Preprocessor only. For Sema, which can be
5003       // nullptr here, we do the same later, in UpdateSema().
5004     }
5005   }
5006 }
5007 
5008 void ASTReader::finalizeForWriting() {
5009   // Nothing to do for now.
5010 }
5011 
5012 /// Reads and return the signature record from \p PCH's control block, or
5013 /// else returns 0.
5014 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5015   BitstreamCursor Stream(PCH);
5016   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5017     // FIXME this drops the error on the floor.
5018     consumeError(std::move(Err));
5019     return ASTFileSignature();
5020   }
5021 
5022   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5023   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5024     return ASTFileSignature();
5025 
5026   // Scan for SIGNATURE inside the diagnostic options block.
5027   ASTReader::RecordData Record;
5028   while (true) {
5029     Expected<llvm::BitstreamEntry> MaybeEntry =
5030         Stream.advanceSkippingSubblocks();
5031     if (!MaybeEntry) {
5032       // FIXME this drops the error on the floor.
5033       consumeError(MaybeEntry.takeError());
5034       return ASTFileSignature();
5035     }
5036     llvm::BitstreamEntry Entry = MaybeEntry.get();
5037 
5038     if (Entry.Kind != llvm::BitstreamEntry::Record)
5039       return ASTFileSignature();
5040 
5041     Record.clear();
5042     StringRef Blob;
5043     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5044     if (!MaybeRecord) {
5045       // FIXME this drops the error on the floor.
5046       consumeError(MaybeRecord.takeError());
5047       return ASTFileSignature();
5048     }
5049     if (SIGNATURE == MaybeRecord.get())
5050       return ASTFileSignature::create(Record.begin(),
5051                                       Record.begin() + ASTFileSignature::size);
5052   }
5053 }
5054 
5055 /// Retrieve the name of the original source file name
5056 /// directly from the AST file, without actually loading the AST
5057 /// file.
5058 std::string ASTReader::getOriginalSourceFile(
5059     const std::string &ASTFileName, FileManager &FileMgr,
5060     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5061   // Open the AST file.
5062   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5063   if (!Buffer) {
5064     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5065         << ASTFileName << Buffer.getError().message();
5066     return std::string();
5067   }
5068 
5069   // Initialize the stream
5070   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5071 
5072   // Sniff for the signature.
5073   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5074     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5075     return std::string();
5076   }
5077 
5078   // Scan for the CONTROL_BLOCK_ID block.
5079   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5080     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5081     return std::string();
5082   }
5083 
5084   // Scan for ORIGINAL_FILE inside the control block.
5085   RecordData Record;
5086   while (true) {
5087     Expected<llvm::BitstreamEntry> MaybeEntry =
5088         Stream.advanceSkippingSubblocks();
5089     if (!MaybeEntry) {
5090       // FIXME this drops errors on the floor.
5091       consumeError(MaybeEntry.takeError());
5092       return std::string();
5093     }
5094     llvm::BitstreamEntry Entry = MaybeEntry.get();
5095 
5096     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5097       return std::string();
5098 
5099     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5100       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5101       return std::string();
5102     }
5103 
5104     Record.clear();
5105     StringRef Blob;
5106     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5107     if (!MaybeRecord) {
5108       // FIXME this drops the errors on the floor.
5109       consumeError(MaybeRecord.takeError());
5110       return std::string();
5111     }
5112     if (ORIGINAL_FILE == MaybeRecord.get())
5113       return Blob.str();
5114   }
5115 }
5116 
5117 namespace {
5118 
5119   class SimplePCHValidator : public ASTReaderListener {
5120     const LangOptions &ExistingLangOpts;
5121     const TargetOptions &ExistingTargetOpts;
5122     const PreprocessorOptions &ExistingPPOpts;
5123     std::string ExistingModuleCachePath;
5124     FileManager &FileMgr;
5125 
5126   public:
5127     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5128                        const TargetOptions &ExistingTargetOpts,
5129                        const PreprocessorOptions &ExistingPPOpts,
5130                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5131         : ExistingLangOpts(ExistingLangOpts),
5132           ExistingTargetOpts(ExistingTargetOpts),
5133           ExistingPPOpts(ExistingPPOpts),
5134           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5135 
5136     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5137                              bool AllowCompatibleDifferences) override {
5138       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5139                                   AllowCompatibleDifferences);
5140     }
5141 
5142     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5143                            bool AllowCompatibleDifferences) override {
5144       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5145                                 AllowCompatibleDifferences);
5146     }
5147 
5148     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5149                                  StringRef SpecificModuleCachePath,
5150                                  bool Complain) override {
5151       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5152                                       ExistingModuleCachePath, nullptr,
5153                                       ExistingLangOpts, ExistingPPOpts);
5154     }
5155 
5156     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5157                                  bool Complain,
5158                                  std::string &SuggestedPredefines) override {
5159       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5160                                       SuggestedPredefines, ExistingLangOpts);
5161     }
5162   };
5163 
5164 } // namespace
5165 
5166 bool ASTReader::readASTFileControlBlock(
5167     StringRef Filename, FileManager &FileMgr,
5168     const PCHContainerReader &PCHContainerRdr,
5169     bool FindModuleFileExtensions,
5170     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5171   // Open the AST file.
5172   // FIXME: This allows use of the VFS; we do not allow use of the
5173   // VFS when actually loading a module.
5174   auto Buffer = FileMgr.getBufferForFile(Filename);
5175   if (!Buffer) {
5176     return true;
5177   }
5178 
5179   // Initialize the stream
5180   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5181   BitstreamCursor Stream(Bytes);
5182 
5183   // Sniff for the signature.
5184   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5185     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5186     return true;
5187   }
5188 
5189   // Scan for the CONTROL_BLOCK_ID block.
5190   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5191     return true;
5192 
5193   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5194   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5195   bool NeedsImports = Listener.needsImportVisitation();
5196   BitstreamCursor InputFilesCursor;
5197 
5198   RecordData Record;
5199   std::string ModuleDir;
5200   bool DoneWithControlBlock = false;
5201   while (!DoneWithControlBlock) {
5202     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5203     if (!MaybeEntry) {
5204       // FIXME this drops the error on the floor.
5205       consumeError(MaybeEntry.takeError());
5206       return true;
5207     }
5208     llvm::BitstreamEntry Entry = MaybeEntry.get();
5209 
5210     switch (Entry.Kind) {
5211     case llvm::BitstreamEntry::SubBlock: {
5212       switch (Entry.ID) {
5213       case OPTIONS_BLOCK_ID: {
5214         std::string IgnoredSuggestedPredefines;
5215         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5216                              /*AllowCompatibleConfigurationMismatch*/ false,
5217                              Listener, IgnoredSuggestedPredefines) != Success)
5218           return true;
5219         break;
5220       }
5221 
5222       case INPUT_FILES_BLOCK_ID:
5223         InputFilesCursor = Stream;
5224         if (llvm::Error Err = Stream.SkipBlock()) {
5225           // FIXME this drops the error on the floor.
5226           consumeError(std::move(Err));
5227           return true;
5228         }
5229         if (NeedsInputFiles &&
5230             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5231           return true;
5232         break;
5233 
5234       default:
5235         if (llvm::Error Err = Stream.SkipBlock()) {
5236           // FIXME this drops the error on the floor.
5237           consumeError(std::move(Err));
5238           return true;
5239         }
5240         break;
5241       }
5242 
5243       continue;
5244     }
5245 
5246     case llvm::BitstreamEntry::EndBlock:
5247       DoneWithControlBlock = true;
5248       break;
5249 
5250     case llvm::BitstreamEntry::Error:
5251       return true;
5252 
5253     case llvm::BitstreamEntry::Record:
5254       break;
5255     }
5256 
5257     if (DoneWithControlBlock) break;
5258 
5259     Record.clear();
5260     StringRef Blob;
5261     Expected<unsigned> MaybeRecCode =
5262         Stream.readRecord(Entry.ID, Record, &Blob);
5263     if (!MaybeRecCode) {
5264       // FIXME this drops the error.
5265       return Failure;
5266     }
5267     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5268     case METADATA:
5269       if (Record[0] != VERSION_MAJOR)
5270         return true;
5271       if (Listener.ReadFullVersionInformation(Blob))
5272         return true;
5273       break;
5274     case MODULE_NAME:
5275       Listener.ReadModuleName(Blob);
5276       break;
5277     case MODULE_DIRECTORY:
5278       ModuleDir = std::string(Blob);
5279       break;
5280     case MODULE_MAP_FILE: {
5281       unsigned Idx = 0;
5282       auto Path = ReadString(Record, Idx);
5283       ResolveImportedPath(Path, ModuleDir);
5284       Listener.ReadModuleMapFile(Path);
5285       break;
5286     }
5287     case INPUT_FILE_OFFSETS: {
5288       if (!NeedsInputFiles)
5289         break;
5290 
5291       unsigned NumInputFiles = Record[0];
5292       unsigned NumUserFiles = Record[1];
5293       const llvm::support::unaligned_uint64_t *InputFileOffs =
5294           (const llvm::support::unaligned_uint64_t *)Blob.data();
5295       for (unsigned I = 0; I != NumInputFiles; ++I) {
5296         // Go find this input file.
5297         bool isSystemFile = I >= NumUserFiles;
5298 
5299         if (isSystemFile && !NeedsSystemInputFiles)
5300           break; // the rest are system input files
5301 
5302         BitstreamCursor &Cursor = InputFilesCursor;
5303         SavedStreamPosition SavedPosition(Cursor);
5304         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5305           // FIXME this drops errors on the floor.
5306           consumeError(std::move(Err));
5307         }
5308 
5309         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5310         if (!MaybeCode) {
5311           // FIXME this drops errors on the floor.
5312           consumeError(MaybeCode.takeError());
5313         }
5314         unsigned Code = MaybeCode.get();
5315 
5316         RecordData Record;
5317         StringRef Blob;
5318         bool shouldContinue = false;
5319         Expected<unsigned> MaybeRecordType =
5320             Cursor.readRecord(Code, Record, &Blob);
5321         if (!MaybeRecordType) {
5322           // FIXME this drops errors on the floor.
5323           consumeError(MaybeRecordType.takeError());
5324         }
5325         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5326         case INPUT_FILE_HASH:
5327           break;
5328         case INPUT_FILE:
5329           bool Overridden = static_cast<bool>(Record[3]);
5330           std::string Filename = std::string(Blob);
5331           ResolveImportedPath(Filename, ModuleDir);
5332           shouldContinue = Listener.visitInputFile(
5333               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5334           break;
5335         }
5336         if (!shouldContinue)
5337           break;
5338       }
5339       break;
5340     }
5341 
5342     case IMPORTS: {
5343       if (!NeedsImports)
5344         break;
5345 
5346       unsigned Idx = 0, N = Record.size();
5347       while (Idx < N) {
5348         // Read information about the AST file.
5349         Idx +=
5350             1 + 1 + 1 + 1 +
5351             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5352         std::string ModuleName = ReadString(Record, Idx);
5353         std::string Filename = ReadString(Record, Idx);
5354         ResolveImportedPath(Filename, ModuleDir);
5355         Listener.visitImport(ModuleName, Filename);
5356       }
5357       break;
5358     }
5359 
5360     default:
5361       // No other validation to perform.
5362       break;
5363     }
5364   }
5365 
5366   // Look for module file extension blocks, if requested.
5367   if (FindModuleFileExtensions) {
5368     BitstreamCursor SavedStream = Stream;
5369     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5370       bool DoneWithExtensionBlock = false;
5371       while (!DoneWithExtensionBlock) {
5372         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5373         if (!MaybeEntry) {
5374           // FIXME this drops the error.
5375           return true;
5376         }
5377         llvm::BitstreamEntry Entry = MaybeEntry.get();
5378 
5379         switch (Entry.Kind) {
5380         case llvm::BitstreamEntry::SubBlock:
5381           if (llvm::Error Err = Stream.SkipBlock()) {
5382             // FIXME this drops the error on the floor.
5383             consumeError(std::move(Err));
5384             return true;
5385           }
5386           continue;
5387 
5388         case llvm::BitstreamEntry::EndBlock:
5389           DoneWithExtensionBlock = true;
5390           continue;
5391 
5392         case llvm::BitstreamEntry::Error:
5393           return true;
5394 
5395         case llvm::BitstreamEntry::Record:
5396           break;
5397         }
5398 
5399        Record.clear();
5400        StringRef Blob;
5401        Expected<unsigned> MaybeRecCode =
5402            Stream.readRecord(Entry.ID, Record, &Blob);
5403        if (!MaybeRecCode) {
5404          // FIXME this drops the error.
5405          return true;
5406        }
5407        switch (MaybeRecCode.get()) {
5408        case EXTENSION_METADATA: {
5409          ModuleFileExtensionMetadata Metadata;
5410          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5411            return true;
5412 
5413          Listener.readModuleFileExtension(Metadata);
5414          break;
5415        }
5416        }
5417       }
5418     }
5419     Stream = SavedStream;
5420   }
5421 
5422   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5423   if (readUnhashedControlBlockImpl(
5424           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5425           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5426           ValidateDiagnosticOptions) != Success)
5427     return true;
5428 
5429   return false;
5430 }
5431 
5432 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5433                                     const PCHContainerReader &PCHContainerRdr,
5434                                     const LangOptions &LangOpts,
5435                                     const TargetOptions &TargetOpts,
5436                                     const PreprocessorOptions &PPOpts,
5437                                     StringRef ExistingModuleCachePath) {
5438   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5439                                ExistingModuleCachePath, FileMgr);
5440   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5441                                   /*FindModuleFileExtensions=*/false,
5442                                   validator,
5443                                   /*ValidateDiagnosticOptions=*/true);
5444 }
5445 
5446 ASTReader::ASTReadResult
5447 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5448   // Enter the submodule block.
5449   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5450     Error(std::move(Err));
5451     return Failure;
5452   }
5453 
5454   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5455   bool First = true;
5456   Module *CurrentModule = nullptr;
5457   RecordData Record;
5458   while (true) {
5459     Expected<llvm::BitstreamEntry> MaybeEntry =
5460         F.Stream.advanceSkippingSubblocks();
5461     if (!MaybeEntry) {
5462       Error(MaybeEntry.takeError());
5463       return Failure;
5464     }
5465     llvm::BitstreamEntry Entry = MaybeEntry.get();
5466 
5467     switch (Entry.Kind) {
5468     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5469     case llvm::BitstreamEntry::Error:
5470       Error("malformed block record in AST file");
5471       return Failure;
5472     case llvm::BitstreamEntry::EndBlock:
5473       return Success;
5474     case llvm::BitstreamEntry::Record:
5475       // The interesting case.
5476       break;
5477     }
5478 
5479     // Read a record.
5480     StringRef Blob;
5481     Record.clear();
5482     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5483     if (!MaybeKind) {
5484       Error(MaybeKind.takeError());
5485       return Failure;
5486     }
5487     unsigned Kind = MaybeKind.get();
5488 
5489     if ((Kind == SUBMODULE_METADATA) != First) {
5490       Error("submodule metadata record should be at beginning of block");
5491       return Failure;
5492     }
5493     First = false;
5494 
5495     // Submodule information is only valid if we have a current module.
5496     // FIXME: Should we error on these cases?
5497     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5498         Kind != SUBMODULE_DEFINITION)
5499       continue;
5500 
5501     switch (Kind) {
5502     default:  // Default behavior: ignore.
5503       break;
5504 
5505     case SUBMODULE_DEFINITION: {
5506       if (Record.size() < 12) {
5507         Error("malformed module definition");
5508         return Failure;
5509       }
5510 
5511       StringRef Name = Blob;
5512       unsigned Idx = 0;
5513       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5514       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5515       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5516       bool IsFramework = Record[Idx++];
5517       bool IsExplicit = Record[Idx++];
5518       bool IsSystem = Record[Idx++];
5519       bool IsExternC = Record[Idx++];
5520       bool InferSubmodules = Record[Idx++];
5521       bool InferExplicitSubmodules = Record[Idx++];
5522       bool InferExportWildcard = Record[Idx++];
5523       bool ConfigMacrosExhaustive = Record[Idx++];
5524       bool ModuleMapIsPrivate = Record[Idx++];
5525 
5526       Module *ParentModule = nullptr;
5527       if (Parent)
5528         ParentModule = getSubmodule(Parent);
5529 
5530       // Retrieve this (sub)module from the module map, creating it if
5531       // necessary.
5532       CurrentModule =
5533           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5534               .first;
5535 
5536       // FIXME: set the definition loc for CurrentModule, or call
5537       // ModMap.setInferredModuleAllowedBy()
5538 
5539       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5540       if (GlobalIndex >= SubmodulesLoaded.size() ||
5541           SubmodulesLoaded[GlobalIndex]) {
5542         Error("too many submodules");
5543         return Failure;
5544       }
5545 
5546       if (!ParentModule) {
5547         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5548           // Don't emit module relocation error if we have -fno-validate-pch
5549           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5550                     DisableValidationForModuleKind::Module) &&
5551               CurFile != F.File) {
5552             Error(diag::err_module_file_conflict,
5553                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5554                   F.File->getName());
5555             return Failure;
5556           }
5557         }
5558 
5559         F.DidReadTopLevelSubmodule = true;
5560         CurrentModule->setASTFile(F.File);
5561         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5562       }
5563 
5564       CurrentModule->Kind = Kind;
5565       CurrentModule->Signature = F.Signature;
5566       CurrentModule->IsFromModuleFile = true;
5567       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5568       CurrentModule->IsExternC = IsExternC;
5569       CurrentModule->InferSubmodules = InferSubmodules;
5570       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5571       CurrentModule->InferExportWildcard = InferExportWildcard;
5572       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5573       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5574       if (DeserializationListener)
5575         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5576 
5577       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5578 
5579       // Clear out data that will be replaced by what is in the module file.
5580       CurrentModule->LinkLibraries.clear();
5581       CurrentModule->ConfigMacros.clear();
5582       CurrentModule->UnresolvedConflicts.clear();
5583       CurrentModule->Conflicts.clear();
5584 
5585       // The module is available unless it's missing a requirement; relevant
5586       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5587       // Missing headers that were present when the module was built do not
5588       // make it unavailable -- if we got this far, this must be an explicitly
5589       // imported module file.
5590       CurrentModule->Requirements.clear();
5591       CurrentModule->MissingHeaders.clear();
5592       CurrentModule->IsUnimportable =
5593           ParentModule && ParentModule->IsUnimportable;
5594       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5595       break;
5596     }
5597 
5598     case SUBMODULE_UMBRELLA_HEADER: {
5599       std::string Filename = std::string(Blob);
5600       ResolveImportedPath(F, Filename);
5601       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5602         if (!CurrentModule->getUmbrellaHeader())
5603           // FIXME: NameAsWritten
5604           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5605         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5606           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5607             Error("mismatched umbrella headers in submodule");
5608           return OutOfDate;
5609         }
5610       }
5611       break;
5612     }
5613 
5614     case SUBMODULE_HEADER:
5615     case SUBMODULE_EXCLUDED_HEADER:
5616     case SUBMODULE_PRIVATE_HEADER:
5617       // We lazily associate headers with their modules via the HeaderInfo table.
5618       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5619       // of complete filenames or remove it entirely.
5620       break;
5621 
5622     case SUBMODULE_TEXTUAL_HEADER:
5623     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5624       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5625       // them here.
5626       break;
5627 
5628     case SUBMODULE_TOPHEADER:
5629       CurrentModule->addTopHeaderFilename(Blob);
5630       break;
5631 
5632     case SUBMODULE_UMBRELLA_DIR: {
5633       std::string Dirname = std::string(Blob);
5634       ResolveImportedPath(F, Dirname);
5635       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5636         if (!CurrentModule->getUmbrellaDir())
5637           // FIXME: NameAsWritten
5638           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5639         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5640           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5641             Error("mismatched umbrella directories in submodule");
5642           return OutOfDate;
5643         }
5644       }
5645       break;
5646     }
5647 
5648     case SUBMODULE_METADATA: {
5649       F.BaseSubmoduleID = getTotalNumSubmodules();
5650       F.LocalNumSubmodules = Record[0];
5651       unsigned LocalBaseSubmoduleID = Record[1];
5652       if (F.LocalNumSubmodules > 0) {
5653         // Introduce the global -> local mapping for submodules within this
5654         // module.
5655         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5656 
5657         // Introduce the local -> global mapping for submodules within this
5658         // module.
5659         F.SubmoduleRemap.insertOrReplace(
5660           std::make_pair(LocalBaseSubmoduleID,
5661                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5662 
5663         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5664       }
5665       break;
5666     }
5667 
5668     case SUBMODULE_IMPORTS:
5669       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5670         UnresolvedModuleRef Unresolved;
5671         Unresolved.File = &F;
5672         Unresolved.Mod = CurrentModule;
5673         Unresolved.ID = Record[Idx];
5674         Unresolved.Kind = UnresolvedModuleRef::Import;
5675         Unresolved.IsWildcard = false;
5676         UnresolvedModuleRefs.push_back(Unresolved);
5677       }
5678       break;
5679 
5680     case SUBMODULE_EXPORTS:
5681       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5682         UnresolvedModuleRef Unresolved;
5683         Unresolved.File = &F;
5684         Unresolved.Mod = CurrentModule;
5685         Unresolved.ID = Record[Idx];
5686         Unresolved.Kind = UnresolvedModuleRef::Export;
5687         Unresolved.IsWildcard = Record[Idx + 1];
5688         UnresolvedModuleRefs.push_back(Unresolved);
5689       }
5690 
5691       // Once we've loaded the set of exports, there's no reason to keep
5692       // the parsed, unresolved exports around.
5693       CurrentModule->UnresolvedExports.clear();
5694       break;
5695 
5696     case SUBMODULE_REQUIRES:
5697       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5698                                     PP.getTargetInfo());
5699       break;
5700 
5701     case SUBMODULE_LINK_LIBRARY:
5702       ModMap.resolveLinkAsDependencies(CurrentModule);
5703       CurrentModule->LinkLibraries.push_back(
5704           Module::LinkLibrary(std::string(Blob), Record[0]));
5705       break;
5706 
5707     case SUBMODULE_CONFIG_MACRO:
5708       CurrentModule->ConfigMacros.push_back(Blob.str());
5709       break;
5710 
5711     case SUBMODULE_CONFLICT: {
5712       UnresolvedModuleRef Unresolved;
5713       Unresolved.File = &F;
5714       Unresolved.Mod = CurrentModule;
5715       Unresolved.ID = Record[0];
5716       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5717       Unresolved.IsWildcard = false;
5718       Unresolved.String = Blob;
5719       UnresolvedModuleRefs.push_back(Unresolved);
5720       break;
5721     }
5722 
5723     case SUBMODULE_INITIALIZERS: {
5724       if (!ContextObj)
5725         break;
5726       SmallVector<uint32_t, 16> Inits;
5727       for (auto &ID : Record)
5728         Inits.push_back(getGlobalDeclID(F, ID));
5729       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5730       break;
5731     }
5732 
5733     case SUBMODULE_EXPORT_AS:
5734       CurrentModule->ExportAsModule = Blob.str();
5735       ModMap.addLinkAsDependency(CurrentModule);
5736       break;
5737     }
5738   }
5739 }
5740 
5741 /// Parse the record that corresponds to a LangOptions data
5742 /// structure.
5743 ///
5744 /// This routine parses the language options from the AST file and then gives
5745 /// them to the AST listener if one is set.
5746 ///
5747 /// \returns true if the listener deems the file unacceptable, false otherwise.
5748 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5749                                      bool Complain,
5750                                      ASTReaderListener &Listener,
5751                                      bool AllowCompatibleDifferences) {
5752   LangOptions LangOpts;
5753   unsigned Idx = 0;
5754 #define LANGOPT(Name, Bits, Default, Description) \
5755   LangOpts.Name = Record[Idx++];
5756 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5757   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5758 #include "clang/Basic/LangOptions.def"
5759 #define SANITIZER(NAME, ID)                                                    \
5760   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5761 #include "clang/Basic/Sanitizers.def"
5762 
5763   for (unsigned N = Record[Idx++]; N; --N)
5764     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5765 
5766   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5767   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5768   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5769 
5770   LangOpts.CurrentModule = ReadString(Record, Idx);
5771 
5772   // Comment options.
5773   for (unsigned N = Record[Idx++]; N; --N) {
5774     LangOpts.CommentOpts.BlockCommandNames.push_back(
5775       ReadString(Record, Idx));
5776   }
5777   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5778 
5779   // OpenMP offloading options.
5780   for (unsigned N = Record[Idx++]; N; --N) {
5781     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5782   }
5783 
5784   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5785 
5786   return Listener.ReadLanguageOptions(LangOpts, Complain,
5787                                       AllowCompatibleDifferences);
5788 }
5789 
5790 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5791                                    ASTReaderListener &Listener,
5792                                    bool AllowCompatibleDifferences) {
5793   unsigned Idx = 0;
5794   TargetOptions TargetOpts;
5795   TargetOpts.Triple = ReadString(Record, Idx);
5796   TargetOpts.CPU = ReadString(Record, Idx);
5797   TargetOpts.TuneCPU = ReadString(Record, Idx);
5798   TargetOpts.ABI = ReadString(Record, Idx);
5799   for (unsigned N = Record[Idx++]; N; --N) {
5800     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5801   }
5802   for (unsigned N = Record[Idx++]; N; --N) {
5803     TargetOpts.Features.push_back(ReadString(Record, Idx));
5804   }
5805 
5806   return Listener.ReadTargetOptions(TargetOpts, Complain,
5807                                     AllowCompatibleDifferences);
5808 }
5809 
5810 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5811                                        ASTReaderListener &Listener) {
5812   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5813   unsigned Idx = 0;
5814 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5815 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5816   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5817 #include "clang/Basic/DiagnosticOptions.def"
5818 
5819   for (unsigned N = Record[Idx++]; N; --N)
5820     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5821   for (unsigned N = Record[Idx++]; N; --N)
5822     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5823 
5824   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5825 }
5826 
5827 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5828                                        ASTReaderListener &Listener) {
5829   FileSystemOptions FSOpts;
5830   unsigned Idx = 0;
5831   FSOpts.WorkingDir = ReadString(Record, Idx);
5832   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5833 }
5834 
5835 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5836                                          bool Complain,
5837                                          ASTReaderListener &Listener) {
5838   HeaderSearchOptions HSOpts;
5839   unsigned Idx = 0;
5840   HSOpts.Sysroot = ReadString(Record, Idx);
5841 
5842   // Include entries.
5843   for (unsigned N = Record[Idx++]; N; --N) {
5844     std::string Path = ReadString(Record, Idx);
5845     frontend::IncludeDirGroup Group
5846       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5847     bool IsFramework = Record[Idx++];
5848     bool IgnoreSysRoot = Record[Idx++];
5849     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5850                                     IgnoreSysRoot);
5851   }
5852 
5853   // System header prefixes.
5854   for (unsigned N = Record[Idx++]; N; --N) {
5855     std::string Prefix = ReadString(Record, Idx);
5856     bool IsSystemHeader = Record[Idx++];
5857     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5858   }
5859 
5860   HSOpts.ResourceDir = ReadString(Record, Idx);
5861   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5862   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5863   HSOpts.DisableModuleHash = Record[Idx++];
5864   HSOpts.ImplicitModuleMaps = Record[Idx++];
5865   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5866   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5867   HSOpts.UseBuiltinIncludes = Record[Idx++];
5868   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5869   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5870   HSOpts.UseLibcxx = Record[Idx++];
5871   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5872 
5873   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5874                                           Complain);
5875 }
5876 
5877 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5878                                          bool Complain,
5879                                          ASTReaderListener &Listener,
5880                                          std::string &SuggestedPredefines) {
5881   PreprocessorOptions PPOpts;
5882   unsigned Idx = 0;
5883 
5884   // Macro definitions/undefs
5885   for (unsigned N = Record[Idx++]; N; --N) {
5886     std::string Macro = ReadString(Record, Idx);
5887     bool IsUndef = Record[Idx++];
5888     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5889   }
5890 
5891   // Includes
5892   for (unsigned N = Record[Idx++]; N; --N) {
5893     PPOpts.Includes.push_back(ReadString(Record, Idx));
5894   }
5895 
5896   // Macro Includes
5897   for (unsigned N = Record[Idx++]; N; --N) {
5898     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5899   }
5900 
5901   PPOpts.UsePredefines = Record[Idx++];
5902   PPOpts.DetailedRecord = Record[Idx++];
5903   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5904   PPOpts.ObjCXXARCStandardLibrary =
5905     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5906   SuggestedPredefines.clear();
5907   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5908                                           SuggestedPredefines);
5909 }
5910 
5911 std::pair<ModuleFile *, unsigned>
5912 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5913   GlobalPreprocessedEntityMapType::iterator
5914   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5915   assert(I != GlobalPreprocessedEntityMap.end() &&
5916          "Corrupted global preprocessed entity map");
5917   ModuleFile *M = I->second;
5918   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5919   return std::make_pair(M, LocalIndex);
5920 }
5921 
5922 llvm::iterator_range<PreprocessingRecord::iterator>
5923 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5924   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5925     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5926                                              Mod.NumPreprocessedEntities);
5927 
5928   return llvm::make_range(PreprocessingRecord::iterator(),
5929                           PreprocessingRecord::iterator());
5930 }
5931 
5932 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5933                                         unsigned int ClientLoadCapabilities) {
5934   return ClientLoadCapabilities & ARR_OutOfDate &&
5935          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5936 }
5937 
5938 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5939 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5940   return llvm::make_range(
5941       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5942       ModuleDeclIterator(this, &Mod,
5943                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5944 }
5945 
5946 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5947   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5948   assert(I != GlobalSkippedRangeMap.end() &&
5949     "Corrupted global skipped range map");
5950   ModuleFile *M = I->second;
5951   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5952   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5953   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5954   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5955                     TranslateSourceLocation(*M, RawRange.getEnd()));
5956   assert(Range.isValid());
5957   return Range;
5958 }
5959 
5960 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5961   PreprocessedEntityID PPID = Index+1;
5962   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5963   ModuleFile &M = *PPInfo.first;
5964   unsigned LocalIndex = PPInfo.second;
5965   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5966 
5967   if (!PP.getPreprocessingRecord()) {
5968     Error("no preprocessing record");
5969     return nullptr;
5970   }
5971 
5972   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5973   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5974           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5975     Error(std::move(Err));
5976     return nullptr;
5977   }
5978 
5979   Expected<llvm::BitstreamEntry> MaybeEntry =
5980       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5981   if (!MaybeEntry) {
5982     Error(MaybeEntry.takeError());
5983     return nullptr;
5984   }
5985   llvm::BitstreamEntry Entry = MaybeEntry.get();
5986 
5987   if (Entry.Kind != llvm::BitstreamEntry::Record)
5988     return nullptr;
5989 
5990   // Read the record.
5991   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5992                     TranslateSourceLocation(M, PPOffs.getEnd()));
5993   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5994   StringRef Blob;
5995   RecordData Record;
5996   Expected<unsigned> MaybeRecType =
5997       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5998   if (!MaybeRecType) {
5999     Error(MaybeRecType.takeError());
6000     return nullptr;
6001   }
6002   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6003   case PPD_MACRO_EXPANSION: {
6004     bool isBuiltin = Record[0];
6005     IdentifierInfo *Name = nullptr;
6006     MacroDefinitionRecord *Def = nullptr;
6007     if (isBuiltin)
6008       Name = getLocalIdentifier(M, Record[1]);
6009     else {
6010       PreprocessedEntityID GlobalID =
6011           getGlobalPreprocessedEntityID(M, Record[1]);
6012       Def = cast<MacroDefinitionRecord>(
6013           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6014     }
6015 
6016     MacroExpansion *ME;
6017     if (isBuiltin)
6018       ME = new (PPRec) MacroExpansion(Name, Range);
6019     else
6020       ME = new (PPRec) MacroExpansion(Def, Range);
6021 
6022     return ME;
6023   }
6024 
6025   case PPD_MACRO_DEFINITION: {
6026     // Decode the identifier info and then check again; if the macro is
6027     // still defined and associated with the identifier,
6028     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6029     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6030 
6031     if (DeserializationListener)
6032       DeserializationListener->MacroDefinitionRead(PPID, MD);
6033 
6034     return MD;
6035   }
6036 
6037   case PPD_INCLUSION_DIRECTIVE: {
6038     const char *FullFileNameStart = Blob.data() + Record[0];
6039     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6040     const FileEntry *File = nullptr;
6041     if (!FullFileName.empty())
6042       if (auto FE = PP.getFileManager().getFile(FullFileName))
6043         File = *FE;
6044 
6045     // FIXME: Stable encoding
6046     InclusionDirective::InclusionKind Kind
6047       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6048     InclusionDirective *ID
6049       = new (PPRec) InclusionDirective(PPRec, Kind,
6050                                        StringRef(Blob.data(), Record[0]),
6051                                        Record[1], Record[3],
6052                                        File,
6053                                        Range);
6054     return ID;
6055   }
6056   }
6057 
6058   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6059 }
6060 
6061 /// Find the next module that contains entities and return the ID
6062 /// of the first entry.
6063 ///
6064 /// \param SLocMapI points at a chunk of a module that contains no
6065 /// preprocessed entities or the entities it contains are not the ones we are
6066 /// looking for.
6067 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6068                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6069   ++SLocMapI;
6070   for (GlobalSLocOffsetMapType::const_iterator
6071          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6072     ModuleFile &M = *SLocMapI->second;
6073     if (M.NumPreprocessedEntities)
6074       return M.BasePreprocessedEntityID;
6075   }
6076 
6077   return getTotalNumPreprocessedEntities();
6078 }
6079 
6080 namespace {
6081 
6082 struct PPEntityComp {
6083   const ASTReader &Reader;
6084   ModuleFile &M;
6085 
6086   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6087 
6088   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6089     SourceLocation LHS = getLoc(L);
6090     SourceLocation RHS = getLoc(R);
6091     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6092   }
6093 
6094   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6095     SourceLocation LHS = getLoc(L);
6096     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6097   }
6098 
6099   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6100     SourceLocation RHS = getLoc(R);
6101     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6102   }
6103 
6104   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6105     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6106   }
6107 };
6108 
6109 } // namespace
6110 
6111 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6112                                                        bool EndsAfter) const {
6113   if (SourceMgr.isLocalSourceLocation(Loc))
6114     return getTotalNumPreprocessedEntities();
6115 
6116   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6117       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6118   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6119          "Corrupted global sloc offset map");
6120 
6121   if (SLocMapI->second->NumPreprocessedEntities == 0)
6122     return findNextPreprocessedEntity(SLocMapI);
6123 
6124   ModuleFile &M = *SLocMapI->second;
6125 
6126   using pp_iterator = const PPEntityOffset *;
6127 
6128   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6129   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6130 
6131   size_t Count = M.NumPreprocessedEntities;
6132   size_t Half;
6133   pp_iterator First = pp_begin;
6134   pp_iterator PPI;
6135 
6136   if (EndsAfter) {
6137     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6138                            PPEntityComp(*this, M));
6139   } else {
6140     // Do a binary search manually instead of using std::lower_bound because
6141     // The end locations of entities may be unordered (when a macro expansion
6142     // is inside another macro argument), but for this case it is not important
6143     // whether we get the first macro expansion or its containing macro.
6144     while (Count > 0) {
6145       Half = Count / 2;
6146       PPI = First;
6147       std::advance(PPI, Half);
6148       if (SourceMgr.isBeforeInTranslationUnit(
6149               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6150         First = PPI;
6151         ++First;
6152         Count = Count - Half - 1;
6153       } else
6154         Count = Half;
6155     }
6156   }
6157 
6158   if (PPI == pp_end)
6159     return findNextPreprocessedEntity(SLocMapI);
6160 
6161   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6162 }
6163 
6164 /// Returns a pair of [Begin, End) indices of preallocated
6165 /// preprocessed entities that \arg Range encompasses.
6166 std::pair<unsigned, unsigned>
6167     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6168   if (Range.isInvalid())
6169     return std::make_pair(0,0);
6170   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6171 
6172   PreprocessedEntityID BeginID =
6173       findPreprocessedEntity(Range.getBegin(), false);
6174   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6175   return std::make_pair(BeginID, EndID);
6176 }
6177 
6178 /// Optionally returns true or false if the preallocated preprocessed
6179 /// entity with index \arg Index came from file \arg FID.
6180 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6181                                                              FileID FID) {
6182   if (FID.isInvalid())
6183     return false;
6184 
6185   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6186   ModuleFile &M = *PPInfo.first;
6187   unsigned LocalIndex = PPInfo.second;
6188   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6189 
6190   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6191   if (Loc.isInvalid())
6192     return false;
6193 
6194   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6195     return true;
6196   else
6197     return false;
6198 }
6199 
6200 namespace {
6201 
6202   /// Visitor used to search for information about a header file.
6203   class HeaderFileInfoVisitor {
6204     const FileEntry *FE;
6205     Optional<HeaderFileInfo> HFI;
6206 
6207   public:
6208     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6209 
6210     bool operator()(ModuleFile &M) {
6211       HeaderFileInfoLookupTable *Table
6212         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6213       if (!Table)
6214         return false;
6215 
6216       // Look in the on-disk hash table for an entry for this file name.
6217       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6218       if (Pos == Table->end())
6219         return false;
6220 
6221       HFI = *Pos;
6222       return true;
6223     }
6224 
6225     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6226   };
6227 
6228 } // namespace
6229 
6230 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6231   HeaderFileInfoVisitor Visitor(FE);
6232   ModuleMgr.visit(Visitor);
6233   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6234     return *HFI;
6235 
6236   return HeaderFileInfo();
6237 }
6238 
6239 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6240   using DiagState = DiagnosticsEngine::DiagState;
6241   SmallVector<DiagState *, 32> DiagStates;
6242 
6243   for (ModuleFile &F : ModuleMgr) {
6244     unsigned Idx = 0;
6245     auto &Record = F.PragmaDiagMappings;
6246     if (Record.empty())
6247       continue;
6248 
6249     DiagStates.clear();
6250 
6251     auto ReadDiagState =
6252         [&](const DiagState &BasedOn, SourceLocation Loc,
6253             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6254       unsigned BackrefID = Record[Idx++];
6255       if (BackrefID != 0)
6256         return DiagStates[BackrefID - 1];
6257 
6258       // A new DiagState was created here.
6259       Diag.DiagStates.push_back(BasedOn);
6260       DiagState *NewState = &Diag.DiagStates.back();
6261       DiagStates.push_back(NewState);
6262       unsigned Size = Record[Idx++];
6263       assert(Idx + Size * 2 <= Record.size() &&
6264              "Invalid data, not enough diag/map pairs");
6265       while (Size--) {
6266         unsigned DiagID = Record[Idx++];
6267         DiagnosticMapping NewMapping =
6268             DiagnosticMapping::deserialize(Record[Idx++]);
6269         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6270           continue;
6271 
6272         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6273 
6274         // If this mapping was specified as a warning but the severity was
6275         // upgraded due to diagnostic settings, simulate the current diagnostic
6276         // settings (and use a warning).
6277         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6278           NewMapping.setSeverity(diag::Severity::Warning);
6279           NewMapping.setUpgradedFromWarning(false);
6280         }
6281 
6282         Mapping = NewMapping;
6283       }
6284       return NewState;
6285     };
6286 
6287     // Read the first state.
6288     DiagState *FirstState;
6289     if (F.Kind == MK_ImplicitModule) {
6290       // Implicitly-built modules are reused with different diagnostic
6291       // settings.  Use the initial diagnostic state from Diag to simulate this
6292       // compilation's diagnostic settings.
6293       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6294       DiagStates.push_back(FirstState);
6295 
6296       // Skip the initial diagnostic state from the serialized module.
6297       assert(Record[1] == 0 &&
6298              "Invalid data, unexpected backref in initial state");
6299       Idx = 3 + Record[2] * 2;
6300       assert(Idx < Record.size() &&
6301              "Invalid data, not enough state change pairs in initial state");
6302     } else if (F.isModule()) {
6303       // For an explicit module, preserve the flags from the module build
6304       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6305       // -Wblah flags.
6306       unsigned Flags = Record[Idx++];
6307       DiagState Initial;
6308       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6309       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6310       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6311       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6312       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6313       Initial.ExtBehavior = (diag::Severity)Flags;
6314       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6315 
6316       assert(F.OriginalSourceFileID.isValid());
6317 
6318       // Set up the root buffer of the module to start with the initial
6319       // diagnostic state of the module itself, to cover files that contain no
6320       // explicit transitions (for which we did not serialize anything).
6321       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6322           .StateTransitions.push_back({FirstState, 0});
6323     } else {
6324       // For prefix ASTs, start with whatever the user configured on the
6325       // command line.
6326       Idx++; // Skip flags.
6327       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6328                                  SourceLocation(), false);
6329     }
6330 
6331     // Read the state transitions.
6332     unsigned NumLocations = Record[Idx++];
6333     while (NumLocations--) {
6334       assert(Idx < Record.size() &&
6335              "Invalid data, missing pragma diagnostic states");
6336       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6337       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6338       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6339       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6340       unsigned Transitions = Record[Idx++];
6341 
6342       // Note that we don't need to set up Parent/ParentOffset here, because
6343       // we won't be changing the diagnostic state within imported FileIDs
6344       // (other than perhaps appending to the main source file, which has no
6345       // parent).
6346       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6347       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6348       for (unsigned I = 0; I != Transitions; ++I) {
6349         unsigned Offset = Record[Idx++];
6350         auto *State =
6351             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6352         F.StateTransitions.push_back({State, Offset});
6353       }
6354     }
6355 
6356     // Read the final state.
6357     assert(Idx < Record.size() &&
6358            "Invalid data, missing final pragma diagnostic state");
6359     SourceLocation CurStateLoc =
6360         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6361     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6362 
6363     if (!F.isModule()) {
6364       Diag.DiagStatesByLoc.CurDiagState = CurState;
6365       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6366 
6367       // Preserve the property that the imaginary root file describes the
6368       // current state.
6369       FileID NullFile;
6370       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6371       if (T.empty())
6372         T.push_back({CurState, 0});
6373       else
6374         T[0].State = CurState;
6375     }
6376 
6377     // Don't try to read these mappings again.
6378     Record.clear();
6379   }
6380 }
6381 
6382 /// Get the correct cursor and offset for loading a type.
6383 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6384   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6385   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6386   ModuleFile *M = I->second;
6387   return RecordLocation(
6388       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6389              M->DeclsBlockStartOffset);
6390 }
6391 
6392 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6393   switch (code) {
6394 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6395   case TYPE_##CODE_ID: return Type::CLASS_ID;
6396 #include "clang/Serialization/TypeBitCodes.def"
6397   default: return llvm::None;
6398   }
6399 }
6400 
6401 /// Read and return the type with the given index..
6402 ///
6403 /// The index is the type ID, shifted and minus the number of predefs. This
6404 /// routine actually reads the record corresponding to the type at the given
6405 /// location. It is a helper routine for GetType, which deals with reading type
6406 /// IDs.
6407 QualType ASTReader::readTypeRecord(unsigned Index) {
6408   assert(ContextObj && "reading type with no AST context");
6409   ASTContext &Context = *ContextObj;
6410   RecordLocation Loc = TypeCursorForIndex(Index);
6411   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6412 
6413   // Keep track of where we are in the stream, then jump back there
6414   // after reading this type.
6415   SavedStreamPosition SavedPosition(DeclsCursor);
6416 
6417   ReadingKindTracker ReadingKind(Read_Type, *this);
6418 
6419   // Note that we are loading a type record.
6420   Deserializing AType(this);
6421 
6422   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6423     Error(std::move(Err));
6424     return QualType();
6425   }
6426   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6427   if (!RawCode) {
6428     Error(RawCode.takeError());
6429     return QualType();
6430   }
6431 
6432   ASTRecordReader Record(*this, *Loc.F);
6433   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6434   if (!Code) {
6435     Error(Code.takeError());
6436     return QualType();
6437   }
6438   if (Code.get() == TYPE_EXT_QUAL) {
6439     QualType baseType = Record.readQualType();
6440     Qualifiers quals = Record.readQualifiers();
6441     return Context.getQualifiedType(baseType, quals);
6442   }
6443 
6444   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6445   if (!maybeClass) {
6446     Error("Unexpected code for type");
6447     return QualType();
6448   }
6449 
6450   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6451   return TypeReader.read(*maybeClass);
6452 }
6453 
6454 namespace clang {
6455 
6456 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6457   ASTRecordReader &Reader;
6458 
6459   SourceLocation readSourceLocation() {
6460     return Reader.readSourceLocation();
6461   }
6462 
6463   TypeSourceInfo *GetTypeSourceInfo() {
6464     return Reader.readTypeSourceInfo();
6465   }
6466 
6467   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6468     return Reader.readNestedNameSpecifierLoc();
6469   }
6470 
6471   Attr *ReadAttr() {
6472     return Reader.readAttr();
6473   }
6474 
6475 public:
6476   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6477 
6478   // We want compile-time assurance that we've enumerated all of
6479   // these, so unfortunately we have to declare them first, then
6480   // define them out-of-line.
6481 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6482 #define TYPELOC(CLASS, PARENT) \
6483   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6484 #include "clang/AST/TypeLocNodes.def"
6485 
6486   void VisitFunctionTypeLoc(FunctionTypeLoc);
6487   void VisitArrayTypeLoc(ArrayTypeLoc);
6488 };
6489 
6490 } // namespace clang
6491 
6492 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6493   // nothing to do
6494 }
6495 
6496 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6497   TL.setBuiltinLoc(readSourceLocation());
6498   if (TL.needsExtraLocalData()) {
6499     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6500     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6501     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6502     TL.setModeAttr(Reader.readInt());
6503   }
6504 }
6505 
6506 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6507   TL.setNameLoc(readSourceLocation());
6508 }
6509 
6510 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6511   TL.setStarLoc(readSourceLocation());
6512 }
6513 
6514 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6515   // nothing to do
6516 }
6517 
6518 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6519   // nothing to do
6520 }
6521 
6522 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6523   TL.setExpansionLoc(readSourceLocation());
6524 }
6525 
6526 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6527   TL.setCaretLoc(readSourceLocation());
6528 }
6529 
6530 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6531   TL.setAmpLoc(readSourceLocation());
6532 }
6533 
6534 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6535   TL.setAmpAmpLoc(readSourceLocation());
6536 }
6537 
6538 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6539   TL.setStarLoc(readSourceLocation());
6540   TL.setClassTInfo(GetTypeSourceInfo());
6541 }
6542 
6543 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6544   TL.setLBracketLoc(readSourceLocation());
6545   TL.setRBracketLoc(readSourceLocation());
6546   if (Reader.readBool())
6547     TL.setSizeExpr(Reader.readExpr());
6548   else
6549     TL.setSizeExpr(nullptr);
6550 }
6551 
6552 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6553   VisitArrayTypeLoc(TL);
6554 }
6555 
6556 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6557   VisitArrayTypeLoc(TL);
6558 }
6559 
6560 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6561   VisitArrayTypeLoc(TL);
6562 }
6563 
6564 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6565                                             DependentSizedArrayTypeLoc TL) {
6566   VisitArrayTypeLoc(TL);
6567 }
6568 
6569 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6570     DependentAddressSpaceTypeLoc TL) {
6571 
6572     TL.setAttrNameLoc(readSourceLocation());
6573     TL.setAttrOperandParensRange(Reader.readSourceRange());
6574     TL.setAttrExprOperand(Reader.readExpr());
6575 }
6576 
6577 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6578                                         DependentSizedExtVectorTypeLoc TL) {
6579   TL.setNameLoc(readSourceLocation());
6580 }
6581 
6582 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6583   TL.setNameLoc(readSourceLocation());
6584 }
6585 
6586 void TypeLocReader::VisitDependentVectorTypeLoc(
6587     DependentVectorTypeLoc TL) {
6588   TL.setNameLoc(readSourceLocation());
6589 }
6590 
6591 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6592   TL.setNameLoc(readSourceLocation());
6593 }
6594 
6595 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6596   TL.setAttrNameLoc(readSourceLocation());
6597   TL.setAttrOperandParensRange(Reader.readSourceRange());
6598   TL.setAttrRowOperand(Reader.readExpr());
6599   TL.setAttrColumnOperand(Reader.readExpr());
6600 }
6601 
6602 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6603     DependentSizedMatrixTypeLoc TL) {
6604   TL.setAttrNameLoc(readSourceLocation());
6605   TL.setAttrOperandParensRange(Reader.readSourceRange());
6606   TL.setAttrRowOperand(Reader.readExpr());
6607   TL.setAttrColumnOperand(Reader.readExpr());
6608 }
6609 
6610 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6611   TL.setLocalRangeBegin(readSourceLocation());
6612   TL.setLParenLoc(readSourceLocation());
6613   TL.setRParenLoc(readSourceLocation());
6614   TL.setExceptionSpecRange(Reader.readSourceRange());
6615   TL.setLocalRangeEnd(readSourceLocation());
6616   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6617     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6618   }
6619 }
6620 
6621 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6622   VisitFunctionTypeLoc(TL);
6623 }
6624 
6625 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6626   VisitFunctionTypeLoc(TL);
6627 }
6628 
6629 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6630   TL.setNameLoc(readSourceLocation());
6631 }
6632 
6633 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6634   TL.setNameLoc(readSourceLocation());
6635 }
6636 
6637 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6638   TL.setTypeofLoc(readSourceLocation());
6639   TL.setLParenLoc(readSourceLocation());
6640   TL.setRParenLoc(readSourceLocation());
6641 }
6642 
6643 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6644   TL.setTypeofLoc(readSourceLocation());
6645   TL.setLParenLoc(readSourceLocation());
6646   TL.setRParenLoc(readSourceLocation());
6647   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6648 }
6649 
6650 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6651   TL.setNameLoc(readSourceLocation());
6652 }
6653 
6654 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6655   TL.setKWLoc(readSourceLocation());
6656   TL.setLParenLoc(readSourceLocation());
6657   TL.setRParenLoc(readSourceLocation());
6658   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6659 }
6660 
6661 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6662   TL.setNameLoc(readSourceLocation());
6663   if (Reader.readBool()) {
6664     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6665     TL.setTemplateKWLoc(readSourceLocation());
6666     TL.setConceptNameLoc(readSourceLocation());
6667     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6668     TL.setLAngleLoc(readSourceLocation());
6669     TL.setRAngleLoc(readSourceLocation());
6670     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6671       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6672                               TL.getTypePtr()->getArg(i).getKind()));
6673   }
6674 }
6675 
6676 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6677     DeducedTemplateSpecializationTypeLoc TL) {
6678   TL.setTemplateNameLoc(readSourceLocation());
6679 }
6680 
6681 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6682   TL.setNameLoc(readSourceLocation());
6683 }
6684 
6685 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6686   TL.setNameLoc(readSourceLocation());
6687 }
6688 
6689 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6690   TL.setAttr(ReadAttr());
6691 }
6692 
6693 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6694   TL.setNameLoc(readSourceLocation());
6695 }
6696 
6697 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6698                                             SubstTemplateTypeParmTypeLoc TL) {
6699   TL.setNameLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6703                                           SubstTemplateTypeParmPackTypeLoc TL) {
6704   TL.setNameLoc(readSourceLocation());
6705 }
6706 
6707 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6708                                            TemplateSpecializationTypeLoc TL) {
6709   TL.setTemplateKeywordLoc(readSourceLocation());
6710   TL.setTemplateNameLoc(readSourceLocation());
6711   TL.setLAngleLoc(readSourceLocation());
6712   TL.setRAngleLoc(readSourceLocation());
6713   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6714     TL.setArgLocInfo(
6715         i,
6716         Reader.readTemplateArgumentLocInfo(
6717           TL.getTypePtr()->getArg(i).getKind()));
6718 }
6719 
6720 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6721   TL.setLParenLoc(readSourceLocation());
6722   TL.setRParenLoc(readSourceLocation());
6723 }
6724 
6725 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6726   TL.setElaboratedKeywordLoc(readSourceLocation());
6727   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6728 }
6729 
6730 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6731   TL.setNameLoc(readSourceLocation());
6732 }
6733 
6734 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6735   TL.setElaboratedKeywordLoc(readSourceLocation());
6736   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6737   TL.setNameLoc(readSourceLocation());
6738 }
6739 
6740 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6741        DependentTemplateSpecializationTypeLoc TL) {
6742   TL.setElaboratedKeywordLoc(readSourceLocation());
6743   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6744   TL.setTemplateKeywordLoc(readSourceLocation());
6745   TL.setTemplateNameLoc(readSourceLocation());
6746   TL.setLAngleLoc(readSourceLocation());
6747   TL.setRAngleLoc(readSourceLocation());
6748   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6749     TL.setArgLocInfo(
6750         I,
6751         Reader.readTemplateArgumentLocInfo(
6752             TL.getTypePtr()->getArg(I).getKind()));
6753 }
6754 
6755 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6756   TL.setEllipsisLoc(readSourceLocation());
6757 }
6758 
6759 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6760   TL.setNameLoc(readSourceLocation());
6761 }
6762 
6763 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6764   if (TL.getNumProtocols()) {
6765     TL.setProtocolLAngleLoc(readSourceLocation());
6766     TL.setProtocolRAngleLoc(readSourceLocation());
6767   }
6768   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6769     TL.setProtocolLoc(i, readSourceLocation());
6770 }
6771 
6772 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6773   TL.setHasBaseTypeAsWritten(Reader.readBool());
6774   TL.setTypeArgsLAngleLoc(readSourceLocation());
6775   TL.setTypeArgsRAngleLoc(readSourceLocation());
6776   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6777     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6778   TL.setProtocolLAngleLoc(readSourceLocation());
6779   TL.setProtocolRAngleLoc(readSourceLocation());
6780   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6781     TL.setProtocolLoc(i, readSourceLocation());
6782 }
6783 
6784 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6785   TL.setStarLoc(readSourceLocation());
6786 }
6787 
6788 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6789   TL.setKWLoc(readSourceLocation());
6790   TL.setLParenLoc(readSourceLocation());
6791   TL.setRParenLoc(readSourceLocation());
6792 }
6793 
6794 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6795   TL.setKWLoc(readSourceLocation());
6796 }
6797 
6798 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6799   TL.setNameLoc(readSourceLocation());
6800 }
6801 void TypeLocReader::VisitDependentExtIntTypeLoc(
6802     clang::DependentExtIntTypeLoc TL) {
6803   TL.setNameLoc(readSourceLocation());
6804 }
6805 
6806 
6807 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6808   TypeLocReader TLR(*this);
6809   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6810     TLR.Visit(TL);
6811 }
6812 
6813 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6814   QualType InfoTy = readType();
6815   if (InfoTy.isNull())
6816     return nullptr;
6817 
6818   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6819   readTypeLoc(TInfo->getTypeLoc());
6820   return TInfo;
6821 }
6822 
6823 QualType ASTReader::GetType(TypeID ID) {
6824   assert(ContextObj && "reading type with no AST context");
6825   ASTContext &Context = *ContextObj;
6826 
6827   unsigned FastQuals = ID & Qualifiers::FastMask;
6828   unsigned Index = ID >> Qualifiers::FastWidth;
6829 
6830   if (Index < NUM_PREDEF_TYPE_IDS) {
6831     QualType T;
6832     switch ((PredefinedTypeIDs)Index) {
6833     case PREDEF_TYPE_NULL_ID:
6834       return QualType();
6835     case PREDEF_TYPE_VOID_ID:
6836       T = Context.VoidTy;
6837       break;
6838     case PREDEF_TYPE_BOOL_ID:
6839       T = Context.BoolTy;
6840       break;
6841     case PREDEF_TYPE_CHAR_U_ID:
6842     case PREDEF_TYPE_CHAR_S_ID:
6843       // FIXME: Check that the signedness of CharTy is correct!
6844       T = Context.CharTy;
6845       break;
6846     case PREDEF_TYPE_UCHAR_ID:
6847       T = Context.UnsignedCharTy;
6848       break;
6849     case PREDEF_TYPE_USHORT_ID:
6850       T = Context.UnsignedShortTy;
6851       break;
6852     case PREDEF_TYPE_UINT_ID:
6853       T = Context.UnsignedIntTy;
6854       break;
6855     case PREDEF_TYPE_ULONG_ID:
6856       T = Context.UnsignedLongTy;
6857       break;
6858     case PREDEF_TYPE_ULONGLONG_ID:
6859       T = Context.UnsignedLongLongTy;
6860       break;
6861     case PREDEF_TYPE_UINT128_ID:
6862       T = Context.UnsignedInt128Ty;
6863       break;
6864     case PREDEF_TYPE_SCHAR_ID:
6865       T = Context.SignedCharTy;
6866       break;
6867     case PREDEF_TYPE_WCHAR_ID:
6868       T = Context.WCharTy;
6869       break;
6870     case PREDEF_TYPE_SHORT_ID:
6871       T = Context.ShortTy;
6872       break;
6873     case PREDEF_TYPE_INT_ID:
6874       T = Context.IntTy;
6875       break;
6876     case PREDEF_TYPE_LONG_ID:
6877       T = Context.LongTy;
6878       break;
6879     case PREDEF_TYPE_LONGLONG_ID:
6880       T = Context.LongLongTy;
6881       break;
6882     case PREDEF_TYPE_INT128_ID:
6883       T = Context.Int128Ty;
6884       break;
6885     case PREDEF_TYPE_BFLOAT16_ID:
6886       T = Context.BFloat16Ty;
6887       break;
6888     case PREDEF_TYPE_HALF_ID:
6889       T = Context.HalfTy;
6890       break;
6891     case PREDEF_TYPE_FLOAT_ID:
6892       T = Context.FloatTy;
6893       break;
6894     case PREDEF_TYPE_DOUBLE_ID:
6895       T = Context.DoubleTy;
6896       break;
6897     case PREDEF_TYPE_LONGDOUBLE_ID:
6898       T = Context.LongDoubleTy;
6899       break;
6900     case PREDEF_TYPE_SHORT_ACCUM_ID:
6901       T = Context.ShortAccumTy;
6902       break;
6903     case PREDEF_TYPE_ACCUM_ID:
6904       T = Context.AccumTy;
6905       break;
6906     case PREDEF_TYPE_LONG_ACCUM_ID:
6907       T = Context.LongAccumTy;
6908       break;
6909     case PREDEF_TYPE_USHORT_ACCUM_ID:
6910       T = Context.UnsignedShortAccumTy;
6911       break;
6912     case PREDEF_TYPE_UACCUM_ID:
6913       T = Context.UnsignedAccumTy;
6914       break;
6915     case PREDEF_TYPE_ULONG_ACCUM_ID:
6916       T = Context.UnsignedLongAccumTy;
6917       break;
6918     case PREDEF_TYPE_SHORT_FRACT_ID:
6919       T = Context.ShortFractTy;
6920       break;
6921     case PREDEF_TYPE_FRACT_ID:
6922       T = Context.FractTy;
6923       break;
6924     case PREDEF_TYPE_LONG_FRACT_ID:
6925       T = Context.LongFractTy;
6926       break;
6927     case PREDEF_TYPE_USHORT_FRACT_ID:
6928       T = Context.UnsignedShortFractTy;
6929       break;
6930     case PREDEF_TYPE_UFRACT_ID:
6931       T = Context.UnsignedFractTy;
6932       break;
6933     case PREDEF_TYPE_ULONG_FRACT_ID:
6934       T = Context.UnsignedLongFractTy;
6935       break;
6936     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6937       T = Context.SatShortAccumTy;
6938       break;
6939     case PREDEF_TYPE_SAT_ACCUM_ID:
6940       T = Context.SatAccumTy;
6941       break;
6942     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6943       T = Context.SatLongAccumTy;
6944       break;
6945     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6946       T = Context.SatUnsignedShortAccumTy;
6947       break;
6948     case PREDEF_TYPE_SAT_UACCUM_ID:
6949       T = Context.SatUnsignedAccumTy;
6950       break;
6951     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6952       T = Context.SatUnsignedLongAccumTy;
6953       break;
6954     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6955       T = Context.SatShortFractTy;
6956       break;
6957     case PREDEF_TYPE_SAT_FRACT_ID:
6958       T = Context.SatFractTy;
6959       break;
6960     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6961       T = Context.SatLongFractTy;
6962       break;
6963     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6964       T = Context.SatUnsignedShortFractTy;
6965       break;
6966     case PREDEF_TYPE_SAT_UFRACT_ID:
6967       T = Context.SatUnsignedFractTy;
6968       break;
6969     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6970       T = Context.SatUnsignedLongFractTy;
6971       break;
6972     case PREDEF_TYPE_FLOAT16_ID:
6973       T = Context.Float16Ty;
6974       break;
6975     case PREDEF_TYPE_FLOAT128_ID:
6976       T = Context.Float128Ty;
6977       break;
6978     case PREDEF_TYPE_OVERLOAD_ID:
6979       T = Context.OverloadTy;
6980       break;
6981     case PREDEF_TYPE_BOUND_MEMBER:
6982       T = Context.BoundMemberTy;
6983       break;
6984     case PREDEF_TYPE_PSEUDO_OBJECT:
6985       T = Context.PseudoObjectTy;
6986       break;
6987     case PREDEF_TYPE_DEPENDENT_ID:
6988       T = Context.DependentTy;
6989       break;
6990     case PREDEF_TYPE_UNKNOWN_ANY:
6991       T = Context.UnknownAnyTy;
6992       break;
6993     case PREDEF_TYPE_NULLPTR_ID:
6994       T = Context.NullPtrTy;
6995       break;
6996     case PREDEF_TYPE_CHAR8_ID:
6997       T = Context.Char8Ty;
6998       break;
6999     case PREDEF_TYPE_CHAR16_ID:
7000       T = Context.Char16Ty;
7001       break;
7002     case PREDEF_TYPE_CHAR32_ID:
7003       T = Context.Char32Ty;
7004       break;
7005     case PREDEF_TYPE_OBJC_ID:
7006       T = Context.ObjCBuiltinIdTy;
7007       break;
7008     case PREDEF_TYPE_OBJC_CLASS:
7009       T = Context.ObjCBuiltinClassTy;
7010       break;
7011     case PREDEF_TYPE_OBJC_SEL:
7012       T = Context.ObjCBuiltinSelTy;
7013       break;
7014 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7015     case PREDEF_TYPE_##Id##_ID: \
7016       T = Context.SingletonId; \
7017       break;
7018 #include "clang/Basic/OpenCLImageTypes.def"
7019 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7020     case PREDEF_TYPE_##Id##_ID: \
7021       T = Context.Id##Ty; \
7022       break;
7023 #include "clang/Basic/OpenCLExtensionTypes.def"
7024     case PREDEF_TYPE_SAMPLER_ID:
7025       T = Context.OCLSamplerTy;
7026       break;
7027     case PREDEF_TYPE_EVENT_ID:
7028       T = Context.OCLEventTy;
7029       break;
7030     case PREDEF_TYPE_CLK_EVENT_ID:
7031       T = Context.OCLClkEventTy;
7032       break;
7033     case PREDEF_TYPE_QUEUE_ID:
7034       T = Context.OCLQueueTy;
7035       break;
7036     case PREDEF_TYPE_RESERVE_ID_ID:
7037       T = Context.OCLReserveIDTy;
7038       break;
7039     case PREDEF_TYPE_AUTO_DEDUCT:
7040       T = Context.getAutoDeductType();
7041       break;
7042     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7043       T = Context.getAutoRRefDeductType();
7044       break;
7045     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7046       T = Context.ARCUnbridgedCastTy;
7047       break;
7048     case PREDEF_TYPE_BUILTIN_FN:
7049       T = Context.BuiltinFnTy;
7050       break;
7051     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7052       T = Context.IncompleteMatrixIdxTy;
7053       break;
7054     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7055       T = Context.OMPArraySectionTy;
7056       break;
7057     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7058       T = Context.OMPArraySectionTy;
7059       break;
7060     case PREDEF_TYPE_OMP_ITERATOR:
7061       T = Context.OMPIteratorTy;
7062       break;
7063 #define SVE_TYPE(Name, Id, SingletonId) \
7064     case PREDEF_TYPE_##Id##_ID: \
7065       T = Context.SingletonId; \
7066       break;
7067 #include "clang/Basic/AArch64SVEACLETypes.def"
7068 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7069     case PREDEF_TYPE_##Id##_ID: \
7070       T = Context.Id##Ty; \
7071       break;
7072 #include "clang/Basic/PPCTypes.def"
7073 #define RVV_TYPE(Name, Id, SingletonId) \
7074     case PREDEF_TYPE_##Id##_ID: \
7075       T = Context.SingletonId; \
7076       break;
7077 #include "clang/Basic/RISCVVTypes.def"
7078     }
7079 
7080     assert(!T.isNull() && "Unknown predefined type");
7081     return T.withFastQualifiers(FastQuals);
7082   }
7083 
7084   Index -= NUM_PREDEF_TYPE_IDS;
7085   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7086   if (TypesLoaded[Index].isNull()) {
7087     TypesLoaded[Index] = readTypeRecord(Index);
7088     if (TypesLoaded[Index].isNull())
7089       return QualType();
7090 
7091     TypesLoaded[Index]->setFromAST();
7092     if (DeserializationListener)
7093       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7094                                         TypesLoaded[Index]);
7095   }
7096 
7097   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7098 }
7099 
7100 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7101   return GetType(getGlobalTypeID(F, LocalID));
7102 }
7103 
7104 serialization::TypeID
7105 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7106   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7107   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7108 
7109   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7110     return LocalID;
7111 
7112   if (!F.ModuleOffsetMap.empty())
7113     ReadModuleOffsetMap(F);
7114 
7115   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7116     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7117   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7118 
7119   unsigned GlobalIndex = LocalIndex + I->second;
7120   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7121 }
7122 
7123 TemplateArgumentLocInfo
7124 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7125   switch (Kind) {
7126   case TemplateArgument::Expression:
7127     return readExpr();
7128   case TemplateArgument::Type:
7129     return readTypeSourceInfo();
7130   case TemplateArgument::Template: {
7131     NestedNameSpecifierLoc QualifierLoc =
7132       readNestedNameSpecifierLoc();
7133     SourceLocation TemplateNameLoc = readSourceLocation();
7134     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7135                                    TemplateNameLoc, SourceLocation());
7136   }
7137   case TemplateArgument::TemplateExpansion: {
7138     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7139     SourceLocation TemplateNameLoc = readSourceLocation();
7140     SourceLocation EllipsisLoc = readSourceLocation();
7141     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7142                                    TemplateNameLoc, EllipsisLoc);
7143   }
7144   case TemplateArgument::Null:
7145   case TemplateArgument::Integral:
7146   case TemplateArgument::Declaration:
7147   case TemplateArgument::NullPtr:
7148   case TemplateArgument::Pack:
7149     // FIXME: Is this right?
7150     return TemplateArgumentLocInfo();
7151   }
7152   llvm_unreachable("unexpected template argument loc");
7153 }
7154 
7155 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7156   TemplateArgument Arg = readTemplateArgument();
7157 
7158   if (Arg.getKind() == TemplateArgument::Expression) {
7159     if (readBool()) // bool InfoHasSameExpr.
7160       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7161   }
7162   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7163 }
7164 
7165 const ASTTemplateArgumentListInfo *
7166 ASTRecordReader::readASTTemplateArgumentListInfo() {
7167   SourceLocation LAngleLoc = readSourceLocation();
7168   SourceLocation RAngleLoc = readSourceLocation();
7169   unsigned NumArgsAsWritten = readInt();
7170   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7171   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7172     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7173   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7174 }
7175 
7176 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7177   return GetDecl(ID);
7178 }
7179 
7180 void ASTReader::CompleteRedeclChain(const Decl *D) {
7181   if (NumCurrentElementsDeserializing) {
7182     // We arrange to not care about the complete redeclaration chain while we're
7183     // deserializing. Just remember that the AST has marked this one as complete
7184     // but that it's not actually complete yet, so we know we still need to
7185     // complete it later.
7186     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7187     return;
7188   }
7189 
7190   if (!D->getDeclContext()) {
7191     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7192     return;
7193   }
7194 
7195   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7196 
7197   // If this is a named declaration, complete it by looking it up
7198   // within its context.
7199   //
7200   // FIXME: Merging a function definition should merge
7201   // all mergeable entities within it.
7202   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7203       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7204     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7205       if (!getContext().getLangOpts().CPlusPlus &&
7206           isa<TranslationUnitDecl>(DC)) {
7207         // Outside of C++, we don't have a lookup table for the TU, so update
7208         // the identifier instead. (For C++ modules, we don't store decls
7209         // in the serialized identifier table, so we do the lookup in the TU.)
7210         auto *II = Name.getAsIdentifierInfo();
7211         assert(II && "non-identifier name in C?");
7212         if (II->isOutOfDate())
7213           updateOutOfDateIdentifier(*II);
7214       } else
7215         DC->lookup(Name);
7216     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7217       // Find all declarations of this kind from the relevant context.
7218       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7219         auto *DC = cast<DeclContext>(DCDecl);
7220         SmallVector<Decl*, 8> Decls;
7221         FindExternalLexicalDecls(
7222             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7223       }
7224     }
7225   }
7226 
7227   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7228     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7229   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7230     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7231   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7232     if (auto *Template = FD->getPrimaryTemplate())
7233       Template->LoadLazySpecializations();
7234   }
7235 }
7236 
7237 CXXCtorInitializer **
7238 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7239   RecordLocation Loc = getLocalBitOffset(Offset);
7240   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7241   SavedStreamPosition SavedPosition(Cursor);
7242   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7243     Error(std::move(Err));
7244     return nullptr;
7245   }
7246   ReadingKindTracker ReadingKind(Read_Decl, *this);
7247 
7248   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7249   if (!MaybeCode) {
7250     Error(MaybeCode.takeError());
7251     return nullptr;
7252   }
7253   unsigned Code = MaybeCode.get();
7254 
7255   ASTRecordReader Record(*this, *Loc.F);
7256   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7257   if (!MaybeRecCode) {
7258     Error(MaybeRecCode.takeError());
7259     return nullptr;
7260   }
7261   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7262     Error("malformed AST file: missing C++ ctor initializers");
7263     return nullptr;
7264   }
7265 
7266   return Record.readCXXCtorInitializers();
7267 }
7268 
7269 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7270   assert(ContextObj && "reading base specifiers with no AST context");
7271   ASTContext &Context = *ContextObj;
7272 
7273   RecordLocation Loc = getLocalBitOffset(Offset);
7274   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7275   SavedStreamPosition SavedPosition(Cursor);
7276   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7277     Error(std::move(Err));
7278     return nullptr;
7279   }
7280   ReadingKindTracker ReadingKind(Read_Decl, *this);
7281 
7282   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7283   if (!MaybeCode) {
7284     Error(MaybeCode.takeError());
7285     return nullptr;
7286   }
7287   unsigned Code = MaybeCode.get();
7288 
7289   ASTRecordReader Record(*this, *Loc.F);
7290   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7291   if (!MaybeRecCode) {
7292     Error(MaybeCode.takeError());
7293     return nullptr;
7294   }
7295   unsigned RecCode = MaybeRecCode.get();
7296 
7297   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7298     Error("malformed AST file: missing C++ base specifiers");
7299     return nullptr;
7300   }
7301 
7302   unsigned NumBases = Record.readInt();
7303   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7304   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7305   for (unsigned I = 0; I != NumBases; ++I)
7306     Bases[I] = Record.readCXXBaseSpecifier();
7307   return Bases;
7308 }
7309 
7310 serialization::DeclID
7311 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7312   if (LocalID < NUM_PREDEF_DECL_IDS)
7313     return LocalID;
7314 
7315   if (!F.ModuleOffsetMap.empty())
7316     ReadModuleOffsetMap(F);
7317 
7318   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7319     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7320   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7321 
7322   return LocalID + I->second;
7323 }
7324 
7325 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7326                                    ModuleFile &M) const {
7327   // Predefined decls aren't from any module.
7328   if (ID < NUM_PREDEF_DECL_IDS)
7329     return false;
7330 
7331   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7332          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7333 }
7334 
7335 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7336   if (!D->isFromASTFile())
7337     return nullptr;
7338   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7339   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7340   return I->second;
7341 }
7342 
7343 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7344   if (ID < NUM_PREDEF_DECL_IDS)
7345     return SourceLocation();
7346 
7347   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7348 
7349   if (Index > DeclsLoaded.size()) {
7350     Error("declaration ID out-of-range for AST file");
7351     return SourceLocation();
7352   }
7353 
7354   if (Decl *D = DeclsLoaded[Index])
7355     return D->getLocation();
7356 
7357   SourceLocation Loc;
7358   DeclCursorForID(ID, Loc);
7359   return Loc;
7360 }
7361 
7362 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7363   switch (ID) {
7364   case PREDEF_DECL_NULL_ID:
7365     return nullptr;
7366 
7367   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7368     return Context.getTranslationUnitDecl();
7369 
7370   case PREDEF_DECL_OBJC_ID_ID:
7371     return Context.getObjCIdDecl();
7372 
7373   case PREDEF_DECL_OBJC_SEL_ID:
7374     return Context.getObjCSelDecl();
7375 
7376   case PREDEF_DECL_OBJC_CLASS_ID:
7377     return Context.getObjCClassDecl();
7378 
7379   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7380     return Context.getObjCProtocolDecl();
7381 
7382   case PREDEF_DECL_INT_128_ID:
7383     return Context.getInt128Decl();
7384 
7385   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7386     return Context.getUInt128Decl();
7387 
7388   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7389     return Context.getObjCInstanceTypeDecl();
7390 
7391   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7392     return Context.getBuiltinVaListDecl();
7393 
7394   case PREDEF_DECL_VA_LIST_TAG:
7395     return Context.getVaListTagDecl();
7396 
7397   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7398     return Context.getBuiltinMSVaListDecl();
7399 
7400   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7401     return Context.getMSGuidTagDecl();
7402 
7403   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7404     return Context.getExternCContextDecl();
7405 
7406   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7407     return Context.getMakeIntegerSeqDecl();
7408 
7409   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7410     return Context.getCFConstantStringDecl();
7411 
7412   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7413     return Context.getCFConstantStringTagDecl();
7414 
7415   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7416     return Context.getTypePackElementDecl();
7417   }
7418   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7419 }
7420 
7421 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7422   assert(ContextObj && "reading decl with no AST context");
7423   if (ID < NUM_PREDEF_DECL_IDS) {
7424     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7425     if (D) {
7426       // Track that we have merged the declaration with ID \p ID into the
7427       // pre-existing predefined declaration \p D.
7428       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7429       if (Merged.empty())
7430         Merged.push_back(ID);
7431     }
7432     return D;
7433   }
7434 
7435   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7436 
7437   if (Index >= DeclsLoaded.size()) {
7438     assert(0 && "declaration ID out-of-range for AST file");
7439     Error("declaration ID out-of-range for AST file");
7440     return nullptr;
7441   }
7442 
7443   return DeclsLoaded[Index];
7444 }
7445 
7446 Decl *ASTReader::GetDecl(DeclID ID) {
7447   if (ID < NUM_PREDEF_DECL_IDS)
7448     return GetExistingDecl(ID);
7449 
7450   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7451 
7452   if (Index >= DeclsLoaded.size()) {
7453     assert(0 && "declaration ID out-of-range for AST file");
7454     Error("declaration ID out-of-range for AST file");
7455     return nullptr;
7456   }
7457 
7458   if (!DeclsLoaded[Index]) {
7459     ReadDeclRecord(ID);
7460     if (DeserializationListener)
7461       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7462   }
7463 
7464   return DeclsLoaded[Index];
7465 }
7466 
7467 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7468                                                   DeclID GlobalID) {
7469   if (GlobalID < NUM_PREDEF_DECL_IDS)
7470     return GlobalID;
7471 
7472   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7473   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7474   ModuleFile *Owner = I->second;
7475 
7476   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7477     = M.GlobalToLocalDeclIDs.find(Owner);
7478   if (Pos == M.GlobalToLocalDeclIDs.end())
7479     return 0;
7480 
7481   return GlobalID - Owner->BaseDeclID + Pos->second;
7482 }
7483 
7484 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7485                                             const RecordData &Record,
7486                                             unsigned &Idx) {
7487   if (Idx >= Record.size()) {
7488     Error("Corrupted AST file");
7489     return 0;
7490   }
7491 
7492   return getGlobalDeclID(F, Record[Idx++]);
7493 }
7494 
7495 /// Resolve the offset of a statement into a statement.
7496 ///
7497 /// This operation will read a new statement from the external
7498 /// source each time it is called, and is meant to be used via a
7499 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7500 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7501   // Switch case IDs are per Decl.
7502   ClearSwitchCaseIDs();
7503 
7504   // Offset here is a global offset across the entire chain.
7505   RecordLocation Loc = getLocalBitOffset(Offset);
7506   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7507     Error(std::move(Err));
7508     return nullptr;
7509   }
7510   assert(NumCurrentElementsDeserializing == 0 &&
7511          "should not be called while already deserializing");
7512   Deserializing D(this);
7513   return ReadStmtFromStream(*Loc.F);
7514 }
7515 
7516 void ASTReader::FindExternalLexicalDecls(
7517     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7518     SmallVectorImpl<Decl *> &Decls) {
7519   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7520 
7521   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7522     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7523     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7524       auto K = (Decl::Kind)+LexicalDecls[I];
7525       if (!IsKindWeWant(K))
7526         continue;
7527 
7528       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7529 
7530       // Don't add predefined declarations to the lexical context more
7531       // than once.
7532       if (ID < NUM_PREDEF_DECL_IDS) {
7533         if (PredefsVisited[ID])
7534           continue;
7535 
7536         PredefsVisited[ID] = true;
7537       }
7538 
7539       if (Decl *D = GetLocalDecl(*M, ID)) {
7540         assert(D->getKind() == K && "wrong kind for lexical decl");
7541         if (!DC->isDeclInLexicalTraversal(D))
7542           Decls.push_back(D);
7543       }
7544     }
7545   };
7546 
7547   if (isa<TranslationUnitDecl>(DC)) {
7548     for (auto Lexical : TULexicalDecls)
7549       Visit(Lexical.first, Lexical.second);
7550   } else {
7551     auto I = LexicalDecls.find(DC);
7552     if (I != LexicalDecls.end())
7553       Visit(I->second.first, I->second.second);
7554   }
7555 
7556   ++NumLexicalDeclContextsRead;
7557 }
7558 
7559 namespace {
7560 
7561 class DeclIDComp {
7562   ASTReader &Reader;
7563   ModuleFile &Mod;
7564 
7565 public:
7566   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7567 
7568   bool operator()(LocalDeclID L, LocalDeclID R) const {
7569     SourceLocation LHS = getLocation(L);
7570     SourceLocation RHS = getLocation(R);
7571     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7572   }
7573 
7574   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7575     SourceLocation RHS = getLocation(R);
7576     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7577   }
7578 
7579   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7580     SourceLocation LHS = getLocation(L);
7581     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7582   }
7583 
7584   SourceLocation getLocation(LocalDeclID ID) const {
7585     return Reader.getSourceManager().getFileLoc(
7586             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7587   }
7588 };
7589 
7590 } // namespace
7591 
7592 void ASTReader::FindFileRegionDecls(FileID File,
7593                                     unsigned Offset, unsigned Length,
7594                                     SmallVectorImpl<Decl *> &Decls) {
7595   SourceManager &SM = getSourceManager();
7596 
7597   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7598   if (I == FileDeclIDs.end())
7599     return;
7600 
7601   FileDeclsInfo &DInfo = I->second;
7602   if (DInfo.Decls.empty())
7603     return;
7604 
7605   SourceLocation
7606     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7607   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7608 
7609   DeclIDComp DIDComp(*this, *DInfo.Mod);
7610   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7611       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7612   if (BeginIt != DInfo.Decls.begin())
7613     --BeginIt;
7614 
7615   // If we are pointing at a top-level decl inside an objc container, we need
7616   // to backtrack until we find it otherwise we will fail to report that the
7617   // region overlaps with an objc container.
7618   while (BeginIt != DInfo.Decls.begin() &&
7619          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7620              ->isTopLevelDeclInObjCContainer())
7621     --BeginIt;
7622 
7623   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7624       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7625   if (EndIt != DInfo.Decls.end())
7626     ++EndIt;
7627 
7628   for (ArrayRef<serialization::LocalDeclID>::iterator
7629          DIt = BeginIt; DIt != EndIt; ++DIt)
7630     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7631 }
7632 
7633 bool
7634 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7635                                           DeclarationName Name) {
7636   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7637          "DeclContext has no visible decls in storage");
7638   if (!Name)
7639     return false;
7640 
7641   auto It = Lookups.find(DC);
7642   if (It == Lookups.end())
7643     return false;
7644 
7645   Deserializing LookupResults(this);
7646 
7647   // Load the list of declarations.
7648   SmallVector<NamedDecl *, 64> Decls;
7649   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7650   for (DeclID ID : It->second.Table.find(Name)) {
7651     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7652     if (ND->getDeclName() == Name && Found.insert(ND).second)
7653       Decls.push_back(ND);
7654   }
7655 
7656   ++NumVisibleDeclContextsRead;
7657   SetExternalVisibleDeclsForName(DC, Name, Decls);
7658   return !Decls.empty();
7659 }
7660 
7661 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7662   if (!DC->hasExternalVisibleStorage())
7663     return;
7664 
7665   auto It = Lookups.find(DC);
7666   assert(It != Lookups.end() &&
7667          "have external visible storage but no lookup tables");
7668 
7669   DeclsMap Decls;
7670 
7671   for (DeclID ID : It->second.Table.findAll()) {
7672     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7673     Decls[ND->getDeclName()].push_back(ND);
7674   }
7675 
7676   ++NumVisibleDeclContextsRead;
7677 
7678   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7679     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7680   }
7681   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7682 }
7683 
7684 const serialization::reader::DeclContextLookupTable *
7685 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7686   auto I = Lookups.find(Primary);
7687   return I == Lookups.end() ? nullptr : &I->second;
7688 }
7689 
7690 /// Under non-PCH compilation the consumer receives the objc methods
7691 /// before receiving the implementation, and codegen depends on this.
7692 /// We simulate this by deserializing and passing to consumer the methods of the
7693 /// implementation before passing the deserialized implementation decl.
7694 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7695                                        ASTConsumer *Consumer) {
7696   assert(ImplD && Consumer);
7697 
7698   for (auto *I : ImplD->methods())
7699     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7700 
7701   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7702 }
7703 
7704 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7705   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7706     PassObjCImplDeclToConsumer(ImplD, Consumer);
7707   else
7708     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7709 }
7710 
7711 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7712   this->Consumer = Consumer;
7713 
7714   if (Consumer)
7715     PassInterestingDeclsToConsumer();
7716 
7717   if (DeserializationListener)
7718     DeserializationListener->ReaderInitialized(this);
7719 }
7720 
7721 void ASTReader::PrintStats() {
7722   std::fprintf(stderr, "*** AST File Statistics:\n");
7723 
7724   unsigned NumTypesLoaded
7725     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7726                                       QualType());
7727   unsigned NumDeclsLoaded
7728     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7729                                       (Decl *)nullptr);
7730   unsigned NumIdentifiersLoaded
7731     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7732                                             IdentifiersLoaded.end(),
7733                                             (IdentifierInfo *)nullptr);
7734   unsigned NumMacrosLoaded
7735     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7736                                        MacrosLoaded.end(),
7737                                        (MacroInfo *)nullptr);
7738   unsigned NumSelectorsLoaded
7739     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7740                                           SelectorsLoaded.end(),
7741                                           Selector());
7742 
7743   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7744     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7745                  NumSLocEntriesRead, TotalNumSLocEntries,
7746                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7747   if (!TypesLoaded.empty())
7748     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7749                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7750                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7751   if (!DeclsLoaded.empty())
7752     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7753                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7754                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7755   if (!IdentifiersLoaded.empty())
7756     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7757                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7758                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7759   if (!MacrosLoaded.empty())
7760     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7761                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7762                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7763   if (!SelectorsLoaded.empty())
7764     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7765                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7766                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7767   if (TotalNumStatements)
7768     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7769                  NumStatementsRead, TotalNumStatements,
7770                  ((float)NumStatementsRead/TotalNumStatements * 100));
7771   if (TotalNumMacros)
7772     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7773                  NumMacrosRead, TotalNumMacros,
7774                  ((float)NumMacrosRead/TotalNumMacros * 100));
7775   if (TotalLexicalDeclContexts)
7776     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7777                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7778                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7779                   * 100));
7780   if (TotalVisibleDeclContexts)
7781     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7782                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7783                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7784                   * 100));
7785   if (TotalNumMethodPoolEntries)
7786     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7787                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7788                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7789                   * 100));
7790   if (NumMethodPoolLookups)
7791     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7792                  NumMethodPoolHits, NumMethodPoolLookups,
7793                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7794   if (NumMethodPoolTableLookups)
7795     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7796                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7797                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7798                   * 100.0));
7799   if (NumIdentifierLookupHits)
7800     std::fprintf(stderr,
7801                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7802                  NumIdentifierLookupHits, NumIdentifierLookups,
7803                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7804 
7805   if (GlobalIndex) {
7806     std::fprintf(stderr, "\n");
7807     GlobalIndex->printStats();
7808   }
7809 
7810   std::fprintf(stderr, "\n");
7811   dump();
7812   std::fprintf(stderr, "\n");
7813 }
7814 
7815 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7816 LLVM_DUMP_METHOD static void
7817 dumpModuleIDMap(StringRef Name,
7818                 const ContinuousRangeMap<Key, ModuleFile *,
7819                                          InitialCapacity> &Map) {
7820   if (Map.begin() == Map.end())
7821     return;
7822 
7823   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7824 
7825   llvm::errs() << Name << ":\n";
7826   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7827        I != IEnd; ++I) {
7828     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7829       << "\n";
7830   }
7831 }
7832 
7833 LLVM_DUMP_METHOD void ASTReader::dump() {
7834   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7835   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7836   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7837   dumpModuleIDMap("Global type map", GlobalTypeMap);
7838   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7839   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7840   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7841   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7842   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7843   dumpModuleIDMap("Global preprocessed entity map",
7844                   GlobalPreprocessedEntityMap);
7845 
7846   llvm::errs() << "\n*** PCH/Modules Loaded:";
7847   for (ModuleFile &M : ModuleMgr)
7848     M.dump();
7849 }
7850 
7851 /// Return the amount of memory used by memory buffers, breaking down
7852 /// by heap-backed versus mmap'ed memory.
7853 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7854   for (ModuleFile &I : ModuleMgr) {
7855     if (llvm::MemoryBuffer *buf = I.Buffer) {
7856       size_t bytes = buf->getBufferSize();
7857       switch (buf->getBufferKind()) {
7858         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7859           sizes.malloc_bytes += bytes;
7860           break;
7861         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7862           sizes.mmap_bytes += bytes;
7863           break;
7864       }
7865     }
7866   }
7867 }
7868 
7869 void ASTReader::InitializeSema(Sema &S) {
7870   SemaObj = &S;
7871   S.addExternalSource(this);
7872 
7873   // Makes sure any declarations that were deserialized "too early"
7874   // still get added to the identifier's declaration chains.
7875   for (uint64_t ID : PreloadedDeclIDs) {
7876     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7877     pushExternalDeclIntoScope(D, D->getDeclName());
7878   }
7879   PreloadedDeclIDs.clear();
7880 
7881   // FIXME: What happens if these are changed by a module import?
7882   if (!FPPragmaOptions.empty()) {
7883     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7884     FPOptionsOverride NewOverrides =
7885         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7886     SemaObj->CurFPFeatures =
7887         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7888   }
7889 
7890   SemaObj->OpenCLFeatures = OpenCLExtensions;
7891 
7892   UpdateSema();
7893 }
7894 
7895 void ASTReader::UpdateSema() {
7896   assert(SemaObj && "no Sema to update");
7897 
7898   // Load the offsets of the declarations that Sema references.
7899   // They will be lazily deserialized when needed.
7900   if (!SemaDeclRefs.empty()) {
7901     assert(SemaDeclRefs.size() % 3 == 0);
7902     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7903       if (!SemaObj->StdNamespace)
7904         SemaObj->StdNamespace = SemaDeclRefs[I];
7905       if (!SemaObj->StdBadAlloc)
7906         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7907       if (!SemaObj->StdAlignValT)
7908         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7909     }
7910     SemaDeclRefs.clear();
7911   }
7912 
7913   // Update the state of pragmas. Use the same API as if we had encountered the
7914   // pragma in the source.
7915   if(OptimizeOffPragmaLocation.isValid())
7916     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7917   if (PragmaMSStructState != -1)
7918     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7919   if (PointersToMembersPragmaLocation.isValid()) {
7920     SemaObj->ActOnPragmaMSPointersToMembers(
7921         (LangOptions::PragmaMSPointersToMembersKind)
7922             PragmaMSPointersToMembersState,
7923         PointersToMembersPragmaLocation);
7924   }
7925   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7926 
7927   if (PragmaAlignPackCurrentValue) {
7928     // The bottom of the stack might have a default value. It must be adjusted
7929     // to the current value to ensure that the packing state is preserved after
7930     // popping entries that were included/imported from a PCH/module.
7931     bool DropFirst = false;
7932     if (!PragmaAlignPackStack.empty() &&
7933         PragmaAlignPackStack.front().Location.isInvalid()) {
7934       assert(PragmaAlignPackStack.front().Value ==
7935                  SemaObj->AlignPackStack.DefaultValue &&
7936              "Expected a default alignment value");
7937       SemaObj->AlignPackStack.Stack.emplace_back(
7938           PragmaAlignPackStack.front().SlotLabel,
7939           SemaObj->AlignPackStack.CurrentValue,
7940           SemaObj->AlignPackStack.CurrentPragmaLocation,
7941           PragmaAlignPackStack.front().PushLocation);
7942       DropFirst = true;
7943     }
7944     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7945                                  .drop_front(DropFirst ? 1 : 0)) {
7946       SemaObj->AlignPackStack.Stack.emplace_back(
7947           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7948     }
7949     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7950       assert(*PragmaAlignPackCurrentValue ==
7951                  SemaObj->AlignPackStack.DefaultValue &&
7952              "Expected a default align and pack value");
7953       // Keep the current values.
7954     } else {
7955       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7956       SemaObj->AlignPackStack.CurrentPragmaLocation =
7957           PragmaAlignPackCurrentLocation;
7958     }
7959   }
7960   if (FpPragmaCurrentValue) {
7961     // The bottom of the stack might have a default value. It must be adjusted
7962     // to the current value to ensure that fp-pragma state is preserved after
7963     // popping entries that were included/imported from a PCH/module.
7964     bool DropFirst = false;
7965     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7966       assert(FpPragmaStack.front().Value ==
7967                  SemaObj->FpPragmaStack.DefaultValue &&
7968              "Expected a default pragma float_control value");
7969       SemaObj->FpPragmaStack.Stack.emplace_back(
7970           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7971           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7972           FpPragmaStack.front().PushLocation);
7973       DropFirst = true;
7974     }
7975     for (const auto &Entry :
7976          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7977       SemaObj->FpPragmaStack.Stack.emplace_back(
7978           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7979     if (FpPragmaCurrentLocation.isInvalid()) {
7980       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7981              "Expected a default pragma float_control value");
7982       // Keep the current values.
7983     } else {
7984       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7985       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7986     }
7987   }
7988 
7989   // For non-modular AST files, restore visiblity of modules.
7990   for (auto &Import : ImportedModules) {
7991     if (Import.ImportLoc.isInvalid())
7992       continue;
7993     if (Module *Imported = getSubmodule(Import.ID)) {
7994       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7995     }
7996   }
7997 }
7998 
7999 IdentifierInfo *ASTReader::get(StringRef Name) {
8000   // Note that we are loading an identifier.
8001   Deserializing AnIdentifier(this);
8002 
8003   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8004                                   NumIdentifierLookups,
8005                                   NumIdentifierLookupHits);
8006 
8007   // We don't need to do identifier table lookups in C++ modules (we preload
8008   // all interesting declarations, and don't need to use the scope for name
8009   // lookups). Perform the lookup in PCH files, though, since we don't build
8010   // a complete initial identifier table if we're carrying on from a PCH.
8011   if (PP.getLangOpts().CPlusPlus) {
8012     for (auto F : ModuleMgr.pch_modules())
8013       if (Visitor(*F))
8014         break;
8015   } else {
8016     // If there is a global index, look there first to determine which modules
8017     // provably do not have any results for this identifier.
8018     GlobalModuleIndex::HitSet Hits;
8019     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8020     if (!loadGlobalIndex()) {
8021       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8022         HitsPtr = &Hits;
8023       }
8024     }
8025 
8026     ModuleMgr.visit(Visitor, HitsPtr);
8027   }
8028 
8029   IdentifierInfo *II = Visitor.getIdentifierInfo();
8030   markIdentifierUpToDate(II);
8031   return II;
8032 }
8033 
8034 namespace clang {
8035 
8036   /// An identifier-lookup iterator that enumerates all of the
8037   /// identifiers stored within a set of AST files.
8038   class ASTIdentifierIterator : public IdentifierIterator {
8039     /// The AST reader whose identifiers are being enumerated.
8040     const ASTReader &Reader;
8041 
8042     /// The current index into the chain of AST files stored in
8043     /// the AST reader.
8044     unsigned Index;
8045 
8046     /// The current position within the identifier lookup table
8047     /// of the current AST file.
8048     ASTIdentifierLookupTable::key_iterator Current;
8049 
8050     /// The end position within the identifier lookup table of
8051     /// the current AST file.
8052     ASTIdentifierLookupTable::key_iterator End;
8053 
8054     /// Whether to skip any modules in the ASTReader.
8055     bool SkipModules;
8056 
8057   public:
8058     explicit ASTIdentifierIterator(const ASTReader &Reader,
8059                                    bool SkipModules = false);
8060 
8061     StringRef Next() override;
8062   };
8063 
8064 } // namespace clang
8065 
8066 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8067                                              bool SkipModules)
8068     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8069 }
8070 
8071 StringRef ASTIdentifierIterator::Next() {
8072   while (Current == End) {
8073     // If we have exhausted all of our AST files, we're done.
8074     if (Index == 0)
8075       return StringRef();
8076 
8077     --Index;
8078     ModuleFile &F = Reader.ModuleMgr[Index];
8079     if (SkipModules && F.isModule())
8080       continue;
8081 
8082     ASTIdentifierLookupTable *IdTable =
8083         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8084     Current = IdTable->key_begin();
8085     End = IdTable->key_end();
8086   }
8087 
8088   // We have any identifiers remaining in the current AST file; return
8089   // the next one.
8090   StringRef Result = *Current;
8091   ++Current;
8092   return Result;
8093 }
8094 
8095 namespace {
8096 
8097 /// A utility for appending two IdentifierIterators.
8098 class ChainedIdentifierIterator : public IdentifierIterator {
8099   std::unique_ptr<IdentifierIterator> Current;
8100   std::unique_ptr<IdentifierIterator> Queued;
8101 
8102 public:
8103   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8104                             std::unique_ptr<IdentifierIterator> Second)
8105       : Current(std::move(First)), Queued(std::move(Second)) {}
8106 
8107   StringRef Next() override {
8108     if (!Current)
8109       return StringRef();
8110 
8111     StringRef result = Current->Next();
8112     if (!result.empty())
8113       return result;
8114 
8115     // Try the queued iterator, which may itself be empty.
8116     Current.reset();
8117     std::swap(Current, Queued);
8118     return Next();
8119   }
8120 };
8121 
8122 } // namespace
8123 
8124 IdentifierIterator *ASTReader::getIdentifiers() {
8125   if (!loadGlobalIndex()) {
8126     std::unique_ptr<IdentifierIterator> ReaderIter(
8127         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8128     std::unique_ptr<IdentifierIterator> ModulesIter(
8129         GlobalIndex->createIdentifierIterator());
8130     return new ChainedIdentifierIterator(std::move(ReaderIter),
8131                                          std::move(ModulesIter));
8132   }
8133 
8134   return new ASTIdentifierIterator(*this);
8135 }
8136 
8137 namespace clang {
8138 namespace serialization {
8139 
8140   class ReadMethodPoolVisitor {
8141     ASTReader &Reader;
8142     Selector Sel;
8143     unsigned PriorGeneration;
8144     unsigned InstanceBits = 0;
8145     unsigned FactoryBits = 0;
8146     bool InstanceHasMoreThanOneDecl = false;
8147     bool FactoryHasMoreThanOneDecl = false;
8148     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8149     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8150 
8151   public:
8152     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8153                           unsigned PriorGeneration)
8154         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8155 
8156     bool operator()(ModuleFile &M) {
8157       if (!M.SelectorLookupTable)
8158         return false;
8159 
8160       // If we've already searched this module file, skip it now.
8161       if (M.Generation <= PriorGeneration)
8162         return true;
8163 
8164       ++Reader.NumMethodPoolTableLookups;
8165       ASTSelectorLookupTable *PoolTable
8166         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8167       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8168       if (Pos == PoolTable->end())
8169         return false;
8170 
8171       ++Reader.NumMethodPoolTableHits;
8172       ++Reader.NumSelectorsRead;
8173       // FIXME: Not quite happy with the statistics here. We probably should
8174       // disable this tracking when called via LoadSelector.
8175       // Also, should entries without methods count as misses?
8176       ++Reader.NumMethodPoolEntriesRead;
8177       ASTSelectorLookupTrait::data_type Data = *Pos;
8178       if (Reader.DeserializationListener)
8179         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8180 
8181       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8182       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8183       InstanceBits = Data.InstanceBits;
8184       FactoryBits = Data.FactoryBits;
8185       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8186       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8187       return true;
8188     }
8189 
8190     /// Retrieve the instance methods found by this visitor.
8191     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8192       return InstanceMethods;
8193     }
8194 
8195     /// Retrieve the instance methods found by this visitor.
8196     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8197       return FactoryMethods;
8198     }
8199 
8200     unsigned getInstanceBits() const { return InstanceBits; }
8201     unsigned getFactoryBits() const { return FactoryBits; }
8202 
8203     bool instanceHasMoreThanOneDecl() const {
8204       return InstanceHasMoreThanOneDecl;
8205     }
8206 
8207     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8208   };
8209 
8210 } // namespace serialization
8211 } // namespace clang
8212 
8213 /// Add the given set of methods to the method list.
8214 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8215                              ObjCMethodList &List) {
8216   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8217     S.addMethodToGlobalList(&List, Methods[I]);
8218   }
8219 }
8220 
8221 void ASTReader::ReadMethodPool(Selector Sel) {
8222   // Get the selector generation and update it to the current generation.
8223   unsigned &Generation = SelectorGeneration[Sel];
8224   unsigned PriorGeneration = Generation;
8225   Generation = getGeneration();
8226   SelectorOutOfDate[Sel] = false;
8227 
8228   // Search for methods defined with this selector.
8229   ++NumMethodPoolLookups;
8230   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8231   ModuleMgr.visit(Visitor);
8232 
8233   if (Visitor.getInstanceMethods().empty() &&
8234       Visitor.getFactoryMethods().empty())
8235     return;
8236 
8237   ++NumMethodPoolHits;
8238 
8239   if (!getSema())
8240     return;
8241 
8242   Sema &S = *getSema();
8243   Sema::GlobalMethodPool::iterator Pos
8244     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8245 
8246   Pos->second.first.setBits(Visitor.getInstanceBits());
8247   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8248   Pos->second.second.setBits(Visitor.getFactoryBits());
8249   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8250 
8251   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8252   // when building a module we keep every method individually and may need to
8253   // update hasMoreThanOneDecl as we add the methods.
8254   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8255   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8256 }
8257 
8258 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8259   if (SelectorOutOfDate[Sel])
8260     ReadMethodPool(Sel);
8261 }
8262 
8263 void ASTReader::ReadKnownNamespaces(
8264                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8265   Namespaces.clear();
8266 
8267   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8268     if (NamespaceDecl *Namespace
8269                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8270       Namespaces.push_back(Namespace);
8271   }
8272 }
8273 
8274 void ASTReader::ReadUndefinedButUsed(
8275     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8276   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8277     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8278     SourceLocation Loc =
8279         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8280     Undefined.insert(std::make_pair(D, Loc));
8281   }
8282 }
8283 
8284 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8285     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8286                                                      Exprs) {
8287   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8288     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8289     uint64_t Count = DelayedDeleteExprs[Idx++];
8290     for (uint64_t C = 0; C < Count; ++C) {
8291       SourceLocation DeleteLoc =
8292           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8293       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8294       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8295     }
8296   }
8297 }
8298 
8299 void ASTReader::ReadTentativeDefinitions(
8300                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8301   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8302     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8303     if (Var)
8304       TentativeDefs.push_back(Var);
8305   }
8306   TentativeDefinitions.clear();
8307 }
8308 
8309 void ASTReader::ReadUnusedFileScopedDecls(
8310                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8311   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8312     DeclaratorDecl *D
8313       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8314     if (D)
8315       Decls.push_back(D);
8316   }
8317   UnusedFileScopedDecls.clear();
8318 }
8319 
8320 void ASTReader::ReadDelegatingConstructors(
8321                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8322   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8323     CXXConstructorDecl *D
8324       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8325     if (D)
8326       Decls.push_back(D);
8327   }
8328   DelegatingCtorDecls.clear();
8329 }
8330 
8331 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8332   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8333     TypedefNameDecl *D
8334       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8335     if (D)
8336       Decls.push_back(D);
8337   }
8338   ExtVectorDecls.clear();
8339 }
8340 
8341 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8342     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8343   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8344        ++I) {
8345     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8346         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8347     if (D)
8348       Decls.insert(D);
8349   }
8350   UnusedLocalTypedefNameCandidates.clear();
8351 }
8352 
8353 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8354     llvm::SmallSetVector<Decl *, 4> &Decls) {
8355   for (auto I : DeclsToCheckForDeferredDiags) {
8356     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8357     if (D)
8358       Decls.insert(D);
8359   }
8360   DeclsToCheckForDeferredDiags.clear();
8361 }
8362 
8363 void ASTReader::ReadReferencedSelectors(
8364        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8365   if (ReferencedSelectorsData.empty())
8366     return;
8367 
8368   // If there are @selector references added them to its pool. This is for
8369   // implementation of -Wselector.
8370   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8371   unsigned I = 0;
8372   while (I < DataSize) {
8373     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8374     SourceLocation SelLoc
8375       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8376     Sels.push_back(std::make_pair(Sel, SelLoc));
8377   }
8378   ReferencedSelectorsData.clear();
8379 }
8380 
8381 void ASTReader::ReadWeakUndeclaredIdentifiers(
8382        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8383   if (WeakUndeclaredIdentifiers.empty())
8384     return;
8385 
8386   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8387     IdentifierInfo *WeakId
8388       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8389     IdentifierInfo *AliasId
8390       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8391     SourceLocation Loc
8392       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8393     bool Used = WeakUndeclaredIdentifiers[I++];
8394     WeakInfo WI(AliasId, Loc);
8395     WI.setUsed(Used);
8396     WeakIDs.push_back(std::make_pair(WeakId, WI));
8397   }
8398   WeakUndeclaredIdentifiers.clear();
8399 }
8400 
8401 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8402   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8403     ExternalVTableUse VT;
8404     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8405     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8406     VT.DefinitionRequired = VTableUses[Idx++];
8407     VTables.push_back(VT);
8408   }
8409 
8410   VTableUses.clear();
8411 }
8412 
8413 void ASTReader::ReadPendingInstantiations(
8414        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8415   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8416     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8417     SourceLocation Loc
8418       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8419 
8420     Pending.push_back(std::make_pair(D, Loc));
8421   }
8422   PendingInstantiations.clear();
8423 }
8424 
8425 void ASTReader::ReadLateParsedTemplates(
8426     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8427         &LPTMap) {
8428   for (auto &LPT : LateParsedTemplates) {
8429     ModuleFile *FMod = LPT.first;
8430     RecordDataImpl &LateParsed = LPT.second;
8431     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8432          /* In loop */) {
8433       FunctionDecl *FD =
8434           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8435 
8436       auto LT = std::make_unique<LateParsedTemplate>();
8437       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8438 
8439       ModuleFile *F = getOwningModuleFile(LT->D);
8440       assert(F && "No module");
8441 
8442       unsigned TokN = LateParsed[Idx++];
8443       LT->Toks.reserve(TokN);
8444       for (unsigned T = 0; T < TokN; ++T)
8445         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8446 
8447       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8448     }
8449   }
8450 }
8451 
8452 void ASTReader::LoadSelector(Selector Sel) {
8453   // It would be complicated to avoid reading the methods anyway. So don't.
8454   ReadMethodPool(Sel);
8455 }
8456 
8457 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8458   assert(ID && "Non-zero identifier ID required");
8459   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8460   IdentifiersLoaded[ID - 1] = II;
8461   if (DeserializationListener)
8462     DeserializationListener->IdentifierRead(ID, II);
8463 }
8464 
8465 /// Set the globally-visible declarations associated with the given
8466 /// identifier.
8467 ///
8468 /// If the AST reader is currently in a state where the given declaration IDs
8469 /// cannot safely be resolved, they are queued until it is safe to resolve
8470 /// them.
8471 ///
8472 /// \param II an IdentifierInfo that refers to one or more globally-visible
8473 /// declarations.
8474 ///
8475 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8476 /// visible at global scope.
8477 ///
8478 /// \param Decls if non-null, this vector will be populated with the set of
8479 /// deserialized declarations. These declarations will not be pushed into
8480 /// scope.
8481 void
8482 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8483                               const SmallVectorImpl<uint32_t> &DeclIDs,
8484                                    SmallVectorImpl<Decl *> *Decls) {
8485   if (NumCurrentElementsDeserializing && !Decls) {
8486     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8487     return;
8488   }
8489 
8490   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8491     if (!SemaObj) {
8492       // Queue this declaration so that it will be added to the
8493       // translation unit scope and identifier's declaration chain
8494       // once a Sema object is known.
8495       PreloadedDeclIDs.push_back(DeclIDs[I]);
8496       continue;
8497     }
8498 
8499     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8500 
8501     // If we're simply supposed to record the declarations, do so now.
8502     if (Decls) {
8503       Decls->push_back(D);
8504       continue;
8505     }
8506 
8507     // Introduce this declaration into the translation-unit scope
8508     // and add it to the declaration chain for this identifier, so
8509     // that (unqualified) name lookup will find it.
8510     pushExternalDeclIntoScope(D, II);
8511   }
8512 }
8513 
8514 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8515   if (ID == 0)
8516     return nullptr;
8517 
8518   if (IdentifiersLoaded.empty()) {
8519     Error("no identifier table in AST file");
8520     return nullptr;
8521   }
8522 
8523   ID -= 1;
8524   if (!IdentifiersLoaded[ID]) {
8525     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8526     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8527     ModuleFile *M = I->second;
8528     unsigned Index = ID - M->BaseIdentifierID;
8529     const unsigned char *Data =
8530         M->IdentifierTableData + M->IdentifierOffsets[Index];
8531 
8532     ASTIdentifierLookupTrait Trait(*this, *M);
8533     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8534     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8535     auto &II = PP.getIdentifierTable().get(Key);
8536     IdentifiersLoaded[ID] = &II;
8537     markIdentifierFromAST(*this,  II);
8538     if (DeserializationListener)
8539       DeserializationListener->IdentifierRead(ID + 1, &II);
8540   }
8541 
8542   return IdentifiersLoaded[ID];
8543 }
8544 
8545 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8546   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8547 }
8548 
8549 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8550   if (LocalID < NUM_PREDEF_IDENT_IDS)
8551     return LocalID;
8552 
8553   if (!M.ModuleOffsetMap.empty())
8554     ReadModuleOffsetMap(M);
8555 
8556   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8557     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8558   assert(I != M.IdentifierRemap.end()
8559          && "Invalid index into identifier index remap");
8560 
8561   return LocalID + I->second;
8562 }
8563 
8564 MacroInfo *ASTReader::getMacro(MacroID ID) {
8565   if (ID == 0)
8566     return nullptr;
8567 
8568   if (MacrosLoaded.empty()) {
8569     Error("no macro table in AST file");
8570     return nullptr;
8571   }
8572 
8573   ID -= NUM_PREDEF_MACRO_IDS;
8574   if (!MacrosLoaded[ID]) {
8575     GlobalMacroMapType::iterator I
8576       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8577     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8578     ModuleFile *M = I->second;
8579     unsigned Index = ID - M->BaseMacroID;
8580     MacrosLoaded[ID] =
8581         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8582 
8583     if (DeserializationListener)
8584       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8585                                          MacrosLoaded[ID]);
8586   }
8587 
8588   return MacrosLoaded[ID];
8589 }
8590 
8591 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8592   if (LocalID < NUM_PREDEF_MACRO_IDS)
8593     return LocalID;
8594 
8595   if (!M.ModuleOffsetMap.empty())
8596     ReadModuleOffsetMap(M);
8597 
8598   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8599     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8600   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8601 
8602   return LocalID + I->second;
8603 }
8604 
8605 serialization::SubmoduleID
8606 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8607   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8608     return LocalID;
8609 
8610   if (!M.ModuleOffsetMap.empty())
8611     ReadModuleOffsetMap(M);
8612 
8613   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8614     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8615   assert(I != M.SubmoduleRemap.end()
8616          && "Invalid index into submodule index remap");
8617 
8618   return LocalID + I->second;
8619 }
8620 
8621 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8622   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8623     assert(GlobalID == 0 && "Unhandled global submodule ID");
8624     return nullptr;
8625   }
8626 
8627   if (GlobalID > SubmodulesLoaded.size()) {
8628     Error("submodule ID out of range in AST file");
8629     return nullptr;
8630   }
8631 
8632   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8633 }
8634 
8635 Module *ASTReader::getModule(unsigned ID) {
8636   return getSubmodule(ID);
8637 }
8638 
8639 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8640   if (ID & 1) {
8641     // It's a module, look it up by submodule ID.
8642     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8643     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8644   } else {
8645     // It's a prefix (preamble, PCH, ...). Look it up by index.
8646     unsigned IndexFromEnd = ID >> 1;
8647     assert(IndexFromEnd && "got reference to unknown module file");
8648     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8649   }
8650 }
8651 
8652 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8653   if (!F)
8654     return 1;
8655 
8656   // For a file representing a module, use the submodule ID of the top-level
8657   // module as the file ID. For any other kind of file, the number of such
8658   // files loaded beforehand will be the same on reload.
8659   // FIXME: Is this true even if we have an explicit module file and a PCH?
8660   if (F->isModule())
8661     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8662 
8663   auto PCHModules = getModuleManager().pch_modules();
8664   auto I = llvm::find(PCHModules, F);
8665   assert(I != PCHModules.end() && "emitting reference to unknown file");
8666   return (I - PCHModules.end()) << 1;
8667 }
8668 
8669 llvm::Optional<ASTSourceDescriptor>
8670 ASTReader::getSourceDescriptor(unsigned ID) {
8671   if (Module *M = getSubmodule(ID))
8672     return ASTSourceDescriptor(*M);
8673 
8674   // If there is only a single PCH, return it instead.
8675   // Chained PCH are not supported.
8676   const auto &PCHChain = ModuleMgr.pch_modules();
8677   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8678     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8679     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8680     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8681     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8682                                MF.Signature);
8683   }
8684   return None;
8685 }
8686 
8687 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8688   auto I = DefinitionSource.find(FD);
8689   if (I == DefinitionSource.end())
8690     return EK_ReplyHazy;
8691   return I->second ? EK_Never : EK_Always;
8692 }
8693 
8694 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8695   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8696 }
8697 
8698 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8699   if (ID == 0)
8700     return Selector();
8701 
8702   if (ID > SelectorsLoaded.size()) {
8703     Error("selector ID out of range in AST file");
8704     return Selector();
8705   }
8706 
8707   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8708     // Load this selector from the selector table.
8709     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8710     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8711     ModuleFile &M = *I->second;
8712     ASTSelectorLookupTrait Trait(*this, M);
8713     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8714     SelectorsLoaded[ID - 1] =
8715       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8716     if (DeserializationListener)
8717       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8718   }
8719 
8720   return SelectorsLoaded[ID - 1];
8721 }
8722 
8723 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8724   return DecodeSelector(ID);
8725 }
8726 
8727 uint32_t ASTReader::GetNumExternalSelectors() {
8728   // ID 0 (the null selector) is considered an external selector.
8729   return getTotalNumSelectors() + 1;
8730 }
8731 
8732 serialization::SelectorID
8733 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8734   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8735     return LocalID;
8736 
8737   if (!M.ModuleOffsetMap.empty())
8738     ReadModuleOffsetMap(M);
8739 
8740   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8741     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8742   assert(I != M.SelectorRemap.end()
8743          && "Invalid index into selector index remap");
8744 
8745   return LocalID + I->second;
8746 }
8747 
8748 DeclarationNameLoc
8749 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8750   switch (Name.getNameKind()) {
8751   case DeclarationName::CXXConstructorName:
8752   case DeclarationName::CXXDestructorName:
8753   case DeclarationName::CXXConversionFunctionName:
8754     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8755 
8756   case DeclarationName::CXXOperatorName:
8757     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8758 
8759   case DeclarationName::CXXLiteralOperatorName:
8760     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8761         readSourceLocation());
8762 
8763   case DeclarationName::Identifier:
8764   case DeclarationName::ObjCZeroArgSelector:
8765   case DeclarationName::ObjCOneArgSelector:
8766   case DeclarationName::ObjCMultiArgSelector:
8767   case DeclarationName::CXXUsingDirective:
8768   case DeclarationName::CXXDeductionGuideName:
8769     break;
8770   }
8771   return DeclarationNameLoc();
8772 }
8773 
8774 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8775   DeclarationNameInfo NameInfo;
8776   NameInfo.setName(readDeclarationName());
8777   NameInfo.setLoc(readSourceLocation());
8778   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8779   return NameInfo;
8780 }
8781 
8782 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8783   Info.QualifierLoc = readNestedNameSpecifierLoc();
8784   unsigned NumTPLists = readInt();
8785   Info.NumTemplParamLists = NumTPLists;
8786   if (NumTPLists) {
8787     Info.TemplParamLists =
8788         new (getContext()) TemplateParameterList *[NumTPLists];
8789     for (unsigned i = 0; i != NumTPLists; ++i)
8790       Info.TemplParamLists[i] = readTemplateParameterList();
8791   }
8792 }
8793 
8794 TemplateParameterList *
8795 ASTRecordReader::readTemplateParameterList() {
8796   SourceLocation TemplateLoc = readSourceLocation();
8797   SourceLocation LAngleLoc = readSourceLocation();
8798   SourceLocation RAngleLoc = readSourceLocation();
8799 
8800   unsigned NumParams = readInt();
8801   SmallVector<NamedDecl *, 16> Params;
8802   Params.reserve(NumParams);
8803   while (NumParams--)
8804     Params.push_back(readDeclAs<NamedDecl>());
8805 
8806   bool HasRequiresClause = readBool();
8807   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8808 
8809   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8810       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8811   return TemplateParams;
8812 }
8813 
8814 void ASTRecordReader::readTemplateArgumentList(
8815                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8816                         bool Canonicalize) {
8817   unsigned NumTemplateArgs = readInt();
8818   TemplArgs.reserve(NumTemplateArgs);
8819   while (NumTemplateArgs--)
8820     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8821 }
8822 
8823 /// Read a UnresolvedSet structure.
8824 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8825   unsigned NumDecls = readInt();
8826   Set.reserve(getContext(), NumDecls);
8827   while (NumDecls--) {
8828     DeclID ID = readDeclID();
8829     AccessSpecifier AS = (AccessSpecifier) readInt();
8830     Set.addLazyDecl(getContext(), ID, AS);
8831   }
8832 }
8833 
8834 CXXBaseSpecifier
8835 ASTRecordReader::readCXXBaseSpecifier() {
8836   bool isVirtual = readBool();
8837   bool isBaseOfClass = readBool();
8838   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8839   bool inheritConstructors = readBool();
8840   TypeSourceInfo *TInfo = readTypeSourceInfo();
8841   SourceRange Range = readSourceRange();
8842   SourceLocation EllipsisLoc = readSourceLocation();
8843   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8844                           EllipsisLoc);
8845   Result.setInheritConstructors(inheritConstructors);
8846   return Result;
8847 }
8848 
8849 CXXCtorInitializer **
8850 ASTRecordReader::readCXXCtorInitializers() {
8851   ASTContext &Context = getContext();
8852   unsigned NumInitializers = readInt();
8853   assert(NumInitializers && "wrote ctor initializers but have no inits");
8854   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8855   for (unsigned i = 0; i != NumInitializers; ++i) {
8856     TypeSourceInfo *TInfo = nullptr;
8857     bool IsBaseVirtual = false;
8858     FieldDecl *Member = nullptr;
8859     IndirectFieldDecl *IndirectMember = nullptr;
8860 
8861     CtorInitializerType Type = (CtorInitializerType) readInt();
8862     switch (Type) {
8863     case CTOR_INITIALIZER_BASE:
8864       TInfo = readTypeSourceInfo();
8865       IsBaseVirtual = readBool();
8866       break;
8867 
8868     case CTOR_INITIALIZER_DELEGATING:
8869       TInfo = readTypeSourceInfo();
8870       break;
8871 
8872      case CTOR_INITIALIZER_MEMBER:
8873       Member = readDeclAs<FieldDecl>();
8874       break;
8875 
8876      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8877       IndirectMember = readDeclAs<IndirectFieldDecl>();
8878       break;
8879     }
8880 
8881     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8882     Expr *Init = readExpr();
8883     SourceLocation LParenLoc = readSourceLocation();
8884     SourceLocation RParenLoc = readSourceLocation();
8885 
8886     CXXCtorInitializer *BOMInit;
8887     if (Type == CTOR_INITIALIZER_BASE)
8888       BOMInit = new (Context)
8889           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8890                              RParenLoc, MemberOrEllipsisLoc);
8891     else if (Type == CTOR_INITIALIZER_DELEGATING)
8892       BOMInit = new (Context)
8893           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8894     else if (Member)
8895       BOMInit = new (Context)
8896           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8897                              Init, RParenLoc);
8898     else
8899       BOMInit = new (Context)
8900           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8901                              LParenLoc, Init, RParenLoc);
8902 
8903     if (/*IsWritten*/readBool()) {
8904       unsigned SourceOrder = readInt();
8905       BOMInit->setSourceOrder(SourceOrder);
8906     }
8907 
8908     CtorInitializers[i] = BOMInit;
8909   }
8910 
8911   return CtorInitializers;
8912 }
8913 
8914 NestedNameSpecifierLoc
8915 ASTRecordReader::readNestedNameSpecifierLoc() {
8916   ASTContext &Context = getContext();
8917   unsigned N = readInt();
8918   NestedNameSpecifierLocBuilder Builder;
8919   for (unsigned I = 0; I != N; ++I) {
8920     auto Kind = readNestedNameSpecifierKind();
8921     switch (Kind) {
8922     case NestedNameSpecifier::Identifier: {
8923       IdentifierInfo *II = readIdentifier();
8924       SourceRange Range = readSourceRange();
8925       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8926       break;
8927     }
8928 
8929     case NestedNameSpecifier::Namespace: {
8930       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8931       SourceRange Range = readSourceRange();
8932       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8933       break;
8934     }
8935 
8936     case NestedNameSpecifier::NamespaceAlias: {
8937       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8938       SourceRange Range = readSourceRange();
8939       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8940       break;
8941     }
8942 
8943     case NestedNameSpecifier::TypeSpec:
8944     case NestedNameSpecifier::TypeSpecWithTemplate: {
8945       bool Template = readBool();
8946       TypeSourceInfo *T = readTypeSourceInfo();
8947       if (!T)
8948         return NestedNameSpecifierLoc();
8949       SourceLocation ColonColonLoc = readSourceLocation();
8950 
8951       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8952       Builder.Extend(Context,
8953                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8954                      T->getTypeLoc(), ColonColonLoc);
8955       break;
8956     }
8957 
8958     case NestedNameSpecifier::Global: {
8959       SourceLocation ColonColonLoc = readSourceLocation();
8960       Builder.MakeGlobal(Context, ColonColonLoc);
8961       break;
8962     }
8963 
8964     case NestedNameSpecifier::Super: {
8965       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8966       SourceRange Range = readSourceRange();
8967       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8968       break;
8969     }
8970     }
8971   }
8972 
8973   return Builder.getWithLocInContext(Context);
8974 }
8975 
8976 SourceRange
8977 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8978                            unsigned &Idx) {
8979   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8980   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8981   return SourceRange(beg, end);
8982 }
8983 
8984 /// Read a floating-point value
8985 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8986   return llvm::APFloat(Sem, readAPInt());
8987 }
8988 
8989 // Read a string
8990 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8991   unsigned Len = Record[Idx++];
8992   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8993   Idx += Len;
8994   return Result;
8995 }
8996 
8997 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8998                                 unsigned &Idx) {
8999   std::string Filename = ReadString(Record, Idx);
9000   ResolveImportedPath(F, Filename);
9001   return Filename;
9002 }
9003 
9004 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9005                                 const RecordData &Record, unsigned &Idx) {
9006   std::string Filename = ReadString(Record, Idx);
9007   if (!BaseDirectory.empty())
9008     ResolveImportedPath(Filename, BaseDirectory);
9009   return Filename;
9010 }
9011 
9012 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9013                                          unsigned &Idx) {
9014   unsigned Major = Record[Idx++];
9015   unsigned Minor = Record[Idx++];
9016   unsigned Subminor = Record[Idx++];
9017   if (Minor == 0)
9018     return VersionTuple(Major);
9019   if (Subminor == 0)
9020     return VersionTuple(Major, Minor - 1);
9021   return VersionTuple(Major, Minor - 1, Subminor - 1);
9022 }
9023 
9024 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9025                                           const RecordData &Record,
9026                                           unsigned &Idx) {
9027   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9028   return CXXTemporary::Create(getContext(), Decl);
9029 }
9030 
9031 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9032   return Diag(CurrentImportLoc, DiagID);
9033 }
9034 
9035 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9036   return Diags.Report(Loc, DiagID);
9037 }
9038 
9039 /// Retrieve the identifier table associated with the
9040 /// preprocessor.
9041 IdentifierTable &ASTReader::getIdentifierTable() {
9042   return PP.getIdentifierTable();
9043 }
9044 
9045 /// Record that the given ID maps to the given switch-case
9046 /// statement.
9047 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9048   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9049          "Already have a SwitchCase with this ID");
9050   (*CurrSwitchCaseStmts)[ID] = SC;
9051 }
9052 
9053 /// Retrieve the switch-case statement with the given ID.
9054 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9055   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9056   return (*CurrSwitchCaseStmts)[ID];
9057 }
9058 
9059 void ASTReader::ClearSwitchCaseIDs() {
9060   CurrSwitchCaseStmts->clear();
9061 }
9062 
9063 void ASTReader::ReadComments() {
9064   ASTContext &Context = getContext();
9065   std::vector<RawComment *> Comments;
9066   for (SmallVectorImpl<std::pair<BitstreamCursor,
9067                                  serialization::ModuleFile *>>::iterator
9068        I = CommentsCursors.begin(),
9069        E = CommentsCursors.end();
9070        I != E; ++I) {
9071     Comments.clear();
9072     BitstreamCursor &Cursor = I->first;
9073     serialization::ModuleFile &F = *I->second;
9074     SavedStreamPosition SavedPosition(Cursor);
9075 
9076     RecordData Record;
9077     while (true) {
9078       Expected<llvm::BitstreamEntry> MaybeEntry =
9079           Cursor.advanceSkippingSubblocks(
9080               BitstreamCursor::AF_DontPopBlockAtEnd);
9081       if (!MaybeEntry) {
9082         Error(MaybeEntry.takeError());
9083         return;
9084       }
9085       llvm::BitstreamEntry Entry = MaybeEntry.get();
9086 
9087       switch (Entry.Kind) {
9088       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9089       case llvm::BitstreamEntry::Error:
9090         Error("malformed block record in AST file");
9091         return;
9092       case llvm::BitstreamEntry::EndBlock:
9093         goto NextCursor;
9094       case llvm::BitstreamEntry::Record:
9095         // The interesting case.
9096         break;
9097       }
9098 
9099       // Read a record.
9100       Record.clear();
9101       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9102       if (!MaybeComment) {
9103         Error(MaybeComment.takeError());
9104         return;
9105       }
9106       switch ((CommentRecordTypes)MaybeComment.get()) {
9107       case COMMENTS_RAW_COMMENT: {
9108         unsigned Idx = 0;
9109         SourceRange SR = ReadSourceRange(F, Record, Idx);
9110         RawComment::CommentKind Kind =
9111             (RawComment::CommentKind) Record[Idx++];
9112         bool IsTrailingComment = Record[Idx++];
9113         bool IsAlmostTrailingComment = Record[Idx++];
9114         Comments.push_back(new (Context) RawComment(
9115             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9116         break;
9117       }
9118       }
9119     }
9120   NextCursor:
9121     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9122         FileToOffsetToComment;
9123     for (RawComment *C : Comments) {
9124       SourceLocation CommentLoc = C->getBeginLoc();
9125       if (CommentLoc.isValid()) {
9126         std::pair<FileID, unsigned> Loc =
9127             SourceMgr.getDecomposedLoc(CommentLoc);
9128         if (Loc.first.isValid())
9129           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9130       }
9131     }
9132   }
9133 }
9134 
9135 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9136                                 bool IncludeSystem, bool Complain,
9137                     llvm::function_ref<void(const serialization::InputFile &IF,
9138                                             bool isSystem)> Visitor) {
9139   unsigned NumUserInputs = MF.NumUserInputFiles;
9140   unsigned NumInputs = MF.InputFilesLoaded.size();
9141   assert(NumUserInputs <= NumInputs);
9142   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9143   for (unsigned I = 0; I < N; ++I) {
9144     bool IsSystem = I >= NumUserInputs;
9145     InputFile IF = getInputFile(MF, I+1, Complain);
9146     Visitor(IF, IsSystem);
9147   }
9148 }
9149 
9150 void ASTReader::visitTopLevelModuleMaps(
9151     serialization::ModuleFile &MF,
9152     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9153   unsigned NumInputs = MF.InputFilesLoaded.size();
9154   for (unsigned I = 0; I < NumInputs; ++I) {
9155     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9156     if (IFI.TopLevelModuleMap)
9157       // FIXME: This unnecessarily re-reads the InputFileInfo.
9158       if (auto FE = getInputFile(MF, I + 1).getFile())
9159         Visitor(FE);
9160   }
9161 }
9162 
9163 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9164   // If we know the owning module, use it.
9165   if (Module *M = D->getImportedOwningModule())
9166     return M->getFullModuleName();
9167 
9168   // Otherwise, use the name of the top-level module the decl is within.
9169   if (ModuleFile *M = getOwningModuleFile(D))
9170     return M->ModuleName;
9171 
9172   // Not from a module.
9173   return {};
9174 }
9175 
9176 void ASTReader::finishPendingActions() {
9177   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9178          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9179          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9180          !PendingUpdateRecords.empty()) {
9181     // If any identifiers with corresponding top-level declarations have
9182     // been loaded, load those declarations now.
9183     using TopLevelDeclsMap =
9184         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9185     TopLevelDeclsMap TopLevelDecls;
9186 
9187     while (!PendingIdentifierInfos.empty()) {
9188       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9189       SmallVector<uint32_t, 4> DeclIDs =
9190           std::move(PendingIdentifierInfos.back().second);
9191       PendingIdentifierInfos.pop_back();
9192 
9193       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9194     }
9195 
9196     // Load each function type that we deferred loading because it was a
9197     // deduced type that might refer to a local type declared within itself.
9198     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9199       auto *FD = PendingFunctionTypes[I].first;
9200       FD->setType(GetType(PendingFunctionTypes[I].second));
9201 
9202       // If we gave a function a deduced return type, remember that we need to
9203       // propagate that along the redeclaration chain.
9204       auto *DT = FD->getReturnType()->getContainedDeducedType();
9205       if (DT && DT->isDeduced())
9206         PendingDeducedTypeUpdates.insert(
9207             {FD->getCanonicalDecl(), FD->getReturnType()});
9208     }
9209     PendingFunctionTypes.clear();
9210 
9211     // For each decl chain that we wanted to complete while deserializing, mark
9212     // it as "still needs to be completed".
9213     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9214       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9215     }
9216     PendingIncompleteDeclChains.clear();
9217 
9218     // Load pending declaration chains.
9219     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9220       loadPendingDeclChain(PendingDeclChains[I].first,
9221                            PendingDeclChains[I].second);
9222     PendingDeclChains.clear();
9223 
9224     // Make the most recent of the top-level declarations visible.
9225     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9226            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9227       IdentifierInfo *II = TLD->first;
9228       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9229         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9230       }
9231     }
9232 
9233     // Load any pending macro definitions.
9234     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9235       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9236       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9237       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9238       // Initialize the macro history from chained-PCHs ahead of module imports.
9239       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9240            ++IDIdx) {
9241         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9242         if (!Info.M->isModule())
9243           resolvePendingMacro(II, Info);
9244       }
9245       // Handle module imports.
9246       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9247            ++IDIdx) {
9248         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9249         if (Info.M->isModule())
9250           resolvePendingMacro(II, Info);
9251       }
9252     }
9253     PendingMacroIDs.clear();
9254 
9255     // Wire up the DeclContexts for Decls that we delayed setting until
9256     // recursive loading is completed.
9257     while (!PendingDeclContextInfos.empty()) {
9258       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9259       PendingDeclContextInfos.pop_front();
9260       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9261       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9262       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9263     }
9264 
9265     // Perform any pending declaration updates.
9266     while (!PendingUpdateRecords.empty()) {
9267       auto Update = PendingUpdateRecords.pop_back_val();
9268       ReadingKindTracker ReadingKind(Read_Decl, *this);
9269       loadDeclUpdateRecords(Update);
9270     }
9271   }
9272 
9273   // At this point, all update records for loaded decls are in place, so any
9274   // fake class definitions should have become real.
9275   assert(PendingFakeDefinitionData.empty() &&
9276          "faked up a class definition but never saw the real one");
9277 
9278   // If we deserialized any C++ or Objective-C class definitions, any
9279   // Objective-C protocol definitions, or any redeclarable templates, make sure
9280   // that all redeclarations point to the definitions. Note that this can only
9281   // happen now, after the redeclaration chains have been fully wired.
9282   for (Decl *D : PendingDefinitions) {
9283     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9284       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9285         // Make sure that the TagType points at the definition.
9286         const_cast<TagType*>(TagT)->decl = TD;
9287       }
9288 
9289       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9290         for (auto *R = getMostRecentExistingDecl(RD); R;
9291              R = R->getPreviousDecl()) {
9292           assert((R == D) ==
9293                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9294                  "declaration thinks it's the definition but it isn't");
9295           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9296         }
9297       }
9298 
9299       continue;
9300     }
9301 
9302     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9303       // Make sure that the ObjCInterfaceType points at the definition.
9304       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9305         ->Decl = ID;
9306 
9307       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9308         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9309 
9310       continue;
9311     }
9312 
9313     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9314       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9315         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9316 
9317       continue;
9318     }
9319 
9320     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9321     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9322       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9323   }
9324   PendingDefinitions.clear();
9325 
9326   // Load the bodies of any functions or methods we've encountered. We do
9327   // this now (delayed) so that we can be sure that the declaration chains
9328   // have been fully wired up (hasBody relies on this).
9329   // FIXME: We shouldn't require complete redeclaration chains here.
9330   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9331                                PBEnd = PendingBodies.end();
9332        PB != PBEnd; ++PB) {
9333     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9334       // For a function defined inline within a class template, force the
9335       // canonical definition to be the one inside the canonical definition of
9336       // the template. This ensures that we instantiate from a correct view
9337       // of the template.
9338       //
9339       // Sadly we can't do this more generally: we can't be sure that all
9340       // copies of an arbitrary class definition will have the same members
9341       // defined (eg, some member functions may not be instantiated, and some
9342       // special members may or may not have been implicitly defined).
9343       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9344         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9345           continue;
9346 
9347       // FIXME: Check for =delete/=default?
9348       // FIXME: Complain about ODR violations here?
9349       const FunctionDecl *Defn = nullptr;
9350       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9351         FD->setLazyBody(PB->second);
9352       } else {
9353         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9354         mergeDefinitionVisibility(NonConstDefn, FD);
9355 
9356         if (!FD->isLateTemplateParsed() &&
9357             !NonConstDefn->isLateTemplateParsed() &&
9358             FD->getODRHash() != NonConstDefn->getODRHash()) {
9359           if (!isa<CXXMethodDecl>(FD)) {
9360             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9361           } else if (FD->getLexicalParent()->isFileContext() &&
9362                      NonConstDefn->getLexicalParent()->isFileContext()) {
9363             // Only diagnose out-of-line method definitions.  If they are
9364             // in class definitions, then an error will be generated when
9365             // processing the class bodies.
9366             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9367           }
9368         }
9369       }
9370       continue;
9371     }
9372 
9373     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9374     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9375       MD->setLazyBody(PB->second);
9376   }
9377   PendingBodies.clear();
9378 
9379   // Do some cleanup.
9380   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9381     getContext().deduplicateMergedDefinitonsFor(ND);
9382   PendingMergedDefinitionsToDeduplicate.clear();
9383 }
9384 
9385 void ASTReader::diagnoseOdrViolations() {
9386   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9387       PendingFunctionOdrMergeFailures.empty() &&
9388       PendingEnumOdrMergeFailures.empty())
9389     return;
9390 
9391   // Trigger the import of the full definition of each class that had any
9392   // odr-merging problems, so we can produce better diagnostics for them.
9393   // These updates may in turn find and diagnose some ODR failures, so take
9394   // ownership of the set first.
9395   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9396   PendingOdrMergeFailures.clear();
9397   for (auto &Merge : OdrMergeFailures) {
9398     Merge.first->buildLookup();
9399     Merge.first->decls_begin();
9400     Merge.first->bases_begin();
9401     Merge.first->vbases_begin();
9402     for (auto &RecordPair : Merge.second) {
9403       auto *RD = RecordPair.first;
9404       RD->decls_begin();
9405       RD->bases_begin();
9406       RD->vbases_begin();
9407     }
9408   }
9409 
9410   // Trigger the import of functions.
9411   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9412   PendingFunctionOdrMergeFailures.clear();
9413   for (auto &Merge : FunctionOdrMergeFailures) {
9414     Merge.first->buildLookup();
9415     Merge.first->decls_begin();
9416     Merge.first->getBody();
9417     for (auto &FD : Merge.second) {
9418       FD->buildLookup();
9419       FD->decls_begin();
9420       FD->getBody();
9421     }
9422   }
9423 
9424   // Trigger the import of enums.
9425   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9426   PendingEnumOdrMergeFailures.clear();
9427   for (auto &Merge : EnumOdrMergeFailures) {
9428     Merge.first->decls_begin();
9429     for (auto &Enum : Merge.second) {
9430       Enum->decls_begin();
9431     }
9432   }
9433 
9434   // For each declaration from a merged context, check that the canonical
9435   // definition of that context also contains a declaration of the same
9436   // entity.
9437   //
9438   // Caution: this loop does things that might invalidate iterators into
9439   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9440   while (!PendingOdrMergeChecks.empty()) {
9441     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9442 
9443     // FIXME: Skip over implicit declarations for now. This matters for things
9444     // like implicitly-declared special member functions. This isn't entirely
9445     // correct; we can end up with multiple unmerged declarations of the same
9446     // implicit entity.
9447     if (D->isImplicit())
9448       continue;
9449 
9450     DeclContext *CanonDef = D->getDeclContext();
9451 
9452     bool Found = false;
9453     const Decl *DCanon = D->getCanonicalDecl();
9454 
9455     for (auto RI : D->redecls()) {
9456       if (RI->getLexicalDeclContext() == CanonDef) {
9457         Found = true;
9458         break;
9459       }
9460     }
9461     if (Found)
9462       continue;
9463 
9464     // Quick check failed, time to do the slow thing. Note, we can't just
9465     // look up the name of D in CanonDef here, because the member that is
9466     // in CanonDef might not be found by name lookup (it might have been
9467     // replaced by a more recent declaration in the lookup table), and we
9468     // can't necessarily find it in the redeclaration chain because it might
9469     // be merely mergeable, not redeclarable.
9470     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9471     for (auto *CanonMember : CanonDef->decls()) {
9472       if (CanonMember->getCanonicalDecl() == DCanon) {
9473         // This can happen if the declaration is merely mergeable and not
9474         // actually redeclarable (we looked for redeclarations earlier).
9475         //
9476         // FIXME: We should be able to detect this more efficiently, without
9477         // pulling in all of the members of CanonDef.
9478         Found = true;
9479         break;
9480       }
9481       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9482         if (ND->getDeclName() == D->getDeclName())
9483           Candidates.push_back(ND);
9484     }
9485 
9486     if (!Found) {
9487       // The AST doesn't like TagDecls becoming invalid after they've been
9488       // completed. We only really need to mark FieldDecls as invalid here.
9489       if (!isa<TagDecl>(D))
9490         D->setInvalidDecl();
9491 
9492       // Ensure we don't accidentally recursively enter deserialization while
9493       // we're producing our diagnostic.
9494       Deserializing RecursionGuard(this);
9495 
9496       std::string CanonDefModule =
9497           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9498       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9499         << D << getOwningModuleNameForDiagnostic(D)
9500         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9501 
9502       if (Candidates.empty())
9503         Diag(cast<Decl>(CanonDef)->getLocation(),
9504              diag::note_module_odr_violation_no_possible_decls) << D;
9505       else {
9506         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9507           Diag(Candidates[I]->getLocation(),
9508                diag::note_module_odr_violation_possible_decl)
9509             << Candidates[I];
9510       }
9511 
9512       DiagnosedOdrMergeFailures.insert(CanonDef);
9513     }
9514   }
9515 
9516   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9517       EnumOdrMergeFailures.empty())
9518     return;
9519 
9520   // Ensure we don't accidentally recursively enter deserialization while
9521   // we're producing our diagnostics.
9522   Deserializing RecursionGuard(this);
9523 
9524   // Common code for hashing helpers.
9525   ODRHash Hash;
9526   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9527     Hash.clear();
9528     Hash.AddQualType(Ty);
9529     return Hash.CalculateHash();
9530   };
9531 
9532   auto ComputeODRHash = [&Hash](const Stmt *S) {
9533     assert(S);
9534     Hash.clear();
9535     Hash.AddStmt(S);
9536     return Hash.CalculateHash();
9537   };
9538 
9539   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9540     assert(D);
9541     Hash.clear();
9542     Hash.AddSubDecl(D);
9543     return Hash.CalculateHash();
9544   };
9545 
9546   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9547     Hash.clear();
9548     Hash.AddTemplateArgument(TA);
9549     return Hash.CalculateHash();
9550   };
9551 
9552   auto ComputeTemplateParameterListODRHash =
9553       [&Hash](const TemplateParameterList *TPL) {
9554         assert(TPL);
9555         Hash.clear();
9556         Hash.AddTemplateParameterList(TPL);
9557         return Hash.CalculateHash();
9558       };
9559 
9560   // Used with err_module_odr_violation_mismatch_decl and
9561   // note_module_odr_violation_mismatch_decl
9562   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9563   enum ODRMismatchDecl {
9564     EndOfClass,
9565     PublicSpecifer,
9566     PrivateSpecifer,
9567     ProtectedSpecifer,
9568     StaticAssert,
9569     Field,
9570     CXXMethod,
9571     TypeAlias,
9572     TypeDef,
9573     Var,
9574     Friend,
9575     FunctionTemplate,
9576     Other
9577   };
9578 
9579   // Used with err_module_odr_violation_mismatch_decl_diff and
9580   // note_module_odr_violation_mismatch_decl_diff
9581   enum ODRMismatchDeclDifference {
9582     StaticAssertCondition,
9583     StaticAssertMessage,
9584     StaticAssertOnlyMessage,
9585     FieldName,
9586     FieldTypeName,
9587     FieldSingleBitField,
9588     FieldDifferentWidthBitField,
9589     FieldSingleMutable,
9590     FieldSingleInitializer,
9591     FieldDifferentInitializers,
9592     MethodName,
9593     MethodDeleted,
9594     MethodDefaulted,
9595     MethodVirtual,
9596     MethodStatic,
9597     MethodVolatile,
9598     MethodConst,
9599     MethodInline,
9600     MethodNumberParameters,
9601     MethodParameterType,
9602     MethodParameterName,
9603     MethodParameterSingleDefaultArgument,
9604     MethodParameterDifferentDefaultArgument,
9605     MethodNoTemplateArguments,
9606     MethodDifferentNumberTemplateArguments,
9607     MethodDifferentTemplateArgument,
9608     MethodSingleBody,
9609     MethodDifferentBody,
9610     TypedefName,
9611     TypedefType,
9612     VarName,
9613     VarType,
9614     VarSingleInitializer,
9615     VarDifferentInitializer,
9616     VarConstexpr,
9617     FriendTypeFunction,
9618     FriendType,
9619     FriendFunction,
9620     FunctionTemplateDifferentNumberParameters,
9621     FunctionTemplateParameterDifferentKind,
9622     FunctionTemplateParameterName,
9623     FunctionTemplateParameterSingleDefaultArgument,
9624     FunctionTemplateParameterDifferentDefaultArgument,
9625     FunctionTemplateParameterDifferentType,
9626     FunctionTemplatePackParameter,
9627   };
9628 
9629   // These lambdas have the common portions of the ODR diagnostics.  This
9630   // has the same return as Diag(), so addition parameters can be passed
9631   // in with operator<<
9632   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9633                                  SourceLocation Loc, SourceRange Range,
9634                                  ODRMismatchDeclDifference DiffType) {
9635     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9636            << FirstRecord << FirstModule.empty() << FirstModule << Range
9637            << DiffType;
9638   };
9639   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9640                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9641     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9642            << SecondModule << Range << DiffType;
9643   };
9644 
9645   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9646                        &ComputeQualTypeODRHash, &ComputeODRHash](
9647                           NamedDecl *FirstRecord, StringRef FirstModule,
9648                           StringRef SecondModule, FieldDecl *FirstField,
9649                           FieldDecl *SecondField) {
9650     IdentifierInfo *FirstII = FirstField->getIdentifier();
9651     IdentifierInfo *SecondII = SecondField->getIdentifier();
9652     if (FirstII->getName() != SecondII->getName()) {
9653       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9654                        FirstField->getSourceRange(), FieldName)
9655           << FirstII;
9656       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9657                       SecondField->getSourceRange(), FieldName)
9658           << SecondII;
9659 
9660       return true;
9661     }
9662 
9663     assert(getContext().hasSameType(FirstField->getType(),
9664                                     SecondField->getType()));
9665 
9666     QualType FirstType = FirstField->getType();
9667     QualType SecondType = SecondField->getType();
9668     if (ComputeQualTypeODRHash(FirstType) !=
9669         ComputeQualTypeODRHash(SecondType)) {
9670       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9671                        FirstField->getSourceRange(), FieldTypeName)
9672           << FirstII << FirstType;
9673       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9674                       SecondField->getSourceRange(), FieldTypeName)
9675           << SecondII << SecondType;
9676 
9677       return true;
9678     }
9679 
9680     const bool IsFirstBitField = FirstField->isBitField();
9681     const bool IsSecondBitField = SecondField->isBitField();
9682     if (IsFirstBitField != IsSecondBitField) {
9683       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9684                        FirstField->getSourceRange(), FieldSingleBitField)
9685           << FirstII << IsFirstBitField;
9686       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9687                       SecondField->getSourceRange(), FieldSingleBitField)
9688           << SecondII << IsSecondBitField;
9689       return true;
9690     }
9691 
9692     if (IsFirstBitField && IsSecondBitField) {
9693       unsigned FirstBitWidthHash =
9694           ComputeODRHash(FirstField->getBitWidth());
9695       unsigned SecondBitWidthHash =
9696           ComputeODRHash(SecondField->getBitWidth());
9697       if (FirstBitWidthHash != SecondBitWidthHash) {
9698         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9699                          FirstField->getSourceRange(),
9700                          FieldDifferentWidthBitField)
9701             << FirstII << FirstField->getBitWidth()->getSourceRange();
9702         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9703                         SecondField->getSourceRange(),
9704                         FieldDifferentWidthBitField)
9705             << SecondII << SecondField->getBitWidth()->getSourceRange();
9706         return true;
9707       }
9708     }
9709 
9710     if (!PP.getLangOpts().CPlusPlus)
9711       return false;
9712 
9713     const bool IsFirstMutable = FirstField->isMutable();
9714     const bool IsSecondMutable = SecondField->isMutable();
9715     if (IsFirstMutable != IsSecondMutable) {
9716       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9717                        FirstField->getSourceRange(), FieldSingleMutable)
9718           << FirstII << IsFirstMutable;
9719       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9720                       SecondField->getSourceRange(), FieldSingleMutable)
9721           << SecondII << IsSecondMutable;
9722       return true;
9723     }
9724 
9725     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9726     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9727     if ((!FirstInitializer && SecondInitializer) ||
9728         (FirstInitializer && !SecondInitializer)) {
9729       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9730                        FirstField->getSourceRange(), FieldSingleInitializer)
9731           << FirstII << (FirstInitializer != nullptr);
9732       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9733                       SecondField->getSourceRange(), FieldSingleInitializer)
9734           << SecondII << (SecondInitializer != nullptr);
9735       return true;
9736     }
9737 
9738     if (FirstInitializer && SecondInitializer) {
9739       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9740       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9741       if (FirstInitHash != SecondInitHash) {
9742         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9743                          FirstField->getSourceRange(),
9744                          FieldDifferentInitializers)
9745             << FirstII << FirstInitializer->getSourceRange();
9746         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9747                         SecondField->getSourceRange(),
9748                         FieldDifferentInitializers)
9749             << SecondII << SecondInitializer->getSourceRange();
9750         return true;
9751       }
9752     }
9753 
9754     return false;
9755   };
9756 
9757   auto ODRDiagTypeDefOrAlias =
9758       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9759           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9760           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9761           bool IsTypeAlias) {
9762         auto FirstName = FirstTD->getDeclName();
9763         auto SecondName = SecondTD->getDeclName();
9764         if (FirstName != SecondName) {
9765           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9766                            FirstTD->getSourceRange(), TypedefName)
9767               << IsTypeAlias << FirstName;
9768           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9769                           SecondTD->getSourceRange(), TypedefName)
9770               << IsTypeAlias << SecondName;
9771           return true;
9772         }
9773 
9774         QualType FirstType = FirstTD->getUnderlyingType();
9775         QualType SecondType = SecondTD->getUnderlyingType();
9776         if (ComputeQualTypeODRHash(FirstType) !=
9777             ComputeQualTypeODRHash(SecondType)) {
9778           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9779                            FirstTD->getSourceRange(), TypedefType)
9780               << IsTypeAlias << FirstName << FirstType;
9781           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9782                           SecondTD->getSourceRange(), TypedefType)
9783               << IsTypeAlias << SecondName << SecondType;
9784           return true;
9785         }
9786 
9787         return false;
9788   };
9789 
9790   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9791                      &ComputeQualTypeODRHash, &ComputeODRHash,
9792                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9793                            StringRef SecondModule, VarDecl *FirstVD,
9794                            VarDecl *SecondVD) {
9795     auto FirstName = FirstVD->getDeclName();
9796     auto SecondName = SecondVD->getDeclName();
9797     if (FirstName != SecondName) {
9798       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9799                        FirstVD->getSourceRange(), VarName)
9800           << FirstName;
9801       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9802                       SecondVD->getSourceRange(), VarName)
9803           << SecondName;
9804       return true;
9805     }
9806 
9807     QualType FirstType = FirstVD->getType();
9808     QualType SecondType = SecondVD->getType();
9809     if (ComputeQualTypeODRHash(FirstType) !=
9810         ComputeQualTypeODRHash(SecondType)) {
9811       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9812                        FirstVD->getSourceRange(), VarType)
9813           << FirstName << FirstType;
9814       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9815                       SecondVD->getSourceRange(), VarType)
9816           << SecondName << SecondType;
9817       return true;
9818     }
9819 
9820     if (!PP.getLangOpts().CPlusPlus)
9821       return false;
9822 
9823     const Expr *FirstInit = FirstVD->getInit();
9824     const Expr *SecondInit = SecondVD->getInit();
9825     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9826       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9827                        FirstVD->getSourceRange(), VarSingleInitializer)
9828           << FirstName << (FirstInit == nullptr)
9829           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9830       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9831                       SecondVD->getSourceRange(), VarSingleInitializer)
9832           << SecondName << (SecondInit == nullptr)
9833           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9834       return true;
9835     }
9836 
9837     if (FirstInit && SecondInit &&
9838         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9839       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9840                        FirstVD->getSourceRange(), VarDifferentInitializer)
9841           << FirstName << FirstInit->getSourceRange();
9842       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9843                       SecondVD->getSourceRange(), VarDifferentInitializer)
9844           << SecondName << SecondInit->getSourceRange();
9845       return true;
9846     }
9847 
9848     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9849     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9850     if (FirstIsConstexpr != SecondIsConstexpr) {
9851       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9852                        FirstVD->getSourceRange(), VarConstexpr)
9853           << FirstName << FirstIsConstexpr;
9854       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9855                       SecondVD->getSourceRange(), VarConstexpr)
9856           << SecondName << SecondIsConstexpr;
9857       return true;
9858     }
9859     return false;
9860   };
9861 
9862   auto DifferenceSelector = [](Decl *D) {
9863     assert(D && "valid Decl required");
9864     switch (D->getKind()) {
9865     default:
9866       return Other;
9867     case Decl::AccessSpec:
9868       switch (D->getAccess()) {
9869       case AS_public:
9870         return PublicSpecifer;
9871       case AS_private:
9872         return PrivateSpecifer;
9873       case AS_protected:
9874         return ProtectedSpecifer;
9875       case AS_none:
9876         break;
9877       }
9878       llvm_unreachable("Invalid access specifier");
9879     case Decl::StaticAssert:
9880       return StaticAssert;
9881     case Decl::Field:
9882       return Field;
9883     case Decl::CXXMethod:
9884     case Decl::CXXConstructor:
9885     case Decl::CXXDestructor:
9886       return CXXMethod;
9887     case Decl::TypeAlias:
9888       return TypeAlias;
9889     case Decl::Typedef:
9890       return TypeDef;
9891     case Decl::Var:
9892       return Var;
9893     case Decl::Friend:
9894       return Friend;
9895     case Decl::FunctionTemplate:
9896       return FunctionTemplate;
9897     }
9898   };
9899 
9900   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9901   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9902                                                  RecordDecl *Record,
9903                                                  const DeclContext *DC) {
9904     for (auto *D : Record->decls()) {
9905       if (!ODRHash::isDeclToBeProcessed(D, DC))
9906         continue;
9907       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9908     }
9909   };
9910 
9911   struct DiffResult {
9912     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9913     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9914   };
9915 
9916   // If there is a diagnoseable difference, FirstDiffType and
9917   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9918   // filled in if not EndOfClass.
9919   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9920                                              DeclHashes &SecondHashes) {
9921     DiffResult DR;
9922     auto FirstIt = FirstHashes.begin();
9923     auto SecondIt = SecondHashes.begin();
9924     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9925       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9926           FirstIt->second == SecondIt->second) {
9927         ++FirstIt;
9928         ++SecondIt;
9929         continue;
9930       }
9931 
9932       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9933       DR.SecondDecl =
9934           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9935 
9936       DR.FirstDiffType =
9937           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9938       DR.SecondDiffType =
9939           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9940       return DR;
9941     }
9942     return DR;
9943   };
9944 
9945   // Use this to diagnose that an unexpected Decl was encountered
9946   // or no difference was detected. This causes a generic error
9947   // message to be emitted.
9948   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9949                                       StringRef FirstModule,
9950                                       NamedDecl *SecondRecord,
9951                                       StringRef SecondModule) {
9952     Diag(FirstRecord->getLocation(),
9953          diag::err_module_odr_violation_different_definitions)
9954         << FirstRecord << FirstModule.empty() << FirstModule;
9955 
9956     if (DR.FirstDecl) {
9957       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9958           << FirstRecord << DR.FirstDecl->getSourceRange();
9959     }
9960 
9961     Diag(SecondRecord->getLocation(),
9962          diag::note_module_odr_violation_different_definitions)
9963         << SecondModule;
9964 
9965     if (DR.SecondDecl) {
9966       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9967           << DR.SecondDecl->getSourceRange();
9968     }
9969   };
9970 
9971   auto DiagnoseODRMismatch =
9972       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9973              NamedDecl *SecondRecord, StringRef SecondModule) {
9974         SourceLocation FirstLoc;
9975         SourceRange FirstRange;
9976         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9977         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9978           FirstLoc = FirstTag->getBraceRange().getEnd();
9979         } else {
9980           FirstLoc = DR.FirstDecl->getLocation();
9981           FirstRange = DR.FirstDecl->getSourceRange();
9982         }
9983         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9984             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9985             << DR.FirstDiffType;
9986 
9987         SourceLocation SecondLoc;
9988         SourceRange SecondRange;
9989         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9990         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9991           SecondLoc = SecondTag->getBraceRange().getEnd();
9992         } else {
9993           SecondLoc = DR.SecondDecl->getLocation();
9994           SecondRange = DR.SecondDecl->getSourceRange();
9995         }
9996         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9997             << SecondModule << SecondRange << DR.SecondDiffType;
9998       };
9999 
10000   // Issue any pending ODR-failure diagnostics.
10001   for (auto &Merge : OdrMergeFailures) {
10002     // If we've already pointed out a specific problem with this class, don't
10003     // bother issuing a general "something's different" diagnostic.
10004     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10005       continue;
10006 
10007     bool Diagnosed = false;
10008     CXXRecordDecl *FirstRecord = Merge.first;
10009     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10010     for (auto &RecordPair : Merge.second) {
10011       CXXRecordDecl *SecondRecord = RecordPair.first;
10012       // Multiple different declarations got merged together; tell the user
10013       // where they came from.
10014       if (FirstRecord == SecondRecord)
10015         continue;
10016 
10017       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10018 
10019       auto *FirstDD = FirstRecord->DefinitionData;
10020       auto *SecondDD = RecordPair.second;
10021 
10022       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10023 
10024       // Diagnostics from DefinitionData are emitted here.
10025       if (FirstDD != SecondDD) {
10026         enum ODRDefinitionDataDifference {
10027           NumBases,
10028           NumVBases,
10029           BaseType,
10030           BaseVirtual,
10031           BaseAccess,
10032         };
10033         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10034                                  this](SourceLocation Loc, SourceRange Range,
10035                                        ODRDefinitionDataDifference DiffType) {
10036           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10037                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10038                  << DiffType;
10039         };
10040         auto ODRDiagBaseNote = [&SecondModule,
10041                                 this](SourceLocation Loc, SourceRange Range,
10042                                       ODRDefinitionDataDifference DiffType) {
10043           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10044                  << SecondModule << Range << DiffType;
10045         };
10046 
10047         unsigned FirstNumBases = FirstDD->NumBases;
10048         unsigned FirstNumVBases = FirstDD->NumVBases;
10049         unsigned SecondNumBases = SecondDD->NumBases;
10050         unsigned SecondNumVBases = SecondDD->NumVBases;
10051 
10052         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10053           unsigned NumBases = DD->NumBases;
10054           if (NumBases == 0) return SourceRange();
10055           auto bases = DD->bases();
10056           return SourceRange(bases[0].getBeginLoc(),
10057                              bases[NumBases - 1].getEndLoc());
10058         };
10059 
10060         if (FirstNumBases != SecondNumBases) {
10061           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10062                            NumBases)
10063               << FirstNumBases;
10064           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10065                           NumBases)
10066               << SecondNumBases;
10067           Diagnosed = true;
10068           break;
10069         }
10070 
10071         if (FirstNumVBases != SecondNumVBases) {
10072           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10073                            NumVBases)
10074               << FirstNumVBases;
10075           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10076                           NumVBases)
10077               << SecondNumVBases;
10078           Diagnosed = true;
10079           break;
10080         }
10081 
10082         auto FirstBases = FirstDD->bases();
10083         auto SecondBases = SecondDD->bases();
10084         unsigned i = 0;
10085         for (i = 0; i < FirstNumBases; ++i) {
10086           auto FirstBase = FirstBases[i];
10087           auto SecondBase = SecondBases[i];
10088           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10089               ComputeQualTypeODRHash(SecondBase.getType())) {
10090             ODRDiagBaseError(FirstRecord->getLocation(),
10091                              FirstBase.getSourceRange(), BaseType)
10092                 << (i + 1) << FirstBase.getType();
10093             ODRDiagBaseNote(SecondRecord->getLocation(),
10094                             SecondBase.getSourceRange(), BaseType)
10095                 << (i + 1) << SecondBase.getType();
10096             break;
10097           }
10098 
10099           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10100             ODRDiagBaseError(FirstRecord->getLocation(),
10101                              FirstBase.getSourceRange(), BaseVirtual)
10102                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10103             ODRDiagBaseNote(SecondRecord->getLocation(),
10104                             SecondBase.getSourceRange(), BaseVirtual)
10105                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10106             break;
10107           }
10108 
10109           if (FirstBase.getAccessSpecifierAsWritten() !=
10110               SecondBase.getAccessSpecifierAsWritten()) {
10111             ODRDiagBaseError(FirstRecord->getLocation(),
10112                              FirstBase.getSourceRange(), BaseAccess)
10113                 << (i + 1) << FirstBase.getType()
10114                 << (int)FirstBase.getAccessSpecifierAsWritten();
10115             ODRDiagBaseNote(SecondRecord->getLocation(),
10116                             SecondBase.getSourceRange(), BaseAccess)
10117                 << (i + 1) << SecondBase.getType()
10118                 << (int)SecondBase.getAccessSpecifierAsWritten();
10119             break;
10120           }
10121         }
10122 
10123         if (i != FirstNumBases) {
10124           Diagnosed = true;
10125           break;
10126         }
10127       }
10128 
10129       const ClassTemplateDecl *FirstTemplate =
10130           FirstRecord->getDescribedClassTemplate();
10131       const ClassTemplateDecl *SecondTemplate =
10132           SecondRecord->getDescribedClassTemplate();
10133 
10134       assert(!FirstTemplate == !SecondTemplate &&
10135              "Both pointers should be null or non-null");
10136 
10137       enum ODRTemplateDifference {
10138         ParamEmptyName,
10139         ParamName,
10140         ParamSingleDefaultArgument,
10141         ParamDifferentDefaultArgument,
10142       };
10143 
10144       if (FirstTemplate && SecondTemplate) {
10145         DeclHashes FirstTemplateHashes;
10146         DeclHashes SecondTemplateHashes;
10147 
10148         auto PopulateTemplateParameterHashs =
10149             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10150                                      const ClassTemplateDecl *TD) {
10151               for (auto *D : TD->getTemplateParameters()->asArray()) {
10152                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10153               }
10154             };
10155 
10156         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10157         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10158 
10159         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10160                "Number of template parameters should be equal.");
10161 
10162         auto FirstIt = FirstTemplateHashes.begin();
10163         auto FirstEnd = FirstTemplateHashes.end();
10164         auto SecondIt = SecondTemplateHashes.begin();
10165         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10166           if (FirstIt->second == SecondIt->second)
10167             continue;
10168 
10169           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10170                                           SourceLocation Loc, SourceRange Range,
10171                                           ODRTemplateDifference DiffType) {
10172             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10173                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10174                    << DiffType;
10175           };
10176           auto ODRDiagTemplateNote = [&SecondModule, this](
10177                                          SourceLocation Loc, SourceRange Range,
10178                                          ODRTemplateDifference DiffType) {
10179             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10180                    << SecondModule << Range << DiffType;
10181           };
10182 
10183           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10184           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10185 
10186           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10187                  "Parameter Decl's should be the same kind.");
10188 
10189           DeclarationName FirstName = FirstDecl->getDeclName();
10190           DeclarationName SecondName = SecondDecl->getDeclName();
10191 
10192           if (FirstName != SecondName) {
10193             const bool FirstNameEmpty =
10194                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10195             const bool SecondNameEmpty =
10196                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10197             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10198                    "Both template parameters cannot be unnamed.");
10199             ODRDiagTemplateError(FirstDecl->getLocation(),
10200                                  FirstDecl->getSourceRange(),
10201                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10202                 << FirstName;
10203             ODRDiagTemplateNote(SecondDecl->getLocation(),
10204                                 SecondDecl->getSourceRange(),
10205                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10206                 << SecondName;
10207             break;
10208           }
10209 
10210           switch (FirstDecl->getKind()) {
10211           default:
10212             llvm_unreachable("Invalid template parameter type.");
10213           case Decl::TemplateTypeParm: {
10214             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10215             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10216             const bool HasFirstDefaultArgument =
10217                 FirstParam->hasDefaultArgument() &&
10218                 !FirstParam->defaultArgumentWasInherited();
10219             const bool HasSecondDefaultArgument =
10220                 SecondParam->hasDefaultArgument() &&
10221                 !SecondParam->defaultArgumentWasInherited();
10222 
10223             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10224               ODRDiagTemplateError(FirstDecl->getLocation(),
10225                                    FirstDecl->getSourceRange(),
10226                                    ParamSingleDefaultArgument)
10227                   << HasFirstDefaultArgument;
10228               ODRDiagTemplateNote(SecondDecl->getLocation(),
10229                                   SecondDecl->getSourceRange(),
10230                                   ParamSingleDefaultArgument)
10231                   << HasSecondDefaultArgument;
10232               break;
10233             }
10234 
10235             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10236                    "Expecting default arguments.");
10237 
10238             ODRDiagTemplateError(FirstDecl->getLocation(),
10239                                  FirstDecl->getSourceRange(),
10240                                  ParamDifferentDefaultArgument);
10241             ODRDiagTemplateNote(SecondDecl->getLocation(),
10242                                 SecondDecl->getSourceRange(),
10243                                 ParamDifferentDefaultArgument);
10244 
10245             break;
10246           }
10247           case Decl::NonTypeTemplateParm: {
10248             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10249             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10250             const bool HasFirstDefaultArgument =
10251                 FirstParam->hasDefaultArgument() &&
10252                 !FirstParam->defaultArgumentWasInherited();
10253             const bool HasSecondDefaultArgument =
10254                 SecondParam->hasDefaultArgument() &&
10255                 !SecondParam->defaultArgumentWasInherited();
10256 
10257             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10258               ODRDiagTemplateError(FirstDecl->getLocation(),
10259                                    FirstDecl->getSourceRange(),
10260                                    ParamSingleDefaultArgument)
10261                   << HasFirstDefaultArgument;
10262               ODRDiagTemplateNote(SecondDecl->getLocation(),
10263                                   SecondDecl->getSourceRange(),
10264                                   ParamSingleDefaultArgument)
10265                   << HasSecondDefaultArgument;
10266               break;
10267             }
10268 
10269             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10270                    "Expecting default arguments.");
10271 
10272             ODRDiagTemplateError(FirstDecl->getLocation(),
10273                                  FirstDecl->getSourceRange(),
10274                                  ParamDifferentDefaultArgument);
10275             ODRDiagTemplateNote(SecondDecl->getLocation(),
10276                                 SecondDecl->getSourceRange(),
10277                                 ParamDifferentDefaultArgument);
10278 
10279             break;
10280           }
10281           case Decl::TemplateTemplateParm: {
10282             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10283             const auto *SecondParam =
10284                 cast<TemplateTemplateParmDecl>(SecondDecl);
10285             const bool HasFirstDefaultArgument =
10286                 FirstParam->hasDefaultArgument() &&
10287                 !FirstParam->defaultArgumentWasInherited();
10288             const bool HasSecondDefaultArgument =
10289                 SecondParam->hasDefaultArgument() &&
10290                 !SecondParam->defaultArgumentWasInherited();
10291 
10292             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10293               ODRDiagTemplateError(FirstDecl->getLocation(),
10294                                    FirstDecl->getSourceRange(),
10295                                    ParamSingleDefaultArgument)
10296                   << HasFirstDefaultArgument;
10297               ODRDiagTemplateNote(SecondDecl->getLocation(),
10298                                   SecondDecl->getSourceRange(),
10299                                   ParamSingleDefaultArgument)
10300                   << HasSecondDefaultArgument;
10301               break;
10302             }
10303 
10304             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10305                    "Expecting default arguments.");
10306 
10307             ODRDiagTemplateError(FirstDecl->getLocation(),
10308                                  FirstDecl->getSourceRange(),
10309                                  ParamDifferentDefaultArgument);
10310             ODRDiagTemplateNote(SecondDecl->getLocation(),
10311                                 SecondDecl->getSourceRange(),
10312                                 ParamDifferentDefaultArgument);
10313 
10314             break;
10315           }
10316           }
10317 
10318           break;
10319         }
10320 
10321         if (FirstIt != FirstEnd) {
10322           Diagnosed = true;
10323           break;
10324         }
10325       }
10326 
10327       DeclHashes FirstHashes;
10328       DeclHashes SecondHashes;
10329       const DeclContext *DC = FirstRecord;
10330       PopulateHashes(FirstHashes, FirstRecord, DC);
10331       PopulateHashes(SecondHashes, SecondRecord, DC);
10332 
10333       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10334       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10335       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10336       Decl *FirstDecl = DR.FirstDecl;
10337       Decl *SecondDecl = DR.SecondDecl;
10338 
10339       if (FirstDiffType == Other || SecondDiffType == Other) {
10340         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10341                               SecondModule);
10342         Diagnosed = true;
10343         break;
10344       }
10345 
10346       if (FirstDiffType != SecondDiffType) {
10347         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10348                             SecondModule);
10349         Diagnosed = true;
10350         break;
10351       }
10352 
10353       assert(FirstDiffType == SecondDiffType);
10354 
10355       switch (FirstDiffType) {
10356       case Other:
10357       case EndOfClass:
10358       case PublicSpecifer:
10359       case PrivateSpecifer:
10360       case ProtectedSpecifer:
10361         llvm_unreachable("Invalid diff type");
10362 
10363       case StaticAssert: {
10364         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10365         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10366 
10367         Expr *FirstExpr = FirstSA->getAssertExpr();
10368         Expr *SecondExpr = SecondSA->getAssertExpr();
10369         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10370         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10371         if (FirstODRHash != SecondODRHash) {
10372           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10373                            FirstExpr->getSourceRange(), StaticAssertCondition);
10374           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10375                           SecondExpr->getSourceRange(), StaticAssertCondition);
10376           Diagnosed = true;
10377           break;
10378         }
10379 
10380         StringLiteral *FirstStr = FirstSA->getMessage();
10381         StringLiteral *SecondStr = SecondSA->getMessage();
10382         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10383         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10384           SourceLocation FirstLoc, SecondLoc;
10385           SourceRange FirstRange, SecondRange;
10386           if (FirstStr) {
10387             FirstLoc = FirstStr->getBeginLoc();
10388             FirstRange = FirstStr->getSourceRange();
10389           } else {
10390             FirstLoc = FirstSA->getBeginLoc();
10391             FirstRange = FirstSA->getSourceRange();
10392           }
10393           if (SecondStr) {
10394             SecondLoc = SecondStr->getBeginLoc();
10395             SecondRange = SecondStr->getSourceRange();
10396           } else {
10397             SecondLoc = SecondSA->getBeginLoc();
10398             SecondRange = SecondSA->getSourceRange();
10399           }
10400           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10401                            StaticAssertOnlyMessage)
10402               << (FirstStr == nullptr);
10403           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10404                           StaticAssertOnlyMessage)
10405               << (SecondStr == nullptr);
10406           Diagnosed = true;
10407           break;
10408         }
10409 
10410         if (FirstStr && SecondStr &&
10411             FirstStr->getString() != SecondStr->getString()) {
10412           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10413                            FirstStr->getSourceRange(), StaticAssertMessage);
10414           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10415                           SecondStr->getSourceRange(), StaticAssertMessage);
10416           Diagnosed = true;
10417           break;
10418         }
10419         break;
10420       }
10421       case Field: {
10422         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10423                                  cast<FieldDecl>(FirstDecl),
10424                                  cast<FieldDecl>(SecondDecl));
10425         break;
10426       }
10427       case CXXMethod: {
10428         enum {
10429           DiagMethod,
10430           DiagConstructor,
10431           DiagDestructor,
10432         } FirstMethodType,
10433             SecondMethodType;
10434         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10435           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10436           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10437           return DiagMethod;
10438         };
10439         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10440         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10441         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10442         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10443         auto FirstName = FirstMethod->getDeclName();
10444         auto SecondName = SecondMethod->getDeclName();
10445         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10446           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10447                            FirstMethod->getSourceRange(), MethodName)
10448               << FirstMethodType << FirstName;
10449           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10450                           SecondMethod->getSourceRange(), MethodName)
10451               << SecondMethodType << SecondName;
10452 
10453           Diagnosed = true;
10454           break;
10455         }
10456 
10457         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10458         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10459         if (FirstDeleted != SecondDeleted) {
10460           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10461                            FirstMethod->getSourceRange(), MethodDeleted)
10462               << FirstMethodType << FirstName << FirstDeleted;
10463 
10464           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10465                           SecondMethod->getSourceRange(), MethodDeleted)
10466               << SecondMethodType << SecondName << SecondDeleted;
10467           Diagnosed = true;
10468           break;
10469         }
10470 
10471         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10472         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10473         if (FirstDefaulted != SecondDefaulted) {
10474           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10475                            FirstMethod->getSourceRange(), MethodDefaulted)
10476               << FirstMethodType << FirstName << FirstDefaulted;
10477 
10478           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10479                           SecondMethod->getSourceRange(), MethodDefaulted)
10480               << SecondMethodType << SecondName << SecondDefaulted;
10481           Diagnosed = true;
10482           break;
10483         }
10484 
10485         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10486         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10487         const bool FirstPure = FirstMethod->isPure();
10488         const bool SecondPure = SecondMethod->isPure();
10489         if ((FirstVirtual || SecondVirtual) &&
10490             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10491           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10492                            FirstMethod->getSourceRange(), MethodVirtual)
10493               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10494           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10495                           SecondMethod->getSourceRange(), MethodVirtual)
10496               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10497           Diagnosed = true;
10498           break;
10499         }
10500 
10501         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10502         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10503         // class needs to be checked instead.
10504         const auto FirstStorage = FirstMethod->getStorageClass();
10505         const auto SecondStorage = SecondMethod->getStorageClass();
10506         const bool FirstStatic = FirstStorage == SC_Static;
10507         const bool SecondStatic = SecondStorage == SC_Static;
10508         if (FirstStatic != SecondStatic) {
10509           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10510                            FirstMethod->getSourceRange(), MethodStatic)
10511               << FirstMethodType << FirstName << FirstStatic;
10512           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10513                           SecondMethod->getSourceRange(), MethodStatic)
10514               << SecondMethodType << SecondName << SecondStatic;
10515           Diagnosed = true;
10516           break;
10517         }
10518 
10519         const bool FirstVolatile = FirstMethod->isVolatile();
10520         const bool SecondVolatile = SecondMethod->isVolatile();
10521         if (FirstVolatile != SecondVolatile) {
10522           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10523                            FirstMethod->getSourceRange(), MethodVolatile)
10524               << FirstMethodType << FirstName << FirstVolatile;
10525           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10526                           SecondMethod->getSourceRange(), MethodVolatile)
10527               << SecondMethodType << SecondName << SecondVolatile;
10528           Diagnosed = true;
10529           break;
10530         }
10531 
10532         const bool FirstConst = FirstMethod->isConst();
10533         const bool SecondConst = SecondMethod->isConst();
10534         if (FirstConst != SecondConst) {
10535           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10536                            FirstMethod->getSourceRange(), MethodConst)
10537               << FirstMethodType << FirstName << FirstConst;
10538           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10539                           SecondMethod->getSourceRange(), MethodConst)
10540               << SecondMethodType << SecondName << SecondConst;
10541           Diagnosed = true;
10542           break;
10543         }
10544 
10545         const bool FirstInline = FirstMethod->isInlineSpecified();
10546         const bool SecondInline = SecondMethod->isInlineSpecified();
10547         if (FirstInline != SecondInline) {
10548           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10549                            FirstMethod->getSourceRange(), MethodInline)
10550               << FirstMethodType << FirstName << FirstInline;
10551           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10552                           SecondMethod->getSourceRange(), MethodInline)
10553               << SecondMethodType << SecondName << SecondInline;
10554           Diagnosed = true;
10555           break;
10556         }
10557 
10558         const unsigned FirstNumParameters = FirstMethod->param_size();
10559         const unsigned SecondNumParameters = SecondMethod->param_size();
10560         if (FirstNumParameters != SecondNumParameters) {
10561           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10562                            FirstMethod->getSourceRange(),
10563                            MethodNumberParameters)
10564               << FirstMethodType << FirstName << FirstNumParameters;
10565           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10566                           SecondMethod->getSourceRange(),
10567                           MethodNumberParameters)
10568               << SecondMethodType << SecondName << SecondNumParameters;
10569           Diagnosed = true;
10570           break;
10571         }
10572 
10573         // Need this status boolean to know when break out of the switch.
10574         bool ParameterMismatch = false;
10575         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10576           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10577           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10578 
10579           QualType FirstParamType = FirstParam->getType();
10580           QualType SecondParamType = SecondParam->getType();
10581           if (FirstParamType != SecondParamType &&
10582               ComputeQualTypeODRHash(FirstParamType) !=
10583                   ComputeQualTypeODRHash(SecondParamType)) {
10584             if (const DecayedType *ParamDecayedType =
10585                     FirstParamType->getAs<DecayedType>()) {
10586               ODRDiagDeclError(
10587                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10588                   FirstMethod->getSourceRange(), MethodParameterType)
10589                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10590                   << true << ParamDecayedType->getOriginalType();
10591             } else {
10592               ODRDiagDeclError(
10593                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10594                   FirstMethod->getSourceRange(), MethodParameterType)
10595                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10596                   << false;
10597             }
10598 
10599             if (const DecayedType *ParamDecayedType =
10600                     SecondParamType->getAs<DecayedType>()) {
10601               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10602                               SecondMethod->getSourceRange(),
10603                               MethodParameterType)
10604                   << SecondMethodType << SecondName << (I + 1)
10605                   << SecondParamType << true
10606                   << ParamDecayedType->getOriginalType();
10607             } else {
10608               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10609                               SecondMethod->getSourceRange(),
10610                               MethodParameterType)
10611                   << SecondMethodType << SecondName << (I + 1)
10612                   << SecondParamType << false;
10613             }
10614             ParameterMismatch = true;
10615             break;
10616           }
10617 
10618           DeclarationName FirstParamName = FirstParam->getDeclName();
10619           DeclarationName SecondParamName = SecondParam->getDeclName();
10620           if (FirstParamName != SecondParamName) {
10621             ODRDiagDeclError(FirstRecord, FirstModule,
10622                              FirstMethod->getLocation(),
10623                              FirstMethod->getSourceRange(), MethodParameterName)
10624                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10625             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10626                             SecondMethod->getSourceRange(), MethodParameterName)
10627                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10628             ParameterMismatch = true;
10629             break;
10630           }
10631 
10632           const Expr *FirstInit = FirstParam->getInit();
10633           const Expr *SecondInit = SecondParam->getInit();
10634           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10635             ODRDiagDeclError(FirstRecord, FirstModule,
10636                              FirstMethod->getLocation(),
10637                              FirstMethod->getSourceRange(),
10638                              MethodParameterSingleDefaultArgument)
10639                 << FirstMethodType << FirstName << (I + 1)
10640                 << (FirstInit == nullptr)
10641                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10642             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10643                             SecondMethod->getSourceRange(),
10644                             MethodParameterSingleDefaultArgument)
10645                 << SecondMethodType << SecondName << (I + 1)
10646                 << (SecondInit == nullptr)
10647                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10648             ParameterMismatch = true;
10649             break;
10650           }
10651 
10652           if (FirstInit && SecondInit &&
10653               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10654             ODRDiagDeclError(FirstRecord, FirstModule,
10655                              FirstMethod->getLocation(),
10656                              FirstMethod->getSourceRange(),
10657                              MethodParameterDifferentDefaultArgument)
10658                 << FirstMethodType << FirstName << (I + 1)
10659                 << FirstInit->getSourceRange();
10660             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10661                             SecondMethod->getSourceRange(),
10662                             MethodParameterDifferentDefaultArgument)
10663                 << SecondMethodType << SecondName << (I + 1)
10664                 << SecondInit->getSourceRange();
10665             ParameterMismatch = true;
10666             break;
10667 
10668           }
10669         }
10670 
10671         if (ParameterMismatch) {
10672           Diagnosed = true;
10673           break;
10674         }
10675 
10676         const auto *FirstTemplateArgs =
10677             FirstMethod->getTemplateSpecializationArgs();
10678         const auto *SecondTemplateArgs =
10679             SecondMethod->getTemplateSpecializationArgs();
10680 
10681         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10682             (!FirstTemplateArgs && SecondTemplateArgs)) {
10683           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10684                            FirstMethod->getSourceRange(),
10685                            MethodNoTemplateArguments)
10686               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10687           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10688                           SecondMethod->getSourceRange(),
10689                           MethodNoTemplateArguments)
10690               << SecondMethodType << SecondName
10691               << (SecondTemplateArgs != nullptr);
10692 
10693           Diagnosed = true;
10694           break;
10695         }
10696 
10697         if (FirstTemplateArgs && SecondTemplateArgs) {
10698           // Remove pack expansions from argument list.
10699           auto ExpandTemplateArgumentList =
10700               [](const TemplateArgumentList *TAL) {
10701                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10702                 for (const TemplateArgument &TA : TAL->asArray()) {
10703                   if (TA.getKind() != TemplateArgument::Pack) {
10704                     ExpandedList.push_back(&TA);
10705                     continue;
10706                   }
10707                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10708                     ExpandedList.push_back(&PackTA);
10709                   }
10710                 }
10711                 return ExpandedList;
10712               };
10713           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10714               ExpandTemplateArgumentList(FirstTemplateArgs);
10715           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10716               ExpandTemplateArgumentList(SecondTemplateArgs);
10717 
10718           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10719             ODRDiagDeclError(FirstRecord, FirstModule,
10720                              FirstMethod->getLocation(),
10721                              FirstMethod->getSourceRange(),
10722                              MethodDifferentNumberTemplateArguments)
10723                 << FirstMethodType << FirstName
10724                 << (unsigned)FirstExpandedList.size();
10725             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10726                             SecondMethod->getSourceRange(),
10727                             MethodDifferentNumberTemplateArguments)
10728                 << SecondMethodType << SecondName
10729                 << (unsigned)SecondExpandedList.size();
10730 
10731             Diagnosed = true;
10732             break;
10733           }
10734 
10735           bool TemplateArgumentMismatch = false;
10736           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10737             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10738                                    &SecondTA = *SecondExpandedList[i];
10739             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10740                 ComputeTemplateArgumentODRHash(SecondTA)) {
10741               continue;
10742             }
10743 
10744             ODRDiagDeclError(
10745                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10746                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10747                 << FirstMethodType << FirstName << FirstTA << i + 1;
10748             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10749                             SecondMethod->getSourceRange(),
10750                             MethodDifferentTemplateArgument)
10751                 << SecondMethodType << SecondName << SecondTA << i + 1;
10752 
10753             TemplateArgumentMismatch = true;
10754             break;
10755           }
10756 
10757           if (TemplateArgumentMismatch) {
10758             Diagnosed = true;
10759             break;
10760           }
10761         }
10762 
10763         // Compute the hash of the method as if it has no body.
10764         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10765           Hash.clear();
10766           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10767           return Hash.CalculateHash();
10768         };
10769 
10770         // Compare the hash generated to the hash stored.  A difference means
10771         // that a body was present in the original source.  Due to merging,
10772         // the stardard way of detecting a body will not work.
10773         const bool HasFirstBody =
10774             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10775         const bool HasSecondBody =
10776             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10777 
10778         if (HasFirstBody != HasSecondBody) {
10779           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10780                            FirstMethod->getSourceRange(), MethodSingleBody)
10781               << FirstMethodType << FirstName << HasFirstBody;
10782           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10783                           SecondMethod->getSourceRange(), MethodSingleBody)
10784               << SecondMethodType << SecondName << HasSecondBody;
10785           Diagnosed = true;
10786           break;
10787         }
10788 
10789         if (HasFirstBody && HasSecondBody) {
10790           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10791                            FirstMethod->getSourceRange(), MethodDifferentBody)
10792               << FirstMethodType << FirstName;
10793           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10794                           SecondMethod->getSourceRange(), MethodDifferentBody)
10795               << SecondMethodType << SecondName;
10796           Diagnosed = true;
10797           break;
10798         }
10799 
10800         break;
10801       }
10802       case TypeAlias:
10803       case TypeDef: {
10804         Diagnosed = ODRDiagTypeDefOrAlias(
10805             FirstRecord, FirstModule, SecondModule,
10806             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10807             FirstDiffType == TypeAlias);
10808         break;
10809       }
10810       case Var: {
10811         Diagnosed =
10812             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10813                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10814         break;
10815       }
10816       case Friend: {
10817         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10818         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10819 
10820         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10821         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10822 
10823         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10824         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10825 
10826         if (FirstND && SecondND) {
10827           ODRDiagDeclError(FirstRecord, FirstModule,
10828                            FirstFriend->getFriendLoc(),
10829                            FirstFriend->getSourceRange(), FriendFunction)
10830               << FirstND;
10831           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10832                           SecondFriend->getSourceRange(), FriendFunction)
10833               << SecondND;
10834 
10835           Diagnosed = true;
10836           break;
10837         }
10838 
10839         if (FirstTSI && SecondTSI) {
10840           QualType FirstFriendType = FirstTSI->getType();
10841           QualType SecondFriendType = SecondTSI->getType();
10842           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10843                  ComputeQualTypeODRHash(SecondFriendType));
10844           ODRDiagDeclError(FirstRecord, FirstModule,
10845                            FirstFriend->getFriendLoc(),
10846                            FirstFriend->getSourceRange(), FriendType)
10847               << FirstFriendType;
10848           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10849                           SecondFriend->getSourceRange(), FriendType)
10850               << SecondFriendType;
10851           Diagnosed = true;
10852           break;
10853         }
10854 
10855         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10856                          FirstFriend->getSourceRange(), FriendTypeFunction)
10857             << (FirstTSI == nullptr);
10858         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10859                         SecondFriend->getSourceRange(), FriendTypeFunction)
10860             << (SecondTSI == nullptr);
10861 
10862         Diagnosed = true;
10863         break;
10864       }
10865       case FunctionTemplate: {
10866         FunctionTemplateDecl *FirstTemplate =
10867             cast<FunctionTemplateDecl>(FirstDecl);
10868         FunctionTemplateDecl *SecondTemplate =
10869             cast<FunctionTemplateDecl>(SecondDecl);
10870 
10871         TemplateParameterList *FirstTPL =
10872             FirstTemplate->getTemplateParameters();
10873         TemplateParameterList *SecondTPL =
10874             SecondTemplate->getTemplateParameters();
10875 
10876         if (FirstTPL->size() != SecondTPL->size()) {
10877           ODRDiagDeclError(FirstRecord, FirstModule,
10878                            FirstTemplate->getLocation(),
10879                            FirstTemplate->getSourceRange(),
10880                            FunctionTemplateDifferentNumberParameters)
10881               << FirstTemplate << FirstTPL->size();
10882           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10883                           SecondTemplate->getSourceRange(),
10884                           FunctionTemplateDifferentNumberParameters)
10885               << SecondTemplate << SecondTPL->size();
10886 
10887           Diagnosed = true;
10888           break;
10889         }
10890 
10891         bool ParameterMismatch = false;
10892         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10893           NamedDecl *FirstParam = FirstTPL->getParam(i);
10894           NamedDecl *SecondParam = SecondTPL->getParam(i);
10895 
10896           if (FirstParam->getKind() != SecondParam->getKind()) {
10897             enum {
10898               TemplateTypeParameter,
10899               NonTypeTemplateParameter,
10900               TemplateTemplateParameter,
10901             };
10902             auto GetParamType = [](NamedDecl *D) {
10903               switch (D->getKind()) {
10904                 default:
10905                   llvm_unreachable("Unexpected template parameter type");
10906                 case Decl::TemplateTypeParm:
10907                   return TemplateTypeParameter;
10908                 case Decl::NonTypeTemplateParm:
10909                   return NonTypeTemplateParameter;
10910                 case Decl::TemplateTemplateParm:
10911                   return TemplateTemplateParameter;
10912               }
10913             };
10914 
10915             ODRDiagDeclError(FirstRecord, FirstModule,
10916                              FirstTemplate->getLocation(),
10917                              FirstTemplate->getSourceRange(),
10918                              FunctionTemplateParameterDifferentKind)
10919                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10920             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10921                             SecondTemplate->getSourceRange(),
10922                             FunctionTemplateParameterDifferentKind)
10923                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10924 
10925             ParameterMismatch = true;
10926             break;
10927           }
10928 
10929           if (FirstParam->getName() != SecondParam->getName()) {
10930             ODRDiagDeclError(
10931                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10932                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10933                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10934                 << FirstParam;
10935             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10936                             SecondTemplate->getSourceRange(),
10937                             FunctionTemplateParameterName)
10938                 << SecondTemplate << (i + 1)
10939                 << (bool)SecondParam->getIdentifier() << SecondParam;
10940             ParameterMismatch = true;
10941             break;
10942           }
10943 
10944           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10945               isa<TemplateTypeParmDecl>(SecondParam)) {
10946             TemplateTypeParmDecl *FirstTTPD =
10947                 cast<TemplateTypeParmDecl>(FirstParam);
10948             TemplateTypeParmDecl *SecondTTPD =
10949                 cast<TemplateTypeParmDecl>(SecondParam);
10950             bool HasFirstDefaultArgument =
10951                 FirstTTPD->hasDefaultArgument() &&
10952                 !FirstTTPD->defaultArgumentWasInherited();
10953             bool HasSecondDefaultArgument =
10954                 SecondTTPD->hasDefaultArgument() &&
10955                 !SecondTTPD->defaultArgumentWasInherited();
10956             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10957               ODRDiagDeclError(FirstRecord, FirstModule,
10958                                FirstTemplate->getLocation(),
10959                                FirstTemplate->getSourceRange(),
10960                                FunctionTemplateParameterSingleDefaultArgument)
10961                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10962               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10963                               SecondTemplate->getSourceRange(),
10964                               FunctionTemplateParameterSingleDefaultArgument)
10965                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10966               ParameterMismatch = true;
10967               break;
10968             }
10969 
10970             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10971               QualType FirstType = FirstTTPD->getDefaultArgument();
10972               QualType SecondType = SecondTTPD->getDefaultArgument();
10973               if (ComputeQualTypeODRHash(FirstType) !=
10974                   ComputeQualTypeODRHash(SecondType)) {
10975                 ODRDiagDeclError(
10976                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10977                     FirstTemplate->getSourceRange(),
10978                     FunctionTemplateParameterDifferentDefaultArgument)
10979                     << FirstTemplate << (i + 1) << FirstType;
10980                 ODRDiagDeclNote(
10981                     SecondModule, SecondTemplate->getLocation(),
10982                     SecondTemplate->getSourceRange(),
10983                     FunctionTemplateParameterDifferentDefaultArgument)
10984                     << SecondTemplate << (i + 1) << SecondType;
10985                 ParameterMismatch = true;
10986                 break;
10987               }
10988             }
10989 
10990             if (FirstTTPD->isParameterPack() !=
10991                 SecondTTPD->isParameterPack()) {
10992               ODRDiagDeclError(FirstRecord, FirstModule,
10993                                FirstTemplate->getLocation(),
10994                                FirstTemplate->getSourceRange(),
10995                                FunctionTemplatePackParameter)
10996                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10997               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10998                               SecondTemplate->getSourceRange(),
10999                               FunctionTemplatePackParameter)
11000                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11001               ParameterMismatch = true;
11002               break;
11003             }
11004           }
11005 
11006           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11007               isa<TemplateTemplateParmDecl>(SecondParam)) {
11008             TemplateTemplateParmDecl *FirstTTPD =
11009                 cast<TemplateTemplateParmDecl>(FirstParam);
11010             TemplateTemplateParmDecl *SecondTTPD =
11011                 cast<TemplateTemplateParmDecl>(SecondParam);
11012 
11013             TemplateParameterList *FirstTPL =
11014                 FirstTTPD->getTemplateParameters();
11015             TemplateParameterList *SecondTPL =
11016                 SecondTTPD->getTemplateParameters();
11017 
11018             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11019                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11020               ODRDiagDeclError(FirstRecord, FirstModule,
11021                                FirstTemplate->getLocation(),
11022                                FirstTemplate->getSourceRange(),
11023                                FunctionTemplateParameterDifferentType)
11024                   << FirstTemplate << (i + 1);
11025               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11026                               SecondTemplate->getSourceRange(),
11027                               FunctionTemplateParameterDifferentType)
11028                   << SecondTemplate << (i + 1);
11029               ParameterMismatch = true;
11030               break;
11031             }
11032 
11033             bool HasFirstDefaultArgument =
11034                 FirstTTPD->hasDefaultArgument() &&
11035                 !FirstTTPD->defaultArgumentWasInherited();
11036             bool HasSecondDefaultArgument =
11037                 SecondTTPD->hasDefaultArgument() &&
11038                 !SecondTTPD->defaultArgumentWasInherited();
11039             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11040               ODRDiagDeclError(FirstRecord, FirstModule,
11041                                FirstTemplate->getLocation(),
11042                                FirstTemplate->getSourceRange(),
11043                                FunctionTemplateParameterSingleDefaultArgument)
11044                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11045               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11046                               SecondTemplate->getSourceRange(),
11047                               FunctionTemplateParameterSingleDefaultArgument)
11048                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11049               ParameterMismatch = true;
11050               break;
11051             }
11052 
11053             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11054               TemplateArgument FirstTA =
11055                   FirstTTPD->getDefaultArgument().getArgument();
11056               TemplateArgument SecondTA =
11057                   SecondTTPD->getDefaultArgument().getArgument();
11058               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11059                   ComputeTemplateArgumentODRHash(SecondTA)) {
11060                 ODRDiagDeclError(
11061                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11062                     FirstTemplate->getSourceRange(),
11063                     FunctionTemplateParameterDifferentDefaultArgument)
11064                     << FirstTemplate << (i + 1) << FirstTA;
11065                 ODRDiagDeclNote(
11066                     SecondModule, SecondTemplate->getLocation(),
11067                     SecondTemplate->getSourceRange(),
11068                     FunctionTemplateParameterDifferentDefaultArgument)
11069                     << SecondTemplate << (i + 1) << SecondTA;
11070                 ParameterMismatch = true;
11071                 break;
11072               }
11073             }
11074 
11075             if (FirstTTPD->isParameterPack() !=
11076                 SecondTTPD->isParameterPack()) {
11077               ODRDiagDeclError(FirstRecord, FirstModule,
11078                                FirstTemplate->getLocation(),
11079                                FirstTemplate->getSourceRange(),
11080                                FunctionTemplatePackParameter)
11081                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11082               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11083                               SecondTemplate->getSourceRange(),
11084                               FunctionTemplatePackParameter)
11085                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11086               ParameterMismatch = true;
11087               break;
11088             }
11089           }
11090 
11091           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11092               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11093             NonTypeTemplateParmDecl *FirstNTTPD =
11094                 cast<NonTypeTemplateParmDecl>(FirstParam);
11095             NonTypeTemplateParmDecl *SecondNTTPD =
11096                 cast<NonTypeTemplateParmDecl>(SecondParam);
11097 
11098             QualType FirstType = FirstNTTPD->getType();
11099             QualType SecondType = SecondNTTPD->getType();
11100             if (ComputeQualTypeODRHash(FirstType) !=
11101                 ComputeQualTypeODRHash(SecondType)) {
11102               ODRDiagDeclError(FirstRecord, FirstModule,
11103                                FirstTemplate->getLocation(),
11104                                FirstTemplate->getSourceRange(),
11105                                FunctionTemplateParameterDifferentType)
11106                   << FirstTemplate << (i + 1);
11107               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11108                               SecondTemplate->getSourceRange(),
11109                               FunctionTemplateParameterDifferentType)
11110                   << SecondTemplate << (i + 1);
11111               ParameterMismatch = true;
11112               break;
11113             }
11114 
11115             bool HasFirstDefaultArgument =
11116                 FirstNTTPD->hasDefaultArgument() &&
11117                 !FirstNTTPD->defaultArgumentWasInherited();
11118             bool HasSecondDefaultArgument =
11119                 SecondNTTPD->hasDefaultArgument() &&
11120                 !SecondNTTPD->defaultArgumentWasInherited();
11121             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11122               ODRDiagDeclError(FirstRecord, FirstModule,
11123                                FirstTemplate->getLocation(),
11124                                FirstTemplate->getSourceRange(),
11125                                FunctionTemplateParameterSingleDefaultArgument)
11126                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11127               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11128                               SecondTemplate->getSourceRange(),
11129                               FunctionTemplateParameterSingleDefaultArgument)
11130                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11131               ParameterMismatch = true;
11132               break;
11133             }
11134 
11135             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11136               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11137               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11138               if (ComputeODRHash(FirstDefaultArgument) !=
11139                   ComputeODRHash(SecondDefaultArgument)) {
11140                 ODRDiagDeclError(
11141                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11142                     FirstTemplate->getSourceRange(),
11143                     FunctionTemplateParameterDifferentDefaultArgument)
11144                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11145                 ODRDiagDeclNote(
11146                     SecondModule, SecondTemplate->getLocation(),
11147                     SecondTemplate->getSourceRange(),
11148                     FunctionTemplateParameterDifferentDefaultArgument)
11149                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11150                 ParameterMismatch = true;
11151                 break;
11152               }
11153             }
11154 
11155             if (FirstNTTPD->isParameterPack() !=
11156                 SecondNTTPD->isParameterPack()) {
11157               ODRDiagDeclError(FirstRecord, FirstModule,
11158                                FirstTemplate->getLocation(),
11159                                FirstTemplate->getSourceRange(),
11160                                FunctionTemplatePackParameter)
11161                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11162               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11163                               SecondTemplate->getSourceRange(),
11164                               FunctionTemplatePackParameter)
11165                   << SecondTemplate << (i + 1)
11166                   << SecondNTTPD->isParameterPack();
11167               ParameterMismatch = true;
11168               break;
11169             }
11170           }
11171         }
11172 
11173         if (ParameterMismatch) {
11174           Diagnosed = true;
11175           break;
11176         }
11177 
11178         break;
11179       }
11180       }
11181 
11182       if (Diagnosed)
11183         continue;
11184 
11185       Diag(FirstDecl->getLocation(),
11186            diag::err_module_odr_violation_mismatch_decl_unknown)
11187           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11188           << FirstDecl->getSourceRange();
11189       Diag(SecondDecl->getLocation(),
11190            diag::note_module_odr_violation_mismatch_decl_unknown)
11191           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11192       Diagnosed = true;
11193     }
11194 
11195     if (!Diagnosed) {
11196       // All definitions are updates to the same declaration. This happens if a
11197       // module instantiates the declaration of a class template specialization
11198       // and two or more other modules instantiate its definition.
11199       //
11200       // FIXME: Indicate which modules had instantiations of this definition.
11201       // FIXME: How can this even happen?
11202       Diag(Merge.first->getLocation(),
11203            diag::err_module_odr_violation_different_instantiations)
11204         << Merge.first;
11205     }
11206   }
11207 
11208   // Issue ODR failures diagnostics for functions.
11209   for (auto &Merge : FunctionOdrMergeFailures) {
11210     enum ODRFunctionDifference {
11211       ReturnType,
11212       ParameterName,
11213       ParameterType,
11214       ParameterSingleDefaultArgument,
11215       ParameterDifferentDefaultArgument,
11216       FunctionBody,
11217     };
11218 
11219     FunctionDecl *FirstFunction = Merge.first;
11220     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11221 
11222     bool Diagnosed = false;
11223     for (auto &SecondFunction : Merge.second) {
11224 
11225       if (FirstFunction == SecondFunction)
11226         continue;
11227 
11228       std::string SecondModule =
11229           getOwningModuleNameForDiagnostic(SecondFunction);
11230 
11231       auto ODRDiagError = [FirstFunction, &FirstModule,
11232                            this](SourceLocation Loc, SourceRange Range,
11233                                  ODRFunctionDifference DiffType) {
11234         return Diag(Loc, diag::err_module_odr_violation_function)
11235                << FirstFunction << FirstModule.empty() << FirstModule << Range
11236                << DiffType;
11237       };
11238       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11239                                                SourceRange Range,
11240                                                ODRFunctionDifference DiffType) {
11241         return Diag(Loc, diag::note_module_odr_violation_function)
11242                << SecondModule << Range << DiffType;
11243       };
11244 
11245       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11246           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11247         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11248                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11249             << FirstFunction->getReturnType();
11250         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11251                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11252             << SecondFunction->getReturnType();
11253         Diagnosed = true;
11254         break;
11255       }
11256 
11257       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11258              "Merged functions with different number of parameters");
11259 
11260       auto ParamSize = FirstFunction->param_size();
11261       bool ParameterMismatch = false;
11262       for (unsigned I = 0; I < ParamSize; ++I) {
11263         auto *FirstParam = FirstFunction->getParamDecl(I);
11264         auto *SecondParam = SecondFunction->getParamDecl(I);
11265 
11266         assert(getContext().hasSameType(FirstParam->getType(),
11267                                       SecondParam->getType()) &&
11268                "Merged function has different parameter types.");
11269 
11270         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11271           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11272                        ParameterName)
11273               << I + 1 << FirstParam->getDeclName();
11274           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11275                       ParameterName)
11276               << I + 1 << SecondParam->getDeclName();
11277           ParameterMismatch = true;
11278           break;
11279         };
11280 
11281         QualType FirstParamType = FirstParam->getType();
11282         QualType SecondParamType = SecondParam->getType();
11283         if (FirstParamType != SecondParamType &&
11284             ComputeQualTypeODRHash(FirstParamType) !=
11285                 ComputeQualTypeODRHash(SecondParamType)) {
11286           if (const DecayedType *ParamDecayedType =
11287                   FirstParamType->getAs<DecayedType>()) {
11288             ODRDiagError(FirstParam->getLocation(),
11289                          FirstParam->getSourceRange(), ParameterType)
11290                 << (I + 1) << FirstParamType << true
11291                 << ParamDecayedType->getOriginalType();
11292           } else {
11293             ODRDiagError(FirstParam->getLocation(),
11294                          FirstParam->getSourceRange(), ParameterType)
11295                 << (I + 1) << FirstParamType << false;
11296           }
11297 
11298           if (const DecayedType *ParamDecayedType =
11299                   SecondParamType->getAs<DecayedType>()) {
11300             ODRDiagNote(SecondParam->getLocation(),
11301                         SecondParam->getSourceRange(), ParameterType)
11302                 << (I + 1) << SecondParamType << true
11303                 << ParamDecayedType->getOriginalType();
11304           } else {
11305             ODRDiagNote(SecondParam->getLocation(),
11306                         SecondParam->getSourceRange(), ParameterType)
11307                 << (I + 1) << SecondParamType << false;
11308           }
11309           ParameterMismatch = true;
11310           break;
11311         }
11312 
11313         const Expr *FirstInit = FirstParam->getInit();
11314         const Expr *SecondInit = SecondParam->getInit();
11315         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11316           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11317                        ParameterSingleDefaultArgument)
11318               << (I + 1) << (FirstInit == nullptr)
11319               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11320           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11321                       ParameterSingleDefaultArgument)
11322               << (I + 1) << (SecondInit == nullptr)
11323               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11324           ParameterMismatch = true;
11325           break;
11326         }
11327 
11328         if (FirstInit && SecondInit &&
11329             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11330           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11331                        ParameterDifferentDefaultArgument)
11332               << (I + 1) << FirstInit->getSourceRange();
11333           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11334                       ParameterDifferentDefaultArgument)
11335               << (I + 1) << SecondInit->getSourceRange();
11336           ParameterMismatch = true;
11337           break;
11338         }
11339 
11340         assert(ComputeSubDeclODRHash(FirstParam) ==
11341                    ComputeSubDeclODRHash(SecondParam) &&
11342                "Undiagnosed parameter difference.");
11343       }
11344 
11345       if (ParameterMismatch) {
11346         Diagnosed = true;
11347         break;
11348       }
11349 
11350       // If no error has been generated before now, assume the problem is in
11351       // the body and generate a message.
11352       ODRDiagError(FirstFunction->getLocation(),
11353                    FirstFunction->getSourceRange(), FunctionBody);
11354       ODRDiagNote(SecondFunction->getLocation(),
11355                   SecondFunction->getSourceRange(), FunctionBody);
11356       Diagnosed = true;
11357       break;
11358     }
11359     (void)Diagnosed;
11360     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11361   }
11362 
11363   // Issue ODR failures diagnostics for enums.
11364   for (auto &Merge : EnumOdrMergeFailures) {
11365     enum ODREnumDifference {
11366       SingleScopedEnum,
11367       EnumTagKeywordMismatch,
11368       SingleSpecifiedType,
11369       DifferentSpecifiedTypes,
11370       DifferentNumberEnumConstants,
11371       EnumConstantName,
11372       EnumConstantSingleInitilizer,
11373       EnumConstantDifferentInitilizer,
11374     };
11375 
11376     // If we've already pointed out a specific problem with this enum, don't
11377     // bother issuing a general "something's different" diagnostic.
11378     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11379       continue;
11380 
11381     EnumDecl *FirstEnum = Merge.first;
11382     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11383 
11384     using DeclHashes =
11385         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11386     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11387                               DeclHashes &Hashes, EnumDecl *Enum) {
11388       for (auto *D : Enum->decls()) {
11389         // Due to decl merging, the first EnumDecl is the parent of
11390         // Decls in both records.
11391         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11392           continue;
11393         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11394         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11395                             ComputeSubDeclODRHash(D));
11396       }
11397     };
11398     DeclHashes FirstHashes;
11399     PopulateHashes(FirstHashes, FirstEnum);
11400     bool Diagnosed = false;
11401     for (auto &SecondEnum : Merge.second) {
11402 
11403       if (FirstEnum == SecondEnum)
11404         continue;
11405 
11406       std::string SecondModule =
11407           getOwningModuleNameForDiagnostic(SecondEnum);
11408 
11409       auto ODRDiagError = [FirstEnum, &FirstModule,
11410                            this](SourceLocation Loc, SourceRange Range,
11411                                  ODREnumDifference DiffType) {
11412         return Diag(Loc, diag::err_module_odr_violation_enum)
11413                << FirstEnum << FirstModule.empty() << FirstModule << Range
11414                << DiffType;
11415       };
11416       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11417                                                SourceRange Range,
11418                                                ODREnumDifference DiffType) {
11419         return Diag(Loc, diag::note_module_odr_violation_enum)
11420                << SecondModule << Range << DiffType;
11421       };
11422 
11423       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11424         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11425                      SingleScopedEnum)
11426             << FirstEnum->isScoped();
11427         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11428                     SingleScopedEnum)
11429             << SecondEnum->isScoped();
11430         Diagnosed = true;
11431         continue;
11432       }
11433 
11434       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11435         if (FirstEnum->isScopedUsingClassTag() !=
11436             SecondEnum->isScopedUsingClassTag()) {
11437           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11438                        EnumTagKeywordMismatch)
11439               << FirstEnum->isScopedUsingClassTag();
11440           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11441                       EnumTagKeywordMismatch)
11442               << SecondEnum->isScopedUsingClassTag();
11443           Diagnosed = true;
11444           continue;
11445         }
11446       }
11447 
11448       QualType FirstUnderlyingType =
11449           FirstEnum->getIntegerTypeSourceInfo()
11450               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11451               : QualType();
11452       QualType SecondUnderlyingType =
11453           SecondEnum->getIntegerTypeSourceInfo()
11454               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11455               : QualType();
11456       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11457           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11458                        SingleSpecifiedType)
11459               << !FirstUnderlyingType.isNull();
11460           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11461                       SingleSpecifiedType)
11462               << !SecondUnderlyingType.isNull();
11463           Diagnosed = true;
11464           continue;
11465       }
11466 
11467       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11468         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11469             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11470           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11471                        DifferentSpecifiedTypes)
11472               << FirstUnderlyingType;
11473           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11474                       DifferentSpecifiedTypes)
11475               << SecondUnderlyingType;
11476           Diagnosed = true;
11477           continue;
11478         }
11479       }
11480 
11481       DeclHashes SecondHashes;
11482       PopulateHashes(SecondHashes, SecondEnum);
11483 
11484       if (FirstHashes.size() != SecondHashes.size()) {
11485         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11486                      DifferentNumberEnumConstants)
11487             << (int)FirstHashes.size();
11488         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11489                     DifferentNumberEnumConstants)
11490             << (int)SecondHashes.size();
11491         Diagnosed = true;
11492         continue;
11493       }
11494 
11495       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11496         if (FirstHashes[I].second == SecondHashes[I].second)
11497           continue;
11498         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11499         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11500 
11501         if (FirstEnumConstant->getDeclName() !=
11502             SecondEnumConstant->getDeclName()) {
11503 
11504           ODRDiagError(FirstEnumConstant->getLocation(),
11505                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11506               << I + 1 << FirstEnumConstant;
11507           ODRDiagNote(SecondEnumConstant->getLocation(),
11508                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11509               << I + 1 << SecondEnumConstant;
11510           Diagnosed = true;
11511           break;
11512         }
11513 
11514         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11515         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11516         if (!FirstInit && !SecondInit)
11517           continue;
11518 
11519         if (!FirstInit || !SecondInit) {
11520           ODRDiagError(FirstEnumConstant->getLocation(),
11521                        FirstEnumConstant->getSourceRange(),
11522                        EnumConstantSingleInitilizer)
11523               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11524           ODRDiagNote(SecondEnumConstant->getLocation(),
11525                       SecondEnumConstant->getSourceRange(),
11526                       EnumConstantSingleInitilizer)
11527               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11528           Diagnosed = true;
11529           break;
11530         }
11531 
11532         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11533           ODRDiagError(FirstEnumConstant->getLocation(),
11534                        FirstEnumConstant->getSourceRange(),
11535                        EnumConstantDifferentInitilizer)
11536               << I + 1 << FirstEnumConstant;
11537           ODRDiagNote(SecondEnumConstant->getLocation(),
11538                       SecondEnumConstant->getSourceRange(),
11539                       EnumConstantDifferentInitilizer)
11540               << I + 1 << SecondEnumConstant;
11541           Diagnosed = true;
11542           break;
11543         }
11544       }
11545     }
11546 
11547     (void)Diagnosed;
11548     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11549   }
11550 }
11551 
11552 void ASTReader::StartedDeserializing() {
11553   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11554     ReadTimer->startTimer();
11555 }
11556 
11557 void ASTReader::FinishedDeserializing() {
11558   assert(NumCurrentElementsDeserializing &&
11559          "FinishedDeserializing not paired with StartedDeserializing");
11560   if (NumCurrentElementsDeserializing == 1) {
11561     // We decrease NumCurrentElementsDeserializing only after pending actions
11562     // are finished, to avoid recursively re-calling finishPendingActions().
11563     finishPendingActions();
11564   }
11565   --NumCurrentElementsDeserializing;
11566 
11567   if (NumCurrentElementsDeserializing == 0) {
11568     // Propagate exception specification and deduced type updates along
11569     // redeclaration chains.
11570     //
11571     // We do this now rather than in finishPendingActions because we want to
11572     // be able to walk the complete redeclaration chains of the updated decls.
11573     while (!PendingExceptionSpecUpdates.empty() ||
11574            !PendingDeducedTypeUpdates.empty()) {
11575       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11576       PendingExceptionSpecUpdates.clear();
11577       for (auto Update : ESUpdates) {
11578         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11579         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11580         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11581         if (auto *Listener = getContext().getASTMutationListener())
11582           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11583         for (auto *Redecl : Update.second->redecls())
11584           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11585       }
11586 
11587       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11588       PendingDeducedTypeUpdates.clear();
11589       for (auto Update : DTUpdates) {
11590         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11591         // FIXME: If the return type is already deduced, check that it matches.
11592         getContext().adjustDeducedFunctionResultType(Update.first,
11593                                                      Update.second);
11594       }
11595     }
11596 
11597     if (ReadTimer)
11598       ReadTimer->stopTimer();
11599 
11600     diagnoseOdrViolations();
11601 
11602     // We are not in recursive loading, so it's safe to pass the "interesting"
11603     // decls to the consumer.
11604     if (Consumer)
11605       PassInterestingDeclsToConsumer();
11606   }
11607 }
11608 
11609 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11610   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11611     // Remove any fake results before adding any real ones.
11612     auto It = PendingFakeLookupResults.find(II);
11613     if (It != PendingFakeLookupResults.end()) {
11614       for (auto *ND : It->second)
11615         SemaObj->IdResolver.RemoveDecl(ND);
11616       // FIXME: this works around module+PCH performance issue.
11617       // Rather than erase the result from the map, which is O(n), just clear
11618       // the vector of NamedDecls.
11619       It->second.clear();
11620     }
11621   }
11622 
11623   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11624     SemaObj->TUScope->AddDecl(D);
11625   } else if (SemaObj->TUScope) {
11626     // Adding the decl to IdResolver may have failed because it was already in
11627     // (even though it was not added in scope). If it is already in, make sure
11628     // it gets in the scope as well.
11629     if (std::find(SemaObj->IdResolver.begin(Name),
11630                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11631       SemaObj->TUScope->AddDecl(D);
11632   }
11633 }
11634 
11635 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11636                      ASTContext *Context,
11637                      const PCHContainerReader &PCHContainerRdr,
11638                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11639                      StringRef isysroot,
11640                      DisableValidationForModuleKind DisableValidationKind,
11641                      bool AllowASTWithCompilerErrors,
11642                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11643                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11644                      std::unique_ptr<llvm::Timer> ReadTimer)
11645     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11646                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11647                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11648       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11649       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11650       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11651                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11652       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11653       DisableValidationKind(DisableValidationKind),
11654       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11655       AllowConfigurationMismatch(AllowConfigurationMismatch),
11656       ValidateSystemInputs(ValidateSystemInputs),
11657       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11658       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11659   SourceMgr.setExternalSLocEntrySource(this);
11660 
11661   for (const auto &Ext : Extensions) {
11662     auto BlockName = Ext->getExtensionMetadata().BlockName;
11663     auto Known = ModuleFileExtensions.find(BlockName);
11664     if (Known != ModuleFileExtensions.end()) {
11665       Diags.Report(diag::warn_duplicate_module_file_extension)
11666         << BlockName;
11667       continue;
11668     }
11669 
11670     ModuleFileExtensions.insert({BlockName, Ext});
11671   }
11672 }
11673 
11674 ASTReader::~ASTReader() {
11675   if (OwnsDeserializationListener)
11676     delete DeserializationListener;
11677 }
11678 
11679 IdentifierResolver &ASTReader::getIdResolver() {
11680   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11681 }
11682 
11683 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11684                                                unsigned AbbrevID) {
11685   Idx = 0;
11686   Record.clear();
11687   return Cursor.readRecord(AbbrevID, Record);
11688 }
11689 //===----------------------------------------------------------------------===//
11690 //// OMPClauseReader implementation
11691 ////===----------------------------------------------------------------------===//
11692 
11693 // This has to be in namespace clang because it's friended by all
11694 // of the OMP clauses.
11695 namespace clang {
11696 
11697 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11698   ASTRecordReader &Record;
11699   ASTContext &Context;
11700 
11701 public:
11702   OMPClauseReader(ASTRecordReader &Record)
11703       : Record(Record), Context(Record.getContext()) {}
11704 #define GEN_CLANG_CLAUSE_CLASS
11705 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11706 #include "llvm/Frontend/OpenMP/OMP.inc"
11707   OMPClause *readClause();
11708   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11709   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11710 };
11711 
11712 } // end namespace clang
11713 
11714 OMPClause *ASTRecordReader::readOMPClause() {
11715   return OMPClauseReader(*this).readClause();
11716 }
11717 
11718 OMPClause *OMPClauseReader::readClause() {
11719   OMPClause *C = nullptr;
11720   switch (llvm::omp::Clause(Record.readInt())) {
11721   case llvm::omp::OMPC_if:
11722     C = new (Context) OMPIfClause();
11723     break;
11724   case llvm::omp::OMPC_final:
11725     C = new (Context) OMPFinalClause();
11726     break;
11727   case llvm::omp::OMPC_num_threads:
11728     C = new (Context) OMPNumThreadsClause();
11729     break;
11730   case llvm::omp::OMPC_safelen:
11731     C = new (Context) OMPSafelenClause();
11732     break;
11733   case llvm::omp::OMPC_simdlen:
11734     C = new (Context) OMPSimdlenClause();
11735     break;
11736   case llvm::omp::OMPC_sizes: {
11737     unsigned NumSizes = Record.readInt();
11738     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11739     break;
11740   }
11741   case llvm::omp::OMPC_full:
11742     C = OMPFullClause::CreateEmpty(Context);
11743     break;
11744   case llvm::omp::OMPC_partial:
11745     C = OMPPartialClause::CreateEmpty(Context);
11746     break;
11747   case llvm::omp::OMPC_allocator:
11748     C = new (Context) OMPAllocatorClause();
11749     break;
11750   case llvm::omp::OMPC_collapse:
11751     C = new (Context) OMPCollapseClause();
11752     break;
11753   case llvm::omp::OMPC_default:
11754     C = new (Context) OMPDefaultClause();
11755     break;
11756   case llvm::omp::OMPC_proc_bind:
11757     C = new (Context) OMPProcBindClause();
11758     break;
11759   case llvm::omp::OMPC_schedule:
11760     C = new (Context) OMPScheduleClause();
11761     break;
11762   case llvm::omp::OMPC_ordered:
11763     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11764     break;
11765   case llvm::omp::OMPC_nowait:
11766     C = new (Context) OMPNowaitClause();
11767     break;
11768   case llvm::omp::OMPC_untied:
11769     C = new (Context) OMPUntiedClause();
11770     break;
11771   case llvm::omp::OMPC_mergeable:
11772     C = new (Context) OMPMergeableClause();
11773     break;
11774   case llvm::omp::OMPC_read:
11775     C = new (Context) OMPReadClause();
11776     break;
11777   case llvm::omp::OMPC_write:
11778     C = new (Context) OMPWriteClause();
11779     break;
11780   case llvm::omp::OMPC_update:
11781     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11782     break;
11783   case llvm::omp::OMPC_capture:
11784     C = new (Context) OMPCaptureClause();
11785     break;
11786   case llvm::omp::OMPC_seq_cst:
11787     C = new (Context) OMPSeqCstClause();
11788     break;
11789   case llvm::omp::OMPC_acq_rel:
11790     C = new (Context) OMPAcqRelClause();
11791     break;
11792   case llvm::omp::OMPC_acquire:
11793     C = new (Context) OMPAcquireClause();
11794     break;
11795   case llvm::omp::OMPC_release:
11796     C = new (Context) OMPReleaseClause();
11797     break;
11798   case llvm::omp::OMPC_relaxed:
11799     C = new (Context) OMPRelaxedClause();
11800     break;
11801   case llvm::omp::OMPC_threads:
11802     C = new (Context) OMPThreadsClause();
11803     break;
11804   case llvm::omp::OMPC_simd:
11805     C = new (Context) OMPSIMDClause();
11806     break;
11807   case llvm::omp::OMPC_nogroup:
11808     C = new (Context) OMPNogroupClause();
11809     break;
11810   case llvm::omp::OMPC_unified_address:
11811     C = new (Context) OMPUnifiedAddressClause();
11812     break;
11813   case llvm::omp::OMPC_unified_shared_memory:
11814     C = new (Context) OMPUnifiedSharedMemoryClause();
11815     break;
11816   case llvm::omp::OMPC_reverse_offload:
11817     C = new (Context) OMPReverseOffloadClause();
11818     break;
11819   case llvm::omp::OMPC_dynamic_allocators:
11820     C = new (Context) OMPDynamicAllocatorsClause();
11821     break;
11822   case llvm::omp::OMPC_atomic_default_mem_order:
11823     C = new (Context) OMPAtomicDefaultMemOrderClause();
11824     break;
11825  case llvm::omp::OMPC_private:
11826     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11827     break;
11828   case llvm::omp::OMPC_firstprivate:
11829     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11830     break;
11831   case llvm::omp::OMPC_lastprivate:
11832     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11833     break;
11834   case llvm::omp::OMPC_shared:
11835     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11836     break;
11837   case llvm::omp::OMPC_reduction: {
11838     unsigned N = Record.readInt();
11839     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11840     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11841     break;
11842   }
11843   case llvm::omp::OMPC_task_reduction:
11844     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11845     break;
11846   case llvm::omp::OMPC_in_reduction:
11847     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11848     break;
11849   case llvm::omp::OMPC_linear:
11850     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11851     break;
11852   case llvm::omp::OMPC_aligned:
11853     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11854     break;
11855   case llvm::omp::OMPC_copyin:
11856     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11857     break;
11858   case llvm::omp::OMPC_copyprivate:
11859     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11860     break;
11861   case llvm::omp::OMPC_flush:
11862     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11863     break;
11864   case llvm::omp::OMPC_depobj:
11865     C = OMPDepobjClause::CreateEmpty(Context);
11866     break;
11867   case llvm::omp::OMPC_depend: {
11868     unsigned NumVars = Record.readInt();
11869     unsigned NumLoops = Record.readInt();
11870     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11871     break;
11872   }
11873   case llvm::omp::OMPC_device:
11874     C = new (Context) OMPDeviceClause();
11875     break;
11876   case llvm::omp::OMPC_map: {
11877     OMPMappableExprListSizeTy Sizes;
11878     Sizes.NumVars = Record.readInt();
11879     Sizes.NumUniqueDeclarations = Record.readInt();
11880     Sizes.NumComponentLists = Record.readInt();
11881     Sizes.NumComponents = Record.readInt();
11882     C = OMPMapClause::CreateEmpty(Context, Sizes);
11883     break;
11884   }
11885   case llvm::omp::OMPC_num_teams:
11886     C = new (Context) OMPNumTeamsClause();
11887     break;
11888   case llvm::omp::OMPC_thread_limit:
11889     C = new (Context) OMPThreadLimitClause();
11890     break;
11891   case llvm::omp::OMPC_priority:
11892     C = new (Context) OMPPriorityClause();
11893     break;
11894   case llvm::omp::OMPC_grainsize:
11895     C = new (Context) OMPGrainsizeClause();
11896     break;
11897   case llvm::omp::OMPC_num_tasks:
11898     C = new (Context) OMPNumTasksClause();
11899     break;
11900   case llvm::omp::OMPC_hint:
11901     C = new (Context) OMPHintClause();
11902     break;
11903   case llvm::omp::OMPC_dist_schedule:
11904     C = new (Context) OMPDistScheduleClause();
11905     break;
11906   case llvm::omp::OMPC_defaultmap:
11907     C = new (Context) OMPDefaultmapClause();
11908     break;
11909   case llvm::omp::OMPC_to: {
11910     OMPMappableExprListSizeTy Sizes;
11911     Sizes.NumVars = Record.readInt();
11912     Sizes.NumUniqueDeclarations = Record.readInt();
11913     Sizes.NumComponentLists = Record.readInt();
11914     Sizes.NumComponents = Record.readInt();
11915     C = OMPToClause::CreateEmpty(Context, Sizes);
11916     break;
11917   }
11918   case llvm::omp::OMPC_from: {
11919     OMPMappableExprListSizeTy Sizes;
11920     Sizes.NumVars = Record.readInt();
11921     Sizes.NumUniqueDeclarations = Record.readInt();
11922     Sizes.NumComponentLists = Record.readInt();
11923     Sizes.NumComponents = Record.readInt();
11924     C = OMPFromClause::CreateEmpty(Context, Sizes);
11925     break;
11926   }
11927   case llvm::omp::OMPC_use_device_ptr: {
11928     OMPMappableExprListSizeTy Sizes;
11929     Sizes.NumVars = Record.readInt();
11930     Sizes.NumUniqueDeclarations = Record.readInt();
11931     Sizes.NumComponentLists = Record.readInt();
11932     Sizes.NumComponents = Record.readInt();
11933     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11934     break;
11935   }
11936   case llvm::omp::OMPC_use_device_addr: {
11937     OMPMappableExprListSizeTy Sizes;
11938     Sizes.NumVars = Record.readInt();
11939     Sizes.NumUniqueDeclarations = Record.readInt();
11940     Sizes.NumComponentLists = Record.readInt();
11941     Sizes.NumComponents = Record.readInt();
11942     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11943     break;
11944   }
11945   case llvm::omp::OMPC_is_device_ptr: {
11946     OMPMappableExprListSizeTy Sizes;
11947     Sizes.NumVars = Record.readInt();
11948     Sizes.NumUniqueDeclarations = Record.readInt();
11949     Sizes.NumComponentLists = Record.readInt();
11950     Sizes.NumComponents = Record.readInt();
11951     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11952     break;
11953   }
11954   case llvm::omp::OMPC_allocate:
11955     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11956     break;
11957   case llvm::omp::OMPC_nontemporal:
11958     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11959     break;
11960   case llvm::omp::OMPC_inclusive:
11961     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11962     break;
11963   case llvm::omp::OMPC_exclusive:
11964     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11965     break;
11966   case llvm::omp::OMPC_order:
11967     C = new (Context) OMPOrderClause();
11968     break;
11969   case llvm::omp::OMPC_init:
11970     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11971     break;
11972   case llvm::omp::OMPC_use:
11973     C = new (Context) OMPUseClause();
11974     break;
11975   case llvm::omp::OMPC_destroy:
11976     C = new (Context) OMPDestroyClause();
11977     break;
11978   case llvm::omp::OMPC_novariants:
11979     C = new (Context) OMPNovariantsClause();
11980     break;
11981   case llvm::omp::OMPC_nocontext:
11982     C = new (Context) OMPNocontextClause();
11983     break;
11984   case llvm::omp::OMPC_detach:
11985     C = new (Context) OMPDetachClause();
11986     break;
11987   case llvm::omp::OMPC_uses_allocators:
11988     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11989     break;
11990   case llvm::omp::OMPC_affinity:
11991     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11992     break;
11993   case llvm::omp::OMPC_filter:
11994     C = new (Context) OMPFilterClause();
11995     break;
11996 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11997   case llvm::omp::Enum:                                                        \
11998     break;
11999 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12000   default:
12001     break;
12002   }
12003   assert(C && "Unknown OMPClause type");
12004 
12005   Visit(C);
12006   C->setLocStart(Record.readSourceLocation());
12007   C->setLocEnd(Record.readSourceLocation());
12008 
12009   return C;
12010 }
12011 
12012 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12013   C->setPreInitStmt(Record.readSubStmt(),
12014                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12015 }
12016 
12017 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12018   VisitOMPClauseWithPreInit(C);
12019   C->setPostUpdateExpr(Record.readSubExpr());
12020 }
12021 
12022 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12023   VisitOMPClauseWithPreInit(C);
12024   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12025   C->setNameModifierLoc(Record.readSourceLocation());
12026   C->setColonLoc(Record.readSourceLocation());
12027   C->setCondition(Record.readSubExpr());
12028   C->setLParenLoc(Record.readSourceLocation());
12029 }
12030 
12031 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12032   VisitOMPClauseWithPreInit(C);
12033   C->setCondition(Record.readSubExpr());
12034   C->setLParenLoc(Record.readSourceLocation());
12035 }
12036 
12037 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12038   VisitOMPClauseWithPreInit(C);
12039   C->setNumThreads(Record.readSubExpr());
12040   C->setLParenLoc(Record.readSourceLocation());
12041 }
12042 
12043 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12044   C->setSafelen(Record.readSubExpr());
12045   C->setLParenLoc(Record.readSourceLocation());
12046 }
12047 
12048 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12049   C->setSimdlen(Record.readSubExpr());
12050   C->setLParenLoc(Record.readSourceLocation());
12051 }
12052 
12053 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12054   for (Expr *&E : C->getSizesRefs())
12055     E = Record.readSubExpr();
12056   C->setLParenLoc(Record.readSourceLocation());
12057 }
12058 
12059 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12060 
12061 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12062   C->setFactor(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::VisitOMPInitClause(OMPInitClause *C) {
12155   unsigned NumVars = C->varlist_size();
12156   SmallVector<Expr *, 16> Vars;
12157   Vars.reserve(NumVars);
12158   for (unsigned I = 0; I != NumVars; ++I)
12159     Vars.push_back(Record.readSubExpr());
12160   C->setVarRefs(Vars);
12161   C->setIsTarget(Record.readBool());
12162   C->setIsTargetSync(Record.readBool());
12163   C->setLParenLoc(Record.readSourceLocation());
12164   C->setVarLoc(Record.readSourceLocation());
12165 }
12166 
12167 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12168   C->setInteropVar(Record.readSubExpr());
12169   C->setLParenLoc(Record.readSourceLocation());
12170   C->setVarLoc(Record.readSourceLocation());
12171 }
12172 
12173 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12174   C->setInteropVar(Record.readSubExpr());
12175   C->setLParenLoc(Record.readSourceLocation());
12176   C->setVarLoc(Record.readSourceLocation());
12177 }
12178 
12179 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12180   VisitOMPClauseWithPreInit(C);
12181   C->setCondition(Record.readSubExpr());
12182   C->setLParenLoc(Record.readSourceLocation());
12183 }
12184 
12185 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12186   VisitOMPClauseWithPreInit(C);
12187   C->setCondition(Record.readSubExpr());
12188   C->setLParenLoc(Record.readSourceLocation());
12189 }
12190 
12191 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12192 
12193 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12194     OMPUnifiedSharedMemoryClause *) {}
12195 
12196 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12197 
12198 void
12199 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12200 }
12201 
12202 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12203     OMPAtomicDefaultMemOrderClause *C) {
12204   C->setAtomicDefaultMemOrderKind(
12205       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12206   C->setLParenLoc(Record.readSourceLocation());
12207   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12208 }
12209 
12210 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12211   C->setLParenLoc(Record.readSourceLocation());
12212   unsigned NumVars = C->varlist_size();
12213   SmallVector<Expr *, 16> Vars;
12214   Vars.reserve(NumVars);
12215   for (unsigned i = 0; i != NumVars; ++i)
12216     Vars.push_back(Record.readSubExpr());
12217   C->setVarRefs(Vars);
12218   Vars.clear();
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Vars.push_back(Record.readSubExpr());
12221   C->setPrivateCopies(Vars);
12222 }
12223 
12224 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12225   VisitOMPClauseWithPreInit(C);
12226   C->setLParenLoc(Record.readSourceLocation());
12227   unsigned NumVars = C->varlist_size();
12228   SmallVector<Expr *, 16> Vars;
12229   Vars.reserve(NumVars);
12230   for (unsigned i = 0; i != NumVars; ++i)
12231     Vars.push_back(Record.readSubExpr());
12232   C->setVarRefs(Vars);
12233   Vars.clear();
12234   for (unsigned i = 0; i != NumVars; ++i)
12235     Vars.push_back(Record.readSubExpr());
12236   C->setPrivateCopies(Vars);
12237   Vars.clear();
12238   for (unsigned i = 0; i != NumVars; ++i)
12239     Vars.push_back(Record.readSubExpr());
12240   C->setInits(Vars);
12241 }
12242 
12243 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12244   VisitOMPClauseWithPostUpdate(C);
12245   C->setLParenLoc(Record.readSourceLocation());
12246   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12247   C->setKindLoc(Record.readSourceLocation());
12248   C->setColonLoc(Record.readSourceLocation());
12249   unsigned NumVars = C->varlist_size();
12250   SmallVector<Expr *, 16> Vars;
12251   Vars.reserve(NumVars);
12252   for (unsigned i = 0; i != NumVars; ++i)
12253     Vars.push_back(Record.readSubExpr());
12254   C->setVarRefs(Vars);
12255   Vars.clear();
12256   for (unsigned i = 0; i != NumVars; ++i)
12257     Vars.push_back(Record.readSubExpr());
12258   C->setPrivateCopies(Vars);
12259   Vars.clear();
12260   for (unsigned i = 0; i != NumVars; ++i)
12261     Vars.push_back(Record.readSubExpr());
12262   C->setSourceExprs(Vars);
12263   Vars.clear();
12264   for (unsigned i = 0; i != NumVars; ++i)
12265     Vars.push_back(Record.readSubExpr());
12266   C->setDestinationExprs(Vars);
12267   Vars.clear();
12268   for (unsigned i = 0; i != NumVars; ++i)
12269     Vars.push_back(Record.readSubExpr());
12270   C->setAssignmentOps(Vars);
12271 }
12272 
12273 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12274   C->setLParenLoc(Record.readSourceLocation());
12275   unsigned NumVars = C->varlist_size();
12276   SmallVector<Expr *, 16> Vars;
12277   Vars.reserve(NumVars);
12278   for (unsigned i = 0; i != NumVars; ++i)
12279     Vars.push_back(Record.readSubExpr());
12280   C->setVarRefs(Vars);
12281 }
12282 
12283 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12284   VisitOMPClauseWithPostUpdate(C);
12285   C->setLParenLoc(Record.readSourceLocation());
12286   C->setModifierLoc(Record.readSourceLocation());
12287   C->setColonLoc(Record.readSourceLocation());
12288   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12289   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12290   C->setQualifierLoc(NNSL);
12291   C->setNameInfo(DNI);
12292 
12293   unsigned NumVars = C->varlist_size();
12294   SmallVector<Expr *, 16> Vars;
12295   Vars.reserve(NumVars);
12296   for (unsigned i = 0; i != NumVars; ++i)
12297     Vars.push_back(Record.readSubExpr());
12298   C->setVarRefs(Vars);
12299   Vars.clear();
12300   for (unsigned i = 0; i != NumVars; ++i)
12301     Vars.push_back(Record.readSubExpr());
12302   C->setPrivates(Vars);
12303   Vars.clear();
12304   for (unsigned i = 0; i != NumVars; ++i)
12305     Vars.push_back(Record.readSubExpr());
12306   C->setLHSExprs(Vars);
12307   Vars.clear();
12308   for (unsigned i = 0; i != NumVars; ++i)
12309     Vars.push_back(Record.readSubExpr());
12310   C->setRHSExprs(Vars);
12311   Vars.clear();
12312   for (unsigned i = 0; i != NumVars; ++i)
12313     Vars.push_back(Record.readSubExpr());
12314   C->setReductionOps(Vars);
12315   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12316     Vars.clear();
12317     for (unsigned i = 0; i != NumVars; ++i)
12318       Vars.push_back(Record.readSubExpr());
12319     C->setInscanCopyOps(Vars);
12320     Vars.clear();
12321     for (unsigned i = 0; i != NumVars; ++i)
12322       Vars.push_back(Record.readSubExpr());
12323     C->setInscanCopyArrayTemps(Vars);
12324     Vars.clear();
12325     for (unsigned i = 0; i != NumVars; ++i)
12326       Vars.push_back(Record.readSubExpr());
12327     C->setInscanCopyArrayElems(Vars);
12328   }
12329 }
12330 
12331 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12332   VisitOMPClauseWithPostUpdate(C);
12333   C->setLParenLoc(Record.readSourceLocation());
12334   C->setColonLoc(Record.readSourceLocation());
12335   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12336   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12337   C->setQualifierLoc(NNSL);
12338   C->setNameInfo(DNI);
12339 
12340   unsigned NumVars = C->varlist_size();
12341   SmallVector<Expr *, 16> Vars;
12342   Vars.reserve(NumVars);
12343   for (unsigned I = 0; I != NumVars; ++I)
12344     Vars.push_back(Record.readSubExpr());
12345   C->setVarRefs(Vars);
12346   Vars.clear();
12347   for (unsigned I = 0; I != NumVars; ++I)
12348     Vars.push_back(Record.readSubExpr());
12349   C->setPrivates(Vars);
12350   Vars.clear();
12351   for (unsigned I = 0; I != NumVars; ++I)
12352     Vars.push_back(Record.readSubExpr());
12353   C->setLHSExprs(Vars);
12354   Vars.clear();
12355   for (unsigned I = 0; I != NumVars; ++I)
12356     Vars.push_back(Record.readSubExpr());
12357   C->setRHSExprs(Vars);
12358   Vars.clear();
12359   for (unsigned I = 0; I != NumVars; ++I)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setReductionOps(Vars);
12362 }
12363 
12364 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12365   VisitOMPClauseWithPostUpdate(C);
12366   C->setLParenLoc(Record.readSourceLocation());
12367   C->setColonLoc(Record.readSourceLocation());
12368   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12369   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12370   C->setQualifierLoc(NNSL);
12371   C->setNameInfo(DNI);
12372 
12373   unsigned NumVars = C->varlist_size();
12374   SmallVector<Expr *, 16> Vars;
12375   Vars.reserve(NumVars);
12376   for (unsigned I = 0; I != NumVars; ++I)
12377     Vars.push_back(Record.readSubExpr());
12378   C->setVarRefs(Vars);
12379   Vars.clear();
12380   for (unsigned I = 0; I != NumVars; ++I)
12381     Vars.push_back(Record.readSubExpr());
12382   C->setPrivates(Vars);
12383   Vars.clear();
12384   for (unsigned I = 0; I != NumVars; ++I)
12385     Vars.push_back(Record.readSubExpr());
12386   C->setLHSExprs(Vars);
12387   Vars.clear();
12388   for (unsigned I = 0; I != NumVars; ++I)
12389     Vars.push_back(Record.readSubExpr());
12390   C->setRHSExprs(Vars);
12391   Vars.clear();
12392   for (unsigned I = 0; I != NumVars; ++I)
12393     Vars.push_back(Record.readSubExpr());
12394   C->setReductionOps(Vars);
12395   Vars.clear();
12396   for (unsigned I = 0; I != NumVars; ++I)
12397     Vars.push_back(Record.readSubExpr());
12398   C->setTaskgroupDescriptors(Vars);
12399 }
12400 
12401 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12402   VisitOMPClauseWithPostUpdate(C);
12403   C->setLParenLoc(Record.readSourceLocation());
12404   C->setColonLoc(Record.readSourceLocation());
12405   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12406   C->setModifierLoc(Record.readSourceLocation());
12407   unsigned NumVars = C->varlist_size();
12408   SmallVector<Expr *, 16> Vars;
12409   Vars.reserve(NumVars);
12410   for (unsigned i = 0; i != NumVars; ++i)
12411     Vars.push_back(Record.readSubExpr());
12412   C->setVarRefs(Vars);
12413   Vars.clear();
12414   for (unsigned i = 0; i != NumVars; ++i)
12415     Vars.push_back(Record.readSubExpr());
12416   C->setPrivates(Vars);
12417   Vars.clear();
12418   for (unsigned i = 0; i != NumVars; ++i)
12419     Vars.push_back(Record.readSubExpr());
12420   C->setInits(Vars);
12421   Vars.clear();
12422   for (unsigned i = 0; i != NumVars; ++i)
12423     Vars.push_back(Record.readSubExpr());
12424   C->setUpdates(Vars);
12425   Vars.clear();
12426   for (unsigned i = 0; i != NumVars; ++i)
12427     Vars.push_back(Record.readSubExpr());
12428   C->setFinals(Vars);
12429   C->setStep(Record.readSubExpr());
12430   C->setCalcStep(Record.readSubExpr());
12431   Vars.clear();
12432   for (unsigned I = 0; I != NumVars + 1; ++I)
12433     Vars.push_back(Record.readSubExpr());
12434   C->setUsedExprs(Vars);
12435 }
12436 
12437 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12438   C->setLParenLoc(Record.readSourceLocation());
12439   C->setColonLoc(Record.readSourceLocation());
12440   unsigned NumVars = C->varlist_size();
12441   SmallVector<Expr *, 16> Vars;
12442   Vars.reserve(NumVars);
12443   for (unsigned i = 0; i != NumVars; ++i)
12444     Vars.push_back(Record.readSubExpr());
12445   C->setVarRefs(Vars);
12446   C->setAlignment(Record.readSubExpr());
12447 }
12448 
12449 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12450   C->setLParenLoc(Record.readSourceLocation());
12451   unsigned NumVars = C->varlist_size();
12452   SmallVector<Expr *, 16> Exprs;
12453   Exprs.reserve(NumVars);
12454   for (unsigned i = 0; i != NumVars; ++i)
12455     Exprs.push_back(Record.readSubExpr());
12456   C->setVarRefs(Exprs);
12457   Exprs.clear();
12458   for (unsigned i = 0; i != NumVars; ++i)
12459     Exprs.push_back(Record.readSubExpr());
12460   C->setSourceExprs(Exprs);
12461   Exprs.clear();
12462   for (unsigned i = 0; i != NumVars; ++i)
12463     Exprs.push_back(Record.readSubExpr());
12464   C->setDestinationExprs(Exprs);
12465   Exprs.clear();
12466   for (unsigned i = 0; i != NumVars; ++i)
12467     Exprs.push_back(Record.readSubExpr());
12468   C->setAssignmentOps(Exprs);
12469 }
12470 
12471 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12472   C->setLParenLoc(Record.readSourceLocation());
12473   unsigned NumVars = C->varlist_size();
12474   SmallVector<Expr *, 16> Exprs;
12475   Exprs.reserve(NumVars);
12476   for (unsigned i = 0; i != NumVars; ++i)
12477     Exprs.push_back(Record.readSubExpr());
12478   C->setVarRefs(Exprs);
12479   Exprs.clear();
12480   for (unsigned i = 0; i != NumVars; ++i)
12481     Exprs.push_back(Record.readSubExpr());
12482   C->setSourceExprs(Exprs);
12483   Exprs.clear();
12484   for (unsigned i = 0; i != NumVars; ++i)
12485     Exprs.push_back(Record.readSubExpr());
12486   C->setDestinationExprs(Exprs);
12487   Exprs.clear();
12488   for (unsigned i = 0; i != NumVars; ++i)
12489     Exprs.push_back(Record.readSubExpr());
12490   C->setAssignmentOps(Exprs);
12491 }
12492 
12493 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12494   C->setLParenLoc(Record.readSourceLocation());
12495   unsigned NumVars = C->varlist_size();
12496   SmallVector<Expr *, 16> Vars;
12497   Vars.reserve(NumVars);
12498   for (unsigned i = 0; i != NumVars; ++i)
12499     Vars.push_back(Record.readSubExpr());
12500   C->setVarRefs(Vars);
12501 }
12502 
12503 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12504   C->setDepobj(Record.readSubExpr());
12505   C->setLParenLoc(Record.readSourceLocation());
12506 }
12507 
12508 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12509   C->setLParenLoc(Record.readSourceLocation());
12510   C->setModifier(Record.readSubExpr());
12511   C->setDependencyKind(
12512       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12513   C->setDependencyLoc(Record.readSourceLocation());
12514   C->setColonLoc(Record.readSourceLocation());
12515   unsigned NumVars = C->varlist_size();
12516   SmallVector<Expr *, 16> Vars;
12517   Vars.reserve(NumVars);
12518   for (unsigned I = 0; I != NumVars; ++I)
12519     Vars.push_back(Record.readSubExpr());
12520   C->setVarRefs(Vars);
12521   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12522     C->setLoopData(I, Record.readSubExpr());
12523 }
12524 
12525 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12526   VisitOMPClauseWithPreInit(C);
12527   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12528   C->setDevice(Record.readSubExpr());
12529   C->setModifierLoc(Record.readSourceLocation());
12530   C->setLParenLoc(Record.readSourceLocation());
12531 }
12532 
12533 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12534   C->setLParenLoc(Record.readSourceLocation());
12535   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12536     C->setMapTypeModifier(
12537         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12538     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12539   }
12540   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12541   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12542   C->setMapType(
12543      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12544   C->setMapLoc(Record.readSourceLocation());
12545   C->setColonLoc(Record.readSourceLocation());
12546   auto NumVars = C->varlist_size();
12547   auto UniqueDecls = C->getUniqueDeclarationsNum();
12548   auto TotalLists = C->getTotalComponentListNum();
12549   auto TotalComponents = C->getTotalComponentsNum();
12550 
12551   SmallVector<Expr *, 16> Vars;
12552   Vars.reserve(NumVars);
12553   for (unsigned i = 0; i != NumVars; ++i)
12554     Vars.push_back(Record.readExpr());
12555   C->setVarRefs(Vars);
12556 
12557   SmallVector<Expr *, 16> UDMappers;
12558   UDMappers.reserve(NumVars);
12559   for (unsigned I = 0; I < NumVars; ++I)
12560     UDMappers.push_back(Record.readExpr());
12561   C->setUDMapperRefs(UDMappers);
12562 
12563   SmallVector<ValueDecl *, 16> Decls;
12564   Decls.reserve(UniqueDecls);
12565   for (unsigned i = 0; i < UniqueDecls; ++i)
12566     Decls.push_back(Record.readDeclAs<ValueDecl>());
12567   C->setUniqueDecls(Decls);
12568 
12569   SmallVector<unsigned, 16> ListsPerDecl;
12570   ListsPerDecl.reserve(UniqueDecls);
12571   for (unsigned i = 0; i < UniqueDecls; ++i)
12572     ListsPerDecl.push_back(Record.readInt());
12573   C->setDeclNumLists(ListsPerDecl);
12574 
12575   SmallVector<unsigned, 32> ListSizes;
12576   ListSizes.reserve(TotalLists);
12577   for (unsigned i = 0; i < TotalLists; ++i)
12578     ListSizes.push_back(Record.readInt());
12579   C->setComponentListSizes(ListSizes);
12580 
12581   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12582   Components.reserve(TotalComponents);
12583   for (unsigned i = 0; i < TotalComponents; ++i) {
12584     Expr *AssociatedExprPr = Record.readExpr();
12585     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12586     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12587                             /*IsNonContiguous=*/false);
12588   }
12589   C->setComponents(Components, ListSizes);
12590 }
12591 
12592 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12593   C->setLParenLoc(Record.readSourceLocation());
12594   C->setColonLoc(Record.readSourceLocation());
12595   C->setAllocator(Record.readSubExpr());
12596   unsigned NumVars = C->varlist_size();
12597   SmallVector<Expr *, 16> Vars;
12598   Vars.reserve(NumVars);
12599   for (unsigned i = 0; i != NumVars; ++i)
12600     Vars.push_back(Record.readSubExpr());
12601   C->setVarRefs(Vars);
12602 }
12603 
12604 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12605   VisitOMPClauseWithPreInit(C);
12606   C->setNumTeams(Record.readSubExpr());
12607   C->setLParenLoc(Record.readSourceLocation());
12608 }
12609 
12610 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12611   VisitOMPClauseWithPreInit(C);
12612   C->setThreadLimit(Record.readSubExpr());
12613   C->setLParenLoc(Record.readSourceLocation());
12614 }
12615 
12616 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12617   VisitOMPClauseWithPreInit(C);
12618   C->setPriority(Record.readSubExpr());
12619   C->setLParenLoc(Record.readSourceLocation());
12620 }
12621 
12622 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12623   VisitOMPClauseWithPreInit(C);
12624   C->setGrainsize(Record.readSubExpr());
12625   C->setLParenLoc(Record.readSourceLocation());
12626 }
12627 
12628 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12629   VisitOMPClauseWithPreInit(C);
12630   C->setNumTasks(Record.readSubExpr());
12631   C->setLParenLoc(Record.readSourceLocation());
12632 }
12633 
12634 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12635   C->setHint(Record.readSubExpr());
12636   C->setLParenLoc(Record.readSourceLocation());
12637 }
12638 
12639 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12640   VisitOMPClauseWithPreInit(C);
12641   C->setDistScheduleKind(
12642       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12643   C->setChunkSize(Record.readSubExpr());
12644   C->setLParenLoc(Record.readSourceLocation());
12645   C->setDistScheduleKindLoc(Record.readSourceLocation());
12646   C->setCommaLoc(Record.readSourceLocation());
12647 }
12648 
12649 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12650   C->setDefaultmapKind(
12651        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12652   C->setDefaultmapModifier(
12653       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12654   C->setLParenLoc(Record.readSourceLocation());
12655   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12656   C->setDefaultmapKindLoc(Record.readSourceLocation());
12657 }
12658 
12659 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12660   C->setLParenLoc(Record.readSourceLocation());
12661   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12662     C->setMotionModifier(
12663         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12664     C->setMotionModifierLoc(I, Record.readSourceLocation());
12665   }
12666   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12667   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12668   C->setColonLoc(Record.readSourceLocation());
12669   auto NumVars = C->varlist_size();
12670   auto UniqueDecls = C->getUniqueDeclarationsNum();
12671   auto TotalLists = C->getTotalComponentListNum();
12672   auto TotalComponents = C->getTotalComponentsNum();
12673 
12674   SmallVector<Expr *, 16> Vars;
12675   Vars.reserve(NumVars);
12676   for (unsigned i = 0; i != NumVars; ++i)
12677     Vars.push_back(Record.readSubExpr());
12678   C->setVarRefs(Vars);
12679 
12680   SmallVector<Expr *, 16> UDMappers;
12681   UDMappers.reserve(NumVars);
12682   for (unsigned I = 0; I < NumVars; ++I)
12683     UDMappers.push_back(Record.readSubExpr());
12684   C->setUDMapperRefs(UDMappers);
12685 
12686   SmallVector<ValueDecl *, 16> Decls;
12687   Decls.reserve(UniqueDecls);
12688   for (unsigned i = 0; i < UniqueDecls; ++i)
12689     Decls.push_back(Record.readDeclAs<ValueDecl>());
12690   C->setUniqueDecls(Decls);
12691 
12692   SmallVector<unsigned, 16> ListsPerDecl;
12693   ListsPerDecl.reserve(UniqueDecls);
12694   for (unsigned i = 0; i < UniqueDecls; ++i)
12695     ListsPerDecl.push_back(Record.readInt());
12696   C->setDeclNumLists(ListsPerDecl);
12697 
12698   SmallVector<unsigned, 32> ListSizes;
12699   ListSizes.reserve(TotalLists);
12700   for (unsigned i = 0; i < TotalLists; ++i)
12701     ListSizes.push_back(Record.readInt());
12702   C->setComponentListSizes(ListSizes);
12703 
12704   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12705   Components.reserve(TotalComponents);
12706   for (unsigned i = 0; i < TotalComponents; ++i) {
12707     Expr *AssociatedExprPr = Record.readSubExpr();
12708     bool IsNonContiguous = Record.readBool();
12709     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12710     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12711   }
12712   C->setComponents(Components, ListSizes);
12713 }
12714 
12715 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12716   C->setLParenLoc(Record.readSourceLocation());
12717   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12718     C->setMotionModifier(
12719         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12720     C->setMotionModifierLoc(I, Record.readSourceLocation());
12721   }
12722   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12723   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12724   C->setColonLoc(Record.readSourceLocation());
12725   auto NumVars = C->varlist_size();
12726   auto UniqueDecls = C->getUniqueDeclarationsNum();
12727   auto TotalLists = C->getTotalComponentListNum();
12728   auto TotalComponents = C->getTotalComponentsNum();
12729 
12730   SmallVector<Expr *, 16> Vars;
12731   Vars.reserve(NumVars);
12732   for (unsigned i = 0; i != NumVars; ++i)
12733     Vars.push_back(Record.readSubExpr());
12734   C->setVarRefs(Vars);
12735 
12736   SmallVector<Expr *, 16> UDMappers;
12737   UDMappers.reserve(NumVars);
12738   for (unsigned I = 0; I < NumVars; ++I)
12739     UDMappers.push_back(Record.readSubExpr());
12740   C->setUDMapperRefs(UDMappers);
12741 
12742   SmallVector<ValueDecl *, 16> Decls;
12743   Decls.reserve(UniqueDecls);
12744   for (unsigned i = 0; i < UniqueDecls; ++i)
12745     Decls.push_back(Record.readDeclAs<ValueDecl>());
12746   C->setUniqueDecls(Decls);
12747 
12748   SmallVector<unsigned, 16> ListsPerDecl;
12749   ListsPerDecl.reserve(UniqueDecls);
12750   for (unsigned i = 0; i < UniqueDecls; ++i)
12751     ListsPerDecl.push_back(Record.readInt());
12752   C->setDeclNumLists(ListsPerDecl);
12753 
12754   SmallVector<unsigned, 32> ListSizes;
12755   ListSizes.reserve(TotalLists);
12756   for (unsigned i = 0; i < TotalLists; ++i)
12757     ListSizes.push_back(Record.readInt());
12758   C->setComponentListSizes(ListSizes);
12759 
12760   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12761   Components.reserve(TotalComponents);
12762   for (unsigned i = 0; i < TotalComponents; ++i) {
12763     Expr *AssociatedExprPr = Record.readSubExpr();
12764     bool IsNonContiguous = Record.readBool();
12765     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12766     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12767   }
12768   C->setComponents(Components, ListSizes);
12769 }
12770 
12771 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12772   C->setLParenLoc(Record.readSourceLocation());
12773   auto NumVars = C->varlist_size();
12774   auto UniqueDecls = C->getUniqueDeclarationsNum();
12775   auto TotalLists = C->getTotalComponentListNum();
12776   auto TotalComponents = C->getTotalComponentsNum();
12777 
12778   SmallVector<Expr *, 16> Vars;
12779   Vars.reserve(NumVars);
12780   for (unsigned i = 0; i != NumVars; ++i)
12781     Vars.push_back(Record.readSubExpr());
12782   C->setVarRefs(Vars);
12783   Vars.clear();
12784   for (unsigned i = 0; i != NumVars; ++i)
12785     Vars.push_back(Record.readSubExpr());
12786   C->setPrivateCopies(Vars);
12787   Vars.clear();
12788   for (unsigned i = 0; i != NumVars; ++i)
12789     Vars.push_back(Record.readSubExpr());
12790   C->setInits(Vars);
12791 
12792   SmallVector<ValueDecl *, 16> Decls;
12793   Decls.reserve(UniqueDecls);
12794   for (unsigned i = 0; i < UniqueDecls; ++i)
12795     Decls.push_back(Record.readDeclAs<ValueDecl>());
12796   C->setUniqueDecls(Decls);
12797 
12798   SmallVector<unsigned, 16> ListsPerDecl;
12799   ListsPerDecl.reserve(UniqueDecls);
12800   for (unsigned i = 0; i < UniqueDecls; ++i)
12801     ListsPerDecl.push_back(Record.readInt());
12802   C->setDeclNumLists(ListsPerDecl);
12803 
12804   SmallVector<unsigned, 32> ListSizes;
12805   ListSizes.reserve(TotalLists);
12806   for (unsigned i = 0; i < TotalLists; ++i)
12807     ListSizes.push_back(Record.readInt());
12808   C->setComponentListSizes(ListSizes);
12809 
12810   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12811   Components.reserve(TotalComponents);
12812   for (unsigned i = 0; i < TotalComponents; ++i) {
12813     auto *AssociatedExprPr = Record.readSubExpr();
12814     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12815     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12816                             /*IsNonContiguous=*/false);
12817   }
12818   C->setComponents(Components, ListSizes);
12819 }
12820 
12821 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12822   C->setLParenLoc(Record.readSourceLocation());
12823   auto NumVars = C->varlist_size();
12824   auto UniqueDecls = C->getUniqueDeclarationsNum();
12825   auto TotalLists = C->getTotalComponentListNum();
12826   auto TotalComponents = C->getTotalComponentsNum();
12827 
12828   SmallVector<Expr *, 16> Vars;
12829   Vars.reserve(NumVars);
12830   for (unsigned i = 0; i != NumVars; ++i)
12831     Vars.push_back(Record.readSubExpr());
12832   C->setVarRefs(Vars);
12833 
12834   SmallVector<ValueDecl *, 16> Decls;
12835   Decls.reserve(UniqueDecls);
12836   for (unsigned i = 0; i < UniqueDecls; ++i)
12837     Decls.push_back(Record.readDeclAs<ValueDecl>());
12838   C->setUniqueDecls(Decls);
12839 
12840   SmallVector<unsigned, 16> ListsPerDecl;
12841   ListsPerDecl.reserve(UniqueDecls);
12842   for (unsigned i = 0; i < UniqueDecls; ++i)
12843     ListsPerDecl.push_back(Record.readInt());
12844   C->setDeclNumLists(ListsPerDecl);
12845 
12846   SmallVector<unsigned, 32> ListSizes;
12847   ListSizes.reserve(TotalLists);
12848   for (unsigned i = 0; i < TotalLists; ++i)
12849     ListSizes.push_back(Record.readInt());
12850   C->setComponentListSizes(ListSizes);
12851 
12852   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12853   Components.reserve(TotalComponents);
12854   for (unsigned i = 0; i < TotalComponents; ++i) {
12855     Expr *AssociatedExpr = Record.readSubExpr();
12856     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12857     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12858                             /*IsNonContiguous*/ false);
12859   }
12860   C->setComponents(Components, ListSizes);
12861 }
12862 
12863 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12864   C->setLParenLoc(Record.readSourceLocation());
12865   auto NumVars = C->varlist_size();
12866   auto UniqueDecls = C->getUniqueDeclarationsNum();
12867   auto TotalLists = C->getTotalComponentListNum();
12868   auto TotalComponents = C->getTotalComponentsNum();
12869 
12870   SmallVector<Expr *, 16> Vars;
12871   Vars.reserve(NumVars);
12872   for (unsigned i = 0; i != NumVars; ++i)
12873     Vars.push_back(Record.readSubExpr());
12874   C->setVarRefs(Vars);
12875   Vars.clear();
12876 
12877   SmallVector<ValueDecl *, 16> Decls;
12878   Decls.reserve(UniqueDecls);
12879   for (unsigned i = 0; i < UniqueDecls; ++i)
12880     Decls.push_back(Record.readDeclAs<ValueDecl>());
12881   C->setUniqueDecls(Decls);
12882 
12883   SmallVector<unsigned, 16> ListsPerDecl;
12884   ListsPerDecl.reserve(UniqueDecls);
12885   for (unsigned i = 0; i < UniqueDecls; ++i)
12886     ListsPerDecl.push_back(Record.readInt());
12887   C->setDeclNumLists(ListsPerDecl);
12888 
12889   SmallVector<unsigned, 32> ListSizes;
12890   ListSizes.reserve(TotalLists);
12891   for (unsigned i = 0; i < TotalLists; ++i)
12892     ListSizes.push_back(Record.readInt());
12893   C->setComponentListSizes(ListSizes);
12894 
12895   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12896   Components.reserve(TotalComponents);
12897   for (unsigned i = 0; i < TotalComponents; ++i) {
12898     Expr *AssociatedExpr = Record.readSubExpr();
12899     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12900     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12901                             /*IsNonContiguous=*/false);
12902   }
12903   C->setComponents(Components, ListSizes);
12904 }
12905 
12906 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12907   C->setLParenLoc(Record.readSourceLocation());
12908   unsigned NumVars = C->varlist_size();
12909   SmallVector<Expr *, 16> Vars;
12910   Vars.reserve(NumVars);
12911   for (unsigned i = 0; i != NumVars; ++i)
12912     Vars.push_back(Record.readSubExpr());
12913   C->setVarRefs(Vars);
12914   Vars.clear();
12915   Vars.reserve(NumVars);
12916   for (unsigned i = 0; i != NumVars; ++i)
12917     Vars.push_back(Record.readSubExpr());
12918   C->setPrivateRefs(Vars);
12919 }
12920 
12921 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12922   C->setLParenLoc(Record.readSourceLocation());
12923   unsigned NumVars = C->varlist_size();
12924   SmallVector<Expr *, 16> Vars;
12925   Vars.reserve(NumVars);
12926   for (unsigned i = 0; i != NumVars; ++i)
12927     Vars.push_back(Record.readSubExpr());
12928   C->setVarRefs(Vars);
12929 }
12930 
12931 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12932   C->setLParenLoc(Record.readSourceLocation());
12933   unsigned NumVars = C->varlist_size();
12934   SmallVector<Expr *, 16> Vars;
12935   Vars.reserve(NumVars);
12936   for (unsigned i = 0; i != NumVars; ++i)
12937     Vars.push_back(Record.readSubExpr());
12938   C->setVarRefs(Vars);
12939 }
12940 
12941 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12942   C->setLParenLoc(Record.readSourceLocation());
12943   unsigned NumOfAllocators = C->getNumberOfAllocators();
12944   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12945   Data.reserve(NumOfAllocators);
12946   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12947     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12948     D.Allocator = Record.readSubExpr();
12949     D.AllocatorTraits = Record.readSubExpr();
12950     D.LParenLoc = Record.readSourceLocation();
12951     D.RParenLoc = Record.readSourceLocation();
12952   }
12953   C->setAllocatorsData(Data);
12954 }
12955 
12956 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12957   C->setLParenLoc(Record.readSourceLocation());
12958   C->setModifier(Record.readSubExpr());
12959   C->setColonLoc(Record.readSourceLocation());
12960   unsigned NumOfLocators = C->varlist_size();
12961   SmallVector<Expr *, 4> Locators;
12962   Locators.reserve(NumOfLocators);
12963   for (unsigned I = 0; I != NumOfLocators; ++I)
12964     Locators.push_back(Record.readSubExpr());
12965   C->setVarRefs(Locators);
12966 }
12967 
12968 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12969   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12970   C->setLParenLoc(Record.readSourceLocation());
12971   C->setKindKwLoc(Record.readSourceLocation());
12972 }
12973 
12974 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12975   VisitOMPClauseWithPreInit(C);
12976   C->setThreadID(Record.readSubExpr());
12977   C->setLParenLoc(Record.readSourceLocation());
12978 }
12979 
12980 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12981   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12982   TI.Sets.resize(readUInt32());
12983   for (auto &Set : TI.Sets) {
12984     Set.Kind = readEnum<llvm::omp::TraitSet>();
12985     Set.Selectors.resize(readUInt32());
12986     for (auto &Selector : Set.Selectors) {
12987       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12988       Selector.ScoreOrCondition = nullptr;
12989       if (readBool())
12990         Selector.ScoreOrCondition = readExprRef();
12991       Selector.Properties.resize(readUInt32());
12992       for (auto &Property : Selector.Properties)
12993         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12994     }
12995   }
12996   return &TI;
12997 }
12998 
12999 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13000   if (!Data)
13001     return;
13002   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13003     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13004     skipInts(3);
13005   }
13006   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13007   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13008     Clauses[I] = readOMPClause();
13009   Data->setClauses(Clauses);
13010   if (Data->hasAssociatedStmt())
13011     Data->setAssociatedStmt(readStmt());
13012   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13013     Data->getChildren()[I] = readStmt();
13014 }
13015