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.getBufferIfLoaded()) {
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() : None) {
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.EnablePrebuiltImplicitModules = Record[Idx++];
5848   HSOpts.UseBuiltinIncludes = Record[Idx++];
5849   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5850   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5851   HSOpts.UseLibcxx = Record[Idx++];
5852   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5853 
5854   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5855                                           Complain);
5856 }
5857 
5858 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5859                                          bool Complain,
5860                                          ASTReaderListener &Listener,
5861                                          std::string &SuggestedPredefines) {
5862   PreprocessorOptions PPOpts;
5863   unsigned Idx = 0;
5864 
5865   // Macro definitions/undefs
5866   for (unsigned N = Record[Idx++]; N; --N) {
5867     std::string Macro = ReadString(Record, Idx);
5868     bool IsUndef = Record[Idx++];
5869     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5870   }
5871 
5872   // Includes
5873   for (unsigned N = Record[Idx++]; N; --N) {
5874     PPOpts.Includes.push_back(ReadString(Record, Idx));
5875   }
5876 
5877   // Macro Includes
5878   for (unsigned N = Record[Idx++]; N; --N) {
5879     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5880   }
5881 
5882   PPOpts.UsePredefines = Record[Idx++];
5883   PPOpts.DetailedRecord = Record[Idx++];
5884   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5885   PPOpts.ObjCXXARCStandardLibrary =
5886     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5887   SuggestedPredefines.clear();
5888   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5889                                           SuggestedPredefines);
5890 }
5891 
5892 std::pair<ModuleFile *, unsigned>
5893 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5894   GlobalPreprocessedEntityMapType::iterator
5895   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5896   assert(I != GlobalPreprocessedEntityMap.end() &&
5897          "Corrupted global preprocessed entity map");
5898   ModuleFile *M = I->second;
5899   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5900   return std::make_pair(M, LocalIndex);
5901 }
5902 
5903 llvm::iterator_range<PreprocessingRecord::iterator>
5904 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5905   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5906     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5907                                              Mod.NumPreprocessedEntities);
5908 
5909   return llvm::make_range(PreprocessingRecord::iterator(),
5910                           PreprocessingRecord::iterator());
5911 }
5912 
5913 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5914 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5915   return llvm::make_range(
5916       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5917       ModuleDeclIterator(this, &Mod,
5918                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5919 }
5920 
5921 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5922   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5923   assert(I != GlobalSkippedRangeMap.end() &&
5924     "Corrupted global skipped range map");
5925   ModuleFile *M = I->second;
5926   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5927   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5928   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5929   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5930                     TranslateSourceLocation(*M, RawRange.getEnd()));
5931   assert(Range.isValid());
5932   return Range;
5933 }
5934 
5935 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5936   PreprocessedEntityID PPID = Index+1;
5937   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5938   ModuleFile &M = *PPInfo.first;
5939   unsigned LocalIndex = PPInfo.second;
5940   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5941 
5942   if (!PP.getPreprocessingRecord()) {
5943     Error("no preprocessing record");
5944     return nullptr;
5945   }
5946 
5947   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5948   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5949           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5950     Error(std::move(Err));
5951     return nullptr;
5952   }
5953 
5954   Expected<llvm::BitstreamEntry> MaybeEntry =
5955       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5956   if (!MaybeEntry) {
5957     Error(MaybeEntry.takeError());
5958     return nullptr;
5959   }
5960   llvm::BitstreamEntry Entry = MaybeEntry.get();
5961 
5962   if (Entry.Kind != llvm::BitstreamEntry::Record)
5963     return nullptr;
5964 
5965   // Read the record.
5966   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5967                     TranslateSourceLocation(M, PPOffs.getEnd()));
5968   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5969   StringRef Blob;
5970   RecordData Record;
5971   Expected<unsigned> MaybeRecType =
5972       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5973   if (!MaybeRecType) {
5974     Error(MaybeRecType.takeError());
5975     return nullptr;
5976   }
5977   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5978   case PPD_MACRO_EXPANSION: {
5979     bool isBuiltin = Record[0];
5980     IdentifierInfo *Name = nullptr;
5981     MacroDefinitionRecord *Def = nullptr;
5982     if (isBuiltin)
5983       Name = getLocalIdentifier(M, Record[1]);
5984     else {
5985       PreprocessedEntityID GlobalID =
5986           getGlobalPreprocessedEntityID(M, Record[1]);
5987       Def = cast<MacroDefinitionRecord>(
5988           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5989     }
5990 
5991     MacroExpansion *ME;
5992     if (isBuiltin)
5993       ME = new (PPRec) MacroExpansion(Name, Range);
5994     else
5995       ME = new (PPRec) MacroExpansion(Def, Range);
5996 
5997     return ME;
5998   }
5999 
6000   case PPD_MACRO_DEFINITION: {
6001     // Decode the identifier info and then check again; if the macro is
6002     // still defined and associated with the identifier,
6003     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6004     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6005 
6006     if (DeserializationListener)
6007       DeserializationListener->MacroDefinitionRead(PPID, MD);
6008 
6009     return MD;
6010   }
6011 
6012   case PPD_INCLUSION_DIRECTIVE: {
6013     const char *FullFileNameStart = Blob.data() + Record[0];
6014     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6015     const FileEntry *File = nullptr;
6016     if (!FullFileName.empty())
6017       if (auto FE = PP.getFileManager().getFile(FullFileName))
6018         File = *FE;
6019 
6020     // FIXME: Stable encoding
6021     InclusionDirective::InclusionKind Kind
6022       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6023     InclusionDirective *ID
6024       = new (PPRec) InclusionDirective(PPRec, Kind,
6025                                        StringRef(Blob.data(), Record[0]),
6026                                        Record[1], Record[3],
6027                                        File,
6028                                        Range);
6029     return ID;
6030   }
6031   }
6032 
6033   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6034 }
6035 
6036 /// Find the next module that contains entities and return the ID
6037 /// of the first entry.
6038 ///
6039 /// \param SLocMapI points at a chunk of a module that contains no
6040 /// preprocessed entities or the entities it contains are not the ones we are
6041 /// looking for.
6042 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6043                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6044   ++SLocMapI;
6045   for (GlobalSLocOffsetMapType::const_iterator
6046          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6047     ModuleFile &M = *SLocMapI->second;
6048     if (M.NumPreprocessedEntities)
6049       return M.BasePreprocessedEntityID;
6050   }
6051 
6052   return getTotalNumPreprocessedEntities();
6053 }
6054 
6055 namespace {
6056 
6057 struct PPEntityComp {
6058   const ASTReader &Reader;
6059   ModuleFile &M;
6060 
6061   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6062 
6063   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6064     SourceLocation LHS = getLoc(L);
6065     SourceLocation RHS = getLoc(R);
6066     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6067   }
6068 
6069   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6070     SourceLocation LHS = getLoc(L);
6071     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6072   }
6073 
6074   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6075     SourceLocation RHS = getLoc(R);
6076     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6077   }
6078 
6079   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6080     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6081   }
6082 };
6083 
6084 } // namespace
6085 
6086 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6087                                                        bool EndsAfter) const {
6088   if (SourceMgr.isLocalSourceLocation(Loc))
6089     return getTotalNumPreprocessedEntities();
6090 
6091   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6092       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6093   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6094          "Corrupted global sloc offset map");
6095 
6096   if (SLocMapI->second->NumPreprocessedEntities == 0)
6097     return findNextPreprocessedEntity(SLocMapI);
6098 
6099   ModuleFile &M = *SLocMapI->second;
6100 
6101   using pp_iterator = const PPEntityOffset *;
6102 
6103   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6104   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6105 
6106   size_t Count = M.NumPreprocessedEntities;
6107   size_t Half;
6108   pp_iterator First = pp_begin;
6109   pp_iterator PPI;
6110 
6111   if (EndsAfter) {
6112     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6113                            PPEntityComp(*this, M));
6114   } else {
6115     // Do a binary search manually instead of using std::lower_bound because
6116     // The end locations of entities may be unordered (when a macro expansion
6117     // is inside another macro argument), but for this case it is not important
6118     // whether we get the first macro expansion or its containing macro.
6119     while (Count > 0) {
6120       Half = Count / 2;
6121       PPI = First;
6122       std::advance(PPI, Half);
6123       if (SourceMgr.isBeforeInTranslationUnit(
6124               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6125         First = PPI;
6126         ++First;
6127         Count = Count - Half - 1;
6128       } else
6129         Count = Half;
6130     }
6131   }
6132 
6133   if (PPI == pp_end)
6134     return findNextPreprocessedEntity(SLocMapI);
6135 
6136   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6137 }
6138 
6139 /// Returns a pair of [Begin, End) indices of preallocated
6140 /// preprocessed entities that \arg Range encompasses.
6141 std::pair<unsigned, unsigned>
6142     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6143   if (Range.isInvalid())
6144     return std::make_pair(0,0);
6145   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6146 
6147   PreprocessedEntityID BeginID =
6148       findPreprocessedEntity(Range.getBegin(), false);
6149   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6150   return std::make_pair(BeginID, EndID);
6151 }
6152 
6153 /// Optionally returns true or false if the preallocated preprocessed
6154 /// entity with index \arg Index came from file \arg FID.
6155 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6156                                                              FileID FID) {
6157   if (FID.isInvalid())
6158     return false;
6159 
6160   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6161   ModuleFile &M = *PPInfo.first;
6162   unsigned LocalIndex = PPInfo.second;
6163   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6164 
6165   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6166   if (Loc.isInvalid())
6167     return false;
6168 
6169   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6170     return true;
6171   else
6172     return false;
6173 }
6174 
6175 namespace {
6176 
6177   /// Visitor used to search for information about a header file.
6178   class HeaderFileInfoVisitor {
6179     const FileEntry *FE;
6180     Optional<HeaderFileInfo> HFI;
6181 
6182   public:
6183     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6184 
6185     bool operator()(ModuleFile &M) {
6186       HeaderFileInfoLookupTable *Table
6187         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6188       if (!Table)
6189         return false;
6190 
6191       // Look in the on-disk hash table for an entry for this file name.
6192       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6193       if (Pos == Table->end())
6194         return false;
6195 
6196       HFI = *Pos;
6197       return true;
6198     }
6199 
6200     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6201   };
6202 
6203 } // namespace
6204 
6205 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6206   HeaderFileInfoVisitor Visitor(FE);
6207   ModuleMgr.visit(Visitor);
6208   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6209     return *HFI;
6210 
6211   return HeaderFileInfo();
6212 }
6213 
6214 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6215   using DiagState = DiagnosticsEngine::DiagState;
6216   SmallVector<DiagState *, 32> DiagStates;
6217 
6218   for (ModuleFile &F : ModuleMgr) {
6219     unsigned Idx = 0;
6220     auto &Record = F.PragmaDiagMappings;
6221     if (Record.empty())
6222       continue;
6223 
6224     DiagStates.clear();
6225 
6226     auto ReadDiagState =
6227         [&](const DiagState &BasedOn, SourceLocation Loc,
6228             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6229       unsigned BackrefID = Record[Idx++];
6230       if (BackrefID != 0)
6231         return DiagStates[BackrefID - 1];
6232 
6233       // A new DiagState was created here.
6234       Diag.DiagStates.push_back(BasedOn);
6235       DiagState *NewState = &Diag.DiagStates.back();
6236       DiagStates.push_back(NewState);
6237       unsigned Size = Record[Idx++];
6238       assert(Idx + Size * 2 <= Record.size() &&
6239              "Invalid data, not enough diag/map pairs");
6240       while (Size--) {
6241         unsigned DiagID = Record[Idx++];
6242         DiagnosticMapping NewMapping =
6243             DiagnosticMapping::deserialize(Record[Idx++]);
6244         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6245           continue;
6246 
6247         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6248 
6249         // If this mapping was specified as a warning but the severity was
6250         // upgraded due to diagnostic settings, simulate the current diagnostic
6251         // settings (and use a warning).
6252         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6253           NewMapping.setSeverity(diag::Severity::Warning);
6254           NewMapping.setUpgradedFromWarning(false);
6255         }
6256 
6257         Mapping = NewMapping;
6258       }
6259       return NewState;
6260     };
6261 
6262     // Read the first state.
6263     DiagState *FirstState;
6264     if (F.Kind == MK_ImplicitModule) {
6265       // Implicitly-built modules are reused with different diagnostic
6266       // settings.  Use the initial diagnostic state from Diag to simulate this
6267       // compilation's diagnostic settings.
6268       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6269       DiagStates.push_back(FirstState);
6270 
6271       // Skip the initial diagnostic state from the serialized module.
6272       assert(Record[1] == 0 &&
6273              "Invalid data, unexpected backref in initial state");
6274       Idx = 3 + Record[2] * 2;
6275       assert(Idx < Record.size() &&
6276              "Invalid data, not enough state change pairs in initial state");
6277     } else if (F.isModule()) {
6278       // For an explicit module, preserve the flags from the module build
6279       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6280       // -Wblah flags.
6281       unsigned Flags = Record[Idx++];
6282       DiagState Initial;
6283       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6284       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6285       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6286       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6287       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6288       Initial.ExtBehavior = (diag::Severity)Flags;
6289       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6290 
6291       assert(F.OriginalSourceFileID.isValid());
6292 
6293       // Set up the root buffer of the module to start with the initial
6294       // diagnostic state of the module itself, to cover files that contain no
6295       // explicit transitions (for which we did not serialize anything).
6296       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6297           .StateTransitions.push_back({FirstState, 0});
6298     } else {
6299       // For prefix ASTs, start with whatever the user configured on the
6300       // command line.
6301       Idx++; // Skip flags.
6302       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6303                                  SourceLocation(), false);
6304     }
6305 
6306     // Read the state transitions.
6307     unsigned NumLocations = Record[Idx++];
6308     while (NumLocations--) {
6309       assert(Idx < Record.size() &&
6310              "Invalid data, missing pragma diagnostic states");
6311       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6312       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6313       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6314       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6315       unsigned Transitions = Record[Idx++];
6316 
6317       // Note that we don't need to set up Parent/ParentOffset here, because
6318       // we won't be changing the diagnostic state within imported FileIDs
6319       // (other than perhaps appending to the main source file, which has no
6320       // parent).
6321       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6322       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6323       for (unsigned I = 0; I != Transitions; ++I) {
6324         unsigned Offset = Record[Idx++];
6325         auto *State =
6326             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6327         F.StateTransitions.push_back({State, Offset});
6328       }
6329     }
6330 
6331     // Read the final state.
6332     assert(Idx < Record.size() &&
6333            "Invalid data, missing final pragma diagnostic state");
6334     SourceLocation CurStateLoc =
6335         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6336     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6337 
6338     if (!F.isModule()) {
6339       Diag.DiagStatesByLoc.CurDiagState = CurState;
6340       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6341 
6342       // Preserve the property that the imaginary root file describes the
6343       // current state.
6344       FileID NullFile;
6345       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6346       if (T.empty())
6347         T.push_back({CurState, 0});
6348       else
6349         T[0].State = CurState;
6350     }
6351 
6352     // Don't try to read these mappings again.
6353     Record.clear();
6354   }
6355 }
6356 
6357 /// Get the correct cursor and offset for loading a type.
6358 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6359   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6360   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6361   ModuleFile *M = I->second;
6362   return RecordLocation(
6363       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6364              M->DeclsBlockStartOffset);
6365 }
6366 
6367 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6368   switch (code) {
6369 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6370   case TYPE_##CODE_ID: return Type::CLASS_ID;
6371 #include "clang/Serialization/TypeBitCodes.def"
6372   default: return llvm::None;
6373   }
6374 }
6375 
6376 /// Read and return the type with the given index..
6377 ///
6378 /// The index is the type ID, shifted and minus the number of predefs. This
6379 /// routine actually reads the record corresponding to the type at the given
6380 /// location. It is a helper routine for GetType, which deals with reading type
6381 /// IDs.
6382 QualType ASTReader::readTypeRecord(unsigned Index) {
6383   assert(ContextObj && "reading type with no AST context");
6384   ASTContext &Context = *ContextObj;
6385   RecordLocation Loc = TypeCursorForIndex(Index);
6386   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6387 
6388   // Keep track of where we are in the stream, then jump back there
6389   // after reading this type.
6390   SavedStreamPosition SavedPosition(DeclsCursor);
6391 
6392   ReadingKindTracker ReadingKind(Read_Type, *this);
6393 
6394   // Note that we are loading a type record.
6395   Deserializing AType(this);
6396 
6397   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6398     Error(std::move(Err));
6399     return QualType();
6400   }
6401   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6402   if (!RawCode) {
6403     Error(RawCode.takeError());
6404     return QualType();
6405   }
6406 
6407   ASTRecordReader Record(*this, *Loc.F);
6408   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6409   if (!Code) {
6410     Error(Code.takeError());
6411     return QualType();
6412   }
6413   if (Code.get() == TYPE_EXT_QUAL) {
6414     QualType baseType = Record.readQualType();
6415     Qualifiers quals = Record.readQualifiers();
6416     return Context.getQualifiedType(baseType, quals);
6417   }
6418 
6419   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6420   if (!maybeClass) {
6421     Error("Unexpected code for type");
6422     return QualType();
6423   }
6424 
6425   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6426   return TypeReader.read(*maybeClass);
6427 }
6428 
6429 namespace clang {
6430 
6431 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6432   ASTRecordReader &Reader;
6433 
6434   SourceLocation readSourceLocation() {
6435     return Reader.readSourceLocation();
6436   }
6437 
6438   TypeSourceInfo *GetTypeSourceInfo() {
6439     return Reader.readTypeSourceInfo();
6440   }
6441 
6442   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6443     return Reader.readNestedNameSpecifierLoc();
6444   }
6445 
6446   Attr *ReadAttr() {
6447     return Reader.readAttr();
6448   }
6449 
6450 public:
6451   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6452 
6453   // We want compile-time assurance that we've enumerated all of
6454   // these, so unfortunately we have to declare them first, then
6455   // define them out-of-line.
6456 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6457 #define TYPELOC(CLASS, PARENT) \
6458   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6459 #include "clang/AST/TypeLocNodes.def"
6460 
6461   void VisitFunctionTypeLoc(FunctionTypeLoc);
6462   void VisitArrayTypeLoc(ArrayTypeLoc);
6463 };
6464 
6465 } // namespace clang
6466 
6467 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6468   // nothing to do
6469 }
6470 
6471 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6472   TL.setBuiltinLoc(readSourceLocation());
6473   if (TL.needsExtraLocalData()) {
6474     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6475     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6476     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6477     TL.setModeAttr(Reader.readInt());
6478   }
6479 }
6480 
6481 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6482   TL.setNameLoc(readSourceLocation());
6483 }
6484 
6485 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6486   TL.setStarLoc(readSourceLocation());
6487 }
6488 
6489 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6490   // nothing to do
6491 }
6492 
6493 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6494   // nothing to do
6495 }
6496 
6497 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6498   TL.setExpansionLoc(readSourceLocation());
6499 }
6500 
6501 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6502   TL.setCaretLoc(readSourceLocation());
6503 }
6504 
6505 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6506   TL.setAmpLoc(readSourceLocation());
6507 }
6508 
6509 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6510   TL.setAmpAmpLoc(readSourceLocation());
6511 }
6512 
6513 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6514   TL.setStarLoc(readSourceLocation());
6515   TL.setClassTInfo(GetTypeSourceInfo());
6516 }
6517 
6518 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6519   TL.setLBracketLoc(readSourceLocation());
6520   TL.setRBracketLoc(readSourceLocation());
6521   if (Reader.readBool())
6522     TL.setSizeExpr(Reader.readExpr());
6523   else
6524     TL.setSizeExpr(nullptr);
6525 }
6526 
6527 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6528   VisitArrayTypeLoc(TL);
6529 }
6530 
6531 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6532   VisitArrayTypeLoc(TL);
6533 }
6534 
6535 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6536   VisitArrayTypeLoc(TL);
6537 }
6538 
6539 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6540                                             DependentSizedArrayTypeLoc TL) {
6541   VisitArrayTypeLoc(TL);
6542 }
6543 
6544 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6545     DependentAddressSpaceTypeLoc TL) {
6546 
6547     TL.setAttrNameLoc(readSourceLocation());
6548     TL.setAttrOperandParensRange(Reader.readSourceRange());
6549     TL.setAttrExprOperand(Reader.readExpr());
6550 }
6551 
6552 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6553                                         DependentSizedExtVectorTypeLoc TL) {
6554   TL.setNameLoc(readSourceLocation());
6555 }
6556 
6557 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6558   TL.setNameLoc(readSourceLocation());
6559 }
6560 
6561 void TypeLocReader::VisitDependentVectorTypeLoc(
6562     DependentVectorTypeLoc TL) {
6563   TL.setNameLoc(readSourceLocation());
6564 }
6565 
6566 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6567   TL.setNameLoc(readSourceLocation());
6568 }
6569 
6570 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6571   TL.setAttrNameLoc(readSourceLocation());
6572   TL.setAttrOperandParensRange(Reader.readSourceRange());
6573   TL.setAttrRowOperand(Reader.readExpr());
6574   TL.setAttrColumnOperand(Reader.readExpr());
6575 }
6576 
6577 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6578     DependentSizedMatrixTypeLoc TL) {
6579   TL.setAttrNameLoc(readSourceLocation());
6580   TL.setAttrOperandParensRange(Reader.readSourceRange());
6581   TL.setAttrRowOperand(Reader.readExpr());
6582   TL.setAttrColumnOperand(Reader.readExpr());
6583 }
6584 
6585 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6586   TL.setLocalRangeBegin(readSourceLocation());
6587   TL.setLParenLoc(readSourceLocation());
6588   TL.setRParenLoc(readSourceLocation());
6589   TL.setExceptionSpecRange(Reader.readSourceRange());
6590   TL.setLocalRangeEnd(readSourceLocation());
6591   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6592     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6593   }
6594 }
6595 
6596 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6597   VisitFunctionTypeLoc(TL);
6598 }
6599 
6600 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6601   VisitFunctionTypeLoc(TL);
6602 }
6603 
6604 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6605   TL.setNameLoc(readSourceLocation());
6606 }
6607 
6608 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6609   TL.setNameLoc(readSourceLocation());
6610 }
6611 
6612 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6613   TL.setTypeofLoc(readSourceLocation());
6614   TL.setLParenLoc(readSourceLocation());
6615   TL.setRParenLoc(readSourceLocation());
6616 }
6617 
6618 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6619   TL.setTypeofLoc(readSourceLocation());
6620   TL.setLParenLoc(readSourceLocation());
6621   TL.setRParenLoc(readSourceLocation());
6622   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6623 }
6624 
6625 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6626   TL.setNameLoc(readSourceLocation());
6627 }
6628 
6629 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6630   TL.setKWLoc(readSourceLocation());
6631   TL.setLParenLoc(readSourceLocation());
6632   TL.setRParenLoc(readSourceLocation());
6633   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6634 }
6635 
6636 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6637   TL.setNameLoc(readSourceLocation());
6638   if (Reader.readBool()) {
6639     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6640     TL.setTemplateKWLoc(readSourceLocation());
6641     TL.setConceptNameLoc(readSourceLocation());
6642     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6643     TL.setLAngleLoc(readSourceLocation());
6644     TL.setRAngleLoc(readSourceLocation());
6645     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6646       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6647                               TL.getTypePtr()->getArg(i).getKind()));
6648   }
6649 }
6650 
6651 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6652     DeducedTemplateSpecializationTypeLoc TL) {
6653   TL.setTemplateNameLoc(readSourceLocation());
6654 }
6655 
6656 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6657   TL.setNameLoc(readSourceLocation());
6658 }
6659 
6660 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6661   TL.setNameLoc(readSourceLocation());
6662 }
6663 
6664 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6665   TL.setAttr(ReadAttr());
6666 }
6667 
6668 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6669   TL.setNameLoc(readSourceLocation());
6670 }
6671 
6672 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6673                                             SubstTemplateTypeParmTypeLoc TL) {
6674   TL.setNameLoc(readSourceLocation());
6675 }
6676 
6677 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6678                                           SubstTemplateTypeParmPackTypeLoc TL) {
6679   TL.setNameLoc(readSourceLocation());
6680 }
6681 
6682 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6683                                            TemplateSpecializationTypeLoc TL) {
6684   TL.setTemplateKeywordLoc(readSourceLocation());
6685   TL.setTemplateNameLoc(readSourceLocation());
6686   TL.setLAngleLoc(readSourceLocation());
6687   TL.setRAngleLoc(readSourceLocation());
6688   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6689     TL.setArgLocInfo(
6690         i,
6691         Reader.readTemplateArgumentLocInfo(
6692           TL.getTypePtr()->getArg(i).getKind()));
6693 }
6694 
6695 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6696   TL.setLParenLoc(readSourceLocation());
6697   TL.setRParenLoc(readSourceLocation());
6698 }
6699 
6700 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6701   TL.setElaboratedKeywordLoc(readSourceLocation());
6702   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6703 }
6704 
6705 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6706   TL.setNameLoc(readSourceLocation());
6707 }
6708 
6709 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6710   TL.setElaboratedKeywordLoc(readSourceLocation());
6711   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6712   TL.setNameLoc(readSourceLocation());
6713 }
6714 
6715 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6716        DependentTemplateSpecializationTypeLoc TL) {
6717   TL.setElaboratedKeywordLoc(readSourceLocation());
6718   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6719   TL.setTemplateKeywordLoc(readSourceLocation());
6720   TL.setTemplateNameLoc(readSourceLocation());
6721   TL.setLAngleLoc(readSourceLocation());
6722   TL.setRAngleLoc(readSourceLocation());
6723   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6724     TL.setArgLocInfo(
6725         I,
6726         Reader.readTemplateArgumentLocInfo(
6727             TL.getTypePtr()->getArg(I).getKind()));
6728 }
6729 
6730 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6731   TL.setEllipsisLoc(readSourceLocation());
6732 }
6733 
6734 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6735   TL.setNameLoc(readSourceLocation());
6736 }
6737 
6738 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6739   if (TL.getNumProtocols()) {
6740     TL.setProtocolLAngleLoc(readSourceLocation());
6741     TL.setProtocolRAngleLoc(readSourceLocation());
6742   }
6743   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6744     TL.setProtocolLoc(i, readSourceLocation());
6745 }
6746 
6747 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6748   TL.setHasBaseTypeAsWritten(Reader.readBool());
6749   TL.setTypeArgsLAngleLoc(readSourceLocation());
6750   TL.setTypeArgsRAngleLoc(readSourceLocation());
6751   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6752     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6753   TL.setProtocolLAngleLoc(readSourceLocation());
6754   TL.setProtocolRAngleLoc(readSourceLocation());
6755   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6756     TL.setProtocolLoc(i, readSourceLocation());
6757 }
6758 
6759 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6760   TL.setStarLoc(readSourceLocation());
6761 }
6762 
6763 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6764   TL.setKWLoc(readSourceLocation());
6765   TL.setLParenLoc(readSourceLocation());
6766   TL.setRParenLoc(readSourceLocation());
6767 }
6768 
6769 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6770   TL.setKWLoc(readSourceLocation());
6771 }
6772 
6773 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6774   TL.setNameLoc(readSourceLocation());
6775 }
6776 void TypeLocReader::VisitDependentExtIntTypeLoc(
6777     clang::DependentExtIntTypeLoc TL) {
6778   TL.setNameLoc(readSourceLocation());
6779 }
6780 
6781 
6782 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6783   TypeLocReader TLR(*this);
6784   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6785     TLR.Visit(TL);
6786 }
6787 
6788 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6789   QualType InfoTy = readType();
6790   if (InfoTy.isNull())
6791     return nullptr;
6792 
6793   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6794   readTypeLoc(TInfo->getTypeLoc());
6795   return TInfo;
6796 }
6797 
6798 QualType ASTReader::GetType(TypeID ID) {
6799   assert(ContextObj && "reading type with no AST context");
6800   ASTContext &Context = *ContextObj;
6801 
6802   unsigned FastQuals = ID & Qualifiers::FastMask;
6803   unsigned Index = ID >> Qualifiers::FastWidth;
6804 
6805   if (Index < NUM_PREDEF_TYPE_IDS) {
6806     QualType T;
6807     switch ((PredefinedTypeIDs)Index) {
6808     case PREDEF_TYPE_NULL_ID:
6809       return QualType();
6810     case PREDEF_TYPE_VOID_ID:
6811       T = Context.VoidTy;
6812       break;
6813     case PREDEF_TYPE_BOOL_ID:
6814       T = Context.BoolTy;
6815       break;
6816     case PREDEF_TYPE_CHAR_U_ID:
6817     case PREDEF_TYPE_CHAR_S_ID:
6818       // FIXME: Check that the signedness of CharTy is correct!
6819       T = Context.CharTy;
6820       break;
6821     case PREDEF_TYPE_UCHAR_ID:
6822       T = Context.UnsignedCharTy;
6823       break;
6824     case PREDEF_TYPE_USHORT_ID:
6825       T = Context.UnsignedShortTy;
6826       break;
6827     case PREDEF_TYPE_UINT_ID:
6828       T = Context.UnsignedIntTy;
6829       break;
6830     case PREDEF_TYPE_ULONG_ID:
6831       T = Context.UnsignedLongTy;
6832       break;
6833     case PREDEF_TYPE_ULONGLONG_ID:
6834       T = Context.UnsignedLongLongTy;
6835       break;
6836     case PREDEF_TYPE_UINT128_ID:
6837       T = Context.UnsignedInt128Ty;
6838       break;
6839     case PREDEF_TYPE_SCHAR_ID:
6840       T = Context.SignedCharTy;
6841       break;
6842     case PREDEF_TYPE_WCHAR_ID:
6843       T = Context.WCharTy;
6844       break;
6845     case PREDEF_TYPE_SHORT_ID:
6846       T = Context.ShortTy;
6847       break;
6848     case PREDEF_TYPE_INT_ID:
6849       T = Context.IntTy;
6850       break;
6851     case PREDEF_TYPE_LONG_ID:
6852       T = Context.LongTy;
6853       break;
6854     case PREDEF_TYPE_LONGLONG_ID:
6855       T = Context.LongLongTy;
6856       break;
6857     case PREDEF_TYPE_INT128_ID:
6858       T = Context.Int128Ty;
6859       break;
6860     case PREDEF_TYPE_BFLOAT16_ID:
6861       T = Context.BFloat16Ty;
6862       break;
6863     case PREDEF_TYPE_HALF_ID:
6864       T = Context.HalfTy;
6865       break;
6866     case PREDEF_TYPE_FLOAT_ID:
6867       T = Context.FloatTy;
6868       break;
6869     case PREDEF_TYPE_DOUBLE_ID:
6870       T = Context.DoubleTy;
6871       break;
6872     case PREDEF_TYPE_LONGDOUBLE_ID:
6873       T = Context.LongDoubleTy;
6874       break;
6875     case PREDEF_TYPE_SHORT_ACCUM_ID:
6876       T = Context.ShortAccumTy;
6877       break;
6878     case PREDEF_TYPE_ACCUM_ID:
6879       T = Context.AccumTy;
6880       break;
6881     case PREDEF_TYPE_LONG_ACCUM_ID:
6882       T = Context.LongAccumTy;
6883       break;
6884     case PREDEF_TYPE_USHORT_ACCUM_ID:
6885       T = Context.UnsignedShortAccumTy;
6886       break;
6887     case PREDEF_TYPE_UACCUM_ID:
6888       T = Context.UnsignedAccumTy;
6889       break;
6890     case PREDEF_TYPE_ULONG_ACCUM_ID:
6891       T = Context.UnsignedLongAccumTy;
6892       break;
6893     case PREDEF_TYPE_SHORT_FRACT_ID:
6894       T = Context.ShortFractTy;
6895       break;
6896     case PREDEF_TYPE_FRACT_ID:
6897       T = Context.FractTy;
6898       break;
6899     case PREDEF_TYPE_LONG_FRACT_ID:
6900       T = Context.LongFractTy;
6901       break;
6902     case PREDEF_TYPE_USHORT_FRACT_ID:
6903       T = Context.UnsignedShortFractTy;
6904       break;
6905     case PREDEF_TYPE_UFRACT_ID:
6906       T = Context.UnsignedFractTy;
6907       break;
6908     case PREDEF_TYPE_ULONG_FRACT_ID:
6909       T = Context.UnsignedLongFractTy;
6910       break;
6911     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6912       T = Context.SatShortAccumTy;
6913       break;
6914     case PREDEF_TYPE_SAT_ACCUM_ID:
6915       T = Context.SatAccumTy;
6916       break;
6917     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6918       T = Context.SatLongAccumTy;
6919       break;
6920     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6921       T = Context.SatUnsignedShortAccumTy;
6922       break;
6923     case PREDEF_TYPE_SAT_UACCUM_ID:
6924       T = Context.SatUnsignedAccumTy;
6925       break;
6926     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6927       T = Context.SatUnsignedLongAccumTy;
6928       break;
6929     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6930       T = Context.SatShortFractTy;
6931       break;
6932     case PREDEF_TYPE_SAT_FRACT_ID:
6933       T = Context.SatFractTy;
6934       break;
6935     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6936       T = Context.SatLongFractTy;
6937       break;
6938     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6939       T = Context.SatUnsignedShortFractTy;
6940       break;
6941     case PREDEF_TYPE_SAT_UFRACT_ID:
6942       T = Context.SatUnsignedFractTy;
6943       break;
6944     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6945       T = Context.SatUnsignedLongFractTy;
6946       break;
6947     case PREDEF_TYPE_FLOAT16_ID:
6948       T = Context.Float16Ty;
6949       break;
6950     case PREDEF_TYPE_FLOAT128_ID:
6951       T = Context.Float128Ty;
6952       break;
6953     case PREDEF_TYPE_OVERLOAD_ID:
6954       T = Context.OverloadTy;
6955       break;
6956     case PREDEF_TYPE_BOUND_MEMBER:
6957       T = Context.BoundMemberTy;
6958       break;
6959     case PREDEF_TYPE_PSEUDO_OBJECT:
6960       T = Context.PseudoObjectTy;
6961       break;
6962     case PREDEF_TYPE_DEPENDENT_ID:
6963       T = Context.DependentTy;
6964       break;
6965     case PREDEF_TYPE_UNKNOWN_ANY:
6966       T = Context.UnknownAnyTy;
6967       break;
6968     case PREDEF_TYPE_NULLPTR_ID:
6969       T = Context.NullPtrTy;
6970       break;
6971     case PREDEF_TYPE_CHAR8_ID:
6972       T = Context.Char8Ty;
6973       break;
6974     case PREDEF_TYPE_CHAR16_ID:
6975       T = Context.Char16Ty;
6976       break;
6977     case PREDEF_TYPE_CHAR32_ID:
6978       T = Context.Char32Ty;
6979       break;
6980     case PREDEF_TYPE_OBJC_ID:
6981       T = Context.ObjCBuiltinIdTy;
6982       break;
6983     case PREDEF_TYPE_OBJC_CLASS:
6984       T = Context.ObjCBuiltinClassTy;
6985       break;
6986     case PREDEF_TYPE_OBJC_SEL:
6987       T = Context.ObjCBuiltinSelTy;
6988       break;
6989 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6990     case PREDEF_TYPE_##Id##_ID: \
6991       T = Context.SingletonId; \
6992       break;
6993 #include "clang/Basic/OpenCLImageTypes.def"
6994 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6995     case PREDEF_TYPE_##Id##_ID: \
6996       T = Context.Id##Ty; \
6997       break;
6998 #include "clang/Basic/OpenCLExtensionTypes.def"
6999     case PREDEF_TYPE_SAMPLER_ID:
7000       T = Context.OCLSamplerTy;
7001       break;
7002     case PREDEF_TYPE_EVENT_ID:
7003       T = Context.OCLEventTy;
7004       break;
7005     case PREDEF_TYPE_CLK_EVENT_ID:
7006       T = Context.OCLClkEventTy;
7007       break;
7008     case PREDEF_TYPE_QUEUE_ID:
7009       T = Context.OCLQueueTy;
7010       break;
7011     case PREDEF_TYPE_RESERVE_ID_ID:
7012       T = Context.OCLReserveIDTy;
7013       break;
7014     case PREDEF_TYPE_AUTO_DEDUCT:
7015       T = Context.getAutoDeductType();
7016       break;
7017     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7018       T = Context.getAutoRRefDeductType();
7019       break;
7020     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7021       T = Context.ARCUnbridgedCastTy;
7022       break;
7023     case PREDEF_TYPE_BUILTIN_FN:
7024       T = Context.BuiltinFnTy;
7025       break;
7026     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7027       T = Context.IncompleteMatrixIdxTy;
7028       break;
7029     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7030       T = Context.OMPArraySectionTy;
7031       break;
7032     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7033       T = Context.OMPArraySectionTy;
7034       break;
7035     case PREDEF_TYPE_OMP_ITERATOR:
7036       T = Context.OMPIteratorTy;
7037       break;
7038 #define SVE_TYPE(Name, Id, SingletonId) \
7039     case PREDEF_TYPE_##Id##_ID: \
7040       T = Context.SingletonId; \
7041       break;
7042 #include "clang/Basic/AArch64SVEACLETypes.def"
7043 #define PPC_MMA_VECTOR_TYPE(Name, Id, Size) \
7044     case PREDEF_TYPE_##Id##_ID: \
7045       T = Context.Id##Ty; \
7046       break;
7047 #include "clang/Basic/PPCTypes.def"
7048     }
7049 
7050     assert(!T.isNull() && "Unknown predefined type");
7051     return T.withFastQualifiers(FastQuals);
7052   }
7053 
7054   Index -= NUM_PREDEF_TYPE_IDS;
7055   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7056   if (TypesLoaded[Index].isNull()) {
7057     TypesLoaded[Index] = readTypeRecord(Index);
7058     if (TypesLoaded[Index].isNull())
7059       return QualType();
7060 
7061     TypesLoaded[Index]->setFromAST();
7062     if (DeserializationListener)
7063       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7064                                         TypesLoaded[Index]);
7065   }
7066 
7067   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7068 }
7069 
7070 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7071   return GetType(getGlobalTypeID(F, LocalID));
7072 }
7073 
7074 serialization::TypeID
7075 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7076   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7077   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7078 
7079   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7080     return LocalID;
7081 
7082   if (!F.ModuleOffsetMap.empty())
7083     ReadModuleOffsetMap(F);
7084 
7085   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7086     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7087   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7088 
7089   unsigned GlobalIndex = LocalIndex + I->second;
7090   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7091 }
7092 
7093 TemplateArgumentLocInfo
7094 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7095   switch (Kind) {
7096   case TemplateArgument::Expression:
7097     return readExpr();
7098   case TemplateArgument::Type:
7099     return readTypeSourceInfo();
7100   case TemplateArgument::Template: {
7101     NestedNameSpecifierLoc QualifierLoc =
7102       readNestedNameSpecifierLoc();
7103     SourceLocation TemplateNameLoc = readSourceLocation();
7104     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7105                                    TemplateNameLoc, SourceLocation());
7106   }
7107   case TemplateArgument::TemplateExpansion: {
7108     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7109     SourceLocation TemplateNameLoc = readSourceLocation();
7110     SourceLocation EllipsisLoc = readSourceLocation();
7111     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7112                                    TemplateNameLoc, EllipsisLoc);
7113   }
7114   case TemplateArgument::Null:
7115   case TemplateArgument::Integral:
7116   case TemplateArgument::Declaration:
7117   case TemplateArgument::NullPtr:
7118   case TemplateArgument::Pack:
7119     // FIXME: Is this right?
7120     return TemplateArgumentLocInfo();
7121   }
7122   llvm_unreachable("unexpected template argument loc");
7123 }
7124 
7125 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7126   TemplateArgument Arg = readTemplateArgument();
7127 
7128   if (Arg.getKind() == TemplateArgument::Expression) {
7129     if (readBool()) // bool InfoHasSameExpr.
7130       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7131   }
7132   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7133 }
7134 
7135 const ASTTemplateArgumentListInfo *
7136 ASTRecordReader::readASTTemplateArgumentListInfo() {
7137   SourceLocation LAngleLoc = readSourceLocation();
7138   SourceLocation RAngleLoc = readSourceLocation();
7139   unsigned NumArgsAsWritten = readInt();
7140   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7141   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7142     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7143   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7144 }
7145 
7146 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7147   return GetDecl(ID);
7148 }
7149 
7150 void ASTReader::CompleteRedeclChain(const Decl *D) {
7151   if (NumCurrentElementsDeserializing) {
7152     // We arrange to not care about the complete redeclaration chain while we're
7153     // deserializing. Just remember that the AST has marked this one as complete
7154     // but that it's not actually complete yet, so we know we still need to
7155     // complete it later.
7156     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7157     return;
7158   }
7159 
7160   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7161 
7162   // If this is a named declaration, complete it by looking it up
7163   // within its context.
7164   //
7165   // FIXME: Merging a function definition should merge
7166   // all mergeable entities within it.
7167   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7168       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7169     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7170       if (!getContext().getLangOpts().CPlusPlus &&
7171           isa<TranslationUnitDecl>(DC)) {
7172         // Outside of C++, we don't have a lookup table for the TU, so update
7173         // the identifier instead. (For C++ modules, we don't store decls
7174         // in the serialized identifier table, so we do the lookup in the TU.)
7175         auto *II = Name.getAsIdentifierInfo();
7176         assert(II && "non-identifier name in C?");
7177         if (II->isOutOfDate())
7178           updateOutOfDateIdentifier(*II);
7179       } else
7180         DC->lookup(Name);
7181     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7182       // Find all declarations of this kind from the relevant context.
7183       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7184         auto *DC = cast<DeclContext>(DCDecl);
7185         SmallVector<Decl*, 8> Decls;
7186         FindExternalLexicalDecls(
7187             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7188       }
7189     }
7190   }
7191 
7192   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7193     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7194   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7195     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7196   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7197     if (auto *Template = FD->getPrimaryTemplate())
7198       Template->LoadLazySpecializations();
7199   }
7200 }
7201 
7202 CXXCtorInitializer **
7203 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7204   RecordLocation Loc = getLocalBitOffset(Offset);
7205   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7206   SavedStreamPosition SavedPosition(Cursor);
7207   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7208     Error(std::move(Err));
7209     return nullptr;
7210   }
7211   ReadingKindTracker ReadingKind(Read_Decl, *this);
7212 
7213   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7214   if (!MaybeCode) {
7215     Error(MaybeCode.takeError());
7216     return nullptr;
7217   }
7218   unsigned Code = MaybeCode.get();
7219 
7220   ASTRecordReader Record(*this, *Loc.F);
7221   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7222   if (!MaybeRecCode) {
7223     Error(MaybeRecCode.takeError());
7224     return nullptr;
7225   }
7226   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7227     Error("malformed AST file: missing C++ ctor initializers");
7228     return nullptr;
7229   }
7230 
7231   return Record.readCXXCtorInitializers();
7232 }
7233 
7234 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7235   assert(ContextObj && "reading base specifiers with no AST context");
7236   ASTContext &Context = *ContextObj;
7237 
7238   RecordLocation Loc = getLocalBitOffset(Offset);
7239   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7240   SavedStreamPosition SavedPosition(Cursor);
7241   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7242     Error(std::move(Err));
7243     return nullptr;
7244   }
7245   ReadingKindTracker ReadingKind(Read_Decl, *this);
7246 
7247   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7248   if (!MaybeCode) {
7249     Error(MaybeCode.takeError());
7250     return nullptr;
7251   }
7252   unsigned Code = MaybeCode.get();
7253 
7254   ASTRecordReader Record(*this, *Loc.F);
7255   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7256   if (!MaybeRecCode) {
7257     Error(MaybeCode.takeError());
7258     return nullptr;
7259   }
7260   unsigned RecCode = MaybeRecCode.get();
7261 
7262   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7263     Error("malformed AST file: missing C++ base specifiers");
7264     return nullptr;
7265   }
7266 
7267   unsigned NumBases = Record.readInt();
7268   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7269   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7270   for (unsigned I = 0; I != NumBases; ++I)
7271     Bases[I] = Record.readCXXBaseSpecifier();
7272   return Bases;
7273 }
7274 
7275 serialization::DeclID
7276 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7277   if (LocalID < NUM_PREDEF_DECL_IDS)
7278     return LocalID;
7279 
7280   if (!F.ModuleOffsetMap.empty())
7281     ReadModuleOffsetMap(F);
7282 
7283   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7284     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7285   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7286 
7287   return LocalID + I->second;
7288 }
7289 
7290 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7291                                    ModuleFile &M) const {
7292   // Predefined decls aren't from any module.
7293   if (ID < NUM_PREDEF_DECL_IDS)
7294     return false;
7295 
7296   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7297          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7298 }
7299 
7300 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7301   if (!D->isFromASTFile())
7302     return nullptr;
7303   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7304   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7305   return I->second;
7306 }
7307 
7308 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7309   if (ID < NUM_PREDEF_DECL_IDS)
7310     return SourceLocation();
7311 
7312   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7313 
7314   if (Index > DeclsLoaded.size()) {
7315     Error("declaration ID out-of-range for AST file");
7316     return SourceLocation();
7317   }
7318 
7319   if (Decl *D = DeclsLoaded[Index])
7320     return D->getLocation();
7321 
7322   SourceLocation Loc;
7323   DeclCursorForID(ID, Loc);
7324   return Loc;
7325 }
7326 
7327 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7328   switch (ID) {
7329   case PREDEF_DECL_NULL_ID:
7330     return nullptr;
7331 
7332   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7333     return Context.getTranslationUnitDecl();
7334 
7335   case PREDEF_DECL_OBJC_ID_ID:
7336     return Context.getObjCIdDecl();
7337 
7338   case PREDEF_DECL_OBJC_SEL_ID:
7339     return Context.getObjCSelDecl();
7340 
7341   case PREDEF_DECL_OBJC_CLASS_ID:
7342     return Context.getObjCClassDecl();
7343 
7344   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7345     return Context.getObjCProtocolDecl();
7346 
7347   case PREDEF_DECL_INT_128_ID:
7348     return Context.getInt128Decl();
7349 
7350   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7351     return Context.getUInt128Decl();
7352 
7353   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7354     return Context.getObjCInstanceTypeDecl();
7355 
7356   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7357     return Context.getBuiltinVaListDecl();
7358 
7359   case PREDEF_DECL_VA_LIST_TAG:
7360     return Context.getVaListTagDecl();
7361 
7362   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7363     return Context.getBuiltinMSVaListDecl();
7364 
7365   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7366     return Context.getMSGuidTagDecl();
7367 
7368   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7369     return Context.getExternCContextDecl();
7370 
7371   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7372     return Context.getMakeIntegerSeqDecl();
7373 
7374   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7375     return Context.getCFConstantStringDecl();
7376 
7377   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7378     return Context.getCFConstantStringTagDecl();
7379 
7380   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7381     return Context.getTypePackElementDecl();
7382   }
7383   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7384 }
7385 
7386 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7387   assert(ContextObj && "reading decl with no AST context");
7388   if (ID < NUM_PREDEF_DECL_IDS) {
7389     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7390     if (D) {
7391       // Track that we have merged the declaration with ID \p ID into the
7392       // pre-existing predefined declaration \p D.
7393       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7394       if (Merged.empty())
7395         Merged.push_back(ID);
7396     }
7397     return D;
7398   }
7399 
7400   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7401 
7402   if (Index >= DeclsLoaded.size()) {
7403     assert(0 && "declaration ID out-of-range for AST file");
7404     Error("declaration ID out-of-range for AST file");
7405     return nullptr;
7406   }
7407 
7408   return DeclsLoaded[Index];
7409 }
7410 
7411 Decl *ASTReader::GetDecl(DeclID ID) {
7412   if (ID < NUM_PREDEF_DECL_IDS)
7413     return GetExistingDecl(ID);
7414 
7415   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7416 
7417   if (Index >= DeclsLoaded.size()) {
7418     assert(0 && "declaration ID out-of-range for AST file");
7419     Error("declaration ID out-of-range for AST file");
7420     return nullptr;
7421   }
7422 
7423   if (!DeclsLoaded[Index]) {
7424     ReadDeclRecord(ID);
7425     if (DeserializationListener)
7426       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7427   }
7428 
7429   return DeclsLoaded[Index];
7430 }
7431 
7432 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7433                                                   DeclID GlobalID) {
7434   if (GlobalID < NUM_PREDEF_DECL_IDS)
7435     return GlobalID;
7436 
7437   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7438   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7439   ModuleFile *Owner = I->second;
7440 
7441   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7442     = M.GlobalToLocalDeclIDs.find(Owner);
7443   if (Pos == M.GlobalToLocalDeclIDs.end())
7444     return 0;
7445 
7446   return GlobalID - Owner->BaseDeclID + Pos->second;
7447 }
7448 
7449 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7450                                             const RecordData &Record,
7451                                             unsigned &Idx) {
7452   if (Idx >= Record.size()) {
7453     Error("Corrupted AST file");
7454     return 0;
7455   }
7456 
7457   return getGlobalDeclID(F, Record[Idx++]);
7458 }
7459 
7460 /// Resolve the offset of a statement into a statement.
7461 ///
7462 /// This operation will read a new statement from the external
7463 /// source each time it is called, and is meant to be used via a
7464 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7465 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7466   // Switch case IDs are per Decl.
7467   ClearSwitchCaseIDs();
7468 
7469   // Offset here is a global offset across the entire chain.
7470   RecordLocation Loc = getLocalBitOffset(Offset);
7471   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7472     Error(std::move(Err));
7473     return nullptr;
7474   }
7475   assert(NumCurrentElementsDeserializing == 0 &&
7476          "should not be called while already deserializing");
7477   Deserializing D(this);
7478   return ReadStmtFromStream(*Loc.F);
7479 }
7480 
7481 void ASTReader::FindExternalLexicalDecls(
7482     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7483     SmallVectorImpl<Decl *> &Decls) {
7484   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7485 
7486   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7487     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7488     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7489       auto K = (Decl::Kind)+LexicalDecls[I];
7490       if (!IsKindWeWant(K))
7491         continue;
7492 
7493       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7494 
7495       // Don't add predefined declarations to the lexical context more
7496       // than once.
7497       if (ID < NUM_PREDEF_DECL_IDS) {
7498         if (PredefsVisited[ID])
7499           continue;
7500 
7501         PredefsVisited[ID] = true;
7502       }
7503 
7504       if (Decl *D = GetLocalDecl(*M, ID)) {
7505         assert(D->getKind() == K && "wrong kind for lexical decl");
7506         if (!DC->isDeclInLexicalTraversal(D))
7507           Decls.push_back(D);
7508       }
7509     }
7510   };
7511 
7512   if (isa<TranslationUnitDecl>(DC)) {
7513     for (auto Lexical : TULexicalDecls)
7514       Visit(Lexical.first, Lexical.second);
7515   } else {
7516     auto I = LexicalDecls.find(DC);
7517     if (I != LexicalDecls.end())
7518       Visit(I->second.first, I->second.second);
7519   }
7520 
7521   ++NumLexicalDeclContextsRead;
7522 }
7523 
7524 namespace {
7525 
7526 class DeclIDComp {
7527   ASTReader &Reader;
7528   ModuleFile &Mod;
7529 
7530 public:
7531   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7532 
7533   bool operator()(LocalDeclID L, LocalDeclID R) const {
7534     SourceLocation LHS = getLocation(L);
7535     SourceLocation RHS = getLocation(R);
7536     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7537   }
7538 
7539   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7540     SourceLocation RHS = getLocation(R);
7541     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7542   }
7543 
7544   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7545     SourceLocation LHS = getLocation(L);
7546     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7547   }
7548 
7549   SourceLocation getLocation(LocalDeclID ID) const {
7550     return Reader.getSourceManager().getFileLoc(
7551             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7552   }
7553 };
7554 
7555 } // namespace
7556 
7557 void ASTReader::FindFileRegionDecls(FileID File,
7558                                     unsigned Offset, unsigned Length,
7559                                     SmallVectorImpl<Decl *> &Decls) {
7560   SourceManager &SM = getSourceManager();
7561 
7562   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7563   if (I == FileDeclIDs.end())
7564     return;
7565 
7566   FileDeclsInfo &DInfo = I->second;
7567   if (DInfo.Decls.empty())
7568     return;
7569 
7570   SourceLocation
7571     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7572   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7573 
7574   DeclIDComp DIDComp(*this, *DInfo.Mod);
7575   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7576       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7577   if (BeginIt != DInfo.Decls.begin())
7578     --BeginIt;
7579 
7580   // If we are pointing at a top-level decl inside an objc container, we need
7581   // to backtrack until we find it otherwise we will fail to report that the
7582   // region overlaps with an objc container.
7583   while (BeginIt != DInfo.Decls.begin() &&
7584          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7585              ->isTopLevelDeclInObjCContainer())
7586     --BeginIt;
7587 
7588   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7589       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7590   if (EndIt != DInfo.Decls.end())
7591     ++EndIt;
7592 
7593   for (ArrayRef<serialization::LocalDeclID>::iterator
7594          DIt = BeginIt; DIt != EndIt; ++DIt)
7595     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7596 }
7597 
7598 bool
7599 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7600                                           DeclarationName Name) {
7601   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7602          "DeclContext has no visible decls in storage");
7603   if (!Name)
7604     return false;
7605 
7606   auto It = Lookups.find(DC);
7607   if (It == Lookups.end())
7608     return false;
7609 
7610   Deserializing LookupResults(this);
7611 
7612   // Load the list of declarations.
7613   SmallVector<NamedDecl *, 64> Decls;
7614   for (DeclID ID : It->second.Table.find(Name)) {
7615     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7616     if (ND->getDeclName() == Name)
7617       Decls.push_back(ND);
7618   }
7619 
7620   ++NumVisibleDeclContextsRead;
7621   SetExternalVisibleDeclsForName(DC, Name, Decls);
7622   return !Decls.empty();
7623 }
7624 
7625 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7626   if (!DC->hasExternalVisibleStorage())
7627     return;
7628 
7629   auto It = Lookups.find(DC);
7630   assert(It != Lookups.end() &&
7631          "have external visible storage but no lookup tables");
7632 
7633   DeclsMap Decls;
7634 
7635   for (DeclID ID : It->second.Table.findAll()) {
7636     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7637     Decls[ND->getDeclName()].push_back(ND);
7638   }
7639 
7640   ++NumVisibleDeclContextsRead;
7641 
7642   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7643     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7644   }
7645   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7646 }
7647 
7648 const serialization::reader::DeclContextLookupTable *
7649 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7650   auto I = Lookups.find(Primary);
7651   return I == Lookups.end() ? nullptr : &I->second;
7652 }
7653 
7654 /// Under non-PCH compilation the consumer receives the objc methods
7655 /// before receiving the implementation, and codegen depends on this.
7656 /// We simulate this by deserializing and passing to consumer the methods of the
7657 /// implementation before passing the deserialized implementation decl.
7658 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7659                                        ASTConsumer *Consumer) {
7660   assert(ImplD && Consumer);
7661 
7662   for (auto *I : ImplD->methods())
7663     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7664 
7665   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7666 }
7667 
7668 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7669   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7670     PassObjCImplDeclToConsumer(ImplD, Consumer);
7671   else
7672     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7673 }
7674 
7675 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7676   this->Consumer = Consumer;
7677 
7678   if (Consumer)
7679     PassInterestingDeclsToConsumer();
7680 
7681   if (DeserializationListener)
7682     DeserializationListener->ReaderInitialized(this);
7683 }
7684 
7685 void ASTReader::PrintStats() {
7686   std::fprintf(stderr, "*** AST File Statistics:\n");
7687 
7688   unsigned NumTypesLoaded
7689     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7690                                       QualType());
7691   unsigned NumDeclsLoaded
7692     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7693                                       (Decl *)nullptr);
7694   unsigned NumIdentifiersLoaded
7695     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7696                                             IdentifiersLoaded.end(),
7697                                             (IdentifierInfo *)nullptr);
7698   unsigned NumMacrosLoaded
7699     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7700                                        MacrosLoaded.end(),
7701                                        (MacroInfo *)nullptr);
7702   unsigned NumSelectorsLoaded
7703     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7704                                           SelectorsLoaded.end(),
7705                                           Selector());
7706 
7707   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7708     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7709                  NumSLocEntriesRead, TotalNumSLocEntries,
7710                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7711   if (!TypesLoaded.empty())
7712     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7713                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7714                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7715   if (!DeclsLoaded.empty())
7716     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7717                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7718                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7719   if (!IdentifiersLoaded.empty())
7720     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7721                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7722                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7723   if (!MacrosLoaded.empty())
7724     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7725                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7726                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7727   if (!SelectorsLoaded.empty())
7728     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7729                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7730                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7731   if (TotalNumStatements)
7732     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7733                  NumStatementsRead, TotalNumStatements,
7734                  ((float)NumStatementsRead/TotalNumStatements * 100));
7735   if (TotalNumMacros)
7736     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7737                  NumMacrosRead, TotalNumMacros,
7738                  ((float)NumMacrosRead/TotalNumMacros * 100));
7739   if (TotalLexicalDeclContexts)
7740     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7741                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7742                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7743                   * 100));
7744   if (TotalVisibleDeclContexts)
7745     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7746                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7747                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7748                   * 100));
7749   if (TotalNumMethodPoolEntries)
7750     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7751                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7752                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7753                   * 100));
7754   if (NumMethodPoolLookups)
7755     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7756                  NumMethodPoolHits, NumMethodPoolLookups,
7757                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7758   if (NumMethodPoolTableLookups)
7759     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7760                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7761                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7762                   * 100.0));
7763   if (NumIdentifierLookupHits)
7764     std::fprintf(stderr,
7765                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7766                  NumIdentifierLookupHits, NumIdentifierLookups,
7767                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7768 
7769   if (GlobalIndex) {
7770     std::fprintf(stderr, "\n");
7771     GlobalIndex->printStats();
7772   }
7773 
7774   std::fprintf(stderr, "\n");
7775   dump();
7776   std::fprintf(stderr, "\n");
7777 }
7778 
7779 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7780 LLVM_DUMP_METHOD static void
7781 dumpModuleIDMap(StringRef Name,
7782                 const ContinuousRangeMap<Key, ModuleFile *,
7783                                          InitialCapacity> &Map) {
7784   if (Map.begin() == Map.end())
7785     return;
7786 
7787   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7788 
7789   llvm::errs() << Name << ":\n";
7790   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7791        I != IEnd; ++I) {
7792     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7793       << "\n";
7794   }
7795 }
7796 
7797 LLVM_DUMP_METHOD void ASTReader::dump() {
7798   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7799   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7800   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7801   dumpModuleIDMap("Global type map", GlobalTypeMap);
7802   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7803   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7804   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7805   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7806   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7807   dumpModuleIDMap("Global preprocessed entity map",
7808                   GlobalPreprocessedEntityMap);
7809 
7810   llvm::errs() << "\n*** PCH/Modules Loaded:";
7811   for (ModuleFile &M : ModuleMgr)
7812     M.dump();
7813 }
7814 
7815 /// Return the amount of memory used by memory buffers, breaking down
7816 /// by heap-backed versus mmap'ed memory.
7817 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7818   for (ModuleFile &I : ModuleMgr) {
7819     if (llvm::MemoryBuffer *buf = I.Buffer) {
7820       size_t bytes = buf->getBufferSize();
7821       switch (buf->getBufferKind()) {
7822         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7823           sizes.malloc_bytes += bytes;
7824           break;
7825         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7826           sizes.mmap_bytes += bytes;
7827           break;
7828       }
7829     }
7830   }
7831 }
7832 
7833 void ASTReader::InitializeSema(Sema &S) {
7834   SemaObj = &S;
7835   S.addExternalSource(this);
7836 
7837   // Makes sure any declarations that were deserialized "too early"
7838   // still get added to the identifier's declaration chains.
7839   for (uint64_t ID : PreloadedDeclIDs) {
7840     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7841     pushExternalDeclIntoScope(D, D->getDeclName());
7842   }
7843   PreloadedDeclIDs.clear();
7844 
7845   // FIXME: What happens if these are changed by a module import?
7846   if (!FPPragmaOptions.empty()) {
7847     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7848     FPOptionsOverride NewOverrides =
7849         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7850     SemaObj->CurFPFeatures =
7851         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7852   }
7853 
7854   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7855   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7856   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7857 
7858   UpdateSema();
7859 }
7860 
7861 void ASTReader::UpdateSema() {
7862   assert(SemaObj && "no Sema to update");
7863 
7864   // Load the offsets of the declarations that Sema references.
7865   // They will be lazily deserialized when needed.
7866   if (!SemaDeclRefs.empty()) {
7867     assert(SemaDeclRefs.size() % 3 == 0);
7868     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7869       if (!SemaObj->StdNamespace)
7870         SemaObj->StdNamespace = SemaDeclRefs[I];
7871       if (!SemaObj->StdBadAlloc)
7872         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7873       if (!SemaObj->StdAlignValT)
7874         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7875     }
7876     SemaDeclRefs.clear();
7877   }
7878 
7879   // Update the state of pragmas. Use the same API as if we had encountered the
7880   // pragma in the source.
7881   if(OptimizeOffPragmaLocation.isValid())
7882     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7883   if (PragmaMSStructState != -1)
7884     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7885   if (PointersToMembersPragmaLocation.isValid()) {
7886     SemaObj->ActOnPragmaMSPointersToMembers(
7887         (LangOptions::PragmaMSPointersToMembersKind)
7888             PragmaMSPointersToMembersState,
7889         PointersToMembersPragmaLocation);
7890   }
7891   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7892 
7893   if (PragmaPackCurrentValue) {
7894     // The bottom of the stack might have a default value. It must be adjusted
7895     // to the current value to ensure that the packing state is preserved after
7896     // popping entries that were included/imported from a PCH/module.
7897     bool DropFirst = false;
7898     if (!PragmaPackStack.empty() &&
7899         PragmaPackStack.front().Location.isInvalid()) {
7900       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7901              "Expected a default alignment value");
7902       SemaObj->PackStack.Stack.emplace_back(
7903           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7904           SemaObj->PackStack.CurrentPragmaLocation,
7905           PragmaPackStack.front().PushLocation);
7906       DropFirst = true;
7907     }
7908     for (const auto &Entry :
7909          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7910       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7911                                             Entry.Location, Entry.PushLocation);
7912     if (PragmaPackCurrentLocation.isInvalid()) {
7913       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7914              "Expected a default alignment value");
7915       // Keep the current values.
7916     } else {
7917       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7918       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7919     }
7920   }
7921   if (FpPragmaCurrentValue) {
7922     // The bottom of the stack might have a default value. It must be adjusted
7923     // to the current value to ensure that fp-pragma state is preserved after
7924     // popping entries that were included/imported from a PCH/module.
7925     bool DropFirst = false;
7926     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7927       assert(FpPragmaStack.front().Value ==
7928                  SemaObj->FpPragmaStack.DefaultValue &&
7929              "Expected a default pragma float_control value");
7930       SemaObj->FpPragmaStack.Stack.emplace_back(
7931           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7932           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7933           FpPragmaStack.front().PushLocation);
7934       DropFirst = true;
7935     }
7936     for (const auto &Entry :
7937          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7938       SemaObj->FpPragmaStack.Stack.emplace_back(
7939           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7940     if (FpPragmaCurrentLocation.isInvalid()) {
7941       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7942              "Expected a default pragma float_control value");
7943       // Keep the current values.
7944     } else {
7945       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7946       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7947     }
7948   }
7949 
7950   // For non-modular AST files, restore visiblity of modules.
7951   for (auto &Import : ImportedModules) {
7952     if (Import.ImportLoc.isInvalid())
7953       continue;
7954     if (Module *Imported = getSubmodule(Import.ID)) {
7955       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7956     }
7957   }
7958 }
7959 
7960 IdentifierInfo *ASTReader::get(StringRef Name) {
7961   // Note that we are loading an identifier.
7962   Deserializing AnIdentifier(this);
7963 
7964   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7965                                   NumIdentifierLookups,
7966                                   NumIdentifierLookupHits);
7967 
7968   // We don't need to do identifier table lookups in C++ modules (we preload
7969   // all interesting declarations, and don't need to use the scope for name
7970   // lookups). Perform the lookup in PCH files, though, since we don't build
7971   // a complete initial identifier table if we're carrying on from a PCH.
7972   if (PP.getLangOpts().CPlusPlus) {
7973     for (auto F : ModuleMgr.pch_modules())
7974       if (Visitor(*F))
7975         break;
7976   } else {
7977     // If there is a global index, look there first to determine which modules
7978     // provably do not have any results for this identifier.
7979     GlobalModuleIndex::HitSet Hits;
7980     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7981     if (!loadGlobalIndex()) {
7982       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7983         HitsPtr = &Hits;
7984       }
7985     }
7986 
7987     ModuleMgr.visit(Visitor, HitsPtr);
7988   }
7989 
7990   IdentifierInfo *II = Visitor.getIdentifierInfo();
7991   markIdentifierUpToDate(II);
7992   return II;
7993 }
7994 
7995 namespace clang {
7996 
7997   /// An identifier-lookup iterator that enumerates all of the
7998   /// identifiers stored within a set of AST files.
7999   class ASTIdentifierIterator : public IdentifierIterator {
8000     /// The AST reader whose identifiers are being enumerated.
8001     const ASTReader &Reader;
8002 
8003     /// The current index into the chain of AST files stored in
8004     /// the AST reader.
8005     unsigned Index;
8006 
8007     /// The current position within the identifier lookup table
8008     /// of the current AST file.
8009     ASTIdentifierLookupTable::key_iterator Current;
8010 
8011     /// The end position within the identifier lookup table of
8012     /// the current AST file.
8013     ASTIdentifierLookupTable::key_iterator End;
8014 
8015     /// Whether to skip any modules in the ASTReader.
8016     bool SkipModules;
8017 
8018   public:
8019     explicit ASTIdentifierIterator(const ASTReader &Reader,
8020                                    bool SkipModules = false);
8021 
8022     StringRef Next() override;
8023   };
8024 
8025 } // namespace clang
8026 
8027 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8028                                              bool SkipModules)
8029     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8030 }
8031 
8032 StringRef ASTIdentifierIterator::Next() {
8033   while (Current == End) {
8034     // If we have exhausted all of our AST files, we're done.
8035     if (Index == 0)
8036       return StringRef();
8037 
8038     --Index;
8039     ModuleFile &F = Reader.ModuleMgr[Index];
8040     if (SkipModules && F.isModule())
8041       continue;
8042 
8043     ASTIdentifierLookupTable *IdTable =
8044         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8045     Current = IdTable->key_begin();
8046     End = IdTable->key_end();
8047   }
8048 
8049   // We have any identifiers remaining in the current AST file; return
8050   // the next one.
8051   StringRef Result = *Current;
8052   ++Current;
8053   return Result;
8054 }
8055 
8056 namespace {
8057 
8058 /// A utility for appending two IdentifierIterators.
8059 class ChainedIdentifierIterator : public IdentifierIterator {
8060   std::unique_ptr<IdentifierIterator> Current;
8061   std::unique_ptr<IdentifierIterator> Queued;
8062 
8063 public:
8064   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8065                             std::unique_ptr<IdentifierIterator> Second)
8066       : Current(std::move(First)), Queued(std::move(Second)) {}
8067 
8068   StringRef Next() override {
8069     if (!Current)
8070       return StringRef();
8071 
8072     StringRef result = Current->Next();
8073     if (!result.empty())
8074       return result;
8075 
8076     // Try the queued iterator, which may itself be empty.
8077     Current.reset();
8078     std::swap(Current, Queued);
8079     return Next();
8080   }
8081 };
8082 
8083 } // namespace
8084 
8085 IdentifierIterator *ASTReader::getIdentifiers() {
8086   if (!loadGlobalIndex()) {
8087     std::unique_ptr<IdentifierIterator> ReaderIter(
8088         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8089     std::unique_ptr<IdentifierIterator> ModulesIter(
8090         GlobalIndex->createIdentifierIterator());
8091     return new ChainedIdentifierIterator(std::move(ReaderIter),
8092                                          std::move(ModulesIter));
8093   }
8094 
8095   return new ASTIdentifierIterator(*this);
8096 }
8097 
8098 namespace clang {
8099 namespace serialization {
8100 
8101   class ReadMethodPoolVisitor {
8102     ASTReader &Reader;
8103     Selector Sel;
8104     unsigned PriorGeneration;
8105     unsigned InstanceBits = 0;
8106     unsigned FactoryBits = 0;
8107     bool InstanceHasMoreThanOneDecl = false;
8108     bool FactoryHasMoreThanOneDecl = false;
8109     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8110     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8111 
8112   public:
8113     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8114                           unsigned PriorGeneration)
8115         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8116 
8117     bool operator()(ModuleFile &M) {
8118       if (!M.SelectorLookupTable)
8119         return false;
8120 
8121       // If we've already searched this module file, skip it now.
8122       if (M.Generation <= PriorGeneration)
8123         return true;
8124 
8125       ++Reader.NumMethodPoolTableLookups;
8126       ASTSelectorLookupTable *PoolTable
8127         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8128       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8129       if (Pos == PoolTable->end())
8130         return false;
8131 
8132       ++Reader.NumMethodPoolTableHits;
8133       ++Reader.NumSelectorsRead;
8134       // FIXME: Not quite happy with the statistics here. We probably should
8135       // disable this tracking when called via LoadSelector.
8136       // Also, should entries without methods count as misses?
8137       ++Reader.NumMethodPoolEntriesRead;
8138       ASTSelectorLookupTrait::data_type Data = *Pos;
8139       if (Reader.DeserializationListener)
8140         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8141 
8142       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8143       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8144       InstanceBits = Data.InstanceBits;
8145       FactoryBits = Data.FactoryBits;
8146       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8147       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8148       return true;
8149     }
8150 
8151     /// Retrieve the instance methods found by this visitor.
8152     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8153       return InstanceMethods;
8154     }
8155 
8156     /// Retrieve the instance methods found by this visitor.
8157     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8158       return FactoryMethods;
8159     }
8160 
8161     unsigned getInstanceBits() const { return InstanceBits; }
8162     unsigned getFactoryBits() const { return FactoryBits; }
8163 
8164     bool instanceHasMoreThanOneDecl() const {
8165       return InstanceHasMoreThanOneDecl;
8166     }
8167 
8168     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8169   };
8170 
8171 } // namespace serialization
8172 } // namespace clang
8173 
8174 /// Add the given set of methods to the method list.
8175 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8176                              ObjCMethodList &List) {
8177   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8178     S.addMethodToGlobalList(&List, Methods[I]);
8179   }
8180 }
8181 
8182 void ASTReader::ReadMethodPool(Selector Sel) {
8183   // Get the selector generation and update it to the current generation.
8184   unsigned &Generation = SelectorGeneration[Sel];
8185   unsigned PriorGeneration = Generation;
8186   Generation = getGeneration();
8187   SelectorOutOfDate[Sel] = false;
8188 
8189   // Search for methods defined with this selector.
8190   ++NumMethodPoolLookups;
8191   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8192   ModuleMgr.visit(Visitor);
8193 
8194   if (Visitor.getInstanceMethods().empty() &&
8195       Visitor.getFactoryMethods().empty())
8196     return;
8197 
8198   ++NumMethodPoolHits;
8199 
8200   if (!getSema())
8201     return;
8202 
8203   Sema &S = *getSema();
8204   Sema::GlobalMethodPool::iterator Pos
8205     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8206 
8207   Pos->second.first.setBits(Visitor.getInstanceBits());
8208   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8209   Pos->second.second.setBits(Visitor.getFactoryBits());
8210   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8211 
8212   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8213   // when building a module we keep every method individually and may need to
8214   // update hasMoreThanOneDecl as we add the methods.
8215   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8216   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8217 }
8218 
8219 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8220   if (SelectorOutOfDate[Sel])
8221     ReadMethodPool(Sel);
8222 }
8223 
8224 void ASTReader::ReadKnownNamespaces(
8225                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8226   Namespaces.clear();
8227 
8228   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8229     if (NamespaceDecl *Namespace
8230                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8231       Namespaces.push_back(Namespace);
8232   }
8233 }
8234 
8235 void ASTReader::ReadUndefinedButUsed(
8236     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8237   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8238     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8239     SourceLocation Loc =
8240         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8241     Undefined.insert(std::make_pair(D, Loc));
8242   }
8243 }
8244 
8245 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8246     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8247                                                      Exprs) {
8248   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8249     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8250     uint64_t Count = DelayedDeleteExprs[Idx++];
8251     for (uint64_t C = 0; C < Count; ++C) {
8252       SourceLocation DeleteLoc =
8253           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8254       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8255       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8256     }
8257   }
8258 }
8259 
8260 void ASTReader::ReadTentativeDefinitions(
8261                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8262   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8263     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8264     if (Var)
8265       TentativeDefs.push_back(Var);
8266   }
8267   TentativeDefinitions.clear();
8268 }
8269 
8270 void ASTReader::ReadUnusedFileScopedDecls(
8271                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8272   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8273     DeclaratorDecl *D
8274       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8275     if (D)
8276       Decls.push_back(D);
8277   }
8278   UnusedFileScopedDecls.clear();
8279 }
8280 
8281 void ASTReader::ReadDelegatingConstructors(
8282                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8283   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8284     CXXConstructorDecl *D
8285       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8286     if (D)
8287       Decls.push_back(D);
8288   }
8289   DelegatingCtorDecls.clear();
8290 }
8291 
8292 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8293   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8294     TypedefNameDecl *D
8295       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8296     if (D)
8297       Decls.push_back(D);
8298   }
8299   ExtVectorDecls.clear();
8300 }
8301 
8302 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8303     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8304   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8305        ++I) {
8306     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8307         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8308     if (D)
8309       Decls.insert(D);
8310   }
8311   UnusedLocalTypedefNameCandidates.clear();
8312 }
8313 
8314 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8315     llvm::SmallVector<Decl *, 4> &Decls) {
8316   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8317        ++I) {
8318     auto *D = dyn_cast_or_null<Decl>(
8319         GetDecl(DeclsToCheckForDeferredDiags[I]));
8320     if (D)
8321       Decls.push_back(D);
8322   }
8323   DeclsToCheckForDeferredDiags.clear();
8324 }
8325 
8326 
8327 void ASTReader::ReadReferencedSelectors(
8328        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8329   if (ReferencedSelectorsData.empty())
8330     return;
8331 
8332   // If there are @selector references added them to its pool. This is for
8333   // implementation of -Wselector.
8334   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8335   unsigned I = 0;
8336   while (I < DataSize) {
8337     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8338     SourceLocation SelLoc
8339       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8340     Sels.push_back(std::make_pair(Sel, SelLoc));
8341   }
8342   ReferencedSelectorsData.clear();
8343 }
8344 
8345 void ASTReader::ReadWeakUndeclaredIdentifiers(
8346        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8347   if (WeakUndeclaredIdentifiers.empty())
8348     return;
8349 
8350   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8351     IdentifierInfo *WeakId
8352       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8353     IdentifierInfo *AliasId
8354       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8355     SourceLocation Loc
8356       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8357     bool Used = WeakUndeclaredIdentifiers[I++];
8358     WeakInfo WI(AliasId, Loc);
8359     WI.setUsed(Used);
8360     WeakIDs.push_back(std::make_pair(WeakId, WI));
8361   }
8362   WeakUndeclaredIdentifiers.clear();
8363 }
8364 
8365 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8366   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8367     ExternalVTableUse VT;
8368     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8369     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8370     VT.DefinitionRequired = VTableUses[Idx++];
8371     VTables.push_back(VT);
8372   }
8373 
8374   VTableUses.clear();
8375 }
8376 
8377 void ASTReader::ReadPendingInstantiations(
8378        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8379   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8380     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8381     SourceLocation Loc
8382       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8383 
8384     Pending.push_back(std::make_pair(D, Loc));
8385   }
8386   PendingInstantiations.clear();
8387 }
8388 
8389 void ASTReader::ReadLateParsedTemplates(
8390     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8391         &LPTMap) {
8392   for (auto &LPT : LateParsedTemplates) {
8393     ModuleFile *FMod = LPT.first;
8394     RecordDataImpl &LateParsed = LPT.second;
8395     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8396          /* In loop */) {
8397       FunctionDecl *FD =
8398           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8399 
8400       auto LT = std::make_unique<LateParsedTemplate>();
8401       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8402 
8403       ModuleFile *F = getOwningModuleFile(LT->D);
8404       assert(F && "No module");
8405 
8406       unsigned TokN = LateParsed[Idx++];
8407       LT->Toks.reserve(TokN);
8408       for (unsigned T = 0; T < TokN; ++T)
8409         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8410 
8411       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8412     }
8413   }
8414 }
8415 
8416 void ASTReader::LoadSelector(Selector Sel) {
8417   // It would be complicated to avoid reading the methods anyway. So don't.
8418   ReadMethodPool(Sel);
8419 }
8420 
8421 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8422   assert(ID && "Non-zero identifier ID required");
8423   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8424   IdentifiersLoaded[ID - 1] = II;
8425   if (DeserializationListener)
8426     DeserializationListener->IdentifierRead(ID, II);
8427 }
8428 
8429 /// Set the globally-visible declarations associated with the given
8430 /// identifier.
8431 ///
8432 /// If the AST reader is currently in a state where the given declaration IDs
8433 /// cannot safely be resolved, they are queued until it is safe to resolve
8434 /// them.
8435 ///
8436 /// \param II an IdentifierInfo that refers to one or more globally-visible
8437 /// declarations.
8438 ///
8439 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8440 /// visible at global scope.
8441 ///
8442 /// \param Decls if non-null, this vector will be populated with the set of
8443 /// deserialized declarations. These declarations will not be pushed into
8444 /// scope.
8445 void
8446 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8447                               const SmallVectorImpl<uint32_t> &DeclIDs,
8448                                    SmallVectorImpl<Decl *> *Decls) {
8449   if (NumCurrentElementsDeserializing && !Decls) {
8450     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8451     return;
8452   }
8453 
8454   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8455     if (!SemaObj) {
8456       // Queue this declaration so that it will be added to the
8457       // translation unit scope and identifier's declaration chain
8458       // once a Sema object is known.
8459       PreloadedDeclIDs.push_back(DeclIDs[I]);
8460       continue;
8461     }
8462 
8463     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8464 
8465     // If we're simply supposed to record the declarations, do so now.
8466     if (Decls) {
8467       Decls->push_back(D);
8468       continue;
8469     }
8470 
8471     // Introduce this declaration into the translation-unit scope
8472     // and add it to the declaration chain for this identifier, so
8473     // that (unqualified) name lookup will find it.
8474     pushExternalDeclIntoScope(D, II);
8475   }
8476 }
8477 
8478 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8479   if (ID == 0)
8480     return nullptr;
8481 
8482   if (IdentifiersLoaded.empty()) {
8483     Error("no identifier table in AST file");
8484     return nullptr;
8485   }
8486 
8487   ID -= 1;
8488   if (!IdentifiersLoaded[ID]) {
8489     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8490     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8491     ModuleFile *M = I->second;
8492     unsigned Index = ID - M->BaseIdentifierID;
8493     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8494 
8495     // All of the strings in the AST file are preceded by a 16-bit length.
8496     // Extract that 16-bit length to avoid having to execute strlen().
8497     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8498     //  unsigned integers.  This is important to avoid integer overflow when
8499     //  we cast them to 'unsigned'.
8500     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8501     unsigned StrLen = (((unsigned) StrLenPtr[0])
8502                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8503     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8504     IdentifiersLoaded[ID] = &II;
8505     markIdentifierFromAST(*this,  II);
8506     if (DeserializationListener)
8507       DeserializationListener->IdentifierRead(ID + 1, &II);
8508   }
8509 
8510   return IdentifiersLoaded[ID];
8511 }
8512 
8513 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8514   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8515 }
8516 
8517 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8518   if (LocalID < NUM_PREDEF_IDENT_IDS)
8519     return LocalID;
8520 
8521   if (!M.ModuleOffsetMap.empty())
8522     ReadModuleOffsetMap(M);
8523 
8524   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8525     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8526   assert(I != M.IdentifierRemap.end()
8527          && "Invalid index into identifier index remap");
8528 
8529   return LocalID + I->second;
8530 }
8531 
8532 MacroInfo *ASTReader::getMacro(MacroID ID) {
8533   if (ID == 0)
8534     return nullptr;
8535 
8536   if (MacrosLoaded.empty()) {
8537     Error("no macro table in AST file");
8538     return nullptr;
8539   }
8540 
8541   ID -= NUM_PREDEF_MACRO_IDS;
8542   if (!MacrosLoaded[ID]) {
8543     GlobalMacroMapType::iterator I
8544       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8545     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8546     ModuleFile *M = I->second;
8547     unsigned Index = ID - M->BaseMacroID;
8548     MacrosLoaded[ID] =
8549         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8550 
8551     if (DeserializationListener)
8552       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8553                                          MacrosLoaded[ID]);
8554   }
8555 
8556   return MacrosLoaded[ID];
8557 }
8558 
8559 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8560   if (LocalID < NUM_PREDEF_MACRO_IDS)
8561     return LocalID;
8562 
8563   if (!M.ModuleOffsetMap.empty())
8564     ReadModuleOffsetMap(M);
8565 
8566   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8567     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8568   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8569 
8570   return LocalID + I->second;
8571 }
8572 
8573 serialization::SubmoduleID
8574 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8575   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8576     return LocalID;
8577 
8578   if (!M.ModuleOffsetMap.empty())
8579     ReadModuleOffsetMap(M);
8580 
8581   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8582     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8583   assert(I != M.SubmoduleRemap.end()
8584          && "Invalid index into submodule index remap");
8585 
8586   return LocalID + I->second;
8587 }
8588 
8589 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8590   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8591     assert(GlobalID == 0 && "Unhandled global submodule ID");
8592     return nullptr;
8593   }
8594 
8595   if (GlobalID > SubmodulesLoaded.size()) {
8596     Error("submodule ID out of range in AST file");
8597     return nullptr;
8598   }
8599 
8600   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8601 }
8602 
8603 Module *ASTReader::getModule(unsigned ID) {
8604   return getSubmodule(ID);
8605 }
8606 
8607 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8608   if (ID & 1) {
8609     // It's a module, look it up by submodule ID.
8610     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8611     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8612   } else {
8613     // It's a prefix (preamble, PCH, ...). Look it up by index.
8614     unsigned IndexFromEnd = ID >> 1;
8615     assert(IndexFromEnd && "got reference to unknown module file");
8616     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8617   }
8618 }
8619 
8620 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8621   if (!F)
8622     return 1;
8623 
8624   // For a file representing a module, use the submodule ID of the top-level
8625   // module as the file ID. For any other kind of file, the number of such
8626   // files loaded beforehand will be the same on reload.
8627   // FIXME: Is this true even if we have an explicit module file and a PCH?
8628   if (F->isModule())
8629     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8630 
8631   auto PCHModules = getModuleManager().pch_modules();
8632   auto I = llvm::find(PCHModules, F);
8633   assert(I != PCHModules.end() && "emitting reference to unknown file");
8634   return (I - PCHModules.end()) << 1;
8635 }
8636 
8637 llvm::Optional<ASTSourceDescriptor>
8638 ASTReader::getSourceDescriptor(unsigned ID) {
8639   if (Module *M = getSubmodule(ID))
8640     return ASTSourceDescriptor(*M);
8641 
8642   // If there is only a single PCH, return it instead.
8643   // Chained PCH are not supported.
8644   const auto &PCHChain = ModuleMgr.pch_modules();
8645   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8646     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8647     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8648     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8649     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8650                                MF.Signature);
8651   }
8652   return None;
8653 }
8654 
8655 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8656   auto I = DefinitionSource.find(FD);
8657   if (I == DefinitionSource.end())
8658     return EK_ReplyHazy;
8659   return I->second ? EK_Never : EK_Always;
8660 }
8661 
8662 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8663   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8664 }
8665 
8666 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8667   if (ID == 0)
8668     return Selector();
8669 
8670   if (ID > SelectorsLoaded.size()) {
8671     Error("selector ID out of range in AST file");
8672     return Selector();
8673   }
8674 
8675   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8676     // Load this selector from the selector table.
8677     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8678     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8679     ModuleFile &M = *I->second;
8680     ASTSelectorLookupTrait Trait(*this, M);
8681     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8682     SelectorsLoaded[ID - 1] =
8683       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8684     if (DeserializationListener)
8685       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8686   }
8687 
8688   return SelectorsLoaded[ID - 1];
8689 }
8690 
8691 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8692   return DecodeSelector(ID);
8693 }
8694 
8695 uint32_t ASTReader::GetNumExternalSelectors() {
8696   // ID 0 (the null selector) is considered an external selector.
8697   return getTotalNumSelectors() + 1;
8698 }
8699 
8700 serialization::SelectorID
8701 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8702   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8703     return LocalID;
8704 
8705   if (!M.ModuleOffsetMap.empty())
8706     ReadModuleOffsetMap(M);
8707 
8708   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8709     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8710   assert(I != M.SelectorRemap.end()
8711          && "Invalid index into selector index remap");
8712 
8713   return LocalID + I->second;
8714 }
8715 
8716 DeclarationNameLoc
8717 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8718   DeclarationNameLoc DNLoc;
8719   switch (Name.getNameKind()) {
8720   case DeclarationName::CXXConstructorName:
8721   case DeclarationName::CXXDestructorName:
8722   case DeclarationName::CXXConversionFunctionName:
8723     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8724     break;
8725 
8726   case DeclarationName::CXXOperatorName:
8727     DNLoc.CXXOperatorName.BeginOpNameLoc
8728       = readSourceLocation().getRawEncoding();
8729     DNLoc.CXXOperatorName.EndOpNameLoc
8730       = readSourceLocation().getRawEncoding();
8731     break;
8732 
8733   case DeclarationName::CXXLiteralOperatorName:
8734     DNLoc.CXXLiteralOperatorName.OpNameLoc
8735       = readSourceLocation().getRawEncoding();
8736     break;
8737 
8738   case DeclarationName::Identifier:
8739   case DeclarationName::ObjCZeroArgSelector:
8740   case DeclarationName::ObjCOneArgSelector:
8741   case DeclarationName::ObjCMultiArgSelector:
8742   case DeclarationName::CXXUsingDirective:
8743   case DeclarationName::CXXDeductionGuideName:
8744     break;
8745   }
8746   return DNLoc;
8747 }
8748 
8749 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8750   DeclarationNameInfo NameInfo;
8751   NameInfo.setName(readDeclarationName());
8752   NameInfo.setLoc(readSourceLocation());
8753   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8754   return NameInfo;
8755 }
8756 
8757 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8758   Info.QualifierLoc = readNestedNameSpecifierLoc();
8759   unsigned NumTPLists = readInt();
8760   Info.NumTemplParamLists = NumTPLists;
8761   if (NumTPLists) {
8762     Info.TemplParamLists =
8763         new (getContext()) TemplateParameterList *[NumTPLists];
8764     for (unsigned i = 0; i != NumTPLists; ++i)
8765       Info.TemplParamLists[i] = readTemplateParameterList();
8766   }
8767 }
8768 
8769 TemplateParameterList *
8770 ASTRecordReader::readTemplateParameterList() {
8771   SourceLocation TemplateLoc = readSourceLocation();
8772   SourceLocation LAngleLoc = readSourceLocation();
8773   SourceLocation RAngleLoc = readSourceLocation();
8774 
8775   unsigned NumParams = readInt();
8776   SmallVector<NamedDecl *, 16> Params;
8777   Params.reserve(NumParams);
8778   while (NumParams--)
8779     Params.push_back(readDeclAs<NamedDecl>());
8780 
8781   bool HasRequiresClause = readBool();
8782   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8783 
8784   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8785       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8786   return TemplateParams;
8787 }
8788 
8789 void ASTRecordReader::readTemplateArgumentList(
8790                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8791                         bool Canonicalize) {
8792   unsigned NumTemplateArgs = readInt();
8793   TemplArgs.reserve(NumTemplateArgs);
8794   while (NumTemplateArgs--)
8795     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8796 }
8797 
8798 /// Read a UnresolvedSet structure.
8799 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8800   unsigned NumDecls = readInt();
8801   Set.reserve(getContext(), NumDecls);
8802   while (NumDecls--) {
8803     DeclID ID = readDeclID();
8804     AccessSpecifier AS = (AccessSpecifier) readInt();
8805     Set.addLazyDecl(getContext(), ID, AS);
8806   }
8807 }
8808 
8809 CXXBaseSpecifier
8810 ASTRecordReader::readCXXBaseSpecifier() {
8811   bool isVirtual = readBool();
8812   bool isBaseOfClass = readBool();
8813   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8814   bool inheritConstructors = readBool();
8815   TypeSourceInfo *TInfo = readTypeSourceInfo();
8816   SourceRange Range = readSourceRange();
8817   SourceLocation EllipsisLoc = readSourceLocation();
8818   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8819                           EllipsisLoc);
8820   Result.setInheritConstructors(inheritConstructors);
8821   return Result;
8822 }
8823 
8824 CXXCtorInitializer **
8825 ASTRecordReader::readCXXCtorInitializers() {
8826   ASTContext &Context = getContext();
8827   unsigned NumInitializers = readInt();
8828   assert(NumInitializers && "wrote ctor initializers but have no inits");
8829   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8830   for (unsigned i = 0; i != NumInitializers; ++i) {
8831     TypeSourceInfo *TInfo = nullptr;
8832     bool IsBaseVirtual = false;
8833     FieldDecl *Member = nullptr;
8834     IndirectFieldDecl *IndirectMember = nullptr;
8835 
8836     CtorInitializerType Type = (CtorInitializerType) readInt();
8837     switch (Type) {
8838     case CTOR_INITIALIZER_BASE:
8839       TInfo = readTypeSourceInfo();
8840       IsBaseVirtual = readBool();
8841       break;
8842 
8843     case CTOR_INITIALIZER_DELEGATING:
8844       TInfo = readTypeSourceInfo();
8845       break;
8846 
8847      case CTOR_INITIALIZER_MEMBER:
8848       Member = readDeclAs<FieldDecl>();
8849       break;
8850 
8851      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8852       IndirectMember = readDeclAs<IndirectFieldDecl>();
8853       break;
8854     }
8855 
8856     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8857     Expr *Init = readExpr();
8858     SourceLocation LParenLoc = readSourceLocation();
8859     SourceLocation RParenLoc = readSourceLocation();
8860 
8861     CXXCtorInitializer *BOMInit;
8862     if (Type == CTOR_INITIALIZER_BASE)
8863       BOMInit = new (Context)
8864           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8865                              RParenLoc, MemberOrEllipsisLoc);
8866     else if (Type == CTOR_INITIALIZER_DELEGATING)
8867       BOMInit = new (Context)
8868           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8869     else if (Member)
8870       BOMInit = new (Context)
8871           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8872                              Init, RParenLoc);
8873     else
8874       BOMInit = new (Context)
8875           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8876                              LParenLoc, Init, RParenLoc);
8877 
8878     if (/*IsWritten*/readBool()) {
8879       unsigned SourceOrder = readInt();
8880       BOMInit->setSourceOrder(SourceOrder);
8881     }
8882 
8883     CtorInitializers[i] = BOMInit;
8884   }
8885 
8886   return CtorInitializers;
8887 }
8888 
8889 NestedNameSpecifierLoc
8890 ASTRecordReader::readNestedNameSpecifierLoc() {
8891   ASTContext &Context = getContext();
8892   unsigned N = readInt();
8893   NestedNameSpecifierLocBuilder Builder;
8894   for (unsigned I = 0; I != N; ++I) {
8895     auto Kind = readNestedNameSpecifierKind();
8896     switch (Kind) {
8897     case NestedNameSpecifier::Identifier: {
8898       IdentifierInfo *II = readIdentifier();
8899       SourceRange Range = readSourceRange();
8900       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8901       break;
8902     }
8903 
8904     case NestedNameSpecifier::Namespace: {
8905       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8906       SourceRange Range = readSourceRange();
8907       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8908       break;
8909     }
8910 
8911     case NestedNameSpecifier::NamespaceAlias: {
8912       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8913       SourceRange Range = readSourceRange();
8914       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8915       break;
8916     }
8917 
8918     case NestedNameSpecifier::TypeSpec:
8919     case NestedNameSpecifier::TypeSpecWithTemplate: {
8920       bool Template = readBool();
8921       TypeSourceInfo *T = readTypeSourceInfo();
8922       if (!T)
8923         return NestedNameSpecifierLoc();
8924       SourceLocation ColonColonLoc = readSourceLocation();
8925 
8926       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8927       Builder.Extend(Context,
8928                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8929                      T->getTypeLoc(), ColonColonLoc);
8930       break;
8931     }
8932 
8933     case NestedNameSpecifier::Global: {
8934       SourceLocation ColonColonLoc = readSourceLocation();
8935       Builder.MakeGlobal(Context, ColonColonLoc);
8936       break;
8937     }
8938 
8939     case NestedNameSpecifier::Super: {
8940       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8941       SourceRange Range = readSourceRange();
8942       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8943       break;
8944     }
8945     }
8946   }
8947 
8948   return Builder.getWithLocInContext(Context);
8949 }
8950 
8951 SourceRange
8952 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8953                            unsigned &Idx) {
8954   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8955   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8956   return SourceRange(beg, end);
8957 }
8958 
8959 static llvm::FixedPointSemantics
8960 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8961                         unsigned &Idx) {
8962   unsigned Width = Record[Idx++];
8963   unsigned Scale = Record[Idx++];
8964   uint64_t Tmp = Record[Idx++];
8965   bool IsSigned = Tmp & 0x1;
8966   bool IsSaturated = Tmp & 0x2;
8967   bool HasUnsignedPadding = Tmp & 0x4;
8968   return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8969                                    HasUnsignedPadding);
8970 }
8971 
8972 APValue ASTRecordReader::readAPValue() {
8973   auto Kind = static_cast<APValue::ValueKind>(asImpl().readUInt32());
8974   switch (Kind) {
8975   case APValue::None:
8976     return APValue();
8977   case APValue::Indeterminate:
8978     return APValue::IndeterminateValue();
8979   case APValue::Int:
8980     return APValue(asImpl().readAPSInt());
8981   case APValue::Float: {
8982     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
8983         static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32()));
8984     return APValue(asImpl().readAPFloat(FloatSema));
8985   }
8986   case APValue::FixedPoint: {
8987     llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8988     return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
8989   }
8990   case APValue::ComplexInt: {
8991     llvm::APSInt First = asImpl().readAPSInt();
8992     return APValue(std::move(First), asImpl().readAPSInt());
8993   }
8994   case APValue::ComplexFloat: {
8995     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
8996         static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32()));
8997     llvm::APFloat First = readAPFloat(FloatSema);
8998     return APValue(std::move(First), asImpl().readAPFloat(FloatSema));
8999   }
9000   case APValue::Vector: {
9001     APValue Result;
9002     Result.MakeVector();
9003     unsigned Length = asImpl().readUInt32();
9004     (void)Result.setVectorUninit(Length);
9005     for (unsigned LoopIdx = 0; LoopIdx < Length; LoopIdx++)
9006       Result.getVectorElt(LoopIdx) = asImpl().readAPValue();
9007     return Result;
9008   }
9009   case APValue::Array: {
9010     APValue Result;
9011     unsigned InitLength = asImpl().readUInt32();
9012     unsigned TotalLength = asImpl().readUInt32();
9013     Result.MakeArray(InitLength, TotalLength);
9014     for (unsigned LoopIdx = 0; LoopIdx < InitLength; LoopIdx++)
9015       Result.getArrayInitializedElt(LoopIdx) = asImpl().readAPValue();
9016     if (Result.hasArrayFiller())
9017       Result.getArrayFiller() = asImpl().readAPValue();
9018     return Result;
9019   }
9020   case APValue::Struct: {
9021     APValue Result;
9022     unsigned BasesLength = asImpl().readUInt32();
9023     unsigned FieldsLength = asImpl().readUInt32();
9024     Result.MakeStruct(BasesLength, FieldsLength);
9025     for (unsigned LoopIdx = 0; LoopIdx < BasesLength; LoopIdx++)
9026       Result.getStructBase(LoopIdx) = asImpl().readAPValue();
9027     for (unsigned LoopIdx = 0; LoopIdx < FieldsLength; LoopIdx++)
9028       Result.getStructField(LoopIdx) = asImpl().readAPValue();
9029     return Result;
9030   }
9031   case APValue::Union: {
9032     auto *FDecl = asImpl().readDeclAs<FieldDecl>();
9033     APValue Value = asImpl().readAPValue();
9034     return APValue(FDecl, std::move(Value));
9035   }
9036   case APValue::AddrLabelDiff: {
9037     auto *LHS = cast<AddrLabelExpr>(asImpl().readExpr());
9038     auto *RHS = cast<AddrLabelExpr>(asImpl().readExpr());
9039     return APValue(LHS, RHS);
9040   }
9041   case APValue::MemberPointer: {
9042     APValue Result;
9043     bool IsDerived = asImpl().readUInt32();
9044     auto *Member = asImpl().readDeclAs<ValueDecl>();
9045     unsigned PathSize = asImpl().readUInt32();
9046     const CXXRecordDecl **PathArray =
9047         Result.setMemberPointerUninit(Member, IsDerived, PathSize).data();
9048     for (unsigned LoopIdx = 0; LoopIdx < PathSize; LoopIdx++)
9049       PathArray[LoopIdx] =
9050           asImpl().readDeclAs<const CXXRecordDecl>()->getCanonicalDecl();
9051     return Result;
9052   }
9053   case APValue::LValue: {
9054     uint64_t Bits = asImpl().readUInt32();
9055     bool HasLValuePath = Bits & 0x1;
9056     bool IsLValueOnePastTheEnd = Bits & 0x2;
9057     bool IsExpr = Bits & 0x4;
9058     bool IsTypeInfo = Bits & 0x8;
9059     bool IsNullPtr = Bits & 0x10;
9060     bool HasBase = Bits & 0x20;
9061     APValue::LValueBase Base;
9062     QualType ElemTy;
9063     assert((!IsExpr || !IsTypeInfo) && "LValueBase cannot be both");
9064     if (HasBase) {
9065       if (!IsTypeInfo) {
9066         unsigned CallIndex = asImpl().readUInt32();
9067         unsigned Version = asImpl().readUInt32();
9068         if (IsExpr) {
9069           Base = APValue::LValueBase(asImpl().readExpr(), CallIndex, Version);
9070           ElemTy = Base.get<const Expr *>()->getType();
9071         } else {
9072           Base = APValue::LValueBase(asImpl().readDeclAs<const ValueDecl>(),
9073                                      CallIndex, Version);
9074           ElemTy = Base.get<const ValueDecl *>()->getType();
9075         }
9076       } else {
9077         QualType TypeInfo = asImpl().readType();
9078         QualType Type = asImpl().readType();
9079         Base = APValue::LValueBase::getTypeInfo(
9080             TypeInfoLValue(TypeInfo.getTypePtr()), Type);
9081         Base.getTypeInfoType();
9082       }
9083     }
9084     CharUnits Offset = CharUnits::fromQuantity(asImpl().readUInt32());
9085     unsigned PathLength = asImpl().readUInt32();
9086     APValue Result;
9087     Result.MakeLValue();
9088     if (HasLValuePath) {
9089       APValue::LValuePathEntry *Path =
9090           Result
9091               .setLValueUninit(Base, Offset, PathLength, IsLValueOnePastTheEnd,
9092                                IsNullPtr)
9093               .data();
9094       for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) {
9095         if (ElemTy->getAs<RecordType>()) {
9096           unsigned Int = asImpl().readUInt32();
9097           Decl *D = asImpl().readDeclAs<Decl>();
9098           if (auto *RD = dyn_cast<CXXRecordDecl>(D))
9099             ElemTy = getASTContext().getRecordType(RD);
9100           else
9101             ElemTy = cast<ValueDecl>(D)->getType();
9102           Path[LoopIdx] =
9103               APValue::LValuePathEntry(APValue::BaseOrMemberType(D, Int));
9104         } else {
9105           ElemTy = getASTContext().getAsArrayType(ElemTy)->getElementType();
9106           Path[LoopIdx] =
9107               APValue::LValuePathEntry::ArrayIndex(asImpl().readUInt32());
9108         }
9109       }
9110     } else
9111       Result.setLValue(Base, Offset, APValue::NoLValuePath{}, IsNullPtr);
9112     return Result;
9113   }
9114   }
9115   llvm_unreachable("Invalid APValue::ValueKind");
9116 }
9117 
9118 /// Read a floating-point value
9119 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9120   return llvm::APFloat(Sem, readAPInt());
9121 }
9122 
9123 // Read a string
9124 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9125   unsigned Len = Record[Idx++];
9126   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9127   Idx += Len;
9128   return Result;
9129 }
9130 
9131 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9132                                 unsigned &Idx) {
9133   std::string Filename = ReadString(Record, Idx);
9134   ResolveImportedPath(F, Filename);
9135   return Filename;
9136 }
9137 
9138 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9139                                 const RecordData &Record, unsigned &Idx) {
9140   std::string Filename = ReadString(Record, Idx);
9141   if (!BaseDirectory.empty())
9142     ResolveImportedPath(Filename, BaseDirectory);
9143   return Filename;
9144 }
9145 
9146 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9147                                          unsigned &Idx) {
9148   unsigned Major = Record[Idx++];
9149   unsigned Minor = Record[Idx++];
9150   unsigned Subminor = Record[Idx++];
9151   if (Minor == 0)
9152     return VersionTuple(Major);
9153   if (Subminor == 0)
9154     return VersionTuple(Major, Minor - 1);
9155   return VersionTuple(Major, Minor - 1, Subminor - 1);
9156 }
9157 
9158 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9159                                           const RecordData &Record,
9160                                           unsigned &Idx) {
9161   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9162   return CXXTemporary::Create(getContext(), Decl);
9163 }
9164 
9165 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9166   return Diag(CurrentImportLoc, DiagID);
9167 }
9168 
9169 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9170   return Diags.Report(Loc, DiagID);
9171 }
9172 
9173 /// Retrieve the identifier table associated with the
9174 /// preprocessor.
9175 IdentifierTable &ASTReader::getIdentifierTable() {
9176   return PP.getIdentifierTable();
9177 }
9178 
9179 /// Record that the given ID maps to the given switch-case
9180 /// statement.
9181 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9182   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9183          "Already have a SwitchCase with this ID");
9184   (*CurrSwitchCaseStmts)[ID] = SC;
9185 }
9186 
9187 /// Retrieve the switch-case statement with the given ID.
9188 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9189   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9190   return (*CurrSwitchCaseStmts)[ID];
9191 }
9192 
9193 void ASTReader::ClearSwitchCaseIDs() {
9194   CurrSwitchCaseStmts->clear();
9195 }
9196 
9197 void ASTReader::ReadComments() {
9198   ASTContext &Context = getContext();
9199   std::vector<RawComment *> Comments;
9200   for (SmallVectorImpl<std::pair<BitstreamCursor,
9201                                  serialization::ModuleFile *>>::iterator
9202        I = CommentsCursors.begin(),
9203        E = CommentsCursors.end();
9204        I != E; ++I) {
9205     Comments.clear();
9206     BitstreamCursor &Cursor = I->first;
9207     serialization::ModuleFile &F = *I->second;
9208     SavedStreamPosition SavedPosition(Cursor);
9209 
9210     RecordData Record;
9211     while (true) {
9212       Expected<llvm::BitstreamEntry> MaybeEntry =
9213           Cursor.advanceSkippingSubblocks(
9214               BitstreamCursor::AF_DontPopBlockAtEnd);
9215       if (!MaybeEntry) {
9216         Error(MaybeEntry.takeError());
9217         return;
9218       }
9219       llvm::BitstreamEntry Entry = MaybeEntry.get();
9220 
9221       switch (Entry.Kind) {
9222       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9223       case llvm::BitstreamEntry::Error:
9224         Error("malformed block record in AST file");
9225         return;
9226       case llvm::BitstreamEntry::EndBlock:
9227         goto NextCursor;
9228       case llvm::BitstreamEntry::Record:
9229         // The interesting case.
9230         break;
9231       }
9232 
9233       // Read a record.
9234       Record.clear();
9235       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9236       if (!MaybeComment) {
9237         Error(MaybeComment.takeError());
9238         return;
9239       }
9240       switch ((CommentRecordTypes)MaybeComment.get()) {
9241       case COMMENTS_RAW_COMMENT: {
9242         unsigned Idx = 0;
9243         SourceRange SR = ReadSourceRange(F, Record, Idx);
9244         RawComment::CommentKind Kind =
9245             (RawComment::CommentKind) Record[Idx++];
9246         bool IsTrailingComment = Record[Idx++];
9247         bool IsAlmostTrailingComment = Record[Idx++];
9248         Comments.push_back(new (Context) RawComment(
9249             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9250         break;
9251       }
9252       }
9253     }
9254   NextCursor:
9255     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9256         FileToOffsetToComment;
9257     for (RawComment *C : Comments) {
9258       SourceLocation CommentLoc = C->getBeginLoc();
9259       if (CommentLoc.isValid()) {
9260         std::pair<FileID, unsigned> Loc =
9261             SourceMgr.getDecomposedLoc(CommentLoc);
9262         if (Loc.first.isValid())
9263           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9264       }
9265     }
9266   }
9267 }
9268 
9269 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9270                                 bool IncludeSystem, bool Complain,
9271                     llvm::function_ref<void(const serialization::InputFile &IF,
9272                                             bool isSystem)> Visitor) {
9273   unsigned NumUserInputs = MF.NumUserInputFiles;
9274   unsigned NumInputs = MF.InputFilesLoaded.size();
9275   assert(NumUserInputs <= NumInputs);
9276   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9277   for (unsigned I = 0; I < N; ++I) {
9278     bool IsSystem = I >= NumUserInputs;
9279     InputFile IF = getInputFile(MF, I+1, Complain);
9280     Visitor(IF, IsSystem);
9281   }
9282 }
9283 
9284 void ASTReader::visitTopLevelModuleMaps(
9285     serialization::ModuleFile &MF,
9286     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9287   unsigned NumInputs = MF.InputFilesLoaded.size();
9288   for (unsigned I = 0; I < NumInputs; ++I) {
9289     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9290     if (IFI.TopLevelModuleMap)
9291       // FIXME: This unnecessarily re-reads the InputFileInfo.
9292       if (auto *FE = getInputFile(MF, I + 1).getFile())
9293         Visitor(FE);
9294   }
9295 }
9296 
9297 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9298   // If we know the owning module, use it.
9299   if (Module *M = D->getImportedOwningModule())
9300     return M->getFullModuleName();
9301 
9302   // Otherwise, use the name of the top-level module the decl is within.
9303   if (ModuleFile *M = getOwningModuleFile(D))
9304     return M->ModuleName;
9305 
9306   // Not from a module.
9307   return {};
9308 }
9309 
9310 void ASTReader::finishPendingActions() {
9311   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9312          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9313          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9314          !PendingUpdateRecords.empty()) {
9315     // If any identifiers with corresponding top-level declarations have
9316     // been loaded, load those declarations now.
9317     using TopLevelDeclsMap =
9318         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9319     TopLevelDeclsMap TopLevelDecls;
9320 
9321     while (!PendingIdentifierInfos.empty()) {
9322       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9323       SmallVector<uint32_t, 4> DeclIDs =
9324           std::move(PendingIdentifierInfos.back().second);
9325       PendingIdentifierInfos.pop_back();
9326 
9327       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9328     }
9329 
9330     // Load each function type that we deferred loading because it was a
9331     // deduced type that might refer to a local type declared within itself.
9332     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9333       auto *FD = PendingFunctionTypes[I].first;
9334       FD->setType(GetType(PendingFunctionTypes[I].second));
9335 
9336       // If we gave a function a deduced return type, remember that we need to
9337       // propagate that along the redeclaration chain.
9338       auto *DT = FD->getReturnType()->getContainedDeducedType();
9339       if (DT && DT->isDeduced())
9340         PendingDeducedTypeUpdates.insert(
9341             {FD->getCanonicalDecl(), FD->getReturnType()});
9342     }
9343     PendingFunctionTypes.clear();
9344 
9345     // For each decl chain that we wanted to complete while deserializing, mark
9346     // it as "still needs to be completed".
9347     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9348       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9349     }
9350     PendingIncompleteDeclChains.clear();
9351 
9352     // Load pending declaration chains.
9353     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9354       loadPendingDeclChain(PendingDeclChains[I].first,
9355                            PendingDeclChains[I].second);
9356     PendingDeclChains.clear();
9357 
9358     // Make the most recent of the top-level declarations visible.
9359     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9360            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9361       IdentifierInfo *II = TLD->first;
9362       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9363         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9364       }
9365     }
9366 
9367     // Load any pending macro definitions.
9368     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9369       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9370       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9371       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9372       // Initialize the macro history from chained-PCHs ahead of module imports.
9373       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9374            ++IDIdx) {
9375         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9376         if (!Info.M->isModule())
9377           resolvePendingMacro(II, Info);
9378       }
9379       // Handle module imports.
9380       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9381            ++IDIdx) {
9382         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9383         if (Info.M->isModule())
9384           resolvePendingMacro(II, Info);
9385       }
9386     }
9387     PendingMacroIDs.clear();
9388 
9389     // Wire up the DeclContexts for Decls that we delayed setting until
9390     // recursive loading is completed.
9391     while (!PendingDeclContextInfos.empty()) {
9392       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9393       PendingDeclContextInfos.pop_front();
9394       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9395       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9396       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9397     }
9398 
9399     // Perform any pending declaration updates.
9400     while (!PendingUpdateRecords.empty()) {
9401       auto Update = PendingUpdateRecords.pop_back_val();
9402       ReadingKindTracker ReadingKind(Read_Decl, *this);
9403       loadDeclUpdateRecords(Update);
9404     }
9405   }
9406 
9407   // At this point, all update records for loaded decls are in place, so any
9408   // fake class definitions should have become real.
9409   assert(PendingFakeDefinitionData.empty() &&
9410          "faked up a class definition but never saw the real one");
9411 
9412   // If we deserialized any C++ or Objective-C class definitions, any
9413   // Objective-C protocol definitions, or any redeclarable templates, make sure
9414   // that all redeclarations point to the definitions. Note that this can only
9415   // happen now, after the redeclaration chains have been fully wired.
9416   for (Decl *D : PendingDefinitions) {
9417     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9418       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9419         // Make sure that the TagType points at the definition.
9420         const_cast<TagType*>(TagT)->decl = TD;
9421       }
9422 
9423       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9424         for (auto *R = getMostRecentExistingDecl(RD); R;
9425              R = R->getPreviousDecl()) {
9426           assert((R == D) ==
9427                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9428                  "declaration thinks it's the definition but it isn't");
9429           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9430         }
9431       }
9432 
9433       continue;
9434     }
9435 
9436     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9437       // Make sure that the ObjCInterfaceType points at the definition.
9438       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9439         ->Decl = ID;
9440 
9441       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9442         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9443 
9444       continue;
9445     }
9446 
9447     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9448       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9449         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9450 
9451       continue;
9452     }
9453 
9454     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9455     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9456       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9457   }
9458   PendingDefinitions.clear();
9459 
9460   // Load the bodies of any functions or methods we've encountered. We do
9461   // this now (delayed) so that we can be sure that the declaration chains
9462   // have been fully wired up (hasBody relies on this).
9463   // FIXME: We shouldn't require complete redeclaration chains here.
9464   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9465                                PBEnd = PendingBodies.end();
9466        PB != PBEnd; ++PB) {
9467     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9468       // For a function defined inline within a class template, force the
9469       // canonical definition to be the one inside the canonical definition of
9470       // the template. This ensures that we instantiate from a correct view
9471       // of the template.
9472       //
9473       // Sadly we can't do this more generally: we can't be sure that all
9474       // copies of an arbitrary class definition will have the same members
9475       // defined (eg, some member functions may not be instantiated, and some
9476       // special members may or may not have been implicitly defined).
9477       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9478         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9479           continue;
9480 
9481       // FIXME: Check for =delete/=default?
9482       // FIXME: Complain about ODR violations here?
9483       const FunctionDecl *Defn = nullptr;
9484       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9485         FD->setLazyBody(PB->second);
9486       } else {
9487         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9488         mergeDefinitionVisibility(NonConstDefn, FD);
9489 
9490         if (!FD->isLateTemplateParsed() &&
9491             !NonConstDefn->isLateTemplateParsed() &&
9492             FD->getODRHash() != NonConstDefn->getODRHash()) {
9493           if (!isa<CXXMethodDecl>(FD)) {
9494             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9495           } else if (FD->getLexicalParent()->isFileContext() &&
9496                      NonConstDefn->getLexicalParent()->isFileContext()) {
9497             // Only diagnose out-of-line method definitions.  If they are
9498             // in class definitions, then an error will be generated when
9499             // processing the class bodies.
9500             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9501           }
9502         }
9503       }
9504       continue;
9505     }
9506 
9507     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9508     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9509       MD->setLazyBody(PB->second);
9510   }
9511   PendingBodies.clear();
9512 
9513   // Do some cleanup.
9514   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9515     getContext().deduplicateMergedDefinitonsFor(ND);
9516   PendingMergedDefinitionsToDeduplicate.clear();
9517 }
9518 
9519 void ASTReader::diagnoseOdrViolations() {
9520   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9521       PendingFunctionOdrMergeFailures.empty() &&
9522       PendingEnumOdrMergeFailures.empty())
9523     return;
9524 
9525   // Trigger the import of the full definition of each class that had any
9526   // odr-merging problems, so we can produce better diagnostics for them.
9527   // These updates may in turn find and diagnose some ODR failures, so take
9528   // ownership of the set first.
9529   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9530   PendingOdrMergeFailures.clear();
9531   for (auto &Merge : OdrMergeFailures) {
9532     Merge.first->buildLookup();
9533     Merge.first->decls_begin();
9534     Merge.first->bases_begin();
9535     Merge.first->vbases_begin();
9536     for (auto &RecordPair : Merge.second) {
9537       auto *RD = RecordPair.first;
9538       RD->decls_begin();
9539       RD->bases_begin();
9540       RD->vbases_begin();
9541     }
9542   }
9543 
9544   // Trigger the import of functions.
9545   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9546   PendingFunctionOdrMergeFailures.clear();
9547   for (auto &Merge : FunctionOdrMergeFailures) {
9548     Merge.first->buildLookup();
9549     Merge.first->decls_begin();
9550     Merge.first->getBody();
9551     for (auto &FD : Merge.second) {
9552       FD->buildLookup();
9553       FD->decls_begin();
9554       FD->getBody();
9555     }
9556   }
9557 
9558   // Trigger the import of enums.
9559   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9560   PendingEnumOdrMergeFailures.clear();
9561   for (auto &Merge : EnumOdrMergeFailures) {
9562     Merge.first->decls_begin();
9563     for (auto &Enum : Merge.second) {
9564       Enum->decls_begin();
9565     }
9566   }
9567 
9568   // For each declaration from a merged context, check that the canonical
9569   // definition of that context also contains a declaration of the same
9570   // entity.
9571   //
9572   // Caution: this loop does things that might invalidate iterators into
9573   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9574   while (!PendingOdrMergeChecks.empty()) {
9575     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9576 
9577     // FIXME: Skip over implicit declarations for now. This matters for things
9578     // like implicitly-declared special member functions. This isn't entirely
9579     // correct; we can end up with multiple unmerged declarations of the same
9580     // implicit entity.
9581     if (D->isImplicit())
9582       continue;
9583 
9584     DeclContext *CanonDef = D->getDeclContext();
9585 
9586     bool Found = false;
9587     const Decl *DCanon = D->getCanonicalDecl();
9588 
9589     for (auto RI : D->redecls()) {
9590       if (RI->getLexicalDeclContext() == CanonDef) {
9591         Found = true;
9592         break;
9593       }
9594     }
9595     if (Found)
9596       continue;
9597 
9598     // Quick check failed, time to do the slow thing. Note, we can't just
9599     // look up the name of D in CanonDef here, because the member that is
9600     // in CanonDef might not be found by name lookup (it might have been
9601     // replaced by a more recent declaration in the lookup table), and we
9602     // can't necessarily find it in the redeclaration chain because it might
9603     // be merely mergeable, not redeclarable.
9604     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9605     for (auto *CanonMember : CanonDef->decls()) {
9606       if (CanonMember->getCanonicalDecl() == DCanon) {
9607         // This can happen if the declaration is merely mergeable and not
9608         // actually redeclarable (we looked for redeclarations earlier).
9609         //
9610         // FIXME: We should be able to detect this more efficiently, without
9611         // pulling in all of the members of CanonDef.
9612         Found = true;
9613         break;
9614       }
9615       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9616         if (ND->getDeclName() == D->getDeclName())
9617           Candidates.push_back(ND);
9618     }
9619 
9620     if (!Found) {
9621       // The AST doesn't like TagDecls becoming invalid after they've been
9622       // completed. We only really need to mark FieldDecls as invalid here.
9623       if (!isa<TagDecl>(D))
9624         D->setInvalidDecl();
9625 
9626       // Ensure we don't accidentally recursively enter deserialization while
9627       // we're producing our diagnostic.
9628       Deserializing RecursionGuard(this);
9629 
9630       std::string CanonDefModule =
9631           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9632       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9633         << D << getOwningModuleNameForDiagnostic(D)
9634         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9635 
9636       if (Candidates.empty())
9637         Diag(cast<Decl>(CanonDef)->getLocation(),
9638              diag::note_module_odr_violation_no_possible_decls) << D;
9639       else {
9640         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9641           Diag(Candidates[I]->getLocation(),
9642                diag::note_module_odr_violation_possible_decl)
9643             << Candidates[I];
9644       }
9645 
9646       DiagnosedOdrMergeFailures.insert(CanonDef);
9647     }
9648   }
9649 
9650   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9651       EnumOdrMergeFailures.empty())
9652     return;
9653 
9654   // Ensure we don't accidentally recursively enter deserialization while
9655   // we're producing our diagnostics.
9656   Deserializing RecursionGuard(this);
9657 
9658   // Common code for hashing helpers.
9659   ODRHash Hash;
9660   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9661     Hash.clear();
9662     Hash.AddQualType(Ty);
9663     return Hash.CalculateHash();
9664   };
9665 
9666   auto ComputeODRHash = [&Hash](const Stmt *S) {
9667     assert(S);
9668     Hash.clear();
9669     Hash.AddStmt(S);
9670     return Hash.CalculateHash();
9671   };
9672 
9673   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9674     assert(D);
9675     Hash.clear();
9676     Hash.AddSubDecl(D);
9677     return Hash.CalculateHash();
9678   };
9679 
9680   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9681     Hash.clear();
9682     Hash.AddTemplateArgument(TA);
9683     return Hash.CalculateHash();
9684   };
9685 
9686   auto ComputeTemplateParameterListODRHash =
9687       [&Hash](const TemplateParameterList *TPL) {
9688         assert(TPL);
9689         Hash.clear();
9690         Hash.AddTemplateParameterList(TPL);
9691         return Hash.CalculateHash();
9692       };
9693 
9694   // Used with err_module_odr_violation_mismatch_decl and
9695   // note_module_odr_violation_mismatch_decl
9696   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9697   enum ODRMismatchDecl {
9698     EndOfClass,
9699     PublicSpecifer,
9700     PrivateSpecifer,
9701     ProtectedSpecifer,
9702     StaticAssert,
9703     Field,
9704     CXXMethod,
9705     TypeAlias,
9706     TypeDef,
9707     Var,
9708     Friend,
9709     FunctionTemplate,
9710     Other
9711   };
9712 
9713   // Used with err_module_odr_violation_mismatch_decl_diff and
9714   // note_module_odr_violation_mismatch_decl_diff
9715   enum ODRMismatchDeclDifference {
9716     StaticAssertCondition,
9717     StaticAssertMessage,
9718     StaticAssertOnlyMessage,
9719     FieldName,
9720     FieldTypeName,
9721     FieldSingleBitField,
9722     FieldDifferentWidthBitField,
9723     FieldSingleMutable,
9724     FieldSingleInitializer,
9725     FieldDifferentInitializers,
9726     MethodName,
9727     MethodDeleted,
9728     MethodDefaulted,
9729     MethodVirtual,
9730     MethodStatic,
9731     MethodVolatile,
9732     MethodConst,
9733     MethodInline,
9734     MethodNumberParameters,
9735     MethodParameterType,
9736     MethodParameterName,
9737     MethodParameterSingleDefaultArgument,
9738     MethodParameterDifferentDefaultArgument,
9739     MethodNoTemplateArguments,
9740     MethodDifferentNumberTemplateArguments,
9741     MethodDifferentTemplateArgument,
9742     MethodSingleBody,
9743     MethodDifferentBody,
9744     TypedefName,
9745     TypedefType,
9746     VarName,
9747     VarType,
9748     VarSingleInitializer,
9749     VarDifferentInitializer,
9750     VarConstexpr,
9751     FriendTypeFunction,
9752     FriendType,
9753     FriendFunction,
9754     FunctionTemplateDifferentNumberParameters,
9755     FunctionTemplateParameterDifferentKind,
9756     FunctionTemplateParameterName,
9757     FunctionTemplateParameterSingleDefaultArgument,
9758     FunctionTemplateParameterDifferentDefaultArgument,
9759     FunctionTemplateParameterDifferentType,
9760     FunctionTemplatePackParameter,
9761   };
9762 
9763   // These lambdas have the common portions of the ODR diagnostics.  This
9764   // has the same return as Diag(), so addition parameters can be passed
9765   // in with operator<<
9766   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9767                                  SourceLocation Loc, SourceRange Range,
9768                                  ODRMismatchDeclDifference DiffType) {
9769     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9770            << FirstRecord << FirstModule.empty() << FirstModule << Range
9771            << DiffType;
9772   };
9773   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9774                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9775     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9776            << SecondModule << Range << DiffType;
9777   };
9778 
9779   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9780                        &ComputeQualTypeODRHash, &ComputeODRHash](
9781                           NamedDecl *FirstRecord, StringRef FirstModule,
9782                           StringRef SecondModule, FieldDecl *FirstField,
9783                           FieldDecl *SecondField) {
9784     IdentifierInfo *FirstII = FirstField->getIdentifier();
9785     IdentifierInfo *SecondII = SecondField->getIdentifier();
9786     if (FirstII->getName() != SecondII->getName()) {
9787       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9788                        FirstField->getSourceRange(), FieldName)
9789           << FirstII;
9790       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9791                       SecondField->getSourceRange(), FieldName)
9792           << SecondII;
9793 
9794       return true;
9795     }
9796 
9797     assert(getContext().hasSameType(FirstField->getType(),
9798                                     SecondField->getType()));
9799 
9800     QualType FirstType = FirstField->getType();
9801     QualType SecondType = SecondField->getType();
9802     if (ComputeQualTypeODRHash(FirstType) !=
9803         ComputeQualTypeODRHash(SecondType)) {
9804       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9805                        FirstField->getSourceRange(), FieldTypeName)
9806           << FirstII << FirstType;
9807       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9808                       SecondField->getSourceRange(), FieldTypeName)
9809           << SecondII << SecondType;
9810 
9811       return true;
9812     }
9813 
9814     const bool IsFirstBitField = FirstField->isBitField();
9815     const bool IsSecondBitField = SecondField->isBitField();
9816     if (IsFirstBitField != IsSecondBitField) {
9817       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9818                        FirstField->getSourceRange(), FieldSingleBitField)
9819           << FirstII << IsFirstBitField;
9820       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9821                       SecondField->getSourceRange(), FieldSingleBitField)
9822           << SecondII << IsSecondBitField;
9823       return true;
9824     }
9825 
9826     if (IsFirstBitField && IsSecondBitField) {
9827       unsigned FirstBitWidthHash =
9828           ComputeODRHash(FirstField->getBitWidth());
9829       unsigned SecondBitWidthHash =
9830           ComputeODRHash(SecondField->getBitWidth());
9831       if (FirstBitWidthHash != SecondBitWidthHash) {
9832         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9833                          FirstField->getSourceRange(),
9834                          FieldDifferentWidthBitField)
9835             << FirstII << FirstField->getBitWidth()->getSourceRange();
9836         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9837                         SecondField->getSourceRange(),
9838                         FieldDifferentWidthBitField)
9839             << SecondII << SecondField->getBitWidth()->getSourceRange();
9840         return true;
9841       }
9842     }
9843 
9844     if (!PP.getLangOpts().CPlusPlus)
9845       return false;
9846 
9847     const bool IsFirstMutable = FirstField->isMutable();
9848     const bool IsSecondMutable = SecondField->isMutable();
9849     if (IsFirstMutable != IsSecondMutable) {
9850       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9851                        FirstField->getSourceRange(), FieldSingleMutable)
9852           << FirstII << IsFirstMutable;
9853       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9854                       SecondField->getSourceRange(), FieldSingleMutable)
9855           << SecondII << IsSecondMutable;
9856       return true;
9857     }
9858 
9859     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9860     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9861     if ((!FirstInitializer && SecondInitializer) ||
9862         (FirstInitializer && !SecondInitializer)) {
9863       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9864                        FirstField->getSourceRange(), FieldSingleInitializer)
9865           << FirstII << (FirstInitializer != nullptr);
9866       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9867                       SecondField->getSourceRange(), FieldSingleInitializer)
9868           << SecondII << (SecondInitializer != nullptr);
9869       return true;
9870     }
9871 
9872     if (FirstInitializer && SecondInitializer) {
9873       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9874       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9875       if (FirstInitHash != SecondInitHash) {
9876         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9877                          FirstField->getSourceRange(),
9878                          FieldDifferentInitializers)
9879             << FirstII << FirstInitializer->getSourceRange();
9880         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9881                         SecondField->getSourceRange(),
9882                         FieldDifferentInitializers)
9883             << SecondII << SecondInitializer->getSourceRange();
9884         return true;
9885       }
9886     }
9887 
9888     return false;
9889   };
9890 
9891   auto ODRDiagTypeDefOrAlias =
9892       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9893           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9894           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9895           bool IsTypeAlias) {
9896         auto FirstName = FirstTD->getDeclName();
9897         auto SecondName = SecondTD->getDeclName();
9898         if (FirstName != SecondName) {
9899           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9900                            FirstTD->getSourceRange(), TypedefName)
9901               << IsTypeAlias << FirstName;
9902           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9903                           SecondTD->getSourceRange(), TypedefName)
9904               << IsTypeAlias << SecondName;
9905           return true;
9906         }
9907 
9908         QualType FirstType = FirstTD->getUnderlyingType();
9909         QualType SecondType = SecondTD->getUnderlyingType();
9910         if (ComputeQualTypeODRHash(FirstType) !=
9911             ComputeQualTypeODRHash(SecondType)) {
9912           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9913                            FirstTD->getSourceRange(), TypedefType)
9914               << IsTypeAlias << FirstName << FirstType;
9915           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9916                           SecondTD->getSourceRange(), TypedefType)
9917               << IsTypeAlias << SecondName << SecondType;
9918           return true;
9919         }
9920 
9921         return false;
9922   };
9923 
9924   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9925                      &ComputeQualTypeODRHash, &ComputeODRHash,
9926                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9927                            StringRef SecondModule, VarDecl *FirstVD,
9928                            VarDecl *SecondVD) {
9929     auto FirstName = FirstVD->getDeclName();
9930     auto SecondName = SecondVD->getDeclName();
9931     if (FirstName != SecondName) {
9932       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9933                        FirstVD->getSourceRange(), VarName)
9934           << FirstName;
9935       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9936                       SecondVD->getSourceRange(), VarName)
9937           << SecondName;
9938       return true;
9939     }
9940 
9941     QualType FirstType = FirstVD->getType();
9942     QualType SecondType = SecondVD->getType();
9943     if (ComputeQualTypeODRHash(FirstType) !=
9944         ComputeQualTypeODRHash(SecondType)) {
9945       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9946                        FirstVD->getSourceRange(), VarType)
9947           << FirstName << FirstType;
9948       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9949                       SecondVD->getSourceRange(), VarType)
9950           << SecondName << SecondType;
9951       return true;
9952     }
9953 
9954     if (!PP.getLangOpts().CPlusPlus)
9955       return false;
9956 
9957     const Expr *FirstInit = FirstVD->getInit();
9958     const Expr *SecondInit = SecondVD->getInit();
9959     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9960       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9961                        FirstVD->getSourceRange(), VarSingleInitializer)
9962           << FirstName << (FirstInit == nullptr)
9963           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9964       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9965                       SecondVD->getSourceRange(), VarSingleInitializer)
9966           << SecondName << (SecondInit == nullptr)
9967           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9968       return true;
9969     }
9970 
9971     if (FirstInit && SecondInit &&
9972         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9973       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9974                        FirstVD->getSourceRange(), VarDifferentInitializer)
9975           << FirstName << FirstInit->getSourceRange();
9976       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9977                       SecondVD->getSourceRange(), VarDifferentInitializer)
9978           << SecondName << SecondInit->getSourceRange();
9979       return true;
9980     }
9981 
9982     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9983     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9984     if (FirstIsConstexpr != SecondIsConstexpr) {
9985       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9986                        FirstVD->getSourceRange(), VarConstexpr)
9987           << FirstName << FirstIsConstexpr;
9988       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9989                       SecondVD->getSourceRange(), VarConstexpr)
9990           << SecondName << SecondIsConstexpr;
9991       return true;
9992     }
9993     return false;
9994   };
9995 
9996   auto DifferenceSelector = [](Decl *D) {
9997     assert(D && "valid Decl required");
9998     switch (D->getKind()) {
9999     default:
10000       return Other;
10001     case Decl::AccessSpec:
10002       switch (D->getAccess()) {
10003       case AS_public:
10004         return PublicSpecifer;
10005       case AS_private:
10006         return PrivateSpecifer;
10007       case AS_protected:
10008         return ProtectedSpecifer;
10009       case AS_none:
10010         break;
10011       }
10012       llvm_unreachable("Invalid access specifier");
10013     case Decl::StaticAssert:
10014       return StaticAssert;
10015     case Decl::Field:
10016       return Field;
10017     case Decl::CXXMethod:
10018     case Decl::CXXConstructor:
10019     case Decl::CXXDestructor:
10020       return CXXMethod;
10021     case Decl::TypeAlias:
10022       return TypeAlias;
10023     case Decl::Typedef:
10024       return TypeDef;
10025     case Decl::Var:
10026       return Var;
10027     case Decl::Friend:
10028       return Friend;
10029     case Decl::FunctionTemplate:
10030       return FunctionTemplate;
10031     }
10032   };
10033 
10034   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10035   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10036                                                  RecordDecl *Record,
10037                                                  const DeclContext *DC) {
10038     for (auto *D : Record->decls()) {
10039       if (!ODRHash::isDeclToBeProcessed(D, DC))
10040         continue;
10041       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10042     }
10043   };
10044 
10045   struct DiffResult {
10046     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
10047     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
10048   };
10049 
10050   // If there is a diagnoseable difference, FirstDiffType and
10051   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10052   // filled in if not EndOfClass.
10053   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
10054                                              DeclHashes &SecondHashes) {
10055     DiffResult DR;
10056     auto FirstIt = FirstHashes.begin();
10057     auto SecondIt = SecondHashes.begin();
10058     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10059       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10060           FirstIt->second == SecondIt->second) {
10061         ++FirstIt;
10062         ++SecondIt;
10063         continue;
10064       }
10065 
10066       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10067       DR.SecondDecl =
10068           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10069 
10070       DR.FirstDiffType =
10071           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
10072       DR.SecondDiffType =
10073           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
10074       return DR;
10075     }
10076     return DR;
10077   };
10078 
10079   // Use this to diagnose that an unexpected Decl was encountered
10080   // or no difference was detected. This causes a generic error
10081   // message to be emitted.
10082   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
10083                                       StringRef FirstModule,
10084                                       NamedDecl *SecondRecord,
10085                                       StringRef SecondModule) {
10086     Diag(FirstRecord->getLocation(),
10087          diag::err_module_odr_violation_different_definitions)
10088         << FirstRecord << FirstModule.empty() << FirstModule;
10089 
10090     if (DR.FirstDecl) {
10091       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
10092           << FirstRecord << DR.FirstDecl->getSourceRange();
10093     }
10094 
10095     Diag(SecondRecord->getLocation(),
10096          diag::note_module_odr_violation_different_definitions)
10097         << SecondModule;
10098 
10099     if (DR.SecondDecl) {
10100       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
10101           << DR.SecondDecl->getSourceRange();
10102     }
10103   };
10104 
10105   auto DiagnoseODRMismatch =
10106       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10107              NamedDecl *SecondRecord, StringRef SecondModule) {
10108         SourceLocation FirstLoc;
10109         SourceRange FirstRange;
10110         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10111         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10112           FirstLoc = FirstTag->getBraceRange().getEnd();
10113         } else {
10114           FirstLoc = DR.FirstDecl->getLocation();
10115           FirstRange = DR.FirstDecl->getSourceRange();
10116         }
10117         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10118             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10119             << DR.FirstDiffType;
10120 
10121         SourceLocation SecondLoc;
10122         SourceRange SecondRange;
10123         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10124         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10125           SecondLoc = SecondTag->getBraceRange().getEnd();
10126         } else {
10127           SecondLoc = DR.SecondDecl->getLocation();
10128           SecondRange = DR.SecondDecl->getSourceRange();
10129         }
10130         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10131             << SecondModule << SecondRange << DR.SecondDiffType;
10132       };
10133 
10134   // Issue any pending ODR-failure diagnostics.
10135   for (auto &Merge : OdrMergeFailures) {
10136     // If we've already pointed out a specific problem with this class, don't
10137     // bother issuing a general "something's different" diagnostic.
10138     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10139       continue;
10140 
10141     bool Diagnosed = false;
10142     CXXRecordDecl *FirstRecord = Merge.first;
10143     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10144     for (auto &RecordPair : Merge.second) {
10145       CXXRecordDecl *SecondRecord = RecordPair.first;
10146       // Multiple different declarations got merged together; tell the user
10147       // where they came from.
10148       if (FirstRecord == SecondRecord)
10149         continue;
10150 
10151       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10152 
10153       auto *FirstDD = FirstRecord->DefinitionData;
10154       auto *SecondDD = RecordPair.second;
10155 
10156       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10157 
10158       // Diagnostics from DefinitionData are emitted here.
10159       if (FirstDD != SecondDD) {
10160         enum ODRDefinitionDataDifference {
10161           NumBases,
10162           NumVBases,
10163           BaseType,
10164           BaseVirtual,
10165           BaseAccess,
10166         };
10167         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10168                                  this](SourceLocation Loc, SourceRange Range,
10169                                        ODRDefinitionDataDifference DiffType) {
10170           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10171                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10172                  << DiffType;
10173         };
10174         auto ODRDiagBaseNote = [&SecondModule,
10175                                 this](SourceLocation Loc, SourceRange Range,
10176                                       ODRDefinitionDataDifference DiffType) {
10177           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10178                  << SecondModule << Range << DiffType;
10179         };
10180 
10181         unsigned FirstNumBases = FirstDD->NumBases;
10182         unsigned FirstNumVBases = FirstDD->NumVBases;
10183         unsigned SecondNumBases = SecondDD->NumBases;
10184         unsigned SecondNumVBases = SecondDD->NumVBases;
10185 
10186         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10187           unsigned NumBases = DD->NumBases;
10188           if (NumBases == 0) return SourceRange();
10189           auto bases = DD->bases();
10190           return SourceRange(bases[0].getBeginLoc(),
10191                              bases[NumBases - 1].getEndLoc());
10192         };
10193 
10194         if (FirstNumBases != SecondNumBases) {
10195           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10196                            NumBases)
10197               << FirstNumBases;
10198           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10199                           NumBases)
10200               << SecondNumBases;
10201           Diagnosed = true;
10202           break;
10203         }
10204 
10205         if (FirstNumVBases != SecondNumVBases) {
10206           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10207                            NumVBases)
10208               << FirstNumVBases;
10209           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10210                           NumVBases)
10211               << SecondNumVBases;
10212           Diagnosed = true;
10213           break;
10214         }
10215 
10216         auto FirstBases = FirstDD->bases();
10217         auto SecondBases = SecondDD->bases();
10218         unsigned i = 0;
10219         for (i = 0; i < FirstNumBases; ++i) {
10220           auto FirstBase = FirstBases[i];
10221           auto SecondBase = SecondBases[i];
10222           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10223               ComputeQualTypeODRHash(SecondBase.getType())) {
10224             ODRDiagBaseError(FirstRecord->getLocation(),
10225                              FirstBase.getSourceRange(), BaseType)
10226                 << (i + 1) << FirstBase.getType();
10227             ODRDiagBaseNote(SecondRecord->getLocation(),
10228                             SecondBase.getSourceRange(), BaseType)
10229                 << (i + 1) << SecondBase.getType();
10230             break;
10231           }
10232 
10233           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10234             ODRDiagBaseError(FirstRecord->getLocation(),
10235                              FirstBase.getSourceRange(), BaseVirtual)
10236                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10237             ODRDiagBaseNote(SecondRecord->getLocation(),
10238                             SecondBase.getSourceRange(), BaseVirtual)
10239                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10240             break;
10241           }
10242 
10243           if (FirstBase.getAccessSpecifierAsWritten() !=
10244               SecondBase.getAccessSpecifierAsWritten()) {
10245             ODRDiagBaseError(FirstRecord->getLocation(),
10246                              FirstBase.getSourceRange(), BaseAccess)
10247                 << (i + 1) << FirstBase.getType()
10248                 << (int)FirstBase.getAccessSpecifierAsWritten();
10249             ODRDiagBaseNote(SecondRecord->getLocation(),
10250                             SecondBase.getSourceRange(), BaseAccess)
10251                 << (i + 1) << SecondBase.getType()
10252                 << (int)SecondBase.getAccessSpecifierAsWritten();
10253             break;
10254           }
10255         }
10256 
10257         if (i != FirstNumBases) {
10258           Diagnosed = true;
10259           break;
10260         }
10261       }
10262 
10263       const ClassTemplateDecl *FirstTemplate =
10264           FirstRecord->getDescribedClassTemplate();
10265       const ClassTemplateDecl *SecondTemplate =
10266           SecondRecord->getDescribedClassTemplate();
10267 
10268       assert(!FirstTemplate == !SecondTemplate &&
10269              "Both pointers should be null or non-null");
10270 
10271       enum ODRTemplateDifference {
10272         ParamEmptyName,
10273         ParamName,
10274         ParamSingleDefaultArgument,
10275         ParamDifferentDefaultArgument,
10276       };
10277 
10278       if (FirstTemplate && SecondTemplate) {
10279         DeclHashes FirstTemplateHashes;
10280         DeclHashes SecondTemplateHashes;
10281 
10282         auto PopulateTemplateParameterHashs =
10283             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10284                                      const ClassTemplateDecl *TD) {
10285               for (auto *D : TD->getTemplateParameters()->asArray()) {
10286                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10287               }
10288             };
10289 
10290         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10291         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10292 
10293         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10294                "Number of template parameters should be equal.");
10295 
10296         auto FirstIt = FirstTemplateHashes.begin();
10297         auto FirstEnd = FirstTemplateHashes.end();
10298         auto SecondIt = SecondTemplateHashes.begin();
10299         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10300           if (FirstIt->second == SecondIt->second)
10301             continue;
10302 
10303           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10304                                           SourceLocation Loc, SourceRange Range,
10305                                           ODRTemplateDifference DiffType) {
10306             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10307                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10308                    << DiffType;
10309           };
10310           auto ODRDiagTemplateNote = [&SecondModule, this](
10311                                          SourceLocation Loc, SourceRange Range,
10312                                          ODRTemplateDifference DiffType) {
10313             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10314                    << SecondModule << Range << DiffType;
10315           };
10316 
10317           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10318           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10319 
10320           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10321                  "Parameter Decl's should be the same kind.");
10322 
10323           DeclarationName FirstName = FirstDecl->getDeclName();
10324           DeclarationName SecondName = SecondDecl->getDeclName();
10325 
10326           if (FirstName != SecondName) {
10327             const bool FirstNameEmpty =
10328                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10329             const bool SecondNameEmpty =
10330                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10331             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10332                    "Both template parameters cannot be unnamed.");
10333             ODRDiagTemplateError(FirstDecl->getLocation(),
10334                                  FirstDecl->getSourceRange(),
10335                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10336                 << FirstName;
10337             ODRDiagTemplateNote(SecondDecl->getLocation(),
10338                                 SecondDecl->getSourceRange(),
10339                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10340                 << SecondName;
10341             break;
10342           }
10343 
10344           switch (FirstDecl->getKind()) {
10345           default:
10346             llvm_unreachable("Invalid template parameter type.");
10347           case Decl::TemplateTypeParm: {
10348             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10349             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10350             const bool HasFirstDefaultArgument =
10351                 FirstParam->hasDefaultArgument() &&
10352                 !FirstParam->defaultArgumentWasInherited();
10353             const bool HasSecondDefaultArgument =
10354                 SecondParam->hasDefaultArgument() &&
10355                 !SecondParam->defaultArgumentWasInherited();
10356 
10357             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10358               ODRDiagTemplateError(FirstDecl->getLocation(),
10359                                    FirstDecl->getSourceRange(),
10360                                    ParamSingleDefaultArgument)
10361                   << HasFirstDefaultArgument;
10362               ODRDiagTemplateNote(SecondDecl->getLocation(),
10363                                   SecondDecl->getSourceRange(),
10364                                   ParamSingleDefaultArgument)
10365                   << HasSecondDefaultArgument;
10366               break;
10367             }
10368 
10369             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10370                    "Expecting default arguments.");
10371 
10372             ODRDiagTemplateError(FirstDecl->getLocation(),
10373                                  FirstDecl->getSourceRange(),
10374                                  ParamDifferentDefaultArgument);
10375             ODRDiagTemplateNote(SecondDecl->getLocation(),
10376                                 SecondDecl->getSourceRange(),
10377                                 ParamDifferentDefaultArgument);
10378 
10379             break;
10380           }
10381           case Decl::NonTypeTemplateParm: {
10382             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10383             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10384             const bool HasFirstDefaultArgument =
10385                 FirstParam->hasDefaultArgument() &&
10386                 !FirstParam->defaultArgumentWasInherited();
10387             const bool HasSecondDefaultArgument =
10388                 SecondParam->hasDefaultArgument() &&
10389                 !SecondParam->defaultArgumentWasInherited();
10390 
10391             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10392               ODRDiagTemplateError(FirstDecl->getLocation(),
10393                                    FirstDecl->getSourceRange(),
10394                                    ParamSingleDefaultArgument)
10395                   << HasFirstDefaultArgument;
10396               ODRDiagTemplateNote(SecondDecl->getLocation(),
10397                                   SecondDecl->getSourceRange(),
10398                                   ParamSingleDefaultArgument)
10399                   << HasSecondDefaultArgument;
10400               break;
10401             }
10402 
10403             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10404                    "Expecting default arguments.");
10405 
10406             ODRDiagTemplateError(FirstDecl->getLocation(),
10407                                  FirstDecl->getSourceRange(),
10408                                  ParamDifferentDefaultArgument);
10409             ODRDiagTemplateNote(SecondDecl->getLocation(),
10410                                 SecondDecl->getSourceRange(),
10411                                 ParamDifferentDefaultArgument);
10412 
10413             break;
10414           }
10415           case Decl::TemplateTemplateParm: {
10416             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10417             const auto *SecondParam =
10418                 cast<TemplateTemplateParmDecl>(SecondDecl);
10419             const bool HasFirstDefaultArgument =
10420                 FirstParam->hasDefaultArgument() &&
10421                 !FirstParam->defaultArgumentWasInherited();
10422             const bool HasSecondDefaultArgument =
10423                 SecondParam->hasDefaultArgument() &&
10424                 !SecondParam->defaultArgumentWasInherited();
10425 
10426             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10427               ODRDiagTemplateError(FirstDecl->getLocation(),
10428                                    FirstDecl->getSourceRange(),
10429                                    ParamSingleDefaultArgument)
10430                   << HasFirstDefaultArgument;
10431               ODRDiagTemplateNote(SecondDecl->getLocation(),
10432                                   SecondDecl->getSourceRange(),
10433                                   ParamSingleDefaultArgument)
10434                   << HasSecondDefaultArgument;
10435               break;
10436             }
10437 
10438             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10439                    "Expecting default arguments.");
10440 
10441             ODRDiagTemplateError(FirstDecl->getLocation(),
10442                                  FirstDecl->getSourceRange(),
10443                                  ParamDifferentDefaultArgument);
10444             ODRDiagTemplateNote(SecondDecl->getLocation(),
10445                                 SecondDecl->getSourceRange(),
10446                                 ParamDifferentDefaultArgument);
10447 
10448             break;
10449           }
10450           }
10451 
10452           break;
10453         }
10454 
10455         if (FirstIt != FirstEnd) {
10456           Diagnosed = true;
10457           break;
10458         }
10459       }
10460 
10461       DeclHashes FirstHashes;
10462       DeclHashes SecondHashes;
10463       const DeclContext *DC = FirstRecord;
10464       PopulateHashes(FirstHashes, FirstRecord, DC);
10465       PopulateHashes(SecondHashes, SecondRecord, DC);
10466 
10467       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10468       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10469       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10470       Decl *FirstDecl = DR.FirstDecl;
10471       Decl *SecondDecl = DR.SecondDecl;
10472 
10473       if (FirstDiffType == Other || SecondDiffType == Other) {
10474         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10475                               SecondModule);
10476         Diagnosed = true;
10477         break;
10478       }
10479 
10480       if (FirstDiffType != SecondDiffType) {
10481         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10482                             SecondModule);
10483         Diagnosed = true;
10484         break;
10485       }
10486 
10487       assert(FirstDiffType == SecondDiffType);
10488 
10489       switch (FirstDiffType) {
10490       case Other:
10491       case EndOfClass:
10492       case PublicSpecifer:
10493       case PrivateSpecifer:
10494       case ProtectedSpecifer:
10495         llvm_unreachable("Invalid diff type");
10496 
10497       case StaticAssert: {
10498         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10499         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10500 
10501         Expr *FirstExpr = FirstSA->getAssertExpr();
10502         Expr *SecondExpr = SecondSA->getAssertExpr();
10503         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10504         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10505         if (FirstODRHash != SecondODRHash) {
10506           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10507                            FirstExpr->getSourceRange(), StaticAssertCondition);
10508           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10509                           SecondExpr->getSourceRange(), StaticAssertCondition);
10510           Diagnosed = true;
10511           break;
10512         }
10513 
10514         StringLiteral *FirstStr = FirstSA->getMessage();
10515         StringLiteral *SecondStr = SecondSA->getMessage();
10516         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10517         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10518           SourceLocation FirstLoc, SecondLoc;
10519           SourceRange FirstRange, SecondRange;
10520           if (FirstStr) {
10521             FirstLoc = FirstStr->getBeginLoc();
10522             FirstRange = FirstStr->getSourceRange();
10523           } else {
10524             FirstLoc = FirstSA->getBeginLoc();
10525             FirstRange = FirstSA->getSourceRange();
10526           }
10527           if (SecondStr) {
10528             SecondLoc = SecondStr->getBeginLoc();
10529             SecondRange = SecondStr->getSourceRange();
10530           } else {
10531             SecondLoc = SecondSA->getBeginLoc();
10532             SecondRange = SecondSA->getSourceRange();
10533           }
10534           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10535                            StaticAssertOnlyMessage)
10536               << (FirstStr == nullptr);
10537           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10538                           StaticAssertOnlyMessage)
10539               << (SecondStr == nullptr);
10540           Diagnosed = true;
10541           break;
10542         }
10543 
10544         if (FirstStr && SecondStr &&
10545             FirstStr->getString() != SecondStr->getString()) {
10546           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10547                            FirstStr->getSourceRange(), StaticAssertMessage);
10548           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10549                           SecondStr->getSourceRange(), StaticAssertMessage);
10550           Diagnosed = true;
10551           break;
10552         }
10553         break;
10554       }
10555       case Field: {
10556         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10557                                  cast<FieldDecl>(FirstDecl),
10558                                  cast<FieldDecl>(SecondDecl));
10559         break;
10560       }
10561       case CXXMethod: {
10562         enum {
10563           DiagMethod,
10564           DiagConstructor,
10565           DiagDestructor,
10566         } FirstMethodType,
10567             SecondMethodType;
10568         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10569           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10570           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10571           return DiagMethod;
10572         };
10573         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10574         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10575         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10576         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10577         auto FirstName = FirstMethod->getDeclName();
10578         auto SecondName = SecondMethod->getDeclName();
10579         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10580           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10581                            FirstMethod->getSourceRange(), MethodName)
10582               << FirstMethodType << FirstName;
10583           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10584                           SecondMethod->getSourceRange(), MethodName)
10585               << SecondMethodType << SecondName;
10586 
10587           Diagnosed = true;
10588           break;
10589         }
10590 
10591         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10592         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10593         if (FirstDeleted != SecondDeleted) {
10594           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10595                            FirstMethod->getSourceRange(), MethodDeleted)
10596               << FirstMethodType << FirstName << FirstDeleted;
10597 
10598           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10599                           SecondMethod->getSourceRange(), MethodDeleted)
10600               << SecondMethodType << SecondName << SecondDeleted;
10601           Diagnosed = true;
10602           break;
10603         }
10604 
10605         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10606         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10607         if (FirstDefaulted != SecondDefaulted) {
10608           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10609                            FirstMethod->getSourceRange(), MethodDefaulted)
10610               << FirstMethodType << FirstName << FirstDefaulted;
10611 
10612           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10613                           SecondMethod->getSourceRange(), MethodDefaulted)
10614               << SecondMethodType << SecondName << SecondDefaulted;
10615           Diagnosed = true;
10616           break;
10617         }
10618 
10619         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10620         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10621         const bool FirstPure = FirstMethod->isPure();
10622         const bool SecondPure = SecondMethod->isPure();
10623         if ((FirstVirtual || SecondVirtual) &&
10624             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10625           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10626                            FirstMethod->getSourceRange(), MethodVirtual)
10627               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10628           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10629                           SecondMethod->getSourceRange(), MethodVirtual)
10630               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10631           Diagnosed = true;
10632           break;
10633         }
10634 
10635         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10636         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10637         // class needs to be checked instead.
10638         const auto FirstStorage = FirstMethod->getStorageClass();
10639         const auto SecondStorage = SecondMethod->getStorageClass();
10640         const bool FirstStatic = FirstStorage == SC_Static;
10641         const bool SecondStatic = SecondStorage == SC_Static;
10642         if (FirstStatic != SecondStatic) {
10643           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10644                            FirstMethod->getSourceRange(), MethodStatic)
10645               << FirstMethodType << FirstName << FirstStatic;
10646           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10647                           SecondMethod->getSourceRange(), MethodStatic)
10648               << SecondMethodType << SecondName << SecondStatic;
10649           Diagnosed = true;
10650           break;
10651         }
10652 
10653         const bool FirstVolatile = FirstMethod->isVolatile();
10654         const bool SecondVolatile = SecondMethod->isVolatile();
10655         if (FirstVolatile != SecondVolatile) {
10656           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10657                            FirstMethod->getSourceRange(), MethodVolatile)
10658               << FirstMethodType << FirstName << FirstVolatile;
10659           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10660                           SecondMethod->getSourceRange(), MethodVolatile)
10661               << SecondMethodType << SecondName << SecondVolatile;
10662           Diagnosed = true;
10663           break;
10664         }
10665 
10666         const bool FirstConst = FirstMethod->isConst();
10667         const bool SecondConst = SecondMethod->isConst();
10668         if (FirstConst != SecondConst) {
10669           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10670                            FirstMethod->getSourceRange(), MethodConst)
10671               << FirstMethodType << FirstName << FirstConst;
10672           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10673                           SecondMethod->getSourceRange(), MethodConst)
10674               << SecondMethodType << SecondName << SecondConst;
10675           Diagnosed = true;
10676           break;
10677         }
10678 
10679         const bool FirstInline = FirstMethod->isInlineSpecified();
10680         const bool SecondInline = SecondMethod->isInlineSpecified();
10681         if (FirstInline != SecondInline) {
10682           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10683                            FirstMethod->getSourceRange(), MethodInline)
10684               << FirstMethodType << FirstName << FirstInline;
10685           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10686                           SecondMethod->getSourceRange(), MethodInline)
10687               << SecondMethodType << SecondName << SecondInline;
10688           Diagnosed = true;
10689           break;
10690         }
10691 
10692         const unsigned FirstNumParameters = FirstMethod->param_size();
10693         const unsigned SecondNumParameters = SecondMethod->param_size();
10694         if (FirstNumParameters != SecondNumParameters) {
10695           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10696                            FirstMethod->getSourceRange(),
10697                            MethodNumberParameters)
10698               << FirstMethodType << FirstName << FirstNumParameters;
10699           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10700                           SecondMethod->getSourceRange(),
10701                           MethodNumberParameters)
10702               << SecondMethodType << SecondName << SecondNumParameters;
10703           Diagnosed = true;
10704           break;
10705         }
10706 
10707         // Need this status boolean to know when break out of the switch.
10708         bool ParameterMismatch = false;
10709         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10710           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10711           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10712 
10713           QualType FirstParamType = FirstParam->getType();
10714           QualType SecondParamType = SecondParam->getType();
10715           if (FirstParamType != SecondParamType &&
10716               ComputeQualTypeODRHash(FirstParamType) !=
10717                   ComputeQualTypeODRHash(SecondParamType)) {
10718             if (const DecayedType *ParamDecayedType =
10719                     FirstParamType->getAs<DecayedType>()) {
10720               ODRDiagDeclError(
10721                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10722                   FirstMethod->getSourceRange(), MethodParameterType)
10723                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10724                   << true << ParamDecayedType->getOriginalType();
10725             } else {
10726               ODRDiagDeclError(
10727                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10728                   FirstMethod->getSourceRange(), MethodParameterType)
10729                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10730                   << false;
10731             }
10732 
10733             if (const DecayedType *ParamDecayedType =
10734                     SecondParamType->getAs<DecayedType>()) {
10735               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10736                               SecondMethod->getSourceRange(),
10737                               MethodParameterType)
10738                   << SecondMethodType << SecondName << (I + 1)
10739                   << SecondParamType << true
10740                   << ParamDecayedType->getOriginalType();
10741             } else {
10742               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10743                               SecondMethod->getSourceRange(),
10744                               MethodParameterType)
10745                   << SecondMethodType << SecondName << (I + 1)
10746                   << SecondParamType << false;
10747             }
10748             ParameterMismatch = true;
10749             break;
10750           }
10751 
10752           DeclarationName FirstParamName = FirstParam->getDeclName();
10753           DeclarationName SecondParamName = SecondParam->getDeclName();
10754           if (FirstParamName != SecondParamName) {
10755             ODRDiagDeclError(FirstRecord, FirstModule,
10756                              FirstMethod->getLocation(),
10757                              FirstMethod->getSourceRange(), MethodParameterName)
10758                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10759             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10760                             SecondMethod->getSourceRange(), MethodParameterName)
10761                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10762             ParameterMismatch = true;
10763             break;
10764           }
10765 
10766           const Expr *FirstInit = FirstParam->getInit();
10767           const Expr *SecondInit = SecondParam->getInit();
10768           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10769             ODRDiagDeclError(FirstRecord, FirstModule,
10770                              FirstMethod->getLocation(),
10771                              FirstMethod->getSourceRange(),
10772                              MethodParameterSingleDefaultArgument)
10773                 << FirstMethodType << FirstName << (I + 1)
10774                 << (FirstInit == nullptr)
10775                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10776             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10777                             SecondMethod->getSourceRange(),
10778                             MethodParameterSingleDefaultArgument)
10779                 << SecondMethodType << SecondName << (I + 1)
10780                 << (SecondInit == nullptr)
10781                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10782             ParameterMismatch = true;
10783             break;
10784           }
10785 
10786           if (FirstInit && SecondInit &&
10787               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10788             ODRDiagDeclError(FirstRecord, FirstModule,
10789                              FirstMethod->getLocation(),
10790                              FirstMethod->getSourceRange(),
10791                              MethodParameterDifferentDefaultArgument)
10792                 << FirstMethodType << FirstName << (I + 1)
10793                 << FirstInit->getSourceRange();
10794             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10795                             SecondMethod->getSourceRange(),
10796                             MethodParameterDifferentDefaultArgument)
10797                 << SecondMethodType << SecondName << (I + 1)
10798                 << SecondInit->getSourceRange();
10799             ParameterMismatch = true;
10800             break;
10801 
10802           }
10803         }
10804 
10805         if (ParameterMismatch) {
10806           Diagnosed = true;
10807           break;
10808         }
10809 
10810         const auto *FirstTemplateArgs =
10811             FirstMethod->getTemplateSpecializationArgs();
10812         const auto *SecondTemplateArgs =
10813             SecondMethod->getTemplateSpecializationArgs();
10814 
10815         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10816             (!FirstTemplateArgs && SecondTemplateArgs)) {
10817           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10818                            FirstMethod->getSourceRange(),
10819                            MethodNoTemplateArguments)
10820               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10821           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10822                           SecondMethod->getSourceRange(),
10823                           MethodNoTemplateArguments)
10824               << SecondMethodType << SecondName
10825               << (SecondTemplateArgs != nullptr);
10826 
10827           Diagnosed = true;
10828           break;
10829         }
10830 
10831         if (FirstTemplateArgs && SecondTemplateArgs) {
10832           // Remove pack expansions from argument list.
10833           auto ExpandTemplateArgumentList =
10834               [](const TemplateArgumentList *TAL) {
10835                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10836                 for (const TemplateArgument &TA : TAL->asArray()) {
10837                   if (TA.getKind() != TemplateArgument::Pack) {
10838                     ExpandedList.push_back(&TA);
10839                     continue;
10840                   }
10841                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10842                     ExpandedList.push_back(&PackTA);
10843                   }
10844                 }
10845                 return ExpandedList;
10846               };
10847           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10848               ExpandTemplateArgumentList(FirstTemplateArgs);
10849           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10850               ExpandTemplateArgumentList(SecondTemplateArgs);
10851 
10852           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10853             ODRDiagDeclError(FirstRecord, FirstModule,
10854                              FirstMethod->getLocation(),
10855                              FirstMethod->getSourceRange(),
10856                              MethodDifferentNumberTemplateArguments)
10857                 << FirstMethodType << FirstName
10858                 << (unsigned)FirstExpandedList.size();
10859             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10860                             SecondMethod->getSourceRange(),
10861                             MethodDifferentNumberTemplateArguments)
10862                 << SecondMethodType << SecondName
10863                 << (unsigned)SecondExpandedList.size();
10864 
10865             Diagnosed = true;
10866             break;
10867           }
10868 
10869           bool TemplateArgumentMismatch = false;
10870           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10871             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10872                                    &SecondTA = *SecondExpandedList[i];
10873             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10874                 ComputeTemplateArgumentODRHash(SecondTA)) {
10875               continue;
10876             }
10877 
10878             ODRDiagDeclError(
10879                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10880                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10881                 << FirstMethodType << FirstName << FirstTA << i + 1;
10882             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10883                             SecondMethod->getSourceRange(),
10884                             MethodDifferentTemplateArgument)
10885                 << SecondMethodType << SecondName << SecondTA << i + 1;
10886 
10887             TemplateArgumentMismatch = true;
10888             break;
10889           }
10890 
10891           if (TemplateArgumentMismatch) {
10892             Diagnosed = true;
10893             break;
10894           }
10895         }
10896 
10897         // Compute the hash of the method as if it has no body.
10898         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10899           Hash.clear();
10900           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10901           return Hash.CalculateHash();
10902         };
10903 
10904         // Compare the hash generated to the hash stored.  A difference means
10905         // that a body was present in the original source.  Due to merging,
10906         // the stardard way of detecting a body will not work.
10907         const bool HasFirstBody =
10908             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10909         const bool HasSecondBody =
10910             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10911 
10912         if (HasFirstBody != HasSecondBody) {
10913           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10914                            FirstMethod->getSourceRange(), MethodSingleBody)
10915               << FirstMethodType << FirstName << HasFirstBody;
10916           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10917                           SecondMethod->getSourceRange(), MethodSingleBody)
10918               << SecondMethodType << SecondName << HasSecondBody;
10919           Diagnosed = true;
10920           break;
10921         }
10922 
10923         if (HasFirstBody && HasSecondBody) {
10924           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10925                            FirstMethod->getSourceRange(), MethodDifferentBody)
10926               << FirstMethodType << FirstName;
10927           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10928                           SecondMethod->getSourceRange(), MethodDifferentBody)
10929               << SecondMethodType << SecondName;
10930           Diagnosed = true;
10931           break;
10932         }
10933 
10934         break;
10935       }
10936       case TypeAlias:
10937       case TypeDef: {
10938         Diagnosed = ODRDiagTypeDefOrAlias(
10939             FirstRecord, FirstModule, SecondModule,
10940             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10941             FirstDiffType == TypeAlias);
10942         break;
10943       }
10944       case Var: {
10945         Diagnosed =
10946             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10947                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10948         break;
10949       }
10950       case Friend: {
10951         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10952         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10953 
10954         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10955         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10956 
10957         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10958         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10959 
10960         if (FirstND && SecondND) {
10961           ODRDiagDeclError(FirstRecord, FirstModule,
10962                            FirstFriend->getFriendLoc(),
10963                            FirstFriend->getSourceRange(), FriendFunction)
10964               << FirstND;
10965           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10966                           SecondFriend->getSourceRange(), FriendFunction)
10967               << SecondND;
10968 
10969           Diagnosed = true;
10970           break;
10971         }
10972 
10973         if (FirstTSI && SecondTSI) {
10974           QualType FirstFriendType = FirstTSI->getType();
10975           QualType SecondFriendType = SecondTSI->getType();
10976           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10977                  ComputeQualTypeODRHash(SecondFriendType));
10978           ODRDiagDeclError(FirstRecord, FirstModule,
10979                            FirstFriend->getFriendLoc(),
10980                            FirstFriend->getSourceRange(), FriendType)
10981               << FirstFriendType;
10982           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10983                           SecondFriend->getSourceRange(), FriendType)
10984               << SecondFriendType;
10985           Diagnosed = true;
10986           break;
10987         }
10988 
10989         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10990                          FirstFriend->getSourceRange(), FriendTypeFunction)
10991             << (FirstTSI == nullptr);
10992         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10993                         SecondFriend->getSourceRange(), FriendTypeFunction)
10994             << (SecondTSI == nullptr);
10995 
10996         Diagnosed = true;
10997         break;
10998       }
10999       case FunctionTemplate: {
11000         FunctionTemplateDecl *FirstTemplate =
11001             cast<FunctionTemplateDecl>(FirstDecl);
11002         FunctionTemplateDecl *SecondTemplate =
11003             cast<FunctionTemplateDecl>(SecondDecl);
11004 
11005         TemplateParameterList *FirstTPL =
11006             FirstTemplate->getTemplateParameters();
11007         TemplateParameterList *SecondTPL =
11008             SecondTemplate->getTemplateParameters();
11009 
11010         if (FirstTPL->size() != SecondTPL->size()) {
11011           ODRDiagDeclError(FirstRecord, FirstModule,
11012                            FirstTemplate->getLocation(),
11013                            FirstTemplate->getSourceRange(),
11014                            FunctionTemplateDifferentNumberParameters)
11015               << FirstTemplate << FirstTPL->size();
11016           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11017                           SecondTemplate->getSourceRange(),
11018                           FunctionTemplateDifferentNumberParameters)
11019               << SecondTemplate << SecondTPL->size();
11020 
11021           Diagnosed = true;
11022           break;
11023         }
11024 
11025         bool ParameterMismatch = false;
11026         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11027           NamedDecl *FirstParam = FirstTPL->getParam(i);
11028           NamedDecl *SecondParam = SecondTPL->getParam(i);
11029 
11030           if (FirstParam->getKind() != SecondParam->getKind()) {
11031             enum {
11032               TemplateTypeParameter,
11033               NonTypeTemplateParameter,
11034               TemplateTemplateParameter,
11035             };
11036             auto GetParamType = [](NamedDecl *D) {
11037               switch (D->getKind()) {
11038                 default:
11039                   llvm_unreachable("Unexpected template parameter type");
11040                 case Decl::TemplateTypeParm:
11041                   return TemplateTypeParameter;
11042                 case Decl::NonTypeTemplateParm:
11043                   return NonTypeTemplateParameter;
11044                 case Decl::TemplateTemplateParm:
11045                   return TemplateTemplateParameter;
11046               }
11047             };
11048 
11049             ODRDiagDeclError(FirstRecord, FirstModule,
11050                              FirstTemplate->getLocation(),
11051                              FirstTemplate->getSourceRange(),
11052                              FunctionTemplateParameterDifferentKind)
11053                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11054             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11055                             SecondTemplate->getSourceRange(),
11056                             FunctionTemplateParameterDifferentKind)
11057                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11058 
11059             ParameterMismatch = true;
11060             break;
11061           }
11062 
11063           if (FirstParam->getName() != SecondParam->getName()) {
11064             ODRDiagDeclError(
11065                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11066                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
11067                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11068                 << FirstParam;
11069             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11070                             SecondTemplate->getSourceRange(),
11071                             FunctionTemplateParameterName)
11072                 << SecondTemplate << (i + 1)
11073                 << (bool)SecondParam->getIdentifier() << SecondParam;
11074             ParameterMismatch = true;
11075             break;
11076           }
11077 
11078           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11079               isa<TemplateTypeParmDecl>(SecondParam)) {
11080             TemplateTypeParmDecl *FirstTTPD =
11081                 cast<TemplateTypeParmDecl>(FirstParam);
11082             TemplateTypeParmDecl *SecondTTPD =
11083                 cast<TemplateTypeParmDecl>(SecondParam);
11084             bool HasFirstDefaultArgument =
11085                 FirstTTPD->hasDefaultArgument() &&
11086                 !FirstTTPD->defaultArgumentWasInherited();
11087             bool HasSecondDefaultArgument =
11088                 SecondTTPD->hasDefaultArgument() &&
11089                 !SecondTTPD->defaultArgumentWasInherited();
11090             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11091               ODRDiagDeclError(FirstRecord, FirstModule,
11092                                FirstTemplate->getLocation(),
11093                                FirstTemplate->getSourceRange(),
11094                                FunctionTemplateParameterSingleDefaultArgument)
11095                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11096               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11097                               SecondTemplate->getSourceRange(),
11098                               FunctionTemplateParameterSingleDefaultArgument)
11099                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11100               ParameterMismatch = true;
11101               break;
11102             }
11103 
11104             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11105               QualType FirstType = FirstTTPD->getDefaultArgument();
11106               QualType SecondType = SecondTTPD->getDefaultArgument();
11107               if (ComputeQualTypeODRHash(FirstType) !=
11108                   ComputeQualTypeODRHash(SecondType)) {
11109                 ODRDiagDeclError(
11110                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11111                     FirstTemplate->getSourceRange(),
11112                     FunctionTemplateParameterDifferentDefaultArgument)
11113                     << FirstTemplate << (i + 1) << FirstType;
11114                 ODRDiagDeclNote(
11115                     SecondModule, SecondTemplate->getLocation(),
11116                     SecondTemplate->getSourceRange(),
11117                     FunctionTemplateParameterDifferentDefaultArgument)
11118                     << SecondTemplate << (i + 1) << SecondType;
11119                 ParameterMismatch = true;
11120                 break;
11121               }
11122             }
11123 
11124             if (FirstTTPD->isParameterPack() !=
11125                 SecondTTPD->isParameterPack()) {
11126               ODRDiagDeclError(FirstRecord, FirstModule,
11127                                FirstTemplate->getLocation(),
11128                                FirstTemplate->getSourceRange(),
11129                                FunctionTemplatePackParameter)
11130                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11131               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11132                               SecondTemplate->getSourceRange(),
11133                               FunctionTemplatePackParameter)
11134                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11135               ParameterMismatch = true;
11136               break;
11137             }
11138           }
11139 
11140           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11141               isa<TemplateTemplateParmDecl>(SecondParam)) {
11142             TemplateTemplateParmDecl *FirstTTPD =
11143                 cast<TemplateTemplateParmDecl>(FirstParam);
11144             TemplateTemplateParmDecl *SecondTTPD =
11145                 cast<TemplateTemplateParmDecl>(SecondParam);
11146 
11147             TemplateParameterList *FirstTPL =
11148                 FirstTTPD->getTemplateParameters();
11149             TemplateParameterList *SecondTPL =
11150                 SecondTTPD->getTemplateParameters();
11151 
11152             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11153                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11154               ODRDiagDeclError(FirstRecord, FirstModule,
11155                                FirstTemplate->getLocation(),
11156                                FirstTemplate->getSourceRange(),
11157                                FunctionTemplateParameterDifferentType)
11158                   << FirstTemplate << (i + 1);
11159               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11160                               SecondTemplate->getSourceRange(),
11161                               FunctionTemplateParameterDifferentType)
11162                   << SecondTemplate << (i + 1);
11163               ParameterMismatch = true;
11164               break;
11165             }
11166 
11167             bool HasFirstDefaultArgument =
11168                 FirstTTPD->hasDefaultArgument() &&
11169                 !FirstTTPD->defaultArgumentWasInherited();
11170             bool HasSecondDefaultArgument =
11171                 SecondTTPD->hasDefaultArgument() &&
11172                 !SecondTTPD->defaultArgumentWasInherited();
11173             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11174               ODRDiagDeclError(FirstRecord, FirstModule,
11175                                FirstTemplate->getLocation(),
11176                                FirstTemplate->getSourceRange(),
11177                                FunctionTemplateParameterSingleDefaultArgument)
11178                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11179               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11180                               SecondTemplate->getSourceRange(),
11181                               FunctionTemplateParameterSingleDefaultArgument)
11182                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11183               ParameterMismatch = true;
11184               break;
11185             }
11186 
11187             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11188               TemplateArgument FirstTA =
11189                   FirstTTPD->getDefaultArgument().getArgument();
11190               TemplateArgument SecondTA =
11191                   SecondTTPD->getDefaultArgument().getArgument();
11192               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11193                   ComputeTemplateArgumentODRHash(SecondTA)) {
11194                 ODRDiagDeclError(
11195                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11196                     FirstTemplate->getSourceRange(),
11197                     FunctionTemplateParameterDifferentDefaultArgument)
11198                     << FirstTemplate << (i + 1) << FirstTA;
11199                 ODRDiagDeclNote(
11200                     SecondModule, SecondTemplate->getLocation(),
11201                     SecondTemplate->getSourceRange(),
11202                     FunctionTemplateParameterDifferentDefaultArgument)
11203                     << SecondTemplate << (i + 1) << SecondTA;
11204                 ParameterMismatch = true;
11205                 break;
11206               }
11207             }
11208 
11209             if (FirstTTPD->isParameterPack() !=
11210                 SecondTTPD->isParameterPack()) {
11211               ODRDiagDeclError(FirstRecord, FirstModule,
11212                                FirstTemplate->getLocation(),
11213                                FirstTemplate->getSourceRange(),
11214                                FunctionTemplatePackParameter)
11215                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11216               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11217                               SecondTemplate->getSourceRange(),
11218                               FunctionTemplatePackParameter)
11219                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11220               ParameterMismatch = true;
11221               break;
11222             }
11223           }
11224 
11225           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11226               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11227             NonTypeTemplateParmDecl *FirstNTTPD =
11228                 cast<NonTypeTemplateParmDecl>(FirstParam);
11229             NonTypeTemplateParmDecl *SecondNTTPD =
11230                 cast<NonTypeTemplateParmDecl>(SecondParam);
11231 
11232             QualType FirstType = FirstNTTPD->getType();
11233             QualType SecondType = SecondNTTPD->getType();
11234             if (ComputeQualTypeODRHash(FirstType) !=
11235                 ComputeQualTypeODRHash(SecondType)) {
11236               ODRDiagDeclError(FirstRecord, FirstModule,
11237                                FirstTemplate->getLocation(),
11238                                FirstTemplate->getSourceRange(),
11239                                FunctionTemplateParameterDifferentType)
11240                   << FirstTemplate << (i + 1);
11241               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11242                               SecondTemplate->getSourceRange(),
11243                               FunctionTemplateParameterDifferentType)
11244                   << SecondTemplate << (i + 1);
11245               ParameterMismatch = true;
11246               break;
11247             }
11248 
11249             bool HasFirstDefaultArgument =
11250                 FirstNTTPD->hasDefaultArgument() &&
11251                 !FirstNTTPD->defaultArgumentWasInherited();
11252             bool HasSecondDefaultArgument =
11253                 SecondNTTPD->hasDefaultArgument() &&
11254                 !SecondNTTPD->defaultArgumentWasInherited();
11255             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11256               ODRDiagDeclError(FirstRecord, FirstModule,
11257                                FirstTemplate->getLocation(),
11258                                FirstTemplate->getSourceRange(),
11259                                FunctionTemplateParameterSingleDefaultArgument)
11260                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11261               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11262                               SecondTemplate->getSourceRange(),
11263                               FunctionTemplateParameterSingleDefaultArgument)
11264                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11265               ParameterMismatch = true;
11266               break;
11267             }
11268 
11269             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11270               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11271               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11272               if (ComputeODRHash(FirstDefaultArgument) !=
11273                   ComputeODRHash(SecondDefaultArgument)) {
11274                 ODRDiagDeclError(
11275                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11276                     FirstTemplate->getSourceRange(),
11277                     FunctionTemplateParameterDifferentDefaultArgument)
11278                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11279                 ODRDiagDeclNote(
11280                     SecondModule, SecondTemplate->getLocation(),
11281                     SecondTemplate->getSourceRange(),
11282                     FunctionTemplateParameterDifferentDefaultArgument)
11283                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11284                 ParameterMismatch = true;
11285                 break;
11286               }
11287             }
11288 
11289             if (FirstNTTPD->isParameterPack() !=
11290                 SecondNTTPD->isParameterPack()) {
11291               ODRDiagDeclError(FirstRecord, FirstModule,
11292                                FirstTemplate->getLocation(),
11293                                FirstTemplate->getSourceRange(),
11294                                FunctionTemplatePackParameter)
11295                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11296               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11297                               SecondTemplate->getSourceRange(),
11298                               FunctionTemplatePackParameter)
11299                   << SecondTemplate << (i + 1)
11300                   << SecondNTTPD->isParameterPack();
11301               ParameterMismatch = true;
11302               break;
11303             }
11304           }
11305         }
11306 
11307         if (ParameterMismatch) {
11308           Diagnosed = true;
11309           break;
11310         }
11311 
11312         break;
11313       }
11314       }
11315 
11316       if (Diagnosed)
11317         continue;
11318 
11319       Diag(FirstDecl->getLocation(),
11320            diag::err_module_odr_violation_mismatch_decl_unknown)
11321           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11322           << FirstDecl->getSourceRange();
11323       Diag(SecondDecl->getLocation(),
11324            diag::note_module_odr_violation_mismatch_decl_unknown)
11325           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11326       Diagnosed = true;
11327     }
11328 
11329     if (!Diagnosed) {
11330       // All definitions are updates to the same declaration. This happens if a
11331       // module instantiates the declaration of a class template specialization
11332       // and two or more other modules instantiate its definition.
11333       //
11334       // FIXME: Indicate which modules had instantiations of this definition.
11335       // FIXME: How can this even happen?
11336       Diag(Merge.first->getLocation(),
11337            diag::err_module_odr_violation_different_instantiations)
11338         << Merge.first;
11339     }
11340   }
11341 
11342   // Issue ODR failures diagnostics for functions.
11343   for (auto &Merge : FunctionOdrMergeFailures) {
11344     enum ODRFunctionDifference {
11345       ReturnType,
11346       ParameterName,
11347       ParameterType,
11348       ParameterSingleDefaultArgument,
11349       ParameterDifferentDefaultArgument,
11350       FunctionBody,
11351     };
11352 
11353     FunctionDecl *FirstFunction = Merge.first;
11354     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11355 
11356     bool Diagnosed = false;
11357     for (auto &SecondFunction : Merge.second) {
11358 
11359       if (FirstFunction == SecondFunction)
11360         continue;
11361 
11362       std::string SecondModule =
11363           getOwningModuleNameForDiagnostic(SecondFunction);
11364 
11365       auto ODRDiagError = [FirstFunction, &FirstModule,
11366                            this](SourceLocation Loc, SourceRange Range,
11367                                  ODRFunctionDifference DiffType) {
11368         return Diag(Loc, diag::err_module_odr_violation_function)
11369                << FirstFunction << FirstModule.empty() << FirstModule << Range
11370                << DiffType;
11371       };
11372       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11373                                                SourceRange Range,
11374                                                ODRFunctionDifference DiffType) {
11375         return Diag(Loc, diag::note_module_odr_violation_function)
11376                << SecondModule << Range << DiffType;
11377       };
11378 
11379       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11380           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11381         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11382                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11383             << FirstFunction->getReturnType();
11384         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11385                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11386             << SecondFunction->getReturnType();
11387         Diagnosed = true;
11388         break;
11389       }
11390 
11391       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11392              "Merged functions with different number of parameters");
11393 
11394       auto ParamSize = FirstFunction->param_size();
11395       bool ParameterMismatch = false;
11396       for (unsigned I = 0; I < ParamSize; ++I) {
11397         auto *FirstParam = FirstFunction->getParamDecl(I);
11398         auto *SecondParam = SecondFunction->getParamDecl(I);
11399 
11400         assert(getContext().hasSameType(FirstParam->getType(),
11401                                       SecondParam->getType()) &&
11402                "Merged function has different parameter types.");
11403 
11404         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11405           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11406                        ParameterName)
11407               << I + 1 << FirstParam->getDeclName();
11408           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11409                       ParameterName)
11410               << I + 1 << SecondParam->getDeclName();
11411           ParameterMismatch = true;
11412           break;
11413         };
11414 
11415         QualType FirstParamType = FirstParam->getType();
11416         QualType SecondParamType = SecondParam->getType();
11417         if (FirstParamType != SecondParamType &&
11418             ComputeQualTypeODRHash(FirstParamType) !=
11419                 ComputeQualTypeODRHash(SecondParamType)) {
11420           if (const DecayedType *ParamDecayedType =
11421                   FirstParamType->getAs<DecayedType>()) {
11422             ODRDiagError(FirstParam->getLocation(),
11423                          FirstParam->getSourceRange(), ParameterType)
11424                 << (I + 1) << FirstParamType << true
11425                 << ParamDecayedType->getOriginalType();
11426           } else {
11427             ODRDiagError(FirstParam->getLocation(),
11428                          FirstParam->getSourceRange(), ParameterType)
11429                 << (I + 1) << FirstParamType << false;
11430           }
11431 
11432           if (const DecayedType *ParamDecayedType =
11433                   SecondParamType->getAs<DecayedType>()) {
11434             ODRDiagNote(SecondParam->getLocation(),
11435                         SecondParam->getSourceRange(), ParameterType)
11436                 << (I + 1) << SecondParamType << true
11437                 << ParamDecayedType->getOriginalType();
11438           } else {
11439             ODRDiagNote(SecondParam->getLocation(),
11440                         SecondParam->getSourceRange(), ParameterType)
11441                 << (I + 1) << SecondParamType << false;
11442           }
11443           ParameterMismatch = true;
11444           break;
11445         }
11446 
11447         const Expr *FirstInit = FirstParam->getInit();
11448         const Expr *SecondInit = SecondParam->getInit();
11449         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11450           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11451                        ParameterSingleDefaultArgument)
11452               << (I + 1) << (FirstInit == nullptr)
11453               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11454           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11455                       ParameterSingleDefaultArgument)
11456               << (I + 1) << (SecondInit == nullptr)
11457               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11458           ParameterMismatch = true;
11459           break;
11460         }
11461 
11462         if (FirstInit && SecondInit &&
11463             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11464           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11465                        ParameterDifferentDefaultArgument)
11466               << (I + 1) << FirstInit->getSourceRange();
11467           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11468                       ParameterDifferentDefaultArgument)
11469               << (I + 1) << SecondInit->getSourceRange();
11470           ParameterMismatch = true;
11471           break;
11472         }
11473 
11474         assert(ComputeSubDeclODRHash(FirstParam) ==
11475                    ComputeSubDeclODRHash(SecondParam) &&
11476                "Undiagnosed parameter difference.");
11477       }
11478 
11479       if (ParameterMismatch) {
11480         Diagnosed = true;
11481         break;
11482       }
11483 
11484       // If no error has been generated before now, assume the problem is in
11485       // the body and generate a message.
11486       ODRDiagError(FirstFunction->getLocation(),
11487                    FirstFunction->getSourceRange(), FunctionBody);
11488       ODRDiagNote(SecondFunction->getLocation(),
11489                   SecondFunction->getSourceRange(), FunctionBody);
11490       Diagnosed = true;
11491       break;
11492     }
11493     (void)Diagnosed;
11494     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11495   }
11496 
11497   // Issue ODR failures diagnostics for enums.
11498   for (auto &Merge : EnumOdrMergeFailures) {
11499     enum ODREnumDifference {
11500       SingleScopedEnum,
11501       EnumTagKeywordMismatch,
11502       SingleSpecifiedType,
11503       DifferentSpecifiedTypes,
11504       DifferentNumberEnumConstants,
11505       EnumConstantName,
11506       EnumConstantSingleInitilizer,
11507       EnumConstantDifferentInitilizer,
11508     };
11509 
11510     // If we've already pointed out a specific problem with this enum, don't
11511     // bother issuing a general "something's different" diagnostic.
11512     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11513       continue;
11514 
11515     EnumDecl *FirstEnum = Merge.first;
11516     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11517 
11518     using DeclHashes =
11519         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11520     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11521                               DeclHashes &Hashes, EnumDecl *Enum) {
11522       for (auto *D : Enum->decls()) {
11523         // Due to decl merging, the first EnumDecl is the parent of
11524         // Decls in both records.
11525         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11526           continue;
11527         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11528         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11529                             ComputeSubDeclODRHash(D));
11530       }
11531     };
11532     DeclHashes FirstHashes;
11533     PopulateHashes(FirstHashes, FirstEnum);
11534     bool Diagnosed = false;
11535     for (auto &SecondEnum : Merge.second) {
11536 
11537       if (FirstEnum == SecondEnum)
11538         continue;
11539 
11540       std::string SecondModule =
11541           getOwningModuleNameForDiagnostic(SecondEnum);
11542 
11543       auto ODRDiagError = [FirstEnum, &FirstModule,
11544                            this](SourceLocation Loc, SourceRange Range,
11545                                  ODREnumDifference DiffType) {
11546         return Diag(Loc, diag::err_module_odr_violation_enum)
11547                << FirstEnum << FirstModule.empty() << FirstModule << Range
11548                << DiffType;
11549       };
11550       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11551                                                SourceRange Range,
11552                                                ODREnumDifference DiffType) {
11553         return Diag(Loc, diag::note_module_odr_violation_enum)
11554                << SecondModule << Range << DiffType;
11555       };
11556 
11557       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11558         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11559                      SingleScopedEnum)
11560             << FirstEnum->isScoped();
11561         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11562                     SingleScopedEnum)
11563             << SecondEnum->isScoped();
11564         Diagnosed = true;
11565         continue;
11566       }
11567 
11568       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11569         if (FirstEnum->isScopedUsingClassTag() !=
11570             SecondEnum->isScopedUsingClassTag()) {
11571           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11572                        EnumTagKeywordMismatch)
11573               << FirstEnum->isScopedUsingClassTag();
11574           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11575                       EnumTagKeywordMismatch)
11576               << SecondEnum->isScopedUsingClassTag();
11577           Diagnosed = true;
11578           continue;
11579         }
11580       }
11581 
11582       QualType FirstUnderlyingType =
11583           FirstEnum->getIntegerTypeSourceInfo()
11584               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11585               : QualType();
11586       QualType SecondUnderlyingType =
11587           SecondEnum->getIntegerTypeSourceInfo()
11588               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11589               : QualType();
11590       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11591           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11592                        SingleSpecifiedType)
11593               << !FirstUnderlyingType.isNull();
11594           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11595                       SingleSpecifiedType)
11596               << !SecondUnderlyingType.isNull();
11597           Diagnosed = true;
11598           continue;
11599       }
11600 
11601       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11602         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11603             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11604           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11605                        DifferentSpecifiedTypes)
11606               << FirstUnderlyingType;
11607           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11608                       DifferentSpecifiedTypes)
11609               << SecondUnderlyingType;
11610           Diagnosed = true;
11611           continue;
11612         }
11613       }
11614 
11615       DeclHashes SecondHashes;
11616       PopulateHashes(SecondHashes, SecondEnum);
11617 
11618       if (FirstHashes.size() != SecondHashes.size()) {
11619         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11620                      DifferentNumberEnumConstants)
11621             << (int)FirstHashes.size();
11622         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11623                     DifferentNumberEnumConstants)
11624             << (int)SecondHashes.size();
11625         Diagnosed = true;
11626         continue;
11627       }
11628 
11629       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11630         if (FirstHashes[I].second == SecondHashes[I].second)
11631           continue;
11632         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11633         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11634 
11635         if (FirstEnumConstant->getDeclName() !=
11636             SecondEnumConstant->getDeclName()) {
11637 
11638           ODRDiagError(FirstEnumConstant->getLocation(),
11639                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11640               << I + 1 << FirstEnumConstant;
11641           ODRDiagNote(SecondEnumConstant->getLocation(),
11642                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11643               << I + 1 << SecondEnumConstant;
11644           Diagnosed = true;
11645           break;
11646         }
11647 
11648         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11649         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11650         if (!FirstInit && !SecondInit)
11651           continue;
11652 
11653         if (!FirstInit || !SecondInit) {
11654           ODRDiagError(FirstEnumConstant->getLocation(),
11655                        FirstEnumConstant->getSourceRange(),
11656                        EnumConstantSingleInitilizer)
11657               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11658           ODRDiagNote(SecondEnumConstant->getLocation(),
11659                       SecondEnumConstant->getSourceRange(),
11660                       EnumConstantSingleInitilizer)
11661               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11662           Diagnosed = true;
11663           break;
11664         }
11665 
11666         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11667           ODRDiagError(FirstEnumConstant->getLocation(),
11668                        FirstEnumConstant->getSourceRange(),
11669                        EnumConstantDifferentInitilizer)
11670               << I + 1 << FirstEnumConstant;
11671           ODRDiagNote(SecondEnumConstant->getLocation(),
11672                       SecondEnumConstant->getSourceRange(),
11673                       EnumConstantDifferentInitilizer)
11674               << I + 1 << SecondEnumConstant;
11675           Diagnosed = true;
11676           break;
11677         }
11678       }
11679     }
11680 
11681     (void)Diagnosed;
11682     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11683   }
11684 }
11685 
11686 void ASTReader::StartedDeserializing() {
11687   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11688     ReadTimer->startTimer();
11689 }
11690 
11691 void ASTReader::FinishedDeserializing() {
11692   assert(NumCurrentElementsDeserializing &&
11693          "FinishedDeserializing not paired with StartedDeserializing");
11694   if (NumCurrentElementsDeserializing == 1) {
11695     // We decrease NumCurrentElementsDeserializing only after pending actions
11696     // are finished, to avoid recursively re-calling finishPendingActions().
11697     finishPendingActions();
11698   }
11699   --NumCurrentElementsDeserializing;
11700 
11701   if (NumCurrentElementsDeserializing == 0) {
11702     // Propagate exception specification and deduced type updates along
11703     // redeclaration chains.
11704     //
11705     // We do this now rather than in finishPendingActions because we want to
11706     // be able to walk the complete redeclaration chains of the updated decls.
11707     while (!PendingExceptionSpecUpdates.empty() ||
11708            !PendingDeducedTypeUpdates.empty()) {
11709       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11710       PendingExceptionSpecUpdates.clear();
11711       for (auto Update : ESUpdates) {
11712         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11713         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11714         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11715         if (auto *Listener = getContext().getASTMutationListener())
11716           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11717         for (auto *Redecl : Update.second->redecls())
11718           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11719       }
11720 
11721       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11722       PendingDeducedTypeUpdates.clear();
11723       for (auto Update : DTUpdates) {
11724         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11725         // FIXME: If the return type is already deduced, check that it matches.
11726         getContext().adjustDeducedFunctionResultType(Update.first,
11727                                                      Update.second);
11728       }
11729     }
11730 
11731     if (ReadTimer)
11732       ReadTimer->stopTimer();
11733 
11734     diagnoseOdrViolations();
11735 
11736     // We are not in recursive loading, so it's safe to pass the "interesting"
11737     // decls to the consumer.
11738     if (Consumer)
11739       PassInterestingDeclsToConsumer();
11740   }
11741 }
11742 
11743 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11744   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11745     // Remove any fake results before adding any real ones.
11746     auto It = PendingFakeLookupResults.find(II);
11747     if (It != PendingFakeLookupResults.end()) {
11748       for (auto *ND : It->second)
11749         SemaObj->IdResolver.RemoveDecl(ND);
11750       // FIXME: this works around module+PCH performance issue.
11751       // Rather than erase the result from the map, which is O(n), just clear
11752       // the vector of NamedDecls.
11753       It->second.clear();
11754     }
11755   }
11756 
11757   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11758     SemaObj->TUScope->AddDecl(D);
11759   } else if (SemaObj->TUScope) {
11760     // Adding the decl to IdResolver may have failed because it was already in
11761     // (even though it was not added in scope). If it is already in, make sure
11762     // it gets in the scope as well.
11763     if (std::find(SemaObj->IdResolver.begin(Name),
11764                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11765       SemaObj->TUScope->AddDecl(D);
11766   }
11767 }
11768 
11769 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11770                      ASTContext *Context,
11771                      const PCHContainerReader &PCHContainerRdr,
11772                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11773                      StringRef isysroot, bool DisableValidation,
11774                      bool AllowASTWithCompilerErrors,
11775                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11776                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11777                      std::unique_ptr<llvm::Timer> ReadTimer)
11778     : Listener(DisableValidation
11779                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11780                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11781       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11782       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11783       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11784                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11785       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11786       DisableValidation(DisableValidation),
11787       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11788       AllowConfigurationMismatch(AllowConfigurationMismatch),
11789       ValidateSystemInputs(ValidateSystemInputs),
11790       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11791       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11792   SourceMgr.setExternalSLocEntrySource(this);
11793 
11794   for (const auto &Ext : Extensions) {
11795     auto BlockName = Ext->getExtensionMetadata().BlockName;
11796     auto Known = ModuleFileExtensions.find(BlockName);
11797     if (Known != ModuleFileExtensions.end()) {
11798       Diags.Report(diag::warn_duplicate_module_file_extension)
11799         << BlockName;
11800       continue;
11801     }
11802 
11803     ModuleFileExtensions.insert({BlockName, Ext});
11804   }
11805 }
11806 
11807 ASTReader::~ASTReader() {
11808   if (OwnsDeserializationListener)
11809     delete DeserializationListener;
11810 }
11811 
11812 IdentifierResolver &ASTReader::getIdResolver() {
11813   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11814 }
11815 
11816 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11817                                                unsigned AbbrevID) {
11818   Idx = 0;
11819   Record.clear();
11820   return Cursor.readRecord(AbbrevID, Record);
11821 }
11822 //===----------------------------------------------------------------------===//
11823 //// OMPClauseReader implementation
11824 ////===----------------------------------------------------------------------===//
11825 
11826 // This has to be in namespace clang because it's friended by all
11827 // of the OMP clauses.
11828 namespace clang {
11829 
11830 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11831   ASTRecordReader &Record;
11832   ASTContext &Context;
11833 
11834 public:
11835   OMPClauseReader(ASTRecordReader &Record)
11836       : Record(Record), Context(Record.getContext()) {}
11837 
11838 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11839 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11840   OMPClause *readClause();
11841   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11842   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11843 };
11844 
11845 } // end namespace clang
11846 
11847 OMPClause *ASTRecordReader::readOMPClause() {
11848   return OMPClauseReader(*this).readClause();
11849 }
11850 
11851 OMPClause *OMPClauseReader::readClause() {
11852   OMPClause *C = nullptr;
11853   switch (llvm::omp::Clause(Record.readInt())) {
11854   case llvm::omp::OMPC_if:
11855     C = new (Context) OMPIfClause();
11856     break;
11857   case llvm::omp::OMPC_final:
11858     C = new (Context) OMPFinalClause();
11859     break;
11860   case llvm::omp::OMPC_num_threads:
11861     C = new (Context) OMPNumThreadsClause();
11862     break;
11863   case llvm::omp::OMPC_safelen:
11864     C = new (Context) OMPSafelenClause();
11865     break;
11866   case llvm::omp::OMPC_simdlen:
11867     C = new (Context) OMPSimdlenClause();
11868     break;
11869   case llvm::omp::OMPC_allocator:
11870     C = new (Context) OMPAllocatorClause();
11871     break;
11872   case llvm::omp::OMPC_collapse:
11873     C = new (Context) OMPCollapseClause();
11874     break;
11875   case llvm::omp::OMPC_default:
11876     C = new (Context) OMPDefaultClause();
11877     break;
11878   case llvm::omp::OMPC_proc_bind:
11879     C = new (Context) OMPProcBindClause();
11880     break;
11881   case llvm::omp::OMPC_schedule:
11882     C = new (Context) OMPScheduleClause();
11883     break;
11884   case llvm::omp::OMPC_ordered:
11885     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11886     break;
11887   case llvm::omp::OMPC_nowait:
11888     C = new (Context) OMPNowaitClause();
11889     break;
11890   case llvm::omp::OMPC_untied:
11891     C = new (Context) OMPUntiedClause();
11892     break;
11893   case llvm::omp::OMPC_mergeable:
11894     C = new (Context) OMPMergeableClause();
11895     break;
11896   case llvm::omp::OMPC_read:
11897     C = new (Context) OMPReadClause();
11898     break;
11899   case llvm::omp::OMPC_write:
11900     C = new (Context) OMPWriteClause();
11901     break;
11902   case llvm::omp::OMPC_update:
11903     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11904     break;
11905   case llvm::omp::OMPC_capture:
11906     C = new (Context) OMPCaptureClause();
11907     break;
11908   case llvm::omp::OMPC_seq_cst:
11909     C = new (Context) OMPSeqCstClause();
11910     break;
11911   case llvm::omp::OMPC_acq_rel:
11912     C = new (Context) OMPAcqRelClause();
11913     break;
11914   case llvm::omp::OMPC_acquire:
11915     C = new (Context) OMPAcquireClause();
11916     break;
11917   case llvm::omp::OMPC_release:
11918     C = new (Context) OMPReleaseClause();
11919     break;
11920   case llvm::omp::OMPC_relaxed:
11921     C = new (Context) OMPRelaxedClause();
11922     break;
11923   case llvm::omp::OMPC_threads:
11924     C = new (Context) OMPThreadsClause();
11925     break;
11926   case llvm::omp::OMPC_simd:
11927     C = new (Context) OMPSIMDClause();
11928     break;
11929   case llvm::omp::OMPC_nogroup:
11930     C = new (Context) OMPNogroupClause();
11931     break;
11932   case llvm::omp::OMPC_unified_address:
11933     C = new (Context) OMPUnifiedAddressClause();
11934     break;
11935   case llvm::omp::OMPC_unified_shared_memory:
11936     C = new (Context) OMPUnifiedSharedMemoryClause();
11937     break;
11938   case llvm::omp::OMPC_reverse_offload:
11939     C = new (Context) OMPReverseOffloadClause();
11940     break;
11941   case llvm::omp::OMPC_dynamic_allocators:
11942     C = new (Context) OMPDynamicAllocatorsClause();
11943     break;
11944   case llvm::omp::OMPC_atomic_default_mem_order:
11945     C = new (Context) OMPAtomicDefaultMemOrderClause();
11946     break;
11947  case llvm::omp::OMPC_private:
11948     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11949     break;
11950   case llvm::omp::OMPC_firstprivate:
11951     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11952     break;
11953   case llvm::omp::OMPC_lastprivate:
11954     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11955     break;
11956   case llvm::omp::OMPC_shared:
11957     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11958     break;
11959   case llvm::omp::OMPC_reduction: {
11960     unsigned N = Record.readInt();
11961     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11962     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11963     break;
11964   }
11965   case llvm::omp::OMPC_task_reduction:
11966     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11967     break;
11968   case llvm::omp::OMPC_in_reduction:
11969     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11970     break;
11971   case llvm::omp::OMPC_linear:
11972     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11973     break;
11974   case llvm::omp::OMPC_aligned:
11975     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11976     break;
11977   case llvm::omp::OMPC_copyin:
11978     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11979     break;
11980   case llvm::omp::OMPC_copyprivate:
11981     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11982     break;
11983   case llvm::omp::OMPC_flush:
11984     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11985     break;
11986   case llvm::omp::OMPC_depobj:
11987     C = OMPDepobjClause::CreateEmpty(Context);
11988     break;
11989   case llvm::omp::OMPC_depend: {
11990     unsigned NumVars = Record.readInt();
11991     unsigned NumLoops = Record.readInt();
11992     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11993     break;
11994   }
11995   case llvm::omp::OMPC_device:
11996     C = new (Context) OMPDeviceClause();
11997     break;
11998   case llvm::omp::OMPC_map: {
11999     OMPMappableExprListSizeTy Sizes;
12000     Sizes.NumVars = Record.readInt();
12001     Sizes.NumUniqueDeclarations = Record.readInt();
12002     Sizes.NumComponentLists = Record.readInt();
12003     Sizes.NumComponents = Record.readInt();
12004     C = OMPMapClause::CreateEmpty(Context, Sizes);
12005     break;
12006   }
12007   case llvm::omp::OMPC_num_teams:
12008     C = new (Context) OMPNumTeamsClause();
12009     break;
12010   case llvm::omp::OMPC_thread_limit:
12011     C = new (Context) OMPThreadLimitClause();
12012     break;
12013   case llvm::omp::OMPC_priority:
12014     C = new (Context) OMPPriorityClause();
12015     break;
12016   case llvm::omp::OMPC_grainsize:
12017     C = new (Context) OMPGrainsizeClause();
12018     break;
12019   case llvm::omp::OMPC_num_tasks:
12020     C = new (Context) OMPNumTasksClause();
12021     break;
12022   case llvm::omp::OMPC_hint:
12023     C = new (Context) OMPHintClause();
12024     break;
12025   case llvm::omp::OMPC_dist_schedule:
12026     C = new (Context) OMPDistScheduleClause();
12027     break;
12028   case llvm::omp::OMPC_defaultmap:
12029     C = new (Context) OMPDefaultmapClause();
12030     break;
12031   case llvm::omp::OMPC_to: {
12032     OMPMappableExprListSizeTy Sizes;
12033     Sizes.NumVars = Record.readInt();
12034     Sizes.NumUniqueDeclarations = Record.readInt();
12035     Sizes.NumComponentLists = Record.readInt();
12036     Sizes.NumComponents = Record.readInt();
12037     C = OMPToClause::CreateEmpty(Context, Sizes);
12038     break;
12039   }
12040   case llvm::omp::OMPC_from: {
12041     OMPMappableExprListSizeTy Sizes;
12042     Sizes.NumVars = Record.readInt();
12043     Sizes.NumUniqueDeclarations = Record.readInt();
12044     Sizes.NumComponentLists = Record.readInt();
12045     Sizes.NumComponents = Record.readInt();
12046     C = OMPFromClause::CreateEmpty(Context, Sizes);
12047     break;
12048   }
12049   case llvm::omp::OMPC_use_device_ptr: {
12050     OMPMappableExprListSizeTy Sizes;
12051     Sizes.NumVars = Record.readInt();
12052     Sizes.NumUniqueDeclarations = Record.readInt();
12053     Sizes.NumComponentLists = Record.readInt();
12054     Sizes.NumComponents = Record.readInt();
12055     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12056     break;
12057   }
12058   case llvm::omp::OMPC_use_device_addr: {
12059     OMPMappableExprListSizeTy Sizes;
12060     Sizes.NumVars = Record.readInt();
12061     Sizes.NumUniqueDeclarations = Record.readInt();
12062     Sizes.NumComponentLists = Record.readInt();
12063     Sizes.NumComponents = Record.readInt();
12064     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
12065     break;
12066   }
12067   case llvm::omp::OMPC_is_device_ptr: {
12068     OMPMappableExprListSizeTy Sizes;
12069     Sizes.NumVars = Record.readInt();
12070     Sizes.NumUniqueDeclarations = Record.readInt();
12071     Sizes.NumComponentLists = Record.readInt();
12072     Sizes.NumComponents = Record.readInt();
12073     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12074     break;
12075   }
12076   case llvm::omp::OMPC_allocate:
12077     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12078     break;
12079   case llvm::omp::OMPC_nontemporal:
12080     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
12081     break;
12082   case llvm::omp::OMPC_inclusive:
12083     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
12084     break;
12085   case llvm::omp::OMPC_exclusive:
12086     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
12087     break;
12088   case llvm::omp::OMPC_order:
12089     C = new (Context) OMPOrderClause();
12090     break;
12091   case llvm::omp::OMPC_destroy:
12092     C = new (Context) OMPDestroyClause();
12093     break;
12094   case llvm::omp::OMPC_detach:
12095     C = new (Context) OMPDetachClause();
12096     break;
12097   case llvm::omp::OMPC_uses_allocators:
12098     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
12099     break;
12100   case llvm::omp::OMPC_affinity:
12101     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
12102     break;
12103 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
12104   case llvm::omp::Enum:                                                        \
12105     break;
12106 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12107   default:
12108     break;
12109   }
12110   assert(C && "Unknown OMPClause type");
12111 
12112   Visit(C);
12113   C->setLocStart(Record.readSourceLocation());
12114   C->setLocEnd(Record.readSourceLocation());
12115 
12116   return C;
12117 }
12118 
12119 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12120   C->setPreInitStmt(Record.readSubStmt(),
12121                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12122 }
12123 
12124 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12125   VisitOMPClauseWithPreInit(C);
12126   C->setPostUpdateExpr(Record.readSubExpr());
12127 }
12128 
12129 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12130   VisitOMPClauseWithPreInit(C);
12131   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12132   C->setNameModifierLoc(Record.readSourceLocation());
12133   C->setColonLoc(Record.readSourceLocation());
12134   C->setCondition(Record.readSubExpr());
12135   C->setLParenLoc(Record.readSourceLocation());
12136 }
12137 
12138 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12139   VisitOMPClauseWithPreInit(C);
12140   C->setCondition(Record.readSubExpr());
12141   C->setLParenLoc(Record.readSourceLocation());
12142 }
12143 
12144 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12145   VisitOMPClauseWithPreInit(C);
12146   C->setNumThreads(Record.readSubExpr());
12147   C->setLParenLoc(Record.readSourceLocation());
12148 }
12149 
12150 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12151   C->setSafelen(Record.readSubExpr());
12152   C->setLParenLoc(Record.readSourceLocation());
12153 }
12154 
12155 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12156   C->setSimdlen(Record.readSubExpr());
12157   C->setLParenLoc(Record.readSourceLocation());
12158 }
12159 
12160 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12161   C->setAllocator(Record.readExpr());
12162   C->setLParenLoc(Record.readSourceLocation());
12163 }
12164 
12165 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12166   C->setNumForLoops(Record.readSubExpr());
12167   C->setLParenLoc(Record.readSourceLocation());
12168 }
12169 
12170 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12171   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12172   C->setLParenLoc(Record.readSourceLocation());
12173   C->setDefaultKindKwLoc(Record.readSourceLocation());
12174 }
12175 
12176 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12177   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12178   C->setLParenLoc(Record.readSourceLocation());
12179   C->setProcBindKindKwLoc(Record.readSourceLocation());
12180 }
12181 
12182 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12183   VisitOMPClauseWithPreInit(C);
12184   C->setScheduleKind(
12185        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12186   C->setFirstScheduleModifier(
12187       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12188   C->setSecondScheduleModifier(
12189       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12190   C->setChunkSize(Record.readSubExpr());
12191   C->setLParenLoc(Record.readSourceLocation());
12192   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12193   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12194   C->setScheduleKindLoc(Record.readSourceLocation());
12195   C->setCommaLoc(Record.readSourceLocation());
12196 }
12197 
12198 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12199   C->setNumForLoops(Record.readSubExpr());
12200   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12201     C->setLoopNumIterations(I, Record.readSubExpr());
12202   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12203     C->setLoopCounter(I, Record.readSubExpr());
12204   C->setLParenLoc(Record.readSourceLocation());
12205 }
12206 
12207 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12208   C->setEventHandler(Record.readSubExpr());
12209   C->setLParenLoc(Record.readSourceLocation());
12210 }
12211 
12212 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12213 
12214 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12215 
12216 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12217 
12218 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12219 
12220 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12221 
12222 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12223   if (C->isExtended()) {
12224     C->setLParenLoc(Record.readSourceLocation());
12225     C->setArgumentLoc(Record.readSourceLocation());
12226     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12227   }
12228 }
12229 
12230 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12231 
12232 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12233 
12234 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12235 
12236 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12237 
12238 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12239 
12240 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12241 
12242 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12243 
12244 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12245 
12246 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12247 
12248 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12249 
12250 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12251 
12252 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12253     OMPUnifiedSharedMemoryClause *) {}
12254 
12255 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12256 
12257 void
12258 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12259 }
12260 
12261 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12262     OMPAtomicDefaultMemOrderClause *C) {
12263   C->setAtomicDefaultMemOrderKind(
12264       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12265   C->setLParenLoc(Record.readSourceLocation());
12266   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12267 }
12268 
12269 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12270   C->setLParenLoc(Record.readSourceLocation());
12271   unsigned NumVars = C->varlist_size();
12272   SmallVector<Expr *, 16> Vars;
12273   Vars.reserve(NumVars);
12274   for (unsigned i = 0; i != NumVars; ++i)
12275     Vars.push_back(Record.readSubExpr());
12276   C->setVarRefs(Vars);
12277   Vars.clear();
12278   for (unsigned i = 0; i != NumVars; ++i)
12279     Vars.push_back(Record.readSubExpr());
12280   C->setPrivateCopies(Vars);
12281 }
12282 
12283 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12284   VisitOMPClauseWithPreInit(C);
12285   C->setLParenLoc(Record.readSourceLocation());
12286   unsigned NumVars = C->varlist_size();
12287   SmallVector<Expr *, 16> Vars;
12288   Vars.reserve(NumVars);
12289   for (unsigned i = 0; i != NumVars; ++i)
12290     Vars.push_back(Record.readSubExpr());
12291   C->setVarRefs(Vars);
12292   Vars.clear();
12293   for (unsigned i = 0; i != NumVars; ++i)
12294     Vars.push_back(Record.readSubExpr());
12295   C->setPrivateCopies(Vars);
12296   Vars.clear();
12297   for (unsigned i = 0; i != NumVars; ++i)
12298     Vars.push_back(Record.readSubExpr());
12299   C->setInits(Vars);
12300 }
12301 
12302 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12303   VisitOMPClauseWithPostUpdate(C);
12304   C->setLParenLoc(Record.readSourceLocation());
12305   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12306   C->setKindLoc(Record.readSourceLocation());
12307   C->setColonLoc(Record.readSourceLocation());
12308   unsigned NumVars = C->varlist_size();
12309   SmallVector<Expr *, 16> Vars;
12310   Vars.reserve(NumVars);
12311   for (unsigned i = 0; i != NumVars; ++i)
12312     Vars.push_back(Record.readSubExpr());
12313   C->setVarRefs(Vars);
12314   Vars.clear();
12315   for (unsigned i = 0; i != NumVars; ++i)
12316     Vars.push_back(Record.readSubExpr());
12317   C->setPrivateCopies(Vars);
12318   Vars.clear();
12319   for (unsigned i = 0; i != NumVars; ++i)
12320     Vars.push_back(Record.readSubExpr());
12321   C->setSourceExprs(Vars);
12322   Vars.clear();
12323   for (unsigned i = 0; i != NumVars; ++i)
12324     Vars.push_back(Record.readSubExpr());
12325   C->setDestinationExprs(Vars);
12326   Vars.clear();
12327   for (unsigned i = 0; i != NumVars; ++i)
12328     Vars.push_back(Record.readSubExpr());
12329   C->setAssignmentOps(Vars);
12330 }
12331 
12332 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12333   C->setLParenLoc(Record.readSourceLocation());
12334   unsigned NumVars = C->varlist_size();
12335   SmallVector<Expr *, 16> Vars;
12336   Vars.reserve(NumVars);
12337   for (unsigned i = 0; i != NumVars; ++i)
12338     Vars.push_back(Record.readSubExpr());
12339   C->setVarRefs(Vars);
12340 }
12341 
12342 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12343   VisitOMPClauseWithPostUpdate(C);
12344   C->setLParenLoc(Record.readSourceLocation());
12345   C->setModifierLoc(Record.readSourceLocation());
12346   C->setColonLoc(Record.readSourceLocation());
12347   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12348   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12349   C->setQualifierLoc(NNSL);
12350   C->setNameInfo(DNI);
12351 
12352   unsigned NumVars = C->varlist_size();
12353   SmallVector<Expr *, 16> Vars;
12354   Vars.reserve(NumVars);
12355   for (unsigned i = 0; i != NumVars; ++i)
12356     Vars.push_back(Record.readSubExpr());
12357   C->setVarRefs(Vars);
12358   Vars.clear();
12359   for (unsigned i = 0; i != NumVars; ++i)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setPrivates(Vars);
12362   Vars.clear();
12363   for (unsigned i = 0; i != NumVars; ++i)
12364     Vars.push_back(Record.readSubExpr());
12365   C->setLHSExprs(Vars);
12366   Vars.clear();
12367   for (unsigned i = 0; i != NumVars; ++i)
12368     Vars.push_back(Record.readSubExpr());
12369   C->setRHSExprs(Vars);
12370   Vars.clear();
12371   for (unsigned i = 0; i != NumVars; ++i)
12372     Vars.push_back(Record.readSubExpr());
12373   C->setReductionOps(Vars);
12374   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12375     Vars.clear();
12376     for (unsigned i = 0; i != NumVars; ++i)
12377       Vars.push_back(Record.readSubExpr());
12378     C->setInscanCopyOps(Vars);
12379     Vars.clear();
12380     for (unsigned i = 0; i != NumVars; ++i)
12381       Vars.push_back(Record.readSubExpr());
12382     C->setInscanCopyArrayTemps(Vars);
12383     Vars.clear();
12384     for (unsigned i = 0; i != NumVars; ++i)
12385       Vars.push_back(Record.readSubExpr());
12386     C->setInscanCopyArrayElems(Vars);
12387   }
12388 }
12389 
12390 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12391   VisitOMPClauseWithPostUpdate(C);
12392   C->setLParenLoc(Record.readSourceLocation());
12393   C->setColonLoc(Record.readSourceLocation());
12394   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12395   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12396   C->setQualifierLoc(NNSL);
12397   C->setNameInfo(DNI);
12398 
12399   unsigned NumVars = C->varlist_size();
12400   SmallVector<Expr *, 16> Vars;
12401   Vars.reserve(NumVars);
12402   for (unsigned I = 0; I != NumVars; ++I)
12403     Vars.push_back(Record.readSubExpr());
12404   C->setVarRefs(Vars);
12405   Vars.clear();
12406   for (unsigned I = 0; I != NumVars; ++I)
12407     Vars.push_back(Record.readSubExpr());
12408   C->setPrivates(Vars);
12409   Vars.clear();
12410   for (unsigned I = 0; I != NumVars; ++I)
12411     Vars.push_back(Record.readSubExpr());
12412   C->setLHSExprs(Vars);
12413   Vars.clear();
12414   for (unsigned I = 0; I != NumVars; ++I)
12415     Vars.push_back(Record.readSubExpr());
12416   C->setRHSExprs(Vars);
12417   Vars.clear();
12418   for (unsigned I = 0; I != NumVars; ++I)
12419     Vars.push_back(Record.readSubExpr());
12420   C->setReductionOps(Vars);
12421 }
12422 
12423 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12424   VisitOMPClauseWithPostUpdate(C);
12425   C->setLParenLoc(Record.readSourceLocation());
12426   C->setColonLoc(Record.readSourceLocation());
12427   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12428   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12429   C->setQualifierLoc(NNSL);
12430   C->setNameInfo(DNI);
12431 
12432   unsigned NumVars = C->varlist_size();
12433   SmallVector<Expr *, 16> Vars;
12434   Vars.reserve(NumVars);
12435   for (unsigned I = 0; I != NumVars; ++I)
12436     Vars.push_back(Record.readSubExpr());
12437   C->setVarRefs(Vars);
12438   Vars.clear();
12439   for (unsigned I = 0; I != NumVars; ++I)
12440     Vars.push_back(Record.readSubExpr());
12441   C->setPrivates(Vars);
12442   Vars.clear();
12443   for (unsigned I = 0; I != NumVars; ++I)
12444     Vars.push_back(Record.readSubExpr());
12445   C->setLHSExprs(Vars);
12446   Vars.clear();
12447   for (unsigned I = 0; I != NumVars; ++I)
12448     Vars.push_back(Record.readSubExpr());
12449   C->setRHSExprs(Vars);
12450   Vars.clear();
12451   for (unsigned I = 0; I != NumVars; ++I)
12452     Vars.push_back(Record.readSubExpr());
12453   C->setReductionOps(Vars);
12454   Vars.clear();
12455   for (unsigned I = 0; I != NumVars; ++I)
12456     Vars.push_back(Record.readSubExpr());
12457   C->setTaskgroupDescriptors(Vars);
12458 }
12459 
12460 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12461   VisitOMPClauseWithPostUpdate(C);
12462   C->setLParenLoc(Record.readSourceLocation());
12463   C->setColonLoc(Record.readSourceLocation());
12464   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12465   C->setModifierLoc(Record.readSourceLocation());
12466   unsigned NumVars = C->varlist_size();
12467   SmallVector<Expr *, 16> Vars;
12468   Vars.reserve(NumVars);
12469   for (unsigned i = 0; i != NumVars; ++i)
12470     Vars.push_back(Record.readSubExpr());
12471   C->setVarRefs(Vars);
12472   Vars.clear();
12473   for (unsigned i = 0; i != NumVars; ++i)
12474     Vars.push_back(Record.readSubExpr());
12475   C->setPrivates(Vars);
12476   Vars.clear();
12477   for (unsigned i = 0; i != NumVars; ++i)
12478     Vars.push_back(Record.readSubExpr());
12479   C->setInits(Vars);
12480   Vars.clear();
12481   for (unsigned i = 0; i != NumVars; ++i)
12482     Vars.push_back(Record.readSubExpr());
12483   C->setUpdates(Vars);
12484   Vars.clear();
12485   for (unsigned i = 0; i != NumVars; ++i)
12486     Vars.push_back(Record.readSubExpr());
12487   C->setFinals(Vars);
12488   C->setStep(Record.readSubExpr());
12489   C->setCalcStep(Record.readSubExpr());
12490   Vars.clear();
12491   for (unsigned I = 0; I != NumVars + 1; ++I)
12492     Vars.push_back(Record.readSubExpr());
12493   C->setUsedExprs(Vars);
12494 }
12495 
12496 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12497   C->setLParenLoc(Record.readSourceLocation());
12498   C->setColonLoc(Record.readSourceLocation());
12499   unsigned NumVars = C->varlist_size();
12500   SmallVector<Expr *, 16> Vars;
12501   Vars.reserve(NumVars);
12502   for (unsigned i = 0; i != NumVars; ++i)
12503     Vars.push_back(Record.readSubExpr());
12504   C->setVarRefs(Vars);
12505   C->setAlignment(Record.readSubExpr());
12506 }
12507 
12508 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12509   C->setLParenLoc(Record.readSourceLocation());
12510   unsigned NumVars = C->varlist_size();
12511   SmallVector<Expr *, 16> Exprs;
12512   Exprs.reserve(NumVars);
12513   for (unsigned i = 0; i != NumVars; ++i)
12514     Exprs.push_back(Record.readSubExpr());
12515   C->setVarRefs(Exprs);
12516   Exprs.clear();
12517   for (unsigned i = 0; i != NumVars; ++i)
12518     Exprs.push_back(Record.readSubExpr());
12519   C->setSourceExprs(Exprs);
12520   Exprs.clear();
12521   for (unsigned i = 0; i != NumVars; ++i)
12522     Exprs.push_back(Record.readSubExpr());
12523   C->setDestinationExprs(Exprs);
12524   Exprs.clear();
12525   for (unsigned i = 0; i != NumVars; ++i)
12526     Exprs.push_back(Record.readSubExpr());
12527   C->setAssignmentOps(Exprs);
12528 }
12529 
12530 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12531   C->setLParenLoc(Record.readSourceLocation());
12532   unsigned NumVars = C->varlist_size();
12533   SmallVector<Expr *, 16> Exprs;
12534   Exprs.reserve(NumVars);
12535   for (unsigned i = 0; i != NumVars; ++i)
12536     Exprs.push_back(Record.readSubExpr());
12537   C->setVarRefs(Exprs);
12538   Exprs.clear();
12539   for (unsigned i = 0; i != NumVars; ++i)
12540     Exprs.push_back(Record.readSubExpr());
12541   C->setSourceExprs(Exprs);
12542   Exprs.clear();
12543   for (unsigned i = 0; i != NumVars; ++i)
12544     Exprs.push_back(Record.readSubExpr());
12545   C->setDestinationExprs(Exprs);
12546   Exprs.clear();
12547   for (unsigned i = 0; i != NumVars; ++i)
12548     Exprs.push_back(Record.readSubExpr());
12549   C->setAssignmentOps(Exprs);
12550 }
12551 
12552 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12553   C->setLParenLoc(Record.readSourceLocation());
12554   unsigned NumVars = C->varlist_size();
12555   SmallVector<Expr *, 16> Vars;
12556   Vars.reserve(NumVars);
12557   for (unsigned i = 0; i != NumVars; ++i)
12558     Vars.push_back(Record.readSubExpr());
12559   C->setVarRefs(Vars);
12560 }
12561 
12562 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12563   C->setDepobj(Record.readSubExpr());
12564   C->setLParenLoc(Record.readSourceLocation());
12565 }
12566 
12567 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12568   C->setLParenLoc(Record.readSourceLocation());
12569   C->setModifier(Record.readSubExpr());
12570   C->setDependencyKind(
12571       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12572   C->setDependencyLoc(Record.readSourceLocation());
12573   C->setColonLoc(Record.readSourceLocation());
12574   unsigned NumVars = C->varlist_size();
12575   SmallVector<Expr *, 16> Vars;
12576   Vars.reserve(NumVars);
12577   for (unsigned I = 0; I != NumVars; ++I)
12578     Vars.push_back(Record.readSubExpr());
12579   C->setVarRefs(Vars);
12580   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12581     C->setLoopData(I, Record.readSubExpr());
12582 }
12583 
12584 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12585   VisitOMPClauseWithPreInit(C);
12586   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12587   C->setDevice(Record.readSubExpr());
12588   C->setModifierLoc(Record.readSourceLocation());
12589   C->setLParenLoc(Record.readSourceLocation());
12590 }
12591 
12592 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12593   C->setLParenLoc(Record.readSourceLocation());
12594   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12595     C->setMapTypeModifier(
12596         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12597     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12598   }
12599   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12600   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12601   C->setMapType(
12602      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12603   C->setMapLoc(Record.readSourceLocation());
12604   C->setColonLoc(Record.readSourceLocation());
12605   auto NumVars = C->varlist_size();
12606   auto UniqueDecls = C->getUniqueDeclarationsNum();
12607   auto TotalLists = C->getTotalComponentListNum();
12608   auto TotalComponents = C->getTotalComponentsNum();
12609 
12610   SmallVector<Expr *, 16> Vars;
12611   Vars.reserve(NumVars);
12612   for (unsigned i = 0; i != NumVars; ++i)
12613     Vars.push_back(Record.readExpr());
12614   C->setVarRefs(Vars);
12615 
12616   SmallVector<Expr *, 16> UDMappers;
12617   UDMappers.reserve(NumVars);
12618   for (unsigned I = 0; I < NumVars; ++I)
12619     UDMappers.push_back(Record.readExpr());
12620   C->setUDMapperRefs(UDMappers);
12621 
12622   SmallVector<ValueDecl *, 16> Decls;
12623   Decls.reserve(UniqueDecls);
12624   for (unsigned i = 0; i < UniqueDecls; ++i)
12625     Decls.push_back(Record.readDeclAs<ValueDecl>());
12626   C->setUniqueDecls(Decls);
12627 
12628   SmallVector<unsigned, 16> ListsPerDecl;
12629   ListsPerDecl.reserve(UniqueDecls);
12630   for (unsigned i = 0; i < UniqueDecls; ++i)
12631     ListsPerDecl.push_back(Record.readInt());
12632   C->setDeclNumLists(ListsPerDecl);
12633 
12634   SmallVector<unsigned, 32> ListSizes;
12635   ListSizes.reserve(TotalLists);
12636   for (unsigned i = 0; i < TotalLists; ++i)
12637     ListSizes.push_back(Record.readInt());
12638   C->setComponentListSizes(ListSizes);
12639 
12640   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12641   Components.reserve(TotalComponents);
12642   for (unsigned i = 0; i < TotalComponents; ++i) {
12643     Expr *AssociatedExpr = Record.readExpr();
12644     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12645     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12646         AssociatedExpr, AssociatedDecl));
12647   }
12648   C->setComponents(Components, ListSizes);
12649 }
12650 
12651 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12652   C->setLParenLoc(Record.readSourceLocation());
12653   C->setColonLoc(Record.readSourceLocation());
12654   C->setAllocator(Record.readSubExpr());
12655   unsigned NumVars = C->varlist_size();
12656   SmallVector<Expr *, 16> Vars;
12657   Vars.reserve(NumVars);
12658   for (unsigned i = 0; i != NumVars; ++i)
12659     Vars.push_back(Record.readSubExpr());
12660   C->setVarRefs(Vars);
12661 }
12662 
12663 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12664   VisitOMPClauseWithPreInit(C);
12665   C->setNumTeams(Record.readSubExpr());
12666   C->setLParenLoc(Record.readSourceLocation());
12667 }
12668 
12669 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12670   VisitOMPClauseWithPreInit(C);
12671   C->setThreadLimit(Record.readSubExpr());
12672   C->setLParenLoc(Record.readSourceLocation());
12673 }
12674 
12675 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12676   VisitOMPClauseWithPreInit(C);
12677   C->setPriority(Record.readSubExpr());
12678   C->setLParenLoc(Record.readSourceLocation());
12679 }
12680 
12681 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12682   VisitOMPClauseWithPreInit(C);
12683   C->setGrainsize(Record.readSubExpr());
12684   C->setLParenLoc(Record.readSourceLocation());
12685 }
12686 
12687 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12688   VisitOMPClauseWithPreInit(C);
12689   C->setNumTasks(Record.readSubExpr());
12690   C->setLParenLoc(Record.readSourceLocation());
12691 }
12692 
12693 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12694   C->setHint(Record.readSubExpr());
12695   C->setLParenLoc(Record.readSourceLocation());
12696 }
12697 
12698 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12699   VisitOMPClauseWithPreInit(C);
12700   C->setDistScheduleKind(
12701       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12702   C->setChunkSize(Record.readSubExpr());
12703   C->setLParenLoc(Record.readSourceLocation());
12704   C->setDistScheduleKindLoc(Record.readSourceLocation());
12705   C->setCommaLoc(Record.readSourceLocation());
12706 }
12707 
12708 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12709   C->setDefaultmapKind(
12710        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12711   C->setDefaultmapModifier(
12712       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12713   C->setLParenLoc(Record.readSourceLocation());
12714   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12715   C->setDefaultmapKindLoc(Record.readSourceLocation());
12716 }
12717 
12718 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12719   C->setLParenLoc(Record.readSourceLocation());
12720   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12721     C->setMotionModifier(
12722         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12723     C->setMotionModifierLoc(I, Record.readSourceLocation());
12724   }
12725   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12726   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12727   C->setColonLoc(Record.readSourceLocation());
12728   auto NumVars = C->varlist_size();
12729   auto UniqueDecls = C->getUniqueDeclarationsNum();
12730   auto TotalLists = C->getTotalComponentListNum();
12731   auto TotalComponents = C->getTotalComponentsNum();
12732 
12733   SmallVector<Expr *, 16> Vars;
12734   Vars.reserve(NumVars);
12735   for (unsigned i = 0; i != NumVars; ++i)
12736     Vars.push_back(Record.readSubExpr());
12737   C->setVarRefs(Vars);
12738 
12739   SmallVector<Expr *, 16> UDMappers;
12740   UDMappers.reserve(NumVars);
12741   for (unsigned I = 0; I < NumVars; ++I)
12742     UDMappers.push_back(Record.readSubExpr());
12743   C->setUDMapperRefs(UDMappers);
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::VisitOMPFromClause(OMPFromClause *C) {
12775   C->setLParenLoc(Record.readSourceLocation());
12776   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12777     C->setMotionModifier(
12778         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12779     C->setMotionModifierLoc(I, Record.readSourceLocation());
12780   }
12781   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12782   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12783   C->setColonLoc(Record.readSourceLocation());
12784   auto NumVars = C->varlist_size();
12785   auto UniqueDecls = C->getUniqueDeclarationsNum();
12786   auto TotalLists = C->getTotalComponentListNum();
12787   auto TotalComponents = C->getTotalComponentsNum();
12788 
12789   SmallVector<Expr *, 16> Vars;
12790   Vars.reserve(NumVars);
12791   for (unsigned i = 0; i != NumVars; ++i)
12792     Vars.push_back(Record.readSubExpr());
12793   C->setVarRefs(Vars);
12794 
12795   SmallVector<Expr *, 16> UDMappers;
12796   UDMappers.reserve(NumVars);
12797   for (unsigned I = 0; I < NumVars; ++I)
12798     UDMappers.push_back(Record.readSubExpr());
12799   C->setUDMapperRefs(UDMappers);
12800 
12801   SmallVector<ValueDecl *, 16> Decls;
12802   Decls.reserve(UniqueDecls);
12803   for (unsigned i = 0; i < UniqueDecls; ++i)
12804     Decls.push_back(Record.readDeclAs<ValueDecl>());
12805   C->setUniqueDecls(Decls);
12806 
12807   SmallVector<unsigned, 16> ListsPerDecl;
12808   ListsPerDecl.reserve(UniqueDecls);
12809   for (unsigned i = 0; i < UniqueDecls; ++i)
12810     ListsPerDecl.push_back(Record.readInt());
12811   C->setDeclNumLists(ListsPerDecl);
12812 
12813   SmallVector<unsigned, 32> ListSizes;
12814   ListSizes.reserve(TotalLists);
12815   for (unsigned i = 0; i < TotalLists; ++i)
12816     ListSizes.push_back(Record.readInt());
12817   C->setComponentListSizes(ListSizes);
12818 
12819   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12820   Components.reserve(TotalComponents);
12821   for (unsigned i = 0; i < TotalComponents; ++i) {
12822     Expr *AssociatedExpr = Record.readSubExpr();
12823     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12824     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12825         AssociatedExpr, AssociatedDecl));
12826   }
12827   C->setComponents(Components, ListSizes);
12828 }
12829 
12830 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12831   C->setLParenLoc(Record.readSourceLocation());
12832   auto NumVars = C->varlist_size();
12833   auto UniqueDecls = C->getUniqueDeclarationsNum();
12834   auto TotalLists = C->getTotalComponentListNum();
12835   auto TotalComponents = C->getTotalComponentsNum();
12836 
12837   SmallVector<Expr *, 16> Vars;
12838   Vars.reserve(NumVars);
12839   for (unsigned i = 0; i != NumVars; ++i)
12840     Vars.push_back(Record.readSubExpr());
12841   C->setVarRefs(Vars);
12842   Vars.clear();
12843   for (unsigned i = 0; i != NumVars; ++i)
12844     Vars.push_back(Record.readSubExpr());
12845   C->setPrivateCopies(Vars);
12846   Vars.clear();
12847   for (unsigned i = 0; i != NumVars; ++i)
12848     Vars.push_back(Record.readSubExpr());
12849   C->setInits(Vars);
12850 
12851   SmallVector<ValueDecl *, 16> Decls;
12852   Decls.reserve(UniqueDecls);
12853   for (unsigned i = 0; i < UniqueDecls; ++i)
12854     Decls.push_back(Record.readDeclAs<ValueDecl>());
12855   C->setUniqueDecls(Decls);
12856 
12857   SmallVector<unsigned, 16> ListsPerDecl;
12858   ListsPerDecl.reserve(UniqueDecls);
12859   for (unsigned i = 0; i < UniqueDecls; ++i)
12860     ListsPerDecl.push_back(Record.readInt());
12861   C->setDeclNumLists(ListsPerDecl);
12862 
12863   SmallVector<unsigned, 32> ListSizes;
12864   ListSizes.reserve(TotalLists);
12865   for (unsigned i = 0; i < TotalLists; ++i)
12866     ListSizes.push_back(Record.readInt());
12867   C->setComponentListSizes(ListSizes);
12868 
12869   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12870   Components.reserve(TotalComponents);
12871   for (unsigned i = 0; i < TotalComponents; ++i) {
12872     Expr *AssociatedExpr = Record.readSubExpr();
12873     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12874     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12875         AssociatedExpr, AssociatedDecl));
12876   }
12877   C->setComponents(Components, ListSizes);
12878 }
12879 
12880 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12881   C->setLParenLoc(Record.readSourceLocation());
12882   auto NumVars = C->varlist_size();
12883   auto UniqueDecls = C->getUniqueDeclarationsNum();
12884   auto TotalLists = C->getTotalComponentListNum();
12885   auto TotalComponents = C->getTotalComponentsNum();
12886 
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   SmallVector<ValueDecl *, 16> Decls;
12894   Decls.reserve(UniqueDecls);
12895   for (unsigned i = 0; i < UniqueDecls; ++i)
12896     Decls.push_back(Record.readDeclAs<ValueDecl>());
12897   C->setUniqueDecls(Decls);
12898 
12899   SmallVector<unsigned, 16> ListsPerDecl;
12900   ListsPerDecl.reserve(UniqueDecls);
12901   for (unsigned i = 0; i < UniqueDecls; ++i)
12902     ListsPerDecl.push_back(Record.readInt());
12903   C->setDeclNumLists(ListsPerDecl);
12904 
12905   SmallVector<unsigned, 32> ListSizes;
12906   ListSizes.reserve(TotalLists);
12907   for (unsigned i = 0; i < TotalLists; ++i)
12908     ListSizes.push_back(Record.readInt());
12909   C->setComponentListSizes(ListSizes);
12910 
12911   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12912   Components.reserve(TotalComponents);
12913   for (unsigned i = 0; i < TotalComponents; ++i) {
12914     Expr *AssociatedExpr = Record.readSubExpr();
12915     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12916     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12917         AssociatedExpr, AssociatedDecl));
12918   }
12919   C->setComponents(Components, ListSizes);
12920 }
12921 
12922 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12923   C->setLParenLoc(Record.readSourceLocation());
12924   auto NumVars = C->varlist_size();
12925   auto UniqueDecls = C->getUniqueDeclarationsNum();
12926   auto TotalLists = C->getTotalComponentListNum();
12927   auto TotalComponents = C->getTotalComponentsNum();
12928 
12929   SmallVector<Expr *, 16> Vars;
12930   Vars.reserve(NumVars);
12931   for (unsigned i = 0; i != NumVars; ++i)
12932     Vars.push_back(Record.readSubExpr());
12933   C->setVarRefs(Vars);
12934   Vars.clear();
12935 
12936   SmallVector<ValueDecl *, 16> Decls;
12937   Decls.reserve(UniqueDecls);
12938   for (unsigned i = 0; i < UniqueDecls; ++i)
12939     Decls.push_back(Record.readDeclAs<ValueDecl>());
12940   C->setUniqueDecls(Decls);
12941 
12942   SmallVector<unsigned, 16> ListsPerDecl;
12943   ListsPerDecl.reserve(UniqueDecls);
12944   for (unsigned i = 0; i < UniqueDecls; ++i)
12945     ListsPerDecl.push_back(Record.readInt());
12946   C->setDeclNumLists(ListsPerDecl);
12947 
12948   SmallVector<unsigned, 32> ListSizes;
12949   ListSizes.reserve(TotalLists);
12950   for (unsigned i = 0; i < TotalLists; ++i)
12951     ListSizes.push_back(Record.readInt());
12952   C->setComponentListSizes(ListSizes);
12953 
12954   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12955   Components.reserve(TotalComponents);
12956   for (unsigned i = 0; i < TotalComponents; ++i) {
12957     Expr *AssociatedExpr = Record.readSubExpr();
12958     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12959     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12960         AssociatedExpr, AssociatedDecl));
12961   }
12962   C->setComponents(Components, ListSizes);
12963 }
12964 
12965 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12966   C->setLParenLoc(Record.readSourceLocation());
12967   unsigned NumVars = C->varlist_size();
12968   SmallVector<Expr *, 16> Vars;
12969   Vars.reserve(NumVars);
12970   for (unsigned i = 0; i != NumVars; ++i)
12971     Vars.push_back(Record.readSubExpr());
12972   C->setVarRefs(Vars);
12973   Vars.clear();
12974   Vars.reserve(NumVars);
12975   for (unsigned i = 0; i != NumVars; ++i)
12976     Vars.push_back(Record.readSubExpr());
12977   C->setPrivateRefs(Vars);
12978 }
12979 
12980 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12981   C->setLParenLoc(Record.readSourceLocation());
12982   unsigned NumVars = C->varlist_size();
12983   SmallVector<Expr *, 16> Vars;
12984   Vars.reserve(NumVars);
12985   for (unsigned i = 0; i != NumVars; ++i)
12986     Vars.push_back(Record.readSubExpr());
12987   C->setVarRefs(Vars);
12988 }
12989 
12990 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12991   C->setLParenLoc(Record.readSourceLocation());
12992   unsigned NumVars = C->varlist_size();
12993   SmallVector<Expr *, 16> Vars;
12994   Vars.reserve(NumVars);
12995   for (unsigned i = 0; i != NumVars; ++i)
12996     Vars.push_back(Record.readSubExpr());
12997   C->setVarRefs(Vars);
12998 }
12999 
13000 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13001   C->setLParenLoc(Record.readSourceLocation());
13002   unsigned NumOfAllocators = C->getNumberOfAllocators();
13003   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13004   Data.reserve(NumOfAllocators);
13005   for (unsigned I = 0; I != NumOfAllocators; ++I) {
13006     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13007     D.Allocator = Record.readSubExpr();
13008     D.AllocatorTraits = Record.readSubExpr();
13009     D.LParenLoc = Record.readSourceLocation();
13010     D.RParenLoc = Record.readSourceLocation();
13011   }
13012   C->setAllocatorsData(Data);
13013 }
13014 
13015 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13016   C->setLParenLoc(Record.readSourceLocation());
13017   C->setModifier(Record.readSubExpr());
13018   C->setColonLoc(Record.readSourceLocation());
13019   unsigned NumOfLocators = C->varlist_size();
13020   SmallVector<Expr *, 4> Locators;
13021   Locators.reserve(NumOfLocators);
13022   for (unsigned I = 0; I != NumOfLocators; ++I)
13023     Locators.push_back(Record.readSubExpr());
13024   C->setVarRefs(Locators);
13025 }
13026 
13027 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13028   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13029   C->setLParenLoc(Record.readSourceLocation());
13030   C->setKindKwLoc(Record.readSourceLocation());
13031 }
13032 
13033 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13034   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13035   TI.Sets.resize(readUInt32());
13036   for (auto &Set : TI.Sets) {
13037     Set.Kind = readEnum<llvm::omp::TraitSet>();
13038     Set.Selectors.resize(readUInt32());
13039     for (auto &Selector : Set.Selectors) {
13040       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13041       Selector.ScoreOrCondition = nullptr;
13042       if (readBool())
13043         Selector.ScoreOrCondition = readExprRef();
13044       Selector.Properties.resize(readUInt32());
13045       for (auto &Property : Selector.Properties)
13046         Property.Kind = readEnum<llvm::omp::TraitProperty>();
13047     }
13048   }
13049   return &TI;
13050 }
13051 
13052 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13053   if (!Data)
13054     return;
13055   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13056     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13057     skipInts(3);
13058   }
13059   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13060   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13061     Clauses[I] = readOMPClause();
13062   Data->setClauses(Clauses);
13063   if (Data->hasAssociatedStmt())
13064     Data->setAssociatedStmt(readStmt());
13065   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13066     Data->getChildren()[I] = readStmt();
13067 }
13068