1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/MemoryBuffer.h"
118 #include "llvm/Support/Path.h"
119 #include "llvm/Support/SaveAndRestore.h"
120 #include "llvm/Support/Timer.h"
121 #include "llvm/Support/VersionTuple.h"
122 #include "llvm/Support/raw_ostream.h"
123 #include <algorithm>
124 #include <cassert>
125 #include <cstddef>
126 #include <cstdint>
127 #include <cstdio>
128 #include <ctime>
129 #include <iterator>
130 #include <limits>
131 #include <map>
132 #include <memory>
133 #include <string>
134 #include <system_error>
135 #include <tuple>
136 #include <utility>
137 #include <vector>
138 
139 using namespace clang;
140 using namespace clang::serialization;
141 using namespace clang::serialization::reader;
142 using llvm::BitstreamCursor;
143 using llvm::RoundingMode;
144 
145 //===----------------------------------------------------------------------===//
146 // ChainedASTReaderListener implementation
147 //===----------------------------------------------------------------------===//
148 
149 bool
150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151   return First->ReadFullVersionInformation(FullVersion) ||
152          Second->ReadFullVersionInformation(FullVersion);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156   First->ReadModuleName(ModuleName);
157   Second->ReadModuleName(ModuleName);
158 }
159 
160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161   First->ReadModuleMapFile(ModuleMapPath);
162   Second->ReadModuleMapFile(ModuleMapPath);
163 }
164 
165 bool
166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167                                               bool Complain,
168                                               bool AllowCompatibleDifferences) {
169   return First->ReadLanguageOptions(LangOpts, Complain,
170                                     AllowCompatibleDifferences) ||
171          Second->ReadLanguageOptions(LangOpts, Complain,
172                                      AllowCompatibleDifferences);
173 }
174 
175 bool ChainedASTReaderListener::ReadTargetOptions(
176     const TargetOptions &TargetOpts, bool Complain,
177     bool AllowCompatibleDifferences) {
178   return First->ReadTargetOptions(TargetOpts, Complain,
179                                   AllowCompatibleDifferences) ||
180          Second->ReadTargetOptions(TargetOpts, Complain,
181                                    AllowCompatibleDifferences);
182 }
183 
184 bool ChainedASTReaderListener::ReadDiagnosticOptions(
185     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187          Second->ReadDiagnosticOptions(DiagOpts, Complain);
188 }
189 
190 bool
191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192                                                 bool Complain) {
193   return First->ReadFileSystemOptions(FSOpts, Complain) ||
194          Second->ReadFileSystemOptions(FSOpts, Complain);
195 }
196 
197 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199     bool Complain) {
200   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                         Complain) ||
202          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                          Complain);
204 }
205 
206 bool ChainedASTReaderListener::ReadPreprocessorOptions(
207     const PreprocessorOptions &PPOpts, bool Complain,
208     std::string &SuggestedPredefines) {
209   return First->ReadPreprocessorOptions(PPOpts, Complain,
210                                         SuggestedPredefines) ||
211          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212 }
213 
214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215                                            unsigned Value) {
216   First->ReadCounter(M, Value);
217   Second->ReadCounter(M, Value);
218 }
219 
220 bool ChainedASTReaderListener::needsInputFileVisitation() {
221   return First->needsInputFileVisitation() ||
222          Second->needsInputFileVisitation();
223 }
224 
225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226   return First->needsSystemInputFileVisitation() ||
227   Second->needsSystemInputFileVisitation();
228 }
229 
230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231                                                ModuleKind Kind) {
232   First->visitModuleFile(Filename, Kind);
233   Second->visitModuleFile(Filename, Kind);
234 }
235 
236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237                                               bool isSystem,
238                                               bool isOverridden,
239                                               bool isExplicitModule) {
240   bool Continue = false;
241   if (First->needsInputFileVisitation() &&
242       (!isSystem || First->needsSystemInputFileVisitation()))
243     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244                                       isExplicitModule);
245   if (Second->needsInputFileVisitation() &&
246       (!isSystem || Second->needsSystemInputFileVisitation()))
247     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248                                        isExplicitModule);
249   return Continue;
250 }
251 
252 void ChainedASTReaderListener::readModuleFileExtension(
253        const ModuleFileExtensionMetadata &Metadata) {
254   First->readModuleFileExtension(Metadata);
255   Second->readModuleFileExtension(Metadata);
256 }
257 
258 //===----------------------------------------------------------------------===//
259 // PCH validator implementation
260 //===----------------------------------------------------------------------===//
261 
262 ASTReaderListener::~ASTReaderListener() = default;
263 
264 /// Compare the given set of language options against an existing set of
265 /// language options.
266 ///
267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268 /// \param AllowCompatibleDifferences If true, differences between compatible
269 ///        language options will be permitted.
270 ///
271 /// \returns true if the languagae options mis-match, false otherwise.
272 static bool checkLanguageOptions(const LangOptions &LangOpts,
273                                  const LangOptions &ExistingLangOpts,
274                                  DiagnosticsEngine *Diags,
275                                  bool AllowCompatibleDifferences = true) {
276 #define LANGOPT(Name, Bits, Default, Description)                 \
277   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
278     if (Diags)                                                    \
279       Diags->Report(diag::err_pch_langopt_mismatch)               \
280         << Description << LangOpts.Name << ExistingLangOpts.Name; \
281     return true;                                                  \
282   }
283 
284 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
285   if (ExistingLangOpts.Name != LangOpts.Name) {           \
286     if (Diags)                                            \
287       Diags->Report(diag::err_pch_langopt_value_mismatch) \
288         << Description;                                   \
289     return true;                                          \
290   }
291 
292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
293   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
294     if (Diags)                                                 \
295       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
296         << Description;                                        \
297     return true;                                               \
298   }
299 
300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
301   if (!AllowCompatibleDifferences)                            \
302     LANGOPT(Name, Bits, Default, Description)
303 
304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
305   if (!AllowCompatibleDifferences)                                 \
306     ENUM_LANGOPT(Name, Bits, Default, Description)
307 
308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309   if (!AllowCompatibleDifferences)                                 \
310     VALUE_LANGOPT(Name, Bits, Default, Description)
311 
312 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315 #include "clang/Basic/LangOptions.def"
316 
317   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318     if (Diags)
319       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320     return true;
321   }
322 
323   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324     if (Diags)
325       Diags->Report(diag::err_pch_langopt_value_mismatch)
326       << "target Objective-C runtime";
327     return true;
328   }
329 
330   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331       LangOpts.CommentOpts.BlockCommandNames) {
332     if (Diags)
333       Diags->Report(diag::err_pch_langopt_value_mismatch)
334         << "block command names";
335     return true;
336   }
337 
338   // Sanitizer feature mismatches are treated as compatible differences. If
339   // compatible differences aren't allowed, we still only want to check for
340   // mismatches of non-modular sanitizers (the only ones which can affect AST
341   // generation).
342   if (!AllowCompatibleDifferences) {
343     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346     ExistingSanitizers.clear(ModularSanitizers);
347     ImportedSanitizers.clear(ModularSanitizers);
348     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349       const std::string Flag = "-fsanitize=";
350       if (Diags) {
351 #define SANITIZER(NAME, ID)                                                    \
352   {                                                                            \
353     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
354     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
355     if (InExistingModule != InImportedModule)                                  \
356       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
357           << InExistingModule << (Flag + NAME);                                \
358   }
359 #include "clang/Basic/Sanitizers.def"
360       }
361       return true;
362     }
363   }
364 
365   return false;
366 }
367 
368 /// Compare the given set of target options against an existing set of
369 /// target options.
370 ///
371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372 ///
373 /// \returns true if the target options mis-match, false otherwise.
374 static bool checkTargetOptions(const TargetOptions &TargetOpts,
375                                const TargetOptions &ExistingTargetOpts,
376                                DiagnosticsEngine *Diags,
377                                bool AllowCompatibleDifferences = true) {
378 #define CHECK_TARGET_OPT(Field, Name)                             \
379   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
380     if (Diags)                                                    \
381       Diags->Report(diag::err_pch_targetopt_mismatch)             \
382         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
383     return true;                                                  \
384   }
385 
386   // The triple and ABI must match exactly.
387   CHECK_TARGET_OPT(Triple, "target");
388   CHECK_TARGET_OPT(ABI, "target ABI");
389 
390   // We can tolerate different CPUs in many cases, notably when one CPU
391   // supports a strict superset of another. When allowing compatible
392   // differences skip this check.
393   if (!AllowCompatibleDifferences) {
394     CHECK_TARGET_OPT(CPU, "target CPU");
395     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
396   }
397 
398 #undef CHECK_TARGET_OPT
399 
400   // Compare feature sets.
401   SmallVector<StringRef, 4> ExistingFeatures(
402                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
403                                              ExistingTargetOpts.FeaturesAsWritten.end());
404   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
405                                          TargetOpts.FeaturesAsWritten.end());
406   llvm::sort(ExistingFeatures);
407   llvm::sort(ReadFeatures);
408 
409   // We compute the set difference in both directions explicitly so that we can
410   // diagnose the differences differently.
411   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
412   std::set_difference(
413       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
414       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
415   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
416                       ExistingFeatures.begin(), ExistingFeatures.end(),
417                       std::back_inserter(UnmatchedReadFeatures));
418 
419   // If we are allowing compatible differences and the read feature set is
420   // a strict subset of the existing feature set, there is nothing to diagnose.
421   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
422     return false;
423 
424   if (Diags) {
425     for (StringRef Feature : UnmatchedReadFeatures)
426       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
427           << /* is-existing-feature */ false << Feature;
428     for (StringRef Feature : UnmatchedExistingFeatures)
429       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
430           << /* is-existing-feature */ true << Feature;
431   }
432 
433   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
434 }
435 
436 bool
437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
438                                   bool Complain,
439                                   bool AllowCompatibleDifferences) {
440   const LangOptions &ExistingLangOpts = PP.getLangOpts();
441   return checkLanguageOptions(LangOpts, ExistingLangOpts,
442                               Complain ? &Reader.Diags : nullptr,
443                               AllowCompatibleDifferences);
444 }
445 
446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
447                                      bool Complain,
448                                      bool AllowCompatibleDifferences) {
449   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
450   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
451                             Complain ? &Reader.Diags : nullptr,
452                             AllowCompatibleDifferences);
453 }
454 
455 namespace {
456 
457 using MacroDefinitionsMap =
458     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
460 
461 } // namespace
462 
463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
464                                          DiagnosticsEngine &Diags,
465                                          bool Complain) {
466   using Level = DiagnosticsEngine::Level;
467 
468   // Check current mappings for new -Werror mappings, and the stored mappings
469   // for cases that were explicitly mapped to *not* be errors that are now
470   // errors because of options like -Werror.
471   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
472 
473   for (DiagnosticsEngine *MappingSource : MappingSources) {
474     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
475       diag::kind DiagID = DiagIDMappingPair.first;
476       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
477       if (CurLevel < DiagnosticsEngine::Error)
478         continue; // not significant
479       Level StoredLevel =
480           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
481       if (StoredLevel < DiagnosticsEngine::Error) {
482         if (Complain)
483           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
484               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
485         return true;
486       }
487     }
488   }
489 
490   return false;
491 }
492 
493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
494   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
495   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
496     return true;
497   return Ext >= diag::Severity::Error;
498 }
499 
500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
501                                     DiagnosticsEngine &Diags,
502                                     bool IsSystem, bool Complain) {
503   // Top-level options
504   if (IsSystem) {
505     if (Diags.getSuppressSystemWarnings())
506       return false;
507     // If -Wsystem-headers was not enabled before, be conservative
508     if (StoredDiags.getSuppressSystemWarnings()) {
509       if (Complain)
510         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
511       return true;
512     }
513   }
514 
515   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
518     return true;
519   }
520 
521   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
522       !StoredDiags.getEnableAllWarnings()) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
525     return true;
526   }
527 
528   if (isExtHandlingFromDiagsError(Diags) &&
529       !isExtHandlingFromDiagsError(StoredDiags)) {
530     if (Complain)
531       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
532     return true;
533   }
534 
535   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
536 }
537 
538 /// Return the top import module if it is implicit, nullptr otherwise.
539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
540                                           Preprocessor &PP) {
541   // If the original import came from a file explicitly generated by the user,
542   // don't check the diagnostic mappings.
543   // FIXME: currently this is approximated by checking whether this is not a
544   // module import of an implicitly-loaded module file.
545   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
546   // the transitive closure of its imports, since unrelated modules cannot be
547   // imported until after this module finishes validation.
548   ModuleFile *TopImport = &*ModuleMgr.rbegin();
549   while (!TopImport->ImportedBy.empty())
550     TopImport = TopImport->ImportedBy[0];
551   if (TopImport->Kind != MK_ImplicitModule)
552     return nullptr;
553 
554   StringRef ModuleName = TopImport->ModuleName;
555   assert(!ModuleName.empty() && "diagnostic options read before module name");
556 
557   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
558   assert(M && "missing module");
559   return M;
560 }
561 
562 bool PCHValidator::ReadDiagnosticOptions(
563     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
564   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
565   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
566   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
567       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
568   // This should never fail, because we would have processed these options
569   // before writing them to an ASTFile.
570   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
571 
572   ModuleManager &ModuleMgr = Reader.getModuleManager();
573   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
574 
575   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
576   if (!TopM)
577     return false;
578 
579   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
580   // contains the union of their flags.
581   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
582                                  Complain);
583 }
584 
585 /// Collect the macro definitions provided by the given preprocessor
586 /// options.
587 static void
588 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
589                         MacroDefinitionsMap &Macros,
590                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
591   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
592     StringRef Macro = PPOpts.Macros[I].first;
593     bool IsUndef = PPOpts.Macros[I].second;
594 
595     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
596     StringRef MacroName = MacroPair.first;
597     StringRef MacroBody = MacroPair.second;
598 
599     // For an #undef'd macro, we only care about the name.
600     if (IsUndef) {
601       if (MacroNames && !Macros.count(MacroName))
602         MacroNames->push_back(MacroName);
603 
604       Macros[MacroName] = std::make_pair("", true);
605       continue;
606     }
607 
608     // For a #define'd macro, figure out the actual definition.
609     if (MacroName.size() == Macro.size())
610       MacroBody = "1";
611     else {
612       // Note: GCC drops anything following an end-of-line character.
613       StringRef::size_type End = MacroBody.find_first_of("\n\r");
614       MacroBody = MacroBody.substr(0, End);
615     }
616 
617     if (MacroNames && !Macros.count(MacroName))
618       MacroNames->push_back(MacroName);
619     Macros[MacroName] = std::make_pair(MacroBody, false);
620   }
621 }
622 
623 /// Check the preprocessor options deserialized from the control block
624 /// against the preprocessor options in an existing preprocessor.
625 ///
626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
627 /// \param Validate If true, validate preprocessor options. If false, allow
628 ///        macros defined by \p ExistingPPOpts to override those defined by
629 ///        \p PPOpts in SuggestedPredefines.
630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
631                                      const PreprocessorOptions &ExistingPPOpts,
632                                      DiagnosticsEngine *Diags,
633                                      FileManager &FileMgr,
634                                      std::string &SuggestedPredefines,
635                                      const LangOptions &LangOpts,
636                                      bool Validate = true) {
637   // Check macro definitions.
638   MacroDefinitionsMap ASTFileMacros;
639   collectMacroDefinitions(PPOpts, ASTFileMacros);
640   MacroDefinitionsMap ExistingMacros;
641   SmallVector<StringRef, 4> ExistingMacroNames;
642   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
643 
644   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
645     // Dig out the macro definition in the existing preprocessor options.
646     StringRef MacroName = ExistingMacroNames[I];
647     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
648 
649     // Check whether we know anything about this macro name or not.
650     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
651         ASTFileMacros.find(MacroName);
652     if (!Validate || Known == ASTFileMacros.end()) {
653       // FIXME: Check whether this identifier was referenced anywhere in the
654       // AST file. If so, we should reject the AST file. Unfortunately, this
655       // information isn't in the control block. What shall we do about it?
656 
657       if (Existing.second) {
658         SuggestedPredefines += "#undef ";
659         SuggestedPredefines += MacroName.str();
660         SuggestedPredefines += '\n';
661       } else {
662         SuggestedPredefines += "#define ";
663         SuggestedPredefines += MacroName.str();
664         SuggestedPredefines += ' ';
665         SuggestedPredefines += Existing.first.str();
666         SuggestedPredefines += '\n';
667       }
668       continue;
669     }
670 
671     // If the macro was defined in one but undef'd in the other, we have a
672     // conflict.
673     if (Existing.second != Known->second.second) {
674       if (Diags) {
675         Diags->Report(diag::err_pch_macro_def_undef)
676           << MacroName << Known->second.second;
677       }
678       return true;
679     }
680 
681     // If the macro was #undef'd in both, or if the macro bodies are identical,
682     // it's fine.
683     if (Existing.second || Existing.first == Known->second.first)
684       continue;
685 
686     // The macro bodies differ; complain.
687     if (Diags) {
688       Diags->Report(diag::err_pch_macro_def_conflict)
689         << MacroName << Known->second.first << Existing.first;
690     }
691     return true;
692   }
693 
694   // Check whether we're using predefines.
695   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
696     if (Diags) {
697       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
698     }
699     return true;
700   }
701 
702   // Detailed record is important since it is used for the module cache hash.
703   if (LangOpts.Modules &&
704       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
705     if (Diags) {
706       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
707     }
708     return true;
709   }
710 
711   // Compute the #include and #include_macros lines we need.
712   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
713     StringRef File = ExistingPPOpts.Includes[I];
714 
715     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
716         !ExistingPPOpts.PCHThroughHeader.empty()) {
717       // In case the through header is an include, we must add all the includes
718       // to the predefines so the start point can be determined.
719       SuggestedPredefines += "#include \"";
720       SuggestedPredefines += File;
721       SuggestedPredefines += "\"\n";
722       continue;
723     }
724 
725     if (File == ExistingPPOpts.ImplicitPCHInclude)
726       continue;
727 
728     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
729           != PPOpts.Includes.end())
730       continue;
731 
732     SuggestedPredefines += "#include \"";
733     SuggestedPredefines += File;
734     SuggestedPredefines += "\"\n";
735   }
736 
737   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
738     StringRef File = ExistingPPOpts.MacroIncludes[I];
739     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
740                   File)
741         != PPOpts.MacroIncludes.end())
742       continue;
743 
744     SuggestedPredefines += "#__include_macros \"";
745     SuggestedPredefines += File;
746     SuggestedPredefines += "\"\n##\n";
747   }
748 
749   return false;
750 }
751 
752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
753                                            bool Complain,
754                                            std::string &SuggestedPredefines) {
755   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
756 
757   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
758                                   Complain? &Reader.Diags : nullptr,
759                                   PP.getFileManager(),
760                                   SuggestedPredefines,
761                                   PP.getLangOpts());
762 }
763 
764 bool SimpleASTReaderListener::ReadPreprocessorOptions(
765                                   const PreprocessorOptions &PPOpts,
766                                   bool Complain,
767                                   std::string &SuggestedPredefines) {
768   return checkPreprocessorOptions(PPOpts,
769                                   PP.getPreprocessorOpts(),
770                                   nullptr,
771                                   PP.getFileManager(),
772                                   SuggestedPredefines,
773                                   PP.getLangOpts(),
774                                   false);
775 }
776 
777 /// Check the header search options deserialized from the control block
778 /// against the header search options in an existing preprocessor.
779 ///
780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
782                                      StringRef SpecificModuleCachePath,
783                                      StringRef ExistingModuleCachePath,
784                                      DiagnosticsEngine *Diags,
785                                      const LangOptions &LangOpts) {
786   if (LangOpts.Modules) {
787     if (SpecificModuleCachePath != ExistingModuleCachePath) {
788       if (Diags)
789         Diags->Report(diag::err_pch_modulecache_mismatch)
790           << SpecificModuleCachePath << ExistingModuleCachePath;
791       return true;
792     }
793   }
794 
795   return false;
796 }
797 
798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
799                                            StringRef SpecificModuleCachePath,
800                                            bool Complain) {
801   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
802                                   PP.getHeaderSearchInfo().getModuleCachePath(),
803                                   Complain ? &Reader.Diags : nullptr,
804                                   PP.getLangOpts());
805 }
806 
807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
808   PP.setCounterValue(Value);
809 }
810 
811 //===----------------------------------------------------------------------===//
812 // AST reader implementation
813 //===----------------------------------------------------------------------===//
814 
815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
816                                            bool TakeOwnership) {
817   DeserializationListener = Listener;
818   OwnsDeserializationListener = TakeOwnership;
819 }
820 
821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
822   return serialization::ComputeHash(Sel);
823 }
824 
825 std::pair<unsigned, unsigned>
826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
827   using namespace llvm::support;
828 
829   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
830   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
831   return std::make_pair(KeyLen, DataLen);
832 }
833 
834 ASTSelectorLookupTrait::internal_key_type
835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
836   using namespace llvm::support;
837 
838   SelectorTable &SelTable = Reader.getContext().Selectors;
839   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
840   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
841       F, endian::readNext<uint32_t, little, unaligned>(d));
842   if (N == 0)
843     return SelTable.getNullarySelector(FirstII);
844   else if (N == 1)
845     return SelTable.getUnarySelector(FirstII);
846 
847   SmallVector<IdentifierInfo *, 16> Args;
848   Args.push_back(FirstII);
849   for (unsigned I = 1; I != N; ++I)
850     Args.push_back(Reader.getLocalIdentifier(
851         F, endian::readNext<uint32_t, little, unaligned>(d)));
852 
853   return SelTable.getSelector(N, Args.data());
854 }
855 
856 ASTSelectorLookupTrait::data_type
857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
858                                  unsigned DataLen) {
859   using namespace llvm::support;
860 
861   data_type Result;
862 
863   Result.ID = Reader.getGlobalSelectorID(
864       F, endian::readNext<uint32_t, little, unaligned>(d));
865   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
866   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
867   Result.InstanceBits = FullInstanceBits & 0x3;
868   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
869   Result.FactoryBits = FullFactoryBits & 0x3;
870   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
871   unsigned NumInstanceMethods = FullInstanceBits >> 3;
872   unsigned NumFactoryMethods = FullFactoryBits >> 3;
873 
874   // Load instance methods
875   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Instance.push_back(Method);
879   }
880 
881   // Load factory methods
882   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
883     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
884             F, endian::readNext<uint32_t, little, unaligned>(d)))
885       Result.Factory.push_back(Method);
886   }
887 
888   return Result;
889 }
890 
891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
892   return llvm::djbHash(a);
893 }
894 
895 std::pair<unsigned, unsigned>
896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
897   using namespace llvm::support;
898 
899   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
900   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
901   return std::make_pair(KeyLen, DataLen);
902 }
903 
904 ASTIdentifierLookupTraitBase::internal_key_type
905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
906   assert(n >= 2 && d[n-1] == '\0');
907   return StringRef((const char*) d, n-1);
908 }
909 
910 /// Whether the given identifier is "interesting".
911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
912                                     bool IsModule) {
913   return II.hadMacroDefinition() || II.isPoisoned() ||
914          (!IsModule && II.getObjCOrBuiltinID()) ||
915          II.hasRevertedTokenIDToIdentifier() ||
916          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
917           II.getFETokenInfo());
918 }
919 
920 static bool readBit(unsigned &Bits) {
921   bool Value = Bits & 0x1;
922   Bits >>= 1;
923   return Value;
924 }
925 
926 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
927   using namespace llvm::support;
928 
929   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
930   return Reader.getGlobalIdentifierID(F, RawID >> 1);
931 }
932 
933 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
934   if (!II.isFromAST()) {
935     II.setIsFromAST();
936     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
937     if (isInterestingIdentifier(Reader, II, IsModule))
938       II.setChangedSinceDeserialization();
939   }
940 }
941 
942 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
943                                                    const unsigned char* d,
944                                                    unsigned DataLen) {
945   using namespace llvm::support;
946 
947   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948   bool IsInteresting = RawID & 0x01;
949 
950   // Wipe out the "is interesting" bit.
951   RawID = RawID >> 1;
952 
953   // Build the IdentifierInfo and link the identifier ID with it.
954   IdentifierInfo *II = KnownII;
955   if (!II) {
956     II = &Reader.getIdentifierTable().getOwn(k);
957     KnownII = II;
958   }
959   markIdentifierFromAST(Reader, *II);
960   Reader.markIdentifierUpToDate(II);
961 
962   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
963   if (!IsInteresting) {
964     // For uninteresting identifiers, there's nothing else to do. Just notify
965     // the reader that we've finished loading this identifier.
966     Reader.SetIdentifierInfo(ID, II);
967     return II;
968   }
969 
970   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
971   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
972   bool CPlusPlusOperatorKeyword = readBit(Bits);
973   bool HasRevertedTokenIDToIdentifier = 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   assert(II->isExtensionToken() == ExtensionToken &&
988          "Incorrect extension token flag");
989   (void)ExtensionToken;
990   if (Poisoned)
991     II->setIsPoisoned(true);
992   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
993          "Incorrect C++ operator keyword flag");
994   (void)CPlusPlusOperatorKeyword;
995 
996   // If this identifier is a macro, deserialize the macro
997   // definition.
998   if (HadMacroDefinition) {
999     uint32_t MacroDirectivesOffset =
1000         endian::readNext<uint32_t, little, unaligned>(d);
1001     DataLen -= 4;
1002 
1003     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1004   }
1005 
1006   Reader.SetIdentifierInfo(ID, II);
1007 
1008   // Read all of the declarations visible at global scope with this
1009   // name.
1010   if (DataLen > 0) {
1011     SmallVector<uint32_t, 4> DeclIDs;
1012     for (; DataLen > 0; DataLen -= 4)
1013       DeclIDs.push_back(Reader.getGlobalDeclID(
1014           F, endian::readNext<uint32_t, little, unaligned>(d)));
1015     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1016   }
1017 
1018   return II;
1019 }
1020 
1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1022     : Kind(Name.getNameKind()) {
1023   switch (Kind) {
1024   case DeclarationName::Identifier:
1025     Data = (uint64_t)Name.getAsIdentifierInfo();
1026     break;
1027   case DeclarationName::ObjCZeroArgSelector:
1028   case DeclarationName::ObjCOneArgSelector:
1029   case DeclarationName::ObjCMultiArgSelector:
1030     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1031     break;
1032   case DeclarationName::CXXOperatorName:
1033     Data = Name.getCXXOverloadedOperator();
1034     break;
1035   case DeclarationName::CXXLiteralOperatorName:
1036     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1037     break;
1038   case DeclarationName::CXXDeductionGuideName:
1039     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1040                ->getDeclName().getAsIdentifierInfo();
1041     break;
1042   case DeclarationName::CXXConstructorName:
1043   case DeclarationName::CXXDestructorName:
1044   case DeclarationName::CXXConversionFunctionName:
1045   case DeclarationName::CXXUsingDirective:
1046     Data = 0;
1047     break;
1048   }
1049 }
1050 
1051 unsigned DeclarationNameKey::getHash() const {
1052   llvm::FoldingSetNodeID ID;
1053   ID.AddInteger(Kind);
1054 
1055   switch (Kind) {
1056   case DeclarationName::Identifier:
1057   case DeclarationName::CXXLiteralOperatorName:
1058   case DeclarationName::CXXDeductionGuideName:
1059     ID.AddString(((IdentifierInfo*)Data)->getName());
1060     break;
1061   case DeclarationName::ObjCZeroArgSelector:
1062   case DeclarationName::ObjCOneArgSelector:
1063   case DeclarationName::ObjCMultiArgSelector:
1064     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1065     break;
1066   case DeclarationName::CXXOperatorName:
1067     ID.AddInteger((OverloadedOperatorKind)Data);
1068     break;
1069   case DeclarationName::CXXConstructorName:
1070   case DeclarationName::CXXDestructorName:
1071   case DeclarationName::CXXConversionFunctionName:
1072   case DeclarationName::CXXUsingDirective:
1073     break;
1074   }
1075 
1076   return ID.ComputeHash();
1077 }
1078 
1079 ModuleFile *
1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1081   using namespace llvm::support;
1082 
1083   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1084   return Reader.getLocalModuleFile(F, ModuleFileID);
1085 }
1086 
1087 std::pair<unsigned, unsigned>
1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1089   using namespace llvm::support;
1090 
1091   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1092   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   return std::make_pair(KeyLen, DataLen);
1094 }
1095 
1096 ASTDeclContextNameLookupTrait::internal_key_type
1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1098   using namespace llvm::support;
1099 
1100   auto Kind = (DeclarationName::NameKind)*d++;
1101   uint64_t Data;
1102   switch (Kind) {
1103   case DeclarationName::Identifier:
1104   case DeclarationName::CXXLiteralOperatorName:
1105   case DeclarationName::CXXDeductionGuideName:
1106     Data = (uint64_t)Reader.getLocalIdentifier(
1107         F, endian::readNext<uint32_t, little, unaligned>(d));
1108     break;
1109   case DeclarationName::ObjCZeroArgSelector:
1110   case DeclarationName::ObjCOneArgSelector:
1111   case DeclarationName::ObjCMultiArgSelector:
1112     Data =
1113         (uint64_t)Reader.getLocalSelector(
1114                              F, endian::readNext<uint32_t, little, unaligned>(
1115                                     d)).getAsOpaquePtr();
1116     break;
1117   case DeclarationName::CXXOperatorName:
1118     Data = *d++; // OverloadedOperatorKind
1119     break;
1120   case DeclarationName::CXXConstructorName:
1121   case DeclarationName::CXXDestructorName:
1122   case DeclarationName::CXXConversionFunctionName:
1123   case DeclarationName::CXXUsingDirective:
1124     Data = 0;
1125     break;
1126   }
1127 
1128   return DeclarationNameKey(Kind, Data);
1129 }
1130 
1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1132                                                  const unsigned char *d,
1133                                                  unsigned DataLen,
1134                                                  data_type_builder &Val) {
1135   using namespace llvm::support;
1136 
1137   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1138     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1139     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1140   }
1141 }
1142 
1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1144                                               BitstreamCursor &Cursor,
1145                                               uint64_t Offset,
1146                                               DeclContext *DC) {
1147   assert(Offset != 0);
1148 
1149   SavedStreamPosition SavedPosition(Cursor);
1150   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1151     Error(std::move(Err));
1152     return true;
1153   }
1154 
1155   RecordData Record;
1156   StringRef Blob;
1157   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1158   if (!MaybeCode) {
1159     Error(MaybeCode.takeError());
1160     return true;
1161   }
1162   unsigned Code = MaybeCode.get();
1163 
1164   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1165   if (!MaybeRecCode) {
1166     Error(MaybeRecCode.takeError());
1167     return true;
1168   }
1169   unsigned RecCode = MaybeRecCode.get();
1170   if (RecCode != DECL_CONTEXT_LEXICAL) {
1171     Error("Expected lexical block");
1172     return true;
1173   }
1174 
1175   assert(!isa<TranslationUnitDecl>(DC) &&
1176          "expected a TU_UPDATE_LEXICAL record for TU");
1177   // If we are handling a C++ class template instantiation, we can see multiple
1178   // lexical updates for the same record. It's important that we select only one
1179   // of them, so that field numbering works properly. Just pick the first one we
1180   // see.
1181   auto &Lex = LexicalDecls[DC];
1182   if (!Lex.first) {
1183     Lex = std::make_pair(
1184         &M, llvm::makeArrayRef(
1185                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1186                     Blob.data()),
1187                 Blob.size() / 4));
1188   }
1189   DC->setHasExternalLexicalStorage(true);
1190   return false;
1191 }
1192 
1193 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1194                                               BitstreamCursor &Cursor,
1195                                               uint64_t Offset,
1196                                               DeclID ID) {
1197   assert(Offset != 0);
1198 
1199   SavedStreamPosition SavedPosition(Cursor);
1200   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1201     Error(std::move(Err));
1202     return true;
1203   }
1204 
1205   RecordData Record;
1206   StringRef Blob;
1207   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1208   if (!MaybeCode) {
1209     Error(MaybeCode.takeError());
1210     return true;
1211   }
1212   unsigned Code = MaybeCode.get();
1213 
1214   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1215   if (!MaybeRecCode) {
1216     Error(MaybeRecCode.takeError());
1217     return true;
1218   }
1219   unsigned RecCode = MaybeRecCode.get();
1220   if (RecCode != DECL_CONTEXT_VISIBLE) {
1221     Error("Expected visible lookup table block");
1222     return true;
1223   }
1224 
1225   // We can't safely determine the primary context yet, so delay attaching the
1226   // lookup table until we're done with recursive deserialization.
1227   auto *Data = (const unsigned char*)Blob.data();
1228   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1229   return false;
1230 }
1231 
1232 void ASTReader::Error(StringRef Msg) const {
1233   Error(diag::err_fe_pch_malformed, Msg);
1234   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1235       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1236     Diag(diag::note_module_cache_path)
1237       << PP.getHeaderSearchInfo().getModuleCachePath();
1238   }
1239 }
1240 
1241 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1242                       StringRef Arg3) const {
1243   if (Diags.isDiagnosticInFlight())
1244     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1245   else
1246     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1247 }
1248 
1249 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1250                       unsigned Select) const {
1251   if (!Diags.isDiagnosticInFlight())
1252     Diag(DiagID) << Arg1 << Arg2 << Select;
1253 }
1254 
1255 void ASTReader::Error(llvm::Error &&Err) const {
1256   Error(toString(std::move(Err)));
1257 }
1258 
1259 //===----------------------------------------------------------------------===//
1260 // Source Manager Deserialization
1261 //===----------------------------------------------------------------------===//
1262 
1263 /// Read the line table in the source manager block.
1264 /// \returns true if there was an error.
1265 bool ASTReader::ParseLineTable(ModuleFile &F,
1266                                const RecordData &Record) {
1267   unsigned Idx = 0;
1268   LineTableInfo &LineTable = SourceMgr.getLineTable();
1269 
1270   // Parse the file names
1271   std::map<int, int> FileIDs;
1272   FileIDs[-1] = -1; // For unspecified filenames.
1273   for (unsigned I = 0; Record[Idx]; ++I) {
1274     // Extract the file name
1275     auto Filename = ReadPath(F, Record, Idx);
1276     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1277   }
1278   ++Idx;
1279 
1280   // Parse the line entries
1281   std::vector<LineEntry> Entries;
1282   while (Idx < Record.size()) {
1283     int FID = Record[Idx++];
1284     assert(FID >= 0 && "Serialized line entries for non-local file.");
1285     // Remap FileID from 1-based old view.
1286     FID += F.SLocEntryBaseID - 1;
1287 
1288     // Extract the line entries
1289     unsigned NumEntries = Record[Idx++];
1290     assert(NumEntries && "no line entries for file ID");
1291     Entries.clear();
1292     Entries.reserve(NumEntries);
1293     for (unsigned I = 0; I != NumEntries; ++I) {
1294       unsigned FileOffset = Record[Idx++];
1295       unsigned LineNo = Record[Idx++];
1296       int FilenameID = FileIDs[Record[Idx++]];
1297       SrcMgr::CharacteristicKind FileKind
1298         = (SrcMgr::CharacteristicKind)Record[Idx++];
1299       unsigned IncludeOffset = Record[Idx++];
1300       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1301                                        FileKind, IncludeOffset));
1302     }
1303     LineTable.AddEntry(FileID::get(FID), Entries);
1304   }
1305 
1306   return false;
1307 }
1308 
1309 /// Read a source manager block
1310 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1311   using namespace SrcMgr;
1312 
1313   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1314 
1315   // Set the source-location entry cursor to the current position in
1316   // the stream. This cursor will be used to read the contents of the
1317   // source manager block initially, and then lazily read
1318   // source-location entries as needed.
1319   SLocEntryCursor = F.Stream;
1320 
1321   // The stream itself is going to skip over the source manager block.
1322   if (llvm::Error Err = F.Stream.SkipBlock()) {
1323     Error(std::move(Err));
1324     return true;
1325   }
1326 
1327   // Enter the source manager block.
1328   if (llvm::Error Err =
1329           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1330     Error(std::move(Err));
1331     return true;
1332   }
1333   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1334 
1335   RecordData Record;
1336   while (true) {
1337     Expected<llvm::BitstreamEntry> MaybeE =
1338         SLocEntryCursor.advanceSkippingSubblocks();
1339     if (!MaybeE) {
1340       Error(MaybeE.takeError());
1341       return true;
1342     }
1343     llvm::BitstreamEntry E = MaybeE.get();
1344 
1345     switch (E.Kind) {
1346     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1347     case llvm::BitstreamEntry::Error:
1348       Error("malformed block record in AST file");
1349       return true;
1350     case llvm::BitstreamEntry::EndBlock:
1351       return false;
1352     case llvm::BitstreamEntry::Record:
1353       // The interesting case.
1354       break;
1355     }
1356 
1357     // Read a record.
1358     Record.clear();
1359     StringRef Blob;
1360     Expected<unsigned> MaybeRecord =
1361         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1362     if (!MaybeRecord) {
1363       Error(MaybeRecord.takeError());
1364       return true;
1365     }
1366     switch (MaybeRecord.get()) {
1367     default:  // Default behavior: ignore.
1368       break;
1369 
1370     case SM_SLOC_FILE_ENTRY:
1371     case SM_SLOC_BUFFER_ENTRY:
1372     case SM_SLOC_EXPANSION_ENTRY:
1373       // Once we hit one of the source location entries, we're done.
1374       return false;
1375     }
1376   }
1377 }
1378 
1379 /// If a header file is not found at the path that we expect it to be
1380 /// and the PCH file was moved from its original location, try to resolve the
1381 /// file by assuming that header+PCH were moved together and the header is in
1382 /// the same place relative to the PCH.
1383 static std::string
1384 resolveFileRelativeToOriginalDir(const std::string &Filename,
1385                                  const std::string &OriginalDir,
1386                                  const std::string &CurrDir) {
1387   assert(OriginalDir != CurrDir &&
1388          "No point trying to resolve the file if the PCH dir didn't change");
1389 
1390   using namespace llvm::sys;
1391 
1392   SmallString<128> filePath(Filename);
1393   fs::make_absolute(filePath);
1394   assert(path::is_absolute(OriginalDir));
1395   SmallString<128> currPCHPath(CurrDir);
1396 
1397   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1398                        fileDirE = path::end(path::parent_path(filePath));
1399   path::const_iterator origDirI = path::begin(OriginalDir),
1400                        origDirE = path::end(OriginalDir);
1401   // Skip the common path components from filePath and OriginalDir.
1402   while (fileDirI != fileDirE && origDirI != origDirE &&
1403          *fileDirI == *origDirI) {
1404     ++fileDirI;
1405     ++origDirI;
1406   }
1407   for (; origDirI != origDirE; ++origDirI)
1408     path::append(currPCHPath, "..");
1409   path::append(currPCHPath, fileDirI, fileDirE);
1410   path::append(currPCHPath, path::filename(Filename));
1411   return std::string(currPCHPath.str());
1412 }
1413 
1414 bool ASTReader::ReadSLocEntry(int ID) {
1415   if (ID == 0)
1416     return false;
1417 
1418   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1419     Error("source location entry ID out-of-range for AST file");
1420     return true;
1421   }
1422 
1423   // Local helper to read the (possibly-compressed) buffer data following the
1424   // entry record.
1425   auto ReadBuffer = [this](
1426       BitstreamCursor &SLocEntryCursor,
1427       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1428     RecordData Record;
1429     StringRef Blob;
1430     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1431     if (!MaybeCode) {
1432       Error(MaybeCode.takeError());
1433       return nullptr;
1434     }
1435     unsigned Code = MaybeCode.get();
1436 
1437     Expected<unsigned> MaybeRecCode =
1438         SLocEntryCursor.readRecord(Code, Record, &Blob);
1439     if (!MaybeRecCode) {
1440       Error(MaybeRecCode.takeError());
1441       return nullptr;
1442     }
1443     unsigned RecCode = MaybeRecCode.get();
1444 
1445     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1446       if (!llvm::zlib::isAvailable()) {
1447         Error("zlib is not available");
1448         return nullptr;
1449       }
1450       SmallString<0> Uncompressed;
1451       if (llvm::Error E =
1452               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1453         Error("could not decompress embedded file contents: " +
1454               llvm::toString(std::move(E)));
1455         return nullptr;
1456       }
1457       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1458     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1459       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1460     } else {
1461       Error("AST record has invalid code");
1462       return nullptr;
1463     }
1464   };
1465 
1466   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1467   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1468           F->SLocEntryOffsetsBase +
1469           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1470     Error(std::move(Err));
1471     return true;
1472   }
1473 
1474   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1475   unsigned BaseOffset = F->SLocEntryBaseOffset;
1476 
1477   ++NumSLocEntriesRead;
1478   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1479   if (!MaybeEntry) {
1480     Error(MaybeEntry.takeError());
1481     return true;
1482   }
1483   llvm::BitstreamEntry Entry = MaybeEntry.get();
1484 
1485   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1486     Error("incorrectly-formatted source location entry in AST file");
1487     return true;
1488   }
1489 
1490   RecordData Record;
1491   StringRef Blob;
1492   Expected<unsigned> MaybeSLOC =
1493       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1494   if (!MaybeSLOC) {
1495     Error(MaybeSLOC.takeError());
1496     return true;
1497   }
1498   switch (MaybeSLOC.get()) {
1499   default:
1500     Error("incorrectly-formatted source location entry in AST file");
1501     return true;
1502 
1503   case SM_SLOC_FILE_ENTRY: {
1504     // We will detect whether a file changed and return 'Failure' for it, but
1505     // we will also try to fail gracefully by setting up the SLocEntry.
1506     unsigned InputID = Record[4];
1507     InputFile IF = getInputFile(*F, InputID);
1508     const FileEntry *File = IF.getFile();
1509     bool OverriddenBuffer = IF.isOverridden();
1510 
1511     // Note that we only check if a File was returned. If it was out-of-date
1512     // we have complained but we will continue creating a FileID to recover
1513     // gracefully.
1514     if (!File)
1515       return true;
1516 
1517     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1518     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1519       // This is the module's main file.
1520       IncludeLoc = getImportLocation(F);
1521     }
1522     SrcMgr::CharacteristicKind
1523       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1524     // FIXME: The FileID should be created from the FileEntryRef.
1525     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1526                                         ID, BaseOffset + Record[0]);
1527     SrcMgr::FileInfo &FileInfo =
1528           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1529     FileInfo.NumCreatedFIDs = Record[5];
1530     if (Record[3])
1531       FileInfo.setHasLineDirectives();
1532 
1533     unsigned NumFileDecls = Record[7];
1534     if (NumFileDecls && ContextObj) {
1535       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1536       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1537       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1538                                                              NumFileDecls));
1539     }
1540 
1541     const SrcMgr::ContentCache *ContentCache
1542       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1543     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1544         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1545         !ContentCache->getRawBuffer()) {
1546       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1547       if (!Buffer)
1548         return true;
1549       SourceMgr.overrideFileContents(File, std::move(Buffer));
1550     }
1551 
1552     break;
1553   }
1554 
1555   case SM_SLOC_BUFFER_ENTRY: {
1556     const char *Name = Blob.data();
1557     unsigned Offset = Record[0];
1558     SrcMgr::CharacteristicKind
1559       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1560     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1561     if (IncludeLoc.isInvalid() && F->isModule()) {
1562       IncludeLoc = getImportLocation(F);
1563     }
1564 
1565     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1566     if (!Buffer)
1567       return true;
1568     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1569                            BaseOffset + Offset, IncludeLoc);
1570     break;
1571   }
1572 
1573   case SM_SLOC_EXPANSION_ENTRY: {
1574     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1575     SourceMgr.createExpansionLoc(SpellingLoc,
1576                                      ReadSourceLocation(*F, Record[2]),
1577                                      ReadSourceLocation(*F, Record[3]),
1578                                      Record[5],
1579                                      Record[4],
1580                                      ID,
1581                                      BaseOffset + Record[0]);
1582     break;
1583   }
1584   }
1585 
1586   return false;
1587 }
1588 
1589 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1590   if (ID == 0)
1591     return std::make_pair(SourceLocation(), "");
1592 
1593   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1594     Error("source location entry ID out-of-range for AST file");
1595     return std::make_pair(SourceLocation(), "");
1596   }
1597 
1598   // Find which module file this entry lands in.
1599   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1600   if (!M->isModule())
1601     return std::make_pair(SourceLocation(), "");
1602 
1603   // FIXME: Can we map this down to a particular submodule? That would be
1604   // ideal.
1605   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1606 }
1607 
1608 /// Find the location where the module F is imported.
1609 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1610   if (F->ImportLoc.isValid())
1611     return F->ImportLoc;
1612 
1613   // Otherwise we have a PCH. It's considered to be "imported" at the first
1614   // location of its includer.
1615   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1616     // Main file is the importer.
1617     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1618     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1619   }
1620   return F->ImportedBy[0]->FirstLoc;
1621 }
1622 
1623 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1624 /// the abbreviations that are at the top of the block and then leave the cursor
1625 /// pointing into the block.
1626 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1627                                  uint64_t *StartOfBlockOffset) {
1628   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1629     // FIXME this drops errors on the floor.
1630     consumeError(std::move(Err));
1631     return true;
1632   }
1633 
1634   if (StartOfBlockOffset)
1635     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
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, ModuleFile *M,
1936                                 uint32_t MacroDirectivesOffset) {
1937   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1938   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1939 }
1940 
1941 void ASTReader::ReadDefinedMacros() {
1942   // Note that we are loading defined macros.
1943   Deserializing Macros(this);
1944 
1945   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1946     BitstreamCursor &MacroCursor = I.MacroCursor;
1947 
1948     // If there was no preprocessor block, skip this file.
1949     if (MacroCursor.getBitcodeBytes().empty())
1950       continue;
1951 
1952     BitstreamCursor Cursor = MacroCursor;
1953     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1954       Error(std::move(Err));
1955       return;
1956     }
1957 
1958     RecordData Record;
1959     while (true) {
1960       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1961       if (!MaybeE) {
1962         Error(MaybeE.takeError());
1963         return;
1964       }
1965       llvm::BitstreamEntry E = MaybeE.get();
1966 
1967       switch (E.Kind) {
1968       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1969       case llvm::BitstreamEntry::Error:
1970         Error("malformed block record in AST file");
1971         return;
1972       case llvm::BitstreamEntry::EndBlock:
1973         goto NextCursor;
1974 
1975       case llvm::BitstreamEntry::Record: {
1976         Record.clear();
1977         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1978         if (!MaybeRecord) {
1979           Error(MaybeRecord.takeError());
1980           return;
1981         }
1982         switch (MaybeRecord.get()) {
1983         default:  // Default behavior: ignore.
1984           break;
1985 
1986         case PP_MACRO_OBJECT_LIKE:
1987         case PP_MACRO_FUNCTION_LIKE: {
1988           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1989           if (II->isOutOfDate())
1990             updateOutOfDateIdentifier(*II);
1991           break;
1992         }
1993 
1994         case PP_TOKEN:
1995           // Ignore tokens.
1996           break;
1997         }
1998         break;
1999       }
2000       }
2001     }
2002     NextCursor:  ;
2003   }
2004 }
2005 
2006 namespace {
2007 
2008   /// Visitor class used to look up identifirs in an AST file.
2009   class IdentifierLookupVisitor {
2010     StringRef Name;
2011     unsigned NameHash;
2012     unsigned PriorGeneration;
2013     unsigned &NumIdentifierLookups;
2014     unsigned &NumIdentifierLookupHits;
2015     IdentifierInfo *Found = nullptr;
2016 
2017   public:
2018     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2019                             unsigned &NumIdentifierLookups,
2020                             unsigned &NumIdentifierLookupHits)
2021       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2022         PriorGeneration(PriorGeneration),
2023         NumIdentifierLookups(NumIdentifierLookups),
2024         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2025 
2026     bool operator()(ModuleFile &M) {
2027       // If we've already searched this module file, skip it now.
2028       if (M.Generation <= PriorGeneration)
2029         return true;
2030 
2031       ASTIdentifierLookupTable *IdTable
2032         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2033       if (!IdTable)
2034         return false;
2035 
2036       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2037                                      Found);
2038       ++NumIdentifierLookups;
2039       ASTIdentifierLookupTable::iterator Pos =
2040           IdTable->find_hashed(Name, NameHash, &Trait);
2041       if (Pos == IdTable->end())
2042         return false;
2043 
2044       // Dereferencing the iterator has the effect of building the
2045       // IdentifierInfo node and populating it with the various
2046       // declarations it needs.
2047       ++NumIdentifierLookupHits;
2048       Found = *Pos;
2049       return true;
2050     }
2051 
2052     // Retrieve the identifier info found within the module
2053     // files.
2054     IdentifierInfo *getIdentifierInfo() const { return Found; }
2055   };
2056 
2057 } // namespace
2058 
2059 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2060   // Note that we are loading an identifier.
2061   Deserializing AnIdentifier(this);
2062 
2063   unsigned PriorGeneration = 0;
2064   if (getContext().getLangOpts().Modules)
2065     PriorGeneration = IdentifierGeneration[&II];
2066 
2067   // If there is a global index, look there first to determine which modules
2068   // provably do not have any results for this identifier.
2069   GlobalModuleIndex::HitSet Hits;
2070   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2071   if (!loadGlobalIndex()) {
2072     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2073       HitsPtr = &Hits;
2074     }
2075   }
2076 
2077   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2078                                   NumIdentifierLookups,
2079                                   NumIdentifierLookupHits);
2080   ModuleMgr.visit(Visitor, HitsPtr);
2081   markIdentifierUpToDate(&II);
2082 }
2083 
2084 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2085   if (!II)
2086     return;
2087 
2088   II->setOutOfDate(false);
2089 
2090   // Update the generation for this identifier.
2091   if (getContext().getLangOpts().Modules)
2092     IdentifierGeneration[II] = getGeneration();
2093 }
2094 
2095 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2096                                     const PendingMacroInfo &PMInfo) {
2097   ModuleFile &M = *PMInfo.M;
2098 
2099   BitstreamCursor &Cursor = M.MacroCursor;
2100   SavedStreamPosition SavedPosition(Cursor);
2101   if (llvm::Error Err =
2102           Cursor.JumpToBit(M.MacroOffsetsBase + 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[6];
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       const std::string &CurBranch = getClangFullRepositoryVersion();
2763       StringRef ASTBranch = Blob;
2764       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2767         return VersionMismatch;
2768       }
2769       break;
2770     }
2771 
2772     case IMPORTS: {
2773       // Validate the AST before processing any imports (otherwise, untangling
2774       // them can be error-prone and expensive).  A module will have a name and
2775       // will already have been validated, but this catches the PCH case.
2776       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2777         return Result;
2778 
2779       // Load each of the imported PCH files.
2780       unsigned Idx = 0, N = Record.size();
2781       while (Idx < N) {
2782         // Read information about the AST file.
2783         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2784         // The import location will be the local one for now; we will adjust
2785         // all import locations of module imports after the global source
2786         // location info are setup, in ReadAST.
2787         SourceLocation ImportLoc =
2788             ReadUntranslatedSourceLocation(Record[Idx++]);
2789         off_t StoredSize = (off_t)Record[Idx++];
2790         time_t StoredModTime = (time_t)Record[Idx++];
2791         auto FirstSignatureByte = Record.begin() + Idx;
2792         ASTFileSignature StoredSignature = ASTFileSignature::create(
2793             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2794         Idx += ASTFileSignature::size;
2795 
2796         std::string ImportedName = ReadString(Record, Idx);
2797         std::string ImportedFile;
2798 
2799         // For prebuilt and explicit modules first consult the file map for
2800         // an override. Note that here we don't search prebuilt module
2801         // directories, only the explicit name to file mappings. Also, we will
2802         // still verify the size/signature making sure it is essentially the
2803         // same file but perhaps in a different location.
2804         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806             ImportedName, /*FileMapOnly*/ true);
2807 
2808         if (ImportedFile.empty())
2809           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810           // ModuleCache as when writing.
2811           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2812         else
2813           SkipPath(Record, Idx);
2814 
2815         // If our client can't cope with us being out of date, we can't cope with
2816         // our dependency being missing.
2817         unsigned Capabilities = ClientLoadCapabilities;
2818         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819           Capabilities &= ~ARR_Missing;
2820 
2821         // Load the AST file.
2822         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823                                   Loaded, StoredSize, StoredModTime,
2824                                   StoredSignature, Capabilities);
2825 
2826         // If we diagnosed a problem, produce a backtrace.
2827         if (isDiagnosedResult(Result, Capabilities))
2828           Diag(diag::note_module_file_imported_by)
2829               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2830 
2831         switch (Result) {
2832         case Failure: return Failure;
2833           // If we have to ignore the dependency, we'll have to ignore this too.
2834         case Missing:
2835         case OutOfDate: return OutOfDate;
2836         case VersionMismatch: return VersionMismatch;
2837         case ConfigurationMismatch: return ConfigurationMismatch;
2838         case HadErrors: return HadErrors;
2839         case Success: break;
2840         }
2841       }
2842       break;
2843     }
2844 
2845     case ORIGINAL_FILE:
2846       F.OriginalSourceFileID = FileID::get(Record[0]);
2847       F.ActualOriginalSourceFileName = std::string(Blob);
2848       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2849       ResolveImportedPath(F, F.OriginalSourceFileName);
2850       break;
2851 
2852     case ORIGINAL_FILE_ID:
2853       F.OriginalSourceFileID = FileID::get(Record[0]);
2854       break;
2855 
2856     case ORIGINAL_PCH_DIR:
2857       F.OriginalDir = std::string(Blob);
2858       break;
2859 
2860     case MODULE_NAME:
2861       F.ModuleName = std::string(Blob);
2862       Diag(diag::remark_module_import)
2863           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2865       if (Listener)
2866         Listener->ReadModuleName(F.ModuleName);
2867 
2868       // Validate the AST as soon as we have a name so we can exit early on
2869       // failure.
2870       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2871         return Result;
2872 
2873       break;
2874 
2875     case MODULE_DIRECTORY: {
2876       // Save the BaseDirectory as written in the PCM for computing the module
2877       // filename for the ModuleCache.
2878       BaseDirectoryAsWritten = Blob;
2879       assert(!F.ModuleName.empty() &&
2880              "MODULE_DIRECTORY found before MODULE_NAME");
2881       // If we've already loaded a module map file covering this module, we may
2882       // have a better path for it (relative to the current build).
2883       Module *M = PP.getHeaderSearchInfo().lookupModule(
2884           F.ModuleName, /*AllowSearch*/ true,
2885           /*AllowExtraModuleMapSearch*/ true);
2886       if (M && M->Directory) {
2887         // If we're implicitly loading a module, the base directory can't
2888         // change between the build and use.
2889         // Don't emit module relocation error if we have -fno-validate-pch
2890         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2892           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893           if (!BuildDir || *BuildDir != M->Directory) {
2894             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895               Diag(diag::err_imported_module_relocated)
2896                   << F.ModuleName << Blob << M->Directory->getName();
2897             return OutOfDate;
2898           }
2899         }
2900         F.BaseDirectory = std::string(M->Directory->getName());
2901       } else {
2902         F.BaseDirectory = std::string(Blob);
2903       }
2904       break;
2905     }
2906 
2907     case MODULE_MAP_FILE:
2908       if (ASTReadResult Result =
2909               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910         return Result;
2911       break;
2912 
2913     case INPUT_FILE_OFFSETS:
2914       NumInputs = Record[0];
2915       NumUserInputs = Record[1];
2916       F.InputFileOffsets =
2917           (const llvm::support::unaligned_uint64_t *)Blob.data();
2918       F.InputFilesLoaded.resize(NumInputs);
2919       F.NumUserInputFiles = NumUserInputs;
2920       break;
2921     }
2922   }
2923 }
2924 
2925 ASTReader::ASTReadResult
2926 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2927   BitstreamCursor &Stream = F.Stream;
2928 
2929   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930     Error(std::move(Err));
2931     return Failure;
2932   }
2933   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2934 
2935   // Read all of the records and blocks for the AST file.
2936   RecordData Record;
2937   while (true) {
2938     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2939     if (!MaybeEntry) {
2940       Error(MaybeEntry.takeError());
2941       return Failure;
2942     }
2943     llvm::BitstreamEntry Entry = MaybeEntry.get();
2944 
2945     switch (Entry.Kind) {
2946     case llvm::BitstreamEntry::Error:
2947       Error("error at end of module block in AST file");
2948       return Failure;
2949     case llvm::BitstreamEntry::EndBlock:
2950       // Outside of C++, we do not store a lookup map for the translation unit.
2951       // Instead, mark it as needing a lookup map to be built if this module
2952       // contains any declarations lexically within it (which it always does!).
2953       // This usually has no cost, since we very rarely need the lookup map for
2954       // the translation unit outside C++.
2955       if (ASTContext *Ctx = ContextObj) {
2956         DeclContext *DC = Ctx->getTranslationUnitDecl();
2957         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2958           DC->setMustBuildLookupTable();
2959       }
2960 
2961       return Success;
2962     case llvm::BitstreamEntry::SubBlock:
2963       switch (Entry.ID) {
2964       case DECLTYPES_BLOCK_ID:
2965         // We lazily load the decls block, but we want to set up the
2966         // DeclsCursor cursor to point into it.  Clone our current bitcode
2967         // cursor to it, enter the block and read the abbrevs in that block.
2968         // With the main cursor, we just skip over it.
2969         F.DeclsCursor = Stream;
2970         if (llvm::Error Err = Stream.SkipBlock()) {
2971           Error(std::move(Err));
2972           return Failure;
2973         }
2974         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2975                              &F.DeclsBlockStartOffset)) {
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 = reinterpret_cast<const UnderalignedInt64 *>(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           getContext().getLangOpts().BuildingPCHWithObjectFile)
3231         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3233       break;
3234 
3235     case SPECIAL_TYPES:
3236       if (SpecialTypes.empty()) {
3237         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3239         break;
3240       }
3241 
3242       if (SpecialTypes.size() != Record.size()) {
3243         Error("invalid special-types record");
3244         return Failure;
3245       }
3246 
3247       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3248         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3249         if (!SpecialTypes[I])
3250           SpecialTypes[I] = ID;
3251         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3252         // merge step?
3253       }
3254       break;
3255 
3256     case STATISTICS:
3257       TotalNumStatements += Record[0];
3258       TotalNumMacros += Record[1];
3259       TotalLexicalDeclContexts += Record[2];
3260       TotalVisibleDeclContexts += Record[3];
3261       break;
3262 
3263     case UNUSED_FILESCOPED_DECLS:
3264       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3266       break;
3267 
3268     case DELEGATING_CTORS:
3269       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3270         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3271       break;
3272 
3273     case WEAK_UNDECLARED_IDENTIFIERS:
3274       if (Record.size() % 4 != 0) {
3275         Error("invalid weak identifiers record");
3276         return Failure;
3277       }
3278 
3279       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3280       // files. This isn't the way to do it :)
3281       WeakUndeclaredIdentifiers.clear();
3282 
3283       // Translate the weak, undeclared identifiers into global IDs.
3284       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3285         WeakUndeclaredIdentifiers.push_back(
3286           getGlobalIdentifierID(F, Record[I++]));
3287         WeakUndeclaredIdentifiers.push_back(
3288           getGlobalIdentifierID(F, Record[I++]));
3289         WeakUndeclaredIdentifiers.push_back(
3290           ReadSourceLocation(F, Record, I).getRawEncoding());
3291         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3292       }
3293       break;
3294 
3295     case SELECTOR_OFFSETS: {
3296       F.SelectorOffsets = (const uint32_t *)Blob.data();
3297       F.LocalNumSelectors = Record[0];
3298       unsigned LocalBaseSelectorID = Record[1];
3299       F.BaseSelectorID = getTotalNumSelectors();
3300 
3301       if (F.LocalNumSelectors > 0) {
3302         // Introduce the global -> local mapping for selectors within this
3303         // module.
3304         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3305 
3306         // Introduce the local -> global mapping for selectors within this
3307         // module.
3308         F.SelectorRemap.insertOrReplace(
3309           std::make_pair(LocalBaseSelectorID,
3310                          F.BaseSelectorID - LocalBaseSelectorID));
3311 
3312         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3313       }
3314       break;
3315     }
3316 
3317     case METHOD_POOL:
3318       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3319       if (Record[0])
3320         F.SelectorLookupTable
3321           = ASTSelectorLookupTable::Create(
3322                         F.SelectorLookupTableData + Record[0],
3323                         F.SelectorLookupTableData,
3324                         ASTSelectorLookupTrait(*this, F));
3325       TotalNumMethodPoolEntries += Record[1];
3326       break;
3327 
3328     case REFERENCED_SELECTOR_POOL:
3329       if (!Record.empty()) {
3330         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3331           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3332                                                                 Record[Idx++]));
3333           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3334                                               getRawEncoding());
3335         }
3336       }
3337       break;
3338 
3339     case PP_CONDITIONAL_STACK:
3340       if (!Record.empty()) {
3341         unsigned Idx = 0, End = Record.size() - 1;
3342         bool ReachedEOFWhileSkipping = Record[Idx++];
3343         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3344         if (ReachedEOFWhileSkipping) {
3345           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3346           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3347           bool FoundNonSkipPortion = Record[Idx++];
3348           bool FoundElse = Record[Idx++];
3349           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3350           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3351                            FoundElse, ElseLoc);
3352         }
3353         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3354         while (Idx < End) {
3355           auto Loc = ReadSourceLocation(F, Record, Idx);
3356           bool WasSkipping = Record[Idx++];
3357           bool FoundNonSkip = Record[Idx++];
3358           bool FoundElse = Record[Idx++];
3359           ConditionalStack.push_back(
3360               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3361         }
3362         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3363       }
3364       break;
3365 
3366     case PP_COUNTER_VALUE:
3367       if (!Record.empty() && Listener)
3368         Listener->ReadCounter(F, Record[0]);
3369       break;
3370 
3371     case FILE_SORTED_DECLS:
3372       F.FileSortedDecls = (const DeclID *)Blob.data();
3373       F.NumFileSortedDecls = Record[0];
3374       break;
3375 
3376     case SOURCE_LOCATION_OFFSETS: {
3377       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3378       F.LocalNumSLocEntries = Record[0];
3379       unsigned SLocSpaceSize = Record[1];
3380       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3381       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3382           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3383                                               SLocSpaceSize);
3384       if (!F.SLocEntryBaseID) {
3385         Error("ran out of source locations");
3386         break;
3387       }
3388       // Make our entry in the range map. BaseID is negative and growing, so
3389       // we invert it. Because we invert it, though, we need the other end of
3390       // the range.
3391       unsigned RangeStart =
3392           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3393       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3394       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3395 
3396       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3397       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3398       GlobalSLocOffsetMap.insert(
3399           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3400                            - SLocSpaceSize,&F));
3401 
3402       // Initialize the remapping table.
3403       // Invalid stays invalid.
3404       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3405       // This module. Base was 2 when being compiled.
3406       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3407                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3408 
3409       TotalNumSLocEntries += F.LocalNumSLocEntries;
3410       break;
3411     }
3412 
3413     case MODULE_OFFSET_MAP:
3414       F.ModuleOffsetMap = Blob;
3415       break;
3416 
3417     case SOURCE_MANAGER_LINE_TABLE:
3418       if (ParseLineTable(F, Record)) {
3419         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3420         return Failure;
3421       }
3422       break;
3423 
3424     case SOURCE_LOCATION_PRELOADS: {
3425       // Need to transform from the local view (1-based IDs) to the global view,
3426       // which is based off F.SLocEntryBaseID.
3427       if (!F.PreloadSLocEntries.empty()) {
3428         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3429         return Failure;
3430       }
3431 
3432       F.PreloadSLocEntries.swap(Record);
3433       break;
3434     }
3435 
3436     case EXT_VECTOR_DECLS:
3437       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3438         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3439       break;
3440 
3441     case VTABLE_USES:
3442       if (Record.size() % 3 != 0) {
3443         Error("Invalid VTABLE_USES record");
3444         return Failure;
3445       }
3446 
3447       // Later tables overwrite earlier ones.
3448       // FIXME: Modules will have some trouble with this. This is clearly not
3449       // the right way to do this.
3450       VTableUses.clear();
3451 
3452       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3453         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3454         VTableUses.push_back(
3455           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3456         VTableUses.push_back(Record[Idx++]);
3457       }
3458       break;
3459 
3460     case PENDING_IMPLICIT_INSTANTIATIONS:
3461       if (PendingInstantiations.size() % 2 != 0) {
3462         Error("Invalid existing PendingInstantiations");
3463         return Failure;
3464       }
3465 
3466       if (Record.size() % 2 != 0) {
3467         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3468         return Failure;
3469       }
3470 
3471       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3472         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3473         PendingInstantiations.push_back(
3474           ReadSourceLocation(F, Record, I).getRawEncoding());
3475       }
3476       break;
3477 
3478     case SEMA_DECL_REFS:
3479       if (Record.size() != 3) {
3480         Error("Invalid SEMA_DECL_REFS block");
3481         return Failure;
3482       }
3483       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3484         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3485       break;
3486 
3487     case PPD_ENTITIES_OFFSETS: {
3488       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3489       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3490       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3491 
3492       unsigned LocalBasePreprocessedEntityID = Record[0];
3493 
3494       unsigned StartingID;
3495       if (!PP.getPreprocessingRecord())
3496         PP.createPreprocessingRecord();
3497       if (!PP.getPreprocessingRecord()->getExternalSource())
3498         PP.getPreprocessingRecord()->SetExternalSource(*this);
3499       StartingID
3500         = PP.getPreprocessingRecord()
3501             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3502       F.BasePreprocessedEntityID = StartingID;
3503 
3504       if (F.NumPreprocessedEntities > 0) {
3505         // Introduce the global -> local mapping for preprocessed entities in
3506         // this module.
3507         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3508 
3509         // Introduce the local -> global mapping for preprocessed entities in
3510         // this module.
3511         F.PreprocessedEntityRemap.insertOrReplace(
3512           std::make_pair(LocalBasePreprocessedEntityID,
3513             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3514       }
3515 
3516       break;
3517     }
3518 
3519     case PPD_SKIPPED_RANGES: {
3520       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3521       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3522       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3523 
3524       if (!PP.getPreprocessingRecord())
3525         PP.createPreprocessingRecord();
3526       if (!PP.getPreprocessingRecord()->getExternalSource())
3527         PP.getPreprocessingRecord()->SetExternalSource(*this);
3528       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3529           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3530 
3531       if (F.NumPreprocessedSkippedRanges > 0)
3532         GlobalSkippedRangeMap.insert(
3533             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3534       break;
3535     }
3536 
3537     case DECL_UPDATE_OFFSETS:
3538       if (Record.size() % 2 != 0) {
3539         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3540         return Failure;
3541       }
3542       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3543         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3544         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3545 
3546         // If we've already loaded the decl, perform the updates when we finish
3547         // loading this block.
3548         if (Decl *D = GetExistingDecl(ID))
3549           PendingUpdateRecords.push_back(
3550               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3551       }
3552       break;
3553 
3554     case OBJC_CATEGORIES_MAP:
3555       if (F.LocalNumObjCCategoriesInMap != 0) {
3556         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3557         return Failure;
3558       }
3559 
3560       F.LocalNumObjCCategoriesInMap = Record[0];
3561       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3562       break;
3563 
3564     case OBJC_CATEGORIES:
3565       F.ObjCCategories.swap(Record);
3566       break;
3567 
3568     case CUDA_SPECIAL_DECL_REFS:
3569       // Later tables overwrite earlier ones.
3570       // FIXME: Modules will have trouble with this.
3571       CUDASpecialDeclRefs.clear();
3572       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3573         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3574       break;
3575 
3576     case HEADER_SEARCH_TABLE:
3577       F.HeaderFileInfoTableData = Blob.data();
3578       F.LocalNumHeaderFileInfos = Record[1];
3579       if (Record[0]) {
3580         F.HeaderFileInfoTable
3581           = HeaderFileInfoLookupTable::Create(
3582                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3583                    (const unsigned char *)F.HeaderFileInfoTableData,
3584                    HeaderFileInfoTrait(*this, F,
3585                                        &PP.getHeaderSearchInfo(),
3586                                        Blob.data() + Record[2]));
3587 
3588         PP.getHeaderSearchInfo().SetExternalSource(this);
3589         if (!PP.getHeaderSearchInfo().getExternalLookup())
3590           PP.getHeaderSearchInfo().SetExternalLookup(this);
3591       }
3592       break;
3593 
3594     case FP_PRAGMA_OPTIONS:
3595       // Later tables overwrite earlier ones.
3596       FPPragmaOptions.swap(Record);
3597       break;
3598 
3599     case OPENCL_EXTENSIONS:
3600       for (unsigned I = 0, E = Record.size(); I != E; ) {
3601         auto Name = ReadString(Record, I);
3602         auto &Opt = OpenCLExtensions.OptMap[Name];
3603         Opt.Supported = Record[I++] != 0;
3604         Opt.Enabled = Record[I++] != 0;
3605         Opt.Avail = Record[I++];
3606         Opt.Core = Record[I++];
3607       }
3608       break;
3609 
3610     case OPENCL_EXTENSION_TYPES:
3611       for (unsigned I = 0, E = Record.size(); I != E;) {
3612         auto TypeID = static_cast<::TypeID>(Record[I++]);
3613         auto *Type = GetType(TypeID).getTypePtr();
3614         auto NumExt = static_cast<unsigned>(Record[I++]);
3615         for (unsigned II = 0; II != NumExt; ++II) {
3616           auto Ext = ReadString(Record, I);
3617           OpenCLTypeExtMap[Type].insert(Ext);
3618         }
3619       }
3620       break;
3621 
3622     case OPENCL_EXTENSION_DECLS:
3623       for (unsigned I = 0, E = Record.size(); I != E;) {
3624         auto DeclID = static_cast<::DeclID>(Record[I++]);
3625         auto *Decl = GetDecl(DeclID);
3626         auto NumExt = static_cast<unsigned>(Record[I++]);
3627         for (unsigned II = 0; II != NumExt; ++II) {
3628           auto Ext = ReadString(Record, I);
3629           OpenCLDeclExtMap[Decl].insert(Ext);
3630         }
3631       }
3632       break;
3633 
3634     case TENTATIVE_DEFINITIONS:
3635       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3637       break;
3638 
3639     case KNOWN_NAMESPACES:
3640       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3641         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3642       break;
3643 
3644     case UNDEFINED_BUT_USED:
3645       if (UndefinedButUsed.size() % 2 != 0) {
3646         Error("Invalid existing UndefinedButUsed");
3647         return Failure;
3648       }
3649 
3650       if (Record.size() % 2 != 0) {
3651         Error("invalid undefined-but-used record");
3652         return Failure;
3653       }
3654       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3655         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3656         UndefinedButUsed.push_back(
3657             ReadSourceLocation(F, Record, I).getRawEncoding());
3658       }
3659       break;
3660 
3661     case DELETE_EXPRS_TO_ANALYZE:
3662       for (unsigned I = 0, N = Record.size(); I != N;) {
3663         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3664         const uint64_t Count = Record[I++];
3665         DelayedDeleteExprs.push_back(Count);
3666         for (uint64_t C = 0; C < Count; ++C) {
3667           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3668           bool IsArrayForm = Record[I++] == 1;
3669           DelayedDeleteExprs.push_back(IsArrayForm);
3670         }
3671       }
3672       break;
3673 
3674     case IMPORTED_MODULES:
3675       if (!F.isModule()) {
3676         // If we aren't loading a module (which has its own exports), make
3677         // all of the imported modules visible.
3678         // FIXME: Deal with macros-only imports.
3679         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3680           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3681           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3682           if (GlobalID) {
3683             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3684             if (DeserializationListener)
3685               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3686           }
3687         }
3688       }
3689       break;
3690 
3691     case MACRO_OFFSET: {
3692       if (F.LocalNumMacros != 0) {
3693         Error("duplicate MACRO_OFFSET record in AST file");
3694         return Failure;
3695       }
3696       F.MacroOffsets = (const uint32_t *)Blob.data();
3697       F.LocalNumMacros = Record[0];
3698       unsigned LocalBaseMacroID = Record[1];
3699       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3700       F.BaseMacroID = getTotalNumMacros();
3701 
3702       if (F.LocalNumMacros > 0) {
3703         // Introduce the global -> local mapping for macros within this module.
3704         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3705 
3706         // Introduce the local -> global mapping for macros within this module.
3707         F.MacroRemap.insertOrReplace(
3708           std::make_pair(LocalBaseMacroID,
3709                          F.BaseMacroID - LocalBaseMacroID));
3710 
3711         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3712       }
3713       break;
3714     }
3715 
3716     case LATE_PARSED_TEMPLATE:
3717       LateParsedTemplates.emplace_back(
3718           std::piecewise_construct, std::forward_as_tuple(&F),
3719           std::forward_as_tuple(Record.begin(), Record.end()));
3720       break;
3721 
3722     case OPTIMIZE_PRAGMA_OPTIONS:
3723       if (Record.size() != 1) {
3724         Error("invalid pragma optimize record");
3725         return Failure;
3726       }
3727       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3728       break;
3729 
3730     case MSSTRUCT_PRAGMA_OPTIONS:
3731       if (Record.size() != 1) {
3732         Error("invalid pragma ms_struct record");
3733         return Failure;
3734       }
3735       PragmaMSStructState = Record[0];
3736       break;
3737 
3738     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3739       if (Record.size() != 2) {
3740         Error("invalid pragma ms_struct record");
3741         return Failure;
3742       }
3743       PragmaMSPointersToMembersState = Record[0];
3744       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3745       break;
3746 
3747     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3748       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3749         UnusedLocalTypedefNameCandidates.push_back(
3750             getGlobalDeclID(F, Record[I]));
3751       break;
3752 
3753     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3754       if (Record.size() != 1) {
3755         Error("invalid cuda pragma options record");
3756         return Failure;
3757       }
3758       ForceCUDAHostDeviceDepth = Record[0];
3759       break;
3760 
3761     case PACK_PRAGMA_OPTIONS: {
3762       if (Record.size() < 3) {
3763         Error("invalid pragma pack record");
3764         return Failure;
3765       }
3766       PragmaPackCurrentValue = Record[0];
3767       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3768       unsigned NumStackEntries = Record[2];
3769       unsigned Idx = 3;
3770       // Reset the stack when importing a new module.
3771       PragmaPackStack.clear();
3772       for (unsigned I = 0; I < NumStackEntries; ++I) {
3773         PragmaPackStackEntry Entry;
3774         Entry.Value = Record[Idx++];
3775         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3776         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3777         PragmaPackStrings.push_back(ReadString(Record, Idx));
3778         Entry.SlotLabel = PragmaPackStrings.back();
3779         PragmaPackStack.push_back(Entry);
3780       }
3781       break;
3782     }
3783 
3784     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3785       if (Record.size() < 3) {
3786         Error("invalid pragma pack record");
3787         return Failure;
3788       }
3789       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3790       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3791       unsigned NumStackEntries = Record[2];
3792       unsigned Idx = 3;
3793       // Reset the stack when importing a new module.
3794       FpPragmaStack.clear();
3795       for (unsigned I = 0; I < NumStackEntries; ++I) {
3796         FpPragmaStackEntry Entry;
3797         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3798         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3799         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3800         FpPragmaStrings.push_back(ReadString(Record, Idx));
3801         Entry.SlotLabel = FpPragmaStrings.back();
3802         FpPragmaStack.push_back(Entry);
3803       }
3804       break;
3805     }
3806 
3807     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3808       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3809         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3810       break;
3811     }
3812   }
3813 }
3814 
3815 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3816   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3817 
3818   // Additional remapping information.
3819   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3820   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3821   F.ModuleOffsetMap = StringRef();
3822 
3823   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3824   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3825     F.SLocRemap.insert(std::make_pair(0U, 0));
3826     F.SLocRemap.insert(std::make_pair(2U, 1));
3827   }
3828 
3829   // Continuous range maps we may be updating in our module.
3830   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3831   RemapBuilder SLocRemap(F.SLocRemap);
3832   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3833   RemapBuilder MacroRemap(F.MacroRemap);
3834   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3835   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3836   RemapBuilder SelectorRemap(F.SelectorRemap);
3837   RemapBuilder DeclRemap(F.DeclRemap);
3838   RemapBuilder TypeRemap(F.TypeRemap);
3839 
3840   while (Data < DataEnd) {
3841     // FIXME: Looking up dependency modules by filename is horrible. Let's
3842     // start fixing this with prebuilt, explicit and implicit modules and see
3843     // how it goes...
3844     using namespace llvm::support;
3845     ModuleKind Kind = static_cast<ModuleKind>(
3846       endian::readNext<uint8_t, little, unaligned>(Data));
3847     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3848     StringRef Name = StringRef((const char*)Data, Len);
3849     Data += Len;
3850     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3851                               Kind == MK_ImplicitModule
3852                           ? ModuleMgr.lookupByModuleName(Name)
3853                           : ModuleMgr.lookupByFileName(Name));
3854     if (!OM) {
3855       std::string Msg =
3856           "SourceLocation remap refers to unknown module, cannot find ";
3857       Msg.append(std::string(Name));
3858       Error(Msg);
3859       return;
3860     }
3861 
3862     uint32_t SLocOffset =
3863         endian::readNext<uint32_t, little, unaligned>(Data);
3864     uint32_t IdentifierIDOffset =
3865         endian::readNext<uint32_t, little, unaligned>(Data);
3866     uint32_t MacroIDOffset =
3867         endian::readNext<uint32_t, little, unaligned>(Data);
3868     uint32_t PreprocessedEntityIDOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t SubmoduleIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t SelectorIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t DeclIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t TypeIndexOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878 
3879     uint32_t None = std::numeric_limits<uint32_t>::max();
3880 
3881     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3882                          RemapBuilder &Remap) {
3883       if (Offset != None)
3884         Remap.insert(std::make_pair(Offset,
3885                                     static_cast<int>(BaseOffset - Offset)));
3886     };
3887     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3888     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3889     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3890     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3891               PreprocessedEntityRemap);
3892     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3893     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3894     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3895     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3896 
3897     // Global -> local mappings.
3898     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3899   }
3900 }
3901 
3902 ASTReader::ASTReadResult
3903 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3904                                   const ModuleFile *ImportedBy,
3905                                   unsigned ClientLoadCapabilities) {
3906   unsigned Idx = 0;
3907   F.ModuleMapPath = ReadPath(F, Record, Idx);
3908 
3909   // Try to resolve ModuleName in the current header search context and
3910   // verify that it is found in the same module map file as we saved. If the
3911   // top-level AST file is a main file, skip this check because there is no
3912   // usable header search context.
3913   assert(!F.ModuleName.empty() &&
3914          "MODULE_NAME should come before MODULE_MAP_FILE");
3915   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3916     // An implicitly-loaded module file should have its module listed in some
3917     // module map file that we've already loaded.
3918     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3919     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3920     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3921     // Don't emit module relocation error if we have -fno-validate-pch
3922     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3923       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3924         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3925           // This module was defined by an imported (explicit) module.
3926           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3927                                                << ASTFE->getName();
3928         } else {
3929           // This module was built with a different module map.
3930           Diag(diag::err_imported_module_not_found)
3931               << F.ModuleName << F.FileName
3932               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3933               << !ImportedBy;
3934           // In case it was imported by a PCH, there's a chance the user is
3935           // just missing to include the search path to the directory containing
3936           // the modulemap.
3937           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3938             Diag(diag::note_imported_by_pch_module_not_found)
3939                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3940         }
3941       }
3942       return OutOfDate;
3943     }
3944 
3945     assert(M && M->Name == F.ModuleName && "found module with different name");
3946 
3947     // Check the primary module map file.
3948     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3949     if (!StoredModMap || *StoredModMap != ModMap) {
3950       assert(ModMap && "found module is missing module map file");
3951       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3952              "top-level import should be verified");
3953       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3954       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3955         Diag(diag::err_imported_module_modmap_changed)
3956             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3957             << ModMap->getName() << F.ModuleMapPath << NotImported;
3958       return OutOfDate;
3959     }
3960 
3961     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3962     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3963       // FIXME: we should use input files rather than storing names.
3964       std::string Filename = ReadPath(F, Record, Idx);
3965       auto F = FileMgr.getFile(Filename, false, false);
3966       if (!F) {
3967         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3968           Error("could not find file '" + Filename +"' referenced by AST file");
3969         return OutOfDate;
3970       }
3971       AdditionalStoredMaps.insert(*F);
3972     }
3973 
3974     // Check any additional module map files (e.g. module.private.modulemap)
3975     // that are not in the pcm.
3976     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3977       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3978         // Remove files that match
3979         // Note: SmallPtrSet::erase is really remove
3980         if (!AdditionalStoredMaps.erase(ModMap)) {
3981           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3982             Diag(diag::err_module_different_modmap)
3983               << F.ModuleName << /*new*/0 << ModMap->getName();
3984           return OutOfDate;
3985         }
3986       }
3987     }
3988 
3989     // Check any additional module map files that are in the pcm, but not
3990     // found in header search. Cases that match are already removed.
3991     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3992       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3993         Diag(diag::err_module_different_modmap)
3994           << F.ModuleName << /*not new*/1 << ModMap->getName();
3995       return OutOfDate;
3996     }
3997   }
3998 
3999   if (Listener)
4000     Listener->ReadModuleMapFile(F.ModuleMapPath);
4001   return Success;
4002 }
4003 
4004 /// Move the given method to the back of the global list of methods.
4005 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4006   // Find the entry for this selector in the method pool.
4007   Sema::GlobalMethodPool::iterator Known
4008     = S.MethodPool.find(Method->getSelector());
4009   if (Known == S.MethodPool.end())
4010     return;
4011 
4012   // Retrieve the appropriate method list.
4013   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4014                                                     : Known->second.second;
4015   bool Found = false;
4016   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4017     if (!Found) {
4018       if (List->getMethod() == Method) {
4019         Found = true;
4020       } else {
4021         // Keep searching.
4022         continue;
4023       }
4024     }
4025 
4026     if (List->getNext())
4027       List->setMethod(List->getNext()->getMethod());
4028     else
4029       List->setMethod(Method);
4030   }
4031 }
4032 
4033 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4034   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4035   for (Decl *D : Names) {
4036     bool wasHidden = !D->isUnconditionallyVisible();
4037     D->setVisibleDespiteOwningModule();
4038 
4039     if (wasHidden && SemaObj) {
4040       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4041         moveMethodToBackOfGlobalList(*SemaObj, Method);
4042       }
4043     }
4044   }
4045 }
4046 
4047 void ASTReader::makeModuleVisible(Module *Mod,
4048                                   Module::NameVisibilityKind NameVisibility,
4049                                   SourceLocation ImportLoc) {
4050   llvm::SmallPtrSet<Module *, 4> Visited;
4051   SmallVector<Module *, 4> Stack;
4052   Stack.push_back(Mod);
4053   while (!Stack.empty()) {
4054     Mod = Stack.pop_back_val();
4055 
4056     if (NameVisibility <= Mod->NameVisibility) {
4057       // This module already has this level of visibility (or greater), so
4058       // there is nothing more to do.
4059       continue;
4060     }
4061 
4062     if (Mod->isUnimportable()) {
4063       // Modules that aren't importable cannot be made visible.
4064       continue;
4065     }
4066 
4067     // Update the module's name visibility.
4068     Mod->NameVisibility = NameVisibility;
4069 
4070     // If we've already deserialized any names from this module,
4071     // mark them as visible.
4072     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4073     if (Hidden != HiddenNamesMap.end()) {
4074       auto HiddenNames = std::move(*Hidden);
4075       HiddenNamesMap.erase(Hidden);
4076       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4077       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4078              "making names visible added hidden names");
4079     }
4080 
4081     // Push any exported modules onto the stack to be marked as visible.
4082     SmallVector<Module *, 16> Exports;
4083     Mod->getExportedModules(Exports);
4084     for (SmallVectorImpl<Module *>::iterator
4085            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4086       Module *Exported = *I;
4087       if (Visited.insert(Exported).second)
4088         Stack.push_back(Exported);
4089     }
4090   }
4091 }
4092 
4093 /// We've merged the definition \p MergedDef into the existing definition
4094 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4095 /// visible.
4096 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4097                                           NamedDecl *MergedDef) {
4098   if (!Def->isUnconditionallyVisible()) {
4099     // If MergedDef is visible or becomes visible, make the definition visible.
4100     if (MergedDef->isUnconditionallyVisible())
4101       Def->setVisibleDespiteOwningModule();
4102     else {
4103       getContext().mergeDefinitionIntoModule(
4104           Def, MergedDef->getImportedOwningModule(),
4105           /*NotifyListeners*/ false);
4106       PendingMergedDefinitionsToDeduplicate.insert(Def);
4107     }
4108   }
4109 }
4110 
4111 bool ASTReader::loadGlobalIndex() {
4112   if (GlobalIndex)
4113     return false;
4114 
4115   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4116       !PP.getLangOpts().Modules)
4117     return true;
4118 
4119   // Try to load the global index.
4120   TriedLoadingGlobalIndex = true;
4121   StringRef ModuleCachePath
4122     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4123   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4124       GlobalModuleIndex::readIndex(ModuleCachePath);
4125   if (llvm::Error Err = std::move(Result.second)) {
4126     assert(!Result.first);
4127     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4128     return true;
4129   }
4130 
4131   GlobalIndex.reset(Result.first);
4132   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4133   return false;
4134 }
4135 
4136 bool ASTReader::isGlobalIndexUnavailable() const {
4137   return PP.getLangOpts().Modules && UseGlobalIndex &&
4138          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4139 }
4140 
4141 static void updateModuleTimestamp(ModuleFile &MF) {
4142   // Overwrite the timestamp file contents so that file's mtime changes.
4143   std::string TimestampFilename = MF.getTimestampFilename();
4144   std::error_code EC;
4145   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4146   if (EC)
4147     return;
4148   OS << "Timestamp file\n";
4149   OS.close();
4150   OS.clear_error(); // Avoid triggering a fatal error.
4151 }
4152 
4153 /// Given a cursor at the start of an AST file, scan ahead and drop the
4154 /// cursor into the start of the given block ID, returning false on success and
4155 /// true on failure.
4156 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4157   while (true) {
4158     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4159     if (!MaybeEntry) {
4160       // FIXME this drops errors on the floor.
4161       consumeError(MaybeEntry.takeError());
4162       return true;
4163     }
4164     llvm::BitstreamEntry Entry = MaybeEntry.get();
4165 
4166     switch (Entry.Kind) {
4167     case llvm::BitstreamEntry::Error:
4168     case llvm::BitstreamEntry::EndBlock:
4169       return true;
4170 
4171     case llvm::BitstreamEntry::Record:
4172       // Ignore top-level records.
4173       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4174         break;
4175       else {
4176         // FIXME this drops errors on the floor.
4177         consumeError(Skipped.takeError());
4178         return true;
4179       }
4180 
4181     case llvm::BitstreamEntry::SubBlock:
4182       if (Entry.ID == BlockID) {
4183         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4184           // FIXME this drops the error on the floor.
4185           consumeError(std::move(Err));
4186           return true;
4187         }
4188         // Found it!
4189         return false;
4190       }
4191 
4192       if (llvm::Error Err = Cursor.SkipBlock()) {
4193         // FIXME this drops the error on the floor.
4194         consumeError(std::move(Err));
4195         return true;
4196       }
4197     }
4198   }
4199 }
4200 
4201 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4202                                             ModuleKind Type,
4203                                             SourceLocation ImportLoc,
4204                                             unsigned ClientLoadCapabilities,
4205                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4206   llvm::SaveAndRestore<SourceLocation>
4207     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4208 
4209   // Defer any pending actions until we get to the end of reading the AST file.
4210   Deserializing AnASTFile(this);
4211 
4212   // Bump the generation number.
4213   unsigned PreviousGeneration = 0;
4214   if (ContextObj)
4215     PreviousGeneration = incrementGeneration(*ContextObj);
4216 
4217   unsigned NumModules = ModuleMgr.size();
4218   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4219     assert(ReadResult && "expected to return error");
4220     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4221                             PP.getLangOpts().Modules
4222                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4223                                 : nullptr);
4224 
4225     // If we find that any modules are unusable, the global index is going
4226     // to be out-of-date. Just remove it.
4227     GlobalIndex.reset();
4228     ModuleMgr.setGlobalIndex(nullptr);
4229     return ReadResult;
4230   };
4231 
4232   SmallVector<ImportedModule, 4> Loaded;
4233   switch (ASTReadResult ReadResult =
4234               ReadASTCore(FileName, Type, ImportLoc,
4235                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4236                           ASTFileSignature(), ClientLoadCapabilities)) {
4237   case Failure:
4238   case Missing:
4239   case OutOfDate:
4240   case VersionMismatch:
4241   case ConfigurationMismatch:
4242   case HadErrors:
4243     return removeModulesAndReturn(ReadResult);
4244   case Success:
4245     break;
4246   }
4247 
4248   // Here comes stuff that we only do once the entire chain is loaded.
4249 
4250   // Load the AST blocks of all of the modules that we loaded.  We can still
4251   // hit errors parsing the ASTs at this point.
4252   for (ImportedModule &M : Loaded) {
4253     ModuleFile &F = *M.Mod;
4254 
4255     // Read the AST block.
4256     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4257       return removeModulesAndReturn(Result);
4258 
4259     // The AST block should always have a definition for the main module.
4260     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4261       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4262       return removeModulesAndReturn(Failure);
4263     }
4264 
4265     // Read the extension blocks.
4266     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4267       if (ASTReadResult Result = ReadExtensionBlock(F))
4268         return removeModulesAndReturn(Result);
4269     }
4270 
4271     // Once read, set the ModuleFile bit base offset and update the size in
4272     // bits of all files we've seen.
4273     F.GlobalBitOffset = TotalModulesSizeInBits;
4274     TotalModulesSizeInBits += F.SizeInBits;
4275     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4276   }
4277 
4278   // Preload source locations and interesting indentifiers.
4279   for (ImportedModule &M : Loaded) {
4280     ModuleFile &F = *M.Mod;
4281 
4282     // Preload SLocEntries.
4283     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4284       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4285       // Load it through the SourceManager and don't call ReadSLocEntry()
4286       // directly because the entry may have already been loaded in which case
4287       // calling ReadSLocEntry() directly would trigger an assertion in
4288       // SourceManager.
4289       SourceMgr.getLoadedSLocEntryByID(Index);
4290     }
4291 
4292     // Map the original source file ID into the ID space of the current
4293     // compilation.
4294     if (F.OriginalSourceFileID.isValid()) {
4295       F.OriginalSourceFileID = FileID::get(
4296           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4297     }
4298 
4299     // Preload all the pending interesting identifiers by marking them out of
4300     // date.
4301     for (auto Offset : F.PreloadIdentifierOffsets) {
4302       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4303           F.IdentifierTableData + Offset);
4304 
4305       ASTIdentifierLookupTrait Trait(*this, F);
4306       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4307       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4308       auto &II = PP.getIdentifierTable().getOwn(Key);
4309       II.setOutOfDate(true);
4310 
4311       // Mark this identifier as being from an AST file so that we can track
4312       // whether we need to serialize it.
4313       markIdentifierFromAST(*this, II);
4314 
4315       // Associate the ID with the identifier so that the writer can reuse it.
4316       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4317       SetIdentifierInfo(ID, &II);
4318     }
4319   }
4320 
4321   // Setup the import locations and notify the module manager that we've
4322   // committed to these module files.
4323   for (ImportedModule &M : Loaded) {
4324     ModuleFile &F = *M.Mod;
4325 
4326     ModuleMgr.moduleFileAccepted(&F);
4327 
4328     // Set the import location.
4329     F.DirectImportLoc = ImportLoc;
4330     // FIXME: We assume that locations from PCH / preamble do not need
4331     // any translation.
4332     if (!M.ImportedBy)
4333       F.ImportLoc = M.ImportLoc;
4334     else
4335       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4336   }
4337 
4338   if (!PP.getLangOpts().CPlusPlus ||
4339       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4340        Type != MK_PrebuiltModule)) {
4341     // Mark all of the identifiers in the identifier table as being out of date,
4342     // so that various accessors know to check the loaded modules when the
4343     // identifier is used.
4344     //
4345     // For C++ modules, we don't need information on many identifiers (just
4346     // those that provide macros or are poisoned), so we mark all of
4347     // the interesting ones via PreloadIdentifierOffsets.
4348     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4349                                 IdEnd = PP.getIdentifierTable().end();
4350          Id != IdEnd; ++Id)
4351       Id->second->setOutOfDate(true);
4352   }
4353   // Mark selectors as out of date.
4354   for (auto Sel : SelectorGeneration)
4355     SelectorOutOfDate[Sel.first] = true;
4356 
4357   // Resolve any unresolved module exports.
4358   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4359     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4360     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4361     Module *ResolvedMod = getSubmodule(GlobalID);
4362 
4363     switch (Unresolved.Kind) {
4364     case UnresolvedModuleRef::Conflict:
4365       if (ResolvedMod) {
4366         Module::Conflict Conflict;
4367         Conflict.Other = ResolvedMod;
4368         Conflict.Message = Unresolved.String.str();
4369         Unresolved.Mod->Conflicts.push_back(Conflict);
4370       }
4371       continue;
4372 
4373     case UnresolvedModuleRef::Import:
4374       if (ResolvedMod)
4375         Unresolved.Mod->Imports.insert(ResolvedMod);
4376       continue;
4377 
4378     case UnresolvedModuleRef::Export:
4379       if (ResolvedMod || Unresolved.IsWildcard)
4380         Unresolved.Mod->Exports.push_back(
4381           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4382       continue;
4383     }
4384   }
4385   UnresolvedModuleRefs.clear();
4386 
4387   if (Imported)
4388     Imported->append(ImportedModules.begin(),
4389                      ImportedModules.end());
4390 
4391   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4392   // Might be unnecessary as use declarations are only used to build the
4393   // module itself.
4394 
4395   if (ContextObj)
4396     InitializeContext();
4397 
4398   if (SemaObj)
4399     UpdateSema();
4400 
4401   if (DeserializationListener)
4402     DeserializationListener->ReaderInitialized(this);
4403 
4404   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4405   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4406     // If this AST file is a precompiled preamble, then set the
4407     // preamble file ID of the source manager to the file source file
4408     // from which the preamble was built.
4409     if (Type == MK_Preamble) {
4410       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4411     } else if (Type == MK_MainFile) {
4412       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4413     }
4414   }
4415 
4416   // For any Objective-C class definitions we have already loaded, make sure
4417   // that we load any additional categories.
4418   if (ContextObj) {
4419     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4420       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4421                          ObjCClassesLoaded[I],
4422                          PreviousGeneration);
4423     }
4424   }
4425 
4426   if (PP.getHeaderSearchInfo()
4427           .getHeaderSearchOpts()
4428           .ModulesValidateOncePerBuildSession) {
4429     // Now we are certain that the module and all modules it depends on are
4430     // up to date.  Create or update timestamp files for modules that are
4431     // located in the module cache (not for PCH files that could be anywhere
4432     // in the filesystem).
4433     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4434       ImportedModule &M = Loaded[I];
4435       if (M.Mod->Kind == MK_ImplicitModule) {
4436         updateModuleTimestamp(*M.Mod);
4437       }
4438     }
4439   }
4440 
4441   return Success;
4442 }
4443 
4444 static ASTFileSignature readASTFileSignature(StringRef PCH);
4445 
4446 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4447 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4448   // FIXME checking magic headers is done in other places such as
4449   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4450   // always done the same. Unify it all with a helper.
4451   if (!Stream.canSkipToPos(4))
4452     return llvm::createStringError(std::errc::illegal_byte_sequence,
4453                                    "file too small to contain AST file magic");
4454   for (unsigned C : {'C', 'P', 'C', 'H'})
4455     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4456       if (Res.get() != C)
4457         return llvm::createStringError(
4458             std::errc::illegal_byte_sequence,
4459             "file doesn't start with AST file magic");
4460     } else
4461       return Res.takeError();
4462   return llvm::Error::success();
4463 }
4464 
4465 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4466   switch (Kind) {
4467   case MK_PCH:
4468     return 0; // PCH
4469   case MK_ImplicitModule:
4470   case MK_ExplicitModule:
4471   case MK_PrebuiltModule:
4472     return 1; // module
4473   case MK_MainFile:
4474   case MK_Preamble:
4475     return 2; // main source file
4476   }
4477   llvm_unreachable("unknown module kind");
4478 }
4479 
4480 ASTReader::ASTReadResult
4481 ASTReader::ReadASTCore(StringRef FileName,
4482                        ModuleKind Type,
4483                        SourceLocation ImportLoc,
4484                        ModuleFile *ImportedBy,
4485                        SmallVectorImpl<ImportedModule> &Loaded,
4486                        off_t ExpectedSize, time_t ExpectedModTime,
4487                        ASTFileSignature ExpectedSignature,
4488                        unsigned ClientLoadCapabilities) {
4489   ModuleFile *M;
4490   std::string ErrorStr;
4491   ModuleManager::AddModuleResult AddResult
4492     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4493                           getGeneration(), ExpectedSize, ExpectedModTime,
4494                           ExpectedSignature, readASTFileSignature,
4495                           M, ErrorStr);
4496 
4497   switch (AddResult) {
4498   case ModuleManager::AlreadyLoaded:
4499     Diag(diag::remark_module_import)
4500         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4501         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4502     return Success;
4503 
4504   case ModuleManager::NewlyLoaded:
4505     // Load module file below.
4506     break;
4507 
4508   case ModuleManager::Missing:
4509     // The module file was missing; if the client can handle that, return
4510     // it.
4511     if (ClientLoadCapabilities & ARR_Missing)
4512       return Missing;
4513 
4514     // Otherwise, return an error.
4515     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4516                                           << FileName << !ErrorStr.empty()
4517                                           << ErrorStr;
4518     return Failure;
4519 
4520   case ModuleManager::OutOfDate:
4521     // We couldn't load the module file because it is out-of-date. If the
4522     // client can handle out-of-date, return it.
4523     if (ClientLoadCapabilities & ARR_OutOfDate)
4524       return OutOfDate;
4525 
4526     // Otherwise, return an error.
4527     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4528                                             << FileName << !ErrorStr.empty()
4529                                             << ErrorStr;
4530     return Failure;
4531   }
4532 
4533   assert(M && "Missing module file");
4534 
4535   bool ShouldFinalizePCM = false;
4536   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4537     auto &MC = getModuleManager().getModuleCache();
4538     if (ShouldFinalizePCM)
4539       MC.finalizePCM(FileName);
4540     else
4541       MC.tryToDropPCM(FileName);
4542   });
4543   ModuleFile &F = *M;
4544   BitstreamCursor &Stream = F.Stream;
4545   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4546   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4547 
4548   // Sniff for the signature.
4549   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4550     Diag(diag::err_module_file_invalid)
4551         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4552     return Failure;
4553   }
4554 
4555   // This is used for compatibility with older PCH formats.
4556   bool HaveReadControlBlock = false;
4557   while (true) {
4558     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4559     if (!MaybeEntry) {
4560       Error(MaybeEntry.takeError());
4561       return Failure;
4562     }
4563     llvm::BitstreamEntry Entry = MaybeEntry.get();
4564 
4565     switch (Entry.Kind) {
4566     case llvm::BitstreamEntry::Error:
4567     case llvm::BitstreamEntry::Record:
4568     case llvm::BitstreamEntry::EndBlock:
4569       Error("invalid record at top-level of AST file");
4570       return Failure;
4571 
4572     case llvm::BitstreamEntry::SubBlock:
4573       break;
4574     }
4575 
4576     switch (Entry.ID) {
4577     case CONTROL_BLOCK_ID:
4578       HaveReadControlBlock = true;
4579       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4580       case Success:
4581         // Check that we didn't try to load a non-module AST file as a module.
4582         //
4583         // FIXME: Should we also perform the converse check? Loading a module as
4584         // a PCH file sort of works, but it's a bit wonky.
4585         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4586              Type == MK_PrebuiltModule) &&
4587             F.ModuleName.empty()) {
4588           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4589           if (Result != OutOfDate ||
4590               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4591             Diag(diag::err_module_file_not_module) << FileName;
4592           return Result;
4593         }
4594         break;
4595 
4596       case Failure: return Failure;
4597       case Missing: return Missing;
4598       case OutOfDate: return OutOfDate;
4599       case VersionMismatch: return VersionMismatch;
4600       case ConfigurationMismatch: return ConfigurationMismatch;
4601       case HadErrors: return HadErrors;
4602       }
4603       break;
4604 
4605     case AST_BLOCK_ID:
4606       if (!HaveReadControlBlock) {
4607         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4608           Diag(diag::err_pch_version_too_old);
4609         return VersionMismatch;
4610       }
4611 
4612       // Record that we've loaded this module.
4613       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4614       ShouldFinalizePCM = true;
4615       return Success;
4616 
4617     case UNHASHED_CONTROL_BLOCK_ID:
4618       // This block is handled using look-ahead during ReadControlBlock.  We
4619       // shouldn't get here!
4620       Error("malformed block record in AST file");
4621       return Failure;
4622 
4623     default:
4624       if (llvm::Error Err = Stream.SkipBlock()) {
4625         Error(std::move(Err));
4626         return Failure;
4627       }
4628       break;
4629     }
4630   }
4631 
4632   llvm_unreachable("unexpected break; expected return");
4633 }
4634 
4635 ASTReader::ASTReadResult
4636 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4637                                     unsigned ClientLoadCapabilities) {
4638   const HeaderSearchOptions &HSOpts =
4639       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4640   bool AllowCompatibleConfigurationMismatch =
4641       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4642 
4643   ASTReadResult Result = readUnhashedControlBlockImpl(
4644       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4645       Listener.get(),
4646       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4647 
4648   // If F was directly imported by another module, it's implicitly validated by
4649   // the importing module.
4650   if (DisableValidation || WasImportedBy ||
4651       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4652     return Success;
4653 
4654   if (Result == Failure) {
4655     Error("malformed block record in AST file");
4656     return Failure;
4657   }
4658 
4659   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4660     // If this module has already been finalized in the ModuleCache, we're stuck
4661     // with it; we can only load a single version of each module.
4662     //
4663     // This can happen when a module is imported in two contexts: in one, as a
4664     // user module; in another, as a system module (due to an import from
4665     // another module marked with the [system] flag).  It usually indicates a
4666     // bug in the module map: this module should also be marked with [system].
4667     //
4668     // If -Wno-system-headers (the default), and the first import is as a
4669     // system module, then validation will fail during the as-user import,
4670     // since -Werror flags won't have been validated.  However, it's reasonable
4671     // to treat this consistently as a system module.
4672     //
4673     // If -Wsystem-headers, the PCM on disk was built with
4674     // -Wno-system-headers, and the first import is as a user module, then
4675     // validation will fail during the as-system import since the PCM on disk
4676     // doesn't guarantee that -Werror was respected.  However, the -Werror
4677     // flags were checked during the initial as-user import.
4678     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4679       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4680       return Success;
4681     }
4682   }
4683 
4684   return Result;
4685 }
4686 
4687 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4688     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4689     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4690     bool ValidateDiagnosticOptions) {
4691   // Initialize a stream.
4692   BitstreamCursor Stream(StreamData);
4693 
4694   // Sniff for the signature.
4695   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4696     // FIXME this drops the error on the floor.
4697     consumeError(std::move(Err));
4698     return Failure;
4699   }
4700 
4701   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4702   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4703     return Failure;
4704 
4705   // Read all of the records in the options block.
4706   RecordData Record;
4707   ASTReadResult Result = Success;
4708   while (true) {
4709     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4710     if (!MaybeEntry) {
4711       // FIXME this drops the error on the floor.
4712       consumeError(MaybeEntry.takeError());
4713       return Failure;
4714     }
4715     llvm::BitstreamEntry Entry = MaybeEntry.get();
4716 
4717     switch (Entry.Kind) {
4718     case llvm::BitstreamEntry::Error:
4719     case llvm::BitstreamEntry::SubBlock:
4720       return Failure;
4721 
4722     case llvm::BitstreamEntry::EndBlock:
4723       return Result;
4724 
4725     case llvm::BitstreamEntry::Record:
4726       // The interesting case.
4727       break;
4728     }
4729 
4730     // Read and process a record.
4731     Record.clear();
4732     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4733     if (!MaybeRecordType) {
4734       // FIXME this drops the error.
4735       return Failure;
4736     }
4737     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4738     case SIGNATURE:
4739       if (F)
4740         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4741       break;
4742     case AST_BLOCK_HASH:
4743       if (F)
4744         F->ASTBlockHash =
4745             ASTFileSignature::create(Record.begin(), Record.end());
4746       break;
4747     case DIAGNOSTIC_OPTIONS: {
4748       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4749       if (Listener && ValidateDiagnosticOptions &&
4750           !AllowCompatibleConfigurationMismatch &&
4751           ParseDiagnosticOptions(Record, Complain, *Listener))
4752         Result = OutOfDate; // Don't return early.  Read the signature.
4753       break;
4754     }
4755     case DIAG_PRAGMA_MAPPINGS:
4756       if (!F)
4757         break;
4758       if (F->PragmaDiagMappings.empty())
4759         F->PragmaDiagMappings.swap(Record);
4760       else
4761         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4762                                      Record.begin(), Record.end());
4763       break;
4764     }
4765   }
4766 }
4767 
4768 /// Parse a record and blob containing module file extension metadata.
4769 static bool parseModuleFileExtensionMetadata(
4770               const SmallVectorImpl<uint64_t> &Record,
4771               StringRef Blob,
4772               ModuleFileExtensionMetadata &Metadata) {
4773   if (Record.size() < 4) return true;
4774 
4775   Metadata.MajorVersion = Record[0];
4776   Metadata.MinorVersion = Record[1];
4777 
4778   unsigned BlockNameLen = Record[2];
4779   unsigned UserInfoLen = Record[3];
4780 
4781   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4782 
4783   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4784   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4785                                   Blob.data() + BlockNameLen + UserInfoLen);
4786   return false;
4787 }
4788 
4789 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4790   BitstreamCursor &Stream = F.Stream;
4791 
4792   RecordData Record;
4793   while (true) {
4794     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4795     if (!MaybeEntry) {
4796       Error(MaybeEntry.takeError());
4797       return Failure;
4798     }
4799     llvm::BitstreamEntry Entry = MaybeEntry.get();
4800 
4801     switch (Entry.Kind) {
4802     case llvm::BitstreamEntry::SubBlock:
4803       if (llvm::Error Err = Stream.SkipBlock()) {
4804         Error(std::move(Err));
4805         return Failure;
4806       }
4807       continue;
4808 
4809     case llvm::BitstreamEntry::EndBlock:
4810       return Success;
4811 
4812     case llvm::BitstreamEntry::Error:
4813       return HadErrors;
4814 
4815     case llvm::BitstreamEntry::Record:
4816       break;
4817     }
4818 
4819     Record.clear();
4820     StringRef Blob;
4821     Expected<unsigned> MaybeRecCode =
4822         Stream.readRecord(Entry.ID, Record, &Blob);
4823     if (!MaybeRecCode) {
4824       Error(MaybeRecCode.takeError());
4825       return Failure;
4826     }
4827     switch (MaybeRecCode.get()) {
4828     case EXTENSION_METADATA: {
4829       ModuleFileExtensionMetadata Metadata;
4830       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4831         Error("malformed EXTENSION_METADATA in AST file");
4832         return Failure;
4833       }
4834 
4835       // Find a module file extension with this block name.
4836       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4837       if (Known == ModuleFileExtensions.end()) break;
4838 
4839       // Form a reader.
4840       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4841                                                              F, Stream)) {
4842         F.ExtensionReaders.push_back(std::move(Reader));
4843       }
4844 
4845       break;
4846     }
4847     }
4848   }
4849 
4850   return Success;
4851 }
4852 
4853 void ASTReader::InitializeContext() {
4854   assert(ContextObj && "no context to initialize");
4855   ASTContext &Context = *ContextObj;
4856 
4857   // If there's a listener, notify them that we "read" the translation unit.
4858   if (DeserializationListener)
4859     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4860                                       Context.getTranslationUnitDecl());
4861 
4862   // FIXME: Find a better way to deal with collisions between these
4863   // built-in types. Right now, we just ignore the problem.
4864 
4865   // Load the special types.
4866   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4867     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4868       if (!Context.CFConstantStringTypeDecl)
4869         Context.setCFConstantStringType(GetType(String));
4870     }
4871 
4872     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4873       QualType FileType = GetType(File);
4874       if (FileType.isNull()) {
4875         Error("FILE type is NULL");
4876         return;
4877       }
4878 
4879       if (!Context.FILEDecl) {
4880         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4881           Context.setFILEDecl(Typedef->getDecl());
4882         else {
4883           const TagType *Tag = FileType->getAs<TagType>();
4884           if (!Tag) {
4885             Error("Invalid FILE type in AST file");
4886             return;
4887           }
4888           Context.setFILEDecl(Tag->getDecl());
4889         }
4890       }
4891     }
4892 
4893     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4894       QualType Jmp_bufType = GetType(Jmp_buf);
4895       if (Jmp_bufType.isNull()) {
4896         Error("jmp_buf type is NULL");
4897         return;
4898       }
4899 
4900       if (!Context.jmp_bufDecl) {
4901         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4902           Context.setjmp_bufDecl(Typedef->getDecl());
4903         else {
4904           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4905           if (!Tag) {
4906             Error("Invalid jmp_buf type in AST file");
4907             return;
4908           }
4909           Context.setjmp_bufDecl(Tag->getDecl());
4910         }
4911       }
4912     }
4913 
4914     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4915       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4916       if (Sigjmp_bufType.isNull()) {
4917         Error("sigjmp_buf type is NULL");
4918         return;
4919       }
4920 
4921       if (!Context.sigjmp_bufDecl) {
4922         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4923           Context.setsigjmp_bufDecl(Typedef->getDecl());
4924         else {
4925           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4926           assert(Tag && "Invalid sigjmp_buf type in AST file");
4927           Context.setsigjmp_bufDecl(Tag->getDecl());
4928         }
4929       }
4930     }
4931 
4932     if (unsigned ObjCIdRedef
4933           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4934       if (Context.ObjCIdRedefinitionType.isNull())
4935         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4936     }
4937 
4938     if (unsigned ObjCClassRedef
4939           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4940       if (Context.ObjCClassRedefinitionType.isNull())
4941         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4942     }
4943 
4944     if (unsigned ObjCSelRedef
4945           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4946       if (Context.ObjCSelRedefinitionType.isNull())
4947         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4948     }
4949 
4950     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4951       QualType Ucontext_tType = GetType(Ucontext_t);
4952       if (Ucontext_tType.isNull()) {
4953         Error("ucontext_t type is NULL");
4954         return;
4955       }
4956 
4957       if (!Context.ucontext_tDecl) {
4958         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4959           Context.setucontext_tDecl(Typedef->getDecl());
4960         else {
4961           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4962           assert(Tag && "Invalid ucontext_t type in AST file");
4963           Context.setucontext_tDecl(Tag->getDecl());
4964         }
4965       }
4966     }
4967   }
4968 
4969   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4970 
4971   // If there were any CUDA special declarations, deserialize them.
4972   if (!CUDASpecialDeclRefs.empty()) {
4973     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4974     Context.setcudaConfigureCallDecl(
4975                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4976   }
4977 
4978   // Re-export any modules that were imported by a non-module AST file.
4979   // FIXME: This does not make macro-only imports visible again.
4980   for (auto &Import : ImportedModules) {
4981     if (Module *Imported = getSubmodule(Import.ID)) {
4982       makeModuleVisible(Imported, Module::AllVisible,
4983                         /*ImportLoc=*/Import.ImportLoc);
4984       if (Import.ImportLoc.isValid())
4985         PP.makeModuleVisible(Imported, Import.ImportLoc);
4986       // This updates visibility for Preprocessor only. For Sema, which can be
4987       // nullptr here, we do the same later, in UpdateSema().
4988     }
4989   }
4990 }
4991 
4992 void ASTReader::finalizeForWriting() {
4993   // Nothing to do for now.
4994 }
4995 
4996 /// Reads and return the signature record from \p PCH's control block, or
4997 /// else returns 0.
4998 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4999   BitstreamCursor Stream(PCH);
5000   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5001     // FIXME this drops the error on the floor.
5002     consumeError(std::move(Err));
5003     return ASTFileSignature();
5004   }
5005 
5006   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5007   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5008     return ASTFileSignature();
5009 
5010   // Scan for SIGNATURE inside the diagnostic options block.
5011   ASTReader::RecordData Record;
5012   while (true) {
5013     Expected<llvm::BitstreamEntry> MaybeEntry =
5014         Stream.advanceSkippingSubblocks();
5015     if (!MaybeEntry) {
5016       // FIXME this drops the error on the floor.
5017       consumeError(MaybeEntry.takeError());
5018       return ASTFileSignature();
5019     }
5020     llvm::BitstreamEntry Entry = MaybeEntry.get();
5021 
5022     if (Entry.Kind != llvm::BitstreamEntry::Record)
5023       return ASTFileSignature();
5024 
5025     Record.clear();
5026     StringRef Blob;
5027     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5028     if (!MaybeRecord) {
5029       // FIXME this drops the error on the floor.
5030       consumeError(MaybeRecord.takeError());
5031       return ASTFileSignature();
5032     }
5033     if (SIGNATURE == MaybeRecord.get())
5034       return ASTFileSignature::create(Record.begin(),
5035                                       Record.begin() + ASTFileSignature::size);
5036   }
5037 }
5038 
5039 /// Retrieve the name of the original source file name
5040 /// directly from the AST file, without actually loading the AST
5041 /// file.
5042 std::string ASTReader::getOriginalSourceFile(
5043     const std::string &ASTFileName, FileManager &FileMgr,
5044     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5045   // Open the AST file.
5046   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5047   if (!Buffer) {
5048     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5049         << ASTFileName << Buffer.getError().message();
5050     return std::string();
5051   }
5052 
5053   // Initialize the stream
5054   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5055 
5056   // Sniff for the signature.
5057   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5058     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5059     return std::string();
5060   }
5061 
5062   // Scan for the CONTROL_BLOCK_ID block.
5063   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5064     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5065     return std::string();
5066   }
5067 
5068   // Scan for ORIGINAL_FILE inside the control block.
5069   RecordData Record;
5070   while (true) {
5071     Expected<llvm::BitstreamEntry> MaybeEntry =
5072         Stream.advanceSkippingSubblocks();
5073     if (!MaybeEntry) {
5074       // FIXME this drops errors on the floor.
5075       consumeError(MaybeEntry.takeError());
5076       return std::string();
5077     }
5078     llvm::BitstreamEntry Entry = MaybeEntry.get();
5079 
5080     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5081       return std::string();
5082 
5083     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5084       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5085       return std::string();
5086     }
5087 
5088     Record.clear();
5089     StringRef Blob;
5090     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5091     if (!MaybeRecord) {
5092       // FIXME this drops the errors on the floor.
5093       consumeError(MaybeRecord.takeError());
5094       return std::string();
5095     }
5096     if (ORIGINAL_FILE == MaybeRecord.get())
5097       return Blob.str();
5098   }
5099 }
5100 
5101 namespace {
5102 
5103   class SimplePCHValidator : public ASTReaderListener {
5104     const LangOptions &ExistingLangOpts;
5105     const TargetOptions &ExistingTargetOpts;
5106     const PreprocessorOptions &ExistingPPOpts;
5107     std::string ExistingModuleCachePath;
5108     FileManager &FileMgr;
5109 
5110   public:
5111     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5112                        const TargetOptions &ExistingTargetOpts,
5113                        const PreprocessorOptions &ExistingPPOpts,
5114                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5115         : ExistingLangOpts(ExistingLangOpts),
5116           ExistingTargetOpts(ExistingTargetOpts),
5117           ExistingPPOpts(ExistingPPOpts),
5118           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5119 
5120     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5121                              bool AllowCompatibleDifferences) override {
5122       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5123                                   AllowCompatibleDifferences);
5124     }
5125 
5126     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5127                            bool AllowCompatibleDifferences) override {
5128       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5129                                 AllowCompatibleDifferences);
5130     }
5131 
5132     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5133                                  StringRef SpecificModuleCachePath,
5134                                  bool Complain) override {
5135       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5136                                       ExistingModuleCachePath,
5137                                       nullptr, ExistingLangOpts);
5138     }
5139 
5140     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5141                                  bool Complain,
5142                                  std::string &SuggestedPredefines) override {
5143       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5144                                       SuggestedPredefines, ExistingLangOpts);
5145     }
5146   };
5147 
5148 } // namespace
5149 
5150 bool ASTReader::readASTFileControlBlock(
5151     StringRef Filename, FileManager &FileMgr,
5152     const PCHContainerReader &PCHContainerRdr,
5153     bool FindModuleFileExtensions,
5154     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5155   // Open the AST file.
5156   // FIXME: This allows use of the VFS; we do not allow use of the
5157   // VFS when actually loading a module.
5158   auto Buffer = FileMgr.getBufferForFile(Filename);
5159   if (!Buffer) {
5160     return true;
5161   }
5162 
5163   // Initialize the stream
5164   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5165   BitstreamCursor Stream(Bytes);
5166 
5167   // Sniff for the signature.
5168   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5169     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5170     return true;
5171   }
5172 
5173   // Scan for the CONTROL_BLOCK_ID block.
5174   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5175     return true;
5176 
5177   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5178   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5179   bool NeedsImports = Listener.needsImportVisitation();
5180   BitstreamCursor InputFilesCursor;
5181 
5182   RecordData Record;
5183   std::string ModuleDir;
5184   bool DoneWithControlBlock = false;
5185   while (!DoneWithControlBlock) {
5186     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5187     if (!MaybeEntry) {
5188       // FIXME this drops the error on the floor.
5189       consumeError(MaybeEntry.takeError());
5190       return true;
5191     }
5192     llvm::BitstreamEntry Entry = MaybeEntry.get();
5193 
5194     switch (Entry.Kind) {
5195     case llvm::BitstreamEntry::SubBlock: {
5196       switch (Entry.ID) {
5197       case OPTIONS_BLOCK_ID: {
5198         std::string IgnoredSuggestedPredefines;
5199         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5200                              /*AllowCompatibleConfigurationMismatch*/ false,
5201                              Listener, IgnoredSuggestedPredefines) != Success)
5202           return true;
5203         break;
5204       }
5205 
5206       case INPUT_FILES_BLOCK_ID:
5207         InputFilesCursor = Stream;
5208         if (llvm::Error Err = Stream.SkipBlock()) {
5209           // FIXME this drops the error on the floor.
5210           consumeError(std::move(Err));
5211           return true;
5212         }
5213         if (NeedsInputFiles &&
5214             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5215           return true;
5216         break;
5217 
5218       default:
5219         if (llvm::Error Err = Stream.SkipBlock()) {
5220           // FIXME this drops the error on the floor.
5221           consumeError(std::move(Err));
5222           return true;
5223         }
5224         break;
5225       }
5226 
5227       continue;
5228     }
5229 
5230     case llvm::BitstreamEntry::EndBlock:
5231       DoneWithControlBlock = true;
5232       break;
5233 
5234     case llvm::BitstreamEntry::Error:
5235       return true;
5236 
5237     case llvm::BitstreamEntry::Record:
5238       break;
5239     }
5240 
5241     if (DoneWithControlBlock) break;
5242 
5243     Record.clear();
5244     StringRef Blob;
5245     Expected<unsigned> MaybeRecCode =
5246         Stream.readRecord(Entry.ID, Record, &Blob);
5247     if (!MaybeRecCode) {
5248       // FIXME this drops the error.
5249       return Failure;
5250     }
5251     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5252     case METADATA:
5253       if (Record[0] != VERSION_MAJOR)
5254         return true;
5255       if (Listener.ReadFullVersionInformation(Blob))
5256         return true;
5257       break;
5258     case MODULE_NAME:
5259       Listener.ReadModuleName(Blob);
5260       break;
5261     case MODULE_DIRECTORY:
5262       ModuleDir = std::string(Blob);
5263       break;
5264     case MODULE_MAP_FILE: {
5265       unsigned Idx = 0;
5266       auto Path = ReadString(Record, Idx);
5267       ResolveImportedPath(Path, ModuleDir);
5268       Listener.ReadModuleMapFile(Path);
5269       break;
5270     }
5271     case INPUT_FILE_OFFSETS: {
5272       if (!NeedsInputFiles)
5273         break;
5274 
5275       unsigned NumInputFiles = Record[0];
5276       unsigned NumUserFiles = Record[1];
5277       const llvm::support::unaligned_uint64_t *InputFileOffs =
5278           (const llvm::support::unaligned_uint64_t *)Blob.data();
5279       for (unsigned I = 0; I != NumInputFiles; ++I) {
5280         // Go find this input file.
5281         bool isSystemFile = I >= NumUserFiles;
5282 
5283         if (isSystemFile && !NeedsSystemInputFiles)
5284           break; // the rest are system input files
5285 
5286         BitstreamCursor &Cursor = InputFilesCursor;
5287         SavedStreamPosition SavedPosition(Cursor);
5288         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5289           // FIXME this drops errors on the floor.
5290           consumeError(std::move(Err));
5291         }
5292 
5293         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5294         if (!MaybeCode) {
5295           // FIXME this drops errors on the floor.
5296           consumeError(MaybeCode.takeError());
5297         }
5298         unsigned Code = MaybeCode.get();
5299 
5300         RecordData Record;
5301         StringRef Blob;
5302         bool shouldContinue = false;
5303         Expected<unsigned> MaybeRecordType =
5304             Cursor.readRecord(Code, Record, &Blob);
5305         if (!MaybeRecordType) {
5306           // FIXME this drops errors on the floor.
5307           consumeError(MaybeRecordType.takeError());
5308         }
5309         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5310         case INPUT_FILE_HASH:
5311           break;
5312         case INPUT_FILE:
5313           bool Overridden = static_cast<bool>(Record[3]);
5314           std::string Filename = std::string(Blob);
5315           ResolveImportedPath(Filename, ModuleDir);
5316           shouldContinue = Listener.visitInputFile(
5317               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5318           break;
5319         }
5320         if (!shouldContinue)
5321           break;
5322       }
5323       break;
5324     }
5325 
5326     case IMPORTS: {
5327       if (!NeedsImports)
5328         break;
5329 
5330       unsigned Idx = 0, N = Record.size();
5331       while (Idx < N) {
5332         // Read information about the AST file.
5333         Idx +=
5334             1 + 1 + 1 + 1 +
5335             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5336         std::string ModuleName = ReadString(Record, Idx);
5337         std::string Filename = ReadString(Record, Idx);
5338         ResolveImportedPath(Filename, ModuleDir);
5339         Listener.visitImport(ModuleName, Filename);
5340       }
5341       break;
5342     }
5343 
5344     default:
5345       // No other validation to perform.
5346       break;
5347     }
5348   }
5349 
5350   // Look for module file extension blocks, if requested.
5351   if (FindModuleFileExtensions) {
5352     BitstreamCursor SavedStream = Stream;
5353     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5354       bool DoneWithExtensionBlock = false;
5355       while (!DoneWithExtensionBlock) {
5356         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5357         if (!MaybeEntry) {
5358           // FIXME this drops the error.
5359           return true;
5360         }
5361         llvm::BitstreamEntry Entry = MaybeEntry.get();
5362 
5363         switch (Entry.Kind) {
5364         case llvm::BitstreamEntry::SubBlock:
5365           if (llvm::Error Err = Stream.SkipBlock()) {
5366             // FIXME this drops the error on the floor.
5367             consumeError(std::move(Err));
5368             return true;
5369           }
5370           continue;
5371 
5372         case llvm::BitstreamEntry::EndBlock:
5373           DoneWithExtensionBlock = true;
5374           continue;
5375 
5376         case llvm::BitstreamEntry::Error:
5377           return true;
5378 
5379         case llvm::BitstreamEntry::Record:
5380           break;
5381         }
5382 
5383        Record.clear();
5384        StringRef Blob;
5385        Expected<unsigned> MaybeRecCode =
5386            Stream.readRecord(Entry.ID, Record, &Blob);
5387        if (!MaybeRecCode) {
5388          // FIXME this drops the error.
5389          return true;
5390        }
5391        switch (MaybeRecCode.get()) {
5392        case EXTENSION_METADATA: {
5393          ModuleFileExtensionMetadata Metadata;
5394          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5395            return true;
5396 
5397          Listener.readModuleFileExtension(Metadata);
5398          break;
5399        }
5400        }
5401       }
5402     }
5403     Stream = SavedStream;
5404   }
5405 
5406   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5407   if (readUnhashedControlBlockImpl(
5408           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5409           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5410           ValidateDiagnosticOptions) != Success)
5411     return true;
5412 
5413   return false;
5414 }
5415 
5416 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5417                                     const PCHContainerReader &PCHContainerRdr,
5418                                     const LangOptions &LangOpts,
5419                                     const TargetOptions &TargetOpts,
5420                                     const PreprocessorOptions &PPOpts,
5421                                     StringRef ExistingModuleCachePath) {
5422   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5423                                ExistingModuleCachePath, FileMgr);
5424   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5425                                   /*FindModuleFileExtensions=*/false,
5426                                   validator,
5427                                   /*ValidateDiagnosticOptions=*/true);
5428 }
5429 
5430 ASTReader::ASTReadResult
5431 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5432   // Enter the submodule block.
5433   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5434     Error(std::move(Err));
5435     return Failure;
5436   }
5437 
5438   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5439   bool First = true;
5440   Module *CurrentModule = nullptr;
5441   RecordData Record;
5442   while (true) {
5443     Expected<llvm::BitstreamEntry> MaybeEntry =
5444         F.Stream.advanceSkippingSubblocks();
5445     if (!MaybeEntry) {
5446       Error(MaybeEntry.takeError());
5447       return Failure;
5448     }
5449     llvm::BitstreamEntry Entry = MaybeEntry.get();
5450 
5451     switch (Entry.Kind) {
5452     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5453     case llvm::BitstreamEntry::Error:
5454       Error("malformed block record in AST file");
5455       return Failure;
5456     case llvm::BitstreamEntry::EndBlock:
5457       return Success;
5458     case llvm::BitstreamEntry::Record:
5459       // The interesting case.
5460       break;
5461     }
5462 
5463     // Read a record.
5464     StringRef Blob;
5465     Record.clear();
5466     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5467     if (!MaybeKind) {
5468       Error(MaybeKind.takeError());
5469       return Failure;
5470     }
5471     unsigned Kind = MaybeKind.get();
5472 
5473     if ((Kind == SUBMODULE_METADATA) != First) {
5474       Error("submodule metadata record should be at beginning of block");
5475       return Failure;
5476     }
5477     First = false;
5478 
5479     // Submodule information is only valid if we have a current module.
5480     // FIXME: Should we error on these cases?
5481     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5482         Kind != SUBMODULE_DEFINITION)
5483       continue;
5484 
5485     switch (Kind) {
5486     default:  // Default behavior: ignore.
5487       break;
5488 
5489     case SUBMODULE_DEFINITION: {
5490       if (Record.size() < 12) {
5491         Error("malformed module definition");
5492         return Failure;
5493       }
5494 
5495       StringRef Name = Blob;
5496       unsigned Idx = 0;
5497       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5498       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5499       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5500       bool IsFramework = Record[Idx++];
5501       bool IsExplicit = Record[Idx++];
5502       bool IsSystem = Record[Idx++];
5503       bool IsExternC = Record[Idx++];
5504       bool InferSubmodules = Record[Idx++];
5505       bool InferExplicitSubmodules = Record[Idx++];
5506       bool InferExportWildcard = Record[Idx++];
5507       bool ConfigMacrosExhaustive = Record[Idx++];
5508       bool ModuleMapIsPrivate = Record[Idx++];
5509 
5510       Module *ParentModule = nullptr;
5511       if (Parent)
5512         ParentModule = getSubmodule(Parent);
5513 
5514       // Retrieve this (sub)module from the module map, creating it if
5515       // necessary.
5516       CurrentModule =
5517           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5518               .first;
5519 
5520       // FIXME: set the definition loc for CurrentModule, or call
5521       // ModMap.setInferredModuleAllowedBy()
5522 
5523       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5524       if (GlobalIndex >= SubmodulesLoaded.size() ||
5525           SubmodulesLoaded[GlobalIndex]) {
5526         Error("too many submodules");
5527         return Failure;
5528       }
5529 
5530       if (!ParentModule) {
5531         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5532           // Don't emit module relocation error if we have -fno-validate-pch
5533           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5534               CurFile != F.File) {
5535             Error(diag::err_module_file_conflict,
5536                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5537                   F.File->getName());
5538             return Failure;
5539           }
5540         }
5541 
5542         F.DidReadTopLevelSubmodule = true;
5543         CurrentModule->setASTFile(F.File);
5544         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5545       }
5546 
5547       CurrentModule->Kind = Kind;
5548       CurrentModule->Signature = F.Signature;
5549       CurrentModule->IsFromModuleFile = true;
5550       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5551       CurrentModule->IsExternC = IsExternC;
5552       CurrentModule->InferSubmodules = InferSubmodules;
5553       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5554       CurrentModule->InferExportWildcard = InferExportWildcard;
5555       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5556       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5557       if (DeserializationListener)
5558         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5559 
5560       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5561 
5562       // Clear out data that will be replaced by what is in the module file.
5563       CurrentModule->LinkLibraries.clear();
5564       CurrentModule->ConfigMacros.clear();
5565       CurrentModule->UnresolvedConflicts.clear();
5566       CurrentModule->Conflicts.clear();
5567 
5568       // The module is available unless it's missing a requirement; relevant
5569       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5570       // Missing headers that were present when the module was built do not
5571       // make it unavailable -- if we got this far, this must be an explicitly
5572       // imported module file.
5573       CurrentModule->Requirements.clear();
5574       CurrentModule->MissingHeaders.clear();
5575       CurrentModule->IsUnimportable =
5576           ParentModule && ParentModule->IsUnimportable;
5577       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5578       break;
5579     }
5580 
5581     case SUBMODULE_UMBRELLA_HEADER: {
5582       std::string Filename = std::string(Blob);
5583       ResolveImportedPath(F, Filename);
5584       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5585         if (!CurrentModule->getUmbrellaHeader())
5586           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5587         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5588           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5589             Error("mismatched umbrella headers in submodule");
5590           return OutOfDate;
5591         }
5592       }
5593       break;
5594     }
5595 
5596     case SUBMODULE_HEADER:
5597     case SUBMODULE_EXCLUDED_HEADER:
5598     case SUBMODULE_PRIVATE_HEADER:
5599       // We lazily associate headers with their modules via the HeaderInfo table.
5600       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5601       // of complete filenames or remove it entirely.
5602       break;
5603 
5604     case SUBMODULE_TEXTUAL_HEADER:
5605     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5606       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5607       // them here.
5608       break;
5609 
5610     case SUBMODULE_TOPHEADER:
5611       CurrentModule->addTopHeaderFilename(Blob);
5612       break;
5613 
5614     case SUBMODULE_UMBRELLA_DIR: {
5615       std::string Dirname = std::string(Blob);
5616       ResolveImportedPath(F, Dirname);
5617       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5618         if (!CurrentModule->getUmbrellaDir())
5619           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5620         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5621           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5622             Error("mismatched umbrella directories in submodule");
5623           return OutOfDate;
5624         }
5625       }
5626       break;
5627     }
5628 
5629     case SUBMODULE_METADATA: {
5630       F.BaseSubmoduleID = getTotalNumSubmodules();
5631       F.LocalNumSubmodules = Record[0];
5632       unsigned LocalBaseSubmoduleID = Record[1];
5633       if (F.LocalNumSubmodules > 0) {
5634         // Introduce the global -> local mapping for submodules within this
5635         // module.
5636         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5637 
5638         // Introduce the local -> global mapping for submodules within this
5639         // module.
5640         F.SubmoduleRemap.insertOrReplace(
5641           std::make_pair(LocalBaseSubmoduleID,
5642                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5643 
5644         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5645       }
5646       break;
5647     }
5648 
5649     case SUBMODULE_IMPORTS:
5650       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5651         UnresolvedModuleRef Unresolved;
5652         Unresolved.File = &F;
5653         Unresolved.Mod = CurrentModule;
5654         Unresolved.ID = Record[Idx];
5655         Unresolved.Kind = UnresolvedModuleRef::Import;
5656         Unresolved.IsWildcard = false;
5657         UnresolvedModuleRefs.push_back(Unresolved);
5658       }
5659       break;
5660 
5661     case SUBMODULE_EXPORTS:
5662       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5663         UnresolvedModuleRef Unresolved;
5664         Unresolved.File = &F;
5665         Unresolved.Mod = CurrentModule;
5666         Unresolved.ID = Record[Idx];
5667         Unresolved.Kind = UnresolvedModuleRef::Export;
5668         Unresolved.IsWildcard = Record[Idx + 1];
5669         UnresolvedModuleRefs.push_back(Unresolved);
5670       }
5671 
5672       // Once we've loaded the set of exports, there's no reason to keep
5673       // the parsed, unresolved exports around.
5674       CurrentModule->UnresolvedExports.clear();
5675       break;
5676 
5677     case SUBMODULE_REQUIRES:
5678       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5679                                     PP.getTargetInfo());
5680       break;
5681 
5682     case SUBMODULE_LINK_LIBRARY:
5683       ModMap.resolveLinkAsDependencies(CurrentModule);
5684       CurrentModule->LinkLibraries.push_back(
5685           Module::LinkLibrary(std::string(Blob), Record[0]));
5686       break;
5687 
5688     case SUBMODULE_CONFIG_MACRO:
5689       CurrentModule->ConfigMacros.push_back(Blob.str());
5690       break;
5691 
5692     case SUBMODULE_CONFLICT: {
5693       UnresolvedModuleRef Unresolved;
5694       Unresolved.File = &F;
5695       Unresolved.Mod = CurrentModule;
5696       Unresolved.ID = Record[0];
5697       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5698       Unresolved.IsWildcard = false;
5699       Unresolved.String = Blob;
5700       UnresolvedModuleRefs.push_back(Unresolved);
5701       break;
5702     }
5703 
5704     case SUBMODULE_INITIALIZERS: {
5705       if (!ContextObj)
5706         break;
5707       SmallVector<uint32_t, 16> Inits;
5708       for (auto &ID : Record)
5709         Inits.push_back(getGlobalDeclID(F, ID));
5710       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5711       break;
5712     }
5713 
5714     case SUBMODULE_EXPORT_AS:
5715       CurrentModule->ExportAsModule = Blob.str();
5716       ModMap.addLinkAsDependency(CurrentModule);
5717       break;
5718     }
5719   }
5720 }
5721 
5722 /// Parse the record that corresponds to a LangOptions data
5723 /// structure.
5724 ///
5725 /// This routine parses the language options from the AST file and then gives
5726 /// them to the AST listener if one is set.
5727 ///
5728 /// \returns true if the listener deems the file unacceptable, false otherwise.
5729 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5730                                      bool Complain,
5731                                      ASTReaderListener &Listener,
5732                                      bool AllowCompatibleDifferences) {
5733   LangOptions LangOpts;
5734   unsigned Idx = 0;
5735 #define LANGOPT(Name, Bits, Default, Description) \
5736   LangOpts.Name = Record[Idx++];
5737 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5738   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5739 #include "clang/Basic/LangOptions.def"
5740 #define SANITIZER(NAME, ID)                                                    \
5741   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5742 #include "clang/Basic/Sanitizers.def"
5743 
5744   for (unsigned N = Record[Idx++]; N; --N)
5745     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5746 
5747   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5748   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5749   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5750 
5751   LangOpts.CurrentModule = ReadString(Record, Idx);
5752 
5753   // Comment options.
5754   for (unsigned N = Record[Idx++]; N; --N) {
5755     LangOpts.CommentOpts.BlockCommandNames.push_back(
5756       ReadString(Record, Idx));
5757   }
5758   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5759 
5760   // OpenMP offloading options.
5761   for (unsigned N = Record[Idx++]; N; --N) {
5762     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5763   }
5764 
5765   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5766 
5767   return Listener.ReadLanguageOptions(LangOpts, Complain,
5768                                       AllowCompatibleDifferences);
5769 }
5770 
5771 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5772                                    ASTReaderListener &Listener,
5773                                    bool AllowCompatibleDifferences) {
5774   unsigned Idx = 0;
5775   TargetOptions TargetOpts;
5776   TargetOpts.Triple = ReadString(Record, Idx);
5777   TargetOpts.CPU = ReadString(Record, Idx);
5778   TargetOpts.TuneCPU = ReadString(Record, Idx);
5779   TargetOpts.ABI = ReadString(Record, Idx);
5780   for (unsigned N = Record[Idx++]; N; --N) {
5781     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5782   }
5783   for (unsigned N = Record[Idx++]; N; --N) {
5784     TargetOpts.Features.push_back(ReadString(Record, Idx));
5785   }
5786 
5787   return Listener.ReadTargetOptions(TargetOpts, Complain,
5788                                     AllowCompatibleDifferences);
5789 }
5790 
5791 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5792                                        ASTReaderListener &Listener) {
5793   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5794   unsigned Idx = 0;
5795 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5796 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5797   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5798 #include "clang/Basic/DiagnosticOptions.def"
5799 
5800   for (unsigned N = Record[Idx++]; N; --N)
5801     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5802   for (unsigned N = Record[Idx++]; N; --N)
5803     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5804 
5805   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5806 }
5807 
5808 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5809                                        ASTReaderListener &Listener) {
5810   FileSystemOptions FSOpts;
5811   unsigned Idx = 0;
5812   FSOpts.WorkingDir = ReadString(Record, Idx);
5813   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5814 }
5815 
5816 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5817                                          bool Complain,
5818                                          ASTReaderListener &Listener) {
5819   HeaderSearchOptions HSOpts;
5820   unsigned Idx = 0;
5821   HSOpts.Sysroot = ReadString(Record, Idx);
5822 
5823   // Include entries.
5824   for (unsigned N = Record[Idx++]; N; --N) {
5825     std::string Path = ReadString(Record, Idx);
5826     frontend::IncludeDirGroup Group
5827       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5828     bool IsFramework = Record[Idx++];
5829     bool IgnoreSysRoot = Record[Idx++];
5830     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5831                                     IgnoreSysRoot);
5832   }
5833 
5834   // System header prefixes.
5835   for (unsigned N = Record[Idx++]; N; --N) {
5836     std::string Prefix = ReadString(Record, Idx);
5837     bool IsSystemHeader = Record[Idx++];
5838     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5839   }
5840 
5841   HSOpts.ResourceDir = ReadString(Record, Idx);
5842   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5843   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5844   HSOpts.DisableModuleHash = Record[Idx++];
5845   HSOpts.ImplicitModuleMaps = Record[Idx++];
5846   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5847   HSOpts.UseBuiltinIncludes = Record[Idx++];
5848   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5849   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5850   HSOpts.UseLibcxx = Record[Idx++];
5851   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5852 
5853   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5854                                           Complain);
5855 }
5856 
5857 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5858                                          bool Complain,
5859                                          ASTReaderListener &Listener,
5860                                          std::string &SuggestedPredefines) {
5861   PreprocessorOptions PPOpts;
5862   unsigned Idx = 0;
5863 
5864   // Macro definitions/undefs
5865   for (unsigned N = Record[Idx++]; N; --N) {
5866     std::string Macro = ReadString(Record, Idx);
5867     bool IsUndef = Record[Idx++];
5868     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5869   }
5870 
5871   // Includes
5872   for (unsigned N = Record[Idx++]; N; --N) {
5873     PPOpts.Includes.push_back(ReadString(Record, Idx));
5874   }
5875 
5876   // Macro Includes
5877   for (unsigned N = Record[Idx++]; N; --N) {
5878     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5879   }
5880 
5881   PPOpts.UsePredefines = Record[Idx++];
5882   PPOpts.DetailedRecord = Record[Idx++];
5883   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5884   PPOpts.ObjCXXARCStandardLibrary =
5885     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5886   SuggestedPredefines.clear();
5887   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5888                                           SuggestedPredefines);
5889 }
5890 
5891 std::pair<ModuleFile *, unsigned>
5892 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5893   GlobalPreprocessedEntityMapType::iterator
5894   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5895   assert(I != GlobalPreprocessedEntityMap.end() &&
5896          "Corrupted global preprocessed entity map");
5897   ModuleFile *M = I->second;
5898   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5899   return std::make_pair(M, LocalIndex);
5900 }
5901 
5902 llvm::iterator_range<PreprocessingRecord::iterator>
5903 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5904   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5905     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5906                                              Mod.NumPreprocessedEntities);
5907 
5908   return llvm::make_range(PreprocessingRecord::iterator(),
5909                           PreprocessingRecord::iterator());
5910 }
5911 
5912 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5913 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5914   return llvm::make_range(
5915       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5916       ModuleDeclIterator(this, &Mod,
5917                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5918 }
5919 
5920 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5921   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5922   assert(I != GlobalSkippedRangeMap.end() &&
5923     "Corrupted global skipped range map");
5924   ModuleFile *M = I->second;
5925   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5926   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5927   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5928   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5929                     TranslateSourceLocation(*M, RawRange.getEnd()));
5930   assert(Range.isValid());
5931   return Range;
5932 }
5933 
5934 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5935   PreprocessedEntityID PPID = Index+1;
5936   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5937   ModuleFile &M = *PPInfo.first;
5938   unsigned LocalIndex = PPInfo.second;
5939   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5940 
5941   if (!PP.getPreprocessingRecord()) {
5942     Error("no preprocessing record");
5943     return nullptr;
5944   }
5945 
5946   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5947   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5948           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5949     Error(std::move(Err));
5950     return nullptr;
5951   }
5952 
5953   Expected<llvm::BitstreamEntry> MaybeEntry =
5954       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5955   if (!MaybeEntry) {
5956     Error(MaybeEntry.takeError());
5957     return nullptr;
5958   }
5959   llvm::BitstreamEntry Entry = MaybeEntry.get();
5960 
5961   if (Entry.Kind != llvm::BitstreamEntry::Record)
5962     return nullptr;
5963 
5964   // Read the record.
5965   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5966                     TranslateSourceLocation(M, PPOffs.getEnd()));
5967   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5968   StringRef Blob;
5969   RecordData Record;
5970   Expected<unsigned> MaybeRecType =
5971       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5972   if (!MaybeRecType) {
5973     Error(MaybeRecType.takeError());
5974     return nullptr;
5975   }
5976   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5977   case PPD_MACRO_EXPANSION: {
5978     bool isBuiltin = Record[0];
5979     IdentifierInfo *Name = nullptr;
5980     MacroDefinitionRecord *Def = nullptr;
5981     if (isBuiltin)
5982       Name = getLocalIdentifier(M, Record[1]);
5983     else {
5984       PreprocessedEntityID GlobalID =
5985           getGlobalPreprocessedEntityID(M, Record[1]);
5986       Def = cast<MacroDefinitionRecord>(
5987           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5988     }
5989 
5990     MacroExpansion *ME;
5991     if (isBuiltin)
5992       ME = new (PPRec) MacroExpansion(Name, Range);
5993     else
5994       ME = new (PPRec) MacroExpansion(Def, Range);
5995 
5996     return ME;
5997   }
5998 
5999   case PPD_MACRO_DEFINITION: {
6000     // Decode the identifier info and then check again; if the macro is
6001     // still defined and associated with the identifier,
6002     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6003     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6004 
6005     if (DeserializationListener)
6006       DeserializationListener->MacroDefinitionRead(PPID, MD);
6007 
6008     return MD;
6009   }
6010 
6011   case PPD_INCLUSION_DIRECTIVE: {
6012     const char *FullFileNameStart = Blob.data() + Record[0];
6013     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6014     const FileEntry *File = nullptr;
6015     if (!FullFileName.empty())
6016       if (auto FE = PP.getFileManager().getFile(FullFileName))
6017         File = *FE;
6018 
6019     // FIXME: Stable encoding
6020     InclusionDirective::InclusionKind Kind
6021       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6022     InclusionDirective *ID
6023       = new (PPRec) InclusionDirective(PPRec, Kind,
6024                                        StringRef(Blob.data(), Record[0]),
6025                                        Record[1], Record[3],
6026                                        File,
6027                                        Range);
6028     return ID;
6029   }
6030   }
6031 
6032   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6033 }
6034 
6035 /// Find the next module that contains entities and return the ID
6036 /// of the first entry.
6037 ///
6038 /// \param SLocMapI points at a chunk of a module that contains no
6039 /// preprocessed entities or the entities it contains are not the ones we are
6040 /// looking for.
6041 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6042                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6043   ++SLocMapI;
6044   for (GlobalSLocOffsetMapType::const_iterator
6045          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6046     ModuleFile &M = *SLocMapI->second;
6047     if (M.NumPreprocessedEntities)
6048       return M.BasePreprocessedEntityID;
6049   }
6050 
6051   return getTotalNumPreprocessedEntities();
6052 }
6053 
6054 namespace {
6055 
6056 struct PPEntityComp {
6057   const ASTReader &Reader;
6058   ModuleFile &M;
6059 
6060   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6061 
6062   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6063     SourceLocation LHS = getLoc(L);
6064     SourceLocation RHS = getLoc(R);
6065     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6066   }
6067 
6068   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6069     SourceLocation LHS = getLoc(L);
6070     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6071   }
6072 
6073   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6074     SourceLocation RHS = getLoc(R);
6075     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6076   }
6077 
6078   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6079     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6080   }
6081 };
6082 
6083 } // namespace
6084 
6085 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6086                                                        bool EndsAfter) const {
6087   if (SourceMgr.isLocalSourceLocation(Loc))
6088     return getTotalNumPreprocessedEntities();
6089 
6090   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6091       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6092   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6093          "Corrupted global sloc offset map");
6094 
6095   if (SLocMapI->second->NumPreprocessedEntities == 0)
6096     return findNextPreprocessedEntity(SLocMapI);
6097 
6098   ModuleFile &M = *SLocMapI->second;
6099 
6100   using pp_iterator = const PPEntityOffset *;
6101 
6102   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6103   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6104 
6105   size_t Count = M.NumPreprocessedEntities;
6106   size_t Half;
6107   pp_iterator First = pp_begin;
6108   pp_iterator PPI;
6109 
6110   if (EndsAfter) {
6111     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6112                            PPEntityComp(*this, M));
6113   } else {
6114     // Do a binary search manually instead of using std::lower_bound because
6115     // The end locations of entities may be unordered (when a macro expansion
6116     // is inside another macro argument), but for this case it is not important
6117     // whether we get the first macro expansion or its containing macro.
6118     while (Count > 0) {
6119       Half = Count / 2;
6120       PPI = First;
6121       std::advance(PPI, Half);
6122       if (SourceMgr.isBeforeInTranslationUnit(
6123               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6124         First = PPI;
6125         ++First;
6126         Count = Count - Half - 1;
6127       } else
6128         Count = Half;
6129     }
6130   }
6131 
6132   if (PPI == pp_end)
6133     return findNextPreprocessedEntity(SLocMapI);
6134 
6135   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6136 }
6137 
6138 /// Returns a pair of [Begin, End) indices of preallocated
6139 /// preprocessed entities that \arg Range encompasses.
6140 std::pair<unsigned, unsigned>
6141     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6142   if (Range.isInvalid())
6143     return std::make_pair(0,0);
6144   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6145 
6146   PreprocessedEntityID BeginID =
6147       findPreprocessedEntity(Range.getBegin(), false);
6148   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6149   return std::make_pair(BeginID, EndID);
6150 }
6151 
6152 /// Optionally returns true or false if the preallocated preprocessed
6153 /// entity with index \arg Index came from file \arg FID.
6154 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6155                                                              FileID FID) {
6156   if (FID.isInvalid())
6157     return false;
6158 
6159   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6160   ModuleFile &M = *PPInfo.first;
6161   unsigned LocalIndex = PPInfo.second;
6162   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6163 
6164   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6165   if (Loc.isInvalid())
6166     return false;
6167 
6168   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6169     return true;
6170   else
6171     return false;
6172 }
6173 
6174 namespace {
6175 
6176   /// Visitor used to search for information about a header file.
6177   class HeaderFileInfoVisitor {
6178     const FileEntry *FE;
6179     Optional<HeaderFileInfo> HFI;
6180 
6181   public:
6182     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6183 
6184     bool operator()(ModuleFile &M) {
6185       HeaderFileInfoLookupTable *Table
6186         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6187       if (!Table)
6188         return false;
6189 
6190       // Look in the on-disk hash table for an entry for this file name.
6191       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6192       if (Pos == Table->end())
6193         return false;
6194 
6195       HFI = *Pos;
6196       return true;
6197     }
6198 
6199     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6200   };
6201 
6202 } // namespace
6203 
6204 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6205   HeaderFileInfoVisitor Visitor(FE);
6206   ModuleMgr.visit(Visitor);
6207   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6208     return *HFI;
6209 
6210   return HeaderFileInfo();
6211 }
6212 
6213 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6214   using DiagState = DiagnosticsEngine::DiagState;
6215   SmallVector<DiagState *, 32> DiagStates;
6216 
6217   for (ModuleFile &F : ModuleMgr) {
6218     unsigned Idx = 0;
6219     auto &Record = F.PragmaDiagMappings;
6220     if (Record.empty())
6221       continue;
6222 
6223     DiagStates.clear();
6224 
6225     auto ReadDiagState =
6226         [&](const DiagState &BasedOn, SourceLocation Loc,
6227             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6228       unsigned BackrefID = Record[Idx++];
6229       if (BackrefID != 0)
6230         return DiagStates[BackrefID - 1];
6231 
6232       // A new DiagState was created here.
6233       Diag.DiagStates.push_back(BasedOn);
6234       DiagState *NewState = &Diag.DiagStates.back();
6235       DiagStates.push_back(NewState);
6236       unsigned Size = Record[Idx++];
6237       assert(Idx + Size * 2 <= Record.size() &&
6238              "Invalid data, not enough diag/map pairs");
6239       while (Size--) {
6240         unsigned DiagID = Record[Idx++];
6241         DiagnosticMapping NewMapping =
6242             DiagnosticMapping::deserialize(Record[Idx++]);
6243         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6244           continue;
6245 
6246         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6247 
6248         // If this mapping was specified as a warning but the severity was
6249         // upgraded due to diagnostic settings, simulate the current diagnostic
6250         // settings (and use a warning).
6251         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6252           NewMapping.setSeverity(diag::Severity::Warning);
6253           NewMapping.setUpgradedFromWarning(false);
6254         }
6255 
6256         Mapping = NewMapping;
6257       }
6258       return NewState;
6259     };
6260 
6261     // Read the first state.
6262     DiagState *FirstState;
6263     if (F.Kind == MK_ImplicitModule) {
6264       // Implicitly-built modules are reused with different diagnostic
6265       // settings.  Use the initial diagnostic state from Diag to simulate this
6266       // compilation's diagnostic settings.
6267       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6268       DiagStates.push_back(FirstState);
6269 
6270       // Skip the initial diagnostic state from the serialized module.
6271       assert(Record[1] == 0 &&
6272              "Invalid data, unexpected backref in initial state");
6273       Idx = 3 + Record[2] * 2;
6274       assert(Idx < Record.size() &&
6275              "Invalid data, not enough state change pairs in initial state");
6276     } else if (F.isModule()) {
6277       // For an explicit module, preserve the flags from the module build
6278       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6279       // -Wblah flags.
6280       unsigned Flags = Record[Idx++];
6281       DiagState Initial;
6282       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6283       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6284       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6285       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6286       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6287       Initial.ExtBehavior = (diag::Severity)Flags;
6288       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6289 
6290       assert(F.OriginalSourceFileID.isValid());
6291 
6292       // Set up the root buffer of the module to start with the initial
6293       // diagnostic state of the module itself, to cover files that contain no
6294       // explicit transitions (for which we did not serialize anything).
6295       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6296           .StateTransitions.push_back({FirstState, 0});
6297     } else {
6298       // For prefix ASTs, start with whatever the user configured on the
6299       // command line.
6300       Idx++; // Skip flags.
6301       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6302                                  SourceLocation(), false);
6303     }
6304 
6305     // Read the state transitions.
6306     unsigned NumLocations = Record[Idx++];
6307     while (NumLocations--) {
6308       assert(Idx < Record.size() &&
6309              "Invalid data, missing pragma diagnostic states");
6310       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6311       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6312       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6313       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6314       unsigned Transitions = Record[Idx++];
6315 
6316       // Note that we don't need to set up Parent/ParentOffset here, because
6317       // we won't be changing the diagnostic state within imported FileIDs
6318       // (other than perhaps appending to the main source file, which has no
6319       // parent).
6320       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6321       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6322       for (unsigned I = 0; I != Transitions; ++I) {
6323         unsigned Offset = Record[Idx++];
6324         auto *State =
6325             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6326         F.StateTransitions.push_back({State, Offset});
6327       }
6328     }
6329 
6330     // Read the final state.
6331     assert(Idx < Record.size() &&
6332            "Invalid data, missing final pragma diagnostic state");
6333     SourceLocation CurStateLoc =
6334         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6335     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6336 
6337     if (!F.isModule()) {
6338       Diag.DiagStatesByLoc.CurDiagState = CurState;
6339       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6340 
6341       // Preserve the property that the imaginary root file describes the
6342       // current state.
6343       FileID NullFile;
6344       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6345       if (T.empty())
6346         T.push_back({CurState, 0});
6347       else
6348         T[0].State = CurState;
6349     }
6350 
6351     // Don't try to read these mappings again.
6352     Record.clear();
6353   }
6354 }
6355 
6356 /// Get the correct cursor and offset for loading a type.
6357 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6358   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6359   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6360   ModuleFile *M = I->second;
6361   return RecordLocation(
6362       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6363              M->DeclsBlockStartOffset);
6364 }
6365 
6366 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6367   switch (code) {
6368 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6369   case TYPE_##CODE_ID: return Type::CLASS_ID;
6370 #include "clang/Serialization/TypeBitCodes.def"
6371   default: return llvm::None;
6372   }
6373 }
6374 
6375 /// Read and return the type with the given index..
6376 ///
6377 /// The index is the type ID, shifted and minus the number of predefs. This
6378 /// routine actually reads the record corresponding to the type at the given
6379 /// location. It is a helper routine for GetType, which deals with reading type
6380 /// IDs.
6381 QualType ASTReader::readTypeRecord(unsigned Index) {
6382   assert(ContextObj && "reading type with no AST context");
6383   ASTContext &Context = *ContextObj;
6384   RecordLocation Loc = TypeCursorForIndex(Index);
6385   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6386 
6387   // Keep track of where we are in the stream, then jump back there
6388   // after reading this type.
6389   SavedStreamPosition SavedPosition(DeclsCursor);
6390 
6391   ReadingKindTracker ReadingKind(Read_Type, *this);
6392 
6393   // Note that we are loading a type record.
6394   Deserializing AType(this);
6395 
6396   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6397     Error(std::move(Err));
6398     return QualType();
6399   }
6400   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6401   if (!RawCode) {
6402     Error(RawCode.takeError());
6403     return QualType();
6404   }
6405 
6406   ASTRecordReader Record(*this, *Loc.F);
6407   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6408   if (!Code) {
6409     Error(Code.takeError());
6410     return QualType();
6411   }
6412   if (Code.get() == TYPE_EXT_QUAL) {
6413     QualType baseType = Record.readQualType();
6414     Qualifiers quals = Record.readQualifiers();
6415     return Context.getQualifiedType(baseType, quals);
6416   }
6417 
6418   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6419   if (!maybeClass) {
6420     Error("Unexpected code for type");
6421     return QualType();
6422   }
6423 
6424   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6425   return TypeReader.read(*maybeClass);
6426 }
6427 
6428 namespace clang {
6429 
6430 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6431   ASTRecordReader &Reader;
6432 
6433   SourceLocation readSourceLocation() {
6434     return Reader.readSourceLocation();
6435   }
6436 
6437   TypeSourceInfo *GetTypeSourceInfo() {
6438     return Reader.readTypeSourceInfo();
6439   }
6440 
6441   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6442     return Reader.readNestedNameSpecifierLoc();
6443   }
6444 
6445   Attr *ReadAttr() {
6446     return Reader.readAttr();
6447   }
6448 
6449 public:
6450   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6451 
6452   // We want compile-time assurance that we've enumerated all of
6453   // these, so unfortunately we have to declare them first, then
6454   // define them out-of-line.
6455 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6456 #define TYPELOC(CLASS, PARENT) \
6457   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6458 #include "clang/AST/TypeLocNodes.def"
6459 
6460   void VisitFunctionTypeLoc(FunctionTypeLoc);
6461   void VisitArrayTypeLoc(ArrayTypeLoc);
6462 };
6463 
6464 } // namespace clang
6465 
6466 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6467   // nothing to do
6468 }
6469 
6470 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6471   TL.setBuiltinLoc(readSourceLocation());
6472   if (TL.needsExtraLocalData()) {
6473     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6474     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6475     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6476     TL.setModeAttr(Reader.readInt());
6477   }
6478 }
6479 
6480 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6481   TL.setNameLoc(readSourceLocation());
6482 }
6483 
6484 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6485   TL.setStarLoc(readSourceLocation());
6486 }
6487 
6488 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6489   // nothing to do
6490 }
6491 
6492 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6493   // nothing to do
6494 }
6495 
6496 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6497   TL.setExpansionLoc(readSourceLocation());
6498 }
6499 
6500 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6501   TL.setCaretLoc(readSourceLocation());
6502 }
6503 
6504 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6505   TL.setAmpLoc(readSourceLocation());
6506 }
6507 
6508 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6509   TL.setAmpAmpLoc(readSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6513   TL.setStarLoc(readSourceLocation());
6514   TL.setClassTInfo(GetTypeSourceInfo());
6515 }
6516 
6517 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6518   TL.setLBracketLoc(readSourceLocation());
6519   TL.setRBracketLoc(readSourceLocation());
6520   if (Reader.readBool())
6521     TL.setSizeExpr(Reader.readExpr());
6522   else
6523     TL.setSizeExpr(nullptr);
6524 }
6525 
6526 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6527   VisitArrayTypeLoc(TL);
6528 }
6529 
6530 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6531   VisitArrayTypeLoc(TL);
6532 }
6533 
6534 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6535   VisitArrayTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6539                                             DependentSizedArrayTypeLoc TL) {
6540   VisitArrayTypeLoc(TL);
6541 }
6542 
6543 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6544     DependentAddressSpaceTypeLoc TL) {
6545 
6546     TL.setAttrNameLoc(readSourceLocation());
6547     TL.setAttrOperandParensRange(Reader.readSourceRange());
6548     TL.setAttrExprOperand(Reader.readExpr());
6549 }
6550 
6551 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6552                                         DependentSizedExtVectorTypeLoc TL) {
6553   TL.setNameLoc(readSourceLocation());
6554 }
6555 
6556 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6557   TL.setNameLoc(readSourceLocation());
6558 }
6559 
6560 void TypeLocReader::VisitDependentVectorTypeLoc(
6561     DependentVectorTypeLoc TL) {
6562   TL.setNameLoc(readSourceLocation());
6563 }
6564 
6565 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6566   TL.setNameLoc(readSourceLocation());
6567 }
6568 
6569 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6570   TL.setAttrNameLoc(readSourceLocation());
6571   TL.setAttrOperandParensRange(Reader.readSourceRange());
6572   TL.setAttrRowOperand(Reader.readExpr());
6573   TL.setAttrColumnOperand(Reader.readExpr());
6574 }
6575 
6576 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6577     DependentSizedMatrixTypeLoc TL) {
6578   TL.setAttrNameLoc(readSourceLocation());
6579   TL.setAttrOperandParensRange(Reader.readSourceRange());
6580   TL.setAttrRowOperand(Reader.readExpr());
6581   TL.setAttrColumnOperand(Reader.readExpr());
6582 }
6583 
6584 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6585   TL.setLocalRangeBegin(readSourceLocation());
6586   TL.setLParenLoc(readSourceLocation());
6587   TL.setRParenLoc(readSourceLocation());
6588   TL.setExceptionSpecRange(Reader.readSourceRange());
6589   TL.setLocalRangeEnd(readSourceLocation());
6590   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6591     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6592   }
6593 }
6594 
6595 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6596   VisitFunctionTypeLoc(TL);
6597 }
6598 
6599 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6600   VisitFunctionTypeLoc(TL);
6601 }
6602 
6603 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6604   TL.setNameLoc(readSourceLocation());
6605 }
6606 
6607 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6608   TL.setNameLoc(readSourceLocation());
6609 }
6610 
6611 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6612   TL.setTypeofLoc(readSourceLocation());
6613   TL.setLParenLoc(readSourceLocation());
6614   TL.setRParenLoc(readSourceLocation());
6615 }
6616 
6617 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6618   TL.setTypeofLoc(readSourceLocation());
6619   TL.setLParenLoc(readSourceLocation());
6620   TL.setRParenLoc(readSourceLocation());
6621   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6622 }
6623 
6624 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6625   TL.setNameLoc(readSourceLocation());
6626 }
6627 
6628 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6629   TL.setKWLoc(readSourceLocation());
6630   TL.setLParenLoc(readSourceLocation());
6631   TL.setRParenLoc(readSourceLocation());
6632   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6633 }
6634 
6635 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6636   TL.setNameLoc(readSourceLocation());
6637   if (Reader.readBool()) {
6638     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6639     TL.setTemplateKWLoc(readSourceLocation());
6640     TL.setConceptNameLoc(readSourceLocation());
6641     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6642     TL.setLAngleLoc(readSourceLocation());
6643     TL.setRAngleLoc(readSourceLocation());
6644     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6645       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6646                               TL.getTypePtr()->getArg(i).getKind()));
6647   }
6648 }
6649 
6650 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6651     DeducedTemplateSpecializationTypeLoc TL) {
6652   TL.setTemplateNameLoc(readSourceLocation());
6653 }
6654 
6655 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6656   TL.setNameLoc(readSourceLocation());
6657 }
6658 
6659 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6660   TL.setNameLoc(readSourceLocation());
6661 }
6662 
6663 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6664   TL.setAttr(ReadAttr());
6665 }
6666 
6667 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6668   TL.setNameLoc(readSourceLocation());
6669 }
6670 
6671 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6672                                             SubstTemplateTypeParmTypeLoc TL) {
6673   TL.setNameLoc(readSourceLocation());
6674 }
6675 
6676 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6677                                           SubstTemplateTypeParmPackTypeLoc TL) {
6678   TL.setNameLoc(readSourceLocation());
6679 }
6680 
6681 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6682                                            TemplateSpecializationTypeLoc TL) {
6683   TL.setTemplateKeywordLoc(readSourceLocation());
6684   TL.setTemplateNameLoc(readSourceLocation());
6685   TL.setLAngleLoc(readSourceLocation());
6686   TL.setRAngleLoc(readSourceLocation());
6687   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6688     TL.setArgLocInfo(
6689         i,
6690         Reader.readTemplateArgumentLocInfo(
6691           TL.getTypePtr()->getArg(i).getKind()));
6692 }
6693 
6694 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6695   TL.setLParenLoc(readSourceLocation());
6696   TL.setRParenLoc(readSourceLocation());
6697 }
6698 
6699 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6700   TL.setElaboratedKeywordLoc(readSourceLocation());
6701   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6702 }
6703 
6704 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6705   TL.setNameLoc(readSourceLocation());
6706 }
6707 
6708 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6709   TL.setElaboratedKeywordLoc(readSourceLocation());
6710   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6711   TL.setNameLoc(readSourceLocation());
6712 }
6713 
6714 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6715        DependentTemplateSpecializationTypeLoc TL) {
6716   TL.setElaboratedKeywordLoc(readSourceLocation());
6717   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6718   TL.setTemplateKeywordLoc(readSourceLocation());
6719   TL.setTemplateNameLoc(readSourceLocation());
6720   TL.setLAngleLoc(readSourceLocation());
6721   TL.setRAngleLoc(readSourceLocation());
6722   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6723     TL.setArgLocInfo(
6724         I,
6725         Reader.readTemplateArgumentLocInfo(
6726             TL.getTypePtr()->getArg(I).getKind()));
6727 }
6728 
6729 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6730   TL.setEllipsisLoc(readSourceLocation());
6731 }
6732 
6733 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6734   TL.setNameLoc(readSourceLocation());
6735 }
6736 
6737 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6738   if (TL.getNumProtocols()) {
6739     TL.setProtocolLAngleLoc(readSourceLocation());
6740     TL.setProtocolRAngleLoc(readSourceLocation());
6741   }
6742   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6743     TL.setProtocolLoc(i, readSourceLocation());
6744 }
6745 
6746 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6747   TL.setHasBaseTypeAsWritten(Reader.readBool());
6748   TL.setTypeArgsLAngleLoc(readSourceLocation());
6749   TL.setTypeArgsRAngleLoc(readSourceLocation());
6750   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6751     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6752   TL.setProtocolLAngleLoc(readSourceLocation());
6753   TL.setProtocolRAngleLoc(readSourceLocation());
6754   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6755     TL.setProtocolLoc(i, readSourceLocation());
6756 }
6757 
6758 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6759   TL.setStarLoc(readSourceLocation());
6760 }
6761 
6762 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6763   TL.setKWLoc(readSourceLocation());
6764   TL.setLParenLoc(readSourceLocation());
6765   TL.setRParenLoc(readSourceLocation());
6766 }
6767 
6768 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6769   TL.setKWLoc(readSourceLocation());
6770 }
6771 
6772 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6773   TL.setNameLoc(readSourceLocation());
6774 }
6775 void TypeLocReader::VisitDependentExtIntTypeLoc(
6776     clang::DependentExtIntTypeLoc TL) {
6777   TL.setNameLoc(readSourceLocation());
6778 }
6779 
6780 
6781 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6782   TypeLocReader TLR(*this);
6783   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6784     TLR.Visit(TL);
6785 }
6786 
6787 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6788   QualType InfoTy = readType();
6789   if (InfoTy.isNull())
6790     return nullptr;
6791 
6792   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6793   readTypeLoc(TInfo->getTypeLoc());
6794   return TInfo;
6795 }
6796 
6797 QualType ASTReader::GetType(TypeID ID) {
6798   assert(ContextObj && "reading type with no AST context");
6799   ASTContext &Context = *ContextObj;
6800 
6801   unsigned FastQuals = ID & Qualifiers::FastMask;
6802   unsigned Index = ID >> Qualifiers::FastWidth;
6803 
6804   if (Index < NUM_PREDEF_TYPE_IDS) {
6805     QualType T;
6806     switch ((PredefinedTypeIDs)Index) {
6807     case PREDEF_TYPE_NULL_ID:
6808       return QualType();
6809     case PREDEF_TYPE_VOID_ID:
6810       T = Context.VoidTy;
6811       break;
6812     case PREDEF_TYPE_BOOL_ID:
6813       T = Context.BoolTy;
6814       break;
6815     case PREDEF_TYPE_CHAR_U_ID:
6816     case PREDEF_TYPE_CHAR_S_ID:
6817       // FIXME: Check that the signedness of CharTy is correct!
6818       T = Context.CharTy;
6819       break;
6820     case PREDEF_TYPE_UCHAR_ID:
6821       T = Context.UnsignedCharTy;
6822       break;
6823     case PREDEF_TYPE_USHORT_ID:
6824       T = Context.UnsignedShortTy;
6825       break;
6826     case PREDEF_TYPE_UINT_ID:
6827       T = Context.UnsignedIntTy;
6828       break;
6829     case PREDEF_TYPE_ULONG_ID:
6830       T = Context.UnsignedLongTy;
6831       break;
6832     case PREDEF_TYPE_ULONGLONG_ID:
6833       T = Context.UnsignedLongLongTy;
6834       break;
6835     case PREDEF_TYPE_UINT128_ID:
6836       T = Context.UnsignedInt128Ty;
6837       break;
6838     case PREDEF_TYPE_SCHAR_ID:
6839       T = Context.SignedCharTy;
6840       break;
6841     case PREDEF_TYPE_WCHAR_ID:
6842       T = Context.WCharTy;
6843       break;
6844     case PREDEF_TYPE_SHORT_ID:
6845       T = Context.ShortTy;
6846       break;
6847     case PREDEF_TYPE_INT_ID:
6848       T = Context.IntTy;
6849       break;
6850     case PREDEF_TYPE_LONG_ID:
6851       T = Context.LongTy;
6852       break;
6853     case PREDEF_TYPE_LONGLONG_ID:
6854       T = Context.LongLongTy;
6855       break;
6856     case PREDEF_TYPE_INT128_ID:
6857       T = Context.Int128Ty;
6858       break;
6859     case PREDEF_TYPE_BFLOAT16_ID:
6860       T = Context.BFloat16Ty;
6861       break;
6862     case PREDEF_TYPE_HALF_ID:
6863       T = Context.HalfTy;
6864       break;
6865     case PREDEF_TYPE_FLOAT_ID:
6866       T = Context.FloatTy;
6867       break;
6868     case PREDEF_TYPE_DOUBLE_ID:
6869       T = Context.DoubleTy;
6870       break;
6871     case PREDEF_TYPE_LONGDOUBLE_ID:
6872       T = Context.LongDoubleTy;
6873       break;
6874     case PREDEF_TYPE_SHORT_ACCUM_ID:
6875       T = Context.ShortAccumTy;
6876       break;
6877     case PREDEF_TYPE_ACCUM_ID:
6878       T = Context.AccumTy;
6879       break;
6880     case PREDEF_TYPE_LONG_ACCUM_ID:
6881       T = Context.LongAccumTy;
6882       break;
6883     case PREDEF_TYPE_USHORT_ACCUM_ID:
6884       T = Context.UnsignedShortAccumTy;
6885       break;
6886     case PREDEF_TYPE_UACCUM_ID:
6887       T = Context.UnsignedAccumTy;
6888       break;
6889     case PREDEF_TYPE_ULONG_ACCUM_ID:
6890       T = Context.UnsignedLongAccumTy;
6891       break;
6892     case PREDEF_TYPE_SHORT_FRACT_ID:
6893       T = Context.ShortFractTy;
6894       break;
6895     case PREDEF_TYPE_FRACT_ID:
6896       T = Context.FractTy;
6897       break;
6898     case PREDEF_TYPE_LONG_FRACT_ID:
6899       T = Context.LongFractTy;
6900       break;
6901     case PREDEF_TYPE_USHORT_FRACT_ID:
6902       T = Context.UnsignedShortFractTy;
6903       break;
6904     case PREDEF_TYPE_UFRACT_ID:
6905       T = Context.UnsignedFractTy;
6906       break;
6907     case PREDEF_TYPE_ULONG_FRACT_ID:
6908       T = Context.UnsignedLongFractTy;
6909       break;
6910     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6911       T = Context.SatShortAccumTy;
6912       break;
6913     case PREDEF_TYPE_SAT_ACCUM_ID:
6914       T = Context.SatAccumTy;
6915       break;
6916     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6917       T = Context.SatLongAccumTy;
6918       break;
6919     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6920       T = Context.SatUnsignedShortAccumTy;
6921       break;
6922     case PREDEF_TYPE_SAT_UACCUM_ID:
6923       T = Context.SatUnsignedAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6926       T = Context.SatUnsignedLongAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6929       T = Context.SatShortFractTy;
6930       break;
6931     case PREDEF_TYPE_SAT_FRACT_ID:
6932       T = Context.SatFractTy;
6933       break;
6934     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6935       T = Context.SatLongFractTy;
6936       break;
6937     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6938       T = Context.SatUnsignedShortFractTy;
6939       break;
6940     case PREDEF_TYPE_SAT_UFRACT_ID:
6941       T = Context.SatUnsignedFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6944       T = Context.SatUnsignedLongFractTy;
6945       break;
6946     case PREDEF_TYPE_FLOAT16_ID:
6947       T = Context.Float16Ty;
6948       break;
6949     case PREDEF_TYPE_FLOAT128_ID:
6950       T = Context.Float128Ty;
6951       break;
6952     case PREDEF_TYPE_OVERLOAD_ID:
6953       T = Context.OverloadTy;
6954       break;
6955     case PREDEF_TYPE_BOUND_MEMBER:
6956       T = Context.BoundMemberTy;
6957       break;
6958     case PREDEF_TYPE_PSEUDO_OBJECT:
6959       T = Context.PseudoObjectTy;
6960       break;
6961     case PREDEF_TYPE_DEPENDENT_ID:
6962       T = Context.DependentTy;
6963       break;
6964     case PREDEF_TYPE_UNKNOWN_ANY:
6965       T = Context.UnknownAnyTy;
6966       break;
6967     case PREDEF_TYPE_NULLPTR_ID:
6968       T = Context.NullPtrTy;
6969       break;
6970     case PREDEF_TYPE_CHAR8_ID:
6971       T = Context.Char8Ty;
6972       break;
6973     case PREDEF_TYPE_CHAR16_ID:
6974       T = Context.Char16Ty;
6975       break;
6976     case PREDEF_TYPE_CHAR32_ID:
6977       T = Context.Char32Ty;
6978       break;
6979     case PREDEF_TYPE_OBJC_ID:
6980       T = Context.ObjCBuiltinIdTy;
6981       break;
6982     case PREDEF_TYPE_OBJC_CLASS:
6983       T = Context.ObjCBuiltinClassTy;
6984       break;
6985     case PREDEF_TYPE_OBJC_SEL:
6986       T = Context.ObjCBuiltinSelTy;
6987       break;
6988 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6989     case PREDEF_TYPE_##Id##_ID: \
6990       T = Context.SingletonId; \
6991       break;
6992 #include "clang/Basic/OpenCLImageTypes.def"
6993 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6994     case PREDEF_TYPE_##Id##_ID: \
6995       T = Context.Id##Ty; \
6996       break;
6997 #include "clang/Basic/OpenCLExtensionTypes.def"
6998     case PREDEF_TYPE_SAMPLER_ID:
6999       T = Context.OCLSamplerTy;
7000       break;
7001     case PREDEF_TYPE_EVENT_ID:
7002       T = Context.OCLEventTy;
7003       break;
7004     case PREDEF_TYPE_CLK_EVENT_ID:
7005       T = Context.OCLClkEventTy;
7006       break;
7007     case PREDEF_TYPE_QUEUE_ID:
7008       T = Context.OCLQueueTy;
7009       break;
7010     case PREDEF_TYPE_RESERVE_ID_ID:
7011       T = Context.OCLReserveIDTy;
7012       break;
7013     case PREDEF_TYPE_AUTO_DEDUCT:
7014       T = Context.getAutoDeductType();
7015       break;
7016     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7017       T = Context.getAutoRRefDeductType();
7018       break;
7019     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7020       T = Context.ARCUnbridgedCastTy;
7021       break;
7022     case PREDEF_TYPE_BUILTIN_FN:
7023       T = Context.BuiltinFnTy;
7024       break;
7025     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7026       T = Context.IncompleteMatrixIdxTy;
7027       break;
7028     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7029       T = Context.OMPArraySectionTy;
7030       break;
7031     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7032       T = Context.OMPArraySectionTy;
7033       break;
7034     case PREDEF_TYPE_OMP_ITERATOR:
7035       T = Context.OMPIteratorTy;
7036       break;
7037 #define SVE_TYPE(Name, Id, SingletonId) \
7038     case PREDEF_TYPE_##Id##_ID: \
7039       T = Context.SingletonId; \
7040       break;
7041 #include "clang/Basic/AArch64SVEACLETypes.def"
7042     }
7043 
7044     assert(!T.isNull() && "Unknown predefined type");
7045     return T.withFastQualifiers(FastQuals);
7046   }
7047 
7048   Index -= NUM_PREDEF_TYPE_IDS;
7049   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7050   if (TypesLoaded[Index].isNull()) {
7051     TypesLoaded[Index] = readTypeRecord(Index);
7052     if (TypesLoaded[Index].isNull())
7053       return QualType();
7054 
7055     TypesLoaded[Index]->setFromAST();
7056     if (DeserializationListener)
7057       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7058                                         TypesLoaded[Index]);
7059   }
7060 
7061   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7062 }
7063 
7064 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7065   return GetType(getGlobalTypeID(F, LocalID));
7066 }
7067 
7068 serialization::TypeID
7069 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7070   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7071   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7072 
7073   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7074     return LocalID;
7075 
7076   if (!F.ModuleOffsetMap.empty())
7077     ReadModuleOffsetMap(F);
7078 
7079   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7080     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7081   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7082 
7083   unsigned GlobalIndex = LocalIndex + I->second;
7084   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7085 }
7086 
7087 TemplateArgumentLocInfo
7088 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7089   switch (Kind) {
7090   case TemplateArgument::Expression:
7091     return readExpr();
7092   case TemplateArgument::Type:
7093     return readTypeSourceInfo();
7094   case TemplateArgument::Template: {
7095     NestedNameSpecifierLoc QualifierLoc =
7096       readNestedNameSpecifierLoc();
7097     SourceLocation TemplateNameLoc = readSourceLocation();
7098     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7099                                    TemplateNameLoc, SourceLocation());
7100   }
7101   case TemplateArgument::TemplateExpansion: {
7102     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7103     SourceLocation TemplateNameLoc = readSourceLocation();
7104     SourceLocation EllipsisLoc = readSourceLocation();
7105     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7106                                    TemplateNameLoc, EllipsisLoc);
7107   }
7108   case TemplateArgument::Null:
7109   case TemplateArgument::Integral:
7110   case TemplateArgument::Declaration:
7111   case TemplateArgument::NullPtr:
7112   case TemplateArgument::Pack:
7113     // FIXME: Is this right?
7114     return TemplateArgumentLocInfo();
7115   }
7116   llvm_unreachable("unexpected template argument loc");
7117 }
7118 
7119 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7120   TemplateArgument Arg = readTemplateArgument();
7121 
7122   if (Arg.getKind() == TemplateArgument::Expression) {
7123     if (readBool()) // bool InfoHasSameExpr.
7124       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7125   }
7126   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7127 }
7128 
7129 const ASTTemplateArgumentListInfo *
7130 ASTRecordReader::readASTTemplateArgumentListInfo() {
7131   SourceLocation LAngleLoc = readSourceLocation();
7132   SourceLocation RAngleLoc = readSourceLocation();
7133   unsigned NumArgsAsWritten = readInt();
7134   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7135   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7136     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7137   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7138 }
7139 
7140 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7141   return GetDecl(ID);
7142 }
7143 
7144 void ASTReader::CompleteRedeclChain(const Decl *D) {
7145   if (NumCurrentElementsDeserializing) {
7146     // We arrange to not care about the complete redeclaration chain while we're
7147     // deserializing. Just remember that the AST has marked this one as complete
7148     // but that it's not actually complete yet, so we know we still need to
7149     // complete it later.
7150     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7151     return;
7152   }
7153 
7154   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7155 
7156   // If this is a named declaration, complete it by looking it up
7157   // within its context.
7158   //
7159   // FIXME: Merging a function definition should merge
7160   // all mergeable entities within it.
7161   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7162       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7163     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7164       if (!getContext().getLangOpts().CPlusPlus &&
7165           isa<TranslationUnitDecl>(DC)) {
7166         // Outside of C++, we don't have a lookup table for the TU, so update
7167         // the identifier instead. (For C++ modules, we don't store decls
7168         // in the serialized identifier table, so we do the lookup in the TU.)
7169         auto *II = Name.getAsIdentifierInfo();
7170         assert(II && "non-identifier name in C?");
7171         if (II->isOutOfDate())
7172           updateOutOfDateIdentifier(*II);
7173       } else
7174         DC->lookup(Name);
7175     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7176       // Find all declarations of this kind from the relevant context.
7177       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7178         auto *DC = cast<DeclContext>(DCDecl);
7179         SmallVector<Decl*, 8> Decls;
7180         FindExternalLexicalDecls(
7181             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7182       }
7183     }
7184   }
7185 
7186   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7187     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7188   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7189     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7190   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7191     if (auto *Template = FD->getPrimaryTemplate())
7192       Template->LoadLazySpecializations();
7193   }
7194 }
7195 
7196 CXXCtorInitializer **
7197 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7198   RecordLocation Loc = getLocalBitOffset(Offset);
7199   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7200   SavedStreamPosition SavedPosition(Cursor);
7201   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7202     Error(std::move(Err));
7203     return nullptr;
7204   }
7205   ReadingKindTracker ReadingKind(Read_Decl, *this);
7206 
7207   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7208   if (!MaybeCode) {
7209     Error(MaybeCode.takeError());
7210     return nullptr;
7211   }
7212   unsigned Code = MaybeCode.get();
7213 
7214   ASTRecordReader Record(*this, *Loc.F);
7215   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7216   if (!MaybeRecCode) {
7217     Error(MaybeRecCode.takeError());
7218     return nullptr;
7219   }
7220   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7221     Error("malformed AST file: missing C++ ctor initializers");
7222     return nullptr;
7223   }
7224 
7225   return Record.readCXXCtorInitializers();
7226 }
7227 
7228 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7229   assert(ContextObj && "reading base specifiers with no AST context");
7230   ASTContext &Context = *ContextObj;
7231 
7232   RecordLocation Loc = getLocalBitOffset(Offset);
7233   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7234   SavedStreamPosition SavedPosition(Cursor);
7235   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7236     Error(std::move(Err));
7237     return nullptr;
7238   }
7239   ReadingKindTracker ReadingKind(Read_Decl, *this);
7240 
7241   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7242   if (!MaybeCode) {
7243     Error(MaybeCode.takeError());
7244     return nullptr;
7245   }
7246   unsigned Code = MaybeCode.get();
7247 
7248   ASTRecordReader Record(*this, *Loc.F);
7249   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7250   if (!MaybeRecCode) {
7251     Error(MaybeCode.takeError());
7252     return nullptr;
7253   }
7254   unsigned RecCode = MaybeRecCode.get();
7255 
7256   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7257     Error("malformed AST file: missing C++ base specifiers");
7258     return nullptr;
7259   }
7260 
7261   unsigned NumBases = Record.readInt();
7262   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7263   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7264   for (unsigned I = 0; I != NumBases; ++I)
7265     Bases[I] = Record.readCXXBaseSpecifier();
7266   return Bases;
7267 }
7268 
7269 serialization::DeclID
7270 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7271   if (LocalID < NUM_PREDEF_DECL_IDS)
7272     return LocalID;
7273 
7274   if (!F.ModuleOffsetMap.empty())
7275     ReadModuleOffsetMap(F);
7276 
7277   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7278     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7279   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7280 
7281   return LocalID + I->second;
7282 }
7283 
7284 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7285                                    ModuleFile &M) const {
7286   // Predefined decls aren't from any module.
7287   if (ID < NUM_PREDEF_DECL_IDS)
7288     return false;
7289 
7290   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7291          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7292 }
7293 
7294 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7295   if (!D->isFromASTFile())
7296     return nullptr;
7297   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7298   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7299   return I->second;
7300 }
7301 
7302 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7303   if (ID < NUM_PREDEF_DECL_IDS)
7304     return SourceLocation();
7305 
7306   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7307 
7308   if (Index > DeclsLoaded.size()) {
7309     Error("declaration ID out-of-range for AST file");
7310     return SourceLocation();
7311   }
7312 
7313   if (Decl *D = DeclsLoaded[Index])
7314     return D->getLocation();
7315 
7316   SourceLocation Loc;
7317   DeclCursorForID(ID, Loc);
7318   return Loc;
7319 }
7320 
7321 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7322   switch (ID) {
7323   case PREDEF_DECL_NULL_ID:
7324     return nullptr;
7325 
7326   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7327     return Context.getTranslationUnitDecl();
7328 
7329   case PREDEF_DECL_OBJC_ID_ID:
7330     return Context.getObjCIdDecl();
7331 
7332   case PREDEF_DECL_OBJC_SEL_ID:
7333     return Context.getObjCSelDecl();
7334 
7335   case PREDEF_DECL_OBJC_CLASS_ID:
7336     return Context.getObjCClassDecl();
7337 
7338   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7339     return Context.getObjCProtocolDecl();
7340 
7341   case PREDEF_DECL_INT_128_ID:
7342     return Context.getInt128Decl();
7343 
7344   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7345     return Context.getUInt128Decl();
7346 
7347   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7348     return Context.getObjCInstanceTypeDecl();
7349 
7350   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7351     return Context.getBuiltinVaListDecl();
7352 
7353   case PREDEF_DECL_VA_LIST_TAG:
7354     return Context.getVaListTagDecl();
7355 
7356   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7357     return Context.getBuiltinMSVaListDecl();
7358 
7359   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7360     return Context.getMSGuidTagDecl();
7361 
7362   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7363     return Context.getExternCContextDecl();
7364 
7365   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7366     return Context.getMakeIntegerSeqDecl();
7367 
7368   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7369     return Context.getCFConstantStringDecl();
7370 
7371   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7372     return Context.getCFConstantStringTagDecl();
7373 
7374   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7375     return Context.getTypePackElementDecl();
7376   }
7377   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7378 }
7379 
7380 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7381   assert(ContextObj && "reading decl with no AST context");
7382   if (ID < NUM_PREDEF_DECL_IDS) {
7383     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7384     if (D) {
7385       // Track that we have merged the declaration with ID \p ID into the
7386       // pre-existing predefined declaration \p D.
7387       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7388       if (Merged.empty())
7389         Merged.push_back(ID);
7390     }
7391     return D;
7392   }
7393 
7394   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7395 
7396   if (Index >= DeclsLoaded.size()) {
7397     assert(0 && "declaration ID out-of-range for AST file");
7398     Error("declaration ID out-of-range for AST file");
7399     return nullptr;
7400   }
7401 
7402   return DeclsLoaded[Index];
7403 }
7404 
7405 Decl *ASTReader::GetDecl(DeclID ID) {
7406   if (ID < NUM_PREDEF_DECL_IDS)
7407     return GetExistingDecl(ID);
7408 
7409   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7410 
7411   if (Index >= DeclsLoaded.size()) {
7412     assert(0 && "declaration ID out-of-range for AST file");
7413     Error("declaration ID out-of-range for AST file");
7414     return nullptr;
7415   }
7416 
7417   if (!DeclsLoaded[Index]) {
7418     ReadDeclRecord(ID);
7419     if (DeserializationListener)
7420       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7421   }
7422 
7423   return DeclsLoaded[Index];
7424 }
7425 
7426 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7427                                                   DeclID GlobalID) {
7428   if (GlobalID < NUM_PREDEF_DECL_IDS)
7429     return GlobalID;
7430 
7431   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7432   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7433   ModuleFile *Owner = I->second;
7434 
7435   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7436     = M.GlobalToLocalDeclIDs.find(Owner);
7437   if (Pos == M.GlobalToLocalDeclIDs.end())
7438     return 0;
7439 
7440   return GlobalID - Owner->BaseDeclID + Pos->second;
7441 }
7442 
7443 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7444                                             const RecordData &Record,
7445                                             unsigned &Idx) {
7446   if (Idx >= Record.size()) {
7447     Error("Corrupted AST file");
7448     return 0;
7449   }
7450 
7451   return getGlobalDeclID(F, Record[Idx++]);
7452 }
7453 
7454 /// Resolve the offset of a statement into a statement.
7455 ///
7456 /// This operation will read a new statement from the external
7457 /// source each time it is called, and is meant to be used via a
7458 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7459 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7460   // Switch case IDs are per Decl.
7461   ClearSwitchCaseIDs();
7462 
7463   // Offset here is a global offset across the entire chain.
7464   RecordLocation Loc = getLocalBitOffset(Offset);
7465   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7466     Error(std::move(Err));
7467     return nullptr;
7468   }
7469   assert(NumCurrentElementsDeserializing == 0 &&
7470          "should not be called while already deserializing");
7471   Deserializing D(this);
7472   return ReadStmtFromStream(*Loc.F);
7473 }
7474 
7475 void ASTReader::FindExternalLexicalDecls(
7476     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7477     SmallVectorImpl<Decl *> &Decls) {
7478   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7479 
7480   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7481     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7482     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7483       auto K = (Decl::Kind)+LexicalDecls[I];
7484       if (!IsKindWeWant(K))
7485         continue;
7486 
7487       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7488 
7489       // Don't add predefined declarations to the lexical context more
7490       // than once.
7491       if (ID < NUM_PREDEF_DECL_IDS) {
7492         if (PredefsVisited[ID])
7493           continue;
7494 
7495         PredefsVisited[ID] = true;
7496       }
7497 
7498       if (Decl *D = GetLocalDecl(*M, ID)) {
7499         assert(D->getKind() == K && "wrong kind for lexical decl");
7500         if (!DC->isDeclInLexicalTraversal(D))
7501           Decls.push_back(D);
7502       }
7503     }
7504   };
7505 
7506   if (isa<TranslationUnitDecl>(DC)) {
7507     for (auto Lexical : TULexicalDecls)
7508       Visit(Lexical.first, Lexical.second);
7509   } else {
7510     auto I = LexicalDecls.find(DC);
7511     if (I != LexicalDecls.end())
7512       Visit(I->second.first, I->second.second);
7513   }
7514 
7515   ++NumLexicalDeclContextsRead;
7516 }
7517 
7518 namespace {
7519 
7520 class DeclIDComp {
7521   ASTReader &Reader;
7522   ModuleFile &Mod;
7523 
7524 public:
7525   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7526 
7527   bool operator()(LocalDeclID L, LocalDeclID R) const {
7528     SourceLocation LHS = getLocation(L);
7529     SourceLocation RHS = getLocation(R);
7530     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7531   }
7532 
7533   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7534     SourceLocation RHS = getLocation(R);
7535     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7536   }
7537 
7538   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7539     SourceLocation LHS = getLocation(L);
7540     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7541   }
7542 
7543   SourceLocation getLocation(LocalDeclID ID) const {
7544     return Reader.getSourceManager().getFileLoc(
7545             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7546   }
7547 };
7548 
7549 } // namespace
7550 
7551 void ASTReader::FindFileRegionDecls(FileID File,
7552                                     unsigned Offset, unsigned Length,
7553                                     SmallVectorImpl<Decl *> &Decls) {
7554   SourceManager &SM = getSourceManager();
7555 
7556   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7557   if (I == FileDeclIDs.end())
7558     return;
7559 
7560   FileDeclsInfo &DInfo = I->second;
7561   if (DInfo.Decls.empty())
7562     return;
7563 
7564   SourceLocation
7565     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7566   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7567 
7568   DeclIDComp DIDComp(*this, *DInfo.Mod);
7569   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7570       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7571   if (BeginIt != DInfo.Decls.begin())
7572     --BeginIt;
7573 
7574   // If we are pointing at a top-level decl inside an objc container, we need
7575   // to backtrack until we find it otherwise we will fail to report that the
7576   // region overlaps with an objc container.
7577   while (BeginIt != DInfo.Decls.begin() &&
7578          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7579              ->isTopLevelDeclInObjCContainer())
7580     --BeginIt;
7581 
7582   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7583       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7584   if (EndIt != DInfo.Decls.end())
7585     ++EndIt;
7586 
7587   for (ArrayRef<serialization::LocalDeclID>::iterator
7588          DIt = BeginIt; DIt != EndIt; ++DIt)
7589     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7590 }
7591 
7592 bool
7593 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7594                                           DeclarationName Name) {
7595   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7596          "DeclContext has no visible decls in storage");
7597   if (!Name)
7598     return false;
7599 
7600   auto It = Lookups.find(DC);
7601   if (It == Lookups.end())
7602     return false;
7603 
7604   Deserializing LookupResults(this);
7605 
7606   // Load the list of declarations.
7607   SmallVector<NamedDecl *, 64> Decls;
7608   for (DeclID ID : It->second.Table.find(Name)) {
7609     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7610     if (ND->getDeclName() == Name)
7611       Decls.push_back(ND);
7612   }
7613 
7614   ++NumVisibleDeclContextsRead;
7615   SetExternalVisibleDeclsForName(DC, Name, Decls);
7616   return !Decls.empty();
7617 }
7618 
7619 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7620   if (!DC->hasExternalVisibleStorage())
7621     return;
7622 
7623   auto It = Lookups.find(DC);
7624   assert(It != Lookups.end() &&
7625          "have external visible storage but no lookup tables");
7626 
7627   DeclsMap Decls;
7628 
7629   for (DeclID ID : It->second.Table.findAll()) {
7630     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7631     Decls[ND->getDeclName()].push_back(ND);
7632   }
7633 
7634   ++NumVisibleDeclContextsRead;
7635 
7636   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7637     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7638   }
7639   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7640 }
7641 
7642 const serialization::reader::DeclContextLookupTable *
7643 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7644   auto I = Lookups.find(Primary);
7645   return I == Lookups.end() ? nullptr : &I->second;
7646 }
7647 
7648 /// Under non-PCH compilation the consumer receives the objc methods
7649 /// before receiving the implementation, and codegen depends on this.
7650 /// We simulate this by deserializing and passing to consumer the methods of the
7651 /// implementation before passing the deserialized implementation decl.
7652 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7653                                        ASTConsumer *Consumer) {
7654   assert(ImplD && Consumer);
7655 
7656   for (auto *I : ImplD->methods())
7657     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7658 
7659   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7660 }
7661 
7662 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7663   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7664     PassObjCImplDeclToConsumer(ImplD, Consumer);
7665   else
7666     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7667 }
7668 
7669 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7670   this->Consumer = Consumer;
7671 
7672   if (Consumer)
7673     PassInterestingDeclsToConsumer();
7674 
7675   if (DeserializationListener)
7676     DeserializationListener->ReaderInitialized(this);
7677 }
7678 
7679 void ASTReader::PrintStats() {
7680   std::fprintf(stderr, "*** AST File Statistics:\n");
7681 
7682   unsigned NumTypesLoaded
7683     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7684                                       QualType());
7685   unsigned NumDeclsLoaded
7686     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7687                                       (Decl *)nullptr);
7688   unsigned NumIdentifiersLoaded
7689     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7690                                             IdentifiersLoaded.end(),
7691                                             (IdentifierInfo *)nullptr);
7692   unsigned NumMacrosLoaded
7693     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7694                                        MacrosLoaded.end(),
7695                                        (MacroInfo *)nullptr);
7696   unsigned NumSelectorsLoaded
7697     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7698                                           SelectorsLoaded.end(),
7699                                           Selector());
7700 
7701   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7702     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7703                  NumSLocEntriesRead, TotalNumSLocEntries,
7704                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7705   if (!TypesLoaded.empty())
7706     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7707                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7708                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7709   if (!DeclsLoaded.empty())
7710     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7711                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7712                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7713   if (!IdentifiersLoaded.empty())
7714     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7715                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7716                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7717   if (!MacrosLoaded.empty())
7718     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7719                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7720                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7721   if (!SelectorsLoaded.empty())
7722     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7723                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7724                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7725   if (TotalNumStatements)
7726     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7727                  NumStatementsRead, TotalNumStatements,
7728                  ((float)NumStatementsRead/TotalNumStatements * 100));
7729   if (TotalNumMacros)
7730     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7731                  NumMacrosRead, TotalNumMacros,
7732                  ((float)NumMacrosRead/TotalNumMacros * 100));
7733   if (TotalLexicalDeclContexts)
7734     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7735                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7736                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7737                   * 100));
7738   if (TotalVisibleDeclContexts)
7739     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7740                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7741                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7742                   * 100));
7743   if (TotalNumMethodPoolEntries)
7744     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7745                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7746                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7747                   * 100));
7748   if (NumMethodPoolLookups)
7749     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7750                  NumMethodPoolHits, NumMethodPoolLookups,
7751                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7752   if (NumMethodPoolTableLookups)
7753     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7754                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7755                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7756                   * 100.0));
7757   if (NumIdentifierLookupHits)
7758     std::fprintf(stderr,
7759                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7760                  NumIdentifierLookupHits, NumIdentifierLookups,
7761                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7762 
7763   if (GlobalIndex) {
7764     std::fprintf(stderr, "\n");
7765     GlobalIndex->printStats();
7766   }
7767 
7768   std::fprintf(stderr, "\n");
7769   dump();
7770   std::fprintf(stderr, "\n");
7771 }
7772 
7773 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7774 LLVM_DUMP_METHOD static void
7775 dumpModuleIDMap(StringRef Name,
7776                 const ContinuousRangeMap<Key, ModuleFile *,
7777                                          InitialCapacity> &Map) {
7778   if (Map.begin() == Map.end())
7779     return;
7780 
7781   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7782 
7783   llvm::errs() << Name << ":\n";
7784   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7785        I != IEnd; ++I) {
7786     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7787       << "\n";
7788   }
7789 }
7790 
7791 LLVM_DUMP_METHOD void ASTReader::dump() {
7792   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7793   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7794   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7795   dumpModuleIDMap("Global type map", GlobalTypeMap);
7796   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7797   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7798   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7799   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7800   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7801   dumpModuleIDMap("Global preprocessed entity map",
7802                   GlobalPreprocessedEntityMap);
7803 
7804   llvm::errs() << "\n*** PCH/Modules Loaded:";
7805   for (ModuleFile &M : ModuleMgr)
7806     M.dump();
7807 }
7808 
7809 /// Return the amount of memory used by memory buffers, breaking down
7810 /// by heap-backed versus mmap'ed memory.
7811 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7812   for (ModuleFile &I : ModuleMgr) {
7813     if (llvm::MemoryBuffer *buf = I.Buffer) {
7814       size_t bytes = buf->getBufferSize();
7815       switch (buf->getBufferKind()) {
7816         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7817           sizes.malloc_bytes += bytes;
7818           break;
7819         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7820           sizes.mmap_bytes += bytes;
7821           break;
7822       }
7823     }
7824   }
7825 }
7826 
7827 void ASTReader::InitializeSema(Sema &S) {
7828   SemaObj = &S;
7829   S.addExternalSource(this);
7830 
7831   // Makes sure any declarations that were deserialized "too early"
7832   // still get added to the identifier's declaration chains.
7833   for (uint64_t ID : PreloadedDeclIDs) {
7834     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7835     pushExternalDeclIntoScope(D, D->getDeclName());
7836   }
7837   PreloadedDeclIDs.clear();
7838 
7839   // FIXME: What happens if these are changed by a module import?
7840   if (!FPPragmaOptions.empty()) {
7841     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7842     FPOptionsOverride NewOverrides =
7843         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7844     SemaObj->CurFPFeatures =
7845         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7846   }
7847 
7848   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7849   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7850   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7851 
7852   UpdateSema();
7853 }
7854 
7855 void ASTReader::UpdateSema() {
7856   assert(SemaObj && "no Sema to update");
7857 
7858   // Load the offsets of the declarations that Sema references.
7859   // They will be lazily deserialized when needed.
7860   if (!SemaDeclRefs.empty()) {
7861     assert(SemaDeclRefs.size() % 3 == 0);
7862     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7863       if (!SemaObj->StdNamespace)
7864         SemaObj->StdNamespace = SemaDeclRefs[I];
7865       if (!SemaObj->StdBadAlloc)
7866         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7867       if (!SemaObj->StdAlignValT)
7868         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7869     }
7870     SemaDeclRefs.clear();
7871   }
7872 
7873   // Update the state of pragmas. Use the same API as if we had encountered the
7874   // pragma in the source.
7875   if(OptimizeOffPragmaLocation.isValid())
7876     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7877   if (PragmaMSStructState != -1)
7878     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7879   if (PointersToMembersPragmaLocation.isValid()) {
7880     SemaObj->ActOnPragmaMSPointersToMembers(
7881         (LangOptions::PragmaMSPointersToMembersKind)
7882             PragmaMSPointersToMembersState,
7883         PointersToMembersPragmaLocation);
7884   }
7885   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7886 
7887   if (PragmaPackCurrentValue) {
7888     // The bottom of the stack might have a default value. It must be adjusted
7889     // to the current value to ensure that the packing state is preserved after
7890     // popping entries that were included/imported from a PCH/module.
7891     bool DropFirst = false;
7892     if (!PragmaPackStack.empty() &&
7893         PragmaPackStack.front().Location.isInvalid()) {
7894       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7895              "Expected a default alignment value");
7896       SemaObj->PackStack.Stack.emplace_back(
7897           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7898           SemaObj->PackStack.CurrentPragmaLocation,
7899           PragmaPackStack.front().PushLocation);
7900       DropFirst = true;
7901     }
7902     for (const auto &Entry :
7903          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7904       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7905                                             Entry.Location, Entry.PushLocation);
7906     if (PragmaPackCurrentLocation.isInvalid()) {
7907       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7908              "Expected a default alignment value");
7909       // Keep the current values.
7910     } else {
7911       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7912       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7913     }
7914   }
7915   if (FpPragmaCurrentValue) {
7916     // The bottom of the stack might have a default value. It must be adjusted
7917     // to the current value to ensure that fp-pragma state is preserved after
7918     // popping entries that were included/imported from a PCH/module.
7919     bool DropFirst = false;
7920     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7921       assert(FpPragmaStack.front().Value ==
7922                  SemaObj->FpPragmaStack.DefaultValue &&
7923              "Expected a default pragma float_control value");
7924       SemaObj->FpPragmaStack.Stack.emplace_back(
7925           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7926           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7927           FpPragmaStack.front().PushLocation);
7928       DropFirst = true;
7929     }
7930     for (const auto &Entry :
7931          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7932       SemaObj->FpPragmaStack.Stack.emplace_back(
7933           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7934     if (FpPragmaCurrentLocation.isInvalid()) {
7935       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7936              "Expected a default pragma float_control value");
7937       // Keep the current values.
7938     } else {
7939       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7940       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7941     }
7942   }
7943 
7944   // For non-modular AST files, restore visiblity of modules.
7945   for (auto &Import : ImportedModules) {
7946     if (Import.ImportLoc.isInvalid())
7947       continue;
7948     if (Module *Imported = getSubmodule(Import.ID)) {
7949       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7950     }
7951   }
7952 }
7953 
7954 IdentifierInfo *ASTReader::get(StringRef Name) {
7955   // Note that we are loading an identifier.
7956   Deserializing AnIdentifier(this);
7957 
7958   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7959                                   NumIdentifierLookups,
7960                                   NumIdentifierLookupHits);
7961 
7962   // We don't need to do identifier table lookups in C++ modules (we preload
7963   // all interesting declarations, and don't need to use the scope for name
7964   // lookups). Perform the lookup in PCH files, though, since we don't build
7965   // a complete initial identifier table if we're carrying on from a PCH.
7966   if (PP.getLangOpts().CPlusPlus) {
7967     for (auto F : ModuleMgr.pch_modules())
7968       if (Visitor(*F))
7969         break;
7970   } else {
7971     // If there is a global index, look there first to determine which modules
7972     // provably do not have any results for this identifier.
7973     GlobalModuleIndex::HitSet Hits;
7974     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7975     if (!loadGlobalIndex()) {
7976       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7977         HitsPtr = &Hits;
7978       }
7979     }
7980 
7981     ModuleMgr.visit(Visitor, HitsPtr);
7982   }
7983 
7984   IdentifierInfo *II = Visitor.getIdentifierInfo();
7985   markIdentifierUpToDate(II);
7986   return II;
7987 }
7988 
7989 namespace clang {
7990 
7991   /// An identifier-lookup iterator that enumerates all of the
7992   /// identifiers stored within a set of AST files.
7993   class ASTIdentifierIterator : public IdentifierIterator {
7994     /// The AST reader whose identifiers are being enumerated.
7995     const ASTReader &Reader;
7996 
7997     /// The current index into the chain of AST files stored in
7998     /// the AST reader.
7999     unsigned Index;
8000 
8001     /// The current position within the identifier lookup table
8002     /// of the current AST file.
8003     ASTIdentifierLookupTable::key_iterator Current;
8004 
8005     /// The end position within the identifier lookup table of
8006     /// the current AST file.
8007     ASTIdentifierLookupTable::key_iterator End;
8008 
8009     /// Whether to skip any modules in the ASTReader.
8010     bool SkipModules;
8011 
8012   public:
8013     explicit ASTIdentifierIterator(const ASTReader &Reader,
8014                                    bool SkipModules = false);
8015 
8016     StringRef Next() override;
8017   };
8018 
8019 } // namespace clang
8020 
8021 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8022                                              bool SkipModules)
8023     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8024 }
8025 
8026 StringRef ASTIdentifierIterator::Next() {
8027   while (Current == End) {
8028     // If we have exhausted all of our AST files, we're done.
8029     if (Index == 0)
8030       return StringRef();
8031 
8032     --Index;
8033     ModuleFile &F = Reader.ModuleMgr[Index];
8034     if (SkipModules && F.isModule())
8035       continue;
8036 
8037     ASTIdentifierLookupTable *IdTable =
8038         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8039     Current = IdTable->key_begin();
8040     End = IdTable->key_end();
8041   }
8042 
8043   // We have any identifiers remaining in the current AST file; return
8044   // the next one.
8045   StringRef Result = *Current;
8046   ++Current;
8047   return Result;
8048 }
8049 
8050 namespace {
8051 
8052 /// A utility for appending two IdentifierIterators.
8053 class ChainedIdentifierIterator : public IdentifierIterator {
8054   std::unique_ptr<IdentifierIterator> Current;
8055   std::unique_ptr<IdentifierIterator> Queued;
8056 
8057 public:
8058   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8059                             std::unique_ptr<IdentifierIterator> Second)
8060       : Current(std::move(First)), Queued(std::move(Second)) {}
8061 
8062   StringRef Next() override {
8063     if (!Current)
8064       return StringRef();
8065 
8066     StringRef result = Current->Next();
8067     if (!result.empty())
8068       return result;
8069 
8070     // Try the queued iterator, which may itself be empty.
8071     Current.reset();
8072     std::swap(Current, Queued);
8073     return Next();
8074   }
8075 };
8076 
8077 } // namespace
8078 
8079 IdentifierIterator *ASTReader::getIdentifiers() {
8080   if (!loadGlobalIndex()) {
8081     std::unique_ptr<IdentifierIterator> ReaderIter(
8082         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8083     std::unique_ptr<IdentifierIterator> ModulesIter(
8084         GlobalIndex->createIdentifierIterator());
8085     return new ChainedIdentifierIterator(std::move(ReaderIter),
8086                                          std::move(ModulesIter));
8087   }
8088 
8089   return new ASTIdentifierIterator(*this);
8090 }
8091 
8092 namespace clang {
8093 namespace serialization {
8094 
8095   class ReadMethodPoolVisitor {
8096     ASTReader &Reader;
8097     Selector Sel;
8098     unsigned PriorGeneration;
8099     unsigned InstanceBits = 0;
8100     unsigned FactoryBits = 0;
8101     bool InstanceHasMoreThanOneDecl = false;
8102     bool FactoryHasMoreThanOneDecl = false;
8103     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8104     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8105 
8106   public:
8107     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8108                           unsigned PriorGeneration)
8109         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8110 
8111     bool operator()(ModuleFile &M) {
8112       if (!M.SelectorLookupTable)
8113         return false;
8114 
8115       // If we've already searched this module file, skip it now.
8116       if (M.Generation <= PriorGeneration)
8117         return true;
8118 
8119       ++Reader.NumMethodPoolTableLookups;
8120       ASTSelectorLookupTable *PoolTable
8121         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8122       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8123       if (Pos == PoolTable->end())
8124         return false;
8125 
8126       ++Reader.NumMethodPoolTableHits;
8127       ++Reader.NumSelectorsRead;
8128       // FIXME: Not quite happy with the statistics here. We probably should
8129       // disable this tracking when called via LoadSelector.
8130       // Also, should entries without methods count as misses?
8131       ++Reader.NumMethodPoolEntriesRead;
8132       ASTSelectorLookupTrait::data_type Data = *Pos;
8133       if (Reader.DeserializationListener)
8134         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8135 
8136       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8137       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8138       InstanceBits = Data.InstanceBits;
8139       FactoryBits = Data.FactoryBits;
8140       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8141       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8142       return true;
8143     }
8144 
8145     /// Retrieve the instance methods found by this visitor.
8146     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8147       return InstanceMethods;
8148     }
8149 
8150     /// Retrieve the instance methods found by this visitor.
8151     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8152       return FactoryMethods;
8153     }
8154 
8155     unsigned getInstanceBits() const { return InstanceBits; }
8156     unsigned getFactoryBits() const { return FactoryBits; }
8157 
8158     bool instanceHasMoreThanOneDecl() const {
8159       return InstanceHasMoreThanOneDecl;
8160     }
8161 
8162     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8163   };
8164 
8165 } // namespace serialization
8166 } // namespace clang
8167 
8168 /// Add the given set of methods to the method list.
8169 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8170                              ObjCMethodList &List) {
8171   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8172     S.addMethodToGlobalList(&List, Methods[I]);
8173   }
8174 }
8175 
8176 void ASTReader::ReadMethodPool(Selector Sel) {
8177   // Get the selector generation and update it to the current generation.
8178   unsigned &Generation = SelectorGeneration[Sel];
8179   unsigned PriorGeneration = Generation;
8180   Generation = getGeneration();
8181   SelectorOutOfDate[Sel] = false;
8182 
8183   // Search for methods defined with this selector.
8184   ++NumMethodPoolLookups;
8185   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8186   ModuleMgr.visit(Visitor);
8187 
8188   if (Visitor.getInstanceMethods().empty() &&
8189       Visitor.getFactoryMethods().empty())
8190     return;
8191 
8192   ++NumMethodPoolHits;
8193 
8194   if (!getSema())
8195     return;
8196 
8197   Sema &S = *getSema();
8198   Sema::GlobalMethodPool::iterator Pos
8199     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8200 
8201   Pos->second.first.setBits(Visitor.getInstanceBits());
8202   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8203   Pos->second.second.setBits(Visitor.getFactoryBits());
8204   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8205 
8206   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8207   // when building a module we keep every method individually and may need to
8208   // update hasMoreThanOneDecl as we add the methods.
8209   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8210   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8211 }
8212 
8213 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8214   if (SelectorOutOfDate[Sel])
8215     ReadMethodPool(Sel);
8216 }
8217 
8218 void ASTReader::ReadKnownNamespaces(
8219                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8220   Namespaces.clear();
8221 
8222   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8223     if (NamespaceDecl *Namespace
8224                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8225       Namespaces.push_back(Namespace);
8226   }
8227 }
8228 
8229 void ASTReader::ReadUndefinedButUsed(
8230     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8231   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8232     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8233     SourceLocation Loc =
8234         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8235     Undefined.insert(std::make_pair(D, Loc));
8236   }
8237 }
8238 
8239 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8240     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8241                                                      Exprs) {
8242   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8243     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8244     uint64_t Count = DelayedDeleteExprs[Idx++];
8245     for (uint64_t C = 0; C < Count; ++C) {
8246       SourceLocation DeleteLoc =
8247           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8248       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8249       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8250     }
8251   }
8252 }
8253 
8254 void ASTReader::ReadTentativeDefinitions(
8255                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8256   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8257     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8258     if (Var)
8259       TentativeDefs.push_back(Var);
8260   }
8261   TentativeDefinitions.clear();
8262 }
8263 
8264 void ASTReader::ReadUnusedFileScopedDecls(
8265                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8266   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8267     DeclaratorDecl *D
8268       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8269     if (D)
8270       Decls.push_back(D);
8271   }
8272   UnusedFileScopedDecls.clear();
8273 }
8274 
8275 void ASTReader::ReadDelegatingConstructors(
8276                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8277   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8278     CXXConstructorDecl *D
8279       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8280     if (D)
8281       Decls.push_back(D);
8282   }
8283   DelegatingCtorDecls.clear();
8284 }
8285 
8286 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8287   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8288     TypedefNameDecl *D
8289       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8290     if (D)
8291       Decls.push_back(D);
8292   }
8293   ExtVectorDecls.clear();
8294 }
8295 
8296 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8297     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8298   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8299        ++I) {
8300     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8301         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8302     if (D)
8303       Decls.insert(D);
8304   }
8305   UnusedLocalTypedefNameCandidates.clear();
8306 }
8307 
8308 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8309     llvm::SmallVector<Decl *, 4> &Decls) {
8310   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8311        ++I) {
8312     auto *D = dyn_cast_or_null<Decl>(
8313         GetDecl(DeclsToCheckForDeferredDiags[I]));
8314     if (D)
8315       Decls.push_back(D);
8316   }
8317   DeclsToCheckForDeferredDiags.clear();
8318 }
8319 
8320 
8321 void ASTReader::ReadReferencedSelectors(
8322        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8323   if (ReferencedSelectorsData.empty())
8324     return;
8325 
8326   // If there are @selector references added them to its pool. This is for
8327   // implementation of -Wselector.
8328   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8329   unsigned I = 0;
8330   while (I < DataSize) {
8331     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8332     SourceLocation SelLoc
8333       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8334     Sels.push_back(std::make_pair(Sel, SelLoc));
8335   }
8336   ReferencedSelectorsData.clear();
8337 }
8338 
8339 void ASTReader::ReadWeakUndeclaredIdentifiers(
8340        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8341   if (WeakUndeclaredIdentifiers.empty())
8342     return;
8343 
8344   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8345     IdentifierInfo *WeakId
8346       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8347     IdentifierInfo *AliasId
8348       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8349     SourceLocation Loc
8350       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8351     bool Used = WeakUndeclaredIdentifiers[I++];
8352     WeakInfo WI(AliasId, Loc);
8353     WI.setUsed(Used);
8354     WeakIDs.push_back(std::make_pair(WeakId, WI));
8355   }
8356   WeakUndeclaredIdentifiers.clear();
8357 }
8358 
8359 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8360   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8361     ExternalVTableUse VT;
8362     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8363     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8364     VT.DefinitionRequired = VTableUses[Idx++];
8365     VTables.push_back(VT);
8366   }
8367 
8368   VTableUses.clear();
8369 }
8370 
8371 void ASTReader::ReadPendingInstantiations(
8372        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8373   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8374     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8375     SourceLocation Loc
8376       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8377 
8378     Pending.push_back(std::make_pair(D, Loc));
8379   }
8380   PendingInstantiations.clear();
8381 }
8382 
8383 void ASTReader::ReadLateParsedTemplates(
8384     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8385         &LPTMap) {
8386   for (auto &LPT : LateParsedTemplates) {
8387     ModuleFile *FMod = LPT.first;
8388     RecordDataImpl &LateParsed = LPT.second;
8389     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8390          /* In loop */) {
8391       FunctionDecl *FD =
8392           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8393 
8394       auto LT = std::make_unique<LateParsedTemplate>();
8395       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8396 
8397       ModuleFile *F = getOwningModuleFile(LT->D);
8398       assert(F && "No module");
8399 
8400       unsigned TokN = LateParsed[Idx++];
8401       LT->Toks.reserve(TokN);
8402       for (unsigned T = 0; T < TokN; ++T)
8403         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8404 
8405       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8406     }
8407   }
8408 }
8409 
8410 void ASTReader::LoadSelector(Selector Sel) {
8411   // It would be complicated to avoid reading the methods anyway. So don't.
8412   ReadMethodPool(Sel);
8413 }
8414 
8415 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8416   assert(ID && "Non-zero identifier ID required");
8417   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8418   IdentifiersLoaded[ID - 1] = II;
8419   if (DeserializationListener)
8420     DeserializationListener->IdentifierRead(ID, II);
8421 }
8422 
8423 /// Set the globally-visible declarations associated with the given
8424 /// identifier.
8425 ///
8426 /// If the AST reader is currently in a state where the given declaration IDs
8427 /// cannot safely be resolved, they are queued until it is safe to resolve
8428 /// them.
8429 ///
8430 /// \param II an IdentifierInfo that refers to one or more globally-visible
8431 /// declarations.
8432 ///
8433 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8434 /// visible at global scope.
8435 ///
8436 /// \param Decls if non-null, this vector will be populated with the set of
8437 /// deserialized declarations. These declarations will not be pushed into
8438 /// scope.
8439 void
8440 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8441                               const SmallVectorImpl<uint32_t> &DeclIDs,
8442                                    SmallVectorImpl<Decl *> *Decls) {
8443   if (NumCurrentElementsDeserializing && !Decls) {
8444     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8445     return;
8446   }
8447 
8448   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8449     if (!SemaObj) {
8450       // Queue this declaration so that it will be added to the
8451       // translation unit scope and identifier's declaration chain
8452       // once a Sema object is known.
8453       PreloadedDeclIDs.push_back(DeclIDs[I]);
8454       continue;
8455     }
8456 
8457     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8458 
8459     // If we're simply supposed to record the declarations, do so now.
8460     if (Decls) {
8461       Decls->push_back(D);
8462       continue;
8463     }
8464 
8465     // Introduce this declaration into the translation-unit scope
8466     // and add it to the declaration chain for this identifier, so
8467     // that (unqualified) name lookup will find it.
8468     pushExternalDeclIntoScope(D, II);
8469   }
8470 }
8471 
8472 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8473   if (ID == 0)
8474     return nullptr;
8475 
8476   if (IdentifiersLoaded.empty()) {
8477     Error("no identifier table in AST file");
8478     return nullptr;
8479   }
8480 
8481   ID -= 1;
8482   if (!IdentifiersLoaded[ID]) {
8483     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8484     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8485     ModuleFile *M = I->second;
8486     unsigned Index = ID - M->BaseIdentifierID;
8487     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8488 
8489     // All of the strings in the AST file are preceded by a 16-bit length.
8490     // Extract that 16-bit length to avoid having to execute strlen().
8491     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8492     //  unsigned integers.  This is important to avoid integer overflow when
8493     //  we cast them to 'unsigned'.
8494     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8495     unsigned StrLen = (((unsigned) StrLenPtr[0])
8496                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8497     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8498     IdentifiersLoaded[ID] = &II;
8499     markIdentifierFromAST(*this,  II);
8500     if (DeserializationListener)
8501       DeserializationListener->IdentifierRead(ID + 1, &II);
8502   }
8503 
8504   return IdentifiersLoaded[ID];
8505 }
8506 
8507 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8508   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8509 }
8510 
8511 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8512   if (LocalID < NUM_PREDEF_IDENT_IDS)
8513     return LocalID;
8514 
8515   if (!M.ModuleOffsetMap.empty())
8516     ReadModuleOffsetMap(M);
8517 
8518   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8519     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8520   assert(I != M.IdentifierRemap.end()
8521          && "Invalid index into identifier index remap");
8522 
8523   return LocalID + I->second;
8524 }
8525 
8526 MacroInfo *ASTReader::getMacro(MacroID ID) {
8527   if (ID == 0)
8528     return nullptr;
8529 
8530   if (MacrosLoaded.empty()) {
8531     Error("no macro table in AST file");
8532     return nullptr;
8533   }
8534 
8535   ID -= NUM_PREDEF_MACRO_IDS;
8536   if (!MacrosLoaded[ID]) {
8537     GlobalMacroMapType::iterator I
8538       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8539     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8540     ModuleFile *M = I->second;
8541     unsigned Index = ID - M->BaseMacroID;
8542     MacrosLoaded[ID] =
8543         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8544 
8545     if (DeserializationListener)
8546       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8547                                          MacrosLoaded[ID]);
8548   }
8549 
8550   return MacrosLoaded[ID];
8551 }
8552 
8553 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8554   if (LocalID < NUM_PREDEF_MACRO_IDS)
8555     return LocalID;
8556 
8557   if (!M.ModuleOffsetMap.empty())
8558     ReadModuleOffsetMap(M);
8559 
8560   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8561     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8562   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8563 
8564   return LocalID + I->second;
8565 }
8566 
8567 serialization::SubmoduleID
8568 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8569   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8570     return LocalID;
8571 
8572   if (!M.ModuleOffsetMap.empty())
8573     ReadModuleOffsetMap(M);
8574 
8575   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8576     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8577   assert(I != M.SubmoduleRemap.end()
8578          && "Invalid index into submodule index remap");
8579 
8580   return LocalID + I->second;
8581 }
8582 
8583 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8584   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8585     assert(GlobalID == 0 && "Unhandled global submodule ID");
8586     return nullptr;
8587   }
8588 
8589   if (GlobalID > SubmodulesLoaded.size()) {
8590     Error("submodule ID out of range in AST file");
8591     return nullptr;
8592   }
8593 
8594   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8595 }
8596 
8597 Module *ASTReader::getModule(unsigned ID) {
8598   return getSubmodule(ID);
8599 }
8600 
8601 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8602   if (ID & 1) {
8603     // It's a module, look it up by submodule ID.
8604     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8605     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8606   } else {
8607     // It's a prefix (preamble, PCH, ...). Look it up by index.
8608     unsigned IndexFromEnd = ID >> 1;
8609     assert(IndexFromEnd && "got reference to unknown module file");
8610     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8611   }
8612 }
8613 
8614 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8615   if (!F)
8616     return 1;
8617 
8618   // For a file representing a module, use the submodule ID of the top-level
8619   // module as the file ID. For any other kind of file, the number of such
8620   // files loaded beforehand will be the same on reload.
8621   // FIXME: Is this true even if we have an explicit module file and a PCH?
8622   if (F->isModule())
8623     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8624 
8625   auto PCHModules = getModuleManager().pch_modules();
8626   auto I = llvm::find(PCHModules, F);
8627   assert(I != PCHModules.end() && "emitting reference to unknown file");
8628   return (I - PCHModules.end()) << 1;
8629 }
8630 
8631 llvm::Optional<ASTSourceDescriptor>
8632 ASTReader::getSourceDescriptor(unsigned ID) {
8633   if (Module *M = getSubmodule(ID))
8634     return ASTSourceDescriptor(*M);
8635 
8636   // If there is only a single PCH, return it instead.
8637   // Chained PCH are not supported.
8638   const auto &PCHChain = ModuleMgr.pch_modules();
8639   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8640     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8641     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8642     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8643     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8644                                MF.Signature);
8645   }
8646   return None;
8647 }
8648 
8649 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8650   auto I = DefinitionSource.find(FD);
8651   if (I == DefinitionSource.end())
8652     return EK_ReplyHazy;
8653   return I->second ? EK_Never : EK_Always;
8654 }
8655 
8656 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8657   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8658 }
8659 
8660 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8661   if (ID == 0)
8662     return Selector();
8663 
8664   if (ID > SelectorsLoaded.size()) {
8665     Error("selector ID out of range in AST file");
8666     return Selector();
8667   }
8668 
8669   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8670     // Load this selector from the selector table.
8671     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8672     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8673     ModuleFile &M = *I->second;
8674     ASTSelectorLookupTrait Trait(*this, M);
8675     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8676     SelectorsLoaded[ID - 1] =
8677       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8678     if (DeserializationListener)
8679       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8680   }
8681 
8682   return SelectorsLoaded[ID - 1];
8683 }
8684 
8685 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8686   return DecodeSelector(ID);
8687 }
8688 
8689 uint32_t ASTReader::GetNumExternalSelectors() {
8690   // ID 0 (the null selector) is considered an external selector.
8691   return getTotalNumSelectors() + 1;
8692 }
8693 
8694 serialization::SelectorID
8695 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8696   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8697     return LocalID;
8698 
8699   if (!M.ModuleOffsetMap.empty())
8700     ReadModuleOffsetMap(M);
8701 
8702   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8703     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8704   assert(I != M.SelectorRemap.end()
8705          && "Invalid index into selector index remap");
8706 
8707   return LocalID + I->second;
8708 }
8709 
8710 DeclarationNameLoc
8711 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8712   DeclarationNameLoc DNLoc;
8713   switch (Name.getNameKind()) {
8714   case DeclarationName::CXXConstructorName:
8715   case DeclarationName::CXXDestructorName:
8716   case DeclarationName::CXXConversionFunctionName:
8717     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8718     break;
8719 
8720   case DeclarationName::CXXOperatorName:
8721     DNLoc.CXXOperatorName.BeginOpNameLoc
8722       = readSourceLocation().getRawEncoding();
8723     DNLoc.CXXOperatorName.EndOpNameLoc
8724       = readSourceLocation().getRawEncoding();
8725     break;
8726 
8727   case DeclarationName::CXXLiteralOperatorName:
8728     DNLoc.CXXLiteralOperatorName.OpNameLoc
8729       = readSourceLocation().getRawEncoding();
8730     break;
8731 
8732   case DeclarationName::Identifier:
8733   case DeclarationName::ObjCZeroArgSelector:
8734   case DeclarationName::ObjCOneArgSelector:
8735   case DeclarationName::ObjCMultiArgSelector:
8736   case DeclarationName::CXXUsingDirective:
8737   case DeclarationName::CXXDeductionGuideName:
8738     break;
8739   }
8740   return DNLoc;
8741 }
8742 
8743 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8744   DeclarationNameInfo NameInfo;
8745   NameInfo.setName(readDeclarationName());
8746   NameInfo.setLoc(readSourceLocation());
8747   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8748   return NameInfo;
8749 }
8750 
8751 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8752   Info.QualifierLoc = readNestedNameSpecifierLoc();
8753   unsigned NumTPLists = readInt();
8754   Info.NumTemplParamLists = NumTPLists;
8755   if (NumTPLists) {
8756     Info.TemplParamLists =
8757         new (getContext()) TemplateParameterList *[NumTPLists];
8758     for (unsigned i = 0; i != NumTPLists; ++i)
8759       Info.TemplParamLists[i] = readTemplateParameterList();
8760   }
8761 }
8762 
8763 TemplateParameterList *
8764 ASTRecordReader::readTemplateParameterList() {
8765   SourceLocation TemplateLoc = readSourceLocation();
8766   SourceLocation LAngleLoc = readSourceLocation();
8767   SourceLocation RAngleLoc = readSourceLocation();
8768 
8769   unsigned NumParams = readInt();
8770   SmallVector<NamedDecl *, 16> Params;
8771   Params.reserve(NumParams);
8772   while (NumParams--)
8773     Params.push_back(readDeclAs<NamedDecl>());
8774 
8775   bool HasRequiresClause = readBool();
8776   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8777 
8778   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8779       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8780   return TemplateParams;
8781 }
8782 
8783 void ASTRecordReader::readTemplateArgumentList(
8784                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8785                         bool Canonicalize) {
8786   unsigned NumTemplateArgs = readInt();
8787   TemplArgs.reserve(NumTemplateArgs);
8788   while (NumTemplateArgs--)
8789     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8790 }
8791 
8792 /// Read a UnresolvedSet structure.
8793 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8794   unsigned NumDecls = readInt();
8795   Set.reserve(getContext(), NumDecls);
8796   while (NumDecls--) {
8797     DeclID ID = readDeclID();
8798     AccessSpecifier AS = (AccessSpecifier) readInt();
8799     Set.addLazyDecl(getContext(), ID, AS);
8800   }
8801 }
8802 
8803 CXXBaseSpecifier
8804 ASTRecordReader::readCXXBaseSpecifier() {
8805   bool isVirtual = readBool();
8806   bool isBaseOfClass = readBool();
8807   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8808   bool inheritConstructors = readBool();
8809   TypeSourceInfo *TInfo = readTypeSourceInfo();
8810   SourceRange Range = readSourceRange();
8811   SourceLocation EllipsisLoc = readSourceLocation();
8812   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8813                           EllipsisLoc);
8814   Result.setInheritConstructors(inheritConstructors);
8815   return Result;
8816 }
8817 
8818 CXXCtorInitializer **
8819 ASTRecordReader::readCXXCtorInitializers() {
8820   ASTContext &Context = getContext();
8821   unsigned NumInitializers = readInt();
8822   assert(NumInitializers && "wrote ctor initializers but have no inits");
8823   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8824   for (unsigned i = 0; i != NumInitializers; ++i) {
8825     TypeSourceInfo *TInfo = nullptr;
8826     bool IsBaseVirtual = false;
8827     FieldDecl *Member = nullptr;
8828     IndirectFieldDecl *IndirectMember = nullptr;
8829 
8830     CtorInitializerType Type = (CtorInitializerType) readInt();
8831     switch (Type) {
8832     case CTOR_INITIALIZER_BASE:
8833       TInfo = readTypeSourceInfo();
8834       IsBaseVirtual = readBool();
8835       break;
8836 
8837     case CTOR_INITIALIZER_DELEGATING:
8838       TInfo = readTypeSourceInfo();
8839       break;
8840 
8841      case CTOR_INITIALIZER_MEMBER:
8842       Member = readDeclAs<FieldDecl>();
8843       break;
8844 
8845      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8846       IndirectMember = readDeclAs<IndirectFieldDecl>();
8847       break;
8848     }
8849 
8850     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8851     Expr *Init = readExpr();
8852     SourceLocation LParenLoc = readSourceLocation();
8853     SourceLocation RParenLoc = readSourceLocation();
8854 
8855     CXXCtorInitializer *BOMInit;
8856     if (Type == CTOR_INITIALIZER_BASE)
8857       BOMInit = new (Context)
8858           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8859                              RParenLoc, MemberOrEllipsisLoc);
8860     else if (Type == CTOR_INITIALIZER_DELEGATING)
8861       BOMInit = new (Context)
8862           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8863     else if (Member)
8864       BOMInit = new (Context)
8865           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8866                              Init, RParenLoc);
8867     else
8868       BOMInit = new (Context)
8869           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8870                              LParenLoc, Init, RParenLoc);
8871 
8872     if (/*IsWritten*/readBool()) {
8873       unsigned SourceOrder = readInt();
8874       BOMInit->setSourceOrder(SourceOrder);
8875     }
8876 
8877     CtorInitializers[i] = BOMInit;
8878   }
8879 
8880   return CtorInitializers;
8881 }
8882 
8883 NestedNameSpecifierLoc
8884 ASTRecordReader::readNestedNameSpecifierLoc() {
8885   ASTContext &Context = getContext();
8886   unsigned N = readInt();
8887   NestedNameSpecifierLocBuilder Builder;
8888   for (unsigned I = 0; I != N; ++I) {
8889     auto Kind = readNestedNameSpecifierKind();
8890     switch (Kind) {
8891     case NestedNameSpecifier::Identifier: {
8892       IdentifierInfo *II = readIdentifier();
8893       SourceRange Range = readSourceRange();
8894       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8895       break;
8896     }
8897 
8898     case NestedNameSpecifier::Namespace: {
8899       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8900       SourceRange Range = readSourceRange();
8901       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8902       break;
8903     }
8904 
8905     case NestedNameSpecifier::NamespaceAlias: {
8906       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8907       SourceRange Range = readSourceRange();
8908       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8909       break;
8910     }
8911 
8912     case NestedNameSpecifier::TypeSpec:
8913     case NestedNameSpecifier::TypeSpecWithTemplate: {
8914       bool Template = readBool();
8915       TypeSourceInfo *T = readTypeSourceInfo();
8916       if (!T)
8917         return NestedNameSpecifierLoc();
8918       SourceLocation ColonColonLoc = readSourceLocation();
8919 
8920       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8921       Builder.Extend(Context,
8922                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8923                      T->getTypeLoc(), ColonColonLoc);
8924       break;
8925     }
8926 
8927     case NestedNameSpecifier::Global: {
8928       SourceLocation ColonColonLoc = readSourceLocation();
8929       Builder.MakeGlobal(Context, ColonColonLoc);
8930       break;
8931     }
8932 
8933     case NestedNameSpecifier::Super: {
8934       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8935       SourceRange Range = readSourceRange();
8936       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8937       break;
8938     }
8939     }
8940   }
8941 
8942   return Builder.getWithLocInContext(Context);
8943 }
8944 
8945 SourceRange
8946 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8947                            unsigned &Idx) {
8948   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8949   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8950   return SourceRange(beg, end);
8951 }
8952 
8953 static llvm::FixedPointSemantics
8954 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8955                         unsigned &Idx) {
8956   unsigned Width = Record[Idx++];
8957   unsigned Scale = Record[Idx++];
8958   uint64_t Tmp = Record[Idx++];
8959   bool IsSigned = Tmp & 0x1;
8960   bool IsSaturated = Tmp & 0x2;
8961   bool HasUnsignedPadding = Tmp & 0x4;
8962   return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8963                                    HasUnsignedPadding);
8964 }
8965 
8966 static const llvm::fltSemantics &
8967 readAPFloatSemantics(ASTRecordReader &reader) {
8968   return llvm::APFloatBase::EnumToSemantics(
8969     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8970 }
8971 
8972 APValue ASTRecordReader::readAPValue() {
8973   unsigned Kind = readInt();
8974   switch ((APValue::ValueKind) Kind) {
8975   case APValue::None:
8976     return APValue();
8977   case APValue::Indeterminate:
8978     return APValue::IndeterminateValue();
8979   case APValue::Int:
8980     return APValue(readAPSInt());
8981   case APValue::Float: {
8982     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8983     return APValue(readAPFloat(FloatSema));
8984   }
8985   case APValue::FixedPoint: {
8986     llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8987     return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
8988   }
8989   case APValue::ComplexInt: {
8990     llvm::APSInt First = readAPSInt();
8991     return APValue(std::move(First), readAPSInt());
8992   }
8993   case APValue::ComplexFloat: {
8994     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8995     llvm::APFloat First = readAPFloat(FloatSema1);
8996     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8997     return APValue(std::move(First), readAPFloat(FloatSema2));
8998   }
8999   case APValue::LValue:
9000   case APValue::Vector:
9001   case APValue::Array:
9002   case APValue::Struct:
9003   case APValue::Union:
9004   case APValue::MemberPointer:
9005   case APValue::AddrLabelDiff:
9006     // TODO : Handle all these APValue::ValueKind.
9007     return APValue();
9008   }
9009   llvm_unreachable("Invalid APValue::ValueKind");
9010 }
9011 
9012 /// Read a floating-point value
9013 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9014   return llvm::APFloat(Sem, readAPInt());
9015 }
9016 
9017 // Read a string
9018 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9019   unsigned Len = Record[Idx++];
9020   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9021   Idx += Len;
9022   return Result;
9023 }
9024 
9025 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9026                                 unsigned &Idx) {
9027   std::string Filename = ReadString(Record, Idx);
9028   ResolveImportedPath(F, Filename);
9029   return Filename;
9030 }
9031 
9032 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9033                                 const RecordData &Record, unsigned &Idx) {
9034   std::string Filename = ReadString(Record, Idx);
9035   if (!BaseDirectory.empty())
9036     ResolveImportedPath(Filename, BaseDirectory);
9037   return Filename;
9038 }
9039 
9040 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9041                                          unsigned &Idx) {
9042   unsigned Major = Record[Idx++];
9043   unsigned Minor = Record[Idx++];
9044   unsigned Subminor = Record[Idx++];
9045   if (Minor == 0)
9046     return VersionTuple(Major);
9047   if (Subminor == 0)
9048     return VersionTuple(Major, Minor - 1);
9049   return VersionTuple(Major, Minor - 1, Subminor - 1);
9050 }
9051 
9052 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9053                                           const RecordData &Record,
9054                                           unsigned &Idx) {
9055   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9056   return CXXTemporary::Create(getContext(), Decl);
9057 }
9058 
9059 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9060   return Diag(CurrentImportLoc, DiagID);
9061 }
9062 
9063 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9064   return Diags.Report(Loc, DiagID);
9065 }
9066 
9067 /// Retrieve the identifier table associated with the
9068 /// preprocessor.
9069 IdentifierTable &ASTReader::getIdentifierTable() {
9070   return PP.getIdentifierTable();
9071 }
9072 
9073 /// Record that the given ID maps to the given switch-case
9074 /// statement.
9075 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9076   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9077          "Already have a SwitchCase with this ID");
9078   (*CurrSwitchCaseStmts)[ID] = SC;
9079 }
9080 
9081 /// Retrieve the switch-case statement with the given ID.
9082 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9083   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9084   return (*CurrSwitchCaseStmts)[ID];
9085 }
9086 
9087 void ASTReader::ClearSwitchCaseIDs() {
9088   CurrSwitchCaseStmts->clear();
9089 }
9090 
9091 void ASTReader::ReadComments() {
9092   ASTContext &Context = getContext();
9093   std::vector<RawComment *> Comments;
9094   for (SmallVectorImpl<std::pair<BitstreamCursor,
9095                                  serialization::ModuleFile *>>::iterator
9096        I = CommentsCursors.begin(),
9097        E = CommentsCursors.end();
9098        I != E; ++I) {
9099     Comments.clear();
9100     BitstreamCursor &Cursor = I->first;
9101     serialization::ModuleFile &F = *I->second;
9102     SavedStreamPosition SavedPosition(Cursor);
9103 
9104     RecordData Record;
9105     while (true) {
9106       Expected<llvm::BitstreamEntry> MaybeEntry =
9107           Cursor.advanceSkippingSubblocks(
9108               BitstreamCursor::AF_DontPopBlockAtEnd);
9109       if (!MaybeEntry) {
9110         Error(MaybeEntry.takeError());
9111         return;
9112       }
9113       llvm::BitstreamEntry Entry = MaybeEntry.get();
9114 
9115       switch (Entry.Kind) {
9116       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9117       case llvm::BitstreamEntry::Error:
9118         Error("malformed block record in AST file");
9119         return;
9120       case llvm::BitstreamEntry::EndBlock:
9121         goto NextCursor;
9122       case llvm::BitstreamEntry::Record:
9123         // The interesting case.
9124         break;
9125       }
9126 
9127       // Read a record.
9128       Record.clear();
9129       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9130       if (!MaybeComment) {
9131         Error(MaybeComment.takeError());
9132         return;
9133       }
9134       switch ((CommentRecordTypes)MaybeComment.get()) {
9135       case COMMENTS_RAW_COMMENT: {
9136         unsigned Idx = 0;
9137         SourceRange SR = ReadSourceRange(F, Record, Idx);
9138         RawComment::CommentKind Kind =
9139             (RawComment::CommentKind) Record[Idx++];
9140         bool IsTrailingComment = Record[Idx++];
9141         bool IsAlmostTrailingComment = Record[Idx++];
9142         Comments.push_back(new (Context) RawComment(
9143             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9144         break;
9145       }
9146       }
9147     }
9148   NextCursor:
9149     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9150         FileToOffsetToComment;
9151     for (RawComment *C : Comments) {
9152       SourceLocation CommentLoc = C->getBeginLoc();
9153       if (CommentLoc.isValid()) {
9154         std::pair<FileID, unsigned> Loc =
9155             SourceMgr.getDecomposedLoc(CommentLoc);
9156         if (Loc.first.isValid())
9157           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9158       }
9159     }
9160   }
9161 }
9162 
9163 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9164                                 bool IncludeSystem, bool Complain,
9165                     llvm::function_ref<void(const serialization::InputFile &IF,
9166                                             bool isSystem)> Visitor) {
9167   unsigned NumUserInputs = MF.NumUserInputFiles;
9168   unsigned NumInputs = MF.InputFilesLoaded.size();
9169   assert(NumUserInputs <= NumInputs);
9170   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9171   for (unsigned I = 0; I < N; ++I) {
9172     bool IsSystem = I >= NumUserInputs;
9173     InputFile IF = getInputFile(MF, I+1, Complain);
9174     Visitor(IF, IsSystem);
9175   }
9176 }
9177 
9178 void ASTReader::visitTopLevelModuleMaps(
9179     serialization::ModuleFile &MF,
9180     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9181   unsigned NumInputs = MF.InputFilesLoaded.size();
9182   for (unsigned I = 0; I < NumInputs; ++I) {
9183     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9184     if (IFI.TopLevelModuleMap)
9185       // FIXME: This unnecessarily re-reads the InputFileInfo.
9186       if (auto *FE = getInputFile(MF, I + 1).getFile())
9187         Visitor(FE);
9188   }
9189 }
9190 
9191 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9192   // If we know the owning module, use it.
9193   if (Module *M = D->getImportedOwningModule())
9194     return M->getFullModuleName();
9195 
9196   // Otherwise, use the name of the top-level module the decl is within.
9197   if (ModuleFile *M = getOwningModuleFile(D))
9198     return M->ModuleName;
9199 
9200   // Not from a module.
9201   return {};
9202 }
9203 
9204 void ASTReader::finishPendingActions() {
9205   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9206          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9207          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9208          !PendingUpdateRecords.empty()) {
9209     // If any identifiers with corresponding top-level declarations have
9210     // been loaded, load those declarations now.
9211     using TopLevelDeclsMap =
9212         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9213     TopLevelDeclsMap TopLevelDecls;
9214 
9215     while (!PendingIdentifierInfos.empty()) {
9216       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9217       SmallVector<uint32_t, 4> DeclIDs =
9218           std::move(PendingIdentifierInfos.back().second);
9219       PendingIdentifierInfos.pop_back();
9220 
9221       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9222     }
9223 
9224     // Load each function type that we deferred loading because it was a
9225     // deduced type that might refer to a local type declared within itself.
9226     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9227       auto *FD = PendingFunctionTypes[I].first;
9228       FD->setType(GetType(PendingFunctionTypes[I].second));
9229 
9230       // If we gave a function a deduced return type, remember that we need to
9231       // propagate that along the redeclaration chain.
9232       auto *DT = FD->getReturnType()->getContainedDeducedType();
9233       if (DT && DT->isDeduced())
9234         PendingDeducedTypeUpdates.insert(
9235             {FD->getCanonicalDecl(), FD->getReturnType()});
9236     }
9237     PendingFunctionTypes.clear();
9238 
9239     // For each decl chain that we wanted to complete while deserializing, mark
9240     // it as "still needs to be completed".
9241     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9242       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9243     }
9244     PendingIncompleteDeclChains.clear();
9245 
9246     // Load pending declaration chains.
9247     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9248       loadPendingDeclChain(PendingDeclChains[I].first,
9249                            PendingDeclChains[I].second);
9250     PendingDeclChains.clear();
9251 
9252     // Make the most recent of the top-level declarations visible.
9253     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9254            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9255       IdentifierInfo *II = TLD->first;
9256       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9257         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9258       }
9259     }
9260 
9261     // Load any pending macro definitions.
9262     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9263       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9264       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9265       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9266       // Initialize the macro history from chained-PCHs ahead of module imports.
9267       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9268            ++IDIdx) {
9269         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9270         if (!Info.M->isModule())
9271           resolvePendingMacro(II, Info);
9272       }
9273       // Handle module imports.
9274       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9275            ++IDIdx) {
9276         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9277         if (Info.M->isModule())
9278           resolvePendingMacro(II, Info);
9279       }
9280     }
9281     PendingMacroIDs.clear();
9282 
9283     // Wire up the DeclContexts for Decls that we delayed setting until
9284     // recursive loading is completed.
9285     while (!PendingDeclContextInfos.empty()) {
9286       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9287       PendingDeclContextInfos.pop_front();
9288       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9289       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9290       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9291     }
9292 
9293     // Perform any pending declaration updates.
9294     while (!PendingUpdateRecords.empty()) {
9295       auto Update = PendingUpdateRecords.pop_back_val();
9296       ReadingKindTracker ReadingKind(Read_Decl, *this);
9297       loadDeclUpdateRecords(Update);
9298     }
9299   }
9300 
9301   // At this point, all update records for loaded decls are in place, so any
9302   // fake class definitions should have become real.
9303   assert(PendingFakeDefinitionData.empty() &&
9304          "faked up a class definition but never saw the real one");
9305 
9306   // If we deserialized any C++ or Objective-C class definitions, any
9307   // Objective-C protocol definitions, or any redeclarable templates, make sure
9308   // that all redeclarations point to the definitions. Note that this can only
9309   // happen now, after the redeclaration chains have been fully wired.
9310   for (Decl *D : PendingDefinitions) {
9311     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9312       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9313         // Make sure that the TagType points at the definition.
9314         const_cast<TagType*>(TagT)->decl = TD;
9315       }
9316 
9317       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9318         for (auto *R = getMostRecentExistingDecl(RD); R;
9319              R = R->getPreviousDecl()) {
9320           assert((R == D) ==
9321                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9322                  "declaration thinks it's the definition but it isn't");
9323           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9324         }
9325       }
9326 
9327       continue;
9328     }
9329 
9330     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9331       // Make sure that the ObjCInterfaceType points at the definition.
9332       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9333         ->Decl = ID;
9334 
9335       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9336         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9337 
9338       continue;
9339     }
9340 
9341     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9342       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9343         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9344 
9345       continue;
9346     }
9347 
9348     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9349     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9350       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9351   }
9352   PendingDefinitions.clear();
9353 
9354   // Load the bodies of any functions or methods we've encountered. We do
9355   // this now (delayed) so that we can be sure that the declaration chains
9356   // have been fully wired up (hasBody relies on this).
9357   // FIXME: We shouldn't require complete redeclaration chains here.
9358   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9359                                PBEnd = PendingBodies.end();
9360        PB != PBEnd; ++PB) {
9361     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9362       // For a function defined inline within a class template, force the
9363       // canonical definition to be the one inside the canonical definition of
9364       // the template. This ensures that we instantiate from a correct view
9365       // of the template.
9366       //
9367       // Sadly we can't do this more generally: we can't be sure that all
9368       // copies of an arbitrary class definition will have the same members
9369       // defined (eg, some member functions may not be instantiated, and some
9370       // special members may or may not have been implicitly defined).
9371       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9372         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9373           continue;
9374 
9375       // FIXME: Check for =delete/=default?
9376       // FIXME: Complain about ODR violations here?
9377       const FunctionDecl *Defn = nullptr;
9378       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9379         FD->setLazyBody(PB->second);
9380       } else {
9381         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9382         mergeDefinitionVisibility(NonConstDefn, FD);
9383 
9384         if (!FD->isLateTemplateParsed() &&
9385             !NonConstDefn->isLateTemplateParsed() &&
9386             FD->getODRHash() != NonConstDefn->getODRHash()) {
9387           if (!isa<CXXMethodDecl>(FD)) {
9388             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9389           } else if (FD->getLexicalParent()->isFileContext() &&
9390                      NonConstDefn->getLexicalParent()->isFileContext()) {
9391             // Only diagnose out-of-line method definitions.  If they are
9392             // in class definitions, then an error will be generated when
9393             // processing the class bodies.
9394             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9395           }
9396         }
9397       }
9398       continue;
9399     }
9400 
9401     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9402     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9403       MD->setLazyBody(PB->second);
9404   }
9405   PendingBodies.clear();
9406 
9407   // Do some cleanup.
9408   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9409     getContext().deduplicateMergedDefinitonsFor(ND);
9410   PendingMergedDefinitionsToDeduplicate.clear();
9411 }
9412 
9413 void ASTReader::diagnoseOdrViolations() {
9414   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9415       PendingFunctionOdrMergeFailures.empty() &&
9416       PendingEnumOdrMergeFailures.empty())
9417     return;
9418 
9419   // Trigger the import of the full definition of each class that had any
9420   // odr-merging problems, so we can produce better diagnostics for them.
9421   // These updates may in turn find and diagnose some ODR failures, so take
9422   // ownership of the set first.
9423   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9424   PendingOdrMergeFailures.clear();
9425   for (auto &Merge : OdrMergeFailures) {
9426     Merge.first->buildLookup();
9427     Merge.first->decls_begin();
9428     Merge.first->bases_begin();
9429     Merge.first->vbases_begin();
9430     for (auto &RecordPair : Merge.second) {
9431       auto *RD = RecordPair.first;
9432       RD->decls_begin();
9433       RD->bases_begin();
9434       RD->vbases_begin();
9435     }
9436   }
9437 
9438   // Trigger the import of functions.
9439   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9440   PendingFunctionOdrMergeFailures.clear();
9441   for (auto &Merge : FunctionOdrMergeFailures) {
9442     Merge.first->buildLookup();
9443     Merge.first->decls_begin();
9444     Merge.first->getBody();
9445     for (auto &FD : Merge.second) {
9446       FD->buildLookup();
9447       FD->decls_begin();
9448       FD->getBody();
9449     }
9450   }
9451 
9452   // Trigger the import of enums.
9453   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9454   PendingEnumOdrMergeFailures.clear();
9455   for (auto &Merge : EnumOdrMergeFailures) {
9456     Merge.first->decls_begin();
9457     for (auto &Enum : Merge.second) {
9458       Enum->decls_begin();
9459     }
9460   }
9461 
9462   // For each declaration from a merged context, check that the canonical
9463   // definition of that context also contains a declaration of the same
9464   // entity.
9465   //
9466   // Caution: this loop does things that might invalidate iterators into
9467   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9468   while (!PendingOdrMergeChecks.empty()) {
9469     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9470 
9471     // FIXME: Skip over implicit declarations for now. This matters for things
9472     // like implicitly-declared special member functions. This isn't entirely
9473     // correct; we can end up with multiple unmerged declarations of the same
9474     // implicit entity.
9475     if (D->isImplicit())
9476       continue;
9477 
9478     DeclContext *CanonDef = D->getDeclContext();
9479 
9480     bool Found = false;
9481     const Decl *DCanon = D->getCanonicalDecl();
9482 
9483     for (auto RI : D->redecls()) {
9484       if (RI->getLexicalDeclContext() == CanonDef) {
9485         Found = true;
9486         break;
9487       }
9488     }
9489     if (Found)
9490       continue;
9491 
9492     // Quick check failed, time to do the slow thing. Note, we can't just
9493     // look up the name of D in CanonDef here, because the member that is
9494     // in CanonDef might not be found by name lookup (it might have been
9495     // replaced by a more recent declaration in the lookup table), and we
9496     // can't necessarily find it in the redeclaration chain because it might
9497     // be merely mergeable, not redeclarable.
9498     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9499     for (auto *CanonMember : CanonDef->decls()) {
9500       if (CanonMember->getCanonicalDecl() == DCanon) {
9501         // This can happen if the declaration is merely mergeable and not
9502         // actually redeclarable (we looked for redeclarations earlier).
9503         //
9504         // FIXME: We should be able to detect this more efficiently, without
9505         // pulling in all of the members of CanonDef.
9506         Found = true;
9507         break;
9508       }
9509       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9510         if (ND->getDeclName() == D->getDeclName())
9511           Candidates.push_back(ND);
9512     }
9513 
9514     if (!Found) {
9515       // The AST doesn't like TagDecls becoming invalid after they've been
9516       // completed. We only really need to mark FieldDecls as invalid here.
9517       if (!isa<TagDecl>(D))
9518         D->setInvalidDecl();
9519 
9520       // Ensure we don't accidentally recursively enter deserialization while
9521       // we're producing our diagnostic.
9522       Deserializing RecursionGuard(this);
9523 
9524       std::string CanonDefModule =
9525           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9526       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9527         << D << getOwningModuleNameForDiagnostic(D)
9528         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9529 
9530       if (Candidates.empty())
9531         Diag(cast<Decl>(CanonDef)->getLocation(),
9532              diag::note_module_odr_violation_no_possible_decls) << D;
9533       else {
9534         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9535           Diag(Candidates[I]->getLocation(),
9536                diag::note_module_odr_violation_possible_decl)
9537             << Candidates[I];
9538       }
9539 
9540       DiagnosedOdrMergeFailures.insert(CanonDef);
9541     }
9542   }
9543 
9544   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9545       EnumOdrMergeFailures.empty())
9546     return;
9547 
9548   // Ensure we don't accidentally recursively enter deserialization while
9549   // we're producing our diagnostics.
9550   Deserializing RecursionGuard(this);
9551 
9552   // Common code for hashing helpers.
9553   ODRHash Hash;
9554   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9555     Hash.clear();
9556     Hash.AddQualType(Ty);
9557     return Hash.CalculateHash();
9558   };
9559 
9560   auto ComputeODRHash = [&Hash](const Stmt *S) {
9561     assert(S);
9562     Hash.clear();
9563     Hash.AddStmt(S);
9564     return Hash.CalculateHash();
9565   };
9566 
9567   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9568     assert(D);
9569     Hash.clear();
9570     Hash.AddSubDecl(D);
9571     return Hash.CalculateHash();
9572   };
9573 
9574   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9575     Hash.clear();
9576     Hash.AddTemplateArgument(TA);
9577     return Hash.CalculateHash();
9578   };
9579 
9580   auto ComputeTemplateParameterListODRHash =
9581       [&Hash](const TemplateParameterList *TPL) {
9582         assert(TPL);
9583         Hash.clear();
9584         Hash.AddTemplateParameterList(TPL);
9585         return Hash.CalculateHash();
9586       };
9587 
9588   // Used with err_module_odr_violation_mismatch_decl and
9589   // note_module_odr_violation_mismatch_decl
9590   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9591   enum ODRMismatchDecl {
9592     EndOfClass,
9593     PublicSpecifer,
9594     PrivateSpecifer,
9595     ProtectedSpecifer,
9596     StaticAssert,
9597     Field,
9598     CXXMethod,
9599     TypeAlias,
9600     TypeDef,
9601     Var,
9602     Friend,
9603     FunctionTemplate,
9604     Other
9605   };
9606 
9607   // Used with err_module_odr_violation_mismatch_decl_diff and
9608   // note_module_odr_violation_mismatch_decl_diff
9609   enum ODRMismatchDeclDifference {
9610     StaticAssertCondition,
9611     StaticAssertMessage,
9612     StaticAssertOnlyMessage,
9613     FieldName,
9614     FieldTypeName,
9615     FieldSingleBitField,
9616     FieldDifferentWidthBitField,
9617     FieldSingleMutable,
9618     FieldSingleInitializer,
9619     FieldDifferentInitializers,
9620     MethodName,
9621     MethodDeleted,
9622     MethodDefaulted,
9623     MethodVirtual,
9624     MethodStatic,
9625     MethodVolatile,
9626     MethodConst,
9627     MethodInline,
9628     MethodNumberParameters,
9629     MethodParameterType,
9630     MethodParameterName,
9631     MethodParameterSingleDefaultArgument,
9632     MethodParameterDifferentDefaultArgument,
9633     MethodNoTemplateArguments,
9634     MethodDifferentNumberTemplateArguments,
9635     MethodDifferentTemplateArgument,
9636     MethodSingleBody,
9637     MethodDifferentBody,
9638     TypedefName,
9639     TypedefType,
9640     VarName,
9641     VarType,
9642     VarSingleInitializer,
9643     VarDifferentInitializer,
9644     VarConstexpr,
9645     FriendTypeFunction,
9646     FriendType,
9647     FriendFunction,
9648     FunctionTemplateDifferentNumberParameters,
9649     FunctionTemplateParameterDifferentKind,
9650     FunctionTemplateParameterName,
9651     FunctionTemplateParameterSingleDefaultArgument,
9652     FunctionTemplateParameterDifferentDefaultArgument,
9653     FunctionTemplateParameterDifferentType,
9654     FunctionTemplatePackParameter,
9655   };
9656 
9657   // These lambdas have the common portions of the ODR diagnostics.  This
9658   // has the same return as Diag(), so addition parameters can be passed
9659   // in with operator<<
9660   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9661                                  SourceLocation Loc, SourceRange Range,
9662                                  ODRMismatchDeclDifference DiffType) {
9663     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9664            << FirstRecord << FirstModule.empty() << FirstModule << Range
9665            << DiffType;
9666   };
9667   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9668                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9669     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9670            << SecondModule << Range << DiffType;
9671   };
9672 
9673   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9674                        &ComputeQualTypeODRHash, &ComputeODRHash](
9675                           NamedDecl *FirstRecord, StringRef FirstModule,
9676                           StringRef SecondModule, FieldDecl *FirstField,
9677                           FieldDecl *SecondField) {
9678     IdentifierInfo *FirstII = FirstField->getIdentifier();
9679     IdentifierInfo *SecondII = SecondField->getIdentifier();
9680     if (FirstII->getName() != SecondII->getName()) {
9681       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9682                        FirstField->getSourceRange(), FieldName)
9683           << FirstII;
9684       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9685                       SecondField->getSourceRange(), FieldName)
9686           << SecondII;
9687 
9688       return true;
9689     }
9690 
9691     assert(getContext().hasSameType(FirstField->getType(),
9692                                     SecondField->getType()));
9693 
9694     QualType FirstType = FirstField->getType();
9695     QualType SecondType = SecondField->getType();
9696     if (ComputeQualTypeODRHash(FirstType) !=
9697         ComputeQualTypeODRHash(SecondType)) {
9698       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9699                        FirstField->getSourceRange(), FieldTypeName)
9700           << FirstII << FirstType;
9701       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9702                       SecondField->getSourceRange(), FieldTypeName)
9703           << SecondII << SecondType;
9704 
9705       return true;
9706     }
9707 
9708     const bool IsFirstBitField = FirstField->isBitField();
9709     const bool IsSecondBitField = SecondField->isBitField();
9710     if (IsFirstBitField != IsSecondBitField) {
9711       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9712                        FirstField->getSourceRange(), FieldSingleBitField)
9713           << FirstII << IsFirstBitField;
9714       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9715                       SecondField->getSourceRange(), FieldSingleBitField)
9716           << SecondII << IsSecondBitField;
9717       return true;
9718     }
9719 
9720     if (IsFirstBitField && IsSecondBitField) {
9721       unsigned FirstBitWidthHash =
9722           ComputeODRHash(FirstField->getBitWidth());
9723       unsigned SecondBitWidthHash =
9724           ComputeODRHash(SecondField->getBitWidth());
9725       if (FirstBitWidthHash != SecondBitWidthHash) {
9726         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9727                          FirstField->getSourceRange(),
9728                          FieldDifferentWidthBitField)
9729             << FirstII << FirstField->getBitWidth()->getSourceRange();
9730         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9731                         SecondField->getSourceRange(),
9732                         FieldDifferentWidthBitField)
9733             << SecondII << SecondField->getBitWidth()->getSourceRange();
9734         return true;
9735       }
9736     }
9737 
9738     if (!PP.getLangOpts().CPlusPlus)
9739       return false;
9740 
9741     const bool IsFirstMutable = FirstField->isMutable();
9742     const bool IsSecondMutable = SecondField->isMutable();
9743     if (IsFirstMutable != IsSecondMutable) {
9744       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9745                        FirstField->getSourceRange(), FieldSingleMutable)
9746           << FirstII << IsFirstMutable;
9747       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9748                       SecondField->getSourceRange(), FieldSingleMutable)
9749           << SecondII << IsSecondMutable;
9750       return true;
9751     }
9752 
9753     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9754     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9755     if ((!FirstInitializer && SecondInitializer) ||
9756         (FirstInitializer && !SecondInitializer)) {
9757       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9758                        FirstField->getSourceRange(), FieldSingleInitializer)
9759           << FirstII << (FirstInitializer != nullptr);
9760       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9761                       SecondField->getSourceRange(), FieldSingleInitializer)
9762           << SecondII << (SecondInitializer != nullptr);
9763       return true;
9764     }
9765 
9766     if (FirstInitializer && SecondInitializer) {
9767       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9768       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9769       if (FirstInitHash != SecondInitHash) {
9770         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9771                          FirstField->getSourceRange(),
9772                          FieldDifferentInitializers)
9773             << FirstII << FirstInitializer->getSourceRange();
9774         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9775                         SecondField->getSourceRange(),
9776                         FieldDifferentInitializers)
9777             << SecondII << SecondInitializer->getSourceRange();
9778         return true;
9779       }
9780     }
9781 
9782     return false;
9783   };
9784 
9785   auto ODRDiagTypeDefOrAlias =
9786       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9787           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9788           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9789           bool IsTypeAlias) {
9790         auto FirstName = FirstTD->getDeclName();
9791         auto SecondName = SecondTD->getDeclName();
9792         if (FirstName != SecondName) {
9793           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9794                            FirstTD->getSourceRange(), TypedefName)
9795               << IsTypeAlias << FirstName;
9796           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9797                           SecondTD->getSourceRange(), TypedefName)
9798               << IsTypeAlias << SecondName;
9799           return true;
9800         }
9801 
9802         QualType FirstType = FirstTD->getUnderlyingType();
9803         QualType SecondType = SecondTD->getUnderlyingType();
9804         if (ComputeQualTypeODRHash(FirstType) !=
9805             ComputeQualTypeODRHash(SecondType)) {
9806           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9807                            FirstTD->getSourceRange(), TypedefType)
9808               << IsTypeAlias << FirstName << FirstType;
9809           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9810                           SecondTD->getSourceRange(), TypedefType)
9811               << IsTypeAlias << SecondName << SecondType;
9812           return true;
9813         }
9814 
9815         return false;
9816   };
9817 
9818   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9819                      &ComputeQualTypeODRHash, &ComputeODRHash,
9820                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9821                            StringRef SecondModule, VarDecl *FirstVD,
9822                            VarDecl *SecondVD) {
9823     auto FirstName = FirstVD->getDeclName();
9824     auto SecondName = SecondVD->getDeclName();
9825     if (FirstName != SecondName) {
9826       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9827                        FirstVD->getSourceRange(), VarName)
9828           << FirstName;
9829       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9830                       SecondVD->getSourceRange(), VarName)
9831           << SecondName;
9832       return true;
9833     }
9834 
9835     QualType FirstType = FirstVD->getType();
9836     QualType SecondType = SecondVD->getType();
9837     if (ComputeQualTypeODRHash(FirstType) !=
9838         ComputeQualTypeODRHash(SecondType)) {
9839       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9840                        FirstVD->getSourceRange(), VarType)
9841           << FirstName << FirstType;
9842       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9843                       SecondVD->getSourceRange(), VarType)
9844           << SecondName << SecondType;
9845       return true;
9846     }
9847 
9848     if (!PP.getLangOpts().CPlusPlus)
9849       return false;
9850 
9851     const Expr *FirstInit = FirstVD->getInit();
9852     const Expr *SecondInit = SecondVD->getInit();
9853     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9854       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9855                        FirstVD->getSourceRange(), VarSingleInitializer)
9856           << FirstName << (FirstInit == nullptr)
9857           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9858       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9859                       SecondVD->getSourceRange(), VarSingleInitializer)
9860           << SecondName << (SecondInit == nullptr)
9861           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9862       return true;
9863     }
9864 
9865     if (FirstInit && SecondInit &&
9866         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9867       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9868                        FirstVD->getSourceRange(), VarDifferentInitializer)
9869           << FirstName << FirstInit->getSourceRange();
9870       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9871                       SecondVD->getSourceRange(), VarDifferentInitializer)
9872           << SecondName << SecondInit->getSourceRange();
9873       return true;
9874     }
9875 
9876     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9877     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9878     if (FirstIsConstexpr != SecondIsConstexpr) {
9879       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9880                        FirstVD->getSourceRange(), VarConstexpr)
9881           << FirstName << FirstIsConstexpr;
9882       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9883                       SecondVD->getSourceRange(), VarConstexpr)
9884           << SecondName << SecondIsConstexpr;
9885       return true;
9886     }
9887     return false;
9888   };
9889 
9890   auto DifferenceSelector = [](Decl *D) {
9891     assert(D && "valid Decl required");
9892     switch (D->getKind()) {
9893     default:
9894       return Other;
9895     case Decl::AccessSpec:
9896       switch (D->getAccess()) {
9897       case AS_public:
9898         return PublicSpecifer;
9899       case AS_private:
9900         return PrivateSpecifer;
9901       case AS_protected:
9902         return ProtectedSpecifer;
9903       case AS_none:
9904         break;
9905       }
9906       llvm_unreachable("Invalid access specifier");
9907     case Decl::StaticAssert:
9908       return StaticAssert;
9909     case Decl::Field:
9910       return Field;
9911     case Decl::CXXMethod:
9912     case Decl::CXXConstructor:
9913     case Decl::CXXDestructor:
9914       return CXXMethod;
9915     case Decl::TypeAlias:
9916       return TypeAlias;
9917     case Decl::Typedef:
9918       return TypeDef;
9919     case Decl::Var:
9920       return Var;
9921     case Decl::Friend:
9922       return Friend;
9923     case Decl::FunctionTemplate:
9924       return FunctionTemplate;
9925     }
9926   };
9927 
9928   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9929   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9930                                                  RecordDecl *Record,
9931                                                  const DeclContext *DC) {
9932     for (auto *D : Record->decls()) {
9933       if (!ODRHash::isDeclToBeProcessed(D, DC))
9934         continue;
9935       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9936     }
9937   };
9938 
9939   struct DiffResult {
9940     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9941     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9942   };
9943 
9944   // If there is a diagnoseable difference, FirstDiffType and
9945   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9946   // filled in if not EndOfClass.
9947   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9948                                              DeclHashes &SecondHashes) {
9949     DiffResult DR;
9950     auto FirstIt = FirstHashes.begin();
9951     auto SecondIt = SecondHashes.begin();
9952     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9953       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9954           FirstIt->second == SecondIt->second) {
9955         ++FirstIt;
9956         ++SecondIt;
9957         continue;
9958       }
9959 
9960       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9961       DR.SecondDecl =
9962           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9963 
9964       DR.FirstDiffType =
9965           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9966       DR.SecondDiffType =
9967           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9968       return DR;
9969     }
9970     return DR;
9971   };
9972 
9973   // Use this to diagnose that an unexpected Decl was encountered
9974   // or no difference was detected. This causes a generic error
9975   // message to be emitted.
9976   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9977                                       StringRef FirstModule,
9978                                       NamedDecl *SecondRecord,
9979                                       StringRef SecondModule) {
9980     Diag(FirstRecord->getLocation(),
9981          diag::err_module_odr_violation_different_definitions)
9982         << FirstRecord << FirstModule.empty() << FirstModule;
9983 
9984     if (DR.FirstDecl) {
9985       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9986           << FirstRecord << DR.FirstDecl->getSourceRange();
9987     }
9988 
9989     Diag(SecondRecord->getLocation(),
9990          diag::note_module_odr_violation_different_definitions)
9991         << SecondModule;
9992 
9993     if (DR.SecondDecl) {
9994       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9995           << DR.SecondDecl->getSourceRange();
9996     }
9997   };
9998 
9999   auto DiagnoseODRMismatch =
10000       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10001              NamedDecl *SecondRecord, StringRef SecondModule) {
10002         SourceLocation FirstLoc;
10003         SourceRange FirstRange;
10004         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10005         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10006           FirstLoc = FirstTag->getBraceRange().getEnd();
10007         } else {
10008           FirstLoc = DR.FirstDecl->getLocation();
10009           FirstRange = DR.FirstDecl->getSourceRange();
10010         }
10011         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10012             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10013             << DR.FirstDiffType;
10014 
10015         SourceLocation SecondLoc;
10016         SourceRange SecondRange;
10017         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10018         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10019           SecondLoc = SecondTag->getBraceRange().getEnd();
10020         } else {
10021           SecondLoc = DR.SecondDecl->getLocation();
10022           SecondRange = DR.SecondDecl->getSourceRange();
10023         }
10024         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10025             << SecondModule << SecondRange << DR.SecondDiffType;
10026       };
10027 
10028   // Issue any pending ODR-failure diagnostics.
10029   for (auto &Merge : OdrMergeFailures) {
10030     // If we've already pointed out a specific problem with this class, don't
10031     // bother issuing a general "something's different" diagnostic.
10032     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10033       continue;
10034 
10035     bool Diagnosed = false;
10036     CXXRecordDecl *FirstRecord = Merge.first;
10037     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10038     for (auto &RecordPair : Merge.second) {
10039       CXXRecordDecl *SecondRecord = RecordPair.first;
10040       // Multiple different declarations got merged together; tell the user
10041       // where they came from.
10042       if (FirstRecord == SecondRecord)
10043         continue;
10044 
10045       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10046 
10047       auto *FirstDD = FirstRecord->DefinitionData;
10048       auto *SecondDD = RecordPair.second;
10049 
10050       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10051 
10052       // Diagnostics from DefinitionData are emitted here.
10053       if (FirstDD != SecondDD) {
10054         enum ODRDefinitionDataDifference {
10055           NumBases,
10056           NumVBases,
10057           BaseType,
10058           BaseVirtual,
10059           BaseAccess,
10060         };
10061         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10062                                  this](SourceLocation Loc, SourceRange Range,
10063                                        ODRDefinitionDataDifference DiffType) {
10064           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10065                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10066                  << DiffType;
10067         };
10068         auto ODRDiagBaseNote = [&SecondModule,
10069                                 this](SourceLocation Loc, SourceRange Range,
10070                                       ODRDefinitionDataDifference DiffType) {
10071           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10072                  << SecondModule << Range << DiffType;
10073         };
10074 
10075         unsigned FirstNumBases = FirstDD->NumBases;
10076         unsigned FirstNumVBases = FirstDD->NumVBases;
10077         unsigned SecondNumBases = SecondDD->NumBases;
10078         unsigned SecondNumVBases = SecondDD->NumVBases;
10079 
10080         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10081           unsigned NumBases = DD->NumBases;
10082           if (NumBases == 0) return SourceRange();
10083           auto bases = DD->bases();
10084           return SourceRange(bases[0].getBeginLoc(),
10085                              bases[NumBases - 1].getEndLoc());
10086         };
10087 
10088         if (FirstNumBases != SecondNumBases) {
10089           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10090                            NumBases)
10091               << FirstNumBases;
10092           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10093                           NumBases)
10094               << SecondNumBases;
10095           Diagnosed = true;
10096           break;
10097         }
10098 
10099         if (FirstNumVBases != SecondNumVBases) {
10100           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10101                            NumVBases)
10102               << FirstNumVBases;
10103           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10104                           NumVBases)
10105               << SecondNumVBases;
10106           Diagnosed = true;
10107           break;
10108         }
10109 
10110         auto FirstBases = FirstDD->bases();
10111         auto SecondBases = SecondDD->bases();
10112         unsigned i = 0;
10113         for (i = 0; i < FirstNumBases; ++i) {
10114           auto FirstBase = FirstBases[i];
10115           auto SecondBase = SecondBases[i];
10116           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10117               ComputeQualTypeODRHash(SecondBase.getType())) {
10118             ODRDiagBaseError(FirstRecord->getLocation(),
10119                              FirstBase.getSourceRange(), BaseType)
10120                 << (i + 1) << FirstBase.getType();
10121             ODRDiagBaseNote(SecondRecord->getLocation(),
10122                             SecondBase.getSourceRange(), BaseType)
10123                 << (i + 1) << SecondBase.getType();
10124             break;
10125           }
10126 
10127           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10128             ODRDiagBaseError(FirstRecord->getLocation(),
10129                              FirstBase.getSourceRange(), BaseVirtual)
10130                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10131             ODRDiagBaseNote(SecondRecord->getLocation(),
10132                             SecondBase.getSourceRange(), BaseVirtual)
10133                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10134             break;
10135           }
10136 
10137           if (FirstBase.getAccessSpecifierAsWritten() !=
10138               SecondBase.getAccessSpecifierAsWritten()) {
10139             ODRDiagBaseError(FirstRecord->getLocation(),
10140                              FirstBase.getSourceRange(), BaseAccess)
10141                 << (i + 1) << FirstBase.getType()
10142                 << (int)FirstBase.getAccessSpecifierAsWritten();
10143             ODRDiagBaseNote(SecondRecord->getLocation(),
10144                             SecondBase.getSourceRange(), BaseAccess)
10145                 << (i + 1) << SecondBase.getType()
10146                 << (int)SecondBase.getAccessSpecifierAsWritten();
10147             break;
10148           }
10149         }
10150 
10151         if (i != FirstNumBases) {
10152           Diagnosed = true;
10153           break;
10154         }
10155       }
10156 
10157       const ClassTemplateDecl *FirstTemplate =
10158           FirstRecord->getDescribedClassTemplate();
10159       const ClassTemplateDecl *SecondTemplate =
10160           SecondRecord->getDescribedClassTemplate();
10161 
10162       assert(!FirstTemplate == !SecondTemplate &&
10163              "Both pointers should be null or non-null");
10164 
10165       enum ODRTemplateDifference {
10166         ParamEmptyName,
10167         ParamName,
10168         ParamSingleDefaultArgument,
10169         ParamDifferentDefaultArgument,
10170       };
10171 
10172       if (FirstTemplate && SecondTemplate) {
10173         DeclHashes FirstTemplateHashes;
10174         DeclHashes SecondTemplateHashes;
10175 
10176         auto PopulateTemplateParameterHashs =
10177             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10178                                      const ClassTemplateDecl *TD) {
10179               for (auto *D : TD->getTemplateParameters()->asArray()) {
10180                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10181               }
10182             };
10183 
10184         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10185         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10186 
10187         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10188                "Number of template parameters should be equal.");
10189 
10190         auto FirstIt = FirstTemplateHashes.begin();
10191         auto FirstEnd = FirstTemplateHashes.end();
10192         auto SecondIt = SecondTemplateHashes.begin();
10193         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10194           if (FirstIt->second == SecondIt->second)
10195             continue;
10196 
10197           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10198                                           SourceLocation Loc, SourceRange Range,
10199                                           ODRTemplateDifference DiffType) {
10200             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10201                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10202                    << DiffType;
10203           };
10204           auto ODRDiagTemplateNote = [&SecondModule, this](
10205                                          SourceLocation Loc, SourceRange Range,
10206                                          ODRTemplateDifference DiffType) {
10207             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10208                    << SecondModule << Range << DiffType;
10209           };
10210 
10211           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10212           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10213 
10214           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10215                  "Parameter Decl's should be the same kind.");
10216 
10217           DeclarationName FirstName = FirstDecl->getDeclName();
10218           DeclarationName SecondName = SecondDecl->getDeclName();
10219 
10220           if (FirstName != SecondName) {
10221             const bool FirstNameEmpty =
10222                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10223             const bool SecondNameEmpty =
10224                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10225             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10226                    "Both template parameters cannot be unnamed.");
10227             ODRDiagTemplateError(FirstDecl->getLocation(),
10228                                  FirstDecl->getSourceRange(),
10229                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10230                 << FirstName;
10231             ODRDiagTemplateNote(SecondDecl->getLocation(),
10232                                 SecondDecl->getSourceRange(),
10233                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10234                 << SecondName;
10235             break;
10236           }
10237 
10238           switch (FirstDecl->getKind()) {
10239           default:
10240             llvm_unreachable("Invalid template parameter type.");
10241           case Decl::TemplateTypeParm: {
10242             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10243             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10244             const bool HasFirstDefaultArgument =
10245                 FirstParam->hasDefaultArgument() &&
10246                 !FirstParam->defaultArgumentWasInherited();
10247             const bool HasSecondDefaultArgument =
10248                 SecondParam->hasDefaultArgument() &&
10249                 !SecondParam->defaultArgumentWasInherited();
10250 
10251             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10252               ODRDiagTemplateError(FirstDecl->getLocation(),
10253                                    FirstDecl->getSourceRange(),
10254                                    ParamSingleDefaultArgument)
10255                   << HasFirstDefaultArgument;
10256               ODRDiagTemplateNote(SecondDecl->getLocation(),
10257                                   SecondDecl->getSourceRange(),
10258                                   ParamSingleDefaultArgument)
10259                   << HasSecondDefaultArgument;
10260               break;
10261             }
10262 
10263             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10264                    "Expecting default arguments.");
10265 
10266             ODRDiagTemplateError(FirstDecl->getLocation(),
10267                                  FirstDecl->getSourceRange(),
10268                                  ParamDifferentDefaultArgument);
10269             ODRDiagTemplateNote(SecondDecl->getLocation(),
10270                                 SecondDecl->getSourceRange(),
10271                                 ParamDifferentDefaultArgument);
10272 
10273             break;
10274           }
10275           case Decl::NonTypeTemplateParm: {
10276             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10277             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10278             const bool HasFirstDefaultArgument =
10279                 FirstParam->hasDefaultArgument() &&
10280                 !FirstParam->defaultArgumentWasInherited();
10281             const bool HasSecondDefaultArgument =
10282                 SecondParam->hasDefaultArgument() &&
10283                 !SecondParam->defaultArgumentWasInherited();
10284 
10285             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10286               ODRDiagTemplateError(FirstDecl->getLocation(),
10287                                    FirstDecl->getSourceRange(),
10288                                    ParamSingleDefaultArgument)
10289                   << HasFirstDefaultArgument;
10290               ODRDiagTemplateNote(SecondDecl->getLocation(),
10291                                   SecondDecl->getSourceRange(),
10292                                   ParamSingleDefaultArgument)
10293                   << HasSecondDefaultArgument;
10294               break;
10295             }
10296 
10297             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10298                    "Expecting default arguments.");
10299 
10300             ODRDiagTemplateError(FirstDecl->getLocation(),
10301                                  FirstDecl->getSourceRange(),
10302                                  ParamDifferentDefaultArgument);
10303             ODRDiagTemplateNote(SecondDecl->getLocation(),
10304                                 SecondDecl->getSourceRange(),
10305                                 ParamDifferentDefaultArgument);
10306 
10307             break;
10308           }
10309           case Decl::TemplateTemplateParm: {
10310             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10311             const auto *SecondParam =
10312                 cast<TemplateTemplateParmDecl>(SecondDecl);
10313             const bool HasFirstDefaultArgument =
10314                 FirstParam->hasDefaultArgument() &&
10315                 !FirstParam->defaultArgumentWasInherited();
10316             const bool HasSecondDefaultArgument =
10317                 SecondParam->hasDefaultArgument() &&
10318                 !SecondParam->defaultArgumentWasInherited();
10319 
10320             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10321               ODRDiagTemplateError(FirstDecl->getLocation(),
10322                                    FirstDecl->getSourceRange(),
10323                                    ParamSingleDefaultArgument)
10324                   << HasFirstDefaultArgument;
10325               ODRDiagTemplateNote(SecondDecl->getLocation(),
10326                                   SecondDecl->getSourceRange(),
10327                                   ParamSingleDefaultArgument)
10328                   << HasSecondDefaultArgument;
10329               break;
10330             }
10331 
10332             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10333                    "Expecting default arguments.");
10334 
10335             ODRDiagTemplateError(FirstDecl->getLocation(),
10336                                  FirstDecl->getSourceRange(),
10337                                  ParamDifferentDefaultArgument);
10338             ODRDiagTemplateNote(SecondDecl->getLocation(),
10339                                 SecondDecl->getSourceRange(),
10340                                 ParamDifferentDefaultArgument);
10341 
10342             break;
10343           }
10344           }
10345 
10346           break;
10347         }
10348 
10349         if (FirstIt != FirstEnd) {
10350           Diagnosed = true;
10351           break;
10352         }
10353       }
10354 
10355       DeclHashes FirstHashes;
10356       DeclHashes SecondHashes;
10357       const DeclContext *DC = FirstRecord;
10358       PopulateHashes(FirstHashes, FirstRecord, DC);
10359       PopulateHashes(SecondHashes, SecondRecord, DC);
10360 
10361       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10362       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10363       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10364       Decl *FirstDecl = DR.FirstDecl;
10365       Decl *SecondDecl = DR.SecondDecl;
10366 
10367       if (FirstDiffType == Other || SecondDiffType == Other) {
10368         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10369                               SecondModule);
10370         Diagnosed = true;
10371         break;
10372       }
10373 
10374       if (FirstDiffType != SecondDiffType) {
10375         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10376                             SecondModule);
10377         Diagnosed = true;
10378         break;
10379       }
10380 
10381       assert(FirstDiffType == SecondDiffType);
10382 
10383       switch (FirstDiffType) {
10384       case Other:
10385       case EndOfClass:
10386       case PublicSpecifer:
10387       case PrivateSpecifer:
10388       case ProtectedSpecifer:
10389         llvm_unreachable("Invalid diff type");
10390 
10391       case StaticAssert: {
10392         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10393         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10394 
10395         Expr *FirstExpr = FirstSA->getAssertExpr();
10396         Expr *SecondExpr = SecondSA->getAssertExpr();
10397         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10398         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10399         if (FirstODRHash != SecondODRHash) {
10400           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10401                            FirstExpr->getSourceRange(), StaticAssertCondition);
10402           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10403                           SecondExpr->getSourceRange(), StaticAssertCondition);
10404           Diagnosed = true;
10405           break;
10406         }
10407 
10408         StringLiteral *FirstStr = FirstSA->getMessage();
10409         StringLiteral *SecondStr = SecondSA->getMessage();
10410         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10411         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10412           SourceLocation FirstLoc, SecondLoc;
10413           SourceRange FirstRange, SecondRange;
10414           if (FirstStr) {
10415             FirstLoc = FirstStr->getBeginLoc();
10416             FirstRange = FirstStr->getSourceRange();
10417           } else {
10418             FirstLoc = FirstSA->getBeginLoc();
10419             FirstRange = FirstSA->getSourceRange();
10420           }
10421           if (SecondStr) {
10422             SecondLoc = SecondStr->getBeginLoc();
10423             SecondRange = SecondStr->getSourceRange();
10424           } else {
10425             SecondLoc = SecondSA->getBeginLoc();
10426             SecondRange = SecondSA->getSourceRange();
10427           }
10428           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10429                            StaticAssertOnlyMessage)
10430               << (FirstStr == nullptr);
10431           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10432                           StaticAssertOnlyMessage)
10433               << (SecondStr == nullptr);
10434           Diagnosed = true;
10435           break;
10436         }
10437 
10438         if (FirstStr && SecondStr &&
10439             FirstStr->getString() != SecondStr->getString()) {
10440           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10441                            FirstStr->getSourceRange(), StaticAssertMessage);
10442           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10443                           SecondStr->getSourceRange(), StaticAssertMessage);
10444           Diagnosed = true;
10445           break;
10446         }
10447         break;
10448       }
10449       case Field: {
10450         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10451                                  cast<FieldDecl>(FirstDecl),
10452                                  cast<FieldDecl>(SecondDecl));
10453         break;
10454       }
10455       case CXXMethod: {
10456         enum {
10457           DiagMethod,
10458           DiagConstructor,
10459           DiagDestructor,
10460         } FirstMethodType,
10461             SecondMethodType;
10462         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10463           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10464           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10465           return DiagMethod;
10466         };
10467         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10468         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10469         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10470         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10471         auto FirstName = FirstMethod->getDeclName();
10472         auto SecondName = SecondMethod->getDeclName();
10473         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10474           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10475                            FirstMethod->getSourceRange(), MethodName)
10476               << FirstMethodType << FirstName;
10477           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10478                           SecondMethod->getSourceRange(), MethodName)
10479               << SecondMethodType << SecondName;
10480 
10481           Diagnosed = true;
10482           break;
10483         }
10484 
10485         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10486         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10487         if (FirstDeleted != SecondDeleted) {
10488           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10489                            FirstMethod->getSourceRange(), MethodDeleted)
10490               << FirstMethodType << FirstName << FirstDeleted;
10491 
10492           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10493                           SecondMethod->getSourceRange(), MethodDeleted)
10494               << SecondMethodType << SecondName << SecondDeleted;
10495           Diagnosed = true;
10496           break;
10497         }
10498 
10499         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10500         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10501         if (FirstDefaulted != SecondDefaulted) {
10502           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10503                            FirstMethod->getSourceRange(), MethodDefaulted)
10504               << FirstMethodType << FirstName << FirstDefaulted;
10505 
10506           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10507                           SecondMethod->getSourceRange(), MethodDefaulted)
10508               << SecondMethodType << SecondName << SecondDefaulted;
10509           Diagnosed = true;
10510           break;
10511         }
10512 
10513         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10514         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10515         const bool FirstPure = FirstMethod->isPure();
10516         const bool SecondPure = SecondMethod->isPure();
10517         if ((FirstVirtual || SecondVirtual) &&
10518             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10519           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10520                            FirstMethod->getSourceRange(), MethodVirtual)
10521               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10522           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10523                           SecondMethod->getSourceRange(), MethodVirtual)
10524               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10525           Diagnosed = true;
10526           break;
10527         }
10528 
10529         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10530         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10531         // class needs to be checked instead.
10532         const auto FirstStorage = FirstMethod->getStorageClass();
10533         const auto SecondStorage = SecondMethod->getStorageClass();
10534         const bool FirstStatic = FirstStorage == SC_Static;
10535         const bool SecondStatic = SecondStorage == SC_Static;
10536         if (FirstStatic != SecondStatic) {
10537           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10538                            FirstMethod->getSourceRange(), MethodStatic)
10539               << FirstMethodType << FirstName << FirstStatic;
10540           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10541                           SecondMethod->getSourceRange(), MethodStatic)
10542               << SecondMethodType << SecondName << SecondStatic;
10543           Diagnosed = true;
10544           break;
10545         }
10546 
10547         const bool FirstVolatile = FirstMethod->isVolatile();
10548         const bool SecondVolatile = SecondMethod->isVolatile();
10549         if (FirstVolatile != SecondVolatile) {
10550           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10551                            FirstMethod->getSourceRange(), MethodVolatile)
10552               << FirstMethodType << FirstName << FirstVolatile;
10553           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10554                           SecondMethod->getSourceRange(), MethodVolatile)
10555               << SecondMethodType << SecondName << SecondVolatile;
10556           Diagnosed = true;
10557           break;
10558         }
10559 
10560         const bool FirstConst = FirstMethod->isConst();
10561         const bool SecondConst = SecondMethod->isConst();
10562         if (FirstConst != SecondConst) {
10563           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10564                            FirstMethod->getSourceRange(), MethodConst)
10565               << FirstMethodType << FirstName << FirstConst;
10566           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10567                           SecondMethod->getSourceRange(), MethodConst)
10568               << SecondMethodType << SecondName << SecondConst;
10569           Diagnosed = true;
10570           break;
10571         }
10572 
10573         const bool FirstInline = FirstMethod->isInlineSpecified();
10574         const bool SecondInline = SecondMethod->isInlineSpecified();
10575         if (FirstInline != SecondInline) {
10576           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10577                            FirstMethod->getSourceRange(), MethodInline)
10578               << FirstMethodType << FirstName << FirstInline;
10579           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10580                           SecondMethod->getSourceRange(), MethodInline)
10581               << SecondMethodType << SecondName << SecondInline;
10582           Diagnosed = true;
10583           break;
10584         }
10585 
10586         const unsigned FirstNumParameters = FirstMethod->param_size();
10587         const unsigned SecondNumParameters = SecondMethod->param_size();
10588         if (FirstNumParameters != SecondNumParameters) {
10589           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10590                            FirstMethod->getSourceRange(),
10591                            MethodNumberParameters)
10592               << FirstMethodType << FirstName << FirstNumParameters;
10593           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10594                           SecondMethod->getSourceRange(),
10595                           MethodNumberParameters)
10596               << SecondMethodType << SecondName << SecondNumParameters;
10597           Diagnosed = true;
10598           break;
10599         }
10600 
10601         // Need this status boolean to know when break out of the switch.
10602         bool ParameterMismatch = false;
10603         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10604           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10605           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10606 
10607           QualType FirstParamType = FirstParam->getType();
10608           QualType SecondParamType = SecondParam->getType();
10609           if (FirstParamType != SecondParamType &&
10610               ComputeQualTypeODRHash(FirstParamType) !=
10611                   ComputeQualTypeODRHash(SecondParamType)) {
10612             if (const DecayedType *ParamDecayedType =
10613                     FirstParamType->getAs<DecayedType>()) {
10614               ODRDiagDeclError(
10615                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10616                   FirstMethod->getSourceRange(), MethodParameterType)
10617                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10618                   << true << ParamDecayedType->getOriginalType();
10619             } else {
10620               ODRDiagDeclError(
10621                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10622                   FirstMethod->getSourceRange(), MethodParameterType)
10623                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10624                   << false;
10625             }
10626 
10627             if (const DecayedType *ParamDecayedType =
10628                     SecondParamType->getAs<DecayedType>()) {
10629               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10630                               SecondMethod->getSourceRange(),
10631                               MethodParameterType)
10632                   << SecondMethodType << SecondName << (I + 1)
10633                   << SecondParamType << true
10634                   << ParamDecayedType->getOriginalType();
10635             } else {
10636               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10637                               SecondMethod->getSourceRange(),
10638                               MethodParameterType)
10639                   << SecondMethodType << SecondName << (I + 1)
10640                   << SecondParamType << false;
10641             }
10642             ParameterMismatch = true;
10643             break;
10644           }
10645 
10646           DeclarationName FirstParamName = FirstParam->getDeclName();
10647           DeclarationName SecondParamName = SecondParam->getDeclName();
10648           if (FirstParamName != SecondParamName) {
10649             ODRDiagDeclError(FirstRecord, FirstModule,
10650                              FirstMethod->getLocation(),
10651                              FirstMethod->getSourceRange(), MethodParameterName)
10652                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10653             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10654                             SecondMethod->getSourceRange(), MethodParameterName)
10655                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10656             ParameterMismatch = true;
10657             break;
10658           }
10659 
10660           const Expr *FirstInit = FirstParam->getInit();
10661           const Expr *SecondInit = SecondParam->getInit();
10662           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10663             ODRDiagDeclError(FirstRecord, FirstModule,
10664                              FirstMethod->getLocation(),
10665                              FirstMethod->getSourceRange(),
10666                              MethodParameterSingleDefaultArgument)
10667                 << FirstMethodType << FirstName << (I + 1)
10668                 << (FirstInit == nullptr)
10669                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10670             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10671                             SecondMethod->getSourceRange(),
10672                             MethodParameterSingleDefaultArgument)
10673                 << SecondMethodType << SecondName << (I + 1)
10674                 << (SecondInit == nullptr)
10675                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10676             ParameterMismatch = true;
10677             break;
10678           }
10679 
10680           if (FirstInit && SecondInit &&
10681               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10682             ODRDiagDeclError(FirstRecord, FirstModule,
10683                              FirstMethod->getLocation(),
10684                              FirstMethod->getSourceRange(),
10685                              MethodParameterDifferentDefaultArgument)
10686                 << FirstMethodType << FirstName << (I + 1)
10687                 << FirstInit->getSourceRange();
10688             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10689                             SecondMethod->getSourceRange(),
10690                             MethodParameterDifferentDefaultArgument)
10691                 << SecondMethodType << SecondName << (I + 1)
10692                 << SecondInit->getSourceRange();
10693             ParameterMismatch = true;
10694             break;
10695 
10696           }
10697         }
10698 
10699         if (ParameterMismatch) {
10700           Diagnosed = true;
10701           break;
10702         }
10703 
10704         const auto *FirstTemplateArgs =
10705             FirstMethod->getTemplateSpecializationArgs();
10706         const auto *SecondTemplateArgs =
10707             SecondMethod->getTemplateSpecializationArgs();
10708 
10709         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10710             (!FirstTemplateArgs && SecondTemplateArgs)) {
10711           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10712                            FirstMethod->getSourceRange(),
10713                            MethodNoTemplateArguments)
10714               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10715           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10716                           SecondMethod->getSourceRange(),
10717                           MethodNoTemplateArguments)
10718               << SecondMethodType << SecondName
10719               << (SecondTemplateArgs != nullptr);
10720 
10721           Diagnosed = true;
10722           break;
10723         }
10724 
10725         if (FirstTemplateArgs && SecondTemplateArgs) {
10726           // Remove pack expansions from argument list.
10727           auto ExpandTemplateArgumentList =
10728               [](const TemplateArgumentList *TAL) {
10729                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10730                 for (const TemplateArgument &TA : TAL->asArray()) {
10731                   if (TA.getKind() != TemplateArgument::Pack) {
10732                     ExpandedList.push_back(&TA);
10733                     continue;
10734                   }
10735                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10736                     ExpandedList.push_back(&PackTA);
10737                   }
10738                 }
10739                 return ExpandedList;
10740               };
10741           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10742               ExpandTemplateArgumentList(FirstTemplateArgs);
10743           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10744               ExpandTemplateArgumentList(SecondTemplateArgs);
10745 
10746           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10747             ODRDiagDeclError(FirstRecord, FirstModule,
10748                              FirstMethod->getLocation(),
10749                              FirstMethod->getSourceRange(),
10750                              MethodDifferentNumberTemplateArguments)
10751                 << FirstMethodType << FirstName
10752                 << (unsigned)FirstExpandedList.size();
10753             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10754                             SecondMethod->getSourceRange(),
10755                             MethodDifferentNumberTemplateArguments)
10756                 << SecondMethodType << SecondName
10757                 << (unsigned)SecondExpandedList.size();
10758 
10759             Diagnosed = true;
10760             break;
10761           }
10762 
10763           bool TemplateArgumentMismatch = false;
10764           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10765             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10766                                    &SecondTA = *SecondExpandedList[i];
10767             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10768                 ComputeTemplateArgumentODRHash(SecondTA)) {
10769               continue;
10770             }
10771 
10772             ODRDiagDeclError(
10773                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10774                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10775                 << FirstMethodType << FirstName << FirstTA << i + 1;
10776             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10777                             SecondMethod->getSourceRange(),
10778                             MethodDifferentTemplateArgument)
10779                 << SecondMethodType << SecondName << SecondTA << i + 1;
10780 
10781             TemplateArgumentMismatch = true;
10782             break;
10783           }
10784 
10785           if (TemplateArgumentMismatch) {
10786             Diagnosed = true;
10787             break;
10788           }
10789         }
10790 
10791         // Compute the hash of the method as if it has no body.
10792         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10793           Hash.clear();
10794           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10795           return Hash.CalculateHash();
10796         };
10797 
10798         // Compare the hash generated to the hash stored.  A difference means
10799         // that a body was present in the original source.  Due to merging,
10800         // the stardard way of detecting a body will not work.
10801         const bool HasFirstBody =
10802             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10803         const bool HasSecondBody =
10804             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10805 
10806         if (HasFirstBody != HasSecondBody) {
10807           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10808                            FirstMethod->getSourceRange(), MethodSingleBody)
10809               << FirstMethodType << FirstName << HasFirstBody;
10810           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10811                           SecondMethod->getSourceRange(), MethodSingleBody)
10812               << SecondMethodType << SecondName << HasSecondBody;
10813           Diagnosed = true;
10814           break;
10815         }
10816 
10817         if (HasFirstBody && HasSecondBody) {
10818           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10819                            FirstMethod->getSourceRange(), MethodDifferentBody)
10820               << FirstMethodType << FirstName;
10821           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10822                           SecondMethod->getSourceRange(), MethodDifferentBody)
10823               << SecondMethodType << SecondName;
10824           Diagnosed = true;
10825           break;
10826         }
10827 
10828         break;
10829       }
10830       case TypeAlias:
10831       case TypeDef: {
10832         Diagnosed = ODRDiagTypeDefOrAlias(
10833             FirstRecord, FirstModule, SecondModule,
10834             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10835             FirstDiffType == TypeAlias);
10836         break;
10837       }
10838       case Var: {
10839         Diagnosed =
10840             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10841                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10842         break;
10843       }
10844       case Friend: {
10845         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10846         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10847 
10848         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10849         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10850 
10851         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10852         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10853 
10854         if (FirstND && SecondND) {
10855           ODRDiagDeclError(FirstRecord, FirstModule,
10856                            FirstFriend->getFriendLoc(),
10857                            FirstFriend->getSourceRange(), FriendFunction)
10858               << FirstND;
10859           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10860                           SecondFriend->getSourceRange(), FriendFunction)
10861               << SecondND;
10862 
10863           Diagnosed = true;
10864           break;
10865         }
10866 
10867         if (FirstTSI && SecondTSI) {
10868           QualType FirstFriendType = FirstTSI->getType();
10869           QualType SecondFriendType = SecondTSI->getType();
10870           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10871                  ComputeQualTypeODRHash(SecondFriendType));
10872           ODRDiagDeclError(FirstRecord, FirstModule,
10873                            FirstFriend->getFriendLoc(),
10874                            FirstFriend->getSourceRange(), FriendType)
10875               << FirstFriendType;
10876           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10877                           SecondFriend->getSourceRange(), FriendType)
10878               << SecondFriendType;
10879           Diagnosed = true;
10880           break;
10881         }
10882 
10883         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10884                          FirstFriend->getSourceRange(), FriendTypeFunction)
10885             << (FirstTSI == nullptr);
10886         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10887                         SecondFriend->getSourceRange(), FriendTypeFunction)
10888             << (SecondTSI == nullptr);
10889 
10890         Diagnosed = true;
10891         break;
10892       }
10893       case FunctionTemplate: {
10894         FunctionTemplateDecl *FirstTemplate =
10895             cast<FunctionTemplateDecl>(FirstDecl);
10896         FunctionTemplateDecl *SecondTemplate =
10897             cast<FunctionTemplateDecl>(SecondDecl);
10898 
10899         TemplateParameterList *FirstTPL =
10900             FirstTemplate->getTemplateParameters();
10901         TemplateParameterList *SecondTPL =
10902             SecondTemplate->getTemplateParameters();
10903 
10904         if (FirstTPL->size() != SecondTPL->size()) {
10905           ODRDiagDeclError(FirstRecord, FirstModule,
10906                            FirstTemplate->getLocation(),
10907                            FirstTemplate->getSourceRange(),
10908                            FunctionTemplateDifferentNumberParameters)
10909               << FirstTemplate << FirstTPL->size();
10910           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10911                           SecondTemplate->getSourceRange(),
10912                           FunctionTemplateDifferentNumberParameters)
10913               << SecondTemplate << SecondTPL->size();
10914 
10915           Diagnosed = true;
10916           break;
10917         }
10918 
10919         bool ParameterMismatch = false;
10920         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10921           NamedDecl *FirstParam = FirstTPL->getParam(i);
10922           NamedDecl *SecondParam = SecondTPL->getParam(i);
10923 
10924           if (FirstParam->getKind() != SecondParam->getKind()) {
10925             enum {
10926               TemplateTypeParameter,
10927               NonTypeTemplateParameter,
10928               TemplateTemplateParameter,
10929             };
10930             auto GetParamType = [](NamedDecl *D) {
10931               switch (D->getKind()) {
10932                 default:
10933                   llvm_unreachable("Unexpected template parameter type");
10934                 case Decl::TemplateTypeParm:
10935                   return TemplateTypeParameter;
10936                 case Decl::NonTypeTemplateParm:
10937                   return NonTypeTemplateParameter;
10938                 case Decl::TemplateTemplateParm:
10939                   return TemplateTemplateParameter;
10940               }
10941             };
10942 
10943             ODRDiagDeclError(FirstRecord, FirstModule,
10944                              FirstTemplate->getLocation(),
10945                              FirstTemplate->getSourceRange(),
10946                              FunctionTemplateParameterDifferentKind)
10947                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10948             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10949                             SecondTemplate->getSourceRange(),
10950                             FunctionTemplateParameterDifferentKind)
10951                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10952 
10953             ParameterMismatch = true;
10954             break;
10955           }
10956 
10957           if (FirstParam->getName() != SecondParam->getName()) {
10958             ODRDiagDeclError(
10959                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10960                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10961                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10962                 << FirstParam;
10963             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10964                             SecondTemplate->getSourceRange(),
10965                             FunctionTemplateParameterName)
10966                 << SecondTemplate << (i + 1)
10967                 << (bool)SecondParam->getIdentifier() << SecondParam;
10968             ParameterMismatch = true;
10969             break;
10970           }
10971 
10972           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10973               isa<TemplateTypeParmDecl>(SecondParam)) {
10974             TemplateTypeParmDecl *FirstTTPD =
10975                 cast<TemplateTypeParmDecl>(FirstParam);
10976             TemplateTypeParmDecl *SecondTTPD =
10977                 cast<TemplateTypeParmDecl>(SecondParam);
10978             bool HasFirstDefaultArgument =
10979                 FirstTTPD->hasDefaultArgument() &&
10980                 !FirstTTPD->defaultArgumentWasInherited();
10981             bool HasSecondDefaultArgument =
10982                 SecondTTPD->hasDefaultArgument() &&
10983                 !SecondTTPD->defaultArgumentWasInherited();
10984             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10985               ODRDiagDeclError(FirstRecord, FirstModule,
10986                                FirstTemplate->getLocation(),
10987                                FirstTemplate->getSourceRange(),
10988                                FunctionTemplateParameterSingleDefaultArgument)
10989                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10990               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10991                               SecondTemplate->getSourceRange(),
10992                               FunctionTemplateParameterSingleDefaultArgument)
10993                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10994               ParameterMismatch = true;
10995               break;
10996             }
10997 
10998             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10999               QualType FirstType = FirstTTPD->getDefaultArgument();
11000               QualType SecondType = SecondTTPD->getDefaultArgument();
11001               if (ComputeQualTypeODRHash(FirstType) !=
11002                   ComputeQualTypeODRHash(SecondType)) {
11003                 ODRDiagDeclError(
11004                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11005                     FirstTemplate->getSourceRange(),
11006                     FunctionTemplateParameterDifferentDefaultArgument)
11007                     << FirstTemplate << (i + 1) << FirstType;
11008                 ODRDiagDeclNote(
11009                     SecondModule, SecondTemplate->getLocation(),
11010                     SecondTemplate->getSourceRange(),
11011                     FunctionTemplateParameterDifferentDefaultArgument)
11012                     << SecondTemplate << (i + 1) << SecondType;
11013                 ParameterMismatch = true;
11014                 break;
11015               }
11016             }
11017 
11018             if (FirstTTPD->isParameterPack() !=
11019                 SecondTTPD->isParameterPack()) {
11020               ODRDiagDeclError(FirstRecord, FirstModule,
11021                                FirstTemplate->getLocation(),
11022                                FirstTemplate->getSourceRange(),
11023                                FunctionTemplatePackParameter)
11024                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11025               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11026                               SecondTemplate->getSourceRange(),
11027                               FunctionTemplatePackParameter)
11028                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11029               ParameterMismatch = true;
11030               break;
11031             }
11032           }
11033 
11034           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11035               isa<TemplateTemplateParmDecl>(SecondParam)) {
11036             TemplateTemplateParmDecl *FirstTTPD =
11037                 cast<TemplateTemplateParmDecl>(FirstParam);
11038             TemplateTemplateParmDecl *SecondTTPD =
11039                 cast<TemplateTemplateParmDecl>(SecondParam);
11040 
11041             TemplateParameterList *FirstTPL =
11042                 FirstTTPD->getTemplateParameters();
11043             TemplateParameterList *SecondTPL =
11044                 SecondTTPD->getTemplateParameters();
11045 
11046             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11047                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11048               ODRDiagDeclError(FirstRecord, FirstModule,
11049                                FirstTemplate->getLocation(),
11050                                FirstTemplate->getSourceRange(),
11051                                FunctionTemplateParameterDifferentType)
11052                   << FirstTemplate << (i + 1);
11053               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11054                               SecondTemplate->getSourceRange(),
11055                               FunctionTemplateParameterDifferentType)
11056                   << SecondTemplate << (i + 1);
11057               ParameterMismatch = true;
11058               break;
11059             }
11060 
11061             bool HasFirstDefaultArgument =
11062                 FirstTTPD->hasDefaultArgument() &&
11063                 !FirstTTPD->defaultArgumentWasInherited();
11064             bool HasSecondDefaultArgument =
11065                 SecondTTPD->hasDefaultArgument() &&
11066                 !SecondTTPD->defaultArgumentWasInherited();
11067             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11068               ODRDiagDeclError(FirstRecord, FirstModule,
11069                                FirstTemplate->getLocation(),
11070                                FirstTemplate->getSourceRange(),
11071                                FunctionTemplateParameterSingleDefaultArgument)
11072                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11073               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11074                               SecondTemplate->getSourceRange(),
11075                               FunctionTemplateParameterSingleDefaultArgument)
11076                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11077               ParameterMismatch = true;
11078               break;
11079             }
11080 
11081             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11082               TemplateArgument FirstTA =
11083                   FirstTTPD->getDefaultArgument().getArgument();
11084               TemplateArgument SecondTA =
11085                   SecondTTPD->getDefaultArgument().getArgument();
11086               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11087                   ComputeTemplateArgumentODRHash(SecondTA)) {
11088                 ODRDiagDeclError(
11089                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11090                     FirstTemplate->getSourceRange(),
11091                     FunctionTemplateParameterDifferentDefaultArgument)
11092                     << FirstTemplate << (i + 1) << FirstTA;
11093                 ODRDiagDeclNote(
11094                     SecondModule, SecondTemplate->getLocation(),
11095                     SecondTemplate->getSourceRange(),
11096                     FunctionTemplateParameterDifferentDefaultArgument)
11097                     << SecondTemplate << (i + 1) << SecondTA;
11098                 ParameterMismatch = true;
11099                 break;
11100               }
11101             }
11102 
11103             if (FirstTTPD->isParameterPack() !=
11104                 SecondTTPD->isParameterPack()) {
11105               ODRDiagDeclError(FirstRecord, FirstModule,
11106                                FirstTemplate->getLocation(),
11107                                FirstTemplate->getSourceRange(),
11108                                FunctionTemplatePackParameter)
11109                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11110               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11111                               SecondTemplate->getSourceRange(),
11112                               FunctionTemplatePackParameter)
11113                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11114               ParameterMismatch = true;
11115               break;
11116             }
11117           }
11118 
11119           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11120               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11121             NonTypeTemplateParmDecl *FirstNTTPD =
11122                 cast<NonTypeTemplateParmDecl>(FirstParam);
11123             NonTypeTemplateParmDecl *SecondNTTPD =
11124                 cast<NonTypeTemplateParmDecl>(SecondParam);
11125 
11126             QualType FirstType = FirstNTTPD->getType();
11127             QualType SecondType = SecondNTTPD->getType();
11128             if (ComputeQualTypeODRHash(FirstType) !=
11129                 ComputeQualTypeODRHash(SecondType)) {
11130               ODRDiagDeclError(FirstRecord, FirstModule,
11131                                FirstTemplate->getLocation(),
11132                                FirstTemplate->getSourceRange(),
11133                                FunctionTemplateParameterDifferentType)
11134                   << FirstTemplate << (i + 1);
11135               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11136                               SecondTemplate->getSourceRange(),
11137                               FunctionTemplateParameterDifferentType)
11138                   << SecondTemplate << (i + 1);
11139               ParameterMismatch = true;
11140               break;
11141             }
11142 
11143             bool HasFirstDefaultArgument =
11144                 FirstNTTPD->hasDefaultArgument() &&
11145                 !FirstNTTPD->defaultArgumentWasInherited();
11146             bool HasSecondDefaultArgument =
11147                 SecondNTTPD->hasDefaultArgument() &&
11148                 !SecondNTTPD->defaultArgumentWasInherited();
11149             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11150               ODRDiagDeclError(FirstRecord, FirstModule,
11151                                FirstTemplate->getLocation(),
11152                                FirstTemplate->getSourceRange(),
11153                                FunctionTemplateParameterSingleDefaultArgument)
11154                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11155               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11156                               SecondTemplate->getSourceRange(),
11157                               FunctionTemplateParameterSingleDefaultArgument)
11158                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11159               ParameterMismatch = true;
11160               break;
11161             }
11162 
11163             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11164               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11165               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11166               if (ComputeODRHash(FirstDefaultArgument) !=
11167                   ComputeODRHash(SecondDefaultArgument)) {
11168                 ODRDiagDeclError(
11169                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11170                     FirstTemplate->getSourceRange(),
11171                     FunctionTemplateParameterDifferentDefaultArgument)
11172                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11173                 ODRDiagDeclNote(
11174                     SecondModule, SecondTemplate->getLocation(),
11175                     SecondTemplate->getSourceRange(),
11176                     FunctionTemplateParameterDifferentDefaultArgument)
11177                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11178                 ParameterMismatch = true;
11179                 break;
11180               }
11181             }
11182 
11183             if (FirstNTTPD->isParameterPack() !=
11184                 SecondNTTPD->isParameterPack()) {
11185               ODRDiagDeclError(FirstRecord, FirstModule,
11186                                FirstTemplate->getLocation(),
11187                                FirstTemplate->getSourceRange(),
11188                                FunctionTemplatePackParameter)
11189                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11190               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11191                               SecondTemplate->getSourceRange(),
11192                               FunctionTemplatePackParameter)
11193                   << SecondTemplate << (i + 1)
11194                   << SecondNTTPD->isParameterPack();
11195               ParameterMismatch = true;
11196               break;
11197             }
11198           }
11199         }
11200 
11201         if (ParameterMismatch) {
11202           Diagnosed = true;
11203           break;
11204         }
11205 
11206         break;
11207       }
11208       }
11209 
11210       if (Diagnosed)
11211         continue;
11212 
11213       Diag(FirstDecl->getLocation(),
11214            diag::err_module_odr_violation_mismatch_decl_unknown)
11215           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11216           << FirstDecl->getSourceRange();
11217       Diag(SecondDecl->getLocation(),
11218            diag::note_module_odr_violation_mismatch_decl_unknown)
11219           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11220       Diagnosed = true;
11221     }
11222 
11223     if (!Diagnosed) {
11224       // All definitions are updates to the same declaration. This happens if a
11225       // module instantiates the declaration of a class template specialization
11226       // and two or more other modules instantiate its definition.
11227       //
11228       // FIXME: Indicate which modules had instantiations of this definition.
11229       // FIXME: How can this even happen?
11230       Diag(Merge.first->getLocation(),
11231            diag::err_module_odr_violation_different_instantiations)
11232         << Merge.first;
11233     }
11234   }
11235 
11236   // Issue ODR failures diagnostics for functions.
11237   for (auto &Merge : FunctionOdrMergeFailures) {
11238     enum ODRFunctionDifference {
11239       ReturnType,
11240       ParameterName,
11241       ParameterType,
11242       ParameterSingleDefaultArgument,
11243       ParameterDifferentDefaultArgument,
11244       FunctionBody,
11245     };
11246 
11247     FunctionDecl *FirstFunction = Merge.first;
11248     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11249 
11250     bool Diagnosed = false;
11251     for (auto &SecondFunction : Merge.second) {
11252 
11253       if (FirstFunction == SecondFunction)
11254         continue;
11255 
11256       std::string SecondModule =
11257           getOwningModuleNameForDiagnostic(SecondFunction);
11258 
11259       auto ODRDiagError = [FirstFunction, &FirstModule,
11260                            this](SourceLocation Loc, SourceRange Range,
11261                                  ODRFunctionDifference DiffType) {
11262         return Diag(Loc, diag::err_module_odr_violation_function)
11263                << FirstFunction << FirstModule.empty() << FirstModule << Range
11264                << DiffType;
11265       };
11266       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11267                                                SourceRange Range,
11268                                                ODRFunctionDifference DiffType) {
11269         return Diag(Loc, diag::note_module_odr_violation_function)
11270                << SecondModule << Range << DiffType;
11271       };
11272 
11273       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11274           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11275         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11276                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11277             << FirstFunction->getReturnType();
11278         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11279                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11280             << SecondFunction->getReturnType();
11281         Diagnosed = true;
11282         break;
11283       }
11284 
11285       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11286              "Merged functions with different number of parameters");
11287 
11288       auto ParamSize = FirstFunction->param_size();
11289       bool ParameterMismatch = false;
11290       for (unsigned I = 0; I < ParamSize; ++I) {
11291         auto *FirstParam = FirstFunction->getParamDecl(I);
11292         auto *SecondParam = SecondFunction->getParamDecl(I);
11293 
11294         assert(getContext().hasSameType(FirstParam->getType(),
11295                                       SecondParam->getType()) &&
11296                "Merged function has different parameter types.");
11297 
11298         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11299           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11300                        ParameterName)
11301               << I + 1 << FirstParam->getDeclName();
11302           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11303                       ParameterName)
11304               << I + 1 << SecondParam->getDeclName();
11305           ParameterMismatch = true;
11306           break;
11307         };
11308 
11309         QualType FirstParamType = FirstParam->getType();
11310         QualType SecondParamType = SecondParam->getType();
11311         if (FirstParamType != SecondParamType &&
11312             ComputeQualTypeODRHash(FirstParamType) !=
11313                 ComputeQualTypeODRHash(SecondParamType)) {
11314           if (const DecayedType *ParamDecayedType =
11315                   FirstParamType->getAs<DecayedType>()) {
11316             ODRDiagError(FirstParam->getLocation(),
11317                          FirstParam->getSourceRange(), ParameterType)
11318                 << (I + 1) << FirstParamType << true
11319                 << ParamDecayedType->getOriginalType();
11320           } else {
11321             ODRDiagError(FirstParam->getLocation(),
11322                          FirstParam->getSourceRange(), ParameterType)
11323                 << (I + 1) << FirstParamType << false;
11324           }
11325 
11326           if (const DecayedType *ParamDecayedType =
11327                   SecondParamType->getAs<DecayedType>()) {
11328             ODRDiagNote(SecondParam->getLocation(),
11329                         SecondParam->getSourceRange(), ParameterType)
11330                 << (I + 1) << SecondParamType << true
11331                 << ParamDecayedType->getOriginalType();
11332           } else {
11333             ODRDiagNote(SecondParam->getLocation(),
11334                         SecondParam->getSourceRange(), ParameterType)
11335                 << (I + 1) << SecondParamType << false;
11336           }
11337           ParameterMismatch = true;
11338           break;
11339         }
11340 
11341         const Expr *FirstInit = FirstParam->getInit();
11342         const Expr *SecondInit = SecondParam->getInit();
11343         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11344           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11345                        ParameterSingleDefaultArgument)
11346               << (I + 1) << (FirstInit == nullptr)
11347               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11348           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11349                       ParameterSingleDefaultArgument)
11350               << (I + 1) << (SecondInit == nullptr)
11351               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11352           ParameterMismatch = true;
11353           break;
11354         }
11355 
11356         if (FirstInit && SecondInit &&
11357             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11358           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11359                        ParameterDifferentDefaultArgument)
11360               << (I + 1) << FirstInit->getSourceRange();
11361           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11362                       ParameterDifferentDefaultArgument)
11363               << (I + 1) << SecondInit->getSourceRange();
11364           ParameterMismatch = true;
11365           break;
11366         }
11367 
11368         assert(ComputeSubDeclODRHash(FirstParam) ==
11369                    ComputeSubDeclODRHash(SecondParam) &&
11370                "Undiagnosed parameter difference.");
11371       }
11372 
11373       if (ParameterMismatch) {
11374         Diagnosed = true;
11375         break;
11376       }
11377 
11378       // If no error has been generated before now, assume the problem is in
11379       // the body and generate a message.
11380       ODRDiagError(FirstFunction->getLocation(),
11381                    FirstFunction->getSourceRange(), FunctionBody);
11382       ODRDiagNote(SecondFunction->getLocation(),
11383                   SecondFunction->getSourceRange(), FunctionBody);
11384       Diagnosed = true;
11385       break;
11386     }
11387     (void)Diagnosed;
11388     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11389   }
11390 
11391   // Issue ODR failures diagnostics for enums.
11392   for (auto &Merge : EnumOdrMergeFailures) {
11393     enum ODREnumDifference {
11394       SingleScopedEnum,
11395       EnumTagKeywordMismatch,
11396       SingleSpecifiedType,
11397       DifferentSpecifiedTypes,
11398       DifferentNumberEnumConstants,
11399       EnumConstantName,
11400       EnumConstantSingleInitilizer,
11401       EnumConstantDifferentInitilizer,
11402     };
11403 
11404     // If we've already pointed out a specific problem with this enum, don't
11405     // bother issuing a general "something's different" diagnostic.
11406     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11407       continue;
11408 
11409     EnumDecl *FirstEnum = Merge.first;
11410     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11411 
11412     using DeclHashes =
11413         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11414     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11415                               DeclHashes &Hashes, EnumDecl *Enum) {
11416       for (auto *D : Enum->decls()) {
11417         // Due to decl merging, the first EnumDecl is the parent of
11418         // Decls in both records.
11419         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11420           continue;
11421         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11422         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11423                             ComputeSubDeclODRHash(D));
11424       }
11425     };
11426     DeclHashes FirstHashes;
11427     PopulateHashes(FirstHashes, FirstEnum);
11428     bool Diagnosed = false;
11429     for (auto &SecondEnum : Merge.second) {
11430 
11431       if (FirstEnum == SecondEnum)
11432         continue;
11433 
11434       std::string SecondModule =
11435           getOwningModuleNameForDiagnostic(SecondEnum);
11436 
11437       auto ODRDiagError = [FirstEnum, &FirstModule,
11438                            this](SourceLocation Loc, SourceRange Range,
11439                                  ODREnumDifference DiffType) {
11440         return Diag(Loc, diag::err_module_odr_violation_enum)
11441                << FirstEnum << FirstModule.empty() << FirstModule << Range
11442                << DiffType;
11443       };
11444       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11445                                                SourceRange Range,
11446                                                ODREnumDifference DiffType) {
11447         return Diag(Loc, diag::note_module_odr_violation_enum)
11448                << SecondModule << Range << DiffType;
11449       };
11450 
11451       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11452         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11453                      SingleScopedEnum)
11454             << FirstEnum->isScoped();
11455         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11456                     SingleScopedEnum)
11457             << SecondEnum->isScoped();
11458         Diagnosed = true;
11459         continue;
11460       }
11461 
11462       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11463         if (FirstEnum->isScopedUsingClassTag() !=
11464             SecondEnum->isScopedUsingClassTag()) {
11465           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11466                        EnumTagKeywordMismatch)
11467               << FirstEnum->isScopedUsingClassTag();
11468           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11469                       EnumTagKeywordMismatch)
11470               << SecondEnum->isScopedUsingClassTag();
11471           Diagnosed = true;
11472           continue;
11473         }
11474       }
11475 
11476       QualType FirstUnderlyingType =
11477           FirstEnum->getIntegerTypeSourceInfo()
11478               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11479               : QualType();
11480       QualType SecondUnderlyingType =
11481           SecondEnum->getIntegerTypeSourceInfo()
11482               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11483               : QualType();
11484       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11485           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11486                        SingleSpecifiedType)
11487               << !FirstUnderlyingType.isNull();
11488           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11489                       SingleSpecifiedType)
11490               << !SecondUnderlyingType.isNull();
11491           Diagnosed = true;
11492           continue;
11493       }
11494 
11495       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11496         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11497             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11498           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11499                        DifferentSpecifiedTypes)
11500               << FirstUnderlyingType;
11501           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11502                       DifferentSpecifiedTypes)
11503               << SecondUnderlyingType;
11504           Diagnosed = true;
11505           continue;
11506         }
11507       }
11508 
11509       DeclHashes SecondHashes;
11510       PopulateHashes(SecondHashes, SecondEnum);
11511 
11512       if (FirstHashes.size() != SecondHashes.size()) {
11513         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11514                      DifferentNumberEnumConstants)
11515             << (int)FirstHashes.size();
11516         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11517                     DifferentNumberEnumConstants)
11518             << (int)SecondHashes.size();
11519         Diagnosed = true;
11520         continue;
11521       }
11522 
11523       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11524         if (FirstHashes[I].second == SecondHashes[I].second)
11525           continue;
11526         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11527         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11528 
11529         if (FirstEnumConstant->getDeclName() !=
11530             SecondEnumConstant->getDeclName()) {
11531 
11532           ODRDiagError(FirstEnumConstant->getLocation(),
11533                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11534               << I + 1 << FirstEnumConstant;
11535           ODRDiagNote(SecondEnumConstant->getLocation(),
11536                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11537               << I + 1 << SecondEnumConstant;
11538           Diagnosed = true;
11539           break;
11540         }
11541 
11542         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11543         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11544         if (!FirstInit && !SecondInit)
11545           continue;
11546 
11547         if (!FirstInit || !SecondInit) {
11548           ODRDiagError(FirstEnumConstant->getLocation(),
11549                        FirstEnumConstant->getSourceRange(),
11550                        EnumConstantSingleInitilizer)
11551               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11552           ODRDiagNote(SecondEnumConstant->getLocation(),
11553                       SecondEnumConstant->getSourceRange(),
11554                       EnumConstantSingleInitilizer)
11555               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11556           Diagnosed = true;
11557           break;
11558         }
11559 
11560         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11561           ODRDiagError(FirstEnumConstant->getLocation(),
11562                        FirstEnumConstant->getSourceRange(),
11563                        EnumConstantDifferentInitilizer)
11564               << I + 1 << FirstEnumConstant;
11565           ODRDiagNote(SecondEnumConstant->getLocation(),
11566                       SecondEnumConstant->getSourceRange(),
11567                       EnumConstantDifferentInitilizer)
11568               << I + 1 << SecondEnumConstant;
11569           Diagnosed = true;
11570           break;
11571         }
11572       }
11573     }
11574 
11575     (void)Diagnosed;
11576     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11577   }
11578 }
11579 
11580 void ASTReader::StartedDeserializing() {
11581   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11582     ReadTimer->startTimer();
11583 }
11584 
11585 void ASTReader::FinishedDeserializing() {
11586   assert(NumCurrentElementsDeserializing &&
11587          "FinishedDeserializing not paired with StartedDeserializing");
11588   if (NumCurrentElementsDeserializing == 1) {
11589     // We decrease NumCurrentElementsDeserializing only after pending actions
11590     // are finished, to avoid recursively re-calling finishPendingActions().
11591     finishPendingActions();
11592   }
11593   --NumCurrentElementsDeserializing;
11594 
11595   if (NumCurrentElementsDeserializing == 0) {
11596     // Propagate exception specification and deduced type updates along
11597     // redeclaration chains.
11598     //
11599     // We do this now rather than in finishPendingActions because we want to
11600     // be able to walk the complete redeclaration chains of the updated decls.
11601     while (!PendingExceptionSpecUpdates.empty() ||
11602            !PendingDeducedTypeUpdates.empty()) {
11603       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11604       PendingExceptionSpecUpdates.clear();
11605       for (auto Update : ESUpdates) {
11606         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11607         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11608         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11609         if (auto *Listener = getContext().getASTMutationListener())
11610           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11611         for (auto *Redecl : Update.second->redecls())
11612           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11613       }
11614 
11615       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11616       PendingDeducedTypeUpdates.clear();
11617       for (auto Update : DTUpdates) {
11618         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11619         // FIXME: If the return type is already deduced, check that it matches.
11620         getContext().adjustDeducedFunctionResultType(Update.first,
11621                                                      Update.second);
11622       }
11623     }
11624 
11625     if (ReadTimer)
11626       ReadTimer->stopTimer();
11627 
11628     diagnoseOdrViolations();
11629 
11630     // We are not in recursive loading, so it's safe to pass the "interesting"
11631     // decls to the consumer.
11632     if (Consumer)
11633       PassInterestingDeclsToConsumer();
11634   }
11635 }
11636 
11637 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11638   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11639     // Remove any fake results before adding any real ones.
11640     auto It = PendingFakeLookupResults.find(II);
11641     if (It != PendingFakeLookupResults.end()) {
11642       for (auto *ND : It->second)
11643         SemaObj->IdResolver.RemoveDecl(ND);
11644       // FIXME: this works around module+PCH performance issue.
11645       // Rather than erase the result from the map, which is O(n), just clear
11646       // the vector of NamedDecls.
11647       It->second.clear();
11648     }
11649   }
11650 
11651   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11652     SemaObj->TUScope->AddDecl(D);
11653   } else if (SemaObj->TUScope) {
11654     // Adding the decl to IdResolver may have failed because it was already in
11655     // (even though it was not added in scope). If it is already in, make sure
11656     // it gets in the scope as well.
11657     if (std::find(SemaObj->IdResolver.begin(Name),
11658                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11659       SemaObj->TUScope->AddDecl(D);
11660   }
11661 }
11662 
11663 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11664                      ASTContext *Context,
11665                      const PCHContainerReader &PCHContainerRdr,
11666                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11667                      StringRef isysroot, bool DisableValidation,
11668                      bool AllowASTWithCompilerErrors,
11669                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11670                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11671                      std::unique_ptr<llvm::Timer> ReadTimer)
11672     : Listener(DisableValidation
11673                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11674                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11675       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11676       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11677       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11678                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11679       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11680       DisableValidation(DisableValidation),
11681       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11682       AllowConfigurationMismatch(AllowConfigurationMismatch),
11683       ValidateSystemInputs(ValidateSystemInputs),
11684       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11685       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11686   SourceMgr.setExternalSLocEntrySource(this);
11687 
11688   for (const auto &Ext : Extensions) {
11689     auto BlockName = Ext->getExtensionMetadata().BlockName;
11690     auto Known = ModuleFileExtensions.find(BlockName);
11691     if (Known != ModuleFileExtensions.end()) {
11692       Diags.Report(diag::warn_duplicate_module_file_extension)
11693         << BlockName;
11694       continue;
11695     }
11696 
11697     ModuleFileExtensions.insert({BlockName, Ext});
11698   }
11699 }
11700 
11701 ASTReader::~ASTReader() {
11702   if (OwnsDeserializationListener)
11703     delete DeserializationListener;
11704 }
11705 
11706 IdentifierResolver &ASTReader::getIdResolver() {
11707   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11708 }
11709 
11710 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11711                                                unsigned AbbrevID) {
11712   Idx = 0;
11713   Record.clear();
11714   return Cursor.readRecord(AbbrevID, Record);
11715 }
11716 //===----------------------------------------------------------------------===//
11717 //// OMPClauseReader implementation
11718 ////===----------------------------------------------------------------------===//
11719 
11720 // This has to be in namespace clang because it's friended by all
11721 // of the OMP clauses.
11722 namespace clang {
11723 
11724 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11725   ASTRecordReader &Record;
11726   ASTContext &Context;
11727 
11728 public:
11729   OMPClauseReader(ASTRecordReader &Record)
11730       : Record(Record), Context(Record.getContext()) {}
11731 
11732 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11733 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11734   OMPClause *readClause();
11735   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11736   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11737 };
11738 
11739 } // end namespace clang
11740 
11741 OMPClause *ASTRecordReader::readOMPClause() {
11742   return OMPClauseReader(*this).readClause();
11743 }
11744 
11745 OMPClause *OMPClauseReader::readClause() {
11746   OMPClause *C = nullptr;
11747   switch (llvm::omp::Clause(Record.readInt())) {
11748   case llvm::omp::OMPC_if:
11749     C = new (Context) OMPIfClause();
11750     break;
11751   case llvm::omp::OMPC_final:
11752     C = new (Context) OMPFinalClause();
11753     break;
11754   case llvm::omp::OMPC_num_threads:
11755     C = new (Context) OMPNumThreadsClause();
11756     break;
11757   case llvm::omp::OMPC_safelen:
11758     C = new (Context) OMPSafelenClause();
11759     break;
11760   case llvm::omp::OMPC_simdlen:
11761     C = new (Context) OMPSimdlenClause();
11762     break;
11763   case llvm::omp::OMPC_allocator:
11764     C = new (Context) OMPAllocatorClause();
11765     break;
11766   case llvm::omp::OMPC_collapse:
11767     C = new (Context) OMPCollapseClause();
11768     break;
11769   case llvm::omp::OMPC_default:
11770     C = new (Context) OMPDefaultClause();
11771     break;
11772   case llvm::omp::OMPC_proc_bind:
11773     C = new (Context) OMPProcBindClause();
11774     break;
11775   case llvm::omp::OMPC_schedule:
11776     C = new (Context) OMPScheduleClause();
11777     break;
11778   case llvm::omp::OMPC_ordered:
11779     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11780     break;
11781   case llvm::omp::OMPC_nowait:
11782     C = new (Context) OMPNowaitClause();
11783     break;
11784   case llvm::omp::OMPC_untied:
11785     C = new (Context) OMPUntiedClause();
11786     break;
11787   case llvm::omp::OMPC_mergeable:
11788     C = new (Context) OMPMergeableClause();
11789     break;
11790   case llvm::omp::OMPC_read:
11791     C = new (Context) OMPReadClause();
11792     break;
11793   case llvm::omp::OMPC_write:
11794     C = new (Context) OMPWriteClause();
11795     break;
11796   case llvm::omp::OMPC_update:
11797     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11798     break;
11799   case llvm::omp::OMPC_capture:
11800     C = new (Context) OMPCaptureClause();
11801     break;
11802   case llvm::omp::OMPC_seq_cst:
11803     C = new (Context) OMPSeqCstClause();
11804     break;
11805   case llvm::omp::OMPC_acq_rel:
11806     C = new (Context) OMPAcqRelClause();
11807     break;
11808   case llvm::omp::OMPC_acquire:
11809     C = new (Context) OMPAcquireClause();
11810     break;
11811   case llvm::omp::OMPC_release:
11812     C = new (Context) OMPReleaseClause();
11813     break;
11814   case llvm::omp::OMPC_relaxed:
11815     C = new (Context) OMPRelaxedClause();
11816     break;
11817   case llvm::omp::OMPC_threads:
11818     C = new (Context) OMPThreadsClause();
11819     break;
11820   case llvm::omp::OMPC_simd:
11821     C = new (Context) OMPSIMDClause();
11822     break;
11823   case llvm::omp::OMPC_nogroup:
11824     C = new (Context) OMPNogroupClause();
11825     break;
11826   case llvm::omp::OMPC_unified_address:
11827     C = new (Context) OMPUnifiedAddressClause();
11828     break;
11829   case llvm::omp::OMPC_unified_shared_memory:
11830     C = new (Context) OMPUnifiedSharedMemoryClause();
11831     break;
11832   case llvm::omp::OMPC_reverse_offload:
11833     C = new (Context) OMPReverseOffloadClause();
11834     break;
11835   case llvm::omp::OMPC_dynamic_allocators:
11836     C = new (Context) OMPDynamicAllocatorsClause();
11837     break;
11838   case llvm::omp::OMPC_atomic_default_mem_order:
11839     C = new (Context) OMPAtomicDefaultMemOrderClause();
11840     break;
11841  case llvm::omp::OMPC_private:
11842     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11843     break;
11844   case llvm::omp::OMPC_firstprivate:
11845     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11846     break;
11847   case llvm::omp::OMPC_lastprivate:
11848     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11849     break;
11850   case llvm::omp::OMPC_shared:
11851     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11852     break;
11853   case llvm::omp::OMPC_reduction: {
11854     unsigned N = Record.readInt();
11855     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11856     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11857     break;
11858   }
11859   case llvm::omp::OMPC_task_reduction:
11860     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11861     break;
11862   case llvm::omp::OMPC_in_reduction:
11863     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11864     break;
11865   case llvm::omp::OMPC_linear:
11866     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11867     break;
11868   case llvm::omp::OMPC_aligned:
11869     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11870     break;
11871   case llvm::omp::OMPC_copyin:
11872     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11873     break;
11874   case llvm::omp::OMPC_copyprivate:
11875     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11876     break;
11877   case llvm::omp::OMPC_flush:
11878     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11879     break;
11880   case llvm::omp::OMPC_depobj:
11881     C = OMPDepobjClause::CreateEmpty(Context);
11882     break;
11883   case llvm::omp::OMPC_depend: {
11884     unsigned NumVars = Record.readInt();
11885     unsigned NumLoops = Record.readInt();
11886     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11887     break;
11888   }
11889   case llvm::omp::OMPC_device:
11890     C = new (Context) OMPDeviceClause();
11891     break;
11892   case llvm::omp::OMPC_map: {
11893     OMPMappableExprListSizeTy Sizes;
11894     Sizes.NumVars = Record.readInt();
11895     Sizes.NumUniqueDeclarations = Record.readInt();
11896     Sizes.NumComponentLists = Record.readInt();
11897     Sizes.NumComponents = Record.readInt();
11898     C = OMPMapClause::CreateEmpty(Context, Sizes);
11899     break;
11900   }
11901   case llvm::omp::OMPC_num_teams:
11902     C = new (Context) OMPNumTeamsClause();
11903     break;
11904   case llvm::omp::OMPC_thread_limit:
11905     C = new (Context) OMPThreadLimitClause();
11906     break;
11907   case llvm::omp::OMPC_priority:
11908     C = new (Context) OMPPriorityClause();
11909     break;
11910   case llvm::omp::OMPC_grainsize:
11911     C = new (Context) OMPGrainsizeClause();
11912     break;
11913   case llvm::omp::OMPC_num_tasks:
11914     C = new (Context) OMPNumTasksClause();
11915     break;
11916   case llvm::omp::OMPC_hint:
11917     C = new (Context) OMPHintClause();
11918     break;
11919   case llvm::omp::OMPC_dist_schedule:
11920     C = new (Context) OMPDistScheduleClause();
11921     break;
11922   case llvm::omp::OMPC_defaultmap:
11923     C = new (Context) OMPDefaultmapClause();
11924     break;
11925   case llvm::omp::OMPC_to: {
11926     OMPMappableExprListSizeTy Sizes;
11927     Sizes.NumVars = Record.readInt();
11928     Sizes.NumUniqueDeclarations = Record.readInt();
11929     Sizes.NumComponentLists = Record.readInt();
11930     Sizes.NumComponents = Record.readInt();
11931     C = OMPToClause::CreateEmpty(Context, Sizes);
11932     break;
11933   }
11934   case llvm::omp::OMPC_from: {
11935     OMPMappableExprListSizeTy Sizes;
11936     Sizes.NumVars = Record.readInt();
11937     Sizes.NumUniqueDeclarations = Record.readInt();
11938     Sizes.NumComponentLists = Record.readInt();
11939     Sizes.NumComponents = Record.readInt();
11940     C = OMPFromClause::CreateEmpty(Context, Sizes);
11941     break;
11942   }
11943   case llvm::omp::OMPC_use_device_ptr: {
11944     OMPMappableExprListSizeTy Sizes;
11945     Sizes.NumVars = Record.readInt();
11946     Sizes.NumUniqueDeclarations = Record.readInt();
11947     Sizes.NumComponentLists = Record.readInt();
11948     Sizes.NumComponents = Record.readInt();
11949     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11950     break;
11951   }
11952   case llvm::omp::OMPC_use_device_addr: {
11953     OMPMappableExprListSizeTy Sizes;
11954     Sizes.NumVars = Record.readInt();
11955     Sizes.NumUniqueDeclarations = Record.readInt();
11956     Sizes.NumComponentLists = Record.readInt();
11957     Sizes.NumComponents = Record.readInt();
11958     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11959     break;
11960   }
11961   case llvm::omp::OMPC_is_device_ptr: {
11962     OMPMappableExprListSizeTy Sizes;
11963     Sizes.NumVars = Record.readInt();
11964     Sizes.NumUniqueDeclarations = Record.readInt();
11965     Sizes.NumComponentLists = Record.readInt();
11966     Sizes.NumComponents = Record.readInt();
11967     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11968     break;
11969   }
11970   case llvm::omp::OMPC_allocate:
11971     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11972     break;
11973   case llvm::omp::OMPC_nontemporal:
11974     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11975     break;
11976   case llvm::omp::OMPC_inclusive:
11977     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11978     break;
11979   case llvm::omp::OMPC_exclusive:
11980     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11981     break;
11982   case llvm::omp::OMPC_order:
11983     C = new (Context) OMPOrderClause();
11984     break;
11985   case llvm::omp::OMPC_destroy:
11986     C = new (Context) OMPDestroyClause();
11987     break;
11988   case llvm::omp::OMPC_detach:
11989     C = new (Context) OMPDetachClause();
11990     break;
11991   case llvm::omp::OMPC_uses_allocators:
11992     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11993     break;
11994   case llvm::omp::OMPC_affinity:
11995     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11996     break;
11997 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11998   case llvm::omp::Enum:                                                        \
11999     break;
12000 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12001   default:
12002     break;
12003   }
12004   assert(C && "Unknown OMPClause type");
12005 
12006   Visit(C);
12007   C->setLocStart(Record.readSourceLocation());
12008   C->setLocEnd(Record.readSourceLocation());
12009 
12010   return C;
12011 }
12012 
12013 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12014   C->setPreInitStmt(Record.readSubStmt(),
12015                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12016 }
12017 
12018 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12019   VisitOMPClauseWithPreInit(C);
12020   C->setPostUpdateExpr(Record.readSubExpr());
12021 }
12022 
12023 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12024   VisitOMPClauseWithPreInit(C);
12025   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12026   C->setNameModifierLoc(Record.readSourceLocation());
12027   C->setColonLoc(Record.readSourceLocation());
12028   C->setCondition(Record.readSubExpr());
12029   C->setLParenLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12033   VisitOMPClauseWithPreInit(C);
12034   C->setCondition(Record.readSubExpr());
12035   C->setLParenLoc(Record.readSourceLocation());
12036 }
12037 
12038 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12039   VisitOMPClauseWithPreInit(C);
12040   C->setNumThreads(Record.readSubExpr());
12041   C->setLParenLoc(Record.readSourceLocation());
12042 }
12043 
12044 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12045   C->setSafelen(Record.readSubExpr());
12046   C->setLParenLoc(Record.readSourceLocation());
12047 }
12048 
12049 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12050   C->setSimdlen(Record.readSubExpr());
12051   C->setLParenLoc(Record.readSourceLocation());
12052 }
12053 
12054 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12055   C->setAllocator(Record.readExpr());
12056   C->setLParenLoc(Record.readSourceLocation());
12057 }
12058 
12059 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12060   C->setNumForLoops(Record.readSubExpr());
12061   C->setLParenLoc(Record.readSourceLocation());
12062 }
12063 
12064 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12065   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12066   C->setLParenLoc(Record.readSourceLocation());
12067   C->setDefaultKindKwLoc(Record.readSourceLocation());
12068 }
12069 
12070 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12071   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12072   C->setLParenLoc(Record.readSourceLocation());
12073   C->setProcBindKindKwLoc(Record.readSourceLocation());
12074 }
12075 
12076 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12077   VisitOMPClauseWithPreInit(C);
12078   C->setScheduleKind(
12079        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12080   C->setFirstScheduleModifier(
12081       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12082   C->setSecondScheduleModifier(
12083       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12084   C->setChunkSize(Record.readSubExpr());
12085   C->setLParenLoc(Record.readSourceLocation());
12086   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12087   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12088   C->setScheduleKindLoc(Record.readSourceLocation());
12089   C->setCommaLoc(Record.readSourceLocation());
12090 }
12091 
12092 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12093   C->setNumForLoops(Record.readSubExpr());
12094   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12095     C->setLoopNumIterations(I, Record.readSubExpr());
12096   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12097     C->setLoopCounter(I, Record.readSubExpr());
12098   C->setLParenLoc(Record.readSourceLocation());
12099 }
12100 
12101 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12102   C->setEventHandler(Record.readSubExpr());
12103   C->setLParenLoc(Record.readSourceLocation());
12104 }
12105 
12106 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12107 
12108 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12109 
12110 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12113 
12114 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12115 
12116 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12117   if (C->isExtended()) {
12118     C->setLParenLoc(Record.readSourceLocation());
12119     C->setArgumentLoc(Record.readSourceLocation());
12120     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12121   }
12122 }
12123 
12124 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12125 
12126 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12127 
12128 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12129 
12130 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12131 
12132 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12133 
12134 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12135 
12136 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12137 
12138 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12139 
12140 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12141 
12142 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12143 
12144 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12145 
12146 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12147     OMPUnifiedSharedMemoryClause *) {}
12148 
12149 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12150 
12151 void
12152 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12153 }
12154 
12155 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12156     OMPAtomicDefaultMemOrderClause *C) {
12157   C->setAtomicDefaultMemOrderKind(
12158       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12159   C->setLParenLoc(Record.readSourceLocation());
12160   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12161 }
12162 
12163 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12164   C->setLParenLoc(Record.readSourceLocation());
12165   unsigned NumVars = C->varlist_size();
12166   SmallVector<Expr *, 16> Vars;
12167   Vars.reserve(NumVars);
12168   for (unsigned i = 0; i != NumVars; ++i)
12169     Vars.push_back(Record.readSubExpr());
12170   C->setVarRefs(Vars);
12171   Vars.clear();
12172   for (unsigned i = 0; i != NumVars; ++i)
12173     Vars.push_back(Record.readSubExpr());
12174   C->setPrivateCopies(Vars);
12175 }
12176 
12177 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12178   VisitOMPClauseWithPreInit(C);
12179   C->setLParenLoc(Record.readSourceLocation());
12180   unsigned NumVars = C->varlist_size();
12181   SmallVector<Expr *, 16> Vars;
12182   Vars.reserve(NumVars);
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setVarRefs(Vars);
12186   Vars.clear();
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setPrivateCopies(Vars);
12190   Vars.clear();
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setInits(Vars);
12194 }
12195 
12196 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12197   VisitOMPClauseWithPostUpdate(C);
12198   C->setLParenLoc(Record.readSourceLocation());
12199   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12200   C->setKindLoc(Record.readSourceLocation());
12201   C->setColonLoc(Record.readSourceLocation());
12202   unsigned NumVars = C->varlist_size();
12203   SmallVector<Expr *, 16> Vars;
12204   Vars.reserve(NumVars);
12205   for (unsigned i = 0; i != NumVars; ++i)
12206     Vars.push_back(Record.readSubExpr());
12207   C->setVarRefs(Vars);
12208   Vars.clear();
12209   for (unsigned i = 0; i != NumVars; ++i)
12210     Vars.push_back(Record.readSubExpr());
12211   C->setPrivateCopies(Vars);
12212   Vars.clear();
12213   for (unsigned i = 0; i != NumVars; ++i)
12214     Vars.push_back(Record.readSubExpr());
12215   C->setSourceExprs(Vars);
12216   Vars.clear();
12217   for (unsigned i = 0; i != NumVars; ++i)
12218     Vars.push_back(Record.readSubExpr());
12219   C->setDestinationExprs(Vars);
12220   Vars.clear();
12221   for (unsigned i = 0; i != NumVars; ++i)
12222     Vars.push_back(Record.readSubExpr());
12223   C->setAssignmentOps(Vars);
12224 }
12225 
12226 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12227   C->setLParenLoc(Record.readSourceLocation());
12228   unsigned NumVars = C->varlist_size();
12229   SmallVector<Expr *, 16> Vars;
12230   Vars.reserve(NumVars);
12231   for (unsigned i = 0; i != NumVars; ++i)
12232     Vars.push_back(Record.readSubExpr());
12233   C->setVarRefs(Vars);
12234 }
12235 
12236 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12237   VisitOMPClauseWithPostUpdate(C);
12238   C->setLParenLoc(Record.readSourceLocation());
12239   C->setModifierLoc(Record.readSourceLocation());
12240   C->setColonLoc(Record.readSourceLocation());
12241   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12242   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12243   C->setQualifierLoc(NNSL);
12244   C->setNameInfo(DNI);
12245 
12246   unsigned NumVars = C->varlist_size();
12247   SmallVector<Expr *, 16> Vars;
12248   Vars.reserve(NumVars);
12249   for (unsigned i = 0; i != NumVars; ++i)
12250     Vars.push_back(Record.readSubExpr());
12251   C->setVarRefs(Vars);
12252   Vars.clear();
12253   for (unsigned i = 0; i != NumVars; ++i)
12254     Vars.push_back(Record.readSubExpr());
12255   C->setPrivates(Vars);
12256   Vars.clear();
12257   for (unsigned i = 0; i != NumVars; ++i)
12258     Vars.push_back(Record.readSubExpr());
12259   C->setLHSExprs(Vars);
12260   Vars.clear();
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setRHSExprs(Vars);
12264   Vars.clear();
12265   for (unsigned i = 0; i != NumVars; ++i)
12266     Vars.push_back(Record.readSubExpr());
12267   C->setReductionOps(Vars);
12268   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12269     Vars.clear();
12270     for (unsigned i = 0; i != NumVars; ++i)
12271       Vars.push_back(Record.readSubExpr());
12272     C->setInscanCopyOps(Vars);
12273     Vars.clear();
12274     for (unsigned i = 0; i != NumVars; ++i)
12275       Vars.push_back(Record.readSubExpr());
12276     C->setInscanCopyArrayTemps(Vars);
12277     Vars.clear();
12278     for (unsigned i = 0; i != NumVars; ++i)
12279       Vars.push_back(Record.readSubExpr());
12280     C->setInscanCopyArrayElems(Vars);
12281   }
12282 }
12283 
12284 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12285   VisitOMPClauseWithPostUpdate(C);
12286   C->setLParenLoc(Record.readSourceLocation());
12287   C->setColonLoc(Record.readSourceLocation());
12288   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12289   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12290   C->setQualifierLoc(NNSL);
12291   C->setNameInfo(DNI);
12292 
12293   unsigned NumVars = C->varlist_size();
12294   SmallVector<Expr *, 16> Vars;
12295   Vars.reserve(NumVars);
12296   for (unsigned I = 0; I != NumVars; ++I)
12297     Vars.push_back(Record.readSubExpr());
12298   C->setVarRefs(Vars);
12299   Vars.clear();
12300   for (unsigned I = 0; I != NumVars; ++I)
12301     Vars.push_back(Record.readSubExpr());
12302   C->setPrivates(Vars);
12303   Vars.clear();
12304   for (unsigned I = 0; I != NumVars; ++I)
12305     Vars.push_back(Record.readSubExpr());
12306   C->setLHSExprs(Vars);
12307   Vars.clear();
12308   for (unsigned I = 0; I != NumVars; ++I)
12309     Vars.push_back(Record.readSubExpr());
12310   C->setRHSExprs(Vars);
12311   Vars.clear();
12312   for (unsigned I = 0; I != NumVars; ++I)
12313     Vars.push_back(Record.readSubExpr());
12314   C->setReductionOps(Vars);
12315 }
12316 
12317 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12318   VisitOMPClauseWithPostUpdate(C);
12319   C->setLParenLoc(Record.readSourceLocation());
12320   C->setColonLoc(Record.readSourceLocation());
12321   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12322   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12323   C->setQualifierLoc(NNSL);
12324   C->setNameInfo(DNI);
12325 
12326   unsigned NumVars = C->varlist_size();
12327   SmallVector<Expr *, 16> Vars;
12328   Vars.reserve(NumVars);
12329   for (unsigned I = 0; I != NumVars; ++I)
12330     Vars.push_back(Record.readSubExpr());
12331   C->setVarRefs(Vars);
12332   Vars.clear();
12333   for (unsigned I = 0; I != NumVars; ++I)
12334     Vars.push_back(Record.readSubExpr());
12335   C->setPrivates(Vars);
12336   Vars.clear();
12337   for (unsigned I = 0; I != NumVars; ++I)
12338     Vars.push_back(Record.readSubExpr());
12339   C->setLHSExprs(Vars);
12340   Vars.clear();
12341   for (unsigned I = 0; I != NumVars; ++I)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setRHSExprs(Vars);
12344   Vars.clear();
12345   for (unsigned I = 0; I != NumVars; ++I)
12346     Vars.push_back(Record.readSubExpr());
12347   C->setReductionOps(Vars);
12348   Vars.clear();
12349   for (unsigned I = 0; I != NumVars; ++I)
12350     Vars.push_back(Record.readSubExpr());
12351   C->setTaskgroupDescriptors(Vars);
12352 }
12353 
12354 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12355   VisitOMPClauseWithPostUpdate(C);
12356   C->setLParenLoc(Record.readSourceLocation());
12357   C->setColonLoc(Record.readSourceLocation());
12358   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12359   C->setModifierLoc(Record.readSourceLocation());
12360   unsigned NumVars = C->varlist_size();
12361   SmallVector<Expr *, 16> Vars;
12362   Vars.reserve(NumVars);
12363   for (unsigned i = 0; i != NumVars; ++i)
12364     Vars.push_back(Record.readSubExpr());
12365   C->setVarRefs(Vars);
12366   Vars.clear();
12367   for (unsigned i = 0; i != NumVars; ++i)
12368     Vars.push_back(Record.readSubExpr());
12369   C->setPrivates(Vars);
12370   Vars.clear();
12371   for (unsigned i = 0; i != NumVars; ++i)
12372     Vars.push_back(Record.readSubExpr());
12373   C->setInits(Vars);
12374   Vars.clear();
12375   for (unsigned i = 0; i != NumVars; ++i)
12376     Vars.push_back(Record.readSubExpr());
12377   C->setUpdates(Vars);
12378   Vars.clear();
12379   for (unsigned i = 0; i != NumVars; ++i)
12380     Vars.push_back(Record.readSubExpr());
12381   C->setFinals(Vars);
12382   C->setStep(Record.readSubExpr());
12383   C->setCalcStep(Record.readSubExpr());
12384   Vars.clear();
12385   for (unsigned I = 0; I != NumVars + 1; ++I)
12386     Vars.push_back(Record.readSubExpr());
12387   C->setUsedExprs(Vars);
12388 }
12389 
12390 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12391   C->setLParenLoc(Record.readSourceLocation());
12392   C->setColonLoc(Record.readSourceLocation());
12393   unsigned NumVars = C->varlist_size();
12394   SmallVector<Expr *, 16> Vars;
12395   Vars.reserve(NumVars);
12396   for (unsigned i = 0; i != NumVars; ++i)
12397     Vars.push_back(Record.readSubExpr());
12398   C->setVarRefs(Vars);
12399   C->setAlignment(Record.readSubExpr());
12400 }
12401 
12402 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12403   C->setLParenLoc(Record.readSourceLocation());
12404   unsigned NumVars = C->varlist_size();
12405   SmallVector<Expr *, 16> Exprs;
12406   Exprs.reserve(NumVars);
12407   for (unsigned i = 0; i != NumVars; ++i)
12408     Exprs.push_back(Record.readSubExpr());
12409   C->setVarRefs(Exprs);
12410   Exprs.clear();
12411   for (unsigned i = 0; i != NumVars; ++i)
12412     Exprs.push_back(Record.readSubExpr());
12413   C->setSourceExprs(Exprs);
12414   Exprs.clear();
12415   for (unsigned i = 0; i != NumVars; ++i)
12416     Exprs.push_back(Record.readSubExpr());
12417   C->setDestinationExprs(Exprs);
12418   Exprs.clear();
12419   for (unsigned i = 0; i != NumVars; ++i)
12420     Exprs.push_back(Record.readSubExpr());
12421   C->setAssignmentOps(Exprs);
12422 }
12423 
12424 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12425   C->setLParenLoc(Record.readSourceLocation());
12426   unsigned NumVars = C->varlist_size();
12427   SmallVector<Expr *, 16> Exprs;
12428   Exprs.reserve(NumVars);
12429   for (unsigned i = 0; i != NumVars; ++i)
12430     Exprs.push_back(Record.readSubExpr());
12431   C->setVarRefs(Exprs);
12432   Exprs.clear();
12433   for (unsigned i = 0; i != NumVars; ++i)
12434     Exprs.push_back(Record.readSubExpr());
12435   C->setSourceExprs(Exprs);
12436   Exprs.clear();
12437   for (unsigned i = 0; i != NumVars; ++i)
12438     Exprs.push_back(Record.readSubExpr());
12439   C->setDestinationExprs(Exprs);
12440   Exprs.clear();
12441   for (unsigned i = 0; i != NumVars; ++i)
12442     Exprs.push_back(Record.readSubExpr());
12443   C->setAssignmentOps(Exprs);
12444 }
12445 
12446 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12447   C->setLParenLoc(Record.readSourceLocation());
12448   unsigned NumVars = C->varlist_size();
12449   SmallVector<Expr *, 16> Vars;
12450   Vars.reserve(NumVars);
12451   for (unsigned i = 0; i != NumVars; ++i)
12452     Vars.push_back(Record.readSubExpr());
12453   C->setVarRefs(Vars);
12454 }
12455 
12456 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12457   C->setDepobj(Record.readSubExpr());
12458   C->setLParenLoc(Record.readSourceLocation());
12459 }
12460 
12461 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12462   C->setLParenLoc(Record.readSourceLocation());
12463   C->setModifier(Record.readSubExpr());
12464   C->setDependencyKind(
12465       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12466   C->setDependencyLoc(Record.readSourceLocation());
12467   C->setColonLoc(Record.readSourceLocation());
12468   unsigned NumVars = C->varlist_size();
12469   SmallVector<Expr *, 16> Vars;
12470   Vars.reserve(NumVars);
12471   for (unsigned I = 0; I != NumVars; ++I)
12472     Vars.push_back(Record.readSubExpr());
12473   C->setVarRefs(Vars);
12474   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12475     C->setLoopData(I, Record.readSubExpr());
12476 }
12477 
12478 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12479   VisitOMPClauseWithPreInit(C);
12480   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12481   C->setDevice(Record.readSubExpr());
12482   C->setModifierLoc(Record.readSourceLocation());
12483   C->setLParenLoc(Record.readSourceLocation());
12484 }
12485 
12486 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12487   C->setLParenLoc(Record.readSourceLocation());
12488   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12489     C->setMapTypeModifier(
12490         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12491     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12492   }
12493   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12494   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12495   C->setMapType(
12496      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12497   C->setMapLoc(Record.readSourceLocation());
12498   C->setColonLoc(Record.readSourceLocation());
12499   auto NumVars = C->varlist_size();
12500   auto UniqueDecls = C->getUniqueDeclarationsNum();
12501   auto TotalLists = C->getTotalComponentListNum();
12502   auto TotalComponents = C->getTotalComponentsNum();
12503 
12504   SmallVector<Expr *, 16> Vars;
12505   Vars.reserve(NumVars);
12506   for (unsigned i = 0; i != NumVars; ++i)
12507     Vars.push_back(Record.readExpr());
12508   C->setVarRefs(Vars);
12509 
12510   SmallVector<Expr *, 16> UDMappers;
12511   UDMappers.reserve(NumVars);
12512   for (unsigned I = 0; I < NumVars; ++I)
12513     UDMappers.push_back(Record.readExpr());
12514   C->setUDMapperRefs(UDMappers);
12515 
12516   SmallVector<ValueDecl *, 16> Decls;
12517   Decls.reserve(UniqueDecls);
12518   for (unsigned i = 0; i < UniqueDecls; ++i)
12519     Decls.push_back(Record.readDeclAs<ValueDecl>());
12520   C->setUniqueDecls(Decls);
12521 
12522   SmallVector<unsigned, 16> ListsPerDecl;
12523   ListsPerDecl.reserve(UniqueDecls);
12524   for (unsigned i = 0; i < UniqueDecls; ++i)
12525     ListsPerDecl.push_back(Record.readInt());
12526   C->setDeclNumLists(ListsPerDecl);
12527 
12528   SmallVector<unsigned, 32> ListSizes;
12529   ListSizes.reserve(TotalLists);
12530   for (unsigned i = 0; i < TotalLists; ++i)
12531     ListSizes.push_back(Record.readInt());
12532   C->setComponentListSizes(ListSizes);
12533 
12534   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12535   Components.reserve(TotalComponents);
12536   for (unsigned i = 0; i < TotalComponents; ++i) {
12537     Expr *AssociatedExpr = Record.readExpr();
12538     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12539     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12540         AssociatedExpr, AssociatedDecl));
12541   }
12542   C->setComponents(Components, ListSizes);
12543 }
12544 
12545 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12546   C->setLParenLoc(Record.readSourceLocation());
12547   C->setColonLoc(Record.readSourceLocation());
12548   C->setAllocator(Record.readSubExpr());
12549   unsigned NumVars = C->varlist_size();
12550   SmallVector<Expr *, 16> Vars;
12551   Vars.reserve(NumVars);
12552   for (unsigned i = 0; i != NumVars; ++i)
12553     Vars.push_back(Record.readSubExpr());
12554   C->setVarRefs(Vars);
12555 }
12556 
12557 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12558   VisitOMPClauseWithPreInit(C);
12559   C->setNumTeams(Record.readSubExpr());
12560   C->setLParenLoc(Record.readSourceLocation());
12561 }
12562 
12563 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12564   VisitOMPClauseWithPreInit(C);
12565   C->setThreadLimit(Record.readSubExpr());
12566   C->setLParenLoc(Record.readSourceLocation());
12567 }
12568 
12569 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12570   VisitOMPClauseWithPreInit(C);
12571   C->setPriority(Record.readSubExpr());
12572   C->setLParenLoc(Record.readSourceLocation());
12573 }
12574 
12575 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12576   VisitOMPClauseWithPreInit(C);
12577   C->setGrainsize(Record.readSubExpr());
12578   C->setLParenLoc(Record.readSourceLocation());
12579 }
12580 
12581 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12582   VisitOMPClauseWithPreInit(C);
12583   C->setNumTasks(Record.readSubExpr());
12584   C->setLParenLoc(Record.readSourceLocation());
12585 }
12586 
12587 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12588   C->setHint(Record.readSubExpr());
12589   C->setLParenLoc(Record.readSourceLocation());
12590 }
12591 
12592 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12593   VisitOMPClauseWithPreInit(C);
12594   C->setDistScheduleKind(
12595       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12596   C->setChunkSize(Record.readSubExpr());
12597   C->setLParenLoc(Record.readSourceLocation());
12598   C->setDistScheduleKindLoc(Record.readSourceLocation());
12599   C->setCommaLoc(Record.readSourceLocation());
12600 }
12601 
12602 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12603   C->setDefaultmapKind(
12604        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12605   C->setDefaultmapModifier(
12606       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12607   C->setLParenLoc(Record.readSourceLocation());
12608   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12609   C->setDefaultmapKindLoc(Record.readSourceLocation());
12610 }
12611 
12612 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12613   C->setLParenLoc(Record.readSourceLocation());
12614   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12615     C->setMotionModifier(
12616         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12617     C->setMotionModifierLoc(I, Record.readSourceLocation());
12618   }
12619   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12620   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12621   C->setColonLoc(Record.readSourceLocation());
12622   auto NumVars = C->varlist_size();
12623   auto UniqueDecls = C->getUniqueDeclarationsNum();
12624   auto TotalLists = C->getTotalComponentListNum();
12625   auto TotalComponents = C->getTotalComponentsNum();
12626 
12627   SmallVector<Expr *, 16> Vars;
12628   Vars.reserve(NumVars);
12629   for (unsigned i = 0; i != NumVars; ++i)
12630     Vars.push_back(Record.readSubExpr());
12631   C->setVarRefs(Vars);
12632 
12633   SmallVector<Expr *, 16> UDMappers;
12634   UDMappers.reserve(NumVars);
12635   for (unsigned I = 0; I < NumVars; ++I)
12636     UDMappers.push_back(Record.readSubExpr());
12637   C->setUDMapperRefs(UDMappers);
12638 
12639   SmallVector<ValueDecl *, 16> Decls;
12640   Decls.reserve(UniqueDecls);
12641   for (unsigned i = 0; i < UniqueDecls; ++i)
12642     Decls.push_back(Record.readDeclAs<ValueDecl>());
12643   C->setUniqueDecls(Decls);
12644 
12645   SmallVector<unsigned, 16> ListsPerDecl;
12646   ListsPerDecl.reserve(UniqueDecls);
12647   for (unsigned i = 0; i < UniqueDecls; ++i)
12648     ListsPerDecl.push_back(Record.readInt());
12649   C->setDeclNumLists(ListsPerDecl);
12650 
12651   SmallVector<unsigned, 32> ListSizes;
12652   ListSizes.reserve(TotalLists);
12653   for (unsigned i = 0; i < TotalLists; ++i)
12654     ListSizes.push_back(Record.readInt());
12655   C->setComponentListSizes(ListSizes);
12656 
12657   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12658   Components.reserve(TotalComponents);
12659   for (unsigned i = 0; i < TotalComponents; ++i) {
12660     Expr *AssociatedExpr = Record.readSubExpr();
12661     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12662     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12663         AssociatedExpr, AssociatedDecl));
12664   }
12665   C->setComponents(Components, ListSizes);
12666 }
12667 
12668 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12669   C->setLParenLoc(Record.readSourceLocation());
12670   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12671     C->setMotionModifier(
12672         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12673     C->setMotionModifierLoc(I, Record.readSourceLocation());
12674   }
12675   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12676   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12677   C->setColonLoc(Record.readSourceLocation());
12678   auto NumVars = C->varlist_size();
12679   auto UniqueDecls = C->getUniqueDeclarationsNum();
12680   auto TotalLists = C->getTotalComponentListNum();
12681   auto TotalComponents = C->getTotalComponentsNum();
12682 
12683   SmallVector<Expr *, 16> Vars;
12684   Vars.reserve(NumVars);
12685   for (unsigned i = 0; i != NumVars; ++i)
12686     Vars.push_back(Record.readSubExpr());
12687   C->setVarRefs(Vars);
12688 
12689   SmallVector<Expr *, 16> UDMappers;
12690   UDMappers.reserve(NumVars);
12691   for (unsigned I = 0; I < NumVars; ++I)
12692     UDMappers.push_back(Record.readSubExpr());
12693   C->setUDMapperRefs(UDMappers);
12694 
12695   SmallVector<ValueDecl *, 16> Decls;
12696   Decls.reserve(UniqueDecls);
12697   for (unsigned i = 0; i < UniqueDecls; ++i)
12698     Decls.push_back(Record.readDeclAs<ValueDecl>());
12699   C->setUniqueDecls(Decls);
12700 
12701   SmallVector<unsigned, 16> ListsPerDecl;
12702   ListsPerDecl.reserve(UniqueDecls);
12703   for (unsigned i = 0; i < UniqueDecls; ++i)
12704     ListsPerDecl.push_back(Record.readInt());
12705   C->setDeclNumLists(ListsPerDecl);
12706 
12707   SmallVector<unsigned, 32> ListSizes;
12708   ListSizes.reserve(TotalLists);
12709   for (unsigned i = 0; i < TotalLists; ++i)
12710     ListSizes.push_back(Record.readInt());
12711   C->setComponentListSizes(ListSizes);
12712 
12713   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12714   Components.reserve(TotalComponents);
12715   for (unsigned i = 0; i < TotalComponents; ++i) {
12716     Expr *AssociatedExpr = Record.readSubExpr();
12717     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12718     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12719         AssociatedExpr, AssociatedDecl));
12720   }
12721   C->setComponents(Components, ListSizes);
12722 }
12723 
12724 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12725   C->setLParenLoc(Record.readSourceLocation());
12726   auto NumVars = C->varlist_size();
12727   auto UniqueDecls = C->getUniqueDeclarationsNum();
12728   auto TotalLists = C->getTotalComponentListNum();
12729   auto TotalComponents = C->getTotalComponentsNum();
12730 
12731   SmallVector<Expr *, 16> Vars;
12732   Vars.reserve(NumVars);
12733   for (unsigned i = 0; i != NumVars; ++i)
12734     Vars.push_back(Record.readSubExpr());
12735   C->setVarRefs(Vars);
12736   Vars.clear();
12737   for (unsigned i = 0; i != NumVars; ++i)
12738     Vars.push_back(Record.readSubExpr());
12739   C->setPrivateCopies(Vars);
12740   Vars.clear();
12741   for (unsigned i = 0; i != NumVars; ++i)
12742     Vars.push_back(Record.readSubExpr());
12743   C->setInits(Vars);
12744 
12745   SmallVector<ValueDecl *, 16> Decls;
12746   Decls.reserve(UniqueDecls);
12747   for (unsigned i = 0; i < UniqueDecls; ++i)
12748     Decls.push_back(Record.readDeclAs<ValueDecl>());
12749   C->setUniqueDecls(Decls);
12750 
12751   SmallVector<unsigned, 16> ListsPerDecl;
12752   ListsPerDecl.reserve(UniqueDecls);
12753   for (unsigned i = 0; i < UniqueDecls; ++i)
12754     ListsPerDecl.push_back(Record.readInt());
12755   C->setDeclNumLists(ListsPerDecl);
12756 
12757   SmallVector<unsigned, 32> ListSizes;
12758   ListSizes.reserve(TotalLists);
12759   for (unsigned i = 0; i < TotalLists; ++i)
12760     ListSizes.push_back(Record.readInt());
12761   C->setComponentListSizes(ListSizes);
12762 
12763   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12764   Components.reserve(TotalComponents);
12765   for (unsigned i = 0; i < TotalComponents; ++i) {
12766     Expr *AssociatedExpr = Record.readSubExpr();
12767     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12768     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12769         AssociatedExpr, AssociatedDecl));
12770   }
12771   C->setComponents(Components, ListSizes);
12772 }
12773 
12774 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12775   C->setLParenLoc(Record.readSourceLocation());
12776   auto NumVars = C->varlist_size();
12777   auto UniqueDecls = C->getUniqueDeclarationsNum();
12778   auto TotalLists = C->getTotalComponentListNum();
12779   auto TotalComponents = C->getTotalComponentsNum();
12780 
12781   SmallVector<Expr *, 16> Vars;
12782   Vars.reserve(NumVars);
12783   for (unsigned i = 0; i != NumVars; ++i)
12784     Vars.push_back(Record.readSubExpr());
12785   C->setVarRefs(Vars);
12786 
12787   SmallVector<ValueDecl *, 16> Decls;
12788   Decls.reserve(UniqueDecls);
12789   for (unsigned i = 0; i < UniqueDecls; ++i)
12790     Decls.push_back(Record.readDeclAs<ValueDecl>());
12791   C->setUniqueDecls(Decls);
12792 
12793   SmallVector<unsigned, 16> ListsPerDecl;
12794   ListsPerDecl.reserve(UniqueDecls);
12795   for (unsigned i = 0; i < UniqueDecls; ++i)
12796     ListsPerDecl.push_back(Record.readInt());
12797   C->setDeclNumLists(ListsPerDecl);
12798 
12799   SmallVector<unsigned, 32> ListSizes;
12800   ListSizes.reserve(TotalLists);
12801   for (unsigned i = 0; i < TotalLists; ++i)
12802     ListSizes.push_back(Record.readInt());
12803   C->setComponentListSizes(ListSizes);
12804 
12805   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12806   Components.reserve(TotalComponents);
12807   for (unsigned i = 0; i < TotalComponents; ++i) {
12808     Expr *AssociatedExpr = Record.readSubExpr();
12809     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12810     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12811         AssociatedExpr, AssociatedDecl));
12812   }
12813   C->setComponents(Components, ListSizes);
12814 }
12815 
12816 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12817   C->setLParenLoc(Record.readSourceLocation());
12818   auto NumVars = C->varlist_size();
12819   auto UniqueDecls = C->getUniqueDeclarationsNum();
12820   auto TotalLists = C->getTotalComponentListNum();
12821   auto TotalComponents = C->getTotalComponentsNum();
12822 
12823   SmallVector<Expr *, 16> Vars;
12824   Vars.reserve(NumVars);
12825   for (unsigned i = 0; i != NumVars; ++i)
12826     Vars.push_back(Record.readSubExpr());
12827   C->setVarRefs(Vars);
12828   Vars.clear();
12829 
12830   SmallVector<ValueDecl *, 16> Decls;
12831   Decls.reserve(UniqueDecls);
12832   for (unsigned i = 0; i < UniqueDecls; ++i)
12833     Decls.push_back(Record.readDeclAs<ValueDecl>());
12834   C->setUniqueDecls(Decls);
12835 
12836   SmallVector<unsigned, 16> ListsPerDecl;
12837   ListsPerDecl.reserve(UniqueDecls);
12838   for (unsigned i = 0; i < UniqueDecls; ++i)
12839     ListsPerDecl.push_back(Record.readInt());
12840   C->setDeclNumLists(ListsPerDecl);
12841 
12842   SmallVector<unsigned, 32> ListSizes;
12843   ListSizes.reserve(TotalLists);
12844   for (unsigned i = 0; i < TotalLists; ++i)
12845     ListSizes.push_back(Record.readInt());
12846   C->setComponentListSizes(ListSizes);
12847 
12848   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12849   Components.reserve(TotalComponents);
12850   for (unsigned i = 0; i < TotalComponents; ++i) {
12851     Expr *AssociatedExpr = Record.readSubExpr();
12852     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12853     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12854         AssociatedExpr, AssociatedDecl));
12855   }
12856   C->setComponents(Components, ListSizes);
12857 }
12858 
12859 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12860   C->setLParenLoc(Record.readSourceLocation());
12861   unsigned NumVars = C->varlist_size();
12862   SmallVector<Expr *, 16> Vars;
12863   Vars.reserve(NumVars);
12864   for (unsigned i = 0; i != NumVars; ++i)
12865     Vars.push_back(Record.readSubExpr());
12866   C->setVarRefs(Vars);
12867   Vars.clear();
12868   Vars.reserve(NumVars);
12869   for (unsigned i = 0; i != NumVars; ++i)
12870     Vars.push_back(Record.readSubExpr());
12871   C->setPrivateRefs(Vars);
12872 }
12873 
12874 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12875   C->setLParenLoc(Record.readSourceLocation());
12876   unsigned NumVars = C->varlist_size();
12877   SmallVector<Expr *, 16> Vars;
12878   Vars.reserve(NumVars);
12879   for (unsigned i = 0; i != NumVars; ++i)
12880     Vars.push_back(Record.readSubExpr());
12881   C->setVarRefs(Vars);
12882 }
12883 
12884 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12885   C->setLParenLoc(Record.readSourceLocation());
12886   unsigned NumVars = C->varlist_size();
12887   SmallVector<Expr *, 16> Vars;
12888   Vars.reserve(NumVars);
12889   for (unsigned i = 0; i != NumVars; ++i)
12890     Vars.push_back(Record.readSubExpr());
12891   C->setVarRefs(Vars);
12892 }
12893 
12894 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12895   C->setLParenLoc(Record.readSourceLocation());
12896   unsigned NumOfAllocators = C->getNumberOfAllocators();
12897   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12898   Data.reserve(NumOfAllocators);
12899   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12900     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12901     D.Allocator = Record.readSubExpr();
12902     D.AllocatorTraits = Record.readSubExpr();
12903     D.LParenLoc = Record.readSourceLocation();
12904     D.RParenLoc = Record.readSourceLocation();
12905   }
12906   C->setAllocatorsData(Data);
12907 }
12908 
12909 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12910   C->setLParenLoc(Record.readSourceLocation());
12911   C->setModifier(Record.readSubExpr());
12912   C->setColonLoc(Record.readSourceLocation());
12913   unsigned NumOfLocators = C->varlist_size();
12914   SmallVector<Expr *, 4> Locators;
12915   Locators.reserve(NumOfLocators);
12916   for (unsigned I = 0; I != NumOfLocators; ++I)
12917     Locators.push_back(Record.readSubExpr());
12918   C->setVarRefs(Locators);
12919 }
12920 
12921 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12922   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12923   C->setLParenLoc(Record.readSourceLocation());
12924   C->setKindKwLoc(Record.readSourceLocation());
12925 }
12926 
12927 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12928   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12929   TI.Sets.resize(readUInt32());
12930   for (auto &Set : TI.Sets) {
12931     Set.Kind = readEnum<llvm::omp::TraitSet>();
12932     Set.Selectors.resize(readUInt32());
12933     for (auto &Selector : Set.Selectors) {
12934       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12935       Selector.ScoreOrCondition = nullptr;
12936       if (readBool())
12937         Selector.ScoreOrCondition = readExprRef();
12938       Selector.Properties.resize(readUInt32());
12939       for (auto &Property : Selector.Properties)
12940         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12941     }
12942   }
12943   return &TI;
12944 }
12945 
12946 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12947   if (!Data)
12948     return;
12949   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12950     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12951     skipInts(3);
12952   }
12953   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12954   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12955     Clauses[I] = readOMPClause();
12956   Data->setClauses(Clauses);
12957   if (Data->hasAssociatedStmt())
12958     Data->setAssociatedStmt(readStmt());
12959   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12960     Data->getChildren()[I] = readStmt();
12961 }
12962