1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/MemoryBuffer.h"
118 #include "llvm/Support/Path.h"
119 #include "llvm/Support/SaveAndRestore.h"
120 #include "llvm/Support/Timer.h"
121 #include "llvm/Support/VersionTuple.h"
122 #include "llvm/Support/raw_ostream.h"
123 #include <algorithm>
124 #include <cassert>
125 #include <cstddef>
126 #include <cstdint>
127 #include <cstdio>
128 #include <ctime>
129 #include <iterator>
130 #include <limits>
131 #include <map>
132 #include <memory>
133 #include <string>
134 #include <system_error>
135 #include <tuple>
136 #include <utility>
137 #include <vector>
138 
139 using namespace clang;
140 using namespace clang::serialization;
141 using namespace clang::serialization::reader;
142 using llvm::BitstreamCursor;
143 using llvm::RoundingMode;
144 
145 //===----------------------------------------------------------------------===//
146 // ChainedASTReaderListener implementation
147 //===----------------------------------------------------------------------===//
148 
149 bool
150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151   return First->ReadFullVersionInformation(FullVersion) ||
152          Second->ReadFullVersionInformation(FullVersion);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156   First->ReadModuleName(ModuleName);
157   Second->ReadModuleName(ModuleName);
158 }
159 
160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161   First->ReadModuleMapFile(ModuleMapPath);
162   Second->ReadModuleMapFile(ModuleMapPath);
163 }
164 
165 bool
166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167                                               bool Complain,
168                                               bool AllowCompatibleDifferences) {
169   return First->ReadLanguageOptions(LangOpts, Complain,
170                                     AllowCompatibleDifferences) ||
171          Second->ReadLanguageOptions(LangOpts, Complain,
172                                      AllowCompatibleDifferences);
173 }
174 
175 bool ChainedASTReaderListener::ReadTargetOptions(
176     const TargetOptions &TargetOpts, bool Complain,
177     bool AllowCompatibleDifferences) {
178   return First->ReadTargetOptions(TargetOpts, Complain,
179                                   AllowCompatibleDifferences) ||
180          Second->ReadTargetOptions(TargetOpts, Complain,
181                                    AllowCompatibleDifferences);
182 }
183 
184 bool ChainedASTReaderListener::ReadDiagnosticOptions(
185     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187          Second->ReadDiagnosticOptions(DiagOpts, Complain);
188 }
189 
190 bool
191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192                                                 bool Complain) {
193   return First->ReadFileSystemOptions(FSOpts, Complain) ||
194          Second->ReadFileSystemOptions(FSOpts, Complain);
195 }
196 
197 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199     bool Complain) {
200   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                         Complain) ||
202          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                          Complain);
204 }
205 
206 bool ChainedASTReaderListener::ReadPreprocessorOptions(
207     const PreprocessorOptions &PPOpts, bool Complain,
208     std::string &SuggestedPredefines) {
209   return First->ReadPreprocessorOptions(PPOpts, Complain,
210                                         SuggestedPredefines) ||
211          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212 }
213 
214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215                                            unsigned Value) {
216   First->ReadCounter(M, Value);
217   Second->ReadCounter(M, Value);
218 }
219 
220 bool ChainedASTReaderListener::needsInputFileVisitation() {
221   return First->needsInputFileVisitation() ||
222          Second->needsInputFileVisitation();
223 }
224 
225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226   return First->needsSystemInputFileVisitation() ||
227   Second->needsSystemInputFileVisitation();
228 }
229 
230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231                                                ModuleKind Kind) {
232   First->visitModuleFile(Filename, Kind);
233   Second->visitModuleFile(Filename, Kind);
234 }
235 
236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237                                               bool isSystem,
238                                               bool isOverridden,
239                                               bool isExplicitModule) {
240   bool Continue = false;
241   if (First->needsInputFileVisitation() &&
242       (!isSystem || First->needsSystemInputFileVisitation()))
243     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244                                       isExplicitModule);
245   if (Second->needsInputFileVisitation() &&
246       (!isSystem || Second->needsSystemInputFileVisitation()))
247     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248                                        isExplicitModule);
249   return Continue;
250 }
251 
252 void ChainedASTReaderListener::readModuleFileExtension(
253        const ModuleFileExtensionMetadata &Metadata) {
254   First->readModuleFileExtension(Metadata);
255   Second->readModuleFileExtension(Metadata);
256 }
257 
258 //===----------------------------------------------------------------------===//
259 // PCH validator implementation
260 //===----------------------------------------------------------------------===//
261 
262 ASTReaderListener::~ASTReaderListener() = default;
263 
264 /// Compare the given set of language options against an existing set of
265 /// language options.
266 ///
267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268 /// \param AllowCompatibleDifferences If true, differences between compatible
269 ///        language options will be permitted.
270 ///
271 /// \returns true if the languagae options mis-match, false otherwise.
272 static bool checkLanguageOptions(const LangOptions &LangOpts,
273                                  const LangOptions &ExistingLangOpts,
274                                  DiagnosticsEngine *Diags,
275                                  bool AllowCompatibleDifferences = true) {
276 #define LANGOPT(Name, Bits, Default, Description)                 \
277   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
278     if (Diags)                                                    \
279       Diags->Report(diag::err_pch_langopt_mismatch)               \
280         << Description << LangOpts.Name << ExistingLangOpts.Name; \
281     return true;                                                  \
282   }
283 
284 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
285   if (ExistingLangOpts.Name != LangOpts.Name) {           \
286     if (Diags)                                            \
287       Diags->Report(diag::err_pch_langopt_value_mismatch) \
288         << Description;                                   \
289     return true;                                          \
290   }
291 
292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
293   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
294     if (Diags)                                                 \
295       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
296         << Description;                                        \
297     return true;                                               \
298   }
299 
300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
301   if (!AllowCompatibleDifferences)                            \
302     LANGOPT(Name, Bits, Default, Description)
303 
304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
305   if (!AllowCompatibleDifferences)                                 \
306     ENUM_LANGOPT(Name, Bits, Default, Description)
307 
308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309   if (!AllowCompatibleDifferences)                                 \
310     VALUE_LANGOPT(Name, Bits, Default, Description)
311 
312 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315 #include "clang/Basic/LangOptions.def"
316 
317   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318     if (Diags)
319       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320     return true;
321   }
322 
323   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324     if (Diags)
325       Diags->Report(diag::err_pch_langopt_value_mismatch)
326       << "target Objective-C runtime";
327     return true;
328   }
329 
330   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331       LangOpts.CommentOpts.BlockCommandNames) {
332     if (Diags)
333       Diags->Report(diag::err_pch_langopt_value_mismatch)
334         << "block command names";
335     return true;
336   }
337 
338   // Sanitizer feature mismatches are treated as compatible differences. If
339   // compatible differences aren't allowed, we still only want to check for
340   // mismatches of non-modular sanitizers (the only ones which can affect AST
341   // generation).
342   if (!AllowCompatibleDifferences) {
343     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346     ExistingSanitizers.clear(ModularSanitizers);
347     ImportedSanitizers.clear(ModularSanitizers);
348     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349       const std::string Flag = "-fsanitize=";
350       if (Diags) {
351 #define SANITIZER(NAME, ID)                                                    \
352   {                                                                            \
353     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
354     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
355     if (InExistingModule != InImportedModule)                                  \
356       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
357           << InExistingModule << (Flag + NAME);                                \
358   }
359 #include "clang/Basic/Sanitizers.def"
360       }
361       return true;
362     }
363   }
364 
365   return false;
366 }
367 
368 /// Compare the given set of target options against an existing set of
369 /// target options.
370 ///
371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372 ///
373 /// \returns true if the target options mis-match, false otherwise.
374 static bool checkTargetOptions(const TargetOptions &TargetOpts,
375                                const TargetOptions &ExistingTargetOpts,
376                                DiagnosticsEngine *Diags,
377                                bool AllowCompatibleDifferences = true) {
378 #define CHECK_TARGET_OPT(Field, Name)                             \
379   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
380     if (Diags)                                                    \
381       Diags->Report(diag::err_pch_targetopt_mismatch)             \
382         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
383     return true;                                                  \
384   }
385 
386   // The triple and ABI must match exactly.
387   CHECK_TARGET_OPT(Triple, "target");
388   CHECK_TARGET_OPT(ABI, "target ABI");
389 
390   // We can tolerate different CPUs in many cases, notably when one CPU
391   // supports a strict superset of another. When allowing compatible
392   // differences skip this check.
393   if (!AllowCompatibleDifferences)
394     CHECK_TARGET_OPT(CPU, "target CPU");
395 
396 #undef CHECK_TARGET_OPT
397 
398   // Compare feature sets.
399   SmallVector<StringRef, 4> ExistingFeatures(
400                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
401                                              ExistingTargetOpts.FeaturesAsWritten.end());
402   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
403                                          TargetOpts.FeaturesAsWritten.end());
404   llvm::sort(ExistingFeatures);
405   llvm::sort(ReadFeatures);
406 
407   // We compute the set difference in both directions explicitly so that we can
408   // diagnose the differences differently.
409   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
410   std::set_difference(
411       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
412       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
413   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
414                       ExistingFeatures.begin(), ExistingFeatures.end(),
415                       std::back_inserter(UnmatchedReadFeatures));
416 
417   // If we are allowing compatible differences and the read feature set is
418   // a strict subset of the existing feature set, there is nothing to diagnose.
419   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
420     return false;
421 
422   if (Diags) {
423     for (StringRef Feature : UnmatchedReadFeatures)
424       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
425           << /* is-existing-feature */ false << Feature;
426     for (StringRef Feature : UnmatchedExistingFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ true << Feature;
429   }
430 
431   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
432 }
433 
434 bool
435 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
436                                   bool Complain,
437                                   bool AllowCompatibleDifferences) {
438   const LangOptions &ExistingLangOpts = PP.getLangOpts();
439   return checkLanguageOptions(LangOpts, ExistingLangOpts,
440                               Complain ? &Reader.Diags : nullptr,
441                               AllowCompatibleDifferences);
442 }
443 
444 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
445                                      bool Complain,
446                                      bool AllowCompatibleDifferences) {
447   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
448   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
449                             Complain ? &Reader.Diags : nullptr,
450                             AllowCompatibleDifferences);
451 }
452 
453 namespace {
454 
455 using MacroDefinitionsMap =
456     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
457 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
458 
459 } // namespace
460 
461 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
462                                          DiagnosticsEngine &Diags,
463                                          bool Complain) {
464   using Level = DiagnosticsEngine::Level;
465 
466   // Check current mappings for new -Werror mappings, and the stored mappings
467   // for cases that were explicitly mapped to *not* be errors that are now
468   // errors because of options like -Werror.
469   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
470 
471   for (DiagnosticsEngine *MappingSource : MappingSources) {
472     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
473       diag::kind DiagID = DiagIDMappingPair.first;
474       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
475       if (CurLevel < DiagnosticsEngine::Error)
476         continue; // not significant
477       Level StoredLevel =
478           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
479       if (StoredLevel < DiagnosticsEngine::Error) {
480         if (Complain)
481           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
482               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
483         return true;
484       }
485     }
486   }
487 
488   return false;
489 }
490 
491 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
492   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
493   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
494     return true;
495   return Ext >= diag::Severity::Error;
496 }
497 
498 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
499                                     DiagnosticsEngine &Diags,
500                                     bool IsSystem, bool Complain) {
501   // Top-level options
502   if (IsSystem) {
503     if (Diags.getSuppressSystemWarnings())
504       return false;
505     // If -Wsystem-headers was not enabled before, be conservative
506     if (StoredDiags.getSuppressSystemWarnings()) {
507       if (Complain)
508         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
509       return true;
510     }
511   }
512 
513   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
514     if (Complain)
515       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
516     return true;
517   }
518 
519   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
520       !StoredDiags.getEnableAllWarnings()) {
521     if (Complain)
522       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
523     return true;
524   }
525 
526   if (isExtHandlingFromDiagsError(Diags) &&
527       !isExtHandlingFromDiagsError(StoredDiags)) {
528     if (Complain)
529       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
530     return true;
531   }
532 
533   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
534 }
535 
536 /// Return the top import module if it is implicit, nullptr otherwise.
537 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
538                                           Preprocessor &PP) {
539   // If the original import came from a file explicitly generated by the user,
540   // don't check the diagnostic mappings.
541   // FIXME: currently this is approximated by checking whether this is not a
542   // module import of an implicitly-loaded module file.
543   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
544   // the transitive closure of its imports, since unrelated modules cannot be
545   // imported until after this module finishes validation.
546   ModuleFile *TopImport = &*ModuleMgr.rbegin();
547   while (!TopImport->ImportedBy.empty())
548     TopImport = TopImport->ImportedBy[0];
549   if (TopImport->Kind != MK_ImplicitModule)
550     return nullptr;
551 
552   StringRef ModuleName = TopImport->ModuleName;
553   assert(!ModuleName.empty() && "diagnostic options read before module name");
554 
555   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
556   assert(M && "missing module");
557   return M;
558 }
559 
560 bool PCHValidator::ReadDiagnosticOptions(
561     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
562   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
563   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
564   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
565       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
566   // This should never fail, because we would have processed these options
567   // before writing them to an ASTFile.
568   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
569 
570   ModuleManager &ModuleMgr = Reader.getModuleManager();
571   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
572 
573   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
574   if (!TopM)
575     return false;
576 
577   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
578   // contains the union of their flags.
579   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
580                                  Complain);
581 }
582 
583 /// Collect the macro definitions provided by the given preprocessor
584 /// options.
585 static void
586 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
587                         MacroDefinitionsMap &Macros,
588                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
589   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
590     StringRef Macro = PPOpts.Macros[I].first;
591     bool IsUndef = PPOpts.Macros[I].second;
592 
593     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
594     StringRef MacroName = MacroPair.first;
595     StringRef MacroBody = MacroPair.second;
596 
597     // For an #undef'd macro, we only care about the name.
598     if (IsUndef) {
599       if (MacroNames && !Macros.count(MacroName))
600         MacroNames->push_back(MacroName);
601 
602       Macros[MacroName] = std::make_pair("", true);
603       continue;
604     }
605 
606     // For a #define'd macro, figure out the actual definition.
607     if (MacroName.size() == Macro.size())
608       MacroBody = "1";
609     else {
610       // Note: GCC drops anything following an end-of-line character.
611       StringRef::size_type End = MacroBody.find_first_of("\n\r");
612       MacroBody = MacroBody.substr(0, End);
613     }
614 
615     if (MacroNames && !Macros.count(MacroName))
616       MacroNames->push_back(MacroName);
617     Macros[MacroName] = std::make_pair(MacroBody, false);
618   }
619 }
620 
621 /// Check the preprocessor options deserialized from the control block
622 /// against the preprocessor options in an existing preprocessor.
623 ///
624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
625 /// \param Validate If true, validate preprocessor options. If false, allow
626 ///        macros defined by \p ExistingPPOpts to override those defined by
627 ///        \p PPOpts in SuggestedPredefines.
628 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
629                                      const PreprocessorOptions &ExistingPPOpts,
630                                      DiagnosticsEngine *Diags,
631                                      FileManager &FileMgr,
632                                      std::string &SuggestedPredefines,
633                                      const LangOptions &LangOpts,
634                                      bool Validate = true) {
635   // Check macro definitions.
636   MacroDefinitionsMap ASTFileMacros;
637   collectMacroDefinitions(PPOpts, ASTFileMacros);
638   MacroDefinitionsMap ExistingMacros;
639   SmallVector<StringRef, 4> ExistingMacroNames;
640   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
641 
642   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
643     // Dig out the macro definition in the existing preprocessor options.
644     StringRef MacroName = ExistingMacroNames[I];
645     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
646 
647     // Check whether we know anything about this macro name or not.
648     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
649         ASTFileMacros.find(MacroName);
650     if (!Validate || Known == ASTFileMacros.end()) {
651       // FIXME: Check whether this identifier was referenced anywhere in the
652       // AST file. If so, we should reject the AST file. Unfortunately, this
653       // information isn't in the control block. What shall we do about it?
654 
655       if (Existing.second) {
656         SuggestedPredefines += "#undef ";
657         SuggestedPredefines += MacroName.str();
658         SuggestedPredefines += '\n';
659       } else {
660         SuggestedPredefines += "#define ";
661         SuggestedPredefines += MacroName.str();
662         SuggestedPredefines += ' ';
663         SuggestedPredefines += Existing.first.str();
664         SuggestedPredefines += '\n';
665       }
666       continue;
667     }
668 
669     // If the macro was defined in one but undef'd in the other, we have a
670     // conflict.
671     if (Existing.second != Known->second.second) {
672       if (Diags) {
673         Diags->Report(diag::err_pch_macro_def_undef)
674           << MacroName << Known->second.second;
675       }
676       return true;
677     }
678 
679     // If the macro was #undef'd in both, or if the macro bodies are identical,
680     // it's fine.
681     if (Existing.second || Existing.first == Known->second.first)
682       continue;
683 
684     // The macro bodies differ; complain.
685     if (Diags) {
686       Diags->Report(diag::err_pch_macro_def_conflict)
687         << MacroName << Known->second.first << Existing.first;
688     }
689     return true;
690   }
691 
692   // Check whether we're using predefines.
693   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
694     if (Diags) {
695       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
696     }
697     return true;
698   }
699 
700   // Detailed record is important since it is used for the module cache hash.
701   if (LangOpts.Modules &&
702       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
703     if (Diags) {
704       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
705     }
706     return true;
707   }
708 
709   // Compute the #include and #include_macros lines we need.
710   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
711     StringRef File = ExistingPPOpts.Includes[I];
712 
713     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
714         !ExistingPPOpts.PCHThroughHeader.empty()) {
715       // In case the through header is an include, we must add all the includes
716       // to the predefines so the start point can be determined.
717       SuggestedPredefines += "#include \"";
718       SuggestedPredefines += File;
719       SuggestedPredefines += "\"\n";
720       continue;
721     }
722 
723     if (File == ExistingPPOpts.ImplicitPCHInclude)
724       continue;
725 
726     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
727           != PPOpts.Includes.end())
728       continue;
729 
730     SuggestedPredefines += "#include \"";
731     SuggestedPredefines += File;
732     SuggestedPredefines += "\"\n";
733   }
734 
735   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
736     StringRef File = ExistingPPOpts.MacroIncludes[I];
737     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
738                   File)
739         != PPOpts.MacroIncludes.end())
740       continue;
741 
742     SuggestedPredefines += "#__include_macros \"";
743     SuggestedPredefines += File;
744     SuggestedPredefines += "\"\n##\n";
745   }
746 
747   return false;
748 }
749 
750 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
751                                            bool Complain,
752                                            std::string &SuggestedPredefines) {
753   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
754 
755   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
756                                   Complain? &Reader.Diags : nullptr,
757                                   PP.getFileManager(),
758                                   SuggestedPredefines,
759                                   PP.getLangOpts());
760 }
761 
762 bool SimpleASTReaderListener::ReadPreprocessorOptions(
763                                   const PreprocessorOptions &PPOpts,
764                                   bool Complain,
765                                   std::string &SuggestedPredefines) {
766   return checkPreprocessorOptions(PPOpts,
767                                   PP.getPreprocessorOpts(),
768                                   nullptr,
769                                   PP.getFileManager(),
770                                   SuggestedPredefines,
771                                   PP.getLangOpts(),
772                                   false);
773 }
774 
775 /// Check the header search options deserialized from the control block
776 /// against the header search options in an existing preprocessor.
777 ///
778 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
779 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
780                                      StringRef SpecificModuleCachePath,
781                                      StringRef ExistingModuleCachePath,
782                                      DiagnosticsEngine *Diags,
783                                      const LangOptions &LangOpts) {
784   if (LangOpts.Modules) {
785     if (SpecificModuleCachePath != ExistingModuleCachePath) {
786       if (Diags)
787         Diags->Report(diag::err_pch_modulecache_mismatch)
788           << SpecificModuleCachePath << ExistingModuleCachePath;
789       return true;
790     }
791   }
792 
793   return false;
794 }
795 
796 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
797                                            StringRef SpecificModuleCachePath,
798                                            bool Complain) {
799   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
800                                   PP.getHeaderSearchInfo().getModuleCachePath(),
801                                   Complain ? &Reader.Diags : nullptr,
802                                   PP.getLangOpts());
803 }
804 
805 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
806   PP.setCounterValue(Value);
807 }
808 
809 //===----------------------------------------------------------------------===//
810 // AST reader implementation
811 //===----------------------------------------------------------------------===//
812 
813 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
814                                            bool TakeOwnership) {
815   DeserializationListener = Listener;
816   OwnsDeserializationListener = TakeOwnership;
817 }
818 
819 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
820   return serialization::ComputeHash(Sel);
821 }
822 
823 std::pair<unsigned, unsigned>
824 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
825   using namespace llvm::support;
826 
827   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
828   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
829   return std::make_pair(KeyLen, DataLen);
830 }
831 
832 ASTSelectorLookupTrait::internal_key_type
833 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
834   using namespace llvm::support;
835 
836   SelectorTable &SelTable = Reader.getContext().Selectors;
837   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
838   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
839       F, endian::readNext<uint32_t, little, unaligned>(d));
840   if (N == 0)
841     return SelTable.getNullarySelector(FirstII);
842   else if (N == 1)
843     return SelTable.getUnarySelector(FirstII);
844 
845   SmallVector<IdentifierInfo *, 16> Args;
846   Args.push_back(FirstII);
847   for (unsigned I = 1; I != N; ++I)
848     Args.push_back(Reader.getLocalIdentifier(
849         F, endian::readNext<uint32_t, little, unaligned>(d)));
850 
851   return SelTable.getSelector(N, Args.data());
852 }
853 
854 ASTSelectorLookupTrait::data_type
855 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
856                                  unsigned DataLen) {
857   using namespace llvm::support;
858 
859   data_type Result;
860 
861   Result.ID = Reader.getGlobalSelectorID(
862       F, endian::readNext<uint32_t, little, unaligned>(d));
863   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
864   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
865   Result.InstanceBits = FullInstanceBits & 0x3;
866   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
867   Result.FactoryBits = FullFactoryBits & 0x3;
868   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
869   unsigned NumInstanceMethods = FullInstanceBits >> 3;
870   unsigned NumFactoryMethods = FullFactoryBits >> 3;
871 
872   // Load instance methods
873   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
874     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
875             F, endian::readNext<uint32_t, little, unaligned>(d)))
876       Result.Instance.push_back(Method);
877   }
878 
879   // Load factory methods
880   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
881     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
882             F, endian::readNext<uint32_t, little, unaligned>(d)))
883       Result.Factory.push_back(Method);
884   }
885 
886   return Result;
887 }
888 
889 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
890   return llvm::djbHash(a);
891 }
892 
893 std::pair<unsigned, unsigned>
894 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
895   using namespace llvm::support;
896 
897   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899   return std::make_pair(KeyLen, DataLen);
900 }
901 
902 ASTIdentifierLookupTraitBase::internal_key_type
903 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
904   assert(n >= 2 && d[n-1] == '\0');
905   return StringRef((const char*) d, n-1);
906 }
907 
908 /// Whether the given identifier is "interesting".
909 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
910                                     bool IsModule) {
911   return II.hadMacroDefinition() ||
912          II.isPoisoned() ||
913          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
914          II.hasRevertedTokenIDToIdentifier() ||
915          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
916           II.getFETokenInfo());
917 }
918 
919 static bool readBit(unsigned &Bits) {
920   bool Value = Bits & 0x1;
921   Bits >>= 1;
922   return Value;
923 }
924 
925 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
926   using namespace llvm::support;
927 
928   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
929   return Reader.getGlobalIdentifierID(F, RawID >> 1);
930 }
931 
932 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
933   if (!II.isFromAST()) {
934     II.setIsFromAST();
935     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
936     if (isInterestingIdentifier(Reader, II, IsModule))
937       II.setChangedSinceDeserialization();
938   }
939 }
940 
941 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
942                                                    const unsigned char* d,
943                                                    unsigned DataLen) {
944   using namespace llvm::support;
945 
946   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
947   bool IsInteresting = RawID & 0x01;
948 
949   // Wipe out the "is interesting" bit.
950   RawID = RawID >> 1;
951 
952   // Build the IdentifierInfo and link the identifier ID with it.
953   IdentifierInfo *II = KnownII;
954   if (!II) {
955     II = &Reader.getIdentifierTable().getOwn(k);
956     KnownII = II;
957   }
958   markIdentifierFromAST(Reader, *II);
959   Reader.markIdentifierUpToDate(II);
960 
961   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
962   if (!IsInteresting) {
963     // For uninteresting identifiers, there's nothing else to do. Just notify
964     // the reader that we've finished loading this identifier.
965     Reader.SetIdentifierInfo(ID, II);
966     return II;
967   }
968 
969   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
970   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
971   bool CPlusPlusOperatorKeyword = readBit(Bits);
972   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
973   bool HasRevertedBuiltin = readBit(Bits);
974   bool Poisoned = readBit(Bits);
975   bool ExtensionToken = readBit(Bits);
976   bool HadMacroDefinition = readBit(Bits);
977 
978   assert(Bits == 0 && "Extra bits in the identifier?");
979   DataLen -= 8;
980 
981   // Set or check the various bits in the IdentifierInfo structure.
982   // Token IDs are read-only.
983   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
984     II->revertTokenIDToIdentifier();
985   if (!F.isModule())
986     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987   else if (HasRevertedBuiltin && II->getBuiltinID()) {
988     II->revertBuiltin();
989     assert((II->hasRevertedBuiltin() ||
990             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
991            "Incorrect ObjC keyword or builtin ID");
992   }
993   assert(II->isExtensionToken() == ExtensionToken &&
994          "Incorrect extension token flag");
995   (void)ExtensionToken;
996   if (Poisoned)
997     II->setIsPoisoned(true);
998   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
999          "Incorrect C++ operator keyword flag");
1000   (void)CPlusPlusOperatorKeyword;
1001 
1002   // If this identifier is a macro, deserialize the macro
1003   // definition.
1004   if (HadMacroDefinition) {
1005     uint32_t MacroDirectivesOffset =
1006         endian::readNext<uint32_t, little, unaligned>(d);
1007     DataLen -= 4;
1008 
1009     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1010   }
1011 
1012   Reader.SetIdentifierInfo(ID, II);
1013 
1014   // Read all of the declarations visible at global scope with this
1015   // name.
1016   if (DataLen > 0) {
1017     SmallVector<uint32_t, 4> DeclIDs;
1018     for (; DataLen > 0; DataLen -= 4)
1019       DeclIDs.push_back(Reader.getGlobalDeclID(
1020           F, endian::readNext<uint32_t, little, unaligned>(d)));
1021     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1022   }
1023 
1024   return II;
1025 }
1026 
1027 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1028     : Kind(Name.getNameKind()) {
1029   switch (Kind) {
1030   case DeclarationName::Identifier:
1031     Data = (uint64_t)Name.getAsIdentifierInfo();
1032     break;
1033   case DeclarationName::ObjCZeroArgSelector:
1034   case DeclarationName::ObjCOneArgSelector:
1035   case DeclarationName::ObjCMultiArgSelector:
1036     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1037     break;
1038   case DeclarationName::CXXOperatorName:
1039     Data = Name.getCXXOverloadedOperator();
1040     break;
1041   case DeclarationName::CXXLiteralOperatorName:
1042     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1043     break;
1044   case DeclarationName::CXXDeductionGuideName:
1045     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1046                ->getDeclName().getAsIdentifierInfo();
1047     break;
1048   case DeclarationName::CXXConstructorName:
1049   case DeclarationName::CXXDestructorName:
1050   case DeclarationName::CXXConversionFunctionName:
1051   case DeclarationName::CXXUsingDirective:
1052     Data = 0;
1053     break;
1054   }
1055 }
1056 
1057 unsigned DeclarationNameKey::getHash() const {
1058   llvm::FoldingSetNodeID ID;
1059   ID.AddInteger(Kind);
1060 
1061   switch (Kind) {
1062   case DeclarationName::Identifier:
1063   case DeclarationName::CXXLiteralOperatorName:
1064   case DeclarationName::CXXDeductionGuideName:
1065     ID.AddString(((IdentifierInfo*)Data)->getName());
1066     break;
1067   case DeclarationName::ObjCZeroArgSelector:
1068   case DeclarationName::ObjCOneArgSelector:
1069   case DeclarationName::ObjCMultiArgSelector:
1070     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1071     break;
1072   case DeclarationName::CXXOperatorName:
1073     ID.AddInteger((OverloadedOperatorKind)Data);
1074     break;
1075   case DeclarationName::CXXConstructorName:
1076   case DeclarationName::CXXDestructorName:
1077   case DeclarationName::CXXConversionFunctionName:
1078   case DeclarationName::CXXUsingDirective:
1079     break;
1080   }
1081 
1082   return ID.ComputeHash();
1083 }
1084 
1085 ModuleFile *
1086 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1087   using namespace llvm::support;
1088 
1089   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1090   return Reader.getLocalModuleFile(F, ModuleFileID);
1091 }
1092 
1093 std::pair<unsigned, unsigned>
1094 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1095   using namespace llvm::support;
1096 
1097   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1098   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1099   return std::make_pair(KeyLen, DataLen);
1100 }
1101 
1102 ASTDeclContextNameLookupTrait::internal_key_type
1103 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1104   using namespace llvm::support;
1105 
1106   auto Kind = (DeclarationName::NameKind)*d++;
1107   uint64_t Data;
1108   switch (Kind) {
1109   case DeclarationName::Identifier:
1110   case DeclarationName::CXXLiteralOperatorName:
1111   case DeclarationName::CXXDeductionGuideName:
1112     Data = (uint64_t)Reader.getLocalIdentifier(
1113         F, endian::readNext<uint32_t, little, unaligned>(d));
1114     break;
1115   case DeclarationName::ObjCZeroArgSelector:
1116   case DeclarationName::ObjCOneArgSelector:
1117   case DeclarationName::ObjCMultiArgSelector:
1118     Data =
1119         (uint64_t)Reader.getLocalSelector(
1120                              F, endian::readNext<uint32_t, little, unaligned>(
1121                                     d)).getAsOpaquePtr();
1122     break;
1123   case DeclarationName::CXXOperatorName:
1124     Data = *d++; // OverloadedOperatorKind
1125     break;
1126   case DeclarationName::CXXConstructorName:
1127   case DeclarationName::CXXDestructorName:
1128   case DeclarationName::CXXConversionFunctionName:
1129   case DeclarationName::CXXUsingDirective:
1130     Data = 0;
1131     break;
1132   }
1133 
1134   return DeclarationNameKey(Kind, Data);
1135 }
1136 
1137 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1138                                                  const unsigned char *d,
1139                                                  unsigned DataLen,
1140                                                  data_type_builder &Val) {
1141   using namespace llvm::support;
1142 
1143   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1144     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1145     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1146   }
1147 }
1148 
1149 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1150                                               BitstreamCursor &Cursor,
1151                                               uint64_t Offset,
1152                                               DeclContext *DC) {
1153   assert(Offset != 0);
1154 
1155   SavedStreamPosition SavedPosition(Cursor);
1156   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1157     Error(std::move(Err));
1158     return true;
1159   }
1160 
1161   RecordData Record;
1162   StringRef Blob;
1163   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1164   if (!MaybeCode) {
1165     Error(MaybeCode.takeError());
1166     return true;
1167   }
1168   unsigned Code = MaybeCode.get();
1169 
1170   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1171   if (!MaybeRecCode) {
1172     Error(MaybeRecCode.takeError());
1173     return true;
1174   }
1175   unsigned RecCode = MaybeRecCode.get();
1176   if (RecCode != DECL_CONTEXT_LEXICAL) {
1177     Error("Expected lexical block");
1178     return true;
1179   }
1180 
1181   assert(!isa<TranslationUnitDecl>(DC) &&
1182          "expected a TU_UPDATE_LEXICAL record for TU");
1183   // If we are handling a C++ class template instantiation, we can see multiple
1184   // lexical updates for the same record. It's important that we select only one
1185   // of them, so that field numbering works properly. Just pick the first one we
1186   // see.
1187   auto &Lex = LexicalDecls[DC];
1188   if (!Lex.first) {
1189     Lex = std::make_pair(
1190         &M, llvm::makeArrayRef(
1191                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1192                     Blob.data()),
1193                 Blob.size() / 4));
1194   }
1195   DC->setHasExternalLexicalStorage(true);
1196   return false;
1197 }
1198 
1199 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1200                                               BitstreamCursor &Cursor,
1201                                               uint64_t Offset,
1202                                               DeclID ID) {
1203   assert(Offset != 0);
1204 
1205   SavedStreamPosition SavedPosition(Cursor);
1206   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1207     Error(std::move(Err));
1208     return true;
1209   }
1210 
1211   RecordData Record;
1212   StringRef Blob;
1213   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1214   if (!MaybeCode) {
1215     Error(MaybeCode.takeError());
1216     return true;
1217   }
1218   unsigned Code = MaybeCode.get();
1219 
1220   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1221   if (!MaybeRecCode) {
1222     Error(MaybeRecCode.takeError());
1223     return true;
1224   }
1225   unsigned RecCode = MaybeRecCode.get();
1226   if (RecCode != DECL_CONTEXT_VISIBLE) {
1227     Error("Expected visible lookup table block");
1228     return true;
1229   }
1230 
1231   // We can't safely determine the primary context yet, so delay attaching the
1232   // lookup table until we're done with recursive deserialization.
1233   auto *Data = (const unsigned char*)Blob.data();
1234   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1235   return false;
1236 }
1237 
1238 void ASTReader::Error(StringRef Msg) const {
1239   Error(diag::err_fe_pch_malformed, Msg);
1240   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1241       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1242     Diag(diag::note_module_cache_path)
1243       << PP.getHeaderSearchInfo().getModuleCachePath();
1244   }
1245 }
1246 
1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248                       StringRef Arg3) const {
1249   if (Diags.isDiagnosticInFlight())
1250     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1251   else
1252     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1253 }
1254 
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256                       unsigned Select) const {
1257   if (!Diags.isDiagnosticInFlight())
1258     Diag(DiagID) << Arg1 << Arg2 << Select;
1259 }
1260 
1261 void ASTReader::Error(llvm::Error &&Err) const {
1262   Error(toString(std::move(Err)));
1263 }
1264 
1265 //===----------------------------------------------------------------------===//
1266 // Source Manager Deserialization
1267 //===----------------------------------------------------------------------===//
1268 
1269 /// Read the line table in the source manager block.
1270 /// \returns true if there was an error.
1271 bool ASTReader::ParseLineTable(ModuleFile &F,
1272                                const RecordData &Record) {
1273   unsigned Idx = 0;
1274   LineTableInfo &LineTable = SourceMgr.getLineTable();
1275 
1276   // Parse the file names
1277   std::map<int, int> FileIDs;
1278   FileIDs[-1] = -1; // For unspecified filenames.
1279   for (unsigned I = 0; Record[Idx]; ++I) {
1280     // Extract the file name
1281     auto Filename = ReadPath(F, Record, Idx);
1282     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1283   }
1284   ++Idx;
1285 
1286   // Parse the line entries
1287   std::vector<LineEntry> Entries;
1288   while (Idx < Record.size()) {
1289     int FID = Record[Idx++];
1290     assert(FID >= 0 && "Serialized line entries for non-local file.");
1291     // Remap FileID from 1-based old view.
1292     FID += F.SLocEntryBaseID - 1;
1293 
1294     // Extract the line entries
1295     unsigned NumEntries = Record[Idx++];
1296     assert(NumEntries && "no line entries for file ID");
1297     Entries.clear();
1298     Entries.reserve(NumEntries);
1299     for (unsigned I = 0; I != NumEntries; ++I) {
1300       unsigned FileOffset = Record[Idx++];
1301       unsigned LineNo = Record[Idx++];
1302       int FilenameID = FileIDs[Record[Idx++]];
1303       SrcMgr::CharacteristicKind FileKind
1304         = (SrcMgr::CharacteristicKind)Record[Idx++];
1305       unsigned IncludeOffset = Record[Idx++];
1306       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1307                                        FileKind, IncludeOffset));
1308     }
1309     LineTable.AddEntry(FileID::get(FID), Entries);
1310   }
1311 
1312   return false;
1313 }
1314 
1315 /// Read a source manager block
1316 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1317   using namespace SrcMgr;
1318 
1319   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1320 
1321   // Set the source-location entry cursor to the current position in
1322   // the stream. This cursor will be used to read the contents of the
1323   // source manager block initially, and then lazily read
1324   // source-location entries as needed.
1325   SLocEntryCursor = F.Stream;
1326 
1327   // The stream itself is going to skip over the source manager block.
1328   if (llvm::Error Err = F.Stream.SkipBlock()) {
1329     Error(std::move(Err));
1330     return true;
1331   }
1332 
1333   // Enter the source manager block.
1334   if (llvm::Error Err =
1335           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1336     Error(std::move(Err));
1337     return true;
1338   }
1339 
1340   RecordData Record;
1341   while (true) {
1342     Expected<llvm::BitstreamEntry> MaybeE =
1343         SLocEntryCursor.advanceSkippingSubblocks();
1344     if (!MaybeE) {
1345       Error(MaybeE.takeError());
1346       return true;
1347     }
1348     llvm::BitstreamEntry E = MaybeE.get();
1349 
1350     switch (E.Kind) {
1351     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1352     case llvm::BitstreamEntry::Error:
1353       Error("malformed block record in AST file");
1354       return true;
1355     case llvm::BitstreamEntry::EndBlock:
1356       return false;
1357     case llvm::BitstreamEntry::Record:
1358       // The interesting case.
1359       break;
1360     }
1361 
1362     // Read a record.
1363     Record.clear();
1364     StringRef Blob;
1365     Expected<unsigned> MaybeRecord =
1366         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1367     if (!MaybeRecord) {
1368       Error(MaybeRecord.takeError());
1369       return true;
1370     }
1371     switch (MaybeRecord.get()) {
1372     default:  // Default behavior: ignore.
1373       break;
1374 
1375     case SM_SLOC_FILE_ENTRY:
1376     case SM_SLOC_BUFFER_ENTRY:
1377     case SM_SLOC_EXPANSION_ENTRY:
1378       // Once we hit one of the source location entries, we're done.
1379       return false;
1380     }
1381   }
1382 }
1383 
1384 /// If a header file is not found at the path that we expect it to be
1385 /// and the PCH file was moved from its original location, try to resolve the
1386 /// file by assuming that header+PCH were moved together and the header is in
1387 /// the same place relative to the PCH.
1388 static std::string
1389 resolveFileRelativeToOriginalDir(const std::string &Filename,
1390                                  const std::string &OriginalDir,
1391                                  const std::string &CurrDir) {
1392   assert(OriginalDir != CurrDir &&
1393          "No point trying to resolve the file if the PCH dir didn't change");
1394 
1395   using namespace llvm::sys;
1396 
1397   SmallString<128> filePath(Filename);
1398   fs::make_absolute(filePath);
1399   assert(path::is_absolute(OriginalDir));
1400   SmallString<128> currPCHPath(CurrDir);
1401 
1402   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1403                        fileDirE = path::end(path::parent_path(filePath));
1404   path::const_iterator origDirI = path::begin(OriginalDir),
1405                        origDirE = path::end(OriginalDir);
1406   // Skip the common path components from filePath and OriginalDir.
1407   while (fileDirI != fileDirE && origDirI != origDirE &&
1408          *fileDirI == *origDirI) {
1409     ++fileDirI;
1410     ++origDirI;
1411   }
1412   for (; origDirI != origDirE; ++origDirI)
1413     path::append(currPCHPath, "..");
1414   path::append(currPCHPath, fileDirI, fileDirE);
1415   path::append(currPCHPath, path::filename(Filename));
1416   return std::string(currPCHPath.str());
1417 }
1418 
1419 bool ASTReader::ReadSLocEntry(int ID) {
1420   if (ID == 0)
1421     return false;
1422 
1423   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1424     Error("source location entry ID out-of-range for AST file");
1425     return true;
1426   }
1427 
1428   // Local helper to read the (possibly-compressed) buffer data following the
1429   // entry record.
1430   auto ReadBuffer = [this](
1431       BitstreamCursor &SLocEntryCursor,
1432       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1433     RecordData Record;
1434     StringRef Blob;
1435     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1436     if (!MaybeCode) {
1437       Error(MaybeCode.takeError());
1438       return nullptr;
1439     }
1440     unsigned Code = MaybeCode.get();
1441 
1442     Expected<unsigned> MaybeRecCode =
1443         SLocEntryCursor.readRecord(Code, Record, &Blob);
1444     if (!MaybeRecCode) {
1445       Error(MaybeRecCode.takeError());
1446       return nullptr;
1447     }
1448     unsigned RecCode = MaybeRecCode.get();
1449 
1450     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1451       if (!llvm::zlib::isAvailable()) {
1452         Error("zlib is not available");
1453         return nullptr;
1454       }
1455       SmallString<0> Uncompressed;
1456       if (llvm::Error E =
1457               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1458         Error("could not decompress embedded file contents: " +
1459               llvm::toString(std::move(E)));
1460         return nullptr;
1461       }
1462       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1463     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1464       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1465     } else {
1466       Error("AST record has invalid code");
1467       return nullptr;
1468     }
1469   };
1470 
1471   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1472   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1473           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1474     Error(std::move(Err));
1475     return true;
1476   }
1477 
1478   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1479   unsigned BaseOffset = F->SLocEntryBaseOffset;
1480 
1481   ++NumSLocEntriesRead;
1482   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1483   if (!MaybeEntry) {
1484     Error(MaybeEntry.takeError());
1485     return true;
1486   }
1487   llvm::BitstreamEntry Entry = MaybeEntry.get();
1488 
1489   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1490     Error("incorrectly-formatted source location entry in AST file");
1491     return true;
1492   }
1493 
1494   RecordData Record;
1495   StringRef Blob;
1496   Expected<unsigned> MaybeSLOC =
1497       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1498   if (!MaybeSLOC) {
1499     Error(MaybeSLOC.takeError());
1500     return true;
1501   }
1502   switch (MaybeSLOC.get()) {
1503   default:
1504     Error("incorrectly-formatted source location entry in AST file");
1505     return true;
1506 
1507   case SM_SLOC_FILE_ENTRY: {
1508     // We will detect whether a file changed and return 'Failure' for it, but
1509     // we will also try to fail gracefully by setting up the SLocEntry.
1510     unsigned InputID = Record[4];
1511     InputFile IF = getInputFile(*F, InputID);
1512     const FileEntry *File = IF.getFile();
1513     bool OverriddenBuffer = IF.isOverridden();
1514 
1515     // Note that we only check if a File was returned. If it was out-of-date
1516     // we have complained but we will continue creating a FileID to recover
1517     // gracefully.
1518     if (!File)
1519       return true;
1520 
1521     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1522     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1523       // This is the module's main file.
1524       IncludeLoc = getImportLocation(F);
1525     }
1526     SrcMgr::CharacteristicKind
1527       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1528     // FIXME: The FileID should be created from the FileEntryRef.
1529     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1530                                         ID, BaseOffset + Record[0]);
1531     SrcMgr::FileInfo &FileInfo =
1532           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1533     FileInfo.NumCreatedFIDs = Record[5];
1534     if (Record[3])
1535       FileInfo.setHasLineDirectives();
1536 
1537     unsigned NumFileDecls = Record[7];
1538     if (NumFileDecls && ContextObj) {
1539       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1540       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1541       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1542                                                              NumFileDecls));
1543     }
1544 
1545     const SrcMgr::ContentCache *ContentCache
1546       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1547     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1548         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1549         !ContentCache->getRawBuffer()) {
1550       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1551       if (!Buffer)
1552         return true;
1553       SourceMgr.overrideFileContents(File, std::move(Buffer));
1554     }
1555 
1556     break;
1557   }
1558 
1559   case SM_SLOC_BUFFER_ENTRY: {
1560     const char *Name = Blob.data();
1561     unsigned Offset = Record[0];
1562     SrcMgr::CharacteristicKind
1563       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1564     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1565     if (IncludeLoc.isInvalid() && F->isModule()) {
1566       IncludeLoc = getImportLocation(F);
1567     }
1568 
1569     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1570     if (!Buffer)
1571       return true;
1572     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1573                            BaseOffset + Offset, IncludeLoc);
1574     break;
1575   }
1576 
1577   case SM_SLOC_EXPANSION_ENTRY: {
1578     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1579     SourceMgr.createExpansionLoc(SpellingLoc,
1580                                      ReadSourceLocation(*F, Record[2]),
1581                                      ReadSourceLocation(*F, Record[3]),
1582                                      Record[5],
1583                                      Record[4],
1584                                      ID,
1585                                      BaseOffset + Record[0]);
1586     break;
1587   }
1588   }
1589 
1590   return false;
1591 }
1592 
1593 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1594   if (ID == 0)
1595     return std::make_pair(SourceLocation(), "");
1596 
1597   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1598     Error("source location entry ID out-of-range for AST file");
1599     return std::make_pair(SourceLocation(), "");
1600   }
1601 
1602   // Find which module file this entry lands in.
1603   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1604   if (!M->isModule())
1605     return std::make_pair(SourceLocation(), "");
1606 
1607   // FIXME: Can we map this down to a particular submodule? That would be
1608   // ideal.
1609   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1610 }
1611 
1612 /// Find the location where the module F is imported.
1613 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1614   if (F->ImportLoc.isValid())
1615     return F->ImportLoc;
1616 
1617   // Otherwise we have a PCH. It's considered to be "imported" at the first
1618   // location of its includer.
1619   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1620     // Main file is the importer.
1621     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1622     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1623   }
1624   return F->ImportedBy[0]->FirstLoc;
1625 }
1626 
1627 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1628 /// the abbreviations that are at the top of the block and then leave the cursor
1629 /// pointing into the block.
1630 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1631   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1632     // FIXME this drops errors on the floor.
1633     consumeError(std::move(Err));
1634     return true;
1635   }
1636 
1637   while (true) {
1638     uint64_t Offset = Cursor.GetCurrentBitNo();
1639     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1640     if (!MaybeCode) {
1641       // FIXME this drops errors on the floor.
1642       consumeError(MaybeCode.takeError());
1643       return true;
1644     }
1645     unsigned Code = MaybeCode.get();
1646 
1647     // We expect all abbrevs to be at the start of the block.
1648     if (Code != llvm::bitc::DEFINE_ABBREV) {
1649       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1650         // FIXME this drops errors on the floor.
1651         consumeError(std::move(Err));
1652         return true;
1653       }
1654       return false;
1655     }
1656     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1657       // FIXME this drops errors on the floor.
1658       consumeError(std::move(Err));
1659       return true;
1660     }
1661   }
1662 }
1663 
1664 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1665                            unsigned &Idx) {
1666   Token Tok;
1667   Tok.startToken();
1668   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1669   Tok.setLength(Record[Idx++]);
1670   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1671     Tok.setIdentifierInfo(II);
1672   Tok.setKind((tok::TokenKind)Record[Idx++]);
1673   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1674   return Tok;
1675 }
1676 
1677 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1678   BitstreamCursor &Stream = F.MacroCursor;
1679 
1680   // Keep track of where we are in the stream, then jump back there
1681   // after reading this macro.
1682   SavedStreamPosition SavedPosition(Stream);
1683 
1684   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1685     // FIXME this drops errors on the floor.
1686     consumeError(std::move(Err));
1687     return nullptr;
1688   }
1689   RecordData Record;
1690   SmallVector<IdentifierInfo*, 16> MacroParams;
1691   MacroInfo *Macro = nullptr;
1692 
1693   while (true) {
1694     // Advance to the next record, but if we get to the end of the block, don't
1695     // pop it (removing all the abbreviations from the cursor) since we want to
1696     // be able to reseek within the block and read entries.
1697     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1698     Expected<llvm::BitstreamEntry> MaybeEntry =
1699         Stream.advanceSkippingSubblocks(Flags);
1700     if (!MaybeEntry) {
1701       Error(MaybeEntry.takeError());
1702       return Macro;
1703     }
1704     llvm::BitstreamEntry Entry = MaybeEntry.get();
1705 
1706     switch (Entry.Kind) {
1707     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1708     case llvm::BitstreamEntry::Error:
1709       Error("malformed block record in AST file");
1710       return Macro;
1711     case llvm::BitstreamEntry::EndBlock:
1712       return Macro;
1713     case llvm::BitstreamEntry::Record:
1714       // The interesting case.
1715       break;
1716     }
1717 
1718     // Read a record.
1719     Record.clear();
1720     PreprocessorRecordTypes RecType;
1721     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1722       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1723     else {
1724       Error(MaybeRecType.takeError());
1725       return Macro;
1726     }
1727     switch (RecType) {
1728     case PP_MODULE_MACRO:
1729     case PP_MACRO_DIRECTIVE_HISTORY:
1730       return Macro;
1731 
1732     case PP_MACRO_OBJECT_LIKE:
1733     case PP_MACRO_FUNCTION_LIKE: {
1734       // If we already have a macro, that means that we've hit the end
1735       // of the definition of the macro we were looking for. We're
1736       // done.
1737       if (Macro)
1738         return Macro;
1739 
1740       unsigned NextIndex = 1; // Skip identifier ID.
1741       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1742       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1743       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1744       MI->setIsUsed(Record[NextIndex++]);
1745       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1746 
1747       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1748         // Decode function-like macro info.
1749         bool isC99VarArgs = Record[NextIndex++];
1750         bool isGNUVarArgs = Record[NextIndex++];
1751         bool hasCommaPasting = Record[NextIndex++];
1752         MacroParams.clear();
1753         unsigned NumArgs = Record[NextIndex++];
1754         for (unsigned i = 0; i != NumArgs; ++i)
1755           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1756 
1757         // Install function-like macro info.
1758         MI->setIsFunctionLike();
1759         if (isC99VarArgs) MI->setIsC99Varargs();
1760         if (isGNUVarArgs) MI->setIsGNUVarargs();
1761         if (hasCommaPasting) MI->setHasCommaPasting();
1762         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1763       }
1764 
1765       // Remember that we saw this macro last so that we add the tokens that
1766       // form its body to it.
1767       Macro = MI;
1768 
1769       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1770           Record[NextIndex]) {
1771         // We have a macro definition. Register the association
1772         PreprocessedEntityID
1773             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1774         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1775         PreprocessingRecord::PPEntityID PPID =
1776             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1777         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1778             PPRec.getPreprocessedEntity(PPID));
1779         if (PPDef)
1780           PPRec.RegisterMacroDefinition(Macro, PPDef);
1781       }
1782 
1783       ++NumMacrosRead;
1784       break;
1785     }
1786 
1787     case PP_TOKEN: {
1788       // If we see a TOKEN before a PP_MACRO_*, then the file is
1789       // erroneous, just pretend we didn't see this.
1790       if (!Macro) break;
1791 
1792       unsigned Idx = 0;
1793       Token Tok = ReadToken(F, Record, Idx);
1794       Macro->AddTokenToBody(Tok);
1795       break;
1796     }
1797     }
1798   }
1799 }
1800 
1801 PreprocessedEntityID
1802 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1803                                          unsigned LocalID) const {
1804   if (!M.ModuleOffsetMap.empty())
1805     ReadModuleOffsetMap(M);
1806 
1807   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1808     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1809   assert(I != M.PreprocessedEntityRemap.end()
1810          && "Invalid index into preprocessed entity index remap");
1811 
1812   return LocalID + I->second;
1813 }
1814 
1815 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1816   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1817 }
1818 
1819 HeaderFileInfoTrait::internal_key_type
1820 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1821   internal_key_type ikey = {FE->getSize(),
1822                             M.HasTimestamps ? FE->getModificationTime() : 0,
1823                             FE->getName(), /*Imported*/ false};
1824   return ikey;
1825 }
1826 
1827 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1828   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1829     return false;
1830 
1831   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1832     return true;
1833 
1834   // Determine whether the actual files are equivalent.
1835   FileManager &FileMgr = Reader.getFileManager();
1836   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1837     if (!Key.Imported) {
1838       if (auto File = FileMgr.getFile(Key.Filename))
1839         return *File;
1840       return nullptr;
1841     }
1842 
1843     std::string Resolved = std::string(Key.Filename);
1844     Reader.ResolveImportedPath(M, Resolved);
1845     if (auto File = FileMgr.getFile(Resolved))
1846       return *File;
1847     return nullptr;
1848   };
1849 
1850   const FileEntry *FEA = GetFile(a);
1851   const FileEntry *FEB = GetFile(b);
1852   return FEA && FEA == FEB;
1853 }
1854 
1855 std::pair<unsigned, unsigned>
1856 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1857   using namespace llvm::support;
1858 
1859   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1860   unsigned DataLen = (unsigned) *d++;
1861   return std::make_pair(KeyLen, DataLen);
1862 }
1863 
1864 HeaderFileInfoTrait::internal_key_type
1865 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1866   using namespace llvm::support;
1867 
1868   internal_key_type ikey;
1869   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1870   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1871   ikey.Filename = (const char *)d;
1872   ikey.Imported = true;
1873   return ikey;
1874 }
1875 
1876 HeaderFileInfoTrait::data_type
1877 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1878                               unsigned DataLen) {
1879   using namespace llvm::support;
1880 
1881   const unsigned char *End = d + DataLen;
1882   HeaderFileInfo HFI;
1883   unsigned Flags = *d++;
1884   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1885   HFI.isImport |= (Flags >> 5) & 0x01;
1886   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1887   HFI.DirInfo = (Flags >> 1) & 0x07;
1888   HFI.IndexHeaderMapHeader = Flags & 0x01;
1889   // FIXME: Find a better way to handle this. Maybe just store a
1890   // "has been included" flag?
1891   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1892                              HFI.NumIncludes);
1893   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1894       M, endian::readNext<uint32_t, little, unaligned>(d));
1895   if (unsigned FrameworkOffset =
1896           endian::readNext<uint32_t, little, unaligned>(d)) {
1897     // The framework offset is 1 greater than the actual offset,
1898     // since 0 is used as an indicator for "no framework name".
1899     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1900     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1901   }
1902 
1903   assert((End - d) % 4 == 0 &&
1904          "Wrong data length in HeaderFileInfo deserialization");
1905   while (d != End) {
1906     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1907     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1908     LocalSMID >>= 2;
1909 
1910     // This header is part of a module. Associate it with the module to enable
1911     // implicit module import.
1912     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1913     Module *Mod = Reader.getSubmodule(GlobalSMID);
1914     FileManager &FileMgr = Reader.getFileManager();
1915     ModuleMap &ModMap =
1916         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1917 
1918     std::string Filename = std::string(key.Filename);
1919     if (key.Imported)
1920       Reader.ResolveImportedPath(M, Filename);
1921     // FIXME: This is not always the right filename-as-written, but we're not
1922     // going to use this information to rebuild the module, so it doesn't make
1923     // a lot of difference.
1924     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1925     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1926     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1927   }
1928 
1929   // This HeaderFileInfo was externally loaded.
1930   HFI.External = true;
1931   HFI.IsValid = true;
1932   return HFI;
1933 }
1934 
1935 void ASTReader::addPendingMacro(IdentifierInfo *II,
1936                                 ModuleFile *M,
1937                                 uint64_t MacroDirectivesOffset) {
1938   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1939   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1940 }
1941 
1942 void ASTReader::ReadDefinedMacros() {
1943   // Note that we are loading defined macros.
1944   Deserializing Macros(this);
1945 
1946   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1947     BitstreamCursor &MacroCursor = I.MacroCursor;
1948 
1949     // If there was no preprocessor block, skip this file.
1950     if (MacroCursor.getBitcodeBytes().empty())
1951       continue;
1952 
1953     BitstreamCursor Cursor = MacroCursor;
1954     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1955       Error(std::move(Err));
1956       return;
1957     }
1958 
1959     RecordData Record;
1960     while (true) {
1961       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1962       if (!MaybeE) {
1963         Error(MaybeE.takeError());
1964         return;
1965       }
1966       llvm::BitstreamEntry E = MaybeE.get();
1967 
1968       switch (E.Kind) {
1969       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1970       case llvm::BitstreamEntry::Error:
1971         Error("malformed block record in AST file");
1972         return;
1973       case llvm::BitstreamEntry::EndBlock:
1974         goto NextCursor;
1975 
1976       case llvm::BitstreamEntry::Record: {
1977         Record.clear();
1978         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1979         if (!MaybeRecord) {
1980           Error(MaybeRecord.takeError());
1981           return;
1982         }
1983         switch (MaybeRecord.get()) {
1984         default:  // Default behavior: ignore.
1985           break;
1986 
1987         case PP_MACRO_OBJECT_LIKE:
1988         case PP_MACRO_FUNCTION_LIKE: {
1989           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1990           if (II->isOutOfDate())
1991             updateOutOfDateIdentifier(*II);
1992           break;
1993         }
1994 
1995         case PP_TOKEN:
1996           // Ignore tokens.
1997           break;
1998         }
1999         break;
2000       }
2001       }
2002     }
2003     NextCursor:  ;
2004   }
2005 }
2006 
2007 namespace {
2008 
2009   /// Visitor class used to look up identifirs in an AST file.
2010   class IdentifierLookupVisitor {
2011     StringRef Name;
2012     unsigned NameHash;
2013     unsigned PriorGeneration;
2014     unsigned &NumIdentifierLookups;
2015     unsigned &NumIdentifierLookupHits;
2016     IdentifierInfo *Found = nullptr;
2017 
2018   public:
2019     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2020                             unsigned &NumIdentifierLookups,
2021                             unsigned &NumIdentifierLookupHits)
2022       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2023         PriorGeneration(PriorGeneration),
2024         NumIdentifierLookups(NumIdentifierLookups),
2025         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2026 
2027     bool operator()(ModuleFile &M) {
2028       // If we've already searched this module file, skip it now.
2029       if (M.Generation <= PriorGeneration)
2030         return true;
2031 
2032       ASTIdentifierLookupTable *IdTable
2033         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2034       if (!IdTable)
2035         return false;
2036 
2037       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2038                                      Found);
2039       ++NumIdentifierLookups;
2040       ASTIdentifierLookupTable::iterator Pos =
2041           IdTable->find_hashed(Name, NameHash, &Trait);
2042       if (Pos == IdTable->end())
2043         return false;
2044 
2045       // Dereferencing the iterator has the effect of building the
2046       // IdentifierInfo node and populating it with the various
2047       // declarations it needs.
2048       ++NumIdentifierLookupHits;
2049       Found = *Pos;
2050       return true;
2051     }
2052 
2053     // Retrieve the identifier info found within the module
2054     // files.
2055     IdentifierInfo *getIdentifierInfo() const { return Found; }
2056   };
2057 
2058 } // namespace
2059 
2060 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2061   // Note that we are loading an identifier.
2062   Deserializing AnIdentifier(this);
2063 
2064   unsigned PriorGeneration = 0;
2065   if (getContext().getLangOpts().Modules)
2066     PriorGeneration = IdentifierGeneration[&II];
2067 
2068   // If there is a global index, look there first to determine which modules
2069   // provably do not have any results for this identifier.
2070   GlobalModuleIndex::HitSet Hits;
2071   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2072   if (!loadGlobalIndex()) {
2073     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2074       HitsPtr = &Hits;
2075     }
2076   }
2077 
2078   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2079                                   NumIdentifierLookups,
2080                                   NumIdentifierLookupHits);
2081   ModuleMgr.visit(Visitor, HitsPtr);
2082   markIdentifierUpToDate(&II);
2083 }
2084 
2085 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2086   if (!II)
2087     return;
2088 
2089   II->setOutOfDate(false);
2090 
2091   // Update the generation for this identifier.
2092   if (getContext().getLangOpts().Modules)
2093     IdentifierGeneration[II] = getGeneration();
2094 }
2095 
2096 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2097                                     const PendingMacroInfo &PMInfo) {
2098   ModuleFile &M = *PMInfo.M;
2099 
2100   BitstreamCursor &Cursor = M.MacroCursor;
2101   SavedStreamPosition SavedPosition(Cursor);
2102   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2103     Error(std::move(Err));
2104     return;
2105   }
2106 
2107   struct ModuleMacroRecord {
2108     SubmoduleID SubModID;
2109     MacroInfo *MI;
2110     SmallVector<SubmoduleID, 8> Overrides;
2111   };
2112   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2113 
2114   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2115   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2116   // macro histroy.
2117   RecordData Record;
2118   while (true) {
2119     Expected<llvm::BitstreamEntry> MaybeEntry =
2120         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2121     if (!MaybeEntry) {
2122       Error(MaybeEntry.takeError());
2123       return;
2124     }
2125     llvm::BitstreamEntry Entry = MaybeEntry.get();
2126 
2127     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2128       Error("malformed block record in AST file");
2129       return;
2130     }
2131 
2132     Record.clear();
2133     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2134     if (!MaybePP) {
2135       Error(MaybePP.takeError());
2136       return;
2137     }
2138     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2139     case PP_MACRO_DIRECTIVE_HISTORY:
2140       break;
2141 
2142     case PP_MODULE_MACRO: {
2143       ModuleMacros.push_back(ModuleMacroRecord());
2144       auto &Info = ModuleMacros.back();
2145       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2146       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2147       for (int I = 2, N = Record.size(); I != N; ++I)
2148         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2149       continue;
2150     }
2151 
2152     default:
2153       Error("malformed block record in AST file");
2154       return;
2155     }
2156 
2157     // We found the macro directive history; that's the last record
2158     // for this macro.
2159     break;
2160   }
2161 
2162   // Module macros are listed in reverse dependency order.
2163   {
2164     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2165     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2166     for (auto &MMR : ModuleMacros) {
2167       Overrides.clear();
2168       for (unsigned ModID : MMR.Overrides) {
2169         Module *Mod = getSubmodule(ModID);
2170         auto *Macro = PP.getModuleMacro(Mod, II);
2171         assert(Macro && "missing definition for overridden macro");
2172         Overrides.push_back(Macro);
2173       }
2174 
2175       bool Inserted = false;
2176       Module *Owner = getSubmodule(MMR.SubModID);
2177       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2178     }
2179   }
2180 
2181   // Don't read the directive history for a module; we don't have anywhere
2182   // to put it.
2183   if (M.isModule())
2184     return;
2185 
2186   // Deserialize the macro directives history in reverse source-order.
2187   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2188   unsigned Idx = 0, N = Record.size();
2189   while (Idx < N) {
2190     MacroDirective *MD = nullptr;
2191     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2192     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2193     switch (K) {
2194     case MacroDirective::MD_Define: {
2195       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2196       MD = PP.AllocateDefMacroDirective(MI, Loc);
2197       break;
2198     }
2199     case MacroDirective::MD_Undefine:
2200       MD = PP.AllocateUndefMacroDirective(Loc);
2201       break;
2202     case MacroDirective::MD_Visibility:
2203       bool isPublic = Record[Idx++];
2204       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2205       break;
2206     }
2207 
2208     if (!Latest)
2209       Latest = MD;
2210     if (Earliest)
2211       Earliest->setPrevious(MD);
2212     Earliest = MD;
2213   }
2214 
2215   if (Latest)
2216     PP.setLoadedMacroDirective(II, Earliest, Latest);
2217 }
2218 
2219 ASTReader::InputFileInfo
2220 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2221   // Go find this input file.
2222   BitstreamCursor &Cursor = F.InputFilesCursor;
2223   SavedStreamPosition SavedPosition(Cursor);
2224   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2225     // FIXME this drops errors on the floor.
2226     consumeError(std::move(Err));
2227   }
2228 
2229   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2230   if (!MaybeCode) {
2231     // FIXME this drops errors on the floor.
2232     consumeError(MaybeCode.takeError());
2233   }
2234   unsigned Code = MaybeCode.get();
2235   RecordData Record;
2236   StringRef Blob;
2237 
2238   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2239     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2240            "invalid record type for input file");
2241   else {
2242     // FIXME this drops errors on the floor.
2243     consumeError(Maybe.takeError());
2244   }
2245 
2246   assert(Record[0] == ID && "Bogus stored ID or offset");
2247   InputFileInfo R;
2248   R.StoredSize = static_cast<off_t>(Record[1]);
2249   R.StoredTime = static_cast<time_t>(Record[2]);
2250   R.Overridden = static_cast<bool>(Record[3]);
2251   R.Transient = static_cast<bool>(Record[4]);
2252   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2253   R.Filename = std::string(Blob);
2254   ResolveImportedPath(F, R.Filename);
2255 
2256   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2257   if (!MaybeEntry) // FIXME this drops errors on the floor.
2258     consumeError(MaybeEntry.takeError());
2259   llvm::BitstreamEntry Entry = MaybeEntry.get();
2260   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2261          "expected record type for input file hash");
2262 
2263   Record.clear();
2264   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2265     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2266            "invalid record type for input file hash");
2267   else {
2268     // FIXME this drops errors on the floor.
2269     consumeError(Maybe.takeError());
2270   }
2271   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2272                   static_cast<uint64_t>(Record[0]);
2273   return R;
2274 }
2275 
2276 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2277 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2278   // If this ID is bogus, just return an empty input file.
2279   if (ID == 0 || ID > F.InputFilesLoaded.size())
2280     return InputFile();
2281 
2282   // If we've already loaded this input file, return it.
2283   if (F.InputFilesLoaded[ID-1].getFile())
2284     return F.InputFilesLoaded[ID-1];
2285 
2286   if (F.InputFilesLoaded[ID-1].isNotFound())
2287     return InputFile();
2288 
2289   // Go find this input file.
2290   BitstreamCursor &Cursor = F.InputFilesCursor;
2291   SavedStreamPosition SavedPosition(Cursor);
2292   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2293     // FIXME this drops errors on the floor.
2294     consumeError(std::move(Err));
2295   }
2296 
2297   InputFileInfo FI = readInputFileInfo(F, ID);
2298   off_t StoredSize = FI.StoredSize;
2299   time_t StoredTime = FI.StoredTime;
2300   bool Overridden = FI.Overridden;
2301   bool Transient = FI.Transient;
2302   StringRef Filename = FI.Filename;
2303   uint64_t StoredContentHash = FI.ContentHash;
2304 
2305   const FileEntry *File = nullptr;
2306   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2307     File = *FE;
2308 
2309   // If we didn't find the file, resolve it relative to the
2310   // original directory from which this AST file was created.
2311   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2312       F.OriginalDir != F.BaseDirectory) {
2313     std::string Resolved = resolveFileRelativeToOriginalDir(
2314         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2315     if (!Resolved.empty())
2316       if (auto FE = FileMgr.getFile(Resolved))
2317         File = *FE;
2318   }
2319 
2320   // For an overridden file, create a virtual file with the stored
2321   // size/timestamp.
2322   if ((Overridden || Transient) && File == nullptr)
2323     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2324 
2325   if (File == nullptr) {
2326     if (Complain) {
2327       std::string ErrorStr = "could not find file '";
2328       ErrorStr += Filename;
2329       ErrorStr += "' referenced by AST file '";
2330       ErrorStr += F.FileName;
2331       ErrorStr += "'";
2332       Error(ErrorStr);
2333     }
2334     // Record that we didn't find the file.
2335     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2336     return InputFile();
2337   }
2338 
2339   // Check if there was a request to override the contents of the file
2340   // that was part of the precompiled header. Overriding such a file
2341   // can lead to problems when lexing using the source locations from the
2342   // PCH.
2343   SourceManager &SM = getSourceManager();
2344   // FIXME: Reject if the overrides are different.
2345   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2346     if (Complain)
2347       Error(diag::err_fe_pch_file_overridden, Filename);
2348 
2349     // After emitting the diagnostic, bypass the overriding file to recover
2350     // (this creates a separate FileEntry).
2351     File = SM.bypassFileContentsOverride(*File);
2352     if (!File) {
2353       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2354       return InputFile();
2355     }
2356   }
2357 
2358   enum ModificationType {
2359     Size,
2360     ModTime,
2361     Content,
2362     None,
2363   };
2364   auto HasInputFileChanged = [&]() {
2365     if (StoredSize != File->getSize())
2366       return ModificationType::Size;
2367     if (!DisableValidation && StoredTime &&
2368         StoredTime != File->getModificationTime()) {
2369       // In case the modification time changes but not the content,
2370       // accept the cached file as legit.
2371       if (ValidateASTInputFilesContent &&
2372           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2373         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2374         if (!MemBuffOrError) {
2375           if (!Complain)
2376             return ModificationType::ModTime;
2377           std::string ErrorStr = "could not get buffer for file '";
2378           ErrorStr += File->getName();
2379           ErrorStr += "'";
2380           Error(ErrorStr);
2381           return ModificationType::ModTime;
2382         }
2383 
2384         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2385         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2386           return ModificationType::None;
2387         return ModificationType::Content;
2388       }
2389       return ModificationType::ModTime;
2390     }
2391     return ModificationType::None;
2392   };
2393 
2394   bool IsOutOfDate = false;
2395   auto FileChange = HasInputFileChanged();
2396   // For an overridden file, there is nothing to validate.
2397   if (!Overridden && FileChange != ModificationType::None) {
2398     if (Complain) {
2399       // Build a list of the PCH imports that got us here (in reverse).
2400       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2401       while (!ImportStack.back()->ImportedBy.empty())
2402         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2403 
2404       // The top-level PCH is stale.
2405       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2406       unsigned DiagnosticKind =
2407           moduleKindForDiagnostic(ImportStack.back()->Kind);
2408       if (DiagnosticKind == 0)
2409         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2410               (unsigned)FileChange);
2411       else if (DiagnosticKind == 1)
2412         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2413               (unsigned)FileChange);
2414       else
2415         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2416               (unsigned)FileChange);
2417 
2418       // Print the import stack.
2419       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2420         Diag(diag::note_pch_required_by)
2421           << Filename << ImportStack[0]->FileName;
2422         for (unsigned I = 1; I < ImportStack.size(); ++I)
2423           Diag(diag::note_pch_required_by)
2424             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2425       }
2426 
2427       if (!Diags.isDiagnosticInFlight())
2428         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2429     }
2430 
2431     IsOutOfDate = true;
2432   }
2433   // FIXME: If the file is overridden and we've already opened it,
2434   // issue an error (or split it into a separate FileEntry).
2435 
2436   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2437 
2438   // Note that we've loaded this input file.
2439   F.InputFilesLoaded[ID-1] = IF;
2440   return IF;
2441 }
2442 
2443 /// If we are loading a relocatable PCH or module file, and the filename
2444 /// is not an absolute path, add the system or module root to the beginning of
2445 /// the file name.
2446 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2447   // Resolve relative to the base directory, if we have one.
2448   if (!M.BaseDirectory.empty())
2449     return ResolveImportedPath(Filename, M.BaseDirectory);
2450 }
2451 
2452 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2453   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2454     return;
2455 
2456   SmallString<128> Buffer;
2457   llvm::sys::path::append(Buffer, Prefix, Filename);
2458   Filename.assign(Buffer.begin(), Buffer.end());
2459 }
2460 
2461 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2462   switch (ARR) {
2463   case ASTReader::Failure: return true;
2464   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2465   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2466   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2467   case ASTReader::ConfigurationMismatch:
2468     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2469   case ASTReader::HadErrors: return true;
2470   case ASTReader::Success: return false;
2471   }
2472 
2473   llvm_unreachable("unknown ASTReadResult");
2474 }
2475 
2476 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2477     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2478     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2479     std::string &SuggestedPredefines) {
2480   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2481     // FIXME this drops errors on the floor.
2482     consumeError(std::move(Err));
2483     return Failure;
2484   }
2485 
2486   // Read all of the records in the options block.
2487   RecordData Record;
2488   ASTReadResult Result = Success;
2489   while (true) {
2490     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2491     if (!MaybeEntry) {
2492       // FIXME this drops errors on the floor.
2493       consumeError(MaybeEntry.takeError());
2494       return Failure;
2495     }
2496     llvm::BitstreamEntry Entry = MaybeEntry.get();
2497 
2498     switch (Entry.Kind) {
2499     case llvm::BitstreamEntry::Error:
2500     case llvm::BitstreamEntry::SubBlock:
2501       return Failure;
2502 
2503     case llvm::BitstreamEntry::EndBlock:
2504       return Result;
2505 
2506     case llvm::BitstreamEntry::Record:
2507       // The interesting case.
2508       break;
2509     }
2510 
2511     // Read and process a record.
2512     Record.clear();
2513     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2514     if (!MaybeRecordType) {
2515       // FIXME this drops errors on the floor.
2516       consumeError(MaybeRecordType.takeError());
2517       return Failure;
2518     }
2519     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2520     case LANGUAGE_OPTIONS: {
2521       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2522       if (ParseLanguageOptions(Record, Complain, Listener,
2523                                AllowCompatibleConfigurationMismatch))
2524         Result = ConfigurationMismatch;
2525       break;
2526     }
2527 
2528     case TARGET_OPTIONS: {
2529       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2530       if (ParseTargetOptions(Record, Complain, Listener,
2531                              AllowCompatibleConfigurationMismatch))
2532         Result = ConfigurationMismatch;
2533       break;
2534     }
2535 
2536     case FILE_SYSTEM_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (!AllowCompatibleConfigurationMismatch &&
2539           ParseFileSystemOptions(Record, Complain, Listener))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case HEADER_SEARCH_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (!AllowCompatibleConfigurationMismatch &&
2547           ParseHeaderSearchOptions(Record, Complain, Listener))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case PREPROCESSOR_OPTIONS:
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParsePreprocessorOptions(Record, Complain, Listener,
2556                                    SuggestedPredefines))
2557         Result = ConfigurationMismatch;
2558       break;
2559     }
2560   }
2561 }
2562 
2563 ASTReader::ASTReadResult
2564 ASTReader::ReadControlBlock(ModuleFile &F,
2565                             SmallVectorImpl<ImportedModule> &Loaded,
2566                             const ModuleFile *ImportedBy,
2567                             unsigned ClientLoadCapabilities) {
2568   BitstreamCursor &Stream = F.Stream;
2569 
2570   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2571     Error(std::move(Err));
2572     return Failure;
2573   }
2574 
2575   // Lambda to read the unhashed control block the first time it's called.
2576   //
2577   // For PCM files, the unhashed control block cannot be read until after the
2578   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2579   // need to look ahead before reading the IMPORTS record.  For consistency,
2580   // this block is always read somehow (see BitstreamEntry::EndBlock).
2581   bool HasReadUnhashedControlBlock = false;
2582   auto readUnhashedControlBlockOnce = [&]() {
2583     if (!HasReadUnhashedControlBlock) {
2584       HasReadUnhashedControlBlock = true;
2585       if (ASTReadResult Result =
2586               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2587         return Result;
2588     }
2589     return Success;
2590   };
2591 
2592   // Read all of the records and blocks in the control block.
2593   RecordData Record;
2594   unsigned NumInputs = 0;
2595   unsigned NumUserInputs = 0;
2596   StringRef BaseDirectoryAsWritten;
2597   while (true) {
2598     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2599     if (!MaybeEntry) {
2600       Error(MaybeEntry.takeError());
2601       return Failure;
2602     }
2603     llvm::BitstreamEntry Entry = MaybeEntry.get();
2604 
2605     switch (Entry.Kind) {
2606     case llvm::BitstreamEntry::Error:
2607       Error("malformed block record in AST file");
2608       return Failure;
2609     case llvm::BitstreamEntry::EndBlock: {
2610       // Validate the module before returning.  This call catches an AST with
2611       // no module name and no imports.
2612       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2613         return Result;
2614 
2615       // Validate input files.
2616       const HeaderSearchOptions &HSOpts =
2617           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2618 
2619       // All user input files reside at the index range [0, NumUserInputs), and
2620       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2621       // loaded module files, ignore missing inputs.
2622       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2623           F.Kind != MK_PrebuiltModule) {
2624         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2625 
2626         // If we are reading a module, we will create a verification timestamp,
2627         // so we verify all input files.  Otherwise, verify only user input
2628         // files.
2629 
2630         unsigned N = NumUserInputs;
2631         if (ValidateSystemInputs ||
2632             (HSOpts.ModulesValidateOncePerBuildSession &&
2633              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2634              F.Kind == MK_ImplicitModule))
2635           N = NumInputs;
2636 
2637         for (unsigned I = 0; I < N; ++I) {
2638           InputFile IF = getInputFile(F, I+1, Complain);
2639           if (!IF.getFile() || IF.isOutOfDate())
2640             return OutOfDate;
2641         }
2642       }
2643 
2644       if (Listener)
2645         Listener->visitModuleFile(F.FileName, F.Kind);
2646 
2647       if (Listener && Listener->needsInputFileVisitation()) {
2648         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2649                                                                 : NumUserInputs;
2650         for (unsigned I = 0; I < N; ++I) {
2651           bool IsSystem = I >= NumUserInputs;
2652           InputFileInfo FI = readInputFileInfo(F, I+1);
2653           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2654                                    F.Kind == MK_ExplicitModule ||
2655                                    F.Kind == MK_PrebuiltModule);
2656         }
2657       }
2658 
2659       return Success;
2660     }
2661 
2662     case llvm::BitstreamEntry::SubBlock:
2663       switch (Entry.ID) {
2664       case INPUT_FILES_BLOCK_ID:
2665         F.InputFilesCursor = Stream;
2666         if (llvm::Error Err = Stream.SkipBlock()) {
2667           Error(std::move(Err));
2668           return Failure;
2669         }
2670         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2671           Error("malformed block record in AST file");
2672           return Failure;
2673         }
2674         continue;
2675 
2676       case OPTIONS_BLOCK_ID:
2677         // If we're reading the first module for this group, check its options
2678         // are compatible with ours. For modules it imports, no further checking
2679         // is required, because we checked them when we built it.
2680         if (Listener && !ImportedBy) {
2681           // Should we allow the configuration of the module file to differ from
2682           // the configuration of the current translation unit in a compatible
2683           // way?
2684           //
2685           // FIXME: Allow this for files explicitly specified with -include-pch.
2686           bool AllowCompatibleConfigurationMismatch =
2687               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2688 
2689           ASTReadResult Result =
2690               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2691                                AllowCompatibleConfigurationMismatch, *Listener,
2692                                SuggestedPredefines);
2693           if (Result == Failure) {
2694             Error("malformed block record in AST file");
2695             return Result;
2696           }
2697 
2698           if (DisableValidation ||
2699               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2700             Result = Success;
2701 
2702           // If we can't load the module, exit early since we likely
2703           // will rebuild the module anyway. The stream may be in the
2704           // middle of a block.
2705           if (Result != Success)
2706             return Result;
2707         } else if (llvm::Error Err = Stream.SkipBlock()) {
2708           Error(std::move(Err));
2709           return Failure;
2710         }
2711         continue;
2712 
2713       default:
2714         if (llvm::Error Err = Stream.SkipBlock()) {
2715           Error(std::move(Err));
2716           return Failure;
2717         }
2718         continue;
2719       }
2720 
2721     case llvm::BitstreamEntry::Record:
2722       // The interesting case.
2723       break;
2724     }
2725 
2726     // Read and process a record.
2727     Record.clear();
2728     StringRef Blob;
2729     Expected<unsigned> MaybeRecordType =
2730         Stream.readRecord(Entry.ID, Record, &Blob);
2731     if (!MaybeRecordType) {
2732       Error(MaybeRecordType.takeError());
2733       return Failure;
2734     }
2735     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2736     case METADATA: {
2737       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2738         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2739           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2740                                         : diag::err_pch_version_too_new);
2741         return VersionMismatch;
2742       }
2743 
2744       bool hasErrors = Record[7];
2745       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2746         Diag(diag::err_pch_with_compiler_errors);
2747         return HadErrors;
2748       }
2749       if (hasErrors) {
2750         Diags.ErrorOccurred = true;
2751         Diags.UncompilableErrorOccurred = true;
2752         Diags.UnrecoverableErrorOccurred = true;
2753       }
2754 
2755       F.RelocatablePCH = Record[4];
2756       // Relative paths in a relocatable PCH are relative to our sysroot.
2757       if (F.RelocatablePCH)
2758         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2759 
2760       F.HasTimestamps = Record[5];
2761 
2762       F.PCHHasObjectFile = Record[6];
2763 
2764       const std::string &CurBranch = getClangFullRepositoryVersion();
2765       StringRef ASTBranch = Blob;
2766       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2767         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2768           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2769         return VersionMismatch;
2770       }
2771       break;
2772     }
2773 
2774     case IMPORTS: {
2775       // Validate the AST before processing any imports (otherwise, untangling
2776       // them can be error-prone and expensive).  A module will have a name and
2777       // will already have been validated, but this catches the PCH case.
2778       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2779         return Result;
2780 
2781       // Load each of the imported PCH files.
2782       unsigned Idx = 0, N = Record.size();
2783       while (Idx < N) {
2784         // Read information about the AST file.
2785         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2786         // The import location will be the local one for now; we will adjust
2787         // all import locations of module imports after the global source
2788         // location info are setup, in ReadAST.
2789         SourceLocation ImportLoc =
2790             ReadUntranslatedSourceLocation(Record[Idx++]);
2791         off_t StoredSize = (off_t)Record[Idx++];
2792         time_t StoredModTime = (time_t)Record[Idx++];
2793         ASTFileSignature StoredSignature = {
2794             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2795               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2796               (uint32_t)Record[Idx++]}}};
2797 
2798         std::string ImportedName = ReadString(Record, Idx);
2799         std::string ImportedFile;
2800 
2801         // For prebuilt and explicit modules first consult the file map for
2802         // an override. Note that here we don't search prebuilt module
2803         // directories, only the explicit name to file mappings. Also, we will
2804         // still verify the size/signature making sure it is essentially the
2805         // same file but perhaps in a different location.
2806         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2807           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2808             ImportedName, /*FileMapOnly*/ true);
2809 
2810         if (ImportedFile.empty())
2811           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2812           // ModuleCache as when writing.
2813           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2814         else
2815           SkipPath(Record, Idx);
2816 
2817         // If our client can't cope with us being out of date, we can't cope with
2818         // our dependency being missing.
2819         unsigned Capabilities = ClientLoadCapabilities;
2820         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2821           Capabilities &= ~ARR_Missing;
2822 
2823         // Load the AST file.
2824         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2825                                   Loaded, StoredSize, StoredModTime,
2826                                   StoredSignature, Capabilities);
2827 
2828         // If we diagnosed a problem, produce a backtrace.
2829         if (isDiagnosedResult(Result, Capabilities))
2830           Diag(diag::note_module_file_imported_by)
2831               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2832 
2833         switch (Result) {
2834         case Failure: return Failure;
2835           // If we have to ignore the dependency, we'll have to ignore this too.
2836         case Missing:
2837         case OutOfDate: return OutOfDate;
2838         case VersionMismatch: return VersionMismatch;
2839         case ConfigurationMismatch: return ConfigurationMismatch;
2840         case HadErrors: return HadErrors;
2841         case Success: break;
2842         }
2843       }
2844       break;
2845     }
2846 
2847     case ORIGINAL_FILE:
2848       F.OriginalSourceFileID = FileID::get(Record[0]);
2849       F.ActualOriginalSourceFileName = std::string(Blob);
2850       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2851       ResolveImportedPath(F, F.OriginalSourceFileName);
2852       break;
2853 
2854     case ORIGINAL_FILE_ID:
2855       F.OriginalSourceFileID = FileID::get(Record[0]);
2856       break;
2857 
2858     case ORIGINAL_PCH_DIR:
2859       F.OriginalDir = std::string(Blob);
2860       break;
2861 
2862     case MODULE_NAME:
2863       F.ModuleName = std::string(Blob);
2864       Diag(diag::remark_module_import)
2865           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2866           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2867       if (Listener)
2868         Listener->ReadModuleName(F.ModuleName);
2869 
2870       // Validate the AST as soon as we have a name so we can exit early on
2871       // failure.
2872       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2873         return Result;
2874 
2875       break;
2876 
2877     case MODULE_DIRECTORY: {
2878       // Save the BaseDirectory as written in the PCM for computing the module
2879       // filename for the ModuleCache.
2880       BaseDirectoryAsWritten = Blob;
2881       assert(!F.ModuleName.empty() &&
2882              "MODULE_DIRECTORY found before MODULE_NAME");
2883       // If we've already loaded a module map file covering this module, we may
2884       // have a better path for it (relative to the current build).
2885       Module *M = PP.getHeaderSearchInfo().lookupModule(
2886           F.ModuleName, /*AllowSearch*/ true,
2887           /*AllowExtraModuleMapSearch*/ true);
2888       if (M && M->Directory) {
2889         // If we're implicitly loading a module, the base directory can't
2890         // change between the build and use.
2891         // Don't emit module relocation error if we have -fno-validate-pch
2892         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2893             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2894           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2895           if (!BuildDir || *BuildDir != M->Directory) {
2896             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2897               Diag(diag::err_imported_module_relocated)
2898                   << F.ModuleName << Blob << M->Directory->getName();
2899             return OutOfDate;
2900           }
2901         }
2902         F.BaseDirectory = std::string(M->Directory->getName());
2903       } else {
2904         F.BaseDirectory = std::string(Blob);
2905       }
2906       break;
2907     }
2908 
2909     case MODULE_MAP_FILE:
2910       if (ASTReadResult Result =
2911               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2912         return Result;
2913       break;
2914 
2915     case INPUT_FILE_OFFSETS:
2916       NumInputs = Record[0];
2917       NumUserInputs = Record[1];
2918       F.InputFileOffsets =
2919           (const llvm::support::unaligned_uint64_t *)Blob.data();
2920       F.InputFilesLoaded.resize(NumInputs);
2921       F.NumUserInputFiles = NumUserInputs;
2922       break;
2923     }
2924   }
2925 }
2926 
2927 ASTReader::ASTReadResult
2928 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2929   BitstreamCursor &Stream = F.Stream;
2930 
2931   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2932     Error(std::move(Err));
2933     return Failure;
2934   }
2935 
2936   // Read all of the records and blocks for the AST file.
2937   RecordData Record;
2938   while (true) {
2939     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2940     if (!MaybeEntry) {
2941       Error(MaybeEntry.takeError());
2942       return Failure;
2943     }
2944     llvm::BitstreamEntry Entry = MaybeEntry.get();
2945 
2946     switch (Entry.Kind) {
2947     case llvm::BitstreamEntry::Error:
2948       Error("error at end of module block in AST file");
2949       return Failure;
2950     case llvm::BitstreamEntry::EndBlock:
2951       // Outside of C++, we do not store a lookup map for the translation unit.
2952       // Instead, mark it as needing a lookup map to be built if this module
2953       // contains any declarations lexically within it (which it always does!).
2954       // This usually has no cost, since we very rarely need the lookup map for
2955       // the translation unit outside C++.
2956       if (ASTContext *Ctx = ContextObj) {
2957         DeclContext *DC = Ctx->getTranslationUnitDecl();
2958         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2959           DC->setMustBuildLookupTable();
2960       }
2961 
2962       return Success;
2963     case llvm::BitstreamEntry::SubBlock:
2964       switch (Entry.ID) {
2965       case DECLTYPES_BLOCK_ID:
2966         // We lazily load the decls block, but we want to set up the
2967         // DeclsCursor cursor to point into it.  Clone our current bitcode
2968         // cursor to it, enter the block and read the abbrevs in that block.
2969         // With the main cursor, we just skip over it.
2970         F.DeclsCursor = Stream;
2971         if (llvm::Error Err = Stream.SkipBlock()) {
2972           Error(std::move(Err));
2973           return Failure;
2974         }
2975         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2976           Error("malformed block record in AST file");
2977           return Failure;
2978         }
2979         break;
2980 
2981       case PREPROCESSOR_BLOCK_ID:
2982         F.MacroCursor = Stream;
2983         if (!PP.getExternalSource())
2984           PP.setExternalSource(this);
2985 
2986         if (llvm::Error Err = Stream.SkipBlock()) {
2987           Error(std::move(Err));
2988           return Failure;
2989         }
2990         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2991           Error("malformed block record in AST file");
2992           return Failure;
2993         }
2994         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2995         break;
2996 
2997       case PREPROCESSOR_DETAIL_BLOCK_ID:
2998         F.PreprocessorDetailCursor = Stream;
2999 
3000         if (llvm::Error Err = Stream.SkipBlock()) {
3001           Error(std::move(Err));
3002           return Failure;
3003         }
3004         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3005                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3006           Error("malformed preprocessor detail record in AST file");
3007           return Failure;
3008         }
3009         F.PreprocessorDetailStartOffset
3010         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3011 
3012         if (!PP.getPreprocessingRecord())
3013           PP.createPreprocessingRecord();
3014         if (!PP.getPreprocessingRecord()->getExternalSource())
3015           PP.getPreprocessingRecord()->SetExternalSource(*this);
3016         break;
3017 
3018       case SOURCE_MANAGER_BLOCK_ID:
3019         if (ReadSourceManagerBlock(F))
3020           return Failure;
3021         break;
3022 
3023       case SUBMODULE_BLOCK_ID:
3024         if (ASTReadResult Result =
3025                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3026           return Result;
3027         break;
3028 
3029       case COMMENTS_BLOCK_ID: {
3030         BitstreamCursor C = Stream;
3031 
3032         if (llvm::Error Err = Stream.SkipBlock()) {
3033           Error(std::move(Err));
3034           return Failure;
3035         }
3036         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3037           Error("malformed comments block in AST file");
3038           return Failure;
3039         }
3040         CommentsCursors.push_back(std::make_pair(C, &F));
3041         break;
3042       }
3043 
3044       default:
3045         if (llvm::Error Err = Stream.SkipBlock()) {
3046           Error(std::move(Err));
3047           return Failure;
3048         }
3049         break;
3050       }
3051       continue;
3052 
3053     case llvm::BitstreamEntry::Record:
3054       // The interesting case.
3055       break;
3056     }
3057 
3058     // Read and process a record.
3059     Record.clear();
3060     StringRef Blob;
3061     Expected<unsigned> MaybeRecordType =
3062         Stream.readRecord(Entry.ID, Record, &Blob);
3063     if (!MaybeRecordType) {
3064       Error(MaybeRecordType.takeError());
3065       return Failure;
3066     }
3067     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3068 
3069     // If we're not loading an AST context, we don't care about most records.
3070     if (!ContextObj) {
3071       switch (RecordType) {
3072       case IDENTIFIER_TABLE:
3073       case IDENTIFIER_OFFSET:
3074       case INTERESTING_IDENTIFIERS:
3075       case STATISTICS:
3076       case PP_CONDITIONAL_STACK:
3077       case PP_COUNTER_VALUE:
3078       case SOURCE_LOCATION_OFFSETS:
3079       case MODULE_OFFSET_MAP:
3080       case SOURCE_MANAGER_LINE_TABLE:
3081       case SOURCE_LOCATION_PRELOADS:
3082       case PPD_ENTITIES_OFFSETS:
3083       case HEADER_SEARCH_TABLE:
3084       case IMPORTED_MODULES:
3085       case MACRO_OFFSET:
3086         break;
3087       default:
3088         continue;
3089       }
3090     }
3091 
3092     switch (RecordType) {
3093     default:  // Default behavior: ignore.
3094       break;
3095 
3096     case TYPE_OFFSET: {
3097       if (F.LocalNumTypes != 0) {
3098         Error("duplicate TYPE_OFFSET record in AST file");
3099         return Failure;
3100       }
3101       F.TypeOffsets = (const uint32_t *)Blob.data();
3102       F.LocalNumTypes = Record[0];
3103       unsigned LocalBaseTypeIndex = Record[1];
3104       F.BaseTypeIndex = getTotalNumTypes();
3105 
3106       if (F.LocalNumTypes > 0) {
3107         // Introduce the global -> local mapping for types within this module.
3108         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3109 
3110         // Introduce the local -> global mapping for types within this module.
3111         F.TypeRemap.insertOrReplace(
3112           std::make_pair(LocalBaseTypeIndex,
3113                          F.BaseTypeIndex - LocalBaseTypeIndex));
3114 
3115         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3116       }
3117       break;
3118     }
3119 
3120     case DECL_OFFSET: {
3121       if (F.LocalNumDecls != 0) {
3122         Error("duplicate DECL_OFFSET record in AST file");
3123         return Failure;
3124       }
3125       F.DeclOffsets = (const DeclOffset *)Blob.data();
3126       F.LocalNumDecls = Record[0];
3127       unsigned LocalBaseDeclID = Record[1];
3128       F.BaseDeclID = getTotalNumDecls();
3129 
3130       if (F.LocalNumDecls > 0) {
3131         // Introduce the global -> local mapping for declarations within this
3132         // module.
3133         GlobalDeclMap.insert(
3134           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3135 
3136         // Introduce the local -> global mapping for declarations within this
3137         // module.
3138         F.DeclRemap.insertOrReplace(
3139           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3140 
3141         // Introduce the global -> local mapping for declarations within this
3142         // module.
3143         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3144 
3145         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3146       }
3147       break;
3148     }
3149 
3150     case TU_UPDATE_LEXICAL: {
3151       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3152       LexicalContents Contents(
3153           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3154               Blob.data()),
3155           static_cast<unsigned int>(Blob.size() / 4));
3156       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3157       TU->setHasExternalLexicalStorage(true);
3158       break;
3159     }
3160 
3161     case UPDATE_VISIBLE: {
3162       unsigned Idx = 0;
3163       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3164       auto *Data = (const unsigned char*)Blob.data();
3165       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3166       // If we've already loaded the decl, perform the updates when we finish
3167       // loading this block.
3168       if (Decl *D = GetExistingDecl(ID))
3169         PendingUpdateRecords.push_back(
3170             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3171       break;
3172     }
3173 
3174     case IDENTIFIER_TABLE:
3175       F.IdentifierTableData = Blob.data();
3176       if (Record[0]) {
3177         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3178             (const unsigned char *)F.IdentifierTableData + Record[0],
3179             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3180             (const unsigned char *)F.IdentifierTableData,
3181             ASTIdentifierLookupTrait(*this, F));
3182 
3183         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3184       }
3185       break;
3186 
3187     case IDENTIFIER_OFFSET: {
3188       if (F.LocalNumIdentifiers != 0) {
3189         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3190         return Failure;
3191       }
3192       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3193       F.LocalNumIdentifiers = Record[0];
3194       unsigned LocalBaseIdentifierID = Record[1];
3195       F.BaseIdentifierID = getTotalNumIdentifiers();
3196 
3197       if (F.LocalNumIdentifiers > 0) {
3198         // Introduce the global -> local mapping for identifiers within this
3199         // module.
3200         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3201                                                   &F));
3202 
3203         // Introduce the local -> global mapping for identifiers within this
3204         // module.
3205         F.IdentifierRemap.insertOrReplace(
3206           std::make_pair(LocalBaseIdentifierID,
3207                          F.BaseIdentifierID - LocalBaseIdentifierID));
3208 
3209         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3210                                  + F.LocalNumIdentifiers);
3211       }
3212       break;
3213     }
3214 
3215     case INTERESTING_IDENTIFIERS:
3216       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3217       break;
3218 
3219     case EAGERLY_DESERIALIZED_DECLS:
3220       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3221       // about "interesting" decls (for instance, if we're building a module).
3222       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3223         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3224       break;
3225 
3226     case MODULAR_CODEGEN_DECLS:
3227       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3228       // them (ie: if we're not codegenerating this module).
3229       if (F.Kind == MK_MainFile)
3230         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3231           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3232       break;
3233 
3234     case SPECIAL_TYPES:
3235       if (SpecialTypes.empty()) {
3236         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3237           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3238         break;
3239       }
3240 
3241       if (SpecialTypes.size() != Record.size()) {
3242         Error("invalid special-types record");
3243         return Failure;
3244       }
3245 
3246       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3247         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3248         if (!SpecialTypes[I])
3249           SpecialTypes[I] = ID;
3250         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3251         // merge step?
3252       }
3253       break;
3254 
3255     case STATISTICS:
3256       TotalNumStatements += Record[0];
3257       TotalNumMacros += Record[1];
3258       TotalLexicalDeclContexts += Record[2];
3259       TotalVisibleDeclContexts += Record[3];
3260       break;
3261 
3262     case UNUSED_FILESCOPED_DECLS:
3263       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3264         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3265       break;
3266 
3267     case DELEGATING_CTORS:
3268       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3269         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3270       break;
3271 
3272     case WEAK_UNDECLARED_IDENTIFIERS:
3273       if (Record.size() % 4 != 0) {
3274         Error("invalid weak identifiers record");
3275         return Failure;
3276       }
3277 
3278       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3279       // files. This isn't the way to do it :)
3280       WeakUndeclaredIdentifiers.clear();
3281 
3282       // Translate the weak, undeclared identifiers into global IDs.
3283       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3284         WeakUndeclaredIdentifiers.push_back(
3285           getGlobalIdentifierID(F, Record[I++]));
3286         WeakUndeclaredIdentifiers.push_back(
3287           getGlobalIdentifierID(F, Record[I++]));
3288         WeakUndeclaredIdentifiers.push_back(
3289           ReadSourceLocation(F, Record, I).getRawEncoding());
3290         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3291       }
3292       break;
3293 
3294     case SELECTOR_OFFSETS: {
3295       F.SelectorOffsets = (const uint32_t *)Blob.data();
3296       F.LocalNumSelectors = Record[0];
3297       unsigned LocalBaseSelectorID = Record[1];
3298       F.BaseSelectorID = getTotalNumSelectors();
3299 
3300       if (F.LocalNumSelectors > 0) {
3301         // Introduce the global -> local mapping for selectors within this
3302         // module.
3303         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3304 
3305         // Introduce the local -> global mapping for selectors within this
3306         // module.
3307         F.SelectorRemap.insertOrReplace(
3308           std::make_pair(LocalBaseSelectorID,
3309                          F.BaseSelectorID - LocalBaseSelectorID));
3310 
3311         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3312       }
3313       break;
3314     }
3315 
3316     case METHOD_POOL:
3317       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3318       if (Record[0])
3319         F.SelectorLookupTable
3320           = ASTSelectorLookupTable::Create(
3321                         F.SelectorLookupTableData + Record[0],
3322                         F.SelectorLookupTableData,
3323                         ASTSelectorLookupTrait(*this, F));
3324       TotalNumMethodPoolEntries += Record[1];
3325       break;
3326 
3327     case REFERENCED_SELECTOR_POOL:
3328       if (!Record.empty()) {
3329         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3330           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3331                                                                 Record[Idx++]));
3332           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3333                                               getRawEncoding());
3334         }
3335       }
3336       break;
3337 
3338     case PP_CONDITIONAL_STACK:
3339       if (!Record.empty()) {
3340         unsigned Idx = 0, End = Record.size() - 1;
3341         bool ReachedEOFWhileSkipping = Record[Idx++];
3342         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3343         if (ReachedEOFWhileSkipping) {
3344           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3345           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3346           bool FoundNonSkipPortion = Record[Idx++];
3347           bool FoundElse = Record[Idx++];
3348           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3349           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3350                            FoundElse, ElseLoc);
3351         }
3352         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3353         while (Idx < End) {
3354           auto Loc = ReadSourceLocation(F, Record, Idx);
3355           bool WasSkipping = Record[Idx++];
3356           bool FoundNonSkip = Record[Idx++];
3357           bool FoundElse = Record[Idx++];
3358           ConditionalStack.push_back(
3359               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3360         }
3361         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3362       }
3363       break;
3364 
3365     case PP_COUNTER_VALUE:
3366       if (!Record.empty() && Listener)
3367         Listener->ReadCounter(F, Record[0]);
3368       break;
3369 
3370     case FILE_SORTED_DECLS:
3371       F.FileSortedDecls = (const DeclID *)Blob.data();
3372       F.NumFileSortedDecls = Record[0];
3373       break;
3374 
3375     case SOURCE_LOCATION_OFFSETS: {
3376       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3377       F.LocalNumSLocEntries = Record[0];
3378       unsigned SLocSpaceSize = Record[1];
3379       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3380           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3381                                               SLocSpaceSize);
3382       if (!F.SLocEntryBaseID) {
3383         Error("ran out of source locations");
3384         break;
3385       }
3386       // Make our entry in the range map. BaseID is negative and growing, so
3387       // we invert it. Because we invert it, though, we need the other end of
3388       // the range.
3389       unsigned RangeStart =
3390           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3391       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3392       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3393 
3394       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3395       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3396       GlobalSLocOffsetMap.insert(
3397           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3398                            - SLocSpaceSize,&F));
3399 
3400       // Initialize the remapping table.
3401       // Invalid stays invalid.
3402       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3403       // This module. Base was 2 when being compiled.
3404       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3405                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3406 
3407       TotalNumSLocEntries += F.LocalNumSLocEntries;
3408       break;
3409     }
3410 
3411     case MODULE_OFFSET_MAP:
3412       F.ModuleOffsetMap = Blob;
3413       break;
3414 
3415     case SOURCE_MANAGER_LINE_TABLE:
3416       if (ParseLineTable(F, Record)) {
3417         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3418         return Failure;
3419       }
3420       break;
3421 
3422     case SOURCE_LOCATION_PRELOADS: {
3423       // Need to transform from the local view (1-based IDs) to the global view,
3424       // which is based off F.SLocEntryBaseID.
3425       if (!F.PreloadSLocEntries.empty()) {
3426         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3427         return Failure;
3428       }
3429 
3430       F.PreloadSLocEntries.swap(Record);
3431       break;
3432     }
3433 
3434     case EXT_VECTOR_DECLS:
3435       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3436         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3437       break;
3438 
3439     case VTABLE_USES:
3440       if (Record.size() % 3 != 0) {
3441         Error("Invalid VTABLE_USES record");
3442         return Failure;
3443       }
3444 
3445       // Later tables overwrite earlier ones.
3446       // FIXME: Modules will have some trouble with this. This is clearly not
3447       // the right way to do this.
3448       VTableUses.clear();
3449 
3450       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3451         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3452         VTableUses.push_back(
3453           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3454         VTableUses.push_back(Record[Idx++]);
3455       }
3456       break;
3457 
3458     case PENDING_IMPLICIT_INSTANTIATIONS:
3459       if (PendingInstantiations.size() % 2 != 0) {
3460         Error("Invalid existing PendingInstantiations");
3461         return Failure;
3462       }
3463 
3464       if (Record.size() % 2 != 0) {
3465         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3466         return Failure;
3467       }
3468 
3469       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3470         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3471         PendingInstantiations.push_back(
3472           ReadSourceLocation(F, Record, I).getRawEncoding());
3473       }
3474       break;
3475 
3476     case SEMA_DECL_REFS:
3477       if (Record.size() != 3) {
3478         Error("Invalid SEMA_DECL_REFS block");
3479         return Failure;
3480       }
3481       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3482         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3483       break;
3484 
3485     case PPD_ENTITIES_OFFSETS: {
3486       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3487       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3488       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3489 
3490       unsigned LocalBasePreprocessedEntityID = Record[0];
3491 
3492       unsigned StartingID;
3493       if (!PP.getPreprocessingRecord())
3494         PP.createPreprocessingRecord();
3495       if (!PP.getPreprocessingRecord()->getExternalSource())
3496         PP.getPreprocessingRecord()->SetExternalSource(*this);
3497       StartingID
3498         = PP.getPreprocessingRecord()
3499             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3500       F.BasePreprocessedEntityID = StartingID;
3501 
3502       if (F.NumPreprocessedEntities > 0) {
3503         // Introduce the global -> local mapping for preprocessed entities in
3504         // this module.
3505         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3506 
3507         // Introduce the local -> global mapping for preprocessed entities in
3508         // this module.
3509         F.PreprocessedEntityRemap.insertOrReplace(
3510           std::make_pair(LocalBasePreprocessedEntityID,
3511             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3512       }
3513 
3514       break;
3515     }
3516 
3517     case PPD_SKIPPED_RANGES: {
3518       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3519       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3520       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3521 
3522       if (!PP.getPreprocessingRecord())
3523         PP.createPreprocessingRecord();
3524       if (!PP.getPreprocessingRecord()->getExternalSource())
3525         PP.getPreprocessingRecord()->SetExternalSource(*this);
3526       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3527           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3528 
3529       if (F.NumPreprocessedSkippedRanges > 0)
3530         GlobalSkippedRangeMap.insert(
3531             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3532       break;
3533     }
3534 
3535     case DECL_UPDATE_OFFSETS:
3536       if (Record.size() % 2 != 0) {
3537         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3538         return Failure;
3539       }
3540       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3541         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3542         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3543 
3544         // If we've already loaded the decl, perform the updates when we finish
3545         // loading this block.
3546         if (Decl *D = GetExistingDecl(ID))
3547           PendingUpdateRecords.push_back(
3548               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3549       }
3550       break;
3551 
3552     case OBJC_CATEGORIES_MAP:
3553       if (F.LocalNumObjCCategoriesInMap != 0) {
3554         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3555         return Failure;
3556       }
3557 
3558       F.LocalNumObjCCategoriesInMap = Record[0];
3559       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3560       break;
3561 
3562     case OBJC_CATEGORIES:
3563       F.ObjCCategories.swap(Record);
3564       break;
3565 
3566     case CUDA_SPECIAL_DECL_REFS:
3567       // Later tables overwrite earlier ones.
3568       // FIXME: Modules will have trouble with this.
3569       CUDASpecialDeclRefs.clear();
3570       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3571         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3572       break;
3573 
3574     case HEADER_SEARCH_TABLE:
3575       F.HeaderFileInfoTableData = Blob.data();
3576       F.LocalNumHeaderFileInfos = Record[1];
3577       if (Record[0]) {
3578         F.HeaderFileInfoTable
3579           = HeaderFileInfoLookupTable::Create(
3580                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3581                    (const unsigned char *)F.HeaderFileInfoTableData,
3582                    HeaderFileInfoTrait(*this, F,
3583                                        &PP.getHeaderSearchInfo(),
3584                                        Blob.data() + Record[2]));
3585 
3586         PP.getHeaderSearchInfo().SetExternalSource(this);
3587         if (!PP.getHeaderSearchInfo().getExternalLookup())
3588           PP.getHeaderSearchInfo().SetExternalLookup(this);
3589       }
3590       break;
3591 
3592     case FP_PRAGMA_OPTIONS:
3593       // Later tables overwrite earlier ones.
3594       FPPragmaOptions.swap(Record);
3595       break;
3596 
3597     case OPENCL_EXTENSIONS:
3598       for (unsigned I = 0, E = Record.size(); I != E; ) {
3599         auto Name = ReadString(Record, I);
3600         auto &Opt = OpenCLExtensions.OptMap[Name];
3601         Opt.Supported = Record[I++] != 0;
3602         Opt.Enabled = Record[I++] != 0;
3603         Opt.Avail = Record[I++];
3604         Opt.Core = Record[I++];
3605       }
3606       break;
3607 
3608     case OPENCL_EXTENSION_TYPES:
3609       for (unsigned I = 0, E = Record.size(); I != E;) {
3610         auto TypeID = static_cast<::TypeID>(Record[I++]);
3611         auto *Type = GetType(TypeID).getTypePtr();
3612         auto NumExt = static_cast<unsigned>(Record[I++]);
3613         for (unsigned II = 0; II != NumExt; ++II) {
3614           auto Ext = ReadString(Record, I);
3615           OpenCLTypeExtMap[Type].insert(Ext);
3616         }
3617       }
3618       break;
3619 
3620     case OPENCL_EXTENSION_DECLS:
3621       for (unsigned I = 0, E = Record.size(); I != E;) {
3622         auto DeclID = static_cast<::DeclID>(Record[I++]);
3623         auto *Decl = GetDecl(DeclID);
3624         auto NumExt = static_cast<unsigned>(Record[I++]);
3625         for (unsigned II = 0; II != NumExt; ++II) {
3626           auto Ext = ReadString(Record, I);
3627           OpenCLDeclExtMap[Decl].insert(Ext);
3628         }
3629       }
3630       break;
3631 
3632     case TENTATIVE_DEFINITIONS:
3633       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3634         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3635       break;
3636 
3637     case KNOWN_NAMESPACES:
3638       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3639         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3640       break;
3641 
3642     case UNDEFINED_BUT_USED:
3643       if (UndefinedButUsed.size() % 2 != 0) {
3644         Error("Invalid existing UndefinedButUsed");
3645         return Failure;
3646       }
3647 
3648       if (Record.size() % 2 != 0) {
3649         Error("invalid undefined-but-used record");
3650         return Failure;
3651       }
3652       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3653         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3654         UndefinedButUsed.push_back(
3655             ReadSourceLocation(F, Record, I).getRawEncoding());
3656       }
3657       break;
3658 
3659     case DELETE_EXPRS_TO_ANALYZE:
3660       for (unsigned I = 0, N = Record.size(); I != N;) {
3661         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3662         const uint64_t Count = Record[I++];
3663         DelayedDeleteExprs.push_back(Count);
3664         for (uint64_t C = 0; C < Count; ++C) {
3665           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3666           bool IsArrayForm = Record[I++] == 1;
3667           DelayedDeleteExprs.push_back(IsArrayForm);
3668         }
3669       }
3670       break;
3671 
3672     case IMPORTED_MODULES:
3673       if (!F.isModule()) {
3674         // If we aren't loading a module (which has its own exports), make
3675         // all of the imported modules visible.
3676         // FIXME: Deal with macros-only imports.
3677         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3678           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3679           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3680           if (GlobalID) {
3681             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3682             if (DeserializationListener)
3683               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3684           }
3685         }
3686       }
3687       break;
3688 
3689     case MACRO_OFFSET: {
3690       if (F.LocalNumMacros != 0) {
3691         Error("duplicate MACRO_OFFSET record in AST file");
3692         return Failure;
3693       }
3694       F.MacroOffsets = (const uint32_t *)Blob.data();
3695       F.LocalNumMacros = Record[0];
3696       unsigned LocalBaseMacroID = Record[1];
3697       F.BaseMacroID = getTotalNumMacros();
3698 
3699       if (F.LocalNumMacros > 0) {
3700         // Introduce the global -> local mapping for macros within this module.
3701         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3702 
3703         // Introduce the local -> global mapping for macros within this module.
3704         F.MacroRemap.insertOrReplace(
3705           std::make_pair(LocalBaseMacroID,
3706                          F.BaseMacroID - LocalBaseMacroID));
3707 
3708         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3709       }
3710       break;
3711     }
3712 
3713     case LATE_PARSED_TEMPLATE:
3714       LateParsedTemplates.append(Record.begin(), Record.end());
3715       break;
3716 
3717     case OPTIMIZE_PRAGMA_OPTIONS:
3718       if (Record.size() != 1) {
3719         Error("invalid pragma optimize record");
3720         return Failure;
3721       }
3722       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3723       break;
3724 
3725     case MSSTRUCT_PRAGMA_OPTIONS:
3726       if (Record.size() != 1) {
3727         Error("invalid pragma ms_struct record");
3728         return Failure;
3729       }
3730       PragmaMSStructState = Record[0];
3731       break;
3732 
3733     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3734       if (Record.size() != 2) {
3735         Error("invalid pragma ms_struct record");
3736         return Failure;
3737       }
3738       PragmaMSPointersToMembersState = Record[0];
3739       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3740       break;
3741 
3742     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3743       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3744         UnusedLocalTypedefNameCandidates.push_back(
3745             getGlobalDeclID(F, Record[I]));
3746       break;
3747 
3748     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3749       if (Record.size() != 1) {
3750         Error("invalid cuda pragma options record");
3751         return Failure;
3752       }
3753       ForceCUDAHostDeviceDepth = Record[0];
3754       break;
3755 
3756     case PACK_PRAGMA_OPTIONS: {
3757       if (Record.size() < 3) {
3758         Error("invalid pragma pack record");
3759         return Failure;
3760       }
3761       PragmaPackCurrentValue = Record[0];
3762       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3763       unsigned NumStackEntries = Record[2];
3764       unsigned Idx = 3;
3765       // Reset the stack when importing a new module.
3766       PragmaPackStack.clear();
3767       for (unsigned I = 0; I < NumStackEntries; ++I) {
3768         PragmaPackStackEntry Entry;
3769         Entry.Value = Record[Idx++];
3770         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3771         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3772         PragmaPackStrings.push_back(ReadString(Record, Idx));
3773         Entry.SlotLabel = PragmaPackStrings.back();
3774         PragmaPackStack.push_back(Entry);
3775       }
3776       break;
3777     }
3778 
3779     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3780       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3781         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3782       break;
3783     }
3784   }
3785 }
3786 
3787 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3788   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3789 
3790   // Additional remapping information.
3791   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3792   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3793   F.ModuleOffsetMap = StringRef();
3794 
3795   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3796   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3797     F.SLocRemap.insert(std::make_pair(0U, 0));
3798     F.SLocRemap.insert(std::make_pair(2U, 1));
3799   }
3800 
3801   // Continuous range maps we may be updating in our module.
3802   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3803   RemapBuilder SLocRemap(F.SLocRemap);
3804   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3805   RemapBuilder MacroRemap(F.MacroRemap);
3806   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3807   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3808   RemapBuilder SelectorRemap(F.SelectorRemap);
3809   RemapBuilder DeclRemap(F.DeclRemap);
3810   RemapBuilder TypeRemap(F.TypeRemap);
3811 
3812   while (Data < DataEnd) {
3813     // FIXME: Looking up dependency modules by filename is horrible. Let's
3814     // start fixing this with prebuilt and explicit modules and see how it
3815     // goes...
3816     using namespace llvm::support;
3817     ModuleKind Kind = static_cast<ModuleKind>(
3818       endian::readNext<uint8_t, little, unaligned>(Data));
3819     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3820     StringRef Name = StringRef((const char*)Data, Len);
3821     Data += Len;
3822     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3823                       ? ModuleMgr.lookupByModuleName(Name)
3824                       : ModuleMgr.lookupByFileName(Name));
3825     if (!OM) {
3826       std::string Msg =
3827           "SourceLocation remap refers to unknown module, cannot find ";
3828       Msg.append(std::string(Name));
3829       Error(Msg);
3830       return;
3831     }
3832 
3833     uint32_t SLocOffset =
3834         endian::readNext<uint32_t, little, unaligned>(Data);
3835     uint32_t IdentifierIDOffset =
3836         endian::readNext<uint32_t, little, unaligned>(Data);
3837     uint32_t MacroIDOffset =
3838         endian::readNext<uint32_t, little, unaligned>(Data);
3839     uint32_t PreprocessedEntityIDOffset =
3840         endian::readNext<uint32_t, little, unaligned>(Data);
3841     uint32_t SubmoduleIDOffset =
3842         endian::readNext<uint32_t, little, unaligned>(Data);
3843     uint32_t SelectorIDOffset =
3844         endian::readNext<uint32_t, little, unaligned>(Data);
3845     uint32_t DeclIDOffset =
3846         endian::readNext<uint32_t, little, unaligned>(Data);
3847     uint32_t TypeIndexOffset =
3848         endian::readNext<uint32_t, little, unaligned>(Data);
3849 
3850     uint32_t None = std::numeric_limits<uint32_t>::max();
3851 
3852     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3853                          RemapBuilder &Remap) {
3854       if (Offset != None)
3855         Remap.insert(std::make_pair(Offset,
3856                                     static_cast<int>(BaseOffset - Offset)));
3857     };
3858     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3859     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3860     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3861     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3862               PreprocessedEntityRemap);
3863     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3864     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3865     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3866     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3867 
3868     // Global -> local mappings.
3869     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3870   }
3871 }
3872 
3873 ASTReader::ASTReadResult
3874 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3875                                   const ModuleFile *ImportedBy,
3876                                   unsigned ClientLoadCapabilities) {
3877   unsigned Idx = 0;
3878   F.ModuleMapPath = ReadPath(F, Record, Idx);
3879 
3880   // Try to resolve ModuleName in the current header search context and
3881   // verify that it is found in the same module map file as we saved. If the
3882   // top-level AST file is a main file, skip this check because there is no
3883   // usable header search context.
3884   assert(!F.ModuleName.empty() &&
3885          "MODULE_NAME should come before MODULE_MAP_FILE");
3886   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3887     // An implicitly-loaded module file should have its module listed in some
3888     // module map file that we've already loaded.
3889     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3890     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3891     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3892     // Don't emit module relocation error if we have -fno-validate-pch
3893     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3894       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3895         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3896           // This module was defined by an imported (explicit) module.
3897           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3898                                                << ASTFE->getName();
3899         } else {
3900           // This module was built with a different module map.
3901           Diag(diag::err_imported_module_not_found)
3902               << F.ModuleName << F.FileName
3903               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3904               << !ImportedBy;
3905           // In case it was imported by a PCH, there's a chance the user is
3906           // just missing to include the search path to the directory containing
3907           // the modulemap.
3908           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3909             Diag(diag::note_imported_by_pch_module_not_found)
3910                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3911         }
3912       }
3913       return OutOfDate;
3914     }
3915 
3916     assert(M->Name == F.ModuleName && "found module with different name");
3917 
3918     // Check the primary module map file.
3919     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3920     if (!StoredModMap || *StoredModMap != ModMap) {
3921       assert(ModMap && "found module is missing module map file");
3922       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3923              "top-level import should be verified");
3924       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3925       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3926         Diag(diag::err_imported_module_modmap_changed)
3927             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3928             << ModMap->getName() << F.ModuleMapPath << NotImported;
3929       return OutOfDate;
3930     }
3931 
3932     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3933     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3934       // FIXME: we should use input files rather than storing names.
3935       std::string Filename = ReadPath(F, Record, Idx);
3936       auto F = FileMgr.getFile(Filename, false, false);
3937       if (!F) {
3938         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3939           Error("could not find file '" + Filename +"' referenced by AST file");
3940         return OutOfDate;
3941       }
3942       AdditionalStoredMaps.insert(*F);
3943     }
3944 
3945     // Check any additional module map files (e.g. module.private.modulemap)
3946     // that are not in the pcm.
3947     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3948       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3949         // Remove files that match
3950         // Note: SmallPtrSet::erase is really remove
3951         if (!AdditionalStoredMaps.erase(ModMap)) {
3952           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3953             Diag(diag::err_module_different_modmap)
3954               << F.ModuleName << /*new*/0 << ModMap->getName();
3955           return OutOfDate;
3956         }
3957       }
3958     }
3959 
3960     // Check any additional module map files that are in the pcm, but not
3961     // found in header search. Cases that match are already removed.
3962     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3963       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3964         Diag(diag::err_module_different_modmap)
3965           << F.ModuleName << /*not new*/1 << ModMap->getName();
3966       return OutOfDate;
3967     }
3968   }
3969 
3970   if (Listener)
3971     Listener->ReadModuleMapFile(F.ModuleMapPath);
3972   return Success;
3973 }
3974 
3975 /// Move the given method to the back of the global list of methods.
3976 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3977   // Find the entry for this selector in the method pool.
3978   Sema::GlobalMethodPool::iterator Known
3979     = S.MethodPool.find(Method->getSelector());
3980   if (Known == S.MethodPool.end())
3981     return;
3982 
3983   // Retrieve the appropriate method list.
3984   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3985                                                     : Known->second.second;
3986   bool Found = false;
3987   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3988     if (!Found) {
3989       if (List->getMethod() == Method) {
3990         Found = true;
3991       } else {
3992         // Keep searching.
3993         continue;
3994       }
3995     }
3996 
3997     if (List->getNext())
3998       List->setMethod(List->getNext()->getMethod());
3999     else
4000       List->setMethod(Method);
4001   }
4002 }
4003 
4004 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4005   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4006   for (Decl *D : Names) {
4007     bool wasHidden = D->isHidden();
4008     D->setVisibleDespiteOwningModule();
4009 
4010     if (wasHidden && SemaObj) {
4011       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4012         moveMethodToBackOfGlobalList(*SemaObj, Method);
4013       }
4014     }
4015   }
4016 }
4017 
4018 void ASTReader::makeModuleVisible(Module *Mod,
4019                                   Module::NameVisibilityKind NameVisibility,
4020                                   SourceLocation ImportLoc) {
4021   llvm::SmallPtrSet<Module *, 4> Visited;
4022   SmallVector<Module *, 4> Stack;
4023   Stack.push_back(Mod);
4024   while (!Stack.empty()) {
4025     Mod = Stack.pop_back_val();
4026 
4027     if (NameVisibility <= Mod->NameVisibility) {
4028       // This module already has this level of visibility (or greater), so
4029       // there is nothing more to do.
4030       continue;
4031     }
4032 
4033     if (!Mod->isAvailable()) {
4034       // Modules that aren't available cannot be made visible.
4035       continue;
4036     }
4037 
4038     // Update the module's name visibility.
4039     Mod->NameVisibility = NameVisibility;
4040 
4041     // If we've already deserialized any names from this module,
4042     // mark them as visible.
4043     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4044     if (Hidden != HiddenNamesMap.end()) {
4045       auto HiddenNames = std::move(*Hidden);
4046       HiddenNamesMap.erase(Hidden);
4047       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4048       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4049              "making names visible added hidden names");
4050     }
4051 
4052     // Push any exported modules onto the stack to be marked as visible.
4053     SmallVector<Module *, 16> Exports;
4054     Mod->getExportedModules(Exports);
4055     for (SmallVectorImpl<Module *>::iterator
4056            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4057       Module *Exported = *I;
4058       if (Visited.insert(Exported).second)
4059         Stack.push_back(Exported);
4060     }
4061   }
4062 }
4063 
4064 /// We've merged the definition \p MergedDef into the existing definition
4065 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4066 /// visible.
4067 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4068                                           NamedDecl *MergedDef) {
4069   if (Def->isHidden()) {
4070     // If MergedDef is visible or becomes visible, make the definition visible.
4071     if (!MergedDef->isHidden())
4072       Def->setVisibleDespiteOwningModule();
4073     else {
4074       getContext().mergeDefinitionIntoModule(
4075           Def, MergedDef->getImportedOwningModule(),
4076           /*NotifyListeners*/ false);
4077       PendingMergedDefinitionsToDeduplicate.insert(Def);
4078     }
4079   }
4080 }
4081 
4082 bool ASTReader::loadGlobalIndex() {
4083   if (GlobalIndex)
4084     return false;
4085 
4086   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4087       !PP.getLangOpts().Modules)
4088     return true;
4089 
4090   // Try to load the global index.
4091   TriedLoadingGlobalIndex = true;
4092   StringRef ModuleCachePath
4093     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4094   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4095       GlobalModuleIndex::readIndex(ModuleCachePath);
4096   if (llvm::Error Err = std::move(Result.second)) {
4097     assert(!Result.first);
4098     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4099     return true;
4100   }
4101 
4102   GlobalIndex.reset(Result.first);
4103   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4104   return false;
4105 }
4106 
4107 bool ASTReader::isGlobalIndexUnavailable() const {
4108   return PP.getLangOpts().Modules && UseGlobalIndex &&
4109          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4110 }
4111 
4112 static void updateModuleTimestamp(ModuleFile &MF) {
4113   // Overwrite the timestamp file contents so that file's mtime changes.
4114   std::string TimestampFilename = MF.getTimestampFilename();
4115   std::error_code EC;
4116   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4117   if (EC)
4118     return;
4119   OS << "Timestamp file\n";
4120   OS.close();
4121   OS.clear_error(); // Avoid triggering a fatal error.
4122 }
4123 
4124 /// Given a cursor at the start of an AST file, scan ahead and drop the
4125 /// cursor into the start of the given block ID, returning false on success and
4126 /// true on failure.
4127 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4128   while (true) {
4129     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4130     if (!MaybeEntry) {
4131       // FIXME this drops errors on the floor.
4132       consumeError(MaybeEntry.takeError());
4133       return true;
4134     }
4135     llvm::BitstreamEntry Entry = MaybeEntry.get();
4136 
4137     switch (Entry.Kind) {
4138     case llvm::BitstreamEntry::Error:
4139     case llvm::BitstreamEntry::EndBlock:
4140       return true;
4141 
4142     case llvm::BitstreamEntry::Record:
4143       // Ignore top-level records.
4144       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4145         break;
4146       else {
4147         // FIXME this drops errors on the floor.
4148         consumeError(Skipped.takeError());
4149         return true;
4150       }
4151 
4152     case llvm::BitstreamEntry::SubBlock:
4153       if (Entry.ID == BlockID) {
4154         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4155           // FIXME this drops the error on the floor.
4156           consumeError(std::move(Err));
4157           return true;
4158         }
4159         // Found it!
4160         return false;
4161       }
4162 
4163       if (llvm::Error Err = Cursor.SkipBlock()) {
4164         // FIXME this drops the error on the floor.
4165         consumeError(std::move(Err));
4166         return true;
4167       }
4168     }
4169   }
4170 }
4171 
4172 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4173                                             ModuleKind Type,
4174                                             SourceLocation ImportLoc,
4175                                             unsigned ClientLoadCapabilities,
4176                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4177   llvm::SaveAndRestore<SourceLocation>
4178     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4179 
4180   // Defer any pending actions until we get to the end of reading the AST file.
4181   Deserializing AnASTFile(this);
4182 
4183   // Bump the generation number.
4184   unsigned PreviousGeneration = 0;
4185   if (ContextObj)
4186     PreviousGeneration = incrementGeneration(*ContextObj);
4187 
4188   unsigned NumModules = ModuleMgr.size();
4189   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4190     assert(ReadResult && "expected to return error");
4191     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4192                             PP.getLangOpts().Modules
4193                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4194                                 : nullptr);
4195 
4196     // If we find that any modules are unusable, the global index is going
4197     // to be out-of-date. Just remove it.
4198     GlobalIndex.reset();
4199     ModuleMgr.setGlobalIndex(nullptr);
4200     return ReadResult;
4201   };
4202 
4203   SmallVector<ImportedModule, 4> Loaded;
4204   switch (ASTReadResult ReadResult =
4205               ReadASTCore(FileName, Type, ImportLoc,
4206                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4207                           ASTFileSignature(), ClientLoadCapabilities)) {
4208   case Failure:
4209   case Missing:
4210   case OutOfDate:
4211   case VersionMismatch:
4212   case ConfigurationMismatch:
4213   case HadErrors:
4214     return removeModulesAndReturn(ReadResult);
4215   case Success:
4216     break;
4217   }
4218 
4219   // Here comes stuff that we only do once the entire chain is loaded.
4220 
4221   // Load the AST blocks of all of the modules that we loaded.  We can still
4222   // hit errors parsing the ASTs at this point.
4223   for (ImportedModule &M : Loaded) {
4224     ModuleFile &F = *M.Mod;
4225 
4226     // Read the AST block.
4227     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4228       return removeModulesAndReturn(Result);
4229 
4230     // The AST block should always have a definition for the main module.
4231     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4232       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4233       return removeModulesAndReturn(Failure);
4234     }
4235 
4236     // Read the extension blocks.
4237     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4238       if (ASTReadResult Result = ReadExtensionBlock(F))
4239         return removeModulesAndReturn(Result);
4240     }
4241 
4242     // Once read, set the ModuleFile bit base offset and update the size in
4243     // bits of all files we've seen.
4244     F.GlobalBitOffset = TotalModulesSizeInBits;
4245     TotalModulesSizeInBits += F.SizeInBits;
4246     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4247   }
4248 
4249   // Preload source locations and interesting indentifiers.
4250   for (ImportedModule &M : Loaded) {
4251     ModuleFile &F = *M.Mod;
4252 
4253     // Preload SLocEntries.
4254     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4255       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4256       // Load it through the SourceManager and don't call ReadSLocEntry()
4257       // directly because the entry may have already been loaded in which case
4258       // calling ReadSLocEntry() directly would trigger an assertion in
4259       // SourceManager.
4260       SourceMgr.getLoadedSLocEntryByID(Index);
4261     }
4262 
4263     // Map the original source file ID into the ID space of the current
4264     // compilation.
4265     if (F.OriginalSourceFileID.isValid()) {
4266       F.OriginalSourceFileID = FileID::get(
4267           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4268     }
4269 
4270     // Preload all the pending interesting identifiers by marking them out of
4271     // date.
4272     for (auto Offset : F.PreloadIdentifierOffsets) {
4273       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4274           F.IdentifierTableData + Offset);
4275 
4276       ASTIdentifierLookupTrait Trait(*this, F);
4277       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4278       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4279       auto &II = PP.getIdentifierTable().getOwn(Key);
4280       II.setOutOfDate(true);
4281 
4282       // Mark this identifier as being from an AST file so that we can track
4283       // whether we need to serialize it.
4284       markIdentifierFromAST(*this, II);
4285 
4286       // Associate the ID with the identifier so that the writer can reuse it.
4287       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4288       SetIdentifierInfo(ID, &II);
4289     }
4290   }
4291 
4292   // Setup the import locations and notify the module manager that we've
4293   // committed to these module files.
4294   for (ImportedModule &M : Loaded) {
4295     ModuleFile &F = *M.Mod;
4296 
4297     ModuleMgr.moduleFileAccepted(&F);
4298 
4299     // Set the import location.
4300     F.DirectImportLoc = ImportLoc;
4301     // FIXME: We assume that locations from PCH / preamble do not need
4302     // any translation.
4303     if (!M.ImportedBy)
4304       F.ImportLoc = M.ImportLoc;
4305     else
4306       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4307   }
4308 
4309   if (!PP.getLangOpts().CPlusPlus ||
4310       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4311        Type != MK_PrebuiltModule)) {
4312     // Mark all of the identifiers in the identifier table as being out of date,
4313     // so that various accessors know to check the loaded modules when the
4314     // identifier is used.
4315     //
4316     // For C++ modules, we don't need information on many identifiers (just
4317     // those that provide macros or are poisoned), so we mark all of
4318     // the interesting ones via PreloadIdentifierOffsets.
4319     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4320                                 IdEnd = PP.getIdentifierTable().end();
4321          Id != IdEnd; ++Id)
4322       Id->second->setOutOfDate(true);
4323   }
4324   // Mark selectors as out of date.
4325   for (auto Sel : SelectorGeneration)
4326     SelectorOutOfDate[Sel.first] = true;
4327 
4328   // Resolve any unresolved module exports.
4329   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4330     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4331     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4332     Module *ResolvedMod = getSubmodule(GlobalID);
4333 
4334     switch (Unresolved.Kind) {
4335     case UnresolvedModuleRef::Conflict:
4336       if (ResolvedMod) {
4337         Module::Conflict Conflict;
4338         Conflict.Other = ResolvedMod;
4339         Conflict.Message = Unresolved.String.str();
4340         Unresolved.Mod->Conflicts.push_back(Conflict);
4341       }
4342       continue;
4343 
4344     case UnresolvedModuleRef::Import:
4345       if (ResolvedMod)
4346         Unresolved.Mod->Imports.insert(ResolvedMod);
4347       continue;
4348 
4349     case UnresolvedModuleRef::Export:
4350       if (ResolvedMod || Unresolved.IsWildcard)
4351         Unresolved.Mod->Exports.push_back(
4352           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4353       continue;
4354     }
4355   }
4356   UnresolvedModuleRefs.clear();
4357 
4358   if (Imported)
4359     Imported->append(ImportedModules.begin(),
4360                      ImportedModules.end());
4361 
4362   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4363   // Might be unnecessary as use declarations are only used to build the
4364   // module itself.
4365 
4366   if (ContextObj)
4367     InitializeContext();
4368 
4369   if (SemaObj)
4370     UpdateSema();
4371 
4372   if (DeserializationListener)
4373     DeserializationListener->ReaderInitialized(this);
4374 
4375   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4376   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4377     // If this AST file is a precompiled preamble, then set the
4378     // preamble file ID of the source manager to the file source file
4379     // from which the preamble was built.
4380     if (Type == MK_Preamble) {
4381       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4382     } else if (Type == MK_MainFile) {
4383       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4384     }
4385   }
4386 
4387   // For any Objective-C class definitions we have already loaded, make sure
4388   // that we load any additional categories.
4389   if (ContextObj) {
4390     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4391       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4392                          ObjCClassesLoaded[I],
4393                          PreviousGeneration);
4394     }
4395   }
4396 
4397   if (PP.getHeaderSearchInfo()
4398           .getHeaderSearchOpts()
4399           .ModulesValidateOncePerBuildSession) {
4400     // Now we are certain that the module and all modules it depends on are
4401     // up to date.  Create or update timestamp files for modules that are
4402     // located in the module cache (not for PCH files that could be anywhere
4403     // in the filesystem).
4404     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4405       ImportedModule &M = Loaded[I];
4406       if (M.Mod->Kind == MK_ImplicitModule) {
4407         updateModuleTimestamp(*M.Mod);
4408       }
4409     }
4410   }
4411 
4412   return Success;
4413 }
4414 
4415 static ASTFileSignature readASTFileSignature(StringRef PCH);
4416 
4417 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4418 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4419   // FIXME checking magic headers is done in other places such as
4420   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4421   // always done the same. Unify it all with a helper.
4422   if (!Stream.canSkipToPos(4))
4423     return llvm::createStringError(std::errc::illegal_byte_sequence,
4424                                    "file too small to contain AST file magic");
4425   for (unsigned C : {'C', 'P', 'C', 'H'})
4426     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4427       if (Res.get() != C)
4428         return llvm::createStringError(
4429             std::errc::illegal_byte_sequence,
4430             "file doesn't start with AST file magic");
4431     } else
4432       return Res.takeError();
4433   return llvm::Error::success();
4434 }
4435 
4436 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4437   switch (Kind) {
4438   case MK_PCH:
4439     return 0; // PCH
4440   case MK_ImplicitModule:
4441   case MK_ExplicitModule:
4442   case MK_PrebuiltModule:
4443     return 1; // module
4444   case MK_MainFile:
4445   case MK_Preamble:
4446     return 2; // main source file
4447   }
4448   llvm_unreachable("unknown module kind");
4449 }
4450 
4451 ASTReader::ASTReadResult
4452 ASTReader::ReadASTCore(StringRef FileName,
4453                        ModuleKind Type,
4454                        SourceLocation ImportLoc,
4455                        ModuleFile *ImportedBy,
4456                        SmallVectorImpl<ImportedModule> &Loaded,
4457                        off_t ExpectedSize, time_t ExpectedModTime,
4458                        ASTFileSignature ExpectedSignature,
4459                        unsigned ClientLoadCapabilities) {
4460   ModuleFile *M;
4461   std::string ErrorStr;
4462   ModuleManager::AddModuleResult AddResult
4463     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4464                           getGeneration(), ExpectedSize, ExpectedModTime,
4465                           ExpectedSignature, readASTFileSignature,
4466                           M, ErrorStr);
4467 
4468   switch (AddResult) {
4469   case ModuleManager::AlreadyLoaded:
4470     Diag(diag::remark_module_import)
4471         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4472         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4473     return Success;
4474 
4475   case ModuleManager::NewlyLoaded:
4476     // Load module file below.
4477     break;
4478 
4479   case ModuleManager::Missing:
4480     // The module file was missing; if the client can handle that, return
4481     // it.
4482     if (ClientLoadCapabilities & ARR_Missing)
4483       return Missing;
4484 
4485     // Otherwise, return an error.
4486     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4487                                           << FileName << !ErrorStr.empty()
4488                                           << ErrorStr;
4489     return Failure;
4490 
4491   case ModuleManager::OutOfDate:
4492     // We couldn't load the module file because it is out-of-date. If the
4493     // client can handle out-of-date, return it.
4494     if (ClientLoadCapabilities & ARR_OutOfDate)
4495       return OutOfDate;
4496 
4497     // Otherwise, return an error.
4498     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4499                                             << FileName << !ErrorStr.empty()
4500                                             << ErrorStr;
4501     return Failure;
4502   }
4503 
4504   assert(M && "Missing module file");
4505 
4506   bool ShouldFinalizePCM = false;
4507   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4508     auto &MC = getModuleManager().getModuleCache();
4509     if (ShouldFinalizePCM)
4510       MC.finalizePCM(FileName);
4511     else
4512       MC.tryToDropPCM(FileName);
4513   });
4514   ModuleFile &F = *M;
4515   BitstreamCursor &Stream = F.Stream;
4516   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4517   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4518 
4519   // Sniff for the signature.
4520   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4521     Diag(diag::err_module_file_invalid)
4522         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4523     return Failure;
4524   }
4525 
4526   // This is used for compatibility with older PCH formats.
4527   bool HaveReadControlBlock = false;
4528   while (true) {
4529     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4530     if (!MaybeEntry) {
4531       Error(MaybeEntry.takeError());
4532       return Failure;
4533     }
4534     llvm::BitstreamEntry Entry = MaybeEntry.get();
4535 
4536     switch (Entry.Kind) {
4537     case llvm::BitstreamEntry::Error:
4538     case llvm::BitstreamEntry::Record:
4539     case llvm::BitstreamEntry::EndBlock:
4540       Error("invalid record at top-level of AST file");
4541       return Failure;
4542 
4543     case llvm::BitstreamEntry::SubBlock:
4544       break;
4545     }
4546 
4547     switch (Entry.ID) {
4548     case CONTROL_BLOCK_ID:
4549       HaveReadControlBlock = true;
4550       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4551       case Success:
4552         // Check that we didn't try to load a non-module AST file as a module.
4553         //
4554         // FIXME: Should we also perform the converse check? Loading a module as
4555         // a PCH file sort of works, but it's a bit wonky.
4556         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4557              Type == MK_PrebuiltModule) &&
4558             F.ModuleName.empty()) {
4559           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4560           if (Result != OutOfDate ||
4561               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4562             Diag(diag::err_module_file_not_module) << FileName;
4563           return Result;
4564         }
4565         break;
4566 
4567       case Failure: return Failure;
4568       case Missing: return Missing;
4569       case OutOfDate: return OutOfDate;
4570       case VersionMismatch: return VersionMismatch;
4571       case ConfigurationMismatch: return ConfigurationMismatch;
4572       case HadErrors: return HadErrors;
4573       }
4574       break;
4575 
4576     case AST_BLOCK_ID:
4577       if (!HaveReadControlBlock) {
4578         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4579           Diag(diag::err_pch_version_too_old);
4580         return VersionMismatch;
4581       }
4582 
4583       // Record that we've loaded this module.
4584       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4585       ShouldFinalizePCM = true;
4586       return Success;
4587 
4588     case UNHASHED_CONTROL_BLOCK_ID:
4589       // This block is handled using look-ahead during ReadControlBlock.  We
4590       // shouldn't get here!
4591       Error("malformed block record in AST file");
4592       return Failure;
4593 
4594     default:
4595       if (llvm::Error Err = Stream.SkipBlock()) {
4596         Error(std::move(Err));
4597         return Failure;
4598       }
4599       break;
4600     }
4601   }
4602 
4603   llvm_unreachable("unexpected break; expected return");
4604 }
4605 
4606 ASTReader::ASTReadResult
4607 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4608                                     unsigned ClientLoadCapabilities) {
4609   const HeaderSearchOptions &HSOpts =
4610       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4611   bool AllowCompatibleConfigurationMismatch =
4612       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4613 
4614   ASTReadResult Result = readUnhashedControlBlockImpl(
4615       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4616       Listener.get(),
4617       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4618 
4619   // If F was directly imported by another module, it's implicitly validated by
4620   // the importing module.
4621   if (DisableValidation || WasImportedBy ||
4622       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4623     return Success;
4624 
4625   if (Result == Failure) {
4626     Error("malformed block record in AST file");
4627     return Failure;
4628   }
4629 
4630   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4631     // If this module has already been finalized in the ModuleCache, we're stuck
4632     // with it; we can only load a single version of each module.
4633     //
4634     // This can happen when a module is imported in two contexts: in one, as a
4635     // user module; in another, as a system module (due to an import from
4636     // another module marked with the [system] flag).  It usually indicates a
4637     // bug in the module map: this module should also be marked with [system].
4638     //
4639     // If -Wno-system-headers (the default), and the first import is as a
4640     // system module, then validation will fail during the as-user import,
4641     // since -Werror flags won't have been validated.  However, it's reasonable
4642     // to treat this consistently as a system module.
4643     //
4644     // If -Wsystem-headers, the PCM on disk was built with
4645     // -Wno-system-headers, and the first import is as a user module, then
4646     // validation will fail during the as-system import since the PCM on disk
4647     // doesn't guarantee that -Werror was respected.  However, the -Werror
4648     // flags were checked during the initial as-user import.
4649     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4650       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4651       return Success;
4652     }
4653   }
4654 
4655   return Result;
4656 }
4657 
4658 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4659     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4660     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4661     bool ValidateDiagnosticOptions) {
4662   // Initialize a stream.
4663   BitstreamCursor Stream(StreamData);
4664 
4665   // Sniff for the signature.
4666   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4667     // FIXME this drops the error on the floor.
4668     consumeError(std::move(Err));
4669     return Failure;
4670   }
4671 
4672   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4673   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4674     return Failure;
4675 
4676   // Read all of the records in the options block.
4677   RecordData Record;
4678   ASTReadResult Result = Success;
4679   while (true) {
4680     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4681     if (!MaybeEntry) {
4682       // FIXME this drops the error on the floor.
4683       consumeError(MaybeEntry.takeError());
4684       return Failure;
4685     }
4686     llvm::BitstreamEntry Entry = MaybeEntry.get();
4687 
4688     switch (Entry.Kind) {
4689     case llvm::BitstreamEntry::Error:
4690     case llvm::BitstreamEntry::SubBlock:
4691       return Failure;
4692 
4693     case llvm::BitstreamEntry::EndBlock:
4694       return Result;
4695 
4696     case llvm::BitstreamEntry::Record:
4697       // The interesting case.
4698       break;
4699     }
4700 
4701     // Read and process a record.
4702     Record.clear();
4703     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4704     if (!MaybeRecordType) {
4705       // FIXME this drops the error.
4706       return Failure;
4707     }
4708     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4709     case SIGNATURE:
4710       if (F)
4711         std::copy(Record.begin(), Record.end(), F->Signature.data());
4712       break;
4713     case DIAGNOSTIC_OPTIONS: {
4714       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4715       if (Listener && ValidateDiagnosticOptions &&
4716           !AllowCompatibleConfigurationMismatch &&
4717           ParseDiagnosticOptions(Record, Complain, *Listener))
4718         Result = OutOfDate; // Don't return early.  Read the signature.
4719       break;
4720     }
4721     case DIAG_PRAGMA_MAPPINGS:
4722       if (!F)
4723         break;
4724       if (F->PragmaDiagMappings.empty())
4725         F->PragmaDiagMappings.swap(Record);
4726       else
4727         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4728                                      Record.begin(), Record.end());
4729       break;
4730     }
4731   }
4732 }
4733 
4734 /// Parse a record and blob containing module file extension metadata.
4735 static bool parseModuleFileExtensionMetadata(
4736               const SmallVectorImpl<uint64_t> &Record,
4737               StringRef Blob,
4738               ModuleFileExtensionMetadata &Metadata) {
4739   if (Record.size() < 4) return true;
4740 
4741   Metadata.MajorVersion = Record[0];
4742   Metadata.MinorVersion = Record[1];
4743 
4744   unsigned BlockNameLen = Record[2];
4745   unsigned UserInfoLen = Record[3];
4746 
4747   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4748 
4749   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4750   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4751                                   Blob.data() + BlockNameLen + UserInfoLen);
4752   return false;
4753 }
4754 
4755 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4756   BitstreamCursor &Stream = F.Stream;
4757 
4758   RecordData Record;
4759   while (true) {
4760     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4761     if (!MaybeEntry) {
4762       Error(MaybeEntry.takeError());
4763       return Failure;
4764     }
4765     llvm::BitstreamEntry Entry = MaybeEntry.get();
4766 
4767     switch (Entry.Kind) {
4768     case llvm::BitstreamEntry::SubBlock:
4769       if (llvm::Error Err = Stream.SkipBlock()) {
4770         Error(std::move(Err));
4771         return Failure;
4772       }
4773       continue;
4774 
4775     case llvm::BitstreamEntry::EndBlock:
4776       return Success;
4777 
4778     case llvm::BitstreamEntry::Error:
4779       return HadErrors;
4780 
4781     case llvm::BitstreamEntry::Record:
4782       break;
4783     }
4784 
4785     Record.clear();
4786     StringRef Blob;
4787     Expected<unsigned> MaybeRecCode =
4788         Stream.readRecord(Entry.ID, Record, &Blob);
4789     if (!MaybeRecCode) {
4790       Error(MaybeRecCode.takeError());
4791       return Failure;
4792     }
4793     switch (MaybeRecCode.get()) {
4794     case EXTENSION_METADATA: {
4795       ModuleFileExtensionMetadata Metadata;
4796       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4797         Error("malformed EXTENSION_METADATA in AST file");
4798         return Failure;
4799       }
4800 
4801       // Find a module file extension with this block name.
4802       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4803       if (Known == ModuleFileExtensions.end()) break;
4804 
4805       // Form a reader.
4806       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4807                                                              F, Stream)) {
4808         F.ExtensionReaders.push_back(std::move(Reader));
4809       }
4810 
4811       break;
4812     }
4813     }
4814   }
4815 
4816   return Success;
4817 }
4818 
4819 void ASTReader::InitializeContext() {
4820   assert(ContextObj && "no context to initialize");
4821   ASTContext &Context = *ContextObj;
4822 
4823   // If there's a listener, notify them that we "read" the translation unit.
4824   if (DeserializationListener)
4825     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4826                                       Context.getTranslationUnitDecl());
4827 
4828   // FIXME: Find a better way to deal with collisions between these
4829   // built-in types. Right now, we just ignore the problem.
4830 
4831   // Load the special types.
4832   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4833     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4834       if (!Context.CFConstantStringTypeDecl)
4835         Context.setCFConstantStringType(GetType(String));
4836     }
4837 
4838     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4839       QualType FileType = GetType(File);
4840       if (FileType.isNull()) {
4841         Error("FILE type is NULL");
4842         return;
4843       }
4844 
4845       if (!Context.FILEDecl) {
4846         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4847           Context.setFILEDecl(Typedef->getDecl());
4848         else {
4849           const TagType *Tag = FileType->getAs<TagType>();
4850           if (!Tag) {
4851             Error("Invalid FILE type in AST file");
4852             return;
4853           }
4854           Context.setFILEDecl(Tag->getDecl());
4855         }
4856       }
4857     }
4858 
4859     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4860       QualType Jmp_bufType = GetType(Jmp_buf);
4861       if (Jmp_bufType.isNull()) {
4862         Error("jmp_buf type is NULL");
4863         return;
4864       }
4865 
4866       if (!Context.jmp_bufDecl) {
4867         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4868           Context.setjmp_bufDecl(Typedef->getDecl());
4869         else {
4870           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4871           if (!Tag) {
4872             Error("Invalid jmp_buf type in AST file");
4873             return;
4874           }
4875           Context.setjmp_bufDecl(Tag->getDecl());
4876         }
4877       }
4878     }
4879 
4880     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4881       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4882       if (Sigjmp_bufType.isNull()) {
4883         Error("sigjmp_buf type is NULL");
4884         return;
4885       }
4886 
4887       if (!Context.sigjmp_bufDecl) {
4888         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4889           Context.setsigjmp_bufDecl(Typedef->getDecl());
4890         else {
4891           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4892           assert(Tag && "Invalid sigjmp_buf type in AST file");
4893           Context.setsigjmp_bufDecl(Tag->getDecl());
4894         }
4895       }
4896     }
4897 
4898     if (unsigned ObjCIdRedef
4899           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4900       if (Context.ObjCIdRedefinitionType.isNull())
4901         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4902     }
4903 
4904     if (unsigned ObjCClassRedef
4905           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4906       if (Context.ObjCClassRedefinitionType.isNull())
4907         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4908     }
4909 
4910     if (unsigned ObjCSelRedef
4911           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4912       if (Context.ObjCSelRedefinitionType.isNull())
4913         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4914     }
4915 
4916     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4917       QualType Ucontext_tType = GetType(Ucontext_t);
4918       if (Ucontext_tType.isNull()) {
4919         Error("ucontext_t type is NULL");
4920         return;
4921       }
4922 
4923       if (!Context.ucontext_tDecl) {
4924         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4925           Context.setucontext_tDecl(Typedef->getDecl());
4926         else {
4927           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4928           assert(Tag && "Invalid ucontext_t type in AST file");
4929           Context.setucontext_tDecl(Tag->getDecl());
4930         }
4931       }
4932     }
4933   }
4934 
4935   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4936 
4937   // If there were any CUDA special declarations, deserialize them.
4938   if (!CUDASpecialDeclRefs.empty()) {
4939     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4940     Context.setcudaConfigureCallDecl(
4941                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4942   }
4943 
4944   // Re-export any modules that were imported by a non-module AST file.
4945   // FIXME: This does not make macro-only imports visible again.
4946   for (auto &Import : ImportedModules) {
4947     if (Module *Imported = getSubmodule(Import.ID)) {
4948       makeModuleVisible(Imported, Module::AllVisible,
4949                         /*ImportLoc=*/Import.ImportLoc);
4950       if (Import.ImportLoc.isValid())
4951         PP.makeModuleVisible(Imported, Import.ImportLoc);
4952       // FIXME: should we tell Sema to make the module visible too?
4953     }
4954   }
4955   ImportedModules.clear();
4956 }
4957 
4958 void ASTReader::finalizeForWriting() {
4959   // Nothing to do for now.
4960 }
4961 
4962 /// Reads and return the signature record from \p PCH's control block, or
4963 /// else returns 0.
4964 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4965   BitstreamCursor Stream(PCH);
4966   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4967     // FIXME this drops the error on the floor.
4968     consumeError(std::move(Err));
4969     return ASTFileSignature();
4970   }
4971 
4972   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4973   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4974     return ASTFileSignature();
4975 
4976   // Scan for SIGNATURE inside the diagnostic options block.
4977   ASTReader::RecordData Record;
4978   while (true) {
4979     Expected<llvm::BitstreamEntry> MaybeEntry =
4980         Stream.advanceSkippingSubblocks();
4981     if (!MaybeEntry) {
4982       // FIXME this drops the error on the floor.
4983       consumeError(MaybeEntry.takeError());
4984       return ASTFileSignature();
4985     }
4986     llvm::BitstreamEntry Entry = MaybeEntry.get();
4987 
4988     if (Entry.Kind != llvm::BitstreamEntry::Record)
4989       return ASTFileSignature();
4990 
4991     Record.clear();
4992     StringRef Blob;
4993     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4994     if (!MaybeRecord) {
4995       // FIXME this drops the error on the floor.
4996       consumeError(MaybeRecord.takeError());
4997       return ASTFileSignature();
4998     }
4999     if (SIGNATURE == MaybeRecord.get())
5000       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
5001                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
5002   }
5003 }
5004 
5005 /// Retrieve the name of the original source file name
5006 /// directly from the AST file, without actually loading the AST
5007 /// file.
5008 std::string ASTReader::getOriginalSourceFile(
5009     const std::string &ASTFileName, FileManager &FileMgr,
5010     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5011   // Open the AST file.
5012   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5013   if (!Buffer) {
5014     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5015         << ASTFileName << Buffer.getError().message();
5016     return std::string();
5017   }
5018 
5019   // Initialize the stream
5020   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5021 
5022   // Sniff for the signature.
5023   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5024     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5025     return std::string();
5026   }
5027 
5028   // Scan for the CONTROL_BLOCK_ID block.
5029   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5030     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5031     return std::string();
5032   }
5033 
5034   // Scan for ORIGINAL_FILE inside the control block.
5035   RecordData Record;
5036   while (true) {
5037     Expected<llvm::BitstreamEntry> MaybeEntry =
5038         Stream.advanceSkippingSubblocks();
5039     if (!MaybeEntry) {
5040       // FIXME this drops errors on the floor.
5041       consumeError(MaybeEntry.takeError());
5042       return std::string();
5043     }
5044     llvm::BitstreamEntry Entry = MaybeEntry.get();
5045 
5046     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5047       return std::string();
5048 
5049     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5050       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5051       return std::string();
5052     }
5053 
5054     Record.clear();
5055     StringRef Blob;
5056     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5057     if (!MaybeRecord) {
5058       // FIXME this drops the errors on the floor.
5059       consumeError(MaybeRecord.takeError());
5060       return std::string();
5061     }
5062     if (ORIGINAL_FILE == MaybeRecord.get())
5063       return Blob.str();
5064   }
5065 }
5066 
5067 namespace {
5068 
5069   class SimplePCHValidator : public ASTReaderListener {
5070     const LangOptions &ExistingLangOpts;
5071     const TargetOptions &ExistingTargetOpts;
5072     const PreprocessorOptions &ExistingPPOpts;
5073     std::string ExistingModuleCachePath;
5074     FileManager &FileMgr;
5075 
5076   public:
5077     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5078                        const TargetOptions &ExistingTargetOpts,
5079                        const PreprocessorOptions &ExistingPPOpts,
5080                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5081         : ExistingLangOpts(ExistingLangOpts),
5082           ExistingTargetOpts(ExistingTargetOpts),
5083           ExistingPPOpts(ExistingPPOpts),
5084           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5085 
5086     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5087                              bool AllowCompatibleDifferences) override {
5088       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5089                                   AllowCompatibleDifferences);
5090     }
5091 
5092     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5093                            bool AllowCompatibleDifferences) override {
5094       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5095                                 AllowCompatibleDifferences);
5096     }
5097 
5098     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5099                                  StringRef SpecificModuleCachePath,
5100                                  bool Complain) override {
5101       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5102                                       ExistingModuleCachePath,
5103                                       nullptr, ExistingLangOpts);
5104     }
5105 
5106     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5107                                  bool Complain,
5108                                  std::string &SuggestedPredefines) override {
5109       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5110                                       SuggestedPredefines, ExistingLangOpts);
5111     }
5112   };
5113 
5114 } // namespace
5115 
5116 bool ASTReader::readASTFileControlBlock(
5117     StringRef Filename, FileManager &FileMgr,
5118     const PCHContainerReader &PCHContainerRdr,
5119     bool FindModuleFileExtensions,
5120     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5121   // Open the AST file.
5122   // FIXME: This allows use of the VFS; we do not allow use of the
5123   // VFS when actually loading a module.
5124   auto Buffer = FileMgr.getBufferForFile(Filename);
5125   if (!Buffer) {
5126     return true;
5127   }
5128 
5129   // Initialize the stream
5130   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5131   BitstreamCursor Stream(Bytes);
5132 
5133   // Sniff for the signature.
5134   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5135     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5136     return true;
5137   }
5138 
5139   // Scan for the CONTROL_BLOCK_ID block.
5140   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5141     return true;
5142 
5143   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5144   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5145   bool NeedsImports = Listener.needsImportVisitation();
5146   BitstreamCursor InputFilesCursor;
5147 
5148   RecordData Record;
5149   std::string ModuleDir;
5150   bool DoneWithControlBlock = false;
5151   while (!DoneWithControlBlock) {
5152     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5153     if (!MaybeEntry) {
5154       // FIXME this drops the error on the floor.
5155       consumeError(MaybeEntry.takeError());
5156       return true;
5157     }
5158     llvm::BitstreamEntry Entry = MaybeEntry.get();
5159 
5160     switch (Entry.Kind) {
5161     case llvm::BitstreamEntry::SubBlock: {
5162       switch (Entry.ID) {
5163       case OPTIONS_BLOCK_ID: {
5164         std::string IgnoredSuggestedPredefines;
5165         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5166                              /*AllowCompatibleConfigurationMismatch*/ false,
5167                              Listener, IgnoredSuggestedPredefines) != Success)
5168           return true;
5169         break;
5170       }
5171 
5172       case INPUT_FILES_BLOCK_ID:
5173         InputFilesCursor = Stream;
5174         if (llvm::Error Err = Stream.SkipBlock()) {
5175           // FIXME this drops the error on the floor.
5176           consumeError(std::move(Err));
5177           return true;
5178         }
5179         if (NeedsInputFiles &&
5180             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5181           return true;
5182         break;
5183 
5184       default:
5185         if (llvm::Error Err = Stream.SkipBlock()) {
5186           // FIXME this drops the error on the floor.
5187           consumeError(std::move(Err));
5188           return true;
5189         }
5190         break;
5191       }
5192 
5193       continue;
5194     }
5195 
5196     case llvm::BitstreamEntry::EndBlock:
5197       DoneWithControlBlock = true;
5198       break;
5199 
5200     case llvm::BitstreamEntry::Error:
5201       return true;
5202 
5203     case llvm::BitstreamEntry::Record:
5204       break;
5205     }
5206 
5207     if (DoneWithControlBlock) break;
5208 
5209     Record.clear();
5210     StringRef Blob;
5211     Expected<unsigned> MaybeRecCode =
5212         Stream.readRecord(Entry.ID, Record, &Blob);
5213     if (!MaybeRecCode) {
5214       // FIXME this drops the error.
5215       return Failure;
5216     }
5217     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5218     case METADATA:
5219       if (Record[0] != VERSION_MAJOR)
5220         return true;
5221       if (Listener.ReadFullVersionInformation(Blob))
5222         return true;
5223       break;
5224     case MODULE_NAME:
5225       Listener.ReadModuleName(Blob);
5226       break;
5227     case MODULE_DIRECTORY:
5228       ModuleDir = std::string(Blob);
5229       break;
5230     case MODULE_MAP_FILE: {
5231       unsigned Idx = 0;
5232       auto Path = ReadString(Record, Idx);
5233       ResolveImportedPath(Path, ModuleDir);
5234       Listener.ReadModuleMapFile(Path);
5235       break;
5236     }
5237     case INPUT_FILE_OFFSETS: {
5238       if (!NeedsInputFiles)
5239         break;
5240 
5241       unsigned NumInputFiles = Record[0];
5242       unsigned NumUserFiles = Record[1];
5243       const llvm::support::unaligned_uint64_t *InputFileOffs =
5244           (const llvm::support::unaligned_uint64_t *)Blob.data();
5245       for (unsigned I = 0; I != NumInputFiles; ++I) {
5246         // Go find this input file.
5247         bool isSystemFile = I >= NumUserFiles;
5248 
5249         if (isSystemFile && !NeedsSystemInputFiles)
5250           break; // the rest are system input files
5251 
5252         BitstreamCursor &Cursor = InputFilesCursor;
5253         SavedStreamPosition SavedPosition(Cursor);
5254         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5255           // FIXME this drops errors on the floor.
5256           consumeError(std::move(Err));
5257         }
5258 
5259         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5260         if (!MaybeCode) {
5261           // FIXME this drops errors on the floor.
5262           consumeError(MaybeCode.takeError());
5263         }
5264         unsigned Code = MaybeCode.get();
5265 
5266         RecordData Record;
5267         StringRef Blob;
5268         bool shouldContinue = false;
5269         Expected<unsigned> MaybeRecordType =
5270             Cursor.readRecord(Code, Record, &Blob);
5271         if (!MaybeRecordType) {
5272           // FIXME this drops errors on the floor.
5273           consumeError(MaybeRecordType.takeError());
5274         }
5275         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5276         case INPUT_FILE_HASH:
5277           break;
5278         case INPUT_FILE:
5279           bool Overridden = static_cast<bool>(Record[3]);
5280           std::string Filename = std::string(Blob);
5281           ResolveImportedPath(Filename, ModuleDir);
5282           shouldContinue = Listener.visitInputFile(
5283               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5284           break;
5285         }
5286         if (!shouldContinue)
5287           break;
5288       }
5289       break;
5290     }
5291 
5292     case IMPORTS: {
5293       if (!NeedsImports)
5294         break;
5295 
5296       unsigned Idx = 0, N = Record.size();
5297       while (Idx < N) {
5298         // Read information about the AST file.
5299         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5300         std::string ModuleName = ReadString(Record, Idx);
5301         std::string Filename = ReadString(Record, Idx);
5302         ResolveImportedPath(Filename, ModuleDir);
5303         Listener.visitImport(ModuleName, Filename);
5304       }
5305       break;
5306     }
5307 
5308     default:
5309       // No other validation to perform.
5310       break;
5311     }
5312   }
5313 
5314   // Look for module file extension blocks, if requested.
5315   if (FindModuleFileExtensions) {
5316     BitstreamCursor SavedStream = Stream;
5317     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5318       bool DoneWithExtensionBlock = false;
5319       while (!DoneWithExtensionBlock) {
5320         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5321         if (!MaybeEntry) {
5322           // FIXME this drops the error.
5323           return true;
5324         }
5325         llvm::BitstreamEntry Entry = MaybeEntry.get();
5326 
5327         switch (Entry.Kind) {
5328         case llvm::BitstreamEntry::SubBlock:
5329           if (llvm::Error Err = Stream.SkipBlock()) {
5330             // FIXME this drops the error on the floor.
5331             consumeError(std::move(Err));
5332             return true;
5333           }
5334           continue;
5335 
5336         case llvm::BitstreamEntry::EndBlock:
5337           DoneWithExtensionBlock = true;
5338           continue;
5339 
5340         case llvm::BitstreamEntry::Error:
5341           return true;
5342 
5343         case llvm::BitstreamEntry::Record:
5344           break;
5345         }
5346 
5347        Record.clear();
5348        StringRef Blob;
5349        Expected<unsigned> MaybeRecCode =
5350            Stream.readRecord(Entry.ID, Record, &Blob);
5351        if (!MaybeRecCode) {
5352          // FIXME this drops the error.
5353          return true;
5354        }
5355        switch (MaybeRecCode.get()) {
5356        case EXTENSION_METADATA: {
5357          ModuleFileExtensionMetadata Metadata;
5358          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5359            return true;
5360 
5361          Listener.readModuleFileExtension(Metadata);
5362          break;
5363        }
5364        }
5365       }
5366     }
5367     Stream = SavedStream;
5368   }
5369 
5370   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5371   if (readUnhashedControlBlockImpl(
5372           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5373           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5374           ValidateDiagnosticOptions) != Success)
5375     return true;
5376 
5377   return false;
5378 }
5379 
5380 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5381                                     const PCHContainerReader &PCHContainerRdr,
5382                                     const LangOptions &LangOpts,
5383                                     const TargetOptions &TargetOpts,
5384                                     const PreprocessorOptions &PPOpts,
5385                                     StringRef ExistingModuleCachePath) {
5386   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5387                                ExistingModuleCachePath, FileMgr);
5388   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5389                                   /*FindModuleFileExtensions=*/false,
5390                                   validator,
5391                                   /*ValidateDiagnosticOptions=*/true);
5392 }
5393 
5394 ASTReader::ASTReadResult
5395 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5396   // Enter the submodule block.
5397   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5398     Error(std::move(Err));
5399     return Failure;
5400   }
5401 
5402   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5403   bool First = true;
5404   Module *CurrentModule = nullptr;
5405   RecordData Record;
5406   while (true) {
5407     Expected<llvm::BitstreamEntry> MaybeEntry =
5408         F.Stream.advanceSkippingSubblocks();
5409     if (!MaybeEntry) {
5410       Error(MaybeEntry.takeError());
5411       return Failure;
5412     }
5413     llvm::BitstreamEntry Entry = MaybeEntry.get();
5414 
5415     switch (Entry.Kind) {
5416     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5417     case llvm::BitstreamEntry::Error:
5418       Error("malformed block record in AST file");
5419       return Failure;
5420     case llvm::BitstreamEntry::EndBlock:
5421       return Success;
5422     case llvm::BitstreamEntry::Record:
5423       // The interesting case.
5424       break;
5425     }
5426 
5427     // Read a record.
5428     StringRef Blob;
5429     Record.clear();
5430     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5431     if (!MaybeKind) {
5432       Error(MaybeKind.takeError());
5433       return Failure;
5434     }
5435     unsigned Kind = MaybeKind.get();
5436 
5437     if ((Kind == SUBMODULE_METADATA) != First) {
5438       Error("submodule metadata record should be at beginning of block");
5439       return Failure;
5440     }
5441     First = false;
5442 
5443     // Submodule information is only valid if we have a current module.
5444     // FIXME: Should we error on these cases?
5445     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5446         Kind != SUBMODULE_DEFINITION)
5447       continue;
5448 
5449     switch (Kind) {
5450     default:  // Default behavior: ignore.
5451       break;
5452 
5453     case SUBMODULE_DEFINITION: {
5454       if (Record.size() < 12) {
5455         Error("malformed module definition");
5456         return Failure;
5457       }
5458 
5459       StringRef Name = Blob;
5460       unsigned Idx = 0;
5461       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5462       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5463       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5464       bool IsFramework = Record[Idx++];
5465       bool IsExplicit = Record[Idx++];
5466       bool IsSystem = Record[Idx++];
5467       bool IsExternC = Record[Idx++];
5468       bool InferSubmodules = Record[Idx++];
5469       bool InferExplicitSubmodules = Record[Idx++];
5470       bool InferExportWildcard = Record[Idx++];
5471       bool ConfigMacrosExhaustive = Record[Idx++];
5472       bool ModuleMapIsPrivate = Record[Idx++];
5473 
5474       Module *ParentModule = nullptr;
5475       if (Parent)
5476         ParentModule = getSubmodule(Parent);
5477 
5478       // Retrieve this (sub)module from the module map, creating it if
5479       // necessary.
5480       CurrentModule =
5481           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5482               .first;
5483 
5484       // FIXME: set the definition loc for CurrentModule, or call
5485       // ModMap.setInferredModuleAllowedBy()
5486 
5487       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5488       if (GlobalIndex >= SubmodulesLoaded.size() ||
5489           SubmodulesLoaded[GlobalIndex]) {
5490         Error("too many submodules");
5491         return Failure;
5492       }
5493 
5494       if (!ParentModule) {
5495         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5496           // Don't emit module relocation error if we have -fno-validate-pch
5497           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5498               CurFile != F.File) {
5499             Error(diag::err_module_file_conflict,
5500                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5501                   F.File->getName());
5502             return Failure;
5503           }
5504         }
5505 
5506         F.DidReadTopLevelSubmodule = true;
5507         CurrentModule->setASTFile(F.File);
5508         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5509       }
5510 
5511       CurrentModule->Kind = Kind;
5512       CurrentModule->Signature = F.Signature;
5513       CurrentModule->IsFromModuleFile = true;
5514       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5515       CurrentModule->IsExternC = IsExternC;
5516       CurrentModule->InferSubmodules = InferSubmodules;
5517       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5518       CurrentModule->InferExportWildcard = InferExportWildcard;
5519       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5520       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5521       if (DeserializationListener)
5522         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5523 
5524       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5525 
5526       // Clear out data that will be replaced by what is in the module file.
5527       CurrentModule->LinkLibraries.clear();
5528       CurrentModule->ConfigMacros.clear();
5529       CurrentModule->UnresolvedConflicts.clear();
5530       CurrentModule->Conflicts.clear();
5531 
5532       // The module is available unless it's missing a requirement; relevant
5533       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5534       // Missing headers that were present when the module was built do not
5535       // make it unavailable -- if we got this far, this must be an explicitly
5536       // imported module file.
5537       CurrentModule->Requirements.clear();
5538       CurrentModule->MissingHeaders.clear();
5539       CurrentModule->IsMissingRequirement =
5540           ParentModule && ParentModule->IsMissingRequirement;
5541       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5542       break;
5543     }
5544 
5545     case SUBMODULE_UMBRELLA_HEADER: {
5546       std::string Filename = std::string(Blob);
5547       ResolveImportedPath(F, Filename);
5548       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5549         if (!CurrentModule->getUmbrellaHeader())
5550           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5551         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5552           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5553             Error("mismatched umbrella headers in submodule");
5554           return OutOfDate;
5555         }
5556       }
5557       break;
5558     }
5559 
5560     case SUBMODULE_HEADER:
5561     case SUBMODULE_EXCLUDED_HEADER:
5562     case SUBMODULE_PRIVATE_HEADER:
5563       // We lazily associate headers with their modules via the HeaderInfo table.
5564       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5565       // of complete filenames or remove it entirely.
5566       break;
5567 
5568     case SUBMODULE_TEXTUAL_HEADER:
5569     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5570       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5571       // them here.
5572       break;
5573 
5574     case SUBMODULE_TOPHEADER:
5575       CurrentModule->addTopHeaderFilename(Blob);
5576       break;
5577 
5578     case SUBMODULE_UMBRELLA_DIR: {
5579       std::string Dirname = std::string(Blob);
5580       ResolveImportedPath(F, Dirname);
5581       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5582         if (!CurrentModule->getUmbrellaDir())
5583           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5584         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5585           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5586             Error("mismatched umbrella directories in submodule");
5587           return OutOfDate;
5588         }
5589       }
5590       break;
5591     }
5592 
5593     case SUBMODULE_METADATA: {
5594       F.BaseSubmoduleID = getTotalNumSubmodules();
5595       F.LocalNumSubmodules = Record[0];
5596       unsigned LocalBaseSubmoduleID = Record[1];
5597       if (F.LocalNumSubmodules > 0) {
5598         // Introduce the global -> local mapping for submodules within this
5599         // module.
5600         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5601 
5602         // Introduce the local -> global mapping for submodules within this
5603         // module.
5604         F.SubmoduleRemap.insertOrReplace(
5605           std::make_pair(LocalBaseSubmoduleID,
5606                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5607 
5608         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5609       }
5610       break;
5611     }
5612 
5613     case SUBMODULE_IMPORTS:
5614       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5615         UnresolvedModuleRef Unresolved;
5616         Unresolved.File = &F;
5617         Unresolved.Mod = CurrentModule;
5618         Unresolved.ID = Record[Idx];
5619         Unresolved.Kind = UnresolvedModuleRef::Import;
5620         Unresolved.IsWildcard = false;
5621         UnresolvedModuleRefs.push_back(Unresolved);
5622       }
5623       break;
5624 
5625     case SUBMODULE_EXPORTS:
5626       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5627         UnresolvedModuleRef Unresolved;
5628         Unresolved.File = &F;
5629         Unresolved.Mod = CurrentModule;
5630         Unresolved.ID = Record[Idx];
5631         Unresolved.Kind = UnresolvedModuleRef::Export;
5632         Unresolved.IsWildcard = Record[Idx + 1];
5633         UnresolvedModuleRefs.push_back(Unresolved);
5634       }
5635 
5636       // Once we've loaded the set of exports, there's no reason to keep
5637       // the parsed, unresolved exports around.
5638       CurrentModule->UnresolvedExports.clear();
5639       break;
5640 
5641     case SUBMODULE_REQUIRES:
5642       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5643                                     PP.getTargetInfo());
5644       break;
5645 
5646     case SUBMODULE_LINK_LIBRARY:
5647       ModMap.resolveLinkAsDependencies(CurrentModule);
5648       CurrentModule->LinkLibraries.push_back(
5649           Module::LinkLibrary(std::string(Blob), Record[0]));
5650       break;
5651 
5652     case SUBMODULE_CONFIG_MACRO:
5653       CurrentModule->ConfigMacros.push_back(Blob.str());
5654       break;
5655 
5656     case SUBMODULE_CONFLICT: {
5657       UnresolvedModuleRef Unresolved;
5658       Unresolved.File = &F;
5659       Unresolved.Mod = CurrentModule;
5660       Unresolved.ID = Record[0];
5661       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5662       Unresolved.IsWildcard = false;
5663       Unresolved.String = Blob;
5664       UnresolvedModuleRefs.push_back(Unresolved);
5665       break;
5666     }
5667 
5668     case SUBMODULE_INITIALIZERS: {
5669       if (!ContextObj)
5670         break;
5671       SmallVector<uint32_t, 16> Inits;
5672       for (auto &ID : Record)
5673         Inits.push_back(getGlobalDeclID(F, ID));
5674       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5675       break;
5676     }
5677 
5678     case SUBMODULE_EXPORT_AS:
5679       CurrentModule->ExportAsModule = Blob.str();
5680       ModMap.addLinkAsDependency(CurrentModule);
5681       break;
5682     }
5683   }
5684 }
5685 
5686 /// Parse the record that corresponds to a LangOptions data
5687 /// structure.
5688 ///
5689 /// This routine parses the language options from the AST file and then gives
5690 /// them to the AST listener if one is set.
5691 ///
5692 /// \returns true if the listener deems the file unacceptable, false otherwise.
5693 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5694                                      bool Complain,
5695                                      ASTReaderListener &Listener,
5696                                      bool AllowCompatibleDifferences) {
5697   LangOptions LangOpts;
5698   unsigned Idx = 0;
5699 #define LANGOPT(Name, Bits, Default, Description) \
5700   LangOpts.Name = Record[Idx++];
5701 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5702   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5703 #include "clang/Basic/LangOptions.def"
5704 #define SANITIZER(NAME, ID)                                                    \
5705   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5706 #include "clang/Basic/Sanitizers.def"
5707 
5708   for (unsigned N = Record[Idx++]; N; --N)
5709     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5710 
5711   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5712   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5713   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5714 
5715   LangOpts.CurrentModule = ReadString(Record, Idx);
5716 
5717   // Comment options.
5718   for (unsigned N = Record[Idx++]; N; --N) {
5719     LangOpts.CommentOpts.BlockCommandNames.push_back(
5720       ReadString(Record, Idx));
5721   }
5722   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5723 
5724   // OpenMP offloading options.
5725   for (unsigned N = Record[Idx++]; N; --N) {
5726     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5727   }
5728 
5729   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5730 
5731   return Listener.ReadLanguageOptions(LangOpts, Complain,
5732                                       AllowCompatibleDifferences);
5733 }
5734 
5735 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5736                                    ASTReaderListener &Listener,
5737                                    bool AllowCompatibleDifferences) {
5738   unsigned Idx = 0;
5739   TargetOptions TargetOpts;
5740   TargetOpts.Triple = ReadString(Record, Idx);
5741   TargetOpts.CPU = ReadString(Record, Idx);
5742   TargetOpts.ABI = ReadString(Record, Idx);
5743   for (unsigned N = Record[Idx++]; N; --N) {
5744     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5745   }
5746   for (unsigned N = Record[Idx++]; N; --N) {
5747     TargetOpts.Features.push_back(ReadString(Record, Idx));
5748   }
5749 
5750   return Listener.ReadTargetOptions(TargetOpts, Complain,
5751                                     AllowCompatibleDifferences);
5752 }
5753 
5754 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5755                                        ASTReaderListener &Listener) {
5756   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5757   unsigned Idx = 0;
5758 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5759 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5760   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5761 #include "clang/Basic/DiagnosticOptions.def"
5762 
5763   for (unsigned N = Record[Idx++]; N; --N)
5764     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5765   for (unsigned N = Record[Idx++]; N; --N)
5766     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5767 
5768   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5769 }
5770 
5771 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5772                                        ASTReaderListener &Listener) {
5773   FileSystemOptions FSOpts;
5774   unsigned Idx = 0;
5775   FSOpts.WorkingDir = ReadString(Record, Idx);
5776   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5777 }
5778 
5779 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5780                                          bool Complain,
5781                                          ASTReaderListener &Listener) {
5782   HeaderSearchOptions HSOpts;
5783   unsigned Idx = 0;
5784   HSOpts.Sysroot = ReadString(Record, Idx);
5785 
5786   // Include entries.
5787   for (unsigned N = Record[Idx++]; N; --N) {
5788     std::string Path = ReadString(Record, Idx);
5789     frontend::IncludeDirGroup Group
5790       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5791     bool IsFramework = Record[Idx++];
5792     bool IgnoreSysRoot = Record[Idx++];
5793     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5794                                     IgnoreSysRoot);
5795   }
5796 
5797   // System header prefixes.
5798   for (unsigned N = Record[Idx++]; N; --N) {
5799     std::string Prefix = ReadString(Record, Idx);
5800     bool IsSystemHeader = Record[Idx++];
5801     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5802   }
5803 
5804   HSOpts.ResourceDir = ReadString(Record, Idx);
5805   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5806   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5807   HSOpts.DisableModuleHash = Record[Idx++];
5808   HSOpts.ImplicitModuleMaps = Record[Idx++];
5809   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5810   HSOpts.UseBuiltinIncludes = Record[Idx++];
5811   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5812   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5813   HSOpts.UseLibcxx = Record[Idx++];
5814   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5815 
5816   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5817                                           Complain);
5818 }
5819 
5820 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5821                                          bool Complain,
5822                                          ASTReaderListener &Listener,
5823                                          std::string &SuggestedPredefines) {
5824   PreprocessorOptions PPOpts;
5825   unsigned Idx = 0;
5826 
5827   // Macro definitions/undefs
5828   for (unsigned N = Record[Idx++]; N; --N) {
5829     std::string Macro = ReadString(Record, Idx);
5830     bool IsUndef = Record[Idx++];
5831     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5832   }
5833 
5834   // Includes
5835   for (unsigned N = Record[Idx++]; N; --N) {
5836     PPOpts.Includes.push_back(ReadString(Record, Idx));
5837   }
5838 
5839   // Macro Includes
5840   for (unsigned N = Record[Idx++]; N; --N) {
5841     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5842   }
5843 
5844   PPOpts.UsePredefines = Record[Idx++];
5845   PPOpts.DetailedRecord = Record[Idx++];
5846   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5847   PPOpts.ObjCXXARCStandardLibrary =
5848     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5849   SuggestedPredefines.clear();
5850   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5851                                           SuggestedPredefines);
5852 }
5853 
5854 std::pair<ModuleFile *, unsigned>
5855 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5856   GlobalPreprocessedEntityMapType::iterator
5857   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5858   assert(I != GlobalPreprocessedEntityMap.end() &&
5859          "Corrupted global preprocessed entity map");
5860   ModuleFile *M = I->second;
5861   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5862   return std::make_pair(M, LocalIndex);
5863 }
5864 
5865 llvm::iterator_range<PreprocessingRecord::iterator>
5866 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5867   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5868     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5869                                              Mod.NumPreprocessedEntities);
5870 
5871   return llvm::make_range(PreprocessingRecord::iterator(),
5872                           PreprocessingRecord::iterator());
5873 }
5874 
5875 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5876 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5877   return llvm::make_range(
5878       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5879       ModuleDeclIterator(this, &Mod,
5880                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5881 }
5882 
5883 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5884   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5885   assert(I != GlobalSkippedRangeMap.end() &&
5886     "Corrupted global skipped range map");
5887   ModuleFile *M = I->second;
5888   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5889   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5890   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5891   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5892                     TranslateSourceLocation(*M, RawRange.getEnd()));
5893   assert(Range.isValid());
5894   return Range;
5895 }
5896 
5897 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5898   PreprocessedEntityID PPID = Index+1;
5899   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5900   ModuleFile &M = *PPInfo.first;
5901   unsigned LocalIndex = PPInfo.second;
5902   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5903 
5904   if (!PP.getPreprocessingRecord()) {
5905     Error("no preprocessing record");
5906     return nullptr;
5907   }
5908 
5909   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5910   if (llvm::Error Err =
5911           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5912     Error(std::move(Err));
5913     return nullptr;
5914   }
5915 
5916   Expected<llvm::BitstreamEntry> MaybeEntry =
5917       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5918   if (!MaybeEntry) {
5919     Error(MaybeEntry.takeError());
5920     return nullptr;
5921   }
5922   llvm::BitstreamEntry Entry = MaybeEntry.get();
5923 
5924   if (Entry.Kind != llvm::BitstreamEntry::Record)
5925     return nullptr;
5926 
5927   // Read the record.
5928   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5929                     TranslateSourceLocation(M, PPOffs.getEnd()));
5930   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5931   StringRef Blob;
5932   RecordData Record;
5933   Expected<unsigned> MaybeRecType =
5934       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5935   if (!MaybeRecType) {
5936     Error(MaybeRecType.takeError());
5937     return nullptr;
5938   }
5939   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5940   case PPD_MACRO_EXPANSION: {
5941     bool isBuiltin = Record[0];
5942     IdentifierInfo *Name = nullptr;
5943     MacroDefinitionRecord *Def = nullptr;
5944     if (isBuiltin)
5945       Name = getLocalIdentifier(M, Record[1]);
5946     else {
5947       PreprocessedEntityID GlobalID =
5948           getGlobalPreprocessedEntityID(M, Record[1]);
5949       Def = cast<MacroDefinitionRecord>(
5950           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5951     }
5952 
5953     MacroExpansion *ME;
5954     if (isBuiltin)
5955       ME = new (PPRec) MacroExpansion(Name, Range);
5956     else
5957       ME = new (PPRec) MacroExpansion(Def, Range);
5958 
5959     return ME;
5960   }
5961 
5962   case PPD_MACRO_DEFINITION: {
5963     // Decode the identifier info and then check again; if the macro is
5964     // still defined and associated with the identifier,
5965     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5966     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5967 
5968     if (DeserializationListener)
5969       DeserializationListener->MacroDefinitionRead(PPID, MD);
5970 
5971     return MD;
5972   }
5973 
5974   case PPD_INCLUSION_DIRECTIVE: {
5975     const char *FullFileNameStart = Blob.data() + Record[0];
5976     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5977     const FileEntry *File = nullptr;
5978     if (!FullFileName.empty())
5979       if (auto FE = PP.getFileManager().getFile(FullFileName))
5980         File = *FE;
5981 
5982     // FIXME: Stable encoding
5983     InclusionDirective::InclusionKind Kind
5984       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5985     InclusionDirective *ID
5986       = new (PPRec) InclusionDirective(PPRec, Kind,
5987                                        StringRef(Blob.data(), Record[0]),
5988                                        Record[1], Record[3],
5989                                        File,
5990                                        Range);
5991     return ID;
5992   }
5993   }
5994 
5995   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5996 }
5997 
5998 /// Find the next module that contains entities and return the ID
5999 /// of the first entry.
6000 ///
6001 /// \param SLocMapI points at a chunk of a module that contains no
6002 /// preprocessed entities or the entities it contains are not the ones we are
6003 /// looking for.
6004 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6005                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6006   ++SLocMapI;
6007   for (GlobalSLocOffsetMapType::const_iterator
6008          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6009     ModuleFile &M = *SLocMapI->second;
6010     if (M.NumPreprocessedEntities)
6011       return M.BasePreprocessedEntityID;
6012   }
6013 
6014   return getTotalNumPreprocessedEntities();
6015 }
6016 
6017 namespace {
6018 
6019 struct PPEntityComp {
6020   const ASTReader &Reader;
6021   ModuleFile &M;
6022 
6023   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6024 
6025   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6026     SourceLocation LHS = getLoc(L);
6027     SourceLocation RHS = getLoc(R);
6028     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6029   }
6030 
6031   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6032     SourceLocation LHS = getLoc(L);
6033     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6034   }
6035 
6036   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6037     SourceLocation RHS = getLoc(R);
6038     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6039   }
6040 
6041   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6042     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6043   }
6044 };
6045 
6046 } // namespace
6047 
6048 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6049                                                        bool EndsAfter) const {
6050   if (SourceMgr.isLocalSourceLocation(Loc))
6051     return getTotalNumPreprocessedEntities();
6052 
6053   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6054       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6055   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6056          "Corrupted global sloc offset map");
6057 
6058   if (SLocMapI->second->NumPreprocessedEntities == 0)
6059     return findNextPreprocessedEntity(SLocMapI);
6060 
6061   ModuleFile &M = *SLocMapI->second;
6062 
6063   using pp_iterator = const PPEntityOffset *;
6064 
6065   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6066   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6067 
6068   size_t Count = M.NumPreprocessedEntities;
6069   size_t Half;
6070   pp_iterator First = pp_begin;
6071   pp_iterator PPI;
6072 
6073   if (EndsAfter) {
6074     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6075                            PPEntityComp(*this, M));
6076   } else {
6077     // Do a binary search manually instead of using std::lower_bound because
6078     // The end locations of entities may be unordered (when a macro expansion
6079     // is inside another macro argument), but for this case it is not important
6080     // whether we get the first macro expansion or its containing macro.
6081     while (Count > 0) {
6082       Half = Count / 2;
6083       PPI = First;
6084       std::advance(PPI, Half);
6085       if (SourceMgr.isBeforeInTranslationUnit(
6086               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6087         First = PPI;
6088         ++First;
6089         Count = Count - Half - 1;
6090       } else
6091         Count = Half;
6092     }
6093   }
6094 
6095   if (PPI == pp_end)
6096     return findNextPreprocessedEntity(SLocMapI);
6097 
6098   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6099 }
6100 
6101 /// Returns a pair of [Begin, End) indices of preallocated
6102 /// preprocessed entities that \arg Range encompasses.
6103 std::pair<unsigned, unsigned>
6104     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6105   if (Range.isInvalid())
6106     return std::make_pair(0,0);
6107   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6108 
6109   PreprocessedEntityID BeginID =
6110       findPreprocessedEntity(Range.getBegin(), false);
6111   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6112   return std::make_pair(BeginID, EndID);
6113 }
6114 
6115 /// Optionally returns true or false if the preallocated preprocessed
6116 /// entity with index \arg Index came from file \arg FID.
6117 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6118                                                              FileID FID) {
6119   if (FID.isInvalid())
6120     return false;
6121 
6122   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6123   ModuleFile &M = *PPInfo.first;
6124   unsigned LocalIndex = PPInfo.second;
6125   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6126 
6127   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6128   if (Loc.isInvalid())
6129     return false;
6130 
6131   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6132     return true;
6133   else
6134     return false;
6135 }
6136 
6137 namespace {
6138 
6139   /// Visitor used to search for information about a header file.
6140   class HeaderFileInfoVisitor {
6141     const FileEntry *FE;
6142     Optional<HeaderFileInfo> HFI;
6143 
6144   public:
6145     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6146 
6147     bool operator()(ModuleFile &M) {
6148       HeaderFileInfoLookupTable *Table
6149         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6150       if (!Table)
6151         return false;
6152 
6153       // Look in the on-disk hash table for an entry for this file name.
6154       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6155       if (Pos == Table->end())
6156         return false;
6157 
6158       HFI = *Pos;
6159       return true;
6160     }
6161 
6162     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6163   };
6164 
6165 } // namespace
6166 
6167 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6168   HeaderFileInfoVisitor Visitor(FE);
6169   ModuleMgr.visit(Visitor);
6170   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6171     return *HFI;
6172 
6173   return HeaderFileInfo();
6174 }
6175 
6176 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6177   using DiagState = DiagnosticsEngine::DiagState;
6178   SmallVector<DiagState *, 32> DiagStates;
6179 
6180   for (ModuleFile &F : ModuleMgr) {
6181     unsigned Idx = 0;
6182     auto &Record = F.PragmaDiagMappings;
6183     if (Record.empty())
6184       continue;
6185 
6186     DiagStates.clear();
6187 
6188     auto ReadDiagState =
6189         [&](const DiagState &BasedOn, SourceLocation Loc,
6190             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6191       unsigned BackrefID = Record[Idx++];
6192       if (BackrefID != 0)
6193         return DiagStates[BackrefID - 1];
6194 
6195       // A new DiagState was created here.
6196       Diag.DiagStates.push_back(BasedOn);
6197       DiagState *NewState = &Diag.DiagStates.back();
6198       DiagStates.push_back(NewState);
6199       unsigned Size = Record[Idx++];
6200       assert(Idx + Size * 2 <= Record.size() &&
6201              "Invalid data, not enough diag/map pairs");
6202       while (Size--) {
6203         unsigned DiagID = Record[Idx++];
6204         DiagnosticMapping NewMapping =
6205             DiagnosticMapping::deserialize(Record[Idx++]);
6206         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6207           continue;
6208 
6209         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6210 
6211         // If this mapping was specified as a warning but the severity was
6212         // upgraded due to diagnostic settings, simulate the current diagnostic
6213         // settings (and use a warning).
6214         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6215           NewMapping.setSeverity(diag::Severity::Warning);
6216           NewMapping.setUpgradedFromWarning(false);
6217         }
6218 
6219         Mapping = NewMapping;
6220       }
6221       return NewState;
6222     };
6223 
6224     // Read the first state.
6225     DiagState *FirstState;
6226     if (F.Kind == MK_ImplicitModule) {
6227       // Implicitly-built modules are reused with different diagnostic
6228       // settings.  Use the initial diagnostic state from Diag to simulate this
6229       // compilation's diagnostic settings.
6230       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6231       DiagStates.push_back(FirstState);
6232 
6233       // Skip the initial diagnostic state from the serialized module.
6234       assert(Record[1] == 0 &&
6235              "Invalid data, unexpected backref in initial state");
6236       Idx = 3 + Record[2] * 2;
6237       assert(Idx < Record.size() &&
6238              "Invalid data, not enough state change pairs in initial state");
6239     } else if (F.isModule()) {
6240       // For an explicit module, preserve the flags from the module build
6241       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6242       // -Wblah flags.
6243       unsigned Flags = Record[Idx++];
6244       DiagState Initial;
6245       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6246       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6247       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6248       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6249       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6250       Initial.ExtBehavior = (diag::Severity)Flags;
6251       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6252 
6253       assert(F.OriginalSourceFileID.isValid());
6254 
6255       // Set up the root buffer of the module to start with the initial
6256       // diagnostic state of the module itself, to cover files that contain no
6257       // explicit transitions (for which we did not serialize anything).
6258       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6259           .StateTransitions.push_back({FirstState, 0});
6260     } else {
6261       // For prefix ASTs, start with whatever the user configured on the
6262       // command line.
6263       Idx++; // Skip flags.
6264       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6265                                  SourceLocation(), false);
6266     }
6267 
6268     // Read the state transitions.
6269     unsigned NumLocations = Record[Idx++];
6270     while (NumLocations--) {
6271       assert(Idx < Record.size() &&
6272              "Invalid data, missing pragma diagnostic states");
6273       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6274       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6275       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6276       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6277       unsigned Transitions = Record[Idx++];
6278 
6279       // Note that we don't need to set up Parent/ParentOffset here, because
6280       // we won't be changing the diagnostic state within imported FileIDs
6281       // (other than perhaps appending to the main source file, which has no
6282       // parent).
6283       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6284       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6285       for (unsigned I = 0; I != Transitions; ++I) {
6286         unsigned Offset = Record[Idx++];
6287         auto *State =
6288             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6289         F.StateTransitions.push_back({State, Offset});
6290       }
6291     }
6292 
6293     // Read the final state.
6294     assert(Idx < Record.size() &&
6295            "Invalid data, missing final pragma diagnostic state");
6296     SourceLocation CurStateLoc =
6297         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6298     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6299 
6300     if (!F.isModule()) {
6301       Diag.DiagStatesByLoc.CurDiagState = CurState;
6302       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6303 
6304       // Preserve the property that the imaginary root file describes the
6305       // current state.
6306       FileID NullFile;
6307       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6308       if (T.empty())
6309         T.push_back({CurState, 0});
6310       else
6311         T[0].State = CurState;
6312     }
6313 
6314     // Don't try to read these mappings again.
6315     Record.clear();
6316   }
6317 }
6318 
6319 /// Get the correct cursor and offset for loading a type.
6320 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6321   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6322   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6323   ModuleFile *M = I->second;
6324   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6325 }
6326 
6327 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6328   switch (code) {
6329 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6330   case TYPE_##CODE_ID: return Type::CLASS_ID;
6331 #include "clang/Serialization/TypeBitCodes.def"
6332   default: return llvm::None;
6333   }
6334 }
6335 
6336 /// Read and return the type with the given index..
6337 ///
6338 /// The index is the type ID, shifted and minus the number of predefs. This
6339 /// routine actually reads the record corresponding to the type at the given
6340 /// location. It is a helper routine for GetType, which deals with reading type
6341 /// IDs.
6342 QualType ASTReader::readTypeRecord(unsigned Index) {
6343   assert(ContextObj && "reading type with no AST context");
6344   ASTContext &Context = *ContextObj;
6345   RecordLocation Loc = TypeCursorForIndex(Index);
6346   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6347 
6348   // Keep track of where we are in the stream, then jump back there
6349   // after reading this type.
6350   SavedStreamPosition SavedPosition(DeclsCursor);
6351 
6352   ReadingKindTracker ReadingKind(Read_Type, *this);
6353 
6354   // Note that we are loading a type record.
6355   Deserializing AType(this);
6356 
6357   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6358     Error(std::move(Err));
6359     return QualType();
6360   }
6361   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6362   if (!RawCode) {
6363     Error(RawCode.takeError());
6364     return QualType();
6365   }
6366 
6367   ASTRecordReader Record(*this, *Loc.F);
6368   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6369   if (!Code) {
6370     Error(Code.takeError());
6371     return QualType();
6372   }
6373   if (Code.get() == TYPE_EXT_QUAL) {
6374     QualType baseType = Record.readQualType();
6375     Qualifiers quals = Record.readQualifiers();
6376     return Context.getQualifiedType(baseType, quals);
6377   }
6378 
6379   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6380   if (!maybeClass) {
6381     Error("Unexpected code for type");
6382     return QualType();
6383   }
6384 
6385   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6386   return TypeReader.read(*maybeClass);
6387 }
6388 
6389 namespace clang {
6390 
6391 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6392   ASTRecordReader &Reader;
6393 
6394   SourceLocation readSourceLocation() {
6395     return Reader.readSourceLocation();
6396   }
6397 
6398   TypeSourceInfo *GetTypeSourceInfo() {
6399     return Reader.readTypeSourceInfo();
6400   }
6401 
6402   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6403     return Reader.readNestedNameSpecifierLoc();
6404   }
6405 
6406   Attr *ReadAttr() {
6407     return Reader.readAttr();
6408   }
6409 
6410 public:
6411   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6412 
6413   // We want compile-time assurance that we've enumerated all of
6414   // these, so unfortunately we have to declare them first, then
6415   // define them out-of-line.
6416 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6417 #define TYPELOC(CLASS, PARENT) \
6418   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6419 #include "clang/AST/TypeLocNodes.def"
6420 
6421   void VisitFunctionTypeLoc(FunctionTypeLoc);
6422   void VisitArrayTypeLoc(ArrayTypeLoc);
6423 };
6424 
6425 } // namespace clang
6426 
6427 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6428   // nothing to do
6429 }
6430 
6431 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6432   TL.setBuiltinLoc(readSourceLocation());
6433   if (TL.needsExtraLocalData()) {
6434     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6435     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6436     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6437     TL.setModeAttr(Reader.readInt());
6438   }
6439 }
6440 
6441 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6442   TL.setNameLoc(readSourceLocation());
6443 }
6444 
6445 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6446   TL.setStarLoc(readSourceLocation());
6447 }
6448 
6449 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6450   // nothing to do
6451 }
6452 
6453 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6454   // nothing to do
6455 }
6456 
6457 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6458   TL.setExpansionLoc(readSourceLocation());
6459 }
6460 
6461 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6462   TL.setCaretLoc(readSourceLocation());
6463 }
6464 
6465 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6466   TL.setAmpLoc(readSourceLocation());
6467 }
6468 
6469 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6470   TL.setAmpAmpLoc(readSourceLocation());
6471 }
6472 
6473 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6474   TL.setStarLoc(readSourceLocation());
6475   TL.setClassTInfo(GetTypeSourceInfo());
6476 }
6477 
6478 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6479   TL.setLBracketLoc(readSourceLocation());
6480   TL.setRBracketLoc(readSourceLocation());
6481   if (Reader.readBool())
6482     TL.setSizeExpr(Reader.readExpr());
6483   else
6484     TL.setSizeExpr(nullptr);
6485 }
6486 
6487 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6488   VisitArrayTypeLoc(TL);
6489 }
6490 
6491 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6492   VisitArrayTypeLoc(TL);
6493 }
6494 
6495 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6496   VisitArrayTypeLoc(TL);
6497 }
6498 
6499 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6500                                             DependentSizedArrayTypeLoc TL) {
6501   VisitArrayTypeLoc(TL);
6502 }
6503 
6504 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6505     DependentAddressSpaceTypeLoc TL) {
6506 
6507     TL.setAttrNameLoc(readSourceLocation());
6508     TL.setAttrOperandParensRange(Reader.readSourceRange());
6509     TL.setAttrExprOperand(Reader.readExpr());
6510 }
6511 
6512 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6513                                         DependentSizedExtVectorTypeLoc TL) {
6514   TL.setNameLoc(readSourceLocation());
6515 }
6516 
6517 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6518   TL.setNameLoc(readSourceLocation());
6519 }
6520 
6521 void TypeLocReader::VisitDependentVectorTypeLoc(
6522     DependentVectorTypeLoc TL) {
6523   TL.setNameLoc(readSourceLocation());
6524 }
6525 
6526 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6527   TL.setNameLoc(readSourceLocation());
6528 }
6529 
6530 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6531   TL.setLocalRangeBegin(readSourceLocation());
6532   TL.setLParenLoc(readSourceLocation());
6533   TL.setRParenLoc(readSourceLocation());
6534   TL.setExceptionSpecRange(Reader.readSourceRange());
6535   TL.setLocalRangeEnd(readSourceLocation());
6536   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6537     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6538   }
6539 }
6540 
6541 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6542   VisitFunctionTypeLoc(TL);
6543 }
6544 
6545 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6546   VisitFunctionTypeLoc(TL);
6547 }
6548 
6549 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6550   TL.setNameLoc(readSourceLocation());
6551 }
6552 
6553 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6554   TL.setNameLoc(readSourceLocation());
6555 }
6556 
6557 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6558   TL.setTypeofLoc(readSourceLocation());
6559   TL.setLParenLoc(readSourceLocation());
6560   TL.setRParenLoc(readSourceLocation());
6561 }
6562 
6563 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6564   TL.setTypeofLoc(readSourceLocation());
6565   TL.setLParenLoc(readSourceLocation());
6566   TL.setRParenLoc(readSourceLocation());
6567   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6568 }
6569 
6570 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6571   TL.setNameLoc(readSourceLocation());
6572 }
6573 
6574 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6575   TL.setKWLoc(readSourceLocation());
6576   TL.setLParenLoc(readSourceLocation());
6577   TL.setRParenLoc(readSourceLocation());
6578   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6579 }
6580 
6581 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6582   TL.setNameLoc(readSourceLocation());
6583   if (Reader.readBool()) {
6584     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6585     TL.setTemplateKWLoc(readSourceLocation());
6586     TL.setConceptNameLoc(readSourceLocation());
6587     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6588     TL.setLAngleLoc(readSourceLocation());
6589     TL.setRAngleLoc(readSourceLocation());
6590     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6591       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6592                               TL.getTypePtr()->getArg(i).getKind()));
6593   }
6594 }
6595 
6596 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6597     DeducedTemplateSpecializationTypeLoc TL) {
6598   TL.setTemplateNameLoc(readSourceLocation());
6599 }
6600 
6601 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6602   TL.setNameLoc(readSourceLocation());
6603 }
6604 
6605 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6606   TL.setNameLoc(readSourceLocation());
6607 }
6608 
6609 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6610   TL.setAttr(ReadAttr());
6611 }
6612 
6613 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6614   TL.setNameLoc(readSourceLocation());
6615 }
6616 
6617 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6618                                             SubstTemplateTypeParmTypeLoc TL) {
6619   TL.setNameLoc(readSourceLocation());
6620 }
6621 
6622 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6623                                           SubstTemplateTypeParmPackTypeLoc TL) {
6624   TL.setNameLoc(readSourceLocation());
6625 }
6626 
6627 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6628                                            TemplateSpecializationTypeLoc TL) {
6629   TL.setTemplateKeywordLoc(readSourceLocation());
6630   TL.setTemplateNameLoc(readSourceLocation());
6631   TL.setLAngleLoc(readSourceLocation());
6632   TL.setRAngleLoc(readSourceLocation());
6633   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6634     TL.setArgLocInfo(
6635         i,
6636         Reader.readTemplateArgumentLocInfo(
6637           TL.getTypePtr()->getArg(i).getKind()));
6638 }
6639 
6640 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6641   TL.setLParenLoc(readSourceLocation());
6642   TL.setRParenLoc(readSourceLocation());
6643 }
6644 
6645 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6646   TL.setElaboratedKeywordLoc(readSourceLocation());
6647   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6648 }
6649 
6650 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6651   TL.setNameLoc(readSourceLocation());
6652 }
6653 
6654 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6655   TL.setElaboratedKeywordLoc(readSourceLocation());
6656   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6657   TL.setNameLoc(readSourceLocation());
6658 }
6659 
6660 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6661        DependentTemplateSpecializationTypeLoc TL) {
6662   TL.setElaboratedKeywordLoc(readSourceLocation());
6663   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6664   TL.setTemplateKeywordLoc(readSourceLocation());
6665   TL.setTemplateNameLoc(readSourceLocation());
6666   TL.setLAngleLoc(readSourceLocation());
6667   TL.setRAngleLoc(readSourceLocation());
6668   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6669     TL.setArgLocInfo(
6670         I,
6671         Reader.readTemplateArgumentLocInfo(
6672             TL.getTypePtr()->getArg(I).getKind()));
6673 }
6674 
6675 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6676   TL.setEllipsisLoc(readSourceLocation());
6677 }
6678 
6679 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6680   TL.setNameLoc(readSourceLocation());
6681 }
6682 
6683 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6684   if (TL.getNumProtocols()) {
6685     TL.setProtocolLAngleLoc(readSourceLocation());
6686     TL.setProtocolRAngleLoc(readSourceLocation());
6687   }
6688   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6689     TL.setProtocolLoc(i, readSourceLocation());
6690 }
6691 
6692 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6693   TL.setHasBaseTypeAsWritten(Reader.readBool());
6694   TL.setTypeArgsLAngleLoc(readSourceLocation());
6695   TL.setTypeArgsRAngleLoc(readSourceLocation());
6696   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6697     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6698   TL.setProtocolLAngleLoc(readSourceLocation());
6699   TL.setProtocolRAngleLoc(readSourceLocation());
6700   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6701     TL.setProtocolLoc(i, readSourceLocation());
6702 }
6703 
6704 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6705   TL.setStarLoc(readSourceLocation());
6706 }
6707 
6708 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6709   TL.setKWLoc(readSourceLocation());
6710   TL.setLParenLoc(readSourceLocation());
6711   TL.setRParenLoc(readSourceLocation());
6712 }
6713 
6714 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6715   TL.setKWLoc(readSourceLocation());
6716 }
6717 
6718 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6719   TypeLocReader TLR(*this);
6720   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6721     TLR.Visit(TL);
6722 }
6723 
6724 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6725   QualType InfoTy = readType();
6726   if (InfoTy.isNull())
6727     return nullptr;
6728 
6729   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6730   readTypeLoc(TInfo->getTypeLoc());
6731   return TInfo;
6732 }
6733 
6734 QualType ASTReader::GetType(TypeID ID) {
6735   assert(ContextObj && "reading type with no AST context");
6736   ASTContext &Context = *ContextObj;
6737 
6738   unsigned FastQuals = ID & Qualifiers::FastMask;
6739   unsigned Index = ID >> Qualifiers::FastWidth;
6740 
6741   if (Index < NUM_PREDEF_TYPE_IDS) {
6742     QualType T;
6743     switch ((PredefinedTypeIDs)Index) {
6744     case PREDEF_TYPE_NULL_ID:
6745       return QualType();
6746     case PREDEF_TYPE_VOID_ID:
6747       T = Context.VoidTy;
6748       break;
6749     case PREDEF_TYPE_BOOL_ID:
6750       T = Context.BoolTy;
6751       break;
6752     case PREDEF_TYPE_CHAR_U_ID:
6753     case PREDEF_TYPE_CHAR_S_ID:
6754       // FIXME: Check that the signedness of CharTy is correct!
6755       T = Context.CharTy;
6756       break;
6757     case PREDEF_TYPE_UCHAR_ID:
6758       T = Context.UnsignedCharTy;
6759       break;
6760     case PREDEF_TYPE_USHORT_ID:
6761       T = Context.UnsignedShortTy;
6762       break;
6763     case PREDEF_TYPE_UINT_ID:
6764       T = Context.UnsignedIntTy;
6765       break;
6766     case PREDEF_TYPE_ULONG_ID:
6767       T = Context.UnsignedLongTy;
6768       break;
6769     case PREDEF_TYPE_ULONGLONG_ID:
6770       T = Context.UnsignedLongLongTy;
6771       break;
6772     case PREDEF_TYPE_UINT128_ID:
6773       T = Context.UnsignedInt128Ty;
6774       break;
6775     case PREDEF_TYPE_SCHAR_ID:
6776       T = Context.SignedCharTy;
6777       break;
6778     case PREDEF_TYPE_WCHAR_ID:
6779       T = Context.WCharTy;
6780       break;
6781     case PREDEF_TYPE_SHORT_ID:
6782       T = Context.ShortTy;
6783       break;
6784     case PREDEF_TYPE_INT_ID:
6785       T = Context.IntTy;
6786       break;
6787     case PREDEF_TYPE_LONG_ID:
6788       T = Context.LongTy;
6789       break;
6790     case PREDEF_TYPE_LONGLONG_ID:
6791       T = Context.LongLongTy;
6792       break;
6793     case PREDEF_TYPE_INT128_ID:
6794       T = Context.Int128Ty;
6795       break;
6796     case PREDEF_TYPE_HALF_ID:
6797       T = Context.HalfTy;
6798       break;
6799     case PREDEF_TYPE_FLOAT_ID:
6800       T = Context.FloatTy;
6801       break;
6802     case PREDEF_TYPE_DOUBLE_ID:
6803       T = Context.DoubleTy;
6804       break;
6805     case PREDEF_TYPE_LONGDOUBLE_ID:
6806       T = Context.LongDoubleTy;
6807       break;
6808     case PREDEF_TYPE_SHORT_ACCUM_ID:
6809       T = Context.ShortAccumTy;
6810       break;
6811     case PREDEF_TYPE_ACCUM_ID:
6812       T = Context.AccumTy;
6813       break;
6814     case PREDEF_TYPE_LONG_ACCUM_ID:
6815       T = Context.LongAccumTy;
6816       break;
6817     case PREDEF_TYPE_USHORT_ACCUM_ID:
6818       T = Context.UnsignedShortAccumTy;
6819       break;
6820     case PREDEF_TYPE_UACCUM_ID:
6821       T = Context.UnsignedAccumTy;
6822       break;
6823     case PREDEF_TYPE_ULONG_ACCUM_ID:
6824       T = Context.UnsignedLongAccumTy;
6825       break;
6826     case PREDEF_TYPE_SHORT_FRACT_ID:
6827       T = Context.ShortFractTy;
6828       break;
6829     case PREDEF_TYPE_FRACT_ID:
6830       T = Context.FractTy;
6831       break;
6832     case PREDEF_TYPE_LONG_FRACT_ID:
6833       T = Context.LongFractTy;
6834       break;
6835     case PREDEF_TYPE_USHORT_FRACT_ID:
6836       T = Context.UnsignedShortFractTy;
6837       break;
6838     case PREDEF_TYPE_UFRACT_ID:
6839       T = Context.UnsignedFractTy;
6840       break;
6841     case PREDEF_TYPE_ULONG_FRACT_ID:
6842       T = Context.UnsignedLongFractTy;
6843       break;
6844     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6845       T = Context.SatShortAccumTy;
6846       break;
6847     case PREDEF_TYPE_SAT_ACCUM_ID:
6848       T = Context.SatAccumTy;
6849       break;
6850     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6851       T = Context.SatLongAccumTy;
6852       break;
6853     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6854       T = Context.SatUnsignedShortAccumTy;
6855       break;
6856     case PREDEF_TYPE_SAT_UACCUM_ID:
6857       T = Context.SatUnsignedAccumTy;
6858       break;
6859     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6860       T = Context.SatUnsignedLongAccumTy;
6861       break;
6862     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6863       T = Context.SatShortFractTy;
6864       break;
6865     case PREDEF_TYPE_SAT_FRACT_ID:
6866       T = Context.SatFractTy;
6867       break;
6868     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6869       T = Context.SatLongFractTy;
6870       break;
6871     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6872       T = Context.SatUnsignedShortFractTy;
6873       break;
6874     case PREDEF_TYPE_SAT_UFRACT_ID:
6875       T = Context.SatUnsignedFractTy;
6876       break;
6877     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6878       T = Context.SatUnsignedLongFractTy;
6879       break;
6880     case PREDEF_TYPE_FLOAT16_ID:
6881       T = Context.Float16Ty;
6882       break;
6883     case PREDEF_TYPE_FLOAT128_ID:
6884       T = Context.Float128Ty;
6885       break;
6886     case PREDEF_TYPE_OVERLOAD_ID:
6887       T = Context.OverloadTy;
6888       break;
6889     case PREDEF_TYPE_BOUND_MEMBER:
6890       T = Context.BoundMemberTy;
6891       break;
6892     case PREDEF_TYPE_PSEUDO_OBJECT:
6893       T = Context.PseudoObjectTy;
6894       break;
6895     case PREDEF_TYPE_DEPENDENT_ID:
6896       T = Context.DependentTy;
6897       break;
6898     case PREDEF_TYPE_UNKNOWN_ANY:
6899       T = Context.UnknownAnyTy;
6900       break;
6901     case PREDEF_TYPE_NULLPTR_ID:
6902       T = Context.NullPtrTy;
6903       break;
6904     case PREDEF_TYPE_CHAR8_ID:
6905       T = Context.Char8Ty;
6906       break;
6907     case PREDEF_TYPE_CHAR16_ID:
6908       T = Context.Char16Ty;
6909       break;
6910     case PREDEF_TYPE_CHAR32_ID:
6911       T = Context.Char32Ty;
6912       break;
6913     case PREDEF_TYPE_OBJC_ID:
6914       T = Context.ObjCBuiltinIdTy;
6915       break;
6916     case PREDEF_TYPE_OBJC_CLASS:
6917       T = Context.ObjCBuiltinClassTy;
6918       break;
6919     case PREDEF_TYPE_OBJC_SEL:
6920       T = Context.ObjCBuiltinSelTy;
6921       break;
6922 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6923     case PREDEF_TYPE_##Id##_ID: \
6924       T = Context.SingletonId; \
6925       break;
6926 #include "clang/Basic/OpenCLImageTypes.def"
6927 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6928     case PREDEF_TYPE_##Id##_ID: \
6929       T = Context.Id##Ty; \
6930       break;
6931 #include "clang/Basic/OpenCLExtensionTypes.def"
6932     case PREDEF_TYPE_SAMPLER_ID:
6933       T = Context.OCLSamplerTy;
6934       break;
6935     case PREDEF_TYPE_EVENT_ID:
6936       T = Context.OCLEventTy;
6937       break;
6938     case PREDEF_TYPE_CLK_EVENT_ID:
6939       T = Context.OCLClkEventTy;
6940       break;
6941     case PREDEF_TYPE_QUEUE_ID:
6942       T = Context.OCLQueueTy;
6943       break;
6944     case PREDEF_TYPE_RESERVE_ID_ID:
6945       T = Context.OCLReserveIDTy;
6946       break;
6947     case PREDEF_TYPE_AUTO_DEDUCT:
6948       T = Context.getAutoDeductType();
6949       break;
6950     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6951       T = Context.getAutoRRefDeductType();
6952       break;
6953     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6954       T = Context.ARCUnbridgedCastTy;
6955       break;
6956     case PREDEF_TYPE_BUILTIN_FN:
6957       T = Context.BuiltinFnTy;
6958       break;
6959     case PREDEF_TYPE_OMP_ARRAY_SECTION:
6960       T = Context.OMPArraySectionTy;
6961       break;
6962     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
6963       T = Context.OMPArraySectionTy;
6964       break;
6965     case PREDEF_TYPE_OMP_ITERATOR:
6966       T = Context.OMPIteratorTy;
6967       break;
6968 #define SVE_TYPE(Name, Id, SingletonId) \
6969     case PREDEF_TYPE_##Id##_ID: \
6970       T = Context.SingletonId; \
6971       break;
6972 #include "clang/Basic/AArch64SVEACLETypes.def"
6973     }
6974 
6975     assert(!T.isNull() && "Unknown predefined type");
6976     return T.withFastQualifiers(FastQuals);
6977   }
6978 
6979   Index -= NUM_PREDEF_TYPE_IDS;
6980   assert(Index < TypesLoaded.size() && "Type index out-of-range");
6981   if (TypesLoaded[Index].isNull()) {
6982     TypesLoaded[Index] = readTypeRecord(Index);
6983     if (TypesLoaded[Index].isNull())
6984       return QualType();
6985 
6986     TypesLoaded[Index]->setFromAST();
6987     if (DeserializationListener)
6988       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6989                                         TypesLoaded[Index]);
6990   }
6991 
6992   return TypesLoaded[Index].withFastQualifiers(FastQuals);
6993 }
6994 
6995 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6996   return GetType(getGlobalTypeID(F, LocalID));
6997 }
6998 
6999 serialization::TypeID
7000 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7001   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7002   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7003 
7004   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7005     return LocalID;
7006 
7007   if (!F.ModuleOffsetMap.empty())
7008     ReadModuleOffsetMap(F);
7009 
7010   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7011     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7012   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7013 
7014   unsigned GlobalIndex = LocalIndex + I->second;
7015   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7016 }
7017 
7018 TemplateArgumentLocInfo
7019 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7020   switch (Kind) {
7021   case TemplateArgument::Expression:
7022     return readExpr();
7023   case TemplateArgument::Type:
7024     return readTypeSourceInfo();
7025   case TemplateArgument::Template: {
7026     NestedNameSpecifierLoc QualifierLoc =
7027       readNestedNameSpecifierLoc();
7028     SourceLocation TemplateNameLoc = readSourceLocation();
7029     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7030                                    SourceLocation());
7031   }
7032   case TemplateArgument::TemplateExpansion: {
7033     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7034     SourceLocation TemplateNameLoc = readSourceLocation();
7035     SourceLocation EllipsisLoc = readSourceLocation();
7036     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7037                                    EllipsisLoc);
7038   }
7039   case TemplateArgument::Null:
7040   case TemplateArgument::Integral:
7041   case TemplateArgument::Declaration:
7042   case TemplateArgument::NullPtr:
7043   case TemplateArgument::Pack:
7044     // FIXME: Is this right?
7045     return TemplateArgumentLocInfo();
7046   }
7047   llvm_unreachable("unexpected template argument loc");
7048 }
7049 
7050 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7051   TemplateArgument Arg = readTemplateArgument();
7052 
7053   if (Arg.getKind() == TemplateArgument::Expression) {
7054     if (readBool()) // bool InfoHasSameExpr.
7055       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7056   }
7057   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7058 }
7059 
7060 const ASTTemplateArgumentListInfo *
7061 ASTRecordReader::readASTTemplateArgumentListInfo() {
7062   SourceLocation LAngleLoc = readSourceLocation();
7063   SourceLocation RAngleLoc = readSourceLocation();
7064   unsigned NumArgsAsWritten = readInt();
7065   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7066   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7067     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7068   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7069 }
7070 
7071 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7072   return GetDecl(ID);
7073 }
7074 
7075 void ASTReader::CompleteRedeclChain(const Decl *D) {
7076   if (NumCurrentElementsDeserializing) {
7077     // We arrange to not care about the complete redeclaration chain while we're
7078     // deserializing. Just remember that the AST has marked this one as complete
7079     // but that it's not actually complete yet, so we know we still need to
7080     // complete it later.
7081     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7082     return;
7083   }
7084 
7085   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7086 
7087   // If this is a named declaration, complete it by looking it up
7088   // within its context.
7089   //
7090   // FIXME: Merging a function definition should merge
7091   // all mergeable entities within it.
7092   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7093       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7094     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7095       if (!getContext().getLangOpts().CPlusPlus &&
7096           isa<TranslationUnitDecl>(DC)) {
7097         // Outside of C++, we don't have a lookup table for the TU, so update
7098         // the identifier instead. (For C++ modules, we don't store decls
7099         // in the serialized identifier table, so we do the lookup in the TU.)
7100         auto *II = Name.getAsIdentifierInfo();
7101         assert(II && "non-identifier name in C?");
7102         if (II->isOutOfDate())
7103           updateOutOfDateIdentifier(*II);
7104       } else
7105         DC->lookup(Name);
7106     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7107       // Find all declarations of this kind from the relevant context.
7108       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7109         auto *DC = cast<DeclContext>(DCDecl);
7110         SmallVector<Decl*, 8> Decls;
7111         FindExternalLexicalDecls(
7112             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7113       }
7114     }
7115   }
7116 
7117   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7118     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7119   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7120     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7121   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7122     if (auto *Template = FD->getPrimaryTemplate())
7123       Template->LoadLazySpecializations();
7124   }
7125 }
7126 
7127 CXXCtorInitializer **
7128 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7129   RecordLocation Loc = getLocalBitOffset(Offset);
7130   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7131   SavedStreamPosition SavedPosition(Cursor);
7132   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7133     Error(std::move(Err));
7134     return nullptr;
7135   }
7136   ReadingKindTracker ReadingKind(Read_Decl, *this);
7137 
7138   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7139   if (!MaybeCode) {
7140     Error(MaybeCode.takeError());
7141     return nullptr;
7142   }
7143   unsigned Code = MaybeCode.get();
7144 
7145   ASTRecordReader Record(*this, *Loc.F);
7146   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7147   if (!MaybeRecCode) {
7148     Error(MaybeRecCode.takeError());
7149     return nullptr;
7150   }
7151   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7152     Error("malformed AST file: missing C++ ctor initializers");
7153     return nullptr;
7154   }
7155 
7156   return Record.readCXXCtorInitializers();
7157 }
7158 
7159 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7160   assert(ContextObj && "reading base specifiers with no AST context");
7161   ASTContext &Context = *ContextObj;
7162 
7163   RecordLocation Loc = getLocalBitOffset(Offset);
7164   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7165   SavedStreamPosition SavedPosition(Cursor);
7166   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7167     Error(std::move(Err));
7168     return nullptr;
7169   }
7170   ReadingKindTracker ReadingKind(Read_Decl, *this);
7171 
7172   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7173   if (!MaybeCode) {
7174     Error(MaybeCode.takeError());
7175     return nullptr;
7176   }
7177   unsigned Code = MaybeCode.get();
7178 
7179   ASTRecordReader Record(*this, *Loc.F);
7180   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7181   if (!MaybeRecCode) {
7182     Error(MaybeCode.takeError());
7183     return nullptr;
7184   }
7185   unsigned RecCode = MaybeRecCode.get();
7186 
7187   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7188     Error("malformed AST file: missing C++ base specifiers");
7189     return nullptr;
7190   }
7191 
7192   unsigned NumBases = Record.readInt();
7193   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7194   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7195   for (unsigned I = 0; I != NumBases; ++I)
7196     Bases[I] = Record.readCXXBaseSpecifier();
7197   return Bases;
7198 }
7199 
7200 serialization::DeclID
7201 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7202   if (LocalID < NUM_PREDEF_DECL_IDS)
7203     return LocalID;
7204 
7205   if (!F.ModuleOffsetMap.empty())
7206     ReadModuleOffsetMap(F);
7207 
7208   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7209     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7210   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7211 
7212   return LocalID + I->second;
7213 }
7214 
7215 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7216                                    ModuleFile &M) const {
7217   // Predefined decls aren't from any module.
7218   if (ID < NUM_PREDEF_DECL_IDS)
7219     return false;
7220 
7221   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7222          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7223 }
7224 
7225 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7226   if (!D->isFromASTFile())
7227     return nullptr;
7228   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7229   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7230   return I->second;
7231 }
7232 
7233 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7234   if (ID < NUM_PREDEF_DECL_IDS)
7235     return SourceLocation();
7236 
7237   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7238 
7239   if (Index > DeclsLoaded.size()) {
7240     Error("declaration ID out-of-range for AST file");
7241     return SourceLocation();
7242   }
7243 
7244   if (Decl *D = DeclsLoaded[Index])
7245     return D->getLocation();
7246 
7247   SourceLocation Loc;
7248   DeclCursorForID(ID, Loc);
7249   return Loc;
7250 }
7251 
7252 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7253   switch (ID) {
7254   case PREDEF_DECL_NULL_ID:
7255     return nullptr;
7256 
7257   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7258     return Context.getTranslationUnitDecl();
7259 
7260   case PREDEF_DECL_OBJC_ID_ID:
7261     return Context.getObjCIdDecl();
7262 
7263   case PREDEF_DECL_OBJC_SEL_ID:
7264     return Context.getObjCSelDecl();
7265 
7266   case PREDEF_DECL_OBJC_CLASS_ID:
7267     return Context.getObjCClassDecl();
7268 
7269   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7270     return Context.getObjCProtocolDecl();
7271 
7272   case PREDEF_DECL_INT_128_ID:
7273     return Context.getInt128Decl();
7274 
7275   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7276     return Context.getUInt128Decl();
7277 
7278   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7279     return Context.getObjCInstanceTypeDecl();
7280 
7281   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7282     return Context.getBuiltinVaListDecl();
7283 
7284   case PREDEF_DECL_VA_LIST_TAG:
7285     return Context.getVaListTagDecl();
7286 
7287   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7288     return Context.getBuiltinMSVaListDecl();
7289 
7290   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7291     return Context.getExternCContextDecl();
7292 
7293   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7294     return Context.getMakeIntegerSeqDecl();
7295 
7296   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7297     return Context.getCFConstantStringDecl();
7298 
7299   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7300     return Context.getCFConstantStringTagDecl();
7301 
7302   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7303     return Context.getTypePackElementDecl();
7304   }
7305   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7306 }
7307 
7308 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7309   assert(ContextObj && "reading decl with no AST context");
7310   if (ID < NUM_PREDEF_DECL_IDS) {
7311     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7312     if (D) {
7313       // Track that we have merged the declaration with ID \p ID into the
7314       // pre-existing predefined declaration \p D.
7315       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7316       if (Merged.empty())
7317         Merged.push_back(ID);
7318     }
7319     return D;
7320   }
7321 
7322   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7323 
7324   if (Index >= DeclsLoaded.size()) {
7325     assert(0 && "declaration ID out-of-range for AST file");
7326     Error("declaration ID out-of-range for AST file");
7327     return nullptr;
7328   }
7329 
7330   return DeclsLoaded[Index];
7331 }
7332 
7333 Decl *ASTReader::GetDecl(DeclID ID) {
7334   if (ID < NUM_PREDEF_DECL_IDS)
7335     return GetExistingDecl(ID);
7336 
7337   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7338 
7339   if (Index >= DeclsLoaded.size()) {
7340     assert(0 && "declaration ID out-of-range for AST file");
7341     Error("declaration ID out-of-range for AST file");
7342     return nullptr;
7343   }
7344 
7345   if (!DeclsLoaded[Index]) {
7346     ReadDeclRecord(ID);
7347     if (DeserializationListener)
7348       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7349   }
7350 
7351   return DeclsLoaded[Index];
7352 }
7353 
7354 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7355                                                   DeclID GlobalID) {
7356   if (GlobalID < NUM_PREDEF_DECL_IDS)
7357     return GlobalID;
7358 
7359   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7360   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7361   ModuleFile *Owner = I->second;
7362 
7363   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7364     = M.GlobalToLocalDeclIDs.find(Owner);
7365   if (Pos == M.GlobalToLocalDeclIDs.end())
7366     return 0;
7367 
7368   return GlobalID - Owner->BaseDeclID + Pos->second;
7369 }
7370 
7371 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7372                                             const RecordData &Record,
7373                                             unsigned &Idx) {
7374   if (Idx >= Record.size()) {
7375     Error("Corrupted AST file");
7376     return 0;
7377   }
7378 
7379   return getGlobalDeclID(F, Record[Idx++]);
7380 }
7381 
7382 /// Resolve the offset of a statement into a statement.
7383 ///
7384 /// This operation will read a new statement from the external
7385 /// source each time it is called, and is meant to be used via a
7386 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7387 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7388   // Switch case IDs are per Decl.
7389   ClearSwitchCaseIDs();
7390 
7391   // Offset here is a global offset across the entire chain.
7392   RecordLocation Loc = getLocalBitOffset(Offset);
7393   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7394     Error(std::move(Err));
7395     return nullptr;
7396   }
7397   assert(NumCurrentElementsDeserializing == 0 &&
7398          "should not be called while already deserializing");
7399   Deserializing D(this);
7400   return ReadStmtFromStream(*Loc.F);
7401 }
7402 
7403 void ASTReader::FindExternalLexicalDecls(
7404     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7405     SmallVectorImpl<Decl *> &Decls) {
7406   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7407 
7408   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7409     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7410     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7411       auto K = (Decl::Kind)+LexicalDecls[I];
7412       if (!IsKindWeWant(K))
7413         continue;
7414 
7415       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7416 
7417       // Don't add predefined declarations to the lexical context more
7418       // than once.
7419       if (ID < NUM_PREDEF_DECL_IDS) {
7420         if (PredefsVisited[ID])
7421           continue;
7422 
7423         PredefsVisited[ID] = true;
7424       }
7425 
7426       if (Decl *D = GetLocalDecl(*M, ID)) {
7427         assert(D->getKind() == K && "wrong kind for lexical decl");
7428         if (!DC->isDeclInLexicalTraversal(D))
7429           Decls.push_back(D);
7430       }
7431     }
7432   };
7433 
7434   if (isa<TranslationUnitDecl>(DC)) {
7435     for (auto Lexical : TULexicalDecls)
7436       Visit(Lexical.first, Lexical.second);
7437   } else {
7438     auto I = LexicalDecls.find(DC);
7439     if (I != LexicalDecls.end())
7440       Visit(I->second.first, I->second.second);
7441   }
7442 
7443   ++NumLexicalDeclContextsRead;
7444 }
7445 
7446 namespace {
7447 
7448 class DeclIDComp {
7449   ASTReader &Reader;
7450   ModuleFile &Mod;
7451 
7452 public:
7453   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7454 
7455   bool operator()(LocalDeclID L, LocalDeclID R) const {
7456     SourceLocation LHS = getLocation(L);
7457     SourceLocation RHS = getLocation(R);
7458     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7459   }
7460 
7461   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7462     SourceLocation RHS = getLocation(R);
7463     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7464   }
7465 
7466   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7467     SourceLocation LHS = getLocation(L);
7468     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7469   }
7470 
7471   SourceLocation getLocation(LocalDeclID ID) const {
7472     return Reader.getSourceManager().getFileLoc(
7473             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7474   }
7475 };
7476 
7477 } // namespace
7478 
7479 void ASTReader::FindFileRegionDecls(FileID File,
7480                                     unsigned Offset, unsigned Length,
7481                                     SmallVectorImpl<Decl *> &Decls) {
7482   SourceManager &SM = getSourceManager();
7483 
7484   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7485   if (I == FileDeclIDs.end())
7486     return;
7487 
7488   FileDeclsInfo &DInfo = I->second;
7489   if (DInfo.Decls.empty())
7490     return;
7491 
7492   SourceLocation
7493     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7494   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7495 
7496   DeclIDComp DIDComp(*this, *DInfo.Mod);
7497   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7498       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7499   if (BeginIt != DInfo.Decls.begin())
7500     --BeginIt;
7501 
7502   // If we are pointing at a top-level decl inside an objc container, we need
7503   // to backtrack until we find it otherwise we will fail to report that the
7504   // region overlaps with an objc container.
7505   while (BeginIt != DInfo.Decls.begin() &&
7506          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7507              ->isTopLevelDeclInObjCContainer())
7508     --BeginIt;
7509 
7510   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7511       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7512   if (EndIt != DInfo.Decls.end())
7513     ++EndIt;
7514 
7515   for (ArrayRef<serialization::LocalDeclID>::iterator
7516          DIt = BeginIt; DIt != EndIt; ++DIt)
7517     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7518 }
7519 
7520 bool
7521 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7522                                           DeclarationName Name) {
7523   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7524          "DeclContext has no visible decls in storage");
7525   if (!Name)
7526     return false;
7527 
7528   auto It = Lookups.find(DC);
7529   if (It == Lookups.end())
7530     return false;
7531 
7532   Deserializing LookupResults(this);
7533 
7534   // Load the list of declarations.
7535   SmallVector<NamedDecl *, 64> Decls;
7536   for (DeclID ID : It->second.Table.find(Name)) {
7537     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7538     if (ND->getDeclName() == Name)
7539       Decls.push_back(ND);
7540   }
7541 
7542   ++NumVisibleDeclContextsRead;
7543   SetExternalVisibleDeclsForName(DC, Name, Decls);
7544   return !Decls.empty();
7545 }
7546 
7547 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7548   if (!DC->hasExternalVisibleStorage())
7549     return;
7550 
7551   auto It = Lookups.find(DC);
7552   assert(It != Lookups.end() &&
7553          "have external visible storage but no lookup tables");
7554 
7555   DeclsMap Decls;
7556 
7557   for (DeclID ID : It->second.Table.findAll()) {
7558     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7559     Decls[ND->getDeclName()].push_back(ND);
7560   }
7561 
7562   ++NumVisibleDeclContextsRead;
7563 
7564   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7565     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7566   }
7567   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7568 }
7569 
7570 const serialization::reader::DeclContextLookupTable *
7571 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7572   auto I = Lookups.find(Primary);
7573   return I == Lookups.end() ? nullptr : &I->second;
7574 }
7575 
7576 /// Under non-PCH compilation the consumer receives the objc methods
7577 /// before receiving the implementation, and codegen depends on this.
7578 /// We simulate this by deserializing and passing to consumer the methods of the
7579 /// implementation before passing the deserialized implementation decl.
7580 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7581                                        ASTConsumer *Consumer) {
7582   assert(ImplD && Consumer);
7583 
7584   for (auto *I : ImplD->methods())
7585     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7586 
7587   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7588 }
7589 
7590 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7591   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7592     PassObjCImplDeclToConsumer(ImplD, Consumer);
7593   else
7594     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7595 }
7596 
7597 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7598   this->Consumer = Consumer;
7599 
7600   if (Consumer)
7601     PassInterestingDeclsToConsumer();
7602 
7603   if (DeserializationListener)
7604     DeserializationListener->ReaderInitialized(this);
7605 }
7606 
7607 void ASTReader::PrintStats() {
7608   std::fprintf(stderr, "*** AST File Statistics:\n");
7609 
7610   unsigned NumTypesLoaded
7611     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7612                                       QualType());
7613   unsigned NumDeclsLoaded
7614     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7615                                       (Decl *)nullptr);
7616   unsigned NumIdentifiersLoaded
7617     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7618                                             IdentifiersLoaded.end(),
7619                                             (IdentifierInfo *)nullptr);
7620   unsigned NumMacrosLoaded
7621     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7622                                        MacrosLoaded.end(),
7623                                        (MacroInfo *)nullptr);
7624   unsigned NumSelectorsLoaded
7625     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7626                                           SelectorsLoaded.end(),
7627                                           Selector());
7628 
7629   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7630     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7631                  NumSLocEntriesRead, TotalNumSLocEntries,
7632                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7633   if (!TypesLoaded.empty())
7634     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7635                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7636                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7637   if (!DeclsLoaded.empty())
7638     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7639                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7640                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7641   if (!IdentifiersLoaded.empty())
7642     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7643                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7644                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7645   if (!MacrosLoaded.empty())
7646     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7647                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7648                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7649   if (!SelectorsLoaded.empty())
7650     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7651                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7652                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7653   if (TotalNumStatements)
7654     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7655                  NumStatementsRead, TotalNumStatements,
7656                  ((float)NumStatementsRead/TotalNumStatements * 100));
7657   if (TotalNumMacros)
7658     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7659                  NumMacrosRead, TotalNumMacros,
7660                  ((float)NumMacrosRead/TotalNumMacros * 100));
7661   if (TotalLexicalDeclContexts)
7662     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7663                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7664                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7665                   * 100));
7666   if (TotalVisibleDeclContexts)
7667     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7668                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7669                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7670                   * 100));
7671   if (TotalNumMethodPoolEntries)
7672     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7673                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7674                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7675                   * 100));
7676   if (NumMethodPoolLookups)
7677     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7678                  NumMethodPoolHits, NumMethodPoolLookups,
7679                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7680   if (NumMethodPoolTableLookups)
7681     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7682                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7683                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7684                   * 100.0));
7685   if (NumIdentifierLookupHits)
7686     std::fprintf(stderr,
7687                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7688                  NumIdentifierLookupHits, NumIdentifierLookups,
7689                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7690 
7691   if (GlobalIndex) {
7692     std::fprintf(stderr, "\n");
7693     GlobalIndex->printStats();
7694   }
7695 
7696   std::fprintf(stderr, "\n");
7697   dump();
7698   std::fprintf(stderr, "\n");
7699 }
7700 
7701 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7702 LLVM_DUMP_METHOD static void
7703 dumpModuleIDMap(StringRef Name,
7704                 const ContinuousRangeMap<Key, ModuleFile *,
7705                                          InitialCapacity> &Map) {
7706   if (Map.begin() == Map.end())
7707     return;
7708 
7709   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7710 
7711   llvm::errs() << Name << ":\n";
7712   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7713        I != IEnd; ++I) {
7714     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7715       << "\n";
7716   }
7717 }
7718 
7719 LLVM_DUMP_METHOD void ASTReader::dump() {
7720   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7721   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7722   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7723   dumpModuleIDMap("Global type map", GlobalTypeMap);
7724   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7725   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7726   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7727   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7728   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7729   dumpModuleIDMap("Global preprocessed entity map",
7730                   GlobalPreprocessedEntityMap);
7731 
7732   llvm::errs() << "\n*** PCH/Modules Loaded:";
7733   for (ModuleFile &M : ModuleMgr)
7734     M.dump();
7735 }
7736 
7737 /// Return the amount of memory used by memory buffers, breaking down
7738 /// by heap-backed versus mmap'ed memory.
7739 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7740   for (ModuleFile &I : ModuleMgr) {
7741     if (llvm::MemoryBuffer *buf = I.Buffer) {
7742       size_t bytes = buf->getBufferSize();
7743       switch (buf->getBufferKind()) {
7744         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7745           sizes.malloc_bytes += bytes;
7746           break;
7747         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7748           sizes.mmap_bytes += bytes;
7749           break;
7750       }
7751     }
7752   }
7753 }
7754 
7755 void ASTReader::InitializeSema(Sema &S) {
7756   SemaObj = &S;
7757   S.addExternalSource(this);
7758 
7759   // Makes sure any declarations that were deserialized "too early"
7760   // still get added to the identifier's declaration chains.
7761   for (uint64_t ID : PreloadedDeclIDs) {
7762     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7763     pushExternalDeclIntoScope(D, D->getDeclName());
7764   }
7765   PreloadedDeclIDs.clear();
7766 
7767   // FIXME: What happens if these are changed by a module import?
7768   if (!FPPragmaOptions.empty()) {
7769     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7770     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7771   }
7772 
7773   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7774   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7775   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7776 
7777   UpdateSema();
7778 }
7779 
7780 void ASTReader::UpdateSema() {
7781   assert(SemaObj && "no Sema to update");
7782 
7783   // Load the offsets of the declarations that Sema references.
7784   // They will be lazily deserialized when needed.
7785   if (!SemaDeclRefs.empty()) {
7786     assert(SemaDeclRefs.size() % 3 == 0);
7787     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7788       if (!SemaObj->StdNamespace)
7789         SemaObj->StdNamespace = SemaDeclRefs[I];
7790       if (!SemaObj->StdBadAlloc)
7791         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7792       if (!SemaObj->StdAlignValT)
7793         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7794     }
7795     SemaDeclRefs.clear();
7796   }
7797 
7798   // Update the state of pragmas. Use the same API as if we had encountered the
7799   // pragma in the source.
7800   if(OptimizeOffPragmaLocation.isValid())
7801     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7802   if (PragmaMSStructState != -1)
7803     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7804   if (PointersToMembersPragmaLocation.isValid()) {
7805     SemaObj->ActOnPragmaMSPointersToMembers(
7806         (LangOptions::PragmaMSPointersToMembersKind)
7807             PragmaMSPointersToMembersState,
7808         PointersToMembersPragmaLocation);
7809   }
7810   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7811 
7812   if (PragmaPackCurrentValue) {
7813     // The bottom of the stack might have a default value. It must be adjusted
7814     // to the current value to ensure that the packing state is preserved after
7815     // popping entries that were included/imported from a PCH/module.
7816     bool DropFirst = false;
7817     if (!PragmaPackStack.empty() &&
7818         PragmaPackStack.front().Location.isInvalid()) {
7819       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7820              "Expected a default alignment value");
7821       SemaObj->PackStack.Stack.emplace_back(
7822           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7823           SemaObj->PackStack.CurrentPragmaLocation,
7824           PragmaPackStack.front().PushLocation);
7825       DropFirst = true;
7826     }
7827     for (const auto &Entry :
7828          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7829       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7830                                             Entry.Location, Entry.PushLocation);
7831     if (PragmaPackCurrentLocation.isInvalid()) {
7832       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7833              "Expected a default alignment value");
7834       // Keep the current values.
7835     } else {
7836       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7837       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7838     }
7839   }
7840 }
7841 
7842 IdentifierInfo *ASTReader::get(StringRef Name) {
7843   // Note that we are loading an identifier.
7844   Deserializing AnIdentifier(this);
7845 
7846   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7847                                   NumIdentifierLookups,
7848                                   NumIdentifierLookupHits);
7849 
7850   // We don't need to do identifier table lookups in C++ modules (we preload
7851   // all interesting declarations, and don't need to use the scope for name
7852   // lookups). Perform the lookup in PCH files, though, since we don't build
7853   // a complete initial identifier table if we're carrying on from a PCH.
7854   if (PP.getLangOpts().CPlusPlus) {
7855     for (auto F : ModuleMgr.pch_modules())
7856       if (Visitor(*F))
7857         break;
7858   } else {
7859     // If there is a global index, look there first to determine which modules
7860     // provably do not have any results for this identifier.
7861     GlobalModuleIndex::HitSet Hits;
7862     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7863     if (!loadGlobalIndex()) {
7864       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7865         HitsPtr = &Hits;
7866       }
7867     }
7868 
7869     ModuleMgr.visit(Visitor, HitsPtr);
7870   }
7871 
7872   IdentifierInfo *II = Visitor.getIdentifierInfo();
7873   markIdentifierUpToDate(II);
7874   return II;
7875 }
7876 
7877 namespace clang {
7878 
7879   /// An identifier-lookup iterator that enumerates all of the
7880   /// identifiers stored within a set of AST files.
7881   class ASTIdentifierIterator : public IdentifierIterator {
7882     /// The AST reader whose identifiers are being enumerated.
7883     const ASTReader &Reader;
7884 
7885     /// The current index into the chain of AST files stored in
7886     /// the AST reader.
7887     unsigned Index;
7888 
7889     /// The current position within the identifier lookup table
7890     /// of the current AST file.
7891     ASTIdentifierLookupTable::key_iterator Current;
7892 
7893     /// The end position within the identifier lookup table of
7894     /// the current AST file.
7895     ASTIdentifierLookupTable::key_iterator End;
7896 
7897     /// Whether to skip any modules in the ASTReader.
7898     bool SkipModules;
7899 
7900   public:
7901     explicit ASTIdentifierIterator(const ASTReader &Reader,
7902                                    bool SkipModules = false);
7903 
7904     StringRef Next() override;
7905   };
7906 
7907 } // namespace clang
7908 
7909 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7910                                              bool SkipModules)
7911     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7912 }
7913 
7914 StringRef ASTIdentifierIterator::Next() {
7915   while (Current == End) {
7916     // If we have exhausted all of our AST files, we're done.
7917     if (Index == 0)
7918       return StringRef();
7919 
7920     --Index;
7921     ModuleFile &F = Reader.ModuleMgr[Index];
7922     if (SkipModules && F.isModule())
7923       continue;
7924 
7925     ASTIdentifierLookupTable *IdTable =
7926         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7927     Current = IdTable->key_begin();
7928     End = IdTable->key_end();
7929   }
7930 
7931   // We have any identifiers remaining in the current AST file; return
7932   // the next one.
7933   StringRef Result = *Current;
7934   ++Current;
7935   return Result;
7936 }
7937 
7938 namespace {
7939 
7940 /// A utility for appending two IdentifierIterators.
7941 class ChainedIdentifierIterator : public IdentifierIterator {
7942   std::unique_ptr<IdentifierIterator> Current;
7943   std::unique_ptr<IdentifierIterator> Queued;
7944 
7945 public:
7946   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7947                             std::unique_ptr<IdentifierIterator> Second)
7948       : Current(std::move(First)), Queued(std::move(Second)) {}
7949 
7950   StringRef Next() override {
7951     if (!Current)
7952       return StringRef();
7953 
7954     StringRef result = Current->Next();
7955     if (!result.empty())
7956       return result;
7957 
7958     // Try the queued iterator, which may itself be empty.
7959     Current.reset();
7960     std::swap(Current, Queued);
7961     return Next();
7962   }
7963 };
7964 
7965 } // namespace
7966 
7967 IdentifierIterator *ASTReader::getIdentifiers() {
7968   if (!loadGlobalIndex()) {
7969     std::unique_ptr<IdentifierIterator> ReaderIter(
7970         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7971     std::unique_ptr<IdentifierIterator> ModulesIter(
7972         GlobalIndex->createIdentifierIterator());
7973     return new ChainedIdentifierIterator(std::move(ReaderIter),
7974                                          std::move(ModulesIter));
7975   }
7976 
7977   return new ASTIdentifierIterator(*this);
7978 }
7979 
7980 namespace clang {
7981 namespace serialization {
7982 
7983   class ReadMethodPoolVisitor {
7984     ASTReader &Reader;
7985     Selector Sel;
7986     unsigned PriorGeneration;
7987     unsigned InstanceBits = 0;
7988     unsigned FactoryBits = 0;
7989     bool InstanceHasMoreThanOneDecl = false;
7990     bool FactoryHasMoreThanOneDecl = false;
7991     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7992     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7993 
7994   public:
7995     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7996                           unsigned PriorGeneration)
7997         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
7998 
7999     bool operator()(ModuleFile &M) {
8000       if (!M.SelectorLookupTable)
8001         return false;
8002 
8003       // If we've already searched this module file, skip it now.
8004       if (M.Generation <= PriorGeneration)
8005         return true;
8006 
8007       ++Reader.NumMethodPoolTableLookups;
8008       ASTSelectorLookupTable *PoolTable
8009         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8010       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8011       if (Pos == PoolTable->end())
8012         return false;
8013 
8014       ++Reader.NumMethodPoolTableHits;
8015       ++Reader.NumSelectorsRead;
8016       // FIXME: Not quite happy with the statistics here. We probably should
8017       // disable this tracking when called via LoadSelector.
8018       // Also, should entries without methods count as misses?
8019       ++Reader.NumMethodPoolEntriesRead;
8020       ASTSelectorLookupTrait::data_type Data = *Pos;
8021       if (Reader.DeserializationListener)
8022         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8023 
8024       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8025       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8026       InstanceBits = Data.InstanceBits;
8027       FactoryBits = Data.FactoryBits;
8028       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8029       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8030       return true;
8031     }
8032 
8033     /// Retrieve the instance methods found by this visitor.
8034     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8035       return InstanceMethods;
8036     }
8037 
8038     /// Retrieve the instance methods found by this visitor.
8039     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8040       return FactoryMethods;
8041     }
8042 
8043     unsigned getInstanceBits() const { return InstanceBits; }
8044     unsigned getFactoryBits() const { return FactoryBits; }
8045 
8046     bool instanceHasMoreThanOneDecl() const {
8047       return InstanceHasMoreThanOneDecl;
8048     }
8049 
8050     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8051   };
8052 
8053 } // namespace serialization
8054 } // namespace clang
8055 
8056 /// Add the given set of methods to the method list.
8057 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8058                              ObjCMethodList &List) {
8059   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8060     S.addMethodToGlobalList(&List, Methods[I]);
8061   }
8062 }
8063 
8064 void ASTReader::ReadMethodPool(Selector Sel) {
8065   // Get the selector generation and update it to the current generation.
8066   unsigned &Generation = SelectorGeneration[Sel];
8067   unsigned PriorGeneration = Generation;
8068   Generation = getGeneration();
8069   SelectorOutOfDate[Sel] = false;
8070 
8071   // Search for methods defined with this selector.
8072   ++NumMethodPoolLookups;
8073   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8074   ModuleMgr.visit(Visitor);
8075 
8076   if (Visitor.getInstanceMethods().empty() &&
8077       Visitor.getFactoryMethods().empty())
8078     return;
8079 
8080   ++NumMethodPoolHits;
8081 
8082   if (!getSema())
8083     return;
8084 
8085   Sema &S = *getSema();
8086   Sema::GlobalMethodPool::iterator Pos
8087     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8088 
8089   Pos->second.first.setBits(Visitor.getInstanceBits());
8090   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8091   Pos->second.second.setBits(Visitor.getFactoryBits());
8092   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8093 
8094   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8095   // when building a module we keep every method individually and may need to
8096   // update hasMoreThanOneDecl as we add the methods.
8097   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8098   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8099 }
8100 
8101 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8102   if (SelectorOutOfDate[Sel])
8103     ReadMethodPool(Sel);
8104 }
8105 
8106 void ASTReader::ReadKnownNamespaces(
8107                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8108   Namespaces.clear();
8109 
8110   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8111     if (NamespaceDecl *Namespace
8112                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8113       Namespaces.push_back(Namespace);
8114   }
8115 }
8116 
8117 void ASTReader::ReadUndefinedButUsed(
8118     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8119   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8120     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8121     SourceLocation Loc =
8122         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8123     Undefined.insert(std::make_pair(D, Loc));
8124   }
8125 }
8126 
8127 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8128     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8129                                                      Exprs) {
8130   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8131     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8132     uint64_t Count = DelayedDeleteExprs[Idx++];
8133     for (uint64_t C = 0; C < Count; ++C) {
8134       SourceLocation DeleteLoc =
8135           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8136       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8137       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8138     }
8139   }
8140 }
8141 
8142 void ASTReader::ReadTentativeDefinitions(
8143                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8144   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8145     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8146     if (Var)
8147       TentativeDefs.push_back(Var);
8148   }
8149   TentativeDefinitions.clear();
8150 }
8151 
8152 void ASTReader::ReadUnusedFileScopedDecls(
8153                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8154   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8155     DeclaratorDecl *D
8156       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8157     if (D)
8158       Decls.push_back(D);
8159   }
8160   UnusedFileScopedDecls.clear();
8161 }
8162 
8163 void ASTReader::ReadDelegatingConstructors(
8164                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8165   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8166     CXXConstructorDecl *D
8167       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8168     if (D)
8169       Decls.push_back(D);
8170   }
8171   DelegatingCtorDecls.clear();
8172 }
8173 
8174 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8175   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8176     TypedefNameDecl *D
8177       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8178     if (D)
8179       Decls.push_back(D);
8180   }
8181   ExtVectorDecls.clear();
8182 }
8183 
8184 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8185     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8186   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8187        ++I) {
8188     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8189         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8190     if (D)
8191       Decls.insert(D);
8192   }
8193   UnusedLocalTypedefNameCandidates.clear();
8194 }
8195 
8196 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8197     llvm::SmallVector<Decl *, 4> &Decls) {
8198   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8199        ++I) {
8200     auto *D = dyn_cast_or_null<Decl>(
8201         GetDecl(DeclsToCheckForDeferredDiags[I]));
8202     if (D)
8203       Decls.push_back(D);
8204   }
8205   DeclsToCheckForDeferredDiags.clear();
8206 }
8207 
8208 
8209 void ASTReader::ReadReferencedSelectors(
8210        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8211   if (ReferencedSelectorsData.empty())
8212     return;
8213 
8214   // If there are @selector references added them to its pool. This is for
8215   // implementation of -Wselector.
8216   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8217   unsigned I = 0;
8218   while (I < DataSize) {
8219     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8220     SourceLocation SelLoc
8221       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8222     Sels.push_back(std::make_pair(Sel, SelLoc));
8223   }
8224   ReferencedSelectorsData.clear();
8225 }
8226 
8227 void ASTReader::ReadWeakUndeclaredIdentifiers(
8228        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8229   if (WeakUndeclaredIdentifiers.empty())
8230     return;
8231 
8232   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8233     IdentifierInfo *WeakId
8234       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8235     IdentifierInfo *AliasId
8236       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8237     SourceLocation Loc
8238       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8239     bool Used = WeakUndeclaredIdentifiers[I++];
8240     WeakInfo WI(AliasId, Loc);
8241     WI.setUsed(Used);
8242     WeakIDs.push_back(std::make_pair(WeakId, WI));
8243   }
8244   WeakUndeclaredIdentifiers.clear();
8245 }
8246 
8247 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8248   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8249     ExternalVTableUse VT;
8250     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8251     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8252     VT.DefinitionRequired = VTableUses[Idx++];
8253     VTables.push_back(VT);
8254   }
8255 
8256   VTableUses.clear();
8257 }
8258 
8259 void ASTReader::ReadPendingInstantiations(
8260        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8261   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8262     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8263     SourceLocation Loc
8264       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8265 
8266     Pending.push_back(std::make_pair(D, Loc));
8267   }
8268   PendingInstantiations.clear();
8269 }
8270 
8271 void ASTReader::ReadLateParsedTemplates(
8272     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8273         &LPTMap) {
8274   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8275        /* In loop */) {
8276     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8277 
8278     auto LT = std::make_unique<LateParsedTemplate>();
8279     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8280 
8281     ModuleFile *F = getOwningModuleFile(LT->D);
8282     assert(F && "No module");
8283 
8284     unsigned TokN = LateParsedTemplates[Idx++];
8285     LT->Toks.reserve(TokN);
8286     for (unsigned T = 0; T < TokN; ++T)
8287       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8288 
8289     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8290   }
8291 
8292   LateParsedTemplates.clear();
8293 }
8294 
8295 void ASTReader::LoadSelector(Selector Sel) {
8296   // It would be complicated to avoid reading the methods anyway. So don't.
8297   ReadMethodPool(Sel);
8298 }
8299 
8300 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8301   assert(ID && "Non-zero identifier ID required");
8302   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8303   IdentifiersLoaded[ID - 1] = II;
8304   if (DeserializationListener)
8305     DeserializationListener->IdentifierRead(ID, II);
8306 }
8307 
8308 /// Set the globally-visible declarations associated with the given
8309 /// identifier.
8310 ///
8311 /// If the AST reader is currently in a state where the given declaration IDs
8312 /// cannot safely be resolved, they are queued until it is safe to resolve
8313 /// them.
8314 ///
8315 /// \param II an IdentifierInfo that refers to one or more globally-visible
8316 /// declarations.
8317 ///
8318 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8319 /// visible at global scope.
8320 ///
8321 /// \param Decls if non-null, this vector will be populated with the set of
8322 /// deserialized declarations. These declarations will not be pushed into
8323 /// scope.
8324 void
8325 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8326                               const SmallVectorImpl<uint32_t> &DeclIDs,
8327                                    SmallVectorImpl<Decl *> *Decls) {
8328   if (NumCurrentElementsDeserializing && !Decls) {
8329     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8330     return;
8331   }
8332 
8333   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8334     if (!SemaObj) {
8335       // Queue this declaration so that it will be added to the
8336       // translation unit scope and identifier's declaration chain
8337       // once a Sema object is known.
8338       PreloadedDeclIDs.push_back(DeclIDs[I]);
8339       continue;
8340     }
8341 
8342     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8343 
8344     // If we're simply supposed to record the declarations, do so now.
8345     if (Decls) {
8346       Decls->push_back(D);
8347       continue;
8348     }
8349 
8350     // Introduce this declaration into the translation-unit scope
8351     // and add it to the declaration chain for this identifier, so
8352     // that (unqualified) name lookup will find it.
8353     pushExternalDeclIntoScope(D, II);
8354   }
8355 }
8356 
8357 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8358   if (ID == 0)
8359     return nullptr;
8360 
8361   if (IdentifiersLoaded.empty()) {
8362     Error("no identifier table in AST file");
8363     return nullptr;
8364   }
8365 
8366   ID -= 1;
8367   if (!IdentifiersLoaded[ID]) {
8368     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8369     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8370     ModuleFile *M = I->second;
8371     unsigned Index = ID - M->BaseIdentifierID;
8372     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8373 
8374     // All of the strings in the AST file are preceded by a 16-bit length.
8375     // Extract that 16-bit length to avoid having to execute strlen().
8376     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8377     //  unsigned integers.  This is important to avoid integer overflow when
8378     //  we cast them to 'unsigned'.
8379     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8380     unsigned StrLen = (((unsigned) StrLenPtr[0])
8381                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8382     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8383     IdentifiersLoaded[ID] = &II;
8384     markIdentifierFromAST(*this,  II);
8385     if (DeserializationListener)
8386       DeserializationListener->IdentifierRead(ID + 1, &II);
8387   }
8388 
8389   return IdentifiersLoaded[ID];
8390 }
8391 
8392 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8393   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8394 }
8395 
8396 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8397   if (LocalID < NUM_PREDEF_IDENT_IDS)
8398     return LocalID;
8399 
8400   if (!M.ModuleOffsetMap.empty())
8401     ReadModuleOffsetMap(M);
8402 
8403   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8404     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8405   assert(I != M.IdentifierRemap.end()
8406          && "Invalid index into identifier index remap");
8407 
8408   return LocalID + I->second;
8409 }
8410 
8411 MacroInfo *ASTReader::getMacro(MacroID ID) {
8412   if (ID == 0)
8413     return nullptr;
8414 
8415   if (MacrosLoaded.empty()) {
8416     Error("no macro table in AST file");
8417     return nullptr;
8418   }
8419 
8420   ID -= NUM_PREDEF_MACRO_IDS;
8421   if (!MacrosLoaded[ID]) {
8422     GlobalMacroMapType::iterator I
8423       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8424     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8425     ModuleFile *M = I->second;
8426     unsigned Index = ID - M->BaseMacroID;
8427     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8428 
8429     if (DeserializationListener)
8430       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8431                                          MacrosLoaded[ID]);
8432   }
8433 
8434   return MacrosLoaded[ID];
8435 }
8436 
8437 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8438   if (LocalID < NUM_PREDEF_MACRO_IDS)
8439     return LocalID;
8440 
8441   if (!M.ModuleOffsetMap.empty())
8442     ReadModuleOffsetMap(M);
8443 
8444   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8445     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8446   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8447 
8448   return LocalID + I->second;
8449 }
8450 
8451 serialization::SubmoduleID
8452 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8453   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8454     return LocalID;
8455 
8456   if (!M.ModuleOffsetMap.empty())
8457     ReadModuleOffsetMap(M);
8458 
8459   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8460     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8461   assert(I != M.SubmoduleRemap.end()
8462          && "Invalid index into submodule index remap");
8463 
8464   return LocalID + I->second;
8465 }
8466 
8467 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8468   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8469     assert(GlobalID == 0 && "Unhandled global submodule ID");
8470     return nullptr;
8471   }
8472 
8473   if (GlobalID > SubmodulesLoaded.size()) {
8474     Error("submodule ID out of range in AST file");
8475     return nullptr;
8476   }
8477 
8478   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8479 }
8480 
8481 Module *ASTReader::getModule(unsigned ID) {
8482   return getSubmodule(ID);
8483 }
8484 
8485 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8486   ModuleFile *MF = getOwningModuleFile(D);
8487   return MF && MF->PCHHasObjectFile;
8488 }
8489 
8490 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8491   if (ID & 1) {
8492     // It's a module, look it up by submodule ID.
8493     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8494     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8495   } else {
8496     // It's a prefix (preamble, PCH, ...). Look it up by index.
8497     unsigned IndexFromEnd = ID >> 1;
8498     assert(IndexFromEnd && "got reference to unknown module file");
8499     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8500   }
8501 }
8502 
8503 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8504   if (!F)
8505     return 1;
8506 
8507   // For a file representing a module, use the submodule ID of the top-level
8508   // module as the file ID. For any other kind of file, the number of such
8509   // files loaded beforehand will be the same on reload.
8510   // FIXME: Is this true even if we have an explicit module file and a PCH?
8511   if (F->isModule())
8512     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8513 
8514   auto PCHModules = getModuleManager().pch_modules();
8515   auto I = llvm::find(PCHModules, F);
8516   assert(I != PCHModules.end() && "emitting reference to unknown file");
8517   return (I - PCHModules.end()) << 1;
8518 }
8519 
8520 llvm::Optional<ASTSourceDescriptor>
8521 ASTReader::getSourceDescriptor(unsigned ID) {
8522   if (Module *M = getSubmodule(ID))
8523     return ASTSourceDescriptor(*M);
8524 
8525   // If there is only a single PCH, return it instead.
8526   // Chained PCH are not supported.
8527   const auto &PCHChain = ModuleMgr.pch_modules();
8528   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8529     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8530     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8531     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8532     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8533                                MF.Signature);
8534   }
8535   return None;
8536 }
8537 
8538 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8539   auto I = DefinitionSource.find(FD);
8540   if (I == DefinitionSource.end())
8541     return EK_ReplyHazy;
8542   return I->second ? EK_Never : EK_Always;
8543 }
8544 
8545 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8546   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8547 }
8548 
8549 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8550   if (ID == 0)
8551     return Selector();
8552 
8553   if (ID > SelectorsLoaded.size()) {
8554     Error("selector ID out of range in AST file");
8555     return Selector();
8556   }
8557 
8558   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8559     // Load this selector from the selector table.
8560     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8561     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8562     ModuleFile &M = *I->second;
8563     ASTSelectorLookupTrait Trait(*this, M);
8564     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8565     SelectorsLoaded[ID - 1] =
8566       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8567     if (DeserializationListener)
8568       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8569   }
8570 
8571   return SelectorsLoaded[ID - 1];
8572 }
8573 
8574 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8575   return DecodeSelector(ID);
8576 }
8577 
8578 uint32_t ASTReader::GetNumExternalSelectors() {
8579   // ID 0 (the null selector) is considered an external selector.
8580   return getTotalNumSelectors() + 1;
8581 }
8582 
8583 serialization::SelectorID
8584 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8585   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8586     return LocalID;
8587 
8588   if (!M.ModuleOffsetMap.empty())
8589     ReadModuleOffsetMap(M);
8590 
8591   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8592     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8593   assert(I != M.SelectorRemap.end()
8594          && "Invalid index into selector index remap");
8595 
8596   return LocalID + I->second;
8597 }
8598 
8599 DeclarationNameLoc
8600 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8601   DeclarationNameLoc DNLoc;
8602   switch (Name.getNameKind()) {
8603   case DeclarationName::CXXConstructorName:
8604   case DeclarationName::CXXDestructorName:
8605   case DeclarationName::CXXConversionFunctionName:
8606     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8607     break;
8608 
8609   case DeclarationName::CXXOperatorName:
8610     DNLoc.CXXOperatorName.BeginOpNameLoc
8611       = readSourceLocation().getRawEncoding();
8612     DNLoc.CXXOperatorName.EndOpNameLoc
8613       = readSourceLocation().getRawEncoding();
8614     break;
8615 
8616   case DeclarationName::CXXLiteralOperatorName:
8617     DNLoc.CXXLiteralOperatorName.OpNameLoc
8618       = readSourceLocation().getRawEncoding();
8619     break;
8620 
8621   case DeclarationName::Identifier:
8622   case DeclarationName::ObjCZeroArgSelector:
8623   case DeclarationName::ObjCOneArgSelector:
8624   case DeclarationName::ObjCMultiArgSelector:
8625   case DeclarationName::CXXUsingDirective:
8626   case DeclarationName::CXXDeductionGuideName:
8627     break;
8628   }
8629   return DNLoc;
8630 }
8631 
8632 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8633   DeclarationNameInfo NameInfo;
8634   NameInfo.setName(readDeclarationName());
8635   NameInfo.setLoc(readSourceLocation());
8636   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8637   return NameInfo;
8638 }
8639 
8640 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8641   Info.QualifierLoc = readNestedNameSpecifierLoc();
8642   unsigned NumTPLists = readInt();
8643   Info.NumTemplParamLists = NumTPLists;
8644   if (NumTPLists) {
8645     Info.TemplParamLists =
8646         new (getContext()) TemplateParameterList *[NumTPLists];
8647     for (unsigned i = 0; i != NumTPLists; ++i)
8648       Info.TemplParamLists[i] = readTemplateParameterList();
8649   }
8650 }
8651 
8652 TemplateParameterList *
8653 ASTRecordReader::readTemplateParameterList() {
8654   SourceLocation TemplateLoc = readSourceLocation();
8655   SourceLocation LAngleLoc = readSourceLocation();
8656   SourceLocation RAngleLoc = readSourceLocation();
8657 
8658   unsigned NumParams = readInt();
8659   SmallVector<NamedDecl *, 16> Params;
8660   Params.reserve(NumParams);
8661   while (NumParams--)
8662     Params.push_back(readDeclAs<NamedDecl>());
8663 
8664   bool HasRequiresClause = readBool();
8665   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8666 
8667   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8668       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8669   return TemplateParams;
8670 }
8671 
8672 void ASTRecordReader::readTemplateArgumentList(
8673                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8674                         bool Canonicalize) {
8675   unsigned NumTemplateArgs = readInt();
8676   TemplArgs.reserve(NumTemplateArgs);
8677   while (NumTemplateArgs--)
8678     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8679 }
8680 
8681 /// Read a UnresolvedSet structure.
8682 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8683   unsigned NumDecls = readInt();
8684   Set.reserve(getContext(), NumDecls);
8685   while (NumDecls--) {
8686     DeclID ID = readDeclID();
8687     AccessSpecifier AS = (AccessSpecifier) readInt();
8688     Set.addLazyDecl(getContext(), ID, AS);
8689   }
8690 }
8691 
8692 CXXBaseSpecifier
8693 ASTRecordReader::readCXXBaseSpecifier() {
8694   bool isVirtual = readBool();
8695   bool isBaseOfClass = readBool();
8696   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8697   bool inheritConstructors = readBool();
8698   TypeSourceInfo *TInfo = readTypeSourceInfo();
8699   SourceRange Range = readSourceRange();
8700   SourceLocation EllipsisLoc = readSourceLocation();
8701   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8702                           EllipsisLoc);
8703   Result.setInheritConstructors(inheritConstructors);
8704   return Result;
8705 }
8706 
8707 CXXCtorInitializer **
8708 ASTRecordReader::readCXXCtorInitializers() {
8709   ASTContext &Context = getContext();
8710   unsigned NumInitializers = readInt();
8711   assert(NumInitializers && "wrote ctor initializers but have no inits");
8712   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8713   for (unsigned i = 0; i != NumInitializers; ++i) {
8714     TypeSourceInfo *TInfo = nullptr;
8715     bool IsBaseVirtual = false;
8716     FieldDecl *Member = nullptr;
8717     IndirectFieldDecl *IndirectMember = nullptr;
8718 
8719     CtorInitializerType Type = (CtorInitializerType) readInt();
8720     switch (Type) {
8721     case CTOR_INITIALIZER_BASE:
8722       TInfo = readTypeSourceInfo();
8723       IsBaseVirtual = readBool();
8724       break;
8725 
8726     case CTOR_INITIALIZER_DELEGATING:
8727       TInfo = readTypeSourceInfo();
8728       break;
8729 
8730      case CTOR_INITIALIZER_MEMBER:
8731       Member = readDeclAs<FieldDecl>();
8732       break;
8733 
8734      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8735       IndirectMember = readDeclAs<IndirectFieldDecl>();
8736       break;
8737     }
8738 
8739     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8740     Expr *Init = readExpr();
8741     SourceLocation LParenLoc = readSourceLocation();
8742     SourceLocation RParenLoc = readSourceLocation();
8743 
8744     CXXCtorInitializer *BOMInit;
8745     if (Type == CTOR_INITIALIZER_BASE)
8746       BOMInit = new (Context)
8747           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8748                              RParenLoc, MemberOrEllipsisLoc);
8749     else if (Type == CTOR_INITIALIZER_DELEGATING)
8750       BOMInit = new (Context)
8751           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8752     else if (Member)
8753       BOMInit = new (Context)
8754           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8755                              Init, RParenLoc);
8756     else
8757       BOMInit = new (Context)
8758           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8759                              LParenLoc, Init, RParenLoc);
8760 
8761     if (/*IsWritten*/readBool()) {
8762       unsigned SourceOrder = readInt();
8763       BOMInit->setSourceOrder(SourceOrder);
8764     }
8765 
8766     CtorInitializers[i] = BOMInit;
8767   }
8768 
8769   return CtorInitializers;
8770 }
8771 
8772 NestedNameSpecifierLoc
8773 ASTRecordReader::readNestedNameSpecifierLoc() {
8774   ASTContext &Context = getContext();
8775   unsigned N = readInt();
8776   NestedNameSpecifierLocBuilder Builder;
8777   for (unsigned I = 0; I != N; ++I) {
8778     auto Kind = readNestedNameSpecifierKind();
8779     switch (Kind) {
8780     case NestedNameSpecifier::Identifier: {
8781       IdentifierInfo *II = readIdentifier();
8782       SourceRange Range = readSourceRange();
8783       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8784       break;
8785     }
8786 
8787     case NestedNameSpecifier::Namespace: {
8788       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8789       SourceRange Range = readSourceRange();
8790       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8791       break;
8792     }
8793 
8794     case NestedNameSpecifier::NamespaceAlias: {
8795       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8796       SourceRange Range = readSourceRange();
8797       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8798       break;
8799     }
8800 
8801     case NestedNameSpecifier::TypeSpec:
8802     case NestedNameSpecifier::TypeSpecWithTemplate: {
8803       bool Template = readBool();
8804       TypeSourceInfo *T = readTypeSourceInfo();
8805       if (!T)
8806         return NestedNameSpecifierLoc();
8807       SourceLocation ColonColonLoc = readSourceLocation();
8808 
8809       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8810       Builder.Extend(Context,
8811                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8812                      T->getTypeLoc(), ColonColonLoc);
8813       break;
8814     }
8815 
8816     case NestedNameSpecifier::Global: {
8817       SourceLocation ColonColonLoc = readSourceLocation();
8818       Builder.MakeGlobal(Context, ColonColonLoc);
8819       break;
8820     }
8821 
8822     case NestedNameSpecifier::Super: {
8823       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8824       SourceRange Range = readSourceRange();
8825       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8826       break;
8827     }
8828     }
8829   }
8830 
8831   return Builder.getWithLocInContext(Context);
8832 }
8833 
8834 SourceRange
8835 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8836                            unsigned &Idx) {
8837   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8838   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8839   return SourceRange(beg, end);
8840 }
8841 
8842 static FixedPointSemantics
8843 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8844                         unsigned &Idx) {
8845   unsigned Width = Record[Idx++];
8846   unsigned Scale = Record[Idx++];
8847   uint64_t Tmp = Record[Idx++];
8848   bool IsSigned = Tmp & 0x1;
8849   bool IsSaturated = Tmp & 0x2;
8850   bool HasUnsignedPadding = Tmp & 0x4;
8851   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8852                              HasUnsignedPadding);
8853 }
8854 
8855 static const llvm::fltSemantics &
8856 readAPFloatSemantics(ASTRecordReader &reader) {
8857   return llvm::APFloatBase::EnumToSemantics(
8858     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8859 }
8860 
8861 APValue ASTRecordReader::readAPValue() {
8862   unsigned Kind = readInt();
8863   switch ((APValue::ValueKind) Kind) {
8864   case APValue::None:
8865     return APValue();
8866   case APValue::Indeterminate:
8867     return APValue::IndeterminateValue();
8868   case APValue::Int:
8869     return APValue(readAPSInt());
8870   case APValue::Float: {
8871     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8872     return APValue(readAPFloat(FloatSema));
8873   }
8874   case APValue::FixedPoint: {
8875     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8876     return APValue(APFixedPoint(readAPInt(), FPSema));
8877   }
8878   case APValue::ComplexInt: {
8879     llvm::APSInt First = readAPSInt();
8880     return APValue(std::move(First), readAPSInt());
8881   }
8882   case APValue::ComplexFloat: {
8883     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8884     llvm::APFloat First = readAPFloat(FloatSema1);
8885     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8886     return APValue(std::move(First), readAPFloat(FloatSema2));
8887   }
8888   case APValue::LValue:
8889   case APValue::Vector:
8890   case APValue::Array:
8891   case APValue::Struct:
8892   case APValue::Union:
8893   case APValue::MemberPointer:
8894   case APValue::AddrLabelDiff:
8895     // TODO : Handle all these APValue::ValueKind.
8896     return APValue();
8897   }
8898   llvm_unreachable("Invalid APValue::ValueKind");
8899 }
8900 
8901 /// Read a floating-point value
8902 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8903   return llvm::APFloat(Sem, readAPInt());
8904 }
8905 
8906 // Read a string
8907 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8908   unsigned Len = Record[Idx++];
8909   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8910   Idx += Len;
8911   return Result;
8912 }
8913 
8914 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8915                                 unsigned &Idx) {
8916   std::string Filename = ReadString(Record, Idx);
8917   ResolveImportedPath(F, Filename);
8918   return Filename;
8919 }
8920 
8921 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8922                                 const RecordData &Record, unsigned &Idx) {
8923   std::string Filename = ReadString(Record, Idx);
8924   if (!BaseDirectory.empty())
8925     ResolveImportedPath(Filename, BaseDirectory);
8926   return Filename;
8927 }
8928 
8929 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8930                                          unsigned &Idx) {
8931   unsigned Major = Record[Idx++];
8932   unsigned Minor = Record[Idx++];
8933   unsigned Subminor = Record[Idx++];
8934   if (Minor == 0)
8935     return VersionTuple(Major);
8936   if (Subminor == 0)
8937     return VersionTuple(Major, Minor - 1);
8938   return VersionTuple(Major, Minor - 1, Subminor - 1);
8939 }
8940 
8941 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8942                                           const RecordData &Record,
8943                                           unsigned &Idx) {
8944   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8945   return CXXTemporary::Create(getContext(), Decl);
8946 }
8947 
8948 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
8949   return Diag(CurrentImportLoc, DiagID);
8950 }
8951 
8952 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
8953   return Diags.Report(Loc, DiagID);
8954 }
8955 
8956 /// Retrieve the identifier table associated with the
8957 /// preprocessor.
8958 IdentifierTable &ASTReader::getIdentifierTable() {
8959   return PP.getIdentifierTable();
8960 }
8961 
8962 /// Record that the given ID maps to the given switch-case
8963 /// statement.
8964 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8965   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8966          "Already have a SwitchCase with this ID");
8967   (*CurrSwitchCaseStmts)[ID] = SC;
8968 }
8969 
8970 /// Retrieve the switch-case statement with the given ID.
8971 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8972   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8973   return (*CurrSwitchCaseStmts)[ID];
8974 }
8975 
8976 void ASTReader::ClearSwitchCaseIDs() {
8977   CurrSwitchCaseStmts->clear();
8978 }
8979 
8980 void ASTReader::ReadComments() {
8981   ASTContext &Context = getContext();
8982   std::vector<RawComment *> Comments;
8983   for (SmallVectorImpl<std::pair<BitstreamCursor,
8984                                  serialization::ModuleFile *>>::iterator
8985        I = CommentsCursors.begin(),
8986        E = CommentsCursors.end();
8987        I != E; ++I) {
8988     Comments.clear();
8989     BitstreamCursor &Cursor = I->first;
8990     serialization::ModuleFile &F = *I->second;
8991     SavedStreamPosition SavedPosition(Cursor);
8992 
8993     RecordData Record;
8994     while (true) {
8995       Expected<llvm::BitstreamEntry> MaybeEntry =
8996           Cursor.advanceSkippingSubblocks(
8997               BitstreamCursor::AF_DontPopBlockAtEnd);
8998       if (!MaybeEntry) {
8999         Error(MaybeEntry.takeError());
9000         return;
9001       }
9002       llvm::BitstreamEntry Entry = MaybeEntry.get();
9003 
9004       switch (Entry.Kind) {
9005       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9006       case llvm::BitstreamEntry::Error:
9007         Error("malformed block record in AST file");
9008         return;
9009       case llvm::BitstreamEntry::EndBlock:
9010         goto NextCursor;
9011       case llvm::BitstreamEntry::Record:
9012         // The interesting case.
9013         break;
9014       }
9015 
9016       // Read a record.
9017       Record.clear();
9018       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9019       if (!MaybeComment) {
9020         Error(MaybeComment.takeError());
9021         return;
9022       }
9023       switch ((CommentRecordTypes)MaybeComment.get()) {
9024       case COMMENTS_RAW_COMMENT: {
9025         unsigned Idx = 0;
9026         SourceRange SR = ReadSourceRange(F, Record, Idx);
9027         RawComment::CommentKind Kind =
9028             (RawComment::CommentKind) Record[Idx++];
9029         bool IsTrailingComment = Record[Idx++];
9030         bool IsAlmostTrailingComment = Record[Idx++];
9031         Comments.push_back(new (Context) RawComment(
9032             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9033         break;
9034       }
9035       }
9036     }
9037   NextCursor:
9038     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9039         FileToOffsetToComment;
9040     for (RawComment *C : Comments) {
9041       SourceLocation CommentLoc = C->getBeginLoc();
9042       if (CommentLoc.isValid()) {
9043         std::pair<FileID, unsigned> Loc =
9044             SourceMgr.getDecomposedLoc(CommentLoc);
9045         if (Loc.first.isValid())
9046           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9047       }
9048     }
9049   }
9050 }
9051 
9052 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9053                                 bool IncludeSystem, bool Complain,
9054                     llvm::function_ref<void(const serialization::InputFile &IF,
9055                                             bool isSystem)> Visitor) {
9056   unsigned NumUserInputs = MF.NumUserInputFiles;
9057   unsigned NumInputs = MF.InputFilesLoaded.size();
9058   assert(NumUserInputs <= NumInputs);
9059   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9060   for (unsigned I = 0; I < N; ++I) {
9061     bool IsSystem = I >= NumUserInputs;
9062     InputFile IF = getInputFile(MF, I+1, Complain);
9063     Visitor(IF, IsSystem);
9064   }
9065 }
9066 
9067 void ASTReader::visitTopLevelModuleMaps(
9068     serialization::ModuleFile &MF,
9069     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9070   unsigned NumInputs = MF.InputFilesLoaded.size();
9071   for (unsigned I = 0; I < NumInputs; ++I) {
9072     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9073     if (IFI.TopLevelModuleMap)
9074       // FIXME: This unnecessarily re-reads the InputFileInfo.
9075       if (auto *FE = getInputFile(MF, I + 1).getFile())
9076         Visitor(FE);
9077   }
9078 }
9079 
9080 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9081   // If we know the owning module, use it.
9082   if (Module *M = D->getImportedOwningModule())
9083     return M->getFullModuleName();
9084 
9085   // Otherwise, use the name of the top-level module the decl is within.
9086   if (ModuleFile *M = getOwningModuleFile(D))
9087     return M->ModuleName;
9088 
9089   // Not from a module.
9090   return {};
9091 }
9092 
9093 void ASTReader::finishPendingActions() {
9094   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9095          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9096          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9097          !PendingUpdateRecords.empty()) {
9098     // If any identifiers with corresponding top-level declarations have
9099     // been loaded, load those declarations now.
9100     using TopLevelDeclsMap =
9101         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9102     TopLevelDeclsMap TopLevelDecls;
9103 
9104     while (!PendingIdentifierInfos.empty()) {
9105       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9106       SmallVector<uint32_t, 4> DeclIDs =
9107           std::move(PendingIdentifierInfos.back().second);
9108       PendingIdentifierInfos.pop_back();
9109 
9110       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9111     }
9112 
9113     // Load each function type that we deferred loading because it was a
9114     // deduced type that might refer to a local type declared within itself.
9115     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9116       auto *FD = PendingFunctionTypes[I].first;
9117       FD->setType(GetType(PendingFunctionTypes[I].second));
9118 
9119       // If we gave a function a deduced return type, remember that we need to
9120       // propagate that along the redeclaration chain.
9121       auto *DT = FD->getReturnType()->getContainedDeducedType();
9122       if (DT && DT->isDeduced())
9123         PendingDeducedTypeUpdates.insert(
9124             {FD->getCanonicalDecl(), FD->getReturnType()});
9125     }
9126     PendingFunctionTypes.clear();
9127 
9128     // For each decl chain that we wanted to complete while deserializing, mark
9129     // it as "still needs to be completed".
9130     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9131       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9132     }
9133     PendingIncompleteDeclChains.clear();
9134 
9135     // Load pending declaration chains.
9136     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9137       loadPendingDeclChain(PendingDeclChains[I].first,
9138                            PendingDeclChains[I].second);
9139     PendingDeclChains.clear();
9140 
9141     // Make the most recent of the top-level declarations visible.
9142     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9143            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9144       IdentifierInfo *II = TLD->first;
9145       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9146         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9147       }
9148     }
9149 
9150     // Load any pending macro definitions.
9151     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9152       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9153       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9154       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9155       // Initialize the macro history from chained-PCHs ahead of module imports.
9156       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9157            ++IDIdx) {
9158         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9159         if (!Info.M->isModule())
9160           resolvePendingMacro(II, Info);
9161       }
9162       // Handle module imports.
9163       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9164            ++IDIdx) {
9165         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9166         if (Info.M->isModule())
9167           resolvePendingMacro(II, Info);
9168       }
9169     }
9170     PendingMacroIDs.clear();
9171 
9172     // Wire up the DeclContexts for Decls that we delayed setting until
9173     // recursive loading is completed.
9174     while (!PendingDeclContextInfos.empty()) {
9175       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9176       PendingDeclContextInfos.pop_front();
9177       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9178       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9179       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9180     }
9181 
9182     // Perform any pending declaration updates.
9183     while (!PendingUpdateRecords.empty()) {
9184       auto Update = PendingUpdateRecords.pop_back_val();
9185       ReadingKindTracker ReadingKind(Read_Decl, *this);
9186       loadDeclUpdateRecords(Update);
9187     }
9188   }
9189 
9190   // At this point, all update records for loaded decls are in place, so any
9191   // fake class definitions should have become real.
9192   assert(PendingFakeDefinitionData.empty() &&
9193          "faked up a class definition but never saw the real one");
9194 
9195   // If we deserialized any C++ or Objective-C class definitions, any
9196   // Objective-C protocol definitions, or any redeclarable templates, make sure
9197   // that all redeclarations point to the definitions. Note that this can only
9198   // happen now, after the redeclaration chains have been fully wired.
9199   for (Decl *D : PendingDefinitions) {
9200     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9201       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9202         // Make sure that the TagType points at the definition.
9203         const_cast<TagType*>(TagT)->decl = TD;
9204       }
9205 
9206       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9207         for (auto *R = getMostRecentExistingDecl(RD); R;
9208              R = R->getPreviousDecl()) {
9209           assert((R == D) ==
9210                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9211                  "declaration thinks it's the definition but it isn't");
9212           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9213         }
9214       }
9215 
9216       continue;
9217     }
9218 
9219     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9220       // Make sure that the ObjCInterfaceType points at the definition.
9221       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9222         ->Decl = ID;
9223 
9224       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9225         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9226 
9227       continue;
9228     }
9229 
9230     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9231       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9232         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9233 
9234       continue;
9235     }
9236 
9237     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9238     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9239       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9240   }
9241   PendingDefinitions.clear();
9242 
9243   // Load the bodies of any functions or methods we've encountered. We do
9244   // this now (delayed) so that we can be sure that the declaration chains
9245   // have been fully wired up (hasBody relies on this).
9246   // FIXME: We shouldn't require complete redeclaration chains here.
9247   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9248                                PBEnd = PendingBodies.end();
9249        PB != PBEnd; ++PB) {
9250     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9251       // For a function defined inline within a class template, force the
9252       // canonical definition to be the one inside the canonical definition of
9253       // the template. This ensures that we instantiate from a correct view
9254       // of the template.
9255       //
9256       // Sadly we can't do this more generally: we can't be sure that all
9257       // copies of an arbitrary class definition will have the same members
9258       // defined (eg, some member functions may not be instantiated, and some
9259       // special members may or may not have been implicitly defined).
9260       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9261         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9262           continue;
9263 
9264       // FIXME: Check for =delete/=default?
9265       // FIXME: Complain about ODR violations here?
9266       const FunctionDecl *Defn = nullptr;
9267       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9268         FD->setLazyBody(PB->second);
9269       } else {
9270         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9271         mergeDefinitionVisibility(NonConstDefn, FD);
9272 
9273         if (!FD->isLateTemplateParsed() &&
9274             !NonConstDefn->isLateTemplateParsed() &&
9275             FD->getODRHash() != NonConstDefn->getODRHash()) {
9276           if (!isa<CXXMethodDecl>(FD)) {
9277             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9278           } else if (FD->getLexicalParent()->isFileContext() &&
9279                      NonConstDefn->getLexicalParent()->isFileContext()) {
9280             // Only diagnose out-of-line method definitions.  If they are
9281             // in class definitions, then an error will be generated when
9282             // processing the class bodies.
9283             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9284           }
9285         }
9286       }
9287       continue;
9288     }
9289 
9290     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9291     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9292       MD->setLazyBody(PB->second);
9293   }
9294   PendingBodies.clear();
9295 
9296   // Do some cleanup.
9297   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9298     getContext().deduplicateMergedDefinitonsFor(ND);
9299   PendingMergedDefinitionsToDeduplicate.clear();
9300 }
9301 
9302 void ASTReader::diagnoseOdrViolations() {
9303   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9304       PendingFunctionOdrMergeFailures.empty() &&
9305       PendingEnumOdrMergeFailures.empty())
9306     return;
9307 
9308   // Trigger the import of the full definition of each class that had any
9309   // odr-merging problems, so we can produce better diagnostics for them.
9310   // These updates may in turn find and diagnose some ODR failures, so take
9311   // ownership of the set first.
9312   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9313   PendingOdrMergeFailures.clear();
9314   for (auto &Merge : OdrMergeFailures) {
9315     Merge.first->buildLookup();
9316     Merge.first->decls_begin();
9317     Merge.first->bases_begin();
9318     Merge.first->vbases_begin();
9319     for (auto &RecordPair : Merge.second) {
9320       auto *RD = RecordPair.first;
9321       RD->decls_begin();
9322       RD->bases_begin();
9323       RD->vbases_begin();
9324     }
9325   }
9326 
9327   // Trigger the import of functions.
9328   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9329   PendingFunctionOdrMergeFailures.clear();
9330   for (auto &Merge : FunctionOdrMergeFailures) {
9331     Merge.first->buildLookup();
9332     Merge.first->decls_begin();
9333     Merge.first->getBody();
9334     for (auto &FD : Merge.second) {
9335       FD->buildLookup();
9336       FD->decls_begin();
9337       FD->getBody();
9338     }
9339   }
9340 
9341   // Trigger the import of enums.
9342   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9343   PendingEnumOdrMergeFailures.clear();
9344   for (auto &Merge : EnumOdrMergeFailures) {
9345     Merge.first->decls_begin();
9346     for (auto &Enum : Merge.second) {
9347       Enum->decls_begin();
9348     }
9349   }
9350 
9351   // For each declaration from a merged context, check that the canonical
9352   // definition of that context also contains a declaration of the same
9353   // entity.
9354   //
9355   // Caution: this loop does things that might invalidate iterators into
9356   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9357   while (!PendingOdrMergeChecks.empty()) {
9358     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9359 
9360     // FIXME: Skip over implicit declarations for now. This matters for things
9361     // like implicitly-declared special member functions. This isn't entirely
9362     // correct; we can end up with multiple unmerged declarations of the same
9363     // implicit entity.
9364     if (D->isImplicit())
9365       continue;
9366 
9367     DeclContext *CanonDef = D->getDeclContext();
9368 
9369     bool Found = false;
9370     const Decl *DCanon = D->getCanonicalDecl();
9371 
9372     for (auto RI : D->redecls()) {
9373       if (RI->getLexicalDeclContext() == CanonDef) {
9374         Found = true;
9375         break;
9376       }
9377     }
9378     if (Found)
9379       continue;
9380 
9381     // Quick check failed, time to do the slow thing. Note, we can't just
9382     // look up the name of D in CanonDef here, because the member that is
9383     // in CanonDef might not be found by name lookup (it might have been
9384     // replaced by a more recent declaration in the lookup table), and we
9385     // can't necessarily find it in the redeclaration chain because it might
9386     // be merely mergeable, not redeclarable.
9387     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9388     for (auto *CanonMember : CanonDef->decls()) {
9389       if (CanonMember->getCanonicalDecl() == DCanon) {
9390         // This can happen if the declaration is merely mergeable and not
9391         // actually redeclarable (we looked for redeclarations earlier).
9392         //
9393         // FIXME: We should be able to detect this more efficiently, without
9394         // pulling in all of the members of CanonDef.
9395         Found = true;
9396         break;
9397       }
9398       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9399         if (ND->getDeclName() == D->getDeclName())
9400           Candidates.push_back(ND);
9401     }
9402 
9403     if (!Found) {
9404       // The AST doesn't like TagDecls becoming invalid after they've been
9405       // completed. We only really need to mark FieldDecls as invalid here.
9406       if (!isa<TagDecl>(D))
9407         D->setInvalidDecl();
9408 
9409       // Ensure we don't accidentally recursively enter deserialization while
9410       // we're producing our diagnostic.
9411       Deserializing RecursionGuard(this);
9412 
9413       std::string CanonDefModule =
9414           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9415       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9416         << D << getOwningModuleNameForDiagnostic(D)
9417         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9418 
9419       if (Candidates.empty())
9420         Diag(cast<Decl>(CanonDef)->getLocation(),
9421              diag::note_module_odr_violation_no_possible_decls) << D;
9422       else {
9423         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9424           Diag(Candidates[I]->getLocation(),
9425                diag::note_module_odr_violation_possible_decl)
9426             << Candidates[I];
9427       }
9428 
9429       DiagnosedOdrMergeFailures.insert(CanonDef);
9430     }
9431   }
9432 
9433   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9434       EnumOdrMergeFailures.empty())
9435     return;
9436 
9437   // Ensure we don't accidentally recursively enter deserialization while
9438   // we're producing our diagnostics.
9439   Deserializing RecursionGuard(this);
9440 
9441   // Common code for hashing helpers.
9442   ODRHash Hash;
9443   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9444     Hash.clear();
9445     Hash.AddQualType(Ty);
9446     return Hash.CalculateHash();
9447   };
9448 
9449   auto ComputeODRHash = [&Hash](const Stmt *S) {
9450     assert(S);
9451     Hash.clear();
9452     Hash.AddStmt(S);
9453     return Hash.CalculateHash();
9454   };
9455 
9456   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9457     assert(D);
9458     Hash.clear();
9459     Hash.AddSubDecl(D);
9460     return Hash.CalculateHash();
9461   };
9462 
9463   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9464     Hash.clear();
9465     Hash.AddTemplateArgument(TA);
9466     return Hash.CalculateHash();
9467   };
9468 
9469   auto ComputeTemplateParameterListODRHash =
9470       [&Hash](const TemplateParameterList *TPL) {
9471         assert(TPL);
9472         Hash.clear();
9473         Hash.AddTemplateParameterList(TPL);
9474         return Hash.CalculateHash();
9475       };
9476 
9477   // Used with err_module_odr_violation_mismatch_decl and
9478   // note_module_odr_violation_mismatch_decl
9479   // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9480   enum ODRMismatchDecl {
9481     EndOfClass,
9482     PublicSpecifer,
9483     PrivateSpecifer,
9484     ProtectedSpecifer,
9485     StaticAssert,
9486     Field,
9487     CXXMethod,
9488     TypeAlias,
9489     TypeDef,
9490     Var,
9491     Friend,
9492     FunctionTemplate,
9493     Other
9494   };
9495 
9496   // Used with err_module_odr_violation_mismatch_decl_diff and
9497   // note_module_odr_violation_mismatch_decl_diff
9498   enum ODRMismatchDeclDifference {
9499     StaticAssertCondition,
9500     StaticAssertMessage,
9501     StaticAssertOnlyMessage,
9502     FieldName,
9503     FieldTypeName,
9504     FieldSingleBitField,
9505     FieldDifferentWidthBitField,
9506     FieldSingleMutable,
9507     FieldSingleInitializer,
9508     FieldDifferentInitializers,
9509     MethodName,
9510     MethodDeleted,
9511     MethodDefaulted,
9512     MethodVirtual,
9513     MethodStatic,
9514     MethodVolatile,
9515     MethodConst,
9516     MethodInline,
9517     MethodNumberParameters,
9518     MethodParameterType,
9519     MethodParameterName,
9520     MethodParameterSingleDefaultArgument,
9521     MethodParameterDifferentDefaultArgument,
9522     MethodNoTemplateArguments,
9523     MethodDifferentNumberTemplateArguments,
9524     MethodDifferentTemplateArgument,
9525     MethodSingleBody,
9526     MethodDifferentBody,
9527     TypedefName,
9528     TypedefType,
9529     VarName,
9530     VarType,
9531     VarSingleInitializer,
9532     VarDifferentInitializer,
9533     VarConstexpr,
9534     FriendTypeFunction,
9535     FriendType,
9536     FriendFunction,
9537     FunctionTemplateDifferentNumberParameters,
9538     FunctionTemplateParameterDifferentKind,
9539     FunctionTemplateParameterName,
9540     FunctionTemplateParameterSingleDefaultArgument,
9541     FunctionTemplateParameterDifferentDefaultArgument,
9542     FunctionTemplateParameterDifferentType,
9543     FunctionTemplatePackParameter,
9544   };
9545 
9546   // These lambdas have the common portions of the ODR diagnostics.  This
9547   // has the same return as Diag(), so addition parameters can be passed
9548   // in with operator<<
9549   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9550                                  SourceLocation Loc, SourceRange Range,
9551                                  ODRMismatchDeclDifference DiffType) {
9552     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9553            << FirstRecord << FirstModule.empty() << FirstModule << Range
9554            << DiffType;
9555   };
9556   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9557                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9558     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9559            << SecondModule << Range << DiffType;
9560   };
9561 
9562   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9563                        &ComputeQualTypeODRHash, &ComputeODRHash](
9564                           NamedDecl *FirstRecord, StringRef FirstModule,
9565                           StringRef SecondModule, FieldDecl *FirstField,
9566                           FieldDecl *SecondField) {
9567     IdentifierInfo *FirstII = FirstField->getIdentifier();
9568     IdentifierInfo *SecondII = SecondField->getIdentifier();
9569     if (FirstII->getName() != SecondII->getName()) {
9570       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9571                        FirstField->getSourceRange(), FieldName)
9572           << FirstII;
9573       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9574                       SecondField->getSourceRange(), FieldName)
9575           << SecondII;
9576 
9577       return true;
9578     }
9579 
9580     assert(getContext().hasSameType(FirstField->getType(),
9581                                     SecondField->getType()));
9582 
9583     QualType FirstType = FirstField->getType();
9584     QualType SecondType = SecondField->getType();
9585     if (ComputeQualTypeODRHash(FirstType) !=
9586         ComputeQualTypeODRHash(SecondType)) {
9587       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9588                        FirstField->getSourceRange(), FieldTypeName)
9589           << FirstII << FirstType;
9590       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9591                       SecondField->getSourceRange(), FieldTypeName)
9592           << SecondII << SecondType;
9593 
9594       return true;
9595     }
9596 
9597     const bool IsFirstBitField = FirstField->isBitField();
9598     const bool IsSecondBitField = SecondField->isBitField();
9599     if (IsFirstBitField != IsSecondBitField) {
9600       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9601                        FirstField->getSourceRange(), FieldSingleBitField)
9602           << FirstII << IsFirstBitField;
9603       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9604                       SecondField->getSourceRange(), FieldSingleBitField)
9605           << SecondII << IsSecondBitField;
9606       return true;
9607     }
9608 
9609     if (IsFirstBitField && IsSecondBitField) {
9610       unsigned FirstBitWidthHash =
9611           ComputeODRHash(FirstField->getBitWidth());
9612       unsigned SecondBitWidthHash =
9613           ComputeODRHash(SecondField->getBitWidth());
9614       if (FirstBitWidthHash != SecondBitWidthHash) {
9615         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9616                          FirstField->getSourceRange(),
9617                          FieldDifferentWidthBitField)
9618             << FirstII << FirstField->getBitWidth()->getSourceRange();
9619         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9620                         SecondField->getSourceRange(),
9621                         FieldDifferentWidthBitField)
9622             << SecondII << SecondField->getBitWidth()->getSourceRange();
9623         return true;
9624       }
9625     }
9626 
9627     if (!PP.getLangOpts().CPlusPlus)
9628       return false;
9629 
9630     const bool IsFirstMutable = FirstField->isMutable();
9631     const bool IsSecondMutable = SecondField->isMutable();
9632     if (IsFirstMutable != IsSecondMutable) {
9633       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9634                        FirstField->getSourceRange(), FieldSingleMutable)
9635           << FirstII << IsFirstMutable;
9636       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9637                       SecondField->getSourceRange(), FieldSingleMutable)
9638           << SecondII << IsSecondMutable;
9639       return true;
9640     }
9641 
9642     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9643     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9644     if ((!FirstInitializer && SecondInitializer) ||
9645         (FirstInitializer && !SecondInitializer)) {
9646       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9647                        FirstField->getSourceRange(), FieldSingleInitializer)
9648           << FirstII << (FirstInitializer != nullptr);
9649       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9650                       SecondField->getSourceRange(), FieldSingleInitializer)
9651           << SecondII << (SecondInitializer != nullptr);
9652       return true;
9653     }
9654 
9655     if (FirstInitializer && SecondInitializer) {
9656       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9657       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9658       if (FirstInitHash != SecondInitHash) {
9659         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9660                          FirstField->getSourceRange(),
9661                          FieldDifferentInitializers)
9662             << FirstII << FirstInitializer->getSourceRange();
9663         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9664                         SecondField->getSourceRange(),
9665                         FieldDifferentInitializers)
9666             << SecondII << SecondInitializer->getSourceRange();
9667         return true;
9668       }
9669     }
9670 
9671     return false;
9672   };
9673 
9674   auto ODRDiagTypeDefOrAlias =
9675       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9676           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9677           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9678           bool IsTypeAlias) {
9679         auto FirstName = FirstTD->getDeclName();
9680         auto SecondName = SecondTD->getDeclName();
9681         if (FirstName != SecondName) {
9682           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9683                            FirstTD->getSourceRange(), TypedefName)
9684               << IsTypeAlias << FirstName;
9685           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9686                           SecondTD->getSourceRange(), TypedefName)
9687               << IsTypeAlias << SecondName;
9688           return true;
9689         }
9690 
9691         QualType FirstType = FirstTD->getUnderlyingType();
9692         QualType SecondType = SecondTD->getUnderlyingType();
9693         if (ComputeQualTypeODRHash(FirstType) !=
9694             ComputeQualTypeODRHash(SecondType)) {
9695           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9696                            FirstTD->getSourceRange(), TypedefType)
9697               << IsTypeAlias << FirstName << FirstType;
9698           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9699                           SecondTD->getSourceRange(), TypedefType)
9700               << IsTypeAlias << SecondName << SecondType;
9701           return true;
9702         }
9703 
9704         return false;
9705   };
9706 
9707   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9708                      &ComputeQualTypeODRHash, &ComputeODRHash,
9709                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9710                            StringRef SecondModule, VarDecl *FirstVD,
9711                            VarDecl *SecondVD) {
9712     auto FirstName = FirstVD->getDeclName();
9713     auto SecondName = SecondVD->getDeclName();
9714     if (FirstName != SecondName) {
9715       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9716                        FirstVD->getSourceRange(), VarName)
9717           << FirstName;
9718       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9719                       SecondVD->getSourceRange(), VarName)
9720           << SecondName;
9721       return true;
9722     }
9723 
9724     QualType FirstType = FirstVD->getType();
9725     QualType SecondType = SecondVD->getType();
9726     if (ComputeQualTypeODRHash(FirstType) !=
9727         ComputeQualTypeODRHash(SecondType)) {
9728       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9729                        FirstVD->getSourceRange(), VarType)
9730           << FirstName << FirstType;
9731       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9732                       SecondVD->getSourceRange(), VarType)
9733           << SecondName << SecondType;
9734       return true;
9735     }
9736 
9737     if (!PP.getLangOpts().CPlusPlus)
9738       return false;
9739 
9740     const Expr *FirstInit = FirstVD->getInit();
9741     const Expr *SecondInit = SecondVD->getInit();
9742     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9743       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9744                        FirstVD->getSourceRange(), VarSingleInitializer)
9745           << FirstName << (FirstInit == nullptr)
9746           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9747       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9748                       SecondVD->getSourceRange(), VarSingleInitializer)
9749           << SecondName << (SecondInit == nullptr)
9750           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9751       return true;
9752     }
9753 
9754     if (FirstInit && SecondInit &&
9755         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9756       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9757                        FirstVD->getSourceRange(), VarDifferentInitializer)
9758           << FirstName << FirstInit->getSourceRange();
9759       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9760                       SecondVD->getSourceRange(), VarDifferentInitializer)
9761           << SecondName << SecondInit->getSourceRange();
9762       return true;
9763     }
9764 
9765     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9766     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9767     if (FirstIsConstexpr != SecondIsConstexpr) {
9768       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9769                        FirstVD->getSourceRange(), VarConstexpr)
9770           << FirstName << FirstIsConstexpr;
9771       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9772                       SecondVD->getSourceRange(), VarConstexpr)
9773           << SecondName << SecondIsConstexpr;
9774       return true;
9775     }
9776     return false;
9777   };
9778 
9779   auto DifferenceSelector = [](Decl *D) {
9780     assert(D && "valid Decl required");
9781     switch (D->getKind()) {
9782     default:
9783       return Other;
9784     case Decl::AccessSpec:
9785       switch (D->getAccess()) {
9786       case AS_public:
9787         return PublicSpecifer;
9788       case AS_private:
9789         return PrivateSpecifer;
9790       case AS_protected:
9791         return ProtectedSpecifer;
9792       case AS_none:
9793         break;
9794       }
9795       llvm_unreachable("Invalid access specifier");
9796     case Decl::StaticAssert:
9797       return StaticAssert;
9798     case Decl::Field:
9799       return Field;
9800     case Decl::CXXMethod:
9801     case Decl::CXXConstructor:
9802     case Decl::CXXDestructor:
9803       return CXXMethod;
9804     case Decl::TypeAlias:
9805       return TypeAlias;
9806     case Decl::Typedef:
9807       return TypeDef;
9808     case Decl::Var:
9809       return Var;
9810     case Decl::Friend:
9811       return Friend;
9812     case Decl::FunctionTemplate:
9813       return FunctionTemplate;
9814     }
9815   };
9816 
9817   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9818   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9819                                                  RecordDecl *Record,
9820                                                  const DeclContext *DC) {
9821     for (auto *D : Record->decls()) {
9822       if (!ODRHash::isWhitelistedDecl(D, DC))
9823         continue;
9824       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9825     }
9826   };
9827 
9828   struct DiffResult {
9829     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9830     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9831   };
9832 
9833   // If there is a diagnoseable difference, FirstDiffType and
9834   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9835   // filled in if not EndOfClass.
9836   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9837                                              DeclHashes &SecondHashes) {
9838     DiffResult DR;
9839     auto FirstIt = FirstHashes.begin();
9840     auto SecondIt = SecondHashes.begin();
9841     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9842       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9843           FirstIt->second == SecondIt->second) {
9844         ++FirstIt;
9845         ++SecondIt;
9846         continue;
9847       }
9848 
9849       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9850       DR.SecondDecl =
9851           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9852 
9853       DR.FirstDiffType =
9854           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9855       DR.SecondDiffType =
9856           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9857       return DR;
9858     }
9859     return DR;
9860   };
9861 
9862   // Use this to diagnose that an unexpected Decl was encountered
9863   // or no difference was detected. This causes a generic error
9864   // message to be emitted.
9865   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9866                                       StringRef FirstModule,
9867                                       NamedDecl *SecondRecord,
9868                                       StringRef SecondModule) {
9869     Diag(FirstRecord->getLocation(),
9870          diag::err_module_odr_violation_different_definitions)
9871         << FirstRecord << FirstModule.empty() << FirstModule;
9872 
9873     if (DR.FirstDecl) {
9874       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9875           << FirstRecord << DR.FirstDecl->getSourceRange();
9876     }
9877 
9878     Diag(SecondRecord->getLocation(),
9879          diag::note_module_odr_violation_different_definitions)
9880         << SecondModule;
9881 
9882     if (DR.SecondDecl) {
9883       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9884           << DR.SecondDecl->getSourceRange();
9885     }
9886   };
9887 
9888   auto DiagnoseODRMismatch =
9889       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9890              NamedDecl *SecondRecord, StringRef SecondModule) {
9891         SourceLocation FirstLoc;
9892         SourceRange FirstRange;
9893         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9894         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9895           FirstLoc = FirstTag->getBraceRange().getEnd();
9896         } else {
9897           FirstLoc = DR.FirstDecl->getLocation();
9898           FirstRange = DR.FirstDecl->getSourceRange();
9899         }
9900         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9901             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9902             << DR.FirstDiffType;
9903 
9904         SourceLocation SecondLoc;
9905         SourceRange SecondRange;
9906         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9907         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9908           SecondLoc = SecondTag->getBraceRange().getEnd();
9909         } else {
9910           SecondLoc = DR.SecondDecl->getLocation();
9911           SecondRange = DR.SecondDecl->getSourceRange();
9912         }
9913         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9914             << SecondModule << SecondRange << DR.SecondDiffType;
9915       };
9916 
9917   // Issue any pending ODR-failure diagnostics.
9918   for (auto &Merge : OdrMergeFailures) {
9919     // If we've already pointed out a specific problem with this class, don't
9920     // bother issuing a general "something's different" diagnostic.
9921     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9922       continue;
9923 
9924     bool Diagnosed = false;
9925     CXXRecordDecl *FirstRecord = Merge.first;
9926     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9927     for (auto &RecordPair : Merge.second) {
9928       CXXRecordDecl *SecondRecord = RecordPair.first;
9929       // Multiple different declarations got merged together; tell the user
9930       // where they came from.
9931       if (FirstRecord == SecondRecord)
9932         continue;
9933 
9934       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9935 
9936       auto *FirstDD = FirstRecord->DefinitionData;
9937       auto *SecondDD = RecordPair.second;
9938 
9939       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9940 
9941       // Diagnostics from DefinitionData are emitted here.
9942       if (FirstDD != SecondDD) {
9943         enum ODRDefinitionDataDifference {
9944           NumBases,
9945           NumVBases,
9946           BaseType,
9947           BaseVirtual,
9948           BaseAccess,
9949         };
9950         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
9951                                  this](SourceLocation Loc, SourceRange Range,
9952                                        ODRDefinitionDataDifference DiffType) {
9953           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9954                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9955                  << DiffType;
9956         };
9957         auto ODRDiagBaseNote = [&SecondModule,
9958                                 this](SourceLocation Loc, SourceRange Range,
9959                                       ODRDefinitionDataDifference DiffType) {
9960           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9961                  << SecondModule << Range << DiffType;
9962         };
9963 
9964         unsigned FirstNumBases = FirstDD->NumBases;
9965         unsigned FirstNumVBases = FirstDD->NumVBases;
9966         unsigned SecondNumBases = SecondDD->NumBases;
9967         unsigned SecondNumVBases = SecondDD->NumVBases;
9968 
9969         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9970           unsigned NumBases = DD->NumBases;
9971           if (NumBases == 0) return SourceRange();
9972           auto bases = DD->bases();
9973           return SourceRange(bases[0].getBeginLoc(),
9974                              bases[NumBases - 1].getEndLoc());
9975         };
9976 
9977         if (FirstNumBases != SecondNumBases) {
9978           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9979                            NumBases)
9980               << FirstNumBases;
9981           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9982                           NumBases)
9983               << SecondNumBases;
9984           Diagnosed = true;
9985           break;
9986         }
9987 
9988         if (FirstNumVBases != SecondNumVBases) {
9989           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9990                            NumVBases)
9991               << FirstNumVBases;
9992           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9993                           NumVBases)
9994               << SecondNumVBases;
9995           Diagnosed = true;
9996           break;
9997         }
9998 
9999         auto FirstBases = FirstDD->bases();
10000         auto SecondBases = SecondDD->bases();
10001         unsigned i = 0;
10002         for (i = 0; i < FirstNumBases; ++i) {
10003           auto FirstBase = FirstBases[i];
10004           auto SecondBase = SecondBases[i];
10005           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10006               ComputeQualTypeODRHash(SecondBase.getType())) {
10007             ODRDiagBaseError(FirstRecord->getLocation(),
10008                              FirstBase.getSourceRange(), BaseType)
10009                 << (i + 1) << FirstBase.getType();
10010             ODRDiagBaseNote(SecondRecord->getLocation(),
10011                             SecondBase.getSourceRange(), BaseType)
10012                 << (i + 1) << SecondBase.getType();
10013             break;
10014           }
10015 
10016           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10017             ODRDiagBaseError(FirstRecord->getLocation(),
10018                              FirstBase.getSourceRange(), BaseVirtual)
10019                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10020             ODRDiagBaseNote(SecondRecord->getLocation(),
10021                             SecondBase.getSourceRange(), BaseVirtual)
10022                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10023             break;
10024           }
10025 
10026           if (FirstBase.getAccessSpecifierAsWritten() !=
10027               SecondBase.getAccessSpecifierAsWritten()) {
10028             ODRDiagBaseError(FirstRecord->getLocation(),
10029                              FirstBase.getSourceRange(), BaseAccess)
10030                 << (i + 1) << FirstBase.getType()
10031                 << (int)FirstBase.getAccessSpecifierAsWritten();
10032             ODRDiagBaseNote(SecondRecord->getLocation(),
10033                             SecondBase.getSourceRange(), BaseAccess)
10034                 << (i + 1) << SecondBase.getType()
10035                 << (int)SecondBase.getAccessSpecifierAsWritten();
10036             break;
10037           }
10038         }
10039 
10040         if (i != FirstNumBases) {
10041           Diagnosed = true;
10042           break;
10043         }
10044       }
10045 
10046       const ClassTemplateDecl *FirstTemplate =
10047           FirstRecord->getDescribedClassTemplate();
10048       const ClassTemplateDecl *SecondTemplate =
10049           SecondRecord->getDescribedClassTemplate();
10050 
10051       assert(!FirstTemplate == !SecondTemplate &&
10052              "Both pointers should be null or non-null");
10053 
10054       enum ODRTemplateDifference {
10055         ParamEmptyName,
10056         ParamName,
10057         ParamSingleDefaultArgument,
10058         ParamDifferentDefaultArgument,
10059       };
10060 
10061       if (FirstTemplate && SecondTemplate) {
10062         DeclHashes FirstTemplateHashes;
10063         DeclHashes SecondTemplateHashes;
10064 
10065         auto PopulateTemplateParameterHashs =
10066             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10067                                      const ClassTemplateDecl *TD) {
10068               for (auto *D : TD->getTemplateParameters()->asArray()) {
10069                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10070               }
10071             };
10072 
10073         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10074         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10075 
10076         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10077                "Number of template parameters should be equal.");
10078 
10079         auto FirstIt = FirstTemplateHashes.begin();
10080         auto FirstEnd = FirstTemplateHashes.end();
10081         auto SecondIt = SecondTemplateHashes.begin();
10082         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10083           if (FirstIt->second == SecondIt->second)
10084             continue;
10085 
10086           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10087                                           SourceLocation Loc, SourceRange Range,
10088                                           ODRTemplateDifference DiffType) {
10089             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10090                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10091                    << DiffType;
10092           };
10093           auto ODRDiagTemplateNote = [&SecondModule, this](
10094                                          SourceLocation Loc, SourceRange Range,
10095                                          ODRTemplateDifference DiffType) {
10096             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10097                    << SecondModule << Range << DiffType;
10098           };
10099 
10100           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10101           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10102 
10103           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10104                  "Parameter Decl's should be the same kind.");
10105 
10106           DeclarationName FirstName = FirstDecl->getDeclName();
10107           DeclarationName SecondName = SecondDecl->getDeclName();
10108 
10109           if (FirstName != SecondName) {
10110             const bool FirstNameEmpty =
10111                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10112             const bool SecondNameEmpty =
10113                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10114             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10115                    "Both template parameters cannot be unnamed.");
10116             ODRDiagTemplateError(FirstDecl->getLocation(),
10117                                  FirstDecl->getSourceRange(),
10118                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10119                 << FirstName;
10120             ODRDiagTemplateNote(SecondDecl->getLocation(),
10121                                 SecondDecl->getSourceRange(),
10122                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10123                 << SecondName;
10124             break;
10125           }
10126 
10127           switch (FirstDecl->getKind()) {
10128           default:
10129             llvm_unreachable("Invalid template parameter type.");
10130           case Decl::TemplateTypeParm: {
10131             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10132             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10133             const bool HasFirstDefaultArgument =
10134                 FirstParam->hasDefaultArgument() &&
10135                 !FirstParam->defaultArgumentWasInherited();
10136             const bool HasSecondDefaultArgument =
10137                 SecondParam->hasDefaultArgument() &&
10138                 !SecondParam->defaultArgumentWasInherited();
10139 
10140             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10141               ODRDiagTemplateError(FirstDecl->getLocation(),
10142                                    FirstDecl->getSourceRange(),
10143                                    ParamSingleDefaultArgument)
10144                   << HasFirstDefaultArgument;
10145               ODRDiagTemplateNote(SecondDecl->getLocation(),
10146                                   SecondDecl->getSourceRange(),
10147                                   ParamSingleDefaultArgument)
10148                   << HasSecondDefaultArgument;
10149               break;
10150             }
10151 
10152             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10153                    "Expecting default arguments.");
10154 
10155             ODRDiagTemplateError(FirstDecl->getLocation(),
10156                                  FirstDecl->getSourceRange(),
10157                                  ParamDifferentDefaultArgument);
10158             ODRDiagTemplateNote(SecondDecl->getLocation(),
10159                                 SecondDecl->getSourceRange(),
10160                                 ParamDifferentDefaultArgument);
10161 
10162             break;
10163           }
10164           case Decl::NonTypeTemplateParm: {
10165             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10166             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10167             const bool HasFirstDefaultArgument =
10168                 FirstParam->hasDefaultArgument() &&
10169                 !FirstParam->defaultArgumentWasInherited();
10170             const bool HasSecondDefaultArgument =
10171                 SecondParam->hasDefaultArgument() &&
10172                 !SecondParam->defaultArgumentWasInherited();
10173 
10174             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10175               ODRDiagTemplateError(FirstDecl->getLocation(),
10176                                    FirstDecl->getSourceRange(),
10177                                    ParamSingleDefaultArgument)
10178                   << HasFirstDefaultArgument;
10179               ODRDiagTemplateNote(SecondDecl->getLocation(),
10180                                   SecondDecl->getSourceRange(),
10181                                   ParamSingleDefaultArgument)
10182                   << HasSecondDefaultArgument;
10183               break;
10184             }
10185 
10186             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10187                    "Expecting default arguments.");
10188 
10189             ODRDiagTemplateError(FirstDecl->getLocation(),
10190                                  FirstDecl->getSourceRange(),
10191                                  ParamDifferentDefaultArgument);
10192             ODRDiagTemplateNote(SecondDecl->getLocation(),
10193                                 SecondDecl->getSourceRange(),
10194                                 ParamDifferentDefaultArgument);
10195 
10196             break;
10197           }
10198           case Decl::TemplateTemplateParm: {
10199             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10200             const auto *SecondParam =
10201                 cast<TemplateTemplateParmDecl>(SecondDecl);
10202             const bool HasFirstDefaultArgument =
10203                 FirstParam->hasDefaultArgument() &&
10204                 !FirstParam->defaultArgumentWasInherited();
10205             const bool HasSecondDefaultArgument =
10206                 SecondParam->hasDefaultArgument() &&
10207                 !SecondParam->defaultArgumentWasInherited();
10208 
10209             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10210               ODRDiagTemplateError(FirstDecl->getLocation(),
10211                                    FirstDecl->getSourceRange(),
10212                                    ParamSingleDefaultArgument)
10213                   << HasFirstDefaultArgument;
10214               ODRDiagTemplateNote(SecondDecl->getLocation(),
10215                                   SecondDecl->getSourceRange(),
10216                                   ParamSingleDefaultArgument)
10217                   << HasSecondDefaultArgument;
10218               break;
10219             }
10220 
10221             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10222                    "Expecting default arguments.");
10223 
10224             ODRDiagTemplateError(FirstDecl->getLocation(),
10225                                  FirstDecl->getSourceRange(),
10226                                  ParamDifferentDefaultArgument);
10227             ODRDiagTemplateNote(SecondDecl->getLocation(),
10228                                 SecondDecl->getSourceRange(),
10229                                 ParamDifferentDefaultArgument);
10230 
10231             break;
10232           }
10233           }
10234 
10235           break;
10236         }
10237 
10238         if (FirstIt != FirstEnd) {
10239           Diagnosed = true;
10240           break;
10241         }
10242       }
10243 
10244       DeclHashes FirstHashes;
10245       DeclHashes SecondHashes;
10246       const DeclContext *DC = FirstRecord;
10247       PopulateHashes(FirstHashes, FirstRecord, DC);
10248       PopulateHashes(SecondHashes, SecondRecord, DC);
10249 
10250       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10251       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10252       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10253       Decl *FirstDecl = DR.FirstDecl;
10254       Decl *SecondDecl = DR.SecondDecl;
10255 
10256       if (FirstDiffType == Other || SecondDiffType == Other) {
10257         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10258                               SecondModule);
10259         Diagnosed = true;
10260         break;
10261       }
10262 
10263       if (FirstDiffType != SecondDiffType) {
10264         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10265                             SecondModule);
10266         Diagnosed = true;
10267         break;
10268       }
10269 
10270       assert(FirstDiffType == SecondDiffType);
10271 
10272       switch (FirstDiffType) {
10273       case Other:
10274       case EndOfClass:
10275       case PublicSpecifer:
10276       case PrivateSpecifer:
10277       case ProtectedSpecifer:
10278         llvm_unreachable("Invalid diff type");
10279 
10280       case StaticAssert: {
10281         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10282         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10283 
10284         Expr *FirstExpr = FirstSA->getAssertExpr();
10285         Expr *SecondExpr = SecondSA->getAssertExpr();
10286         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10287         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10288         if (FirstODRHash != SecondODRHash) {
10289           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10290                            FirstExpr->getSourceRange(), StaticAssertCondition);
10291           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10292                           SecondExpr->getSourceRange(), StaticAssertCondition);
10293           Diagnosed = true;
10294           break;
10295         }
10296 
10297         StringLiteral *FirstStr = FirstSA->getMessage();
10298         StringLiteral *SecondStr = SecondSA->getMessage();
10299         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10300         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10301           SourceLocation FirstLoc, SecondLoc;
10302           SourceRange FirstRange, SecondRange;
10303           if (FirstStr) {
10304             FirstLoc = FirstStr->getBeginLoc();
10305             FirstRange = FirstStr->getSourceRange();
10306           } else {
10307             FirstLoc = FirstSA->getBeginLoc();
10308             FirstRange = FirstSA->getSourceRange();
10309           }
10310           if (SecondStr) {
10311             SecondLoc = SecondStr->getBeginLoc();
10312             SecondRange = SecondStr->getSourceRange();
10313           } else {
10314             SecondLoc = SecondSA->getBeginLoc();
10315             SecondRange = SecondSA->getSourceRange();
10316           }
10317           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10318                            StaticAssertOnlyMessage)
10319               << (FirstStr == nullptr);
10320           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10321                           StaticAssertOnlyMessage)
10322               << (SecondStr == nullptr);
10323           Diagnosed = true;
10324           break;
10325         }
10326 
10327         if (FirstStr && SecondStr &&
10328             FirstStr->getString() != SecondStr->getString()) {
10329           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10330                            FirstStr->getSourceRange(), StaticAssertMessage);
10331           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10332                           SecondStr->getSourceRange(), StaticAssertMessage);
10333           Diagnosed = true;
10334           break;
10335         }
10336         break;
10337       }
10338       case Field: {
10339         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10340                                  cast<FieldDecl>(FirstDecl),
10341                                  cast<FieldDecl>(SecondDecl));
10342         break;
10343       }
10344       case CXXMethod: {
10345         enum {
10346           DiagMethod,
10347           DiagConstructor,
10348           DiagDestructor,
10349         } FirstMethodType,
10350             SecondMethodType;
10351         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10352           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10353           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10354           return DiagMethod;
10355         };
10356         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10357         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10358         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10359         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10360         auto FirstName = FirstMethod->getDeclName();
10361         auto SecondName = SecondMethod->getDeclName();
10362         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10363           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10364                            FirstMethod->getSourceRange(), MethodName)
10365               << FirstMethodType << FirstName;
10366           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10367                           SecondMethod->getSourceRange(), MethodName)
10368               << SecondMethodType << SecondName;
10369 
10370           Diagnosed = true;
10371           break;
10372         }
10373 
10374         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10375         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10376         if (FirstDeleted != SecondDeleted) {
10377           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10378                            FirstMethod->getSourceRange(), MethodDeleted)
10379               << FirstMethodType << FirstName << FirstDeleted;
10380 
10381           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10382                           SecondMethod->getSourceRange(), MethodDeleted)
10383               << SecondMethodType << SecondName << SecondDeleted;
10384           Diagnosed = true;
10385           break;
10386         }
10387 
10388         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10389         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10390         if (FirstDefaulted != SecondDefaulted) {
10391           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10392                            FirstMethod->getSourceRange(), MethodDefaulted)
10393               << FirstMethodType << FirstName << FirstDefaulted;
10394 
10395           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10396                           SecondMethod->getSourceRange(), MethodDefaulted)
10397               << SecondMethodType << SecondName << SecondDefaulted;
10398           Diagnosed = true;
10399           break;
10400         }
10401 
10402         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10403         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10404         const bool FirstPure = FirstMethod->isPure();
10405         const bool SecondPure = SecondMethod->isPure();
10406         if ((FirstVirtual || SecondVirtual) &&
10407             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10408           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10409                            FirstMethod->getSourceRange(), MethodVirtual)
10410               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10411           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10412                           SecondMethod->getSourceRange(), MethodVirtual)
10413               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10414           Diagnosed = true;
10415           break;
10416         }
10417 
10418         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10419         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10420         // class needs to be checked instead.
10421         const auto FirstStorage = FirstMethod->getStorageClass();
10422         const auto SecondStorage = SecondMethod->getStorageClass();
10423         const bool FirstStatic = FirstStorage == SC_Static;
10424         const bool SecondStatic = SecondStorage == SC_Static;
10425         if (FirstStatic != SecondStatic) {
10426           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10427                            FirstMethod->getSourceRange(), MethodStatic)
10428               << FirstMethodType << FirstName << FirstStatic;
10429           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10430                           SecondMethod->getSourceRange(), MethodStatic)
10431               << SecondMethodType << SecondName << SecondStatic;
10432           Diagnosed = true;
10433           break;
10434         }
10435 
10436         const bool FirstVolatile = FirstMethod->isVolatile();
10437         const bool SecondVolatile = SecondMethod->isVolatile();
10438         if (FirstVolatile != SecondVolatile) {
10439           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10440                            FirstMethod->getSourceRange(), MethodVolatile)
10441               << FirstMethodType << FirstName << FirstVolatile;
10442           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10443                           SecondMethod->getSourceRange(), MethodVolatile)
10444               << SecondMethodType << SecondName << SecondVolatile;
10445           Diagnosed = true;
10446           break;
10447         }
10448 
10449         const bool FirstConst = FirstMethod->isConst();
10450         const bool SecondConst = SecondMethod->isConst();
10451         if (FirstConst != SecondConst) {
10452           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10453                            FirstMethod->getSourceRange(), MethodConst)
10454               << FirstMethodType << FirstName << FirstConst;
10455           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10456                           SecondMethod->getSourceRange(), MethodConst)
10457               << SecondMethodType << SecondName << SecondConst;
10458           Diagnosed = true;
10459           break;
10460         }
10461 
10462         const bool FirstInline = FirstMethod->isInlineSpecified();
10463         const bool SecondInline = SecondMethod->isInlineSpecified();
10464         if (FirstInline != SecondInline) {
10465           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10466                            FirstMethod->getSourceRange(), MethodInline)
10467               << FirstMethodType << FirstName << FirstInline;
10468           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10469                           SecondMethod->getSourceRange(), MethodInline)
10470               << SecondMethodType << SecondName << SecondInline;
10471           Diagnosed = true;
10472           break;
10473         }
10474 
10475         const unsigned FirstNumParameters = FirstMethod->param_size();
10476         const unsigned SecondNumParameters = SecondMethod->param_size();
10477         if (FirstNumParameters != SecondNumParameters) {
10478           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10479                            FirstMethod->getSourceRange(),
10480                            MethodNumberParameters)
10481               << FirstMethodType << FirstName << FirstNumParameters;
10482           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10483                           SecondMethod->getSourceRange(),
10484                           MethodNumberParameters)
10485               << SecondMethodType << SecondName << SecondNumParameters;
10486           Diagnosed = true;
10487           break;
10488         }
10489 
10490         // Need this status boolean to know when break out of the switch.
10491         bool ParameterMismatch = false;
10492         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10493           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10494           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10495 
10496           QualType FirstParamType = FirstParam->getType();
10497           QualType SecondParamType = SecondParam->getType();
10498           if (FirstParamType != SecondParamType &&
10499               ComputeQualTypeODRHash(FirstParamType) !=
10500                   ComputeQualTypeODRHash(SecondParamType)) {
10501             if (const DecayedType *ParamDecayedType =
10502                     FirstParamType->getAs<DecayedType>()) {
10503               ODRDiagDeclError(
10504                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10505                   FirstMethod->getSourceRange(), MethodParameterType)
10506                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10507                   << true << ParamDecayedType->getOriginalType();
10508             } else {
10509               ODRDiagDeclError(
10510                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10511                   FirstMethod->getSourceRange(), MethodParameterType)
10512                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10513                   << false;
10514             }
10515 
10516             if (const DecayedType *ParamDecayedType =
10517                     SecondParamType->getAs<DecayedType>()) {
10518               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10519                               SecondMethod->getSourceRange(),
10520                               MethodParameterType)
10521                   << SecondMethodType << SecondName << (I + 1)
10522                   << SecondParamType << true
10523                   << ParamDecayedType->getOriginalType();
10524             } else {
10525               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10526                               SecondMethod->getSourceRange(),
10527                               MethodParameterType)
10528                   << SecondMethodType << SecondName << (I + 1)
10529                   << SecondParamType << false;
10530             }
10531             ParameterMismatch = true;
10532             break;
10533           }
10534 
10535           DeclarationName FirstParamName = FirstParam->getDeclName();
10536           DeclarationName SecondParamName = SecondParam->getDeclName();
10537           if (FirstParamName != SecondParamName) {
10538             ODRDiagDeclError(FirstRecord, FirstModule,
10539                              FirstMethod->getLocation(),
10540                              FirstMethod->getSourceRange(), MethodParameterName)
10541                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10542             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10543                             SecondMethod->getSourceRange(), MethodParameterName)
10544                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10545             ParameterMismatch = true;
10546             break;
10547           }
10548 
10549           const Expr *FirstInit = FirstParam->getInit();
10550           const Expr *SecondInit = SecondParam->getInit();
10551           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10552             ODRDiagDeclError(FirstRecord, FirstModule,
10553                              FirstMethod->getLocation(),
10554                              FirstMethod->getSourceRange(),
10555                              MethodParameterSingleDefaultArgument)
10556                 << FirstMethodType << FirstName << (I + 1)
10557                 << (FirstInit == nullptr)
10558                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10559             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10560                             SecondMethod->getSourceRange(),
10561                             MethodParameterSingleDefaultArgument)
10562                 << SecondMethodType << SecondName << (I + 1)
10563                 << (SecondInit == nullptr)
10564                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10565             ParameterMismatch = true;
10566             break;
10567           }
10568 
10569           if (FirstInit && SecondInit &&
10570               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10571             ODRDiagDeclError(FirstRecord, FirstModule,
10572                              FirstMethod->getLocation(),
10573                              FirstMethod->getSourceRange(),
10574                              MethodParameterDifferentDefaultArgument)
10575                 << FirstMethodType << FirstName << (I + 1)
10576                 << FirstInit->getSourceRange();
10577             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10578                             SecondMethod->getSourceRange(),
10579                             MethodParameterDifferentDefaultArgument)
10580                 << SecondMethodType << SecondName << (I + 1)
10581                 << SecondInit->getSourceRange();
10582             ParameterMismatch = true;
10583             break;
10584 
10585           }
10586         }
10587 
10588         if (ParameterMismatch) {
10589           Diagnosed = true;
10590           break;
10591         }
10592 
10593         const auto *FirstTemplateArgs =
10594             FirstMethod->getTemplateSpecializationArgs();
10595         const auto *SecondTemplateArgs =
10596             SecondMethod->getTemplateSpecializationArgs();
10597 
10598         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10599             (!FirstTemplateArgs && SecondTemplateArgs)) {
10600           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10601                            FirstMethod->getSourceRange(),
10602                            MethodNoTemplateArguments)
10603               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10604           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10605                           SecondMethod->getSourceRange(),
10606                           MethodNoTemplateArguments)
10607               << SecondMethodType << SecondName
10608               << (SecondTemplateArgs != nullptr);
10609 
10610           Diagnosed = true;
10611           break;
10612         }
10613 
10614         if (FirstTemplateArgs && SecondTemplateArgs) {
10615           // Remove pack expansions from argument list.
10616           auto ExpandTemplateArgumentList =
10617               [](const TemplateArgumentList *TAL) {
10618                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10619                 for (const TemplateArgument &TA : TAL->asArray()) {
10620                   if (TA.getKind() != TemplateArgument::Pack) {
10621                     ExpandedList.push_back(&TA);
10622                     continue;
10623                   }
10624                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10625                     ExpandedList.push_back(&PackTA);
10626                   }
10627                 }
10628                 return ExpandedList;
10629               };
10630           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10631               ExpandTemplateArgumentList(FirstTemplateArgs);
10632           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10633               ExpandTemplateArgumentList(SecondTemplateArgs);
10634 
10635           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10636             ODRDiagDeclError(FirstRecord, FirstModule,
10637                              FirstMethod->getLocation(),
10638                              FirstMethod->getSourceRange(),
10639                              MethodDifferentNumberTemplateArguments)
10640                 << FirstMethodType << FirstName
10641                 << (unsigned)FirstExpandedList.size();
10642             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10643                             SecondMethod->getSourceRange(),
10644                             MethodDifferentNumberTemplateArguments)
10645                 << SecondMethodType << SecondName
10646                 << (unsigned)SecondExpandedList.size();
10647 
10648             Diagnosed = true;
10649             break;
10650           }
10651 
10652           bool TemplateArgumentMismatch = false;
10653           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10654             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10655                                    &SecondTA = *SecondExpandedList[i];
10656             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10657                 ComputeTemplateArgumentODRHash(SecondTA)) {
10658               continue;
10659             }
10660 
10661             ODRDiagDeclError(
10662                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10663                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10664                 << FirstMethodType << FirstName << FirstTA << i + 1;
10665             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10666                             SecondMethod->getSourceRange(),
10667                             MethodDifferentTemplateArgument)
10668                 << SecondMethodType << SecondName << SecondTA << i + 1;
10669 
10670             TemplateArgumentMismatch = true;
10671             break;
10672           }
10673 
10674           if (TemplateArgumentMismatch) {
10675             Diagnosed = true;
10676             break;
10677           }
10678         }
10679 
10680         // Compute the hash of the method as if it has no body.
10681         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10682           Hash.clear();
10683           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10684           return Hash.CalculateHash();
10685         };
10686 
10687         // Compare the hash generated to the hash stored.  A difference means
10688         // that a body was present in the original source.  Due to merging,
10689         // the stardard way of detecting a body will not work.
10690         const bool HasFirstBody =
10691             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10692         const bool HasSecondBody =
10693             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10694 
10695         if (HasFirstBody != HasSecondBody) {
10696           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10697                            FirstMethod->getSourceRange(), MethodSingleBody)
10698               << FirstMethodType << FirstName << HasFirstBody;
10699           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10700                           SecondMethod->getSourceRange(), MethodSingleBody)
10701               << SecondMethodType << SecondName << HasSecondBody;
10702           Diagnosed = true;
10703           break;
10704         }
10705 
10706         if (HasFirstBody && HasSecondBody) {
10707           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10708                            FirstMethod->getSourceRange(), MethodDifferentBody)
10709               << FirstMethodType << FirstName;
10710           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10711                           SecondMethod->getSourceRange(), MethodDifferentBody)
10712               << SecondMethodType << SecondName;
10713           Diagnosed = true;
10714           break;
10715         }
10716 
10717         break;
10718       }
10719       case TypeAlias:
10720       case TypeDef: {
10721         Diagnosed = ODRDiagTypeDefOrAlias(
10722             FirstRecord, FirstModule, SecondModule,
10723             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10724             FirstDiffType == TypeAlias);
10725         break;
10726       }
10727       case Var: {
10728         Diagnosed =
10729             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10730                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10731         break;
10732       }
10733       case Friend: {
10734         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10735         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10736 
10737         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10738         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10739 
10740         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10741         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10742 
10743         if (FirstND && SecondND) {
10744           ODRDiagDeclError(FirstRecord, FirstModule,
10745                            FirstFriend->getFriendLoc(),
10746                            FirstFriend->getSourceRange(), FriendFunction)
10747               << FirstND;
10748           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10749                           SecondFriend->getSourceRange(), FriendFunction)
10750               << SecondND;
10751 
10752           Diagnosed = true;
10753           break;
10754         }
10755 
10756         if (FirstTSI && SecondTSI) {
10757           QualType FirstFriendType = FirstTSI->getType();
10758           QualType SecondFriendType = SecondTSI->getType();
10759           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10760                  ComputeQualTypeODRHash(SecondFriendType));
10761           ODRDiagDeclError(FirstRecord, FirstModule,
10762                            FirstFriend->getFriendLoc(),
10763                            FirstFriend->getSourceRange(), FriendType)
10764               << FirstFriendType;
10765           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10766                           SecondFriend->getSourceRange(), FriendType)
10767               << SecondFriendType;
10768           Diagnosed = true;
10769           break;
10770         }
10771 
10772         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10773                          FirstFriend->getSourceRange(), FriendTypeFunction)
10774             << (FirstTSI == nullptr);
10775         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10776                         SecondFriend->getSourceRange(), FriendTypeFunction)
10777             << (SecondTSI == nullptr);
10778 
10779         Diagnosed = true;
10780         break;
10781       }
10782       case FunctionTemplate: {
10783         FunctionTemplateDecl *FirstTemplate =
10784             cast<FunctionTemplateDecl>(FirstDecl);
10785         FunctionTemplateDecl *SecondTemplate =
10786             cast<FunctionTemplateDecl>(SecondDecl);
10787 
10788         TemplateParameterList *FirstTPL =
10789             FirstTemplate->getTemplateParameters();
10790         TemplateParameterList *SecondTPL =
10791             SecondTemplate->getTemplateParameters();
10792 
10793         if (FirstTPL->size() != SecondTPL->size()) {
10794           ODRDiagDeclError(FirstRecord, FirstModule,
10795                            FirstTemplate->getLocation(),
10796                            FirstTemplate->getSourceRange(),
10797                            FunctionTemplateDifferentNumberParameters)
10798               << FirstTemplate << FirstTPL->size();
10799           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10800                           SecondTemplate->getSourceRange(),
10801                           FunctionTemplateDifferentNumberParameters)
10802               << SecondTemplate << SecondTPL->size();
10803 
10804           Diagnosed = true;
10805           break;
10806         }
10807 
10808         bool ParameterMismatch = false;
10809         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10810           NamedDecl *FirstParam = FirstTPL->getParam(i);
10811           NamedDecl *SecondParam = SecondTPL->getParam(i);
10812 
10813           if (FirstParam->getKind() != SecondParam->getKind()) {
10814             enum {
10815               TemplateTypeParameter,
10816               NonTypeTemplateParameter,
10817               TemplateTemplateParameter,
10818             };
10819             auto GetParamType = [](NamedDecl *D) {
10820               switch (D->getKind()) {
10821                 default:
10822                   llvm_unreachable("Unexpected template parameter type");
10823                 case Decl::TemplateTypeParm:
10824                   return TemplateTypeParameter;
10825                 case Decl::NonTypeTemplateParm:
10826                   return NonTypeTemplateParameter;
10827                 case Decl::TemplateTemplateParm:
10828                   return TemplateTemplateParameter;
10829               }
10830             };
10831 
10832             ODRDiagDeclError(FirstRecord, FirstModule,
10833                              FirstTemplate->getLocation(),
10834                              FirstTemplate->getSourceRange(),
10835                              FunctionTemplateParameterDifferentKind)
10836                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10837             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10838                             SecondTemplate->getSourceRange(),
10839                             FunctionTemplateParameterDifferentKind)
10840                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10841 
10842             ParameterMismatch = true;
10843             break;
10844           }
10845 
10846           if (FirstParam->getName() != SecondParam->getName()) {
10847             ODRDiagDeclError(
10848                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10849                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10850                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10851                 << FirstParam;
10852             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10853                             SecondTemplate->getSourceRange(),
10854                             FunctionTemplateParameterName)
10855                 << SecondTemplate << (i + 1)
10856                 << (bool)SecondParam->getIdentifier() << SecondParam;
10857             ParameterMismatch = true;
10858             break;
10859           }
10860 
10861           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10862               isa<TemplateTypeParmDecl>(SecondParam)) {
10863             TemplateTypeParmDecl *FirstTTPD =
10864                 cast<TemplateTypeParmDecl>(FirstParam);
10865             TemplateTypeParmDecl *SecondTTPD =
10866                 cast<TemplateTypeParmDecl>(SecondParam);
10867             bool HasFirstDefaultArgument =
10868                 FirstTTPD->hasDefaultArgument() &&
10869                 !FirstTTPD->defaultArgumentWasInherited();
10870             bool HasSecondDefaultArgument =
10871                 SecondTTPD->hasDefaultArgument() &&
10872                 !SecondTTPD->defaultArgumentWasInherited();
10873             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10874               ODRDiagDeclError(FirstRecord, FirstModule,
10875                                FirstTemplate->getLocation(),
10876                                FirstTemplate->getSourceRange(),
10877                                FunctionTemplateParameterSingleDefaultArgument)
10878                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10879               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10880                               SecondTemplate->getSourceRange(),
10881                               FunctionTemplateParameterSingleDefaultArgument)
10882                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10883               ParameterMismatch = true;
10884               break;
10885             }
10886 
10887             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10888               QualType FirstType = FirstTTPD->getDefaultArgument();
10889               QualType SecondType = SecondTTPD->getDefaultArgument();
10890               if (ComputeQualTypeODRHash(FirstType) !=
10891                   ComputeQualTypeODRHash(SecondType)) {
10892                 ODRDiagDeclError(
10893                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10894                     FirstTemplate->getSourceRange(),
10895                     FunctionTemplateParameterDifferentDefaultArgument)
10896                     << FirstTemplate << (i + 1) << FirstType;
10897                 ODRDiagDeclNote(
10898                     SecondModule, SecondTemplate->getLocation(),
10899                     SecondTemplate->getSourceRange(),
10900                     FunctionTemplateParameterDifferentDefaultArgument)
10901                     << SecondTemplate << (i + 1) << SecondType;
10902                 ParameterMismatch = true;
10903                 break;
10904               }
10905             }
10906 
10907             if (FirstTTPD->isParameterPack() !=
10908                 SecondTTPD->isParameterPack()) {
10909               ODRDiagDeclError(FirstRecord, FirstModule,
10910                                FirstTemplate->getLocation(),
10911                                FirstTemplate->getSourceRange(),
10912                                FunctionTemplatePackParameter)
10913                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10914               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10915                               SecondTemplate->getSourceRange(),
10916                               FunctionTemplatePackParameter)
10917                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10918               ParameterMismatch = true;
10919               break;
10920             }
10921           }
10922 
10923           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10924               isa<TemplateTemplateParmDecl>(SecondParam)) {
10925             TemplateTemplateParmDecl *FirstTTPD =
10926                 cast<TemplateTemplateParmDecl>(FirstParam);
10927             TemplateTemplateParmDecl *SecondTTPD =
10928                 cast<TemplateTemplateParmDecl>(SecondParam);
10929 
10930             TemplateParameterList *FirstTPL =
10931                 FirstTTPD->getTemplateParameters();
10932             TemplateParameterList *SecondTPL =
10933                 SecondTTPD->getTemplateParameters();
10934 
10935             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10936                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10937               ODRDiagDeclError(FirstRecord, FirstModule,
10938                                FirstTemplate->getLocation(),
10939                                FirstTemplate->getSourceRange(),
10940                                FunctionTemplateParameterDifferentType)
10941                   << FirstTemplate << (i + 1);
10942               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10943                               SecondTemplate->getSourceRange(),
10944                               FunctionTemplateParameterDifferentType)
10945                   << SecondTemplate << (i + 1);
10946               ParameterMismatch = true;
10947               break;
10948             }
10949 
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               TemplateArgument FirstTA =
10972                   FirstTTPD->getDefaultArgument().getArgument();
10973               TemplateArgument SecondTA =
10974                   SecondTTPD->getDefaultArgument().getArgument();
10975               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10976                   ComputeTemplateArgumentODRHash(SecondTA)) {
10977                 ODRDiagDeclError(
10978                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10979                     FirstTemplate->getSourceRange(),
10980                     FunctionTemplateParameterDifferentDefaultArgument)
10981                     << FirstTemplate << (i + 1) << FirstTA;
10982                 ODRDiagDeclNote(
10983                     SecondModule, SecondTemplate->getLocation(),
10984                     SecondTemplate->getSourceRange(),
10985                     FunctionTemplateParameterDifferentDefaultArgument)
10986                     << SecondTemplate << (i + 1) << SecondTA;
10987                 ParameterMismatch = true;
10988                 break;
10989               }
10990             }
10991 
10992             if (FirstTTPD->isParameterPack() !=
10993                 SecondTTPD->isParameterPack()) {
10994               ODRDiagDeclError(FirstRecord, FirstModule,
10995                                FirstTemplate->getLocation(),
10996                                FirstTemplate->getSourceRange(),
10997                                FunctionTemplatePackParameter)
10998                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10999               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11000                               SecondTemplate->getSourceRange(),
11001                               FunctionTemplatePackParameter)
11002                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11003               ParameterMismatch = true;
11004               break;
11005             }
11006           }
11007 
11008           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11009               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11010             NonTypeTemplateParmDecl *FirstNTTPD =
11011                 cast<NonTypeTemplateParmDecl>(FirstParam);
11012             NonTypeTemplateParmDecl *SecondNTTPD =
11013                 cast<NonTypeTemplateParmDecl>(SecondParam);
11014 
11015             QualType FirstType = FirstNTTPD->getType();
11016             QualType SecondType = SecondNTTPD->getType();
11017             if (ComputeQualTypeODRHash(FirstType) !=
11018                 ComputeQualTypeODRHash(SecondType)) {
11019               ODRDiagDeclError(FirstRecord, FirstModule,
11020                                FirstTemplate->getLocation(),
11021                                FirstTemplate->getSourceRange(),
11022                                FunctionTemplateParameterDifferentType)
11023                   << FirstTemplate << (i + 1);
11024               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11025                               SecondTemplate->getSourceRange(),
11026                               FunctionTemplateParameterDifferentType)
11027                   << SecondTemplate << (i + 1);
11028               ParameterMismatch = true;
11029               break;
11030             }
11031 
11032             bool HasFirstDefaultArgument =
11033                 FirstNTTPD->hasDefaultArgument() &&
11034                 !FirstNTTPD->defaultArgumentWasInherited();
11035             bool HasSecondDefaultArgument =
11036                 SecondNTTPD->hasDefaultArgument() &&
11037                 !SecondNTTPD->defaultArgumentWasInherited();
11038             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11039               ODRDiagDeclError(FirstRecord, FirstModule,
11040                                FirstTemplate->getLocation(),
11041                                FirstTemplate->getSourceRange(),
11042                                FunctionTemplateParameterSingleDefaultArgument)
11043                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11044               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11045                               SecondTemplate->getSourceRange(),
11046                               FunctionTemplateParameterSingleDefaultArgument)
11047                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11048               ParameterMismatch = true;
11049               break;
11050             }
11051 
11052             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11053               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11054               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11055               if (ComputeODRHash(FirstDefaultArgument) !=
11056                   ComputeODRHash(SecondDefaultArgument)) {
11057                 ODRDiagDeclError(
11058                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11059                     FirstTemplate->getSourceRange(),
11060                     FunctionTemplateParameterDifferentDefaultArgument)
11061                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11062                 ODRDiagDeclNote(
11063                     SecondModule, SecondTemplate->getLocation(),
11064                     SecondTemplate->getSourceRange(),
11065                     FunctionTemplateParameterDifferentDefaultArgument)
11066                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11067                 ParameterMismatch = true;
11068                 break;
11069               }
11070             }
11071 
11072             if (FirstNTTPD->isParameterPack() !=
11073                 SecondNTTPD->isParameterPack()) {
11074               ODRDiagDeclError(FirstRecord, FirstModule,
11075                                FirstTemplate->getLocation(),
11076                                FirstTemplate->getSourceRange(),
11077                                FunctionTemplatePackParameter)
11078                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11079               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11080                               SecondTemplate->getSourceRange(),
11081                               FunctionTemplatePackParameter)
11082                   << SecondTemplate << (i + 1)
11083                   << SecondNTTPD->isParameterPack();
11084               ParameterMismatch = true;
11085               break;
11086             }
11087           }
11088         }
11089 
11090         if (ParameterMismatch) {
11091           Diagnosed = true;
11092           break;
11093         }
11094 
11095         break;
11096       }
11097       }
11098 
11099       if (Diagnosed)
11100         continue;
11101 
11102       Diag(FirstDecl->getLocation(),
11103            diag::err_module_odr_violation_mismatch_decl_unknown)
11104           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11105           << FirstDecl->getSourceRange();
11106       Diag(SecondDecl->getLocation(),
11107            diag::note_module_odr_violation_mismatch_decl_unknown)
11108           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11109       Diagnosed = true;
11110     }
11111 
11112     if (!Diagnosed) {
11113       // All definitions are updates to the same declaration. This happens if a
11114       // module instantiates the declaration of a class template specialization
11115       // and two or more other modules instantiate its definition.
11116       //
11117       // FIXME: Indicate which modules had instantiations of this definition.
11118       // FIXME: How can this even happen?
11119       Diag(Merge.first->getLocation(),
11120            diag::err_module_odr_violation_different_instantiations)
11121         << Merge.first;
11122     }
11123   }
11124 
11125   // Issue ODR failures diagnostics for functions.
11126   for (auto &Merge : FunctionOdrMergeFailures) {
11127     enum ODRFunctionDifference {
11128       ReturnType,
11129       ParameterName,
11130       ParameterType,
11131       ParameterSingleDefaultArgument,
11132       ParameterDifferentDefaultArgument,
11133       FunctionBody,
11134     };
11135 
11136     FunctionDecl *FirstFunction = Merge.first;
11137     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11138 
11139     bool Diagnosed = false;
11140     for (auto &SecondFunction : Merge.second) {
11141 
11142       if (FirstFunction == SecondFunction)
11143         continue;
11144 
11145       std::string SecondModule =
11146           getOwningModuleNameForDiagnostic(SecondFunction);
11147 
11148       auto ODRDiagError = [FirstFunction, &FirstModule,
11149                            this](SourceLocation Loc, SourceRange Range,
11150                                  ODRFunctionDifference DiffType) {
11151         return Diag(Loc, diag::err_module_odr_violation_function)
11152                << FirstFunction << FirstModule.empty() << FirstModule << Range
11153                << DiffType;
11154       };
11155       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11156                                                SourceRange Range,
11157                                                ODRFunctionDifference DiffType) {
11158         return Diag(Loc, diag::note_module_odr_violation_function)
11159                << SecondModule << Range << DiffType;
11160       };
11161 
11162       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11163           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11164         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11165                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11166             << FirstFunction->getReturnType();
11167         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11168                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11169             << SecondFunction->getReturnType();
11170         Diagnosed = true;
11171         break;
11172       }
11173 
11174       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11175              "Merged functions with different number of parameters");
11176 
11177       auto ParamSize = FirstFunction->param_size();
11178       bool ParameterMismatch = false;
11179       for (unsigned I = 0; I < ParamSize; ++I) {
11180         auto *FirstParam = FirstFunction->getParamDecl(I);
11181         auto *SecondParam = SecondFunction->getParamDecl(I);
11182 
11183         assert(getContext().hasSameType(FirstParam->getType(),
11184                                       SecondParam->getType()) &&
11185                "Merged function has different parameter types.");
11186 
11187         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11188           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11189                        ParameterName)
11190               << I + 1 << FirstParam->getDeclName();
11191           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11192                       ParameterName)
11193               << I + 1 << SecondParam->getDeclName();
11194           ParameterMismatch = true;
11195           break;
11196         };
11197 
11198         QualType FirstParamType = FirstParam->getType();
11199         QualType SecondParamType = SecondParam->getType();
11200         if (FirstParamType != SecondParamType &&
11201             ComputeQualTypeODRHash(FirstParamType) !=
11202                 ComputeQualTypeODRHash(SecondParamType)) {
11203           if (const DecayedType *ParamDecayedType =
11204                   FirstParamType->getAs<DecayedType>()) {
11205             ODRDiagError(FirstParam->getLocation(),
11206                          FirstParam->getSourceRange(), ParameterType)
11207                 << (I + 1) << FirstParamType << true
11208                 << ParamDecayedType->getOriginalType();
11209           } else {
11210             ODRDiagError(FirstParam->getLocation(),
11211                          FirstParam->getSourceRange(), ParameterType)
11212                 << (I + 1) << FirstParamType << false;
11213           }
11214 
11215           if (const DecayedType *ParamDecayedType =
11216                   SecondParamType->getAs<DecayedType>()) {
11217             ODRDiagNote(SecondParam->getLocation(),
11218                         SecondParam->getSourceRange(), ParameterType)
11219                 << (I + 1) << SecondParamType << true
11220                 << ParamDecayedType->getOriginalType();
11221           } else {
11222             ODRDiagNote(SecondParam->getLocation(),
11223                         SecondParam->getSourceRange(), ParameterType)
11224                 << (I + 1) << SecondParamType << false;
11225           }
11226           ParameterMismatch = true;
11227           break;
11228         }
11229 
11230         const Expr *FirstInit = FirstParam->getInit();
11231         const Expr *SecondInit = SecondParam->getInit();
11232         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11233           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11234                        ParameterSingleDefaultArgument)
11235               << (I + 1) << (FirstInit == nullptr)
11236               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11237           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11238                       ParameterSingleDefaultArgument)
11239               << (I + 1) << (SecondInit == nullptr)
11240               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11241           ParameterMismatch = true;
11242           break;
11243         }
11244 
11245         if (FirstInit && SecondInit &&
11246             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11247           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11248                        ParameterDifferentDefaultArgument)
11249               << (I + 1) << FirstInit->getSourceRange();
11250           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11251                       ParameterDifferentDefaultArgument)
11252               << (I + 1) << SecondInit->getSourceRange();
11253           ParameterMismatch = true;
11254           break;
11255         }
11256 
11257         assert(ComputeSubDeclODRHash(FirstParam) ==
11258                    ComputeSubDeclODRHash(SecondParam) &&
11259                "Undiagnosed parameter difference.");
11260       }
11261 
11262       if (ParameterMismatch) {
11263         Diagnosed = true;
11264         break;
11265       }
11266 
11267       // If no error has been generated before now, assume the problem is in
11268       // the body and generate a message.
11269       ODRDiagError(FirstFunction->getLocation(),
11270                    FirstFunction->getSourceRange(), FunctionBody);
11271       ODRDiagNote(SecondFunction->getLocation(),
11272                   SecondFunction->getSourceRange(), FunctionBody);
11273       Diagnosed = true;
11274       break;
11275     }
11276     (void)Diagnosed;
11277     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11278   }
11279 
11280   // Issue ODR failures diagnostics for enums.
11281   for (auto &Merge : EnumOdrMergeFailures) {
11282     enum ODREnumDifference {
11283       SingleScopedEnum,
11284       EnumTagKeywordMismatch,
11285       SingleSpecifiedType,
11286       DifferentSpecifiedTypes,
11287       DifferentNumberEnumConstants,
11288       EnumConstantName,
11289       EnumConstantSingleInitilizer,
11290       EnumConstantDifferentInitilizer,
11291     };
11292 
11293     // If we've already pointed out a specific problem with this enum, don't
11294     // bother issuing a general "something's different" diagnostic.
11295     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11296       continue;
11297 
11298     EnumDecl *FirstEnum = Merge.first;
11299     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11300 
11301     using DeclHashes =
11302         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11303     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11304                               DeclHashes &Hashes, EnumDecl *Enum) {
11305       for (auto *D : Enum->decls()) {
11306         // Due to decl merging, the first EnumDecl is the parent of
11307         // Decls in both records.
11308         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11309           continue;
11310         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11311         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11312                             ComputeSubDeclODRHash(D));
11313       }
11314     };
11315     DeclHashes FirstHashes;
11316     PopulateHashes(FirstHashes, FirstEnum);
11317     bool Diagnosed = false;
11318     for (auto &SecondEnum : Merge.second) {
11319 
11320       if (FirstEnum == SecondEnum)
11321         continue;
11322 
11323       std::string SecondModule =
11324           getOwningModuleNameForDiagnostic(SecondEnum);
11325 
11326       auto ODRDiagError = [FirstEnum, &FirstModule,
11327                            this](SourceLocation Loc, SourceRange Range,
11328                                  ODREnumDifference DiffType) {
11329         return Diag(Loc, diag::err_module_odr_violation_enum)
11330                << FirstEnum << FirstModule.empty() << FirstModule << Range
11331                << DiffType;
11332       };
11333       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11334                                                SourceRange Range,
11335                                                ODREnumDifference DiffType) {
11336         return Diag(Loc, diag::note_module_odr_violation_enum)
11337                << SecondModule << Range << DiffType;
11338       };
11339 
11340       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11341         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11342                      SingleScopedEnum)
11343             << FirstEnum->isScoped();
11344         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11345                     SingleScopedEnum)
11346             << SecondEnum->isScoped();
11347         Diagnosed = true;
11348         continue;
11349       }
11350 
11351       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11352         if (FirstEnum->isScopedUsingClassTag() !=
11353             SecondEnum->isScopedUsingClassTag()) {
11354           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11355                        EnumTagKeywordMismatch)
11356               << FirstEnum->isScopedUsingClassTag();
11357           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11358                       EnumTagKeywordMismatch)
11359               << SecondEnum->isScopedUsingClassTag();
11360           Diagnosed = true;
11361           continue;
11362         }
11363       }
11364 
11365       QualType FirstUnderlyingType =
11366           FirstEnum->getIntegerTypeSourceInfo()
11367               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11368               : QualType();
11369       QualType SecondUnderlyingType =
11370           SecondEnum->getIntegerTypeSourceInfo()
11371               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11372               : QualType();
11373       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11374           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11375                        SingleSpecifiedType)
11376               << !FirstUnderlyingType.isNull();
11377           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11378                       SingleSpecifiedType)
11379               << !SecondUnderlyingType.isNull();
11380           Diagnosed = true;
11381           continue;
11382       }
11383 
11384       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11385         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11386             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11387           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11388                        DifferentSpecifiedTypes)
11389               << FirstUnderlyingType;
11390           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11391                       DifferentSpecifiedTypes)
11392               << SecondUnderlyingType;
11393           Diagnosed = true;
11394           continue;
11395         }
11396       }
11397 
11398       DeclHashes SecondHashes;
11399       PopulateHashes(SecondHashes, SecondEnum);
11400 
11401       if (FirstHashes.size() != SecondHashes.size()) {
11402         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11403                      DifferentNumberEnumConstants)
11404             << (int)FirstHashes.size();
11405         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11406                     DifferentNumberEnumConstants)
11407             << (int)SecondHashes.size();
11408         Diagnosed = true;
11409         continue;
11410       }
11411 
11412       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11413         if (FirstHashes[I].second == SecondHashes[I].second)
11414           continue;
11415         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11416         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11417 
11418         if (FirstEnumConstant->getDeclName() !=
11419             SecondEnumConstant->getDeclName()) {
11420 
11421           ODRDiagError(FirstEnumConstant->getLocation(),
11422                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11423               << I + 1 << FirstEnumConstant;
11424           ODRDiagNote(SecondEnumConstant->getLocation(),
11425                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11426               << I + 1 << SecondEnumConstant;
11427           Diagnosed = true;
11428           break;
11429         }
11430 
11431         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11432         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11433         if (!FirstInit && !SecondInit)
11434           continue;
11435 
11436         if (!FirstInit || !SecondInit) {
11437           ODRDiagError(FirstEnumConstant->getLocation(),
11438                        FirstEnumConstant->getSourceRange(),
11439                        EnumConstantSingleInitilizer)
11440               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11441           ODRDiagNote(SecondEnumConstant->getLocation(),
11442                       SecondEnumConstant->getSourceRange(),
11443                       EnumConstantSingleInitilizer)
11444               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11445           Diagnosed = true;
11446           break;
11447         }
11448 
11449         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11450           ODRDiagError(FirstEnumConstant->getLocation(),
11451                        FirstEnumConstant->getSourceRange(),
11452                        EnumConstantDifferentInitilizer)
11453               << I + 1 << FirstEnumConstant;
11454           ODRDiagNote(SecondEnumConstant->getLocation(),
11455                       SecondEnumConstant->getSourceRange(),
11456                       EnumConstantDifferentInitilizer)
11457               << I + 1 << SecondEnumConstant;
11458           Diagnosed = true;
11459           break;
11460         }
11461       }
11462     }
11463 
11464     (void)Diagnosed;
11465     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11466   }
11467 }
11468 
11469 void ASTReader::StartedDeserializing() {
11470   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11471     ReadTimer->startTimer();
11472 }
11473 
11474 void ASTReader::FinishedDeserializing() {
11475   assert(NumCurrentElementsDeserializing &&
11476          "FinishedDeserializing not paired with StartedDeserializing");
11477   if (NumCurrentElementsDeserializing == 1) {
11478     // We decrease NumCurrentElementsDeserializing only after pending actions
11479     // are finished, to avoid recursively re-calling finishPendingActions().
11480     finishPendingActions();
11481   }
11482   --NumCurrentElementsDeserializing;
11483 
11484   if (NumCurrentElementsDeserializing == 0) {
11485     // Propagate exception specification and deduced type updates along
11486     // redeclaration chains.
11487     //
11488     // We do this now rather than in finishPendingActions because we want to
11489     // be able to walk the complete redeclaration chains of the updated decls.
11490     while (!PendingExceptionSpecUpdates.empty() ||
11491            !PendingDeducedTypeUpdates.empty()) {
11492       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11493       PendingExceptionSpecUpdates.clear();
11494       for (auto Update : ESUpdates) {
11495         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11496         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11497         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11498         if (auto *Listener = getContext().getASTMutationListener())
11499           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11500         for (auto *Redecl : Update.second->redecls())
11501           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11502       }
11503 
11504       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11505       PendingDeducedTypeUpdates.clear();
11506       for (auto Update : DTUpdates) {
11507         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11508         // FIXME: If the return type is already deduced, check that it matches.
11509         getContext().adjustDeducedFunctionResultType(Update.first,
11510                                                      Update.second);
11511       }
11512     }
11513 
11514     if (ReadTimer)
11515       ReadTimer->stopTimer();
11516 
11517     diagnoseOdrViolations();
11518 
11519     // We are not in recursive loading, so it's safe to pass the "interesting"
11520     // decls to the consumer.
11521     if (Consumer)
11522       PassInterestingDeclsToConsumer();
11523   }
11524 }
11525 
11526 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11527   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11528     // Remove any fake results before adding any real ones.
11529     auto It = PendingFakeLookupResults.find(II);
11530     if (It != PendingFakeLookupResults.end()) {
11531       for (auto *ND : It->second)
11532         SemaObj->IdResolver.RemoveDecl(ND);
11533       // FIXME: this works around module+PCH performance issue.
11534       // Rather than erase the result from the map, which is O(n), just clear
11535       // the vector of NamedDecls.
11536       It->second.clear();
11537     }
11538   }
11539 
11540   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11541     SemaObj->TUScope->AddDecl(D);
11542   } else if (SemaObj->TUScope) {
11543     // Adding the decl to IdResolver may have failed because it was already in
11544     // (even though it was not added in scope). If it is already in, make sure
11545     // it gets in the scope as well.
11546     if (std::find(SemaObj->IdResolver.begin(Name),
11547                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11548       SemaObj->TUScope->AddDecl(D);
11549   }
11550 }
11551 
11552 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11553                      ASTContext *Context,
11554                      const PCHContainerReader &PCHContainerRdr,
11555                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11556                      StringRef isysroot, bool DisableValidation,
11557                      bool AllowASTWithCompilerErrors,
11558                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11559                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11560                      std::unique_ptr<llvm::Timer> ReadTimer)
11561     : Listener(DisableValidation
11562                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11563                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11564       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11565       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11566       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11567                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11568       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11569       DisableValidation(DisableValidation),
11570       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11571       AllowConfigurationMismatch(AllowConfigurationMismatch),
11572       ValidateSystemInputs(ValidateSystemInputs),
11573       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11574       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11575   SourceMgr.setExternalSLocEntrySource(this);
11576 
11577   for (const auto &Ext : Extensions) {
11578     auto BlockName = Ext->getExtensionMetadata().BlockName;
11579     auto Known = ModuleFileExtensions.find(BlockName);
11580     if (Known != ModuleFileExtensions.end()) {
11581       Diags.Report(diag::warn_duplicate_module_file_extension)
11582         << BlockName;
11583       continue;
11584     }
11585 
11586     ModuleFileExtensions.insert({BlockName, Ext});
11587   }
11588 }
11589 
11590 ASTReader::~ASTReader() {
11591   if (OwnsDeserializationListener)
11592     delete DeserializationListener;
11593 }
11594 
11595 IdentifierResolver &ASTReader::getIdResolver() {
11596   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11597 }
11598 
11599 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11600                                                unsigned AbbrevID) {
11601   Idx = 0;
11602   Record.clear();
11603   return Cursor.readRecord(AbbrevID, Record);
11604 }
11605 //===----------------------------------------------------------------------===//
11606 //// OMPClauseReader implementation
11607 ////===----------------------------------------------------------------------===//
11608 
11609 // This has to be in namespace clang because it's friended by all
11610 // of the OMP clauses.
11611 namespace clang {
11612 
11613 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11614   ASTRecordReader &Record;
11615   ASTContext &Context;
11616 
11617 public:
11618   OMPClauseReader(ASTRecordReader &Record)
11619       : Record(Record), Context(Record.getContext()) {}
11620 
11621 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11622 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11623   OMPClause *readClause();
11624   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11625   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11626 };
11627 
11628 } // end namespace clang
11629 
11630 OMPClause *ASTRecordReader::readOMPClause() {
11631   return OMPClauseReader(*this).readClause();
11632 }
11633 
11634 OMPClause *OMPClauseReader::readClause() {
11635   OMPClause *C = nullptr;
11636   switch (llvm::omp::Clause(Record.readInt())) {
11637   case llvm::omp::OMPC_if:
11638     C = new (Context) OMPIfClause();
11639     break;
11640   case llvm::omp::OMPC_final:
11641     C = new (Context) OMPFinalClause();
11642     break;
11643   case llvm::omp::OMPC_num_threads:
11644     C = new (Context) OMPNumThreadsClause();
11645     break;
11646   case llvm::omp::OMPC_safelen:
11647     C = new (Context) OMPSafelenClause();
11648     break;
11649   case llvm::omp::OMPC_simdlen:
11650     C = new (Context) OMPSimdlenClause();
11651     break;
11652   case llvm::omp::OMPC_allocator:
11653     C = new (Context) OMPAllocatorClause();
11654     break;
11655   case llvm::omp::OMPC_collapse:
11656     C = new (Context) OMPCollapseClause();
11657     break;
11658   case llvm::omp::OMPC_default:
11659     C = new (Context) OMPDefaultClause();
11660     break;
11661   case llvm::omp::OMPC_proc_bind:
11662     C = new (Context) OMPProcBindClause();
11663     break;
11664   case llvm::omp::OMPC_schedule:
11665     C = new (Context) OMPScheduleClause();
11666     break;
11667   case llvm::omp::OMPC_ordered:
11668     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11669     break;
11670   case llvm::omp::OMPC_nowait:
11671     C = new (Context) OMPNowaitClause();
11672     break;
11673   case llvm::omp::OMPC_untied:
11674     C = new (Context) OMPUntiedClause();
11675     break;
11676   case llvm::omp::OMPC_mergeable:
11677     C = new (Context) OMPMergeableClause();
11678     break;
11679   case llvm::omp::OMPC_read:
11680     C = new (Context) OMPReadClause();
11681     break;
11682   case llvm::omp::OMPC_write:
11683     C = new (Context) OMPWriteClause();
11684     break;
11685   case llvm::omp::OMPC_update:
11686     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11687     break;
11688   case llvm::omp::OMPC_capture:
11689     C = new (Context) OMPCaptureClause();
11690     break;
11691   case llvm::omp::OMPC_seq_cst:
11692     C = new (Context) OMPSeqCstClause();
11693     break;
11694   case llvm::omp::OMPC_acq_rel:
11695     C = new (Context) OMPAcqRelClause();
11696     break;
11697   case llvm::omp::OMPC_acquire:
11698     C = new (Context) OMPAcquireClause();
11699     break;
11700   case llvm::omp::OMPC_release:
11701     C = new (Context) OMPReleaseClause();
11702     break;
11703   case llvm::omp::OMPC_relaxed:
11704     C = new (Context) OMPRelaxedClause();
11705     break;
11706   case llvm::omp::OMPC_threads:
11707     C = new (Context) OMPThreadsClause();
11708     break;
11709   case llvm::omp::OMPC_simd:
11710     C = new (Context) OMPSIMDClause();
11711     break;
11712   case llvm::omp::OMPC_nogroup:
11713     C = new (Context) OMPNogroupClause();
11714     break;
11715   case llvm::omp::OMPC_unified_address:
11716     C = new (Context) OMPUnifiedAddressClause();
11717     break;
11718   case llvm::omp::OMPC_unified_shared_memory:
11719     C = new (Context) OMPUnifiedSharedMemoryClause();
11720     break;
11721   case llvm::omp::OMPC_reverse_offload:
11722     C = new (Context) OMPReverseOffloadClause();
11723     break;
11724   case llvm::omp::OMPC_dynamic_allocators:
11725     C = new (Context) OMPDynamicAllocatorsClause();
11726     break;
11727   case llvm::omp::OMPC_atomic_default_mem_order:
11728     C = new (Context) OMPAtomicDefaultMemOrderClause();
11729     break;
11730  case llvm::omp::OMPC_private:
11731     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11732     break;
11733   case llvm::omp::OMPC_firstprivate:
11734     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11735     break;
11736   case llvm::omp::OMPC_lastprivate:
11737     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11738     break;
11739   case llvm::omp::OMPC_shared:
11740     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11741     break;
11742   case llvm::omp::OMPC_reduction:
11743     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11744     break;
11745   case llvm::omp::OMPC_task_reduction:
11746     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11747     break;
11748   case llvm::omp::OMPC_in_reduction:
11749     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11750     break;
11751   case llvm::omp::OMPC_linear:
11752     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11753     break;
11754   case llvm::omp::OMPC_aligned:
11755     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11756     break;
11757   case llvm::omp::OMPC_copyin:
11758     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11759     break;
11760   case llvm::omp::OMPC_copyprivate:
11761     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11762     break;
11763   case llvm::omp::OMPC_flush:
11764     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11765     break;
11766   case llvm::omp::OMPC_depobj:
11767     C = OMPDepobjClause::CreateEmpty(Context);
11768     break;
11769   case llvm::omp::OMPC_depend: {
11770     unsigned NumVars = Record.readInt();
11771     unsigned NumLoops = Record.readInt();
11772     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11773     break;
11774   }
11775   case llvm::omp::OMPC_device:
11776     C = new (Context) OMPDeviceClause();
11777     break;
11778   case llvm::omp::OMPC_map: {
11779     OMPMappableExprListSizeTy Sizes;
11780     Sizes.NumVars = Record.readInt();
11781     Sizes.NumUniqueDeclarations = Record.readInt();
11782     Sizes.NumComponentLists = Record.readInt();
11783     Sizes.NumComponents = Record.readInt();
11784     C = OMPMapClause::CreateEmpty(Context, Sizes);
11785     break;
11786   }
11787   case llvm::omp::OMPC_num_teams:
11788     C = new (Context) OMPNumTeamsClause();
11789     break;
11790   case llvm::omp::OMPC_thread_limit:
11791     C = new (Context) OMPThreadLimitClause();
11792     break;
11793   case llvm::omp::OMPC_priority:
11794     C = new (Context) OMPPriorityClause();
11795     break;
11796   case llvm::omp::OMPC_grainsize:
11797     C = new (Context) OMPGrainsizeClause();
11798     break;
11799   case llvm::omp::OMPC_num_tasks:
11800     C = new (Context) OMPNumTasksClause();
11801     break;
11802   case llvm::omp::OMPC_hint:
11803     C = new (Context) OMPHintClause();
11804     break;
11805   case llvm::omp::OMPC_dist_schedule:
11806     C = new (Context) OMPDistScheduleClause();
11807     break;
11808   case llvm::omp::OMPC_defaultmap:
11809     C = new (Context) OMPDefaultmapClause();
11810     break;
11811   case llvm::omp::OMPC_to: {
11812     OMPMappableExprListSizeTy Sizes;
11813     Sizes.NumVars = Record.readInt();
11814     Sizes.NumUniqueDeclarations = Record.readInt();
11815     Sizes.NumComponentLists = Record.readInt();
11816     Sizes.NumComponents = Record.readInt();
11817     C = OMPToClause::CreateEmpty(Context, Sizes);
11818     break;
11819   }
11820   case llvm::omp::OMPC_from: {
11821     OMPMappableExprListSizeTy Sizes;
11822     Sizes.NumVars = Record.readInt();
11823     Sizes.NumUniqueDeclarations = Record.readInt();
11824     Sizes.NumComponentLists = Record.readInt();
11825     Sizes.NumComponents = Record.readInt();
11826     C = OMPFromClause::CreateEmpty(Context, Sizes);
11827     break;
11828   }
11829   case llvm::omp::OMPC_use_device_ptr: {
11830     OMPMappableExprListSizeTy Sizes;
11831     Sizes.NumVars = Record.readInt();
11832     Sizes.NumUniqueDeclarations = Record.readInt();
11833     Sizes.NumComponentLists = Record.readInt();
11834     Sizes.NumComponents = Record.readInt();
11835     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11836     break;
11837   }
11838   case llvm::omp::OMPC_is_device_ptr: {
11839     OMPMappableExprListSizeTy Sizes;
11840     Sizes.NumVars = Record.readInt();
11841     Sizes.NumUniqueDeclarations = Record.readInt();
11842     Sizes.NumComponentLists = Record.readInt();
11843     Sizes.NumComponents = Record.readInt();
11844     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11845     break;
11846   }
11847   case llvm::omp::OMPC_allocate:
11848     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11849     break;
11850   case llvm::omp::OMPC_nontemporal:
11851     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11852     break;
11853   case llvm::omp::OMPC_inclusive:
11854     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11855     break;
11856   case llvm::omp::OMPC_exclusive:
11857     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11858     break;
11859   case llvm::omp::OMPC_order:
11860     C = new (Context) OMPOrderClause();
11861     break;
11862   case llvm::omp::OMPC_destroy:
11863     C = new (Context) OMPDestroyClause();
11864     break;
11865   case llvm::omp::OMPC_detach:
11866     C = new (Context) OMPDetachClause();
11867     break;
11868 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11869   case llvm::omp::Enum:                                                        \
11870     break;
11871 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11872   }
11873   assert(C && "Unknown OMPClause type");
11874 
11875   Visit(C);
11876   C->setLocStart(Record.readSourceLocation());
11877   C->setLocEnd(Record.readSourceLocation());
11878 
11879   return C;
11880 }
11881 
11882 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11883   C->setPreInitStmt(Record.readSubStmt(),
11884                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11885 }
11886 
11887 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11888   VisitOMPClauseWithPreInit(C);
11889   C->setPostUpdateExpr(Record.readSubExpr());
11890 }
11891 
11892 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11893   VisitOMPClauseWithPreInit(C);
11894   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11895   C->setNameModifierLoc(Record.readSourceLocation());
11896   C->setColonLoc(Record.readSourceLocation());
11897   C->setCondition(Record.readSubExpr());
11898   C->setLParenLoc(Record.readSourceLocation());
11899 }
11900 
11901 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11902   VisitOMPClauseWithPreInit(C);
11903   C->setCondition(Record.readSubExpr());
11904   C->setLParenLoc(Record.readSourceLocation());
11905 }
11906 
11907 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11908   VisitOMPClauseWithPreInit(C);
11909   C->setNumThreads(Record.readSubExpr());
11910   C->setLParenLoc(Record.readSourceLocation());
11911 }
11912 
11913 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11914   C->setSafelen(Record.readSubExpr());
11915   C->setLParenLoc(Record.readSourceLocation());
11916 }
11917 
11918 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11919   C->setSimdlen(Record.readSubExpr());
11920   C->setLParenLoc(Record.readSourceLocation());
11921 }
11922 
11923 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11924   C->setAllocator(Record.readExpr());
11925   C->setLParenLoc(Record.readSourceLocation());
11926 }
11927 
11928 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11929   C->setNumForLoops(Record.readSubExpr());
11930   C->setLParenLoc(Record.readSourceLocation());
11931 }
11932 
11933 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11934   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11935   C->setLParenLoc(Record.readSourceLocation());
11936   C->setDefaultKindKwLoc(Record.readSourceLocation());
11937 }
11938 
11939 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11940   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11941   C->setLParenLoc(Record.readSourceLocation());
11942   C->setProcBindKindKwLoc(Record.readSourceLocation());
11943 }
11944 
11945 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11946   VisitOMPClauseWithPreInit(C);
11947   C->setScheduleKind(
11948        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11949   C->setFirstScheduleModifier(
11950       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11951   C->setSecondScheduleModifier(
11952       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11953   C->setChunkSize(Record.readSubExpr());
11954   C->setLParenLoc(Record.readSourceLocation());
11955   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11956   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11957   C->setScheduleKindLoc(Record.readSourceLocation());
11958   C->setCommaLoc(Record.readSourceLocation());
11959 }
11960 
11961 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11962   C->setNumForLoops(Record.readSubExpr());
11963   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11964     C->setLoopNumIterations(I, Record.readSubExpr());
11965   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11966     C->setLoopCounter(I, Record.readSubExpr());
11967   C->setLParenLoc(Record.readSourceLocation());
11968 }
11969 
11970 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11971   C->setEventHandler(Record.readSubExpr());
11972   C->setLParenLoc(Record.readSourceLocation());
11973 }
11974 
11975 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11976 
11977 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11978 
11979 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11980 
11981 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11982 
11983 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11984 
11985 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11986   if (C->isExtended()) {
11987     C->setLParenLoc(Record.readSourceLocation());
11988     C->setArgumentLoc(Record.readSourceLocation());
11989     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11990   }
11991 }
11992 
11993 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11994 
11995 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11996 
11997 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11998 
11999 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12000 
12001 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12002 
12003 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12004 
12005 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12006 
12007 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12008 
12009 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12010 
12011 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12012 
12013 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12014 
12015 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12016     OMPUnifiedSharedMemoryClause *) {}
12017 
12018 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12019 
12020 void
12021 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12022 }
12023 
12024 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12025     OMPAtomicDefaultMemOrderClause *C) {
12026   C->setAtomicDefaultMemOrderKind(
12027       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12028   C->setLParenLoc(Record.readSourceLocation());
12029   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12033   C->setLParenLoc(Record.readSourceLocation());
12034   unsigned NumVars = C->varlist_size();
12035   SmallVector<Expr *, 16> Vars;
12036   Vars.reserve(NumVars);
12037   for (unsigned i = 0; i != NumVars; ++i)
12038     Vars.push_back(Record.readSubExpr());
12039   C->setVarRefs(Vars);
12040   Vars.clear();
12041   for (unsigned i = 0; i != NumVars; ++i)
12042     Vars.push_back(Record.readSubExpr());
12043   C->setPrivateCopies(Vars);
12044 }
12045 
12046 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12047   VisitOMPClauseWithPreInit(C);
12048   C->setLParenLoc(Record.readSourceLocation());
12049   unsigned NumVars = C->varlist_size();
12050   SmallVector<Expr *, 16> Vars;
12051   Vars.reserve(NumVars);
12052   for (unsigned i = 0; i != NumVars; ++i)
12053     Vars.push_back(Record.readSubExpr());
12054   C->setVarRefs(Vars);
12055   Vars.clear();
12056   for (unsigned i = 0; i != NumVars; ++i)
12057     Vars.push_back(Record.readSubExpr());
12058   C->setPrivateCopies(Vars);
12059   Vars.clear();
12060   for (unsigned i = 0; i != NumVars; ++i)
12061     Vars.push_back(Record.readSubExpr());
12062   C->setInits(Vars);
12063 }
12064 
12065 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12066   VisitOMPClauseWithPostUpdate(C);
12067   C->setLParenLoc(Record.readSourceLocation());
12068   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12069   C->setKindLoc(Record.readSourceLocation());
12070   C->setColonLoc(Record.readSourceLocation());
12071   unsigned NumVars = C->varlist_size();
12072   SmallVector<Expr *, 16> Vars;
12073   Vars.reserve(NumVars);
12074   for (unsigned i = 0; i != NumVars; ++i)
12075     Vars.push_back(Record.readSubExpr());
12076   C->setVarRefs(Vars);
12077   Vars.clear();
12078   for (unsigned i = 0; i != NumVars; ++i)
12079     Vars.push_back(Record.readSubExpr());
12080   C->setPrivateCopies(Vars);
12081   Vars.clear();
12082   for (unsigned i = 0; i != NumVars; ++i)
12083     Vars.push_back(Record.readSubExpr());
12084   C->setSourceExprs(Vars);
12085   Vars.clear();
12086   for (unsigned i = 0; i != NumVars; ++i)
12087     Vars.push_back(Record.readSubExpr());
12088   C->setDestinationExprs(Vars);
12089   Vars.clear();
12090   for (unsigned i = 0; i != NumVars; ++i)
12091     Vars.push_back(Record.readSubExpr());
12092   C->setAssignmentOps(Vars);
12093 }
12094 
12095 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12096   C->setLParenLoc(Record.readSourceLocation());
12097   unsigned NumVars = C->varlist_size();
12098   SmallVector<Expr *, 16> Vars;
12099   Vars.reserve(NumVars);
12100   for (unsigned i = 0; i != NumVars; ++i)
12101     Vars.push_back(Record.readSubExpr());
12102   C->setVarRefs(Vars);
12103 }
12104 
12105 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12106   VisitOMPClauseWithPostUpdate(C);
12107   C->setLParenLoc(Record.readSourceLocation());
12108   C->setModifierLoc(Record.readSourceLocation());
12109   C->setColonLoc(Record.readSourceLocation());
12110   C->setModifier(Record.readEnum<OpenMPReductionClauseModifier>());
12111   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12112   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12113   C->setQualifierLoc(NNSL);
12114   C->setNameInfo(DNI);
12115 
12116   unsigned NumVars = C->varlist_size();
12117   SmallVector<Expr *, 16> Vars;
12118   Vars.reserve(NumVars);
12119   for (unsigned i = 0; i != NumVars; ++i)
12120     Vars.push_back(Record.readSubExpr());
12121   C->setVarRefs(Vars);
12122   Vars.clear();
12123   for (unsigned i = 0; i != NumVars; ++i)
12124     Vars.push_back(Record.readSubExpr());
12125   C->setPrivates(Vars);
12126   Vars.clear();
12127   for (unsigned i = 0; i != NumVars; ++i)
12128     Vars.push_back(Record.readSubExpr());
12129   C->setLHSExprs(Vars);
12130   Vars.clear();
12131   for (unsigned i = 0; i != NumVars; ++i)
12132     Vars.push_back(Record.readSubExpr());
12133   C->setRHSExprs(Vars);
12134   Vars.clear();
12135   for (unsigned i = 0; i != NumVars; ++i)
12136     Vars.push_back(Record.readSubExpr());
12137   C->setReductionOps(Vars);
12138 }
12139 
12140 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12141   VisitOMPClauseWithPostUpdate(C);
12142   C->setLParenLoc(Record.readSourceLocation());
12143   C->setColonLoc(Record.readSourceLocation());
12144   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12145   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12146   C->setQualifierLoc(NNSL);
12147   C->setNameInfo(DNI);
12148 
12149   unsigned NumVars = C->varlist_size();
12150   SmallVector<Expr *, 16> Vars;
12151   Vars.reserve(NumVars);
12152   for (unsigned I = 0; I != NumVars; ++I)
12153     Vars.push_back(Record.readSubExpr());
12154   C->setVarRefs(Vars);
12155   Vars.clear();
12156   for (unsigned I = 0; I != NumVars; ++I)
12157     Vars.push_back(Record.readSubExpr());
12158   C->setPrivates(Vars);
12159   Vars.clear();
12160   for (unsigned I = 0; I != NumVars; ++I)
12161     Vars.push_back(Record.readSubExpr());
12162   C->setLHSExprs(Vars);
12163   Vars.clear();
12164   for (unsigned I = 0; I != NumVars; ++I)
12165     Vars.push_back(Record.readSubExpr());
12166   C->setRHSExprs(Vars);
12167   Vars.clear();
12168   for (unsigned I = 0; I != NumVars; ++I)
12169     Vars.push_back(Record.readSubExpr());
12170   C->setReductionOps(Vars);
12171 }
12172 
12173 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12174   VisitOMPClauseWithPostUpdate(C);
12175   C->setLParenLoc(Record.readSourceLocation());
12176   C->setColonLoc(Record.readSourceLocation());
12177   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12178   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12179   C->setQualifierLoc(NNSL);
12180   C->setNameInfo(DNI);
12181 
12182   unsigned NumVars = C->varlist_size();
12183   SmallVector<Expr *, 16> Vars;
12184   Vars.reserve(NumVars);
12185   for (unsigned I = 0; I != NumVars; ++I)
12186     Vars.push_back(Record.readSubExpr());
12187   C->setVarRefs(Vars);
12188   Vars.clear();
12189   for (unsigned I = 0; I != NumVars; ++I)
12190     Vars.push_back(Record.readSubExpr());
12191   C->setPrivates(Vars);
12192   Vars.clear();
12193   for (unsigned I = 0; I != NumVars; ++I)
12194     Vars.push_back(Record.readSubExpr());
12195   C->setLHSExprs(Vars);
12196   Vars.clear();
12197   for (unsigned I = 0; I != NumVars; ++I)
12198     Vars.push_back(Record.readSubExpr());
12199   C->setRHSExprs(Vars);
12200   Vars.clear();
12201   for (unsigned I = 0; I != NumVars; ++I)
12202     Vars.push_back(Record.readSubExpr());
12203   C->setReductionOps(Vars);
12204   Vars.clear();
12205   for (unsigned I = 0; I != NumVars; ++I)
12206     Vars.push_back(Record.readSubExpr());
12207   C->setTaskgroupDescriptors(Vars);
12208 }
12209 
12210 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12211   VisitOMPClauseWithPostUpdate(C);
12212   C->setLParenLoc(Record.readSourceLocation());
12213   C->setColonLoc(Record.readSourceLocation());
12214   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12215   C->setModifierLoc(Record.readSourceLocation());
12216   unsigned NumVars = C->varlist_size();
12217   SmallVector<Expr *, 16> Vars;
12218   Vars.reserve(NumVars);
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Vars.push_back(Record.readSubExpr());
12221   C->setVarRefs(Vars);
12222   Vars.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Vars.push_back(Record.readSubExpr());
12225   C->setPrivates(Vars);
12226   Vars.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Vars.push_back(Record.readSubExpr());
12229   C->setInits(Vars);
12230   Vars.clear();
12231   for (unsigned i = 0; i != NumVars; ++i)
12232     Vars.push_back(Record.readSubExpr());
12233   C->setUpdates(Vars);
12234   Vars.clear();
12235   for (unsigned i = 0; i != NumVars; ++i)
12236     Vars.push_back(Record.readSubExpr());
12237   C->setFinals(Vars);
12238   C->setStep(Record.readSubExpr());
12239   C->setCalcStep(Record.readSubExpr());
12240   Vars.clear();
12241   for (unsigned I = 0; I != NumVars + 1; ++I)
12242     Vars.push_back(Record.readSubExpr());
12243   C->setUsedExprs(Vars);
12244 }
12245 
12246 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12247   C->setLParenLoc(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   C->setAlignment(Record.readSubExpr());
12256 }
12257 
12258 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12259   C->setLParenLoc(Record.readSourceLocation());
12260   unsigned NumVars = C->varlist_size();
12261   SmallVector<Expr *, 16> Exprs;
12262   Exprs.reserve(NumVars);
12263   for (unsigned i = 0; i != NumVars; ++i)
12264     Exprs.push_back(Record.readSubExpr());
12265   C->setVarRefs(Exprs);
12266   Exprs.clear();
12267   for (unsigned i = 0; i != NumVars; ++i)
12268     Exprs.push_back(Record.readSubExpr());
12269   C->setSourceExprs(Exprs);
12270   Exprs.clear();
12271   for (unsigned i = 0; i != NumVars; ++i)
12272     Exprs.push_back(Record.readSubExpr());
12273   C->setDestinationExprs(Exprs);
12274   Exprs.clear();
12275   for (unsigned i = 0; i != NumVars; ++i)
12276     Exprs.push_back(Record.readSubExpr());
12277   C->setAssignmentOps(Exprs);
12278 }
12279 
12280 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12281   C->setLParenLoc(Record.readSourceLocation());
12282   unsigned NumVars = C->varlist_size();
12283   SmallVector<Expr *, 16> Exprs;
12284   Exprs.reserve(NumVars);
12285   for (unsigned i = 0; i != NumVars; ++i)
12286     Exprs.push_back(Record.readSubExpr());
12287   C->setVarRefs(Exprs);
12288   Exprs.clear();
12289   for (unsigned i = 0; i != NumVars; ++i)
12290     Exprs.push_back(Record.readSubExpr());
12291   C->setSourceExprs(Exprs);
12292   Exprs.clear();
12293   for (unsigned i = 0; i != NumVars; ++i)
12294     Exprs.push_back(Record.readSubExpr());
12295   C->setDestinationExprs(Exprs);
12296   Exprs.clear();
12297   for (unsigned i = 0; i != NumVars; ++i)
12298     Exprs.push_back(Record.readSubExpr());
12299   C->setAssignmentOps(Exprs);
12300 }
12301 
12302 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12303   C->setLParenLoc(Record.readSourceLocation());
12304   unsigned NumVars = C->varlist_size();
12305   SmallVector<Expr *, 16> Vars;
12306   Vars.reserve(NumVars);
12307   for (unsigned i = 0; i != NumVars; ++i)
12308     Vars.push_back(Record.readSubExpr());
12309   C->setVarRefs(Vars);
12310 }
12311 
12312 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12313   C->setDepobj(Record.readSubExpr());
12314   C->setLParenLoc(Record.readSourceLocation());
12315 }
12316 
12317 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12318   C->setLParenLoc(Record.readSourceLocation());
12319   C->setModifier(Record.readSubExpr());
12320   C->setDependencyKind(
12321       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12322   C->setDependencyLoc(Record.readSourceLocation());
12323   C->setColonLoc(Record.readSourceLocation());
12324   unsigned NumVars = C->varlist_size();
12325   SmallVector<Expr *, 16> Vars;
12326   Vars.reserve(NumVars);
12327   for (unsigned I = 0; I != NumVars; ++I)
12328     Vars.push_back(Record.readSubExpr());
12329   C->setVarRefs(Vars);
12330   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12331     C->setLoopData(I, Record.readSubExpr());
12332 }
12333 
12334 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12335   VisitOMPClauseWithPreInit(C);
12336   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12337   C->setDevice(Record.readSubExpr());
12338   C->setModifierLoc(Record.readSourceLocation());
12339   C->setLParenLoc(Record.readSourceLocation());
12340 }
12341 
12342 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12343   C->setLParenLoc(Record.readSourceLocation());
12344   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12345     C->setMapTypeModifier(
12346         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12347     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12348   }
12349   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12350   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12351   C->setMapType(
12352      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12353   C->setMapLoc(Record.readSourceLocation());
12354   C->setColonLoc(Record.readSourceLocation());
12355   auto NumVars = C->varlist_size();
12356   auto UniqueDecls = C->getUniqueDeclarationsNum();
12357   auto TotalLists = C->getTotalComponentListNum();
12358   auto TotalComponents = C->getTotalComponentsNum();
12359 
12360   SmallVector<Expr *, 16> Vars;
12361   Vars.reserve(NumVars);
12362   for (unsigned i = 0; i != NumVars; ++i)
12363     Vars.push_back(Record.readExpr());
12364   C->setVarRefs(Vars);
12365 
12366   SmallVector<Expr *, 16> UDMappers;
12367   UDMappers.reserve(NumVars);
12368   for (unsigned I = 0; I < NumVars; ++I)
12369     UDMappers.push_back(Record.readExpr());
12370   C->setUDMapperRefs(UDMappers);
12371 
12372   SmallVector<ValueDecl *, 16> Decls;
12373   Decls.reserve(UniqueDecls);
12374   for (unsigned i = 0; i < UniqueDecls; ++i)
12375     Decls.push_back(Record.readDeclAs<ValueDecl>());
12376   C->setUniqueDecls(Decls);
12377 
12378   SmallVector<unsigned, 16> ListsPerDecl;
12379   ListsPerDecl.reserve(UniqueDecls);
12380   for (unsigned i = 0; i < UniqueDecls; ++i)
12381     ListsPerDecl.push_back(Record.readInt());
12382   C->setDeclNumLists(ListsPerDecl);
12383 
12384   SmallVector<unsigned, 32> ListSizes;
12385   ListSizes.reserve(TotalLists);
12386   for (unsigned i = 0; i < TotalLists; ++i)
12387     ListSizes.push_back(Record.readInt());
12388   C->setComponentListSizes(ListSizes);
12389 
12390   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12391   Components.reserve(TotalComponents);
12392   for (unsigned i = 0; i < TotalComponents; ++i) {
12393     Expr *AssociatedExpr = Record.readExpr();
12394     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12395     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12396         AssociatedExpr, AssociatedDecl));
12397   }
12398   C->setComponents(Components, ListSizes);
12399 }
12400 
12401 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12402   C->setLParenLoc(Record.readSourceLocation());
12403   C->setColonLoc(Record.readSourceLocation());
12404   C->setAllocator(Record.readSubExpr());
12405   unsigned NumVars = C->varlist_size();
12406   SmallVector<Expr *, 16> Vars;
12407   Vars.reserve(NumVars);
12408   for (unsigned i = 0; i != NumVars; ++i)
12409     Vars.push_back(Record.readSubExpr());
12410   C->setVarRefs(Vars);
12411 }
12412 
12413 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12414   VisitOMPClauseWithPreInit(C);
12415   C->setNumTeams(Record.readSubExpr());
12416   C->setLParenLoc(Record.readSourceLocation());
12417 }
12418 
12419 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12420   VisitOMPClauseWithPreInit(C);
12421   C->setThreadLimit(Record.readSubExpr());
12422   C->setLParenLoc(Record.readSourceLocation());
12423 }
12424 
12425 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12426   VisitOMPClauseWithPreInit(C);
12427   C->setPriority(Record.readSubExpr());
12428   C->setLParenLoc(Record.readSourceLocation());
12429 }
12430 
12431 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12432   VisitOMPClauseWithPreInit(C);
12433   C->setGrainsize(Record.readSubExpr());
12434   C->setLParenLoc(Record.readSourceLocation());
12435 }
12436 
12437 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12438   VisitOMPClauseWithPreInit(C);
12439   C->setNumTasks(Record.readSubExpr());
12440   C->setLParenLoc(Record.readSourceLocation());
12441 }
12442 
12443 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12444   C->setHint(Record.readSubExpr());
12445   C->setLParenLoc(Record.readSourceLocation());
12446 }
12447 
12448 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12449   VisitOMPClauseWithPreInit(C);
12450   C->setDistScheduleKind(
12451       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12452   C->setChunkSize(Record.readSubExpr());
12453   C->setLParenLoc(Record.readSourceLocation());
12454   C->setDistScheduleKindLoc(Record.readSourceLocation());
12455   C->setCommaLoc(Record.readSourceLocation());
12456 }
12457 
12458 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12459   C->setDefaultmapKind(
12460        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12461   C->setDefaultmapModifier(
12462       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12463   C->setLParenLoc(Record.readSourceLocation());
12464   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12465   C->setDefaultmapKindLoc(Record.readSourceLocation());
12466 }
12467 
12468 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12469   C->setLParenLoc(Record.readSourceLocation());
12470   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12471   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12472   auto NumVars = C->varlist_size();
12473   auto UniqueDecls = C->getUniqueDeclarationsNum();
12474   auto TotalLists = C->getTotalComponentListNum();
12475   auto TotalComponents = C->getTotalComponentsNum();
12476 
12477   SmallVector<Expr *, 16> Vars;
12478   Vars.reserve(NumVars);
12479   for (unsigned i = 0; i != NumVars; ++i)
12480     Vars.push_back(Record.readSubExpr());
12481   C->setVarRefs(Vars);
12482 
12483   SmallVector<Expr *, 16> UDMappers;
12484   UDMappers.reserve(NumVars);
12485   for (unsigned I = 0; I < NumVars; ++I)
12486     UDMappers.push_back(Record.readSubExpr());
12487   C->setUDMapperRefs(UDMappers);
12488 
12489   SmallVector<ValueDecl *, 16> Decls;
12490   Decls.reserve(UniqueDecls);
12491   for (unsigned i = 0; i < UniqueDecls; ++i)
12492     Decls.push_back(Record.readDeclAs<ValueDecl>());
12493   C->setUniqueDecls(Decls);
12494 
12495   SmallVector<unsigned, 16> ListsPerDecl;
12496   ListsPerDecl.reserve(UniqueDecls);
12497   for (unsigned i = 0; i < UniqueDecls; ++i)
12498     ListsPerDecl.push_back(Record.readInt());
12499   C->setDeclNumLists(ListsPerDecl);
12500 
12501   SmallVector<unsigned, 32> ListSizes;
12502   ListSizes.reserve(TotalLists);
12503   for (unsigned i = 0; i < TotalLists; ++i)
12504     ListSizes.push_back(Record.readInt());
12505   C->setComponentListSizes(ListSizes);
12506 
12507   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12508   Components.reserve(TotalComponents);
12509   for (unsigned i = 0; i < TotalComponents; ++i) {
12510     Expr *AssociatedExpr = Record.readSubExpr();
12511     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12512     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12513         AssociatedExpr, AssociatedDecl));
12514   }
12515   C->setComponents(Components, ListSizes);
12516 }
12517 
12518 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12519   C->setLParenLoc(Record.readSourceLocation());
12520   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12521   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12522   auto NumVars = C->varlist_size();
12523   auto UniqueDecls = C->getUniqueDeclarationsNum();
12524   auto TotalLists = C->getTotalComponentListNum();
12525   auto TotalComponents = C->getTotalComponentsNum();
12526 
12527   SmallVector<Expr *, 16> Vars;
12528   Vars.reserve(NumVars);
12529   for (unsigned i = 0; i != NumVars; ++i)
12530     Vars.push_back(Record.readSubExpr());
12531   C->setVarRefs(Vars);
12532 
12533   SmallVector<Expr *, 16> UDMappers;
12534   UDMappers.reserve(NumVars);
12535   for (unsigned I = 0; I < NumVars; ++I)
12536     UDMappers.push_back(Record.readSubExpr());
12537   C->setUDMapperRefs(UDMappers);
12538 
12539   SmallVector<ValueDecl *, 16> Decls;
12540   Decls.reserve(UniqueDecls);
12541   for (unsigned i = 0; i < UniqueDecls; ++i)
12542     Decls.push_back(Record.readDeclAs<ValueDecl>());
12543   C->setUniqueDecls(Decls);
12544 
12545   SmallVector<unsigned, 16> ListsPerDecl;
12546   ListsPerDecl.reserve(UniqueDecls);
12547   for (unsigned i = 0; i < UniqueDecls; ++i)
12548     ListsPerDecl.push_back(Record.readInt());
12549   C->setDeclNumLists(ListsPerDecl);
12550 
12551   SmallVector<unsigned, 32> ListSizes;
12552   ListSizes.reserve(TotalLists);
12553   for (unsigned i = 0; i < TotalLists; ++i)
12554     ListSizes.push_back(Record.readInt());
12555   C->setComponentListSizes(ListSizes);
12556 
12557   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12558   Components.reserve(TotalComponents);
12559   for (unsigned i = 0; i < TotalComponents; ++i) {
12560     Expr *AssociatedExpr = Record.readSubExpr();
12561     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12562     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12563         AssociatedExpr, AssociatedDecl));
12564   }
12565   C->setComponents(Components, ListSizes);
12566 }
12567 
12568 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12569   C->setLParenLoc(Record.readSourceLocation());
12570   auto NumVars = C->varlist_size();
12571   auto UniqueDecls = C->getUniqueDeclarationsNum();
12572   auto TotalLists = C->getTotalComponentListNum();
12573   auto TotalComponents = C->getTotalComponentsNum();
12574 
12575   SmallVector<Expr *, 16> Vars;
12576   Vars.reserve(NumVars);
12577   for (unsigned i = 0; i != NumVars; ++i)
12578     Vars.push_back(Record.readSubExpr());
12579   C->setVarRefs(Vars);
12580   Vars.clear();
12581   for (unsigned i = 0; i != NumVars; ++i)
12582     Vars.push_back(Record.readSubExpr());
12583   C->setPrivateCopies(Vars);
12584   Vars.clear();
12585   for (unsigned i = 0; i != NumVars; ++i)
12586     Vars.push_back(Record.readSubExpr());
12587   C->setInits(Vars);
12588 
12589   SmallVector<ValueDecl *, 16> Decls;
12590   Decls.reserve(UniqueDecls);
12591   for (unsigned i = 0; i < UniqueDecls; ++i)
12592     Decls.push_back(Record.readDeclAs<ValueDecl>());
12593   C->setUniqueDecls(Decls);
12594 
12595   SmallVector<unsigned, 16> ListsPerDecl;
12596   ListsPerDecl.reserve(UniqueDecls);
12597   for (unsigned i = 0; i < UniqueDecls; ++i)
12598     ListsPerDecl.push_back(Record.readInt());
12599   C->setDeclNumLists(ListsPerDecl);
12600 
12601   SmallVector<unsigned, 32> ListSizes;
12602   ListSizes.reserve(TotalLists);
12603   for (unsigned i = 0; i < TotalLists; ++i)
12604     ListSizes.push_back(Record.readInt());
12605   C->setComponentListSizes(ListSizes);
12606 
12607   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12608   Components.reserve(TotalComponents);
12609   for (unsigned i = 0; i < TotalComponents; ++i) {
12610     Expr *AssociatedExpr = Record.readSubExpr();
12611     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12612     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12613         AssociatedExpr, AssociatedDecl));
12614   }
12615   C->setComponents(Components, ListSizes);
12616 }
12617 
12618 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12619   C->setLParenLoc(Record.readSourceLocation());
12620   auto NumVars = C->varlist_size();
12621   auto UniqueDecls = C->getUniqueDeclarationsNum();
12622   auto TotalLists = C->getTotalComponentListNum();
12623   auto TotalComponents = C->getTotalComponentsNum();
12624 
12625   SmallVector<Expr *, 16> Vars;
12626   Vars.reserve(NumVars);
12627   for (unsigned i = 0; i != NumVars; ++i)
12628     Vars.push_back(Record.readSubExpr());
12629   C->setVarRefs(Vars);
12630   Vars.clear();
12631 
12632   SmallVector<ValueDecl *, 16> Decls;
12633   Decls.reserve(UniqueDecls);
12634   for (unsigned i = 0; i < UniqueDecls; ++i)
12635     Decls.push_back(Record.readDeclAs<ValueDecl>());
12636   C->setUniqueDecls(Decls);
12637 
12638   SmallVector<unsigned, 16> ListsPerDecl;
12639   ListsPerDecl.reserve(UniqueDecls);
12640   for (unsigned i = 0; i < UniqueDecls; ++i)
12641     ListsPerDecl.push_back(Record.readInt());
12642   C->setDeclNumLists(ListsPerDecl);
12643 
12644   SmallVector<unsigned, 32> ListSizes;
12645   ListSizes.reserve(TotalLists);
12646   for (unsigned i = 0; i < TotalLists; ++i)
12647     ListSizes.push_back(Record.readInt());
12648   C->setComponentListSizes(ListSizes);
12649 
12650   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12651   Components.reserve(TotalComponents);
12652   for (unsigned i = 0; i < TotalComponents; ++i) {
12653     Expr *AssociatedExpr = Record.readSubExpr();
12654     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12655     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12656         AssociatedExpr, AssociatedDecl));
12657   }
12658   C->setComponents(Components, ListSizes);
12659 }
12660 
12661 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12662   C->setLParenLoc(Record.readSourceLocation());
12663   unsigned NumVars = C->varlist_size();
12664   SmallVector<Expr *, 16> Vars;
12665   Vars.reserve(NumVars);
12666   for (unsigned i = 0; i != NumVars; ++i)
12667     Vars.push_back(Record.readSubExpr());
12668   C->setVarRefs(Vars);
12669   Vars.clear();
12670   Vars.reserve(NumVars);
12671   for (unsigned i = 0; i != NumVars; ++i)
12672     Vars.push_back(Record.readSubExpr());
12673   C->setPrivateRefs(Vars);
12674 }
12675 
12676 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12677   C->setLParenLoc(Record.readSourceLocation());
12678   unsigned NumVars = C->varlist_size();
12679   SmallVector<Expr *, 16> Vars;
12680   Vars.reserve(NumVars);
12681   for (unsigned i = 0; i != NumVars; ++i)
12682     Vars.push_back(Record.readSubExpr());
12683   C->setVarRefs(Vars);
12684 }
12685 
12686 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12687   C->setLParenLoc(Record.readSourceLocation());
12688   unsigned NumVars = C->varlist_size();
12689   SmallVector<Expr *, 16> Vars;
12690   Vars.reserve(NumVars);
12691   for (unsigned i = 0; i != NumVars; ++i)
12692     Vars.push_back(Record.readSubExpr());
12693   C->setVarRefs(Vars);
12694 }
12695 
12696 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12697   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12698   C->setLParenLoc(Record.readSourceLocation());
12699   C->setKindKwLoc(Record.readSourceLocation());
12700 }
12701 
12702 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12703   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12704   TI.Sets.resize(readUInt32());
12705   for (auto &Set : TI.Sets) {
12706     Set.Kind = readEnum<llvm::omp::TraitSet>();
12707     Set.Selectors.resize(readUInt32());
12708     for (auto &Selector : Set.Selectors) {
12709       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12710       Selector.ScoreOrCondition = nullptr;
12711       if (readBool())
12712         Selector.ScoreOrCondition = readExprRef();
12713       Selector.Properties.resize(readUInt32());
12714       for (auto &Property : Selector.Properties)
12715         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12716     }
12717   }
12718   return &TI;
12719 }
12720