1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/MemoryBuffer.h"
118 #include "llvm/Support/Path.h"
119 #include "llvm/Support/SaveAndRestore.h"
120 #include "llvm/Support/Timer.h"
121 #include "llvm/Support/VersionTuple.h"
122 #include "llvm/Support/raw_ostream.h"
123 #include <algorithm>
124 #include <cassert>
125 #include <cstddef>
126 #include <cstdint>
127 #include <cstdio>
128 #include <ctime>
129 #include <iterator>
130 #include <limits>
131 #include <map>
132 #include <memory>
133 #include <string>
134 #include <system_error>
135 #include <tuple>
136 #include <utility>
137 #include <vector>
138 
139 using namespace clang;
140 using namespace clang::serialization;
141 using namespace clang::serialization::reader;
142 using llvm::BitstreamCursor;
143 using llvm::RoundingMode;
144 
145 //===----------------------------------------------------------------------===//
146 // ChainedASTReaderListener implementation
147 //===----------------------------------------------------------------------===//
148 
149 bool
150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151   return First->ReadFullVersionInformation(FullVersion) ||
152          Second->ReadFullVersionInformation(FullVersion);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156   First->ReadModuleName(ModuleName);
157   Second->ReadModuleName(ModuleName);
158 }
159 
160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161   First->ReadModuleMapFile(ModuleMapPath);
162   Second->ReadModuleMapFile(ModuleMapPath);
163 }
164 
165 bool
166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167                                               bool Complain,
168                                               bool AllowCompatibleDifferences) {
169   return First->ReadLanguageOptions(LangOpts, Complain,
170                                     AllowCompatibleDifferences) ||
171          Second->ReadLanguageOptions(LangOpts, Complain,
172                                      AllowCompatibleDifferences);
173 }
174 
175 bool ChainedASTReaderListener::ReadTargetOptions(
176     const TargetOptions &TargetOpts, bool Complain,
177     bool AllowCompatibleDifferences) {
178   return First->ReadTargetOptions(TargetOpts, Complain,
179                                   AllowCompatibleDifferences) ||
180          Second->ReadTargetOptions(TargetOpts, Complain,
181                                    AllowCompatibleDifferences);
182 }
183 
184 bool ChainedASTReaderListener::ReadDiagnosticOptions(
185     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187          Second->ReadDiagnosticOptions(DiagOpts, Complain);
188 }
189 
190 bool
191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192                                                 bool Complain) {
193   return First->ReadFileSystemOptions(FSOpts, Complain) ||
194          Second->ReadFileSystemOptions(FSOpts, Complain);
195 }
196 
197 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199     bool Complain) {
200   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                         Complain) ||
202          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                          Complain);
204 }
205 
206 bool ChainedASTReaderListener::ReadPreprocessorOptions(
207     const PreprocessorOptions &PPOpts, bool Complain,
208     std::string &SuggestedPredefines) {
209   return First->ReadPreprocessorOptions(PPOpts, Complain,
210                                         SuggestedPredefines) ||
211          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212 }
213 
214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215                                            unsigned Value) {
216   First->ReadCounter(M, Value);
217   Second->ReadCounter(M, Value);
218 }
219 
220 bool ChainedASTReaderListener::needsInputFileVisitation() {
221   return First->needsInputFileVisitation() ||
222          Second->needsInputFileVisitation();
223 }
224 
225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226   return First->needsSystemInputFileVisitation() ||
227   Second->needsSystemInputFileVisitation();
228 }
229 
230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231                                                ModuleKind Kind) {
232   First->visitModuleFile(Filename, Kind);
233   Second->visitModuleFile(Filename, Kind);
234 }
235 
236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237                                               bool isSystem,
238                                               bool isOverridden,
239                                               bool isExplicitModule) {
240   bool Continue = false;
241   if (First->needsInputFileVisitation() &&
242       (!isSystem || First->needsSystemInputFileVisitation()))
243     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244                                       isExplicitModule);
245   if (Second->needsInputFileVisitation() &&
246       (!isSystem || Second->needsSystemInputFileVisitation()))
247     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248                                        isExplicitModule);
249   return Continue;
250 }
251 
252 void ChainedASTReaderListener::readModuleFileExtension(
253        const ModuleFileExtensionMetadata &Metadata) {
254   First->readModuleFileExtension(Metadata);
255   Second->readModuleFileExtension(Metadata);
256 }
257 
258 //===----------------------------------------------------------------------===//
259 // PCH validator implementation
260 //===----------------------------------------------------------------------===//
261 
262 ASTReaderListener::~ASTReaderListener() = default;
263 
264 /// Compare the given set of language options against an existing set of
265 /// language options.
266 ///
267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268 /// \param AllowCompatibleDifferences If true, differences between compatible
269 ///        language options will be permitted.
270 ///
271 /// \returns true if the languagae options mis-match, false otherwise.
272 static bool checkLanguageOptions(const LangOptions &LangOpts,
273                                  const LangOptions &ExistingLangOpts,
274                                  DiagnosticsEngine *Diags,
275                                  bool AllowCompatibleDifferences = true) {
276 #define LANGOPT(Name, Bits, Default, Description)                 \
277   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
278     if (Diags)                                                    \
279       Diags->Report(diag::err_pch_langopt_mismatch)               \
280         << Description << LangOpts.Name << ExistingLangOpts.Name; \
281     return true;                                                  \
282   }
283 
284 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
285   if (ExistingLangOpts.Name != LangOpts.Name) {           \
286     if (Diags)                                            \
287       Diags->Report(diag::err_pch_langopt_value_mismatch) \
288         << Description;                                   \
289     return true;                                          \
290   }
291 
292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
293   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
294     if (Diags)                                                 \
295       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
296         << Description;                                        \
297     return true;                                               \
298   }
299 
300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
301   if (!AllowCompatibleDifferences)                            \
302     LANGOPT(Name, Bits, Default, Description)
303 
304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
305   if (!AllowCompatibleDifferences)                                 \
306     ENUM_LANGOPT(Name, Bits, Default, Description)
307 
308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309   if (!AllowCompatibleDifferences)                                 \
310     VALUE_LANGOPT(Name, Bits, Default, Description)
311 
312 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315 #include "clang/Basic/LangOptions.def"
316 
317   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318     if (Diags)
319       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320     return true;
321   }
322 
323   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324     if (Diags)
325       Diags->Report(diag::err_pch_langopt_value_mismatch)
326       << "target Objective-C runtime";
327     return true;
328   }
329 
330   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331       LangOpts.CommentOpts.BlockCommandNames) {
332     if (Diags)
333       Diags->Report(diag::err_pch_langopt_value_mismatch)
334         << "block command names";
335     return true;
336   }
337 
338   // Sanitizer feature mismatches are treated as compatible differences. If
339   // compatible differences aren't allowed, we still only want to check for
340   // mismatches of non-modular sanitizers (the only ones which can affect AST
341   // generation).
342   if (!AllowCompatibleDifferences) {
343     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346     ExistingSanitizers.clear(ModularSanitizers);
347     ImportedSanitizers.clear(ModularSanitizers);
348     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349       const std::string Flag = "-fsanitize=";
350       if (Diags) {
351 #define SANITIZER(NAME, ID)                                                    \
352   {                                                                            \
353     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
354     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
355     if (InExistingModule != InImportedModule)                                  \
356       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
357           << InExistingModule << (Flag + NAME);                                \
358   }
359 #include "clang/Basic/Sanitizers.def"
360       }
361       return true;
362     }
363   }
364 
365   return false;
366 }
367 
368 /// Compare the given set of target options against an existing set of
369 /// target options.
370 ///
371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372 ///
373 /// \returns true if the target options mis-match, false otherwise.
374 static bool checkTargetOptions(const TargetOptions &TargetOpts,
375                                const TargetOptions &ExistingTargetOpts,
376                                DiagnosticsEngine *Diags,
377                                bool AllowCompatibleDifferences = true) {
378 #define CHECK_TARGET_OPT(Field, Name)                             \
379   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
380     if (Diags)                                                    \
381       Diags->Report(diag::err_pch_targetopt_mismatch)             \
382         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
383     return true;                                                  \
384   }
385 
386   // The triple and ABI must match exactly.
387   CHECK_TARGET_OPT(Triple, "target");
388   CHECK_TARGET_OPT(ABI, "target ABI");
389 
390   // We can tolerate different CPUs in many cases, notably when one CPU
391   // supports a strict superset of another. When allowing compatible
392   // differences skip this check.
393   if (!AllowCompatibleDifferences) {
394     CHECK_TARGET_OPT(CPU, "target CPU");
395     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
396   }
397 
398 #undef CHECK_TARGET_OPT
399 
400   // Compare feature sets.
401   SmallVector<StringRef, 4> ExistingFeatures(
402                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
403                                              ExistingTargetOpts.FeaturesAsWritten.end());
404   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
405                                          TargetOpts.FeaturesAsWritten.end());
406   llvm::sort(ExistingFeatures);
407   llvm::sort(ReadFeatures);
408 
409   // We compute the set difference in both directions explicitly so that we can
410   // diagnose the differences differently.
411   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
412   std::set_difference(
413       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
414       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
415   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
416                       ExistingFeatures.begin(), ExistingFeatures.end(),
417                       std::back_inserter(UnmatchedReadFeatures));
418 
419   // If we are allowing compatible differences and the read feature set is
420   // a strict subset of the existing feature set, there is nothing to diagnose.
421   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
422     return false;
423 
424   if (Diags) {
425     for (StringRef Feature : UnmatchedReadFeatures)
426       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
427           << /* is-existing-feature */ false << Feature;
428     for (StringRef Feature : UnmatchedExistingFeatures)
429       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
430           << /* is-existing-feature */ true << Feature;
431   }
432 
433   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
434 }
435 
436 bool
437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
438                                   bool Complain,
439                                   bool AllowCompatibleDifferences) {
440   const LangOptions &ExistingLangOpts = PP.getLangOpts();
441   return checkLanguageOptions(LangOpts, ExistingLangOpts,
442                               Complain ? &Reader.Diags : nullptr,
443                               AllowCompatibleDifferences);
444 }
445 
446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
447                                      bool Complain,
448                                      bool AllowCompatibleDifferences) {
449   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
450   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
451                             Complain ? &Reader.Diags : nullptr,
452                             AllowCompatibleDifferences);
453 }
454 
455 namespace {
456 
457 using MacroDefinitionsMap =
458     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
460 
461 } // namespace
462 
463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
464                                          DiagnosticsEngine &Diags,
465                                          bool Complain) {
466   using Level = DiagnosticsEngine::Level;
467 
468   // Check current mappings for new -Werror mappings, and the stored mappings
469   // for cases that were explicitly mapped to *not* be errors that are now
470   // errors because of options like -Werror.
471   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
472 
473   for (DiagnosticsEngine *MappingSource : MappingSources) {
474     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
475       diag::kind DiagID = DiagIDMappingPair.first;
476       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
477       if (CurLevel < DiagnosticsEngine::Error)
478         continue; // not significant
479       Level StoredLevel =
480           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
481       if (StoredLevel < DiagnosticsEngine::Error) {
482         if (Complain)
483           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
484               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
485         return true;
486       }
487     }
488   }
489 
490   return false;
491 }
492 
493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
494   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
495   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
496     return true;
497   return Ext >= diag::Severity::Error;
498 }
499 
500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
501                                     DiagnosticsEngine &Diags,
502                                     bool IsSystem, bool Complain) {
503   // Top-level options
504   if (IsSystem) {
505     if (Diags.getSuppressSystemWarnings())
506       return false;
507     // If -Wsystem-headers was not enabled before, be conservative
508     if (StoredDiags.getSuppressSystemWarnings()) {
509       if (Complain)
510         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
511       return true;
512     }
513   }
514 
515   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
518     return true;
519   }
520 
521   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
522       !StoredDiags.getEnableAllWarnings()) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
525     return true;
526   }
527 
528   if (isExtHandlingFromDiagsError(Diags) &&
529       !isExtHandlingFromDiagsError(StoredDiags)) {
530     if (Complain)
531       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
532     return true;
533   }
534 
535   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
536 }
537 
538 /// Return the top import module if it is implicit, nullptr otherwise.
539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
540                                           Preprocessor &PP) {
541   // If the original import came from a file explicitly generated by the user,
542   // don't check the diagnostic mappings.
543   // FIXME: currently this is approximated by checking whether this is not a
544   // module import of an implicitly-loaded module file.
545   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
546   // the transitive closure of its imports, since unrelated modules cannot be
547   // imported until after this module finishes validation.
548   ModuleFile *TopImport = &*ModuleMgr.rbegin();
549   while (!TopImport->ImportedBy.empty())
550     TopImport = TopImport->ImportedBy[0];
551   if (TopImport->Kind != MK_ImplicitModule)
552     return nullptr;
553 
554   StringRef ModuleName = TopImport->ModuleName;
555   assert(!ModuleName.empty() && "diagnostic options read before module name");
556 
557   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
558   assert(M && "missing module");
559   return M;
560 }
561 
562 bool PCHValidator::ReadDiagnosticOptions(
563     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
564   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
565   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
566   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
567       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
568   // This should never fail, because we would have processed these options
569   // before writing them to an ASTFile.
570   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
571 
572   ModuleManager &ModuleMgr = Reader.getModuleManager();
573   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
574 
575   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
576   if (!TopM)
577     return false;
578 
579   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
580   // contains the union of their flags.
581   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
582                                  Complain);
583 }
584 
585 /// Collect the macro definitions provided by the given preprocessor
586 /// options.
587 static void
588 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
589                         MacroDefinitionsMap &Macros,
590                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
591   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
592     StringRef Macro = PPOpts.Macros[I].first;
593     bool IsUndef = PPOpts.Macros[I].second;
594 
595     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
596     StringRef MacroName = MacroPair.first;
597     StringRef MacroBody = MacroPair.second;
598 
599     // For an #undef'd macro, we only care about the name.
600     if (IsUndef) {
601       if (MacroNames && !Macros.count(MacroName))
602         MacroNames->push_back(MacroName);
603 
604       Macros[MacroName] = std::make_pair("", true);
605       continue;
606     }
607 
608     // For a #define'd macro, figure out the actual definition.
609     if (MacroName.size() == Macro.size())
610       MacroBody = "1";
611     else {
612       // Note: GCC drops anything following an end-of-line character.
613       StringRef::size_type End = MacroBody.find_first_of("\n\r");
614       MacroBody = MacroBody.substr(0, End);
615     }
616 
617     if (MacroNames && !Macros.count(MacroName))
618       MacroNames->push_back(MacroName);
619     Macros[MacroName] = std::make_pair(MacroBody, false);
620   }
621 }
622 
623 /// Check the preprocessor options deserialized from the control block
624 /// against the preprocessor options in an existing preprocessor.
625 ///
626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
627 /// \param Validate If true, validate preprocessor options. If false, allow
628 ///        macros defined by \p ExistingPPOpts to override those defined by
629 ///        \p PPOpts in SuggestedPredefines.
630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
631                                      const PreprocessorOptions &ExistingPPOpts,
632                                      DiagnosticsEngine *Diags,
633                                      FileManager &FileMgr,
634                                      std::string &SuggestedPredefines,
635                                      const LangOptions &LangOpts,
636                                      bool Validate = true) {
637   // Check macro definitions.
638   MacroDefinitionsMap ASTFileMacros;
639   collectMacroDefinitions(PPOpts, ASTFileMacros);
640   MacroDefinitionsMap ExistingMacros;
641   SmallVector<StringRef, 4> ExistingMacroNames;
642   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
643 
644   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
645     // Dig out the macro definition in the existing preprocessor options.
646     StringRef MacroName = ExistingMacroNames[I];
647     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
648 
649     // Check whether we know anything about this macro name or not.
650     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
651         ASTFileMacros.find(MacroName);
652     if (!Validate || Known == ASTFileMacros.end()) {
653       // FIXME: Check whether this identifier was referenced anywhere in the
654       // AST file. If so, we should reject the AST file. Unfortunately, this
655       // information isn't in the control block. What shall we do about it?
656 
657       if (Existing.second) {
658         SuggestedPredefines += "#undef ";
659         SuggestedPredefines += MacroName.str();
660         SuggestedPredefines += '\n';
661       } else {
662         SuggestedPredefines += "#define ";
663         SuggestedPredefines += MacroName.str();
664         SuggestedPredefines += ' ';
665         SuggestedPredefines += Existing.first.str();
666         SuggestedPredefines += '\n';
667       }
668       continue;
669     }
670 
671     // If the macro was defined in one but undef'd in the other, we have a
672     // conflict.
673     if (Existing.second != Known->second.second) {
674       if (Diags) {
675         Diags->Report(diag::err_pch_macro_def_undef)
676           << MacroName << Known->second.second;
677       }
678       return true;
679     }
680 
681     // If the macro was #undef'd in both, or if the macro bodies are identical,
682     // it's fine.
683     if (Existing.second || Existing.first == Known->second.first)
684       continue;
685 
686     // The macro bodies differ; complain.
687     if (Diags) {
688       Diags->Report(diag::err_pch_macro_def_conflict)
689         << MacroName << Known->second.first << Existing.first;
690     }
691     return true;
692   }
693 
694   // Check whether we're using predefines.
695   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
696     if (Diags) {
697       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
698     }
699     return true;
700   }
701 
702   // Detailed record is important since it is used for the module cache hash.
703   if (LangOpts.Modules &&
704       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
705     if (Diags) {
706       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
707     }
708     return true;
709   }
710 
711   // Compute the #include and #include_macros lines we need.
712   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
713     StringRef File = ExistingPPOpts.Includes[I];
714 
715     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
716         !ExistingPPOpts.PCHThroughHeader.empty()) {
717       // In case the through header is an include, we must add all the includes
718       // to the predefines so the start point can be determined.
719       SuggestedPredefines += "#include \"";
720       SuggestedPredefines += File;
721       SuggestedPredefines += "\"\n";
722       continue;
723     }
724 
725     if (File == ExistingPPOpts.ImplicitPCHInclude)
726       continue;
727 
728     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
729           != PPOpts.Includes.end())
730       continue;
731 
732     SuggestedPredefines += "#include \"";
733     SuggestedPredefines += File;
734     SuggestedPredefines += "\"\n";
735   }
736 
737   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
738     StringRef File = ExistingPPOpts.MacroIncludes[I];
739     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
740                   File)
741         != PPOpts.MacroIncludes.end())
742       continue;
743 
744     SuggestedPredefines += "#__include_macros \"";
745     SuggestedPredefines += File;
746     SuggestedPredefines += "\"\n##\n";
747   }
748 
749   return false;
750 }
751 
752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
753                                            bool Complain,
754                                            std::string &SuggestedPredefines) {
755   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
756 
757   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
758                                   Complain? &Reader.Diags : nullptr,
759                                   PP.getFileManager(),
760                                   SuggestedPredefines,
761                                   PP.getLangOpts());
762 }
763 
764 bool SimpleASTReaderListener::ReadPreprocessorOptions(
765                                   const PreprocessorOptions &PPOpts,
766                                   bool Complain,
767                                   std::string &SuggestedPredefines) {
768   return checkPreprocessorOptions(PPOpts,
769                                   PP.getPreprocessorOpts(),
770                                   nullptr,
771                                   PP.getFileManager(),
772                                   SuggestedPredefines,
773                                   PP.getLangOpts(),
774                                   false);
775 }
776 
777 /// Check the header search options deserialized from the control block
778 /// against the header search options in an existing preprocessor.
779 ///
780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
782                                      StringRef SpecificModuleCachePath,
783                                      StringRef ExistingModuleCachePath,
784                                      DiagnosticsEngine *Diags,
785                                      const LangOptions &LangOpts) {
786   if (LangOpts.Modules) {
787     if (SpecificModuleCachePath != ExistingModuleCachePath) {
788       if (Diags)
789         Diags->Report(diag::err_pch_modulecache_mismatch)
790           << SpecificModuleCachePath << ExistingModuleCachePath;
791       return true;
792     }
793   }
794 
795   return false;
796 }
797 
798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
799                                            StringRef SpecificModuleCachePath,
800                                            bool Complain) {
801   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
802                                   PP.getHeaderSearchInfo().getModuleCachePath(),
803                                   Complain ? &Reader.Diags : nullptr,
804                                   PP.getLangOpts());
805 }
806 
807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
808   PP.setCounterValue(Value);
809 }
810 
811 //===----------------------------------------------------------------------===//
812 // AST reader implementation
813 //===----------------------------------------------------------------------===//
814 
815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
816                                            bool TakeOwnership) {
817   DeserializationListener = Listener;
818   OwnsDeserializationListener = TakeOwnership;
819 }
820 
821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
822   return serialization::ComputeHash(Sel);
823 }
824 
825 std::pair<unsigned, unsigned>
826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
827   using namespace llvm::support;
828 
829   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
830   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
831   return std::make_pair(KeyLen, DataLen);
832 }
833 
834 ASTSelectorLookupTrait::internal_key_type
835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
836   using namespace llvm::support;
837 
838   SelectorTable &SelTable = Reader.getContext().Selectors;
839   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
840   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
841       F, endian::readNext<uint32_t, little, unaligned>(d));
842   if (N == 0)
843     return SelTable.getNullarySelector(FirstII);
844   else if (N == 1)
845     return SelTable.getUnarySelector(FirstII);
846 
847   SmallVector<IdentifierInfo *, 16> Args;
848   Args.push_back(FirstII);
849   for (unsigned I = 1; I != N; ++I)
850     Args.push_back(Reader.getLocalIdentifier(
851         F, endian::readNext<uint32_t, little, unaligned>(d)));
852 
853   return SelTable.getSelector(N, Args.data());
854 }
855 
856 ASTSelectorLookupTrait::data_type
857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
858                                  unsigned DataLen) {
859   using namespace llvm::support;
860 
861   data_type Result;
862 
863   Result.ID = Reader.getGlobalSelectorID(
864       F, endian::readNext<uint32_t, little, unaligned>(d));
865   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
866   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
867   Result.InstanceBits = FullInstanceBits & 0x3;
868   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
869   Result.FactoryBits = FullFactoryBits & 0x3;
870   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
871   unsigned NumInstanceMethods = FullInstanceBits >> 3;
872   unsigned NumFactoryMethods = FullFactoryBits >> 3;
873 
874   // Load instance methods
875   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Instance.push_back(Method);
879   }
880 
881   // Load factory methods
882   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
883     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
884             F, endian::readNext<uint32_t, little, unaligned>(d)))
885       Result.Factory.push_back(Method);
886   }
887 
888   return Result;
889 }
890 
891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
892   return llvm::djbHash(a);
893 }
894 
895 std::pair<unsigned, unsigned>
896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
897   using namespace llvm::support;
898 
899   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
900   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
901   return std::make_pair(KeyLen, DataLen);
902 }
903 
904 ASTIdentifierLookupTraitBase::internal_key_type
905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
906   assert(n >= 2 && d[n-1] == '\0');
907   return StringRef((const char*) d, n-1);
908 }
909 
910 /// Whether the given identifier is "interesting".
911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
912                                     bool IsModule) {
913   return II.hadMacroDefinition() || II.isPoisoned() ||
914          (!IsModule && II.getObjCOrBuiltinID()) ||
915          II.hasRevertedTokenIDToIdentifier() ||
916          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
917           II.getFETokenInfo());
918 }
919 
920 static bool readBit(unsigned &Bits) {
921   bool Value = Bits & 0x1;
922   Bits >>= 1;
923   return Value;
924 }
925 
926 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
927   using namespace llvm::support;
928 
929   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
930   return Reader.getGlobalIdentifierID(F, RawID >> 1);
931 }
932 
933 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
934   if (!II.isFromAST()) {
935     II.setIsFromAST();
936     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
937     if (isInterestingIdentifier(Reader, II, IsModule))
938       II.setChangedSinceDeserialization();
939   }
940 }
941 
942 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
943                                                    const unsigned char* d,
944                                                    unsigned DataLen) {
945   using namespace llvm::support;
946 
947   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948   bool IsInteresting = RawID & 0x01;
949 
950   // Wipe out the "is interesting" bit.
951   RawID = RawID >> 1;
952 
953   // Build the IdentifierInfo and link the identifier ID with it.
954   IdentifierInfo *II = KnownII;
955   if (!II) {
956     II = &Reader.getIdentifierTable().getOwn(k);
957     KnownII = II;
958   }
959   markIdentifierFromAST(Reader, *II);
960   Reader.markIdentifierUpToDate(II);
961 
962   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
963   if (!IsInteresting) {
964     // For uninteresting identifiers, there's nothing else to do. Just notify
965     // the reader that we've finished loading this identifier.
966     Reader.SetIdentifierInfo(ID, II);
967     return II;
968   }
969 
970   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
971   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
972   bool CPlusPlusOperatorKeyword = readBit(Bits);
973   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
974   bool Poisoned = readBit(Bits);
975   bool ExtensionToken = readBit(Bits);
976   bool HadMacroDefinition = readBit(Bits);
977 
978   assert(Bits == 0 && "Extra bits in the identifier?");
979   DataLen -= 8;
980 
981   // Set or check the various bits in the IdentifierInfo structure.
982   // Token IDs are read-only.
983   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
984     II->revertTokenIDToIdentifier();
985   if (!F.isModule())
986     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987   assert(II->isExtensionToken() == ExtensionToken &&
988          "Incorrect extension token flag");
989   (void)ExtensionToken;
990   if (Poisoned)
991     II->setIsPoisoned(true);
992   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
993          "Incorrect C++ operator keyword flag");
994   (void)CPlusPlusOperatorKeyword;
995 
996   // If this identifier is a macro, deserialize the macro
997   // definition.
998   if (HadMacroDefinition) {
999     uint32_t MacroDirectivesOffset =
1000         endian::readNext<uint32_t, little, unaligned>(d);
1001     DataLen -= 4;
1002 
1003     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1004   }
1005 
1006   Reader.SetIdentifierInfo(ID, II);
1007 
1008   // Read all of the declarations visible at global scope with this
1009   // name.
1010   if (DataLen > 0) {
1011     SmallVector<uint32_t, 4> DeclIDs;
1012     for (; DataLen > 0; DataLen -= 4)
1013       DeclIDs.push_back(Reader.getGlobalDeclID(
1014           F, endian::readNext<uint32_t, little, unaligned>(d)));
1015     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1016   }
1017 
1018   return II;
1019 }
1020 
1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1022     : Kind(Name.getNameKind()) {
1023   switch (Kind) {
1024   case DeclarationName::Identifier:
1025     Data = (uint64_t)Name.getAsIdentifierInfo();
1026     break;
1027   case DeclarationName::ObjCZeroArgSelector:
1028   case DeclarationName::ObjCOneArgSelector:
1029   case DeclarationName::ObjCMultiArgSelector:
1030     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1031     break;
1032   case DeclarationName::CXXOperatorName:
1033     Data = Name.getCXXOverloadedOperator();
1034     break;
1035   case DeclarationName::CXXLiteralOperatorName:
1036     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1037     break;
1038   case DeclarationName::CXXDeductionGuideName:
1039     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1040                ->getDeclName().getAsIdentifierInfo();
1041     break;
1042   case DeclarationName::CXXConstructorName:
1043   case DeclarationName::CXXDestructorName:
1044   case DeclarationName::CXXConversionFunctionName:
1045   case DeclarationName::CXXUsingDirective:
1046     Data = 0;
1047     break;
1048   }
1049 }
1050 
1051 unsigned DeclarationNameKey::getHash() const {
1052   llvm::FoldingSetNodeID ID;
1053   ID.AddInteger(Kind);
1054 
1055   switch (Kind) {
1056   case DeclarationName::Identifier:
1057   case DeclarationName::CXXLiteralOperatorName:
1058   case DeclarationName::CXXDeductionGuideName:
1059     ID.AddString(((IdentifierInfo*)Data)->getName());
1060     break;
1061   case DeclarationName::ObjCZeroArgSelector:
1062   case DeclarationName::ObjCOneArgSelector:
1063   case DeclarationName::ObjCMultiArgSelector:
1064     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1065     break;
1066   case DeclarationName::CXXOperatorName:
1067     ID.AddInteger((OverloadedOperatorKind)Data);
1068     break;
1069   case DeclarationName::CXXConstructorName:
1070   case DeclarationName::CXXDestructorName:
1071   case DeclarationName::CXXConversionFunctionName:
1072   case DeclarationName::CXXUsingDirective:
1073     break;
1074   }
1075 
1076   return ID.ComputeHash();
1077 }
1078 
1079 ModuleFile *
1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1081   using namespace llvm::support;
1082 
1083   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1084   return Reader.getLocalModuleFile(F, ModuleFileID);
1085 }
1086 
1087 std::pair<unsigned, unsigned>
1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1089   using namespace llvm::support;
1090 
1091   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1092   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   return std::make_pair(KeyLen, DataLen);
1094 }
1095 
1096 ASTDeclContextNameLookupTrait::internal_key_type
1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1098   using namespace llvm::support;
1099 
1100   auto Kind = (DeclarationName::NameKind)*d++;
1101   uint64_t Data;
1102   switch (Kind) {
1103   case DeclarationName::Identifier:
1104   case DeclarationName::CXXLiteralOperatorName:
1105   case DeclarationName::CXXDeductionGuideName:
1106     Data = (uint64_t)Reader.getLocalIdentifier(
1107         F, endian::readNext<uint32_t, little, unaligned>(d));
1108     break;
1109   case DeclarationName::ObjCZeroArgSelector:
1110   case DeclarationName::ObjCOneArgSelector:
1111   case DeclarationName::ObjCMultiArgSelector:
1112     Data =
1113         (uint64_t)Reader.getLocalSelector(
1114                              F, endian::readNext<uint32_t, little, unaligned>(
1115                                     d)).getAsOpaquePtr();
1116     break;
1117   case DeclarationName::CXXOperatorName:
1118     Data = *d++; // OverloadedOperatorKind
1119     break;
1120   case DeclarationName::CXXConstructorName:
1121   case DeclarationName::CXXDestructorName:
1122   case DeclarationName::CXXConversionFunctionName:
1123   case DeclarationName::CXXUsingDirective:
1124     Data = 0;
1125     break;
1126   }
1127 
1128   return DeclarationNameKey(Kind, Data);
1129 }
1130 
1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1132                                                  const unsigned char *d,
1133                                                  unsigned DataLen,
1134                                                  data_type_builder &Val) {
1135   using namespace llvm::support;
1136 
1137   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1138     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1139     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1140   }
1141 }
1142 
1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1144                                               BitstreamCursor &Cursor,
1145                                               uint64_t Offset,
1146                                               DeclContext *DC) {
1147   assert(Offset != 0);
1148 
1149   SavedStreamPosition SavedPosition(Cursor);
1150   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1151     Error(std::move(Err));
1152     return true;
1153   }
1154 
1155   RecordData Record;
1156   StringRef Blob;
1157   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1158   if (!MaybeCode) {
1159     Error(MaybeCode.takeError());
1160     return true;
1161   }
1162   unsigned Code = MaybeCode.get();
1163 
1164   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1165   if (!MaybeRecCode) {
1166     Error(MaybeRecCode.takeError());
1167     return true;
1168   }
1169   unsigned RecCode = MaybeRecCode.get();
1170   if (RecCode != DECL_CONTEXT_LEXICAL) {
1171     Error("Expected lexical block");
1172     return true;
1173   }
1174 
1175   assert(!isa<TranslationUnitDecl>(DC) &&
1176          "expected a TU_UPDATE_LEXICAL record for TU");
1177   // If we are handling a C++ class template instantiation, we can see multiple
1178   // lexical updates for the same record. It's important that we select only one
1179   // of them, so that field numbering works properly. Just pick the first one we
1180   // see.
1181   auto &Lex = LexicalDecls[DC];
1182   if (!Lex.first) {
1183     Lex = std::make_pair(
1184         &M, llvm::makeArrayRef(
1185                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1186                     Blob.data()),
1187                 Blob.size() / 4));
1188   }
1189   DC->setHasExternalLexicalStorage(true);
1190   return false;
1191 }
1192 
1193 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1194                                               BitstreamCursor &Cursor,
1195                                               uint64_t Offset,
1196                                               DeclID ID) {
1197   assert(Offset != 0);
1198 
1199   SavedStreamPosition SavedPosition(Cursor);
1200   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1201     Error(std::move(Err));
1202     return true;
1203   }
1204 
1205   RecordData Record;
1206   StringRef Blob;
1207   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1208   if (!MaybeCode) {
1209     Error(MaybeCode.takeError());
1210     return true;
1211   }
1212   unsigned Code = MaybeCode.get();
1213 
1214   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1215   if (!MaybeRecCode) {
1216     Error(MaybeRecCode.takeError());
1217     return true;
1218   }
1219   unsigned RecCode = MaybeRecCode.get();
1220   if (RecCode != DECL_CONTEXT_VISIBLE) {
1221     Error("Expected visible lookup table block");
1222     return true;
1223   }
1224 
1225   // We can't safely determine the primary context yet, so delay attaching the
1226   // lookup table until we're done with recursive deserialization.
1227   auto *Data = (const unsigned char*)Blob.data();
1228   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1229   return false;
1230 }
1231 
1232 void ASTReader::Error(StringRef Msg) const {
1233   Error(diag::err_fe_pch_malformed, Msg);
1234   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1235       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1236     Diag(diag::note_module_cache_path)
1237       << PP.getHeaderSearchInfo().getModuleCachePath();
1238   }
1239 }
1240 
1241 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1242                       StringRef Arg3) const {
1243   if (Diags.isDiagnosticInFlight())
1244     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1245   else
1246     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1247 }
1248 
1249 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1250                       unsigned Select) const {
1251   if (!Diags.isDiagnosticInFlight())
1252     Diag(DiagID) << Arg1 << Arg2 << Select;
1253 }
1254 
1255 void ASTReader::Error(llvm::Error &&Err) const {
1256   Error(toString(std::move(Err)));
1257 }
1258 
1259 //===----------------------------------------------------------------------===//
1260 // Source Manager Deserialization
1261 //===----------------------------------------------------------------------===//
1262 
1263 /// Read the line table in the source manager block.
1264 /// \returns true if there was an error.
1265 bool ASTReader::ParseLineTable(ModuleFile &F,
1266                                const RecordData &Record) {
1267   unsigned Idx = 0;
1268   LineTableInfo &LineTable = SourceMgr.getLineTable();
1269 
1270   // Parse the file names
1271   std::map<int, int> FileIDs;
1272   FileIDs[-1] = -1; // For unspecified filenames.
1273   for (unsigned I = 0; Record[Idx]; ++I) {
1274     // Extract the file name
1275     auto Filename = ReadPath(F, Record, Idx);
1276     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1277   }
1278   ++Idx;
1279 
1280   // Parse the line entries
1281   std::vector<LineEntry> Entries;
1282   while (Idx < Record.size()) {
1283     int FID = Record[Idx++];
1284     assert(FID >= 0 && "Serialized line entries for non-local file.");
1285     // Remap FileID from 1-based old view.
1286     FID += F.SLocEntryBaseID - 1;
1287 
1288     // Extract the line entries
1289     unsigned NumEntries = Record[Idx++];
1290     assert(NumEntries && "no line entries for file ID");
1291     Entries.clear();
1292     Entries.reserve(NumEntries);
1293     for (unsigned I = 0; I != NumEntries; ++I) {
1294       unsigned FileOffset = Record[Idx++];
1295       unsigned LineNo = Record[Idx++];
1296       int FilenameID = FileIDs[Record[Idx++]];
1297       SrcMgr::CharacteristicKind FileKind
1298         = (SrcMgr::CharacteristicKind)Record[Idx++];
1299       unsigned IncludeOffset = Record[Idx++];
1300       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1301                                        FileKind, IncludeOffset));
1302     }
1303     LineTable.AddEntry(FileID::get(FID), Entries);
1304   }
1305 
1306   return false;
1307 }
1308 
1309 /// Read a source manager block
1310 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1311   using namespace SrcMgr;
1312 
1313   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1314 
1315   // Set the source-location entry cursor to the current position in
1316   // the stream. This cursor will be used to read the contents of the
1317   // source manager block initially, and then lazily read
1318   // source-location entries as needed.
1319   SLocEntryCursor = F.Stream;
1320 
1321   // The stream itself is going to skip over the source manager block.
1322   if (llvm::Error Err = F.Stream.SkipBlock()) {
1323     Error(std::move(Err));
1324     return true;
1325   }
1326 
1327   // Enter the source manager block.
1328   if (llvm::Error Err =
1329           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1330     Error(std::move(Err));
1331     return true;
1332   }
1333   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1334 
1335   RecordData Record;
1336   while (true) {
1337     Expected<llvm::BitstreamEntry> MaybeE =
1338         SLocEntryCursor.advanceSkippingSubblocks();
1339     if (!MaybeE) {
1340       Error(MaybeE.takeError());
1341       return true;
1342     }
1343     llvm::BitstreamEntry E = MaybeE.get();
1344 
1345     switch (E.Kind) {
1346     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1347     case llvm::BitstreamEntry::Error:
1348       Error("malformed block record in AST file");
1349       return true;
1350     case llvm::BitstreamEntry::EndBlock:
1351       return false;
1352     case llvm::BitstreamEntry::Record:
1353       // The interesting case.
1354       break;
1355     }
1356 
1357     // Read a record.
1358     Record.clear();
1359     StringRef Blob;
1360     Expected<unsigned> MaybeRecord =
1361         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1362     if (!MaybeRecord) {
1363       Error(MaybeRecord.takeError());
1364       return true;
1365     }
1366     switch (MaybeRecord.get()) {
1367     default:  // Default behavior: ignore.
1368       break;
1369 
1370     case SM_SLOC_FILE_ENTRY:
1371     case SM_SLOC_BUFFER_ENTRY:
1372     case SM_SLOC_EXPANSION_ENTRY:
1373       // Once we hit one of the source location entries, we're done.
1374       return false;
1375     }
1376   }
1377 }
1378 
1379 /// If a header file is not found at the path that we expect it to be
1380 /// and the PCH file was moved from its original location, try to resolve the
1381 /// file by assuming that header+PCH were moved together and the header is in
1382 /// the same place relative to the PCH.
1383 static std::string
1384 resolveFileRelativeToOriginalDir(const std::string &Filename,
1385                                  const std::string &OriginalDir,
1386                                  const std::string &CurrDir) {
1387   assert(OriginalDir != CurrDir &&
1388          "No point trying to resolve the file if the PCH dir didn't change");
1389 
1390   using namespace llvm::sys;
1391 
1392   SmallString<128> filePath(Filename);
1393   fs::make_absolute(filePath);
1394   assert(path::is_absolute(OriginalDir));
1395   SmallString<128> currPCHPath(CurrDir);
1396 
1397   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1398                        fileDirE = path::end(path::parent_path(filePath));
1399   path::const_iterator origDirI = path::begin(OriginalDir),
1400                        origDirE = path::end(OriginalDir);
1401   // Skip the common path components from filePath and OriginalDir.
1402   while (fileDirI != fileDirE && origDirI != origDirE &&
1403          *fileDirI == *origDirI) {
1404     ++fileDirI;
1405     ++origDirI;
1406   }
1407   for (; origDirI != origDirE; ++origDirI)
1408     path::append(currPCHPath, "..");
1409   path::append(currPCHPath, fileDirI, fileDirE);
1410   path::append(currPCHPath, path::filename(Filename));
1411   return std::string(currPCHPath.str());
1412 }
1413 
1414 bool ASTReader::ReadSLocEntry(int ID) {
1415   if (ID == 0)
1416     return false;
1417 
1418   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1419     Error("source location entry ID out-of-range for AST file");
1420     return true;
1421   }
1422 
1423   // Local helper to read the (possibly-compressed) buffer data following the
1424   // entry record.
1425   auto ReadBuffer = [this](
1426       BitstreamCursor &SLocEntryCursor,
1427       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1428     RecordData Record;
1429     StringRef Blob;
1430     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1431     if (!MaybeCode) {
1432       Error(MaybeCode.takeError());
1433       return nullptr;
1434     }
1435     unsigned Code = MaybeCode.get();
1436 
1437     Expected<unsigned> MaybeRecCode =
1438         SLocEntryCursor.readRecord(Code, Record, &Blob);
1439     if (!MaybeRecCode) {
1440       Error(MaybeRecCode.takeError());
1441       return nullptr;
1442     }
1443     unsigned RecCode = MaybeRecCode.get();
1444 
1445     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1446       if (!llvm::zlib::isAvailable()) {
1447         Error("zlib is not available");
1448         return nullptr;
1449       }
1450       SmallString<0> Uncompressed;
1451       if (llvm::Error E =
1452               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1453         Error("could not decompress embedded file contents: " +
1454               llvm::toString(std::move(E)));
1455         return nullptr;
1456       }
1457       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1458     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1459       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1460     } else {
1461       Error("AST record has invalid code");
1462       return nullptr;
1463     }
1464   };
1465 
1466   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1467   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1468           F->SLocEntryOffsetsBase +
1469           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1470     Error(std::move(Err));
1471     return true;
1472   }
1473 
1474   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1475   unsigned BaseOffset = F->SLocEntryBaseOffset;
1476 
1477   ++NumSLocEntriesRead;
1478   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1479   if (!MaybeEntry) {
1480     Error(MaybeEntry.takeError());
1481     return true;
1482   }
1483   llvm::BitstreamEntry Entry = MaybeEntry.get();
1484 
1485   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1486     Error("incorrectly-formatted source location entry in AST file");
1487     return true;
1488   }
1489 
1490   RecordData Record;
1491   StringRef Blob;
1492   Expected<unsigned> MaybeSLOC =
1493       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1494   if (!MaybeSLOC) {
1495     Error(MaybeSLOC.takeError());
1496     return true;
1497   }
1498   switch (MaybeSLOC.get()) {
1499   default:
1500     Error("incorrectly-formatted source location entry in AST file");
1501     return true;
1502 
1503   case SM_SLOC_FILE_ENTRY: {
1504     // We will detect whether a file changed and return 'Failure' for it, but
1505     // we will also try to fail gracefully by setting up the SLocEntry.
1506     unsigned InputID = Record[4];
1507     InputFile IF = getInputFile(*F, InputID);
1508     const FileEntry *File = IF.getFile();
1509     bool OverriddenBuffer = IF.isOverridden();
1510 
1511     // Note that we only check if a File was returned. If it was out-of-date
1512     // we have complained but we will continue creating a FileID to recover
1513     // gracefully.
1514     if (!File)
1515       return true;
1516 
1517     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1518     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1519       // This is the module's main file.
1520       IncludeLoc = getImportLocation(F);
1521     }
1522     SrcMgr::CharacteristicKind
1523       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1524     // FIXME: The FileID should be created from the FileEntryRef.
1525     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1526                                         ID, BaseOffset + Record[0]);
1527     SrcMgr::FileInfo &FileInfo =
1528           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1529     FileInfo.NumCreatedFIDs = Record[5];
1530     if (Record[3])
1531       FileInfo.setHasLineDirectives();
1532 
1533     unsigned NumFileDecls = Record[7];
1534     if (NumFileDecls && ContextObj) {
1535       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1536       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1537       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1538                                                              NumFileDecls));
1539     }
1540 
1541     const SrcMgr::ContentCache *ContentCache
1542       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1543     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1544         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1545         !ContentCache->getRawBuffer()) {
1546       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1547       if (!Buffer)
1548         return true;
1549       SourceMgr.overrideFileContents(File, std::move(Buffer));
1550     }
1551 
1552     break;
1553   }
1554 
1555   case SM_SLOC_BUFFER_ENTRY: {
1556     const char *Name = Blob.data();
1557     unsigned Offset = Record[0];
1558     SrcMgr::CharacteristicKind
1559       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1560     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1561     if (IncludeLoc.isInvalid() && F->isModule()) {
1562       IncludeLoc = getImportLocation(F);
1563     }
1564 
1565     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1566     if (!Buffer)
1567       return true;
1568     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1569                            BaseOffset + Offset, IncludeLoc);
1570     break;
1571   }
1572 
1573   case SM_SLOC_EXPANSION_ENTRY: {
1574     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1575     SourceMgr.createExpansionLoc(SpellingLoc,
1576                                      ReadSourceLocation(*F, Record[2]),
1577                                      ReadSourceLocation(*F, Record[3]),
1578                                      Record[5],
1579                                      Record[4],
1580                                      ID,
1581                                      BaseOffset + Record[0]);
1582     break;
1583   }
1584   }
1585 
1586   return false;
1587 }
1588 
1589 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1590   if (ID == 0)
1591     return std::make_pair(SourceLocation(), "");
1592 
1593   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1594     Error("source location entry ID out-of-range for AST file");
1595     return std::make_pair(SourceLocation(), "");
1596   }
1597 
1598   // Find which module file this entry lands in.
1599   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1600   if (!M->isModule())
1601     return std::make_pair(SourceLocation(), "");
1602 
1603   // FIXME: Can we map this down to a particular submodule? That would be
1604   // ideal.
1605   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1606 }
1607 
1608 /// Find the location where the module F is imported.
1609 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1610   if (F->ImportLoc.isValid())
1611     return F->ImportLoc;
1612 
1613   // Otherwise we have a PCH. It's considered to be "imported" at the first
1614   // location of its includer.
1615   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1616     // Main file is the importer.
1617     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1618     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1619   }
1620   return F->ImportedBy[0]->FirstLoc;
1621 }
1622 
1623 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1624 /// the abbreviations that are at the top of the block and then leave the cursor
1625 /// pointing into the block.
1626 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1627                                  uint64_t *StartOfBlockOffset) {
1628   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1629     // FIXME this drops errors on the floor.
1630     consumeError(std::move(Err));
1631     return true;
1632   }
1633 
1634   if (StartOfBlockOffset)
1635     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1636 
1637   while (true) {
1638     uint64_t Offset = Cursor.GetCurrentBitNo();
1639     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1640     if (!MaybeCode) {
1641       // FIXME this drops errors on the floor.
1642       consumeError(MaybeCode.takeError());
1643       return true;
1644     }
1645     unsigned Code = MaybeCode.get();
1646 
1647     // We expect all abbrevs to be at the start of the block.
1648     if (Code != llvm::bitc::DEFINE_ABBREV) {
1649       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1650         // FIXME this drops errors on the floor.
1651         consumeError(std::move(Err));
1652         return true;
1653       }
1654       return false;
1655     }
1656     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1657       // FIXME this drops errors on the floor.
1658       consumeError(std::move(Err));
1659       return true;
1660     }
1661   }
1662 }
1663 
1664 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1665                            unsigned &Idx) {
1666   Token Tok;
1667   Tok.startToken();
1668   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1669   Tok.setLength(Record[Idx++]);
1670   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1671     Tok.setIdentifierInfo(II);
1672   Tok.setKind((tok::TokenKind)Record[Idx++]);
1673   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1674   return Tok;
1675 }
1676 
1677 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1678   BitstreamCursor &Stream = F.MacroCursor;
1679 
1680   // Keep track of where we are in the stream, then jump back there
1681   // after reading this macro.
1682   SavedStreamPosition SavedPosition(Stream);
1683 
1684   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1685     // FIXME this drops errors on the floor.
1686     consumeError(std::move(Err));
1687     return nullptr;
1688   }
1689   RecordData Record;
1690   SmallVector<IdentifierInfo*, 16> MacroParams;
1691   MacroInfo *Macro = nullptr;
1692 
1693   while (true) {
1694     // Advance to the next record, but if we get to the end of the block, don't
1695     // pop it (removing all the abbreviations from the cursor) since we want to
1696     // be able to reseek within the block and read entries.
1697     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1698     Expected<llvm::BitstreamEntry> MaybeEntry =
1699         Stream.advanceSkippingSubblocks(Flags);
1700     if (!MaybeEntry) {
1701       Error(MaybeEntry.takeError());
1702       return Macro;
1703     }
1704     llvm::BitstreamEntry Entry = MaybeEntry.get();
1705 
1706     switch (Entry.Kind) {
1707     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1708     case llvm::BitstreamEntry::Error:
1709       Error("malformed block record in AST file");
1710       return Macro;
1711     case llvm::BitstreamEntry::EndBlock:
1712       return Macro;
1713     case llvm::BitstreamEntry::Record:
1714       // The interesting case.
1715       break;
1716     }
1717 
1718     // Read a record.
1719     Record.clear();
1720     PreprocessorRecordTypes RecType;
1721     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1722       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1723     else {
1724       Error(MaybeRecType.takeError());
1725       return Macro;
1726     }
1727     switch (RecType) {
1728     case PP_MODULE_MACRO:
1729     case PP_MACRO_DIRECTIVE_HISTORY:
1730       return Macro;
1731 
1732     case PP_MACRO_OBJECT_LIKE:
1733     case PP_MACRO_FUNCTION_LIKE: {
1734       // If we already have a macro, that means that we've hit the end
1735       // of the definition of the macro we were looking for. We're
1736       // done.
1737       if (Macro)
1738         return Macro;
1739 
1740       unsigned NextIndex = 1; // Skip identifier ID.
1741       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1742       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1743       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1744       MI->setIsUsed(Record[NextIndex++]);
1745       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1746 
1747       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1748         // Decode function-like macro info.
1749         bool isC99VarArgs = Record[NextIndex++];
1750         bool isGNUVarArgs = Record[NextIndex++];
1751         bool hasCommaPasting = Record[NextIndex++];
1752         MacroParams.clear();
1753         unsigned NumArgs = Record[NextIndex++];
1754         for (unsigned i = 0; i != NumArgs; ++i)
1755           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1756 
1757         // Install function-like macro info.
1758         MI->setIsFunctionLike();
1759         if (isC99VarArgs) MI->setIsC99Varargs();
1760         if (isGNUVarArgs) MI->setIsGNUVarargs();
1761         if (hasCommaPasting) MI->setHasCommaPasting();
1762         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1763       }
1764 
1765       // Remember that we saw this macro last so that we add the tokens that
1766       // form its body to it.
1767       Macro = MI;
1768 
1769       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1770           Record[NextIndex]) {
1771         // We have a macro definition. Register the association
1772         PreprocessedEntityID
1773             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1774         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1775         PreprocessingRecord::PPEntityID PPID =
1776             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1777         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1778             PPRec.getPreprocessedEntity(PPID));
1779         if (PPDef)
1780           PPRec.RegisterMacroDefinition(Macro, PPDef);
1781       }
1782 
1783       ++NumMacrosRead;
1784       break;
1785     }
1786 
1787     case PP_TOKEN: {
1788       // If we see a TOKEN before a PP_MACRO_*, then the file is
1789       // erroneous, just pretend we didn't see this.
1790       if (!Macro) break;
1791 
1792       unsigned Idx = 0;
1793       Token Tok = ReadToken(F, Record, Idx);
1794       Macro->AddTokenToBody(Tok);
1795       break;
1796     }
1797     }
1798   }
1799 }
1800 
1801 PreprocessedEntityID
1802 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1803                                          unsigned LocalID) const {
1804   if (!M.ModuleOffsetMap.empty())
1805     ReadModuleOffsetMap(M);
1806 
1807   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1808     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1809   assert(I != M.PreprocessedEntityRemap.end()
1810          && "Invalid index into preprocessed entity index remap");
1811 
1812   return LocalID + I->second;
1813 }
1814 
1815 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1816   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1817 }
1818 
1819 HeaderFileInfoTrait::internal_key_type
1820 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1821   internal_key_type ikey = {FE->getSize(),
1822                             M.HasTimestamps ? FE->getModificationTime() : 0,
1823                             FE->getName(), /*Imported*/ false};
1824   return ikey;
1825 }
1826 
1827 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1828   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1829     return false;
1830 
1831   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1832     return true;
1833 
1834   // Determine whether the actual files are equivalent.
1835   FileManager &FileMgr = Reader.getFileManager();
1836   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1837     if (!Key.Imported) {
1838       if (auto File = FileMgr.getFile(Key.Filename))
1839         return *File;
1840       return nullptr;
1841     }
1842 
1843     std::string Resolved = std::string(Key.Filename);
1844     Reader.ResolveImportedPath(M, Resolved);
1845     if (auto File = FileMgr.getFile(Resolved))
1846       return *File;
1847     return nullptr;
1848   };
1849 
1850   const FileEntry *FEA = GetFile(a);
1851   const FileEntry *FEB = GetFile(b);
1852   return FEA && FEA == FEB;
1853 }
1854 
1855 std::pair<unsigned, unsigned>
1856 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1857   using namespace llvm::support;
1858 
1859   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1860   unsigned DataLen = (unsigned) *d++;
1861   return std::make_pair(KeyLen, DataLen);
1862 }
1863 
1864 HeaderFileInfoTrait::internal_key_type
1865 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1866   using namespace llvm::support;
1867 
1868   internal_key_type ikey;
1869   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1870   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1871   ikey.Filename = (const char *)d;
1872   ikey.Imported = true;
1873   return ikey;
1874 }
1875 
1876 HeaderFileInfoTrait::data_type
1877 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1878                               unsigned DataLen) {
1879   using namespace llvm::support;
1880 
1881   const unsigned char *End = d + DataLen;
1882   HeaderFileInfo HFI;
1883   unsigned Flags = *d++;
1884   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1885   HFI.isImport |= (Flags >> 5) & 0x01;
1886   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1887   HFI.DirInfo = (Flags >> 1) & 0x07;
1888   HFI.IndexHeaderMapHeader = Flags & 0x01;
1889   // FIXME: Find a better way to handle this. Maybe just store a
1890   // "has been included" flag?
1891   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1892                              HFI.NumIncludes);
1893   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1894       M, endian::readNext<uint32_t, little, unaligned>(d));
1895   if (unsigned FrameworkOffset =
1896           endian::readNext<uint32_t, little, unaligned>(d)) {
1897     // The framework offset is 1 greater than the actual offset,
1898     // since 0 is used as an indicator for "no framework name".
1899     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1900     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1901   }
1902 
1903   assert((End - d) % 4 == 0 &&
1904          "Wrong data length in HeaderFileInfo deserialization");
1905   while (d != End) {
1906     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1907     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1908     LocalSMID >>= 2;
1909 
1910     // This header is part of a module. Associate it with the module to enable
1911     // implicit module import.
1912     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1913     Module *Mod = Reader.getSubmodule(GlobalSMID);
1914     FileManager &FileMgr = Reader.getFileManager();
1915     ModuleMap &ModMap =
1916         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1917 
1918     std::string Filename = std::string(key.Filename);
1919     if (key.Imported)
1920       Reader.ResolveImportedPath(M, Filename);
1921     // FIXME: This is not always the right filename-as-written, but we're not
1922     // going to use this information to rebuild the module, so it doesn't make
1923     // a lot of difference.
1924     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1925     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1926     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1927   }
1928 
1929   // This HeaderFileInfo was externally loaded.
1930   HFI.External = true;
1931   HFI.IsValid = true;
1932   return HFI;
1933 }
1934 
1935 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1936                                 uint32_t MacroDirectivesOffset) {
1937   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1938   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1939 }
1940 
1941 void ASTReader::ReadDefinedMacros() {
1942   // Note that we are loading defined macros.
1943   Deserializing Macros(this);
1944 
1945   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1946     BitstreamCursor &MacroCursor = I.MacroCursor;
1947 
1948     // If there was no preprocessor block, skip this file.
1949     if (MacroCursor.getBitcodeBytes().empty())
1950       continue;
1951 
1952     BitstreamCursor Cursor = MacroCursor;
1953     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1954       Error(std::move(Err));
1955       return;
1956     }
1957 
1958     RecordData Record;
1959     while (true) {
1960       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1961       if (!MaybeE) {
1962         Error(MaybeE.takeError());
1963         return;
1964       }
1965       llvm::BitstreamEntry E = MaybeE.get();
1966 
1967       switch (E.Kind) {
1968       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1969       case llvm::BitstreamEntry::Error:
1970         Error("malformed block record in AST file");
1971         return;
1972       case llvm::BitstreamEntry::EndBlock:
1973         goto NextCursor;
1974 
1975       case llvm::BitstreamEntry::Record: {
1976         Record.clear();
1977         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1978         if (!MaybeRecord) {
1979           Error(MaybeRecord.takeError());
1980           return;
1981         }
1982         switch (MaybeRecord.get()) {
1983         default:  // Default behavior: ignore.
1984           break;
1985 
1986         case PP_MACRO_OBJECT_LIKE:
1987         case PP_MACRO_FUNCTION_LIKE: {
1988           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1989           if (II->isOutOfDate())
1990             updateOutOfDateIdentifier(*II);
1991           break;
1992         }
1993 
1994         case PP_TOKEN:
1995           // Ignore tokens.
1996           break;
1997         }
1998         break;
1999       }
2000       }
2001     }
2002     NextCursor:  ;
2003   }
2004 }
2005 
2006 namespace {
2007 
2008   /// Visitor class used to look up identifirs in an AST file.
2009   class IdentifierLookupVisitor {
2010     StringRef Name;
2011     unsigned NameHash;
2012     unsigned PriorGeneration;
2013     unsigned &NumIdentifierLookups;
2014     unsigned &NumIdentifierLookupHits;
2015     IdentifierInfo *Found = nullptr;
2016 
2017   public:
2018     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2019                             unsigned &NumIdentifierLookups,
2020                             unsigned &NumIdentifierLookupHits)
2021       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2022         PriorGeneration(PriorGeneration),
2023         NumIdentifierLookups(NumIdentifierLookups),
2024         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2025 
2026     bool operator()(ModuleFile &M) {
2027       // If we've already searched this module file, skip it now.
2028       if (M.Generation <= PriorGeneration)
2029         return true;
2030 
2031       ASTIdentifierLookupTable *IdTable
2032         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2033       if (!IdTable)
2034         return false;
2035 
2036       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2037                                      Found);
2038       ++NumIdentifierLookups;
2039       ASTIdentifierLookupTable::iterator Pos =
2040           IdTable->find_hashed(Name, NameHash, &Trait);
2041       if (Pos == IdTable->end())
2042         return false;
2043 
2044       // Dereferencing the iterator has the effect of building the
2045       // IdentifierInfo node and populating it with the various
2046       // declarations it needs.
2047       ++NumIdentifierLookupHits;
2048       Found = *Pos;
2049       return true;
2050     }
2051 
2052     // Retrieve the identifier info found within the module
2053     // files.
2054     IdentifierInfo *getIdentifierInfo() const { return Found; }
2055   };
2056 
2057 } // namespace
2058 
2059 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2060   // Note that we are loading an identifier.
2061   Deserializing AnIdentifier(this);
2062 
2063   unsigned PriorGeneration = 0;
2064   if (getContext().getLangOpts().Modules)
2065     PriorGeneration = IdentifierGeneration[&II];
2066 
2067   // If there is a global index, look there first to determine which modules
2068   // provably do not have any results for this identifier.
2069   GlobalModuleIndex::HitSet Hits;
2070   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2071   if (!loadGlobalIndex()) {
2072     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2073       HitsPtr = &Hits;
2074     }
2075   }
2076 
2077   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2078                                   NumIdentifierLookups,
2079                                   NumIdentifierLookupHits);
2080   ModuleMgr.visit(Visitor, HitsPtr);
2081   markIdentifierUpToDate(&II);
2082 }
2083 
2084 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2085   if (!II)
2086     return;
2087 
2088   II->setOutOfDate(false);
2089 
2090   // Update the generation for this identifier.
2091   if (getContext().getLangOpts().Modules)
2092     IdentifierGeneration[II] = getGeneration();
2093 }
2094 
2095 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2096                                     const PendingMacroInfo &PMInfo) {
2097   ModuleFile &M = *PMInfo.M;
2098 
2099   BitstreamCursor &Cursor = M.MacroCursor;
2100   SavedStreamPosition SavedPosition(Cursor);
2101   if (llvm::Error Err =
2102           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2103     Error(std::move(Err));
2104     return;
2105   }
2106 
2107   struct ModuleMacroRecord {
2108     SubmoduleID SubModID;
2109     MacroInfo *MI;
2110     SmallVector<SubmoduleID, 8> Overrides;
2111   };
2112   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2113 
2114   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2115   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2116   // macro histroy.
2117   RecordData Record;
2118   while (true) {
2119     Expected<llvm::BitstreamEntry> MaybeEntry =
2120         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2121     if (!MaybeEntry) {
2122       Error(MaybeEntry.takeError());
2123       return;
2124     }
2125     llvm::BitstreamEntry Entry = MaybeEntry.get();
2126 
2127     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2128       Error("malformed block record in AST file");
2129       return;
2130     }
2131 
2132     Record.clear();
2133     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2134     if (!MaybePP) {
2135       Error(MaybePP.takeError());
2136       return;
2137     }
2138     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2139     case PP_MACRO_DIRECTIVE_HISTORY:
2140       break;
2141 
2142     case PP_MODULE_MACRO: {
2143       ModuleMacros.push_back(ModuleMacroRecord());
2144       auto &Info = ModuleMacros.back();
2145       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2146       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2147       for (int I = 2, N = Record.size(); I != N; ++I)
2148         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2149       continue;
2150     }
2151 
2152     default:
2153       Error("malformed block record in AST file");
2154       return;
2155     }
2156 
2157     // We found the macro directive history; that's the last record
2158     // for this macro.
2159     break;
2160   }
2161 
2162   // Module macros are listed in reverse dependency order.
2163   {
2164     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2165     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2166     for (auto &MMR : ModuleMacros) {
2167       Overrides.clear();
2168       for (unsigned ModID : MMR.Overrides) {
2169         Module *Mod = getSubmodule(ModID);
2170         auto *Macro = PP.getModuleMacro(Mod, II);
2171         assert(Macro && "missing definition for overridden macro");
2172         Overrides.push_back(Macro);
2173       }
2174 
2175       bool Inserted = false;
2176       Module *Owner = getSubmodule(MMR.SubModID);
2177       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2178     }
2179   }
2180 
2181   // Don't read the directive history for a module; we don't have anywhere
2182   // to put it.
2183   if (M.isModule())
2184     return;
2185 
2186   // Deserialize the macro directives history in reverse source-order.
2187   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2188   unsigned Idx = 0, N = Record.size();
2189   while (Idx < N) {
2190     MacroDirective *MD = nullptr;
2191     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2192     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2193     switch (K) {
2194     case MacroDirective::MD_Define: {
2195       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2196       MD = PP.AllocateDefMacroDirective(MI, Loc);
2197       break;
2198     }
2199     case MacroDirective::MD_Undefine:
2200       MD = PP.AllocateUndefMacroDirective(Loc);
2201       break;
2202     case MacroDirective::MD_Visibility:
2203       bool isPublic = Record[Idx++];
2204       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2205       break;
2206     }
2207 
2208     if (!Latest)
2209       Latest = MD;
2210     if (Earliest)
2211       Earliest->setPrevious(MD);
2212     Earliest = MD;
2213   }
2214 
2215   if (Latest)
2216     PP.setLoadedMacroDirective(II, Earliest, Latest);
2217 }
2218 
2219 ASTReader::InputFileInfo
2220 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2221   // Go find this input file.
2222   BitstreamCursor &Cursor = F.InputFilesCursor;
2223   SavedStreamPosition SavedPosition(Cursor);
2224   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2225     // FIXME this drops errors on the floor.
2226     consumeError(std::move(Err));
2227   }
2228 
2229   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2230   if (!MaybeCode) {
2231     // FIXME this drops errors on the floor.
2232     consumeError(MaybeCode.takeError());
2233   }
2234   unsigned Code = MaybeCode.get();
2235   RecordData Record;
2236   StringRef Blob;
2237 
2238   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2239     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2240            "invalid record type for input file");
2241   else {
2242     // FIXME this drops errors on the floor.
2243     consumeError(Maybe.takeError());
2244   }
2245 
2246   assert(Record[0] == ID && "Bogus stored ID or offset");
2247   InputFileInfo R;
2248   R.StoredSize = static_cast<off_t>(Record[1]);
2249   R.StoredTime = static_cast<time_t>(Record[2]);
2250   R.Overridden = static_cast<bool>(Record[3]);
2251   R.Transient = static_cast<bool>(Record[4]);
2252   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2253   R.Filename = std::string(Blob);
2254   ResolveImportedPath(F, R.Filename);
2255 
2256   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2257   if (!MaybeEntry) // FIXME this drops errors on the floor.
2258     consumeError(MaybeEntry.takeError());
2259   llvm::BitstreamEntry Entry = MaybeEntry.get();
2260   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2261          "expected record type for input file hash");
2262 
2263   Record.clear();
2264   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2265     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2266            "invalid record type for input file hash");
2267   else {
2268     // FIXME this drops errors on the floor.
2269     consumeError(Maybe.takeError());
2270   }
2271   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2272                   static_cast<uint64_t>(Record[0]);
2273   return R;
2274 }
2275 
2276 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2277 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2278   // If this ID is bogus, just return an empty input file.
2279   if (ID == 0 || ID > F.InputFilesLoaded.size())
2280     return InputFile();
2281 
2282   // If we've already loaded this input file, return it.
2283   if (F.InputFilesLoaded[ID-1].getFile())
2284     return F.InputFilesLoaded[ID-1];
2285 
2286   if (F.InputFilesLoaded[ID-1].isNotFound())
2287     return InputFile();
2288 
2289   // Go find this input file.
2290   BitstreamCursor &Cursor = F.InputFilesCursor;
2291   SavedStreamPosition SavedPosition(Cursor);
2292   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2293     // FIXME this drops errors on the floor.
2294     consumeError(std::move(Err));
2295   }
2296 
2297   InputFileInfo FI = readInputFileInfo(F, ID);
2298   off_t StoredSize = FI.StoredSize;
2299   time_t StoredTime = FI.StoredTime;
2300   bool Overridden = FI.Overridden;
2301   bool Transient = FI.Transient;
2302   StringRef Filename = FI.Filename;
2303   uint64_t StoredContentHash = FI.ContentHash;
2304 
2305   const FileEntry *File = nullptr;
2306   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2307     File = *FE;
2308 
2309   // If we didn't find the file, resolve it relative to the
2310   // original directory from which this AST file was created.
2311   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2312       F.OriginalDir != F.BaseDirectory) {
2313     std::string Resolved = resolveFileRelativeToOriginalDir(
2314         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2315     if (!Resolved.empty())
2316       if (auto FE = FileMgr.getFile(Resolved))
2317         File = *FE;
2318   }
2319 
2320   // For an overridden file, create a virtual file with the stored
2321   // size/timestamp.
2322   if ((Overridden || Transient) && File == nullptr)
2323     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2324 
2325   if (File == nullptr) {
2326     if (Complain) {
2327       std::string ErrorStr = "could not find file '";
2328       ErrorStr += Filename;
2329       ErrorStr += "' referenced by AST file '";
2330       ErrorStr += F.FileName;
2331       ErrorStr += "'";
2332       Error(ErrorStr);
2333     }
2334     // Record that we didn't find the file.
2335     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2336     return InputFile();
2337   }
2338 
2339   // Check if there was a request to override the contents of the file
2340   // that was part of the precompiled header. Overriding such a file
2341   // can lead to problems when lexing using the source locations from the
2342   // PCH.
2343   SourceManager &SM = getSourceManager();
2344   // FIXME: Reject if the overrides are different.
2345   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2346     if (Complain)
2347       Error(diag::err_fe_pch_file_overridden, Filename);
2348 
2349     // After emitting the diagnostic, bypass the overriding file to recover
2350     // (this creates a separate FileEntry).
2351     File = SM.bypassFileContentsOverride(*File);
2352     if (!File) {
2353       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2354       return InputFile();
2355     }
2356   }
2357 
2358   enum ModificationType {
2359     Size,
2360     ModTime,
2361     Content,
2362     None,
2363   };
2364   auto HasInputFileChanged = [&]() {
2365     if (StoredSize != File->getSize())
2366       return ModificationType::Size;
2367     if (!DisableValidation && StoredTime &&
2368         StoredTime != File->getModificationTime()) {
2369       // In case the modification time changes but not the content,
2370       // accept the cached file as legit.
2371       if (ValidateASTInputFilesContent &&
2372           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2373         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2374         if (!MemBuffOrError) {
2375           if (!Complain)
2376             return ModificationType::ModTime;
2377           std::string ErrorStr = "could not get buffer for file '";
2378           ErrorStr += File->getName();
2379           ErrorStr += "'";
2380           Error(ErrorStr);
2381           return ModificationType::ModTime;
2382         }
2383 
2384         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2385         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2386           return ModificationType::None;
2387         return ModificationType::Content;
2388       }
2389       return ModificationType::ModTime;
2390     }
2391     return ModificationType::None;
2392   };
2393 
2394   bool IsOutOfDate = false;
2395   auto FileChange = HasInputFileChanged();
2396   // For an overridden file, there is nothing to validate.
2397   if (!Overridden && FileChange != ModificationType::None) {
2398     if (Complain) {
2399       // Build a list of the PCH imports that got us here (in reverse).
2400       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2401       while (!ImportStack.back()->ImportedBy.empty())
2402         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2403 
2404       // The top-level PCH is stale.
2405       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2406       unsigned DiagnosticKind =
2407           moduleKindForDiagnostic(ImportStack.back()->Kind);
2408       if (DiagnosticKind == 0)
2409         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2410               (unsigned)FileChange);
2411       else if (DiagnosticKind == 1)
2412         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2413               (unsigned)FileChange);
2414       else
2415         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2416               (unsigned)FileChange);
2417 
2418       // Print the import stack.
2419       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2420         Diag(diag::note_pch_required_by)
2421           << Filename << ImportStack[0]->FileName;
2422         for (unsigned I = 1; I < ImportStack.size(); ++I)
2423           Diag(diag::note_pch_required_by)
2424             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2425       }
2426 
2427       if (!Diags.isDiagnosticInFlight())
2428         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2429     }
2430 
2431     IsOutOfDate = true;
2432   }
2433   // FIXME: If the file is overridden and we've already opened it,
2434   // issue an error (or split it into a separate FileEntry).
2435 
2436   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2437 
2438   // Note that we've loaded this input file.
2439   F.InputFilesLoaded[ID-1] = IF;
2440   return IF;
2441 }
2442 
2443 /// If we are loading a relocatable PCH or module file, and the filename
2444 /// is not an absolute path, add the system or module root to the beginning of
2445 /// the file name.
2446 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2447   // Resolve relative to the base directory, if we have one.
2448   if (!M.BaseDirectory.empty())
2449     return ResolveImportedPath(Filename, M.BaseDirectory);
2450 }
2451 
2452 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2453   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2454     return;
2455 
2456   SmallString<128> Buffer;
2457   llvm::sys::path::append(Buffer, Prefix, Filename);
2458   Filename.assign(Buffer.begin(), Buffer.end());
2459 }
2460 
2461 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2462   switch (ARR) {
2463   case ASTReader::Failure: return true;
2464   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2465   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2466   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2467   case ASTReader::ConfigurationMismatch:
2468     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2469   case ASTReader::HadErrors: return true;
2470   case ASTReader::Success: return false;
2471   }
2472 
2473   llvm_unreachable("unknown ASTReadResult");
2474 }
2475 
2476 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2477     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2478     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2479     std::string &SuggestedPredefines) {
2480   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2481     // FIXME this drops errors on the floor.
2482     consumeError(std::move(Err));
2483     return Failure;
2484   }
2485 
2486   // Read all of the records in the options block.
2487   RecordData Record;
2488   ASTReadResult Result = Success;
2489   while (true) {
2490     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2491     if (!MaybeEntry) {
2492       // FIXME this drops errors on the floor.
2493       consumeError(MaybeEntry.takeError());
2494       return Failure;
2495     }
2496     llvm::BitstreamEntry Entry = MaybeEntry.get();
2497 
2498     switch (Entry.Kind) {
2499     case llvm::BitstreamEntry::Error:
2500     case llvm::BitstreamEntry::SubBlock:
2501       return Failure;
2502 
2503     case llvm::BitstreamEntry::EndBlock:
2504       return Result;
2505 
2506     case llvm::BitstreamEntry::Record:
2507       // The interesting case.
2508       break;
2509     }
2510 
2511     // Read and process a record.
2512     Record.clear();
2513     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2514     if (!MaybeRecordType) {
2515       // FIXME this drops errors on the floor.
2516       consumeError(MaybeRecordType.takeError());
2517       return Failure;
2518     }
2519     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2520     case LANGUAGE_OPTIONS: {
2521       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2522       if (ParseLanguageOptions(Record, Complain, Listener,
2523                                AllowCompatibleConfigurationMismatch))
2524         Result = ConfigurationMismatch;
2525       break;
2526     }
2527 
2528     case TARGET_OPTIONS: {
2529       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2530       if (ParseTargetOptions(Record, Complain, Listener,
2531                              AllowCompatibleConfigurationMismatch))
2532         Result = ConfigurationMismatch;
2533       break;
2534     }
2535 
2536     case FILE_SYSTEM_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (!AllowCompatibleConfigurationMismatch &&
2539           ParseFileSystemOptions(Record, Complain, Listener))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case HEADER_SEARCH_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (!AllowCompatibleConfigurationMismatch &&
2547           ParseHeaderSearchOptions(Record, Complain, Listener))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case PREPROCESSOR_OPTIONS:
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParsePreprocessorOptions(Record, Complain, Listener,
2556                                    SuggestedPredefines))
2557         Result = ConfigurationMismatch;
2558       break;
2559     }
2560   }
2561 }
2562 
2563 ASTReader::ASTReadResult
2564 ASTReader::ReadControlBlock(ModuleFile &F,
2565                             SmallVectorImpl<ImportedModule> &Loaded,
2566                             const ModuleFile *ImportedBy,
2567                             unsigned ClientLoadCapabilities) {
2568   BitstreamCursor &Stream = F.Stream;
2569 
2570   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2571     Error(std::move(Err));
2572     return Failure;
2573   }
2574 
2575   // Lambda to read the unhashed control block the first time it's called.
2576   //
2577   // For PCM files, the unhashed control block cannot be read until after the
2578   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2579   // need to look ahead before reading the IMPORTS record.  For consistency,
2580   // this block is always read somehow (see BitstreamEntry::EndBlock).
2581   bool HasReadUnhashedControlBlock = false;
2582   auto readUnhashedControlBlockOnce = [&]() {
2583     if (!HasReadUnhashedControlBlock) {
2584       HasReadUnhashedControlBlock = true;
2585       if (ASTReadResult Result =
2586               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2587         return Result;
2588     }
2589     return Success;
2590   };
2591 
2592   // Read all of the records and blocks in the control block.
2593   RecordData Record;
2594   unsigned NumInputs = 0;
2595   unsigned NumUserInputs = 0;
2596   StringRef BaseDirectoryAsWritten;
2597   while (true) {
2598     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2599     if (!MaybeEntry) {
2600       Error(MaybeEntry.takeError());
2601       return Failure;
2602     }
2603     llvm::BitstreamEntry Entry = MaybeEntry.get();
2604 
2605     switch (Entry.Kind) {
2606     case llvm::BitstreamEntry::Error:
2607       Error("malformed block record in AST file");
2608       return Failure;
2609     case llvm::BitstreamEntry::EndBlock: {
2610       // Validate the module before returning.  This call catches an AST with
2611       // no module name and no imports.
2612       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2613         return Result;
2614 
2615       // Validate input files.
2616       const HeaderSearchOptions &HSOpts =
2617           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2618 
2619       // All user input files reside at the index range [0, NumUserInputs), and
2620       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2621       // loaded module files, ignore missing inputs.
2622       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2623           F.Kind != MK_PrebuiltModule) {
2624         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2625 
2626         // If we are reading a module, we will create a verification timestamp,
2627         // so we verify all input files.  Otherwise, verify only user input
2628         // files.
2629 
2630         unsigned N = NumUserInputs;
2631         if (ValidateSystemInputs ||
2632             (HSOpts.ModulesValidateOncePerBuildSession &&
2633              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2634              F.Kind == MK_ImplicitModule))
2635           N = NumInputs;
2636 
2637         for (unsigned I = 0; I < N; ++I) {
2638           InputFile IF = getInputFile(F, I+1, Complain);
2639           if (!IF.getFile() || IF.isOutOfDate())
2640             return OutOfDate;
2641         }
2642       }
2643 
2644       if (Listener)
2645         Listener->visitModuleFile(F.FileName, F.Kind);
2646 
2647       if (Listener && Listener->needsInputFileVisitation()) {
2648         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2649                                                                 : NumUserInputs;
2650         for (unsigned I = 0; I < N; ++I) {
2651           bool IsSystem = I >= NumUserInputs;
2652           InputFileInfo FI = readInputFileInfo(F, I+1);
2653           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2654                                    F.Kind == MK_ExplicitModule ||
2655                                    F.Kind == MK_PrebuiltModule);
2656         }
2657       }
2658 
2659       return Success;
2660     }
2661 
2662     case llvm::BitstreamEntry::SubBlock:
2663       switch (Entry.ID) {
2664       case INPUT_FILES_BLOCK_ID:
2665         F.InputFilesCursor = Stream;
2666         if (llvm::Error Err = Stream.SkipBlock()) {
2667           Error(std::move(Err));
2668           return Failure;
2669         }
2670         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2671           Error("malformed block record in AST file");
2672           return Failure;
2673         }
2674         continue;
2675 
2676       case OPTIONS_BLOCK_ID:
2677         // If we're reading the first module for this group, check its options
2678         // are compatible with ours. For modules it imports, no further checking
2679         // is required, because we checked them when we built it.
2680         if (Listener && !ImportedBy) {
2681           // Should we allow the configuration of the module file to differ from
2682           // the configuration of the current translation unit in a compatible
2683           // way?
2684           //
2685           // FIXME: Allow this for files explicitly specified with -include-pch.
2686           bool AllowCompatibleConfigurationMismatch =
2687               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2688 
2689           ASTReadResult Result =
2690               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2691                                AllowCompatibleConfigurationMismatch, *Listener,
2692                                SuggestedPredefines);
2693           if (Result == Failure) {
2694             Error("malformed block record in AST file");
2695             return Result;
2696           }
2697 
2698           if (DisableValidation ||
2699               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2700             Result = Success;
2701 
2702           // If we can't load the module, exit early since we likely
2703           // will rebuild the module anyway. The stream may be in the
2704           // middle of a block.
2705           if (Result != Success)
2706             return Result;
2707         } else if (llvm::Error Err = Stream.SkipBlock()) {
2708           Error(std::move(Err));
2709           return Failure;
2710         }
2711         continue;
2712 
2713       default:
2714         if (llvm::Error Err = Stream.SkipBlock()) {
2715           Error(std::move(Err));
2716           return Failure;
2717         }
2718         continue;
2719       }
2720 
2721     case llvm::BitstreamEntry::Record:
2722       // The interesting case.
2723       break;
2724     }
2725 
2726     // Read and process a record.
2727     Record.clear();
2728     StringRef Blob;
2729     Expected<unsigned> MaybeRecordType =
2730         Stream.readRecord(Entry.ID, Record, &Blob);
2731     if (!MaybeRecordType) {
2732       Error(MaybeRecordType.takeError());
2733       return Failure;
2734     }
2735     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2736     case METADATA: {
2737       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2738         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2739           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2740                                         : diag::err_pch_version_too_new);
2741         return VersionMismatch;
2742       }
2743 
2744       bool hasErrors = Record[6];
2745       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2746         Diag(diag::err_pch_with_compiler_errors);
2747         return HadErrors;
2748       }
2749       if (hasErrors) {
2750         Diags.ErrorOccurred = true;
2751         Diags.UncompilableErrorOccurred = true;
2752         Diags.UnrecoverableErrorOccurred = true;
2753       }
2754 
2755       F.RelocatablePCH = Record[4];
2756       // Relative paths in a relocatable PCH are relative to our sysroot.
2757       if (F.RelocatablePCH)
2758         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2759 
2760       F.HasTimestamps = Record[5];
2761 
2762       const std::string &CurBranch = getClangFullRepositoryVersion();
2763       StringRef ASTBranch = Blob;
2764       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2767         return VersionMismatch;
2768       }
2769       break;
2770     }
2771 
2772     case IMPORTS: {
2773       // Validate the AST before processing any imports (otherwise, untangling
2774       // them can be error-prone and expensive).  A module will have a name and
2775       // will already have been validated, but this catches the PCH case.
2776       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2777         return Result;
2778 
2779       // Load each of the imported PCH files.
2780       unsigned Idx = 0, N = Record.size();
2781       while (Idx < N) {
2782         // Read information about the AST file.
2783         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2784         // The import location will be the local one for now; we will adjust
2785         // all import locations of module imports after the global source
2786         // location info are setup, in ReadAST.
2787         SourceLocation ImportLoc =
2788             ReadUntranslatedSourceLocation(Record[Idx++]);
2789         off_t StoredSize = (off_t)Record[Idx++];
2790         time_t StoredModTime = (time_t)Record[Idx++];
2791         auto FirstSignatureByte = Record.begin() + Idx;
2792         ASTFileSignature StoredSignature = ASTFileSignature::create(
2793             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2794         Idx += ASTFileSignature::size;
2795 
2796         std::string ImportedName = ReadString(Record, Idx);
2797         std::string ImportedFile;
2798 
2799         // For prebuilt and explicit modules first consult the file map for
2800         // an override. Note that here we don't search prebuilt module
2801         // directories, only the explicit name to file mappings. Also, we will
2802         // still verify the size/signature making sure it is essentially the
2803         // same file but perhaps in a different location.
2804         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806             ImportedName, /*FileMapOnly*/ true);
2807 
2808         if (ImportedFile.empty())
2809           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810           // ModuleCache as when writing.
2811           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2812         else
2813           SkipPath(Record, Idx);
2814 
2815         // If our client can't cope with us being out of date, we can't cope with
2816         // our dependency being missing.
2817         unsigned Capabilities = ClientLoadCapabilities;
2818         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819           Capabilities &= ~ARR_Missing;
2820 
2821         // Load the AST file.
2822         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823                                   Loaded, StoredSize, StoredModTime,
2824                                   StoredSignature, Capabilities);
2825 
2826         // If we diagnosed a problem, produce a backtrace.
2827         if (isDiagnosedResult(Result, Capabilities))
2828           Diag(diag::note_module_file_imported_by)
2829               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2830 
2831         switch (Result) {
2832         case Failure: return Failure;
2833           // If we have to ignore the dependency, we'll have to ignore this too.
2834         case Missing:
2835         case OutOfDate: return OutOfDate;
2836         case VersionMismatch: return VersionMismatch;
2837         case ConfigurationMismatch: return ConfigurationMismatch;
2838         case HadErrors: return HadErrors;
2839         case Success: break;
2840         }
2841       }
2842       break;
2843     }
2844 
2845     case ORIGINAL_FILE:
2846       F.OriginalSourceFileID = FileID::get(Record[0]);
2847       F.ActualOriginalSourceFileName = std::string(Blob);
2848       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2849       ResolveImportedPath(F, F.OriginalSourceFileName);
2850       break;
2851 
2852     case ORIGINAL_FILE_ID:
2853       F.OriginalSourceFileID = FileID::get(Record[0]);
2854       break;
2855 
2856     case ORIGINAL_PCH_DIR:
2857       F.OriginalDir = std::string(Blob);
2858       break;
2859 
2860     case MODULE_NAME:
2861       F.ModuleName = std::string(Blob);
2862       Diag(diag::remark_module_import)
2863           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2865       if (Listener)
2866         Listener->ReadModuleName(F.ModuleName);
2867 
2868       // Validate the AST as soon as we have a name so we can exit early on
2869       // failure.
2870       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2871         return Result;
2872 
2873       break;
2874 
2875     case MODULE_DIRECTORY: {
2876       // Save the BaseDirectory as written in the PCM for computing the module
2877       // filename for the ModuleCache.
2878       BaseDirectoryAsWritten = Blob;
2879       assert(!F.ModuleName.empty() &&
2880              "MODULE_DIRECTORY found before MODULE_NAME");
2881       // If we've already loaded a module map file covering this module, we may
2882       // have a better path for it (relative to the current build).
2883       Module *M = PP.getHeaderSearchInfo().lookupModule(
2884           F.ModuleName, /*AllowSearch*/ true,
2885           /*AllowExtraModuleMapSearch*/ true);
2886       if (M && M->Directory) {
2887         // If we're implicitly loading a module, the base directory can't
2888         // change between the build and use.
2889         // Don't emit module relocation error if we have -fno-validate-pch
2890         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2892           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893           if (!BuildDir || *BuildDir != M->Directory) {
2894             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895               Diag(diag::err_imported_module_relocated)
2896                   << F.ModuleName << Blob << M->Directory->getName();
2897             return OutOfDate;
2898           }
2899         }
2900         F.BaseDirectory = std::string(M->Directory->getName());
2901       } else {
2902         F.BaseDirectory = std::string(Blob);
2903       }
2904       break;
2905     }
2906 
2907     case MODULE_MAP_FILE:
2908       if (ASTReadResult Result =
2909               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910         return Result;
2911       break;
2912 
2913     case INPUT_FILE_OFFSETS:
2914       NumInputs = Record[0];
2915       NumUserInputs = Record[1];
2916       F.InputFileOffsets =
2917           (const llvm::support::unaligned_uint64_t *)Blob.data();
2918       F.InputFilesLoaded.resize(NumInputs);
2919       F.NumUserInputFiles = NumUserInputs;
2920       break;
2921     }
2922   }
2923 }
2924 
2925 ASTReader::ASTReadResult
2926 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2927   BitstreamCursor &Stream = F.Stream;
2928 
2929   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930     Error(std::move(Err));
2931     return Failure;
2932   }
2933   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2934 
2935   // Read all of the records and blocks for the AST file.
2936   RecordData Record;
2937   while (true) {
2938     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2939     if (!MaybeEntry) {
2940       Error(MaybeEntry.takeError());
2941       return Failure;
2942     }
2943     llvm::BitstreamEntry Entry = MaybeEntry.get();
2944 
2945     switch (Entry.Kind) {
2946     case llvm::BitstreamEntry::Error:
2947       Error("error at end of module block in AST file");
2948       return Failure;
2949     case llvm::BitstreamEntry::EndBlock:
2950       // Outside of C++, we do not store a lookup map for the translation unit.
2951       // Instead, mark it as needing a lookup map to be built if this module
2952       // contains any declarations lexically within it (which it always does!).
2953       // This usually has no cost, since we very rarely need the lookup map for
2954       // the translation unit outside C++.
2955       if (ASTContext *Ctx = ContextObj) {
2956         DeclContext *DC = Ctx->getTranslationUnitDecl();
2957         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2958           DC->setMustBuildLookupTable();
2959       }
2960 
2961       return Success;
2962     case llvm::BitstreamEntry::SubBlock:
2963       switch (Entry.ID) {
2964       case DECLTYPES_BLOCK_ID:
2965         // We lazily load the decls block, but we want to set up the
2966         // DeclsCursor cursor to point into it.  Clone our current bitcode
2967         // cursor to it, enter the block and read the abbrevs in that block.
2968         // With the main cursor, we just skip over it.
2969         F.DeclsCursor = Stream;
2970         if (llvm::Error Err = Stream.SkipBlock()) {
2971           Error(std::move(Err));
2972           return Failure;
2973         }
2974         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2975                              &F.DeclsBlockStartOffset)) {
2976           Error("malformed block record in AST file");
2977           return Failure;
2978         }
2979         break;
2980 
2981       case PREPROCESSOR_BLOCK_ID:
2982         F.MacroCursor = Stream;
2983         if (!PP.getExternalSource())
2984           PP.setExternalSource(this);
2985 
2986         if (llvm::Error Err = Stream.SkipBlock()) {
2987           Error(std::move(Err));
2988           return Failure;
2989         }
2990         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2991           Error("malformed block record in AST file");
2992           return Failure;
2993         }
2994         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2995         break;
2996 
2997       case PREPROCESSOR_DETAIL_BLOCK_ID:
2998         F.PreprocessorDetailCursor = Stream;
2999 
3000         if (llvm::Error Err = Stream.SkipBlock()) {
3001           Error(std::move(Err));
3002           return Failure;
3003         }
3004         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3005                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3006           Error("malformed preprocessor detail record in AST file");
3007           return Failure;
3008         }
3009         F.PreprocessorDetailStartOffset
3010         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3011 
3012         if (!PP.getPreprocessingRecord())
3013           PP.createPreprocessingRecord();
3014         if (!PP.getPreprocessingRecord()->getExternalSource())
3015           PP.getPreprocessingRecord()->SetExternalSource(*this);
3016         break;
3017 
3018       case SOURCE_MANAGER_BLOCK_ID:
3019         if (ReadSourceManagerBlock(F))
3020           return Failure;
3021         break;
3022 
3023       case SUBMODULE_BLOCK_ID:
3024         if (ASTReadResult Result =
3025                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3026           return Result;
3027         break;
3028 
3029       case COMMENTS_BLOCK_ID: {
3030         BitstreamCursor C = Stream;
3031 
3032         if (llvm::Error Err = Stream.SkipBlock()) {
3033           Error(std::move(Err));
3034           return Failure;
3035         }
3036         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3037           Error("malformed comments block in AST file");
3038           return Failure;
3039         }
3040         CommentsCursors.push_back(std::make_pair(C, &F));
3041         break;
3042       }
3043 
3044       default:
3045         if (llvm::Error Err = Stream.SkipBlock()) {
3046           Error(std::move(Err));
3047           return Failure;
3048         }
3049         break;
3050       }
3051       continue;
3052 
3053     case llvm::BitstreamEntry::Record:
3054       // The interesting case.
3055       break;
3056     }
3057 
3058     // Read and process a record.
3059     Record.clear();
3060     StringRef Blob;
3061     Expected<unsigned> MaybeRecordType =
3062         Stream.readRecord(Entry.ID, Record, &Blob);
3063     if (!MaybeRecordType) {
3064       Error(MaybeRecordType.takeError());
3065       return Failure;
3066     }
3067     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3068 
3069     // If we're not loading an AST context, we don't care about most records.
3070     if (!ContextObj) {
3071       switch (RecordType) {
3072       case IDENTIFIER_TABLE:
3073       case IDENTIFIER_OFFSET:
3074       case INTERESTING_IDENTIFIERS:
3075       case STATISTICS:
3076       case PP_CONDITIONAL_STACK:
3077       case PP_COUNTER_VALUE:
3078       case SOURCE_LOCATION_OFFSETS:
3079       case MODULE_OFFSET_MAP:
3080       case SOURCE_MANAGER_LINE_TABLE:
3081       case SOURCE_LOCATION_PRELOADS:
3082       case PPD_ENTITIES_OFFSETS:
3083       case HEADER_SEARCH_TABLE:
3084       case IMPORTED_MODULES:
3085       case MACRO_OFFSET:
3086         break;
3087       default:
3088         continue;
3089       }
3090     }
3091 
3092     switch (RecordType) {
3093     default:  // Default behavior: ignore.
3094       break;
3095 
3096     case TYPE_OFFSET: {
3097       if (F.LocalNumTypes != 0) {
3098         Error("duplicate TYPE_OFFSET record in AST file");
3099         return Failure;
3100       }
3101       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3102       F.LocalNumTypes = Record[0];
3103       unsigned LocalBaseTypeIndex = Record[1];
3104       F.BaseTypeIndex = getTotalNumTypes();
3105 
3106       if (F.LocalNumTypes > 0) {
3107         // Introduce the global -> local mapping for types within this module.
3108         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3109 
3110         // Introduce the local -> global mapping for types within this module.
3111         F.TypeRemap.insertOrReplace(
3112           std::make_pair(LocalBaseTypeIndex,
3113                          F.BaseTypeIndex - LocalBaseTypeIndex));
3114 
3115         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3116       }
3117       break;
3118     }
3119 
3120     case DECL_OFFSET: {
3121       if (F.LocalNumDecls != 0) {
3122         Error("duplicate DECL_OFFSET record in AST file");
3123         return Failure;
3124       }
3125       F.DeclOffsets = (const DeclOffset *)Blob.data();
3126       F.LocalNumDecls = Record[0];
3127       unsigned LocalBaseDeclID = Record[1];
3128       F.BaseDeclID = getTotalNumDecls();
3129 
3130       if (F.LocalNumDecls > 0) {
3131         // Introduce the global -> local mapping for declarations within this
3132         // module.
3133         GlobalDeclMap.insert(
3134           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3135 
3136         // Introduce the local -> global mapping for declarations within this
3137         // module.
3138         F.DeclRemap.insertOrReplace(
3139           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3140 
3141         // Introduce the global -> local mapping for declarations within this
3142         // module.
3143         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3144 
3145         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3146       }
3147       break;
3148     }
3149 
3150     case TU_UPDATE_LEXICAL: {
3151       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3152       LexicalContents Contents(
3153           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3154               Blob.data()),
3155           static_cast<unsigned int>(Blob.size() / 4));
3156       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3157       TU->setHasExternalLexicalStorage(true);
3158       break;
3159     }
3160 
3161     case UPDATE_VISIBLE: {
3162       unsigned Idx = 0;
3163       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3164       auto *Data = (const unsigned char*)Blob.data();
3165       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3166       // If we've already loaded the decl, perform the updates when we finish
3167       // loading this block.
3168       if (Decl *D = GetExistingDecl(ID))
3169         PendingUpdateRecords.push_back(
3170             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3171       break;
3172     }
3173 
3174     case IDENTIFIER_TABLE:
3175       F.IdentifierTableData = Blob.data();
3176       if (Record[0]) {
3177         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3178             (const unsigned char *)F.IdentifierTableData + Record[0],
3179             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3180             (const unsigned char *)F.IdentifierTableData,
3181             ASTIdentifierLookupTrait(*this, F));
3182 
3183         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3184       }
3185       break;
3186 
3187     case IDENTIFIER_OFFSET: {
3188       if (F.LocalNumIdentifiers != 0) {
3189         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3190         return Failure;
3191       }
3192       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3193       F.LocalNumIdentifiers = Record[0];
3194       unsigned LocalBaseIdentifierID = Record[1];
3195       F.BaseIdentifierID = getTotalNumIdentifiers();
3196 
3197       if (F.LocalNumIdentifiers > 0) {
3198         // Introduce the global -> local mapping for identifiers within this
3199         // module.
3200         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3201                                                   &F));
3202 
3203         // Introduce the local -> global mapping for identifiers within this
3204         // module.
3205         F.IdentifierRemap.insertOrReplace(
3206           std::make_pair(LocalBaseIdentifierID,
3207                          F.BaseIdentifierID - LocalBaseIdentifierID));
3208 
3209         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3210                                  + F.LocalNumIdentifiers);
3211       }
3212       break;
3213     }
3214 
3215     case INTERESTING_IDENTIFIERS:
3216       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3217       break;
3218 
3219     case EAGERLY_DESERIALIZED_DECLS:
3220       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3221       // about "interesting" decls (for instance, if we're building a module).
3222       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3223         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3224       break;
3225 
3226     case MODULAR_CODEGEN_DECLS:
3227       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3228       // them (ie: if we're not codegenerating this module).
3229       if (F.Kind == MK_MainFile ||
3230           getContext().getLangOpts().BuildingPCHWithObjectFile)
3231         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3233       break;
3234 
3235     case SPECIAL_TYPES:
3236       if (SpecialTypes.empty()) {
3237         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3239         break;
3240       }
3241 
3242       if (SpecialTypes.size() != Record.size()) {
3243         Error("invalid special-types record");
3244         return Failure;
3245       }
3246 
3247       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3248         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3249         if (!SpecialTypes[I])
3250           SpecialTypes[I] = ID;
3251         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3252         // merge step?
3253       }
3254       break;
3255 
3256     case STATISTICS:
3257       TotalNumStatements += Record[0];
3258       TotalNumMacros += Record[1];
3259       TotalLexicalDeclContexts += Record[2];
3260       TotalVisibleDeclContexts += Record[3];
3261       break;
3262 
3263     case UNUSED_FILESCOPED_DECLS:
3264       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3266       break;
3267 
3268     case DELEGATING_CTORS:
3269       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3270         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3271       break;
3272 
3273     case WEAK_UNDECLARED_IDENTIFIERS:
3274       if (Record.size() % 4 != 0) {
3275         Error("invalid weak identifiers record");
3276         return Failure;
3277       }
3278 
3279       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3280       // files. This isn't the way to do it :)
3281       WeakUndeclaredIdentifiers.clear();
3282 
3283       // Translate the weak, undeclared identifiers into global IDs.
3284       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3285         WeakUndeclaredIdentifiers.push_back(
3286           getGlobalIdentifierID(F, Record[I++]));
3287         WeakUndeclaredIdentifiers.push_back(
3288           getGlobalIdentifierID(F, Record[I++]));
3289         WeakUndeclaredIdentifiers.push_back(
3290           ReadSourceLocation(F, Record, I).getRawEncoding());
3291         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3292       }
3293       break;
3294 
3295     case SELECTOR_OFFSETS: {
3296       F.SelectorOffsets = (const uint32_t *)Blob.data();
3297       F.LocalNumSelectors = Record[0];
3298       unsigned LocalBaseSelectorID = Record[1];
3299       F.BaseSelectorID = getTotalNumSelectors();
3300 
3301       if (F.LocalNumSelectors > 0) {
3302         // Introduce the global -> local mapping for selectors within this
3303         // module.
3304         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3305 
3306         // Introduce the local -> global mapping for selectors within this
3307         // module.
3308         F.SelectorRemap.insertOrReplace(
3309           std::make_pair(LocalBaseSelectorID,
3310                          F.BaseSelectorID - LocalBaseSelectorID));
3311 
3312         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3313       }
3314       break;
3315     }
3316 
3317     case METHOD_POOL:
3318       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3319       if (Record[0])
3320         F.SelectorLookupTable
3321           = ASTSelectorLookupTable::Create(
3322                         F.SelectorLookupTableData + Record[0],
3323                         F.SelectorLookupTableData,
3324                         ASTSelectorLookupTrait(*this, F));
3325       TotalNumMethodPoolEntries += Record[1];
3326       break;
3327 
3328     case REFERENCED_SELECTOR_POOL:
3329       if (!Record.empty()) {
3330         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3331           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3332                                                                 Record[Idx++]));
3333           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3334                                               getRawEncoding());
3335         }
3336       }
3337       break;
3338 
3339     case PP_CONDITIONAL_STACK:
3340       if (!Record.empty()) {
3341         unsigned Idx = 0, End = Record.size() - 1;
3342         bool ReachedEOFWhileSkipping = Record[Idx++];
3343         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3344         if (ReachedEOFWhileSkipping) {
3345           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3346           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3347           bool FoundNonSkipPortion = Record[Idx++];
3348           bool FoundElse = Record[Idx++];
3349           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3350           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3351                            FoundElse, ElseLoc);
3352         }
3353         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3354         while (Idx < End) {
3355           auto Loc = ReadSourceLocation(F, Record, Idx);
3356           bool WasSkipping = Record[Idx++];
3357           bool FoundNonSkip = Record[Idx++];
3358           bool FoundElse = Record[Idx++];
3359           ConditionalStack.push_back(
3360               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3361         }
3362         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3363       }
3364       break;
3365 
3366     case PP_COUNTER_VALUE:
3367       if (!Record.empty() && Listener)
3368         Listener->ReadCounter(F, Record[0]);
3369       break;
3370 
3371     case FILE_SORTED_DECLS:
3372       F.FileSortedDecls = (const DeclID *)Blob.data();
3373       F.NumFileSortedDecls = Record[0];
3374       break;
3375 
3376     case SOURCE_LOCATION_OFFSETS: {
3377       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3378       F.LocalNumSLocEntries = Record[0];
3379       unsigned SLocSpaceSize = Record[1];
3380       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3381       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3382           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3383                                               SLocSpaceSize);
3384       if (!F.SLocEntryBaseID) {
3385         Error("ran out of source locations");
3386         break;
3387       }
3388       // Make our entry in the range map. BaseID is negative and growing, so
3389       // we invert it. Because we invert it, though, we need the other end of
3390       // the range.
3391       unsigned RangeStart =
3392           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3393       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3394       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3395 
3396       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3397       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3398       GlobalSLocOffsetMap.insert(
3399           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3400                            - SLocSpaceSize,&F));
3401 
3402       // Initialize the remapping table.
3403       // Invalid stays invalid.
3404       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3405       // This module. Base was 2 when being compiled.
3406       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3407                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3408 
3409       TotalNumSLocEntries += F.LocalNumSLocEntries;
3410       break;
3411     }
3412 
3413     case MODULE_OFFSET_MAP:
3414       F.ModuleOffsetMap = Blob;
3415       break;
3416 
3417     case SOURCE_MANAGER_LINE_TABLE:
3418       if (ParseLineTable(F, Record)) {
3419         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3420         return Failure;
3421       }
3422       break;
3423 
3424     case SOURCE_LOCATION_PRELOADS: {
3425       // Need to transform from the local view (1-based IDs) to the global view,
3426       // which is based off F.SLocEntryBaseID.
3427       if (!F.PreloadSLocEntries.empty()) {
3428         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3429         return Failure;
3430       }
3431 
3432       F.PreloadSLocEntries.swap(Record);
3433       break;
3434     }
3435 
3436     case EXT_VECTOR_DECLS:
3437       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3438         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3439       break;
3440 
3441     case VTABLE_USES:
3442       if (Record.size() % 3 != 0) {
3443         Error("Invalid VTABLE_USES record");
3444         return Failure;
3445       }
3446 
3447       // Later tables overwrite earlier ones.
3448       // FIXME: Modules will have some trouble with this. This is clearly not
3449       // the right way to do this.
3450       VTableUses.clear();
3451 
3452       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3453         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3454         VTableUses.push_back(
3455           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3456         VTableUses.push_back(Record[Idx++]);
3457       }
3458       break;
3459 
3460     case PENDING_IMPLICIT_INSTANTIATIONS:
3461       if (PendingInstantiations.size() % 2 != 0) {
3462         Error("Invalid existing PendingInstantiations");
3463         return Failure;
3464       }
3465 
3466       if (Record.size() % 2 != 0) {
3467         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3468         return Failure;
3469       }
3470 
3471       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3472         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3473         PendingInstantiations.push_back(
3474           ReadSourceLocation(F, Record, I).getRawEncoding());
3475       }
3476       break;
3477 
3478     case SEMA_DECL_REFS:
3479       if (Record.size() != 3) {
3480         Error("Invalid SEMA_DECL_REFS block");
3481         return Failure;
3482       }
3483       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3484         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3485       break;
3486 
3487     case PPD_ENTITIES_OFFSETS: {
3488       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3489       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3490       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3491 
3492       unsigned LocalBasePreprocessedEntityID = Record[0];
3493 
3494       unsigned StartingID;
3495       if (!PP.getPreprocessingRecord())
3496         PP.createPreprocessingRecord();
3497       if (!PP.getPreprocessingRecord()->getExternalSource())
3498         PP.getPreprocessingRecord()->SetExternalSource(*this);
3499       StartingID
3500         = PP.getPreprocessingRecord()
3501             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3502       F.BasePreprocessedEntityID = StartingID;
3503 
3504       if (F.NumPreprocessedEntities > 0) {
3505         // Introduce the global -> local mapping for preprocessed entities in
3506         // this module.
3507         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3508 
3509         // Introduce the local -> global mapping for preprocessed entities in
3510         // this module.
3511         F.PreprocessedEntityRemap.insertOrReplace(
3512           std::make_pair(LocalBasePreprocessedEntityID,
3513             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3514       }
3515 
3516       break;
3517     }
3518 
3519     case PPD_SKIPPED_RANGES: {
3520       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3521       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3522       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3523 
3524       if (!PP.getPreprocessingRecord())
3525         PP.createPreprocessingRecord();
3526       if (!PP.getPreprocessingRecord()->getExternalSource())
3527         PP.getPreprocessingRecord()->SetExternalSource(*this);
3528       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3529           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3530 
3531       if (F.NumPreprocessedSkippedRanges > 0)
3532         GlobalSkippedRangeMap.insert(
3533             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3534       break;
3535     }
3536 
3537     case DECL_UPDATE_OFFSETS:
3538       if (Record.size() % 2 != 0) {
3539         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3540         return Failure;
3541       }
3542       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3543         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3544         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3545 
3546         // If we've already loaded the decl, perform the updates when we finish
3547         // loading this block.
3548         if (Decl *D = GetExistingDecl(ID))
3549           PendingUpdateRecords.push_back(
3550               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3551       }
3552       break;
3553 
3554     case OBJC_CATEGORIES_MAP:
3555       if (F.LocalNumObjCCategoriesInMap != 0) {
3556         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3557         return Failure;
3558       }
3559 
3560       F.LocalNumObjCCategoriesInMap = Record[0];
3561       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3562       break;
3563 
3564     case OBJC_CATEGORIES:
3565       F.ObjCCategories.swap(Record);
3566       break;
3567 
3568     case CUDA_SPECIAL_DECL_REFS:
3569       // Later tables overwrite earlier ones.
3570       // FIXME: Modules will have trouble with this.
3571       CUDASpecialDeclRefs.clear();
3572       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3573         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3574       break;
3575 
3576     case HEADER_SEARCH_TABLE:
3577       F.HeaderFileInfoTableData = Blob.data();
3578       F.LocalNumHeaderFileInfos = Record[1];
3579       if (Record[0]) {
3580         F.HeaderFileInfoTable
3581           = HeaderFileInfoLookupTable::Create(
3582                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3583                    (const unsigned char *)F.HeaderFileInfoTableData,
3584                    HeaderFileInfoTrait(*this, F,
3585                                        &PP.getHeaderSearchInfo(),
3586                                        Blob.data() + Record[2]));
3587 
3588         PP.getHeaderSearchInfo().SetExternalSource(this);
3589         if (!PP.getHeaderSearchInfo().getExternalLookup())
3590           PP.getHeaderSearchInfo().SetExternalLookup(this);
3591       }
3592       break;
3593 
3594     case FP_PRAGMA_OPTIONS:
3595       // Later tables overwrite earlier ones.
3596       FPPragmaOptions.swap(Record);
3597       break;
3598 
3599     case OPENCL_EXTENSIONS:
3600       for (unsigned I = 0, E = Record.size(); I != E; ) {
3601         auto Name = ReadString(Record, I);
3602         auto &Opt = OpenCLExtensions.OptMap[Name];
3603         Opt.Supported = Record[I++] != 0;
3604         Opt.Enabled = Record[I++] != 0;
3605         Opt.Avail = Record[I++];
3606         Opt.Core = Record[I++];
3607       }
3608       break;
3609 
3610     case OPENCL_EXTENSION_TYPES:
3611       for (unsigned I = 0, E = Record.size(); I != E;) {
3612         auto TypeID = static_cast<::TypeID>(Record[I++]);
3613         auto *Type = GetType(TypeID).getTypePtr();
3614         auto NumExt = static_cast<unsigned>(Record[I++]);
3615         for (unsigned II = 0; II != NumExt; ++II) {
3616           auto Ext = ReadString(Record, I);
3617           OpenCLTypeExtMap[Type].insert(Ext);
3618         }
3619       }
3620       break;
3621 
3622     case OPENCL_EXTENSION_DECLS:
3623       for (unsigned I = 0, E = Record.size(); I != E;) {
3624         auto DeclID = static_cast<::DeclID>(Record[I++]);
3625         auto *Decl = GetDecl(DeclID);
3626         auto NumExt = static_cast<unsigned>(Record[I++]);
3627         for (unsigned II = 0; II != NumExt; ++II) {
3628           auto Ext = ReadString(Record, I);
3629           OpenCLDeclExtMap[Decl].insert(Ext);
3630         }
3631       }
3632       break;
3633 
3634     case TENTATIVE_DEFINITIONS:
3635       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3637       break;
3638 
3639     case KNOWN_NAMESPACES:
3640       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3641         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3642       break;
3643 
3644     case UNDEFINED_BUT_USED:
3645       if (UndefinedButUsed.size() % 2 != 0) {
3646         Error("Invalid existing UndefinedButUsed");
3647         return Failure;
3648       }
3649 
3650       if (Record.size() % 2 != 0) {
3651         Error("invalid undefined-but-used record");
3652         return Failure;
3653       }
3654       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3655         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3656         UndefinedButUsed.push_back(
3657             ReadSourceLocation(F, Record, I).getRawEncoding());
3658       }
3659       break;
3660 
3661     case DELETE_EXPRS_TO_ANALYZE:
3662       for (unsigned I = 0, N = Record.size(); I != N;) {
3663         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3664         const uint64_t Count = Record[I++];
3665         DelayedDeleteExprs.push_back(Count);
3666         for (uint64_t C = 0; C < Count; ++C) {
3667           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3668           bool IsArrayForm = Record[I++] == 1;
3669           DelayedDeleteExprs.push_back(IsArrayForm);
3670         }
3671       }
3672       break;
3673 
3674     case IMPORTED_MODULES:
3675       if (!F.isModule()) {
3676         // If we aren't loading a module (which has its own exports), make
3677         // all of the imported modules visible.
3678         // FIXME: Deal with macros-only imports.
3679         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3680           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3681           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3682           if (GlobalID) {
3683             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3684             if (DeserializationListener)
3685               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3686           }
3687         }
3688       }
3689       break;
3690 
3691     case MACRO_OFFSET: {
3692       if (F.LocalNumMacros != 0) {
3693         Error("duplicate MACRO_OFFSET record in AST file");
3694         return Failure;
3695       }
3696       F.MacroOffsets = (const uint32_t *)Blob.data();
3697       F.LocalNumMacros = Record[0];
3698       unsigned LocalBaseMacroID = Record[1];
3699       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3700       F.BaseMacroID = getTotalNumMacros();
3701 
3702       if (F.LocalNumMacros > 0) {
3703         // Introduce the global -> local mapping for macros within this module.
3704         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3705 
3706         // Introduce the local -> global mapping for macros within this module.
3707         F.MacroRemap.insertOrReplace(
3708           std::make_pair(LocalBaseMacroID,
3709                          F.BaseMacroID - LocalBaseMacroID));
3710 
3711         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3712       }
3713       break;
3714     }
3715 
3716     case LATE_PARSED_TEMPLATE:
3717       LateParsedTemplates.emplace_back(
3718           std::piecewise_construct, std::forward_as_tuple(&F),
3719           std::forward_as_tuple(Record.begin(), Record.end()));
3720       break;
3721 
3722     case OPTIMIZE_PRAGMA_OPTIONS:
3723       if (Record.size() != 1) {
3724         Error("invalid pragma optimize record");
3725         return Failure;
3726       }
3727       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3728       break;
3729 
3730     case MSSTRUCT_PRAGMA_OPTIONS:
3731       if (Record.size() != 1) {
3732         Error("invalid pragma ms_struct record");
3733         return Failure;
3734       }
3735       PragmaMSStructState = Record[0];
3736       break;
3737 
3738     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3739       if (Record.size() != 2) {
3740         Error("invalid pragma ms_struct record");
3741         return Failure;
3742       }
3743       PragmaMSPointersToMembersState = Record[0];
3744       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3745       break;
3746 
3747     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3748       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3749         UnusedLocalTypedefNameCandidates.push_back(
3750             getGlobalDeclID(F, Record[I]));
3751       break;
3752 
3753     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3754       if (Record.size() != 1) {
3755         Error("invalid cuda pragma options record");
3756         return Failure;
3757       }
3758       ForceCUDAHostDeviceDepth = Record[0];
3759       break;
3760 
3761     case PACK_PRAGMA_OPTIONS: {
3762       if (Record.size() < 3) {
3763         Error("invalid pragma pack record");
3764         return Failure;
3765       }
3766       PragmaPackCurrentValue = Record[0];
3767       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3768       unsigned NumStackEntries = Record[2];
3769       unsigned Idx = 3;
3770       // Reset the stack when importing a new module.
3771       PragmaPackStack.clear();
3772       for (unsigned I = 0; I < NumStackEntries; ++I) {
3773         PragmaPackStackEntry Entry;
3774         Entry.Value = Record[Idx++];
3775         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3776         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3777         PragmaPackStrings.push_back(ReadString(Record, Idx));
3778         Entry.SlotLabel = PragmaPackStrings.back();
3779         PragmaPackStack.push_back(Entry);
3780       }
3781       break;
3782     }
3783 
3784     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3785       if (Record.size() < 3) {
3786         Error("invalid pragma pack record");
3787         return Failure;
3788       }
3789       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3790       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3791       unsigned NumStackEntries = Record[2];
3792       unsigned Idx = 3;
3793       // Reset the stack when importing a new module.
3794       FpPragmaStack.clear();
3795       for (unsigned I = 0; I < NumStackEntries; ++I) {
3796         FpPragmaStackEntry Entry;
3797         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3798         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3799         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3800         FpPragmaStrings.push_back(ReadString(Record, Idx));
3801         Entry.SlotLabel = FpPragmaStrings.back();
3802         FpPragmaStack.push_back(Entry);
3803       }
3804       break;
3805     }
3806 
3807     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3808       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3809         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3810       break;
3811     }
3812   }
3813 }
3814 
3815 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3816   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3817 
3818   // Additional remapping information.
3819   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3820   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3821   F.ModuleOffsetMap = StringRef();
3822 
3823   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3824   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3825     F.SLocRemap.insert(std::make_pair(0U, 0));
3826     F.SLocRemap.insert(std::make_pair(2U, 1));
3827   }
3828 
3829   // Continuous range maps we may be updating in our module.
3830   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3831   RemapBuilder SLocRemap(F.SLocRemap);
3832   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3833   RemapBuilder MacroRemap(F.MacroRemap);
3834   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3835   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3836   RemapBuilder SelectorRemap(F.SelectorRemap);
3837   RemapBuilder DeclRemap(F.DeclRemap);
3838   RemapBuilder TypeRemap(F.TypeRemap);
3839 
3840   while (Data < DataEnd) {
3841     // FIXME: Looking up dependency modules by filename is horrible. Let's
3842     // start fixing this with prebuilt, explicit and implicit modules and see
3843     // how it goes...
3844     using namespace llvm::support;
3845     ModuleKind Kind = static_cast<ModuleKind>(
3846       endian::readNext<uint8_t, little, unaligned>(Data));
3847     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3848     StringRef Name = StringRef((const char*)Data, Len);
3849     Data += Len;
3850     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3851                               Kind == MK_ImplicitModule
3852                           ? ModuleMgr.lookupByModuleName(Name)
3853                           : ModuleMgr.lookupByFileName(Name));
3854     if (!OM) {
3855       std::string Msg =
3856           "SourceLocation remap refers to unknown module, cannot find ";
3857       Msg.append(std::string(Name));
3858       Error(Msg);
3859       return;
3860     }
3861 
3862     uint32_t SLocOffset =
3863         endian::readNext<uint32_t, little, unaligned>(Data);
3864     uint32_t IdentifierIDOffset =
3865         endian::readNext<uint32_t, little, unaligned>(Data);
3866     uint32_t MacroIDOffset =
3867         endian::readNext<uint32_t, little, unaligned>(Data);
3868     uint32_t PreprocessedEntityIDOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t SubmoduleIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t SelectorIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t DeclIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t TypeIndexOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878 
3879     uint32_t None = std::numeric_limits<uint32_t>::max();
3880 
3881     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3882                          RemapBuilder &Remap) {
3883       if (Offset != None)
3884         Remap.insert(std::make_pair(Offset,
3885                                     static_cast<int>(BaseOffset - Offset)));
3886     };
3887     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3888     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3889     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3890     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3891               PreprocessedEntityRemap);
3892     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3893     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3894     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3895     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3896 
3897     // Global -> local mappings.
3898     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3899   }
3900 }
3901 
3902 ASTReader::ASTReadResult
3903 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3904                                   const ModuleFile *ImportedBy,
3905                                   unsigned ClientLoadCapabilities) {
3906   unsigned Idx = 0;
3907   F.ModuleMapPath = ReadPath(F, Record, Idx);
3908 
3909   // Try to resolve ModuleName in the current header search context and
3910   // verify that it is found in the same module map file as we saved. If the
3911   // top-level AST file is a main file, skip this check because there is no
3912   // usable header search context.
3913   assert(!F.ModuleName.empty() &&
3914          "MODULE_NAME should come before MODULE_MAP_FILE");
3915   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3916     // An implicitly-loaded module file should have its module listed in some
3917     // module map file that we've already loaded.
3918     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3919     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3920     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3921     // Don't emit module relocation error if we have -fno-validate-pch
3922     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3923       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3924         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3925           // This module was defined by an imported (explicit) module.
3926           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3927                                                << ASTFE->getName();
3928         } else {
3929           // This module was built with a different module map.
3930           Diag(diag::err_imported_module_not_found)
3931               << F.ModuleName << F.FileName
3932               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3933               << !ImportedBy;
3934           // In case it was imported by a PCH, there's a chance the user is
3935           // just missing to include the search path to the directory containing
3936           // the modulemap.
3937           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3938             Diag(diag::note_imported_by_pch_module_not_found)
3939                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3940         }
3941       }
3942       return OutOfDate;
3943     }
3944 
3945     assert(M && M->Name == F.ModuleName && "found module with different name");
3946 
3947     // Check the primary module map file.
3948     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3949     if (!StoredModMap || *StoredModMap != ModMap) {
3950       assert(ModMap && "found module is missing module map file");
3951       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3952              "top-level import should be verified");
3953       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3954       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3955         Diag(diag::err_imported_module_modmap_changed)
3956             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3957             << ModMap->getName() << F.ModuleMapPath << NotImported;
3958       return OutOfDate;
3959     }
3960 
3961     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3962     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3963       // FIXME: we should use input files rather than storing names.
3964       std::string Filename = ReadPath(F, Record, Idx);
3965       auto F = FileMgr.getFile(Filename, false, false);
3966       if (!F) {
3967         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3968           Error("could not find file '" + Filename +"' referenced by AST file");
3969         return OutOfDate;
3970       }
3971       AdditionalStoredMaps.insert(*F);
3972     }
3973 
3974     // Check any additional module map files (e.g. module.private.modulemap)
3975     // that are not in the pcm.
3976     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3977       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3978         // Remove files that match
3979         // Note: SmallPtrSet::erase is really remove
3980         if (!AdditionalStoredMaps.erase(ModMap)) {
3981           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3982             Diag(diag::err_module_different_modmap)
3983               << F.ModuleName << /*new*/0 << ModMap->getName();
3984           return OutOfDate;
3985         }
3986       }
3987     }
3988 
3989     // Check any additional module map files that are in the pcm, but not
3990     // found in header search. Cases that match are already removed.
3991     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3992       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3993         Diag(diag::err_module_different_modmap)
3994           << F.ModuleName << /*not new*/1 << ModMap->getName();
3995       return OutOfDate;
3996     }
3997   }
3998 
3999   if (Listener)
4000     Listener->ReadModuleMapFile(F.ModuleMapPath);
4001   return Success;
4002 }
4003 
4004 /// Move the given method to the back of the global list of methods.
4005 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4006   // Find the entry for this selector in the method pool.
4007   Sema::GlobalMethodPool::iterator Known
4008     = S.MethodPool.find(Method->getSelector());
4009   if (Known == S.MethodPool.end())
4010     return;
4011 
4012   // Retrieve the appropriate method list.
4013   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4014                                                     : Known->second.second;
4015   bool Found = false;
4016   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4017     if (!Found) {
4018       if (List->getMethod() == Method) {
4019         Found = true;
4020       } else {
4021         // Keep searching.
4022         continue;
4023       }
4024     }
4025 
4026     if (List->getNext())
4027       List->setMethod(List->getNext()->getMethod());
4028     else
4029       List->setMethod(Method);
4030   }
4031 }
4032 
4033 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4034   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4035   for (Decl *D : Names) {
4036     bool wasHidden = !D->isUnconditionallyVisible();
4037     D->setVisibleDespiteOwningModule();
4038 
4039     if (wasHidden && SemaObj) {
4040       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4041         moveMethodToBackOfGlobalList(*SemaObj, Method);
4042       }
4043     }
4044   }
4045 }
4046 
4047 void ASTReader::makeModuleVisible(Module *Mod,
4048                                   Module::NameVisibilityKind NameVisibility,
4049                                   SourceLocation ImportLoc) {
4050   llvm::SmallPtrSet<Module *, 4> Visited;
4051   SmallVector<Module *, 4> Stack;
4052   Stack.push_back(Mod);
4053   while (!Stack.empty()) {
4054     Mod = Stack.pop_back_val();
4055 
4056     if (NameVisibility <= Mod->NameVisibility) {
4057       // This module already has this level of visibility (or greater), so
4058       // there is nothing more to do.
4059       continue;
4060     }
4061 
4062     if (Mod->isUnimportable()) {
4063       // Modules that aren't importable cannot be made visible.
4064       continue;
4065     }
4066 
4067     // Update the module's name visibility.
4068     Mod->NameVisibility = NameVisibility;
4069 
4070     // If we've already deserialized any names from this module,
4071     // mark them as visible.
4072     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4073     if (Hidden != HiddenNamesMap.end()) {
4074       auto HiddenNames = std::move(*Hidden);
4075       HiddenNamesMap.erase(Hidden);
4076       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4077       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4078              "making names visible added hidden names");
4079     }
4080 
4081     // Push any exported modules onto the stack to be marked as visible.
4082     SmallVector<Module *, 16> Exports;
4083     Mod->getExportedModules(Exports);
4084     for (SmallVectorImpl<Module *>::iterator
4085            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4086       Module *Exported = *I;
4087       if (Visited.insert(Exported).second)
4088         Stack.push_back(Exported);
4089     }
4090   }
4091 }
4092 
4093 /// We've merged the definition \p MergedDef into the existing definition
4094 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4095 /// visible.
4096 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4097                                           NamedDecl *MergedDef) {
4098   if (!Def->isUnconditionallyVisible()) {
4099     // If MergedDef is visible or becomes visible, make the definition visible.
4100     if (MergedDef->isUnconditionallyVisible())
4101       Def->setVisibleDespiteOwningModule();
4102     else {
4103       getContext().mergeDefinitionIntoModule(
4104           Def, MergedDef->getImportedOwningModule(),
4105           /*NotifyListeners*/ false);
4106       PendingMergedDefinitionsToDeduplicate.insert(Def);
4107     }
4108   }
4109 }
4110 
4111 bool ASTReader::loadGlobalIndex() {
4112   if (GlobalIndex)
4113     return false;
4114 
4115   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4116       !PP.getLangOpts().Modules)
4117     return true;
4118 
4119   // Try to load the global index.
4120   TriedLoadingGlobalIndex = true;
4121   StringRef ModuleCachePath
4122     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4123   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4124       GlobalModuleIndex::readIndex(ModuleCachePath);
4125   if (llvm::Error Err = std::move(Result.second)) {
4126     assert(!Result.first);
4127     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4128     return true;
4129   }
4130 
4131   GlobalIndex.reset(Result.first);
4132   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4133   return false;
4134 }
4135 
4136 bool ASTReader::isGlobalIndexUnavailable() const {
4137   return PP.getLangOpts().Modules && UseGlobalIndex &&
4138          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4139 }
4140 
4141 static void updateModuleTimestamp(ModuleFile &MF) {
4142   // Overwrite the timestamp file contents so that file's mtime changes.
4143   std::string TimestampFilename = MF.getTimestampFilename();
4144   std::error_code EC;
4145   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4146   if (EC)
4147     return;
4148   OS << "Timestamp file\n";
4149   OS.close();
4150   OS.clear_error(); // Avoid triggering a fatal error.
4151 }
4152 
4153 /// Given a cursor at the start of an AST file, scan ahead and drop the
4154 /// cursor into the start of the given block ID, returning false on success and
4155 /// true on failure.
4156 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4157   while (true) {
4158     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4159     if (!MaybeEntry) {
4160       // FIXME this drops errors on the floor.
4161       consumeError(MaybeEntry.takeError());
4162       return true;
4163     }
4164     llvm::BitstreamEntry Entry = MaybeEntry.get();
4165 
4166     switch (Entry.Kind) {
4167     case llvm::BitstreamEntry::Error:
4168     case llvm::BitstreamEntry::EndBlock:
4169       return true;
4170 
4171     case llvm::BitstreamEntry::Record:
4172       // Ignore top-level records.
4173       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4174         break;
4175       else {
4176         // FIXME this drops errors on the floor.
4177         consumeError(Skipped.takeError());
4178         return true;
4179       }
4180 
4181     case llvm::BitstreamEntry::SubBlock:
4182       if (Entry.ID == BlockID) {
4183         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4184           // FIXME this drops the error on the floor.
4185           consumeError(std::move(Err));
4186           return true;
4187         }
4188         // Found it!
4189         return false;
4190       }
4191 
4192       if (llvm::Error Err = Cursor.SkipBlock()) {
4193         // FIXME this drops the error on the floor.
4194         consumeError(std::move(Err));
4195         return true;
4196       }
4197     }
4198   }
4199 }
4200 
4201 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4202                                             ModuleKind Type,
4203                                             SourceLocation ImportLoc,
4204                                             unsigned ClientLoadCapabilities,
4205                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4206   llvm::SaveAndRestore<SourceLocation>
4207     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4208 
4209   // Defer any pending actions until we get to the end of reading the AST file.
4210   Deserializing AnASTFile(this);
4211 
4212   // Bump the generation number.
4213   unsigned PreviousGeneration = 0;
4214   if (ContextObj)
4215     PreviousGeneration = incrementGeneration(*ContextObj);
4216 
4217   unsigned NumModules = ModuleMgr.size();
4218   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4219     assert(ReadResult && "expected to return error");
4220     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4221                             PP.getLangOpts().Modules
4222                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4223                                 : nullptr);
4224 
4225     // If we find that any modules are unusable, the global index is going
4226     // to be out-of-date. Just remove it.
4227     GlobalIndex.reset();
4228     ModuleMgr.setGlobalIndex(nullptr);
4229     return ReadResult;
4230   };
4231 
4232   SmallVector<ImportedModule, 4> Loaded;
4233   switch (ASTReadResult ReadResult =
4234               ReadASTCore(FileName, Type, ImportLoc,
4235                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4236                           ASTFileSignature(), ClientLoadCapabilities)) {
4237   case Failure:
4238   case Missing:
4239   case OutOfDate:
4240   case VersionMismatch:
4241   case ConfigurationMismatch:
4242   case HadErrors:
4243     return removeModulesAndReturn(ReadResult);
4244   case Success:
4245     break;
4246   }
4247 
4248   // Here comes stuff that we only do once the entire chain is loaded.
4249 
4250   // Load the AST blocks of all of the modules that we loaded.  We can still
4251   // hit errors parsing the ASTs at this point.
4252   for (ImportedModule &M : Loaded) {
4253     ModuleFile &F = *M.Mod;
4254 
4255     // Read the AST block.
4256     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4257       return removeModulesAndReturn(Result);
4258 
4259     // The AST block should always have a definition for the main module.
4260     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4261       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4262       return removeModulesAndReturn(Failure);
4263     }
4264 
4265     // Read the extension blocks.
4266     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4267       if (ASTReadResult Result = ReadExtensionBlock(F))
4268         return removeModulesAndReturn(Result);
4269     }
4270 
4271     // Once read, set the ModuleFile bit base offset and update the size in
4272     // bits of all files we've seen.
4273     F.GlobalBitOffset = TotalModulesSizeInBits;
4274     TotalModulesSizeInBits += F.SizeInBits;
4275     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4276   }
4277 
4278   // Preload source locations and interesting indentifiers.
4279   for (ImportedModule &M : Loaded) {
4280     ModuleFile &F = *M.Mod;
4281 
4282     // Preload SLocEntries.
4283     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4284       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4285       // Load it through the SourceManager and don't call ReadSLocEntry()
4286       // directly because the entry may have already been loaded in which case
4287       // calling ReadSLocEntry() directly would trigger an assertion in
4288       // SourceManager.
4289       SourceMgr.getLoadedSLocEntryByID(Index);
4290     }
4291 
4292     // Map the original source file ID into the ID space of the current
4293     // compilation.
4294     if (F.OriginalSourceFileID.isValid()) {
4295       F.OriginalSourceFileID = FileID::get(
4296           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4297     }
4298 
4299     // Preload all the pending interesting identifiers by marking them out of
4300     // date.
4301     for (auto Offset : F.PreloadIdentifierOffsets) {
4302       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4303           F.IdentifierTableData + Offset);
4304 
4305       ASTIdentifierLookupTrait Trait(*this, F);
4306       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4307       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4308       auto &II = PP.getIdentifierTable().getOwn(Key);
4309       II.setOutOfDate(true);
4310 
4311       // Mark this identifier as being from an AST file so that we can track
4312       // whether we need to serialize it.
4313       markIdentifierFromAST(*this, II);
4314 
4315       // Associate the ID with the identifier so that the writer can reuse it.
4316       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4317       SetIdentifierInfo(ID, &II);
4318     }
4319   }
4320 
4321   // Setup the import locations and notify the module manager that we've
4322   // committed to these module files.
4323   for (ImportedModule &M : Loaded) {
4324     ModuleFile &F = *M.Mod;
4325 
4326     ModuleMgr.moduleFileAccepted(&F);
4327 
4328     // Set the import location.
4329     F.DirectImportLoc = ImportLoc;
4330     // FIXME: We assume that locations from PCH / preamble do not need
4331     // any translation.
4332     if (!M.ImportedBy)
4333       F.ImportLoc = M.ImportLoc;
4334     else
4335       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4336   }
4337 
4338   if (!PP.getLangOpts().CPlusPlus ||
4339       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4340        Type != MK_PrebuiltModule)) {
4341     // Mark all of the identifiers in the identifier table as being out of date,
4342     // so that various accessors know to check the loaded modules when the
4343     // identifier is used.
4344     //
4345     // For C++ modules, we don't need information on many identifiers (just
4346     // those that provide macros or are poisoned), so we mark all of
4347     // the interesting ones via PreloadIdentifierOffsets.
4348     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4349                                 IdEnd = PP.getIdentifierTable().end();
4350          Id != IdEnd; ++Id)
4351       Id->second->setOutOfDate(true);
4352   }
4353   // Mark selectors as out of date.
4354   for (auto Sel : SelectorGeneration)
4355     SelectorOutOfDate[Sel.first] = true;
4356 
4357   // Resolve any unresolved module exports.
4358   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4359     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4360     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4361     Module *ResolvedMod = getSubmodule(GlobalID);
4362 
4363     switch (Unresolved.Kind) {
4364     case UnresolvedModuleRef::Conflict:
4365       if (ResolvedMod) {
4366         Module::Conflict Conflict;
4367         Conflict.Other = ResolvedMod;
4368         Conflict.Message = Unresolved.String.str();
4369         Unresolved.Mod->Conflicts.push_back(Conflict);
4370       }
4371       continue;
4372 
4373     case UnresolvedModuleRef::Import:
4374       if (ResolvedMod)
4375         Unresolved.Mod->Imports.insert(ResolvedMod);
4376       continue;
4377 
4378     case UnresolvedModuleRef::Export:
4379       if (ResolvedMod || Unresolved.IsWildcard)
4380         Unresolved.Mod->Exports.push_back(
4381           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4382       continue;
4383     }
4384   }
4385   UnresolvedModuleRefs.clear();
4386 
4387   if (Imported)
4388     Imported->append(ImportedModules.begin(),
4389                      ImportedModules.end());
4390 
4391   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4392   // Might be unnecessary as use declarations are only used to build the
4393   // module itself.
4394 
4395   if (ContextObj)
4396     InitializeContext();
4397 
4398   if (SemaObj)
4399     UpdateSema();
4400 
4401   if (DeserializationListener)
4402     DeserializationListener->ReaderInitialized(this);
4403 
4404   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4405   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4406     // If this AST file is a precompiled preamble, then set the
4407     // preamble file ID of the source manager to the file source file
4408     // from which the preamble was built.
4409     if (Type == MK_Preamble) {
4410       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4411     } else if (Type == MK_MainFile) {
4412       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4413     }
4414   }
4415 
4416   // For any Objective-C class definitions we have already loaded, make sure
4417   // that we load any additional categories.
4418   if (ContextObj) {
4419     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4420       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4421                          ObjCClassesLoaded[I],
4422                          PreviousGeneration);
4423     }
4424   }
4425 
4426   if (PP.getHeaderSearchInfo()
4427           .getHeaderSearchOpts()
4428           .ModulesValidateOncePerBuildSession) {
4429     // Now we are certain that the module and all modules it depends on are
4430     // up to date.  Create or update timestamp files for modules that are
4431     // located in the module cache (not for PCH files that could be anywhere
4432     // in the filesystem).
4433     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4434       ImportedModule &M = Loaded[I];
4435       if (M.Mod->Kind == MK_ImplicitModule) {
4436         updateModuleTimestamp(*M.Mod);
4437       }
4438     }
4439   }
4440 
4441   return Success;
4442 }
4443 
4444 static ASTFileSignature readASTFileSignature(StringRef PCH);
4445 
4446 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4447 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4448   // FIXME checking magic headers is done in other places such as
4449   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4450   // always done the same. Unify it all with a helper.
4451   if (!Stream.canSkipToPos(4))
4452     return llvm::createStringError(std::errc::illegal_byte_sequence,
4453                                    "file too small to contain AST file magic");
4454   for (unsigned C : {'C', 'P', 'C', 'H'})
4455     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4456       if (Res.get() != C)
4457         return llvm::createStringError(
4458             std::errc::illegal_byte_sequence,
4459             "file doesn't start with AST file magic");
4460     } else
4461       return Res.takeError();
4462   return llvm::Error::success();
4463 }
4464 
4465 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4466   switch (Kind) {
4467   case MK_PCH:
4468     return 0; // PCH
4469   case MK_ImplicitModule:
4470   case MK_ExplicitModule:
4471   case MK_PrebuiltModule:
4472     return 1; // module
4473   case MK_MainFile:
4474   case MK_Preamble:
4475     return 2; // main source file
4476   }
4477   llvm_unreachable("unknown module kind");
4478 }
4479 
4480 ASTReader::ASTReadResult
4481 ASTReader::ReadASTCore(StringRef FileName,
4482                        ModuleKind Type,
4483                        SourceLocation ImportLoc,
4484                        ModuleFile *ImportedBy,
4485                        SmallVectorImpl<ImportedModule> &Loaded,
4486                        off_t ExpectedSize, time_t ExpectedModTime,
4487                        ASTFileSignature ExpectedSignature,
4488                        unsigned ClientLoadCapabilities) {
4489   ModuleFile *M;
4490   std::string ErrorStr;
4491   ModuleManager::AddModuleResult AddResult
4492     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4493                           getGeneration(), ExpectedSize, ExpectedModTime,
4494                           ExpectedSignature, readASTFileSignature,
4495                           M, ErrorStr);
4496 
4497   switch (AddResult) {
4498   case ModuleManager::AlreadyLoaded:
4499     Diag(diag::remark_module_import)
4500         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4501         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4502     return Success;
4503 
4504   case ModuleManager::NewlyLoaded:
4505     // Load module file below.
4506     break;
4507 
4508   case ModuleManager::Missing:
4509     // The module file was missing; if the client can handle that, return
4510     // it.
4511     if (ClientLoadCapabilities & ARR_Missing)
4512       return Missing;
4513 
4514     // Otherwise, return an error.
4515     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4516                                           << FileName << !ErrorStr.empty()
4517                                           << ErrorStr;
4518     return Failure;
4519 
4520   case ModuleManager::OutOfDate:
4521     // We couldn't load the module file because it is out-of-date. If the
4522     // client can handle out-of-date, return it.
4523     if (ClientLoadCapabilities & ARR_OutOfDate)
4524       return OutOfDate;
4525 
4526     // Otherwise, return an error.
4527     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4528                                             << FileName << !ErrorStr.empty()
4529                                             << ErrorStr;
4530     return Failure;
4531   }
4532 
4533   assert(M && "Missing module file");
4534 
4535   bool ShouldFinalizePCM = false;
4536   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4537     auto &MC = getModuleManager().getModuleCache();
4538     if (ShouldFinalizePCM)
4539       MC.finalizePCM(FileName);
4540     else
4541       MC.tryToDropPCM(FileName);
4542   });
4543   ModuleFile &F = *M;
4544   BitstreamCursor &Stream = F.Stream;
4545   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4546   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4547 
4548   // Sniff for the signature.
4549   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4550     Diag(diag::err_module_file_invalid)
4551         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4552     return Failure;
4553   }
4554 
4555   // This is used for compatibility with older PCH formats.
4556   bool HaveReadControlBlock = false;
4557   while (true) {
4558     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4559     if (!MaybeEntry) {
4560       Error(MaybeEntry.takeError());
4561       return Failure;
4562     }
4563     llvm::BitstreamEntry Entry = MaybeEntry.get();
4564 
4565     switch (Entry.Kind) {
4566     case llvm::BitstreamEntry::Error:
4567     case llvm::BitstreamEntry::Record:
4568     case llvm::BitstreamEntry::EndBlock:
4569       Error("invalid record at top-level of AST file");
4570       return Failure;
4571 
4572     case llvm::BitstreamEntry::SubBlock:
4573       break;
4574     }
4575 
4576     switch (Entry.ID) {
4577     case CONTROL_BLOCK_ID:
4578       HaveReadControlBlock = true;
4579       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4580       case Success:
4581         // Check that we didn't try to load a non-module AST file as a module.
4582         //
4583         // FIXME: Should we also perform the converse check? Loading a module as
4584         // a PCH file sort of works, but it's a bit wonky.
4585         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4586              Type == MK_PrebuiltModule) &&
4587             F.ModuleName.empty()) {
4588           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4589           if (Result != OutOfDate ||
4590               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4591             Diag(diag::err_module_file_not_module) << FileName;
4592           return Result;
4593         }
4594         break;
4595 
4596       case Failure: return Failure;
4597       case Missing: return Missing;
4598       case OutOfDate: return OutOfDate;
4599       case VersionMismatch: return VersionMismatch;
4600       case ConfigurationMismatch: return ConfigurationMismatch;
4601       case HadErrors: return HadErrors;
4602       }
4603       break;
4604 
4605     case AST_BLOCK_ID:
4606       if (!HaveReadControlBlock) {
4607         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4608           Diag(diag::err_pch_version_too_old);
4609         return VersionMismatch;
4610       }
4611 
4612       // Record that we've loaded this module.
4613       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4614       ShouldFinalizePCM = true;
4615       return Success;
4616 
4617     case UNHASHED_CONTROL_BLOCK_ID:
4618       // This block is handled using look-ahead during ReadControlBlock.  We
4619       // shouldn't get here!
4620       Error("malformed block record in AST file");
4621       return Failure;
4622 
4623     default:
4624       if (llvm::Error Err = Stream.SkipBlock()) {
4625         Error(std::move(Err));
4626         return Failure;
4627       }
4628       break;
4629     }
4630   }
4631 
4632   llvm_unreachable("unexpected break; expected return");
4633 }
4634 
4635 ASTReader::ASTReadResult
4636 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4637                                     unsigned ClientLoadCapabilities) {
4638   const HeaderSearchOptions &HSOpts =
4639       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4640   bool AllowCompatibleConfigurationMismatch =
4641       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4642 
4643   ASTReadResult Result = readUnhashedControlBlockImpl(
4644       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4645       Listener.get(),
4646       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4647 
4648   // If F was directly imported by another module, it's implicitly validated by
4649   // the importing module.
4650   if (DisableValidation || WasImportedBy ||
4651       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4652     return Success;
4653 
4654   if (Result == Failure) {
4655     Error("malformed block record in AST file");
4656     return Failure;
4657   }
4658 
4659   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4660     // If this module has already been finalized in the ModuleCache, we're stuck
4661     // with it; we can only load a single version of each module.
4662     //
4663     // This can happen when a module is imported in two contexts: in one, as a
4664     // user module; in another, as a system module (due to an import from
4665     // another module marked with the [system] flag).  It usually indicates a
4666     // bug in the module map: this module should also be marked with [system].
4667     //
4668     // If -Wno-system-headers (the default), and the first import is as a
4669     // system module, then validation will fail during the as-user import,
4670     // since -Werror flags won't have been validated.  However, it's reasonable
4671     // to treat this consistently as a system module.
4672     //
4673     // If -Wsystem-headers, the PCM on disk was built with
4674     // -Wno-system-headers, and the first import is as a user module, then
4675     // validation will fail during the as-system import since the PCM on disk
4676     // doesn't guarantee that -Werror was respected.  However, the -Werror
4677     // flags were checked during the initial as-user import.
4678     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4679       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4680       return Success;
4681     }
4682   }
4683 
4684   return Result;
4685 }
4686 
4687 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4688     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4689     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4690     bool ValidateDiagnosticOptions) {
4691   // Initialize a stream.
4692   BitstreamCursor Stream(StreamData);
4693 
4694   // Sniff for the signature.
4695   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4696     // FIXME this drops the error on the floor.
4697     consumeError(std::move(Err));
4698     return Failure;
4699   }
4700 
4701   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4702   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4703     return Failure;
4704 
4705   // Read all of the records in the options block.
4706   RecordData Record;
4707   ASTReadResult Result = Success;
4708   while (true) {
4709     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4710     if (!MaybeEntry) {
4711       // FIXME this drops the error on the floor.
4712       consumeError(MaybeEntry.takeError());
4713       return Failure;
4714     }
4715     llvm::BitstreamEntry Entry = MaybeEntry.get();
4716 
4717     switch (Entry.Kind) {
4718     case llvm::BitstreamEntry::Error:
4719     case llvm::BitstreamEntry::SubBlock:
4720       return Failure;
4721 
4722     case llvm::BitstreamEntry::EndBlock:
4723       return Result;
4724 
4725     case llvm::BitstreamEntry::Record:
4726       // The interesting case.
4727       break;
4728     }
4729 
4730     // Read and process a record.
4731     Record.clear();
4732     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4733     if (!MaybeRecordType) {
4734       // FIXME this drops the error.
4735       return Failure;
4736     }
4737     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4738     case SIGNATURE:
4739       if (F)
4740         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4741       break;
4742     case AST_BLOCK_HASH:
4743       if (F)
4744         F->ASTBlockHash =
4745             ASTFileSignature::create(Record.begin(), Record.end());
4746       break;
4747     case DIAGNOSTIC_OPTIONS: {
4748       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4749       if (Listener && ValidateDiagnosticOptions &&
4750           !AllowCompatibleConfigurationMismatch &&
4751           ParseDiagnosticOptions(Record, Complain, *Listener))
4752         Result = OutOfDate; // Don't return early.  Read the signature.
4753       break;
4754     }
4755     case DIAG_PRAGMA_MAPPINGS:
4756       if (!F)
4757         break;
4758       if (F->PragmaDiagMappings.empty())
4759         F->PragmaDiagMappings.swap(Record);
4760       else
4761         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4762                                      Record.begin(), Record.end());
4763       break;
4764     }
4765   }
4766 }
4767 
4768 /// Parse a record and blob containing module file extension metadata.
4769 static bool parseModuleFileExtensionMetadata(
4770               const SmallVectorImpl<uint64_t> &Record,
4771               StringRef Blob,
4772               ModuleFileExtensionMetadata &Metadata) {
4773   if (Record.size() < 4) return true;
4774 
4775   Metadata.MajorVersion = Record[0];
4776   Metadata.MinorVersion = Record[1];
4777 
4778   unsigned BlockNameLen = Record[2];
4779   unsigned UserInfoLen = Record[3];
4780 
4781   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4782 
4783   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4784   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4785                                   Blob.data() + BlockNameLen + UserInfoLen);
4786   return false;
4787 }
4788 
4789 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4790   BitstreamCursor &Stream = F.Stream;
4791 
4792   RecordData Record;
4793   while (true) {
4794     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4795     if (!MaybeEntry) {
4796       Error(MaybeEntry.takeError());
4797       return Failure;
4798     }
4799     llvm::BitstreamEntry Entry = MaybeEntry.get();
4800 
4801     switch (Entry.Kind) {
4802     case llvm::BitstreamEntry::SubBlock:
4803       if (llvm::Error Err = Stream.SkipBlock()) {
4804         Error(std::move(Err));
4805         return Failure;
4806       }
4807       continue;
4808 
4809     case llvm::BitstreamEntry::EndBlock:
4810       return Success;
4811 
4812     case llvm::BitstreamEntry::Error:
4813       return HadErrors;
4814 
4815     case llvm::BitstreamEntry::Record:
4816       break;
4817     }
4818 
4819     Record.clear();
4820     StringRef Blob;
4821     Expected<unsigned> MaybeRecCode =
4822         Stream.readRecord(Entry.ID, Record, &Blob);
4823     if (!MaybeRecCode) {
4824       Error(MaybeRecCode.takeError());
4825       return Failure;
4826     }
4827     switch (MaybeRecCode.get()) {
4828     case EXTENSION_METADATA: {
4829       ModuleFileExtensionMetadata Metadata;
4830       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4831         Error("malformed EXTENSION_METADATA in AST file");
4832         return Failure;
4833       }
4834 
4835       // Find a module file extension with this block name.
4836       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4837       if (Known == ModuleFileExtensions.end()) break;
4838 
4839       // Form a reader.
4840       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4841                                                              F, Stream)) {
4842         F.ExtensionReaders.push_back(std::move(Reader));
4843       }
4844 
4845       break;
4846     }
4847     }
4848   }
4849 
4850   return Success;
4851 }
4852 
4853 void ASTReader::InitializeContext() {
4854   assert(ContextObj && "no context to initialize");
4855   ASTContext &Context = *ContextObj;
4856 
4857   // If there's a listener, notify them that we "read" the translation unit.
4858   if (DeserializationListener)
4859     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4860                                       Context.getTranslationUnitDecl());
4861 
4862   // FIXME: Find a better way to deal with collisions between these
4863   // built-in types. Right now, we just ignore the problem.
4864 
4865   // Load the special types.
4866   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4867     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4868       if (!Context.CFConstantStringTypeDecl)
4869         Context.setCFConstantStringType(GetType(String));
4870     }
4871 
4872     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4873       QualType FileType = GetType(File);
4874       if (FileType.isNull()) {
4875         Error("FILE type is NULL");
4876         return;
4877       }
4878 
4879       if (!Context.FILEDecl) {
4880         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4881           Context.setFILEDecl(Typedef->getDecl());
4882         else {
4883           const TagType *Tag = FileType->getAs<TagType>();
4884           if (!Tag) {
4885             Error("Invalid FILE type in AST file");
4886             return;
4887           }
4888           Context.setFILEDecl(Tag->getDecl());
4889         }
4890       }
4891     }
4892 
4893     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4894       QualType Jmp_bufType = GetType(Jmp_buf);
4895       if (Jmp_bufType.isNull()) {
4896         Error("jmp_buf type is NULL");
4897         return;
4898       }
4899 
4900       if (!Context.jmp_bufDecl) {
4901         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4902           Context.setjmp_bufDecl(Typedef->getDecl());
4903         else {
4904           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4905           if (!Tag) {
4906             Error("Invalid jmp_buf type in AST file");
4907             return;
4908           }
4909           Context.setjmp_bufDecl(Tag->getDecl());
4910         }
4911       }
4912     }
4913 
4914     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4915       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4916       if (Sigjmp_bufType.isNull()) {
4917         Error("sigjmp_buf type is NULL");
4918         return;
4919       }
4920 
4921       if (!Context.sigjmp_bufDecl) {
4922         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4923           Context.setsigjmp_bufDecl(Typedef->getDecl());
4924         else {
4925           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4926           assert(Tag && "Invalid sigjmp_buf type in AST file");
4927           Context.setsigjmp_bufDecl(Tag->getDecl());
4928         }
4929       }
4930     }
4931 
4932     if (unsigned ObjCIdRedef
4933           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4934       if (Context.ObjCIdRedefinitionType.isNull())
4935         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4936     }
4937 
4938     if (unsigned ObjCClassRedef
4939           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4940       if (Context.ObjCClassRedefinitionType.isNull())
4941         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4942     }
4943 
4944     if (unsigned ObjCSelRedef
4945           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4946       if (Context.ObjCSelRedefinitionType.isNull())
4947         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4948     }
4949 
4950     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4951       QualType Ucontext_tType = GetType(Ucontext_t);
4952       if (Ucontext_tType.isNull()) {
4953         Error("ucontext_t type is NULL");
4954         return;
4955       }
4956 
4957       if (!Context.ucontext_tDecl) {
4958         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4959           Context.setucontext_tDecl(Typedef->getDecl());
4960         else {
4961           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4962           assert(Tag && "Invalid ucontext_t type in AST file");
4963           Context.setucontext_tDecl(Tag->getDecl());
4964         }
4965       }
4966     }
4967   }
4968 
4969   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4970 
4971   // If there were any CUDA special declarations, deserialize them.
4972   if (!CUDASpecialDeclRefs.empty()) {
4973     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4974     Context.setcudaConfigureCallDecl(
4975                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4976   }
4977 
4978   // Re-export any modules that were imported by a non-module AST file.
4979   // FIXME: This does not make macro-only imports visible again.
4980   for (auto &Import : ImportedModules) {
4981     if (Module *Imported = getSubmodule(Import.ID)) {
4982       makeModuleVisible(Imported, Module::AllVisible,
4983                         /*ImportLoc=*/Import.ImportLoc);
4984       if (Import.ImportLoc.isValid())
4985         PP.makeModuleVisible(Imported, Import.ImportLoc);
4986       // This updates visibility for Preprocessor only. For Sema, which can be
4987       // nullptr here, we do the same later, in UpdateSema().
4988     }
4989   }
4990 }
4991 
4992 void ASTReader::finalizeForWriting() {
4993   // Nothing to do for now.
4994 }
4995 
4996 /// Reads and return the signature record from \p PCH's control block, or
4997 /// else returns 0.
4998 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4999   BitstreamCursor Stream(PCH);
5000   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5001     // FIXME this drops the error on the floor.
5002     consumeError(std::move(Err));
5003     return ASTFileSignature();
5004   }
5005 
5006   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5007   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5008     return ASTFileSignature();
5009 
5010   // Scan for SIGNATURE inside the diagnostic options block.
5011   ASTReader::RecordData Record;
5012   while (true) {
5013     Expected<llvm::BitstreamEntry> MaybeEntry =
5014         Stream.advanceSkippingSubblocks();
5015     if (!MaybeEntry) {
5016       // FIXME this drops the error on the floor.
5017       consumeError(MaybeEntry.takeError());
5018       return ASTFileSignature();
5019     }
5020     llvm::BitstreamEntry Entry = MaybeEntry.get();
5021 
5022     if (Entry.Kind != llvm::BitstreamEntry::Record)
5023       return ASTFileSignature();
5024 
5025     Record.clear();
5026     StringRef Blob;
5027     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5028     if (!MaybeRecord) {
5029       // FIXME this drops the error on the floor.
5030       consumeError(MaybeRecord.takeError());
5031       return ASTFileSignature();
5032     }
5033     if (SIGNATURE == MaybeRecord.get())
5034       return ASTFileSignature::create(Record.begin(),
5035                                       Record.begin() + ASTFileSignature::size);
5036   }
5037 }
5038 
5039 /// Retrieve the name of the original source file name
5040 /// directly from the AST file, without actually loading the AST
5041 /// file.
5042 std::string ASTReader::getOriginalSourceFile(
5043     const std::string &ASTFileName, FileManager &FileMgr,
5044     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5045   // Open the AST file.
5046   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5047   if (!Buffer) {
5048     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5049         << ASTFileName << Buffer.getError().message();
5050     return std::string();
5051   }
5052 
5053   // Initialize the stream
5054   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5055 
5056   // Sniff for the signature.
5057   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5058     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5059     return std::string();
5060   }
5061 
5062   // Scan for the CONTROL_BLOCK_ID block.
5063   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5064     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5065     return std::string();
5066   }
5067 
5068   // Scan for ORIGINAL_FILE inside the control block.
5069   RecordData Record;
5070   while (true) {
5071     Expected<llvm::BitstreamEntry> MaybeEntry =
5072         Stream.advanceSkippingSubblocks();
5073     if (!MaybeEntry) {
5074       // FIXME this drops errors on the floor.
5075       consumeError(MaybeEntry.takeError());
5076       return std::string();
5077     }
5078     llvm::BitstreamEntry Entry = MaybeEntry.get();
5079 
5080     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5081       return std::string();
5082 
5083     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5084       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5085       return std::string();
5086     }
5087 
5088     Record.clear();
5089     StringRef Blob;
5090     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5091     if (!MaybeRecord) {
5092       // FIXME this drops the errors on the floor.
5093       consumeError(MaybeRecord.takeError());
5094       return std::string();
5095     }
5096     if (ORIGINAL_FILE == MaybeRecord.get())
5097       return Blob.str();
5098   }
5099 }
5100 
5101 namespace {
5102 
5103   class SimplePCHValidator : public ASTReaderListener {
5104     const LangOptions &ExistingLangOpts;
5105     const TargetOptions &ExistingTargetOpts;
5106     const PreprocessorOptions &ExistingPPOpts;
5107     std::string ExistingModuleCachePath;
5108     FileManager &FileMgr;
5109 
5110   public:
5111     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5112                        const TargetOptions &ExistingTargetOpts,
5113                        const PreprocessorOptions &ExistingPPOpts,
5114                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5115         : ExistingLangOpts(ExistingLangOpts),
5116           ExistingTargetOpts(ExistingTargetOpts),
5117           ExistingPPOpts(ExistingPPOpts),
5118           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5119 
5120     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5121                              bool AllowCompatibleDifferences) override {
5122       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5123                                   AllowCompatibleDifferences);
5124     }
5125 
5126     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5127                            bool AllowCompatibleDifferences) override {
5128       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5129                                 AllowCompatibleDifferences);
5130     }
5131 
5132     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5133                                  StringRef SpecificModuleCachePath,
5134                                  bool Complain) override {
5135       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5136                                       ExistingModuleCachePath,
5137                                       nullptr, ExistingLangOpts);
5138     }
5139 
5140     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5141                                  bool Complain,
5142                                  std::string &SuggestedPredefines) override {
5143       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5144                                       SuggestedPredefines, ExistingLangOpts);
5145     }
5146   };
5147 
5148 } // namespace
5149 
5150 bool ASTReader::readASTFileControlBlock(
5151     StringRef Filename, FileManager &FileMgr,
5152     const PCHContainerReader &PCHContainerRdr,
5153     bool FindModuleFileExtensions,
5154     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5155   // Open the AST file.
5156   // FIXME: This allows use of the VFS; we do not allow use of the
5157   // VFS when actually loading a module.
5158   auto Buffer = FileMgr.getBufferForFile(Filename);
5159   if (!Buffer) {
5160     return true;
5161   }
5162 
5163   // Initialize the stream
5164   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5165   BitstreamCursor Stream(Bytes);
5166 
5167   // Sniff for the signature.
5168   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5169     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5170     return true;
5171   }
5172 
5173   // Scan for the CONTROL_BLOCK_ID block.
5174   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5175     return true;
5176 
5177   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5178   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5179   bool NeedsImports = Listener.needsImportVisitation();
5180   BitstreamCursor InputFilesCursor;
5181 
5182   RecordData Record;
5183   std::string ModuleDir;
5184   bool DoneWithControlBlock = false;
5185   while (!DoneWithControlBlock) {
5186     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5187     if (!MaybeEntry) {
5188       // FIXME this drops the error on the floor.
5189       consumeError(MaybeEntry.takeError());
5190       return true;
5191     }
5192     llvm::BitstreamEntry Entry = MaybeEntry.get();
5193 
5194     switch (Entry.Kind) {
5195     case llvm::BitstreamEntry::SubBlock: {
5196       switch (Entry.ID) {
5197       case OPTIONS_BLOCK_ID: {
5198         std::string IgnoredSuggestedPredefines;
5199         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5200                              /*AllowCompatibleConfigurationMismatch*/ false,
5201                              Listener, IgnoredSuggestedPredefines) != Success)
5202           return true;
5203         break;
5204       }
5205 
5206       case INPUT_FILES_BLOCK_ID:
5207         InputFilesCursor = Stream;
5208         if (llvm::Error Err = Stream.SkipBlock()) {
5209           // FIXME this drops the error on the floor.
5210           consumeError(std::move(Err));
5211           return true;
5212         }
5213         if (NeedsInputFiles &&
5214             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5215           return true;
5216         break;
5217 
5218       default:
5219         if (llvm::Error Err = Stream.SkipBlock()) {
5220           // FIXME this drops the error on the floor.
5221           consumeError(std::move(Err));
5222           return true;
5223         }
5224         break;
5225       }
5226 
5227       continue;
5228     }
5229 
5230     case llvm::BitstreamEntry::EndBlock:
5231       DoneWithControlBlock = true;
5232       break;
5233 
5234     case llvm::BitstreamEntry::Error:
5235       return true;
5236 
5237     case llvm::BitstreamEntry::Record:
5238       break;
5239     }
5240 
5241     if (DoneWithControlBlock) break;
5242 
5243     Record.clear();
5244     StringRef Blob;
5245     Expected<unsigned> MaybeRecCode =
5246         Stream.readRecord(Entry.ID, Record, &Blob);
5247     if (!MaybeRecCode) {
5248       // FIXME this drops the error.
5249       return Failure;
5250     }
5251     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5252     case METADATA:
5253       if (Record[0] != VERSION_MAJOR)
5254         return true;
5255       if (Listener.ReadFullVersionInformation(Blob))
5256         return true;
5257       break;
5258     case MODULE_NAME:
5259       Listener.ReadModuleName(Blob);
5260       break;
5261     case MODULE_DIRECTORY:
5262       ModuleDir = std::string(Blob);
5263       break;
5264     case MODULE_MAP_FILE: {
5265       unsigned Idx = 0;
5266       auto Path = ReadString(Record, Idx);
5267       ResolveImportedPath(Path, ModuleDir);
5268       Listener.ReadModuleMapFile(Path);
5269       break;
5270     }
5271     case INPUT_FILE_OFFSETS: {
5272       if (!NeedsInputFiles)
5273         break;
5274 
5275       unsigned NumInputFiles = Record[0];
5276       unsigned NumUserFiles = Record[1];
5277       const llvm::support::unaligned_uint64_t *InputFileOffs =
5278           (const llvm::support::unaligned_uint64_t *)Blob.data();
5279       for (unsigned I = 0; I != NumInputFiles; ++I) {
5280         // Go find this input file.
5281         bool isSystemFile = I >= NumUserFiles;
5282 
5283         if (isSystemFile && !NeedsSystemInputFiles)
5284           break; // the rest are system input files
5285 
5286         BitstreamCursor &Cursor = InputFilesCursor;
5287         SavedStreamPosition SavedPosition(Cursor);
5288         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5289           // FIXME this drops errors on the floor.
5290           consumeError(std::move(Err));
5291         }
5292 
5293         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5294         if (!MaybeCode) {
5295           // FIXME this drops errors on the floor.
5296           consumeError(MaybeCode.takeError());
5297         }
5298         unsigned Code = MaybeCode.get();
5299 
5300         RecordData Record;
5301         StringRef Blob;
5302         bool shouldContinue = false;
5303         Expected<unsigned> MaybeRecordType =
5304             Cursor.readRecord(Code, Record, &Blob);
5305         if (!MaybeRecordType) {
5306           // FIXME this drops errors on the floor.
5307           consumeError(MaybeRecordType.takeError());
5308         }
5309         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5310         case INPUT_FILE_HASH:
5311           break;
5312         case INPUT_FILE:
5313           bool Overridden = static_cast<bool>(Record[3]);
5314           std::string Filename = std::string(Blob);
5315           ResolveImportedPath(Filename, ModuleDir);
5316           shouldContinue = Listener.visitInputFile(
5317               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5318           break;
5319         }
5320         if (!shouldContinue)
5321           break;
5322       }
5323       break;
5324     }
5325 
5326     case IMPORTS: {
5327       if (!NeedsImports)
5328         break;
5329 
5330       unsigned Idx = 0, N = Record.size();
5331       while (Idx < N) {
5332         // Read information about the AST file.
5333         Idx +=
5334             1 + 1 + 1 + 1 +
5335             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5336         std::string ModuleName = ReadString(Record, Idx);
5337         std::string Filename = ReadString(Record, Idx);
5338         ResolveImportedPath(Filename, ModuleDir);
5339         Listener.visitImport(ModuleName, Filename);
5340       }
5341       break;
5342     }
5343 
5344     default:
5345       // No other validation to perform.
5346       break;
5347     }
5348   }
5349 
5350   // Look for module file extension blocks, if requested.
5351   if (FindModuleFileExtensions) {
5352     BitstreamCursor SavedStream = Stream;
5353     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5354       bool DoneWithExtensionBlock = false;
5355       while (!DoneWithExtensionBlock) {
5356         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5357         if (!MaybeEntry) {
5358           // FIXME this drops the error.
5359           return true;
5360         }
5361         llvm::BitstreamEntry Entry = MaybeEntry.get();
5362 
5363         switch (Entry.Kind) {
5364         case llvm::BitstreamEntry::SubBlock:
5365           if (llvm::Error Err = Stream.SkipBlock()) {
5366             // FIXME this drops the error on the floor.
5367             consumeError(std::move(Err));
5368             return true;
5369           }
5370           continue;
5371 
5372         case llvm::BitstreamEntry::EndBlock:
5373           DoneWithExtensionBlock = true;
5374           continue;
5375 
5376         case llvm::BitstreamEntry::Error:
5377           return true;
5378 
5379         case llvm::BitstreamEntry::Record:
5380           break;
5381         }
5382 
5383        Record.clear();
5384        StringRef Blob;
5385        Expected<unsigned> MaybeRecCode =
5386            Stream.readRecord(Entry.ID, Record, &Blob);
5387        if (!MaybeRecCode) {
5388          // FIXME this drops the error.
5389          return true;
5390        }
5391        switch (MaybeRecCode.get()) {
5392        case EXTENSION_METADATA: {
5393          ModuleFileExtensionMetadata Metadata;
5394          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5395            return true;
5396 
5397          Listener.readModuleFileExtension(Metadata);
5398          break;
5399        }
5400        }
5401       }
5402     }
5403     Stream = SavedStream;
5404   }
5405 
5406   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5407   if (readUnhashedControlBlockImpl(
5408           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5409           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5410           ValidateDiagnosticOptions) != Success)
5411     return true;
5412 
5413   return false;
5414 }
5415 
5416 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5417                                     const PCHContainerReader &PCHContainerRdr,
5418                                     const LangOptions &LangOpts,
5419                                     const TargetOptions &TargetOpts,
5420                                     const PreprocessorOptions &PPOpts,
5421                                     StringRef ExistingModuleCachePath) {
5422   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5423                                ExistingModuleCachePath, FileMgr);
5424   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5425                                   /*FindModuleFileExtensions=*/false,
5426                                   validator,
5427                                   /*ValidateDiagnosticOptions=*/true);
5428 }
5429 
5430 ASTReader::ASTReadResult
5431 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5432   // Enter the submodule block.
5433   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5434     Error(std::move(Err));
5435     return Failure;
5436   }
5437 
5438   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5439   bool First = true;
5440   Module *CurrentModule = nullptr;
5441   RecordData Record;
5442   while (true) {
5443     Expected<llvm::BitstreamEntry> MaybeEntry =
5444         F.Stream.advanceSkippingSubblocks();
5445     if (!MaybeEntry) {
5446       Error(MaybeEntry.takeError());
5447       return Failure;
5448     }
5449     llvm::BitstreamEntry Entry = MaybeEntry.get();
5450 
5451     switch (Entry.Kind) {
5452     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5453     case llvm::BitstreamEntry::Error:
5454       Error("malformed block record in AST file");
5455       return Failure;
5456     case llvm::BitstreamEntry::EndBlock:
5457       return Success;
5458     case llvm::BitstreamEntry::Record:
5459       // The interesting case.
5460       break;
5461     }
5462 
5463     // Read a record.
5464     StringRef Blob;
5465     Record.clear();
5466     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5467     if (!MaybeKind) {
5468       Error(MaybeKind.takeError());
5469       return Failure;
5470     }
5471     unsigned Kind = MaybeKind.get();
5472 
5473     if ((Kind == SUBMODULE_METADATA) != First) {
5474       Error("submodule metadata record should be at beginning of block");
5475       return Failure;
5476     }
5477     First = false;
5478 
5479     // Submodule information is only valid if we have a current module.
5480     // FIXME: Should we error on these cases?
5481     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5482         Kind != SUBMODULE_DEFINITION)
5483       continue;
5484 
5485     switch (Kind) {
5486     default:  // Default behavior: ignore.
5487       break;
5488 
5489     case SUBMODULE_DEFINITION: {
5490       if (Record.size() < 12) {
5491         Error("malformed module definition");
5492         return Failure;
5493       }
5494 
5495       StringRef Name = Blob;
5496       unsigned Idx = 0;
5497       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5498       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5499       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5500       bool IsFramework = Record[Idx++];
5501       bool IsExplicit = Record[Idx++];
5502       bool IsSystem = Record[Idx++];
5503       bool IsExternC = Record[Idx++];
5504       bool InferSubmodules = Record[Idx++];
5505       bool InferExplicitSubmodules = Record[Idx++];
5506       bool InferExportWildcard = Record[Idx++];
5507       bool ConfigMacrosExhaustive = Record[Idx++];
5508       bool ModuleMapIsPrivate = Record[Idx++];
5509 
5510       Module *ParentModule = nullptr;
5511       if (Parent)
5512         ParentModule = getSubmodule(Parent);
5513 
5514       // Retrieve this (sub)module from the module map, creating it if
5515       // necessary.
5516       CurrentModule =
5517           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5518               .first;
5519 
5520       // FIXME: set the definition loc for CurrentModule, or call
5521       // ModMap.setInferredModuleAllowedBy()
5522 
5523       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5524       if (GlobalIndex >= SubmodulesLoaded.size() ||
5525           SubmodulesLoaded[GlobalIndex]) {
5526         Error("too many submodules");
5527         return Failure;
5528       }
5529 
5530       if (!ParentModule) {
5531         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5532           // Don't emit module relocation error if we have -fno-validate-pch
5533           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5534               CurFile != F.File) {
5535             Error(diag::err_module_file_conflict,
5536                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5537                   F.File->getName());
5538             return Failure;
5539           }
5540         }
5541 
5542         F.DidReadTopLevelSubmodule = true;
5543         CurrentModule->setASTFile(F.File);
5544         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5545       }
5546 
5547       CurrentModule->Kind = Kind;
5548       CurrentModule->Signature = F.Signature;
5549       CurrentModule->IsFromModuleFile = true;
5550       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5551       CurrentModule->IsExternC = IsExternC;
5552       CurrentModule->InferSubmodules = InferSubmodules;
5553       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5554       CurrentModule->InferExportWildcard = InferExportWildcard;
5555       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5556       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5557       if (DeserializationListener)
5558         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5559 
5560       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5561 
5562       // Clear out data that will be replaced by what is in the module file.
5563       CurrentModule->LinkLibraries.clear();
5564       CurrentModule->ConfigMacros.clear();
5565       CurrentModule->UnresolvedConflicts.clear();
5566       CurrentModule->Conflicts.clear();
5567 
5568       // The module is available unless it's missing a requirement; relevant
5569       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5570       // Missing headers that were present when the module was built do not
5571       // make it unavailable -- if we got this far, this must be an explicitly
5572       // imported module file.
5573       CurrentModule->Requirements.clear();
5574       CurrentModule->MissingHeaders.clear();
5575       CurrentModule->IsUnimportable =
5576           ParentModule && ParentModule->IsUnimportable;
5577       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5578       break;
5579     }
5580 
5581     case SUBMODULE_UMBRELLA_HEADER: {
5582       std::string Filename = std::string(Blob);
5583       ResolveImportedPath(F, Filename);
5584       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5585         if (!CurrentModule->getUmbrellaHeader())
5586           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5587         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5588           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5589             Error("mismatched umbrella headers in submodule");
5590           return OutOfDate;
5591         }
5592       }
5593       break;
5594     }
5595 
5596     case SUBMODULE_HEADER:
5597     case SUBMODULE_EXCLUDED_HEADER:
5598     case SUBMODULE_PRIVATE_HEADER:
5599       // We lazily associate headers with their modules via the HeaderInfo table.
5600       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5601       // of complete filenames or remove it entirely.
5602       break;
5603 
5604     case SUBMODULE_TEXTUAL_HEADER:
5605     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5606       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5607       // them here.
5608       break;
5609 
5610     case SUBMODULE_TOPHEADER:
5611       CurrentModule->addTopHeaderFilename(Blob);
5612       break;
5613 
5614     case SUBMODULE_UMBRELLA_DIR: {
5615       std::string Dirname = std::string(Blob);
5616       ResolveImportedPath(F, Dirname);
5617       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5618         if (!CurrentModule->getUmbrellaDir())
5619           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5620         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5621           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5622             Error("mismatched umbrella directories in submodule");
5623           return OutOfDate;
5624         }
5625       }
5626       break;
5627     }
5628 
5629     case SUBMODULE_METADATA: {
5630       F.BaseSubmoduleID = getTotalNumSubmodules();
5631       F.LocalNumSubmodules = Record[0];
5632       unsigned LocalBaseSubmoduleID = Record[1];
5633       if (F.LocalNumSubmodules > 0) {
5634         // Introduce the global -> local mapping for submodules within this
5635         // module.
5636         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5637 
5638         // Introduce the local -> global mapping for submodules within this
5639         // module.
5640         F.SubmoduleRemap.insertOrReplace(
5641           std::make_pair(LocalBaseSubmoduleID,
5642                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5643 
5644         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5645       }
5646       break;
5647     }
5648 
5649     case SUBMODULE_IMPORTS:
5650       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5651         UnresolvedModuleRef Unresolved;
5652         Unresolved.File = &F;
5653         Unresolved.Mod = CurrentModule;
5654         Unresolved.ID = Record[Idx];
5655         Unresolved.Kind = UnresolvedModuleRef::Import;
5656         Unresolved.IsWildcard = false;
5657         UnresolvedModuleRefs.push_back(Unresolved);
5658       }
5659       break;
5660 
5661     case SUBMODULE_EXPORTS:
5662       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5663         UnresolvedModuleRef Unresolved;
5664         Unresolved.File = &F;
5665         Unresolved.Mod = CurrentModule;
5666         Unresolved.ID = Record[Idx];
5667         Unresolved.Kind = UnresolvedModuleRef::Export;
5668         Unresolved.IsWildcard = Record[Idx + 1];
5669         UnresolvedModuleRefs.push_back(Unresolved);
5670       }
5671 
5672       // Once we've loaded the set of exports, there's no reason to keep
5673       // the parsed, unresolved exports around.
5674       CurrentModule->UnresolvedExports.clear();
5675       break;
5676 
5677     case SUBMODULE_REQUIRES:
5678       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5679                                     PP.getTargetInfo());
5680       break;
5681 
5682     case SUBMODULE_LINK_LIBRARY:
5683       ModMap.resolveLinkAsDependencies(CurrentModule);
5684       CurrentModule->LinkLibraries.push_back(
5685           Module::LinkLibrary(std::string(Blob), Record[0]));
5686       break;
5687 
5688     case SUBMODULE_CONFIG_MACRO:
5689       CurrentModule->ConfigMacros.push_back(Blob.str());
5690       break;
5691 
5692     case SUBMODULE_CONFLICT: {
5693       UnresolvedModuleRef Unresolved;
5694       Unresolved.File = &F;
5695       Unresolved.Mod = CurrentModule;
5696       Unresolved.ID = Record[0];
5697       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5698       Unresolved.IsWildcard = false;
5699       Unresolved.String = Blob;
5700       UnresolvedModuleRefs.push_back(Unresolved);
5701       break;
5702     }
5703 
5704     case SUBMODULE_INITIALIZERS: {
5705       if (!ContextObj)
5706         break;
5707       SmallVector<uint32_t, 16> Inits;
5708       for (auto &ID : Record)
5709         Inits.push_back(getGlobalDeclID(F, ID));
5710       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5711       break;
5712     }
5713 
5714     case SUBMODULE_EXPORT_AS:
5715       CurrentModule->ExportAsModule = Blob.str();
5716       ModMap.addLinkAsDependency(CurrentModule);
5717       break;
5718     }
5719   }
5720 }
5721 
5722 /// Parse the record that corresponds to a LangOptions data
5723 /// structure.
5724 ///
5725 /// This routine parses the language options from the AST file and then gives
5726 /// them to the AST listener if one is set.
5727 ///
5728 /// \returns true if the listener deems the file unacceptable, false otherwise.
5729 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5730                                      bool Complain,
5731                                      ASTReaderListener &Listener,
5732                                      bool AllowCompatibleDifferences) {
5733   LangOptions LangOpts;
5734   unsigned Idx = 0;
5735 #define LANGOPT(Name, Bits, Default, Description) \
5736   LangOpts.Name = Record[Idx++];
5737 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5738   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5739 #include "clang/Basic/LangOptions.def"
5740 #define SANITIZER(NAME, ID)                                                    \
5741   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5742 #include "clang/Basic/Sanitizers.def"
5743 
5744   for (unsigned N = Record[Idx++]; N; --N)
5745     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5746 
5747   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5748   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5749   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5750 
5751   LangOpts.CurrentModule = ReadString(Record, Idx);
5752 
5753   // Comment options.
5754   for (unsigned N = Record[Idx++]; N; --N) {
5755     LangOpts.CommentOpts.BlockCommandNames.push_back(
5756       ReadString(Record, Idx));
5757   }
5758   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5759 
5760   // OpenMP offloading options.
5761   for (unsigned N = Record[Idx++]; N; --N) {
5762     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5763   }
5764 
5765   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5766 
5767   return Listener.ReadLanguageOptions(LangOpts, Complain,
5768                                       AllowCompatibleDifferences);
5769 }
5770 
5771 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5772                                    ASTReaderListener &Listener,
5773                                    bool AllowCompatibleDifferences) {
5774   unsigned Idx = 0;
5775   TargetOptions TargetOpts;
5776   TargetOpts.Triple = ReadString(Record, Idx);
5777   TargetOpts.CPU = ReadString(Record, Idx);
5778   TargetOpts.TuneCPU = ReadString(Record, Idx);
5779   TargetOpts.ABI = ReadString(Record, Idx);
5780   for (unsigned N = Record[Idx++]; N; --N) {
5781     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5782   }
5783   for (unsigned N = Record[Idx++]; N; --N) {
5784     TargetOpts.Features.push_back(ReadString(Record, Idx));
5785   }
5786 
5787   return Listener.ReadTargetOptions(TargetOpts, Complain,
5788                                     AllowCompatibleDifferences);
5789 }
5790 
5791 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5792                                        ASTReaderListener &Listener) {
5793   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5794   unsigned Idx = 0;
5795 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5796 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5797   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5798 #include "clang/Basic/DiagnosticOptions.def"
5799 
5800   for (unsigned N = Record[Idx++]; N; --N)
5801     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5802   for (unsigned N = Record[Idx++]; N; --N)
5803     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5804 
5805   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5806 }
5807 
5808 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5809                                        ASTReaderListener &Listener) {
5810   FileSystemOptions FSOpts;
5811   unsigned Idx = 0;
5812   FSOpts.WorkingDir = ReadString(Record, Idx);
5813   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5814 }
5815 
5816 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5817                                          bool Complain,
5818                                          ASTReaderListener &Listener) {
5819   HeaderSearchOptions HSOpts;
5820   unsigned Idx = 0;
5821   HSOpts.Sysroot = ReadString(Record, Idx);
5822 
5823   // Include entries.
5824   for (unsigned N = Record[Idx++]; N; --N) {
5825     std::string Path = ReadString(Record, Idx);
5826     frontend::IncludeDirGroup Group
5827       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5828     bool IsFramework = Record[Idx++];
5829     bool IgnoreSysRoot = Record[Idx++];
5830     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5831                                     IgnoreSysRoot);
5832   }
5833 
5834   // System header prefixes.
5835   for (unsigned N = Record[Idx++]; N; --N) {
5836     std::string Prefix = ReadString(Record, Idx);
5837     bool IsSystemHeader = Record[Idx++];
5838     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5839   }
5840 
5841   HSOpts.ResourceDir = ReadString(Record, Idx);
5842   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5843   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5844   HSOpts.DisableModuleHash = Record[Idx++];
5845   HSOpts.ImplicitModuleMaps = Record[Idx++];
5846   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5847   HSOpts.UseBuiltinIncludes = Record[Idx++];
5848   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5849   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5850   HSOpts.UseLibcxx = Record[Idx++];
5851   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5852 
5853   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5854                                           Complain);
5855 }
5856 
5857 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5858                                          bool Complain,
5859                                          ASTReaderListener &Listener,
5860                                          std::string &SuggestedPredefines) {
5861   PreprocessorOptions PPOpts;
5862   unsigned Idx = 0;
5863 
5864   // Macro definitions/undefs
5865   for (unsigned N = Record[Idx++]; N; --N) {
5866     std::string Macro = ReadString(Record, Idx);
5867     bool IsUndef = Record[Idx++];
5868     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5869   }
5870 
5871   // Includes
5872   for (unsigned N = Record[Idx++]; N; --N) {
5873     PPOpts.Includes.push_back(ReadString(Record, Idx));
5874   }
5875 
5876   // Macro Includes
5877   for (unsigned N = Record[Idx++]; N; --N) {
5878     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5879   }
5880 
5881   PPOpts.UsePredefines = Record[Idx++];
5882   PPOpts.DetailedRecord = Record[Idx++];
5883   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5884   PPOpts.ObjCXXARCStandardLibrary =
5885     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5886   SuggestedPredefines.clear();
5887   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5888                                           SuggestedPredefines);
5889 }
5890 
5891 std::pair<ModuleFile *, unsigned>
5892 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5893   GlobalPreprocessedEntityMapType::iterator
5894   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5895   assert(I != GlobalPreprocessedEntityMap.end() &&
5896          "Corrupted global preprocessed entity map");
5897   ModuleFile *M = I->second;
5898   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5899   return std::make_pair(M, LocalIndex);
5900 }
5901 
5902 llvm::iterator_range<PreprocessingRecord::iterator>
5903 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5904   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5905     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5906                                              Mod.NumPreprocessedEntities);
5907 
5908   return llvm::make_range(PreprocessingRecord::iterator(),
5909                           PreprocessingRecord::iterator());
5910 }
5911 
5912 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5913 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5914   return llvm::make_range(
5915       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5916       ModuleDeclIterator(this, &Mod,
5917                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5918 }
5919 
5920 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5921   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5922   assert(I != GlobalSkippedRangeMap.end() &&
5923     "Corrupted global skipped range map");
5924   ModuleFile *M = I->second;
5925   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5926   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5927   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5928   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5929                     TranslateSourceLocation(*M, RawRange.getEnd()));
5930   assert(Range.isValid());
5931   return Range;
5932 }
5933 
5934 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5935   PreprocessedEntityID PPID = Index+1;
5936   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5937   ModuleFile &M = *PPInfo.first;
5938   unsigned LocalIndex = PPInfo.second;
5939   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5940 
5941   if (!PP.getPreprocessingRecord()) {
5942     Error("no preprocessing record");
5943     return nullptr;
5944   }
5945 
5946   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5947   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5948           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5949     Error(std::move(Err));
5950     return nullptr;
5951   }
5952 
5953   Expected<llvm::BitstreamEntry> MaybeEntry =
5954       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5955   if (!MaybeEntry) {
5956     Error(MaybeEntry.takeError());
5957     return nullptr;
5958   }
5959   llvm::BitstreamEntry Entry = MaybeEntry.get();
5960 
5961   if (Entry.Kind != llvm::BitstreamEntry::Record)
5962     return nullptr;
5963 
5964   // Read the record.
5965   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5966                     TranslateSourceLocation(M, PPOffs.getEnd()));
5967   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5968   StringRef Blob;
5969   RecordData Record;
5970   Expected<unsigned> MaybeRecType =
5971       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5972   if (!MaybeRecType) {
5973     Error(MaybeRecType.takeError());
5974     return nullptr;
5975   }
5976   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5977   case PPD_MACRO_EXPANSION: {
5978     bool isBuiltin = Record[0];
5979     IdentifierInfo *Name = nullptr;
5980     MacroDefinitionRecord *Def = nullptr;
5981     if (isBuiltin)
5982       Name = getLocalIdentifier(M, Record[1]);
5983     else {
5984       PreprocessedEntityID GlobalID =
5985           getGlobalPreprocessedEntityID(M, Record[1]);
5986       Def = cast<MacroDefinitionRecord>(
5987           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5988     }
5989 
5990     MacroExpansion *ME;
5991     if (isBuiltin)
5992       ME = new (PPRec) MacroExpansion(Name, Range);
5993     else
5994       ME = new (PPRec) MacroExpansion(Def, Range);
5995 
5996     return ME;
5997   }
5998 
5999   case PPD_MACRO_DEFINITION: {
6000     // Decode the identifier info and then check again; if the macro is
6001     // still defined and associated with the identifier,
6002     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6003     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6004 
6005     if (DeserializationListener)
6006       DeserializationListener->MacroDefinitionRead(PPID, MD);
6007 
6008     return MD;
6009   }
6010 
6011   case PPD_INCLUSION_DIRECTIVE: {
6012     const char *FullFileNameStart = Blob.data() + Record[0];
6013     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6014     const FileEntry *File = nullptr;
6015     if (!FullFileName.empty())
6016       if (auto FE = PP.getFileManager().getFile(FullFileName))
6017         File = *FE;
6018 
6019     // FIXME: Stable encoding
6020     InclusionDirective::InclusionKind Kind
6021       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6022     InclusionDirective *ID
6023       = new (PPRec) InclusionDirective(PPRec, Kind,
6024                                        StringRef(Blob.data(), Record[0]),
6025                                        Record[1], Record[3],
6026                                        File,
6027                                        Range);
6028     return ID;
6029   }
6030   }
6031 
6032   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6033 }
6034 
6035 /// Find the next module that contains entities and return the ID
6036 /// of the first entry.
6037 ///
6038 /// \param SLocMapI points at a chunk of a module that contains no
6039 /// preprocessed entities or the entities it contains are not the ones we are
6040 /// looking for.
6041 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6042                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6043   ++SLocMapI;
6044   for (GlobalSLocOffsetMapType::const_iterator
6045          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6046     ModuleFile &M = *SLocMapI->second;
6047     if (M.NumPreprocessedEntities)
6048       return M.BasePreprocessedEntityID;
6049   }
6050 
6051   return getTotalNumPreprocessedEntities();
6052 }
6053 
6054 namespace {
6055 
6056 struct PPEntityComp {
6057   const ASTReader &Reader;
6058   ModuleFile &M;
6059 
6060   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6061 
6062   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6063     SourceLocation LHS = getLoc(L);
6064     SourceLocation RHS = getLoc(R);
6065     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6066   }
6067 
6068   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6069     SourceLocation LHS = getLoc(L);
6070     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6071   }
6072 
6073   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6074     SourceLocation RHS = getLoc(R);
6075     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6076   }
6077 
6078   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6079     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6080   }
6081 };
6082 
6083 } // namespace
6084 
6085 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6086                                                        bool EndsAfter) const {
6087   if (SourceMgr.isLocalSourceLocation(Loc))
6088     return getTotalNumPreprocessedEntities();
6089 
6090   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6091       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6092   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6093          "Corrupted global sloc offset map");
6094 
6095   if (SLocMapI->second->NumPreprocessedEntities == 0)
6096     return findNextPreprocessedEntity(SLocMapI);
6097 
6098   ModuleFile &M = *SLocMapI->second;
6099 
6100   using pp_iterator = const PPEntityOffset *;
6101 
6102   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6103   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6104 
6105   size_t Count = M.NumPreprocessedEntities;
6106   size_t Half;
6107   pp_iterator First = pp_begin;
6108   pp_iterator PPI;
6109 
6110   if (EndsAfter) {
6111     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6112                            PPEntityComp(*this, M));
6113   } else {
6114     // Do a binary search manually instead of using std::lower_bound because
6115     // The end locations of entities may be unordered (when a macro expansion
6116     // is inside another macro argument), but for this case it is not important
6117     // whether we get the first macro expansion or its containing macro.
6118     while (Count > 0) {
6119       Half = Count / 2;
6120       PPI = First;
6121       std::advance(PPI, Half);
6122       if (SourceMgr.isBeforeInTranslationUnit(
6123               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6124         First = PPI;
6125         ++First;
6126         Count = Count - Half - 1;
6127       } else
6128         Count = Half;
6129     }
6130   }
6131 
6132   if (PPI == pp_end)
6133     return findNextPreprocessedEntity(SLocMapI);
6134 
6135   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6136 }
6137 
6138 /// Returns a pair of [Begin, End) indices of preallocated
6139 /// preprocessed entities that \arg Range encompasses.
6140 std::pair<unsigned, unsigned>
6141     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6142   if (Range.isInvalid())
6143     return std::make_pair(0,0);
6144   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6145 
6146   PreprocessedEntityID BeginID =
6147       findPreprocessedEntity(Range.getBegin(), false);
6148   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6149   return std::make_pair(BeginID, EndID);
6150 }
6151 
6152 /// Optionally returns true or false if the preallocated preprocessed
6153 /// entity with index \arg Index came from file \arg FID.
6154 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6155                                                              FileID FID) {
6156   if (FID.isInvalid())
6157     return false;
6158 
6159   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6160   ModuleFile &M = *PPInfo.first;
6161   unsigned LocalIndex = PPInfo.second;
6162   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6163 
6164   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6165   if (Loc.isInvalid())
6166     return false;
6167 
6168   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6169     return true;
6170   else
6171     return false;
6172 }
6173 
6174 namespace {
6175 
6176   /// Visitor used to search for information about a header file.
6177   class HeaderFileInfoVisitor {
6178     const FileEntry *FE;
6179     Optional<HeaderFileInfo> HFI;
6180 
6181   public:
6182     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6183 
6184     bool operator()(ModuleFile &M) {
6185       HeaderFileInfoLookupTable *Table
6186         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6187       if (!Table)
6188         return false;
6189 
6190       // Look in the on-disk hash table for an entry for this file name.
6191       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6192       if (Pos == Table->end())
6193         return false;
6194 
6195       HFI = *Pos;
6196       return true;
6197     }
6198 
6199     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6200   };
6201 
6202 } // namespace
6203 
6204 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6205   HeaderFileInfoVisitor Visitor(FE);
6206   ModuleMgr.visit(Visitor);
6207   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6208     return *HFI;
6209 
6210   return HeaderFileInfo();
6211 }
6212 
6213 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6214   using DiagState = DiagnosticsEngine::DiagState;
6215   SmallVector<DiagState *, 32> DiagStates;
6216 
6217   for (ModuleFile &F : ModuleMgr) {
6218     unsigned Idx = 0;
6219     auto &Record = F.PragmaDiagMappings;
6220     if (Record.empty())
6221       continue;
6222 
6223     DiagStates.clear();
6224 
6225     auto ReadDiagState =
6226         [&](const DiagState &BasedOn, SourceLocation Loc,
6227             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6228       unsigned BackrefID = Record[Idx++];
6229       if (BackrefID != 0)
6230         return DiagStates[BackrefID - 1];
6231 
6232       // A new DiagState was created here.
6233       Diag.DiagStates.push_back(BasedOn);
6234       DiagState *NewState = &Diag.DiagStates.back();
6235       DiagStates.push_back(NewState);
6236       unsigned Size = Record[Idx++];
6237       assert(Idx + Size * 2 <= Record.size() &&
6238              "Invalid data, not enough diag/map pairs");
6239       while (Size--) {
6240         unsigned DiagID = Record[Idx++];
6241         DiagnosticMapping NewMapping =
6242             DiagnosticMapping::deserialize(Record[Idx++]);
6243         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6244           continue;
6245 
6246         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6247 
6248         // If this mapping was specified as a warning but the severity was
6249         // upgraded due to diagnostic settings, simulate the current diagnostic
6250         // settings (and use a warning).
6251         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6252           NewMapping.setSeverity(diag::Severity::Warning);
6253           NewMapping.setUpgradedFromWarning(false);
6254         }
6255 
6256         Mapping = NewMapping;
6257       }
6258       return NewState;
6259     };
6260 
6261     // Read the first state.
6262     DiagState *FirstState;
6263     if (F.Kind == MK_ImplicitModule) {
6264       // Implicitly-built modules are reused with different diagnostic
6265       // settings.  Use the initial diagnostic state from Diag to simulate this
6266       // compilation's diagnostic settings.
6267       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6268       DiagStates.push_back(FirstState);
6269 
6270       // Skip the initial diagnostic state from the serialized module.
6271       assert(Record[1] == 0 &&
6272              "Invalid data, unexpected backref in initial state");
6273       Idx = 3 + Record[2] * 2;
6274       assert(Idx < Record.size() &&
6275              "Invalid data, not enough state change pairs in initial state");
6276     } else if (F.isModule()) {
6277       // For an explicit module, preserve the flags from the module build
6278       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6279       // -Wblah flags.
6280       unsigned Flags = Record[Idx++];
6281       DiagState Initial;
6282       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6283       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6284       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6285       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6286       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6287       Initial.ExtBehavior = (diag::Severity)Flags;
6288       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6289 
6290       assert(F.OriginalSourceFileID.isValid());
6291 
6292       // Set up the root buffer of the module to start with the initial
6293       // diagnostic state of the module itself, to cover files that contain no
6294       // explicit transitions (for which we did not serialize anything).
6295       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6296           .StateTransitions.push_back({FirstState, 0});
6297     } else {
6298       // For prefix ASTs, start with whatever the user configured on the
6299       // command line.
6300       Idx++; // Skip flags.
6301       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6302                                  SourceLocation(), false);
6303     }
6304 
6305     // Read the state transitions.
6306     unsigned NumLocations = Record[Idx++];
6307     while (NumLocations--) {
6308       assert(Idx < Record.size() &&
6309              "Invalid data, missing pragma diagnostic states");
6310       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6311       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6312       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6313       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6314       unsigned Transitions = Record[Idx++];
6315 
6316       // Note that we don't need to set up Parent/ParentOffset here, because
6317       // we won't be changing the diagnostic state within imported FileIDs
6318       // (other than perhaps appending to the main source file, which has no
6319       // parent).
6320       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6321       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6322       for (unsigned I = 0; I != Transitions; ++I) {
6323         unsigned Offset = Record[Idx++];
6324         auto *State =
6325             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6326         F.StateTransitions.push_back({State, Offset});
6327       }
6328     }
6329 
6330     // Read the final state.
6331     assert(Idx < Record.size() &&
6332            "Invalid data, missing final pragma diagnostic state");
6333     SourceLocation CurStateLoc =
6334         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6335     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6336 
6337     if (!F.isModule()) {
6338       Diag.DiagStatesByLoc.CurDiagState = CurState;
6339       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6340 
6341       // Preserve the property that the imaginary root file describes the
6342       // current state.
6343       FileID NullFile;
6344       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6345       if (T.empty())
6346         T.push_back({CurState, 0});
6347       else
6348         T[0].State = CurState;
6349     }
6350 
6351     // Don't try to read these mappings again.
6352     Record.clear();
6353   }
6354 }
6355 
6356 /// Get the correct cursor and offset for loading a type.
6357 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6358   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6359   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6360   ModuleFile *M = I->second;
6361   return RecordLocation(
6362       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6363              M->DeclsBlockStartOffset);
6364 }
6365 
6366 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6367   switch (code) {
6368 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6369   case TYPE_##CODE_ID: return Type::CLASS_ID;
6370 #include "clang/Serialization/TypeBitCodes.def"
6371   default: return llvm::None;
6372   }
6373 }
6374 
6375 /// Read and return the type with the given index..
6376 ///
6377 /// The index is the type ID, shifted and minus the number of predefs. This
6378 /// routine actually reads the record corresponding to the type at the given
6379 /// location. It is a helper routine for GetType, which deals with reading type
6380 /// IDs.
6381 QualType ASTReader::readTypeRecord(unsigned Index) {
6382   assert(ContextObj && "reading type with no AST context");
6383   ASTContext &Context = *ContextObj;
6384   RecordLocation Loc = TypeCursorForIndex(Index);
6385   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6386 
6387   // Keep track of where we are in the stream, then jump back there
6388   // after reading this type.
6389   SavedStreamPosition SavedPosition(DeclsCursor);
6390 
6391   ReadingKindTracker ReadingKind(Read_Type, *this);
6392 
6393   // Note that we are loading a type record.
6394   Deserializing AType(this);
6395 
6396   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6397     Error(std::move(Err));
6398     return QualType();
6399   }
6400   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6401   if (!RawCode) {
6402     Error(RawCode.takeError());
6403     return QualType();
6404   }
6405 
6406   ASTRecordReader Record(*this, *Loc.F);
6407   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6408   if (!Code) {
6409     Error(Code.takeError());
6410     return QualType();
6411   }
6412   if (Code.get() == TYPE_EXT_QUAL) {
6413     QualType baseType = Record.readQualType();
6414     Qualifiers quals = Record.readQualifiers();
6415     return Context.getQualifiedType(baseType, quals);
6416   }
6417 
6418   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6419   if (!maybeClass) {
6420     Error("Unexpected code for type");
6421     return QualType();
6422   }
6423 
6424   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6425   return TypeReader.read(*maybeClass);
6426 }
6427 
6428 namespace clang {
6429 
6430 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6431   ASTRecordReader &Reader;
6432 
6433   SourceLocation readSourceLocation() {
6434     return Reader.readSourceLocation();
6435   }
6436 
6437   TypeSourceInfo *GetTypeSourceInfo() {
6438     return Reader.readTypeSourceInfo();
6439   }
6440 
6441   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6442     return Reader.readNestedNameSpecifierLoc();
6443   }
6444 
6445   Attr *ReadAttr() {
6446     return Reader.readAttr();
6447   }
6448 
6449 public:
6450   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6451 
6452   // We want compile-time assurance that we've enumerated all of
6453   // these, so unfortunately we have to declare them first, then
6454   // define them out-of-line.
6455 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6456 #define TYPELOC(CLASS, PARENT) \
6457   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6458 #include "clang/AST/TypeLocNodes.def"
6459 
6460   void VisitFunctionTypeLoc(FunctionTypeLoc);
6461   void VisitArrayTypeLoc(ArrayTypeLoc);
6462 };
6463 
6464 } // namespace clang
6465 
6466 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6467   // nothing to do
6468 }
6469 
6470 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6471   TL.setBuiltinLoc(readSourceLocation());
6472   if (TL.needsExtraLocalData()) {
6473     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6474     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6475     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6476     TL.setModeAttr(Reader.readInt());
6477   }
6478 }
6479 
6480 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6481   TL.setNameLoc(readSourceLocation());
6482 }
6483 
6484 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6485   TL.setStarLoc(readSourceLocation());
6486 }
6487 
6488 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6489   // nothing to do
6490 }
6491 
6492 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6493   // nothing to do
6494 }
6495 
6496 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6497   TL.setExpansionLoc(readSourceLocation());
6498 }
6499 
6500 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6501   TL.setCaretLoc(readSourceLocation());
6502 }
6503 
6504 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6505   TL.setAmpLoc(readSourceLocation());
6506 }
6507 
6508 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6509   TL.setAmpAmpLoc(readSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6513   TL.setStarLoc(readSourceLocation());
6514   TL.setClassTInfo(GetTypeSourceInfo());
6515 }
6516 
6517 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6518   TL.setLBracketLoc(readSourceLocation());
6519   TL.setRBracketLoc(readSourceLocation());
6520   if (Reader.readBool())
6521     TL.setSizeExpr(Reader.readExpr());
6522   else
6523     TL.setSizeExpr(nullptr);
6524 }
6525 
6526 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6527   VisitArrayTypeLoc(TL);
6528 }
6529 
6530 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6531   VisitArrayTypeLoc(TL);
6532 }
6533 
6534 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6535   VisitArrayTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6539                                             DependentSizedArrayTypeLoc TL) {
6540   VisitArrayTypeLoc(TL);
6541 }
6542 
6543 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6544     DependentAddressSpaceTypeLoc TL) {
6545 
6546     TL.setAttrNameLoc(readSourceLocation());
6547     TL.setAttrOperandParensRange(Reader.readSourceRange());
6548     TL.setAttrExprOperand(Reader.readExpr());
6549 }
6550 
6551 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6552                                         DependentSizedExtVectorTypeLoc TL) {
6553   TL.setNameLoc(readSourceLocation());
6554 }
6555 
6556 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6557   TL.setNameLoc(readSourceLocation());
6558 }
6559 
6560 void TypeLocReader::VisitDependentVectorTypeLoc(
6561     DependentVectorTypeLoc TL) {
6562   TL.setNameLoc(readSourceLocation());
6563 }
6564 
6565 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6566   TL.setNameLoc(readSourceLocation());
6567 }
6568 
6569 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6570   TL.setAttrNameLoc(readSourceLocation());
6571   TL.setAttrOperandParensRange(Reader.readSourceRange());
6572   TL.setAttrRowOperand(Reader.readExpr());
6573   TL.setAttrColumnOperand(Reader.readExpr());
6574 }
6575 
6576 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6577     DependentSizedMatrixTypeLoc TL) {
6578   TL.setAttrNameLoc(readSourceLocation());
6579   TL.setAttrOperandParensRange(Reader.readSourceRange());
6580   TL.setAttrRowOperand(Reader.readExpr());
6581   TL.setAttrColumnOperand(Reader.readExpr());
6582 }
6583 
6584 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6585   TL.setLocalRangeBegin(readSourceLocation());
6586   TL.setLParenLoc(readSourceLocation());
6587   TL.setRParenLoc(readSourceLocation());
6588   TL.setExceptionSpecRange(Reader.readSourceRange());
6589   TL.setLocalRangeEnd(readSourceLocation());
6590   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6591     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6592   }
6593 }
6594 
6595 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6596   VisitFunctionTypeLoc(TL);
6597 }
6598 
6599 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6600   VisitFunctionTypeLoc(TL);
6601 }
6602 
6603 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6604   TL.setNameLoc(readSourceLocation());
6605 }
6606 
6607 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6608   TL.setNameLoc(readSourceLocation());
6609 }
6610 
6611 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6612   TL.setTypeofLoc(readSourceLocation());
6613   TL.setLParenLoc(readSourceLocation());
6614   TL.setRParenLoc(readSourceLocation());
6615 }
6616 
6617 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6618   TL.setTypeofLoc(readSourceLocation());
6619   TL.setLParenLoc(readSourceLocation());
6620   TL.setRParenLoc(readSourceLocation());
6621   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6622 }
6623 
6624 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6625   TL.setNameLoc(readSourceLocation());
6626 }
6627 
6628 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6629   TL.setKWLoc(readSourceLocation());
6630   TL.setLParenLoc(readSourceLocation());
6631   TL.setRParenLoc(readSourceLocation());
6632   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6633 }
6634 
6635 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6636   TL.setNameLoc(readSourceLocation());
6637   if (Reader.readBool()) {
6638     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6639     TL.setTemplateKWLoc(readSourceLocation());
6640     TL.setConceptNameLoc(readSourceLocation());
6641     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6642     TL.setLAngleLoc(readSourceLocation());
6643     TL.setRAngleLoc(readSourceLocation());
6644     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6645       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6646                               TL.getTypePtr()->getArg(i).getKind()));
6647   }
6648 }
6649 
6650 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6651     DeducedTemplateSpecializationTypeLoc TL) {
6652   TL.setTemplateNameLoc(readSourceLocation());
6653 }
6654 
6655 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6656   TL.setNameLoc(readSourceLocation());
6657 }
6658 
6659 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6660   TL.setNameLoc(readSourceLocation());
6661 }
6662 
6663 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6664   TL.setAttr(ReadAttr());
6665 }
6666 
6667 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6668   TL.setNameLoc(readSourceLocation());
6669 }
6670 
6671 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6672                                             SubstTemplateTypeParmTypeLoc TL) {
6673   TL.setNameLoc(readSourceLocation());
6674 }
6675 
6676 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6677                                           SubstTemplateTypeParmPackTypeLoc TL) {
6678   TL.setNameLoc(readSourceLocation());
6679 }
6680 
6681 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6682                                            TemplateSpecializationTypeLoc TL) {
6683   TL.setTemplateKeywordLoc(readSourceLocation());
6684   TL.setTemplateNameLoc(readSourceLocation());
6685   TL.setLAngleLoc(readSourceLocation());
6686   TL.setRAngleLoc(readSourceLocation());
6687   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6688     TL.setArgLocInfo(
6689         i,
6690         Reader.readTemplateArgumentLocInfo(
6691           TL.getTypePtr()->getArg(i).getKind()));
6692 }
6693 
6694 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6695   TL.setLParenLoc(readSourceLocation());
6696   TL.setRParenLoc(readSourceLocation());
6697 }
6698 
6699 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6700   TL.setElaboratedKeywordLoc(readSourceLocation());
6701   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6702 }
6703 
6704 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6705   TL.setNameLoc(readSourceLocation());
6706 }
6707 
6708 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6709   TL.setElaboratedKeywordLoc(readSourceLocation());
6710   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6711   TL.setNameLoc(readSourceLocation());
6712 }
6713 
6714 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6715        DependentTemplateSpecializationTypeLoc TL) {
6716   TL.setElaboratedKeywordLoc(readSourceLocation());
6717   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6718   TL.setTemplateKeywordLoc(readSourceLocation());
6719   TL.setTemplateNameLoc(readSourceLocation());
6720   TL.setLAngleLoc(readSourceLocation());
6721   TL.setRAngleLoc(readSourceLocation());
6722   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6723     TL.setArgLocInfo(
6724         I,
6725         Reader.readTemplateArgumentLocInfo(
6726             TL.getTypePtr()->getArg(I).getKind()));
6727 }
6728 
6729 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6730   TL.setEllipsisLoc(readSourceLocation());
6731 }
6732 
6733 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6734   TL.setNameLoc(readSourceLocation());
6735 }
6736 
6737 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6738   if (TL.getNumProtocols()) {
6739     TL.setProtocolLAngleLoc(readSourceLocation());
6740     TL.setProtocolRAngleLoc(readSourceLocation());
6741   }
6742   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6743     TL.setProtocolLoc(i, readSourceLocation());
6744 }
6745 
6746 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6747   TL.setHasBaseTypeAsWritten(Reader.readBool());
6748   TL.setTypeArgsLAngleLoc(readSourceLocation());
6749   TL.setTypeArgsRAngleLoc(readSourceLocation());
6750   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6751     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6752   TL.setProtocolLAngleLoc(readSourceLocation());
6753   TL.setProtocolRAngleLoc(readSourceLocation());
6754   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6755     TL.setProtocolLoc(i, readSourceLocation());
6756 }
6757 
6758 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6759   TL.setStarLoc(readSourceLocation());
6760 }
6761 
6762 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6763   TL.setKWLoc(readSourceLocation());
6764   TL.setLParenLoc(readSourceLocation());
6765   TL.setRParenLoc(readSourceLocation());
6766 }
6767 
6768 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6769   TL.setKWLoc(readSourceLocation());
6770 }
6771 
6772 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6773   TL.setNameLoc(readSourceLocation());
6774 }
6775 void TypeLocReader::VisitDependentExtIntTypeLoc(
6776     clang::DependentExtIntTypeLoc TL) {
6777   TL.setNameLoc(readSourceLocation());
6778 }
6779 
6780 
6781 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6782   TypeLocReader TLR(*this);
6783   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6784     TLR.Visit(TL);
6785 }
6786 
6787 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6788   QualType InfoTy = readType();
6789   if (InfoTy.isNull())
6790     return nullptr;
6791 
6792   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6793   readTypeLoc(TInfo->getTypeLoc());
6794   return TInfo;
6795 }
6796 
6797 QualType ASTReader::GetType(TypeID ID) {
6798   assert(ContextObj && "reading type with no AST context");
6799   ASTContext &Context = *ContextObj;
6800 
6801   unsigned FastQuals = ID & Qualifiers::FastMask;
6802   unsigned Index = ID >> Qualifiers::FastWidth;
6803 
6804   if (Index < NUM_PREDEF_TYPE_IDS) {
6805     QualType T;
6806     switch ((PredefinedTypeIDs)Index) {
6807     case PREDEF_TYPE_NULL_ID:
6808       return QualType();
6809     case PREDEF_TYPE_VOID_ID:
6810       T = Context.VoidTy;
6811       break;
6812     case PREDEF_TYPE_BOOL_ID:
6813       T = Context.BoolTy;
6814       break;
6815     case PREDEF_TYPE_CHAR_U_ID:
6816     case PREDEF_TYPE_CHAR_S_ID:
6817       // FIXME: Check that the signedness of CharTy is correct!
6818       T = Context.CharTy;
6819       break;
6820     case PREDEF_TYPE_UCHAR_ID:
6821       T = Context.UnsignedCharTy;
6822       break;
6823     case PREDEF_TYPE_USHORT_ID:
6824       T = Context.UnsignedShortTy;
6825       break;
6826     case PREDEF_TYPE_UINT_ID:
6827       T = Context.UnsignedIntTy;
6828       break;
6829     case PREDEF_TYPE_ULONG_ID:
6830       T = Context.UnsignedLongTy;
6831       break;
6832     case PREDEF_TYPE_ULONGLONG_ID:
6833       T = Context.UnsignedLongLongTy;
6834       break;
6835     case PREDEF_TYPE_UINT128_ID:
6836       T = Context.UnsignedInt128Ty;
6837       break;
6838     case PREDEF_TYPE_SCHAR_ID:
6839       T = Context.SignedCharTy;
6840       break;
6841     case PREDEF_TYPE_WCHAR_ID:
6842       T = Context.WCharTy;
6843       break;
6844     case PREDEF_TYPE_SHORT_ID:
6845       T = Context.ShortTy;
6846       break;
6847     case PREDEF_TYPE_INT_ID:
6848       T = Context.IntTy;
6849       break;
6850     case PREDEF_TYPE_LONG_ID:
6851       T = Context.LongTy;
6852       break;
6853     case PREDEF_TYPE_LONGLONG_ID:
6854       T = Context.LongLongTy;
6855       break;
6856     case PREDEF_TYPE_INT128_ID:
6857       T = Context.Int128Ty;
6858       break;
6859     case PREDEF_TYPE_BFLOAT16_ID:
6860       T = Context.BFloat16Ty;
6861       break;
6862     case PREDEF_TYPE_HALF_ID:
6863       T = Context.HalfTy;
6864       break;
6865     case PREDEF_TYPE_FLOAT_ID:
6866       T = Context.FloatTy;
6867       break;
6868     case PREDEF_TYPE_DOUBLE_ID:
6869       T = Context.DoubleTy;
6870       break;
6871     case PREDEF_TYPE_LONGDOUBLE_ID:
6872       T = Context.LongDoubleTy;
6873       break;
6874     case PREDEF_TYPE_SHORT_ACCUM_ID:
6875       T = Context.ShortAccumTy;
6876       break;
6877     case PREDEF_TYPE_ACCUM_ID:
6878       T = Context.AccumTy;
6879       break;
6880     case PREDEF_TYPE_LONG_ACCUM_ID:
6881       T = Context.LongAccumTy;
6882       break;
6883     case PREDEF_TYPE_USHORT_ACCUM_ID:
6884       T = Context.UnsignedShortAccumTy;
6885       break;
6886     case PREDEF_TYPE_UACCUM_ID:
6887       T = Context.UnsignedAccumTy;
6888       break;
6889     case PREDEF_TYPE_ULONG_ACCUM_ID:
6890       T = Context.UnsignedLongAccumTy;
6891       break;
6892     case PREDEF_TYPE_SHORT_FRACT_ID:
6893       T = Context.ShortFractTy;
6894       break;
6895     case PREDEF_TYPE_FRACT_ID:
6896       T = Context.FractTy;
6897       break;
6898     case PREDEF_TYPE_LONG_FRACT_ID:
6899       T = Context.LongFractTy;
6900       break;
6901     case PREDEF_TYPE_USHORT_FRACT_ID:
6902       T = Context.UnsignedShortFractTy;
6903       break;
6904     case PREDEF_TYPE_UFRACT_ID:
6905       T = Context.UnsignedFractTy;
6906       break;
6907     case PREDEF_TYPE_ULONG_FRACT_ID:
6908       T = Context.UnsignedLongFractTy;
6909       break;
6910     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6911       T = Context.SatShortAccumTy;
6912       break;
6913     case PREDEF_TYPE_SAT_ACCUM_ID:
6914       T = Context.SatAccumTy;
6915       break;
6916     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6917       T = Context.SatLongAccumTy;
6918       break;
6919     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6920       T = Context.SatUnsignedShortAccumTy;
6921       break;
6922     case PREDEF_TYPE_SAT_UACCUM_ID:
6923       T = Context.SatUnsignedAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6926       T = Context.SatUnsignedLongAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6929       T = Context.SatShortFractTy;
6930       break;
6931     case PREDEF_TYPE_SAT_FRACT_ID:
6932       T = Context.SatFractTy;
6933       break;
6934     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6935       T = Context.SatLongFractTy;
6936       break;
6937     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6938       T = Context.SatUnsignedShortFractTy;
6939       break;
6940     case PREDEF_TYPE_SAT_UFRACT_ID:
6941       T = Context.SatUnsignedFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6944       T = Context.SatUnsignedLongFractTy;
6945       break;
6946     case PREDEF_TYPE_FLOAT16_ID:
6947       T = Context.Float16Ty;
6948       break;
6949     case PREDEF_TYPE_FLOAT128_ID:
6950       T = Context.Float128Ty;
6951       break;
6952     case PREDEF_TYPE_OVERLOAD_ID:
6953       T = Context.OverloadTy;
6954       break;
6955     case PREDEF_TYPE_BOUND_MEMBER:
6956       T = Context.BoundMemberTy;
6957       break;
6958     case PREDEF_TYPE_PSEUDO_OBJECT:
6959       T = Context.PseudoObjectTy;
6960       break;
6961     case PREDEF_TYPE_DEPENDENT_ID:
6962       T = Context.DependentTy;
6963       break;
6964     case PREDEF_TYPE_UNKNOWN_ANY:
6965       T = Context.UnknownAnyTy;
6966       break;
6967     case PREDEF_TYPE_NULLPTR_ID:
6968       T = Context.NullPtrTy;
6969       break;
6970     case PREDEF_TYPE_CHAR8_ID:
6971       T = Context.Char8Ty;
6972       break;
6973     case PREDEF_TYPE_CHAR16_ID:
6974       T = Context.Char16Ty;
6975       break;
6976     case PREDEF_TYPE_CHAR32_ID:
6977       T = Context.Char32Ty;
6978       break;
6979     case PREDEF_TYPE_OBJC_ID:
6980       T = Context.ObjCBuiltinIdTy;
6981       break;
6982     case PREDEF_TYPE_OBJC_CLASS:
6983       T = Context.ObjCBuiltinClassTy;
6984       break;
6985     case PREDEF_TYPE_OBJC_SEL:
6986       T = Context.ObjCBuiltinSelTy;
6987       break;
6988 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6989     case PREDEF_TYPE_##Id##_ID: \
6990       T = Context.SingletonId; \
6991       break;
6992 #include "clang/Basic/OpenCLImageTypes.def"
6993 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6994     case PREDEF_TYPE_##Id##_ID: \
6995       T = Context.Id##Ty; \
6996       break;
6997 #include "clang/Basic/OpenCLExtensionTypes.def"
6998     case PREDEF_TYPE_SAMPLER_ID:
6999       T = Context.OCLSamplerTy;
7000       break;
7001     case PREDEF_TYPE_EVENT_ID:
7002       T = Context.OCLEventTy;
7003       break;
7004     case PREDEF_TYPE_CLK_EVENT_ID:
7005       T = Context.OCLClkEventTy;
7006       break;
7007     case PREDEF_TYPE_QUEUE_ID:
7008       T = Context.OCLQueueTy;
7009       break;
7010     case PREDEF_TYPE_RESERVE_ID_ID:
7011       T = Context.OCLReserveIDTy;
7012       break;
7013     case PREDEF_TYPE_AUTO_DEDUCT:
7014       T = Context.getAutoDeductType();
7015       break;
7016     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7017       T = Context.getAutoRRefDeductType();
7018       break;
7019     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7020       T = Context.ARCUnbridgedCastTy;
7021       break;
7022     case PREDEF_TYPE_BUILTIN_FN:
7023       T = Context.BuiltinFnTy;
7024       break;
7025     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7026       T = Context.IncompleteMatrixIdxTy;
7027       break;
7028     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7029       T = Context.OMPArraySectionTy;
7030       break;
7031     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7032       T = Context.OMPArraySectionTy;
7033       break;
7034     case PREDEF_TYPE_OMP_ITERATOR:
7035       T = Context.OMPIteratorTy;
7036       break;
7037 #define SVE_TYPE(Name, Id, SingletonId) \
7038     case PREDEF_TYPE_##Id##_ID: \
7039       T = Context.SingletonId; \
7040       break;
7041 #include "clang/Basic/AArch64SVEACLETypes.def"
7042     }
7043 
7044     assert(!T.isNull() && "Unknown predefined type");
7045     return T.withFastQualifiers(FastQuals);
7046   }
7047 
7048   Index -= NUM_PREDEF_TYPE_IDS;
7049   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7050   if (TypesLoaded[Index].isNull()) {
7051     TypesLoaded[Index] = readTypeRecord(Index);
7052     if (TypesLoaded[Index].isNull())
7053       return QualType();
7054 
7055     TypesLoaded[Index]->setFromAST();
7056     if (DeserializationListener)
7057       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7058                                         TypesLoaded[Index]);
7059   }
7060 
7061   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7062 }
7063 
7064 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7065   return GetType(getGlobalTypeID(F, LocalID));
7066 }
7067 
7068 serialization::TypeID
7069 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7070   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7071   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7072 
7073   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7074     return LocalID;
7075 
7076   if (!F.ModuleOffsetMap.empty())
7077     ReadModuleOffsetMap(F);
7078 
7079   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7080     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7081   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7082 
7083   unsigned GlobalIndex = LocalIndex + I->second;
7084   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7085 }
7086 
7087 TemplateArgumentLocInfo
7088 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7089   switch (Kind) {
7090   case TemplateArgument::Expression:
7091     return readExpr();
7092   case TemplateArgument::Type:
7093     return readTypeSourceInfo();
7094   case TemplateArgument::Template: {
7095     NestedNameSpecifierLoc QualifierLoc =
7096       readNestedNameSpecifierLoc();
7097     SourceLocation TemplateNameLoc = readSourceLocation();
7098     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7099                                    TemplateNameLoc, SourceLocation());
7100   }
7101   case TemplateArgument::TemplateExpansion: {
7102     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7103     SourceLocation TemplateNameLoc = readSourceLocation();
7104     SourceLocation EllipsisLoc = readSourceLocation();
7105     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7106                                    TemplateNameLoc, EllipsisLoc);
7107   }
7108   case TemplateArgument::Null:
7109   case TemplateArgument::Integral:
7110   case TemplateArgument::Declaration:
7111   case TemplateArgument::NullPtr:
7112   case TemplateArgument::Pack:
7113     // FIXME: Is this right?
7114     return TemplateArgumentLocInfo();
7115   }
7116   llvm_unreachable("unexpected template argument loc");
7117 }
7118 
7119 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7120   TemplateArgument Arg = readTemplateArgument();
7121 
7122   if (Arg.getKind() == TemplateArgument::Expression) {
7123     if (readBool()) // bool InfoHasSameExpr.
7124       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7125   }
7126   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7127 }
7128 
7129 const ASTTemplateArgumentListInfo *
7130 ASTRecordReader::readASTTemplateArgumentListInfo() {
7131   SourceLocation LAngleLoc = readSourceLocation();
7132   SourceLocation RAngleLoc = readSourceLocation();
7133   unsigned NumArgsAsWritten = readInt();
7134   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7135   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7136     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7137   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7138 }
7139 
7140 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7141   return GetDecl(ID);
7142 }
7143 
7144 void ASTReader::CompleteRedeclChain(const Decl *D) {
7145   if (NumCurrentElementsDeserializing) {
7146     // We arrange to not care about the complete redeclaration chain while we're
7147     // deserializing. Just remember that the AST has marked this one as complete
7148     // but that it's not actually complete yet, so we know we still need to
7149     // complete it later.
7150     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7151     return;
7152   }
7153 
7154   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7155 
7156   // If this is a named declaration, complete it by looking it up
7157   // within its context.
7158   //
7159   // FIXME: Merging a function definition should merge
7160   // all mergeable entities within it.
7161   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7162       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7163     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7164       if (!getContext().getLangOpts().CPlusPlus &&
7165           isa<TranslationUnitDecl>(DC)) {
7166         // Outside of C++, we don't have a lookup table for the TU, so update
7167         // the identifier instead. (For C++ modules, we don't store decls
7168         // in the serialized identifier table, so we do the lookup in the TU.)
7169         auto *II = Name.getAsIdentifierInfo();
7170         assert(II && "non-identifier name in C?");
7171         if (II->isOutOfDate())
7172           updateOutOfDateIdentifier(*II);
7173       } else
7174         DC->lookup(Name);
7175     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7176       // Find all declarations of this kind from the relevant context.
7177       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7178         auto *DC = cast<DeclContext>(DCDecl);
7179         SmallVector<Decl*, 8> Decls;
7180         FindExternalLexicalDecls(
7181             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7182       }
7183     }
7184   }
7185 
7186   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7187     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7188   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7189     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7190   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7191     if (auto *Template = FD->getPrimaryTemplate())
7192       Template->LoadLazySpecializations();
7193   }
7194 }
7195 
7196 CXXCtorInitializer **
7197 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7198   RecordLocation Loc = getLocalBitOffset(Offset);
7199   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7200   SavedStreamPosition SavedPosition(Cursor);
7201   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7202     Error(std::move(Err));
7203     return nullptr;
7204   }
7205   ReadingKindTracker ReadingKind(Read_Decl, *this);
7206 
7207   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7208   if (!MaybeCode) {
7209     Error(MaybeCode.takeError());
7210     return nullptr;
7211   }
7212   unsigned Code = MaybeCode.get();
7213 
7214   ASTRecordReader Record(*this, *Loc.F);
7215   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7216   if (!MaybeRecCode) {
7217     Error(MaybeRecCode.takeError());
7218     return nullptr;
7219   }
7220   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7221     Error("malformed AST file: missing C++ ctor initializers");
7222     return nullptr;
7223   }
7224 
7225   return Record.readCXXCtorInitializers();
7226 }
7227 
7228 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7229   assert(ContextObj && "reading base specifiers with no AST context");
7230   ASTContext &Context = *ContextObj;
7231 
7232   RecordLocation Loc = getLocalBitOffset(Offset);
7233   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7234   SavedStreamPosition SavedPosition(Cursor);
7235   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7236     Error(std::move(Err));
7237     return nullptr;
7238   }
7239   ReadingKindTracker ReadingKind(Read_Decl, *this);
7240 
7241   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7242   if (!MaybeCode) {
7243     Error(MaybeCode.takeError());
7244     return nullptr;
7245   }
7246   unsigned Code = MaybeCode.get();
7247 
7248   ASTRecordReader Record(*this, *Loc.F);
7249   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7250   if (!MaybeRecCode) {
7251     Error(MaybeCode.takeError());
7252     return nullptr;
7253   }
7254   unsigned RecCode = MaybeRecCode.get();
7255 
7256   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7257     Error("malformed AST file: missing C++ base specifiers");
7258     return nullptr;
7259   }
7260 
7261   unsigned NumBases = Record.readInt();
7262   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7263   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7264   for (unsigned I = 0; I != NumBases; ++I)
7265     Bases[I] = Record.readCXXBaseSpecifier();
7266   return Bases;
7267 }
7268 
7269 serialization::DeclID
7270 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7271   if (LocalID < NUM_PREDEF_DECL_IDS)
7272     return LocalID;
7273 
7274   if (!F.ModuleOffsetMap.empty())
7275     ReadModuleOffsetMap(F);
7276 
7277   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7278     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7279   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7280 
7281   return LocalID + I->second;
7282 }
7283 
7284 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7285                                    ModuleFile &M) const {
7286   // Predefined decls aren't from any module.
7287   if (ID < NUM_PREDEF_DECL_IDS)
7288     return false;
7289 
7290   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7291          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7292 }
7293 
7294 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7295   if (!D->isFromASTFile())
7296     return nullptr;
7297   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7298   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7299   return I->second;
7300 }
7301 
7302 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7303   if (ID < NUM_PREDEF_DECL_IDS)
7304     return SourceLocation();
7305 
7306   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7307 
7308   if (Index > DeclsLoaded.size()) {
7309     Error("declaration ID out-of-range for AST file");
7310     return SourceLocation();
7311   }
7312 
7313   if (Decl *D = DeclsLoaded[Index])
7314     return D->getLocation();
7315 
7316   SourceLocation Loc;
7317   DeclCursorForID(ID, Loc);
7318   return Loc;
7319 }
7320 
7321 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7322   switch (ID) {
7323   case PREDEF_DECL_NULL_ID:
7324     return nullptr;
7325 
7326   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7327     return Context.getTranslationUnitDecl();
7328 
7329   case PREDEF_DECL_OBJC_ID_ID:
7330     return Context.getObjCIdDecl();
7331 
7332   case PREDEF_DECL_OBJC_SEL_ID:
7333     return Context.getObjCSelDecl();
7334 
7335   case PREDEF_DECL_OBJC_CLASS_ID:
7336     return Context.getObjCClassDecl();
7337 
7338   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7339     return Context.getObjCProtocolDecl();
7340 
7341   case PREDEF_DECL_INT_128_ID:
7342     return Context.getInt128Decl();
7343 
7344   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7345     return Context.getUInt128Decl();
7346 
7347   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7348     return Context.getObjCInstanceTypeDecl();
7349 
7350   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7351     return Context.getBuiltinVaListDecl();
7352 
7353   case PREDEF_DECL_VA_LIST_TAG:
7354     return Context.getVaListTagDecl();
7355 
7356   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7357     return Context.getBuiltinMSVaListDecl();
7358 
7359   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7360     return Context.getMSGuidTagDecl();
7361 
7362   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7363     return Context.getExternCContextDecl();
7364 
7365   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7366     return Context.getMakeIntegerSeqDecl();
7367 
7368   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7369     return Context.getCFConstantStringDecl();
7370 
7371   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7372     return Context.getCFConstantStringTagDecl();
7373 
7374   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7375     return Context.getTypePackElementDecl();
7376   }
7377   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7378 }
7379 
7380 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7381   assert(ContextObj && "reading decl with no AST context");
7382   if (ID < NUM_PREDEF_DECL_IDS) {
7383     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7384     if (D) {
7385       // Track that we have merged the declaration with ID \p ID into the
7386       // pre-existing predefined declaration \p D.
7387       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7388       if (Merged.empty())
7389         Merged.push_back(ID);
7390     }
7391     return D;
7392   }
7393 
7394   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7395 
7396   if (Index >= DeclsLoaded.size()) {
7397     assert(0 && "declaration ID out-of-range for AST file");
7398     Error("declaration ID out-of-range for AST file");
7399     return nullptr;
7400   }
7401 
7402   return DeclsLoaded[Index];
7403 }
7404 
7405 Decl *ASTReader::GetDecl(DeclID ID) {
7406   if (ID < NUM_PREDEF_DECL_IDS)
7407     return GetExistingDecl(ID);
7408 
7409   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7410 
7411   if (Index >= DeclsLoaded.size()) {
7412     assert(0 && "declaration ID out-of-range for AST file");
7413     Error("declaration ID out-of-range for AST file");
7414     return nullptr;
7415   }
7416 
7417   if (!DeclsLoaded[Index]) {
7418     ReadDeclRecord(ID);
7419     if (DeserializationListener)
7420       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7421   }
7422 
7423   return DeclsLoaded[Index];
7424 }
7425 
7426 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7427                                                   DeclID GlobalID) {
7428   if (GlobalID < NUM_PREDEF_DECL_IDS)
7429     return GlobalID;
7430 
7431   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7432   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7433   ModuleFile *Owner = I->second;
7434 
7435   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7436     = M.GlobalToLocalDeclIDs.find(Owner);
7437   if (Pos == M.GlobalToLocalDeclIDs.end())
7438     return 0;
7439 
7440   return GlobalID - Owner->BaseDeclID + Pos->second;
7441 }
7442 
7443 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7444                                             const RecordData &Record,
7445                                             unsigned &Idx) {
7446   if (Idx >= Record.size()) {
7447     Error("Corrupted AST file");
7448     return 0;
7449   }
7450 
7451   return getGlobalDeclID(F, Record[Idx++]);
7452 }
7453 
7454 /// Resolve the offset of a statement into a statement.
7455 ///
7456 /// This operation will read a new statement from the external
7457 /// source each time it is called, and is meant to be used via a
7458 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7459 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7460   // Switch case IDs are per Decl.
7461   ClearSwitchCaseIDs();
7462 
7463   // Offset here is a global offset across the entire chain.
7464   RecordLocation Loc = getLocalBitOffset(Offset);
7465   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7466     Error(std::move(Err));
7467     return nullptr;
7468   }
7469   assert(NumCurrentElementsDeserializing == 0 &&
7470          "should not be called while already deserializing");
7471   Deserializing D(this);
7472   return ReadStmtFromStream(*Loc.F);
7473 }
7474 
7475 void ASTReader::FindExternalLexicalDecls(
7476     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7477     SmallVectorImpl<Decl *> &Decls) {
7478   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7479 
7480   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7481     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7482     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7483       auto K = (Decl::Kind)+LexicalDecls[I];
7484       if (!IsKindWeWant(K))
7485         continue;
7486 
7487       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7488 
7489       // Don't add predefined declarations to the lexical context more
7490       // than once.
7491       if (ID < NUM_PREDEF_DECL_IDS) {
7492         if (PredefsVisited[ID])
7493           continue;
7494 
7495         PredefsVisited[ID] = true;
7496       }
7497 
7498       if (Decl *D = GetLocalDecl(*M, ID)) {
7499         assert(D->getKind() == K && "wrong kind for lexical decl");
7500         if (!DC->isDeclInLexicalTraversal(D))
7501           Decls.push_back(D);
7502       }
7503     }
7504   };
7505 
7506   if (isa<TranslationUnitDecl>(DC)) {
7507     for (auto Lexical : TULexicalDecls)
7508       Visit(Lexical.first, Lexical.second);
7509   } else {
7510     auto I = LexicalDecls.find(DC);
7511     if (I != LexicalDecls.end())
7512       Visit(I->second.first, I->second.second);
7513   }
7514 
7515   ++NumLexicalDeclContextsRead;
7516 }
7517 
7518 namespace {
7519 
7520 class DeclIDComp {
7521   ASTReader &Reader;
7522   ModuleFile &Mod;
7523 
7524 public:
7525   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7526 
7527   bool operator()(LocalDeclID L, LocalDeclID R) const {
7528     SourceLocation LHS = getLocation(L);
7529     SourceLocation RHS = getLocation(R);
7530     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7531   }
7532 
7533   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7534     SourceLocation RHS = getLocation(R);
7535     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7536   }
7537 
7538   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7539     SourceLocation LHS = getLocation(L);
7540     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7541   }
7542 
7543   SourceLocation getLocation(LocalDeclID ID) const {
7544     return Reader.getSourceManager().getFileLoc(
7545             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7546   }
7547 };
7548 
7549 } // namespace
7550 
7551 void ASTReader::FindFileRegionDecls(FileID File,
7552                                     unsigned Offset, unsigned Length,
7553                                     SmallVectorImpl<Decl *> &Decls) {
7554   SourceManager &SM = getSourceManager();
7555 
7556   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7557   if (I == FileDeclIDs.end())
7558     return;
7559 
7560   FileDeclsInfo &DInfo = I->second;
7561   if (DInfo.Decls.empty())
7562     return;
7563 
7564   SourceLocation
7565     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7566   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7567 
7568   DeclIDComp DIDComp(*this, *DInfo.Mod);
7569   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7570       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7571   if (BeginIt != DInfo.Decls.begin())
7572     --BeginIt;
7573 
7574   // If we are pointing at a top-level decl inside an objc container, we need
7575   // to backtrack until we find it otherwise we will fail to report that the
7576   // region overlaps with an objc container.
7577   while (BeginIt != DInfo.Decls.begin() &&
7578          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7579              ->isTopLevelDeclInObjCContainer())
7580     --BeginIt;
7581 
7582   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7583       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7584   if (EndIt != DInfo.Decls.end())
7585     ++EndIt;
7586 
7587   for (ArrayRef<serialization::LocalDeclID>::iterator
7588          DIt = BeginIt; DIt != EndIt; ++DIt)
7589     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7590 }
7591 
7592 bool
7593 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7594                                           DeclarationName Name) {
7595   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7596          "DeclContext has no visible decls in storage");
7597   if (!Name)
7598     return false;
7599 
7600   auto It = Lookups.find(DC);
7601   if (It == Lookups.end())
7602     return false;
7603 
7604   Deserializing LookupResults(this);
7605 
7606   // Load the list of declarations.
7607   SmallVector<NamedDecl *, 64> Decls;
7608   for (DeclID ID : It->second.Table.find(Name)) {
7609     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7610     if (ND->getDeclName() == Name)
7611       Decls.push_back(ND);
7612   }
7613 
7614   ++NumVisibleDeclContextsRead;
7615   SetExternalVisibleDeclsForName(DC, Name, Decls);
7616   return !Decls.empty();
7617 }
7618 
7619 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7620   if (!DC->hasExternalVisibleStorage())
7621     return;
7622 
7623   auto It = Lookups.find(DC);
7624   assert(It != Lookups.end() &&
7625          "have external visible storage but no lookup tables");
7626 
7627   DeclsMap Decls;
7628 
7629   for (DeclID ID : It->second.Table.findAll()) {
7630     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7631     Decls[ND->getDeclName()].push_back(ND);
7632   }
7633 
7634   ++NumVisibleDeclContextsRead;
7635 
7636   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7637     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7638   }
7639   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7640 }
7641 
7642 const serialization::reader::DeclContextLookupTable *
7643 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7644   auto I = Lookups.find(Primary);
7645   return I == Lookups.end() ? nullptr : &I->second;
7646 }
7647 
7648 /// Under non-PCH compilation the consumer receives the objc methods
7649 /// before receiving the implementation, and codegen depends on this.
7650 /// We simulate this by deserializing and passing to consumer the methods of the
7651 /// implementation before passing the deserialized implementation decl.
7652 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7653                                        ASTConsumer *Consumer) {
7654   assert(ImplD && Consumer);
7655 
7656   for (auto *I : ImplD->methods())
7657     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7658 
7659   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7660 }
7661 
7662 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7663   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7664     PassObjCImplDeclToConsumer(ImplD, Consumer);
7665   else
7666     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7667 }
7668 
7669 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7670   this->Consumer = Consumer;
7671 
7672   if (Consumer)
7673     PassInterestingDeclsToConsumer();
7674 
7675   if (DeserializationListener)
7676     DeserializationListener->ReaderInitialized(this);
7677 }
7678 
7679 void ASTReader::PrintStats() {
7680   std::fprintf(stderr, "*** AST File Statistics:\n");
7681 
7682   unsigned NumTypesLoaded
7683     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7684                                       QualType());
7685   unsigned NumDeclsLoaded
7686     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7687                                       (Decl *)nullptr);
7688   unsigned NumIdentifiersLoaded
7689     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7690                                             IdentifiersLoaded.end(),
7691                                             (IdentifierInfo *)nullptr);
7692   unsigned NumMacrosLoaded
7693     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7694                                        MacrosLoaded.end(),
7695                                        (MacroInfo *)nullptr);
7696   unsigned NumSelectorsLoaded
7697     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7698                                           SelectorsLoaded.end(),
7699                                           Selector());
7700 
7701   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7702     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7703                  NumSLocEntriesRead, TotalNumSLocEntries,
7704                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7705   if (!TypesLoaded.empty())
7706     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7707                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7708                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7709   if (!DeclsLoaded.empty())
7710     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7711                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7712                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7713   if (!IdentifiersLoaded.empty())
7714     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7715                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7716                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7717   if (!MacrosLoaded.empty())
7718     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7719                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7720                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7721   if (!SelectorsLoaded.empty())
7722     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7723                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7724                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7725   if (TotalNumStatements)
7726     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7727                  NumStatementsRead, TotalNumStatements,
7728                  ((float)NumStatementsRead/TotalNumStatements * 100));
7729   if (TotalNumMacros)
7730     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7731                  NumMacrosRead, TotalNumMacros,
7732                  ((float)NumMacrosRead/TotalNumMacros * 100));
7733   if (TotalLexicalDeclContexts)
7734     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7735                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7736                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7737                   * 100));
7738   if (TotalVisibleDeclContexts)
7739     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7740                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7741                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7742                   * 100));
7743   if (TotalNumMethodPoolEntries)
7744     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7745                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7746                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7747                   * 100));
7748   if (NumMethodPoolLookups)
7749     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7750                  NumMethodPoolHits, NumMethodPoolLookups,
7751                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7752   if (NumMethodPoolTableLookups)
7753     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7754                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7755                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7756                   * 100.0));
7757   if (NumIdentifierLookupHits)
7758     std::fprintf(stderr,
7759                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7760                  NumIdentifierLookupHits, NumIdentifierLookups,
7761                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7762 
7763   if (GlobalIndex) {
7764     std::fprintf(stderr, "\n");
7765     GlobalIndex->printStats();
7766   }
7767 
7768   std::fprintf(stderr, "\n");
7769   dump();
7770   std::fprintf(stderr, "\n");
7771 }
7772 
7773 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7774 LLVM_DUMP_METHOD static void
7775 dumpModuleIDMap(StringRef Name,
7776                 const ContinuousRangeMap<Key, ModuleFile *,
7777                                          InitialCapacity> &Map) {
7778   if (Map.begin() == Map.end())
7779     return;
7780 
7781   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7782 
7783   llvm::errs() << Name << ":\n";
7784   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7785        I != IEnd; ++I) {
7786     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7787       << "\n";
7788   }
7789 }
7790 
7791 LLVM_DUMP_METHOD void ASTReader::dump() {
7792   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7793   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7794   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7795   dumpModuleIDMap("Global type map", GlobalTypeMap);
7796   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7797   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7798   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7799   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7800   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7801   dumpModuleIDMap("Global preprocessed entity map",
7802                   GlobalPreprocessedEntityMap);
7803 
7804   llvm::errs() << "\n*** PCH/Modules Loaded:";
7805   for (ModuleFile &M : ModuleMgr)
7806     M.dump();
7807 }
7808 
7809 /// Return the amount of memory used by memory buffers, breaking down
7810 /// by heap-backed versus mmap'ed memory.
7811 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7812   for (ModuleFile &I : ModuleMgr) {
7813     if (llvm::MemoryBuffer *buf = I.Buffer) {
7814       size_t bytes = buf->getBufferSize();
7815       switch (buf->getBufferKind()) {
7816         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7817           sizes.malloc_bytes += bytes;
7818           break;
7819         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7820           sizes.mmap_bytes += bytes;
7821           break;
7822       }
7823     }
7824   }
7825 }
7826 
7827 void ASTReader::InitializeSema(Sema &S) {
7828   SemaObj = &S;
7829   S.addExternalSource(this);
7830 
7831   // Makes sure any declarations that were deserialized "too early"
7832   // still get added to the identifier's declaration chains.
7833   for (uint64_t ID : PreloadedDeclIDs) {
7834     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7835     pushExternalDeclIntoScope(D, D->getDeclName());
7836   }
7837   PreloadedDeclIDs.clear();
7838 
7839   // FIXME: What happens if these are changed by a module import?
7840   if (!FPPragmaOptions.empty()) {
7841     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7842     FPOptionsOverride NewOverrides =
7843         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7844     SemaObj->CurFPFeatures =
7845         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7846   }
7847 
7848   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7849   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7850   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7851 
7852   UpdateSema();
7853 }
7854 
7855 void ASTReader::UpdateSema() {
7856   assert(SemaObj && "no Sema to update");
7857 
7858   // Load the offsets of the declarations that Sema references.
7859   // They will be lazily deserialized when needed.
7860   if (!SemaDeclRefs.empty()) {
7861     assert(SemaDeclRefs.size() % 3 == 0);
7862     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7863       if (!SemaObj->StdNamespace)
7864         SemaObj->StdNamespace = SemaDeclRefs[I];
7865       if (!SemaObj->StdBadAlloc)
7866         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7867       if (!SemaObj->StdAlignValT)
7868         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7869     }
7870     SemaDeclRefs.clear();
7871   }
7872 
7873   // Update the state of pragmas. Use the same API as if we had encountered the
7874   // pragma in the source.
7875   if(OptimizeOffPragmaLocation.isValid())
7876     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7877   if (PragmaMSStructState != -1)
7878     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7879   if (PointersToMembersPragmaLocation.isValid()) {
7880     SemaObj->ActOnPragmaMSPointersToMembers(
7881         (LangOptions::PragmaMSPointersToMembersKind)
7882             PragmaMSPointersToMembersState,
7883         PointersToMembersPragmaLocation);
7884   }
7885   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7886 
7887   if (PragmaPackCurrentValue) {
7888     // The bottom of the stack might have a default value. It must be adjusted
7889     // to the current value to ensure that the packing state is preserved after
7890     // popping entries that were included/imported from a PCH/module.
7891     bool DropFirst = false;
7892     if (!PragmaPackStack.empty() &&
7893         PragmaPackStack.front().Location.isInvalid()) {
7894       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7895              "Expected a default alignment value");
7896       SemaObj->PackStack.Stack.emplace_back(
7897           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7898           SemaObj->PackStack.CurrentPragmaLocation,
7899           PragmaPackStack.front().PushLocation);
7900       DropFirst = true;
7901     }
7902     for (const auto &Entry :
7903          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7904       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7905                                             Entry.Location, Entry.PushLocation);
7906     if (PragmaPackCurrentLocation.isInvalid()) {
7907       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7908              "Expected a default alignment value");
7909       // Keep the current values.
7910     } else {
7911       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7912       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7913     }
7914   }
7915   if (FpPragmaCurrentValue) {
7916     // The bottom of the stack might have a default value. It must be adjusted
7917     // to the current value to ensure that fp-pragma state is preserved after
7918     // popping entries that were included/imported from a PCH/module.
7919     bool DropFirst = false;
7920     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7921       assert(FpPragmaStack.front().Value ==
7922                  SemaObj->FpPragmaStack.DefaultValue &&
7923              "Expected a default pragma float_control value");
7924       SemaObj->FpPragmaStack.Stack.emplace_back(
7925           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7926           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7927           FpPragmaStack.front().PushLocation);
7928       DropFirst = true;
7929     }
7930     for (const auto &Entry :
7931          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7932       SemaObj->FpPragmaStack.Stack.emplace_back(
7933           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7934     if (FpPragmaCurrentLocation.isInvalid()) {
7935       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7936              "Expected a default pragma float_control value");
7937       // Keep the current values.
7938     } else {
7939       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7940       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7941     }
7942   }
7943 
7944   // For non-modular AST files, restore visiblity of modules.
7945   for (auto &Import : ImportedModules) {
7946     if (Import.ImportLoc.isInvalid())
7947       continue;
7948     if (Module *Imported = getSubmodule(Import.ID)) {
7949       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7950     }
7951   }
7952 }
7953 
7954 IdentifierInfo *ASTReader::get(StringRef Name) {
7955   // Note that we are loading an identifier.
7956   Deserializing AnIdentifier(this);
7957 
7958   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7959                                   NumIdentifierLookups,
7960                                   NumIdentifierLookupHits);
7961 
7962   // We don't need to do identifier table lookups in C++ modules (we preload
7963   // all interesting declarations, and don't need to use the scope for name
7964   // lookups). Perform the lookup in PCH files, though, since we don't build
7965   // a complete initial identifier table if we're carrying on from a PCH.
7966   if (PP.getLangOpts().CPlusPlus) {
7967     for (auto F : ModuleMgr.pch_modules())
7968       if (Visitor(*F))
7969         break;
7970   } else {
7971     // If there is a global index, look there first to determine which modules
7972     // provably do not have any results for this identifier.
7973     GlobalModuleIndex::HitSet Hits;
7974     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7975     if (!loadGlobalIndex()) {
7976       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7977         HitsPtr = &Hits;
7978       }
7979     }
7980 
7981     ModuleMgr.visit(Visitor, HitsPtr);
7982   }
7983 
7984   IdentifierInfo *II = Visitor.getIdentifierInfo();
7985   markIdentifierUpToDate(II);
7986   return II;
7987 }
7988 
7989 namespace clang {
7990 
7991   /// An identifier-lookup iterator that enumerates all of the
7992   /// identifiers stored within a set of AST files.
7993   class ASTIdentifierIterator : public IdentifierIterator {
7994     /// The AST reader whose identifiers are being enumerated.
7995     const ASTReader &Reader;
7996 
7997     /// The current index into the chain of AST files stored in
7998     /// the AST reader.
7999     unsigned Index;
8000 
8001     /// The current position within the identifier lookup table
8002     /// of the current AST file.
8003     ASTIdentifierLookupTable::key_iterator Current;
8004 
8005     /// The end position within the identifier lookup table of
8006     /// the current AST file.
8007     ASTIdentifierLookupTable::key_iterator End;
8008 
8009     /// Whether to skip any modules in the ASTReader.
8010     bool SkipModules;
8011 
8012   public:
8013     explicit ASTIdentifierIterator(const ASTReader &Reader,
8014                                    bool SkipModules = false);
8015 
8016     StringRef Next() override;
8017   };
8018 
8019 } // namespace clang
8020 
8021 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8022                                              bool SkipModules)
8023     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8024 }
8025 
8026 StringRef ASTIdentifierIterator::Next() {
8027   while (Current == End) {
8028     // If we have exhausted all of our AST files, we're done.
8029     if (Index == 0)
8030       return StringRef();
8031 
8032     --Index;
8033     ModuleFile &F = Reader.ModuleMgr[Index];
8034     if (SkipModules && F.isModule())
8035       continue;
8036 
8037     ASTIdentifierLookupTable *IdTable =
8038         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8039     Current = IdTable->key_begin();
8040     End = IdTable->key_end();
8041   }
8042 
8043   // We have any identifiers remaining in the current AST file; return
8044   // the next one.
8045   StringRef Result = *Current;
8046   ++Current;
8047   return Result;
8048 }
8049 
8050 namespace {
8051 
8052 /// A utility for appending two IdentifierIterators.
8053 class ChainedIdentifierIterator : public IdentifierIterator {
8054   std::unique_ptr<IdentifierIterator> Current;
8055   std::unique_ptr<IdentifierIterator> Queued;
8056 
8057 public:
8058   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8059                             std::unique_ptr<IdentifierIterator> Second)
8060       : Current(std::move(First)), Queued(std::move(Second)) {}
8061 
8062   StringRef Next() override {
8063     if (!Current)
8064       return StringRef();
8065 
8066     StringRef result = Current->Next();
8067     if (!result.empty())
8068       return result;
8069 
8070     // Try the queued iterator, which may itself be empty.
8071     Current.reset();
8072     std::swap(Current, Queued);
8073     return Next();
8074   }
8075 };
8076 
8077 } // namespace
8078 
8079 IdentifierIterator *ASTReader::getIdentifiers() {
8080   if (!loadGlobalIndex()) {
8081     std::unique_ptr<IdentifierIterator> ReaderIter(
8082         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8083     std::unique_ptr<IdentifierIterator> ModulesIter(
8084         GlobalIndex->createIdentifierIterator());
8085     return new ChainedIdentifierIterator(std::move(ReaderIter),
8086                                          std::move(ModulesIter));
8087   }
8088 
8089   return new ASTIdentifierIterator(*this);
8090 }
8091 
8092 namespace clang {
8093 namespace serialization {
8094 
8095   class ReadMethodPoolVisitor {
8096     ASTReader &Reader;
8097     Selector Sel;
8098     unsigned PriorGeneration;
8099     unsigned InstanceBits = 0;
8100     unsigned FactoryBits = 0;
8101     bool InstanceHasMoreThanOneDecl = false;
8102     bool FactoryHasMoreThanOneDecl = false;
8103     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8104     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8105 
8106   public:
8107     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8108                           unsigned PriorGeneration)
8109         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8110 
8111     bool operator()(ModuleFile &M) {
8112       if (!M.SelectorLookupTable)
8113         return false;
8114 
8115       // If we've already searched this module file, skip it now.
8116       if (M.Generation <= PriorGeneration)
8117         return true;
8118 
8119       ++Reader.NumMethodPoolTableLookups;
8120       ASTSelectorLookupTable *PoolTable
8121         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8122       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8123       if (Pos == PoolTable->end())
8124         return false;
8125 
8126       ++Reader.NumMethodPoolTableHits;
8127       ++Reader.NumSelectorsRead;
8128       // FIXME: Not quite happy with the statistics here. We probably should
8129       // disable this tracking when called via LoadSelector.
8130       // Also, should entries without methods count as misses?
8131       ++Reader.NumMethodPoolEntriesRead;
8132       ASTSelectorLookupTrait::data_type Data = *Pos;
8133       if (Reader.DeserializationListener)
8134         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8135 
8136       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8137       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8138       InstanceBits = Data.InstanceBits;
8139       FactoryBits = Data.FactoryBits;
8140       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8141       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8142       return true;
8143     }
8144 
8145     /// Retrieve the instance methods found by this visitor.
8146     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8147       return InstanceMethods;
8148     }
8149 
8150     /// Retrieve the instance methods found by this visitor.
8151     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8152       return FactoryMethods;
8153     }
8154 
8155     unsigned getInstanceBits() const { return InstanceBits; }
8156     unsigned getFactoryBits() const { return FactoryBits; }
8157 
8158     bool instanceHasMoreThanOneDecl() const {
8159       return InstanceHasMoreThanOneDecl;
8160     }
8161 
8162     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8163   };
8164 
8165 } // namespace serialization
8166 } // namespace clang
8167 
8168 /// Add the given set of methods to the method list.
8169 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8170                              ObjCMethodList &List) {
8171   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8172     S.addMethodToGlobalList(&List, Methods[I]);
8173   }
8174 }
8175 
8176 void ASTReader::ReadMethodPool(Selector Sel) {
8177   // Get the selector generation and update it to the current generation.
8178   unsigned &Generation = SelectorGeneration[Sel];
8179   unsigned PriorGeneration = Generation;
8180   Generation = getGeneration();
8181   SelectorOutOfDate[Sel] = false;
8182 
8183   // Search for methods defined with this selector.
8184   ++NumMethodPoolLookups;
8185   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8186   ModuleMgr.visit(Visitor);
8187 
8188   if (Visitor.getInstanceMethods().empty() &&
8189       Visitor.getFactoryMethods().empty())
8190     return;
8191 
8192   ++NumMethodPoolHits;
8193 
8194   if (!getSema())
8195     return;
8196 
8197   Sema &S = *getSema();
8198   Sema::GlobalMethodPool::iterator Pos
8199     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8200 
8201   Pos->second.first.setBits(Visitor.getInstanceBits());
8202   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8203   Pos->second.second.setBits(Visitor.getFactoryBits());
8204   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8205 
8206   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8207   // when building a module we keep every method individually and may need to
8208   // update hasMoreThanOneDecl as we add the methods.
8209   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8210   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8211 }
8212 
8213 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8214   if (SelectorOutOfDate[Sel])
8215     ReadMethodPool(Sel);
8216 }
8217 
8218 void ASTReader::ReadKnownNamespaces(
8219                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8220   Namespaces.clear();
8221 
8222   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8223     if (NamespaceDecl *Namespace
8224                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8225       Namespaces.push_back(Namespace);
8226   }
8227 }
8228 
8229 void ASTReader::ReadUndefinedButUsed(
8230     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8231   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8232     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8233     SourceLocation Loc =
8234         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8235     Undefined.insert(std::make_pair(D, Loc));
8236   }
8237 }
8238 
8239 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8240     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8241                                                      Exprs) {
8242   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8243     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8244     uint64_t Count = DelayedDeleteExprs[Idx++];
8245     for (uint64_t C = 0; C < Count; ++C) {
8246       SourceLocation DeleteLoc =
8247           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8248       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8249       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8250     }
8251   }
8252 }
8253 
8254 void ASTReader::ReadTentativeDefinitions(
8255                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8256   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8257     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8258     if (Var)
8259       TentativeDefs.push_back(Var);
8260   }
8261   TentativeDefinitions.clear();
8262 }
8263 
8264 void ASTReader::ReadUnusedFileScopedDecls(
8265                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8266   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8267     DeclaratorDecl *D
8268       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8269     if (D)
8270       Decls.push_back(D);
8271   }
8272   UnusedFileScopedDecls.clear();
8273 }
8274 
8275 void ASTReader::ReadDelegatingConstructors(
8276                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8277   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8278     CXXConstructorDecl *D
8279       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8280     if (D)
8281       Decls.push_back(D);
8282   }
8283   DelegatingCtorDecls.clear();
8284 }
8285 
8286 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8287   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8288     TypedefNameDecl *D
8289       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8290     if (D)
8291       Decls.push_back(D);
8292   }
8293   ExtVectorDecls.clear();
8294 }
8295 
8296 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8297     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8298   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8299        ++I) {
8300     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8301         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8302     if (D)
8303       Decls.insert(D);
8304   }
8305   UnusedLocalTypedefNameCandidates.clear();
8306 }
8307 
8308 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8309     llvm::SmallVector<Decl *, 4> &Decls) {
8310   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8311        ++I) {
8312     auto *D = dyn_cast_or_null<Decl>(
8313         GetDecl(DeclsToCheckForDeferredDiags[I]));
8314     if (D)
8315       Decls.push_back(D);
8316   }
8317   DeclsToCheckForDeferredDiags.clear();
8318 }
8319 
8320 
8321 void ASTReader::ReadReferencedSelectors(
8322        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8323   if (ReferencedSelectorsData.empty())
8324     return;
8325 
8326   // If there are @selector references added them to its pool. This is for
8327   // implementation of -Wselector.
8328   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8329   unsigned I = 0;
8330   while (I < DataSize) {
8331     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8332     SourceLocation SelLoc
8333       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8334     Sels.push_back(std::make_pair(Sel, SelLoc));
8335   }
8336   ReferencedSelectorsData.clear();
8337 }
8338 
8339 void ASTReader::ReadWeakUndeclaredIdentifiers(
8340        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8341   if (WeakUndeclaredIdentifiers.empty())
8342     return;
8343 
8344   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8345     IdentifierInfo *WeakId
8346       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8347     IdentifierInfo *AliasId
8348       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8349     SourceLocation Loc
8350       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8351     bool Used = WeakUndeclaredIdentifiers[I++];
8352     WeakInfo WI(AliasId, Loc);
8353     WI.setUsed(Used);
8354     WeakIDs.push_back(std::make_pair(WeakId, WI));
8355   }
8356   WeakUndeclaredIdentifiers.clear();
8357 }
8358 
8359 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8360   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8361     ExternalVTableUse VT;
8362     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8363     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8364     VT.DefinitionRequired = VTableUses[Idx++];
8365     VTables.push_back(VT);
8366   }
8367 
8368   VTableUses.clear();
8369 }
8370 
8371 void ASTReader::ReadPendingInstantiations(
8372        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8373   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8374     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8375     SourceLocation Loc
8376       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8377 
8378     Pending.push_back(std::make_pair(D, Loc));
8379   }
8380   PendingInstantiations.clear();
8381 }
8382 
8383 void ASTReader::ReadLateParsedTemplates(
8384     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8385         &LPTMap) {
8386   for (auto &LPT : LateParsedTemplates) {
8387     ModuleFile *FMod = LPT.first;
8388     RecordDataImpl &LateParsed = LPT.second;
8389     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8390          /* In loop */) {
8391       FunctionDecl *FD =
8392           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8393 
8394       auto LT = std::make_unique<LateParsedTemplate>();
8395       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8396 
8397       ModuleFile *F = getOwningModuleFile(LT->D);
8398       assert(F && "No module");
8399 
8400       unsigned TokN = LateParsed[Idx++];
8401       LT->Toks.reserve(TokN);
8402       for (unsigned T = 0; T < TokN; ++T)
8403         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8404 
8405       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8406     }
8407   }
8408 }
8409 
8410 void ASTReader::LoadSelector(Selector Sel) {
8411   // It would be complicated to avoid reading the methods anyway. So don't.
8412   ReadMethodPool(Sel);
8413 }
8414 
8415 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8416   assert(ID && "Non-zero identifier ID required");
8417   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8418   IdentifiersLoaded[ID - 1] = II;
8419   if (DeserializationListener)
8420     DeserializationListener->IdentifierRead(ID, II);
8421 }
8422 
8423 /// Set the globally-visible declarations associated with the given
8424 /// identifier.
8425 ///
8426 /// If the AST reader is currently in a state where the given declaration IDs
8427 /// cannot safely be resolved, they are queued until it is safe to resolve
8428 /// them.
8429 ///
8430 /// \param II an IdentifierInfo that refers to one or more globally-visible
8431 /// declarations.
8432 ///
8433 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8434 /// visible at global scope.
8435 ///
8436 /// \param Decls if non-null, this vector will be populated with the set of
8437 /// deserialized declarations. These declarations will not be pushed into
8438 /// scope.
8439 void
8440 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8441                               const SmallVectorImpl<uint32_t> &DeclIDs,
8442                                    SmallVectorImpl<Decl *> *Decls) {
8443   if (NumCurrentElementsDeserializing && !Decls) {
8444     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8445     return;
8446   }
8447 
8448   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8449     if (!SemaObj) {
8450       // Queue this declaration so that it will be added to the
8451       // translation unit scope and identifier's declaration chain
8452       // once a Sema object is known.
8453       PreloadedDeclIDs.push_back(DeclIDs[I]);
8454       continue;
8455     }
8456 
8457     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8458 
8459     // If we're simply supposed to record the declarations, do so now.
8460     if (Decls) {
8461       Decls->push_back(D);
8462       continue;
8463     }
8464 
8465     // Introduce this declaration into the translation-unit scope
8466     // and add it to the declaration chain for this identifier, so
8467     // that (unqualified) name lookup will find it.
8468     pushExternalDeclIntoScope(D, II);
8469   }
8470 }
8471 
8472 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8473   if (ID == 0)
8474     return nullptr;
8475 
8476   if (IdentifiersLoaded.empty()) {
8477     Error("no identifier table in AST file");
8478     return nullptr;
8479   }
8480 
8481   ID -= 1;
8482   if (!IdentifiersLoaded[ID]) {
8483     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8484     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8485     ModuleFile *M = I->second;
8486     unsigned Index = ID - M->BaseIdentifierID;
8487     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8488 
8489     // All of the strings in the AST file are preceded by a 16-bit length.
8490     // Extract that 16-bit length to avoid having to execute strlen().
8491     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8492     //  unsigned integers.  This is important to avoid integer overflow when
8493     //  we cast them to 'unsigned'.
8494     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8495     unsigned StrLen = (((unsigned) StrLenPtr[0])
8496                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8497     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8498     IdentifiersLoaded[ID] = &II;
8499     markIdentifierFromAST(*this,  II);
8500     if (DeserializationListener)
8501       DeserializationListener->IdentifierRead(ID + 1, &II);
8502   }
8503 
8504   return IdentifiersLoaded[ID];
8505 }
8506 
8507 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8508   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8509 }
8510 
8511 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8512   if (LocalID < NUM_PREDEF_IDENT_IDS)
8513     return LocalID;
8514 
8515   if (!M.ModuleOffsetMap.empty())
8516     ReadModuleOffsetMap(M);
8517 
8518   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8519     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8520   assert(I != M.IdentifierRemap.end()
8521          && "Invalid index into identifier index remap");
8522 
8523   return LocalID + I->second;
8524 }
8525 
8526 MacroInfo *ASTReader::getMacro(MacroID ID) {
8527   if (ID == 0)
8528     return nullptr;
8529 
8530   if (MacrosLoaded.empty()) {
8531     Error("no macro table in AST file");
8532     return nullptr;
8533   }
8534 
8535   ID -= NUM_PREDEF_MACRO_IDS;
8536   if (!MacrosLoaded[ID]) {
8537     GlobalMacroMapType::iterator I
8538       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8539     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8540     ModuleFile *M = I->second;
8541     unsigned Index = ID - M->BaseMacroID;
8542     MacrosLoaded[ID] =
8543         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8544 
8545     if (DeserializationListener)
8546       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8547                                          MacrosLoaded[ID]);
8548   }
8549 
8550   return MacrosLoaded[ID];
8551 }
8552 
8553 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8554   if (LocalID < NUM_PREDEF_MACRO_IDS)
8555     return LocalID;
8556 
8557   if (!M.ModuleOffsetMap.empty())
8558     ReadModuleOffsetMap(M);
8559 
8560   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8561     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8562   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8563 
8564   return LocalID + I->second;
8565 }
8566 
8567 serialization::SubmoduleID
8568 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8569   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8570     return LocalID;
8571 
8572   if (!M.ModuleOffsetMap.empty())
8573     ReadModuleOffsetMap(M);
8574 
8575   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8576     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8577   assert(I != M.SubmoduleRemap.end()
8578          && "Invalid index into submodule index remap");
8579 
8580   return LocalID + I->second;
8581 }
8582 
8583 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8584   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8585     assert(GlobalID == 0 && "Unhandled global submodule ID");
8586     return nullptr;
8587   }
8588 
8589   if (GlobalID > SubmodulesLoaded.size()) {
8590     Error("submodule ID out of range in AST file");
8591     return nullptr;
8592   }
8593 
8594   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8595 }
8596 
8597 Module *ASTReader::getModule(unsigned ID) {
8598   return getSubmodule(ID);
8599 }
8600 
8601 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8602   if (ID & 1) {
8603     // It's a module, look it up by submodule ID.
8604     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8605     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8606   } else {
8607     // It's a prefix (preamble, PCH, ...). Look it up by index.
8608     unsigned IndexFromEnd = ID >> 1;
8609     assert(IndexFromEnd && "got reference to unknown module file");
8610     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8611   }
8612 }
8613 
8614 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8615   if (!F)
8616     return 1;
8617 
8618   // For a file representing a module, use the submodule ID of the top-level
8619   // module as the file ID. For any other kind of file, the number of such
8620   // files loaded beforehand will be the same on reload.
8621   // FIXME: Is this true even if we have an explicit module file and a PCH?
8622   if (F->isModule())
8623     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8624 
8625   auto PCHModules = getModuleManager().pch_modules();
8626   auto I = llvm::find(PCHModules, F);
8627   assert(I != PCHModules.end() && "emitting reference to unknown file");
8628   return (I - PCHModules.end()) << 1;
8629 }
8630 
8631 llvm::Optional<ASTSourceDescriptor>
8632 ASTReader::getSourceDescriptor(unsigned ID) {
8633   if (Module *M = getSubmodule(ID))
8634     return ASTSourceDescriptor(*M);
8635 
8636   // If there is only a single PCH, return it instead.
8637   // Chained PCH are not supported.
8638   const auto &PCHChain = ModuleMgr.pch_modules();
8639   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8640     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8641     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8642     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8643     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8644                                MF.Signature);
8645   }
8646   return None;
8647 }
8648 
8649 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8650   auto I = DefinitionSource.find(FD);
8651   if (I == DefinitionSource.end())
8652     return EK_ReplyHazy;
8653   return I->second ? EK_Never : EK_Always;
8654 }
8655 
8656 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8657   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8658 }
8659 
8660 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8661   if (ID == 0)
8662     return Selector();
8663 
8664   if (ID > SelectorsLoaded.size()) {
8665     Error("selector ID out of range in AST file");
8666     return Selector();
8667   }
8668 
8669   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8670     // Load this selector from the selector table.
8671     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8672     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8673     ModuleFile &M = *I->second;
8674     ASTSelectorLookupTrait Trait(*this, M);
8675     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8676     SelectorsLoaded[ID - 1] =
8677       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8678     if (DeserializationListener)
8679       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8680   }
8681 
8682   return SelectorsLoaded[ID - 1];
8683 }
8684 
8685 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8686   return DecodeSelector(ID);
8687 }
8688 
8689 uint32_t ASTReader::GetNumExternalSelectors() {
8690   // ID 0 (the null selector) is considered an external selector.
8691   return getTotalNumSelectors() + 1;
8692 }
8693 
8694 serialization::SelectorID
8695 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8696   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8697     return LocalID;
8698 
8699   if (!M.ModuleOffsetMap.empty())
8700     ReadModuleOffsetMap(M);
8701 
8702   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8703     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8704   assert(I != M.SelectorRemap.end()
8705          && "Invalid index into selector index remap");
8706 
8707   return LocalID + I->second;
8708 }
8709 
8710 DeclarationNameLoc
8711 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8712   DeclarationNameLoc DNLoc;
8713   switch (Name.getNameKind()) {
8714   case DeclarationName::CXXConstructorName:
8715   case DeclarationName::CXXDestructorName:
8716   case DeclarationName::CXXConversionFunctionName:
8717     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8718     break;
8719 
8720   case DeclarationName::CXXOperatorName:
8721     DNLoc.CXXOperatorName.BeginOpNameLoc
8722       = readSourceLocation().getRawEncoding();
8723     DNLoc.CXXOperatorName.EndOpNameLoc
8724       = readSourceLocation().getRawEncoding();
8725     break;
8726 
8727   case DeclarationName::CXXLiteralOperatorName:
8728     DNLoc.CXXLiteralOperatorName.OpNameLoc
8729       = readSourceLocation().getRawEncoding();
8730     break;
8731 
8732   case DeclarationName::Identifier:
8733   case DeclarationName::ObjCZeroArgSelector:
8734   case DeclarationName::ObjCOneArgSelector:
8735   case DeclarationName::ObjCMultiArgSelector:
8736   case DeclarationName::CXXUsingDirective:
8737   case DeclarationName::CXXDeductionGuideName:
8738     break;
8739   }
8740   return DNLoc;
8741 }
8742 
8743 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8744   DeclarationNameInfo NameInfo;
8745   NameInfo.setName(readDeclarationName());
8746   NameInfo.setLoc(readSourceLocation());
8747   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8748   return NameInfo;
8749 }
8750 
8751 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8752   Info.QualifierLoc = readNestedNameSpecifierLoc();
8753   unsigned NumTPLists = readInt();
8754   Info.NumTemplParamLists = NumTPLists;
8755   if (NumTPLists) {
8756     Info.TemplParamLists =
8757         new (getContext()) TemplateParameterList *[NumTPLists];
8758     for (unsigned i = 0; i != NumTPLists; ++i)
8759       Info.TemplParamLists[i] = readTemplateParameterList();
8760   }
8761 }
8762 
8763 TemplateParameterList *
8764 ASTRecordReader::readTemplateParameterList() {
8765   SourceLocation TemplateLoc = readSourceLocation();
8766   SourceLocation LAngleLoc = readSourceLocation();
8767   SourceLocation RAngleLoc = readSourceLocation();
8768 
8769   unsigned NumParams = readInt();
8770   SmallVector<NamedDecl *, 16> Params;
8771   Params.reserve(NumParams);
8772   while (NumParams--)
8773     Params.push_back(readDeclAs<NamedDecl>());
8774 
8775   bool HasRequiresClause = readBool();
8776   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8777 
8778   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8779       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8780   return TemplateParams;
8781 }
8782 
8783 void ASTRecordReader::readTemplateArgumentList(
8784                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8785                         bool Canonicalize) {
8786   unsigned NumTemplateArgs = readInt();
8787   TemplArgs.reserve(NumTemplateArgs);
8788   while (NumTemplateArgs--)
8789     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8790 }
8791 
8792 /// Read a UnresolvedSet structure.
8793 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8794   unsigned NumDecls = readInt();
8795   Set.reserve(getContext(), NumDecls);
8796   while (NumDecls--) {
8797     DeclID ID = readDeclID();
8798     AccessSpecifier AS = (AccessSpecifier) readInt();
8799     Set.addLazyDecl(getContext(), ID, AS);
8800   }
8801 }
8802 
8803 CXXBaseSpecifier
8804 ASTRecordReader::readCXXBaseSpecifier() {
8805   bool isVirtual = readBool();
8806   bool isBaseOfClass = readBool();
8807   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8808   bool inheritConstructors = readBool();
8809   TypeSourceInfo *TInfo = readTypeSourceInfo();
8810   SourceRange Range = readSourceRange();
8811   SourceLocation EllipsisLoc = readSourceLocation();
8812   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8813                           EllipsisLoc);
8814   Result.setInheritConstructors(inheritConstructors);
8815   return Result;
8816 }
8817 
8818 CXXCtorInitializer **
8819 ASTRecordReader::readCXXCtorInitializers() {
8820   ASTContext &Context = getContext();
8821   unsigned NumInitializers = readInt();
8822   assert(NumInitializers && "wrote ctor initializers but have no inits");
8823   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8824   for (unsigned i = 0; i != NumInitializers; ++i) {
8825     TypeSourceInfo *TInfo = nullptr;
8826     bool IsBaseVirtual = false;
8827     FieldDecl *Member = nullptr;
8828     IndirectFieldDecl *IndirectMember = nullptr;
8829 
8830     CtorInitializerType Type = (CtorInitializerType) readInt();
8831     switch (Type) {
8832     case CTOR_INITIALIZER_BASE:
8833       TInfo = readTypeSourceInfo();
8834       IsBaseVirtual = readBool();
8835       break;
8836 
8837     case CTOR_INITIALIZER_DELEGATING:
8838       TInfo = readTypeSourceInfo();
8839       break;
8840 
8841      case CTOR_INITIALIZER_MEMBER:
8842       Member = readDeclAs<FieldDecl>();
8843       break;
8844 
8845      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8846       IndirectMember = readDeclAs<IndirectFieldDecl>();
8847       break;
8848     }
8849 
8850     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8851     Expr *Init = readExpr();
8852     SourceLocation LParenLoc = readSourceLocation();
8853     SourceLocation RParenLoc = readSourceLocation();
8854 
8855     CXXCtorInitializer *BOMInit;
8856     if (Type == CTOR_INITIALIZER_BASE)
8857       BOMInit = new (Context)
8858           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8859                              RParenLoc, MemberOrEllipsisLoc);
8860     else if (Type == CTOR_INITIALIZER_DELEGATING)
8861       BOMInit = new (Context)
8862           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8863     else if (Member)
8864       BOMInit = new (Context)
8865           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8866                              Init, RParenLoc);
8867     else
8868       BOMInit = new (Context)
8869           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8870                              LParenLoc, Init, RParenLoc);
8871 
8872     if (/*IsWritten*/readBool()) {
8873       unsigned SourceOrder = readInt();
8874       BOMInit->setSourceOrder(SourceOrder);
8875     }
8876 
8877     CtorInitializers[i] = BOMInit;
8878   }
8879 
8880   return CtorInitializers;
8881 }
8882 
8883 NestedNameSpecifierLoc
8884 ASTRecordReader::readNestedNameSpecifierLoc() {
8885   ASTContext &Context = getContext();
8886   unsigned N = readInt();
8887   NestedNameSpecifierLocBuilder Builder;
8888   for (unsigned I = 0; I != N; ++I) {
8889     auto Kind = readNestedNameSpecifierKind();
8890     switch (Kind) {
8891     case NestedNameSpecifier::Identifier: {
8892       IdentifierInfo *II = readIdentifier();
8893       SourceRange Range = readSourceRange();
8894       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8895       break;
8896     }
8897 
8898     case NestedNameSpecifier::Namespace: {
8899       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8900       SourceRange Range = readSourceRange();
8901       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8902       break;
8903     }
8904 
8905     case NestedNameSpecifier::NamespaceAlias: {
8906       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8907       SourceRange Range = readSourceRange();
8908       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8909       break;
8910     }
8911 
8912     case NestedNameSpecifier::TypeSpec:
8913     case NestedNameSpecifier::TypeSpecWithTemplate: {
8914       bool Template = readBool();
8915       TypeSourceInfo *T = readTypeSourceInfo();
8916       if (!T)
8917         return NestedNameSpecifierLoc();
8918       SourceLocation ColonColonLoc = readSourceLocation();
8919 
8920       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8921       Builder.Extend(Context,
8922                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8923                      T->getTypeLoc(), ColonColonLoc);
8924       break;
8925     }
8926 
8927     case NestedNameSpecifier::Global: {
8928       SourceLocation ColonColonLoc = readSourceLocation();
8929       Builder.MakeGlobal(Context, ColonColonLoc);
8930       break;
8931     }
8932 
8933     case NestedNameSpecifier::Super: {
8934       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8935       SourceRange Range = readSourceRange();
8936       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8937       break;
8938     }
8939     }
8940   }
8941 
8942   return Builder.getWithLocInContext(Context);
8943 }
8944 
8945 SourceRange
8946 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8947                            unsigned &Idx) {
8948   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8949   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8950   return SourceRange(beg, end);
8951 }
8952 
8953 static llvm::FixedPointSemantics
8954 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8955                         unsigned &Idx) {
8956   unsigned Width = Record[Idx++];
8957   unsigned Scale = Record[Idx++];
8958   uint64_t Tmp = Record[Idx++];
8959   bool IsSigned = Tmp & 0x1;
8960   bool IsSaturated = Tmp & 0x2;
8961   bool HasUnsignedPadding = Tmp & 0x4;
8962   return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8963                                    HasUnsignedPadding);
8964 }
8965 
8966 APValue ASTRecordReader::readAPValue() {
8967   auto Kind = static_cast<APValue::ValueKind>(asImpl().readUInt32());
8968   switch (Kind) {
8969   case APValue::None:
8970     return APValue();
8971   case APValue::Indeterminate:
8972     return APValue::IndeterminateValue();
8973   case APValue::Int:
8974     return APValue(asImpl().readAPSInt());
8975   case APValue::Float: {
8976     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
8977         static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32()));
8978     return APValue(asImpl().readAPFloat(FloatSema));
8979   }
8980   case APValue::FixedPoint: {
8981     llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8982     return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
8983   }
8984   case APValue::ComplexInt: {
8985     llvm::APSInt First = asImpl().readAPSInt();
8986     return APValue(std::move(First), asImpl().readAPSInt());
8987   }
8988   case APValue::ComplexFloat: {
8989     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
8990         static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32()));
8991     llvm::APFloat First = readAPFloat(FloatSema);
8992     return APValue(std::move(First), asImpl().readAPFloat(FloatSema));
8993   }
8994   case APValue::Vector: {
8995     APValue Result;
8996     Result.MakeVector();
8997     unsigned Length = asImpl().readUInt32();
8998     (void)Result.setVectorUninit(Length);
8999     for (unsigned LoopIdx = 0; LoopIdx < Length; LoopIdx++)
9000       Result.getVectorElt(LoopIdx) = asImpl().readAPValue();
9001     return Result;
9002   }
9003   case APValue::Array: {
9004     APValue Result;
9005     unsigned InitLength = asImpl().readUInt32();
9006     unsigned TotalLength = asImpl().readUInt32();
9007     Result.MakeArray(InitLength, TotalLength);
9008     for (unsigned LoopIdx = 0; LoopIdx < InitLength; LoopIdx++)
9009       Result.getArrayInitializedElt(LoopIdx) = asImpl().readAPValue();
9010     return Result;
9011   }
9012   case APValue::Struct: {
9013     APValue Result;
9014     unsigned BasesLength = asImpl().readUInt32();
9015     unsigned FieldsLength = asImpl().readUInt32();
9016     Result.MakeStruct(BasesLength, FieldsLength);
9017     for (unsigned LoopIdx = 0; LoopIdx < BasesLength; LoopIdx++)
9018       Result.getStructBase(LoopIdx) = asImpl().readAPValue();
9019     for (unsigned LoopIdx = 0; LoopIdx < FieldsLength; LoopIdx++)
9020       Result.getStructField(LoopIdx) = asImpl().readAPValue();
9021     return Result;
9022   }
9023   case APValue::Union: {
9024     auto *FDecl = asImpl().readDeclAs<FieldDecl>();
9025     APValue Value = asImpl().readAPValue();
9026     return APValue(FDecl, std::move(Value));
9027   }
9028   case APValue::AddrLabelDiff: {
9029     auto *LHS = cast<AddrLabelExpr>(asImpl().readExpr());
9030     auto *RHS = cast<AddrLabelExpr>(asImpl().readExpr());
9031     return APValue(LHS, RHS);
9032   }
9033   case APValue::MemberPointer: {
9034     APValue Result;
9035     bool IsDerived = asImpl().readUInt32();
9036     auto *Member = asImpl().readDeclAs<ValueDecl>();
9037     unsigned PathSize = asImpl().readUInt32();
9038     const CXXRecordDecl **PathArray =
9039         Result.setMemberPointerUninit(Member, IsDerived, PathSize).data();
9040     for (unsigned LoopIdx = 0; LoopIdx < PathSize; LoopIdx++)
9041       PathArray[LoopIdx] =
9042           asImpl().readDeclAs<const CXXRecordDecl>()->getCanonicalDecl();
9043     return Result;
9044   }
9045   case APValue::LValue: {
9046     uint64_t Bits = asImpl().readUInt32();
9047     bool HasLValuePath = Bits & 0x1;
9048     bool IsLValueOnePastTheEnd = Bits & 0x2;
9049     bool IsExpr = Bits & 0x4;
9050     bool IsTypeInfo = Bits & 0x8;
9051     bool IsNullPtr = Bits & 0x10;
9052     bool HasBase = Bits & 0x20;
9053     APValue::LValueBase Base;
9054     QualType ElemTy;
9055     assert((!IsExpr || !IsTypeInfo) && "LValueBase cannot be both");
9056     if (HasBase) {
9057       if (!IsTypeInfo) {
9058         unsigned CallIndex = asImpl().readUInt32();
9059         unsigned Version = asImpl().readUInt32();
9060         if (IsExpr) {
9061           Base = APValue::LValueBase(asImpl().readExpr(), CallIndex, Version);
9062           ElemTy = Base.get<const Expr *>()->getType();
9063         } else {
9064           Base = APValue::LValueBase(asImpl().readDeclAs<const ValueDecl>(),
9065                                      CallIndex, Version);
9066           ElemTy = Base.get<const ValueDecl *>()->getType();
9067         }
9068       } else {
9069         QualType TypeInfo = asImpl().readType();
9070         QualType Type = asImpl().readType();
9071         Base = APValue::LValueBase::getTypeInfo(
9072             TypeInfoLValue(TypeInfo.getTypePtr()), Type);
9073         Base.getTypeInfoType();
9074       }
9075     }
9076     CharUnits Offset = CharUnits::fromQuantity(asImpl().readUInt32());
9077     unsigned PathLength = asImpl().readUInt32();
9078     APValue Result;
9079     Result.MakeLValue();
9080     if (HasLValuePath) {
9081       APValue::LValuePathEntry *Path =
9082           Result
9083               .setLValueUninit(Base, Offset, PathLength, IsLValueOnePastTheEnd,
9084                                IsNullPtr)
9085               .data();
9086       for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) {
9087         if (ElemTy->getAs<RecordType>()) {
9088           unsigned Int = asImpl().readUInt32();
9089           Decl *D = asImpl().readDeclAs<Decl>();
9090           if (auto *RD = dyn_cast<CXXRecordDecl>(D))
9091             ElemTy = getASTContext().getRecordType(RD);
9092           else
9093             ElemTy = cast<ValueDecl>(D)->getType();
9094           Path[LoopIdx] =
9095               APValue::LValuePathEntry(APValue::BaseOrMemberType(D, Int));
9096         } else {
9097           ElemTy = getASTContext().getAsArrayType(ElemTy)->getElementType();
9098           Path[LoopIdx] =
9099               APValue::LValuePathEntry::ArrayIndex(asImpl().readUInt32());
9100         }
9101       }
9102     } else
9103       Result.setLValue(Base, Offset, APValue::NoLValuePath{}, IsNullPtr);
9104     return Result;
9105   }
9106   }
9107   llvm_unreachable("Invalid APValue::ValueKind");
9108 }
9109 
9110 /// Read a floating-point value
9111 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9112   return llvm::APFloat(Sem, readAPInt());
9113 }
9114 
9115 // Read a string
9116 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9117   unsigned Len = Record[Idx++];
9118   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9119   Idx += Len;
9120   return Result;
9121 }
9122 
9123 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9124                                 unsigned &Idx) {
9125   std::string Filename = ReadString(Record, Idx);
9126   ResolveImportedPath(F, Filename);
9127   return Filename;
9128 }
9129 
9130 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9131                                 const RecordData &Record, unsigned &Idx) {
9132   std::string Filename = ReadString(Record, Idx);
9133   if (!BaseDirectory.empty())
9134     ResolveImportedPath(Filename, BaseDirectory);
9135   return Filename;
9136 }
9137 
9138 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9139                                          unsigned &Idx) {
9140   unsigned Major = Record[Idx++];
9141   unsigned Minor = Record[Idx++];
9142   unsigned Subminor = Record[Idx++];
9143   if (Minor == 0)
9144     return VersionTuple(Major);
9145   if (Subminor == 0)
9146     return VersionTuple(Major, Minor - 1);
9147   return VersionTuple(Major, Minor - 1, Subminor - 1);
9148 }
9149 
9150 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9151                                           const RecordData &Record,
9152                                           unsigned &Idx) {
9153   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9154   return CXXTemporary::Create(getContext(), Decl);
9155 }
9156 
9157 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9158   return Diag(CurrentImportLoc, DiagID);
9159 }
9160 
9161 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9162   return Diags.Report(Loc, DiagID);
9163 }
9164 
9165 /// Retrieve the identifier table associated with the
9166 /// preprocessor.
9167 IdentifierTable &ASTReader::getIdentifierTable() {
9168   return PP.getIdentifierTable();
9169 }
9170 
9171 /// Record that the given ID maps to the given switch-case
9172 /// statement.
9173 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9174   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9175          "Already have a SwitchCase with this ID");
9176   (*CurrSwitchCaseStmts)[ID] = SC;
9177 }
9178 
9179 /// Retrieve the switch-case statement with the given ID.
9180 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9181   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9182   return (*CurrSwitchCaseStmts)[ID];
9183 }
9184 
9185 void ASTReader::ClearSwitchCaseIDs() {
9186   CurrSwitchCaseStmts->clear();
9187 }
9188 
9189 void ASTReader::ReadComments() {
9190   ASTContext &Context = getContext();
9191   std::vector<RawComment *> Comments;
9192   for (SmallVectorImpl<std::pair<BitstreamCursor,
9193                                  serialization::ModuleFile *>>::iterator
9194        I = CommentsCursors.begin(),
9195        E = CommentsCursors.end();
9196        I != E; ++I) {
9197     Comments.clear();
9198     BitstreamCursor &Cursor = I->first;
9199     serialization::ModuleFile &F = *I->second;
9200     SavedStreamPosition SavedPosition(Cursor);
9201 
9202     RecordData Record;
9203     while (true) {
9204       Expected<llvm::BitstreamEntry> MaybeEntry =
9205           Cursor.advanceSkippingSubblocks(
9206               BitstreamCursor::AF_DontPopBlockAtEnd);
9207       if (!MaybeEntry) {
9208         Error(MaybeEntry.takeError());
9209         return;
9210       }
9211       llvm::BitstreamEntry Entry = MaybeEntry.get();
9212 
9213       switch (Entry.Kind) {
9214       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9215       case llvm::BitstreamEntry::Error:
9216         Error("malformed block record in AST file");
9217         return;
9218       case llvm::BitstreamEntry::EndBlock:
9219         goto NextCursor;
9220       case llvm::BitstreamEntry::Record:
9221         // The interesting case.
9222         break;
9223       }
9224 
9225       // Read a record.
9226       Record.clear();
9227       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9228       if (!MaybeComment) {
9229         Error(MaybeComment.takeError());
9230         return;
9231       }
9232       switch ((CommentRecordTypes)MaybeComment.get()) {
9233       case COMMENTS_RAW_COMMENT: {
9234         unsigned Idx = 0;
9235         SourceRange SR = ReadSourceRange(F, Record, Idx);
9236         RawComment::CommentKind Kind =
9237             (RawComment::CommentKind) Record[Idx++];
9238         bool IsTrailingComment = Record[Idx++];
9239         bool IsAlmostTrailingComment = Record[Idx++];
9240         Comments.push_back(new (Context) RawComment(
9241             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9242         break;
9243       }
9244       }
9245     }
9246   NextCursor:
9247     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9248         FileToOffsetToComment;
9249     for (RawComment *C : Comments) {
9250       SourceLocation CommentLoc = C->getBeginLoc();
9251       if (CommentLoc.isValid()) {
9252         std::pair<FileID, unsigned> Loc =
9253             SourceMgr.getDecomposedLoc(CommentLoc);
9254         if (Loc.first.isValid())
9255           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9256       }
9257     }
9258   }
9259 }
9260 
9261 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9262                                 bool IncludeSystem, bool Complain,
9263                     llvm::function_ref<void(const serialization::InputFile &IF,
9264                                             bool isSystem)> Visitor) {
9265   unsigned NumUserInputs = MF.NumUserInputFiles;
9266   unsigned NumInputs = MF.InputFilesLoaded.size();
9267   assert(NumUserInputs <= NumInputs);
9268   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9269   for (unsigned I = 0; I < N; ++I) {
9270     bool IsSystem = I >= NumUserInputs;
9271     InputFile IF = getInputFile(MF, I+1, Complain);
9272     Visitor(IF, IsSystem);
9273   }
9274 }
9275 
9276 void ASTReader::visitTopLevelModuleMaps(
9277     serialization::ModuleFile &MF,
9278     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9279   unsigned NumInputs = MF.InputFilesLoaded.size();
9280   for (unsigned I = 0; I < NumInputs; ++I) {
9281     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9282     if (IFI.TopLevelModuleMap)
9283       // FIXME: This unnecessarily re-reads the InputFileInfo.
9284       if (auto *FE = getInputFile(MF, I + 1).getFile())
9285         Visitor(FE);
9286   }
9287 }
9288 
9289 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9290   // If we know the owning module, use it.
9291   if (Module *M = D->getImportedOwningModule())
9292     return M->getFullModuleName();
9293 
9294   // Otherwise, use the name of the top-level module the decl is within.
9295   if (ModuleFile *M = getOwningModuleFile(D))
9296     return M->ModuleName;
9297 
9298   // Not from a module.
9299   return {};
9300 }
9301 
9302 void ASTReader::finishPendingActions() {
9303   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9304          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9305          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9306          !PendingUpdateRecords.empty()) {
9307     // If any identifiers with corresponding top-level declarations have
9308     // been loaded, load those declarations now.
9309     using TopLevelDeclsMap =
9310         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9311     TopLevelDeclsMap TopLevelDecls;
9312 
9313     while (!PendingIdentifierInfos.empty()) {
9314       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9315       SmallVector<uint32_t, 4> DeclIDs =
9316           std::move(PendingIdentifierInfos.back().second);
9317       PendingIdentifierInfos.pop_back();
9318 
9319       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9320     }
9321 
9322     // Load each function type that we deferred loading because it was a
9323     // deduced type that might refer to a local type declared within itself.
9324     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9325       auto *FD = PendingFunctionTypes[I].first;
9326       FD->setType(GetType(PendingFunctionTypes[I].second));
9327 
9328       // If we gave a function a deduced return type, remember that we need to
9329       // propagate that along the redeclaration chain.
9330       auto *DT = FD->getReturnType()->getContainedDeducedType();
9331       if (DT && DT->isDeduced())
9332         PendingDeducedTypeUpdates.insert(
9333             {FD->getCanonicalDecl(), FD->getReturnType()});
9334     }
9335     PendingFunctionTypes.clear();
9336 
9337     // For each decl chain that we wanted to complete while deserializing, mark
9338     // it as "still needs to be completed".
9339     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9340       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9341     }
9342     PendingIncompleteDeclChains.clear();
9343 
9344     // Load pending declaration chains.
9345     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9346       loadPendingDeclChain(PendingDeclChains[I].first,
9347                            PendingDeclChains[I].second);
9348     PendingDeclChains.clear();
9349 
9350     // Make the most recent of the top-level declarations visible.
9351     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9352            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9353       IdentifierInfo *II = TLD->first;
9354       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9355         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9356       }
9357     }
9358 
9359     // Load any pending macro definitions.
9360     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9361       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9362       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9363       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9364       // Initialize the macro history from chained-PCHs ahead of module imports.
9365       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9366            ++IDIdx) {
9367         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9368         if (!Info.M->isModule())
9369           resolvePendingMacro(II, Info);
9370       }
9371       // Handle module imports.
9372       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9373            ++IDIdx) {
9374         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9375         if (Info.M->isModule())
9376           resolvePendingMacro(II, Info);
9377       }
9378     }
9379     PendingMacroIDs.clear();
9380 
9381     // Wire up the DeclContexts for Decls that we delayed setting until
9382     // recursive loading is completed.
9383     while (!PendingDeclContextInfos.empty()) {
9384       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9385       PendingDeclContextInfos.pop_front();
9386       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9387       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9388       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9389     }
9390 
9391     // Perform any pending declaration updates.
9392     while (!PendingUpdateRecords.empty()) {
9393       auto Update = PendingUpdateRecords.pop_back_val();
9394       ReadingKindTracker ReadingKind(Read_Decl, *this);
9395       loadDeclUpdateRecords(Update);
9396     }
9397   }
9398 
9399   // At this point, all update records for loaded decls are in place, so any
9400   // fake class definitions should have become real.
9401   assert(PendingFakeDefinitionData.empty() &&
9402          "faked up a class definition but never saw the real one");
9403 
9404   // If we deserialized any C++ or Objective-C class definitions, any
9405   // Objective-C protocol definitions, or any redeclarable templates, make sure
9406   // that all redeclarations point to the definitions. Note that this can only
9407   // happen now, after the redeclaration chains have been fully wired.
9408   for (Decl *D : PendingDefinitions) {
9409     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9410       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9411         // Make sure that the TagType points at the definition.
9412         const_cast<TagType*>(TagT)->decl = TD;
9413       }
9414 
9415       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9416         for (auto *R = getMostRecentExistingDecl(RD); R;
9417              R = R->getPreviousDecl()) {
9418           assert((R == D) ==
9419                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9420                  "declaration thinks it's the definition but it isn't");
9421           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9422         }
9423       }
9424 
9425       continue;
9426     }
9427 
9428     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9429       // Make sure that the ObjCInterfaceType points at the definition.
9430       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9431         ->Decl = ID;
9432 
9433       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9434         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9435 
9436       continue;
9437     }
9438 
9439     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9440       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9441         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9442 
9443       continue;
9444     }
9445 
9446     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9447     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9448       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9449   }
9450   PendingDefinitions.clear();
9451 
9452   // Load the bodies of any functions or methods we've encountered. We do
9453   // this now (delayed) so that we can be sure that the declaration chains
9454   // have been fully wired up (hasBody relies on this).
9455   // FIXME: We shouldn't require complete redeclaration chains here.
9456   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9457                                PBEnd = PendingBodies.end();
9458        PB != PBEnd; ++PB) {
9459     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9460       // For a function defined inline within a class template, force the
9461       // canonical definition to be the one inside the canonical definition of
9462       // the template. This ensures that we instantiate from a correct view
9463       // of the template.
9464       //
9465       // Sadly we can't do this more generally: we can't be sure that all
9466       // copies of an arbitrary class definition will have the same members
9467       // defined (eg, some member functions may not be instantiated, and some
9468       // special members may or may not have been implicitly defined).
9469       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9470         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9471           continue;
9472 
9473       // FIXME: Check for =delete/=default?
9474       // FIXME: Complain about ODR violations here?
9475       const FunctionDecl *Defn = nullptr;
9476       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9477         FD->setLazyBody(PB->second);
9478       } else {
9479         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9480         mergeDefinitionVisibility(NonConstDefn, FD);
9481 
9482         if (!FD->isLateTemplateParsed() &&
9483             !NonConstDefn->isLateTemplateParsed() &&
9484             FD->getODRHash() != NonConstDefn->getODRHash()) {
9485           if (!isa<CXXMethodDecl>(FD)) {
9486             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9487           } else if (FD->getLexicalParent()->isFileContext() &&
9488                      NonConstDefn->getLexicalParent()->isFileContext()) {
9489             // Only diagnose out-of-line method definitions.  If they are
9490             // in class definitions, then an error will be generated when
9491             // processing the class bodies.
9492             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9493           }
9494         }
9495       }
9496       continue;
9497     }
9498 
9499     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9500     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9501       MD->setLazyBody(PB->second);
9502   }
9503   PendingBodies.clear();
9504 
9505   // Do some cleanup.
9506   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9507     getContext().deduplicateMergedDefinitonsFor(ND);
9508   PendingMergedDefinitionsToDeduplicate.clear();
9509 }
9510 
9511 void ASTReader::diagnoseOdrViolations() {
9512   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9513       PendingFunctionOdrMergeFailures.empty() &&
9514       PendingEnumOdrMergeFailures.empty())
9515     return;
9516 
9517   // Trigger the import of the full definition of each class that had any
9518   // odr-merging problems, so we can produce better diagnostics for them.
9519   // These updates may in turn find and diagnose some ODR failures, so take
9520   // ownership of the set first.
9521   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9522   PendingOdrMergeFailures.clear();
9523   for (auto &Merge : OdrMergeFailures) {
9524     Merge.first->buildLookup();
9525     Merge.first->decls_begin();
9526     Merge.first->bases_begin();
9527     Merge.first->vbases_begin();
9528     for (auto &RecordPair : Merge.second) {
9529       auto *RD = RecordPair.first;
9530       RD->decls_begin();
9531       RD->bases_begin();
9532       RD->vbases_begin();
9533     }
9534   }
9535 
9536   // Trigger the import of functions.
9537   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9538   PendingFunctionOdrMergeFailures.clear();
9539   for (auto &Merge : FunctionOdrMergeFailures) {
9540     Merge.first->buildLookup();
9541     Merge.first->decls_begin();
9542     Merge.first->getBody();
9543     for (auto &FD : Merge.second) {
9544       FD->buildLookup();
9545       FD->decls_begin();
9546       FD->getBody();
9547     }
9548   }
9549 
9550   // Trigger the import of enums.
9551   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9552   PendingEnumOdrMergeFailures.clear();
9553   for (auto &Merge : EnumOdrMergeFailures) {
9554     Merge.first->decls_begin();
9555     for (auto &Enum : Merge.second) {
9556       Enum->decls_begin();
9557     }
9558   }
9559 
9560   // For each declaration from a merged context, check that the canonical
9561   // definition of that context also contains a declaration of the same
9562   // entity.
9563   //
9564   // Caution: this loop does things that might invalidate iterators into
9565   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9566   while (!PendingOdrMergeChecks.empty()) {
9567     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9568 
9569     // FIXME: Skip over implicit declarations for now. This matters for things
9570     // like implicitly-declared special member functions. This isn't entirely
9571     // correct; we can end up with multiple unmerged declarations of the same
9572     // implicit entity.
9573     if (D->isImplicit())
9574       continue;
9575 
9576     DeclContext *CanonDef = D->getDeclContext();
9577 
9578     bool Found = false;
9579     const Decl *DCanon = D->getCanonicalDecl();
9580 
9581     for (auto RI : D->redecls()) {
9582       if (RI->getLexicalDeclContext() == CanonDef) {
9583         Found = true;
9584         break;
9585       }
9586     }
9587     if (Found)
9588       continue;
9589 
9590     // Quick check failed, time to do the slow thing. Note, we can't just
9591     // look up the name of D in CanonDef here, because the member that is
9592     // in CanonDef might not be found by name lookup (it might have been
9593     // replaced by a more recent declaration in the lookup table), and we
9594     // can't necessarily find it in the redeclaration chain because it might
9595     // be merely mergeable, not redeclarable.
9596     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9597     for (auto *CanonMember : CanonDef->decls()) {
9598       if (CanonMember->getCanonicalDecl() == DCanon) {
9599         // This can happen if the declaration is merely mergeable and not
9600         // actually redeclarable (we looked for redeclarations earlier).
9601         //
9602         // FIXME: We should be able to detect this more efficiently, without
9603         // pulling in all of the members of CanonDef.
9604         Found = true;
9605         break;
9606       }
9607       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9608         if (ND->getDeclName() == D->getDeclName())
9609           Candidates.push_back(ND);
9610     }
9611 
9612     if (!Found) {
9613       // The AST doesn't like TagDecls becoming invalid after they've been
9614       // completed. We only really need to mark FieldDecls as invalid here.
9615       if (!isa<TagDecl>(D))
9616         D->setInvalidDecl();
9617 
9618       // Ensure we don't accidentally recursively enter deserialization while
9619       // we're producing our diagnostic.
9620       Deserializing RecursionGuard(this);
9621 
9622       std::string CanonDefModule =
9623           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9624       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9625         << D << getOwningModuleNameForDiagnostic(D)
9626         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9627 
9628       if (Candidates.empty())
9629         Diag(cast<Decl>(CanonDef)->getLocation(),
9630              diag::note_module_odr_violation_no_possible_decls) << D;
9631       else {
9632         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9633           Diag(Candidates[I]->getLocation(),
9634                diag::note_module_odr_violation_possible_decl)
9635             << Candidates[I];
9636       }
9637 
9638       DiagnosedOdrMergeFailures.insert(CanonDef);
9639     }
9640   }
9641 
9642   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9643       EnumOdrMergeFailures.empty())
9644     return;
9645 
9646   // Ensure we don't accidentally recursively enter deserialization while
9647   // we're producing our diagnostics.
9648   Deserializing RecursionGuard(this);
9649 
9650   // Common code for hashing helpers.
9651   ODRHash Hash;
9652   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9653     Hash.clear();
9654     Hash.AddQualType(Ty);
9655     return Hash.CalculateHash();
9656   };
9657 
9658   auto ComputeODRHash = [&Hash](const Stmt *S) {
9659     assert(S);
9660     Hash.clear();
9661     Hash.AddStmt(S);
9662     return Hash.CalculateHash();
9663   };
9664 
9665   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9666     assert(D);
9667     Hash.clear();
9668     Hash.AddSubDecl(D);
9669     return Hash.CalculateHash();
9670   };
9671 
9672   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9673     Hash.clear();
9674     Hash.AddTemplateArgument(TA);
9675     return Hash.CalculateHash();
9676   };
9677 
9678   auto ComputeTemplateParameterListODRHash =
9679       [&Hash](const TemplateParameterList *TPL) {
9680         assert(TPL);
9681         Hash.clear();
9682         Hash.AddTemplateParameterList(TPL);
9683         return Hash.CalculateHash();
9684       };
9685 
9686   // Used with err_module_odr_violation_mismatch_decl and
9687   // note_module_odr_violation_mismatch_decl
9688   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9689   enum ODRMismatchDecl {
9690     EndOfClass,
9691     PublicSpecifer,
9692     PrivateSpecifer,
9693     ProtectedSpecifer,
9694     StaticAssert,
9695     Field,
9696     CXXMethod,
9697     TypeAlias,
9698     TypeDef,
9699     Var,
9700     Friend,
9701     FunctionTemplate,
9702     Other
9703   };
9704 
9705   // Used with err_module_odr_violation_mismatch_decl_diff and
9706   // note_module_odr_violation_mismatch_decl_diff
9707   enum ODRMismatchDeclDifference {
9708     StaticAssertCondition,
9709     StaticAssertMessage,
9710     StaticAssertOnlyMessage,
9711     FieldName,
9712     FieldTypeName,
9713     FieldSingleBitField,
9714     FieldDifferentWidthBitField,
9715     FieldSingleMutable,
9716     FieldSingleInitializer,
9717     FieldDifferentInitializers,
9718     MethodName,
9719     MethodDeleted,
9720     MethodDefaulted,
9721     MethodVirtual,
9722     MethodStatic,
9723     MethodVolatile,
9724     MethodConst,
9725     MethodInline,
9726     MethodNumberParameters,
9727     MethodParameterType,
9728     MethodParameterName,
9729     MethodParameterSingleDefaultArgument,
9730     MethodParameterDifferentDefaultArgument,
9731     MethodNoTemplateArguments,
9732     MethodDifferentNumberTemplateArguments,
9733     MethodDifferentTemplateArgument,
9734     MethodSingleBody,
9735     MethodDifferentBody,
9736     TypedefName,
9737     TypedefType,
9738     VarName,
9739     VarType,
9740     VarSingleInitializer,
9741     VarDifferentInitializer,
9742     VarConstexpr,
9743     FriendTypeFunction,
9744     FriendType,
9745     FriendFunction,
9746     FunctionTemplateDifferentNumberParameters,
9747     FunctionTemplateParameterDifferentKind,
9748     FunctionTemplateParameterName,
9749     FunctionTemplateParameterSingleDefaultArgument,
9750     FunctionTemplateParameterDifferentDefaultArgument,
9751     FunctionTemplateParameterDifferentType,
9752     FunctionTemplatePackParameter,
9753   };
9754 
9755   // These lambdas have the common portions of the ODR diagnostics.  This
9756   // has the same return as Diag(), so addition parameters can be passed
9757   // in with operator<<
9758   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9759                                  SourceLocation Loc, SourceRange Range,
9760                                  ODRMismatchDeclDifference DiffType) {
9761     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9762            << FirstRecord << FirstModule.empty() << FirstModule << Range
9763            << DiffType;
9764   };
9765   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9766                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9767     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9768            << SecondModule << Range << DiffType;
9769   };
9770 
9771   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9772                        &ComputeQualTypeODRHash, &ComputeODRHash](
9773                           NamedDecl *FirstRecord, StringRef FirstModule,
9774                           StringRef SecondModule, FieldDecl *FirstField,
9775                           FieldDecl *SecondField) {
9776     IdentifierInfo *FirstII = FirstField->getIdentifier();
9777     IdentifierInfo *SecondII = SecondField->getIdentifier();
9778     if (FirstII->getName() != SecondII->getName()) {
9779       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9780                        FirstField->getSourceRange(), FieldName)
9781           << FirstII;
9782       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9783                       SecondField->getSourceRange(), FieldName)
9784           << SecondII;
9785 
9786       return true;
9787     }
9788 
9789     assert(getContext().hasSameType(FirstField->getType(),
9790                                     SecondField->getType()));
9791 
9792     QualType FirstType = FirstField->getType();
9793     QualType SecondType = SecondField->getType();
9794     if (ComputeQualTypeODRHash(FirstType) !=
9795         ComputeQualTypeODRHash(SecondType)) {
9796       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9797                        FirstField->getSourceRange(), FieldTypeName)
9798           << FirstII << FirstType;
9799       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9800                       SecondField->getSourceRange(), FieldTypeName)
9801           << SecondII << SecondType;
9802 
9803       return true;
9804     }
9805 
9806     const bool IsFirstBitField = FirstField->isBitField();
9807     const bool IsSecondBitField = SecondField->isBitField();
9808     if (IsFirstBitField != IsSecondBitField) {
9809       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9810                        FirstField->getSourceRange(), FieldSingleBitField)
9811           << FirstII << IsFirstBitField;
9812       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9813                       SecondField->getSourceRange(), FieldSingleBitField)
9814           << SecondII << IsSecondBitField;
9815       return true;
9816     }
9817 
9818     if (IsFirstBitField && IsSecondBitField) {
9819       unsigned FirstBitWidthHash =
9820           ComputeODRHash(FirstField->getBitWidth());
9821       unsigned SecondBitWidthHash =
9822           ComputeODRHash(SecondField->getBitWidth());
9823       if (FirstBitWidthHash != SecondBitWidthHash) {
9824         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9825                          FirstField->getSourceRange(),
9826                          FieldDifferentWidthBitField)
9827             << FirstII << FirstField->getBitWidth()->getSourceRange();
9828         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9829                         SecondField->getSourceRange(),
9830                         FieldDifferentWidthBitField)
9831             << SecondII << SecondField->getBitWidth()->getSourceRange();
9832         return true;
9833       }
9834     }
9835 
9836     if (!PP.getLangOpts().CPlusPlus)
9837       return false;
9838 
9839     const bool IsFirstMutable = FirstField->isMutable();
9840     const bool IsSecondMutable = SecondField->isMutable();
9841     if (IsFirstMutable != IsSecondMutable) {
9842       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9843                        FirstField->getSourceRange(), FieldSingleMutable)
9844           << FirstII << IsFirstMutable;
9845       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9846                       SecondField->getSourceRange(), FieldSingleMutable)
9847           << SecondII << IsSecondMutable;
9848       return true;
9849     }
9850 
9851     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9852     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9853     if ((!FirstInitializer && SecondInitializer) ||
9854         (FirstInitializer && !SecondInitializer)) {
9855       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9856                        FirstField->getSourceRange(), FieldSingleInitializer)
9857           << FirstII << (FirstInitializer != nullptr);
9858       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9859                       SecondField->getSourceRange(), FieldSingleInitializer)
9860           << SecondII << (SecondInitializer != nullptr);
9861       return true;
9862     }
9863 
9864     if (FirstInitializer && SecondInitializer) {
9865       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9866       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9867       if (FirstInitHash != SecondInitHash) {
9868         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9869                          FirstField->getSourceRange(),
9870                          FieldDifferentInitializers)
9871             << FirstII << FirstInitializer->getSourceRange();
9872         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9873                         SecondField->getSourceRange(),
9874                         FieldDifferentInitializers)
9875             << SecondII << SecondInitializer->getSourceRange();
9876         return true;
9877       }
9878     }
9879 
9880     return false;
9881   };
9882 
9883   auto ODRDiagTypeDefOrAlias =
9884       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9885           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9886           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9887           bool IsTypeAlias) {
9888         auto FirstName = FirstTD->getDeclName();
9889         auto SecondName = SecondTD->getDeclName();
9890         if (FirstName != SecondName) {
9891           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9892                            FirstTD->getSourceRange(), TypedefName)
9893               << IsTypeAlias << FirstName;
9894           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9895                           SecondTD->getSourceRange(), TypedefName)
9896               << IsTypeAlias << SecondName;
9897           return true;
9898         }
9899 
9900         QualType FirstType = FirstTD->getUnderlyingType();
9901         QualType SecondType = SecondTD->getUnderlyingType();
9902         if (ComputeQualTypeODRHash(FirstType) !=
9903             ComputeQualTypeODRHash(SecondType)) {
9904           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9905                            FirstTD->getSourceRange(), TypedefType)
9906               << IsTypeAlias << FirstName << FirstType;
9907           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9908                           SecondTD->getSourceRange(), TypedefType)
9909               << IsTypeAlias << SecondName << SecondType;
9910           return true;
9911         }
9912 
9913         return false;
9914   };
9915 
9916   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9917                      &ComputeQualTypeODRHash, &ComputeODRHash,
9918                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9919                            StringRef SecondModule, VarDecl *FirstVD,
9920                            VarDecl *SecondVD) {
9921     auto FirstName = FirstVD->getDeclName();
9922     auto SecondName = SecondVD->getDeclName();
9923     if (FirstName != SecondName) {
9924       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9925                        FirstVD->getSourceRange(), VarName)
9926           << FirstName;
9927       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9928                       SecondVD->getSourceRange(), VarName)
9929           << SecondName;
9930       return true;
9931     }
9932 
9933     QualType FirstType = FirstVD->getType();
9934     QualType SecondType = SecondVD->getType();
9935     if (ComputeQualTypeODRHash(FirstType) !=
9936         ComputeQualTypeODRHash(SecondType)) {
9937       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9938                        FirstVD->getSourceRange(), VarType)
9939           << FirstName << FirstType;
9940       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9941                       SecondVD->getSourceRange(), VarType)
9942           << SecondName << SecondType;
9943       return true;
9944     }
9945 
9946     if (!PP.getLangOpts().CPlusPlus)
9947       return false;
9948 
9949     const Expr *FirstInit = FirstVD->getInit();
9950     const Expr *SecondInit = SecondVD->getInit();
9951     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9952       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9953                        FirstVD->getSourceRange(), VarSingleInitializer)
9954           << FirstName << (FirstInit == nullptr)
9955           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9956       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9957                       SecondVD->getSourceRange(), VarSingleInitializer)
9958           << SecondName << (SecondInit == nullptr)
9959           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9960       return true;
9961     }
9962 
9963     if (FirstInit && SecondInit &&
9964         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9965       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9966                        FirstVD->getSourceRange(), VarDifferentInitializer)
9967           << FirstName << FirstInit->getSourceRange();
9968       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9969                       SecondVD->getSourceRange(), VarDifferentInitializer)
9970           << SecondName << SecondInit->getSourceRange();
9971       return true;
9972     }
9973 
9974     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9975     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9976     if (FirstIsConstexpr != SecondIsConstexpr) {
9977       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9978                        FirstVD->getSourceRange(), VarConstexpr)
9979           << FirstName << FirstIsConstexpr;
9980       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9981                       SecondVD->getSourceRange(), VarConstexpr)
9982           << SecondName << SecondIsConstexpr;
9983       return true;
9984     }
9985     return false;
9986   };
9987 
9988   auto DifferenceSelector = [](Decl *D) {
9989     assert(D && "valid Decl required");
9990     switch (D->getKind()) {
9991     default:
9992       return Other;
9993     case Decl::AccessSpec:
9994       switch (D->getAccess()) {
9995       case AS_public:
9996         return PublicSpecifer;
9997       case AS_private:
9998         return PrivateSpecifer;
9999       case AS_protected:
10000         return ProtectedSpecifer;
10001       case AS_none:
10002         break;
10003       }
10004       llvm_unreachable("Invalid access specifier");
10005     case Decl::StaticAssert:
10006       return StaticAssert;
10007     case Decl::Field:
10008       return Field;
10009     case Decl::CXXMethod:
10010     case Decl::CXXConstructor:
10011     case Decl::CXXDestructor:
10012       return CXXMethod;
10013     case Decl::TypeAlias:
10014       return TypeAlias;
10015     case Decl::Typedef:
10016       return TypeDef;
10017     case Decl::Var:
10018       return Var;
10019     case Decl::Friend:
10020       return Friend;
10021     case Decl::FunctionTemplate:
10022       return FunctionTemplate;
10023     }
10024   };
10025 
10026   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10027   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10028                                                  RecordDecl *Record,
10029                                                  const DeclContext *DC) {
10030     for (auto *D : Record->decls()) {
10031       if (!ODRHash::isDeclToBeProcessed(D, DC))
10032         continue;
10033       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10034     }
10035   };
10036 
10037   struct DiffResult {
10038     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
10039     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
10040   };
10041 
10042   // If there is a diagnoseable difference, FirstDiffType and
10043   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10044   // filled in if not EndOfClass.
10045   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
10046                                              DeclHashes &SecondHashes) {
10047     DiffResult DR;
10048     auto FirstIt = FirstHashes.begin();
10049     auto SecondIt = SecondHashes.begin();
10050     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10051       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10052           FirstIt->second == SecondIt->second) {
10053         ++FirstIt;
10054         ++SecondIt;
10055         continue;
10056       }
10057 
10058       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10059       DR.SecondDecl =
10060           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10061 
10062       DR.FirstDiffType =
10063           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
10064       DR.SecondDiffType =
10065           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
10066       return DR;
10067     }
10068     return DR;
10069   };
10070 
10071   // Use this to diagnose that an unexpected Decl was encountered
10072   // or no difference was detected. This causes a generic error
10073   // message to be emitted.
10074   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
10075                                       StringRef FirstModule,
10076                                       NamedDecl *SecondRecord,
10077                                       StringRef SecondModule) {
10078     Diag(FirstRecord->getLocation(),
10079          diag::err_module_odr_violation_different_definitions)
10080         << FirstRecord << FirstModule.empty() << FirstModule;
10081 
10082     if (DR.FirstDecl) {
10083       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
10084           << FirstRecord << DR.FirstDecl->getSourceRange();
10085     }
10086 
10087     Diag(SecondRecord->getLocation(),
10088          diag::note_module_odr_violation_different_definitions)
10089         << SecondModule;
10090 
10091     if (DR.SecondDecl) {
10092       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
10093           << DR.SecondDecl->getSourceRange();
10094     }
10095   };
10096 
10097   auto DiagnoseODRMismatch =
10098       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10099              NamedDecl *SecondRecord, StringRef SecondModule) {
10100         SourceLocation FirstLoc;
10101         SourceRange FirstRange;
10102         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10103         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10104           FirstLoc = FirstTag->getBraceRange().getEnd();
10105         } else {
10106           FirstLoc = DR.FirstDecl->getLocation();
10107           FirstRange = DR.FirstDecl->getSourceRange();
10108         }
10109         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10110             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10111             << DR.FirstDiffType;
10112 
10113         SourceLocation SecondLoc;
10114         SourceRange SecondRange;
10115         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10116         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10117           SecondLoc = SecondTag->getBraceRange().getEnd();
10118         } else {
10119           SecondLoc = DR.SecondDecl->getLocation();
10120           SecondRange = DR.SecondDecl->getSourceRange();
10121         }
10122         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10123             << SecondModule << SecondRange << DR.SecondDiffType;
10124       };
10125 
10126   // Issue any pending ODR-failure diagnostics.
10127   for (auto &Merge : OdrMergeFailures) {
10128     // If we've already pointed out a specific problem with this class, don't
10129     // bother issuing a general "something's different" diagnostic.
10130     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10131       continue;
10132 
10133     bool Diagnosed = false;
10134     CXXRecordDecl *FirstRecord = Merge.first;
10135     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10136     for (auto &RecordPair : Merge.second) {
10137       CXXRecordDecl *SecondRecord = RecordPair.first;
10138       // Multiple different declarations got merged together; tell the user
10139       // where they came from.
10140       if (FirstRecord == SecondRecord)
10141         continue;
10142 
10143       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10144 
10145       auto *FirstDD = FirstRecord->DefinitionData;
10146       auto *SecondDD = RecordPair.second;
10147 
10148       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10149 
10150       // Diagnostics from DefinitionData are emitted here.
10151       if (FirstDD != SecondDD) {
10152         enum ODRDefinitionDataDifference {
10153           NumBases,
10154           NumVBases,
10155           BaseType,
10156           BaseVirtual,
10157           BaseAccess,
10158         };
10159         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10160                                  this](SourceLocation Loc, SourceRange Range,
10161                                        ODRDefinitionDataDifference DiffType) {
10162           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10163                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10164                  << DiffType;
10165         };
10166         auto ODRDiagBaseNote = [&SecondModule,
10167                                 this](SourceLocation Loc, SourceRange Range,
10168                                       ODRDefinitionDataDifference DiffType) {
10169           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10170                  << SecondModule << Range << DiffType;
10171         };
10172 
10173         unsigned FirstNumBases = FirstDD->NumBases;
10174         unsigned FirstNumVBases = FirstDD->NumVBases;
10175         unsigned SecondNumBases = SecondDD->NumBases;
10176         unsigned SecondNumVBases = SecondDD->NumVBases;
10177 
10178         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10179           unsigned NumBases = DD->NumBases;
10180           if (NumBases == 0) return SourceRange();
10181           auto bases = DD->bases();
10182           return SourceRange(bases[0].getBeginLoc(),
10183                              bases[NumBases - 1].getEndLoc());
10184         };
10185 
10186         if (FirstNumBases != SecondNumBases) {
10187           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10188                            NumBases)
10189               << FirstNumBases;
10190           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10191                           NumBases)
10192               << SecondNumBases;
10193           Diagnosed = true;
10194           break;
10195         }
10196 
10197         if (FirstNumVBases != SecondNumVBases) {
10198           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10199                            NumVBases)
10200               << FirstNumVBases;
10201           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10202                           NumVBases)
10203               << SecondNumVBases;
10204           Diagnosed = true;
10205           break;
10206         }
10207 
10208         auto FirstBases = FirstDD->bases();
10209         auto SecondBases = SecondDD->bases();
10210         unsigned i = 0;
10211         for (i = 0; i < FirstNumBases; ++i) {
10212           auto FirstBase = FirstBases[i];
10213           auto SecondBase = SecondBases[i];
10214           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10215               ComputeQualTypeODRHash(SecondBase.getType())) {
10216             ODRDiagBaseError(FirstRecord->getLocation(),
10217                              FirstBase.getSourceRange(), BaseType)
10218                 << (i + 1) << FirstBase.getType();
10219             ODRDiagBaseNote(SecondRecord->getLocation(),
10220                             SecondBase.getSourceRange(), BaseType)
10221                 << (i + 1) << SecondBase.getType();
10222             break;
10223           }
10224 
10225           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10226             ODRDiagBaseError(FirstRecord->getLocation(),
10227                              FirstBase.getSourceRange(), BaseVirtual)
10228                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10229             ODRDiagBaseNote(SecondRecord->getLocation(),
10230                             SecondBase.getSourceRange(), BaseVirtual)
10231                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10232             break;
10233           }
10234 
10235           if (FirstBase.getAccessSpecifierAsWritten() !=
10236               SecondBase.getAccessSpecifierAsWritten()) {
10237             ODRDiagBaseError(FirstRecord->getLocation(),
10238                              FirstBase.getSourceRange(), BaseAccess)
10239                 << (i + 1) << FirstBase.getType()
10240                 << (int)FirstBase.getAccessSpecifierAsWritten();
10241             ODRDiagBaseNote(SecondRecord->getLocation(),
10242                             SecondBase.getSourceRange(), BaseAccess)
10243                 << (i + 1) << SecondBase.getType()
10244                 << (int)SecondBase.getAccessSpecifierAsWritten();
10245             break;
10246           }
10247         }
10248 
10249         if (i != FirstNumBases) {
10250           Diagnosed = true;
10251           break;
10252         }
10253       }
10254 
10255       const ClassTemplateDecl *FirstTemplate =
10256           FirstRecord->getDescribedClassTemplate();
10257       const ClassTemplateDecl *SecondTemplate =
10258           SecondRecord->getDescribedClassTemplate();
10259 
10260       assert(!FirstTemplate == !SecondTemplate &&
10261              "Both pointers should be null or non-null");
10262 
10263       enum ODRTemplateDifference {
10264         ParamEmptyName,
10265         ParamName,
10266         ParamSingleDefaultArgument,
10267         ParamDifferentDefaultArgument,
10268       };
10269 
10270       if (FirstTemplate && SecondTemplate) {
10271         DeclHashes FirstTemplateHashes;
10272         DeclHashes SecondTemplateHashes;
10273 
10274         auto PopulateTemplateParameterHashs =
10275             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10276                                      const ClassTemplateDecl *TD) {
10277               for (auto *D : TD->getTemplateParameters()->asArray()) {
10278                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10279               }
10280             };
10281 
10282         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10283         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10284 
10285         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10286                "Number of template parameters should be equal.");
10287 
10288         auto FirstIt = FirstTemplateHashes.begin();
10289         auto FirstEnd = FirstTemplateHashes.end();
10290         auto SecondIt = SecondTemplateHashes.begin();
10291         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10292           if (FirstIt->second == SecondIt->second)
10293             continue;
10294 
10295           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10296                                           SourceLocation Loc, SourceRange Range,
10297                                           ODRTemplateDifference DiffType) {
10298             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10299                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10300                    << DiffType;
10301           };
10302           auto ODRDiagTemplateNote = [&SecondModule, this](
10303                                          SourceLocation Loc, SourceRange Range,
10304                                          ODRTemplateDifference DiffType) {
10305             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10306                    << SecondModule << Range << DiffType;
10307           };
10308 
10309           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10310           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10311 
10312           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10313                  "Parameter Decl's should be the same kind.");
10314 
10315           DeclarationName FirstName = FirstDecl->getDeclName();
10316           DeclarationName SecondName = SecondDecl->getDeclName();
10317 
10318           if (FirstName != SecondName) {
10319             const bool FirstNameEmpty =
10320                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10321             const bool SecondNameEmpty =
10322                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10323             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10324                    "Both template parameters cannot be unnamed.");
10325             ODRDiagTemplateError(FirstDecl->getLocation(),
10326                                  FirstDecl->getSourceRange(),
10327                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10328                 << FirstName;
10329             ODRDiagTemplateNote(SecondDecl->getLocation(),
10330                                 SecondDecl->getSourceRange(),
10331                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10332                 << SecondName;
10333             break;
10334           }
10335 
10336           switch (FirstDecl->getKind()) {
10337           default:
10338             llvm_unreachable("Invalid template parameter type.");
10339           case Decl::TemplateTypeParm: {
10340             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10341             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10342             const bool HasFirstDefaultArgument =
10343                 FirstParam->hasDefaultArgument() &&
10344                 !FirstParam->defaultArgumentWasInherited();
10345             const bool HasSecondDefaultArgument =
10346                 SecondParam->hasDefaultArgument() &&
10347                 !SecondParam->defaultArgumentWasInherited();
10348 
10349             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10350               ODRDiagTemplateError(FirstDecl->getLocation(),
10351                                    FirstDecl->getSourceRange(),
10352                                    ParamSingleDefaultArgument)
10353                   << HasFirstDefaultArgument;
10354               ODRDiagTemplateNote(SecondDecl->getLocation(),
10355                                   SecondDecl->getSourceRange(),
10356                                   ParamSingleDefaultArgument)
10357                   << HasSecondDefaultArgument;
10358               break;
10359             }
10360 
10361             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10362                    "Expecting default arguments.");
10363 
10364             ODRDiagTemplateError(FirstDecl->getLocation(),
10365                                  FirstDecl->getSourceRange(),
10366                                  ParamDifferentDefaultArgument);
10367             ODRDiagTemplateNote(SecondDecl->getLocation(),
10368                                 SecondDecl->getSourceRange(),
10369                                 ParamDifferentDefaultArgument);
10370 
10371             break;
10372           }
10373           case Decl::NonTypeTemplateParm: {
10374             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10375             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10376             const bool HasFirstDefaultArgument =
10377                 FirstParam->hasDefaultArgument() &&
10378                 !FirstParam->defaultArgumentWasInherited();
10379             const bool HasSecondDefaultArgument =
10380                 SecondParam->hasDefaultArgument() &&
10381                 !SecondParam->defaultArgumentWasInherited();
10382 
10383             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10384               ODRDiagTemplateError(FirstDecl->getLocation(),
10385                                    FirstDecl->getSourceRange(),
10386                                    ParamSingleDefaultArgument)
10387                   << HasFirstDefaultArgument;
10388               ODRDiagTemplateNote(SecondDecl->getLocation(),
10389                                   SecondDecl->getSourceRange(),
10390                                   ParamSingleDefaultArgument)
10391                   << HasSecondDefaultArgument;
10392               break;
10393             }
10394 
10395             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10396                    "Expecting default arguments.");
10397 
10398             ODRDiagTemplateError(FirstDecl->getLocation(),
10399                                  FirstDecl->getSourceRange(),
10400                                  ParamDifferentDefaultArgument);
10401             ODRDiagTemplateNote(SecondDecl->getLocation(),
10402                                 SecondDecl->getSourceRange(),
10403                                 ParamDifferentDefaultArgument);
10404 
10405             break;
10406           }
10407           case Decl::TemplateTemplateParm: {
10408             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10409             const auto *SecondParam =
10410                 cast<TemplateTemplateParmDecl>(SecondDecl);
10411             const bool HasFirstDefaultArgument =
10412                 FirstParam->hasDefaultArgument() &&
10413                 !FirstParam->defaultArgumentWasInherited();
10414             const bool HasSecondDefaultArgument =
10415                 SecondParam->hasDefaultArgument() &&
10416                 !SecondParam->defaultArgumentWasInherited();
10417 
10418             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10419               ODRDiagTemplateError(FirstDecl->getLocation(),
10420                                    FirstDecl->getSourceRange(),
10421                                    ParamSingleDefaultArgument)
10422                   << HasFirstDefaultArgument;
10423               ODRDiagTemplateNote(SecondDecl->getLocation(),
10424                                   SecondDecl->getSourceRange(),
10425                                   ParamSingleDefaultArgument)
10426                   << HasSecondDefaultArgument;
10427               break;
10428             }
10429 
10430             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10431                    "Expecting default arguments.");
10432 
10433             ODRDiagTemplateError(FirstDecl->getLocation(),
10434                                  FirstDecl->getSourceRange(),
10435                                  ParamDifferentDefaultArgument);
10436             ODRDiagTemplateNote(SecondDecl->getLocation(),
10437                                 SecondDecl->getSourceRange(),
10438                                 ParamDifferentDefaultArgument);
10439 
10440             break;
10441           }
10442           }
10443 
10444           break;
10445         }
10446 
10447         if (FirstIt != FirstEnd) {
10448           Diagnosed = true;
10449           break;
10450         }
10451       }
10452 
10453       DeclHashes FirstHashes;
10454       DeclHashes SecondHashes;
10455       const DeclContext *DC = FirstRecord;
10456       PopulateHashes(FirstHashes, FirstRecord, DC);
10457       PopulateHashes(SecondHashes, SecondRecord, DC);
10458 
10459       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10460       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10461       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10462       Decl *FirstDecl = DR.FirstDecl;
10463       Decl *SecondDecl = DR.SecondDecl;
10464 
10465       if (FirstDiffType == Other || SecondDiffType == Other) {
10466         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10467                               SecondModule);
10468         Diagnosed = true;
10469         break;
10470       }
10471 
10472       if (FirstDiffType != SecondDiffType) {
10473         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10474                             SecondModule);
10475         Diagnosed = true;
10476         break;
10477       }
10478 
10479       assert(FirstDiffType == SecondDiffType);
10480 
10481       switch (FirstDiffType) {
10482       case Other:
10483       case EndOfClass:
10484       case PublicSpecifer:
10485       case PrivateSpecifer:
10486       case ProtectedSpecifer:
10487         llvm_unreachable("Invalid diff type");
10488 
10489       case StaticAssert: {
10490         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10491         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10492 
10493         Expr *FirstExpr = FirstSA->getAssertExpr();
10494         Expr *SecondExpr = SecondSA->getAssertExpr();
10495         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10496         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10497         if (FirstODRHash != SecondODRHash) {
10498           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10499                            FirstExpr->getSourceRange(), StaticAssertCondition);
10500           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10501                           SecondExpr->getSourceRange(), StaticAssertCondition);
10502           Diagnosed = true;
10503           break;
10504         }
10505 
10506         StringLiteral *FirstStr = FirstSA->getMessage();
10507         StringLiteral *SecondStr = SecondSA->getMessage();
10508         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10509         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10510           SourceLocation FirstLoc, SecondLoc;
10511           SourceRange FirstRange, SecondRange;
10512           if (FirstStr) {
10513             FirstLoc = FirstStr->getBeginLoc();
10514             FirstRange = FirstStr->getSourceRange();
10515           } else {
10516             FirstLoc = FirstSA->getBeginLoc();
10517             FirstRange = FirstSA->getSourceRange();
10518           }
10519           if (SecondStr) {
10520             SecondLoc = SecondStr->getBeginLoc();
10521             SecondRange = SecondStr->getSourceRange();
10522           } else {
10523             SecondLoc = SecondSA->getBeginLoc();
10524             SecondRange = SecondSA->getSourceRange();
10525           }
10526           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10527                            StaticAssertOnlyMessage)
10528               << (FirstStr == nullptr);
10529           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10530                           StaticAssertOnlyMessage)
10531               << (SecondStr == nullptr);
10532           Diagnosed = true;
10533           break;
10534         }
10535 
10536         if (FirstStr && SecondStr &&
10537             FirstStr->getString() != SecondStr->getString()) {
10538           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10539                            FirstStr->getSourceRange(), StaticAssertMessage);
10540           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10541                           SecondStr->getSourceRange(), StaticAssertMessage);
10542           Diagnosed = true;
10543           break;
10544         }
10545         break;
10546       }
10547       case Field: {
10548         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10549                                  cast<FieldDecl>(FirstDecl),
10550                                  cast<FieldDecl>(SecondDecl));
10551         break;
10552       }
10553       case CXXMethod: {
10554         enum {
10555           DiagMethod,
10556           DiagConstructor,
10557           DiagDestructor,
10558         } FirstMethodType,
10559             SecondMethodType;
10560         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10561           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10562           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10563           return DiagMethod;
10564         };
10565         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10566         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10567         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10568         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10569         auto FirstName = FirstMethod->getDeclName();
10570         auto SecondName = SecondMethod->getDeclName();
10571         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10572           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10573                            FirstMethod->getSourceRange(), MethodName)
10574               << FirstMethodType << FirstName;
10575           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10576                           SecondMethod->getSourceRange(), MethodName)
10577               << SecondMethodType << SecondName;
10578 
10579           Diagnosed = true;
10580           break;
10581         }
10582 
10583         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10584         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10585         if (FirstDeleted != SecondDeleted) {
10586           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10587                            FirstMethod->getSourceRange(), MethodDeleted)
10588               << FirstMethodType << FirstName << FirstDeleted;
10589 
10590           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10591                           SecondMethod->getSourceRange(), MethodDeleted)
10592               << SecondMethodType << SecondName << SecondDeleted;
10593           Diagnosed = true;
10594           break;
10595         }
10596 
10597         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10598         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10599         if (FirstDefaulted != SecondDefaulted) {
10600           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10601                            FirstMethod->getSourceRange(), MethodDefaulted)
10602               << FirstMethodType << FirstName << FirstDefaulted;
10603 
10604           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10605                           SecondMethod->getSourceRange(), MethodDefaulted)
10606               << SecondMethodType << SecondName << SecondDefaulted;
10607           Diagnosed = true;
10608           break;
10609         }
10610 
10611         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10612         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10613         const bool FirstPure = FirstMethod->isPure();
10614         const bool SecondPure = SecondMethod->isPure();
10615         if ((FirstVirtual || SecondVirtual) &&
10616             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10617           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10618                            FirstMethod->getSourceRange(), MethodVirtual)
10619               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10620           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10621                           SecondMethod->getSourceRange(), MethodVirtual)
10622               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10623           Diagnosed = true;
10624           break;
10625         }
10626 
10627         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10628         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10629         // class needs to be checked instead.
10630         const auto FirstStorage = FirstMethod->getStorageClass();
10631         const auto SecondStorage = SecondMethod->getStorageClass();
10632         const bool FirstStatic = FirstStorage == SC_Static;
10633         const bool SecondStatic = SecondStorage == SC_Static;
10634         if (FirstStatic != SecondStatic) {
10635           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10636                            FirstMethod->getSourceRange(), MethodStatic)
10637               << FirstMethodType << FirstName << FirstStatic;
10638           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10639                           SecondMethod->getSourceRange(), MethodStatic)
10640               << SecondMethodType << SecondName << SecondStatic;
10641           Diagnosed = true;
10642           break;
10643         }
10644 
10645         const bool FirstVolatile = FirstMethod->isVolatile();
10646         const bool SecondVolatile = SecondMethod->isVolatile();
10647         if (FirstVolatile != SecondVolatile) {
10648           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10649                            FirstMethod->getSourceRange(), MethodVolatile)
10650               << FirstMethodType << FirstName << FirstVolatile;
10651           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10652                           SecondMethod->getSourceRange(), MethodVolatile)
10653               << SecondMethodType << SecondName << SecondVolatile;
10654           Diagnosed = true;
10655           break;
10656         }
10657 
10658         const bool FirstConst = FirstMethod->isConst();
10659         const bool SecondConst = SecondMethod->isConst();
10660         if (FirstConst != SecondConst) {
10661           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10662                            FirstMethod->getSourceRange(), MethodConst)
10663               << FirstMethodType << FirstName << FirstConst;
10664           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10665                           SecondMethod->getSourceRange(), MethodConst)
10666               << SecondMethodType << SecondName << SecondConst;
10667           Diagnosed = true;
10668           break;
10669         }
10670 
10671         const bool FirstInline = FirstMethod->isInlineSpecified();
10672         const bool SecondInline = SecondMethod->isInlineSpecified();
10673         if (FirstInline != SecondInline) {
10674           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10675                            FirstMethod->getSourceRange(), MethodInline)
10676               << FirstMethodType << FirstName << FirstInline;
10677           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10678                           SecondMethod->getSourceRange(), MethodInline)
10679               << SecondMethodType << SecondName << SecondInline;
10680           Diagnosed = true;
10681           break;
10682         }
10683 
10684         const unsigned FirstNumParameters = FirstMethod->param_size();
10685         const unsigned SecondNumParameters = SecondMethod->param_size();
10686         if (FirstNumParameters != SecondNumParameters) {
10687           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10688                            FirstMethod->getSourceRange(),
10689                            MethodNumberParameters)
10690               << FirstMethodType << FirstName << FirstNumParameters;
10691           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10692                           SecondMethod->getSourceRange(),
10693                           MethodNumberParameters)
10694               << SecondMethodType << SecondName << SecondNumParameters;
10695           Diagnosed = true;
10696           break;
10697         }
10698 
10699         // Need this status boolean to know when break out of the switch.
10700         bool ParameterMismatch = false;
10701         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10702           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10703           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10704 
10705           QualType FirstParamType = FirstParam->getType();
10706           QualType SecondParamType = SecondParam->getType();
10707           if (FirstParamType != SecondParamType &&
10708               ComputeQualTypeODRHash(FirstParamType) !=
10709                   ComputeQualTypeODRHash(SecondParamType)) {
10710             if (const DecayedType *ParamDecayedType =
10711                     FirstParamType->getAs<DecayedType>()) {
10712               ODRDiagDeclError(
10713                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10714                   FirstMethod->getSourceRange(), MethodParameterType)
10715                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10716                   << true << ParamDecayedType->getOriginalType();
10717             } else {
10718               ODRDiagDeclError(
10719                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10720                   FirstMethod->getSourceRange(), MethodParameterType)
10721                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10722                   << false;
10723             }
10724 
10725             if (const DecayedType *ParamDecayedType =
10726                     SecondParamType->getAs<DecayedType>()) {
10727               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10728                               SecondMethod->getSourceRange(),
10729                               MethodParameterType)
10730                   << SecondMethodType << SecondName << (I + 1)
10731                   << SecondParamType << true
10732                   << ParamDecayedType->getOriginalType();
10733             } else {
10734               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10735                               SecondMethod->getSourceRange(),
10736                               MethodParameterType)
10737                   << SecondMethodType << SecondName << (I + 1)
10738                   << SecondParamType << false;
10739             }
10740             ParameterMismatch = true;
10741             break;
10742           }
10743 
10744           DeclarationName FirstParamName = FirstParam->getDeclName();
10745           DeclarationName SecondParamName = SecondParam->getDeclName();
10746           if (FirstParamName != SecondParamName) {
10747             ODRDiagDeclError(FirstRecord, FirstModule,
10748                              FirstMethod->getLocation(),
10749                              FirstMethod->getSourceRange(), MethodParameterName)
10750                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10751             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10752                             SecondMethod->getSourceRange(), MethodParameterName)
10753                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10754             ParameterMismatch = true;
10755             break;
10756           }
10757 
10758           const Expr *FirstInit = FirstParam->getInit();
10759           const Expr *SecondInit = SecondParam->getInit();
10760           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10761             ODRDiagDeclError(FirstRecord, FirstModule,
10762                              FirstMethod->getLocation(),
10763                              FirstMethod->getSourceRange(),
10764                              MethodParameterSingleDefaultArgument)
10765                 << FirstMethodType << FirstName << (I + 1)
10766                 << (FirstInit == nullptr)
10767                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10768             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10769                             SecondMethod->getSourceRange(),
10770                             MethodParameterSingleDefaultArgument)
10771                 << SecondMethodType << SecondName << (I + 1)
10772                 << (SecondInit == nullptr)
10773                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10774             ParameterMismatch = true;
10775             break;
10776           }
10777 
10778           if (FirstInit && SecondInit &&
10779               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10780             ODRDiagDeclError(FirstRecord, FirstModule,
10781                              FirstMethod->getLocation(),
10782                              FirstMethod->getSourceRange(),
10783                              MethodParameterDifferentDefaultArgument)
10784                 << FirstMethodType << FirstName << (I + 1)
10785                 << FirstInit->getSourceRange();
10786             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10787                             SecondMethod->getSourceRange(),
10788                             MethodParameterDifferentDefaultArgument)
10789                 << SecondMethodType << SecondName << (I + 1)
10790                 << SecondInit->getSourceRange();
10791             ParameterMismatch = true;
10792             break;
10793 
10794           }
10795         }
10796 
10797         if (ParameterMismatch) {
10798           Diagnosed = true;
10799           break;
10800         }
10801 
10802         const auto *FirstTemplateArgs =
10803             FirstMethod->getTemplateSpecializationArgs();
10804         const auto *SecondTemplateArgs =
10805             SecondMethod->getTemplateSpecializationArgs();
10806 
10807         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10808             (!FirstTemplateArgs && SecondTemplateArgs)) {
10809           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10810                            FirstMethod->getSourceRange(),
10811                            MethodNoTemplateArguments)
10812               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10813           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10814                           SecondMethod->getSourceRange(),
10815                           MethodNoTemplateArguments)
10816               << SecondMethodType << SecondName
10817               << (SecondTemplateArgs != nullptr);
10818 
10819           Diagnosed = true;
10820           break;
10821         }
10822 
10823         if (FirstTemplateArgs && SecondTemplateArgs) {
10824           // Remove pack expansions from argument list.
10825           auto ExpandTemplateArgumentList =
10826               [](const TemplateArgumentList *TAL) {
10827                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10828                 for (const TemplateArgument &TA : TAL->asArray()) {
10829                   if (TA.getKind() != TemplateArgument::Pack) {
10830                     ExpandedList.push_back(&TA);
10831                     continue;
10832                   }
10833                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10834                     ExpandedList.push_back(&PackTA);
10835                   }
10836                 }
10837                 return ExpandedList;
10838               };
10839           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10840               ExpandTemplateArgumentList(FirstTemplateArgs);
10841           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10842               ExpandTemplateArgumentList(SecondTemplateArgs);
10843 
10844           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10845             ODRDiagDeclError(FirstRecord, FirstModule,
10846                              FirstMethod->getLocation(),
10847                              FirstMethod->getSourceRange(),
10848                              MethodDifferentNumberTemplateArguments)
10849                 << FirstMethodType << FirstName
10850                 << (unsigned)FirstExpandedList.size();
10851             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10852                             SecondMethod->getSourceRange(),
10853                             MethodDifferentNumberTemplateArguments)
10854                 << SecondMethodType << SecondName
10855                 << (unsigned)SecondExpandedList.size();
10856 
10857             Diagnosed = true;
10858             break;
10859           }
10860 
10861           bool TemplateArgumentMismatch = false;
10862           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10863             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10864                                    &SecondTA = *SecondExpandedList[i];
10865             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10866                 ComputeTemplateArgumentODRHash(SecondTA)) {
10867               continue;
10868             }
10869 
10870             ODRDiagDeclError(
10871                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10872                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10873                 << FirstMethodType << FirstName << FirstTA << i + 1;
10874             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10875                             SecondMethod->getSourceRange(),
10876                             MethodDifferentTemplateArgument)
10877                 << SecondMethodType << SecondName << SecondTA << i + 1;
10878 
10879             TemplateArgumentMismatch = true;
10880             break;
10881           }
10882 
10883           if (TemplateArgumentMismatch) {
10884             Diagnosed = true;
10885             break;
10886           }
10887         }
10888 
10889         // Compute the hash of the method as if it has no body.
10890         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10891           Hash.clear();
10892           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10893           return Hash.CalculateHash();
10894         };
10895 
10896         // Compare the hash generated to the hash stored.  A difference means
10897         // that a body was present in the original source.  Due to merging,
10898         // the stardard way of detecting a body will not work.
10899         const bool HasFirstBody =
10900             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10901         const bool HasSecondBody =
10902             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10903 
10904         if (HasFirstBody != HasSecondBody) {
10905           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10906                            FirstMethod->getSourceRange(), MethodSingleBody)
10907               << FirstMethodType << FirstName << HasFirstBody;
10908           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10909                           SecondMethod->getSourceRange(), MethodSingleBody)
10910               << SecondMethodType << SecondName << HasSecondBody;
10911           Diagnosed = true;
10912           break;
10913         }
10914 
10915         if (HasFirstBody && HasSecondBody) {
10916           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10917                            FirstMethod->getSourceRange(), MethodDifferentBody)
10918               << FirstMethodType << FirstName;
10919           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10920                           SecondMethod->getSourceRange(), MethodDifferentBody)
10921               << SecondMethodType << SecondName;
10922           Diagnosed = true;
10923           break;
10924         }
10925 
10926         break;
10927       }
10928       case TypeAlias:
10929       case TypeDef: {
10930         Diagnosed = ODRDiagTypeDefOrAlias(
10931             FirstRecord, FirstModule, SecondModule,
10932             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10933             FirstDiffType == TypeAlias);
10934         break;
10935       }
10936       case Var: {
10937         Diagnosed =
10938             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10939                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10940         break;
10941       }
10942       case Friend: {
10943         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10944         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10945 
10946         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10947         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10948 
10949         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10950         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10951 
10952         if (FirstND && SecondND) {
10953           ODRDiagDeclError(FirstRecord, FirstModule,
10954                            FirstFriend->getFriendLoc(),
10955                            FirstFriend->getSourceRange(), FriendFunction)
10956               << FirstND;
10957           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10958                           SecondFriend->getSourceRange(), FriendFunction)
10959               << SecondND;
10960 
10961           Diagnosed = true;
10962           break;
10963         }
10964 
10965         if (FirstTSI && SecondTSI) {
10966           QualType FirstFriendType = FirstTSI->getType();
10967           QualType SecondFriendType = SecondTSI->getType();
10968           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10969                  ComputeQualTypeODRHash(SecondFriendType));
10970           ODRDiagDeclError(FirstRecord, FirstModule,
10971                            FirstFriend->getFriendLoc(),
10972                            FirstFriend->getSourceRange(), FriendType)
10973               << FirstFriendType;
10974           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10975                           SecondFriend->getSourceRange(), FriendType)
10976               << SecondFriendType;
10977           Diagnosed = true;
10978           break;
10979         }
10980 
10981         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10982                          FirstFriend->getSourceRange(), FriendTypeFunction)
10983             << (FirstTSI == nullptr);
10984         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10985                         SecondFriend->getSourceRange(), FriendTypeFunction)
10986             << (SecondTSI == nullptr);
10987 
10988         Diagnosed = true;
10989         break;
10990       }
10991       case FunctionTemplate: {
10992         FunctionTemplateDecl *FirstTemplate =
10993             cast<FunctionTemplateDecl>(FirstDecl);
10994         FunctionTemplateDecl *SecondTemplate =
10995             cast<FunctionTemplateDecl>(SecondDecl);
10996 
10997         TemplateParameterList *FirstTPL =
10998             FirstTemplate->getTemplateParameters();
10999         TemplateParameterList *SecondTPL =
11000             SecondTemplate->getTemplateParameters();
11001 
11002         if (FirstTPL->size() != SecondTPL->size()) {
11003           ODRDiagDeclError(FirstRecord, FirstModule,
11004                            FirstTemplate->getLocation(),
11005                            FirstTemplate->getSourceRange(),
11006                            FunctionTemplateDifferentNumberParameters)
11007               << FirstTemplate << FirstTPL->size();
11008           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11009                           SecondTemplate->getSourceRange(),
11010                           FunctionTemplateDifferentNumberParameters)
11011               << SecondTemplate << SecondTPL->size();
11012 
11013           Diagnosed = true;
11014           break;
11015         }
11016 
11017         bool ParameterMismatch = false;
11018         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11019           NamedDecl *FirstParam = FirstTPL->getParam(i);
11020           NamedDecl *SecondParam = SecondTPL->getParam(i);
11021 
11022           if (FirstParam->getKind() != SecondParam->getKind()) {
11023             enum {
11024               TemplateTypeParameter,
11025               NonTypeTemplateParameter,
11026               TemplateTemplateParameter,
11027             };
11028             auto GetParamType = [](NamedDecl *D) {
11029               switch (D->getKind()) {
11030                 default:
11031                   llvm_unreachable("Unexpected template parameter type");
11032                 case Decl::TemplateTypeParm:
11033                   return TemplateTypeParameter;
11034                 case Decl::NonTypeTemplateParm:
11035                   return NonTypeTemplateParameter;
11036                 case Decl::TemplateTemplateParm:
11037                   return TemplateTemplateParameter;
11038               }
11039             };
11040 
11041             ODRDiagDeclError(FirstRecord, FirstModule,
11042                              FirstTemplate->getLocation(),
11043                              FirstTemplate->getSourceRange(),
11044                              FunctionTemplateParameterDifferentKind)
11045                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11046             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11047                             SecondTemplate->getSourceRange(),
11048                             FunctionTemplateParameterDifferentKind)
11049                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11050 
11051             ParameterMismatch = true;
11052             break;
11053           }
11054 
11055           if (FirstParam->getName() != SecondParam->getName()) {
11056             ODRDiagDeclError(
11057                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11058                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
11059                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11060                 << FirstParam;
11061             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11062                             SecondTemplate->getSourceRange(),
11063                             FunctionTemplateParameterName)
11064                 << SecondTemplate << (i + 1)
11065                 << (bool)SecondParam->getIdentifier() << SecondParam;
11066             ParameterMismatch = true;
11067             break;
11068           }
11069 
11070           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11071               isa<TemplateTypeParmDecl>(SecondParam)) {
11072             TemplateTypeParmDecl *FirstTTPD =
11073                 cast<TemplateTypeParmDecl>(FirstParam);
11074             TemplateTypeParmDecl *SecondTTPD =
11075                 cast<TemplateTypeParmDecl>(SecondParam);
11076             bool HasFirstDefaultArgument =
11077                 FirstTTPD->hasDefaultArgument() &&
11078                 !FirstTTPD->defaultArgumentWasInherited();
11079             bool HasSecondDefaultArgument =
11080                 SecondTTPD->hasDefaultArgument() &&
11081                 !SecondTTPD->defaultArgumentWasInherited();
11082             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11083               ODRDiagDeclError(FirstRecord, FirstModule,
11084                                FirstTemplate->getLocation(),
11085                                FirstTemplate->getSourceRange(),
11086                                FunctionTemplateParameterSingleDefaultArgument)
11087                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11088               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11089                               SecondTemplate->getSourceRange(),
11090                               FunctionTemplateParameterSingleDefaultArgument)
11091                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11092               ParameterMismatch = true;
11093               break;
11094             }
11095 
11096             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11097               QualType FirstType = FirstTTPD->getDefaultArgument();
11098               QualType SecondType = SecondTTPD->getDefaultArgument();
11099               if (ComputeQualTypeODRHash(FirstType) !=
11100                   ComputeQualTypeODRHash(SecondType)) {
11101                 ODRDiagDeclError(
11102                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11103                     FirstTemplate->getSourceRange(),
11104                     FunctionTemplateParameterDifferentDefaultArgument)
11105                     << FirstTemplate << (i + 1) << FirstType;
11106                 ODRDiagDeclNote(
11107                     SecondModule, SecondTemplate->getLocation(),
11108                     SecondTemplate->getSourceRange(),
11109                     FunctionTemplateParameterDifferentDefaultArgument)
11110                     << SecondTemplate << (i + 1) << SecondType;
11111                 ParameterMismatch = true;
11112                 break;
11113               }
11114             }
11115 
11116             if (FirstTTPD->isParameterPack() !=
11117                 SecondTTPD->isParameterPack()) {
11118               ODRDiagDeclError(FirstRecord, FirstModule,
11119                                FirstTemplate->getLocation(),
11120                                FirstTemplate->getSourceRange(),
11121                                FunctionTemplatePackParameter)
11122                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11123               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11124                               SecondTemplate->getSourceRange(),
11125                               FunctionTemplatePackParameter)
11126                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11127               ParameterMismatch = true;
11128               break;
11129             }
11130           }
11131 
11132           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11133               isa<TemplateTemplateParmDecl>(SecondParam)) {
11134             TemplateTemplateParmDecl *FirstTTPD =
11135                 cast<TemplateTemplateParmDecl>(FirstParam);
11136             TemplateTemplateParmDecl *SecondTTPD =
11137                 cast<TemplateTemplateParmDecl>(SecondParam);
11138 
11139             TemplateParameterList *FirstTPL =
11140                 FirstTTPD->getTemplateParameters();
11141             TemplateParameterList *SecondTPL =
11142                 SecondTTPD->getTemplateParameters();
11143 
11144             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11145                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11146               ODRDiagDeclError(FirstRecord, FirstModule,
11147                                FirstTemplate->getLocation(),
11148                                FirstTemplate->getSourceRange(),
11149                                FunctionTemplateParameterDifferentType)
11150                   << FirstTemplate << (i + 1);
11151               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11152                               SecondTemplate->getSourceRange(),
11153                               FunctionTemplateParameterDifferentType)
11154                   << SecondTemplate << (i + 1);
11155               ParameterMismatch = true;
11156               break;
11157             }
11158 
11159             bool HasFirstDefaultArgument =
11160                 FirstTTPD->hasDefaultArgument() &&
11161                 !FirstTTPD->defaultArgumentWasInherited();
11162             bool HasSecondDefaultArgument =
11163                 SecondTTPD->hasDefaultArgument() &&
11164                 !SecondTTPD->defaultArgumentWasInherited();
11165             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11166               ODRDiagDeclError(FirstRecord, FirstModule,
11167                                FirstTemplate->getLocation(),
11168                                FirstTemplate->getSourceRange(),
11169                                FunctionTemplateParameterSingleDefaultArgument)
11170                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11171               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11172                               SecondTemplate->getSourceRange(),
11173                               FunctionTemplateParameterSingleDefaultArgument)
11174                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11175               ParameterMismatch = true;
11176               break;
11177             }
11178 
11179             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11180               TemplateArgument FirstTA =
11181                   FirstTTPD->getDefaultArgument().getArgument();
11182               TemplateArgument SecondTA =
11183                   SecondTTPD->getDefaultArgument().getArgument();
11184               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11185                   ComputeTemplateArgumentODRHash(SecondTA)) {
11186                 ODRDiagDeclError(
11187                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11188                     FirstTemplate->getSourceRange(),
11189                     FunctionTemplateParameterDifferentDefaultArgument)
11190                     << FirstTemplate << (i + 1) << FirstTA;
11191                 ODRDiagDeclNote(
11192                     SecondModule, SecondTemplate->getLocation(),
11193                     SecondTemplate->getSourceRange(),
11194                     FunctionTemplateParameterDifferentDefaultArgument)
11195                     << SecondTemplate << (i + 1) << SecondTA;
11196                 ParameterMismatch = true;
11197                 break;
11198               }
11199             }
11200 
11201             if (FirstTTPD->isParameterPack() !=
11202                 SecondTTPD->isParameterPack()) {
11203               ODRDiagDeclError(FirstRecord, FirstModule,
11204                                FirstTemplate->getLocation(),
11205                                FirstTemplate->getSourceRange(),
11206                                FunctionTemplatePackParameter)
11207                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11208               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11209                               SecondTemplate->getSourceRange(),
11210                               FunctionTemplatePackParameter)
11211                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11212               ParameterMismatch = true;
11213               break;
11214             }
11215           }
11216 
11217           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11218               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11219             NonTypeTemplateParmDecl *FirstNTTPD =
11220                 cast<NonTypeTemplateParmDecl>(FirstParam);
11221             NonTypeTemplateParmDecl *SecondNTTPD =
11222                 cast<NonTypeTemplateParmDecl>(SecondParam);
11223 
11224             QualType FirstType = FirstNTTPD->getType();
11225             QualType SecondType = SecondNTTPD->getType();
11226             if (ComputeQualTypeODRHash(FirstType) !=
11227                 ComputeQualTypeODRHash(SecondType)) {
11228               ODRDiagDeclError(FirstRecord, FirstModule,
11229                                FirstTemplate->getLocation(),
11230                                FirstTemplate->getSourceRange(),
11231                                FunctionTemplateParameterDifferentType)
11232                   << FirstTemplate << (i + 1);
11233               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11234                               SecondTemplate->getSourceRange(),
11235                               FunctionTemplateParameterDifferentType)
11236                   << SecondTemplate << (i + 1);
11237               ParameterMismatch = true;
11238               break;
11239             }
11240 
11241             bool HasFirstDefaultArgument =
11242                 FirstNTTPD->hasDefaultArgument() &&
11243                 !FirstNTTPD->defaultArgumentWasInherited();
11244             bool HasSecondDefaultArgument =
11245                 SecondNTTPD->hasDefaultArgument() &&
11246                 !SecondNTTPD->defaultArgumentWasInherited();
11247             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11248               ODRDiagDeclError(FirstRecord, FirstModule,
11249                                FirstTemplate->getLocation(),
11250                                FirstTemplate->getSourceRange(),
11251                                FunctionTemplateParameterSingleDefaultArgument)
11252                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11253               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11254                               SecondTemplate->getSourceRange(),
11255                               FunctionTemplateParameterSingleDefaultArgument)
11256                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11257               ParameterMismatch = true;
11258               break;
11259             }
11260 
11261             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11262               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11263               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11264               if (ComputeODRHash(FirstDefaultArgument) !=
11265                   ComputeODRHash(SecondDefaultArgument)) {
11266                 ODRDiagDeclError(
11267                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11268                     FirstTemplate->getSourceRange(),
11269                     FunctionTemplateParameterDifferentDefaultArgument)
11270                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11271                 ODRDiagDeclNote(
11272                     SecondModule, SecondTemplate->getLocation(),
11273                     SecondTemplate->getSourceRange(),
11274                     FunctionTemplateParameterDifferentDefaultArgument)
11275                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11276                 ParameterMismatch = true;
11277                 break;
11278               }
11279             }
11280 
11281             if (FirstNTTPD->isParameterPack() !=
11282                 SecondNTTPD->isParameterPack()) {
11283               ODRDiagDeclError(FirstRecord, FirstModule,
11284                                FirstTemplate->getLocation(),
11285                                FirstTemplate->getSourceRange(),
11286                                FunctionTemplatePackParameter)
11287                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11288               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11289                               SecondTemplate->getSourceRange(),
11290                               FunctionTemplatePackParameter)
11291                   << SecondTemplate << (i + 1)
11292                   << SecondNTTPD->isParameterPack();
11293               ParameterMismatch = true;
11294               break;
11295             }
11296           }
11297         }
11298 
11299         if (ParameterMismatch) {
11300           Diagnosed = true;
11301           break;
11302         }
11303 
11304         break;
11305       }
11306       }
11307 
11308       if (Diagnosed)
11309         continue;
11310 
11311       Diag(FirstDecl->getLocation(),
11312            diag::err_module_odr_violation_mismatch_decl_unknown)
11313           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11314           << FirstDecl->getSourceRange();
11315       Diag(SecondDecl->getLocation(),
11316            diag::note_module_odr_violation_mismatch_decl_unknown)
11317           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11318       Diagnosed = true;
11319     }
11320 
11321     if (!Diagnosed) {
11322       // All definitions are updates to the same declaration. This happens if a
11323       // module instantiates the declaration of a class template specialization
11324       // and two or more other modules instantiate its definition.
11325       //
11326       // FIXME: Indicate which modules had instantiations of this definition.
11327       // FIXME: How can this even happen?
11328       Diag(Merge.first->getLocation(),
11329            diag::err_module_odr_violation_different_instantiations)
11330         << Merge.first;
11331     }
11332   }
11333 
11334   // Issue ODR failures diagnostics for functions.
11335   for (auto &Merge : FunctionOdrMergeFailures) {
11336     enum ODRFunctionDifference {
11337       ReturnType,
11338       ParameterName,
11339       ParameterType,
11340       ParameterSingleDefaultArgument,
11341       ParameterDifferentDefaultArgument,
11342       FunctionBody,
11343     };
11344 
11345     FunctionDecl *FirstFunction = Merge.first;
11346     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11347 
11348     bool Diagnosed = false;
11349     for (auto &SecondFunction : Merge.second) {
11350 
11351       if (FirstFunction == SecondFunction)
11352         continue;
11353 
11354       std::string SecondModule =
11355           getOwningModuleNameForDiagnostic(SecondFunction);
11356 
11357       auto ODRDiagError = [FirstFunction, &FirstModule,
11358                            this](SourceLocation Loc, SourceRange Range,
11359                                  ODRFunctionDifference DiffType) {
11360         return Diag(Loc, diag::err_module_odr_violation_function)
11361                << FirstFunction << FirstModule.empty() << FirstModule << Range
11362                << DiffType;
11363       };
11364       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11365                                                SourceRange Range,
11366                                                ODRFunctionDifference DiffType) {
11367         return Diag(Loc, diag::note_module_odr_violation_function)
11368                << SecondModule << Range << DiffType;
11369       };
11370 
11371       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11372           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11373         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11374                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11375             << FirstFunction->getReturnType();
11376         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11377                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11378             << SecondFunction->getReturnType();
11379         Diagnosed = true;
11380         break;
11381       }
11382 
11383       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11384              "Merged functions with different number of parameters");
11385 
11386       auto ParamSize = FirstFunction->param_size();
11387       bool ParameterMismatch = false;
11388       for (unsigned I = 0; I < ParamSize; ++I) {
11389         auto *FirstParam = FirstFunction->getParamDecl(I);
11390         auto *SecondParam = SecondFunction->getParamDecl(I);
11391 
11392         assert(getContext().hasSameType(FirstParam->getType(),
11393                                       SecondParam->getType()) &&
11394                "Merged function has different parameter types.");
11395 
11396         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11397           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11398                        ParameterName)
11399               << I + 1 << FirstParam->getDeclName();
11400           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11401                       ParameterName)
11402               << I + 1 << SecondParam->getDeclName();
11403           ParameterMismatch = true;
11404           break;
11405         };
11406 
11407         QualType FirstParamType = FirstParam->getType();
11408         QualType SecondParamType = SecondParam->getType();
11409         if (FirstParamType != SecondParamType &&
11410             ComputeQualTypeODRHash(FirstParamType) !=
11411                 ComputeQualTypeODRHash(SecondParamType)) {
11412           if (const DecayedType *ParamDecayedType =
11413                   FirstParamType->getAs<DecayedType>()) {
11414             ODRDiagError(FirstParam->getLocation(),
11415                          FirstParam->getSourceRange(), ParameterType)
11416                 << (I + 1) << FirstParamType << true
11417                 << ParamDecayedType->getOriginalType();
11418           } else {
11419             ODRDiagError(FirstParam->getLocation(),
11420                          FirstParam->getSourceRange(), ParameterType)
11421                 << (I + 1) << FirstParamType << false;
11422           }
11423 
11424           if (const DecayedType *ParamDecayedType =
11425                   SecondParamType->getAs<DecayedType>()) {
11426             ODRDiagNote(SecondParam->getLocation(),
11427                         SecondParam->getSourceRange(), ParameterType)
11428                 << (I + 1) << SecondParamType << true
11429                 << ParamDecayedType->getOriginalType();
11430           } else {
11431             ODRDiagNote(SecondParam->getLocation(),
11432                         SecondParam->getSourceRange(), ParameterType)
11433                 << (I + 1) << SecondParamType << false;
11434           }
11435           ParameterMismatch = true;
11436           break;
11437         }
11438 
11439         const Expr *FirstInit = FirstParam->getInit();
11440         const Expr *SecondInit = SecondParam->getInit();
11441         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11442           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11443                        ParameterSingleDefaultArgument)
11444               << (I + 1) << (FirstInit == nullptr)
11445               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11446           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11447                       ParameterSingleDefaultArgument)
11448               << (I + 1) << (SecondInit == nullptr)
11449               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11450           ParameterMismatch = true;
11451           break;
11452         }
11453 
11454         if (FirstInit && SecondInit &&
11455             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11456           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11457                        ParameterDifferentDefaultArgument)
11458               << (I + 1) << FirstInit->getSourceRange();
11459           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11460                       ParameterDifferentDefaultArgument)
11461               << (I + 1) << SecondInit->getSourceRange();
11462           ParameterMismatch = true;
11463           break;
11464         }
11465 
11466         assert(ComputeSubDeclODRHash(FirstParam) ==
11467                    ComputeSubDeclODRHash(SecondParam) &&
11468                "Undiagnosed parameter difference.");
11469       }
11470 
11471       if (ParameterMismatch) {
11472         Diagnosed = true;
11473         break;
11474       }
11475 
11476       // If no error has been generated before now, assume the problem is in
11477       // the body and generate a message.
11478       ODRDiagError(FirstFunction->getLocation(),
11479                    FirstFunction->getSourceRange(), FunctionBody);
11480       ODRDiagNote(SecondFunction->getLocation(),
11481                   SecondFunction->getSourceRange(), FunctionBody);
11482       Diagnosed = true;
11483       break;
11484     }
11485     (void)Diagnosed;
11486     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11487   }
11488 
11489   // Issue ODR failures diagnostics for enums.
11490   for (auto &Merge : EnumOdrMergeFailures) {
11491     enum ODREnumDifference {
11492       SingleScopedEnum,
11493       EnumTagKeywordMismatch,
11494       SingleSpecifiedType,
11495       DifferentSpecifiedTypes,
11496       DifferentNumberEnumConstants,
11497       EnumConstantName,
11498       EnumConstantSingleInitilizer,
11499       EnumConstantDifferentInitilizer,
11500     };
11501 
11502     // If we've already pointed out a specific problem with this enum, don't
11503     // bother issuing a general "something's different" diagnostic.
11504     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11505       continue;
11506 
11507     EnumDecl *FirstEnum = Merge.first;
11508     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11509 
11510     using DeclHashes =
11511         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11512     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11513                               DeclHashes &Hashes, EnumDecl *Enum) {
11514       for (auto *D : Enum->decls()) {
11515         // Due to decl merging, the first EnumDecl is the parent of
11516         // Decls in both records.
11517         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11518           continue;
11519         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11520         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11521                             ComputeSubDeclODRHash(D));
11522       }
11523     };
11524     DeclHashes FirstHashes;
11525     PopulateHashes(FirstHashes, FirstEnum);
11526     bool Diagnosed = false;
11527     for (auto &SecondEnum : Merge.second) {
11528 
11529       if (FirstEnum == SecondEnum)
11530         continue;
11531 
11532       std::string SecondModule =
11533           getOwningModuleNameForDiagnostic(SecondEnum);
11534 
11535       auto ODRDiagError = [FirstEnum, &FirstModule,
11536                            this](SourceLocation Loc, SourceRange Range,
11537                                  ODREnumDifference DiffType) {
11538         return Diag(Loc, diag::err_module_odr_violation_enum)
11539                << FirstEnum << FirstModule.empty() << FirstModule << Range
11540                << DiffType;
11541       };
11542       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11543                                                SourceRange Range,
11544                                                ODREnumDifference DiffType) {
11545         return Diag(Loc, diag::note_module_odr_violation_enum)
11546                << SecondModule << Range << DiffType;
11547       };
11548 
11549       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11550         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11551                      SingleScopedEnum)
11552             << FirstEnum->isScoped();
11553         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11554                     SingleScopedEnum)
11555             << SecondEnum->isScoped();
11556         Diagnosed = true;
11557         continue;
11558       }
11559 
11560       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11561         if (FirstEnum->isScopedUsingClassTag() !=
11562             SecondEnum->isScopedUsingClassTag()) {
11563           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11564                        EnumTagKeywordMismatch)
11565               << FirstEnum->isScopedUsingClassTag();
11566           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11567                       EnumTagKeywordMismatch)
11568               << SecondEnum->isScopedUsingClassTag();
11569           Diagnosed = true;
11570           continue;
11571         }
11572       }
11573 
11574       QualType FirstUnderlyingType =
11575           FirstEnum->getIntegerTypeSourceInfo()
11576               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11577               : QualType();
11578       QualType SecondUnderlyingType =
11579           SecondEnum->getIntegerTypeSourceInfo()
11580               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11581               : QualType();
11582       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11583           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11584                        SingleSpecifiedType)
11585               << !FirstUnderlyingType.isNull();
11586           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11587                       SingleSpecifiedType)
11588               << !SecondUnderlyingType.isNull();
11589           Diagnosed = true;
11590           continue;
11591       }
11592 
11593       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11594         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11595             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11596           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11597                        DifferentSpecifiedTypes)
11598               << FirstUnderlyingType;
11599           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11600                       DifferentSpecifiedTypes)
11601               << SecondUnderlyingType;
11602           Diagnosed = true;
11603           continue;
11604         }
11605       }
11606 
11607       DeclHashes SecondHashes;
11608       PopulateHashes(SecondHashes, SecondEnum);
11609 
11610       if (FirstHashes.size() != SecondHashes.size()) {
11611         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11612                      DifferentNumberEnumConstants)
11613             << (int)FirstHashes.size();
11614         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11615                     DifferentNumberEnumConstants)
11616             << (int)SecondHashes.size();
11617         Diagnosed = true;
11618         continue;
11619       }
11620 
11621       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11622         if (FirstHashes[I].second == SecondHashes[I].second)
11623           continue;
11624         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11625         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11626 
11627         if (FirstEnumConstant->getDeclName() !=
11628             SecondEnumConstant->getDeclName()) {
11629 
11630           ODRDiagError(FirstEnumConstant->getLocation(),
11631                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11632               << I + 1 << FirstEnumConstant;
11633           ODRDiagNote(SecondEnumConstant->getLocation(),
11634                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11635               << I + 1 << SecondEnumConstant;
11636           Diagnosed = true;
11637           break;
11638         }
11639 
11640         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11641         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11642         if (!FirstInit && !SecondInit)
11643           continue;
11644 
11645         if (!FirstInit || !SecondInit) {
11646           ODRDiagError(FirstEnumConstant->getLocation(),
11647                        FirstEnumConstant->getSourceRange(),
11648                        EnumConstantSingleInitilizer)
11649               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11650           ODRDiagNote(SecondEnumConstant->getLocation(),
11651                       SecondEnumConstant->getSourceRange(),
11652                       EnumConstantSingleInitilizer)
11653               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11654           Diagnosed = true;
11655           break;
11656         }
11657 
11658         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11659           ODRDiagError(FirstEnumConstant->getLocation(),
11660                        FirstEnumConstant->getSourceRange(),
11661                        EnumConstantDifferentInitilizer)
11662               << I + 1 << FirstEnumConstant;
11663           ODRDiagNote(SecondEnumConstant->getLocation(),
11664                       SecondEnumConstant->getSourceRange(),
11665                       EnumConstantDifferentInitilizer)
11666               << I + 1 << SecondEnumConstant;
11667           Diagnosed = true;
11668           break;
11669         }
11670       }
11671     }
11672 
11673     (void)Diagnosed;
11674     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11675   }
11676 }
11677 
11678 void ASTReader::StartedDeserializing() {
11679   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11680     ReadTimer->startTimer();
11681 }
11682 
11683 void ASTReader::FinishedDeserializing() {
11684   assert(NumCurrentElementsDeserializing &&
11685          "FinishedDeserializing not paired with StartedDeserializing");
11686   if (NumCurrentElementsDeserializing == 1) {
11687     // We decrease NumCurrentElementsDeserializing only after pending actions
11688     // are finished, to avoid recursively re-calling finishPendingActions().
11689     finishPendingActions();
11690   }
11691   --NumCurrentElementsDeserializing;
11692 
11693   if (NumCurrentElementsDeserializing == 0) {
11694     // Propagate exception specification and deduced type updates along
11695     // redeclaration chains.
11696     //
11697     // We do this now rather than in finishPendingActions because we want to
11698     // be able to walk the complete redeclaration chains of the updated decls.
11699     while (!PendingExceptionSpecUpdates.empty() ||
11700            !PendingDeducedTypeUpdates.empty()) {
11701       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11702       PendingExceptionSpecUpdates.clear();
11703       for (auto Update : ESUpdates) {
11704         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11705         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11706         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11707         if (auto *Listener = getContext().getASTMutationListener())
11708           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11709         for (auto *Redecl : Update.second->redecls())
11710           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11711       }
11712 
11713       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11714       PendingDeducedTypeUpdates.clear();
11715       for (auto Update : DTUpdates) {
11716         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11717         // FIXME: If the return type is already deduced, check that it matches.
11718         getContext().adjustDeducedFunctionResultType(Update.first,
11719                                                      Update.second);
11720       }
11721     }
11722 
11723     if (ReadTimer)
11724       ReadTimer->stopTimer();
11725 
11726     diagnoseOdrViolations();
11727 
11728     // We are not in recursive loading, so it's safe to pass the "interesting"
11729     // decls to the consumer.
11730     if (Consumer)
11731       PassInterestingDeclsToConsumer();
11732   }
11733 }
11734 
11735 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11736   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11737     // Remove any fake results before adding any real ones.
11738     auto It = PendingFakeLookupResults.find(II);
11739     if (It != PendingFakeLookupResults.end()) {
11740       for (auto *ND : It->second)
11741         SemaObj->IdResolver.RemoveDecl(ND);
11742       // FIXME: this works around module+PCH performance issue.
11743       // Rather than erase the result from the map, which is O(n), just clear
11744       // the vector of NamedDecls.
11745       It->second.clear();
11746     }
11747   }
11748 
11749   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11750     SemaObj->TUScope->AddDecl(D);
11751   } else if (SemaObj->TUScope) {
11752     // Adding the decl to IdResolver may have failed because it was already in
11753     // (even though it was not added in scope). If it is already in, make sure
11754     // it gets in the scope as well.
11755     if (std::find(SemaObj->IdResolver.begin(Name),
11756                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11757       SemaObj->TUScope->AddDecl(D);
11758   }
11759 }
11760 
11761 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11762                      ASTContext *Context,
11763                      const PCHContainerReader &PCHContainerRdr,
11764                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11765                      StringRef isysroot, bool DisableValidation,
11766                      bool AllowASTWithCompilerErrors,
11767                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11768                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11769                      std::unique_ptr<llvm::Timer> ReadTimer)
11770     : Listener(DisableValidation
11771                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11772                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11773       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11774       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11775       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11776                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11777       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11778       DisableValidation(DisableValidation),
11779       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11780       AllowConfigurationMismatch(AllowConfigurationMismatch),
11781       ValidateSystemInputs(ValidateSystemInputs),
11782       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11783       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11784   SourceMgr.setExternalSLocEntrySource(this);
11785 
11786   for (const auto &Ext : Extensions) {
11787     auto BlockName = Ext->getExtensionMetadata().BlockName;
11788     auto Known = ModuleFileExtensions.find(BlockName);
11789     if (Known != ModuleFileExtensions.end()) {
11790       Diags.Report(diag::warn_duplicate_module_file_extension)
11791         << BlockName;
11792       continue;
11793     }
11794 
11795     ModuleFileExtensions.insert({BlockName, Ext});
11796   }
11797 }
11798 
11799 ASTReader::~ASTReader() {
11800   if (OwnsDeserializationListener)
11801     delete DeserializationListener;
11802 }
11803 
11804 IdentifierResolver &ASTReader::getIdResolver() {
11805   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11806 }
11807 
11808 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11809                                                unsigned AbbrevID) {
11810   Idx = 0;
11811   Record.clear();
11812   return Cursor.readRecord(AbbrevID, Record);
11813 }
11814 //===----------------------------------------------------------------------===//
11815 //// OMPClauseReader implementation
11816 ////===----------------------------------------------------------------------===//
11817 
11818 // This has to be in namespace clang because it's friended by all
11819 // of the OMP clauses.
11820 namespace clang {
11821 
11822 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11823   ASTRecordReader &Record;
11824   ASTContext &Context;
11825 
11826 public:
11827   OMPClauseReader(ASTRecordReader &Record)
11828       : Record(Record), Context(Record.getContext()) {}
11829 
11830 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11831 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11832   OMPClause *readClause();
11833   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11834   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11835 };
11836 
11837 } // end namespace clang
11838 
11839 OMPClause *ASTRecordReader::readOMPClause() {
11840   return OMPClauseReader(*this).readClause();
11841 }
11842 
11843 OMPClause *OMPClauseReader::readClause() {
11844   OMPClause *C = nullptr;
11845   switch (llvm::omp::Clause(Record.readInt())) {
11846   case llvm::omp::OMPC_if:
11847     C = new (Context) OMPIfClause();
11848     break;
11849   case llvm::omp::OMPC_final:
11850     C = new (Context) OMPFinalClause();
11851     break;
11852   case llvm::omp::OMPC_num_threads:
11853     C = new (Context) OMPNumThreadsClause();
11854     break;
11855   case llvm::omp::OMPC_safelen:
11856     C = new (Context) OMPSafelenClause();
11857     break;
11858   case llvm::omp::OMPC_simdlen:
11859     C = new (Context) OMPSimdlenClause();
11860     break;
11861   case llvm::omp::OMPC_allocator:
11862     C = new (Context) OMPAllocatorClause();
11863     break;
11864   case llvm::omp::OMPC_collapse:
11865     C = new (Context) OMPCollapseClause();
11866     break;
11867   case llvm::omp::OMPC_default:
11868     C = new (Context) OMPDefaultClause();
11869     break;
11870   case llvm::omp::OMPC_proc_bind:
11871     C = new (Context) OMPProcBindClause();
11872     break;
11873   case llvm::omp::OMPC_schedule:
11874     C = new (Context) OMPScheduleClause();
11875     break;
11876   case llvm::omp::OMPC_ordered:
11877     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11878     break;
11879   case llvm::omp::OMPC_nowait:
11880     C = new (Context) OMPNowaitClause();
11881     break;
11882   case llvm::omp::OMPC_untied:
11883     C = new (Context) OMPUntiedClause();
11884     break;
11885   case llvm::omp::OMPC_mergeable:
11886     C = new (Context) OMPMergeableClause();
11887     break;
11888   case llvm::omp::OMPC_read:
11889     C = new (Context) OMPReadClause();
11890     break;
11891   case llvm::omp::OMPC_write:
11892     C = new (Context) OMPWriteClause();
11893     break;
11894   case llvm::omp::OMPC_update:
11895     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11896     break;
11897   case llvm::omp::OMPC_capture:
11898     C = new (Context) OMPCaptureClause();
11899     break;
11900   case llvm::omp::OMPC_seq_cst:
11901     C = new (Context) OMPSeqCstClause();
11902     break;
11903   case llvm::omp::OMPC_acq_rel:
11904     C = new (Context) OMPAcqRelClause();
11905     break;
11906   case llvm::omp::OMPC_acquire:
11907     C = new (Context) OMPAcquireClause();
11908     break;
11909   case llvm::omp::OMPC_release:
11910     C = new (Context) OMPReleaseClause();
11911     break;
11912   case llvm::omp::OMPC_relaxed:
11913     C = new (Context) OMPRelaxedClause();
11914     break;
11915   case llvm::omp::OMPC_threads:
11916     C = new (Context) OMPThreadsClause();
11917     break;
11918   case llvm::omp::OMPC_simd:
11919     C = new (Context) OMPSIMDClause();
11920     break;
11921   case llvm::omp::OMPC_nogroup:
11922     C = new (Context) OMPNogroupClause();
11923     break;
11924   case llvm::omp::OMPC_unified_address:
11925     C = new (Context) OMPUnifiedAddressClause();
11926     break;
11927   case llvm::omp::OMPC_unified_shared_memory:
11928     C = new (Context) OMPUnifiedSharedMemoryClause();
11929     break;
11930   case llvm::omp::OMPC_reverse_offload:
11931     C = new (Context) OMPReverseOffloadClause();
11932     break;
11933   case llvm::omp::OMPC_dynamic_allocators:
11934     C = new (Context) OMPDynamicAllocatorsClause();
11935     break;
11936   case llvm::omp::OMPC_atomic_default_mem_order:
11937     C = new (Context) OMPAtomicDefaultMemOrderClause();
11938     break;
11939  case llvm::omp::OMPC_private:
11940     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11941     break;
11942   case llvm::omp::OMPC_firstprivate:
11943     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11944     break;
11945   case llvm::omp::OMPC_lastprivate:
11946     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11947     break;
11948   case llvm::omp::OMPC_shared:
11949     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11950     break;
11951   case llvm::omp::OMPC_reduction: {
11952     unsigned N = Record.readInt();
11953     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11954     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11955     break;
11956   }
11957   case llvm::omp::OMPC_task_reduction:
11958     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11959     break;
11960   case llvm::omp::OMPC_in_reduction:
11961     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11962     break;
11963   case llvm::omp::OMPC_linear:
11964     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11965     break;
11966   case llvm::omp::OMPC_aligned:
11967     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11968     break;
11969   case llvm::omp::OMPC_copyin:
11970     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11971     break;
11972   case llvm::omp::OMPC_copyprivate:
11973     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11974     break;
11975   case llvm::omp::OMPC_flush:
11976     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11977     break;
11978   case llvm::omp::OMPC_depobj:
11979     C = OMPDepobjClause::CreateEmpty(Context);
11980     break;
11981   case llvm::omp::OMPC_depend: {
11982     unsigned NumVars = Record.readInt();
11983     unsigned NumLoops = Record.readInt();
11984     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11985     break;
11986   }
11987   case llvm::omp::OMPC_device:
11988     C = new (Context) OMPDeviceClause();
11989     break;
11990   case llvm::omp::OMPC_map: {
11991     OMPMappableExprListSizeTy Sizes;
11992     Sizes.NumVars = Record.readInt();
11993     Sizes.NumUniqueDeclarations = Record.readInt();
11994     Sizes.NumComponentLists = Record.readInt();
11995     Sizes.NumComponents = Record.readInt();
11996     C = OMPMapClause::CreateEmpty(Context, Sizes);
11997     break;
11998   }
11999   case llvm::omp::OMPC_num_teams:
12000     C = new (Context) OMPNumTeamsClause();
12001     break;
12002   case llvm::omp::OMPC_thread_limit:
12003     C = new (Context) OMPThreadLimitClause();
12004     break;
12005   case llvm::omp::OMPC_priority:
12006     C = new (Context) OMPPriorityClause();
12007     break;
12008   case llvm::omp::OMPC_grainsize:
12009     C = new (Context) OMPGrainsizeClause();
12010     break;
12011   case llvm::omp::OMPC_num_tasks:
12012     C = new (Context) OMPNumTasksClause();
12013     break;
12014   case llvm::omp::OMPC_hint:
12015     C = new (Context) OMPHintClause();
12016     break;
12017   case llvm::omp::OMPC_dist_schedule:
12018     C = new (Context) OMPDistScheduleClause();
12019     break;
12020   case llvm::omp::OMPC_defaultmap:
12021     C = new (Context) OMPDefaultmapClause();
12022     break;
12023   case llvm::omp::OMPC_to: {
12024     OMPMappableExprListSizeTy Sizes;
12025     Sizes.NumVars = Record.readInt();
12026     Sizes.NumUniqueDeclarations = Record.readInt();
12027     Sizes.NumComponentLists = Record.readInt();
12028     Sizes.NumComponents = Record.readInt();
12029     C = OMPToClause::CreateEmpty(Context, Sizes);
12030     break;
12031   }
12032   case llvm::omp::OMPC_from: {
12033     OMPMappableExprListSizeTy Sizes;
12034     Sizes.NumVars = Record.readInt();
12035     Sizes.NumUniqueDeclarations = Record.readInt();
12036     Sizes.NumComponentLists = Record.readInt();
12037     Sizes.NumComponents = Record.readInt();
12038     C = OMPFromClause::CreateEmpty(Context, Sizes);
12039     break;
12040   }
12041   case llvm::omp::OMPC_use_device_ptr: {
12042     OMPMappableExprListSizeTy Sizes;
12043     Sizes.NumVars = Record.readInt();
12044     Sizes.NumUniqueDeclarations = Record.readInt();
12045     Sizes.NumComponentLists = Record.readInt();
12046     Sizes.NumComponents = Record.readInt();
12047     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12048     break;
12049   }
12050   case llvm::omp::OMPC_use_device_addr: {
12051     OMPMappableExprListSizeTy Sizes;
12052     Sizes.NumVars = Record.readInt();
12053     Sizes.NumUniqueDeclarations = Record.readInt();
12054     Sizes.NumComponentLists = Record.readInt();
12055     Sizes.NumComponents = Record.readInt();
12056     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
12057     break;
12058   }
12059   case llvm::omp::OMPC_is_device_ptr: {
12060     OMPMappableExprListSizeTy Sizes;
12061     Sizes.NumVars = Record.readInt();
12062     Sizes.NumUniqueDeclarations = Record.readInt();
12063     Sizes.NumComponentLists = Record.readInt();
12064     Sizes.NumComponents = Record.readInt();
12065     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12066     break;
12067   }
12068   case llvm::omp::OMPC_allocate:
12069     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12070     break;
12071   case llvm::omp::OMPC_nontemporal:
12072     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
12073     break;
12074   case llvm::omp::OMPC_inclusive:
12075     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
12076     break;
12077   case llvm::omp::OMPC_exclusive:
12078     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
12079     break;
12080   case llvm::omp::OMPC_order:
12081     C = new (Context) OMPOrderClause();
12082     break;
12083   case llvm::omp::OMPC_destroy:
12084     C = new (Context) OMPDestroyClause();
12085     break;
12086   case llvm::omp::OMPC_detach:
12087     C = new (Context) OMPDetachClause();
12088     break;
12089   case llvm::omp::OMPC_uses_allocators:
12090     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
12091     break;
12092   case llvm::omp::OMPC_affinity:
12093     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
12094     break;
12095 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
12096   case llvm::omp::Enum:                                                        \
12097     break;
12098 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12099   default:
12100     break;
12101   }
12102   assert(C && "Unknown OMPClause type");
12103 
12104   Visit(C);
12105   C->setLocStart(Record.readSourceLocation());
12106   C->setLocEnd(Record.readSourceLocation());
12107 
12108   return C;
12109 }
12110 
12111 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12112   C->setPreInitStmt(Record.readSubStmt(),
12113                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12114 }
12115 
12116 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12117   VisitOMPClauseWithPreInit(C);
12118   C->setPostUpdateExpr(Record.readSubExpr());
12119 }
12120 
12121 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12122   VisitOMPClauseWithPreInit(C);
12123   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12124   C->setNameModifierLoc(Record.readSourceLocation());
12125   C->setColonLoc(Record.readSourceLocation());
12126   C->setCondition(Record.readSubExpr());
12127   C->setLParenLoc(Record.readSourceLocation());
12128 }
12129 
12130 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12131   VisitOMPClauseWithPreInit(C);
12132   C->setCondition(Record.readSubExpr());
12133   C->setLParenLoc(Record.readSourceLocation());
12134 }
12135 
12136 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12137   VisitOMPClauseWithPreInit(C);
12138   C->setNumThreads(Record.readSubExpr());
12139   C->setLParenLoc(Record.readSourceLocation());
12140 }
12141 
12142 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12143   C->setSafelen(Record.readSubExpr());
12144   C->setLParenLoc(Record.readSourceLocation());
12145 }
12146 
12147 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12148   C->setSimdlen(Record.readSubExpr());
12149   C->setLParenLoc(Record.readSourceLocation());
12150 }
12151 
12152 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12153   C->setAllocator(Record.readExpr());
12154   C->setLParenLoc(Record.readSourceLocation());
12155 }
12156 
12157 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12158   C->setNumForLoops(Record.readSubExpr());
12159   C->setLParenLoc(Record.readSourceLocation());
12160 }
12161 
12162 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12163   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12164   C->setLParenLoc(Record.readSourceLocation());
12165   C->setDefaultKindKwLoc(Record.readSourceLocation());
12166 }
12167 
12168 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12169   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12170   C->setLParenLoc(Record.readSourceLocation());
12171   C->setProcBindKindKwLoc(Record.readSourceLocation());
12172 }
12173 
12174 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12175   VisitOMPClauseWithPreInit(C);
12176   C->setScheduleKind(
12177        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12178   C->setFirstScheduleModifier(
12179       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12180   C->setSecondScheduleModifier(
12181       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12182   C->setChunkSize(Record.readSubExpr());
12183   C->setLParenLoc(Record.readSourceLocation());
12184   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12185   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12186   C->setScheduleKindLoc(Record.readSourceLocation());
12187   C->setCommaLoc(Record.readSourceLocation());
12188 }
12189 
12190 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12191   C->setNumForLoops(Record.readSubExpr());
12192   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12193     C->setLoopNumIterations(I, Record.readSubExpr());
12194   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12195     C->setLoopCounter(I, Record.readSubExpr());
12196   C->setLParenLoc(Record.readSourceLocation());
12197 }
12198 
12199 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12200   C->setEventHandler(Record.readSubExpr());
12201   C->setLParenLoc(Record.readSourceLocation());
12202 }
12203 
12204 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12205 
12206 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12207 
12208 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12209 
12210 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12211 
12212 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12213 
12214 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12215   if (C->isExtended()) {
12216     C->setLParenLoc(Record.readSourceLocation());
12217     C->setArgumentLoc(Record.readSourceLocation());
12218     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12219   }
12220 }
12221 
12222 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12223 
12224 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12225 
12226 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12227 
12228 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12229 
12230 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12231 
12232 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12233 
12234 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12235 
12236 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12237 
12238 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12239 
12240 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12241 
12242 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12243 
12244 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12245     OMPUnifiedSharedMemoryClause *) {}
12246 
12247 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12248 
12249 void
12250 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12251 }
12252 
12253 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12254     OMPAtomicDefaultMemOrderClause *C) {
12255   C->setAtomicDefaultMemOrderKind(
12256       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12257   C->setLParenLoc(Record.readSourceLocation());
12258   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12259 }
12260 
12261 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12262   C->setLParenLoc(Record.readSourceLocation());
12263   unsigned NumVars = C->varlist_size();
12264   SmallVector<Expr *, 16> Vars;
12265   Vars.reserve(NumVars);
12266   for (unsigned i = 0; i != NumVars; ++i)
12267     Vars.push_back(Record.readSubExpr());
12268   C->setVarRefs(Vars);
12269   Vars.clear();
12270   for (unsigned i = 0; i != NumVars; ++i)
12271     Vars.push_back(Record.readSubExpr());
12272   C->setPrivateCopies(Vars);
12273 }
12274 
12275 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12276   VisitOMPClauseWithPreInit(C);
12277   C->setLParenLoc(Record.readSourceLocation());
12278   unsigned NumVars = C->varlist_size();
12279   SmallVector<Expr *, 16> Vars;
12280   Vars.reserve(NumVars);
12281   for (unsigned i = 0; i != NumVars; ++i)
12282     Vars.push_back(Record.readSubExpr());
12283   C->setVarRefs(Vars);
12284   Vars.clear();
12285   for (unsigned i = 0; i != NumVars; ++i)
12286     Vars.push_back(Record.readSubExpr());
12287   C->setPrivateCopies(Vars);
12288   Vars.clear();
12289   for (unsigned i = 0; i != NumVars; ++i)
12290     Vars.push_back(Record.readSubExpr());
12291   C->setInits(Vars);
12292 }
12293 
12294 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12295   VisitOMPClauseWithPostUpdate(C);
12296   C->setLParenLoc(Record.readSourceLocation());
12297   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12298   C->setKindLoc(Record.readSourceLocation());
12299   C->setColonLoc(Record.readSourceLocation());
12300   unsigned NumVars = C->varlist_size();
12301   SmallVector<Expr *, 16> Vars;
12302   Vars.reserve(NumVars);
12303   for (unsigned i = 0; i != NumVars; ++i)
12304     Vars.push_back(Record.readSubExpr());
12305   C->setVarRefs(Vars);
12306   Vars.clear();
12307   for (unsigned i = 0; i != NumVars; ++i)
12308     Vars.push_back(Record.readSubExpr());
12309   C->setPrivateCopies(Vars);
12310   Vars.clear();
12311   for (unsigned i = 0; i != NumVars; ++i)
12312     Vars.push_back(Record.readSubExpr());
12313   C->setSourceExprs(Vars);
12314   Vars.clear();
12315   for (unsigned i = 0; i != NumVars; ++i)
12316     Vars.push_back(Record.readSubExpr());
12317   C->setDestinationExprs(Vars);
12318   Vars.clear();
12319   for (unsigned i = 0; i != NumVars; ++i)
12320     Vars.push_back(Record.readSubExpr());
12321   C->setAssignmentOps(Vars);
12322 }
12323 
12324 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12325   C->setLParenLoc(Record.readSourceLocation());
12326   unsigned NumVars = C->varlist_size();
12327   SmallVector<Expr *, 16> Vars;
12328   Vars.reserve(NumVars);
12329   for (unsigned i = 0; i != NumVars; ++i)
12330     Vars.push_back(Record.readSubExpr());
12331   C->setVarRefs(Vars);
12332 }
12333 
12334 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12335   VisitOMPClauseWithPostUpdate(C);
12336   C->setLParenLoc(Record.readSourceLocation());
12337   C->setModifierLoc(Record.readSourceLocation());
12338   C->setColonLoc(Record.readSourceLocation());
12339   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12340   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12341   C->setQualifierLoc(NNSL);
12342   C->setNameInfo(DNI);
12343 
12344   unsigned NumVars = C->varlist_size();
12345   SmallVector<Expr *, 16> Vars;
12346   Vars.reserve(NumVars);
12347   for (unsigned i = 0; i != NumVars; ++i)
12348     Vars.push_back(Record.readSubExpr());
12349   C->setVarRefs(Vars);
12350   Vars.clear();
12351   for (unsigned i = 0; i != NumVars; ++i)
12352     Vars.push_back(Record.readSubExpr());
12353   C->setPrivates(Vars);
12354   Vars.clear();
12355   for (unsigned i = 0; i != NumVars; ++i)
12356     Vars.push_back(Record.readSubExpr());
12357   C->setLHSExprs(Vars);
12358   Vars.clear();
12359   for (unsigned i = 0; i != NumVars; ++i)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setRHSExprs(Vars);
12362   Vars.clear();
12363   for (unsigned i = 0; i != NumVars; ++i)
12364     Vars.push_back(Record.readSubExpr());
12365   C->setReductionOps(Vars);
12366   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12367     Vars.clear();
12368     for (unsigned i = 0; i != NumVars; ++i)
12369       Vars.push_back(Record.readSubExpr());
12370     C->setInscanCopyOps(Vars);
12371     Vars.clear();
12372     for (unsigned i = 0; i != NumVars; ++i)
12373       Vars.push_back(Record.readSubExpr());
12374     C->setInscanCopyArrayTemps(Vars);
12375     Vars.clear();
12376     for (unsigned i = 0; i != NumVars; ++i)
12377       Vars.push_back(Record.readSubExpr());
12378     C->setInscanCopyArrayElems(Vars);
12379   }
12380 }
12381 
12382 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12383   VisitOMPClauseWithPostUpdate(C);
12384   C->setLParenLoc(Record.readSourceLocation());
12385   C->setColonLoc(Record.readSourceLocation());
12386   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12387   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12388   C->setQualifierLoc(NNSL);
12389   C->setNameInfo(DNI);
12390 
12391   unsigned NumVars = C->varlist_size();
12392   SmallVector<Expr *, 16> Vars;
12393   Vars.reserve(NumVars);
12394   for (unsigned I = 0; I != NumVars; ++I)
12395     Vars.push_back(Record.readSubExpr());
12396   C->setVarRefs(Vars);
12397   Vars.clear();
12398   for (unsigned I = 0; I != NumVars; ++I)
12399     Vars.push_back(Record.readSubExpr());
12400   C->setPrivates(Vars);
12401   Vars.clear();
12402   for (unsigned I = 0; I != NumVars; ++I)
12403     Vars.push_back(Record.readSubExpr());
12404   C->setLHSExprs(Vars);
12405   Vars.clear();
12406   for (unsigned I = 0; I != NumVars; ++I)
12407     Vars.push_back(Record.readSubExpr());
12408   C->setRHSExprs(Vars);
12409   Vars.clear();
12410   for (unsigned I = 0; I != NumVars; ++I)
12411     Vars.push_back(Record.readSubExpr());
12412   C->setReductionOps(Vars);
12413 }
12414 
12415 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12416   VisitOMPClauseWithPostUpdate(C);
12417   C->setLParenLoc(Record.readSourceLocation());
12418   C->setColonLoc(Record.readSourceLocation());
12419   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12420   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12421   C->setQualifierLoc(NNSL);
12422   C->setNameInfo(DNI);
12423 
12424   unsigned NumVars = C->varlist_size();
12425   SmallVector<Expr *, 16> Vars;
12426   Vars.reserve(NumVars);
12427   for (unsigned I = 0; I != NumVars; ++I)
12428     Vars.push_back(Record.readSubExpr());
12429   C->setVarRefs(Vars);
12430   Vars.clear();
12431   for (unsigned I = 0; I != NumVars; ++I)
12432     Vars.push_back(Record.readSubExpr());
12433   C->setPrivates(Vars);
12434   Vars.clear();
12435   for (unsigned I = 0; I != NumVars; ++I)
12436     Vars.push_back(Record.readSubExpr());
12437   C->setLHSExprs(Vars);
12438   Vars.clear();
12439   for (unsigned I = 0; I != NumVars; ++I)
12440     Vars.push_back(Record.readSubExpr());
12441   C->setRHSExprs(Vars);
12442   Vars.clear();
12443   for (unsigned I = 0; I != NumVars; ++I)
12444     Vars.push_back(Record.readSubExpr());
12445   C->setReductionOps(Vars);
12446   Vars.clear();
12447   for (unsigned I = 0; I != NumVars; ++I)
12448     Vars.push_back(Record.readSubExpr());
12449   C->setTaskgroupDescriptors(Vars);
12450 }
12451 
12452 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12453   VisitOMPClauseWithPostUpdate(C);
12454   C->setLParenLoc(Record.readSourceLocation());
12455   C->setColonLoc(Record.readSourceLocation());
12456   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12457   C->setModifierLoc(Record.readSourceLocation());
12458   unsigned NumVars = C->varlist_size();
12459   SmallVector<Expr *, 16> Vars;
12460   Vars.reserve(NumVars);
12461   for (unsigned i = 0; i != NumVars; ++i)
12462     Vars.push_back(Record.readSubExpr());
12463   C->setVarRefs(Vars);
12464   Vars.clear();
12465   for (unsigned i = 0; i != NumVars; ++i)
12466     Vars.push_back(Record.readSubExpr());
12467   C->setPrivates(Vars);
12468   Vars.clear();
12469   for (unsigned i = 0; i != NumVars; ++i)
12470     Vars.push_back(Record.readSubExpr());
12471   C->setInits(Vars);
12472   Vars.clear();
12473   for (unsigned i = 0; i != NumVars; ++i)
12474     Vars.push_back(Record.readSubExpr());
12475   C->setUpdates(Vars);
12476   Vars.clear();
12477   for (unsigned i = 0; i != NumVars; ++i)
12478     Vars.push_back(Record.readSubExpr());
12479   C->setFinals(Vars);
12480   C->setStep(Record.readSubExpr());
12481   C->setCalcStep(Record.readSubExpr());
12482   Vars.clear();
12483   for (unsigned I = 0; I != NumVars + 1; ++I)
12484     Vars.push_back(Record.readSubExpr());
12485   C->setUsedExprs(Vars);
12486 }
12487 
12488 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12489   C->setLParenLoc(Record.readSourceLocation());
12490   C->setColonLoc(Record.readSourceLocation());
12491   unsigned NumVars = C->varlist_size();
12492   SmallVector<Expr *, 16> Vars;
12493   Vars.reserve(NumVars);
12494   for (unsigned i = 0; i != NumVars; ++i)
12495     Vars.push_back(Record.readSubExpr());
12496   C->setVarRefs(Vars);
12497   C->setAlignment(Record.readSubExpr());
12498 }
12499 
12500 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12501   C->setLParenLoc(Record.readSourceLocation());
12502   unsigned NumVars = C->varlist_size();
12503   SmallVector<Expr *, 16> Exprs;
12504   Exprs.reserve(NumVars);
12505   for (unsigned i = 0; i != NumVars; ++i)
12506     Exprs.push_back(Record.readSubExpr());
12507   C->setVarRefs(Exprs);
12508   Exprs.clear();
12509   for (unsigned i = 0; i != NumVars; ++i)
12510     Exprs.push_back(Record.readSubExpr());
12511   C->setSourceExprs(Exprs);
12512   Exprs.clear();
12513   for (unsigned i = 0; i != NumVars; ++i)
12514     Exprs.push_back(Record.readSubExpr());
12515   C->setDestinationExprs(Exprs);
12516   Exprs.clear();
12517   for (unsigned i = 0; i != NumVars; ++i)
12518     Exprs.push_back(Record.readSubExpr());
12519   C->setAssignmentOps(Exprs);
12520 }
12521 
12522 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12523   C->setLParenLoc(Record.readSourceLocation());
12524   unsigned NumVars = C->varlist_size();
12525   SmallVector<Expr *, 16> Exprs;
12526   Exprs.reserve(NumVars);
12527   for (unsigned i = 0; i != NumVars; ++i)
12528     Exprs.push_back(Record.readSubExpr());
12529   C->setVarRefs(Exprs);
12530   Exprs.clear();
12531   for (unsigned i = 0; i != NumVars; ++i)
12532     Exprs.push_back(Record.readSubExpr());
12533   C->setSourceExprs(Exprs);
12534   Exprs.clear();
12535   for (unsigned i = 0; i != NumVars; ++i)
12536     Exprs.push_back(Record.readSubExpr());
12537   C->setDestinationExprs(Exprs);
12538   Exprs.clear();
12539   for (unsigned i = 0; i != NumVars; ++i)
12540     Exprs.push_back(Record.readSubExpr());
12541   C->setAssignmentOps(Exprs);
12542 }
12543 
12544 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12545   C->setLParenLoc(Record.readSourceLocation());
12546   unsigned NumVars = C->varlist_size();
12547   SmallVector<Expr *, 16> Vars;
12548   Vars.reserve(NumVars);
12549   for (unsigned i = 0; i != NumVars; ++i)
12550     Vars.push_back(Record.readSubExpr());
12551   C->setVarRefs(Vars);
12552 }
12553 
12554 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12555   C->setDepobj(Record.readSubExpr());
12556   C->setLParenLoc(Record.readSourceLocation());
12557 }
12558 
12559 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12560   C->setLParenLoc(Record.readSourceLocation());
12561   C->setModifier(Record.readSubExpr());
12562   C->setDependencyKind(
12563       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12564   C->setDependencyLoc(Record.readSourceLocation());
12565   C->setColonLoc(Record.readSourceLocation());
12566   unsigned NumVars = C->varlist_size();
12567   SmallVector<Expr *, 16> Vars;
12568   Vars.reserve(NumVars);
12569   for (unsigned I = 0; I != NumVars; ++I)
12570     Vars.push_back(Record.readSubExpr());
12571   C->setVarRefs(Vars);
12572   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12573     C->setLoopData(I, Record.readSubExpr());
12574 }
12575 
12576 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12577   VisitOMPClauseWithPreInit(C);
12578   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12579   C->setDevice(Record.readSubExpr());
12580   C->setModifierLoc(Record.readSourceLocation());
12581   C->setLParenLoc(Record.readSourceLocation());
12582 }
12583 
12584 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12585   C->setLParenLoc(Record.readSourceLocation());
12586   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12587     C->setMapTypeModifier(
12588         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12589     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12590   }
12591   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12592   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12593   C->setMapType(
12594      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12595   C->setMapLoc(Record.readSourceLocation());
12596   C->setColonLoc(Record.readSourceLocation());
12597   auto NumVars = C->varlist_size();
12598   auto UniqueDecls = C->getUniqueDeclarationsNum();
12599   auto TotalLists = C->getTotalComponentListNum();
12600   auto TotalComponents = C->getTotalComponentsNum();
12601 
12602   SmallVector<Expr *, 16> Vars;
12603   Vars.reserve(NumVars);
12604   for (unsigned i = 0; i != NumVars; ++i)
12605     Vars.push_back(Record.readExpr());
12606   C->setVarRefs(Vars);
12607 
12608   SmallVector<Expr *, 16> UDMappers;
12609   UDMappers.reserve(NumVars);
12610   for (unsigned I = 0; I < NumVars; ++I)
12611     UDMappers.push_back(Record.readExpr());
12612   C->setUDMapperRefs(UDMappers);
12613 
12614   SmallVector<ValueDecl *, 16> Decls;
12615   Decls.reserve(UniqueDecls);
12616   for (unsigned i = 0; i < UniqueDecls; ++i)
12617     Decls.push_back(Record.readDeclAs<ValueDecl>());
12618   C->setUniqueDecls(Decls);
12619 
12620   SmallVector<unsigned, 16> ListsPerDecl;
12621   ListsPerDecl.reserve(UniqueDecls);
12622   for (unsigned i = 0; i < UniqueDecls; ++i)
12623     ListsPerDecl.push_back(Record.readInt());
12624   C->setDeclNumLists(ListsPerDecl);
12625 
12626   SmallVector<unsigned, 32> ListSizes;
12627   ListSizes.reserve(TotalLists);
12628   for (unsigned i = 0; i < TotalLists; ++i)
12629     ListSizes.push_back(Record.readInt());
12630   C->setComponentListSizes(ListSizes);
12631 
12632   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12633   Components.reserve(TotalComponents);
12634   for (unsigned i = 0; i < TotalComponents; ++i) {
12635     Expr *AssociatedExpr = Record.readExpr();
12636     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12637     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12638         AssociatedExpr, AssociatedDecl));
12639   }
12640   C->setComponents(Components, ListSizes);
12641 }
12642 
12643 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12644   C->setLParenLoc(Record.readSourceLocation());
12645   C->setColonLoc(Record.readSourceLocation());
12646   C->setAllocator(Record.readSubExpr());
12647   unsigned NumVars = C->varlist_size();
12648   SmallVector<Expr *, 16> Vars;
12649   Vars.reserve(NumVars);
12650   for (unsigned i = 0; i != NumVars; ++i)
12651     Vars.push_back(Record.readSubExpr());
12652   C->setVarRefs(Vars);
12653 }
12654 
12655 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12656   VisitOMPClauseWithPreInit(C);
12657   C->setNumTeams(Record.readSubExpr());
12658   C->setLParenLoc(Record.readSourceLocation());
12659 }
12660 
12661 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12662   VisitOMPClauseWithPreInit(C);
12663   C->setThreadLimit(Record.readSubExpr());
12664   C->setLParenLoc(Record.readSourceLocation());
12665 }
12666 
12667 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12668   VisitOMPClauseWithPreInit(C);
12669   C->setPriority(Record.readSubExpr());
12670   C->setLParenLoc(Record.readSourceLocation());
12671 }
12672 
12673 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12674   VisitOMPClauseWithPreInit(C);
12675   C->setGrainsize(Record.readSubExpr());
12676   C->setLParenLoc(Record.readSourceLocation());
12677 }
12678 
12679 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12680   VisitOMPClauseWithPreInit(C);
12681   C->setNumTasks(Record.readSubExpr());
12682   C->setLParenLoc(Record.readSourceLocation());
12683 }
12684 
12685 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12686   C->setHint(Record.readSubExpr());
12687   C->setLParenLoc(Record.readSourceLocation());
12688 }
12689 
12690 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12691   VisitOMPClauseWithPreInit(C);
12692   C->setDistScheduleKind(
12693       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12694   C->setChunkSize(Record.readSubExpr());
12695   C->setLParenLoc(Record.readSourceLocation());
12696   C->setDistScheduleKindLoc(Record.readSourceLocation());
12697   C->setCommaLoc(Record.readSourceLocation());
12698 }
12699 
12700 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12701   C->setDefaultmapKind(
12702        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12703   C->setDefaultmapModifier(
12704       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12705   C->setLParenLoc(Record.readSourceLocation());
12706   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12707   C->setDefaultmapKindLoc(Record.readSourceLocation());
12708 }
12709 
12710 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12711   C->setLParenLoc(Record.readSourceLocation());
12712   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12713     C->setMotionModifier(
12714         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12715     C->setMotionModifierLoc(I, Record.readSourceLocation());
12716   }
12717   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12718   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12719   C->setColonLoc(Record.readSourceLocation());
12720   auto NumVars = C->varlist_size();
12721   auto UniqueDecls = C->getUniqueDeclarationsNum();
12722   auto TotalLists = C->getTotalComponentListNum();
12723   auto TotalComponents = C->getTotalComponentsNum();
12724 
12725   SmallVector<Expr *, 16> Vars;
12726   Vars.reserve(NumVars);
12727   for (unsigned i = 0; i != NumVars; ++i)
12728     Vars.push_back(Record.readSubExpr());
12729   C->setVarRefs(Vars);
12730 
12731   SmallVector<Expr *, 16> UDMappers;
12732   UDMappers.reserve(NumVars);
12733   for (unsigned I = 0; I < NumVars; ++I)
12734     UDMappers.push_back(Record.readSubExpr());
12735   C->setUDMapperRefs(UDMappers);
12736 
12737   SmallVector<ValueDecl *, 16> Decls;
12738   Decls.reserve(UniqueDecls);
12739   for (unsigned i = 0; i < UniqueDecls; ++i)
12740     Decls.push_back(Record.readDeclAs<ValueDecl>());
12741   C->setUniqueDecls(Decls);
12742 
12743   SmallVector<unsigned, 16> ListsPerDecl;
12744   ListsPerDecl.reserve(UniqueDecls);
12745   for (unsigned i = 0; i < UniqueDecls; ++i)
12746     ListsPerDecl.push_back(Record.readInt());
12747   C->setDeclNumLists(ListsPerDecl);
12748 
12749   SmallVector<unsigned, 32> ListSizes;
12750   ListSizes.reserve(TotalLists);
12751   for (unsigned i = 0; i < TotalLists; ++i)
12752     ListSizes.push_back(Record.readInt());
12753   C->setComponentListSizes(ListSizes);
12754 
12755   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12756   Components.reserve(TotalComponents);
12757   for (unsigned i = 0; i < TotalComponents; ++i) {
12758     Expr *AssociatedExpr = Record.readSubExpr();
12759     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12760     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12761         AssociatedExpr, AssociatedDecl));
12762   }
12763   C->setComponents(Components, ListSizes);
12764 }
12765 
12766 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12767   C->setLParenLoc(Record.readSourceLocation());
12768   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12769     C->setMotionModifier(
12770         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12771     C->setMotionModifierLoc(I, Record.readSourceLocation());
12772   }
12773   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12774   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12775   C->setColonLoc(Record.readSourceLocation());
12776   auto NumVars = C->varlist_size();
12777   auto UniqueDecls = C->getUniqueDeclarationsNum();
12778   auto TotalLists = C->getTotalComponentListNum();
12779   auto TotalComponents = C->getTotalComponentsNum();
12780 
12781   SmallVector<Expr *, 16> Vars;
12782   Vars.reserve(NumVars);
12783   for (unsigned i = 0; i != NumVars; ++i)
12784     Vars.push_back(Record.readSubExpr());
12785   C->setVarRefs(Vars);
12786 
12787   SmallVector<Expr *, 16> UDMappers;
12788   UDMappers.reserve(NumVars);
12789   for (unsigned I = 0; I < NumVars; ++I)
12790     UDMappers.push_back(Record.readSubExpr());
12791   C->setUDMapperRefs(UDMappers);
12792 
12793   SmallVector<ValueDecl *, 16> Decls;
12794   Decls.reserve(UniqueDecls);
12795   for (unsigned i = 0; i < UniqueDecls; ++i)
12796     Decls.push_back(Record.readDeclAs<ValueDecl>());
12797   C->setUniqueDecls(Decls);
12798 
12799   SmallVector<unsigned, 16> ListsPerDecl;
12800   ListsPerDecl.reserve(UniqueDecls);
12801   for (unsigned i = 0; i < UniqueDecls; ++i)
12802     ListsPerDecl.push_back(Record.readInt());
12803   C->setDeclNumLists(ListsPerDecl);
12804 
12805   SmallVector<unsigned, 32> ListSizes;
12806   ListSizes.reserve(TotalLists);
12807   for (unsigned i = 0; i < TotalLists; ++i)
12808     ListSizes.push_back(Record.readInt());
12809   C->setComponentListSizes(ListSizes);
12810 
12811   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12812   Components.reserve(TotalComponents);
12813   for (unsigned i = 0; i < TotalComponents; ++i) {
12814     Expr *AssociatedExpr = Record.readSubExpr();
12815     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12816     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12817         AssociatedExpr, AssociatedDecl));
12818   }
12819   C->setComponents(Components, ListSizes);
12820 }
12821 
12822 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12823   C->setLParenLoc(Record.readSourceLocation());
12824   auto NumVars = C->varlist_size();
12825   auto UniqueDecls = C->getUniqueDeclarationsNum();
12826   auto TotalLists = C->getTotalComponentListNum();
12827   auto TotalComponents = C->getTotalComponentsNum();
12828 
12829   SmallVector<Expr *, 16> Vars;
12830   Vars.reserve(NumVars);
12831   for (unsigned i = 0; i != NumVars; ++i)
12832     Vars.push_back(Record.readSubExpr());
12833   C->setVarRefs(Vars);
12834   Vars.clear();
12835   for (unsigned i = 0; i != NumVars; ++i)
12836     Vars.push_back(Record.readSubExpr());
12837   C->setPrivateCopies(Vars);
12838   Vars.clear();
12839   for (unsigned i = 0; i != NumVars; ++i)
12840     Vars.push_back(Record.readSubExpr());
12841   C->setInits(Vars);
12842 
12843   SmallVector<ValueDecl *, 16> Decls;
12844   Decls.reserve(UniqueDecls);
12845   for (unsigned i = 0; i < UniqueDecls; ++i)
12846     Decls.push_back(Record.readDeclAs<ValueDecl>());
12847   C->setUniqueDecls(Decls);
12848 
12849   SmallVector<unsigned, 16> ListsPerDecl;
12850   ListsPerDecl.reserve(UniqueDecls);
12851   for (unsigned i = 0; i < UniqueDecls; ++i)
12852     ListsPerDecl.push_back(Record.readInt());
12853   C->setDeclNumLists(ListsPerDecl);
12854 
12855   SmallVector<unsigned, 32> ListSizes;
12856   ListSizes.reserve(TotalLists);
12857   for (unsigned i = 0; i < TotalLists; ++i)
12858     ListSizes.push_back(Record.readInt());
12859   C->setComponentListSizes(ListSizes);
12860 
12861   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12862   Components.reserve(TotalComponents);
12863   for (unsigned i = 0; i < TotalComponents; ++i) {
12864     Expr *AssociatedExpr = Record.readSubExpr();
12865     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12866     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12867         AssociatedExpr, AssociatedDecl));
12868   }
12869   C->setComponents(Components, ListSizes);
12870 }
12871 
12872 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12873   C->setLParenLoc(Record.readSourceLocation());
12874   auto NumVars = C->varlist_size();
12875   auto UniqueDecls = C->getUniqueDeclarationsNum();
12876   auto TotalLists = C->getTotalComponentListNum();
12877   auto TotalComponents = C->getTotalComponentsNum();
12878 
12879   SmallVector<Expr *, 16> Vars;
12880   Vars.reserve(NumVars);
12881   for (unsigned i = 0; i != NumVars; ++i)
12882     Vars.push_back(Record.readSubExpr());
12883   C->setVarRefs(Vars);
12884 
12885   SmallVector<ValueDecl *, 16> Decls;
12886   Decls.reserve(UniqueDecls);
12887   for (unsigned i = 0; i < UniqueDecls; ++i)
12888     Decls.push_back(Record.readDeclAs<ValueDecl>());
12889   C->setUniqueDecls(Decls);
12890 
12891   SmallVector<unsigned, 16> ListsPerDecl;
12892   ListsPerDecl.reserve(UniqueDecls);
12893   for (unsigned i = 0; i < UniqueDecls; ++i)
12894     ListsPerDecl.push_back(Record.readInt());
12895   C->setDeclNumLists(ListsPerDecl);
12896 
12897   SmallVector<unsigned, 32> ListSizes;
12898   ListSizes.reserve(TotalLists);
12899   for (unsigned i = 0; i < TotalLists; ++i)
12900     ListSizes.push_back(Record.readInt());
12901   C->setComponentListSizes(ListSizes);
12902 
12903   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12904   Components.reserve(TotalComponents);
12905   for (unsigned i = 0; i < TotalComponents; ++i) {
12906     Expr *AssociatedExpr = Record.readSubExpr();
12907     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12908     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12909         AssociatedExpr, AssociatedDecl));
12910   }
12911   C->setComponents(Components, ListSizes);
12912 }
12913 
12914 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12915   C->setLParenLoc(Record.readSourceLocation());
12916   auto NumVars = C->varlist_size();
12917   auto UniqueDecls = C->getUniqueDeclarationsNum();
12918   auto TotalLists = C->getTotalComponentListNum();
12919   auto TotalComponents = C->getTotalComponentsNum();
12920 
12921   SmallVector<Expr *, 16> Vars;
12922   Vars.reserve(NumVars);
12923   for (unsigned i = 0; i != NumVars; ++i)
12924     Vars.push_back(Record.readSubExpr());
12925   C->setVarRefs(Vars);
12926   Vars.clear();
12927 
12928   SmallVector<ValueDecl *, 16> Decls;
12929   Decls.reserve(UniqueDecls);
12930   for (unsigned i = 0; i < UniqueDecls; ++i)
12931     Decls.push_back(Record.readDeclAs<ValueDecl>());
12932   C->setUniqueDecls(Decls);
12933 
12934   SmallVector<unsigned, 16> ListsPerDecl;
12935   ListsPerDecl.reserve(UniqueDecls);
12936   for (unsigned i = 0; i < UniqueDecls; ++i)
12937     ListsPerDecl.push_back(Record.readInt());
12938   C->setDeclNumLists(ListsPerDecl);
12939 
12940   SmallVector<unsigned, 32> ListSizes;
12941   ListSizes.reserve(TotalLists);
12942   for (unsigned i = 0; i < TotalLists; ++i)
12943     ListSizes.push_back(Record.readInt());
12944   C->setComponentListSizes(ListSizes);
12945 
12946   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12947   Components.reserve(TotalComponents);
12948   for (unsigned i = 0; i < TotalComponents; ++i) {
12949     Expr *AssociatedExpr = Record.readSubExpr();
12950     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12951     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12952         AssociatedExpr, AssociatedDecl));
12953   }
12954   C->setComponents(Components, ListSizes);
12955 }
12956 
12957 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12958   C->setLParenLoc(Record.readSourceLocation());
12959   unsigned NumVars = C->varlist_size();
12960   SmallVector<Expr *, 16> Vars;
12961   Vars.reserve(NumVars);
12962   for (unsigned i = 0; i != NumVars; ++i)
12963     Vars.push_back(Record.readSubExpr());
12964   C->setVarRefs(Vars);
12965   Vars.clear();
12966   Vars.reserve(NumVars);
12967   for (unsigned i = 0; i != NumVars; ++i)
12968     Vars.push_back(Record.readSubExpr());
12969   C->setPrivateRefs(Vars);
12970 }
12971 
12972 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12973   C->setLParenLoc(Record.readSourceLocation());
12974   unsigned NumVars = C->varlist_size();
12975   SmallVector<Expr *, 16> Vars;
12976   Vars.reserve(NumVars);
12977   for (unsigned i = 0; i != NumVars; ++i)
12978     Vars.push_back(Record.readSubExpr());
12979   C->setVarRefs(Vars);
12980 }
12981 
12982 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12983   C->setLParenLoc(Record.readSourceLocation());
12984   unsigned NumVars = C->varlist_size();
12985   SmallVector<Expr *, 16> Vars;
12986   Vars.reserve(NumVars);
12987   for (unsigned i = 0; i != NumVars; ++i)
12988     Vars.push_back(Record.readSubExpr());
12989   C->setVarRefs(Vars);
12990 }
12991 
12992 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12993   C->setLParenLoc(Record.readSourceLocation());
12994   unsigned NumOfAllocators = C->getNumberOfAllocators();
12995   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12996   Data.reserve(NumOfAllocators);
12997   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12998     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12999     D.Allocator = Record.readSubExpr();
13000     D.AllocatorTraits = Record.readSubExpr();
13001     D.LParenLoc = Record.readSourceLocation();
13002     D.RParenLoc = Record.readSourceLocation();
13003   }
13004   C->setAllocatorsData(Data);
13005 }
13006 
13007 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13008   C->setLParenLoc(Record.readSourceLocation());
13009   C->setModifier(Record.readSubExpr());
13010   C->setColonLoc(Record.readSourceLocation());
13011   unsigned NumOfLocators = C->varlist_size();
13012   SmallVector<Expr *, 4> Locators;
13013   Locators.reserve(NumOfLocators);
13014   for (unsigned I = 0; I != NumOfLocators; ++I)
13015     Locators.push_back(Record.readSubExpr());
13016   C->setVarRefs(Locators);
13017 }
13018 
13019 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13020   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13021   C->setLParenLoc(Record.readSourceLocation());
13022   C->setKindKwLoc(Record.readSourceLocation());
13023 }
13024 
13025 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13026   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13027   TI.Sets.resize(readUInt32());
13028   for (auto &Set : TI.Sets) {
13029     Set.Kind = readEnum<llvm::omp::TraitSet>();
13030     Set.Selectors.resize(readUInt32());
13031     for (auto &Selector : Set.Selectors) {
13032       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13033       Selector.ScoreOrCondition = nullptr;
13034       if (readBool())
13035         Selector.ScoreOrCondition = readExprRef();
13036       Selector.Properties.resize(readUInt32());
13037       for (auto &Property : Selector.Properties)
13038         Property.Kind = readEnum<llvm::omp::TraitProperty>();
13039     }
13040   }
13041   return &TI;
13042 }
13043 
13044 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13045   if (!Data)
13046     return;
13047   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13048     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13049     skipInts(3);
13050   }
13051   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13052   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13053     Clauses[I] = readOMPClause();
13054   Data->setClauses(Clauses);
13055   if (Data->hasAssociatedStmt())
13056     Data->setAssociatedStmt(readStmt());
13057   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13058     Data->getChildren()[I] = readStmt();
13059 }
13060