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/Serialization/ASTRecordReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/AbstractTypeReader.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/ASTMutationListener.h"
20 #include "clang/AST/ASTUnresolvedSet.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/DeclarationName.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
31 #include "clang/AST/ExternalASTSource.h"
32 #include "clang/AST/NestedNameSpecifier.h"
33 #include "clang/AST/OpenMPClause.h"
34 #include "clang/AST/ODRHash.h"
35 #include "clang/AST/RawCommentList.h"
36 #include "clang/AST/TemplateBase.h"
37 #include "clang/AST/TemplateName.h"
38 #include "clang/AST/Type.h"
39 #include "clang/AST/TypeLoc.h"
40 #include "clang/AST/TypeLocVisitor.h"
41 #include "clang/AST/UnresolvedSet.h"
42 #include "clang/Basic/CommentOptions.h"
43 #include "clang/Basic/Diagnostic.h"
44 #include "clang/Basic/DiagnosticOptions.h"
45 #include "clang/Basic/ExceptionSpecificationType.h"
46 #include "clang/Basic/FileManager.h"
47 #include "clang/Basic/FileSystemOptions.h"
48 #include "clang/Basic/IdentifierTable.h"
49 #include "clang/Basic/LLVM.h"
50 #include "clang/Basic/LangOptions.h"
51 #include "clang/Basic/Module.h"
52 #include "clang/Basic/ObjCRuntime.h"
53 #include "clang/Basic/OperatorKinds.h"
54 #include "clang/Basic/PragmaKinds.h"
55 #include "clang/Basic/Sanitizers.h"
56 #include "clang/Basic/SourceLocation.h"
57 #include "clang/Basic/SourceManager.h"
58 #include "clang/Basic/SourceManagerInternals.h"
59 #include "clang/Basic/Specifiers.h"
60 #include "clang/Basic/TargetInfo.h"
61 #include "clang/Basic/TargetOptions.h"
62 #include "clang/Basic/TokenKinds.h"
63 #include "clang/Basic/Version.h"
64 #include "clang/Lex/HeaderSearch.h"
65 #include "clang/Lex/HeaderSearchOptions.h"
66 #include "clang/Lex/MacroInfo.h"
67 #include "clang/Lex/ModuleMap.h"
68 #include "clang/Lex/PreprocessingRecord.h"
69 #include "clang/Lex/Preprocessor.h"
70 #include "clang/Lex/PreprocessorOptions.h"
71 #include "clang/Lex/Token.h"
72 #include "clang/Sema/ObjCMethodList.h"
73 #include "clang/Sema/Scope.h"
74 #include "clang/Sema/Sema.h"
75 #include "clang/Sema/Weak.h"
76 #include "clang/Serialization/ASTBitCodes.h"
77 #include "clang/Serialization/ASTDeserializationListener.h"
78 #include "clang/Serialization/ContinuousRangeMap.h"
79 #include "clang/Serialization/GlobalModuleIndex.h"
80 #include "clang/Serialization/InMemoryModuleCache.h"
81 #include "clang/Serialization/ModuleFile.h"
82 #include "clang/Serialization/ModuleFileExtension.h"
83 #include "clang/Serialization/ModuleManager.h"
84 #include "clang/Serialization/PCHContainerOperations.h"
85 #include "clang/Serialization/SerializationDiagnostic.h"
86 #include "llvm/ADT/APFloat.h"
87 #include "llvm/ADT/APInt.h"
88 #include "llvm/ADT/APSInt.h"
89 #include "llvm/ADT/ArrayRef.h"
90 #include "llvm/ADT/DenseMap.h"
91 #include "llvm/ADT/FoldingSet.h"
92 #include "llvm/ADT/Hashing.h"
93 #include "llvm/ADT/IntrusiveRefCntPtr.h"
94 #include "llvm/ADT/None.h"
95 #include "llvm/ADT/Optional.h"
96 #include "llvm/ADT/STLExtras.h"
97 #include "llvm/ADT/ScopeExit.h"
98 #include "llvm/ADT/SmallPtrSet.h"
99 #include "llvm/ADT/SmallString.h"
100 #include "llvm/ADT/SmallVector.h"
101 #include "llvm/ADT/StringExtras.h"
102 #include "llvm/ADT/StringMap.h"
103 #include "llvm/ADT/StringRef.h"
104 #include "llvm/ADT/Triple.h"
105 #include "llvm/ADT/iterator_range.h"
106 #include "llvm/Bitstream/BitstreamReader.h"
107 #include "llvm/Support/Casting.h"
108 #include "llvm/Support/Compiler.h"
109 #include "llvm/Support/Compression.h"
110 #include "llvm/Support/DJB.h"
111 #include "llvm/Support/Endian.h"
112 #include "llvm/Support/Error.h"
113 #include "llvm/Support/ErrorHandling.h"
114 #include "llvm/Support/FileSystem.h"
115 #include "llvm/Support/MemoryBuffer.h"
116 #include "llvm/Support/Path.h"
117 #include "llvm/Support/SaveAndRestore.h"
118 #include "llvm/Support/Timer.h"
119 #include "llvm/Support/VersionTuple.h"
120 #include "llvm/Support/raw_ostream.h"
121 #include <algorithm>
122 #include <cassert>
123 #include <cstddef>
124 #include <cstdint>
125 #include <cstdio>
126 #include <ctime>
127 #include <iterator>
128 #include <limits>
129 #include <map>
130 #include <memory>
131 #include <string>
132 #include <system_error>
133 #include <tuple>
134 #include <utility>
135 #include <vector>
136 
137 using namespace clang;
138 using namespace clang::serialization;
139 using namespace clang::serialization::reader;
140 using llvm::BitstreamCursor;
141 
142 //===----------------------------------------------------------------------===//
143 // ChainedASTReaderListener implementation
144 //===----------------------------------------------------------------------===//
145 
146 bool
147 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
148   return First->ReadFullVersionInformation(FullVersion) ||
149          Second->ReadFullVersionInformation(FullVersion);
150 }
151 
152 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
153   First->ReadModuleName(ModuleName);
154   Second->ReadModuleName(ModuleName);
155 }
156 
157 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
158   First->ReadModuleMapFile(ModuleMapPath);
159   Second->ReadModuleMapFile(ModuleMapPath);
160 }
161 
162 bool
163 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
164                                               bool Complain,
165                                               bool AllowCompatibleDifferences) {
166   return First->ReadLanguageOptions(LangOpts, Complain,
167                                     AllowCompatibleDifferences) ||
168          Second->ReadLanguageOptions(LangOpts, Complain,
169                                      AllowCompatibleDifferences);
170 }
171 
172 bool ChainedASTReaderListener::ReadTargetOptions(
173     const TargetOptions &TargetOpts, bool Complain,
174     bool AllowCompatibleDifferences) {
175   return First->ReadTargetOptions(TargetOpts, Complain,
176                                   AllowCompatibleDifferences) ||
177          Second->ReadTargetOptions(TargetOpts, Complain,
178                                    AllowCompatibleDifferences);
179 }
180 
181 bool ChainedASTReaderListener::ReadDiagnosticOptions(
182     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
183   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
184          Second->ReadDiagnosticOptions(DiagOpts, Complain);
185 }
186 
187 bool
188 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
189                                                 bool Complain) {
190   return First->ReadFileSystemOptions(FSOpts, Complain) ||
191          Second->ReadFileSystemOptions(FSOpts, Complain);
192 }
193 
194 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
195     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
196     bool Complain) {
197   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198                                         Complain) ||
199          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
200                                          Complain);
201 }
202 
203 bool ChainedASTReaderListener::ReadPreprocessorOptions(
204     const PreprocessorOptions &PPOpts, bool Complain,
205     std::string &SuggestedPredefines) {
206   return First->ReadPreprocessorOptions(PPOpts, Complain,
207                                         SuggestedPredefines) ||
208          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
209 }
210 
211 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
212                                            unsigned Value) {
213   First->ReadCounter(M, Value);
214   Second->ReadCounter(M, Value);
215 }
216 
217 bool ChainedASTReaderListener::needsInputFileVisitation() {
218   return First->needsInputFileVisitation() ||
219          Second->needsInputFileVisitation();
220 }
221 
222 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
223   return First->needsSystemInputFileVisitation() ||
224   Second->needsSystemInputFileVisitation();
225 }
226 
227 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
228                                                ModuleKind Kind) {
229   First->visitModuleFile(Filename, Kind);
230   Second->visitModuleFile(Filename, Kind);
231 }
232 
233 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
234                                               bool isSystem,
235                                               bool isOverridden,
236                                               bool isExplicitModule) {
237   bool Continue = false;
238   if (First->needsInputFileVisitation() &&
239       (!isSystem || First->needsSystemInputFileVisitation()))
240     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
241                                       isExplicitModule);
242   if (Second->needsInputFileVisitation() &&
243       (!isSystem || Second->needsSystemInputFileVisitation()))
244     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
245                                        isExplicitModule);
246   return Continue;
247 }
248 
249 void ChainedASTReaderListener::readModuleFileExtension(
250        const ModuleFileExtensionMetadata &Metadata) {
251   First->readModuleFileExtension(Metadata);
252   Second->readModuleFileExtension(Metadata);
253 }
254 
255 //===----------------------------------------------------------------------===//
256 // PCH validator implementation
257 //===----------------------------------------------------------------------===//
258 
259 ASTReaderListener::~ASTReaderListener() = default;
260 
261 /// Compare the given set of language options against an existing set of
262 /// language options.
263 ///
264 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
265 /// \param AllowCompatibleDifferences If true, differences between compatible
266 ///        language options will be permitted.
267 ///
268 /// \returns true if the languagae options mis-match, false otherwise.
269 static bool checkLanguageOptions(const LangOptions &LangOpts,
270                                  const LangOptions &ExistingLangOpts,
271                                  DiagnosticsEngine *Diags,
272                                  bool AllowCompatibleDifferences = true) {
273 #define LANGOPT(Name, Bits, Default, Description)                 \
274   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
275     if (Diags)                                                    \
276       Diags->Report(diag::err_pch_langopt_mismatch)               \
277         << Description << LangOpts.Name << ExistingLangOpts.Name; \
278     return true;                                                  \
279   }
280 
281 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
282   if (ExistingLangOpts.Name != LangOpts.Name) {           \
283     if (Diags)                                            \
284       Diags->Report(diag::err_pch_langopt_value_mismatch) \
285         << Description;                                   \
286     return true;                                          \
287   }
288 
289 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
290   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
291     if (Diags)                                                 \
292       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
293         << Description;                                        \
294     return true;                                               \
295   }
296 
297 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
298   if (!AllowCompatibleDifferences)                            \
299     LANGOPT(Name, Bits, Default, Description)
300 
301 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                                 \
303     ENUM_LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
306   if (!AllowCompatibleDifferences)                                 \
307     VALUE_LANGOPT(Name, Bits, Default, Description)
308 
309 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
310 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
311 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
312 #include "clang/Basic/LangOptions.def"
313 
314   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
315     if (Diags)
316       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
317     return true;
318   }
319 
320   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
321     if (Diags)
322       Diags->Report(diag::err_pch_langopt_value_mismatch)
323       << "target Objective-C runtime";
324     return true;
325   }
326 
327   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
328       LangOpts.CommentOpts.BlockCommandNames) {
329     if (Diags)
330       Diags->Report(diag::err_pch_langopt_value_mismatch)
331         << "block command names";
332     return true;
333   }
334 
335   // Sanitizer feature mismatches are treated as compatible differences. If
336   // compatible differences aren't allowed, we still only want to check for
337   // mismatches of non-modular sanitizers (the only ones which can affect AST
338   // generation).
339   if (!AllowCompatibleDifferences) {
340     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
341     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
342     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
343     ExistingSanitizers.clear(ModularSanitizers);
344     ImportedSanitizers.clear(ModularSanitizers);
345     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
346       const std::string Flag = "-fsanitize=";
347       if (Diags) {
348 #define SANITIZER(NAME, ID)                                                    \
349   {                                                                            \
350     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
351     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
352     if (InExistingModule != InImportedModule)                                  \
353       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
354           << InExistingModule << (Flag + NAME);                                \
355   }
356 #include "clang/Basic/Sanitizers.def"
357       }
358       return true;
359     }
360   }
361 
362   return false;
363 }
364 
365 /// Compare the given set of target options against an existing set of
366 /// target options.
367 ///
368 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
369 ///
370 /// \returns true if the target options mis-match, false otherwise.
371 static bool checkTargetOptions(const TargetOptions &TargetOpts,
372                                const TargetOptions &ExistingTargetOpts,
373                                DiagnosticsEngine *Diags,
374                                bool AllowCompatibleDifferences = true) {
375 #define CHECK_TARGET_OPT(Field, Name)                             \
376   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
377     if (Diags)                                                    \
378       Diags->Report(diag::err_pch_targetopt_mismatch)             \
379         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
380     return true;                                                  \
381   }
382 
383   // The triple and ABI must match exactly.
384   CHECK_TARGET_OPT(Triple, "target");
385   CHECK_TARGET_OPT(ABI, "target ABI");
386 
387   // We can tolerate different CPUs in many cases, notably when one CPU
388   // supports a strict superset of another. When allowing compatible
389   // differences skip this check.
390   if (!AllowCompatibleDifferences)
391     CHECK_TARGET_OPT(CPU, "target CPU");
392 
393 #undef CHECK_TARGET_OPT
394 
395   // Compare feature sets.
396   SmallVector<StringRef, 4> ExistingFeatures(
397                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
398                                              ExistingTargetOpts.FeaturesAsWritten.end());
399   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
400                                          TargetOpts.FeaturesAsWritten.end());
401   llvm::sort(ExistingFeatures);
402   llvm::sort(ReadFeatures);
403 
404   // We compute the set difference in both directions explicitly so that we can
405   // diagnose the differences differently.
406   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
407   std::set_difference(
408       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
409       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
410   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
411                       ExistingFeatures.begin(), ExistingFeatures.end(),
412                       std::back_inserter(UnmatchedReadFeatures));
413 
414   // If we are allowing compatible differences and the read feature set is
415   // a strict subset of the existing feature set, there is nothing to diagnose.
416   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
417     return false;
418 
419   if (Diags) {
420     for (StringRef Feature : UnmatchedReadFeatures)
421       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
422           << /* is-existing-feature */ false << Feature;
423     for (StringRef Feature : UnmatchedExistingFeatures)
424       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
425           << /* is-existing-feature */ true << Feature;
426   }
427 
428   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
429 }
430 
431 bool
432 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
433                                   bool Complain,
434                                   bool AllowCompatibleDifferences) {
435   const LangOptions &ExistingLangOpts = PP.getLangOpts();
436   return checkLanguageOptions(LangOpts, ExistingLangOpts,
437                               Complain ? &Reader.Diags : nullptr,
438                               AllowCompatibleDifferences);
439 }
440 
441 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
442                                      bool Complain,
443                                      bool AllowCompatibleDifferences) {
444   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
445   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
446                             Complain ? &Reader.Diags : nullptr,
447                             AllowCompatibleDifferences);
448 }
449 
450 namespace {
451 
452 using MacroDefinitionsMap =
453     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
454 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
455 
456 } // namespace
457 
458 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
459                                          DiagnosticsEngine &Diags,
460                                          bool Complain) {
461   using Level = DiagnosticsEngine::Level;
462 
463   // Check current mappings for new -Werror mappings, and the stored mappings
464   // for cases that were explicitly mapped to *not* be errors that are now
465   // errors because of options like -Werror.
466   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
467 
468   for (DiagnosticsEngine *MappingSource : MappingSources) {
469     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
470       diag::kind DiagID = DiagIDMappingPair.first;
471       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
472       if (CurLevel < DiagnosticsEngine::Error)
473         continue; // not significant
474       Level StoredLevel =
475           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
476       if (StoredLevel < DiagnosticsEngine::Error) {
477         if (Complain)
478           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
479               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
480         return true;
481       }
482     }
483   }
484 
485   return false;
486 }
487 
488 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
489   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
490   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
491     return true;
492   return Ext >= diag::Severity::Error;
493 }
494 
495 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
496                                     DiagnosticsEngine &Diags,
497                                     bool IsSystem, bool Complain) {
498   // Top-level options
499   if (IsSystem) {
500     if (Diags.getSuppressSystemWarnings())
501       return false;
502     // If -Wsystem-headers was not enabled before, be conservative
503     if (StoredDiags.getSuppressSystemWarnings()) {
504       if (Complain)
505         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
506       return true;
507     }
508   }
509 
510   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
511     if (Complain)
512       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
513     return true;
514   }
515 
516   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
517       !StoredDiags.getEnableAllWarnings()) {
518     if (Complain)
519       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
520     return true;
521   }
522 
523   if (isExtHandlingFromDiagsError(Diags) &&
524       !isExtHandlingFromDiagsError(StoredDiags)) {
525     if (Complain)
526       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
527     return true;
528   }
529 
530   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
531 }
532 
533 /// Return the top import module if it is implicit, nullptr otherwise.
534 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
535                                           Preprocessor &PP) {
536   // If the original import came from a file explicitly generated by the user,
537   // don't check the diagnostic mappings.
538   // FIXME: currently this is approximated by checking whether this is not a
539   // module import of an implicitly-loaded module file.
540   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
541   // the transitive closure of its imports, since unrelated modules cannot be
542   // imported until after this module finishes validation.
543   ModuleFile *TopImport = &*ModuleMgr.rbegin();
544   while (!TopImport->ImportedBy.empty())
545     TopImport = TopImport->ImportedBy[0];
546   if (TopImport->Kind != MK_ImplicitModule)
547     return nullptr;
548 
549   StringRef ModuleName = TopImport->ModuleName;
550   assert(!ModuleName.empty() && "diagnostic options read before module name");
551 
552   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
553   assert(M && "missing module");
554   return M;
555 }
556 
557 bool PCHValidator::ReadDiagnosticOptions(
558     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
559   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
560   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
561   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
562       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
563   // This should never fail, because we would have processed these options
564   // before writing them to an ASTFile.
565   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
566 
567   ModuleManager &ModuleMgr = Reader.getModuleManager();
568   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
569 
570   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
571   if (!TopM)
572     return false;
573 
574   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
575   // contains the union of their flags.
576   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
577                                  Complain);
578 }
579 
580 /// Collect the macro definitions provided by the given preprocessor
581 /// options.
582 static void
583 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
584                         MacroDefinitionsMap &Macros,
585                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
586   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
587     StringRef Macro = PPOpts.Macros[I].first;
588     bool IsUndef = PPOpts.Macros[I].second;
589 
590     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
591     StringRef MacroName = MacroPair.first;
592     StringRef MacroBody = MacroPair.second;
593 
594     // For an #undef'd macro, we only care about the name.
595     if (IsUndef) {
596       if (MacroNames && !Macros.count(MacroName))
597         MacroNames->push_back(MacroName);
598 
599       Macros[MacroName] = std::make_pair("", true);
600       continue;
601     }
602 
603     // For a #define'd macro, figure out the actual definition.
604     if (MacroName.size() == Macro.size())
605       MacroBody = "1";
606     else {
607       // Note: GCC drops anything following an end-of-line character.
608       StringRef::size_type End = MacroBody.find_first_of("\n\r");
609       MacroBody = MacroBody.substr(0, End);
610     }
611 
612     if (MacroNames && !Macros.count(MacroName))
613       MacroNames->push_back(MacroName);
614     Macros[MacroName] = std::make_pair(MacroBody, false);
615   }
616 }
617 
618 /// Check the preprocessor options deserialized from the control block
619 /// against the preprocessor options in an existing preprocessor.
620 ///
621 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
622 /// \param Validate If true, validate preprocessor options. If false, allow
623 ///        macros defined by \p ExistingPPOpts to override those defined by
624 ///        \p PPOpts in SuggestedPredefines.
625 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
626                                      const PreprocessorOptions &ExistingPPOpts,
627                                      DiagnosticsEngine *Diags,
628                                      FileManager &FileMgr,
629                                      std::string &SuggestedPredefines,
630                                      const LangOptions &LangOpts,
631                                      bool Validate = true) {
632   // Check macro definitions.
633   MacroDefinitionsMap ASTFileMacros;
634   collectMacroDefinitions(PPOpts, ASTFileMacros);
635   MacroDefinitionsMap ExistingMacros;
636   SmallVector<StringRef, 4> ExistingMacroNames;
637   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
638 
639   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
640     // Dig out the macro definition in the existing preprocessor options.
641     StringRef MacroName = ExistingMacroNames[I];
642     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
643 
644     // Check whether we know anything about this macro name or not.
645     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
646         ASTFileMacros.find(MacroName);
647     if (!Validate || Known == ASTFileMacros.end()) {
648       // FIXME: Check whether this identifier was referenced anywhere in the
649       // AST file. If so, we should reject the AST file. Unfortunately, this
650       // information isn't in the control block. What shall we do about it?
651 
652       if (Existing.second) {
653         SuggestedPredefines += "#undef ";
654         SuggestedPredefines += MacroName.str();
655         SuggestedPredefines += '\n';
656       } else {
657         SuggestedPredefines += "#define ";
658         SuggestedPredefines += MacroName.str();
659         SuggestedPredefines += ' ';
660         SuggestedPredefines += Existing.first.str();
661         SuggestedPredefines += '\n';
662       }
663       continue;
664     }
665 
666     // If the macro was defined in one but undef'd in the other, we have a
667     // conflict.
668     if (Existing.second != Known->second.second) {
669       if (Diags) {
670         Diags->Report(diag::err_pch_macro_def_undef)
671           << MacroName << Known->second.second;
672       }
673       return true;
674     }
675 
676     // If the macro was #undef'd in both, or if the macro bodies are identical,
677     // it's fine.
678     if (Existing.second || Existing.first == Known->second.first)
679       continue;
680 
681     // The macro bodies differ; complain.
682     if (Diags) {
683       Diags->Report(diag::err_pch_macro_def_conflict)
684         << MacroName << Known->second.first << Existing.first;
685     }
686     return true;
687   }
688 
689   // Check whether we're using predefines.
690   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
691     if (Diags) {
692       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
693     }
694     return true;
695   }
696 
697   // Detailed record is important since it is used for the module cache hash.
698   if (LangOpts.Modules &&
699       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
700     if (Diags) {
701       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
702     }
703     return true;
704   }
705 
706   // Compute the #include and #include_macros lines we need.
707   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
708     StringRef File = ExistingPPOpts.Includes[I];
709 
710     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
711         !ExistingPPOpts.PCHThroughHeader.empty()) {
712       // In case the through header is an include, we must add all the includes
713       // to the predefines so the start point can be determined.
714       SuggestedPredefines += "#include \"";
715       SuggestedPredefines += File;
716       SuggestedPredefines += "\"\n";
717       continue;
718     }
719 
720     if (File == ExistingPPOpts.ImplicitPCHInclude)
721       continue;
722 
723     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
724           != PPOpts.Includes.end())
725       continue;
726 
727     SuggestedPredefines += "#include \"";
728     SuggestedPredefines += File;
729     SuggestedPredefines += "\"\n";
730   }
731 
732   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
733     StringRef File = ExistingPPOpts.MacroIncludes[I];
734     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
735                   File)
736         != PPOpts.MacroIncludes.end())
737       continue;
738 
739     SuggestedPredefines += "#__include_macros \"";
740     SuggestedPredefines += File;
741     SuggestedPredefines += "\"\n##\n";
742   }
743 
744   return false;
745 }
746 
747 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
748                                            bool Complain,
749                                            std::string &SuggestedPredefines) {
750   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
751 
752   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
753                                   Complain? &Reader.Diags : nullptr,
754                                   PP.getFileManager(),
755                                   SuggestedPredefines,
756                                   PP.getLangOpts());
757 }
758 
759 bool SimpleASTReaderListener::ReadPreprocessorOptions(
760                                   const PreprocessorOptions &PPOpts,
761                                   bool Complain,
762                                   std::string &SuggestedPredefines) {
763   return checkPreprocessorOptions(PPOpts,
764                                   PP.getPreprocessorOpts(),
765                                   nullptr,
766                                   PP.getFileManager(),
767                                   SuggestedPredefines,
768                                   PP.getLangOpts(),
769                                   false);
770 }
771 
772 /// Check the header search options deserialized from the control block
773 /// against the header search options in an existing preprocessor.
774 ///
775 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
776 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
777                                      StringRef SpecificModuleCachePath,
778                                      StringRef ExistingModuleCachePath,
779                                      DiagnosticsEngine *Diags,
780                                      const LangOptions &LangOpts) {
781   if (LangOpts.Modules) {
782     if (SpecificModuleCachePath != ExistingModuleCachePath) {
783       if (Diags)
784         Diags->Report(diag::err_pch_modulecache_mismatch)
785           << SpecificModuleCachePath << ExistingModuleCachePath;
786       return true;
787     }
788   }
789 
790   return false;
791 }
792 
793 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
794                                            StringRef SpecificModuleCachePath,
795                                            bool Complain) {
796   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
797                                   PP.getHeaderSearchInfo().getModuleCachePath(),
798                                   Complain ? &Reader.Diags : nullptr,
799                                   PP.getLangOpts());
800 }
801 
802 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
803   PP.setCounterValue(Value);
804 }
805 
806 //===----------------------------------------------------------------------===//
807 // AST reader implementation
808 //===----------------------------------------------------------------------===//
809 
810 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
811                                            bool TakeOwnership) {
812   DeserializationListener = Listener;
813   OwnsDeserializationListener = TakeOwnership;
814 }
815 
816 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
817   return serialization::ComputeHash(Sel);
818 }
819 
820 std::pair<unsigned, unsigned>
821 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
822   using namespace llvm::support;
823 
824   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
825   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
826   return std::make_pair(KeyLen, DataLen);
827 }
828 
829 ASTSelectorLookupTrait::internal_key_type
830 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
831   using namespace llvm::support;
832 
833   SelectorTable &SelTable = Reader.getContext().Selectors;
834   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
835   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
836       F, endian::readNext<uint32_t, little, unaligned>(d));
837   if (N == 0)
838     return SelTable.getNullarySelector(FirstII);
839   else if (N == 1)
840     return SelTable.getUnarySelector(FirstII);
841 
842   SmallVector<IdentifierInfo *, 16> Args;
843   Args.push_back(FirstII);
844   for (unsigned I = 1; I != N; ++I)
845     Args.push_back(Reader.getLocalIdentifier(
846         F, endian::readNext<uint32_t, little, unaligned>(d)));
847 
848   return SelTable.getSelector(N, Args.data());
849 }
850 
851 ASTSelectorLookupTrait::data_type
852 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
853                                  unsigned DataLen) {
854   using namespace llvm::support;
855 
856   data_type Result;
857 
858   Result.ID = Reader.getGlobalSelectorID(
859       F, endian::readNext<uint32_t, little, unaligned>(d));
860   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
861   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
862   Result.InstanceBits = FullInstanceBits & 0x3;
863   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
864   Result.FactoryBits = FullFactoryBits & 0x3;
865   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
866   unsigned NumInstanceMethods = FullInstanceBits >> 3;
867   unsigned NumFactoryMethods = FullFactoryBits >> 3;
868 
869   // Load instance methods
870   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
871     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
872             F, endian::readNext<uint32_t, little, unaligned>(d)))
873       Result.Instance.push_back(Method);
874   }
875 
876   // Load factory methods
877   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
878     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
879             F, endian::readNext<uint32_t, little, unaligned>(d)))
880       Result.Factory.push_back(Method);
881   }
882 
883   return Result;
884 }
885 
886 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
887   return llvm::djbHash(a);
888 }
889 
890 std::pair<unsigned, unsigned>
891 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
892   using namespace llvm::support;
893 
894   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
895   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
896   return std::make_pair(KeyLen, DataLen);
897 }
898 
899 ASTIdentifierLookupTraitBase::internal_key_type
900 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
901   assert(n >= 2 && d[n-1] == '\0');
902   return StringRef((const char*) d, n-1);
903 }
904 
905 /// Whether the given identifier is "interesting".
906 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
907                                     bool IsModule) {
908   return II.hadMacroDefinition() ||
909          II.isPoisoned() ||
910          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
911          II.hasRevertedTokenIDToIdentifier() ||
912          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
913           II.getFETokenInfo());
914 }
915 
916 static bool readBit(unsigned &Bits) {
917   bool Value = Bits & 0x1;
918   Bits >>= 1;
919   return Value;
920 }
921 
922 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
923   using namespace llvm::support;
924 
925   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
926   return Reader.getGlobalIdentifierID(F, RawID >> 1);
927 }
928 
929 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
930   if (!II.isFromAST()) {
931     II.setIsFromAST();
932     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
933     if (isInterestingIdentifier(Reader, II, IsModule))
934       II.setChangedSinceDeserialization();
935   }
936 }
937 
938 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
939                                                    const unsigned char* d,
940                                                    unsigned DataLen) {
941   using namespace llvm::support;
942 
943   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
944   bool IsInteresting = RawID & 0x01;
945 
946   // Wipe out the "is interesting" bit.
947   RawID = RawID >> 1;
948 
949   // Build the IdentifierInfo and link the identifier ID with it.
950   IdentifierInfo *II = KnownII;
951   if (!II) {
952     II = &Reader.getIdentifierTable().getOwn(k);
953     KnownII = II;
954   }
955   markIdentifierFromAST(Reader, *II);
956   Reader.markIdentifierUpToDate(II);
957 
958   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
959   if (!IsInteresting) {
960     // For uninteresting identifiers, there's nothing else to do. Just notify
961     // the reader that we've finished loading this identifier.
962     Reader.SetIdentifierInfo(ID, II);
963     return II;
964   }
965 
966   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
967   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
968   bool CPlusPlusOperatorKeyword = readBit(Bits);
969   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
970   bool HasRevertedBuiltin = readBit(Bits);
971   bool Poisoned = readBit(Bits);
972   bool ExtensionToken = readBit(Bits);
973   bool HadMacroDefinition = readBit(Bits);
974 
975   assert(Bits == 0 && "Extra bits in the identifier?");
976   DataLen -= 8;
977 
978   // Set or check the various bits in the IdentifierInfo structure.
979   // Token IDs are read-only.
980   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
981     II->revertTokenIDToIdentifier();
982   if (!F.isModule())
983     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
984   else if (HasRevertedBuiltin && II->getBuiltinID()) {
985     II->revertBuiltin();
986     assert((II->hasRevertedBuiltin() ||
987             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
988            "Incorrect ObjC keyword or builtin ID");
989   }
990   assert(II->isExtensionToken() == ExtensionToken &&
991          "Incorrect extension token flag");
992   (void)ExtensionToken;
993   if (Poisoned)
994     II->setIsPoisoned(true);
995   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
996          "Incorrect C++ operator keyword flag");
997   (void)CPlusPlusOperatorKeyword;
998 
999   // If this identifier is a macro, deserialize the macro
1000   // definition.
1001   if (HadMacroDefinition) {
1002     uint32_t MacroDirectivesOffset =
1003         endian::readNext<uint32_t, little, unaligned>(d);
1004     DataLen -= 4;
1005 
1006     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1007   }
1008 
1009   Reader.SetIdentifierInfo(ID, II);
1010 
1011   // Read all of the declarations visible at global scope with this
1012   // name.
1013   if (DataLen > 0) {
1014     SmallVector<uint32_t, 4> DeclIDs;
1015     for (; DataLen > 0; DataLen -= 4)
1016       DeclIDs.push_back(Reader.getGlobalDeclID(
1017           F, endian::readNext<uint32_t, little, unaligned>(d)));
1018     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1019   }
1020 
1021   return II;
1022 }
1023 
1024 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1025     : Kind(Name.getNameKind()) {
1026   switch (Kind) {
1027   case DeclarationName::Identifier:
1028     Data = (uint64_t)Name.getAsIdentifierInfo();
1029     break;
1030   case DeclarationName::ObjCZeroArgSelector:
1031   case DeclarationName::ObjCOneArgSelector:
1032   case DeclarationName::ObjCMultiArgSelector:
1033     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1034     break;
1035   case DeclarationName::CXXOperatorName:
1036     Data = Name.getCXXOverloadedOperator();
1037     break;
1038   case DeclarationName::CXXLiteralOperatorName:
1039     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1040     break;
1041   case DeclarationName::CXXDeductionGuideName:
1042     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1043                ->getDeclName().getAsIdentifierInfo();
1044     break;
1045   case DeclarationName::CXXConstructorName:
1046   case DeclarationName::CXXDestructorName:
1047   case DeclarationName::CXXConversionFunctionName:
1048   case DeclarationName::CXXUsingDirective:
1049     Data = 0;
1050     break;
1051   }
1052 }
1053 
1054 unsigned DeclarationNameKey::getHash() const {
1055   llvm::FoldingSetNodeID ID;
1056   ID.AddInteger(Kind);
1057 
1058   switch (Kind) {
1059   case DeclarationName::Identifier:
1060   case DeclarationName::CXXLiteralOperatorName:
1061   case DeclarationName::CXXDeductionGuideName:
1062     ID.AddString(((IdentifierInfo*)Data)->getName());
1063     break;
1064   case DeclarationName::ObjCZeroArgSelector:
1065   case DeclarationName::ObjCOneArgSelector:
1066   case DeclarationName::ObjCMultiArgSelector:
1067     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1068     break;
1069   case DeclarationName::CXXOperatorName:
1070     ID.AddInteger((OverloadedOperatorKind)Data);
1071     break;
1072   case DeclarationName::CXXConstructorName:
1073   case DeclarationName::CXXDestructorName:
1074   case DeclarationName::CXXConversionFunctionName:
1075   case DeclarationName::CXXUsingDirective:
1076     break;
1077   }
1078 
1079   return ID.ComputeHash();
1080 }
1081 
1082 ModuleFile *
1083 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1084   using namespace llvm::support;
1085 
1086   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1087   return Reader.getLocalModuleFile(F, ModuleFileID);
1088 }
1089 
1090 std::pair<unsigned, unsigned>
1091 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1092   using namespace llvm::support;
1093 
1094   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1095   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1096   return std::make_pair(KeyLen, DataLen);
1097 }
1098 
1099 ASTDeclContextNameLookupTrait::internal_key_type
1100 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1101   using namespace llvm::support;
1102 
1103   auto Kind = (DeclarationName::NameKind)*d++;
1104   uint64_t Data;
1105   switch (Kind) {
1106   case DeclarationName::Identifier:
1107   case DeclarationName::CXXLiteralOperatorName:
1108   case DeclarationName::CXXDeductionGuideName:
1109     Data = (uint64_t)Reader.getLocalIdentifier(
1110         F, endian::readNext<uint32_t, little, unaligned>(d));
1111     break;
1112   case DeclarationName::ObjCZeroArgSelector:
1113   case DeclarationName::ObjCOneArgSelector:
1114   case DeclarationName::ObjCMultiArgSelector:
1115     Data =
1116         (uint64_t)Reader.getLocalSelector(
1117                              F, endian::readNext<uint32_t, little, unaligned>(
1118                                     d)).getAsOpaquePtr();
1119     break;
1120   case DeclarationName::CXXOperatorName:
1121     Data = *d++; // OverloadedOperatorKind
1122     break;
1123   case DeclarationName::CXXConstructorName:
1124   case DeclarationName::CXXDestructorName:
1125   case DeclarationName::CXXConversionFunctionName:
1126   case DeclarationName::CXXUsingDirective:
1127     Data = 0;
1128     break;
1129   }
1130 
1131   return DeclarationNameKey(Kind, Data);
1132 }
1133 
1134 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1135                                                  const unsigned char *d,
1136                                                  unsigned DataLen,
1137                                                  data_type_builder &Val) {
1138   using namespace llvm::support;
1139 
1140   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1141     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1142     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1143   }
1144 }
1145 
1146 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1147                                               BitstreamCursor &Cursor,
1148                                               uint64_t Offset,
1149                                               DeclContext *DC) {
1150   assert(Offset != 0);
1151 
1152   SavedStreamPosition SavedPosition(Cursor);
1153   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1154     Error(std::move(Err));
1155     return true;
1156   }
1157 
1158   RecordData Record;
1159   StringRef Blob;
1160   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1161   if (!MaybeCode) {
1162     Error(MaybeCode.takeError());
1163     return true;
1164   }
1165   unsigned Code = MaybeCode.get();
1166 
1167   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1168   if (!MaybeRecCode) {
1169     Error(MaybeRecCode.takeError());
1170     return true;
1171   }
1172   unsigned RecCode = MaybeRecCode.get();
1173   if (RecCode != DECL_CONTEXT_LEXICAL) {
1174     Error("Expected lexical block");
1175     return true;
1176   }
1177 
1178   assert(!isa<TranslationUnitDecl>(DC) &&
1179          "expected a TU_UPDATE_LEXICAL record for TU");
1180   // If we are handling a C++ class template instantiation, we can see multiple
1181   // lexical updates for the same record. It's important that we select only one
1182   // of them, so that field numbering works properly. Just pick the first one we
1183   // see.
1184   auto &Lex = LexicalDecls[DC];
1185   if (!Lex.first) {
1186     Lex = std::make_pair(
1187         &M, llvm::makeArrayRef(
1188                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1189                     Blob.data()),
1190                 Blob.size() / 4));
1191   }
1192   DC->setHasExternalLexicalStorage(true);
1193   return false;
1194 }
1195 
1196 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1197                                               BitstreamCursor &Cursor,
1198                                               uint64_t Offset,
1199                                               DeclID ID) {
1200   assert(Offset != 0);
1201 
1202   SavedStreamPosition SavedPosition(Cursor);
1203   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1204     Error(std::move(Err));
1205     return true;
1206   }
1207 
1208   RecordData Record;
1209   StringRef Blob;
1210   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1211   if (!MaybeCode) {
1212     Error(MaybeCode.takeError());
1213     return true;
1214   }
1215   unsigned Code = MaybeCode.get();
1216 
1217   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1218   if (!MaybeRecCode) {
1219     Error(MaybeRecCode.takeError());
1220     return true;
1221   }
1222   unsigned RecCode = MaybeRecCode.get();
1223   if (RecCode != DECL_CONTEXT_VISIBLE) {
1224     Error("Expected visible lookup table block");
1225     return true;
1226   }
1227 
1228   // We can't safely determine the primary context yet, so delay attaching the
1229   // lookup table until we're done with recursive deserialization.
1230   auto *Data = (const unsigned char*)Blob.data();
1231   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1232   return false;
1233 }
1234 
1235 void ASTReader::Error(StringRef Msg) const {
1236   Error(diag::err_fe_pch_malformed, Msg);
1237   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1238       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1239     Diag(diag::note_module_cache_path)
1240       << PP.getHeaderSearchInfo().getModuleCachePath();
1241   }
1242 }
1243 
1244 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1245                       StringRef Arg3) const {
1246   if (Diags.isDiagnosticInFlight())
1247     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1248   else
1249     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1250 }
1251 
1252 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1253                       unsigned Select) const {
1254   if (!Diags.isDiagnosticInFlight())
1255     Diag(DiagID) << Arg1 << Arg2 << Select;
1256 }
1257 
1258 void ASTReader::Error(llvm::Error &&Err) const {
1259   Error(toString(std::move(Err)));
1260 }
1261 
1262 //===----------------------------------------------------------------------===//
1263 // Source Manager Deserialization
1264 //===----------------------------------------------------------------------===//
1265 
1266 /// Read the line table in the source manager block.
1267 /// \returns true if there was an error.
1268 bool ASTReader::ParseLineTable(ModuleFile &F,
1269                                const RecordData &Record) {
1270   unsigned Idx = 0;
1271   LineTableInfo &LineTable = SourceMgr.getLineTable();
1272 
1273   // Parse the file names
1274   std::map<int, int> FileIDs;
1275   FileIDs[-1] = -1; // For unspecified filenames.
1276   for (unsigned I = 0; Record[Idx]; ++I) {
1277     // Extract the file name
1278     auto Filename = ReadPath(F, Record, Idx);
1279     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1280   }
1281   ++Idx;
1282 
1283   // Parse the line entries
1284   std::vector<LineEntry> Entries;
1285   while (Idx < Record.size()) {
1286     int FID = Record[Idx++];
1287     assert(FID >= 0 && "Serialized line entries for non-local file.");
1288     // Remap FileID from 1-based old view.
1289     FID += F.SLocEntryBaseID - 1;
1290 
1291     // Extract the line entries
1292     unsigned NumEntries = Record[Idx++];
1293     assert(NumEntries && "no line entries for file ID");
1294     Entries.clear();
1295     Entries.reserve(NumEntries);
1296     for (unsigned I = 0; I != NumEntries; ++I) {
1297       unsigned FileOffset = Record[Idx++];
1298       unsigned LineNo = Record[Idx++];
1299       int FilenameID = FileIDs[Record[Idx++]];
1300       SrcMgr::CharacteristicKind FileKind
1301         = (SrcMgr::CharacteristicKind)Record[Idx++];
1302       unsigned IncludeOffset = Record[Idx++];
1303       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1304                                        FileKind, IncludeOffset));
1305     }
1306     LineTable.AddEntry(FileID::get(FID), Entries);
1307   }
1308 
1309   return false;
1310 }
1311 
1312 /// Read a source manager block
1313 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1314   using namespace SrcMgr;
1315 
1316   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1317 
1318   // Set the source-location entry cursor to the current position in
1319   // the stream. This cursor will be used to read the contents of the
1320   // source manager block initially, and then lazily read
1321   // source-location entries as needed.
1322   SLocEntryCursor = F.Stream;
1323 
1324   // The stream itself is going to skip over the source manager block.
1325   if (llvm::Error Err = F.Stream.SkipBlock()) {
1326     Error(std::move(Err));
1327     return true;
1328   }
1329 
1330   // Enter the source manager block.
1331   if (llvm::Error Err =
1332           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1333     Error(std::move(Err));
1334     return true;
1335   }
1336 
1337   RecordData Record;
1338   while (true) {
1339     Expected<llvm::BitstreamEntry> MaybeE =
1340         SLocEntryCursor.advanceSkippingSubblocks();
1341     if (!MaybeE) {
1342       Error(MaybeE.takeError());
1343       return true;
1344     }
1345     llvm::BitstreamEntry E = MaybeE.get();
1346 
1347     switch (E.Kind) {
1348     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1349     case llvm::BitstreamEntry::Error:
1350       Error("malformed block record in AST file");
1351       return true;
1352     case llvm::BitstreamEntry::EndBlock:
1353       return false;
1354     case llvm::BitstreamEntry::Record:
1355       // The interesting case.
1356       break;
1357     }
1358 
1359     // Read a record.
1360     Record.clear();
1361     StringRef Blob;
1362     Expected<unsigned> MaybeRecord =
1363         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1364     if (!MaybeRecord) {
1365       Error(MaybeRecord.takeError());
1366       return true;
1367     }
1368     switch (MaybeRecord.get()) {
1369     default:  // Default behavior: ignore.
1370       break;
1371 
1372     case SM_SLOC_FILE_ENTRY:
1373     case SM_SLOC_BUFFER_ENTRY:
1374     case SM_SLOC_EXPANSION_ENTRY:
1375       // Once we hit one of the source location entries, we're done.
1376       return false;
1377     }
1378   }
1379 }
1380 
1381 /// If a header file is not found at the path that we expect it to be
1382 /// and the PCH file was moved from its original location, try to resolve the
1383 /// file by assuming that header+PCH were moved together and the header is in
1384 /// the same place relative to the PCH.
1385 static std::string
1386 resolveFileRelativeToOriginalDir(const std::string &Filename,
1387                                  const std::string &OriginalDir,
1388                                  const std::string &CurrDir) {
1389   assert(OriginalDir != CurrDir &&
1390          "No point trying to resolve the file if the PCH dir didn't change");
1391 
1392   using namespace llvm::sys;
1393 
1394   SmallString<128> filePath(Filename);
1395   fs::make_absolute(filePath);
1396   assert(path::is_absolute(OriginalDir));
1397   SmallString<128> currPCHPath(CurrDir);
1398 
1399   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1400                        fileDirE = path::end(path::parent_path(filePath));
1401   path::const_iterator origDirI = path::begin(OriginalDir),
1402                        origDirE = path::end(OriginalDir);
1403   // Skip the common path components from filePath and OriginalDir.
1404   while (fileDirI != fileDirE && origDirI != origDirE &&
1405          *fileDirI == *origDirI) {
1406     ++fileDirI;
1407     ++origDirI;
1408   }
1409   for (; origDirI != origDirE; ++origDirI)
1410     path::append(currPCHPath, "..");
1411   path::append(currPCHPath, fileDirI, fileDirE);
1412   path::append(currPCHPath, path::filename(Filename));
1413   return currPCHPath.str();
1414 }
1415 
1416 bool ASTReader::ReadSLocEntry(int ID) {
1417   if (ID == 0)
1418     return false;
1419 
1420   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1421     Error("source location entry ID out-of-range for AST file");
1422     return true;
1423   }
1424 
1425   // Local helper to read the (possibly-compressed) buffer data following the
1426   // entry record.
1427   auto ReadBuffer = [this](
1428       BitstreamCursor &SLocEntryCursor,
1429       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1430     RecordData Record;
1431     StringRef Blob;
1432     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1433     if (!MaybeCode) {
1434       Error(MaybeCode.takeError());
1435       return nullptr;
1436     }
1437     unsigned Code = MaybeCode.get();
1438 
1439     Expected<unsigned> MaybeRecCode =
1440         SLocEntryCursor.readRecord(Code, Record, &Blob);
1441     if (!MaybeRecCode) {
1442       Error(MaybeRecCode.takeError());
1443       return nullptr;
1444     }
1445     unsigned RecCode = MaybeRecCode.get();
1446 
1447     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1448       if (!llvm::zlib::isAvailable()) {
1449         Error("zlib is not available");
1450         return nullptr;
1451       }
1452       SmallString<0> Uncompressed;
1453       if (llvm::Error E =
1454               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1455         Error("could not decompress embedded file contents: " +
1456               llvm::toString(std::move(E)));
1457         return nullptr;
1458       }
1459       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1460     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1461       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1462     } else {
1463       Error("AST record has invalid code");
1464       return nullptr;
1465     }
1466   };
1467 
1468   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1469   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1470           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1471     Error(std::move(Err));
1472     return true;
1473   }
1474 
1475   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1476   unsigned BaseOffset = F->SLocEntryBaseOffset;
1477 
1478   ++NumSLocEntriesRead;
1479   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1480   if (!MaybeEntry) {
1481     Error(MaybeEntry.takeError());
1482     return true;
1483   }
1484   llvm::BitstreamEntry Entry = MaybeEntry.get();
1485 
1486   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1487     Error("incorrectly-formatted source location entry in AST file");
1488     return true;
1489   }
1490 
1491   RecordData Record;
1492   StringRef Blob;
1493   Expected<unsigned> MaybeSLOC =
1494       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1495   if (!MaybeSLOC) {
1496     Error(MaybeSLOC.takeError());
1497     return true;
1498   }
1499   switch (MaybeSLOC.get()) {
1500   default:
1501     Error("incorrectly-formatted source location entry in AST file");
1502     return true;
1503 
1504   case SM_SLOC_FILE_ENTRY: {
1505     // We will detect whether a file changed and return 'Failure' for it, but
1506     // we will also try to fail gracefully by setting up the SLocEntry.
1507     unsigned InputID = Record[4];
1508     InputFile IF = getInputFile(*F, InputID);
1509     const FileEntry *File = IF.getFile();
1510     bool OverriddenBuffer = IF.isOverridden();
1511 
1512     // Note that we only check if a File was returned. If it was out-of-date
1513     // we have complained but we will continue creating a FileID to recover
1514     // gracefully.
1515     if (!File)
1516       return true;
1517 
1518     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1519     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1520       // This is the module's main file.
1521       IncludeLoc = getImportLocation(F);
1522     }
1523     SrcMgr::CharacteristicKind
1524       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1525     // FIXME: The FileID should be created from the FileEntryRef.
1526     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1527                                         ID, BaseOffset + Record[0]);
1528     SrcMgr::FileInfo &FileInfo =
1529           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1530     FileInfo.NumCreatedFIDs = Record[5];
1531     if (Record[3])
1532       FileInfo.setHasLineDirectives();
1533 
1534     unsigned NumFileDecls = Record[7];
1535     if (NumFileDecls && ContextObj) {
1536       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1537       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1538       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1539                                                              NumFileDecls));
1540     }
1541 
1542     const SrcMgr::ContentCache *ContentCache
1543       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1544     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1545         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1546         !ContentCache->getRawBuffer()) {
1547       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1548       if (!Buffer)
1549         return true;
1550       SourceMgr.overrideFileContents(File, std::move(Buffer));
1551     }
1552 
1553     break;
1554   }
1555 
1556   case SM_SLOC_BUFFER_ENTRY: {
1557     const char *Name = Blob.data();
1558     unsigned Offset = Record[0];
1559     SrcMgr::CharacteristicKind
1560       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1561     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1562     if (IncludeLoc.isInvalid() && F->isModule()) {
1563       IncludeLoc = getImportLocation(F);
1564     }
1565 
1566     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1567     if (!Buffer)
1568       return true;
1569     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1570                            BaseOffset + Offset, IncludeLoc);
1571     break;
1572   }
1573 
1574   case SM_SLOC_EXPANSION_ENTRY: {
1575     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1576     SourceMgr.createExpansionLoc(SpellingLoc,
1577                                      ReadSourceLocation(*F, Record[2]),
1578                                      ReadSourceLocation(*F, Record[3]),
1579                                      Record[5],
1580                                      Record[4],
1581                                      ID,
1582                                      BaseOffset + Record[0]);
1583     break;
1584   }
1585   }
1586 
1587   return false;
1588 }
1589 
1590 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1591   if (ID == 0)
1592     return std::make_pair(SourceLocation(), "");
1593 
1594   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1595     Error("source location entry ID out-of-range for AST file");
1596     return std::make_pair(SourceLocation(), "");
1597   }
1598 
1599   // Find which module file this entry lands in.
1600   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1601   if (!M->isModule())
1602     return std::make_pair(SourceLocation(), "");
1603 
1604   // FIXME: Can we map this down to a particular submodule? That would be
1605   // ideal.
1606   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1607 }
1608 
1609 /// Find the location where the module F is imported.
1610 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1611   if (F->ImportLoc.isValid())
1612     return F->ImportLoc;
1613 
1614   // Otherwise we have a PCH. It's considered to be "imported" at the first
1615   // location of its includer.
1616   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1617     // Main file is the importer.
1618     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1619     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1620   }
1621   return F->ImportedBy[0]->FirstLoc;
1622 }
1623 
1624 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1625 /// the abbreviations that are at the top of the block and then leave the cursor
1626 /// pointing into the block.
1627 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
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   while (true) {
1635     uint64_t Offset = Cursor.GetCurrentBitNo();
1636     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1637     if (!MaybeCode) {
1638       // FIXME this drops errors on the floor.
1639       consumeError(MaybeCode.takeError());
1640       return true;
1641     }
1642     unsigned Code = MaybeCode.get();
1643 
1644     // We expect all abbrevs to be at the start of the block.
1645     if (Code != llvm::bitc::DEFINE_ABBREV) {
1646       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1647         // FIXME this drops errors on the floor.
1648         consumeError(std::move(Err));
1649         return true;
1650       }
1651       return false;
1652     }
1653     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1654       // FIXME this drops errors on the floor.
1655       consumeError(std::move(Err));
1656       return true;
1657     }
1658   }
1659 }
1660 
1661 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1662                            unsigned &Idx) {
1663   Token Tok;
1664   Tok.startToken();
1665   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1666   Tok.setLength(Record[Idx++]);
1667   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1668     Tok.setIdentifierInfo(II);
1669   Tok.setKind((tok::TokenKind)Record[Idx++]);
1670   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1671   return Tok;
1672 }
1673 
1674 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1675   BitstreamCursor &Stream = F.MacroCursor;
1676 
1677   // Keep track of where we are in the stream, then jump back there
1678   // after reading this macro.
1679   SavedStreamPosition SavedPosition(Stream);
1680 
1681   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1682     // FIXME this drops errors on the floor.
1683     consumeError(std::move(Err));
1684     return nullptr;
1685   }
1686   RecordData Record;
1687   SmallVector<IdentifierInfo*, 16> MacroParams;
1688   MacroInfo *Macro = nullptr;
1689 
1690   while (true) {
1691     // Advance to the next record, but if we get to the end of the block, don't
1692     // pop it (removing all the abbreviations from the cursor) since we want to
1693     // be able to reseek within the block and read entries.
1694     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1695     Expected<llvm::BitstreamEntry> MaybeEntry =
1696         Stream.advanceSkippingSubblocks(Flags);
1697     if (!MaybeEntry) {
1698       Error(MaybeEntry.takeError());
1699       return Macro;
1700     }
1701     llvm::BitstreamEntry Entry = MaybeEntry.get();
1702 
1703     switch (Entry.Kind) {
1704     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1705     case llvm::BitstreamEntry::Error:
1706       Error("malformed block record in AST file");
1707       return Macro;
1708     case llvm::BitstreamEntry::EndBlock:
1709       return Macro;
1710     case llvm::BitstreamEntry::Record:
1711       // The interesting case.
1712       break;
1713     }
1714 
1715     // Read a record.
1716     Record.clear();
1717     PreprocessorRecordTypes RecType;
1718     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1719       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1720     else {
1721       Error(MaybeRecType.takeError());
1722       return Macro;
1723     }
1724     switch (RecType) {
1725     case PP_MODULE_MACRO:
1726     case PP_MACRO_DIRECTIVE_HISTORY:
1727       return Macro;
1728 
1729     case PP_MACRO_OBJECT_LIKE:
1730     case PP_MACRO_FUNCTION_LIKE: {
1731       // If we already have a macro, that means that we've hit the end
1732       // of the definition of the macro we were looking for. We're
1733       // done.
1734       if (Macro)
1735         return Macro;
1736 
1737       unsigned NextIndex = 1; // Skip identifier ID.
1738       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1739       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1740       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1741       MI->setIsUsed(Record[NextIndex++]);
1742       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1743 
1744       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1745         // Decode function-like macro info.
1746         bool isC99VarArgs = Record[NextIndex++];
1747         bool isGNUVarArgs = Record[NextIndex++];
1748         bool hasCommaPasting = Record[NextIndex++];
1749         MacroParams.clear();
1750         unsigned NumArgs = Record[NextIndex++];
1751         for (unsigned i = 0; i != NumArgs; ++i)
1752           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1753 
1754         // Install function-like macro info.
1755         MI->setIsFunctionLike();
1756         if (isC99VarArgs) MI->setIsC99Varargs();
1757         if (isGNUVarArgs) MI->setIsGNUVarargs();
1758         if (hasCommaPasting) MI->setHasCommaPasting();
1759         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1760       }
1761 
1762       // Remember that we saw this macro last so that we add the tokens that
1763       // form its body to it.
1764       Macro = MI;
1765 
1766       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1767           Record[NextIndex]) {
1768         // We have a macro definition. Register the association
1769         PreprocessedEntityID
1770             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1771         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1772         PreprocessingRecord::PPEntityID PPID =
1773             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1774         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1775             PPRec.getPreprocessedEntity(PPID));
1776         if (PPDef)
1777           PPRec.RegisterMacroDefinition(Macro, PPDef);
1778       }
1779 
1780       ++NumMacrosRead;
1781       break;
1782     }
1783 
1784     case PP_TOKEN: {
1785       // If we see a TOKEN before a PP_MACRO_*, then the file is
1786       // erroneous, just pretend we didn't see this.
1787       if (!Macro) break;
1788 
1789       unsigned Idx = 0;
1790       Token Tok = ReadToken(F, Record, Idx);
1791       Macro->AddTokenToBody(Tok);
1792       break;
1793     }
1794     }
1795   }
1796 }
1797 
1798 PreprocessedEntityID
1799 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1800                                          unsigned LocalID) const {
1801   if (!M.ModuleOffsetMap.empty())
1802     ReadModuleOffsetMap(M);
1803 
1804   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1805     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1806   assert(I != M.PreprocessedEntityRemap.end()
1807          && "Invalid index into preprocessed entity index remap");
1808 
1809   return LocalID + I->second;
1810 }
1811 
1812 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1813   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1814 }
1815 
1816 HeaderFileInfoTrait::internal_key_type
1817 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1818   internal_key_type ikey = {FE->getSize(),
1819                             M.HasTimestamps ? FE->getModificationTime() : 0,
1820                             FE->getName(), /*Imported*/ false};
1821   return ikey;
1822 }
1823 
1824 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1825   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1826     return false;
1827 
1828   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1829     return true;
1830 
1831   // Determine whether the actual files are equivalent.
1832   FileManager &FileMgr = Reader.getFileManager();
1833   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1834     if (!Key.Imported) {
1835       if (auto File = FileMgr.getFile(Key.Filename))
1836         return *File;
1837       return nullptr;
1838     }
1839 
1840     std::string Resolved = Key.Filename;
1841     Reader.ResolveImportedPath(M, Resolved);
1842     if (auto File = FileMgr.getFile(Resolved))
1843       return *File;
1844     return nullptr;
1845   };
1846 
1847   const FileEntry *FEA = GetFile(a);
1848   const FileEntry *FEB = GetFile(b);
1849   return FEA && FEA == FEB;
1850 }
1851 
1852 std::pair<unsigned, unsigned>
1853 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1854   using namespace llvm::support;
1855 
1856   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1857   unsigned DataLen = (unsigned) *d++;
1858   return std::make_pair(KeyLen, DataLen);
1859 }
1860 
1861 HeaderFileInfoTrait::internal_key_type
1862 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1863   using namespace llvm::support;
1864 
1865   internal_key_type ikey;
1866   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1867   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1868   ikey.Filename = (const char *)d;
1869   ikey.Imported = true;
1870   return ikey;
1871 }
1872 
1873 HeaderFileInfoTrait::data_type
1874 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1875                               unsigned DataLen) {
1876   using namespace llvm::support;
1877 
1878   const unsigned char *End = d + DataLen;
1879   HeaderFileInfo HFI;
1880   unsigned Flags = *d++;
1881   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1882   HFI.isImport |= (Flags >> 5) & 0x01;
1883   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1884   HFI.DirInfo = (Flags >> 1) & 0x07;
1885   HFI.IndexHeaderMapHeader = Flags & 0x01;
1886   // FIXME: Find a better way to handle this. Maybe just store a
1887   // "has been included" flag?
1888   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1889                              HFI.NumIncludes);
1890   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1891       M, endian::readNext<uint32_t, little, unaligned>(d));
1892   if (unsigned FrameworkOffset =
1893           endian::readNext<uint32_t, little, unaligned>(d)) {
1894     // The framework offset is 1 greater than the actual offset,
1895     // since 0 is used as an indicator for "no framework name".
1896     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1897     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1898   }
1899 
1900   assert((End - d) % 4 == 0 &&
1901          "Wrong data length in HeaderFileInfo deserialization");
1902   while (d != End) {
1903     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1904     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1905     LocalSMID >>= 2;
1906 
1907     // This header is part of a module. Associate it with the module to enable
1908     // implicit module import.
1909     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1910     Module *Mod = Reader.getSubmodule(GlobalSMID);
1911     FileManager &FileMgr = Reader.getFileManager();
1912     ModuleMap &ModMap =
1913         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1914 
1915     std::string Filename = key.Filename;
1916     if (key.Imported)
1917       Reader.ResolveImportedPath(M, Filename);
1918     // FIXME: This is not always the right filename-as-written, but we're not
1919     // going to use this information to rebuild the module, so it doesn't make
1920     // a lot of difference.
1921     Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1922     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1923     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1924   }
1925 
1926   // This HeaderFileInfo was externally loaded.
1927   HFI.External = true;
1928   HFI.IsValid = true;
1929   return HFI;
1930 }
1931 
1932 void ASTReader::addPendingMacro(IdentifierInfo *II,
1933                                 ModuleFile *M,
1934                                 uint64_t MacroDirectivesOffset) {
1935   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1936   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1937 }
1938 
1939 void ASTReader::ReadDefinedMacros() {
1940   // Note that we are loading defined macros.
1941   Deserializing Macros(this);
1942 
1943   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1944     BitstreamCursor &MacroCursor = I.MacroCursor;
1945 
1946     // If there was no preprocessor block, skip this file.
1947     if (MacroCursor.getBitcodeBytes().empty())
1948       continue;
1949 
1950     BitstreamCursor Cursor = MacroCursor;
1951     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1952       Error(std::move(Err));
1953       return;
1954     }
1955 
1956     RecordData Record;
1957     while (true) {
1958       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1959       if (!MaybeE) {
1960         Error(MaybeE.takeError());
1961         return;
1962       }
1963       llvm::BitstreamEntry E = MaybeE.get();
1964 
1965       switch (E.Kind) {
1966       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1967       case llvm::BitstreamEntry::Error:
1968         Error("malformed block record in AST file");
1969         return;
1970       case llvm::BitstreamEntry::EndBlock:
1971         goto NextCursor;
1972 
1973       case llvm::BitstreamEntry::Record: {
1974         Record.clear();
1975         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1976         if (!MaybeRecord) {
1977           Error(MaybeRecord.takeError());
1978           return;
1979         }
1980         switch (MaybeRecord.get()) {
1981         default:  // Default behavior: ignore.
1982           break;
1983 
1984         case PP_MACRO_OBJECT_LIKE:
1985         case PP_MACRO_FUNCTION_LIKE: {
1986           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1987           if (II->isOutOfDate())
1988             updateOutOfDateIdentifier(*II);
1989           break;
1990         }
1991 
1992         case PP_TOKEN:
1993           // Ignore tokens.
1994           break;
1995         }
1996         break;
1997       }
1998       }
1999     }
2000     NextCursor:  ;
2001   }
2002 }
2003 
2004 namespace {
2005 
2006   /// Visitor class used to look up identifirs in an AST file.
2007   class IdentifierLookupVisitor {
2008     StringRef Name;
2009     unsigned NameHash;
2010     unsigned PriorGeneration;
2011     unsigned &NumIdentifierLookups;
2012     unsigned &NumIdentifierLookupHits;
2013     IdentifierInfo *Found = nullptr;
2014 
2015   public:
2016     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2017                             unsigned &NumIdentifierLookups,
2018                             unsigned &NumIdentifierLookupHits)
2019       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2020         PriorGeneration(PriorGeneration),
2021         NumIdentifierLookups(NumIdentifierLookups),
2022         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2023 
2024     bool operator()(ModuleFile &M) {
2025       // If we've already searched this module file, skip it now.
2026       if (M.Generation <= PriorGeneration)
2027         return true;
2028 
2029       ASTIdentifierLookupTable *IdTable
2030         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2031       if (!IdTable)
2032         return false;
2033 
2034       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2035                                      Found);
2036       ++NumIdentifierLookups;
2037       ASTIdentifierLookupTable::iterator Pos =
2038           IdTable->find_hashed(Name, NameHash, &Trait);
2039       if (Pos == IdTable->end())
2040         return false;
2041 
2042       // Dereferencing the iterator has the effect of building the
2043       // IdentifierInfo node and populating it with the various
2044       // declarations it needs.
2045       ++NumIdentifierLookupHits;
2046       Found = *Pos;
2047       return true;
2048     }
2049 
2050     // Retrieve the identifier info found within the module
2051     // files.
2052     IdentifierInfo *getIdentifierInfo() const { return Found; }
2053   };
2054 
2055 } // namespace
2056 
2057 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2058   // Note that we are loading an identifier.
2059   Deserializing AnIdentifier(this);
2060 
2061   unsigned PriorGeneration = 0;
2062   if (getContext().getLangOpts().Modules)
2063     PriorGeneration = IdentifierGeneration[&II];
2064 
2065   // If there is a global index, look there first to determine which modules
2066   // provably do not have any results for this identifier.
2067   GlobalModuleIndex::HitSet Hits;
2068   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2069   if (!loadGlobalIndex()) {
2070     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2071       HitsPtr = &Hits;
2072     }
2073   }
2074 
2075   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2076                                   NumIdentifierLookups,
2077                                   NumIdentifierLookupHits);
2078   ModuleMgr.visit(Visitor, HitsPtr);
2079   markIdentifierUpToDate(&II);
2080 }
2081 
2082 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2083   if (!II)
2084     return;
2085 
2086   II->setOutOfDate(false);
2087 
2088   // Update the generation for this identifier.
2089   if (getContext().getLangOpts().Modules)
2090     IdentifierGeneration[II] = getGeneration();
2091 }
2092 
2093 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2094                                     const PendingMacroInfo &PMInfo) {
2095   ModuleFile &M = *PMInfo.M;
2096 
2097   BitstreamCursor &Cursor = M.MacroCursor;
2098   SavedStreamPosition SavedPosition(Cursor);
2099   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2100     Error(std::move(Err));
2101     return;
2102   }
2103 
2104   struct ModuleMacroRecord {
2105     SubmoduleID SubModID;
2106     MacroInfo *MI;
2107     SmallVector<SubmoduleID, 8> Overrides;
2108   };
2109   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2110 
2111   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2112   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2113   // macro histroy.
2114   RecordData Record;
2115   while (true) {
2116     Expected<llvm::BitstreamEntry> MaybeEntry =
2117         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2118     if (!MaybeEntry) {
2119       Error(MaybeEntry.takeError());
2120       return;
2121     }
2122     llvm::BitstreamEntry Entry = MaybeEntry.get();
2123 
2124     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2125       Error("malformed block record in AST file");
2126       return;
2127     }
2128 
2129     Record.clear();
2130     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2131     if (!MaybePP) {
2132       Error(MaybePP.takeError());
2133       return;
2134     }
2135     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2136     case PP_MACRO_DIRECTIVE_HISTORY:
2137       break;
2138 
2139     case PP_MODULE_MACRO: {
2140       ModuleMacros.push_back(ModuleMacroRecord());
2141       auto &Info = ModuleMacros.back();
2142       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2143       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2144       for (int I = 2, N = Record.size(); I != N; ++I)
2145         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2146       continue;
2147     }
2148 
2149     default:
2150       Error("malformed block record in AST file");
2151       return;
2152     }
2153 
2154     // We found the macro directive history; that's the last record
2155     // for this macro.
2156     break;
2157   }
2158 
2159   // Module macros are listed in reverse dependency order.
2160   {
2161     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2162     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2163     for (auto &MMR : ModuleMacros) {
2164       Overrides.clear();
2165       for (unsigned ModID : MMR.Overrides) {
2166         Module *Mod = getSubmodule(ModID);
2167         auto *Macro = PP.getModuleMacro(Mod, II);
2168         assert(Macro && "missing definition for overridden macro");
2169         Overrides.push_back(Macro);
2170       }
2171 
2172       bool Inserted = false;
2173       Module *Owner = getSubmodule(MMR.SubModID);
2174       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2175     }
2176   }
2177 
2178   // Don't read the directive history for a module; we don't have anywhere
2179   // to put it.
2180   if (M.isModule())
2181     return;
2182 
2183   // Deserialize the macro directives history in reverse source-order.
2184   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2185   unsigned Idx = 0, N = Record.size();
2186   while (Idx < N) {
2187     MacroDirective *MD = nullptr;
2188     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2189     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2190     switch (K) {
2191     case MacroDirective::MD_Define: {
2192       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2193       MD = PP.AllocateDefMacroDirective(MI, Loc);
2194       break;
2195     }
2196     case MacroDirective::MD_Undefine:
2197       MD = PP.AllocateUndefMacroDirective(Loc);
2198       break;
2199     case MacroDirective::MD_Visibility:
2200       bool isPublic = Record[Idx++];
2201       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2202       break;
2203     }
2204 
2205     if (!Latest)
2206       Latest = MD;
2207     if (Earliest)
2208       Earliest->setPrevious(MD);
2209     Earliest = MD;
2210   }
2211 
2212   if (Latest)
2213     PP.setLoadedMacroDirective(II, Earliest, Latest);
2214 }
2215 
2216 ASTReader::InputFileInfo
2217 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2218   // Go find this input file.
2219   BitstreamCursor &Cursor = F.InputFilesCursor;
2220   SavedStreamPosition SavedPosition(Cursor);
2221   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2222     // FIXME this drops errors on the floor.
2223     consumeError(std::move(Err));
2224   }
2225 
2226   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2227   if (!MaybeCode) {
2228     // FIXME this drops errors on the floor.
2229     consumeError(MaybeCode.takeError());
2230   }
2231   unsigned Code = MaybeCode.get();
2232   RecordData Record;
2233   StringRef Blob;
2234 
2235   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2236     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2237            "invalid record type for input file");
2238   else {
2239     // FIXME this drops errors on the floor.
2240     consumeError(Maybe.takeError());
2241   }
2242 
2243   assert(Record[0] == ID && "Bogus stored ID or offset");
2244   InputFileInfo R;
2245   R.StoredSize = static_cast<off_t>(Record[1]);
2246   R.StoredTime = static_cast<time_t>(Record[2]);
2247   R.Overridden = static_cast<bool>(Record[3]);
2248   R.Transient = static_cast<bool>(Record[4]);
2249   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2250   R.Filename = Blob;
2251   ResolveImportedPath(F, R.Filename);
2252 
2253   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2254   if (!MaybeEntry) // FIXME this drops errors on the floor.
2255     consumeError(MaybeEntry.takeError());
2256   llvm::BitstreamEntry Entry = MaybeEntry.get();
2257   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2258          "expected record type for input file hash");
2259 
2260   Record.clear();
2261   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2262     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2263            "invalid record type for input file hash");
2264   else {
2265     // FIXME this drops errors on the floor.
2266     consumeError(Maybe.takeError());
2267   }
2268   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2269                   static_cast<uint64_t>(Record[0]);
2270   return R;
2271 }
2272 
2273 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2274 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2275   // If this ID is bogus, just return an empty input file.
2276   if (ID == 0 || ID > F.InputFilesLoaded.size())
2277     return InputFile();
2278 
2279   // If we've already loaded this input file, return it.
2280   if (F.InputFilesLoaded[ID-1].getFile())
2281     return F.InputFilesLoaded[ID-1];
2282 
2283   if (F.InputFilesLoaded[ID-1].isNotFound())
2284     return InputFile();
2285 
2286   // Go find this input file.
2287   BitstreamCursor &Cursor = F.InputFilesCursor;
2288   SavedStreamPosition SavedPosition(Cursor);
2289   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2290     // FIXME this drops errors on the floor.
2291     consumeError(std::move(Err));
2292   }
2293 
2294   InputFileInfo FI = readInputFileInfo(F, ID);
2295   off_t StoredSize = FI.StoredSize;
2296   time_t StoredTime = FI.StoredTime;
2297   bool Overridden = FI.Overridden;
2298   bool Transient = FI.Transient;
2299   StringRef Filename = FI.Filename;
2300   uint64_t StoredContentHash = FI.ContentHash;
2301 
2302   const FileEntry *File = nullptr;
2303   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2304     File = *FE;
2305 
2306   // If we didn't find the file, resolve it relative to the
2307   // original directory from which this AST file was created.
2308   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2309       F.OriginalDir != F.BaseDirectory) {
2310     std::string Resolved = resolveFileRelativeToOriginalDir(
2311         Filename, F.OriginalDir, F.BaseDirectory);
2312     if (!Resolved.empty())
2313       if (auto FE = FileMgr.getFile(Resolved))
2314         File = *FE;
2315   }
2316 
2317   // For an overridden file, create a virtual file with the stored
2318   // size/timestamp.
2319   if ((Overridden || Transient) && File == nullptr)
2320     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2321 
2322   if (File == nullptr) {
2323     if (Complain) {
2324       std::string ErrorStr = "could not find file '";
2325       ErrorStr += Filename;
2326       ErrorStr += "' referenced by AST file '";
2327       ErrorStr += F.FileName;
2328       ErrorStr += "'";
2329       Error(ErrorStr);
2330     }
2331     // Record that we didn't find the file.
2332     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2333     return InputFile();
2334   }
2335 
2336   // Check if there was a request to override the contents of the file
2337   // that was part of the precompiled header. Overriding such a file
2338   // can lead to problems when lexing using the source locations from the
2339   // PCH.
2340   SourceManager &SM = getSourceManager();
2341   // FIXME: Reject if the overrides are different.
2342   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2343     if (Complain)
2344       Error(diag::err_fe_pch_file_overridden, Filename);
2345 
2346     // After emitting the diagnostic, bypass the overriding file to recover
2347     // (this creates a separate FileEntry).
2348     File = SM.bypassFileContentsOverride(*File);
2349     if (!File) {
2350       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2351       return InputFile();
2352     }
2353   }
2354 
2355   enum ModificationType {
2356     Size,
2357     ModTime,
2358     Content,
2359     None,
2360   };
2361   auto HasInputFileChanged = [&]() {
2362     if (StoredSize != File->getSize())
2363       return ModificationType::Size;
2364     if (!DisableValidation && StoredTime &&
2365         StoredTime != File->getModificationTime()) {
2366       // In case the modification time changes but not the content,
2367       // accept the cached file as legit.
2368       if (ValidateASTInputFilesContent &&
2369           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2370         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2371         if (!MemBuffOrError) {
2372           if (!Complain)
2373             return ModificationType::ModTime;
2374           std::string ErrorStr = "could not get buffer for file '";
2375           ErrorStr += File->getName();
2376           ErrorStr += "'";
2377           Error(ErrorStr);
2378           return ModificationType::ModTime;
2379         }
2380 
2381         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2382         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2383           return ModificationType::None;
2384         return ModificationType::Content;
2385       }
2386       return ModificationType::ModTime;
2387     }
2388     return ModificationType::None;
2389   };
2390 
2391   bool IsOutOfDate = false;
2392   auto FileChange = HasInputFileChanged();
2393   // For an overridden file, there is nothing to validate.
2394   if (!Overridden && FileChange != ModificationType::None) {
2395     if (Complain) {
2396       // Build a list of the PCH imports that got us here (in reverse).
2397       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2398       while (!ImportStack.back()->ImportedBy.empty())
2399         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2400 
2401       // The top-level PCH is stale.
2402       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2403       unsigned DiagnosticKind =
2404           moduleKindForDiagnostic(ImportStack.back()->Kind);
2405       if (DiagnosticKind == 0)
2406         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2407               (unsigned)FileChange);
2408       else if (DiagnosticKind == 1)
2409         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2410               (unsigned)FileChange);
2411       else
2412         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2413               (unsigned)FileChange);
2414 
2415       // Print the import stack.
2416       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2417         Diag(diag::note_pch_required_by)
2418           << Filename << ImportStack[0]->FileName;
2419         for (unsigned I = 1; I < ImportStack.size(); ++I)
2420           Diag(diag::note_pch_required_by)
2421             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2422       }
2423 
2424       if (!Diags.isDiagnosticInFlight())
2425         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2426     }
2427 
2428     IsOutOfDate = true;
2429   }
2430   // FIXME: If the file is overridden and we've already opened it,
2431   // issue an error (or split it into a separate FileEntry).
2432 
2433   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2434 
2435   // Note that we've loaded this input file.
2436   F.InputFilesLoaded[ID-1] = IF;
2437   return IF;
2438 }
2439 
2440 /// If we are loading a relocatable PCH or module file, and the filename
2441 /// is not an absolute path, add the system or module root to the beginning of
2442 /// the file name.
2443 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2444   // Resolve relative to the base directory, if we have one.
2445   if (!M.BaseDirectory.empty())
2446     return ResolveImportedPath(Filename, M.BaseDirectory);
2447 }
2448 
2449 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2450   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2451     return;
2452 
2453   SmallString<128> Buffer;
2454   llvm::sys::path::append(Buffer, Prefix, Filename);
2455   Filename.assign(Buffer.begin(), Buffer.end());
2456 }
2457 
2458 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2459   switch (ARR) {
2460   case ASTReader::Failure: return true;
2461   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2462   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2463   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2464   case ASTReader::ConfigurationMismatch:
2465     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2466   case ASTReader::HadErrors: return true;
2467   case ASTReader::Success: return false;
2468   }
2469 
2470   llvm_unreachable("unknown ASTReadResult");
2471 }
2472 
2473 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2474     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2475     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2476     std::string &SuggestedPredefines) {
2477   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2478     // FIXME this drops errors on the floor.
2479     consumeError(std::move(Err));
2480     return Failure;
2481   }
2482 
2483   // Read all of the records in the options block.
2484   RecordData Record;
2485   ASTReadResult Result = Success;
2486   while (true) {
2487     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2488     if (!MaybeEntry) {
2489       // FIXME this drops errors on the floor.
2490       consumeError(MaybeEntry.takeError());
2491       return Failure;
2492     }
2493     llvm::BitstreamEntry Entry = MaybeEntry.get();
2494 
2495     switch (Entry.Kind) {
2496     case llvm::BitstreamEntry::Error:
2497     case llvm::BitstreamEntry::SubBlock:
2498       return Failure;
2499 
2500     case llvm::BitstreamEntry::EndBlock:
2501       return Result;
2502 
2503     case llvm::BitstreamEntry::Record:
2504       // The interesting case.
2505       break;
2506     }
2507 
2508     // Read and process a record.
2509     Record.clear();
2510     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2511     if (!MaybeRecordType) {
2512       // FIXME this drops errors on the floor.
2513       consumeError(MaybeRecordType.takeError());
2514       return Failure;
2515     }
2516     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2517     case LANGUAGE_OPTIONS: {
2518       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2519       if (ParseLanguageOptions(Record, Complain, Listener,
2520                                AllowCompatibleConfigurationMismatch))
2521         Result = ConfigurationMismatch;
2522       break;
2523     }
2524 
2525     case TARGET_OPTIONS: {
2526       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2527       if (ParseTargetOptions(Record, Complain, Listener,
2528                              AllowCompatibleConfigurationMismatch))
2529         Result = ConfigurationMismatch;
2530       break;
2531     }
2532 
2533     case FILE_SYSTEM_OPTIONS: {
2534       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2535       if (!AllowCompatibleConfigurationMismatch &&
2536           ParseFileSystemOptions(Record, Complain, Listener))
2537         Result = ConfigurationMismatch;
2538       break;
2539     }
2540 
2541     case HEADER_SEARCH_OPTIONS: {
2542       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2543       if (!AllowCompatibleConfigurationMismatch &&
2544           ParseHeaderSearchOptions(Record, Complain, Listener))
2545         Result = ConfigurationMismatch;
2546       break;
2547     }
2548 
2549     case PREPROCESSOR_OPTIONS:
2550       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2551       if (!AllowCompatibleConfigurationMismatch &&
2552           ParsePreprocessorOptions(Record, Complain, Listener,
2553                                    SuggestedPredefines))
2554         Result = ConfigurationMismatch;
2555       break;
2556     }
2557   }
2558 }
2559 
2560 ASTReader::ASTReadResult
2561 ASTReader::ReadControlBlock(ModuleFile &F,
2562                             SmallVectorImpl<ImportedModule> &Loaded,
2563                             const ModuleFile *ImportedBy,
2564                             unsigned ClientLoadCapabilities) {
2565   BitstreamCursor &Stream = F.Stream;
2566 
2567   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2568     Error(std::move(Err));
2569     return Failure;
2570   }
2571 
2572   // Lambda to read the unhashed control block the first time it's called.
2573   //
2574   // For PCM files, the unhashed control block cannot be read until after the
2575   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2576   // need to look ahead before reading the IMPORTS record.  For consistency,
2577   // this block is always read somehow (see BitstreamEntry::EndBlock).
2578   bool HasReadUnhashedControlBlock = false;
2579   auto readUnhashedControlBlockOnce = [&]() {
2580     if (!HasReadUnhashedControlBlock) {
2581       HasReadUnhashedControlBlock = true;
2582       if (ASTReadResult Result =
2583               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2584         return Result;
2585     }
2586     return Success;
2587   };
2588 
2589   // Read all of the records and blocks in the control block.
2590   RecordData Record;
2591   unsigned NumInputs = 0;
2592   unsigned NumUserInputs = 0;
2593   StringRef BaseDirectoryAsWritten;
2594   while (true) {
2595     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2596     if (!MaybeEntry) {
2597       Error(MaybeEntry.takeError());
2598       return Failure;
2599     }
2600     llvm::BitstreamEntry Entry = MaybeEntry.get();
2601 
2602     switch (Entry.Kind) {
2603     case llvm::BitstreamEntry::Error:
2604       Error("malformed block record in AST file");
2605       return Failure;
2606     case llvm::BitstreamEntry::EndBlock: {
2607       // Validate the module before returning.  This call catches an AST with
2608       // no module name and no imports.
2609       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2610         return Result;
2611 
2612       // Validate input files.
2613       const HeaderSearchOptions &HSOpts =
2614           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2615 
2616       // All user input files reside at the index range [0, NumUserInputs), and
2617       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2618       // loaded module files, ignore missing inputs.
2619       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2620           F.Kind != MK_PrebuiltModule) {
2621         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2622 
2623         // If we are reading a module, we will create a verification timestamp,
2624         // so we verify all input files.  Otherwise, verify only user input
2625         // files.
2626 
2627         unsigned N = NumUserInputs;
2628         if (ValidateSystemInputs ||
2629             (HSOpts.ModulesValidateOncePerBuildSession &&
2630              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2631              F.Kind == MK_ImplicitModule))
2632           N = NumInputs;
2633 
2634         for (unsigned I = 0; I < N; ++I) {
2635           InputFile IF = getInputFile(F, I+1, Complain);
2636           if (!IF.getFile() || IF.isOutOfDate())
2637             return OutOfDate;
2638         }
2639       }
2640 
2641       if (Listener)
2642         Listener->visitModuleFile(F.FileName, F.Kind);
2643 
2644       if (Listener && Listener->needsInputFileVisitation()) {
2645         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2646                                                                 : NumUserInputs;
2647         for (unsigned I = 0; I < N; ++I) {
2648           bool IsSystem = I >= NumUserInputs;
2649           InputFileInfo FI = readInputFileInfo(F, I+1);
2650           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2651                                    F.Kind == MK_ExplicitModule ||
2652                                    F.Kind == MK_PrebuiltModule);
2653         }
2654       }
2655 
2656       return Success;
2657     }
2658 
2659     case llvm::BitstreamEntry::SubBlock:
2660       switch (Entry.ID) {
2661       case INPUT_FILES_BLOCK_ID:
2662         F.InputFilesCursor = Stream;
2663         if (llvm::Error Err = Stream.SkipBlock()) {
2664           Error(std::move(Err));
2665           return Failure;
2666         }
2667         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2668           Error("malformed block record in AST file");
2669           return Failure;
2670         }
2671         continue;
2672 
2673       case OPTIONS_BLOCK_ID:
2674         // If we're reading the first module for this group, check its options
2675         // are compatible with ours. For modules it imports, no further checking
2676         // is required, because we checked them when we built it.
2677         if (Listener && !ImportedBy) {
2678           // Should we allow the configuration of the module file to differ from
2679           // the configuration of the current translation unit in a compatible
2680           // way?
2681           //
2682           // FIXME: Allow this for files explicitly specified with -include-pch.
2683           bool AllowCompatibleConfigurationMismatch =
2684               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2685 
2686           ASTReadResult Result =
2687               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2688                                AllowCompatibleConfigurationMismatch, *Listener,
2689                                SuggestedPredefines);
2690           if (Result == Failure) {
2691             Error("malformed block record in AST file");
2692             return Result;
2693           }
2694 
2695           if (DisableValidation ||
2696               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2697             Result = Success;
2698 
2699           // If we can't load the module, exit early since we likely
2700           // will rebuild the module anyway. The stream may be in the
2701           // middle of a block.
2702           if (Result != Success)
2703             return Result;
2704         } else if (llvm::Error Err = Stream.SkipBlock()) {
2705           Error(std::move(Err));
2706           return Failure;
2707         }
2708         continue;
2709 
2710       default:
2711         if (llvm::Error Err = Stream.SkipBlock()) {
2712           Error(std::move(Err));
2713           return Failure;
2714         }
2715         continue;
2716       }
2717 
2718     case llvm::BitstreamEntry::Record:
2719       // The interesting case.
2720       break;
2721     }
2722 
2723     // Read and process a record.
2724     Record.clear();
2725     StringRef Blob;
2726     Expected<unsigned> MaybeRecordType =
2727         Stream.readRecord(Entry.ID, Record, &Blob);
2728     if (!MaybeRecordType) {
2729       Error(MaybeRecordType.takeError());
2730       return Failure;
2731     }
2732     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2733     case METADATA: {
2734       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2735         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2736           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2737                                         : diag::err_pch_version_too_new);
2738         return VersionMismatch;
2739       }
2740 
2741       bool hasErrors = Record[7];
2742       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2743         Diag(diag::err_pch_with_compiler_errors);
2744         return HadErrors;
2745       }
2746       if (hasErrors) {
2747         Diags.ErrorOccurred = true;
2748         Diags.UncompilableErrorOccurred = true;
2749         Diags.UnrecoverableErrorOccurred = true;
2750       }
2751 
2752       F.RelocatablePCH = Record[4];
2753       // Relative paths in a relocatable PCH are relative to our sysroot.
2754       if (F.RelocatablePCH)
2755         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2756 
2757       F.HasTimestamps = Record[5];
2758 
2759       F.PCHHasObjectFile = Record[6];
2760 
2761       const std::string &CurBranch = getClangFullRepositoryVersion();
2762       StringRef ASTBranch = Blob;
2763       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2764         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2765           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2766         return VersionMismatch;
2767       }
2768       break;
2769     }
2770 
2771     case IMPORTS: {
2772       // Validate the AST before processing any imports (otherwise, untangling
2773       // them can be error-prone and expensive).  A module will have a name and
2774       // will already have been validated, but this catches the PCH case.
2775       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2776         return Result;
2777 
2778       // Load each of the imported PCH files.
2779       unsigned Idx = 0, N = Record.size();
2780       while (Idx < N) {
2781         // Read information about the AST file.
2782         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2783         // The import location will be the local one for now; we will adjust
2784         // all import locations of module imports after the global source
2785         // location info are setup, in ReadAST.
2786         SourceLocation ImportLoc =
2787             ReadUntranslatedSourceLocation(Record[Idx++]);
2788         off_t StoredSize = (off_t)Record[Idx++];
2789         time_t StoredModTime = (time_t)Record[Idx++];
2790         ASTFileSignature StoredSignature = {
2791             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2792               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2793               (uint32_t)Record[Idx++]}}};
2794 
2795         std::string ImportedName = ReadString(Record, Idx);
2796         std::string ImportedFile;
2797 
2798         // For prebuilt and explicit modules first consult the file map for
2799         // an override. Note that here we don't search prebuilt module
2800         // directories, only the explicit name to file mappings. Also, we will
2801         // still verify the size/signature making sure it is essentially the
2802         // same file but perhaps in a different location.
2803         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2804           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2805             ImportedName, /*FileMapOnly*/ true);
2806 
2807         if (ImportedFile.empty())
2808           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2809           // ModuleCache as when writing.
2810           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2811         else
2812           SkipPath(Record, Idx);
2813 
2814         // If our client can't cope with us being out of date, we can't cope with
2815         // our dependency being missing.
2816         unsigned Capabilities = ClientLoadCapabilities;
2817         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2818           Capabilities &= ~ARR_Missing;
2819 
2820         // Load the AST file.
2821         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2822                                   Loaded, StoredSize, StoredModTime,
2823                                   StoredSignature, Capabilities);
2824 
2825         // If we diagnosed a problem, produce a backtrace.
2826         if (isDiagnosedResult(Result, Capabilities))
2827           Diag(diag::note_module_file_imported_by)
2828               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2829 
2830         switch (Result) {
2831         case Failure: return Failure;
2832           // If we have to ignore the dependency, we'll have to ignore this too.
2833         case Missing:
2834         case OutOfDate: return OutOfDate;
2835         case VersionMismatch: return VersionMismatch;
2836         case ConfigurationMismatch: return ConfigurationMismatch;
2837         case HadErrors: return HadErrors;
2838         case Success: break;
2839         }
2840       }
2841       break;
2842     }
2843 
2844     case ORIGINAL_FILE:
2845       F.OriginalSourceFileID = FileID::get(Record[0]);
2846       F.ActualOriginalSourceFileName = Blob;
2847       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2848       ResolveImportedPath(F, F.OriginalSourceFileName);
2849       break;
2850 
2851     case ORIGINAL_FILE_ID:
2852       F.OriginalSourceFileID = FileID::get(Record[0]);
2853       break;
2854 
2855     case ORIGINAL_PCH_DIR:
2856       F.OriginalDir = Blob;
2857       break;
2858 
2859     case MODULE_NAME:
2860       F.ModuleName = Blob;
2861       Diag(diag::remark_module_import)
2862           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2863           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2864       if (Listener)
2865         Listener->ReadModuleName(F.ModuleName);
2866 
2867       // Validate the AST as soon as we have a name so we can exit early on
2868       // failure.
2869       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2870         return Result;
2871 
2872       break;
2873 
2874     case MODULE_DIRECTORY: {
2875       // Save the BaseDirectory as written in the PCM for computing the module
2876       // filename for the ModuleCache.
2877       BaseDirectoryAsWritten = Blob;
2878       assert(!F.ModuleName.empty() &&
2879              "MODULE_DIRECTORY found before MODULE_NAME");
2880       // If we've already loaded a module map file covering this module, we may
2881       // have a better path for it (relative to the current build).
2882       Module *M = PP.getHeaderSearchInfo().lookupModule(
2883           F.ModuleName, /*AllowSearch*/ true,
2884           /*AllowExtraModuleMapSearch*/ true);
2885       if (M && M->Directory) {
2886         // If we're implicitly loading a module, the base directory can't
2887         // change between the build and use.
2888         // Don't emit module relocation error if we have -fno-validate-pch
2889         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2890             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2891           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2892           if (!BuildDir || *BuildDir != M->Directory) {
2893             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2894               Diag(diag::err_imported_module_relocated)
2895                   << F.ModuleName << Blob << M->Directory->getName();
2896             return OutOfDate;
2897           }
2898         }
2899         F.BaseDirectory = M->Directory->getName();
2900       } else {
2901         F.BaseDirectory = Blob;
2902       }
2903       break;
2904     }
2905 
2906     case MODULE_MAP_FILE:
2907       if (ASTReadResult Result =
2908               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2909         return Result;
2910       break;
2911 
2912     case INPUT_FILE_OFFSETS:
2913       NumInputs = Record[0];
2914       NumUserInputs = Record[1];
2915       F.InputFileOffsets =
2916           (const llvm::support::unaligned_uint64_t *)Blob.data();
2917       F.InputFilesLoaded.resize(NumInputs);
2918       F.NumUserInputFiles = NumUserInputs;
2919       break;
2920     }
2921   }
2922 }
2923 
2924 ASTReader::ASTReadResult
2925 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2926   BitstreamCursor &Stream = F.Stream;
2927 
2928   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2929     Error(std::move(Err));
2930     return Failure;
2931   }
2932 
2933   // Read all of the records and blocks for the AST file.
2934   RecordData Record;
2935   while (true) {
2936     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2937     if (!MaybeEntry) {
2938       Error(MaybeEntry.takeError());
2939       return Failure;
2940     }
2941     llvm::BitstreamEntry Entry = MaybeEntry.get();
2942 
2943     switch (Entry.Kind) {
2944     case llvm::BitstreamEntry::Error:
2945       Error("error at end of module block in AST file");
2946       return Failure;
2947     case llvm::BitstreamEntry::EndBlock:
2948       // Outside of C++, we do not store a lookup map for the translation unit.
2949       // Instead, mark it as needing a lookup map to be built if this module
2950       // contains any declarations lexically within it (which it always does!).
2951       // This usually has no cost, since we very rarely need the lookup map for
2952       // the translation unit outside C++.
2953       if (ASTContext *Ctx = ContextObj) {
2954         DeclContext *DC = Ctx->getTranslationUnitDecl();
2955         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2956           DC->setMustBuildLookupTable();
2957       }
2958 
2959       return Success;
2960     case llvm::BitstreamEntry::SubBlock:
2961       switch (Entry.ID) {
2962       case DECLTYPES_BLOCK_ID:
2963         // We lazily load the decls block, but we want to set up the
2964         // DeclsCursor cursor to point into it.  Clone our current bitcode
2965         // cursor to it, enter the block and read the abbrevs in that block.
2966         // With the main cursor, we just skip over it.
2967         F.DeclsCursor = Stream;
2968         if (llvm::Error Err = Stream.SkipBlock()) {
2969           Error(std::move(Err));
2970           return Failure;
2971         }
2972         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2973           Error("malformed block record in AST file");
2974           return Failure;
2975         }
2976         break;
2977 
2978       case PREPROCESSOR_BLOCK_ID:
2979         F.MacroCursor = Stream;
2980         if (!PP.getExternalSource())
2981           PP.setExternalSource(this);
2982 
2983         if (llvm::Error Err = Stream.SkipBlock()) {
2984           Error(std::move(Err));
2985           return Failure;
2986         }
2987         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2988           Error("malformed block record in AST file");
2989           return Failure;
2990         }
2991         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2992         break;
2993 
2994       case PREPROCESSOR_DETAIL_BLOCK_ID:
2995         F.PreprocessorDetailCursor = Stream;
2996 
2997         if (llvm::Error Err = Stream.SkipBlock()) {
2998           Error(std::move(Err));
2999           return Failure;
3000         }
3001         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3002                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3003           Error("malformed preprocessor detail record in AST file");
3004           return Failure;
3005         }
3006         F.PreprocessorDetailStartOffset
3007         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3008 
3009         if (!PP.getPreprocessingRecord())
3010           PP.createPreprocessingRecord();
3011         if (!PP.getPreprocessingRecord()->getExternalSource())
3012           PP.getPreprocessingRecord()->SetExternalSource(*this);
3013         break;
3014 
3015       case SOURCE_MANAGER_BLOCK_ID:
3016         if (ReadSourceManagerBlock(F))
3017           return Failure;
3018         break;
3019 
3020       case SUBMODULE_BLOCK_ID:
3021         if (ASTReadResult Result =
3022                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3023           return Result;
3024         break;
3025 
3026       case COMMENTS_BLOCK_ID: {
3027         BitstreamCursor C = Stream;
3028 
3029         if (llvm::Error Err = Stream.SkipBlock()) {
3030           Error(std::move(Err));
3031           return Failure;
3032         }
3033         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3034           Error("malformed comments block in AST file");
3035           return Failure;
3036         }
3037         CommentsCursors.push_back(std::make_pair(C, &F));
3038         break;
3039       }
3040 
3041       default:
3042         if (llvm::Error Err = Stream.SkipBlock()) {
3043           Error(std::move(Err));
3044           return Failure;
3045         }
3046         break;
3047       }
3048       continue;
3049 
3050     case llvm::BitstreamEntry::Record:
3051       // The interesting case.
3052       break;
3053     }
3054 
3055     // Read and process a record.
3056     Record.clear();
3057     StringRef Blob;
3058     Expected<unsigned> MaybeRecordType =
3059         Stream.readRecord(Entry.ID, Record, &Blob);
3060     if (!MaybeRecordType) {
3061       Error(MaybeRecordType.takeError());
3062       return Failure;
3063     }
3064     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3065 
3066     // If we're not loading an AST context, we don't care about most records.
3067     if (!ContextObj) {
3068       switch (RecordType) {
3069       case IDENTIFIER_TABLE:
3070       case IDENTIFIER_OFFSET:
3071       case INTERESTING_IDENTIFIERS:
3072       case STATISTICS:
3073       case PP_CONDITIONAL_STACK:
3074       case PP_COUNTER_VALUE:
3075       case SOURCE_LOCATION_OFFSETS:
3076       case MODULE_OFFSET_MAP:
3077       case SOURCE_MANAGER_LINE_TABLE:
3078       case SOURCE_LOCATION_PRELOADS:
3079       case PPD_ENTITIES_OFFSETS:
3080       case HEADER_SEARCH_TABLE:
3081       case IMPORTED_MODULES:
3082       case MACRO_OFFSET:
3083         break;
3084       default:
3085         continue;
3086       }
3087     }
3088 
3089     switch (RecordType) {
3090     default:  // Default behavior: ignore.
3091       break;
3092 
3093     case TYPE_OFFSET: {
3094       if (F.LocalNumTypes != 0) {
3095         Error("duplicate TYPE_OFFSET record in AST file");
3096         return Failure;
3097       }
3098       F.TypeOffsets = (const uint32_t *)Blob.data();
3099       F.LocalNumTypes = Record[0];
3100       unsigned LocalBaseTypeIndex = Record[1];
3101       F.BaseTypeIndex = getTotalNumTypes();
3102 
3103       if (F.LocalNumTypes > 0) {
3104         // Introduce the global -> local mapping for types within this module.
3105         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3106 
3107         // Introduce the local -> global mapping for types within this module.
3108         F.TypeRemap.insertOrReplace(
3109           std::make_pair(LocalBaseTypeIndex,
3110                          F.BaseTypeIndex - LocalBaseTypeIndex));
3111 
3112         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3113       }
3114       break;
3115     }
3116 
3117     case DECL_OFFSET: {
3118       if (F.LocalNumDecls != 0) {
3119         Error("duplicate DECL_OFFSET record in AST file");
3120         return Failure;
3121       }
3122       F.DeclOffsets = (const DeclOffset *)Blob.data();
3123       F.LocalNumDecls = Record[0];
3124       unsigned LocalBaseDeclID = Record[1];
3125       F.BaseDeclID = getTotalNumDecls();
3126 
3127       if (F.LocalNumDecls > 0) {
3128         // Introduce the global -> local mapping for declarations within this
3129         // module.
3130         GlobalDeclMap.insert(
3131           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3132 
3133         // Introduce the local -> global mapping for declarations within this
3134         // module.
3135         F.DeclRemap.insertOrReplace(
3136           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3137 
3138         // Introduce the global -> local mapping for declarations within this
3139         // module.
3140         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3141 
3142         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3143       }
3144       break;
3145     }
3146 
3147     case TU_UPDATE_LEXICAL: {
3148       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3149       LexicalContents Contents(
3150           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3151               Blob.data()),
3152           static_cast<unsigned int>(Blob.size() / 4));
3153       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3154       TU->setHasExternalLexicalStorage(true);
3155       break;
3156     }
3157 
3158     case UPDATE_VISIBLE: {
3159       unsigned Idx = 0;
3160       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3161       auto *Data = (const unsigned char*)Blob.data();
3162       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3163       // If we've already loaded the decl, perform the updates when we finish
3164       // loading this block.
3165       if (Decl *D = GetExistingDecl(ID))
3166         PendingUpdateRecords.push_back(
3167             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3168       break;
3169     }
3170 
3171     case IDENTIFIER_TABLE:
3172       F.IdentifierTableData = Blob.data();
3173       if (Record[0]) {
3174         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3175             (const unsigned char *)F.IdentifierTableData + Record[0],
3176             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3177             (const unsigned char *)F.IdentifierTableData,
3178             ASTIdentifierLookupTrait(*this, F));
3179 
3180         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3181       }
3182       break;
3183 
3184     case IDENTIFIER_OFFSET: {
3185       if (F.LocalNumIdentifiers != 0) {
3186         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3187         return Failure;
3188       }
3189       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3190       F.LocalNumIdentifiers = Record[0];
3191       unsigned LocalBaseIdentifierID = Record[1];
3192       F.BaseIdentifierID = getTotalNumIdentifiers();
3193 
3194       if (F.LocalNumIdentifiers > 0) {
3195         // Introduce the global -> local mapping for identifiers within this
3196         // module.
3197         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3198                                                   &F));
3199 
3200         // Introduce the local -> global mapping for identifiers within this
3201         // module.
3202         F.IdentifierRemap.insertOrReplace(
3203           std::make_pair(LocalBaseIdentifierID,
3204                          F.BaseIdentifierID - LocalBaseIdentifierID));
3205 
3206         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3207                                  + F.LocalNumIdentifiers);
3208       }
3209       break;
3210     }
3211 
3212     case INTERESTING_IDENTIFIERS:
3213       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3214       break;
3215 
3216     case EAGERLY_DESERIALIZED_DECLS:
3217       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3218       // about "interesting" decls (for instance, if we're building a module).
3219       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3220         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3221       break;
3222 
3223     case MODULAR_CODEGEN_DECLS:
3224       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3225       // them (ie: if we're not codegenerating this module).
3226       if (F.Kind == MK_MainFile)
3227         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3228           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3229       break;
3230 
3231     case SPECIAL_TYPES:
3232       if (SpecialTypes.empty()) {
3233         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3234           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3235         break;
3236       }
3237 
3238       if (SpecialTypes.size() != Record.size()) {
3239         Error("invalid special-types record");
3240         return Failure;
3241       }
3242 
3243       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3244         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3245         if (!SpecialTypes[I])
3246           SpecialTypes[I] = ID;
3247         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3248         // merge step?
3249       }
3250       break;
3251 
3252     case STATISTICS:
3253       TotalNumStatements += Record[0];
3254       TotalNumMacros += Record[1];
3255       TotalLexicalDeclContexts += Record[2];
3256       TotalVisibleDeclContexts += Record[3];
3257       break;
3258 
3259     case UNUSED_FILESCOPED_DECLS:
3260       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3261         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3262       break;
3263 
3264     case DELEGATING_CTORS:
3265       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3266         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3267       break;
3268 
3269     case WEAK_UNDECLARED_IDENTIFIERS:
3270       if (Record.size() % 4 != 0) {
3271         Error("invalid weak identifiers record");
3272         return Failure;
3273       }
3274 
3275       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3276       // files. This isn't the way to do it :)
3277       WeakUndeclaredIdentifiers.clear();
3278 
3279       // Translate the weak, undeclared identifiers into global IDs.
3280       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3281         WeakUndeclaredIdentifiers.push_back(
3282           getGlobalIdentifierID(F, Record[I++]));
3283         WeakUndeclaredIdentifiers.push_back(
3284           getGlobalIdentifierID(F, Record[I++]));
3285         WeakUndeclaredIdentifiers.push_back(
3286           ReadSourceLocation(F, Record, I).getRawEncoding());
3287         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3288       }
3289       break;
3290 
3291     case SELECTOR_OFFSETS: {
3292       F.SelectorOffsets = (const uint32_t *)Blob.data();
3293       F.LocalNumSelectors = Record[0];
3294       unsigned LocalBaseSelectorID = Record[1];
3295       F.BaseSelectorID = getTotalNumSelectors();
3296 
3297       if (F.LocalNumSelectors > 0) {
3298         // Introduce the global -> local mapping for selectors within this
3299         // module.
3300         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3301 
3302         // Introduce the local -> global mapping for selectors within this
3303         // module.
3304         F.SelectorRemap.insertOrReplace(
3305           std::make_pair(LocalBaseSelectorID,
3306                          F.BaseSelectorID - LocalBaseSelectorID));
3307 
3308         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3309       }
3310       break;
3311     }
3312 
3313     case METHOD_POOL:
3314       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3315       if (Record[0])
3316         F.SelectorLookupTable
3317           = ASTSelectorLookupTable::Create(
3318                         F.SelectorLookupTableData + Record[0],
3319                         F.SelectorLookupTableData,
3320                         ASTSelectorLookupTrait(*this, F));
3321       TotalNumMethodPoolEntries += Record[1];
3322       break;
3323 
3324     case REFERENCED_SELECTOR_POOL:
3325       if (!Record.empty()) {
3326         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3327           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3328                                                                 Record[Idx++]));
3329           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3330                                               getRawEncoding());
3331         }
3332       }
3333       break;
3334 
3335     case PP_CONDITIONAL_STACK:
3336       if (!Record.empty()) {
3337         unsigned Idx = 0, End = Record.size() - 1;
3338         bool ReachedEOFWhileSkipping = Record[Idx++];
3339         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3340         if (ReachedEOFWhileSkipping) {
3341           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3342           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3343           bool FoundNonSkipPortion = Record[Idx++];
3344           bool FoundElse = Record[Idx++];
3345           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3346           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3347                            FoundElse, ElseLoc);
3348         }
3349         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3350         while (Idx < End) {
3351           auto Loc = ReadSourceLocation(F, Record, Idx);
3352           bool WasSkipping = Record[Idx++];
3353           bool FoundNonSkip = Record[Idx++];
3354           bool FoundElse = Record[Idx++];
3355           ConditionalStack.push_back(
3356               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3357         }
3358         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3359       }
3360       break;
3361 
3362     case PP_COUNTER_VALUE:
3363       if (!Record.empty() && Listener)
3364         Listener->ReadCounter(F, Record[0]);
3365       break;
3366 
3367     case FILE_SORTED_DECLS:
3368       F.FileSortedDecls = (const DeclID *)Blob.data();
3369       F.NumFileSortedDecls = Record[0];
3370       break;
3371 
3372     case SOURCE_LOCATION_OFFSETS: {
3373       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3374       F.LocalNumSLocEntries = Record[0];
3375       unsigned SLocSpaceSize = Record[1];
3376       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3377           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3378                                               SLocSpaceSize);
3379       if (!F.SLocEntryBaseID) {
3380         Error("ran out of source locations");
3381         break;
3382       }
3383       // Make our entry in the range map. BaseID is negative and growing, so
3384       // we invert it. Because we invert it, though, we need the other end of
3385       // the range.
3386       unsigned RangeStart =
3387           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3388       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3389       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3390 
3391       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3392       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3393       GlobalSLocOffsetMap.insert(
3394           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3395                            - SLocSpaceSize,&F));
3396 
3397       // Initialize the remapping table.
3398       // Invalid stays invalid.
3399       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3400       // This module. Base was 2 when being compiled.
3401       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3402                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3403 
3404       TotalNumSLocEntries += F.LocalNumSLocEntries;
3405       break;
3406     }
3407 
3408     case MODULE_OFFSET_MAP:
3409       F.ModuleOffsetMap = Blob;
3410       break;
3411 
3412     case SOURCE_MANAGER_LINE_TABLE:
3413       if (ParseLineTable(F, Record)) {
3414         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3415         return Failure;
3416       }
3417       break;
3418 
3419     case SOURCE_LOCATION_PRELOADS: {
3420       // Need to transform from the local view (1-based IDs) to the global view,
3421       // which is based off F.SLocEntryBaseID.
3422       if (!F.PreloadSLocEntries.empty()) {
3423         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3424         return Failure;
3425       }
3426 
3427       F.PreloadSLocEntries.swap(Record);
3428       break;
3429     }
3430 
3431     case EXT_VECTOR_DECLS:
3432       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3433         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3434       break;
3435 
3436     case VTABLE_USES:
3437       if (Record.size() % 3 != 0) {
3438         Error("Invalid VTABLE_USES record");
3439         return Failure;
3440       }
3441 
3442       // Later tables overwrite earlier ones.
3443       // FIXME: Modules will have some trouble with this. This is clearly not
3444       // the right way to do this.
3445       VTableUses.clear();
3446 
3447       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3448         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3449         VTableUses.push_back(
3450           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3451         VTableUses.push_back(Record[Idx++]);
3452       }
3453       break;
3454 
3455     case PENDING_IMPLICIT_INSTANTIATIONS:
3456       if (PendingInstantiations.size() % 2 != 0) {
3457         Error("Invalid existing PendingInstantiations");
3458         return Failure;
3459       }
3460 
3461       if (Record.size() % 2 != 0) {
3462         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3463         return Failure;
3464       }
3465 
3466       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3467         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3468         PendingInstantiations.push_back(
3469           ReadSourceLocation(F, Record, I).getRawEncoding());
3470       }
3471       break;
3472 
3473     case SEMA_DECL_REFS:
3474       if (Record.size() != 3) {
3475         Error("Invalid SEMA_DECL_REFS block");
3476         return Failure;
3477       }
3478       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3479         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3480       break;
3481 
3482     case PPD_ENTITIES_OFFSETS: {
3483       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3484       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3485       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3486 
3487       unsigned LocalBasePreprocessedEntityID = Record[0];
3488 
3489       unsigned StartingID;
3490       if (!PP.getPreprocessingRecord())
3491         PP.createPreprocessingRecord();
3492       if (!PP.getPreprocessingRecord()->getExternalSource())
3493         PP.getPreprocessingRecord()->SetExternalSource(*this);
3494       StartingID
3495         = PP.getPreprocessingRecord()
3496             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3497       F.BasePreprocessedEntityID = StartingID;
3498 
3499       if (F.NumPreprocessedEntities > 0) {
3500         // Introduce the global -> local mapping for preprocessed entities in
3501         // this module.
3502         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3503 
3504         // Introduce the local -> global mapping for preprocessed entities in
3505         // this module.
3506         F.PreprocessedEntityRemap.insertOrReplace(
3507           std::make_pair(LocalBasePreprocessedEntityID,
3508             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3509       }
3510 
3511       break;
3512     }
3513 
3514     case PPD_SKIPPED_RANGES: {
3515       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3516       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3517       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3518 
3519       if (!PP.getPreprocessingRecord())
3520         PP.createPreprocessingRecord();
3521       if (!PP.getPreprocessingRecord()->getExternalSource())
3522         PP.getPreprocessingRecord()->SetExternalSource(*this);
3523       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3524           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3525 
3526       if (F.NumPreprocessedSkippedRanges > 0)
3527         GlobalSkippedRangeMap.insert(
3528             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3529       break;
3530     }
3531 
3532     case DECL_UPDATE_OFFSETS:
3533       if (Record.size() % 2 != 0) {
3534         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3535         return Failure;
3536       }
3537       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3538         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3539         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3540 
3541         // If we've already loaded the decl, perform the updates when we finish
3542         // loading this block.
3543         if (Decl *D = GetExistingDecl(ID))
3544           PendingUpdateRecords.push_back(
3545               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3546       }
3547       break;
3548 
3549     case OBJC_CATEGORIES_MAP:
3550       if (F.LocalNumObjCCategoriesInMap != 0) {
3551         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3552         return Failure;
3553       }
3554 
3555       F.LocalNumObjCCategoriesInMap = Record[0];
3556       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3557       break;
3558 
3559     case OBJC_CATEGORIES:
3560       F.ObjCCategories.swap(Record);
3561       break;
3562 
3563     case CUDA_SPECIAL_DECL_REFS:
3564       // Later tables overwrite earlier ones.
3565       // FIXME: Modules will have trouble with this.
3566       CUDASpecialDeclRefs.clear();
3567       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3568         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3569       break;
3570 
3571     case HEADER_SEARCH_TABLE:
3572       F.HeaderFileInfoTableData = Blob.data();
3573       F.LocalNumHeaderFileInfos = Record[1];
3574       if (Record[0]) {
3575         F.HeaderFileInfoTable
3576           = HeaderFileInfoLookupTable::Create(
3577                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3578                    (const unsigned char *)F.HeaderFileInfoTableData,
3579                    HeaderFileInfoTrait(*this, F,
3580                                        &PP.getHeaderSearchInfo(),
3581                                        Blob.data() + Record[2]));
3582 
3583         PP.getHeaderSearchInfo().SetExternalSource(this);
3584         if (!PP.getHeaderSearchInfo().getExternalLookup())
3585           PP.getHeaderSearchInfo().SetExternalLookup(this);
3586       }
3587       break;
3588 
3589     case FP_PRAGMA_OPTIONS:
3590       // Later tables overwrite earlier ones.
3591       FPPragmaOptions.swap(Record);
3592       break;
3593 
3594     case OPENCL_EXTENSIONS:
3595       for (unsigned I = 0, E = Record.size(); I != E; ) {
3596         auto Name = ReadString(Record, I);
3597         auto &Opt = OpenCLExtensions.OptMap[Name];
3598         Opt.Supported = Record[I++] != 0;
3599         Opt.Enabled = Record[I++] != 0;
3600         Opt.Avail = Record[I++];
3601         Opt.Core = Record[I++];
3602       }
3603       break;
3604 
3605     case OPENCL_EXTENSION_TYPES:
3606       for (unsigned I = 0, E = Record.size(); I != E;) {
3607         auto TypeID = static_cast<::TypeID>(Record[I++]);
3608         auto *Type = GetType(TypeID).getTypePtr();
3609         auto NumExt = static_cast<unsigned>(Record[I++]);
3610         for (unsigned II = 0; II != NumExt; ++II) {
3611           auto Ext = ReadString(Record, I);
3612           OpenCLTypeExtMap[Type].insert(Ext);
3613         }
3614       }
3615       break;
3616 
3617     case OPENCL_EXTENSION_DECLS:
3618       for (unsigned I = 0, E = Record.size(); I != E;) {
3619         auto DeclID = static_cast<::DeclID>(Record[I++]);
3620         auto *Decl = GetDecl(DeclID);
3621         auto NumExt = static_cast<unsigned>(Record[I++]);
3622         for (unsigned II = 0; II != NumExt; ++II) {
3623           auto Ext = ReadString(Record, I);
3624           OpenCLDeclExtMap[Decl].insert(Ext);
3625         }
3626       }
3627       break;
3628 
3629     case TENTATIVE_DEFINITIONS:
3630       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3631         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3632       break;
3633 
3634     case KNOWN_NAMESPACES:
3635       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3637       break;
3638 
3639     case UNDEFINED_BUT_USED:
3640       if (UndefinedButUsed.size() % 2 != 0) {
3641         Error("Invalid existing UndefinedButUsed");
3642         return Failure;
3643       }
3644 
3645       if (Record.size() % 2 != 0) {
3646         Error("invalid undefined-but-used record");
3647         return Failure;
3648       }
3649       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3650         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3651         UndefinedButUsed.push_back(
3652             ReadSourceLocation(F, Record, I).getRawEncoding());
3653       }
3654       break;
3655 
3656     case DELETE_EXPRS_TO_ANALYZE:
3657       for (unsigned I = 0, N = Record.size(); I != N;) {
3658         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3659         const uint64_t Count = Record[I++];
3660         DelayedDeleteExprs.push_back(Count);
3661         for (uint64_t C = 0; C < Count; ++C) {
3662           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3663           bool IsArrayForm = Record[I++] == 1;
3664           DelayedDeleteExprs.push_back(IsArrayForm);
3665         }
3666       }
3667       break;
3668 
3669     case IMPORTED_MODULES:
3670       if (!F.isModule()) {
3671         // If we aren't loading a module (which has its own exports), make
3672         // all of the imported modules visible.
3673         // FIXME: Deal with macros-only imports.
3674         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3675           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3676           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3677           if (GlobalID) {
3678             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3679             if (DeserializationListener)
3680               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3681           }
3682         }
3683       }
3684       break;
3685 
3686     case MACRO_OFFSET: {
3687       if (F.LocalNumMacros != 0) {
3688         Error("duplicate MACRO_OFFSET record in AST file");
3689         return Failure;
3690       }
3691       F.MacroOffsets = (const uint32_t *)Blob.data();
3692       F.LocalNumMacros = Record[0];
3693       unsigned LocalBaseMacroID = Record[1];
3694       F.BaseMacroID = getTotalNumMacros();
3695 
3696       if (F.LocalNumMacros > 0) {
3697         // Introduce the global -> local mapping for macros within this module.
3698         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3699 
3700         // Introduce the local -> global mapping for macros within this module.
3701         F.MacroRemap.insertOrReplace(
3702           std::make_pair(LocalBaseMacroID,
3703                          F.BaseMacroID - LocalBaseMacroID));
3704 
3705         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3706       }
3707       break;
3708     }
3709 
3710     case LATE_PARSED_TEMPLATE:
3711       LateParsedTemplates.append(Record.begin(), Record.end());
3712       break;
3713 
3714     case OPTIMIZE_PRAGMA_OPTIONS:
3715       if (Record.size() != 1) {
3716         Error("invalid pragma optimize record");
3717         return Failure;
3718       }
3719       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3720       break;
3721 
3722     case MSSTRUCT_PRAGMA_OPTIONS:
3723       if (Record.size() != 1) {
3724         Error("invalid pragma ms_struct record");
3725         return Failure;
3726       }
3727       PragmaMSStructState = Record[0];
3728       break;
3729 
3730     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3731       if (Record.size() != 2) {
3732         Error("invalid pragma ms_struct record");
3733         return Failure;
3734       }
3735       PragmaMSPointersToMembersState = Record[0];
3736       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3737       break;
3738 
3739     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3740       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3741         UnusedLocalTypedefNameCandidates.push_back(
3742             getGlobalDeclID(F, Record[I]));
3743       break;
3744 
3745     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3746       if (Record.size() != 1) {
3747         Error("invalid cuda pragma options record");
3748         return Failure;
3749       }
3750       ForceCUDAHostDeviceDepth = Record[0];
3751       break;
3752 
3753     case PACK_PRAGMA_OPTIONS: {
3754       if (Record.size() < 3) {
3755         Error("invalid pragma pack record");
3756         return Failure;
3757       }
3758       PragmaPackCurrentValue = Record[0];
3759       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3760       unsigned NumStackEntries = Record[2];
3761       unsigned Idx = 3;
3762       // Reset the stack when importing a new module.
3763       PragmaPackStack.clear();
3764       for (unsigned I = 0; I < NumStackEntries; ++I) {
3765         PragmaPackStackEntry Entry;
3766         Entry.Value = Record[Idx++];
3767         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3768         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3769         PragmaPackStrings.push_back(ReadString(Record, Idx));
3770         Entry.SlotLabel = PragmaPackStrings.back();
3771         PragmaPackStack.push_back(Entry);
3772       }
3773       break;
3774     }
3775     }
3776   }
3777 }
3778 
3779 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3780   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3781 
3782   // Additional remapping information.
3783   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3784   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3785   F.ModuleOffsetMap = StringRef();
3786 
3787   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3788   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3789     F.SLocRemap.insert(std::make_pair(0U, 0));
3790     F.SLocRemap.insert(std::make_pair(2U, 1));
3791   }
3792 
3793   // Continuous range maps we may be updating in our module.
3794   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3795   RemapBuilder SLocRemap(F.SLocRemap);
3796   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3797   RemapBuilder MacroRemap(F.MacroRemap);
3798   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3799   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3800   RemapBuilder SelectorRemap(F.SelectorRemap);
3801   RemapBuilder DeclRemap(F.DeclRemap);
3802   RemapBuilder TypeRemap(F.TypeRemap);
3803 
3804   while (Data < DataEnd) {
3805     // FIXME: Looking up dependency modules by filename is horrible. Let's
3806     // start fixing this with prebuilt and explicit modules and see how it
3807     // goes...
3808     using namespace llvm::support;
3809     ModuleKind Kind = static_cast<ModuleKind>(
3810       endian::readNext<uint8_t, little, unaligned>(Data));
3811     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3812     StringRef Name = StringRef((const char*)Data, Len);
3813     Data += Len;
3814     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3815                       ? ModuleMgr.lookupByModuleName(Name)
3816                       : ModuleMgr.lookupByFileName(Name));
3817     if (!OM) {
3818       std::string Msg =
3819           "SourceLocation remap refers to unknown module, cannot find ";
3820       Msg.append(Name);
3821       Error(Msg);
3822       return;
3823     }
3824 
3825     uint32_t SLocOffset =
3826         endian::readNext<uint32_t, little, unaligned>(Data);
3827     uint32_t IdentifierIDOffset =
3828         endian::readNext<uint32_t, little, unaligned>(Data);
3829     uint32_t MacroIDOffset =
3830         endian::readNext<uint32_t, little, unaligned>(Data);
3831     uint32_t PreprocessedEntityIDOffset =
3832         endian::readNext<uint32_t, little, unaligned>(Data);
3833     uint32_t SubmoduleIDOffset =
3834         endian::readNext<uint32_t, little, unaligned>(Data);
3835     uint32_t SelectorIDOffset =
3836         endian::readNext<uint32_t, little, unaligned>(Data);
3837     uint32_t DeclIDOffset =
3838         endian::readNext<uint32_t, little, unaligned>(Data);
3839     uint32_t TypeIndexOffset =
3840         endian::readNext<uint32_t, little, unaligned>(Data);
3841 
3842     uint32_t None = std::numeric_limits<uint32_t>::max();
3843 
3844     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3845                          RemapBuilder &Remap) {
3846       if (Offset != None)
3847         Remap.insert(std::make_pair(Offset,
3848                                     static_cast<int>(BaseOffset - Offset)));
3849     };
3850     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3851     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3852     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3853     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3854               PreprocessedEntityRemap);
3855     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3856     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3857     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3858     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3859 
3860     // Global -> local mappings.
3861     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3862   }
3863 }
3864 
3865 ASTReader::ASTReadResult
3866 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3867                                   const ModuleFile *ImportedBy,
3868                                   unsigned ClientLoadCapabilities) {
3869   unsigned Idx = 0;
3870   F.ModuleMapPath = ReadPath(F, Record, Idx);
3871 
3872   // Try to resolve ModuleName in the current header search context and
3873   // verify that it is found in the same module map file as we saved. If the
3874   // top-level AST file is a main file, skip this check because there is no
3875   // usable header search context.
3876   assert(!F.ModuleName.empty() &&
3877          "MODULE_NAME should come before MODULE_MAP_FILE");
3878   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3879     // An implicitly-loaded module file should have its module listed in some
3880     // module map file that we've already loaded.
3881     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3882     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3883     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3884     // Don't emit module relocation error if we have -fno-validate-pch
3885     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3886       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3887         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3888           // This module was defined by an imported (explicit) module.
3889           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3890                                                << ASTFE->getName();
3891         } else {
3892           // This module was built with a different module map.
3893           Diag(diag::err_imported_module_not_found)
3894               << F.ModuleName << F.FileName
3895               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3896               << !ImportedBy;
3897           // In case it was imported by a PCH, there's a chance the user is
3898           // just missing to include the search path to the directory containing
3899           // the modulemap.
3900           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3901             Diag(diag::note_imported_by_pch_module_not_found)
3902                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3903         }
3904       }
3905       return OutOfDate;
3906     }
3907 
3908     assert(M->Name == F.ModuleName && "found module with different name");
3909 
3910     // Check the primary module map file.
3911     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3912     if (!StoredModMap || *StoredModMap != ModMap) {
3913       assert(ModMap && "found module is missing module map file");
3914       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3915              "top-level import should be verified");
3916       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3917       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3918         Diag(diag::err_imported_module_modmap_changed)
3919             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3920             << ModMap->getName() << F.ModuleMapPath << NotImported;
3921       return OutOfDate;
3922     }
3923 
3924     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3925     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3926       // FIXME: we should use input files rather than storing names.
3927       std::string Filename = ReadPath(F, Record, Idx);
3928       auto F = FileMgr.getFile(Filename, false, false);
3929       if (!F) {
3930         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3931           Error("could not find file '" + Filename +"' referenced by AST file");
3932         return OutOfDate;
3933       }
3934       AdditionalStoredMaps.insert(*F);
3935     }
3936 
3937     // Check any additional module map files (e.g. module.private.modulemap)
3938     // that are not in the pcm.
3939     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3940       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3941         // Remove files that match
3942         // Note: SmallPtrSet::erase is really remove
3943         if (!AdditionalStoredMaps.erase(ModMap)) {
3944           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3945             Diag(diag::err_module_different_modmap)
3946               << F.ModuleName << /*new*/0 << ModMap->getName();
3947           return OutOfDate;
3948         }
3949       }
3950     }
3951 
3952     // Check any additional module map files that are in the pcm, but not
3953     // found in header search. Cases that match are already removed.
3954     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3955       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3956         Diag(diag::err_module_different_modmap)
3957           << F.ModuleName << /*not new*/1 << ModMap->getName();
3958       return OutOfDate;
3959     }
3960   }
3961 
3962   if (Listener)
3963     Listener->ReadModuleMapFile(F.ModuleMapPath);
3964   return Success;
3965 }
3966 
3967 /// Move the given method to the back of the global list of methods.
3968 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3969   // Find the entry for this selector in the method pool.
3970   Sema::GlobalMethodPool::iterator Known
3971     = S.MethodPool.find(Method->getSelector());
3972   if (Known == S.MethodPool.end())
3973     return;
3974 
3975   // Retrieve the appropriate method list.
3976   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3977                                                     : Known->second.second;
3978   bool Found = false;
3979   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3980     if (!Found) {
3981       if (List->getMethod() == Method) {
3982         Found = true;
3983       } else {
3984         // Keep searching.
3985         continue;
3986       }
3987     }
3988 
3989     if (List->getNext())
3990       List->setMethod(List->getNext()->getMethod());
3991     else
3992       List->setMethod(Method);
3993   }
3994 }
3995 
3996 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3997   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3998   for (Decl *D : Names) {
3999     bool wasHidden = D->isHidden();
4000     D->setVisibleDespiteOwningModule();
4001 
4002     if (wasHidden && SemaObj) {
4003       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4004         moveMethodToBackOfGlobalList(*SemaObj, Method);
4005       }
4006     }
4007   }
4008 }
4009 
4010 void ASTReader::makeModuleVisible(Module *Mod,
4011                                   Module::NameVisibilityKind NameVisibility,
4012                                   SourceLocation ImportLoc) {
4013   llvm::SmallPtrSet<Module *, 4> Visited;
4014   SmallVector<Module *, 4> Stack;
4015   Stack.push_back(Mod);
4016   while (!Stack.empty()) {
4017     Mod = Stack.pop_back_val();
4018 
4019     if (NameVisibility <= Mod->NameVisibility) {
4020       // This module already has this level of visibility (or greater), so
4021       // there is nothing more to do.
4022       continue;
4023     }
4024 
4025     if (!Mod->isAvailable()) {
4026       // Modules that aren't available cannot be made visible.
4027       continue;
4028     }
4029 
4030     // Update the module's name visibility.
4031     Mod->NameVisibility = NameVisibility;
4032 
4033     // If we've already deserialized any names from this module,
4034     // mark them as visible.
4035     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4036     if (Hidden != HiddenNamesMap.end()) {
4037       auto HiddenNames = std::move(*Hidden);
4038       HiddenNamesMap.erase(Hidden);
4039       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4040       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4041              "making names visible added hidden names");
4042     }
4043 
4044     // Push any exported modules onto the stack to be marked as visible.
4045     SmallVector<Module *, 16> Exports;
4046     Mod->getExportedModules(Exports);
4047     for (SmallVectorImpl<Module *>::iterator
4048            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4049       Module *Exported = *I;
4050       if (Visited.insert(Exported).second)
4051         Stack.push_back(Exported);
4052     }
4053   }
4054 }
4055 
4056 /// We've merged the definition \p MergedDef into the existing definition
4057 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4058 /// visible.
4059 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4060                                           NamedDecl *MergedDef) {
4061   if (Def->isHidden()) {
4062     // If MergedDef is visible or becomes visible, make the definition visible.
4063     if (!MergedDef->isHidden())
4064       Def->setVisibleDespiteOwningModule();
4065     else {
4066       getContext().mergeDefinitionIntoModule(
4067           Def, MergedDef->getImportedOwningModule(),
4068           /*NotifyListeners*/ false);
4069       PendingMergedDefinitionsToDeduplicate.insert(Def);
4070     }
4071   }
4072 }
4073 
4074 bool ASTReader::loadGlobalIndex() {
4075   if (GlobalIndex)
4076     return false;
4077 
4078   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4079       !PP.getLangOpts().Modules)
4080     return true;
4081 
4082   // Try to load the global index.
4083   TriedLoadingGlobalIndex = true;
4084   StringRef ModuleCachePath
4085     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4086   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4087       GlobalModuleIndex::readIndex(ModuleCachePath);
4088   if (llvm::Error Err = std::move(Result.second)) {
4089     assert(!Result.first);
4090     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4091     return true;
4092   }
4093 
4094   GlobalIndex.reset(Result.first);
4095   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4096   return false;
4097 }
4098 
4099 bool ASTReader::isGlobalIndexUnavailable() const {
4100   return PP.getLangOpts().Modules && UseGlobalIndex &&
4101          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4102 }
4103 
4104 static void updateModuleTimestamp(ModuleFile &MF) {
4105   // Overwrite the timestamp file contents so that file's mtime changes.
4106   std::string TimestampFilename = MF.getTimestampFilename();
4107   std::error_code EC;
4108   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4109   if (EC)
4110     return;
4111   OS << "Timestamp file\n";
4112   OS.close();
4113   OS.clear_error(); // Avoid triggering a fatal error.
4114 }
4115 
4116 /// Given a cursor at the start of an AST file, scan ahead and drop the
4117 /// cursor into the start of the given block ID, returning false on success and
4118 /// true on failure.
4119 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4120   while (true) {
4121     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4122     if (!MaybeEntry) {
4123       // FIXME this drops errors on the floor.
4124       consumeError(MaybeEntry.takeError());
4125       return true;
4126     }
4127     llvm::BitstreamEntry Entry = MaybeEntry.get();
4128 
4129     switch (Entry.Kind) {
4130     case llvm::BitstreamEntry::Error:
4131     case llvm::BitstreamEntry::EndBlock:
4132       return true;
4133 
4134     case llvm::BitstreamEntry::Record:
4135       // Ignore top-level records.
4136       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4137         break;
4138       else {
4139         // FIXME this drops errors on the floor.
4140         consumeError(Skipped.takeError());
4141         return true;
4142       }
4143 
4144     case llvm::BitstreamEntry::SubBlock:
4145       if (Entry.ID == BlockID) {
4146         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4147           // FIXME this drops the error on the floor.
4148           consumeError(std::move(Err));
4149           return true;
4150         }
4151         // Found it!
4152         return false;
4153       }
4154 
4155       if (llvm::Error Err = Cursor.SkipBlock()) {
4156         // FIXME this drops the error on the floor.
4157         consumeError(std::move(Err));
4158         return true;
4159       }
4160     }
4161   }
4162 }
4163 
4164 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4165                                             ModuleKind Type,
4166                                             SourceLocation ImportLoc,
4167                                             unsigned ClientLoadCapabilities,
4168                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4169   llvm::SaveAndRestore<SourceLocation>
4170     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4171 
4172   // Defer any pending actions until we get to the end of reading the AST file.
4173   Deserializing AnASTFile(this);
4174 
4175   // Bump the generation number.
4176   unsigned PreviousGeneration = 0;
4177   if (ContextObj)
4178     PreviousGeneration = incrementGeneration(*ContextObj);
4179 
4180   unsigned NumModules = ModuleMgr.size();
4181   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4182     assert(ReadResult && "expected to return error");
4183     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4184                             PP.getLangOpts().Modules
4185                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4186                                 : nullptr);
4187 
4188     // If we find that any modules are unusable, the global index is going
4189     // to be out-of-date. Just remove it.
4190     GlobalIndex.reset();
4191     ModuleMgr.setGlobalIndex(nullptr);
4192     return ReadResult;
4193   };
4194 
4195   SmallVector<ImportedModule, 4> Loaded;
4196   switch (ASTReadResult ReadResult =
4197               ReadASTCore(FileName, Type, ImportLoc,
4198                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4199                           ASTFileSignature(), ClientLoadCapabilities)) {
4200   case Failure:
4201   case Missing:
4202   case OutOfDate:
4203   case VersionMismatch:
4204   case ConfigurationMismatch:
4205   case HadErrors:
4206     return removeModulesAndReturn(ReadResult);
4207   case Success:
4208     break;
4209   }
4210 
4211   // Here comes stuff that we only do once the entire chain is loaded.
4212 
4213   // Load the AST blocks of all of the modules that we loaded.  We can still
4214   // hit errors parsing the ASTs at this point.
4215   for (ImportedModule &M : Loaded) {
4216     ModuleFile &F = *M.Mod;
4217 
4218     // Read the AST block.
4219     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4220       return removeModulesAndReturn(Result);
4221 
4222     // The AST block should always have a definition for the main module.
4223     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4224       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4225       return removeModulesAndReturn(Failure);
4226     }
4227 
4228     // Read the extension blocks.
4229     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4230       if (ASTReadResult Result = ReadExtensionBlock(F))
4231         return removeModulesAndReturn(Result);
4232     }
4233 
4234     // Once read, set the ModuleFile bit base offset and update the size in
4235     // bits of all files we've seen.
4236     F.GlobalBitOffset = TotalModulesSizeInBits;
4237     TotalModulesSizeInBits += F.SizeInBits;
4238     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4239   }
4240 
4241   // Preload source locations and interesting indentifiers.
4242   for (ImportedModule &M : Loaded) {
4243     ModuleFile &F = *M.Mod;
4244 
4245     // Preload SLocEntries.
4246     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4247       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4248       // Load it through the SourceManager and don't call ReadSLocEntry()
4249       // directly because the entry may have already been loaded in which case
4250       // calling ReadSLocEntry() directly would trigger an assertion in
4251       // SourceManager.
4252       SourceMgr.getLoadedSLocEntryByID(Index);
4253     }
4254 
4255     // Map the original source file ID into the ID space of the current
4256     // compilation.
4257     if (F.OriginalSourceFileID.isValid()) {
4258       F.OriginalSourceFileID = FileID::get(
4259           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4260     }
4261 
4262     // Preload all the pending interesting identifiers by marking them out of
4263     // date.
4264     for (auto Offset : F.PreloadIdentifierOffsets) {
4265       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4266           F.IdentifierTableData + Offset);
4267 
4268       ASTIdentifierLookupTrait Trait(*this, F);
4269       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4270       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4271       auto &II = PP.getIdentifierTable().getOwn(Key);
4272       II.setOutOfDate(true);
4273 
4274       // Mark this identifier as being from an AST file so that we can track
4275       // whether we need to serialize it.
4276       markIdentifierFromAST(*this, II);
4277 
4278       // Associate the ID with the identifier so that the writer can reuse it.
4279       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4280       SetIdentifierInfo(ID, &II);
4281     }
4282   }
4283 
4284   // Setup the import locations and notify the module manager that we've
4285   // committed to these module files.
4286   for (ImportedModule &M : Loaded) {
4287     ModuleFile &F = *M.Mod;
4288 
4289     ModuleMgr.moduleFileAccepted(&F);
4290 
4291     // Set the import location.
4292     F.DirectImportLoc = ImportLoc;
4293     // FIXME: We assume that locations from PCH / preamble do not need
4294     // any translation.
4295     if (!M.ImportedBy)
4296       F.ImportLoc = M.ImportLoc;
4297     else
4298       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4299   }
4300 
4301   if (!PP.getLangOpts().CPlusPlus ||
4302       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4303        Type != MK_PrebuiltModule)) {
4304     // Mark all of the identifiers in the identifier table as being out of date,
4305     // so that various accessors know to check the loaded modules when the
4306     // identifier is used.
4307     //
4308     // For C++ modules, we don't need information on many identifiers (just
4309     // those that provide macros or are poisoned), so we mark all of
4310     // the interesting ones via PreloadIdentifierOffsets.
4311     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4312                                 IdEnd = PP.getIdentifierTable().end();
4313          Id != IdEnd; ++Id)
4314       Id->second->setOutOfDate(true);
4315   }
4316   // Mark selectors as out of date.
4317   for (auto Sel : SelectorGeneration)
4318     SelectorOutOfDate[Sel.first] = true;
4319 
4320   // Resolve any unresolved module exports.
4321   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4322     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4323     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4324     Module *ResolvedMod = getSubmodule(GlobalID);
4325 
4326     switch (Unresolved.Kind) {
4327     case UnresolvedModuleRef::Conflict:
4328       if (ResolvedMod) {
4329         Module::Conflict Conflict;
4330         Conflict.Other = ResolvedMod;
4331         Conflict.Message = Unresolved.String.str();
4332         Unresolved.Mod->Conflicts.push_back(Conflict);
4333       }
4334       continue;
4335 
4336     case UnresolvedModuleRef::Import:
4337       if (ResolvedMod)
4338         Unresolved.Mod->Imports.insert(ResolvedMod);
4339       continue;
4340 
4341     case UnresolvedModuleRef::Export:
4342       if (ResolvedMod || Unresolved.IsWildcard)
4343         Unresolved.Mod->Exports.push_back(
4344           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4345       continue;
4346     }
4347   }
4348   UnresolvedModuleRefs.clear();
4349 
4350   if (Imported)
4351     Imported->append(ImportedModules.begin(),
4352                      ImportedModules.end());
4353 
4354   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4355   // Might be unnecessary as use declarations are only used to build the
4356   // module itself.
4357 
4358   if (ContextObj)
4359     InitializeContext();
4360 
4361   if (SemaObj)
4362     UpdateSema();
4363 
4364   if (DeserializationListener)
4365     DeserializationListener->ReaderInitialized(this);
4366 
4367   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4368   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4369     // If this AST file is a precompiled preamble, then set the
4370     // preamble file ID of the source manager to the file source file
4371     // from which the preamble was built.
4372     if (Type == MK_Preamble) {
4373       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4374     } else if (Type == MK_MainFile) {
4375       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4376     }
4377   }
4378 
4379   // For any Objective-C class definitions we have already loaded, make sure
4380   // that we load any additional categories.
4381   if (ContextObj) {
4382     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4383       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4384                          ObjCClassesLoaded[I],
4385                          PreviousGeneration);
4386     }
4387   }
4388 
4389   if (PP.getHeaderSearchInfo()
4390           .getHeaderSearchOpts()
4391           .ModulesValidateOncePerBuildSession) {
4392     // Now we are certain that the module and all modules it depends on are
4393     // up to date.  Create or update timestamp files for modules that are
4394     // located in the module cache (not for PCH files that could be anywhere
4395     // in the filesystem).
4396     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4397       ImportedModule &M = Loaded[I];
4398       if (M.Mod->Kind == MK_ImplicitModule) {
4399         updateModuleTimestamp(*M.Mod);
4400       }
4401     }
4402   }
4403 
4404   return Success;
4405 }
4406 
4407 static ASTFileSignature readASTFileSignature(StringRef PCH);
4408 
4409 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4410 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4411   // FIXME checking magic headers is done in other places such as
4412   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4413   // always done the same. Unify it all with a helper.
4414   if (!Stream.canSkipToPos(4))
4415     return llvm::createStringError(std::errc::illegal_byte_sequence,
4416                                    "file too small to contain AST file magic");
4417   for (unsigned C : {'C', 'P', 'C', 'H'})
4418     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4419       if (Res.get() != C)
4420         return llvm::createStringError(
4421             std::errc::illegal_byte_sequence,
4422             "file doesn't start with AST file magic");
4423     } else
4424       return Res.takeError();
4425   return llvm::Error::success();
4426 }
4427 
4428 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4429   switch (Kind) {
4430   case MK_PCH:
4431     return 0; // PCH
4432   case MK_ImplicitModule:
4433   case MK_ExplicitModule:
4434   case MK_PrebuiltModule:
4435     return 1; // module
4436   case MK_MainFile:
4437   case MK_Preamble:
4438     return 2; // main source file
4439   }
4440   llvm_unreachable("unknown module kind");
4441 }
4442 
4443 ASTReader::ASTReadResult
4444 ASTReader::ReadASTCore(StringRef FileName,
4445                        ModuleKind Type,
4446                        SourceLocation ImportLoc,
4447                        ModuleFile *ImportedBy,
4448                        SmallVectorImpl<ImportedModule> &Loaded,
4449                        off_t ExpectedSize, time_t ExpectedModTime,
4450                        ASTFileSignature ExpectedSignature,
4451                        unsigned ClientLoadCapabilities) {
4452   ModuleFile *M;
4453   std::string ErrorStr;
4454   ModuleManager::AddModuleResult AddResult
4455     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4456                           getGeneration(), ExpectedSize, ExpectedModTime,
4457                           ExpectedSignature, readASTFileSignature,
4458                           M, ErrorStr);
4459 
4460   switch (AddResult) {
4461   case ModuleManager::AlreadyLoaded:
4462     Diag(diag::remark_module_import)
4463         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4464         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4465     return Success;
4466 
4467   case ModuleManager::NewlyLoaded:
4468     // Load module file below.
4469     break;
4470 
4471   case ModuleManager::Missing:
4472     // The module file was missing; if the client can handle that, return
4473     // it.
4474     if (ClientLoadCapabilities & ARR_Missing)
4475       return Missing;
4476 
4477     // Otherwise, return an error.
4478     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4479                                           << FileName << !ErrorStr.empty()
4480                                           << ErrorStr;
4481     return Failure;
4482 
4483   case ModuleManager::OutOfDate:
4484     // We couldn't load the module file because it is out-of-date. If the
4485     // client can handle out-of-date, return it.
4486     if (ClientLoadCapabilities & ARR_OutOfDate)
4487       return OutOfDate;
4488 
4489     // Otherwise, return an error.
4490     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4491                                             << FileName << !ErrorStr.empty()
4492                                             << ErrorStr;
4493     return Failure;
4494   }
4495 
4496   assert(M && "Missing module file");
4497 
4498   bool ShouldFinalizePCM = false;
4499   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4500     auto &MC = getModuleManager().getModuleCache();
4501     if (ShouldFinalizePCM)
4502       MC.finalizePCM(FileName);
4503     else
4504       MC.tryToDropPCM(FileName);
4505   });
4506   ModuleFile &F = *M;
4507   BitstreamCursor &Stream = F.Stream;
4508   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4509   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4510 
4511   // Sniff for the signature.
4512   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4513     Diag(diag::err_module_file_invalid)
4514         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4515     return Failure;
4516   }
4517 
4518   // This is used for compatibility with older PCH formats.
4519   bool HaveReadControlBlock = false;
4520   while (true) {
4521     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4522     if (!MaybeEntry) {
4523       Error(MaybeEntry.takeError());
4524       return Failure;
4525     }
4526     llvm::BitstreamEntry Entry = MaybeEntry.get();
4527 
4528     switch (Entry.Kind) {
4529     case llvm::BitstreamEntry::Error:
4530     case llvm::BitstreamEntry::Record:
4531     case llvm::BitstreamEntry::EndBlock:
4532       Error("invalid record at top-level of AST file");
4533       return Failure;
4534 
4535     case llvm::BitstreamEntry::SubBlock:
4536       break;
4537     }
4538 
4539     switch (Entry.ID) {
4540     case CONTROL_BLOCK_ID:
4541       HaveReadControlBlock = true;
4542       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4543       case Success:
4544         // Check that we didn't try to load a non-module AST file as a module.
4545         //
4546         // FIXME: Should we also perform the converse check? Loading a module as
4547         // a PCH file sort of works, but it's a bit wonky.
4548         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4549              Type == MK_PrebuiltModule) &&
4550             F.ModuleName.empty()) {
4551           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4552           if (Result != OutOfDate ||
4553               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4554             Diag(diag::err_module_file_not_module) << FileName;
4555           return Result;
4556         }
4557         break;
4558 
4559       case Failure: return Failure;
4560       case Missing: return Missing;
4561       case OutOfDate: return OutOfDate;
4562       case VersionMismatch: return VersionMismatch;
4563       case ConfigurationMismatch: return ConfigurationMismatch;
4564       case HadErrors: return HadErrors;
4565       }
4566       break;
4567 
4568     case AST_BLOCK_ID:
4569       if (!HaveReadControlBlock) {
4570         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4571           Diag(diag::err_pch_version_too_old);
4572         return VersionMismatch;
4573       }
4574 
4575       // Record that we've loaded this module.
4576       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4577       ShouldFinalizePCM = true;
4578       return Success;
4579 
4580     case UNHASHED_CONTROL_BLOCK_ID:
4581       // This block is handled using look-ahead during ReadControlBlock.  We
4582       // shouldn't get here!
4583       Error("malformed block record in AST file");
4584       return Failure;
4585 
4586     default:
4587       if (llvm::Error Err = Stream.SkipBlock()) {
4588         Error(std::move(Err));
4589         return Failure;
4590       }
4591       break;
4592     }
4593   }
4594 
4595   llvm_unreachable("unexpected break; expected return");
4596 }
4597 
4598 ASTReader::ASTReadResult
4599 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4600                                     unsigned ClientLoadCapabilities) {
4601   const HeaderSearchOptions &HSOpts =
4602       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4603   bool AllowCompatibleConfigurationMismatch =
4604       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4605 
4606   ASTReadResult Result = readUnhashedControlBlockImpl(
4607       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4608       Listener.get(),
4609       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4610 
4611   // If F was directly imported by another module, it's implicitly validated by
4612   // the importing module.
4613   if (DisableValidation || WasImportedBy ||
4614       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4615     return Success;
4616 
4617   if (Result == Failure) {
4618     Error("malformed block record in AST file");
4619     return Failure;
4620   }
4621 
4622   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4623     // If this module has already been finalized in the ModuleCache, we're stuck
4624     // with it; we can only load a single version of each module.
4625     //
4626     // This can happen when a module is imported in two contexts: in one, as a
4627     // user module; in another, as a system module (due to an import from
4628     // another module marked with the [system] flag).  It usually indicates a
4629     // bug in the module map: this module should also be marked with [system].
4630     //
4631     // If -Wno-system-headers (the default), and the first import is as a
4632     // system module, then validation will fail during the as-user import,
4633     // since -Werror flags won't have been validated.  However, it's reasonable
4634     // to treat this consistently as a system module.
4635     //
4636     // If -Wsystem-headers, the PCM on disk was built with
4637     // -Wno-system-headers, and the first import is as a user module, then
4638     // validation will fail during the as-system import since the PCM on disk
4639     // doesn't guarantee that -Werror was respected.  However, the -Werror
4640     // flags were checked during the initial as-user import.
4641     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4642       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4643       return Success;
4644     }
4645   }
4646 
4647   return Result;
4648 }
4649 
4650 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4651     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4652     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4653     bool ValidateDiagnosticOptions) {
4654   // Initialize a stream.
4655   BitstreamCursor Stream(StreamData);
4656 
4657   // Sniff for the signature.
4658   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4659     // FIXME this drops the error on the floor.
4660     consumeError(std::move(Err));
4661     return Failure;
4662   }
4663 
4664   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4665   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4666     return Failure;
4667 
4668   // Read all of the records in the options block.
4669   RecordData Record;
4670   ASTReadResult Result = Success;
4671   while (true) {
4672     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4673     if (!MaybeEntry) {
4674       // FIXME this drops the error on the floor.
4675       consumeError(MaybeEntry.takeError());
4676       return Failure;
4677     }
4678     llvm::BitstreamEntry Entry = MaybeEntry.get();
4679 
4680     switch (Entry.Kind) {
4681     case llvm::BitstreamEntry::Error:
4682     case llvm::BitstreamEntry::SubBlock:
4683       return Failure;
4684 
4685     case llvm::BitstreamEntry::EndBlock:
4686       return Result;
4687 
4688     case llvm::BitstreamEntry::Record:
4689       // The interesting case.
4690       break;
4691     }
4692 
4693     // Read and process a record.
4694     Record.clear();
4695     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4696     if (!MaybeRecordType) {
4697       // FIXME this drops the error.
4698       return Failure;
4699     }
4700     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4701     case SIGNATURE:
4702       if (F)
4703         std::copy(Record.begin(), Record.end(), F->Signature.data());
4704       break;
4705     case DIAGNOSTIC_OPTIONS: {
4706       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4707       if (Listener && ValidateDiagnosticOptions &&
4708           !AllowCompatibleConfigurationMismatch &&
4709           ParseDiagnosticOptions(Record, Complain, *Listener))
4710         Result = OutOfDate; // Don't return early.  Read the signature.
4711       break;
4712     }
4713     case DIAG_PRAGMA_MAPPINGS:
4714       if (!F)
4715         break;
4716       if (F->PragmaDiagMappings.empty())
4717         F->PragmaDiagMappings.swap(Record);
4718       else
4719         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4720                                      Record.begin(), Record.end());
4721       break;
4722     }
4723   }
4724 }
4725 
4726 /// Parse a record and blob containing module file extension metadata.
4727 static bool parseModuleFileExtensionMetadata(
4728               const SmallVectorImpl<uint64_t> &Record,
4729               StringRef Blob,
4730               ModuleFileExtensionMetadata &Metadata) {
4731   if (Record.size() < 4) return true;
4732 
4733   Metadata.MajorVersion = Record[0];
4734   Metadata.MinorVersion = Record[1];
4735 
4736   unsigned BlockNameLen = Record[2];
4737   unsigned UserInfoLen = Record[3];
4738 
4739   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4740 
4741   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4742   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4743                                   Blob.data() + BlockNameLen + UserInfoLen);
4744   return false;
4745 }
4746 
4747 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4748   BitstreamCursor &Stream = F.Stream;
4749 
4750   RecordData Record;
4751   while (true) {
4752     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4753     if (!MaybeEntry) {
4754       Error(MaybeEntry.takeError());
4755       return Failure;
4756     }
4757     llvm::BitstreamEntry Entry = MaybeEntry.get();
4758 
4759     switch (Entry.Kind) {
4760     case llvm::BitstreamEntry::SubBlock:
4761       if (llvm::Error Err = Stream.SkipBlock()) {
4762         Error(std::move(Err));
4763         return Failure;
4764       }
4765       continue;
4766 
4767     case llvm::BitstreamEntry::EndBlock:
4768       return Success;
4769 
4770     case llvm::BitstreamEntry::Error:
4771       return HadErrors;
4772 
4773     case llvm::BitstreamEntry::Record:
4774       break;
4775     }
4776 
4777     Record.clear();
4778     StringRef Blob;
4779     Expected<unsigned> MaybeRecCode =
4780         Stream.readRecord(Entry.ID, Record, &Blob);
4781     if (!MaybeRecCode) {
4782       Error(MaybeRecCode.takeError());
4783       return Failure;
4784     }
4785     switch (MaybeRecCode.get()) {
4786     case EXTENSION_METADATA: {
4787       ModuleFileExtensionMetadata Metadata;
4788       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4789         Error("malformed EXTENSION_METADATA in AST file");
4790         return Failure;
4791       }
4792 
4793       // Find a module file extension with this block name.
4794       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4795       if (Known == ModuleFileExtensions.end()) break;
4796 
4797       // Form a reader.
4798       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4799                                                              F, Stream)) {
4800         F.ExtensionReaders.push_back(std::move(Reader));
4801       }
4802 
4803       break;
4804     }
4805     }
4806   }
4807 
4808   return Success;
4809 }
4810 
4811 void ASTReader::InitializeContext() {
4812   assert(ContextObj && "no context to initialize");
4813   ASTContext &Context = *ContextObj;
4814 
4815   // If there's a listener, notify them that we "read" the translation unit.
4816   if (DeserializationListener)
4817     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4818                                       Context.getTranslationUnitDecl());
4819 
4820   // FIXME: Find a better way to deal with collisions between these
4821   // built-in types. Right now, we just ignore the problem.
4822 
4823   // Load the special types.
4824   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4825     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4826       if (!Context.CFConstantStringTypeDecl)
4827         Context.setCFConstantStringType(GetType(String));
4828     }
4829 
4830     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4831       QualType FileType = GetType(File);
4832       if (FileType.isNull()) {
4833         Error("FILE type is NULL");
4834         return;
4835       }
4836 
4837       if (!Context.FILEDecl) {
4838         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4839           Context.setFILEDecl(Typedef->getDecl());
4840         else {
4841           const TagType *Tag = FileType->getAs<TagType>();
4842           if (!Tag) {
4843             Error("Invalid FILE type in AST file");
4844             return;
4845           }
4846           Context.setFILEDecl(Tag->getDecl());
4847         }
4848       }
4849     }
4850 
4851     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4852       QualType Jmp_bufType = GetType(Jmp_buf);
4853       if (Jmp_bufType.isNull()) {
4854         Error("jmp_buf type is NULL");
4855         return;
4856       }
4857 
4858       if (!Context.jmp_bufDecl) {
4859         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4860           Context.setjmp_bufDecl(Typedef->getDecl());
4861         else {
4862           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4863           if (!Tag) {
4864             Error("Invalid jmp_buf type in AST file");
4865             return;
4866           }
4867           Context.setjmp_bufDecl(Tag->getDecl());
4868         }
4869       }
4870     }
4871 
4872     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4873       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4874       if (Sigjmp_bufType.isNull()) {
4875         Error("sigjmp_buf type is NULL");
4876         return;
4877       }
4878 
4879       if (!Context.sigjmp_bufDecl) {
4880         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4881           Context.setsigjmp_bufDecl(Typedef->getDecl());
4882         else {
4883           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4884           assert(Tag && "Invalid sigjmp_buf type in AST file");
4885           Context.setsigjmp_bufDecl(Tag->getDecl());
4886         }
4887       }
4888     }
4889 
4890     if (unsigned ObjCIdRedef
4891           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4892       if (Context.ObjCIdRedefinitionType.isNull())
4893         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4894     }
4895 
4896     if (unsigned ObjCClassRedef
4897           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4898       if (Context.ObjCClassRedefinitionType.isNull())
4899         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4900     }
4901 
4902     if (unsigned ObjCSelRedef
4903           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4904       if (Context.ObjCSelRedefinitionType.isNull())
4905         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4906     }
4907 
4908     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4909       QualType Ucontext_tType = GetType(Ucontext_t);
4910       if (Ucontext_tType.isNull()) {
4911         Error("ucontext_t type is NULL");
4912         return;
4913       }
4914 
4915       if (!Context.ucontext_tDecl) {
4916         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4917           Context.setucontext_tDecl(Typedef->getDecl());
4918         else {
4919           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4920           assert(Tag && "Invalid ucontext_t type in AST file");
4921           Context.setucontext_tDecl(Tag->getDecl());
4922         }
4923       }
4924     }
4925   }
4926 
4927   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4928 
4929   // If there were any CUDA special declarations, deserialize them.
4930   if (!CUDASpecialDeclRefs.empty()) {
4931     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4932     Context.setcudaConfigureCallDecl(
4933                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4934   }
4935 
4936   // Re-export any modules that were imported by a non-module AST file.
4937   // FIXME: This does not make macro-only imports visible again.
4938   for (auto &Import : ImportedModules) {
4939     if (Module *Imported = getSubmodule(Import.ID)) {
4940       makeModuleVisible(Imported, Module::AllVisible,
4941                         /*ImportLoc=*/Import.ImportLoc);
4942       if (Import.ImportLoc.isValid())
4943         PP.makeModuleVisible(Imported, Import.ImportLoc);
4944       // FIXME: should we tell Sema to make the module visible too?
4945     }
4946   }
4947   ImportedModules.clear();
4948 }
4949 
4950 void ASTReader::finalizeForWriting() {
4951   // Nothing to do for now.
4952 }
4953 
4954 /// Reads and return the signature record from \p PCH's control block, or
4955 /// else returns 0.
4956 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4957   BitstreamCursor Stream(PCH);
4958   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4959     // FIXME this drops the error on the floor.
4960     consumeError(std::move(Err));
4961     return ASTFileSignature();
4962   }
4963 
4964   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4965   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4966     return ASTFileSignature();
4967 
4968   // Scan for SIGNATURE inside the diagnostic options block.
4969   ASTReader::RecordData Record;
4970   while (true) {
4971     Expected<llvm::BitstreamEntry> MaybeEntry =
4972         Stream.advanceSkippingSubblocks();
4973     if (!MaybeEntry) {
4974       // FIXME this drops the error on the floor.
4975       consumeError(MaybeEntry.takeError());
4976       return ASTFileSignature();
4977     }
4978     llvm::BitstreamEntry Entry = MaybeEntry.get();
4979 
4980     if (Entry.Kind != llvm::BitstreamEntry::Record)
4981       return ASTFileSignature();
4982 
4983     Record.clear();
4984     StringRef Blob;
4985     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4986     if (!MaybeRecord) {
4987       // FIXME this drops the error on the floor.
4988       consumeError(MaybeRecord.takeError());
4989       return ASTFileSignature();
4990     }
4991     if (SIGNATURE == MaybeRecord.get())
4992       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4993                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4994   }
4995 }
4996 
4997 /// Retrieve the name of the original source file name
4998 /// directly from the AST file, without actually loading the AST
4999 /// file.
5000 std::string ASTReader::getOriginalSourceFile(
5001     const std::string &ASTFileName, FileManager &FileMgr,
5002     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5003   // Open the AST file.
5004   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5005   if (!Buffer) {
5006     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5007         << ASTFileName << Buffer.getError().message();
5008     return std::string();
5009   }
5010 
5011   // Initialize the stream
5012   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5013 
5014   // Sniff for the signature.
5015   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5016     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5017     return std::string();
5018   }
5019 
5020   // Scan for the CONTROL_BLOCK_ID block.
5021   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5022     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5023     return std::string();
5024   }
5025 
5026   // Scan for ORIGINAL_FILE inside the control block.
5027   RecordData Record;
5028   while (true) {
5029     Expected<llvm::BitstreamEntry> MaybeEntry =
5030         Stream.advanceSkippingSubblocks();
5031     if (!MaybeEntry) {
5032       // FIXME this drops errors on the floor.
5033       consumeError(MaybeEntry.takeError());
5034       return std::string();
5035     }
5036     llvm::BitstreamEntry Entry = MaybeEntry.get();
5037 
5038     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5039       return std::string();
5040 
5041     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5042       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5043       return std::string();
5044     }
5045 
5046     Record.clear();
5047     StringRef Blob;
5048     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5049     if (!MaybeRecord) {
5050       // FIXME this drops the errors on the floor.
5051       consumeError(MaybeRecord.takeError());
5052       return std::string();
5053     }
5054     if (ORIGINAL_FILE == MaybeRecord.get())
5055       return Blob.str();
5056   }
5057 }
5058 
5059 namespace {
5060 
5061   class SimplePCHValidator : public ASTReaderListener {
5062     const LangOptions &ExistingLangOpts;
5063     const TargetOptions &ExistingTargetOpts;
5064     const PreprocessorOptions &ExistingPPOpts;
5065     std::string ExistingModuleCachePath;
5066     FileManager &FileMgr;
5067 
5068   public:
5069     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5070                        const TargetOptions &ExistingTargetOpts,
5071                        const PreprocessorOptions &ExistingPPOpts,
5072                        StringRef ExistingModuleCachePath,
5073                        FileManager &FileMgr)
5074       : ExistingLangOpts(ExistingLangOpts),
5075         ExistingTargetOpts(ExistingTargetOpts),
5076         ExistingPPOpts(ExistingPPOpts),
5077         ExistingModuleCachePath(ExistingModuleCachePath),
5078         FileMgr(FileMgr) {}
5079 
5080     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5081                              bool AllowCompatibleDifferences) override {
5082       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5083                                   AllowCompatibleDifferences);
5084     }
5085 
5086     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5087                            bool AllowCompatibleDifferences) override {
5088       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5089                                 AllowCompatibleDifferences);
5090     }
5091 
5092     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5093                                  StringRef SpecificModuleCachePath,
5094                                  bool Complain) override {
5095       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5096                                       ExistingModuleCachePath,
5097                                       nullptr, ExistingLangOpts);
5098     }
5099 
5100     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5101                                  bool Complain,
5102                                  std::string &SuggestedPredefines) override {
5103       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5104                                       SuggestedPredefines, ExistingLangOpts);
5105     }
5106   };
5107 
5108 } // namespace
5109 
5110 bool ASTReader::readASTFileControlBlock(
5111     StringRef Filename, FileManager &FileMgr,
5112     const PCHContainerReader &PCHContainerRdr,
5113     bool FindModuleFileExtensions,
5114     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5115   // Open the AST file.
5116   // FIXME: This allows use of the VFS; we do not allow use of the
5117   // VFS when actually loading a module.
5118   auto Buffer = FileMgr.getBufferForFile(Filename);
5119   if (!Buffer) {
5120     return true;
5121   }
5122 
5123   // Initialize the stream
5124   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5125   BitstreamCursor Stream(Bytes);
5126 
5127   // Sniff for the signature.
5128   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5129     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5130     return true;
5131   }
5132 
5133   // Scan for the CONTROL_BLOCK_ID block.
5134   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5135     return true;
5136 
5137   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5138   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5139   bool NeedsImports = Listener.needsImportVisitation();
5140   BitstreamCursor InputFilesCursor;
5141 
5142   RecordData Record;
5143   std::string ModuleDir;
5144   bool DoneWithControlBlock = false;
5145   while (!DoneWithControlBlock) {
5146     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5147     if (!MaybeEntry) {
5148       // FIXME this drops the error on the floor.
5149       consumeError(MaybeEntry.takeError());
5150       return true;
5151     }
5152     llvm::BitstreamEntry Entry = MaybeEntry.get();
5153 
5154     switch (Entry.Kind) {
5155     case llvm::BitstreamEntry::SubBlock: {
5156       switch (Entry.ID) {
5157       case OPTIONS_BLOCK_ID: {
5158         std::string IgnoredSuggestedPredefines;
5159         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5160                              /*AllowCompatibleConfigurationMismatch*/ false,
5161                              Listener, IgnoredSuggestedPredefines) != Success)
5162           return true;
5163         break;
5164       }
5165 
5166       case INPUT_FILES_BLOCK_ID:
5167         InputFilesCursor = Stream;
5168         if (llvm::Error Err = Stream.SkipBlock()) {
5169           // FIXME this drops the error on the floor.
5170           consumeError(std::move(Err));
5171           return true;
5172         }
5173         if (NeedsInputFiles &&
5174             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5175           return true;
5176         break;
5177 
5178       default:
5179         if (llvm::Error Err = Stream.SkipBlock()) {
5180           // FIXME this drops the error on the floor.
5181           consumeError(std::move(Err));
5182           return true;
5183         }
5184         break;
5185       }
5186 
5187       continue;
5188     }
5189 
5190     case llvm::BitstreamEntry::EndBlock:
5191       DoneWithControlBlock = true;
5192       break;
5193 
5194     case llvm::BitstreamEntry::Error:
5195       return true;
5196 
5197     case llvm::BitstreamEntry::Record:
5198       break;
5199     }
5200 
5201     if (DoneWithControlBlock) break;
5202 
5203     Record.clear();
5204     StringRef Blob;
5205     Expected<unsigned> MaybeRecCode =
5206         Stream.readRecord(Entry.ID, Record, &Blob);
5207     if (!MaybeRecCode) {
5208       // FIXME this drops the error.
5209       return Failure;
5210     }
5211     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5212     case METADATA:
5213       if (Record[0] != VERSION_MAJOR)
5214         return true;
5215       if (Listener.ReadFullVersionInformation(Blob))
5216         return true;
5217       break;
5218     case MODULE_NAME:
5219       Listener.ReadModuleName(Blob);
5220       break;
5221     case MODULE_DIRECTORY:
5222       ModuleDir = Blob;
5223       break;
5224     case MODULE_MAP_FILE: {
5225       unsigned Idx = 0;
5226       auto Path = ReadString(Record, Idx);
5227       ResolveImportedPath(Path, ModuleDir);
5228       Listener.ReadModuleMapFile(Path);
5229       break;
5230     }
5231     case INPUT_FILE_OFFSETS: {
5232       if (!NeedsInputFiles)
5233         break;
5234 
5235       unsigned NumInputFiles = Record[0];
5236       unsigned NumUserFiles = Record[1];
5237       const llvm::support::unaligned_uint64_t *InputFileOffs =
5238           (const llvm::support::unaligned_uint64_t *)Blob.data();
5239       for (unsigned I = 0; I != NumInputFiles; ++I) {
5240         // Go find this input file.
5241         bool isSystemFile = I >= NumUserFiles;
5242 
5243         if (isSystemFile && !NeedsSystemInputFiles)
5244           break; // the rest are system input files
5245 
5246         BitstreamCursor &Cursor = InputFilesCursor;
5247         SavedStreamPosition SavedPosition(Cursor);
5248         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5249           // FIXME this drops errors on the floor.
5250           consumeError(std::move(Err));
5251         }
5252 
5253         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5254         if (!MaybeCode) {
5255           // FIXME this drops errors on the floor.
5256           consumeError(MaybeCode.takeError());
5257         }
5258         unsigned Code = MaybeCode.get();
5259 
5260         RecordData Record;
5261         StringRef Blob;
5262         bool shouldContinue = false;
5263         Expected<unsigned> MaybeRecordType =
5264             Cursor.readRecord(Code, Record, &Blob);
5265         if (!MaybeRecordType) {
5266           // FIXME this drops errors on the floor.
5267           consumeError(MaybeRecordType.takeError());
5268         }
5269         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5270         case INPUT_FILE_HASH:
5271           break;
5272         case INPUT_FILE:
5273           bool Overridden = static_cast<bool>(Record[3]);
5274           std::string Filename = Blob;
5275           ResolveImportedPath(Filename, ModuleDir);
5276           shouldContinue = Listener.visitInputFile(
5277               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5278           break;
5279         }
5280         if (!shouldContinue)
5281           break;
5282       }
5283       break;
5284     }
5285 
5286     case IMPORTS: {
5287       if (!NeedsImports)
5288         break;
5289 
5290       unsigned Idx = 0, N = Record.size();
5291       while (Idx < N) {
5292         // Read information about the AST file.
5293         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5294         std::string ModuleName = ReadString(Record, Idx);
5295         std::string Filename = ReadString(Record, Idx);
5296         ResolveImportedPath(Filename, ModuleDir);
5297         Listener.visitImport(ModuleName, Filename);
5298       }
5299       break;
5300     }
5301 
5302     default:
5303       // No other validation to perform.
5304       break;
5305     }
5306   }
5307 
5308   // Look for module file extension blocks, if requested.
5309   if (FindModuleFileExtensions) {
5310     BitstreamCursor SavedStream = Stream;
5311     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5312       bool DoneWithExtensionBlock = false;
5313       while (!DoneWithExtensionBlock) {
5314         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5315         if (!MaybeEntry) {
5316           // FIXME this drops the error.
5317           return true;
5318         }
5319         llvm::BitstreamEntry Entry = MaybeEntry.get();
5320 
5321         switch (Entry.Kind) {
5322         case llvm::BitstreamEntry::SubBlock:
5323           if (llvm::Error Err = Stream.SkipBlock()) {
5324             // FIXME this drops the error on the floor.
5325             consumeError(std::move(Err));
5326             return true;
5327           }
5328           continue;
5329 
5330         case llvm::BitstreamEntry::EndBlock:
5331           DoneWithExtensionBlock = true;
5332           continue;
5333 
5334         case llvm::BitstreamEntry::Error:
5335           return true;
5336 
5337         case llvm::BitstreamEntry::Record:
5338           break;
5339         }
5340 
5341        Record.clear();
5342        StringRef Blob;
5343        Expected<unsigned> MaybeRecCode =
5344            Stream.readRecord(Entry.ID, Record, &Blob);
5345        if (!MaybeRecCode) {
5346          // FIXME this drops the error.
5347          return true;
5348        }
5349        switch (MaybeRecCode.get()) {
5350        case EXTENSION_METADATA: {
5351          ModuleFileExtensionMetadata Metadata;
5352          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5353            return true;
5354 
5355          Listener.readModuleFileExtension(Metadata);
5356          break;
5357        }
5358        }
5359       }
5360     }
5361     Stream = SavedStream;
5362   }
5363 
5364   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5365   if (readUnhashedControlBlockImpl(
5366           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5367           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5368           ValidateDiagnosticOptions) != Success)
5369     return true;
5370 
5371   return false;
5372 }
5373 
5374 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5375                                     const PCHContainerReader &PCHContainerRdr,
5376                                     const LangOptions &LangOpts,
5377                                     const TargetOptions &TargetOpts,
5378                                     const PreprocessorOptions &PPOpts,
5379                                     StringRef ExistingModuleCachePath) {
5380   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5381                                ExistingModuleCachePath, FileMgr);
5382   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5383                                   /*FindModuleFileExtensions=*/false,
5384                                   validator,
5385                                   /*ValidateDiagnosticOptions=*/true);
5386 }
5387 
5388 ASTReader::ASTReadResult
5389 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5390   // Enter the submodule block.
5391   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5392     Error(std::move(Err));
5393     return Failure;
5394   }
5395 
5396   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5397   bool First = true;
5398   Module *CurrentModule = nullptr;
5399   RecordData Record;
5400   while (true) {
5401     Expected<llvm::BitstreamEntry> MaybeEntry =
5402         F.Stream.advanceSkippingSubblocks();
5403     if (!MaybeEntry) {
5404       Error(MaybeEntry.takeError());
5405       return Failure;
5406     }
5407     llvm::BitstreamEntry Entry = MaybeEntry.get();
5408 
5409     switch (Entry.Kind) {
5410     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5411     case llvm::BitstreamEntry::Error:
5412       Error("malformed block record in AST file");
5413       return Failure;
5414     case llvm::BitstreamEntry::EndBlock:
5415       return Success;
5416     case llvm::BitstreamEntry::Record:
5417       // The interesting case.
5418       break;
5419     }
5420 
5421     // Read a record.
5422     StringRef Blob;
5423     Record.clear();
5424     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5425     if (!MaybeKind) {
5426       Error(MaybeKind.takeError());
5427       return Failure;
5428     }
5429     unsigned Kind = MaybeKind.get();
5430 
5431     if ((Kind == SUBMODULE_METADATA) != First) {
5432       Error("submodule metadata record should be at beginning of block");
5433       return Failure;
5434     }
5435     First = false;
5436 
5437     // Submodule information is only valid if we have a current module.
5438     // FIXME: Should we error on these cases?
5439     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5440         Kind != SUBMODULE_DEFINITION)
5441       continue;
5442 
5443     switch (Kind) {
5444     default:  // Default behavior: ignore.
5445       break;
5446 
5447     case SUBMODULE_DEFINITION: {
5448       if (Record.size() < 12) {
5449         Error("malformed module definition");
5450         return Failure;
5451       }
5452 
5453       StringRef Name = Blob;
5454       unsigned Idx = 0;
5455       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5456       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5457       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5458       bool IsFramework = Record[Idx++];
5459       bool IsExplicit = Record[Idx++];
5460       bool IsSystem = Record[Idx++];
5461       bool IsExternC = Record[Idx++];
5462       bool InferSubmodules = Record[Idx++];
5463       bool InferExplicitSubmodules = Record[Idx++];
5464       bool InferExportWildcard = Record[Idx++];
5465       bool ConfigMacrosExhaustive = Record[Idx++];
5466       bool ModuleMapIsPrivate = Record[Idx++];
5467 
5468       Module *ParentModule = nullptr;
5469       if (Parent)
5470         ParentModule = getSubmodule(Parent);
5471 
5472       // Retrieve this (sub)module from the module map, creating it if
5473       // necessary.
5474       CurrentModule =
5475           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5476               .first;
5477 
5478       // FIXME: set the definition loc for CurrentModule, or call
5479       // ModMap.setInferredModuleAllowedBy()
5480 
5481       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5482       if (GlobalIndex >= SubmodulesLoaded.size() ||
5483           SubmodulesLoaded[GlobalIndex]) {
5484         Error("too many submodules");
5485         return Failure;
5486       }
5487 
5488       if (!ParentModule) {
5489         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5490           // Don't emit module relocation error if we have -fno-validate-pch
5491           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5492               CurFile != F.File) {
5493             Error(diag::err_module_file_conflict,
5494                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5495                   F.File->getName());
5496             return Failure;
5497           }
5498         }
5499 
5500         F.DidReadTopLevelSubmodule = true;
5501         CurrentModule->setASTFile(F.File);
5502         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5503       }
5504 
5505       CurrentModule->Kind = Kind;
5506       CurrentModule->Signature = F.Signature;
5507       CurrentModule->IsFromModuleFile = true;
5508       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5509       CurrentModule->IsExternC = IsExternC;
5510       CurrentModule->InferSubmodules = InferSubmodules;
5511       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5512       CurrentModule->InferExportWildcard = InferExportWildcard;
5513       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5514       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5515       if (DeserializationListener)
5516         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5517 
5518       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5519 
5520       // Clear out data that will be replaced by what is in the module file.
5521       CurrentModule->LinkLibraries.clear();
5522       CurrentModule->ConfigMacros.clear();
5523       CurrentModule->UnresolvedConflicts.clear();
5524       CurrentModule->Conflicts.clear();
5525 
5526       // The module is available unless it's missing a requirement; relevant
5527       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5528       // Missing headers that were present when the module was built do not
5529       // make it unavailable -- if we got this far, this must be an explicitly
5530       // imported module file.
5531       CurrentModule->Requirements.clear();
5532       CurrentModule->MissingHeaders.clear();
5533       CurrentModule->IsMissingRequirement =
5534           ParentModule && ParentModule->IsMissingRequirement;
5535       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5536       break;
5537     }
5538 
5539     case SUBMODULE_UMBRELLA_HEADER: {
5540       std::string Filename = Blob;
5541       ResolveImportedPath(F, Filename);
5542       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5543         if (!CurrentModule->getUmbrellaHeader())
5544           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5545         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5546           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5547             Error("mismatched umbrella headers in submodule");
5548           return OutOfDate;
5549         }
5550       }
5551       break;
5552     }
5553 
5554     case SUBMODULE_HEADER:
5555     case SUBMODULE_EXCLUDED_HEADER:
5556     case SUBMODULE_PRIVATE_HEADER:
5557       // We lazily associate headers with their modules via the HeaderInfo table.
5558       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5559       // of complete filenames or remove it entirely.
5560       break;
5561 
5562     case SUBMODULE_TEXTUAL_HEADER:
5563     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5564       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5565       // them here.
5566       break;
5567 
5568     case SUBMODULE_TOPHEADER:
5569       CurrentModule->addTopHeaderFilename(Blob);
5570       break;
5571 
5572     case SUBMODULE_UMBRELLA_DIR: {
5573       std::string Dirname = Blob;
5574       ResolveImportedPath(F, Dirname);
5575       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5576         if (!CurrentModule->getUmbrellaDir())
5577           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5578         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5579           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5580             Error("mismatched umbrella directories in submodule");
5581           return OutOfDate;
5582         }
5583       }
5584       break;
5585     }
5586 
5587     case SUBMODULE_METADATA: {
5588       F.BaseSubmoduleID = getTotalNumSubmodules();
5589       F.LocalNumSubmodules = Record[0];
5590       unsigned LocalBaseSubmoduleID = Record[1];
5591       if (F.LocalNumSubmodules > 0) {
5592         // Introduce the global -> local mapping for submodules within this
5593         // module.
5594         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5595 
5596         // Introduce the local -> global mapping for submodules within this
5597         // module.
5598         F.SubmoduleRemap.insertOrReplace(
5599           std::make_pair(LocalBaseSubmoduleID,
5600                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5601 
5602         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5603       }
5604       break;
5605     }
5606 
5607     case SUBMODULE_IMPORTS:
5608       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5609         UnresolvedModuleRef Unresolved;
5610         Unresolved.File = &F;
5611         Unresolved.Mod = CurrentModule;
5612         Unresolved.ID = Record[Idx];
5613         Unresolved.Kind = UnresolvedModuleRef::Import;
5614         Unresolved.IsWildcard = false;
5615         UnresolvedModuleRefs.push_back(Unresolved);
5616       }
5617       break;
5618 
5619     case SUBMODULE_EXPORTS:
5620       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5621         UnresolvedModuleRef Unresolved;
5622         Unresolved.File = &F;
5623         Unresolved.Mod = CurrentModule;
5624         Unresolved.ID = Record[Idx];
5625         Unresolved.Kind = UnresolvedModuleRef::Export;
5626         Unresolved.IsWildcard = Record[Idx + 1];
5627         UnresolvedModuleRefs.push_back(Unresolved);
5628       }
5629 
5630       // Once we've loaded the set of exports, there's no reason to keep
5631       // the parsed, unresolved exports around.
5632       CurrentModule->UnresolvedExports.clear();
5633       break;
5634 
5635     case SUBMODULE_REQUIRES:
5636       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5637                                     PP.getTargetInfo());
5638       break;
5639 
5640     case SUBMODULE_LINK_LIBRARY:
5641       ModMap.resolveLinkAsDependencies(CurrentModule);
5642       CurrentModule->LinkLibraries.push_back(
5643                                          Module::LinkLibrary(Blob, Record[0]));
5644       break;
5645 
5646     case SUBMODULE_CONFIG_MACRO:
5647       CurrentModule->ConfigMacros.push_back(Blob.str());
5648       break;
5649 
5650     case SUBMODULE_CONFLICT: {
5651       UnresolvedModuleRef Unresolved;
5652       Unresolved.File = &F;
5653       Unresolved.Mod = CurrentModule;
5654       Unresolved.ID = Record[0];
5655       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5656       Unresolved.IsWildcard = false;
5657       Unresolved.String = Blob;
5658       UnresolvedModuleRefs.push_back(Unresolved);
5659       break;
5660     }
5661 
5662     case SUBMODULE_INITIALIZERS: {
5663       if (!ContextObj)
5664         break;
5665       SmallVector<uint32_t, 16> Inits;
5666       for (auto &ID : Record)
5667         Inits.push_back(getGlobalDeclID(F, ID));
5668       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5669       break;
5670     }
5671 
5672     case SUBMODULE_EXPORT_AS:
5673       CurrentModule->ExportAsModule = Blob.str();
5674       ModMap.addLinkAsDependency(CurrentModule);
5675       break;
5676     }
5677   }
5678 }
5679 
5680 /// Parse the record that corresponds to a LangOptions data
5681 /// structure.
5682 ///
5683 /// This routine parses the language options from the AST file and then gives
5684 /// them to the AST listener if one is set.
5685 ///
5686 /// \returns true if the listener deems the file unacceptable, false otherwise.
5687 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5688                                      bool Complain,
5689                                      ASTReaderListener &Listener,
5690                                      bool AllowCompatibleDifferences) {
5691   LangOptions LangOpts;
5692   unsigned Idx = 0;
5693 #define LANGOPT(Name, Bits, Default, Description) \
5694   LangOpts.Name = Record[Idx++];
5695 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5696   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5697 #include "clang/Basic/LangOptions.def"
5698 #define SANITIZER(NAME, ID)                                                    \
5699   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5700 #include "clang/Basic/Sanitizers.def"
5701 
5702   for (unsigned N = Record[Idx++]; N; --N)
5703     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5704 
5705   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5706   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5707   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5708 
5709   LangOpts.CurrentModule = ReadString(Record, Idx);
5710 
5711   // Comment options.
5712   for (unsigned N = Record[Idx++]; N; --N) {
5713     LangOpts.CommentOpts.BlockCommandNames.push_back(
5714       ReadString(Record, Idx));
5715   }
5716   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5717 
5718   // OpenMP offloading options.
5719   for (unsigned N = Record[Idx++]; N; --N) {
5720     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5721   }
5722 
5723   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5724 
5725   return Listener.ReadLanguageOptions(LangOpts, Complain,
5726                                       AllowCompatibleDifferences);
5727 }
5728 
5729 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5730                                    ASTReaderListener &Listener,
5731                                    bool AllowCompatibleDifferences) {
5732   unsigned Idx = 0;
5733   TargetOptions TargetOpts;
5734   TargetOpts.Triple = ReadString(Record, Idx);
5735   TargetOpts.CPU = ReadString(Record, Idx);
5736   TargetOpts.ABI = ReadString(Record, Idx);
5737   for (unsigned N = Record[Idx++]; N; --N) {
5738     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5739   }
5740   for (unsigned N = Record[Idx++]; N; --N) {
5741     TargetOpts.Features.push_back(ReadString(Record, Idx));
5742   }
5743 
5744   return Listener.ReadTargetOptions(TargetOpts, Complain,
5745                                     AllowCompatibleDifferences);
5746 }
5747 
5748 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5749                                        ASTReaderListener &Listener) {
5750   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5751   unsigned Idx = 0;
5752 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5753 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5754   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5755 #include "clang/Basic/DiagnosticOptions.def"
5756 
5757   for (unsigned N = Record[Idx++]; N; --N)
5758     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5759   for (unsigned N = Record[Idx++]; N; --N)
5760     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5761 
5762   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5763 }
5764 
5765 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5766                                        ASTReaderListener &Listener) {
5767   FileSystemOptions FSOpts;
5768   unsigned Idx = 0;
5769   FSOpts.WorkingDir = ReadString(Record, Idx);
5770   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5771 }
5772 
5773 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5774                                          bool Complain,
5775                                          ASTReaderListener &Listener) {
5776   HeaderSearchOptions HSOpts;
5777   unsigned Idx = 0;
5778   HSOpts.Sysroot = ReadString(Record, Idx);
5779 
5780   // Include entries.
5781   for (unsigned N = Record[Idx++]; N; --N) {
5782     std::string Path = ReadString(Record, Idx);
5783     frontend::IncludeDirGroup Group
5784       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5785     bool IsFramework = Record[Idx++];
5786     bool IgnoreSysRoot = Record[Idx++];
5787     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5788                                     IgnoreSysRoot);
5789   }
5790 
5791   // System header prefixes.
5792   for (unsigned N = Record[Idx++]; N; --N) {
5793     std::string Prefix = ReadString(Record, Idx);
5794     bool IsSystemHeader = Record[Idx++];
5795     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5796   }
5797 
5798   HSOpts.ResourceDir = ReadString(Record, Idx);
5799   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5800   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5801   HSOpts.DisableModuleHash = Record[Idx++];
5802   HSOpts.ImplicitModuleMaps = Record[Idx++];
5803   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5804   HSOpts.UseBuiltinIncludes = Record[Idx++];
5805   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5806   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5807   HSOpts.UseLibcxx = Record[Idx++];
5808   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5809 
5810   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5811                                           Complain);
5812 }
5813 
5814 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5815                                          bool Complain,
5816                                          ASTReaderListener &Listener,
5817                                          std::string &SuggestedPredefines) {
5818   PreprocessorOptions PPOpts;
5819   unsigned Idx = 0;
5820 
5821   // Macro definitions/undefs
5822   for (unsigned N = Record[Idx++]; N; --N) {
5823     std::string Macro = ReadString(Record, Idx);
5824     bool IsUndef = Record[Idx++];
5825     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5826   }
5827 
5828   // Includes
5829   for (unsigned N = Record[Idx++]; N; --N) {
5830     PPOpts.Includes.push_back(ReadString(Record, Idx));
5831   }
5832 
5833   // Macro Includes
5834   for (unsigned N = Record[Idx++]; N; --N) {
5835     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5836   }
5837 
5838   PPOpts.UsePredefines = Record[Idx++];
5839   PPOpts.DetailedRecord = Record[Idx++];
5840   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5841   PPOpts.ObjCXXARCStandardLibrary =
5842     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5843   SuggestedPredefines.clear();
5844   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5845                                           SuggestedPredefines);
5846 }
5847 
5848 std::pair<ModuleFile *, unsigned>
5849 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5850   GlobalPreprocessedEntityMapType::iterator
5851   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5852   assert(I != GlobalPreprocessedEntityMap.end() &&
5853          "Corrupted global preprocessed entity map");
5854   ModuleFile *M = I->second;
5855   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5856   return std::make_pair(M, LocalIndex);
5857 }
5858 
5859 llvm::iterator_range<PreprocessingRecord::iterator>
5860 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5861   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5862     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5863                                              Mod.NumPreprocessedEntities);
5864 
5865   return llvm::make_range(PreprocessingRecord::iterator(),
5866                           PreprocessingRecord::iterator());
5867 }
5868 
5869 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5870 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5871   return llvm::make_range(
5872       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5873       ModuleDeclIterator(this, &Mod,
5874                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5875 }
5876 
5877 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5878   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5879   assert(I != GlobalSkippedRangeMap.end() &&
5880     "Corrupted global skipped range map");
5881   ModuleFile *M = I->second;
5882   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5883   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5884   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5885   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5886                     TranslateSourceLocation(*M, RawRange.getEnd()));
5887   assert(Range.isValid());
5888   return Range;
5889 }
5890 
5891 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5892   PreprocessedEntityID PPID = Index+1;
5893   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5894   ModuleFile &M = *PPInfo.first;
5895   unsigned LocalIndex = PPInfo.second;
5896   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5897 
5898   if (!PP.getPreprocessingRecord()) {
5899     Error("no preprocessing record");
5900     return nullptr;
5901   }
5902 
5903   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5904   if (llvm::Error Err =
5905           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5906     Error(std::move(Err));
5907     return nullptr;
5908   }
5909 
5910   Expected<llvm::BitstreamEntry> MaybeEntry =
5911       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5912   if (!MaybeEntry) {
5913     Error(MaybeEntry.takeError());
5914     return nullptr;
5915   }
5916   llvm::BitstreamEntry Entry = MaybeEntry.get();
5917 
5918   if (Entry.Kind != llvm::BitstreamEntry::Record)
5919     return nullptr;
5920 
5921   // Read the record.
5922   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5923                     TranslateSourceLocation(M, PPOffs.getEnd()));
5924   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5925   StringRef Blob;
5926   RecordData Record;
5927   Expected<unsigned> MaybeRecType =
5928       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5929   if (!MaybeRecType) {
5930     Error(MaybeRecType.takeError());
5931     return nullptr;
5932   }
5933   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5934   case PPD_MACRO_EXPANSION: {
5935     bool isBuiltin = Record[0];
5936     IdentifierInfo *Name = nullptr;
5937     MacroDefinitionRecord *Def = nullptr;
5938     if (isBuiltin)
5939       Name = getLocalIdentifier(M, Record[1]);
5940     else {
5941       PreprocessedEntityID GlobalID =
5942           getGlobalPreprocessedEntityID(M, Record[1]);
5943       Def = cast<MacroDefinitionRecord>(
5944           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5945     }
5946 
5947     MacroExpansion *ME;
5948     if (isBuiltin)
5949       ME = new (PPRec) MacroExpansion(Name, Range);
5950     else
5951       ME = new (PPRec) MacroExpansion(Def, Range);
5952 
5953     return ME;
5954   }
5955 
5956   case PPD_MACRO_DEFINITION: {
5957     // Decode the identifier info and then check again; if the macro is
5958     // still defined and associated with the identifier,
5959     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5960     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5961 
5962     if (DeserializationListener)
5963       DeserializationListener->MacroDefinitionRead(PPID, MD);
5964 
5965     return MD;
5966   }
5967 
5968   case PPD_INCLUSION_DIRECTIVE: {
5969     const char *FullFileNameStart = Blob.data() + Record[0];
5970     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5971     const FileEntry *File = nullptr;
5972     if (!FullFileName.empty())
5973       if (auto FE = PP.getFileManager().getFile(FullFileName))
5974         File = *FE;
5975 
5976     // FIXME: Stable encoding
5977     InclusionDirective::InclusionKind Kind
5978       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5979     InclusionDirective *ID
5980       = new (PPRec) InclusionDirective(PPRec, Kind,
5981                                        StringRef(Blob.data(), Record[0]),
5982                                        Record[1], Record[3],
5983                                        File,
5984                                        Range);
5985     return ID;
5986   }
5987   }
5988 
5989   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5990 }
5991 
5992 /// Find the next module that contains entities and return the ID
5993 /// of the first entry.
5994 ///
5995 /// \param SLocMapI points at a chunk of a module that contains no
5996 /// preprocessed entities or the entities it contains are not the ones we are
5997 /// looking for.
5998 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5999                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6000   ++SLocMapI;
6001   for (GlobalSLocOffsetMapType::const_iterator
6002          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6003     ModuleFile &M = *SLocMapI->second;
6004     if (M.NumPreprocessedEntities)
6005       return M.BasePreprocessedEntityID;
6006   }
6007 
6008   return getTotalNumPreprocessedEntities();
6009 }
6010 
6011 namespace {
6012 
6013 struct PPEntityComp {
6014   const ASTReader &Reader;
6015   ModuleFile &M;
6016 
6017   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6018 
6019   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6020     SourceLocation LHS = getLoc(L);
6021     SourceLocation RHS = getLoc(R);
6022     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6023   }
6024 
6025   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6026     SourceLocation LHS = getLoc(L);
6027     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6028   }
6029 
6030   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6031     SourceLocation RHS = getLoc(R);
6032     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6033   }
6034 
6035   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6036     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6037   }
6038 };
6039 
6040 } // namespace
6041 
6042 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6043                                                        bool EndsAfter) const {
6044   if (SourceMgr.isLocalSourceLocation(Loc))
6045     return getTotalNumPreprocessedEntities();
6046 
6047   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6048       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6049   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6050          "Corrupted global sloc offset map");
6051 
6052   if (SLocMapI->second->NumPreprocessedEntities == 0)
6053     return findNextPreprocessedEntity(SLocMapI);
6054 
6055   ModuleFile &M = *SLocMapI->second;
6056 
6057   using pp_iterator = const PPEntityOffset *;
6058 
6059   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6060   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6061 
6062   size_t Count = M.NumPreprocessedEntities;
6063   size_t Half;
6064   pp_iterator First = pp_begin;
6065   pp_iterator PPI;
6066 
6067   if (EndsAfter) {
6068     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6069                            PPEntityComp(*this, M));
6070   } else {
6071     // Do a binary search manually instead of using std::lower_bound because
6072     // The end locations of entities may be unordered (when a macro expansion
6073     // is inside another macro argument), but for this case it is not important
6074     // whether we get the first macro expansion or its containing macro.
6075     while (Count > 0) {
6076       Half = Count / 2;
6077       PPI = First;
6078       std::advance(PPI, Half);
6079       if (SourceMgr.isBeforeInTranslationUnit(
6080               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6081         First = PPI;
6082         ++First;
6083         Count = Count - Half - 1;
6084       } else
6085         Count = Half;
6086     }
6087   }
6088 
6089   if (PPI == pp_end)
6090     return findNextPreprocessedEntity(SLocMapI);
6091 
6092   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6093 }
6094 
6095 /// Returns a pair of [Begin, End) indices of preallocated
6096 /// preprocessed entities that \arg Range encompasses.
6097 std::pair<unsigned, unsigned>
6098     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6099   if (Range.isInvalid())
6100     return std::make_pair(0,0);
6101   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6102 
6103   PreprocessedEntityID BeginID =
6104       findPreprocessedEntity(Range.getBegin(), false);
6105   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6106   return std::make_pair(BeginID, EndID);
6107 }
6108 
6109 /// Optionally returns true or false if the preallocated preprocessed
6110 /// entity with index \arg Index came from file \arg FID.
6111 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6112                                                              FileID FID) {
6113   if (FID.isInvalid())
6114     return false;
6115 
6116   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6117   ModuleFile &M = *PPInfo.first;
6118   unsigned LocalIndex = PPInfo.second;
6119   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6120 
6121   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6122   if (Loc.isInvalid())
6123     return false;
6124 
6125   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6126     return true;
6127   else
6128     return false;
6129 }
6130 
6131 namespace {
6132 
6133   /// Visitor used to search for information about a header file.
6134   class HeaderFileInfoVisitor {
6135     const FileEntry *FE;
6136     Optional<HeaderFileInfo> HFI;
6137 
6138   public:
6139     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6140 
6141     bool operator()(ModuleFile &M) {
6142       HeaderFileInfoLookupTable *Table
6143         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6144       if (!Table)
6145         return false;
6146 
6147       // Look in the on-disk hash table for an entry for this file name.
6148       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6149       if (Pos == Table->end())
6150         return false;
6151 
6152       HFI = *Pos;
6153       return true;
6154     }
6155 
6156     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6157   };
6158 
6159 } // namespace
6160 
6161 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6162   HeaderFileInfoVisitor Visitor(FE);
6163   ModuleMgr.visit(Visitor);
6164   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6165     return *HFI;
6166 
6167   return HeaderFileInfo();
6168 }
6169 
6170 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6171   using DiagState = DiagnosticsEngine::DiagState;
6172   SmallVector<DiagState *, 32> DiagStates;
6173 
6174   for (ModuleFile &F : ModuleMgr) {
6175     unsigned Idx = 0;
6176     auto &Record = F.PragmaDiagMappings;
6177     if (Record.empty())
6178       continue;
6179 
6180     DiagStates.clear();
6181 
6182     auto ReadDiagState =
6183         [&](const DiagState &BasedOn, SourceLocation Loc,
6184             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6185       unsigned BackrefID = Record[Idx++];
6186       if (BackrefID != 0)
6187         return DiagStates[BackrefID - 1];
6188 
6189       // A new DiagState was created here.
6190       Diag.DiagStates.push_back(BasedOn);
6191       DiagState *NewState = &Diag.DiagStates.back();
6192       DiagStates.push_back(NewState);
6193       unsigned Size = Record[Idx++];
6194       assert(Idx + Size * 2 <= Record.size() &&
6195              "Invalid data, not enough diag/map pairs");
6196       while (Size--) {
6197         unsigned DiagID = Record[Idx++];
6198         DiagnosticMapping NewMapping =
6199             DiagnosticMapping::deserialize(Record[Idx++]);
6200         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6201           continue;
6202 
6203         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6204 
6205         // If this mapping was specified as a warning but the severity was
6206         // upgraded due to diagnostic settings, simulate the current diagnostic
6207         // settings (and use a warning).
6208         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6209           NewMapping.setSeverity(diag::Severity::Warning);
6210           NewMapping.setUpgradedFromWarning(false);
6211         }
6212 
6213         Mapping = NewMapping;
6214       }
6215       return NewState;
6216     };
6217 
6218     // Read the first state.
6219     DiagState *FirstState;
6220     if (F.Kind == MK_ImplicitModule) {
6221       // Implicitly-built modules are reused with different diagnostic
6222       // settings.  Use the initial diagnostic state from Diag to simulate this
6223       // compilation's diagnostic settings.
6224       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6225       DiagStates.push_back(FirstState);
6226 
6227       // Skip the initial diagnostic state from the serialized module.
6228       assert(Record[1] == 0 &&
6229              "Invalid data, unexpected backref in initial state");
6230       Idx = 3 + Record[2] * 2;
6231       assert(Idx < Record.size() &&
6232              "Invalid data, not enough state change pairs in initial state");
6233     } else if (F.isModule()) {
6234       // For an explicit module, preserve the flags from the module build
6235       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6236       // -Wblah flags.
6237       unsigned Flags = Record[Idx++];
6238       DiagState Initial;
6239       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6240       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6241       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6242       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6243       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6244       Initial.ExtBehavior = (diag::Severity)Flags;
6245       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6246 
6247       assert(F.OriginalSourceFileID.isValid());
6248 
6249       // Set up the root buffer of the module to start with the initial
6250       // diagnostic state of the module itself, to cover files that contain no
6251       // explicit transitions (for which we did not serialize anything).
6252       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6253           .StateTransitions.push_back({FirstState, 0});
6254     } else {
6255       // For prefix ASTs, start with whatever the user configured on the
6256       // command line.
6257       Idx++; // Skip flags.
6258       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6259                                  SourceLocation(), false);
6260     }
6261 
6262     // Read the state transitions.
6263     unsigned NumLocations = Record[Idx++];
6264     while (NumLocations--) {
6265       assert(Idx < Record.size() &&
6266              "Invalid data, missing pragma diagnostic states");
6267       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6268       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6269       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6270       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6271       unsigned Transitions = Record[Idx++];
6272 
6273       // Note that we don't need to set up Parent/ParentOffset here, because
6274       // we won't be changing the diagnostic state within imported FileIDs
6275       // (other than perhaps appending to the main source file, which has no
6276       // parent).
6277       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6278       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6279       for (unsigned I = 0; I != Transitions; ++I) {
6280         unsigned Offset = Record[Idx++];
6281         auto *State =
6282             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6283         F.StateTransitions.push_back({State, Offset});
6284       }
6285     }
6286 
6287     // Read the final state.
6288     assert(Idx < Record.size() &&
6289            "Invalid data, missing final pragma diagnostic state");
6290     SourceLocation CurStateLoc =
6291         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6292     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6293 
6294     if (!F.isModule()) {
6295       Diag.DiagStatesByLoc.CurDiagState = CurState;
6296       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6297 
6298       // Preserve the property that the imaginary root file describes the
6299       // current state.
6300       FileID NullFile;
6301       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6302       if (T.empty())
6303         T.push_back({CurState, 0});
6304       else
6305         T[0].State = CurState;
6306     }
6307 
6308     // Don't try to read these mappings again.
6309     Record.clear();
6310   }
6311 }
6312 
6313 /// Get the correct cursor and offset for loading a type.
6314 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6315   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6316   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6317   ModuleFile *M = I->second;
6318   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6319 }
6320 
6321 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6322   switch (code) {
6323 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6324   case TYPE_##CODE_ID: return Type::CLASS_ID;
6325 #include "clang/Serialization/TypeBitCodes.def"
6326   default: return llvm::None;
6327   }
6328 }
6329 
6330 /// Read and return the type with the given index..
6331 ///
6332 /// The index is the type ID, shifted and minus the number of predefs. This
6333 /// routine actually reads the record corresponding to the type at the given
6334 /// location. It is a helper routine for GetType, which deals with reading type
6335 /// IDs.
6336 QualType ASTReader::readTypeRecord(unsigned Index) {
6337   assert(ContextObj && "reading type with no AST context");
6338   ASTContext &Context = *ContextObj;
6339   RecordLocation Loc = TypeCursorForIndex(Index);
6340   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6341 
6342   // Keep track of where we are in the stream, then jump back there
6343   // after reading this type.
6344   SavedStreamPosition SavedPosition(DeclsCursor);
6345 
6346   ReadingKindTracker ReadingKind(Read_Type, *this);
6347 
6348   // Note that we are loading a type record.
6349   Deserializing AType(this);
6350 
6351   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6352     Error(std::move(Err));
6353     return QualType();
6354   }
6355   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6356   if (!RawCode) {
6357     Error(RawCode.takeError());
6358     return QualType();
6359   }
6360 
6361   ASTRecordReader Record(*this, *Loc.F);
6362   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6363   if (!Code) {
6364     Error(Code.takeError());
6365     return QualType();
6366   }
6367   if (Code.get() == TYPE_EXT_QUAL) {
6368     QualType baseType = Record.readQualType();
6369     Qualifiers quals = Record.readQualifiers();
6370     return Context.getQualifiedType(baseType, quals);
6371   }
6372 
6373   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6374   if (!maybeClass) {
6375     Error("Unexpected code for type");
6376     return QualType();
6377   }
6378 
6379   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6380   return TypeReader.read(*maybeClass);
6381 }
6382 
6383 namespace clang {
6384 
6385 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6386   ASTRecordReader &Reader;
6387 
6388   SourceLocation readSourceLocation() {
6389     return Reader.readSourceLocation();
6390   }
6391 
6392   TypeSourceInfo *GetTypeSourceInfo() {
6393     return Reader.readTypeSourceInfo();
6394   }
6395 
6396   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6397     return Reader.readNestedNameSpecifierLoc();
6398   }
6399 
6400   Attr *ReadAttr() {
6401     return Reader.readAttr();
6402   }
6403 
6404 public:
6405   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6406 
6407   // We want compile-time assurance that we've enumerated all of
6408   // these, so unfortunately we have to declare them first, then
6409   // define them out-of-line.
6410 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6411 #define TYPELOC(CLASS, PARENT) \
6412   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6413 #include "clang/AST/TypeLocNodes.def"
6414 
6415   void VisitFunctionTypeLoc(FunctionTypeLoc);
6416   void VisitArrayTypeLoc(ArrayTypeLoc);
6417 };
6418 
6419 } // namespace clang
6420 
6421 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6422   // nothing to do
6423 }
6424 
6425 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6426   TL.setBuiltinLoc(readSourceLocation());
6427   if (TL.needsExtraLocalData()) {
6428     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6429     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6430     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6431     TL.setModeAttr(Reader.readInt());
6432   }
6433 }
6434 
6435 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6436   TL.setNameLoc(readSourceLocation());
6437 }
6438 
6439 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6440   TL.setStarLoc(readSourceLocation());
6441 }
6442 
6443 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6444   // nothing to do
6445 }
6446 
6447 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6448   // nothing to do
6449 }
6450 
6451 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6452   TL.setExpansionLoc(readSourceLocation());
6453 }
6454 
6455 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6456   TL.setCaretLoc(readSourceLocation());
6457 }
6458 
6459 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6460   TL.setAmpLoc(readSourceLocation());
6461 }
6462 
6463 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6464   TL.setAmpAmpLoc(readSourceLocation());
6465 }
6466 
6467 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6468   TL.setStarLoc(readSourceLocation());
6469   TL.setClassTInfo(GetTypeSourceInfo());
6470 }
6471 
6472 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6473   TL.setLBracketLoc(readSourceLocation());
6474   TL.setRBracketLoc(readSourceLocation());
6475   if (Reader.readBool())
6476     TL.setSizeExpr(Reader.readExpr());
6477   else
6478     TL.setSizeExpr(nullptr);
6479 }
6480 
6481 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6482   VisitArrayTypeLoc(TL);
6483 }
6484 
6485 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6486   VisitArrayTypeLoc(TL);
6487 }
6488 
6489 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6490   VisitArrayTypeLoc(TL);
6491 }
6492 
6493 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6494                                             DependentSizedArrayTypeLoc TL) {
6495   VisitArrayTypeLoc(TL);
6496 }
6497 
6498 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6499     DependentAddressSpaceTypeLoc TL) {
6500 
6501     TL.setAttrNameLoc(readSourceLocation());
6502     TL.setAttrOperandParensRange(Reader.readSourceRange());
6503     TL.setAttrExprOperand(Reader.readExpr());
6504 }
6505 
6506 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6507                                         DependentSizedExtVectorTypeLoc TL) {
6508   TL.setNameLoc(readSourceLocation());
6509 }
6510 
6511 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6512   TL.setNameLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitDependentVectorTypeLoc(
6516     DependentVectorTypeLoc TL) {
6517   TL.setNameLoc(readSourceLocation());
6518 }
6519 
6520 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6521   TL.setNameLoc(readSourceLocation());
6522 }
6523 
6524 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6525   TL.setLocalRangeBegin(readSourceLocation());
6526   TL.setLParenLoc(readSourceLocation());
6527   TL.setRParenLoc(readSourceLocation());
6528   TL.setExceptionSpecRange(Reader.readSourceRange());
6529   TL.setLocalRangeEnd(readSourceLocation());
6530   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6531     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6532   }
6533 }
6534 
6535 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6536   VisitFunctionTypeLoc(TL);
6537 }
6538 
6539 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6540   VisitFunctionTypeLoc(TL);
6541 }
6542 
6543 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6544   TL.setNameLoc(readSourceLocation());
6545 }
6546 
6547 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6548   TL.setNameLoc(readSourceLocation());
6549 }
6550 
6551 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6552   TL.setTypeofLoc(readSourceLocation());
6553   TL.setLParenLoc(readSourceLocation());
6554   TL.setRParenLoc(readSourceLocation());
6555 }
6556 
6557 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6558   TL.setTypeofLoc(readSourceLocation());
6559   TL.setLParenLoc(readSourceLocation());
6560   TL.setRParenLoc(readSourceLocation());
6561   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6562 }
6563 
6564 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6569   TL.setKWLoc(readSourceLocation());
6570   TL.setLParenLoc(readSourceLocation());
6571   TL.setRParenLoc(readSourceLocation());
6572   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6573 }
6574 
6575 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6576   TL.setNameLoc(readSourceLocation());
6577 }
6578 
6579 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6580     DeducedTemplateSpecializationTypeLoc TL) {
6581   TL.setTemplateNameLoc(readSourceLocation());
6582 }
6583 
6584 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6585   TL.setNameLoc(readSourceLocation());
6586 }
6587 
6588 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6589   TL.setNameLoc(readSourceLocation());
6590 }
6591 
6592 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6593   TL.setAttr(ReadAttr());
6594 }
6595 
6596 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6597   TL.setNameLoc(readSourceLocation());
6598 }
6599 
6600 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6601                                             SubstTemplateTypeParmTypeLoc TL) {
6602   TL.setNameLoc(readSourceLocation());
6603 }
6604 
6605 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6606                                           SubstTemplateTypeParmPackTypeLoc TL) {
6607   TL.setNameLoc(readSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6611                                            TemplateSpecializationTypeLoc TL) {
6612   TL.setTemplateKeywordLoc(readSourceLocation());
6613   TL.setTemplateNameLoc(readSourceLocation());
6614   TL.setLAngleLoc(readSourceLocation());
6615   TL.setRAngleLoc(readSourceLocation());
6616   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6617     TL.setArgLocInfo(
6618         i,
6619         Reader.readTemplateArgumentLocInfo(
6620           TL.getTypePtr()->getArg(i).getKind()));
6621 }
6622 
6623 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6624   TL.setLParenLoc(readSourceLocation());
6625   TL.setRParenLoc(readSourceLocation());
6626 }
6627 
6628 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6629   TL.setElaboratedKeywordLoc(readSourceLocation());
6630   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6631 }
6632 
6633 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6634   TL.setNameLoc(readSourceLocation());
6635 }
6636 
6637 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6638   TL.setElaboratedKeywordLoc(readSourceLocation());
6639   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6640   TL.setNameLoc(readSourceLocation());
6641 }
6642 
6643 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6644        DependentTemplateSpecializationTypeLoc TL) {
6645   TL.setElaboratedKeywordLoc(readSourceLocation());
6646   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6647   TL.setTemplateKeywordLoc(readSourceLocation());
6648   TL.setTemplateNameLoc(readSourceLocation());
6649   TL.setLAngleLoc(readSourceLocation());
6650   TL.setRAngleLoc(readSourceLocation());
6651   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6652     TL.setArgLocInfo(
6653         I,
6654         Reader.readTemplateArgumentLocInfo(
6655             TL.getTypePtr()->getArg(I).getKind()));
6656 }
6657 
6658 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6659   TL.setEllipsisLoc(readSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6663   TL.setNameLoc(readSourceLocation());
6664 }
6665 
6666 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6667   if (TL.getNumProtocols()) {
6668     TL.setProtocolLAngleLoc(readSourceLocation());
6669     TL.setProtocolRAngleLoc(readSourceLocation());
6670   }
6671   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6672     TL.setProtocolLoc(i, readSourceLocation());
6673 }
6674 
6675 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6676   TL.setHasBaseTypeAsWritten(Reader.readBool());
6677   TL.setTypeArgsLAngleLoc(readSourceLocation());
6678   TL.setTypeArgsRAngleLoc(readSourceLocation());
6679   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6680     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6681   TL.setProtocolLAngleLoc(readSourceLocation());
6682   TL.setProtocolRAngleLoc(readSourceLocation());
6683   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6684     TL.setProtocolLoc(i, readSourceLocation());
6685 }
6686 
6687 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6688   TL.setStarLoc(readSourceLocation());
6689 }
6690 
6691 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6692   TL.setKWLoc(readSourceLocation());
6693   TL.setLParenLoc(readSourceLocation());
6694   TL.setRParenLoc(readSourceLocation());
6695 }
6696 
6697 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6698   TL.setKWLoc(readSourceLocation());
6699 }
6700 
6701 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6702   TypeLocReader TLR(*this);
6703   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6704     TLR.Visit(TL);
6705 }
6706 
6707 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6708   QualType InfoTy = readType();
6709   if (InfoTy.isNull())
6710     return nullptr;
6711 
6712   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6713   readTypeLoc(TInfo->getTypeLoc());
6714   return TInfo;
6715 }
6716 
6717 QualType ASTReader::GetType(TypeID ID) {
6718   assert(ContextObj && "reading type with no AST context");
6719   ASTContext &Context = *ContextObj;
6720 
6721   unsigned FastQuals = ID & Qualifiers::FastMask;
6722   unsigned Index = ID >> Qualifiers::FastWidth;
6723 
6724   if (Index < NUM_PREDEF_TYPE_IDS) {
6725     QualType T;
6726     switch ((PredefinedTypeIDs)Index) {
6727     case PREDEF_TYPE_NULL_ID:
6728       return QualType();
6729     case PREDEF_TYPE_VOID_ID:
6730       T = Context.VoidTy;
6731       break;
6732     case PREDEF_TYPE_BOOL_ID:
6733       T = Context.BoolTy;
6734       break;
6735     case PREDEF_TYPE_CHAR_U_ID:
6736     case PREDEF_TYPE_CHAR_S_ID:
6737       // FIXME: Check that the signedness of CharTy is correct!
6738       T = Context.CharTy;
6739       break;
6740     case PREDEF_TYPE_UCHAR_ID:
6741       T = Context.UnsignedCharTy;
6742       break;
6743     case PREDEF_TYPE_USHORT_ID:
6744       T = Context.UnsignedShortTy;
6745       break;
6746     case PREDEF_TYPE_UINT_ID:
6747       T = Context.UnsignedIntTy;
6748       break;
6749     case PREDEF_TYPE_ULONG_ID:
6750       T = Context.UnsignedLongTy;
6751       break;
6752     case PREDEF_TYPE_ULONGLONG_ID:
6753       T = Context.UnsignedLongLongTy;
6754       break;
6755     case PREDEF_TYPE_UINT128_ID:
6756       T = Context.UnsignedInt128Ty;
6757       break;
6758     case PREDEF_TYPE_SCHAR_ID:
6759       T = Context.SignedCharTy;
6760       break;
6761     case PREDEF_TYPE_WCHAR_ID:
6762       T = Context.WCharTy;
6763       break;
6764     case PREDEF_TYPE_SHORT_ID:
6765       T = Context.ShortTy;
6766       break;
6767     case PREDEF_TYPE_INT_ID:
6768       T = Context.IntTy;
6769       break;
6770     case PREDEF_TYPE_LONG_ID:
6771       T = Context.LongTy;
6772       break;
6773     case PREDEF_TYPE_LONGLONG_ID:
6774       T = Context.LongLongTy;
6775       break;
6776     case PREDEF_TYPE_INT128_ID:
6777       T = Context.Int128Ty;
6778       break;
6779     case PREDEF_TYPE_HALF_ID:
6780       T = Context.HalfTy;
6781       break;
6782     case PREDEF_TYPE_FLOAT_ID:
6783       T = Context.FloatTy;
6784       break;
6785     case PREDEF_TYPE_DOUBLE_ID:
6786       T = Context.DoubleTy;
6787       break;
6788     case PREDEF_TYPE_LONGDOUBLE_ID:
6789       T = Context.LongDoubleTy;
6790       break;
6791     case PREDEF_TYPE_SHORT_ACCUM_ID:
6792       T = Context.ShortAccumTy;
6793       break;
6794     case PREDEF_TYPE_ACCUM_ID:
6795       T = Context.AccumTy;
6796       break;
6797     case PREDEF_TYPE_LONG_ACCUM_ID:
6798       T = Context.LongAccumTy;
6799       break;
6800     case PREDEF_TYPE_USHORT_ACCUM_ID:
6801       T = Context.UnsignedShortAccumTy;
6802       break;
6803     case PREDEF_TYPE_UACCUM_ID:
6804       T = Context.UnsignedAccumTy;
6805       break;
6806     case PREDEF_TYPE_ULONG_ACCUM_ID:
6807       T = Context.UnsignedLongAccumTy;
6808       break;
6809     case PREDEF_TYPE_SHORT_FRACT_ID:
6810       T = Context.ShortFractTy;
6811       break;
6812     case PREDEF_TYPE_FRACT_ID:
6813       T = Context.FractTy;
6814       break;
6815     case PREDEF_TYPE_LONG_FRACT_ID:
6816       T = Context.LongFractTy;
6817       break;
6818     case PREDEF_TYPE_USHORT_FRACT_ID:
6819       T = Context.UnsignedShortFractTy;
6820       break;
6821     case PREDEF_TYPE_UFRACT_ID:
6822       T = Context.UnsignedFractTy;
6823       break;
6824     case PREDEF_TYPE_ULONG_FRACT_ID:
6825       T = Context.UnsignedLongFractTy;
6826       break;
6827     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6828       T = Context.SatShortAccumTy;
6829       break;
6830     case PREDEF_TYPE_SAT_ACCUM_ID:
6831       T = Context.SatAccumTy;
6832       break;
6833     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6834       T = Context.SatLongAccumTy;
6835       break;
6836     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6837       T = Context.SatUnsignedShortAccumTy;
6838       break;
6839     case PREDEF_TYPE_SAT_UACCUM_ID:
6840       T = Context.SatUnsignedAccumTy;
6841       break;
6842     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6843       T = Context.SatUnsignedLongAccumTy;
6844       break;
6845     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6846       T = Context.SatShortFractTy;
6847       break;
6848     case PREDEF_TYPE_SAT_FRACT_ID:
6849       T = Context.SatFractTy;
6850       break;
6851     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6852       T = Context.SatLongFractTy;
6853       break;
6854     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6855       T = Context.SatUnsignedShortFractTy;
6856       break;
6857     case PREDEF_TYPE_SAT_UFRACT_ID:
6858       T = Context.SatUnsignedFractTy;
6859       break;
6860     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6861       T = Context.SatUnsignedLongFractTy;
6862       break;
6863     case PREDEF_TYPE_FLOAT16_ID:
6864       T = Context.Float16Ty;
6865       break;
6866     case PREDEF_TYPE_FLOAT128_ID:
6867       T = Context.Float128Ty;
6868       break;
6869     case PREDEF_TYPE_OVERLOAD_ID:
6870       T = Context.OverloadTy;
6871       break;
6872     case PREDEF_TYPE_BOUND_MEMBER:
6873       T = Context.BoundMemberTy;
6874       break;
6875     case PREDEF_TYPE_PSEUDO_OBJECT:
6876       T = Context.PseudoObjectTy;
6877       break;
6878     case PREDEF_TYPE_DEPENDENT_ID:
6879       T = Context.DependentTy;
6880       break;
6881     case PREDEF_TYPE_UNKNOWN_ANY:
6882       T = Context.UnknownAnyTy;
6883       break;
6884     case PREDEF_TYPE_NULLPTR_ID:
6885       T = Context.NullPtrTy;
6886       break;
6887     case PREDEF_TYPE_CHAR8_ID:
6888       T = Context.Char8Ty;
6889       break;
6890     case PREDEF_TYPE_CHAR16_ID:
6891       T = Context.Char16Ty;
6892       break;
6893     case PREDEF_TYPE_CHAR32_ID:
6894       T = Context.Char32Ty;
6895       break;
6896     case PREDEF_TYPE_OBJC_ID:
6897       T = Context.ObjCBuiltinIdTy;
6898       break;
6899     case PREDEF_TYPE_OBJC_CLASS:
6900       T = Context.ObjCBuiltinClassTy;
6901       break;
6902     case PREDEF_TYPE_OBJC_SEL:
6903       T = Context.ObjCBuiltinSelTy;
6904       break;
6905 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6906     case PREDEF_TYPE_##Id##_ID: \
6907       T = Context.SingletonId; \
6908       break;
6909 #include "clang/Basic/OpenCLImageTypes.def"
6910 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6911     case PREDEF_TYPE_##Id##_ID: \
6912       T = Context.Id##Ty; \
6913       break;
6914 #include "clang/Basic/OpenCLExtensionTypes.def"
6915     case PREDEF_TYPE_SAMPLER_ID:
6916       T = Context.OCLSamplerTy;
6917       break;
6918     case PREDEF_TYPE_EVENT_ID:
6919       T = Context.OCLEventTy;
6920       break;
6921     case PREDEF_TYPE_CLK_EVENT_ID:
6922       T = Context.OCLClkEventTy;
6923       break;
6924     case PREDEF_TYPE_QUEUE_ID:
6925       T = Context.OCLQueueTy;
6926       break;
6927     case PREDEF_TYPE_RESERVE_ID_ID:
6928       T = Context.OCLReserveIDTy;
6929       break;
6930     case PREDEF_TYPE_AUTO_DEDUCT:
6931       T = Context.getAutoDeductType();
6932       break;
6933     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6934       T = Context.getAutoRRefDeductType();
6935       break;
6936     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6937       T = Context.ARCUnbridgedCastTy;
6938       break;
6939     case PREDEF_TYPE_BUILTIN_FN:
6940       T = Context.BuiltinFnTy;
6941       break;
6942     case PREDEF_TYPE_OMP_ARRAY_SECTION:
6943       T = Context.OMPArraySectionTy;
6944       break;
6945 #define SVE_TYPE(Name, Id, SingletonId) \
6946     case PREDEF_TYPE_##Id##_ID: \
6947       T = Context.SingletonId; \
6948       break;
6949 #include "clang/Basic/AArch64SVEACLETypes.def"
6950     }
6951 
6952     assert(!T.isNull() && "Unknown predefined type");
6953     return T.withFastQualifiers(FastQuals);
6954   }
6955 
6956   Index -= NUM_PREDEF_TYPE_IDS;
6957   assert(Index < TypesLoaded.size() && "Type index out-of-range");
6958   if (TypesLoaded[Index].isNull()) {
6959     TypesLoaded[Index] = readTypeRecord(Index);
6960     if (TypesLoaded[Index].isNull())
6961       return QualType();
6962 
6963     TypesLoaded[Index]->setFromAST();
6964     if (DeserializationListener)
6965       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6966                                         TypesLoaded[Index]);
6967   }
6968 
6969   return TypesLoaded[Index].withFastQualifiers(FastQuals);
6970 }
6971 
6972 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6973   return GetType(getGlobalTypeID(F, LocalID));
6974 }
6975 
6976 serialization::TypeID
6977 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6978   unsigned FastQuals = LocalID & Qualifiers::FastMask;
6979   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6980 
6981   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6982     return LocalID;
6983 
6984   if (!F.ModuleOffsetMap.empty())
6985     ReadModuleOffsetMap(F);
6986 
6987   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6988     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6989   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6990 
6991   unsigned GlobalIndex = LocalIndex + I->second;
6992   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6993 }
6994 
6995 TemplateArgumentLocInfo
6996 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
6997   switch (Kind) {
6998   case TemplateArgument::Expression:
6999     return readExpr();
7000   case TemplateArgument::Type:
7001     return readTypeSourceInfo();
7002   case TemplateArgument::Template: {
7003     NestedNameSpecifierLoc QualifierLoc =
7004       readNestedNameSpecifierLoc();
7005     SourceLocation TemplateNameLoc = readSourceLocation();
7006     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7007                                    SourceLocation());
7008   }
7009   case TemplateArgument::TemplateExpansion: {
7010     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7011     SourceLocation TemplateNameLoc = readSourceLocation();
7012     SourceLocation EllipsisLoc = readSourceLocation();
7013     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7014                                    EllipsisLoc);
7015   }
7016   case TemplateArgument::Null:
7017   case TemplateArgument::Integral:
7018   case TemplateArgument::Declaration:
7019   case TemplateArgument::NullPtr:
7020   case TemplateArgument::Pack:
7021     // FIXME: Is this right?
7022     return TemplateArgumentLocInfo();
7023   }
7024   llvm_unreachable("unexpected template argument loc");
7025 }
7026 
7027 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7028   TemplateArgument Arg = readTemplateArgument();
7029 
7030   if (Arg.getKind() == TemplateArgument::Expression) {
7031     if (readBool()) // bool InfoHasSameExpr.
7032       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7033   }
7034   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7035 }
7036 
7037 const ASTTemplateArgumentListInfo *
7038 ASTRecordReader::readASTTemplateArgumentListInfo() {
7039   SourceLocation LAngleLoc = readSourceLocation();
7040   SourceLocation RAngleLoc = readSourceLocation();
7041   unsigned NumArgsAsWritten = readInt();
7042   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7043   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7044     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7045   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7046 }
7047 
7048 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7049   return GetDecl(ID);
7050 }
7051 
7052 void ASTReader::CompleteRedeclChain(const Decl *D) {
7053   if (NumCurrentElementsDeserializing) {
7054     // We arrange to not care about the complete redeclaration chain while we're
7055     // deserializing. Just remember that the AST has marked this one as complete
7056     // but that it's not actually complete yet, so we know we still need to
7057     // complete it later.
7058     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7059     return;
7060   }
7061 
7062   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7063 
7064   // If this is a named declaration, complete it by looking it up
7065   // within its context.
7066   //
7067   // FIXME: Merging a function definition should merge
7068   // all mergeable entities within it.
7069   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7070       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7071     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7072       if (!getContext().getLangOpts().CPlusPlus &&
7073           isa<TranslationUnitDecl>(DC)) {
7074         // Outside of C++, we don't have a lookup table for the TU, so update
7075         // the identifier instead. (For C++ modules, we don't store decls
7076         // in the serialized identifier table, so we do the lookup in the TU.)
7077         auto *II = Name.getAsIdentifierInfo();
7078         assert(II && "non-identifier name in C?");
7079         if (II->isOutOfDate())
7080           updateOutOfDateIdentifier(*II);
7081       } else
7082         DC->lookup(Name);
7083     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7084       // Find all declarations of this kind from the relevant context.
7085       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7086         auto *DC = cast<DeclContext>(DCDecl);
7087         SmallVector<Decl*, 8> Decls;
7088         FindExternalLexicalDecls(
7089             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7090       }
7091     }
7092   }
7093 
7094   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7095     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7096   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7097     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7098   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7099     if (auto *Template = FD->getPrimaryTemplate())
7100       Template->LoadLazySpecializations();
7101   }
7102 }
7103 
7104 CXXCtorInitializer **
7105 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7106   RecordLocation Loc = getLocalBitOffset(Offset);
7107   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7108   SavedStreamPosition SavedPosition(Cursor);
7109   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7110     Error(std::move(Err));
7111     return nullptr;
7112   }
7113   ReadingKindTracker ReadingKind(Read_Decl, *this);
7114 
7115   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7116   if (!MaybeCode) {
7117     Error(MaybeCode.takeError());
7118     return nullptr;
7119   }
7120   unsigned Code = MaybeCode.get();
7121 
7122   ASTRecordReader Record(*this, *Loc.F);
7123   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7124   if (!MaybeRecCode) {
7125     Error(MaybeRecCode.takeError());
7126     return nullptr;
7127   }
7128   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7129     Error("malformed AST file: missing C++ ctor initializers");
7130     return nullptr;
7131   }
7132 
7133   return Record.readCXXCtorInitializers();
7134 }
7135 
7136 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7137   assert(ContextObj && "reading base specifiers with no AST context");
7138   ASTContext &Context = *ContextObj;
7139 
7140   RecordLocation Loc = getLocalBitOffset(Offset);
7141   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7142   SavedStreamPosition SavedPosition(Cursor);
7143   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7144     Error(std::move(Err));
7145     return nullptr;
7146   }
7147   ReadingKindTracker ReadingKind(Read_Decl, *this);
7148 
7149   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7150   if (!MaybeCode) {
7151     Error(MaybeCode.takeError());
7152     return nullptr;
7153   }
7154   unsigned Code = MaybeCode.get();
7155 
7156   ASTRecordReader Record(*this, *Loc.F);
7157   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7158   if (!MaybeRecCode) {
7159     Error(MaybeCode.takeError());
7160     return nullptr;
7161   }
7162   unsigned RecCode = MaybeRecCode.get();
7163 
7164   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7165     Error("malformed AST file: missing C++ base specifiers");
7166     return nullptr;
7167   }
7168 
7169   unsigned NumBases = Record.readInt();
7170   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7171   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7172   for (unsigned I = 0; I != NumBases; ++I)
7173     Bases[I] = Record.readCXXBaseSpecifier();
7174   return Bases;
7175 }
7176 
7177 serialization::DeclID
7178 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7179   if (LocalID < NUM_PREDEF_DECL_IDS)
7180     return LocalID;
7181 
7182   if (!F.ModuleOffsetMap.empty())
7183     ReadModuleOffsetMap(F);
7184 
7185   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7186     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7187   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7188 
7189   return LocalID + I->second;
7190 }
7191 
7192 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7193                                    ModuleFile &M) const {
7194   // Predefined decls aren't from any module.
7195   if (ID < NUM_PREDEF_DECL_IDS)
7196     return false;
7197 
7198   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7199          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7200 }
7201 
7202 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7203   if (!D->isFromASTFile())
7204     return nullptr;
7205   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7206   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7207   return I->second;
7208 }
7209 
7210 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7211   if (ID < NUM_PREDEF_DECL_IDS)
7212     return SourceLocation();
7213 
7214   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7215 
7216   if (Index > DeclsLoaded.size()) {
7217     Error("declaration ID out-of-range for AST file");
7218     return SourceLocation();
7219   }
7220 
7221   if (Decl *D = DeclsLoaded[Index])
7222     return D->getLocation();
7223 
7224   SourceLocation Loc;
7225   DeclCursorForID(ID, Loc);
7226   return Loc;
7227 }
7228 
7229 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7230   switch (ID) {
7231   case PREDEF_DECL_NULL_ID:
7232     return nullptr;
7233 
7234   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7235     return Context.getTranslationUnitDecl();
7236 
7237   case PREDEF_DECL_OBJC_ID_ID:
7238     return Context.getObjCIdDecl();
7239 
7240   case PREDEF_DECL_OBJC_SEL_ID:
7241     return Context.getObjCSelDecl();
7242 
7243   case PREDEF_DECL_OBJC_CLASS_ID:
7244     return Context.getObjCClassDecl();
7245 
7246   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7247     return Context.getObjCProtocolDecl();
7248 
7249   case PREDEF_DECL_INT_128_ID:
7250     return Context.getInt128Decl();
7251 
7252   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7253     return Context.getUInt128Decl();
7254 
7255   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7256     return Context.getObjCInstanceTypeDecl();
7257 
7258   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7259     return Context.getBuiltinVaListDecl();
7260 
7261   case PREDEF_DECL_VA_LIST_TAG:
7262     return Context.getVaListTagDecl();
7263 
7264   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7265     return Context.getBuiltinMSVaListDecl();
7266 
7267   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7268     return Context.getExternCContextDecl();
7269 
7270   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7271     return Context.getMakeIntegerSeqDecl();
7272 
7273   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7274     return Context.getCFConstantStringDecl();
7275 
7276   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7277     return Context.getCFConstantStringTagDecl();
7278 
7279   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7280     return Context.getTypePackElementDecl();
7281   }
7282   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7283 }
7284 
7285 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7286   assert(ContextObj && "reading decl with no AST context");
7287   if (ID < NUM_PREDEF_DECL_IDS) {
7288     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7289     if (D) {
7290       // Track that we have merged the declaration with ID \p ID into the
7291       // pre-existing predefined declaration \p D.
7292       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7293       if (Merged.empty())
7294         Merged.push_back(ID);
7295     }
7296     return D;
7297   }
7298 
7299   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7300 
7301   if (Index >= DeclsLoaded.size()) {
7302     assert(0 && "declaration ID out-of-range for AST file");
7303     Error("declaration ID out-of-range for AST file");
7304     return nullptr;
7305   }
7306 
7307   return DeclsLoaded[Index];
7308 }
7309 
7310 Decl *ASTReader::GetDecl(DeclID ID) {
7311   if (ID < NUM_PREDEF_DECL_IDS)
7312     return GetExistingDecl(ID);
7313 
7314   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7315 
7316   if (Index >= DeclsLoaded.size()) {
7317     assert(0 && "declaration ID out-of-range for AST file");
7318     Error("declaration ID out-of-range for AST file");
7319     return nullptr;
7320   }
7321 
7322   if (!DeclsLoaded[Index]) {
7323     ReadDeclRecord(ID);
7324     if (DeserializationListener)
7325       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7326   }
7327 
7328   return DeclsLoaded[Index];
7329 }
7330 
7331 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7332                                                   DeclID GlobalID) {
7333   if (GlobalID < NUM_PREDEF_DECL_IDS)
7334     return GlobalID;
7335 
7336   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7337   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7338   ModuleFile *Owner = I->second;
7339 
7340   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7341     = M.GlobalToLocalDeclIDs.find(Owner);
7342   if (Pos == M.GlobalToLocalDeclIDs.end())
7343     return 0;
7344 
7345   return GlobalID - Owner->BaseDeclID + Pos->second;
7346 }
7347 
7348 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7349                                             const RecordData &Record,
7350                                             unsigned &Idx) {
7351   if (Idx >= Record.size()) {
7352     Error("Corrupted AST file");
7353     return 0;
7354   }
7355 
7356   return getGlobalDeclID(F, Record[Idx++]);
7357 }
7358 
7359 /// Resolve the offset of a statement into a statement.
7360 ///
7361 /// This operation will read a new statement from the external
7362 /// source each time it is called, and is meant to be used via a
7363 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7364 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7365   // Switch case IDs are per Decl.
7366   ClearSwitchCaseIDs();
7367 
7368   // Offset here is a global offset across the entire chain.
7369   RecordLocation Loc = getLocalBitOffset(Offset);
7370   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7371     Error(std::move(Err));
7372     return nullptr;
7373   }
7374   assert(NumCurrentElementsDeserializing == 0 &&
7375          "should not be called while already deserializing");
7376   Deserializing D(this);
7377   return ReadStmtFromStream(*Loc.F);
7378 }
7379 
7380 void ASTReader::FindExternalLexicalDecls(
7381     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7382     SmallVectorImpl<Decl *> &Decls) {
7383   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7384 
7385   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7386     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7387     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7388       auto K = (Decl::Kind)+LexicalDecls[I];
7389       if (!IsKindWeWant(K))
7390         continue;
7391 
7392       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7393 
7394       // Don't add predefined declarations to the lexical context more
7395       // than once.
7396       if (ID < NUM_PREDEF_DECL_IDS) {
7397         if (PredefsVisited[ID])
7398           continue;
7399 
7400         PredefsVisited[ID] = true;
7401       }
7402 
7403       if (Decl *D = GetLocalDecl(*M, ID)) {
7404         assert(D->getKind() == K && "wrong kind for lexical decl");
7405         if (!DC->isDeclInLexicalTraversal(D))
7406           Decls.push_back(D);
7407       }
7408     }
7409   };
7410 
7411   if (isa<TranslationUnitDecl>(DC)) {
7412     for (auto Lexical : TULexicalDecls)
7413       Visit(Lexical.first, Lexical.second);
7414   } else {
7415     auto I = LexicalDecls.find(DC);
7416     if (I != LexicalDecls.end())
7417       Visit(I->second.first, I->second.second);
7418   }
7419 
7420   ++NumLexicalDeclContextsRead;
7421 }
7422 
7423 namespace {
7424 
7425 class DeclIDComp {
7426   ASTReader &Reader;
7427   ModuleFile &Mod;
7428 
7429 public:
7430   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7431 
7432   bool operator()(LocalDeclID L, LocalDeclID R) const {
7433     SourceLocation LHS = getLocation(L);
7434     SourceLocation RHS = getLocation(R);
7435     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7436   }
7437 
7438   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7439     SourceLocation RHS = getLocation(R);
7440     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7441   }
7442 
7443   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7444     SourceLocation LHS = getLocation(L);
7445     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7446   }
7447 
7448   SourceLocation getLocation(LocalDeclID ID) const {
7449     return Reader.getSourceManager().getFileLoc(
7450             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7451   }
7452 };
7453 
7454 } // namespace
7455 
7456 void ASTReader::FindFileRegionDecls(FileID File,
7457                                     unsigned Offset, unsigned Length,
7458                                     SmallVectorImpl<Decl *> &Decls) {
7459   SourceManager &SM = getSourceManager();
7460 
7461   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7462   if (I == FileDeclIDs.end())
7463     return;
7464 
7465   FileDeclsInfo &DInfo = I->second;
7466   if (DInfo.Decls.empty())
7467     return;
7468 
7469   SourceLocation
7470     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7471   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7472 
7473   DeclIDComp DIDComp(*this, *DInfo.Mod);
7474   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7475       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7476   if (BeginIt != DInfo.Decls.begin())
7477     --BeginIt;
7478 
7479   // If we are pointing at a top-level decl inside an objc container, we need
7480   // to backtrack until we find it otherwise we will fail to report that the
7481   // region overlaps with an objc container.
7482   while (BeginIt != DInfo.Decls.begin() &&
7483          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7484              ->isTopLevelDeclInObjCContainer())
7485     --BeginIt;
7486 
7487   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7488       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7489   if (EndIt != DInfo.Decls.end())
7490     ++EndIt;
7491 
7492   for (ArrayRef<serialization::LocalDeclID>::iterator
7493          DIt = BeginIt; DIt != EndIt; ++DIt)
7494     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7495 }
7496 
7497 bool
7498 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7499                                           DeclarationName Name) {
7500   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7501          "DeclContext has no visible decls in storage");
7502   if (!Name)
7503     return false;
7504 
7505   auto It = Lookups.find(DC);
7506   if (It == Lookups.end())
7507     return false;
7508 
7509   Deserializing LookupResults(this);
7510 
7511   // Load the list of declarations.
7512   SmallVector<NamedDecl *, 64> Decls;
7513   for (DeclID ID : It->second.Table.find(Name)) {
7514     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7515     if (ND->getDeclName() == Name)
7516       Decls.push_back(ND);
7517   }
7518 
7519   ++NumVisibleDeclContextsRead;
7520   SetExternalVisibleDeclsForName(DC, Name, Decls);
7521   return !Decls.empty();
7522 }
7523 
7524 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7525   if (!DC->hasExternalVisibleStorage())
7526     return;
7527 
7528   auto It = Lookups.find(DC);
7529   assert(It != Lookups.end() &&
7530          "have external visible storage but no lookup tables");
7531 
7532   DeclsMap Decls;
7533 
7534   for (DeclID ID : It->second.Table.findAll()) {
7535     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7536     Decls[ND->getDeclName()].push_back(ND);
7537   }
7538 
7539   ++NumVisibleDeclContextsRead;
7540 
7541   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7542     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7543   }
7544   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7545 }
7546 
7547 const serialization::reader::DeclContextLookupTable *
7548 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7549   auto I = Lookups.find(Primary);
7550   return I == Lookups.end() ? nullptr : &I->second;
7551 }
7552 
7553 /// Under non-PCH compilation the consumer receives the objc methods
7554 /// before receiving the implementation, and codegen depends on this.
7555 /// We simulate this by deserializing and passing to consumer the methods of the
7556 /// implementation before passing the deserialized implementation decl.
7557 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7558                                        ASTConsumer *Consumer) {
7559   assert(ImplD && Consumer);
7560 
7561   for (auto *I : ImplD->methods())
7562     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7563 
7564   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7565 }
7566 
7567 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7568   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7569     PassObjCImplDeclToConsumer(ImplD, Consumer);
7570   else
7571     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7572 }
7573 
7574 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7575   this->Consumer = Consumer;
7576 
7577   if (Consumer)
7578     PassInterestingDeclsToConsumer();
7579 
7580   if (DeserializationListener)
7581     DeserializationListener->ReaderInitialized(this);
7582 }
7583 
7584 void ASTReader::PrintStats() {
7585   std::fprintf(stderr, "*** AST File Statistics:\n");
7586 
7587   unsigned NumTypesLoaded
7588     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7589                                       QualType());
7590   unsigned NumDeclsLoaded
7591     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7592                                       (Decl *)nullptr);
7593   unsigned NumIdentifiersLoaded
7594     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7595                                             IdentifiersLoaded.end(),
7596                                             (IdentifierInfo *)nullptr);
7597   unsigned NumMacrosLoaded
7598     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7599                                        MacrosLoaded.end(),
7600                                        (MacroInfo *)nullptr);
7601   unsigned NumSelectorsLoaded
7602     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7603                                           SelectorsLoaded.end(),
7604                                           Selector());
7605 
7606   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7607     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7608                  NumSLocEntriesRead, TotalNumSLocEntries,
7609                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7610   if (!TypesLoaded.empty())
7611     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7612                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7613                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7614   if (!DeclsLoaded.empty())
7615     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7616                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7617                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7618   if (!IdentifiersLoaded.empty())
7619     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7620                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7621                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7622   if (!MacrosLoaded.empty())
7623     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7624                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7625                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7626   if (!SelectorsLoaded.empty())
7627     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7628                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7629                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7630   if (TotalNumStatements)
7631     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7632                  NumStatementsRead, TotalNumStatements,
7633                  ((float)NumStatementsRead/TotalNumStatements * 100));
7634   if (TotalNumMacros)
7635     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7636                  NumMacrosRead, TotalNumMacros,
7637                  ((float)NumMacrosRead/TotalNumMacros * 100));
7638   if (TotalLexicalDeclContexts)
7639     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7640                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7641                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7642                   * 100));
7643   if (TotalVisibleDeclContexts)
7644     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7645                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7646                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7647                   * 100));
7648   if (TotalNumMethodPoolEntries)
7649     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7650                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7651                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7652                   * 100));
7653   if (NumMethodPoolLookups)
7654     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7655                  NumMethodPoolHits, NumMethodPoolLookups,
7656                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7657   if (NumMethodPoolTableLookups)
7658     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7659                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7660                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7661                   * 100.0));
7662   if (NumIdentifierLookupHits)
7663     std::fprintf(stderr,
7664                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7665                  NumIdentifierLookupHits, NumIdentifierLookups,
7666                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7667 
7668   if (GlobalIndex) {
7669     std::fprintf(stderr, "\n");
7670     GlobalIndex->printStats();
7671   }
7672 
7673   std::fprintf(stderr, "\n");
7674   dump();
7675   std::fprintf(stderr, "\n");
7676 }
7677 
7678 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7679 LLVM_DUMP_METHOD static void
7680 dumpModuleIDMap(StringRef Name,
7681                 const ContinuousRangeMap<Key, ModuleFile *,
7682                                          InitialCapacity> &Map) {
7683   if (Map.begin() == Map.end())
7684     return;
7685 
7686   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7687 
7688   llvm::errs() << Name << ":\n";
7689   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7690        I != IEnd; ++I) {
7691     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7692       << "\n";
7693   }
7694 }
7695 
7696 LLVM_DUMP_METHOD void ASTReader::dump() {
7697   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7698   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7699   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7700   dumpModuleIDMap("Global type map", GlobalTypeMap);
7701   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7702   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7703   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7704   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7705   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7706   dumpModuleIDMap("Global preprocessed entity map",
7707                   GlobalPreprocessedEntityMap);
7708 
7709   llvm::errs() << "\n*** PCH/Modules Loaded:";
7710   for (ModuleFile &M : ModuleMgr)
7711     M.dump();
7712 }
7713 
7714 /// Return the amount of memory used by memory buffers, breaking down
7715 /// by heap-backed versus mmap'ed memory.
7716 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7717   for (ModuleFile &I : ModuleMgr) {
7718     if (llvm::MemoryBuffer *buf = I.Buffer) {
7719       size_t bytes = buf->getBufferSize();
7720       switch (buf->getBufferKind()) {
7721         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7722           sizes.malloc_bytes += bytes;
7723           break;
7724         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7725           sizes.mmap_bytes += bytes;
7726           break;
7727       }
7728     }
7729   }
7730 }
7731 
7732 void ASTReader::InitializeSema(Sema &S) {
7733   SemaObj = &S;
7734   S.addExternalSource(this);
7735 
7736   // Makes sure any declarations that were deserialized "too early"
7737   // still get added to the identifier's declaration chains.
7738   for (uint64_t ID : PreloadedDeclIDs) {
7739     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7740     pushExternalDeclIntoScope(D, D->getDeclName());
7741   }
7742   PreloadedDeclIDs.clear();
7743 
7744   // FIXME: What happens if these are changed by a module import?
7745   if (!FPPragmaOptions.empty()) {
7746     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7747     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7748   }
7749 
7750   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7751   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7752   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7753 
7754   UpdateSema();
7755 }
7756 
7757 void ASTReader::UpdateSema() {
7758   assert(SemaObj && "no Sema to update");
7759 
7760   // Load the offsets of the declarations that Sema references.
7761   // They will be lazily deserialized when needed.
7762   if (!SemaDeclRefs.empty()) {
7763     assert(SemaDeclRefs.size() % 3 == 0);
7764     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7765       if (!SemaObj->StdNamespace)
7766         SemaObj->StdNamespace = SemaDeclRefs[I];
7767       if (!SemaObj->StdBadAlloc)
7768         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7769       if (!SemaObj->StdAlignValT)
7770         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7771     }
7772     SemaDeclRefs.clear();
7773   }
7774 
7775   // Update the state of pragmas. Use the same API as if we had encountered the
7776   // pragma in the source.
7777   if(OptimizeOffPragmaLocation.isValid())
7778     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7779   if (PragmaMSStructState != -1)
7780     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7781   if (PointersToMembersPragmaLocation.isValid()) {
7782     SemaObj->ActOnPragmaMSPointersToMembers(
7783         (LangOptions::PragmaMSPointersToMembersKind)
7784             PragmaMSPointersToMembersState,
7785         PointersToMembersPragmaLocation);
7786   }
7787   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7788 
7789   if (PragmaPackCurrentValue) {
7790     // The bottom of the stack might have a default value. It must be adjusted
7791     // to the current value to ensure that the packing state is preserved after
7792     // popping entries that were included/imported from a PCH/module.
7793     bool DropFirst = false;
7794     if (!PragmaPackStack.empty() &&
7795         PragmaPackStack.front().Location.isInvalid()) {
7796       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7797              "Expected a default alignment value");
7798       SemaObj->PackStack.Stack.emplace_back(
7799           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7800           SemaObj->PackStack.CurrentPragmaLocation,
7801           PragmaPackStack.front().PushLocation);
7802       DropFirst = true;
7803     }
7804     for (const auto &Entry :
7805          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7806       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7807                                             Entry.Location, Entry.PushLocation);
7808     if (PragmaPackCurrentLocation.isInvalid()) {
7809       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7810              "Expected a default alignment value");
7811       // Keep the current values.
7812     } else {
7813       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7814       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7815     }
7816   }
7817 }
7818 
7819 IdentifierInfo *ASTReader::get(StringRef Name) {
7820   // Note that we are loading an identifier.
7821   Deserializing AnIdentifier(this);
7822 
7823   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7824                                   NumIdentifierLookups,
7825                                   NumIdentifierLookupHits);
7826 
7827   // We don't need to do identifier table lookups in C++ modules (we preload
7828   // all interesting declarations, and don't need to use the scope for name
7829   // lookups). Perform the lookup in PCH files, though, since we don't build
7830   // a complete initial identifier table if we're carrying on from a PCH.
7831   if (PP.getLangOpts().CPlusPlus) {
7832     for (auto F : ModuleMgr.pch_modules())
7833       if (Visitor(*F))
7834         break;
7835   } else {
7836     // If there is a global index, look there first to determine which modules
7837     // provably do not have any results for this identifier.
7838     GlobalModuleIndex::HitSet Hits;
7839     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7840     if (!loadGlobalIndex()) {
7841       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7842         HitsPtr = &Hits;
7843       }
7844     }
7845 
7846     ModuleMgr.visit(Visitor, HitsPtr);
7847   }
7848 
7849   IdentifierInfo *II = Visitor.getIdentifierInfo();
7850   markIdentifierUpToDate(II);
7851   return II;
7852 }
7853 
7854 namespace clang {
7855 
7856   /// An identifier-lookup iterator that enumerates all of the
7857   /// identifiers stored within a set of AST files.
7858   class ASTIdentifierIterator : public IdentifierIterator {
7859     /// The AST reader whose identifiers are being enumerated.
7860     const ASTReader &Reader;
7861 
7862     /// The current index into the chain of AST files stored in
7863     /// the AST reader.
7864     unsigned Index;
7865 
7866     /// The current position within the identifier lookup table
7867     /// of the current AST file.
7868     ASTIdentifierLookupTable::key_iterator Current;
7869 
7870     /// The end position within the identifier lookup table of
7871     /// the current AST file.
7872     ASTIdentifierLookupTable::key_iterator End;
7873 
7874     /// Whether to skip any modules in the ASTReader.
7875     bool SkipModules;
7876 
7877   public:
7878     explicit ASTIdentifierIterator(const ASTReader &Reader,
7879                                    bool SkipModules = false);
7880 
7881     StringRef Next() override;
7882   };
7883 
7884 } // namespace clang
7885 
7886 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7887                                              bool SkipModules)
7888     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7889 }
7890 
7891 StringRef ASTIdentifierIterator::Next() {
7892   while (Current == End) {
7893     // If we have exhausted all of our AST files, we're done.
7894     if (Index == 0)
7895       return StringRef();
7896 
7897     --Index;
7898     ModuleFile &F = Reader.ModuleMgr[Index];
7899     if (SkipModules && F.isModule())
7900       continue;
7901 
7902     ASTIdentifierLookupTable *IdTable =
7903         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7904     Current = IdTable->key_begin();
7905     End = IdTable->key_end();
7906   }
7907 
7908   // We have any identifiers remaining in the current AST file; return
7909   // the next one.
7910   StringRef Result = *Current;
7911   ++Current;
7912   return Result;
7913 }
7914 
7915 namespace {
7916 
7917 /// A utility for appending two IdentifierIterators.
7918 class ChainedIdentifierIterator : public IdentifierIterator {
7919   std::unique_ptr<IdentifierIterator> Current;
7920   std::unique_ptr<IdentifierIterator> Queued;
7921 
7922 public:
7923   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7924                             std::unique_ptr<IdentifierIterator> Second)
7925       : Current(std::move(First)), Queued(std::move(Second)) {}
7926 
7927   StringRef Next() override {
7928     if (!Current)
7929       return StringRef();
7930 
7931     StringRef result = Current->Next();
7932     if (!result.empty())
7933       return result;
7934 
7935     // Try the queued iterator, which may itself be empty.
7936     Current.reset();
7937     std::swap(Current, Queued);
7938     return Next();
7939   }
7940 };
7941 
7942 } // namespace
7943 
7944 IdentifierIterator *ASTReader::getIdentifiers() {
7945   if (!loadGlobalIndex()) {
7946     std::unique_ptr<IdentifierIterator> ReaderIter(
7947         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7948     std::unique_ptr<IdentifierIterator> ModulesIter(
7949         GlobalIndex->createIdentifierIterator());
7950     return new ChainedIdentifierIterator(std::move(ReaderIter),
7951                                          std::move(ModulesIter));
7952   }
7953 
7954   return new ASTIdentifierIterator(*this);
7955 }
7956 
7957 namespace clang {
7958 namespace serialization {
7959 
7960   class ReadMethodPoolVisitor {
7961     ASTReader &Reader;
7962     Selector Sel;
7963     unsigned PriorGeneration;
7964     unsigned InstanceBits = 0;
7965     unsigned FactoryBits = 0;
7966     bool InstanceHasMoreThanOneDecl = false;
7967     bool FactoryHasMoreThanOneDecl = false;
7968     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7969     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7970 
7971   public:
7972     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7973                           unsigned PriorGeneration)
7974         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
7975 
7976     bool operator()(ModuleFile &M) {
7977       if (!M.SelectorLookupTable)
7978         return false;
7979 
7980       // If we've already searched this module file, skip it now.
7981       if (M.Generation <= PriorGeneration)
7982         return true;
7983 
7984       ++Reader.NumMethodPoolTableLookups;
7985       ASTSelectorLookupTable *PoolTable
7986         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7987       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
7988       if (Pos == PoolTable->end())
7989         return false;
7990 
7991       ++Reader.NumMethodPoolTableHits;
7992       ++Reader.NumSelectorsRead;
7993       // FIXME: Not quite happy with the statistics here. We probably should
7994       // disable this tracking when called via LoadSelector.
7995       // Also, should entries without methods count as misses?
7996       ++Reader.NumMethodPoolEntriesRead;
7997       ASTSelectorLookupTrait::data_type Data = *Pos;
7998       if (Reader.DeserializationListener)
7999         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8000 
8001       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8002       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8003       InstanceBits = Data.InstanceBits;
8004       FactoryBits = Data.FactoryBits;
8005       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8006       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8007       return true;
8008     }
8009 
8010     /// Retrieve the instance methods found by this visitor.
8011     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8012       return InstanceMethods;
8013     }
8014 
8015     /// Retrieve the instance methods found by this visitor.
8016     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8017       return FactoryMethods;
8018     }
8019 
8020     unsigned getInstanceBits() const { return InstanceBits; }
8021     unsigned getFactoryBits() const { return FactoryBits; }
8022 
8023     bool instanceHasMoreThanOneDecl() const {
8024       return InstanceHasMoreThanOneDecl;
8025     }
8026 
8027     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8028   };
8029 
8030 } // namespace serialization
8031 } // namespace clang
8032 
8033 /// Add the given set of methods to the method list.
8034 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8035                              ObjCMethodList &List) {
8036   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8037     S.addMethodToGlobalList(&List, Methods[I]);
8038   }
8039 }
8040 
8041 void ASTReader::ReadMethodPool(Selector Sel) {
8042   // Get the selector generation and update it to the current generation.
8043   unsigned &Generation = SelectorGeneration[Sel];
8044   unsigned PriorGeneration = Generation;
8045   Generation = getGeneration();
8046   SelectorOutOfDate[Sel] = false;
8047 
8048   // Search for methods defined with this selector.
8049   ++NumMethodPoolLookups;
8050   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8051   ModuleMgr.visit(Visitor);
8052 
8053   if (Visitor.getInstanceMethods().empty() &&
8054       Visitor.getFactoryMethods().empty())
8055     return;
8056 
8057   ++NumMethodPoolHits;
8058 
8059   if (!getSema())
8060     return;
8061 
8062   Sema &S = *getSema();
8063   Sema::GlobalMethodPool::iterator Pos
8064     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8065 
8066   Pos->second.first.setBits(Visitor.getInstanceBits());
8067   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8068   Pos->second.second.setBits(Visitor.getFactoryBits());
8069   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8070 
8071   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8072   // when building a module we keep every method individually and may need to
8073   // update hasMoreThanOneDecl as we add the methods.
8074   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8075   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8076 }
8077 
8078 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8079   if (SelectorOutOfDate[Sel])
8080     ReadMethodPool(Sel);
8081 }
8082 
8083 void ASTReader::ReadKnownNamespaces(
8084                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8085   Namespaces.clear();
8086 
8087   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8088     if (NamespaceDecl *Namespace
8089                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8090       Namespaces.push_back(Namespace);
8091   }
8092 }
8093 
8094 void ASTReader::ReadUndefinedButUsed(
8095     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8096   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8097     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8098     SourceLocation Loc =
8099         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8100     Undefined.insert(std::make_pair(D, Loc));
8101   }
8102 }
8103 
8104 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8105     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8106                                                      Exprs) {
8107   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8108     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8109     uint64_t Count = DelayedDeleteExprs[Idx++];
8110     for (uint64_t C = 0; C < Count; ++C) {
8111       SourceLocation DeleteLoc =
8112           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8113       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8114       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8115     }
8116   }
8117 }
8118 
8119 void ASTReader::ReadTentativeDefinitions(
8120                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8121   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8122     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8123     if (Var)
8124       TentativeDefs.push_back(Var);
8125   }
8126   TentativeDefinitions.clear();
8127 }
8128 
8129 void ASTReader::ReadUnusedFileScopedDecls(
8130                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8131   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8132     DeclaratorDecl *D
8133       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8134     if (D)
8135       Decls.push_back(D);
8136   }
8137   UnusedFileScopedDecls.clear();
8138 }
8139 
8140 void ASTReader::ReadDelegatingConstructors(
8141                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8142   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8143     CXXConstructorDecl *D
8144       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8145     if (D)
8146       Decls.push_back(D);
8147   }
8148   DelegatingCtorDecls.clear();
8149 }
8150 
8151 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8152   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8153     TypedefNameDecl *D
8154       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8155     if (D)
8156       Decls.push_back(D);
8157   }
8158   ExtVectorDecls.clear();
8159 }
8160 
8161 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8162     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8163   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8164        ++I) {
8165     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8166         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8167     if (D)
8168       Decls.insert(D);
8169   }
8170   UnusedLocalTypedefNameCandidates.clear();
8171 }
8172 
8173 void ASTReader::ReadReferencedSelectors(
8174        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8175   if (ReferencedSelectorsData.empty())
8176     return;
8177 
8178   // If there are @selector references added them to its pool. This is for
8179   // implementation of -Wselector.
8180   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8181   unsigned I = 0;
8182   while (I < DataSize) {
8183     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8184     SourceLocation SelLoc
8185       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8186     Sels.push_back(std::make_pair(Sel, SelLoc));
8187   }
8188   ReferencedSelectorsData.clear();
8189 }
8190 
8191 void ASTReader::ReadWeakUndeclaredIdentifiers(
8192        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8193   if (WeakUndeclaredIdentifiers.empty())
8194     return;
8195 
8196   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8197     IdentifierInfo *WeakId
8198       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8199     IdentifierInfo *AliasId
8200       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8201     SourceLocation Loc
8202       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8203     bool Used = WeakUndeclaredIdentifiers[I++];
8204     WeakInfo WI(AliasId, Loc);
8205     WI.setUsed(Used);
8206     WeakIDs.push_back(std::make_pair(WeakId, WI));
8207   }
8208   WeakUndeclaredIdentifiers.clear();
8209 }
8210 
8211 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8212   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8213     ExternalVTableUse VT;
8214     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8215     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8216     VT.DefinitionRequired = VTableUses[Idx++];
8217     VTables.push_back(VT);
8218   }
8219 
8220   VTableUses.clear();
8221 }
8222 
8223 void ASTReader::ReadPendingInstantiations(
8224        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8225   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8226     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8227     SourceLocation Loc
8228       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8229 
8230     Pending.push_back(std::make_pair(D, Loc));
8231   }
8232   PendingInstantiations.clear();
8233 }
8234 
8235 void ASTReader::ReadLateParsedTemplates(
8236     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8237         &LPTMap) {
8238   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8239        /* In loop */) {
8240     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8241 
8242     auto LT = std::make_unique<LateParsedTemplate>();
8243     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8244 
8245     ModuleFile *F = getOwningModuleFile(LT->D);
8246     assert(F && "No module");
8247 
8248     unsigned TokN = LateParsedTemplates[Idx++];
8249     LT->Toks.reserve(TokN);
8250     for (unsigned T = 0; T < TokN; ++T)
8251       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8252 
8253     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8254   }
8255 
8256   LateParsedTemplates.clear();
8257 }
8258 
8259 void ASTReader::LoadSelector(Selector Sel) {
8260   // It would be complicated to avoid reading the methods anyway. So don't.
8261   ReadMethodPool(Sel);
8262 }
8263 
8264 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8265   assert(ID && "Non-zero identifier ID required");
8266   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8267   IdentifiersLoaded[ID - 1] = II;
8268   if (DeserializationListener)
8269     DeserializationListener->IdentifierRead(ID, II);
8270 }
8271 
8272 /// Set the globally-visible declarations associated with the given
8273 /// identifier.
8274 ///
8275 /// If the AST reader is currently in a state where the given declaration IDs
8276 /// cannot safely be resolved, they are queued until it is safe to resolve
8277 /// them.
8278 ///
8279 /// \param II an IdentifierInfo that refers to one or more globally-visible
8280 /// declarations.
8281 ///
8282 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8283 /// visible at global scope.
8284 ///
8285 /// \param Decls if non-null, this vector will be populated with the set of
8286 /// deserialized declarations. These declarations will not be pushed into
8287 /// scope.
8288 void
8289 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8290                               const SmallVectorImpl<uint32_t> &DeclIDs,
8291                                    SmallVectorImpl<Decl *> *Decls) {
8292   if (NumCurrentElementsDeserializing && !Decls) {
8293     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8294     return;
8295   }
8296 
8297   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8298     if (!SemaObj) {
8299       // Queue this declaration so that it will be added to the
8300       // translation unit scope and identifier's declaration chain
8301       // once a Sema object is known.
8302       PreloadedDeclIDs.push_back(DeclIDs[I]);
8303       continue;
8304     }
8305 
8306     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8307 
8308     // If we're simply supposed to record the declarations, do so now.
8309     if (Decls) {
8310       Decls->push_back(D);
8311       continue;
8312     }
8313 
8314     // Introduce this declaration into the translation-unit scope
8315     // and add it to the declaration chain for this identifier, so
8316     // that (unqualified) name lookup will find it.
8317     pushExternalDeclIntoScope(D, II);
8318   }
8319 }
8320 
8321 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8322   if (ID == 0)
8323     return nullptr;
8324 
8325   if (IdentifiersLoaded.empty()) {
8326     Error("no identifier table in AST file");
8327     return nullptr;
8328   }
8329 
8330   ID -= 1;
8331   if (!IdentifiersLoaded[ID]) {
8332     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8333     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8334     ModuleFile *M = I->second;
8335     unsigned Index = ID - M->BaseIdentifierID;
8336     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8337 
8338     // All of the strings in the AST file are preceded by a 16-bit length.
8339     // Extract that 16-bit length to avoid having to execute strlen().
8340     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8341     //  unsigned integers.  This is important to avoid integer overflow when
8342     //  we cast them to 'unsigned'.
8343     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8344     unsigned StrLen = (((unsigned) StrLenPtr[0])
8345                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8346     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8347     IdentifiersLoaded[ID] = &II;
8348     markIdentifierFromAST(*this,  II);
8349     if (DeserializationListener)
8350       DeserializationListener->IdentifierRead(ID + 1, &II);
8351   }
8352 
8353   return IdentifiersLoaded[ID];
8354 }
8355 
8356 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8357   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8358 }
8359 
8360 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8361   if (LocalID < NUM_PREDEF_IDENT_IDS)
8362     return LocalID;
8363 
8364   if (!M.ModuleOffsetMap.empty())
8365     ReadModuleOffsetMap(M);
8366 
8367   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8368     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8369   assert(I != M.IdentifierRemap.end()
8370          && "Invalid index into identifier index remap");
8371 
8372   return LocalID + I->second;
8373 }
8374 
8375 MacroInfo *ASTReader::getMacro(MacroID ID) {
8376   if (ID == 0)
8377     return nullptr;
8378 
8379   if (MacrosLoaded.empty()) {
8380     Error("no macro table in AST file");
8381     return nullptr;
8382   }
8383 
8384   ID -= NUM_PREDEF_MACRO_IDS;
8385   if (!MacrosLoaded[ID]) {
8386     GlobalMacroMapType::iterator I
8387       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8388     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8389     ModuleFile *M = I->second;
8390     unsigned Index = ID - M->BaseMacroID;
8391     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8392 
8393     if (DeserializationListener)
8394       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8395                                          MacrosLoaded[ID]);
8396   }
8397 
8398   return MacrosLoaded[ID];
8399 }
8400 
8401 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8402   if (LocalID < NUM_PREDEF_MACRO_IDS)
8403     return LocalID;
8404 
8405   if (!M.ModuleOffsetMap.empty())
8406     ReadModuleOffsetMap(M);
8407 
8408   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8409     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8410   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8411 
8412   return LocalID + I->second;
8413 }
8414 
8415 serialization::SubmoduleID
8416 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8417   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8418     return LocalID;
8419 
8420   if (!M.ModuleOffsetMap.empty())
8421     ReadModuleOffsetMap(M);
8422 
8423   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8424     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8425   assert(I != M.SubmoduleRemap.end()
8426          && "Invalid index into submodule index remap");
8427 
8428   return LocalID + I->second;
8429 }
8430 
8431 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8432   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8433     assert(GlobalID == 0 && "Unhandled global submodule ID");
8434     return nullptr;
8435   }
8436 
8437   if (GlobalID > SubmodulesLoaded.size()) {
8438     Error("submodule ID out of range in AST file");
8439     return nullptr;
8440   }
8441 
8442   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8443 }
8444 
8445 Module *ASTReader::getModule(unsigned ID) {
8446   return getSubmodule(ID);
8447 }
8448 
8449 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8450   ModuleFile *MF = getOwningModuleFile(D);
8451   return MF && MF->PCHHasObjectFile;
8452 }
8453 
8454 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8455   if (ID & 1) {
8456     // It's a module, look it up by submodule ID.
8457     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8458     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8459   } else {
8460     // It's a prefix (preamble, PCH, ...). Look it up by index.
8461     unsigned IndexFromEnd = ID >> 1;
8462     assert(IndexFromEnd && "got reference to unknown module file");
8463     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8464   }
8465 }
8466 
8467 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8468   if (!F)
8469     return 1;
8470 
8471   // For a file representing a module, use the submodule ID of the top-level
8472   // module as the file ID. For any other kind of file, the number of such
8473   // files loaded beforehand will be the same on reload.
8474   // FIXME: Is this true even if we have an explicit module file and a PCH?
8475   if (F->isModule())
8476     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8477 
8478   auto PCHModules = getModuleManager().pch_modules();
8479   auto I = llvm::find(PCHModules, F);
8480   assert(I != PCHModules.end() && "emitting reference to unknown file");
8481   return (I - PCHModules.end()) << 1;
8482 }
8483 
8484 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8485 ASTReader::getSourceDescriptor(unsigned ID) {
8486   if (const Module *M = getSubmodule(ID))
8487     return ExternalASTSource::ASTSourceDescriptor(*M);
8488 
8489   // If there is only a single PCH, return it instead.
8490   // Chained PCH are not supported.
8491   const auto &PCHChain = ModuleMgr.pch_modules();
8492   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8493     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8494     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8495     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8496     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8497                                           MF.Signature);
8498   }
8499   return None;
8500 }
8501 
8502 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8503   auto I = DefinitionSource.find(FD);
8504   if (I == DefinitionSource.end())
8505     return EK_ReplyHazy;
8506   return I->second ? EK_Never : EK_Always;
8507 }
8508 
8509 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8510   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8511 }
8512 
8513 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8514   if (ID == 0)
8515     return Selector();
8516 
8517   if (ID > SelectorsLoaded.size()) {
8518     Error("selector ID out of range in AST file");
8519     return Selector();
8520   }
8521 
8522   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8523     // Load this selector from the selector table.
8524     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8525     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8526     ModuleFile &M = *I->second;
8527     ASTSelectorLookupTrait Trait(*this, M);
8528     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8529     SelectorsLoaded[ID - 1] =
8530       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8531     if (DeserializationListener)
8532       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8533   }
8534 
8535   return SelectorsLoaded[ID - 1];
8536 }
8537 
8538 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8539   return DecodeSelector(ID);
8540 }
8541 
8542 uint32_t ASTReader::GetNumExternalSelectors() {
8543   // ID 0 (the null selector) is considered an external selector.
8544   return getTotalNumSelectors() + 1;
8545 }
8546 
8547 serialization::SelectorID
8548 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8549   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8550     return LocalID;
8551 
8552   if (!M.ModuleOffsetMap.empty())
8553     ReadModuleOffsetMap(M);
8554 
8555   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8556     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8557   assert(I != M.SelectorRemap.end()
8558          && "Invalid index into selector index remap");
8559 
8560   return LocalID + I->second;
8561 }
8562 
8563 DeclarationNameLoc
8564 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8565   DeclarationNameLoc DNLoc;
8566   switch (Name.getNameKind()) {
8567   case DeclarationName::CXXConstructorName:
8568   case DeclarationName::CXXDestructorName:
8569   case DeclarationName::CXXConversionFunctionName:
8570     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8571     break;
8572 
8573   case DeclarationName::CXXOperatorName:
8574     DNLoc.CXXOperatorName.BeginOpNameLoc
8575       = readSourceLocation().getRawEncoding();
8576     DNLoc.CXXOperatorName.EndOpNameLoc
8577       = readSourceLocation().getRawEncoding();
8578     break;
8579 
8580   case DeclarationName::CXXLiteralOperatorName:
8581     DNLoc.CXXLiteralOperatorName.OpNameLoc
8582       = readSourceLocation().getRawEncoding();
8583     break;
8584 
8585   case DeclarationName::Identifier:
8586   case DeclarationName::ObjCZeroArgSelector:
8587   case DeclarationName::ObjCOneArgSelector:
8588   case DeclarationName::ObjCMultiArgSelector:
8589   case DeclarationName::CXXUsingDirective:
8590   case DeclarationName::CXXDeductionGuideName:
8591     break;
8592   }
8593   return DNLoc;
8594 }
8595 
8596 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8597   DeclarationNameInfo NameInfo;
8598   NameInfo.setName(readDeclarationName());
8599   NameInfo.setLoc(readSourceLocation());
8600   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8601   return NameInfo;
8602 }
8603 
8604 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8605   Info.QualifierLoc = readNestedNameSpecifierLoc();
8606   unsigned NumTPLists = readInt();
8607   Info.NumTemplParamLists = NumTPLists;
8608   if (NumTPLists) {
8609     Info.TemplParamLists =
8610         new (getContext()) TemplateParameterList *[NumTPLists];
8611     for (unsigned i = 0; i != NumTPLists; ++i)
8612       Info.TemplParamLists[i] = readTemplateParameterList();
8613   }
8614 }
8615 
8616 TemplateParameterList *
8617 ASTRecordReader::readTemplateParameterList() {
8618   SourceLocation TemplateLoc = readSourceLocation();
8619   SourceLocation LAngleLoc = readSourceLocation();
8620   SourceLocation RAngleLoc = readSourceLocation();
8621 
8622   unsigned NumParams = readInt();
8623   SmallVector<NamedDecl *, 16> Params;
8624   Params.reserve(NumParams);
8625   while (NumParams--)
8626     Params.push_back(readDeclAs<NamedDecl>());
8627 
8628   bool HasRequiresClause = readBool();
8629   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8630 
8631   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8632       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8633   return TemplateParams;
8634 }
8635 
8636 void ASTRecordReader::readTemplateArgumentList(
8637                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8638                         bool Canonicalize) {
8639   unsigned NumTemplateArgs = readInt();
8640   TemplArgs.reserve(NumTemplateArgs);
8641   while (NumTemplateArgs--)
8642     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8643 }
8644 
8645 /// Read a UnresolvedSet structure.
8646 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8647   unsigned NumDecls = readInt();
8648   Set.reserve(getContext(), NumDecls);
8649   while (NumDecls--) {
8650     DeclID ID = readDeclID();
8651     AccessSpecifier AS = (AccessSpecifier) readInt();
8652     Set.addLazyDecl(getContext(), ID, AS);
8653   }
8654 }
8655 
8656 CXXBaseSpecifier
8657 ASTRecordReader::readCXXBaseSpecifier() {
8658   bool isVirtual = readBool();
8659   bool isBaseOfClass = readBool();
8660   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8661   bool inheritConstructors = readBool();
8662   TypeSourceInfo *TInfo = readTypeSourceInfo();
8663   SourceRange Range = readSourceRange();
8664   SourceLocation EllipsisLoc = readSourceLocation();
8665   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8666                           EllipsisLoc);
8667   Result.setInheritConstructors(inheritConstructors);
8668   return Result;
8669 }
8670 
8671 CXXCtorInitializer **
8672 ASTRecordReader::readCXXCtorInitializers() {
8673   ASTContext &Context = getContext();
8674   unsigned NumInitializers = readInt();
8675   assert(NumInitializers && "wrote ctor initializers but have no inits");
8676   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8677   for (unsigned i = 0; i != NumInitializers; ++i) {
8678     TypeSourceInfo *TInfo = nullptr;
8679     bool IsBaseVirtual = false;
8680     FieldDecl *Member = nullptr;
8681     IndirectFieldDecl *IndirectMember = nullptr;
8682 
8683     CtorInitializerType Type = (CtorInitializerType) readInt();
8684     switch (Type) {
8685     case CTOR_INITIALIZER_BASE:
8686       TInfo = readTypeSourceInfo();
8687       IsBaseVirtual = readBool();
8688       break;
8689 
8690     case CTOR_INITIALIZER_DELEGATING:
8691       TInfo = readTypeSourceInfo();
8692       break;
8693 
8694      case CTOR_INITIALIZER_MEMBER:
8695       Member = readDeclAs<FieldDecl>();
8696       break;
8697 
8698      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8699       IndirectMember = readDeclAs<IndirectFieldDecl>();
8700       break;
8701     }
8702 
8703     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8704     Expr *Init = readExpr();
8705     SourceLocation LParenLoc = readSourceLocation();
8706     SourceLocation RParenLoc = readSourceLocation();
8707 
8708     CXXCtorInitializer *BOMInit;
8709     if (Type == CTOR_INITIALIZER_BASE)
8710       BOMInit = new (Context)
8711           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8712                              RParenLoc, MemberOrEllipsisLoc);
8713     else if (Type == CTOR_INITIALIZER_DELEGATING)
8714       BOMInit = new (Context)
8715           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8716     else if (Member)
8717       BOMInit = new (Context)
8718           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8719                              Init, RParenLoc);
8720     else
8721       BOMInit = new (Context)
8722           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8723                              LParenLoc, Init, RParenLoc);
8724 
8725     if (/*IsWritten*/readBool()) {
8726       unsigned SourceOrder = readInt();
8727       BOMInit->setSourceOrder(SourceOrder);
8728     }
8729 
8730     CtorInitializers[i] = BOMInit;
8731   }
8732 
8733   return CtorInitializers;
8734 }
8735 
8736 NestedNameSpecifierLoc
8737 ASTRecordReader::readNestedNameSpecifierLoc() {
8738   ASTContext &Context = getContext();
8739   unsigned N = readInt();
8740   NestedNameSpecifierLocBuilder Builder;
8741   for (unsigned I = 0; I != N; ++I) {
8742     auto Kind = readNestedNameSpecifierKind();
8743     switch (Kind) {
8744     case NestedNameSpecifier::Identifier: {
8745       IdentifierInfo *II = readIdentifier();
8746       SourceRange Range = readSourceRange();
8747       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8748       break;
8749     }
8750 
8751     case NestedNameSpecifier::Namespace: {
8752       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8753       SourceRange Range = readSourceRange();
8754       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8755       break;
8756     }
8757 
8758     case NestedNameSpecifier::NamespaceAlias: {
8759       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8760       SourceRange Range = readSourceRange();
8761       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8762       break;
8763     }
8764 
8765     case NestedNameSpecifier::TypeSpec:
8766     case NestedNameSpecifier::TypeSpecWithTemplate: {
8767       bool Template = readBool();
8768       TypeSourceInfo *T = readTypeSourceInfo();
8769       if (!T)
8770         return NestedNameSpecifierLoc();
8771       SourceLocation ColonColonLoc = readSourceLocation();
8772 
8773       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8774       Builder.Extend(Context,
8775                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8776                      T->getTypeLoc(), ColonColonLoc);
8777       break;
8778     }
8779 
8780     case NestedNameSpecifier::Global: {
8781       SourceLocation ColonColonLoc = readSourceLocation();
8782       Builder.MakeGlobal(Context, ColonColonLoc);
8783       break;
8784     }
8785 
8786     case NestedNameSpecifier::Super: {
8787       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8788       SourceRange Range = readSourceRange();
8789       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8790       break;
8791     }
8792     }
8793   }
8794 
8795   return Builder.getWithLocInContext(Context);
8796 }
8797 
8798 SourceRange
8799 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8800                            unsigned &Idx) {
8801   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8802   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8803   return SourceRange(beg, end);
8804 }
8805 
8806 static FixedPointSemantics
8807 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8808                         unsigned &Idx) {
8809   unsigned Width = Record[Idx++];
8810   unsigned Scale = Record[Idx++];
8811   uint64_t Tmp = Record[Idx++];
8812   bool IsSigned = Tmp & 0x1;
8813   bool IsSaturated = Tmp & 0x2;
8814   bool HasUnsignedPadding = Tmp & 0x4;
8815   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8816                              HasUnsignedPadding);
8817 }
8818 
8819 static const llvm::fltSemantics &
8820 readAPFloatSemantics(ASTRecordReader &reader) {
8821   return llvm::APFloatBase::EnumToSemantics(
8822     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8823 }
8824 
8825 APValue ASTRecordReader::readAPValue() {
8826   unsigned Kind = readInt();
8827   switch ((APValue::ValueKind) Kind) {
8828   case APValue::None:
8829     return APValue();
8830   case APValue::Indeterminate:
8831     return APValue::IndeterminateValue();
8832   case APValue::Int:
8833     return APValue(readAPSInt());
8834   case APValue::Float: {
8835     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8836     return APValue(readAPFloat(FloatSema));
8837   }
8838   case APValue::FixedPoint: {
8839     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8840     return APValue(APFixedPoint(readAPInt(), FPSema));
8841   }
8842   case APValue::ComplexInt: {
8843     llvm::APSInt First = readAPSInt();
8844     return APValue(std::move(First), readAPSInt());
8845   }
8846   case APValue::ComplexFloat: {
8847     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8848     llvm::APFloat First = readAPFloat(FloatSema1);
8849     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8850     return APValue(std::move(First), readAPFloat(FloatSema2));
8851   }
8852   case APValue::LValue:
8853   case APValue::Vector:
8854   case APValue::Array:
8855   case APValue::Struct:
8856   case APValue::Union:
8857   case APValue::MemberPointer:
8858   case APValue::AddrLabelDiff:
8859     // TODO : Handle all these APValue::ValueKind.
8860     return APValue();
8861   }
8862   llvm_unreachable("Invalid APValue::ValueKind");
8863 }
8864 
8865 /// Read a floating-point value
8866 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8867   return llvm::APFloat(Sem, readAPInt());
8868 }
8869 
8870 // Read a string
8871 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8872   unsigned Len = Record[Idx++];
8873   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8874   Idx += Len;
8875   return Result;
8876 }
8877 
8878 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8879                                 unsigned &Idx) {
8880   std::string Filename = ReadString(Record, Idx);
8881   ResolveImportedPath(F, Filename);
8882   return Filename;
8883 }
8884 
8885 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8886                                 const RecordData &Record, unsigned &Idx) {
8887   std::string Filename = ReadString(Record, Idx);
8888   if (!BaseDirectory.empty())
8889     ResolveImportedPath(Filename, BaseDirectory);
8890   return Filename;
8891 }
8892 
8893 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8894                                          unsigned &Idx) {
8895   unsigned Major = Record[Idx++];
8896   unsigned Minor = Record[Idx++];
8897   unsigned Subminor = Record[Idx++];
8898   if (Minor == 0)
8899     return VersionTuple(Major);
8900   if (Subminor == 0)
8901     return VersionTuple(Major, Minor - 1);
8902   return VersionTuple(Major, Minor - 1, Subminor - 1);
8903 }
8904 
8905 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8906                                           const RecordData &Record,
8907                                           unsigned &Idx) {
8908   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8909   return CXXTemporary::Create(getContext(), Decl);
8910 }
8911 
8912 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
8913   return Diag(CurrentImportLoc, DiagID);
8914 }
8915 
8916 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
8917   return Diags.Report(Loc, DiagID);
8918 }
8919 
8920 /// Retrieve the identifier table associated with the
8921 /// preprocessor.
8922 IdentifierTable &ASTReader::getIdentifierTable() {
8923   return PP.getIdentifierTable();
8924 }
8925 
8926 /// Record that the given ID maps to the given switch-case
8927 /// statement.
8928 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8929   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8930          "Already have a SwitchCase with this ID");
8931   (*CurrSwitchCaseStmts)[ID] = SC;
8932 }
8933 
8934 /// Retrieve the switch-case statement with the given ID.
8935 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8936   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8937   return (*CurrSwitchCaseStmts)[ID];
8938 }
8939 
8940 void ASTReader::ClearSwitchCaseIDs() {
8941   CurrSwitchCaseStmts->clear();
8942 }
8943 
8944 void ASTReader::ReadComments() {
8945   ASTContext &Context = getContext();
8946   std::vector<RawComment *> Comments;
8947   for (SmallVectorImpl<std::pair<BitstreamCursor,
8948                                  serialization::ModuleFile *>>::iterator
8949        I = CommentsCursors.begin(),
8950        E = CommentsCursors.end();
8951        I != E; ++I) {
8952     Comments.clear();
8953     BitstreamCursor &Cursor = I->first;
8954     serialization::ModuleFile &F = *I->second;
8955     SavedStreamPosition SavedPosition(Cursor);
8956 
8957     RecordData Record;
8958     while (true) {
8959       Expected<llvm::BitstreamEntry> MaybeEntry =
8960           Cursor.advanceSkippingSubblocks(
8961               BitstreamCursor::AF_DontPopBlockAtEnd);
8962       if (!MaybeEntry) {
8963         Error(MaybeEntry.takeError());
8964         return;
8965       }
8966       llvm::BitstreamEntry Entry = MaybeEntry.get();
8967 
8968       switch (Entry.Kind) {
8969       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8970       case llvm::BitstreamEntry::Error:
8971         Error("malformed block record in AST file");
8972         return;
8973       case llvm::BitstreamEntry::EndBlock:
8974         goto NextCursor;
8975       case llvm::BitstreamEntry::Record:
8976         // The interesting case.
8977         break;
8978       }
8979 
8980       // Read a record.
8981       Record.clear();
8982       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
8983       if (!MaybeComment) {
8984         Error(MaybeComment.takeError());
8985         return;
8986       }
8987       switch ((CommentRecordTypes)MaybeComment.get()) {
8988       case COMMENTS_RAW_COMMENT: {
8989         unsigned Idx = 0;
8990         SourceRange SR = ReadSourceRange(F, Record, Idx);
8991         RawComment::CommentKind Kind =
8992             (RawComment::CommentKind) Record[Idx++];
8993         bool IsTrailingComment = Record[Idx++];
8994         bool IsAlmostTrailingComment = Record[Idx++];
8995         Comments.push_back(new (Context) RawComment(
8996             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
8997         break;
8998       }
8999       }
9000     }
9001   NextCursor:
9002     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9003         FileToOffsetToComment;
9004     for (RawComment *C : Comments) {
9005       SourceLocation CommentLoc = C->getBeginLoc();
9006       if (CommentLoc.isValid()) {
9007         std::pair<FileID, unsigned> Loc =
9008             SourceMgr.getDecomposedLoc(CommentLoc);
9009         if (Loc.first.isValid())
9010           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9011       }
9012     }
9013   }
9014 }
9015 
9016 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9017                                 bool IncludeSystem, bool Complain,
9018                     llvm::function_ref<void(const serialization::InputFile &IF,
9019                                             bool isSystem)> Visitor) {
9020   unsigned NumUserInputs = MF.NumUserInputFiles;
9021   unsigned NumInputs = MF.InputFilesLoaded.size();
9022   assert(NumUserInputs <= NumInputs);
9023   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9024   for (unsigned I = 0; I < N; ++I) {
9025     bool IsSystem = I >= NumUserInputs;
9026     InputFile IF = getInputFile(MF, I+1, Complain);
9027     Visitor(IF, IsSystem);
9028   }
9029 }
9030 
9031 void ASTReader::visitTopLevelModuleMaps(
9032     serialization::ModuleFile &MF,
9033     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9034   unsigned NumInputs = MF.InputFilesLoaded.size();
9035   for (unsigned I = 0; I < NumInputs; ++I) {
9036     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9037     if (IFI.TopLevelModuleMap)
9038       // FIXME: This unnecessarily re-reads the InputFileInfo.
9039       if (auto *FE = getInputFile(MF, I + 1).getFile())
9040         Visitor(FE);
9041   }
9042 }
9043 
9044 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9045   // If we know the owning module, use it.
9046   if (Module *M = D->getImportedOwningModule())
9047     return M->getFullModuleName();
9048 
9049   // Otherwise, use the name of the top-level module the decl is within.
9050   if (ModuleFile *M = getOwningModuleFile(D))
9051     return M->ModuleName;
9052 
9053   // Not from a module.
9054   return {};
9055 }
9056 
9057 void ASTReader::finishPendingActions() {
9058   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9059          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9060          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9061          !PendingUpdateRecords.empty()) {
9062     // If any identifiers with corresponding top-level declarations have
9063     // been loaded, load those declarations now.
9064     using TopLevelDeclsMap =
9065         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9066     TopLevelDeclsMap TopLevelDecls;
9067 
9068     while (!PendingIdentifierInfos.empty()) {
9069       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9070       SmallVector<uint32_t, 4> DeclIDs =
9071           std::move(PendingIdentifierInfos.back().second);
9072       PendingIdentifierInfos.pop_back();
9073 
9074       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9075     }
9076 
9077     // Load each function type that we deferred loading because it was a
9078     // deduced type that might refer to a local type declared within itself.
9079     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9080       auto *FD = PendingFunctionTypes[I].first;
9081       FD->setType(GetType(PendingFunctionTypes[I].second));
9082 
9083       // If we gave a function a deduced return type, remember that we need to
9084       // propagate that along the redeclaration chain.
9085       auto *DT = FD->getReturnType()->getContainedDeducedType();
9086       if (DT && DT->isDeduced())
9087         PendingDeducedTypeUpdates.insert(
9088             {FD->getCanonicalDecl(), FD->getReturnType()});
9089     }
9090     PendingFunctionTypes.clear();
9091 
9092     // For each decl chain that we wanted to complete while deserializing, mark
9093     // it as "still needs to be completed".
9094     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9095       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9096     }
9097     PendingIncompleteDeclChains.clear();
9098 
9099     // Load pending declaration chains.
9100     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9101       loadPendingDeclChain(PendingDeclChains[I].first,
9102                            PendingDeclChains[I].second);
9103     PendingDeclChains.clear();
9104 
9105     // Make the most recent of the top-level declarations visible.
9106     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9107            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9108       IdentifierInfo *II = TLD->first;
9109       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9110         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9111       }
9112     }
9113 
9114     // Load any pending macro definitions.
9115     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9116       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9117       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9118       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9119       // Initialize the macro history from chained-PCHs ahead of module imports.
9120       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9121            ++IDIdx) {
9122         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9123         if (!Info.M->isModule())
9124           resolvePendingMacro(II, Info);
9125       }
9126       // Handle module imports.
9127       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9128            ++IDIdx) {
9129         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9130         if (Info.M->isModule())
9131           resolvePendingMacro(II, Info);
9132       }
9133     }
9134     PendingMacroIDs.clear();
9135 
9136     // Wire up the DeclContexts for Decls that we delayed setting until
9137     // recursive loading is completed.
9138     while (!PendingDeclContextInfos.empty()) {
9139       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9140       PendingDeclContextInfos.pop_front();
9141       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9142       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9143       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9144     }
9145 
9146     // Perform any pending declaration updates.
9147     while (!PendingUpdateRecords.empty()) {
9148       auto Update = PendingUpdateRecords.pop_back_val();
9149       ReadingKindTracker ReadingKind(Read_Decl, *this);
9150       loadDeclUpdateRecords(Update);
9151     }
9152   }
9153 
9154   // At this point, all update records for loaded decls are in place, so any
9155   // fake class definitions should have become real.
9156   assert(PendingFakeDefinitionData.empty() &&
9157          "faked up a class definition but never saw the real one");
9158 
9159   // If we deserialized any C++ or Objective-C class definitions, any
9160   // Objective-C protocol definitions, or any redeclarable templates, make sure
9161   // that all redeclarations point to the definitions. Note that this can only
9162   // happen now, after the redeclaration chains have been fully wired.
9163   for (Decl *D : PendingDefinitions) {
9164     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9165       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9166         // Make sure that the TagType points at the definition.
9167         const_cast<TagType*>(TagT)->decl = TD;
9168       }
9169 
9170       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9171         for (auto *R = getMostRecentExistingDecl(RD); R;
9172              R = R->getPreviousDecl()) {
9173           assert((R == D) ==
9174                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9175                  "declaration thinks it's the definition but it isn't");
9176           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9177         }
9178       }
9179 
9180       continue;
9181     }
9182 
9183     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9184       // Make sure that the ObjCInterfaceType points at the definition.
9185       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9186         ->Decl = ID;
9187 
9188       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9189         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9190 
9191       continue;
9192     }
9193 
9194     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9195       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9196         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9197 
9198       continue;
9199     }
9200 
9201     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9202     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9203       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9204   }
9205   PendingDefinitions.clear();
9206 
9207   // Load the bodies of any functions or methods we've encountered. We do
9208   // this now (delayed) so that we can be sure that the declaration chains
9209   // have been fully wired up (hasBody relies on this).
9210   // FIXME: We shouldn't require complete redeclaration chains here.
9211   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9212                                PBEnd = PendingBodies.end();
9213        PB != PBEnd; ++PB) {
9214     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9215       // For a function defined inline within a class template, force the
9216       // canonical definition to be the one inside the canonical definition of
9217       // the template. This ensures that we instantiate from a correct view
9218       // of the template.
9219       //
9220       // Sadly we can't do this more generally: we can't be sure that all
9221       // copies of an arbitrary class definition will have the same members
9222       // defined (eg, some member functions may not be instantiated, and some
9223       // special members may or may not have been implicitly defined).
9224       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9225         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9226           continue;
9227 
9228       // FIXME: Check for =delete/=default?
9229       // FIXME: Complain about ODR violations here?
9230       const FunctionDecl *Defn = nullptr;
9231       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9232         FD->setLazyBody(PB->second);
9233       } else {
9234         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9235         mergeDefinitionVisibility(NonConstDefn, FD);
9236 
9237         if (!FD->isLateTemplateParsed() &&
9238             !NonConstDefn->isLateTemplateParsed() &&
9239             FD->getODRHash() != NonConstDefn->getODRHash()) {
9240           if (!isa<CXXMethodDecl>(FD)) {
9241             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9242           } else if (FD->getLexicalParent()->isFileContext() &&
9243                      NonConstDefn->getLexicalParent()->isFileContext()) {
9244             // Only diagnose out-of-line method definitions.  If they are
9245             // in class definitions, then an error will be generated when
9246             // processing the class bodies.
9247             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9248           }
9249         }
9250       }
9251       continue;
9252     }
9253 
9254     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9255     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9256       MD->setLazyBody(PB->second);
9257   }
9258   PendingBodies.clear();
9259 
9260   // Do some cleanup.
9261   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9262     getContext().deduplicateMergedDefinitonsFor(ND);
9263   PendingMergedDefinitionsToDeduplicate.clear();
9264 }
9265 
9266 void ASTReader::diagnoseOdrViolations() {
9267   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9268       PendingFunctionOdrMergeFailures.empty() &&
9269       PendingEnumOdrMergeFailures.empty())
9270     return;
9271 
9272   // Trigger the import of the full definition of each class that had any
9273   // odr-merging problems, so we can produce better diagnostics for them.
9274   // These updates may in turn find and diagnose some ODR failures, so take
9275   // ownership of the set first.
9276   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9277   PendingOdrMergeFailures.clear();
9278   for (auto &Merge : OdrMergeFailures) {
9279     Merge.first->buildLookup();
9280     Merge.first->decls_begin();
9281     Merge.first->bases_begin();
9282     Merge.first->vbases_begin();
9283     for (auto &RecordPair : Merge.second) {
9284       auto *RD = RecordPair.first;
9285       RD->decls_begin();
9286       RD->bases_begin();
9287       RD->vbases_begin();
9288     }
9289   }
9290 
9291   // Trigger the import of functions.
9292   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9293   PendingFunctionOdrMergeFailures.clear();
9294   for (auto &Merge : FunctionOdrMergeFailures) {
9295     Merge.first->buildLookup();
9296     Merge.first->decls_begin();
9297     Merge.first->getBody();
9298     for (auto &FD : Merge.second) {
9299       FD->buildLookup();
9300       FD->decls_begin();
9301       FD->getBody();
9302     }
9303   }
9304 
9305   // Trigger the import of enums.
9306   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9307   PendingEnumOdrMergeFailures.clear();
9308   for (auto &Merge : EnumOdrMergeFailures) {
9309     Merge.first->decls_begin();
9310     for (auto &Enum : Merge.second) {
9311       Enum->decls_begin();
9312     }
9313   }
9314 
9315   // For each declaration from a merged context, check that the canonical
9316   // definition of that context also contains a declaration of the same
9317   // entity.
9318   //
9319   // Caution: this loop does things that might invalidate iterators into
9320   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9321   while (!PendingOdrMergeChecks.empty()) {
9322     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9323 
9324     // FIXME: Skip over implicit declarations for now. This matters for things
9325     // like implicitly-declared special member functions. This isn't entirely
9326     // correct; we can end up with multiple unmerged declarations of the same
9327     // implicit entity.
9328     if (D->isImplicit())
9329       continue;
9330 
9331     DeclContext *CanonDef = D->getDeclContext();
9332 
9333     bool Found = false;
9334     const Decl *DCanon = D->getCanonicalDecl();
9335 
9336     for (auto RI : D->redecls()) {
9337       if (RI->getLexicalDeclContext() == CanonDef) {
9338         Found = true;
9339         break;
9340       }
9341     }
9342     if (Found)
9343       continue;
9344 
9345     // Quick check failed, time to do the slow thing. Note, we can't just
9346     // look up the name of D in CanonDef here, because the member that is
9347     // in CanonDef might not be found by name lookup (it might have been
9348     // replaced by a more recent declaration in the lookup table), and we
9349     // can't necessarily find it in the redeclaration chain because it might
9350     // be merely mergeable, not redeclarable.
9351     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9352     for (auto *CanonMember : CanonDef->decls()) {
9353       if (CanonMember->getCanonicalDecl() == DCanon) {
9354         // This can happen if the declaration is merely mergeable and not
9355         // actually redeclarable (we looked for redeclarations earlier).
9356         //
9357         // FIXME: We should be able to detect this more efficiently, without
9358         // pulling in all of the members of CanonDef.
9359         Found = true;
9360         break;
9361       }
9362       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9363         if (ND->getDeclName() == D->getDeclName())
9364           Candidates.push_back(ND);
9365     }
9366 
9367     if (!Found) {
9368       // The AST doesn't like TagDecls becoming invalid after they've been
9369       // completed. We only really need to mark FieldDecls as invalid here.
9370       if (!isa<TagDecl>(D))
9371         D->setInvalidDecl();
9372 
9373       // Ensure we don't accidentally recursively enter deserialization while
9374       // we're producing our diagnostic.
9375       Deserializing RecursionGuard(this);
9376 
9377       std::string CanonDefModule =
9378           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9379       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9380         << D << getOwningModuleNameForDiagnostic(D)
9381         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9382 
9383       if (Candidates.empty())
9384         Diag(cast<Decl>(CanonDef)->getLocation(),
9385              diag::note_module_odr_violation_no_possible_decls) << D;
9386       else {
9387         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9388           Diag(Candidates[I]->getLocation(),
9389                diag::note_module_odr_violation_possible_decl)
9390             << Candidates[I];
9391       }
9392 
9393       DiagnosedOdrMergeFailures.insert(CanonDef);
9394     }
9395   }
9396 
9397   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9398       EnumOdrMergeFailures.empty())
9399     return;
9400 
9401   // Ensure we don't accidentally recursively enter deserialization while
9402   // we're producing our diagnostics.
9403   Deserializing RecursionGuard(this);
9404 
9405   // Common code for hashing helpers.
9406   ODRHash Hash;
9407   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9408     Hash.clear();
9409     Hash.AddQualType(Ty);
9410     return Hash.CalculateHash();
9411   };
9412 
9413   auto ComputeODRHash = [&Hash](const Stmt *S) {
9414     assert(S);
9415     Hash.clear();
9416     Hash.AddStmt(S);
9417     return Hash.CalculateHash();
9418   };
9419 
9420   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9421     assert(D);
9422     Hash.clear();
9423     Hash.AddSubDecl(D);
9424     return Hash.CalculateHash();
9425   };
9426 
9427   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9428     Hash.clear();
9429     Hash.AddTemplateArgument(TA);
9430     return Hash.CalculateHash();
9431   };
9432 
9433   auto ComputeTemplateParameterListODRHash =
9434       [&Hash](const TemplateParameterList *TPL) {
9435         assert(TPL);
9436         Hash.clear();
9437         Hash.AddTemplateParameterList(TPL);
9438         return Hash.CalculateHash();
9439       };
9440 
9441   // Issue any pending ODR-failure diagnostics.
9442   for (auto &Merge : OdrMergeFailures) {
9443     // If we've already pointed out a specific problem with this class, don't
9444     // bother issuing a general "something's different" diagnostic.
9445     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9446       continue;
9447 
9448     bool Diagnosed = false;
9449     CXXRecordDecl *FirstRecord = Merge.first;
9450     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9451     for (auto &RecordPair : Merge.second) {
9452       CXXRecordDecl *SecondRecord = RecordPair.first;
9453       // Multiple different declarations got merged together; tell the user
9454       // where they came from.
9455       if (FirstRecord == SecondRecord)
9456         continue;
9457 
9458       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9459 
9460       auto *FirstDD = FirstRecord->DefinitionData;
9461       auto *SecondDD = RecordPair.second;
9462 
9463       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9464 
9465       // Diagnostics from DefinitionData are emitted here.
9466       if (FirstDD != SecondDD) {
9467         enum ODRDefinitionDataDifference {
9468           NumBases,
9469           NumVBases,
9470           BaseType,
9471           BaseVirtual,
9472           BaseAccess,
9473         };
9474         auto ODRDiagError = [FirstRecord, &FirstModule,
9475                              this](SourceLocation Loc, SourceRange Range,
9476                                    ODRDefinitionDataDifference DiffType) {
9477           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9478                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9479                  << DiffType;
9480         };
9481         auto ODRDiagNote = [&SecondModule,
9482                             this](SourceLocation Loc, SourceRange Range,
9483                                   ODRDefinitionDataDifference DiffType) {
9484           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9485                  << SecondModule << Range << DiffType;
9486         };
9487 
9488         unsigned FirstNumBases = FirstDD->NumBases;
9489         unsigned FirstNumVBases = FirstDD->NumVBases;
9490         unsigned SecondNumBases = SecondDD->NumBases;
9491         unsigned SecondNumVBases = SecondDD->NumVBases;
9492 
9493         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9494           unsigned NumBases = DD->NumBases;
9495           if (NumBases == 0) return SourceRange();
9496           auto bases = DD->bases();
9497           return SourceRange(bases[0].getBeginLoc(),
9498                              bases[NumBases - 1].getEndLoc());
9499         };
9500 
9501         if (FirstNumBases != SecondNumBases) {
9502           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9503                        NumBases)
9504               << FirstNumBases;
9505           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9506                       NumBases)
9507               << SecondNumBases;
9508           Diagnosed = true;
9509           break;
9510         }
9511 
9512         if (FirstNumVBases != SecondNumVBases) {
9513           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9514                        NumVBases)
9515               << FirstNumVBases;
9516           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9517                       NumVBases)
9518               << SecondNumVBases;
9519           Diagnosed = true;
9520           break;
9521         }
9522 
9523         auto FirstBases = FirstDD->bases();
9524         auto SecondBases = SecondDD->bases();
9525         unsigned i = 0;
9526         for (i = 0; i < FirstNumBases; ++i) {
9527           auto FirstBase = FirstBases[i];
9528           auto SecondBase = SecondBases[i];
9529           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9530               ComputeQualTypeODRHash(SecondBase.getType())) {
9531             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9532                          BaseType)
9533                 << (i + 1) << FirstBase.getType();
9534             ODRDiagNote(SecondRecord->getLocation(),
9535                         SecondBase.getSourceRange(), BaseType)
9536                 << (i + 1) << SecondBase.getType();
9537             break;
9538           }
9539 
9540           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9541             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9542                          BaseVirtual)
9543                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9544             ODRDiagNote(SecondRecord->getLocation(),
9545                         SecondBase.getSourceRange(), BaseVirtual)
9546                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9547             break;
9548           }
9549 
9550           if (FirstBase.getAccessSpecifierAsWritten() !=
9551               SecondBase.getAccessSpecifierAsWritten()) {
9552             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9553                          BaseAccess)
9554                 << (i + 1) << FirstBase.getType()
9555                 << (int)FirstBase.getAccessSpecifierAsWritten();
9556             ODRDiagNote(SecondRecord->getLocation(),
9557                         SecondBase.getSourceRange(), BaseAccess)
9558                 << (i + 1) << SecondBase.getType()
9559                 << (int)SecondBase.getAccessSpecifierAsWritten();
9560             break;
9561           }
9562         }
9563 
9564         if (i != FirstNumBases) {
9565           Diagnosed = true;
9566           break;
9567         }
9568       }
9569 
9570       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9571 
9572       const ClassTemplateDecl *FirstTemplate =
9573           FirstRecord->getDescribedClassTemplate();
9574       const ClassTemplateDecl *SecondTemplate =
9575           SecondRecord->getDescribedClassTemplate();
9576 
9577       assert(!FirstTemplate == !SecondTemplate &&
9578              "Both pointers should be null or non-null");
9579 
9580       enum ODRTemplateDifference {
9581         ParamEmptyName,
9582         ParamName,
9583         ParamSingleDefaultArgument,
9584         ParamDifferentDefaultArgument,
9585       };
9586 
9587       if (FirstTemplate && SecondTemplate) {
9588         DeclHashes FirstTemplateHashes;
9589         DeclHashes SecondTemplateHashes;
9590 
9591         auto PopulateTemplateParameterHashs =
9592             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9593                                      const ClassTemplateDecl *TD) {
9594               for (auto *D : TD->getTemplateParameters()->asArray()) {
9595                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9596               }
9597             };
9598 
9599         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
9600         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
9601 
9602         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
9603                "Number of template parameters should be equal.");
9604 
9605         auto FirstIt = FirstTemplateHashes.begin();
9606         auto FirstEnd = FirstTemplateHashes.end();
9607         auto SecondIt = SecondTemplateHashes.begin();
9608         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
9609           if (FirstIt->second == SecondIt->second)
9610             continue;
9611 
9612           auto ODRDiagError = [FirstRecord, &FirstModule,
9613                                this](SourceLocation Loc, SourceRange Range,
9614                                      ODRTemplateDifference DiffType) {
9615             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
9616                    << FirstRecord << FirstModule.empty() << FirstModule << Range
9617                    << DiffType;
9618           };
9619           auto ODRDiagNote = [&SecondModule,
9620                               this](SourceLocation Loc, SourceRange Range,
9621                                     ODRTemplateDifference DiffType) {
9622             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
9623                    << SecondModule << Range << DiffType;
9624           };
9625 
9626           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
9627           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
9628 
9629           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
9630                  "Parameter Decl's should be the same kind.");
9631 
9632           DeclarationName FirstName = FirstDecl->getDeclName();
9633           DeclarationName SecondName = SecondDecl->getDeclName();
9634 
9635           if (FirstName != SecondName) {
9636             const bool FirstNameEmpty =
9637                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
9638             const bool SecondNameEmpty =
9639                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
9640             assert((!FirstNameEmpty || !SecondNameEmpty) &&
9641                    "Both template parameters cannot be unnamed.");
9642             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9643                          FirstNameEmpty ? ParamEmptyName : ParamName)
9644                 << FirstName;
9645             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9646                         SecondNameEmpty ? ParamEmptyName : ParamName)
9647                 << SecondName;
9648             break;
9649           }
9650 
9651           switch (FirstDecl->getKind()) {
9652           default:
9653             llvm_unreachable("Invalid template parameter type.");
9654           case Decl::TemplateTypeParm: {
9655             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
9656             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
9657             const bool HasFirstDefaultArgument =
9658                 FirstParam->hasDefaultArgument() &&
9659                 !FirstParam->defaultArgumentWasInherited();
9660             const bool HasSecondDefaultArgument =
9661                 SecondParam->hasDefaultArgument() &&
9662                 !SecondParam->defaultArgumentWasInherited();
9663 
9664             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9665               ODRDiagError(FirstDecl->getLocation(),
9666                            FirstDecl->getSourceRange(),
9667                            ParamSingleDefaultArgument)
9668                   << HasFirstDefaultArgument;
9669               ODRDiagNote(SecondDecl->getLocation(),
9670                           SecondDecl->getSourceRange(),
9671                           ParamSingleDefaultArgument)
9672                   << HasSecondDefaultArgument;
9673               break;
9674             }
9675 
9676             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9677                    "Expecting default arguments.");
9678 
9679             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9680                          ParamDifferentDefaultArgument);
9681             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9682                         ParamDifferentDefaultArgument);
9683 
9684             break;
9685           }
9686           case Decl::NonTypeTemplateParm: {
9687             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
9688             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
9689             const bool HasFirstDefaultArgument =
9690                 FirstParam->hasDefaultArgument() &&
9691                 !FirstParam->defaultArgumentWasInherited();
9692             const bool HasSecondDefaultArgument =
9693                 SecondParam->hasDefaultArgument() &&
9694                 !SecondParam->defaultArgumentWasInherited();
9695 
9696             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9697               ODRDiagError(FirstDecl->getLocation(),
9698                            FirstDecl->getSourceRange(),
9699                            ParamSingleDefaultArgument)
9700                   << HasFirstDefaultArgument;
9701               ODRDiagNote(SecondDecl->getLocation(),
9702                           SecondDecl->getSourceRange(),
9703                           ParamSingleDefaultArgument)
9704                   << HasSecondDefaultArgument;
9705               break;
9706             }
9707 
9708             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9709                    "Expecting default arguments.");
9710 
9711             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9712                          ParamDifferentDefaultArgument);
9713             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9714                         ParamDifferentDefaultArgument);
9715 
9716             break;
9717           }
9718           case Decl::TemplateTemplateParm: {
9719             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
9720             const auto *SecondParam =
9721                 cast<TemplateTemplateParmDecl>(SecondDecl);
9722             const bool HasFirstDefaultArgument =
9723                 FirstParam->hasDefaultArgument() &&
9724                 !FirstParam->defaultArgumentWasInherited();
9725             const bool HasSecondDefaultArgument =
9726                 SecondParam->hasDefaultArgument() &&
9727                 !SecondParam->defaultArgumentWasInherited();
9728 
9729             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9730               ODRDiagError(FirstDecl->getLocation(),
9731                            FirstDecl->getSourceRange(),
9732                            ParamSingleDefaultArgument)
9733                   << HasFirstDefaultArgument;
9734               ODRDiagNote(SecondDecl->getLocation(),
9735                           SecondDecl->getSourceRange(),
9736                           ParamSingleDefaultArgument)
9737                   << HasSecondDefaultArgument;
9738               break;
9739             }
9740 
9741             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9742                    "Expecting default arguments.");
9743 
9744             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9745                          ParamDifferentDefaultArgument);
9746             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9747                         ParamDifferentDefaultArgument);
9748 
9749             break;
9750           }
9751           }
9752 
9753           break;
9754         }
9755 
9756         if (FirstIt != FirstEnd) {
9757           Diagnosed = true;
9758           break;
9759         }
9760       }
9761 
9762       DeclHashes FirstHashes;
9763       DeclHashes SecondHashes;
9764 
9765       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
9766                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
9767         for (auto *D : Record->decls()) {
9768           // Due to decl merging, the first CXXRecordDecl is the parent of
9769           // Decls in both records.
9770           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
9771             continue;
9772           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9773         }
9774       };
9775       PopulateHashes(FirstHashes, FirstRecord);
9776       PopulateHashes(SecondHashes, SecondRecord);
9777 
9778       // Used with err_module_odr_violation_mismatch_decl and
9779       // note_module_odr_violation_mismatch_decl
9780       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9781       enum {
9782         EndOfClass,
9783         PublicSpecifer,
9784         PrivateSpecifer,
9785         ProtectedSpecifer,
9786         StaticAssert,
9787         Field,
9788         CXXMethod,
9789         TypeAlias,
9790         TypeDef,
9791         Var,
9792         Friend,
9793         FunctionTemplate,
9794         Other
9795       } FirstDiffType = Other,
9796         SecondDiffType = Other;
9797 
9798       auto DifferenceSelector = [](Decl *D) {
9799         assert(D && "valid Decl required");
9800         switch (D->getKind()) {
9801         default:
9802           return Other;
9803         case Decl::AccessSpec:
9804           switch (D->getAccess()) {
9805           case AS_public:
9806             return PublicSpecifer;
9807           case AS_private:
9808             return PrivateSpecifer;
9809           case AS_protected:
9810             return ProtectedSpecifer;
9811           case AS_none:
9812             break;
9813           }
9814           llvm_unreachable("Invalid access specifier");
9815         case Decl::StaticAssert:
9816           return StaticAssert;
9817         case Decl::Field:
9818           return Field;
9819         case Decl::CXXMethod:
9820         case Decl::CXXConstructor:
9821         case Decl::CXXDestructor:
9822           return CXXMethod;
9823         case Decl::TypeAlias:
9824           return TypeAlias;
9825         case Decl::Typedef:
9826           return TypeDef;
9827         case Decl::Var:
9828           return Var;
9829         case Decl::Friend:
9830           return Friend;
9831         case Decl::FunctionTemplate:
9832           return FunctionTemplate;
9833         }
9834       };
9835 
9836       Decl *FirstDecl = nullptr;
9837       Decl *SecondDecl = nullptr;
9838       auto FirstIt = FirstHashes.begin();
9839       auto SecondIt = SecondHashes.begin();
9840 
9841       // If there is a diagnoseable difference, FirstDiffType and
9842       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9843       // filled in if not EndOfClass.
9844       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9845         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9846             FirstIt->second == SecondIt->second) {
9847           ++FirstIt;
9848           ++SecondIt;
9849           continue;
9850         }
9851 
9852         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9853         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9854 
9855         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
9856         SecondDiffType =
9857             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
9858 
9859         break;
9860       }
9861 
9862       if (FirstDiffType == Other || SecondDiffType == Other) {
9863         // Reaching this point means an unexpected Decl was encountered
9864         // or no difference was detected.  This causes a generic error
9865         // message to be emitted.
9866         Diag(FirstRecord->getLocation(),
9867              diag::err_module_odr_violation_different_definitions)
9868             << FirstRecord << FirstModule.empty() << FirstModule;
9869 
9870         if (FirstDecl) {
9871           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
9872               << FirstRecord << FirstDecl->getSourceRange();
9873         }
9874 
9875         Diag(SecondRecord->getLocation(),
9876              diag::note_module_odr_violation_different_definitions)
9877             << SecondModule;
9878 
9879         if (SecondDecl) {
9880           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
9881               << SecondDecl->getSourceRange();
9882         }
9883 
9884         Diagnosed = true;
9885         break;
9886       }
9887 
9888       if (FirstDiffType != SecondDiffType) {
9889         SourceLocation FirstLoc;
9890         SourceRange FirstRange;
9891         if (FirstDiffType == EndOfClass) {
9892           FirstLoc = FirstRecord->getBraceRange().getEnd();
9893         } else {
9894           FirstLoc = FirstIt->first->getLocation();
9895           FirstRange = FirstIt->first->getSourceRange();
9896         }
9897         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9898             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9899             << FirstDiffType;
9900 
9901         SourceLocation SecondLoc;
9902         SourceRange SecondRange;
9903         if (SecondDiffType == EndOfClass) {
9904           SecondLoc = SecondRecord->getBraceRange().getEnd();
9905         } else {
9906           SecondLoc = SecondDecl->getLocation();
9907           SecondRange = SecondDecl->getSourceRange();
9908         }
9909         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9910             << SecondModule << SecondRange << SecondDiffType;
9911         Diagnosed = true;
9912         break;
9913       }
9914 
9915       assert(FirstDiffType == SecondDiffType);
9916 
9917       // Used with err_module_odr_violation_mismatch_decl_diff and
9918       // note_module_odr_violation_mismatch_decl_diff
9919       enum ODRDeclDifference {
9920         StaticAssertCondition,
9921         StaticAssertMessage,
9922         StaticAssertOnlyMessage,
9923         FieldName,
9924         FieldTypeName,
9925         FieldSingleBitField,
9926         FieldDifferentWidthBitField,
9927         FieldSingleMutable,
9928         FieldSingleInitializer,
9929         FieldDifferentInitializers,
9930         MethodName,
9931         MethodDeleted,
9932         MethodDefaulted,
9933         MethodVirtual,
9934         MethodStatic,
9935         MethodVolatile,
9936         MethodConst,
9937         MethodInline,
9938         MethodNumberParameters,
9939         MethodParameterType,
9940         MethodParameterName,
9941         MethodParameterSingleDefaultArgument,
9942         MethodParameterDifferentDefaultArgument,
9943         MethodNoTemplateArguments,
9944         MethodDifferentNumberTemplateArguments,
9945         MethodDifferentTemplateArgument,
9946         MethodSingleBody,
9947         MethodDifferentBody,
9948         TypedefName,
9949         TypedefType,
9950         VarName,
9951         VarType,
9952         VarSingleInitializer,
9953         VarDifferentInitializer,
9954         VarConstexpr,
9955         FriendTypeFunction,
9956         FriendType,
9957         FriendFunction,
9958         FunctionTemplateDifferentNumberParameters,
9959         FunctionTemplateParameterDifferentKind,
9960         FunctionTemplateParameterName,
9961         FunctionTemplateParameterSingleDefaultArgument,
9962         FunctionTemplateParameterDifferentDefaultArgument,
9963         FunctionTemplateParameterDifferentType,
9964         FunctionTemplatePackParameter,
9965       };
9966 
9967       // These lambdas have the common portions of the ODR diagnostics.  This
9968       // has the same return as Diag(), so addition parameters can be passed
9969       // in with operator<<
9970       auto ODRDiagError = [FirstRecord, &FirstModule, this](
9971           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
9972         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9973                << FirstRecord << FirstModule.empty() << FirstModule << Range
9974                << DiffType;
9975       };
9976       auto ODRDiagNote = [&SecondModule, this](
9977           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
9978         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9979                << SecondModule << Range << DiffType;
9980       };
9981 
9982       switch (FirstDiffType) {
9983       case Other:
9984       case EndOfClass:
9985       case PublicSpecifer:
9986       case PrivateSpecifer:
9987       case ProtectedSpecifer:
9988         llvm_unreachable("Invalid diff type");
9989 
9990       case StaticAssert: {
9991         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
9992         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
9993 
9994         Expr *FirstExpr = FirstSA->getAssertExpr();
9995         Expr *SecondExpr = SecondSA->getAssertExpr();
9996         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
9997         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
9998         if (FirstODRHash != SecondODRHash) {
9999           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10000                        StaticAssertCondition);
10001           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10002                       StaticAssertCondition);
10003           Diagnosed = true;
10004           break;
10005         }
10006 
10007         StringLiteral *FirstStr = FirstSA->getMessage();
10008         StringLiteral *SecondStr = SecondSA->getMessage();
10009         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10010         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10011           SourceLocation FirstLoc, SecondLoc;
10012           SourceRange FirstRange, SecondRange;
10013           if (FirstStr) {
10014             FirstLoc = FirstStr->getBeginLoc();
10015             FirstRange = FirstStr->getSourceRange();
10016           } else {
10017             FirstLoc = FirstSA->getBeginLoc();
10018             FirstRange = FirstSA->getSourceRange();
10019           }
10020           if (SecondStr) {
10021             SecondLoc = SecondStr->getBeginLoc();
10022             SecondRange = SecondStr->getSourceRange();
10023           } else {
10024             SecondLoc = SecondSA->getBeginLoc();
10025             SecondRange = SecondSA->getSourceRange();
10026           }
10027           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10028               << (FirstStr == nullptr);
10029           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10030               << (SecondStr == nullptr);
10031           Diagnosed = true;
10032           break;
10033         }
10034 
10035         if (FirstStr && SecondStr &&
10036             FirstStr->getString() != SecondStr->getString()) {
10037           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10038                        StaticAssertMessage);
10039           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10040                       StaticAssertMessage);
10041           Diagnosed = true;
10042           break;
10043         }
10044         break;
10045       }
10046       case Field: {
10047         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10048         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10049         IdentifierInfo *FirstII = FirstField->getIdentifier();
10050         IdentifierInfo *SecondII = SecondField->getIdentifier();
10051         if (FirstII->getName() != SecondII->getName()) {
10052           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10053                        FieldName)
10054               << FirstII;
10055           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10056                       FieldName)
10057               << SecondII;
10058 
10059           Diagnosed = true;
10060           break;
10061         }
10062 
10063         assert(getContext().hasSameType(FirstField->getType(),
10064                                         SecondField->getType()));
10065 
10066         QualType FirstType = FirstField->getType();
10067         QualType SecondType = SecondField->getType();
10068         if (ComputeQualTypeODRHash(FirstType) !=
10069             ComputeQualTypeODRHash(SecondType)) {
10070           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10071                        FieldTypeName)
10072               << FirstII << FirstType;
10073           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10074                       FieldTypeName)
10075               << SecondII << SecondType;
10076 
10077           Diagnosed = true;
10078           break;
10079         }
10080 
10081         const bool IsFirstBitField = FirstField->isBitField();
10082         const bool IsSecondBitField = SecondField->isBitField();
10083         if (IsFirstBitField != IsSecondBitField) {
10084           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10085                        FieldSingleBitField)
10086               << FirstII << IsFirstBitField;
10087           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10088                       FieldSingleBitField)
10089               << SecondII << IsSecondBitField;
10090           Diagnosed = true;
10091           break;
10092         }
10093 
10094         if (IsFirstBitField && IsSecondBitField) {
10095           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10096                        FieldDifferentWidthBitField)
10097               << FirstII << FirstField->getBitWidth()->getSourceRange();
10098           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10099                       FieldDifferentWidthBitField)
10100               << SecondII << SecondField->getBitWidth()->getSourceRange();
10101           Diagnosed = true;
10102           break;
10103         }
10104 
10105         const bool IsFirstMutable = FirstField->isMutable();
10106         const bool IsSecondMutable = SecondField->isMutable();
10107         if (IsFirstMutable != IsSecondMutable) {
10108           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10109                        FieldSingleMutable)
10110               << FirstII << IsFirstMutable;
10111           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10112                       FieldSingleMutable)
10113               << SecondII << IsSecondMutable;
10114           Diagnosed = true;
10115           break;
10116         }
10117 
10118         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10119         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10120         if ((!FirstInitializer && SecondInitializer) ||
10121             (FirstInitializer && !SecondInitializer)) {
10122           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10123                        FieldSingleInitializer)
10124               << FirstII << (FirstInitializer != nullptr);
10125           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10126                       FieldSingleInitializer)
10127               << SecondII << (SecondInitializer != nullptr);
10128           Diagnosed = true;
10129           break;
10130         }
10131 
10132         if (FirstInitializer && SecondInitializer) {
10133           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10134           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10135           if (FirstInitHash != SecondInitHash) {
10136             ODRDiagError(FirstField->getLocation(),
10137                          FirstField->getSourceRange(),
10138                          FieldDifferentInitializers)
10139                 << FirstII << FirstInitializer->getSourceRange();
10140             ODRDiagNote(SecondField->getLocation(),
10141                         SecondField->getSourceRange(),
10142                         FieldDifferentInitializers)
10143                 << SecondII << SecondInitializer->getSourceRange();
10144             Diagnosed = true;
10145             break;
10146           }
10147         }
10148 
10149         break;
10150       }
10151       case CXXMethod: {
10152         enum {
10153           DiagMethod,
10154           DiagConstructor,
10155           DiagDestructor,
10156         } FirstMethodType,
10157             SecondMethodType;
10158         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10159           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10160           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10161           return DiagMethod;
10162         };
10163         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10164         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10165         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10166         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10167         auto FirstName = FirstMethod->getDeclName();
10168         auto SecondName = SecondMethod->getDeclName();
10169         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10170           ODRDiagError(FirstMethod->getLocation(),
10171                        FirstMethod->getSourceRange(), MethodName)
10172               << FirstMethodType << FirstName;
10173           ODRDiagNote(SecondMethod->getLocation(),
10174                       SecondMethod->getSourceRange(), MethodName)
10175               << SecondMethodType << SecondName;
10176 
10177           Diagnosed = true;
10178           break;
10179         }
10180 
10181         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10182         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10183         if (FirstDeleted != SecondDeleted) {
10184           ODRDiagError(FirstMethod->getLocation(),
10185                        FirstMethod->getSourceRange(), MethodDeleted)
10186               << FirstMethodType << FirstName << FirstDeleted;
10187 
10188           ODRDiagNote(SecondMethod->getLocation(),
10189                       SecondMethod->getSourceRange(), MethodDeleted)
10190               << SecondMethodType << SecondName << SecondDeleted;
10191           Diagnosed = true;
10192           break;
10193         }
10194 
10195         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10196         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10197         if (FirstDefaulted != SecondDefaulted) {
10198           ODRDiagError(FirstMethod->getLocation(),
10199                        FirstMethod->getSourceRange(), MethodDefaulted)
10200               << FirstMethodType << FirstName << FirstDefaulted;
10201 
10202           ODRDiagNote(SecondMethod->getLocation(),
10203                       SecondMethod->getSourceRange(), MethodDefaulted)
10204               << SecondMethodType << SecondName << SecondDefaulted;
10205           Diagnosed = true;
10206           break;
10207         }
10208 
10209         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10210         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10211         const bool FirstPure = FirstMethod->isPure();
10212         const bool SecondPure = SecondMethod->isPure();
10213         if ((FirstVirtual || SecondVirtual) &&
10214             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10215           ODRDiagError(FirstMethod->getLocation(),
10216                        FirstMethod->getSourceRange(), MethodVirtual)
10217               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10218           ODRDiagNote(SecondMethod->getLocation(),
10219                       SecondMethod->getSourceRange(), MethodVirtual)
10220               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10221           Diagnosed = true;
10222           break;
10223         }
10224 
10225         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10226         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10227         // class needs to be checked instead.
10228         const auto FirstStorage = FirstMethod->getStorageClass();
10229         const auto SecondStorage = SecondMethod->getStorageClass();
10230         const bool FirstStatic = FirstStorage == SC_Static;
10231         const bool SecondStatic = SecondStorage == SC_Static;
10232         if (FirstStatic != SecondStatic) {
10233           ODRDiagError(FirstMethod->getLocation(),
10234                        FirstMethod->getSourceRange(), MethodStatic)
10235               << FirstMethodType << FirstName << FirstStatic;
10236           ODRDiagNote(SecondMethod->getLocation(),
10237                       SecondMethod->getSourceRange(), MethodStatic)
10238               << SecondMethodType << SecondName << SecondStatic;
10239           Diagnosed = true;
10240           break;
10241         }
10242 
10243         const bool FirstVolatile = FirstMethod->isVolatile();
10244         const bool SecondVolatile = SecondMethod->isVolatile();
10245         if (FirstVolatile != SecondVolatile) {
10246           ODRDiagError(FirstMethod->getLocation(),
10247                        FirstMethod->getSourceRange(), MethodVolatile)
10248               << FirstMethodType << FirstName << FirstVolatile;
10249           ODRDiagNote(SecondMethod->getLocation(),
10250                       SecondMethod->getSourceRange(), MethodVolatile)
10251               << SecondMethodType << SecondName << SecondVolatile;
10252           Diagnosed = true;
10253           break;
10254         }
10255 
10256         const bool FirstConst = FirstMethod->isConst();
10257         const bool SecondConst = SecondMethod->isConst();
10258         if (FirstConst != SecondConst) {
10259           ODRDiagError(FirstMethod->getLocation(),
10260                        FirstMethod->getSourceRange(), MethodConst)
10261               << FirstMethodType << FirstName << FirstConst;
10262           ODRDiagNote(SecondMethod->getLocation(),
10263                       SecondMethod->getSourceRange(), MethodConst)
10264               << SecondMethodType << SecondName << SecondConst;
10265           Diagnosed = true;
10266           break;
10267         }
10268 
10269         const bool FirstInline = FirstMethod->isInlineSpecified();
10270         const bool SecondInline = SecondMethod->isInlineSpecified();
10271         if (FirstInline != SecondInline) {
10272           ODRDiagError(FirstMethod->getLocation(),
10273                        FirstMethod->getSourceRange(), MethodInline)
10274               << FirstMethodType << FirstName << FirstInline;
10275           ODRDiagNote(SecondMethod->getLocation(),
10276                       SecondMethod->getSourceRange(), MethodInline)
10277               << SecondMethodType << SecondName << SecondInline;
10278           Diagnosed = true;
10279           break;
10280         }
10281 
10282         const unsigned FirstNumParameters = FirstMethod->param_size();
10283         const unsigned SecondNumParameters = SecondMethod->param_size();
10284         if (FirstNumParameters != SecondNumParameters) {
10285           ODRDiagError(FirstMethod->getLocation(),
10286                        FirstMethod->getSourceRange(), MethodNumberParameters)
10287               << FirstMethodType << FirstName << FirstNumParameters;
10288           ODRDiagNote(SecondMethod->getLocation(),
10289                       SecondMethod->getSourceRange(), MethodNumberParameters)
10290               << SecondMethodType << SecondName << SecondNumParameters;
10291           Diagnosed = true;
10292           break;
10293         }
10294 
10295         // Need this status boolean to know when break out of the switch.
10296         bool ParameterMismatch = false;
10297         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10298           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10299           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10300 
10301           QualType FirstParamType = FirstParam->getType();
10302           QualType SecondParamType = SecondParam->getType();
10303           if (FirstParamType != SecondParamType &&
10304               ComputeQualTypeODRHash(FirstParamType) !=
10305                   ComputeQualTypeODRHash(SecondParamType)) {
10306             if (const DecayedType *ParamDecayedType =
10307                     FirstParamType->getAs<DecayedType>()) {
10308               ODRDiagError(FirstMethod->getLocation(),
10309                            FirstMethod->getSourceRange(), MethodParameterType)
10310                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10311                   << true << ParamDecayedType->getOriginalType();
10312             } else {
10313               ODRDiagError(FirstMethod->getLocation(),
10314                            FirstMethod->getSourceRange(), MethodParameterType)
10315                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10316                   << false;
10317             }
10318 
10319             if (const DecayedType *ParamDecayedType =
10320                     SecondParamType->getAs<DecayedType>()) {
10321               ODRDiagNote(SecondMethod->getLocation(),
10322                           SecondMethod->getSourceRange(), MethodParameterType)
10323                   << SecondMethodType << SecondName << (I + 1)
10324                   << SecondParamType << true
10325                   << ParamDecayedType->getOriginalType();
10326             } else {
10327               ODRDiagNote(SecondMethod->getLocation(),
10328                           SecondMethod->getSourceRange(), MethodParameterType)
10329                   << SecondMethodType << SecondName << (I + 1)
10330                   << SecondParamType << false;
10331             }
10332             ParameterMismatch = true;
10333             break;
10334           }
10335 
10336           DeclarationName FirstParamName = FirstParam->getDeclName();
10337           DeclarationName SecondParamName = SecondParam->getDeclName();
10338           if (FirstParamName != SecondParamName) {
10339             ODRDiagError(FirstMethod->getLocation(),
10340                          FirstMethod->getSourceRange(), MethodParameterName)
10341                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10342             ODRDiagNote(SecondMethod->getLocation(),
10343                         SecondMethod->getSourceRange(), MethodParameterName)
10344                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10345             ParameterMismatch = true;
10346             break;
10347           }
10348 
10349           const Expr *FirstInit = FirstParam->getInit();
10350           const Expr *SecondInit = SecondParam->getInit();
10351           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10352             ODRDiagError(FirstMethod->getLocation(),
10353                          FirstMethod->getSourceRange(),
10354                          MethodParameterSingleDefaultArgument)
10355                 << FirstMethodType << FirstName << (I + 1)
10356                 << (FirstInit == nullptr)
10357                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10358             ODRDiagNote(SecondMethod->getLocation(),
10359                         SecondMethod->getSourceRange(),
10360                         MethodParameterSingleDefaultArgument)
10361                 << SecondMethodType << SecondName << (I + 1)
10362                 << (SecondInit == nullptr)
10363                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10364             ParameterMismatch = true;
10365             break;
10366           }
10367 
10368           if (FirstInit && SecondInit &&
10369               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10370             ODRDiagError(FirstMethod->getLocation(),
10371                          FirstMethod->getSourceRange(),
10372                          MethodParameterDifferentDefaultArgument)
10373                 << FirstMethodType << FirstName << (I + 1)
10374                 << FirstInit->getSourceRange();
10375             ODRDiagNote(SecondMethod->getLocation(),
10376                         SecondMethod->getSourceRange(),
10377                         MethodParameterDifferentDefaultArgument)
10378                 << SecondMethodType << SecondName << (I + 1)
10379                 << SecondInit->getSourceRange();
10380             ParameterMismatch = true;
10381             break;
10382 
10383           }
10384         }
10385 
10386         if (ParameterMismatch) {
10387           Diagnosed = true;
10388           break;
10389         }
10390 
10391         const auto *FirstTemplateArgs =
10392             FirstMethod->getTemplateSpecializationArgs();
10393         const auto *SecondTemplateArgs =
10394             SecondMethod->getTemplateSpecializationArgs();
10395 
10396         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10397             (!FirstTemplateArgs && SecondTemplateArgs)) {
10398           ODRDiagError(FirstMethod->getLocation(),
10399                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
10400               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10401           ODRDiagNote(SecondMethod->getLocation(),
10402                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
10403               << SecondMethodType << SecondName
10404               << (SecondTemplateArgs != nullptr);
10405 
10406           Diagnosed = true;
10407           break;
10408         }
10409 
10410         if (FirstTemplateArgs && SecondTemplateArgs) {
10411           // Remove pack expansions from argument list.
10412           auto ExpandTemplateArgumentList =
10413               [](const TemplateArgumentList *TAL) {
10414                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10415                 for (const TemplateArgument &TA : TAL->asArray()) {
10416                   if (TA.getKind() != TemplateArgument::Pack) {
10417                     ExpandedList.push_back(&TA);
10418                     continue;
10419                   }
10420                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10421                     ExpandedList.push_back(&PackTA);
10422                   }
10423                 }
10424                 return ExpandedList;
10425               };
10426           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10427               ExpandTemplateArgumentList(FirstTemplateArgs);
10428           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10429               ExpandTemplateArgumentList(SecondTemplateArgs);
10430 
10431           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10432             ODRDiagError(FirstMethod->getLocation(),
10433                          FirstMethod->getSourceRange(),
10434                          MethodDifferentNumberTemplateArguments)
10435                 << FirstMethodType << FirstName
10436                 << (unsigned)FirstExpandedList.size();
10437             ODRDiagNote(SecondMethod->getLocation(),
10438                         SecondMethod->getSourceRange(),
10439                         MethodDifferentNumberTemplateArguments)
10440                 << SecondMethodType << SecondName
10441                 << (unsigned)SecondExpandedList.size();
10442 
10443             Diagnosed = true;
10444             break;
10445           }
10446 
10447           bool TemplateArgumentMismatch = false;
10448           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10449             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10450                                    &SecondTA = *SecondExpandedList[i];
10451             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10452                 ComputeTemplateArgumentODRHash(SecondTA)) {
10453               continue;
10454             }
10455 
10456             ODRDiagError(FirstMethod->getLocation(),
10457                          FirstMethod->getSourceRange(),
10458                          MethodDifferentTemplateArgument)
10459                 << FirstMethodType << FirstName << FirstTA << i + 1;
10460             ODRDiagNote(SecondMethod->getLocation(),
10461                         SecondMethod->getSourceRange(),
10462                         MethodDifferentTemplateArgument)
10463                 << SecondMethodType << SecondName << SecondTA << i + 1;
10464 
10465             TemplateArgumentMismatch = true;
10466             break;
10467           }
10468 
10469           if (TemplateArgumentMismatch) {
10470             Diagnosed = true;
10471             break;
10472           }
10473         }
10474 
10475         // Compute the hash of the method as if it has no body.
10476         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10477           Hash.clear();
10478           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10479           return Hash.CalculateHash();
10480         };
10481 
10482         // Compare the hash generated to the hash stored.  A difference means
10483         // that a body was present in the original source.  Due to merging,
10484         // the stardard way of detecting a body will not work.
10485         const bool HasFirstBody =
10486             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10487         const bool HasSecondBody =
10488             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10489 
10490         if (HasFirstBody != HasSecondBody) {
10491           ODRDiagError(FirstMethod->getLocation(),
10492                        FirstMethod->getSourceRange(), MethodSingleBody)
10493               << FirstMethodType << FirstName << HasFirstBody;
10494           ODRDiagNote(SecondMethod->getLocation(),
10495                       SecondMethod->getSourceRange(), MethodSingleBody)
10496               << SecondMethodType << SecondName << HasSecondBody;
10497           Diagnosed = true;
10498           break;
10499         }
10500 
10501         if (HasFirstBody && HasSecondBody) {
10502           ODRDiagError(FirstMethod->getLocation(),
10503                        FirstMethod->getSourceRange(), MethodDifferentBody)
10504               << FirstMethodType << FirstName;
10505           ODRDiagNote(SecondMethod->getLocation(),
10506                       SecondMethod->getSourceRange(), MethodDifferentBody)
10507               << SecondMethodType << SecondName;
10508           Diagnosed = true;
10509           break;
10510         }
10511 
10512         break;
10513       }
10514       case TypeAlias:
10515       case TypeDef: {
10516         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
10517         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
10518         auto FirstName = FirstTD->getDeclName();
10519         auto SecondName = SecondTD->getDeclName();
10520         if (FirstName != SecondName) {
10521           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10522                        TypedefName)
10523               << (FirstDiffType == TypeAlias) << FirstName;
10524           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10525                       TypedefName)
10526               << (FirstDiffType == TypeAlias) << SecondName;
10527           Diagnosed = true;
10528           break;
10529         }
10530 
10531         QualType FirstType = FirstTD->getUnderlyingType();
10532         QualType SecondType = SecondTD->getUnderlyingType();
10533         if (ComputeQualTypeODRHash(FirstType) !=
10534             ComputeQualTypeODRHash(SecondType)) {
10535           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10536                        TypedefType)
10537               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
10538           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10539                       TypedefType)
10540               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
10541           Diagnosed = true;
10542           break;
10543         }
10544         break;
10545       }
10546       case Var: {
10547         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
10548         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
10549         auto FirstName = FirstVD->getDeclName();
10550         auto SecondName = SecondVD->getDeclName();
10551         if (FirstName != SecondName) {
10552           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10553                        VarName)
10554               << FirstName;
10555           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10556                       VarName)
10557               << SecondName;
10558           Diagnosed = true;
10559           break;
10560         }
10561 
10562         QualType FirstType = FirstVD->getType();
10563         QualType SecondType = SecondVD->getType();
10564         if (ComputeQualTypeODRHash(FirstType) !=
10565                         ComputeQualTypeODRHash(SecondType)) {
10566           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10567                        VarType)
10568               << FirstName << FirstType;
10569           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10570                       VarType)
10571               << SecondName << SecondType;
10572           Diagnosed = true;
10573           break;
10574         }
10575 
10576         const Expr *FirstInit = FirstVD->getInit();
10577         const Expr *SecondInit = SecondVD->getInit();
10578         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10579           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10580                        VarSingleInitializer)
10581               << FirstName << (FirstInit == nullptr)
10582               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
10583           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10584                       VarSingleInitializer)
10585               << SecondName << (SecondInit == nullptr)
10586               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10587           Diagnosed = true;
10588           break;
10589         }
10590 
10591         if (FirstInit && SecondInit &&
10592             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10593           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10594                        VarDifferentInitializer)
10595               << FirstName << FirstInit->getSourceRange();
10596           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10597                       VarDifferentInitializer)
10598               << SecondName << SecondInit->getSourceRange();
10599           Diagnosed = true;
10600           break;
10601         }
10602 
10603         const bool FirstIsConstexpr = FirstVD->isConstexpr();
10604         const bool SecondIsConstexpr = SecondVD->isConstexpr();
10605         if (FirstIsConstexpr != SecondIsConstexpr) {
10606           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10607                        VarConstexpr)
10608               << FirstName << FirstIsConstexpr;
10609           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10610                       VarConstexpr)
10611               << SecondName << SecondIsConstexpr;
10612           Diagnosed = true;
10613           break;
10614         }
10615         break;
10616       }
10617       case Friend: {
10618         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10619         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10620 
10621         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10622         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10623 
10624         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10625         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10626 
10627         if (FirstND && SecondND) {
10628           ODRDiagError(FirstFriend->getFriendLoc(),
10629                        FirstFriend->getSourceRange(), FriendFunction)
10630               << FirstND;
10631           ODRDiagNote(SecondFriend->getFriendLoc(),
10632                       SecondFriend->getSourceRange(), FriendFunction)
10633               << SecondND;
10634 
10635           Diagnosed = true;
10636           break;
10637         }
10638 
10639         if (FirstTSI && SecondTSI) {
10640           QualType FirstFriendType = FirstTSI->getType();
10641           QualType SecondFriendType = SecondTSI->getType();
10642           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10643                  ComputeQualTypeODRHash(SecondFriendType));
10644           ODRDiagError(FirstFriend->getFriendLoc(),
10645                        FirstFriend->getSourceRange(), FriendType)
10646               << FirstFriendType;
10647           ODRDiagNote(SecondFriend->getFriendLoc(),
10648                       SecondFriend->getSourceRange(), FriendType)
10649               << SecondFriendType;
10650           Diagnosed = true;
10651           break;
10652         }
10653 
10654         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
10655                      FriendTypeFunction)
10656             << (FirstTSI == nullptr);
10657         ODRDiagNote(SecondFriend->getFriendLoc(),
10658                     SecondFriend->getSourceRange(), FriendTypeFunction)
10659             << (SecondTSI == nullptr);
10660 
10661         Diagnosed = true;
10662         break;
10663       }
10664       case FunctionTemplate: {
10665         FunctionTemplateDecl *FirstTemplate =
10666             cast<FunctionTemplateDecl>(FirstDecl);
10667         FunctionTemplateDecl *SecondTemplate =
10668             cast<FunctionTemplateDecl>(SecondDecl);
10669 
10670         TemplateParameterList *FirstTPL =
10671             FirstTemplate->getTemplateParameters();
10672         TemplateParameterList *SecondTPL =
10673             SecondTemplate->getTemplateParameters();
10674 
10675         if (FirstTPL->size() != SecondTPL->size()) {
10676           ODRDiagError(FirstTemplate->getLocation(),
10677                        FirstTemplate->getSourceRange(),
10678                        FunctionTemplateDifferentNumberParameters)
10679               << FirstTemplate << FirstTPL->size();
10680           ODRDiagNote(SecondTemplate->getLocation(),
10681                       SecondTemplate->getSourceRange(),
10682                       FunctionTemplateDifferentNumberParameters)
10683               << SecondTemplate  << SecondTPL->size();
10684 
10685           Diagnosed = true;
10686           break;
10687         }
10688 
10689         bool ParameterMismatch = false;
10690         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10691           NamedDecl *FirstParam = FirstTPL->getParam(i);
10692           NamedDecl *SecondParam = SecondTPL->getParam(i);
10693 
10694           if (FirstParam->getKind() != SecondParam->getKind()) {
10695             enum {
10696               TemplateTypeParameter,
10697               NonTypeTemplateParameter,
10698               TemplateTemplateParameter,
10699             };
10700             auto GetParamType = [](NamedDecl *D) {
10701               switch (D->getKind()) {
10702                 default:
10703                   llvm_unreachable("Unexpected template parameter type");
10704                 case Decl::TemplateTypeParm:
10705                   return TemplateTypeParameter;
10706                 case Decl::NonTypeTemplateParm:
10707                   return NonTypeTemplateParameter;
10708                 case Decl::TemplateTemplateParm:
10709                   return TemplateTemplateParameter;
10710               }
10711             };
10712 
10713             ODRDiagError(FirstTemplate->getLocation(),
10714                          FirstTemplate->getSourceRange(),
10715                          FunctionTemplateParameterDifferentKind)
10716                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10717             ODRDiagNote(SecondTemplate->getLocation(),
10718                         SecondTemplate->getSourceRange(),
10719                         FunctionTemplateParameterDifferentKind)
10720                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10721 
10722             ParameterMismatch = true;
10723             break;
10724           }
10725 
10726           if (FirstParam->getName() != SecondParam->getName()) {
10727             ODRDiagError(FirstTemplate->getLocation(),
10728                          FirstTemplate->getSourceRange(),
10729                          FunctionTemplateParameterName)
10730                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10731                 << FirstParam;
10732             ODRDiagNote(SecondTemplate->getLocation(),
10733                         SecondTemplate->getSourceRange(),
10734                         FunctionTemplateParameterName)
10735                 << SecondTemplate << (i + 1)
10736                 << (bool)SecondParam->getIdentifier() << SecondParam;
10737             ParameterMismatch = true;
10738             break;
10739           }
10740 
10741           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10742               isa<TemplateTypeParmDecl>(SecondParam)) {
10743             TemplateTypeParmDecl *FirstTTPD =
10744                 cast<TemplateTypeParmDecl>(FirstParam);
10745             TemplateTypeParmDecl *SecondTTPD =
10746                 cast<TemplateTypeParmDecl>(SecondParam);
10747             bool HasFirstDefaultArgument =
10748                 FirstTTPD->hasDefaultArgument() &&
10749                 !FirstTTPD->defaultArgumentWasInherited();
10750             bool HasSecondDefaultArgument =
10751                 SecondTTPD->hasDefaultArgument() &&
10752                 !SecondTTPD->defaultArgumentWasInherited();
10753             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10754               ODRDiagError(FirstTemplate->getLocation(),
10755                            FirstTemplate->getSourceRange(),
10756                            FunctionTemplateParameterSingleDefaultArgument)
10757                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10758               ODRDiagNote(SecondTemplate->getLocation(),
10759                           SecondTemplate->getSourceRange(),
10760                           FunctionTemplateParameterSingleDefaultArgument)
10761                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10762               ParameterMismatch = true;
10763               break;
10764             }
10765 
10766             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10767               QualType FirstType = FirstTTPD->getDefaultArgument();
10768               QualType SecondType = SecondTTPD->getDefaultArgument();
10769               if (ComputeQualTypeODRHash(FirstType) !=
10770                   ComputeQualTypeODRHash(SecondType)) {
10771                 ODRDiagError(FirstTemplate->getLocation(),
10772                              FirstTemplate->getSourceRange(),
10773                              FunctionTemplateParameterDifferentDefaultArgument)
10774                     << FirstTemplate << (i + 1) << FirstType;
10775                 ODRDiagNote(SecondTemplate->getLocation(),
10776                             SecondTemplate->getSourceRange(),
10777                             FunctionTemplateParameterDifferentDefaultArgument)
10778                     << SecondTemplate << (i + 1) << SecondType;
10779                 ParameterMismatch = true;
10780                 break;
10781               }
10782             }
10783 
10784             if (FirstTTPD->isParameterPack() !=
10785                 SecondTTPD->isParameterPack()) {
10786               ODRDiagError(FirstTemplate->getLocation(),
10787                            FirstTemplate->getSourceRange(),
10788                            FunctionTemplatePackParameter)
10789                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10790               ODRDiagNote(SecondTemplate->getLocation(),
10791                           SecondTemplate->getSourceRange(),
10792                           FunctionTemplatePackParameter)
10793                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10794               ParameterMismatch = true;
10795               break;
10796             }
10797           }
10798 
10799           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10800               isa<TemplateTemplateParmDecl>(SecondParam)) {
10801             TemplateTemplateParmDecl *FirstTTPD =
10802                 cast<TemplateTemplateParmDecl>(FirstParam);
10803             TemplateTemplateParmDecl *SecondTTPD =
10804                 cast<TemplateTemplateParmDecl>(SecondParam);
10805 
10806             TemplateParameterList *FirstTPL =
10807                 FirstTTPD->getTemplateParameters();
10808             TemplateParameterList *SecondTPL =
10809                 SecondTTPD->getTemplateParameters();
10810 
10811             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10812                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10813               ODRDiagError(FirstTemplate->getLocation(),
10814                            FirstTemplate->getSourceRange(),
10815                            FunctionTemplateParameterDifferentType)
10816                   << FirstTemplate << (i + 1);
10817               ODRDiagNote(SecondTemplate->getLocation(),
10818                           SecondTemplate->getSourceRange(),
10819                           FunctionTemplateParameterDifferentType)
10820                   << SecondTemplate << (i + 1);
10821               ParameterMismatch = true;
10822               break;
10823             }
10824 
10825             bool HasFirstDefaultArgument =
10826                 FirstTTPD->hasDefaultArgument() &&
10827                 !FirstTTPD->defaultArgumentWasInherited();
10828             bool HasSecondDefaultArgument =
10829                 SecondTTPD->hasDefaultArgument() &&
10830                 !SecondTTPD->defaultArgumentWasInherited();
10831             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10832               ODRDiagError(FirstTemplate->getLocation(),
10833                            FirstTemplate->getSourceRange(),
10834                            FunctionTemplateParameterSingleDefaultArgument)
10835                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10836               ODRDiagNote(SecondTemplate->getLocation(),
10837                           SecondTemplate->getSourceRange(),
10838                           FunctionTemplateParameterSingleDefaultArgument)
10839                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10840               ParameterMismatch = true;
10841               break;
10842             }
10843 
10844             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10845               TemplateArgument FirstTA =
10846                   FirstTTPD->getDefaultArgument().getArgument();
10847               TemplateArgument SecondTA =
10848                   SecondTTPD->getDefaultArgument().getArgument();
10849               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10850                   ComputeTemplateArgumentODRHash(SecondTA)) {
10851                 ODRDiagError(FirstTemplate->getLocation(),
10852                              FirstTemplate->getSourceRange(),
10853                              FunctionTemplateParameterDifferentDefaultArgument)
10854                     << FirstTemplate << (i + 1) << FirstTA;
10855                 ODRDiagNote(SecondTemplate->getLocation(),
10856                             SecondTemplate->getSourceRange(),
10857                             FunctionTemplateParameterDifferentDefaultArgument)
10858                     << SecondTemplate << (i + 1) << SecondTA;
10859                 ParameterMismatch = true;
10860                 break;
10861               }
10862             }
10863 
10864             if (FirstTTPD->isParameterPack() !=
10865                 SecondTTPD->isParameterPack()) {
10866               ODRDiagError(FirstTemplate->getLocation(),
10867                            FirstTemplate->getSourceRange(),
10868                            FunctionTemplatePackParameter)
10869                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10870               ODRDiagNote(SecondTemplate->getLocation(),
10871                           SecondTemplate->getSourceRange(),
10872                           FunctionTemplatePackParameter)
10873                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10874               ParameterMismatch = true;
10875               break;
10876             }
10877           }
10878 
10879           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10880               isa<NonTypeTemplateParmDecl>(SecondParam)) {
10881             NonTypeTemplateParmDecl *FirstNTTPD =
10882                 cast<NonTypeTemplateParmDecl>(FirstParam);
10883             NonTypeTemplateParmDecl *SecondNTTPD =
10884                 cast<NonTypeTemplateParmDecl>(SecondParam);
10885 
10886             QualType FirstType = FirstNTTPD->getType();
10887             QualType SecondType = SecondNTTPD->getType();
10888             if (ComputeQualTypeODRHash(FirstType) !=
10889                 ComputeQualTypeODRHash(SecondType)) {
10890               ODRDiagError(FirstTemplate->getLocation(),
10891                            FirstTemplate->getSourceRange(),
10892                            FunctionTemplateParameterDifferentType)
10893                   << FirstTemplate << (i + 1);
10894               ODRDiagNote(SecondTemplate->getLocation(),
10895                           SecondTemplate->getSourceRange(),
10896                           FunctionTemplateParameterDifferentType)
10897                   << SecondTemplate << (i + 1);
10898               ParameterMismatch = true;
10899               break;
10900             }
10901 
10902             bool HasFirstDefaultArgument =
10903                 FirstNTTPD->hasDefaultArgument() &&
10904                 !FirstNTTPD->defaultArgumentWasInherited();
10905             bool HasSecondDefaultArgument =
10906                 SecondNTTPD->hasDefaultArgument() &&
10907                 !SecondNTTPD->defaultArgumentWasInherited();
10908             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10909               ODRDiagError(FirstTemplate->getLocation(),
10910                            FirstTemplate->getSourceRange(),
10911                            FunctionTemplateParameterSingleDefaultArgument)
10912                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10913               ODRDiagNote(SecondTemplate->getLocation(),
10914                           SecondTemplate->getSourceRange(),
10915                           FunctionTemplateParameterSingleDefaultArgument)
10916                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10917               ParameterMismatch = true;
10918               break;
10919             }
10920 
10921             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10922               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
10923               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
10924               if (ComputeODRHash(FirstDefaultArgument) !=
10925                   ComputeODRHash(SecondDefaultArgument)) {
10926                 ODRDiagError(FirstTemplate->getLocation(),
10927                              FirstTemplate->getSourceRange(),
10928                              FunctionTemplateParameterDifferentDefaultArgument)
10929                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
10930                 ODRDiagNote(SecondTemplate->getLocation(),
10931                             SecondTemplate->getSourceRange(),
10932                             FunctionTemplateParameterDifferentDefaultArgument)
10933                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
10934                 ParameterMismatch = true;
10935                 break;
10936               }
10937             }
10938 
10939             if (FirstNTTPD->isParameterPack() !=
10940                 SecondNTTPD->isParameterPack()) {
10941               ODRDiagError(FirstTemplate->getLocation(),
10942                            FirstTemplate->getSourceRange(),
10943                            FunctionTemplatePackParameter)
10944                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
10945               ODRDiagNote(SecondTemplate->getLocation(),
10946                           SecondTemplate->getSourceRange(),
10947                           FunctionTemplatePackParameter)
10948                   << SecondTemplate << (i + 1)
10949                   << SecondNTTPD->isParameterPack();
10950               ParameterMismatch = true;
10951               break;
10952             }
10953           }
10954         }
10955 
10956         if (ParameterMismatch) {
10957           Diagnosed = true;
10958           break;
10959         }
10960 
10961         break;
10962       }
10963       }
10964 
10965       if (Diagnosed)
10966         continue;
10967 
10968       Diag(FirstDecl->getLocation(),
10969            diag::err_module_odr_violation_mismatch_decl_unknown)
10970           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
10971           << FirstDecl->getSourceRange();
10972       Diag(SecondDecl->getLocation(),
10973            diag::note_module_odr_violation_mismatch_decl_unknown)
10974           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
10975       Diagnosed = true;
10976     }
10977 
10978     if (!Diagnosed) {
10979       // All definitions are updates to the same declaration. This happens if a
10980       // module instantiates the declaration of a class template specialization
10981       // and two or more other modules instantiate its definition.
10982       //
10983       // FIXME: Indicate which modules had instantiations of this definition.
10984       // FIXME: How can this even happen?
10985       Diag(Merge.first->getLocation(),
10986            diag::err_module_odr_violation_different_instantiations)
10987         << Merge.first;
10988     }
10989   }
10990 
10991   // Issue ODR failures diagnostics for functions.
10992   for (auto &Merge : FunctionOdrMergeFailures) {
10993     enum ODRFunctionDifference {
10994       ReturnType,
10995       ParameterName,
10996       ParameterType,
10997       ParameterSingleDefaultArgument,
10998       ParameterDifferentDefaultArgument,
10999       FunctionBody,
11000     };
11001 
11002     FunctionDecl *FirstFunction = Merge.first;
11003     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11004 
11005     bool Diagnosed = false;
11006     for (auto &SecondFunction : Merge.second) {
11007 
11008       if (FirstFunction == SecondFunction)
11009         continue;
11010 
11011       std::string SecondModule =
11012           getOwningModuleNameForDiagnostic(SecondFunction);
11013 
11014       auto ODRDiagError = [FirstFunction, &FirstModule,
11015                            this](SourceLocation Loc, SourceRange Range,
11016                                  ODRFunctionDifference DiffType) {
11017         return Diag(Loc, diag::err_module_odr_violation_function)
11018                << FirstFunction << FirstModule.empty() << FirstModule << Range
11019                << DiffType;
11020       };
11021       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11022                                                SourceRange Range,
11023                                                ODRFunctionDifference DiffType) {
11024         return Diag(Loc, diag::note_module_odr_violation_function)
11025                << SecondModule << Range << DiffType;
11026       };
11027 
11028       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11029           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11030         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11031                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11032             << FirstFunction->getReturnType();
11033         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11034                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11035             << SecondFunction->getReturnType();
11036         Diagnosed = true;
11037         break;
11038       }
11039 
11040       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11041              "Merged functions with different number of parameters");
11042 
11043       auto ParamSize = FirstFunction->param_size();
11044       bool ParameterMismatch = false;
11045       for (unsigned I = 0; I < ParamSize; ++I) {
11046         auto *FirstParam = FirstFunction->getParamDecl(I);
11047         auto *SecondParam = SecondFunction->getParamDecl(I);
11048 
11049         assert(getContext().hasSameType(FirstParam->getType(),
11050                                       SecondParam->getType()) &&
11051                "Merged function has different parameter types.");
11052 
11053         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11054           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11055                        ParameterName)
11056               << I + 1 << FirstParam->getDeclName();
11057           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11058                       ParameterName)
11059               << I + 1 << SecondParam->getDeclName();
11060           ParameterMismatch = true;
11061           break;
11062         };
11063 
11064         QualType FirstParamType = FirstParam->getType();
11065         QualType SecondParamType = SecondParam->getType();
11066         if (FirstParamType != SecondParamType &&
11067             ComputeQualTypeODRHash(FirstParamType) !=
11068                 ComputeQualTypeODRHash(SecondParamType)) {
11069           if (const DecayedType *ParamDecayedType =
11070                   FirstParamType->getAs<DecayedType>()) {
11071             ODRDiagError(FirstParam->getLocation(),
11072                          FirstParam->getSourceRange(), ParameterType)
11073                 << (I + 1) << FirstParamType << true
11074                 << ParamDecayedType->getOriginalType();
11075           } else {
11076             ODRDiagError(FirstParam->getLocation(),
11077                          FirstParam->getSourceRange(), ParameterType)
11078                 << (I + 1) << FirstParamType << false;
11079           }
11080 
11081           if (const DecayedType *ParamDecayedType =
11082                   SecondParamType->getAs<DecayedType>()) {
11083             ODRDiagNote(SecondParam->getLocation(),
11084                         SecondParam->getSourceRange(), ParameterType)
11085                 << (I + 1) << SecondParamType << true
11086                 << ParamDecayedType->getOriginalType();
11087           } else {
11088             ODRDiagNote(SecondParam->getLocation(),
11089                         SecondParam->getSourceRange(), ParameterType)
11090                 << (I + 1) << SecondParamType << false;
11091           }
11092           ParameterMismatch = true;
11093           break;
11094         }
11095 
11096         const Expr *FirstInit = FirstParam->getInit();
11097         const Expr *SecondInit = SecondParam->getInit();
11098         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11099           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11100                        ParameterSingleDefaultArgument)
11101               << (I + 1) << (FirstInit == nullptr)
11102               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11103           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11104                       ParameterSingleDefaultArgument)
11105               << (I + 1) << (SecondInit == nullptr)
11106               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11107           ParameterMismatch = true;
11108           break;
11109         }
11110 
11111         if (FirstInit && SecondInit &&
11112             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11113           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11114                        ParameterDifferentDefaultArgument)
11115               << (I + 1) << FirstInit->getSourceRange();
11116           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11117                       ParameterDifferentDefaultArgument)
11118               << (I + 1) << SecondInit->getSourceRange();
11119           ParameterMismatch = true;
11120           break;
11121         }
11122 
11123         assert(ComputeSubDeclODRHash(FirstParam) ==
11124                    ComputeSubDeclODRHash(SecondParam) &&
11125                "Undiagnosed parameter difference.");
11126       }
11127 
11128       if (ParameterMismatch) {
11129         Diagnosed = true;
11130         break;
11131       }
11132 
11133       // If no error has been generated before now, assume the problem is in
11134       // the body and generate a message.
11135       ODRDiagError(FirstFunction->getLocation(),
11136                    FirstFunction->getSourceRange(), FunctionBody);
11137       ODRDiagNote(SecondFunction->getLocation(),
11138                   SecondFunction->getSourceRange(), FunctionBody);
11139       Diagnosed = true;
11140       break;
11141     }
11142     (void)Diagnosed;
11143     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11144   }
11145 
11146   // Issue ODR failures diagnostics for enums.
11147   for (auto &Merge : EnumOdrMergeFailures) {
11148     enum ODREnumDifference {
11149       SingleScopedEnum,
11150       EnumTagKeywordMismatch,
11151       SingleSpecifiedType,
11152       DifferentSpecifiedTypes,
11153       DifferentNumberEnumConstants,
11154       EnumConstantName,
11155       EnumConstantSingleInitilizer,
11156       EnumConstantDifferentInitilizer,
11157     };
11158 
11159     // If we've already pointed out a specific problem with this enum, don't
11160     // bother issuing a general "something's different" diagnostic.
11161     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11162       continue;
11163 
11164     EnumDecl *FirstEnum = Merge.first;
11165     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11166 
11167     using DeclHashes =
11168         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11169     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11170                               DeclHashes &Hashes, EnumDecl *Enum) {
11171       for (auto *D : Enum->decls()) {
11172         // Due to decl merging, the first EnumDecl is the parent of
11173         // Decls in both records.
11174         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11175           continue;
11176         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11177         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11178                             ComputeSubDeclODRHash(D));
11179       }
11180     };
11181     DeclHashes FirstHashes;
11182     PopulateHashes(FirstHashes, FirstEnum);
11183     bool Diagnosed = false;
11184     for (auto &SecondEnum : Merge.second) {
11185 
11186       if (FirstEnum == SecondEnum)
11187         continue;
11188 
11189       std::string SecondModule =
11190           getOwningModuleNameForDiagnostic(SecondEnum);
11191 
11192       auto ODRDiagError = [FirstEnum, &FirstModule,
11193                            this](SourceLocation Loc, SourceRange Range,
11194                                  ODREnumDifference DiffType) {
11195         return Diag(Loc, diag::err_module_odr_violation_enum)
11196                << FirstEnum << FirstModule.empty() << FirstModule << Range
11197                << DiffType;
11198       };
11199       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11200                                                SourceRange Range,
11201                                                ODREnumDifference DiffType) {
11202         return Diag(Loc, diag::note_module_odr_violation_enum)
11203                << SecondModule << Range << DiffType;
11204       };
11205 
11206       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11207         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11208                      SingleScopedEnum)
11209             << FirstEnum->isScoped();
11210         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11211                     SingleScopedEnum)
11212             << SecondEnum->isScoped();
11213         Diagnosed = true;
11214         continue;
11215       }
11216 
11217       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11218         if (FirstEnum->isScopedUsingClassTag() !=
11219             SecondEnum->isScopedUsingClassTag()) {
11220           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11221                        EnumTagKeywordMismatch)
11222               << FirstEnum->isScopedUsingClassTag();
11223           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11224                       EnumTagKeywordMismatch)
11225               << SecondEnum->isScopedUsingClassTag();
11226           Diagnosed = true;
11227           continue;
11228         }
11229       }
11230 
11231       QualType FirstUnderlyingType =
11232           FirstEnum->getIntegerTypeSourceInfo()
11233               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11234               : QualType();
11235       QualType SecondUnderlyingType =
11236           SecondEnum->getIntegerTypeSourceInfo()
11237               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11238               : QualType();
11239       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11240           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11241                        SingleSpecifiedType)
11242               << !FirstUnderlyingType.isNull();
11243           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11244                       SingleSpecifiedType)
11245               << !SecondUnderlyingType.isNull();
11246           Diagnosed = true;
11247           continue;
11248       }
11249 
11250       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11251         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11252             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11253           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11254                        DifferentSpecifiedTypes)
11255               << FirstUnderlyingType;
11256           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11257                       DifferentSpecifiedTypes)
11258               << SecondUnderlyingType;
11259           Diagnosed = true;
11260           continue;
11261         }
11262       }
11263 
11264       DeclHashes SecondHashes;
11265       PopulateHashes(SecondHashes, SecondEnum);
11266 
11267       if (FirstHashes.size() != SecondHashes.size()) {
11268         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11269                      DifferentNumberEnumConstants)
11270             << (int)FirstHashes.size();
11271         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11272                     DifferentNumberEnumConstants)
11273             << (int)SecondHashes.size();
11274         Diagnosed = true;
11275         continue;
11276       }
11277 
11278       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11279         if (FirstHashes[I].second == SecondHashes[I].second)
11280           continue;
11281         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11282         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11283 
11284         if (FirstEnumConstant->getDeclName() !=
11285             SecondEnumConstant->getDeclName()) {
11286 
11287           ODRDiagError(FirstEnumConstant->getLocation(),
11288                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11289               << I + 1 << FirstEnumConstant;
11290           ODRDiagNote(SecondEnumConstant->getLocation(),
11291                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11292               << I + 1 << SecondEnumConstant;
11293           Diagnosed = true;
11294           break;
11295         }
11296 
11297         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11298         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11299         if (!FirstInit && !SecondInit)
11300           continue;
11301 
11302         if (!FirstInit || !SecondInit) {
11303           ODRDiagError(FirstEnumConstant->getLocation(),
11304                        FirstEnumConstant->getSourceRange(),
11305                        EnumConstantSingleInitilizer)
11306               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11307           ODRDiagNote(SecondEnumConstant->getLocation(),
11308                       SecondEnumConstant->getSourceRange(),
11309                       EnumConstantSingleInitilizer)
11310               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11311           Diagnosed = true;
11312           break;
11313         }
11314 
11315         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11316           ODRDiagError(FirstEnumConstant->getLocation(),
11317                        FirstEnumConstant->getSourceRange(),
11318                        EnumConstantDifferentInitilizer)
11319               << I + 1 << FirstEnumConstant;
11320           ODRDiagNote(SecondEnumConstant->getLocation(),
11321                       SecondEnumConstant->getSourceRange(),
11322                       EnumConstantDifferentInitilizer)
11323               << I + 1 << SecondEnumConstant;
11324           Diagnosed = true;
11325           break;
11326         }
11327       }
11328     }
11329 
11330     (void)Diagnosed;
11331     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11332   }
11333 }
11334 
11335 void ASTReader::StartedDeserializing() {
11336   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11337     ReadTimer->startTimer();
11338 }
11339 
11340 void ASTReader::FinishedDeserializing() {
11341   assert(NumCurrentElementsDeserializing &&
11342          "FinishedDeserializing not paired with StartedDeserializing");
11343   if (NumCurrentElementsDeserializing == 1) {
11344     // We decrease NumCurrentElementsDeserializing only after pending actions
11345     // are finished, to avoid recursively re-calling finishPendingActions().
11346     finishPendingActions();
11347   }
11348   --NumCurrentElementsDeserializing;
11349 
11350   if (NumCurrentElementsDeserializing == 0) {
11351     // Propagate exception specification and deduced type updates along
11352     // redeclaration chains.
11353     //
11354     // We do this now rather than in finishPendingActions because we want to
11355     // be able to walk the complete redeclaration chains of the updated decls.
11356     while (!PendingExceptionSpecUpdates.empty() ||
11357            !PendingDeducedTypeUpdates.empty()) {
11358       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11359       PendingExceptionSpecUpdates.clear();
11360       for (auto Update : ESUpdates) {
11361         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11362         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11363         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11364         if (auto *Listener = getContext().getASTMutationListener())
11365           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11366         for (auto *Redecl : Update.second->redecls())
11367           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11368       }
11369 
11370       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11371       PendingDeducedTypeUpdates.clear();
11372       for (auto Update : DTUpdates) {
11373         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11374         // FIXME: If the return type is already deduced, check that it matches.
11375         getContext().adjustDeducedFunctionResultType(Update.first,
11376                                                      Update.second);
11377       }
11378     }
11379 
11380     if (ReadTimer)
11381       ReadTimer->stopTimer();
11382 
11383     diagnoseOdrViolations();
11384 
11385     // We are not in recursive loading, so it's safe to pass the "interesting"
11386     // decls to the consumer.
11387     if (Consumer)
11388       PassInterestingDeclsToConsumer();
11389   }
11390 }
11391 
11392 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11393   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11394     // Remove any fake results before adding any real ones.
11395     auto It = PendingFakeLookupResults.find(II);
11396     if (It != PendingFakeLookupResults.end()) {
11397       for (auto *ND : It->second)
11398         SemaObj->IdResolver.RemoveDecl(ND);
11399       // FIXME: this works around module+PCH performance issue.
11400       // Rather than erase the result from the map, which is O(n), just clear
11401       // the vector of NamedDecls.
11402       It->second.clear();
11403     }
11404   }
11405 
11406   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11407     SemaObj->TUScope->AddDecl(D);
11408   } else if (SemaObj->TUScope) {
11409     // Adding the decl to IdResolver may have failed because it was already in
11410     // (even though it was not added in scope). If it is already in, make sure
11411     // it gets in the scope as well.
11412     if (std::find(SemaObj->IdResolver.begin(Name),
11413                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11414       SemaObj->TUScope->AddDecl(D);
11415   }
11416 }
11417 
11418 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11419                      ASTContext *Context,
11420                      const PCHContainerReader &PCHContainerRdr,
11421                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11422                      StringRef isysroot, bool DisableValidation,
11423                      bool AllowASTWithCompilerErrors,
11424                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11425                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11426                      std::unique_ptr<llvm::Timer> ReadTimer)
11427     : Listener(DisableValidation
11428                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11429                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11430       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11431       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11432       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11433                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11434       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11435       DisableValidation(DisableValidation),
11436       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11437       AllowConfigurationMismatch(AllowConfigurationMismatch),
11438       ValidateSystemInputs(ValidateSystemInputs),
11439       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11440       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11441   SourceMgr.setExternalSLocEntrySource(this);
11442 
11443   for (const auto &Ext : Extensions) {
11444     auto BlockName = Ext->getExtensionMetadata().BlockName;
11445     auto Known = ModuleFileExtensions.find(BlockName);
11446     if (Known != ModuleFileExtensions.end()) {
11447       Diags.Report(diag::warn_duplicate_module_file_extension)
11448         << BlockName;
11449       continue;
11450     }
11451 
11452     ModuleFileExtensions.insert({BlockName, Ext});
11453   }
11454 }
11455 
11456 ASTReader::~ASTReader() {
11457   if (OwnsDeserializationListener)
11458     delete DeserializationListener;
11459 }
11460 
11461 IdentifierResolver &ASTReader::getIdResolver() {
11462   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11463 }
11464 
11465 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11466                                                unsigned AbbrevID) {
11467   Idx = 0;
11468   Record.clear();
11469   return Cursor.readRecord(AbbrevID, Record);
11470 }
11471 //===----------------------------------------------------------------------===//
11472 //// OMPClauseReader implementation
11473 ////===----------------------------------------------------------------------===//
11474 
11475 // This has to be in namespace clang because it's friended by all
11476 // of the OMP clauses.
11477 namespace clang {
11478 
11479 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11480   ASTRecordReader &Record;
11481   ASTContext &Context;
11482 
11483 public:
11484   OMPClauseReader(ASTRecordReader &Record)
11485       : Record(Record), Context(Record.getContext()) {}
11486 
11487 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C);
11488 #include "clang/Basic/OpenMPKinds.def"
11489   OMPClause *readClause();
11490   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11491   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11492 };
11493 
11494 } // end namespace clang
11495 
11496 OMPClause *ASTRecordReader::readOMPClause() {
11497   return OMPClauseReader(*this).readClause();
11498 }
11499 
11500 OMPClause *OMPClauseReader::readClause() {
11501   OMPClause *C = nullptr;
11502   switch (Record.readInt()) {
11503   case OMPC_if:
11504     C = new (Context) OMPIfClause();
11505     break;
11506   case OMPC_final:
11507     C = new (Context) OMPFinalClause();
11508     break;
11509   case OMPC_num_threads:
11510     C = new (Context) OMPNumThreadsClause();
11511     break;
11512   case OMPC_safelen:
11513     C = new (Context) OMPSafelenClause();
11514     break;
11515   case OMPC_simdlen:
11516     C = new (Context) OMPSimdlenClause();
11517     break;
11518   case OMPC_allocator:
11519     C = new (Context) OMPAllocatorClause();
11520     break;
11521   case OMPC_collapse:
11522     C = new (Context) OMPCollapseClause();
11523     break;
11524   case OMPC_default:
11525     C = new (Context) OMPDefaultClause();
11526     break;
11527   case OMPC_proc_bind:
11528     C = new (Context) OMPProcBindClause();
11529     break;
11530   case OMPC_schedule:
11531     C = new (Context) OMPScheduleClause();
11532     break;
11533   case OMPC_ordered:
11534     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11535     break;
11536   case OMPC_nowait:
11537     C = new (Context) OMPNowaitClause();
11538     break;
11539   case OMPC_untied:
11540     C = new (Context) OMPUntiedClause();
11541     break;
11542   case OMPC_mergeable:
11543     C = new (Context) OMPMergeableClause();
11544     break;
11545   case OMPC_read:
11546     C = new (Context) OMPReadClause();
11547     break;
11548   case OMPC_write:
11549     C = new (Context) OMPWriteClause();
11550     break;
11551   case OMPC_update:
11552     C = new (Context) OMPUpdateClause();
11553     break;
11554   case OMPC_capture:
11555     C = new (Context) OMPCaptureClause();
11556     break;
11557   case OMPC_seq_cst:
11558     C = new (Context) OMPSeqCstClause();
11559     break;
11560   case OMPC_threads:
11561     C = new (Context) OMPThreadsClause();
11562     break;
11563   case OMPC_simd:
11564     C = new (Context) OMPSIMDClause();
11565     break;
11566   case OMPC_nogroup:
11567     C = new (Context) OMPNogroupClause();
11568     break;
11569   case OMPC_unified_address:
11570     C = new (Context) OMPUnifiedAddressClause();
11571     break;
11572   case OMPC_unified_shared_memory:
11573     C = new (Context) OMPUnifiedSharedMemoryClause();
11574     break;
11575   case OMPC_reverse_offload:
11576     C = new (Context) OMPReverseOffloadClause();
11577     break;
11578   case OMPC_dynamic_allocators:
11579     C = new (Context) OMPDynamicAllocatorsClause();
11580     break;
11581   case OMPC_atomic_default_mem_order:
11582     C = new (Context) OMPAtomicDefaultMemOrderClause();
11583     break;
11584  case OMPC_private:
11585     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11586     break;
11587   case OMPC_firstprivate:
11588     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11589     break;
11590   case OMPC_lastprivate:
11591     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11592     break;
11593   case OMPC_shared:
11594     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11595     break;
11596   case OMPC_reduction:
11597     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11598     break;
11599   case OMPC_task_reduction:
11600     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11601     break;
11602   case OMPC_in_reduction:
11603     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11604     break;
11605   case OMPC_linear:
11606     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11607     break;
11608   case OMPC_aligned:
11609     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11610     break;
11611   case OMPC_copyin:
11612     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11613     break;
11614   case OMPC_copyprivate:
11615     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11616     break;
11617   case OMPC_flush:
11618     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11619     break;
11620   case OMPC_depend: {
11621     unsigned NumVars = Record.readInt();
11622     unsigned NumLoops = Record.readInt();
11623     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11624     break;
11625   }
11626   case OMPC_device:
11627     C = new (Context) OMPDeviceClause();
11628     break;
11629   case OMPC_map: {
11630     OMPMappableExprListSizeTy Sizes;
11631     Sizes.NumVars = Record.readInt();
11632     Sizes.NumUniqueDeclarations = Record.readInt();
11633     Sizes.NumComponentLists = Record.readInt();
11634     Sizes.NumComponents = Record.readInt();
11635     C = OMPMapClause::CreateEmpty(Context, Sizes);
11636     break;
11637   }
11638   case OMPC_num_teams:
11639     C = new (Context) OMPNumTeamsClause();
11640     break;
11641   case OMPC_thread_limit:
11642     C = new (Context) OMPThreadLimitClause();
11643     break;
11644   case OMPC_priority:
11645     C = new (Context) OMPPriorityClause();
11646     break;
11647   case OMPC_grainsize:
11648     C = new (Context) OMPGrainsizeClause();
11649     break;
11650   case OMPC_num_tasks:
11651     C = new (Context) OMPNumTasksClause();
11652     break;
11653   case OMPC_hint:
11654     C = new (Context) OMPHintClause();
11655     break;
11656   case OMPC_dist_schedule:
11657     C = new (Context) OMPDistScheduleClause();
11658     break;
11659   case OMPC_defaultmap:
11660     C = new (Context) OMPDefaultmapClause();
11661     break;
11662   case OMPC_to: {
11663     OMPMappableExprListSizeTy Sizes;
11664     Sizes.NumVars = Record.readInt();
11665     Sizes.NumUniqueDeclarations = Record.readInt();
11666     Sizes.NumComponentLists = Record.readInt();
11667     Sizes.NumComponents = Record.readInt();
11668     C = OMPToClause::CreateEmpty(Context, Sizes);
11669     break;
11670   }
11671   case OMPC_from: {
11672     OMPMappableExprListSizeTy Sizes;
11673     Sizes.NumVars = Record.readInt();
11674     Sizes.NumUniqueDeclarations = Record.readInt();
11675     Sizes.NumComponentLists = Record.readInt();
11676     Sizes.NumComponents = Record.readInt();
11677     C = OMPFromClause::CreateEmpty(Context, Sizes);
11678     break;
11679   }
11680   case OMPC_use_device_ptr: {
11681     OMPMappableExprListSizeTy Sizes;
11682     Sizes.NumVars = Record.readInt();
11683     Sizes.NumUniqueDeclarations = Record.readInt();
11684     Sizes.NumComponentLists = Record.readInt();
11685     Sizes.NumComponents = Record.readInt();
11686     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11687     break;
11688   }
11689   case OMPC_is_device_ptr: {
11690     OMPMappableExprListSizeTy Sizes;
11691     Sizes.NumVars = Record.readInt();
11692     Sizes.NumUniqueDeclarations = Record.readInt();
11693     Sizes.NumComponentLists = Record.readInt();
11694     Sizes.NumComponents = Record.readInt();
11695     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11696     break;
11697   }
11698   case OMPC_allocate:
11699     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11700     break;
11701   case OMPC_nontemporal:
11702     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11703     break;
11704   }
11705   assert(C && "Unknown OMPClause type");
11706 
11707   Visit(C);
11708   C->setLocStart(Record.readSourceLocation());
11709   C->setLocEnd(Record.readSourceLocation());
11710 
11711   return C;
11712 }
11713 
11714 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11715   C->setPreInitStmt(Record.readSubStmt(),
11716                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11717 }
11718 
11719 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11720   VisitOMPClauseWithPreInit(C);
11721   C->setPostUpdateExpr(Record.readSubExpr());
11722 }
11723 
11724 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11725   VisitOMPClauseWithPreInit(C);
11726   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11727   C->setNameModifierLoc(Record.readSourceLocation());
11728   C->setColonLoc(Record.readSourceLocation());
11729   C->setCondition(Record.readSubExpr());
11730   C->setLParenLoc(Record.readSourceLocation());
11731 }
11732 
11733 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11734   VisitOMPClauseWithPreInit(C);
11735   C->setCondition(Record.readSubExpr());
11736   C->setLParenLoc(Record.readSourceLocation());
11737 }
11738 
11739 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11740   VisitOMPClauseWithPreInit(C);
11741   C->setNumThreads(Record.readSubExpr());
11742   C->setLParenLoc(Record.readSourceLocation());
11743 }
11744 
11745 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11746   C->setSafelen(Record.readSubExpr());
11747   C->setLParenLoc(Record.readSourceLocation());
11748 }
11749 
11750 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11751   C->setSimdlen(Record.readSubExpr());
11752   C->setLParenLoc(Record.readSourceLocation());
11753 }
11754 
11755 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11756   C->setAllocator(Record.readExpr());
11757   C->setLParenLoc(Record.readSourceLocation());
11758 }
11759 
11760 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11761   C->setNumForLoops(Record.readSubExpr());
11762   C->setLParenLoc(Record.readSourceLocation());
11763 }
11764 
11765 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11766   C->setDefaultKind(
11767        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
11768   C->setLParenLoc(Record.readSourceLocation());
11769   C->setDefaultKindKwLoc(Record.readSourceLocation());
11770 }
11771 
11772 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11773   C->setProcBindKind(
11774        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
11775   C->setLParenLoc(Record.readSourceLocation());
11776   C->setProcBindKindKwLoc(Record.readSourceLocation());
11777 }
11778 
11779 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11780   VisitOMPClauseWithPreInit(C);
11781   C->setScheduleKind(
11782        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11783   C->setFirstScheduleModifier(
11784       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11785   C->setSecondScheduleModifier(
11786       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11787   C->setChunkSize(Record.readSubExpr());
11788   C->setLParenLoc(Record.readSourceLocation());
11789   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11790   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11791   C->setScheduleKindLoc(Record.readSourceLocation());
11792   C->setCommaLoc(Record.readSourceLocation());
11793 }
11794 
11795 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11796   C->setNumForLoops(Record.readSubExpr());
11797   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11798     C->setLoopNumIterations(I, Record.readSubExpr());
11799   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11800     C->setLoopCounter(I, Record.readSubExpr());
11801   C->setLParenLoc(Record.readSourceLocation());
11802 }
11803 
11804 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11805 
11806 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11807 
11808 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11809 
11810 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11811 
11812 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11813 
11814 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
11815 
11816 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11817 
11818 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11819 
11820 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11821 
11822 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11823 
11824 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11825 
11826 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11827 
11828 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11829     OMPUnifiedSharedMemoryClause *) {}
11830 
11831 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11832 
11833 void
11834 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11835 }
11836 
11837 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11838     OMPAtomicDefaultMemOrderClause *C) {
11839   C->setAtomicDefaultMemOrderKind(
11840       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11841   C->setLParenLoc(Record.readSourceLocation());
11842   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11843 }
11844 
11845 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11846   C->setLParenLoc(Record.readSourceLocation());
11847   unsigned NumVars = C->varlist_size();
11848   SmallVector<Expr *, 16> Vars;
11849   Vars.reserve(NumVars);
11850   for (unsigned i = 0; i != NumVars; ++i)
11851     Vars.push_back(Record.readSubExpr());
11852   C->setVarRefs(Vars);
11853   Vars.clear();
11854   for (unsigned i = 0; i != NumVars; ++i)
11855     Vars.push_back(Record.readSubExpr());
11856   C->setPrivateCopies(Vars);
11857 }
11858 
11859 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
11860   VisitOMPClauseWithPreInit(C);
11861   C->setLParenLoc(Record.readSourceLocation());
11862   unsigned NumVars = C->varlist_size();
11863   SmallVector<Expr *, 16> Vars;
11864   Vars.reserve(NumVars);
11865   for (unsigned i = 0; i != NumVars; ++i)
11866     Vars.push_back(Record.readSubExpr());
11867   C->setVarRefs(Vars);
11868   Vars.clear();
11869   for (unsigned i = 0; i != NumVars; ++i)
11870     Vars.push_back(Record.readSubExpr());
11871   C->setPrivateCopies(Vars);
11872   Vars.clear();
11873   for (unsigned i = 0; i != NumVars; ++i)
11874     Vars.push_back(Record.readSubExpr());
11875   C->setInits(Vars);
11876 }
11877 
11878 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
11879   VisitOMPClauseWithPostUpdate(C);
11880   C->setLParenLoc(Record.readSourceLocation());
11881   unsigned NumVars = C->varlist_size();
11882   SmallVector<Expr *, 16> Vars;
11883   Vars.reserve(NumVars);
11884   for (unsigned i = 0; i != NumVars; ++i)
11885     Vars.push_back(Record.readSubExpr());
11886   C->setVarRefs(Vars);
11887   Vars.clear();
11888   for (unsigned i = 0; i != NumVars; ++i)
11889     Vars.push_back(Record.readSubExpr());
11890   C->setPrivateCopies(Vars);
11891   Vars.clear();
11892   for (unsigned i = 0; i != NumVars; ++i)
11893     Vars.push_back(Record.readSubExpr());
11894   C->setSourceExprs(Vars);
11895   Vars.clear();
11896   for (unsigned i = 0; i != NumVars; ++i)
11897     Vars.push_back(Record.readSubExpr());
11898   C->setDestinationExprs(Vars);
11899   Vars.clear();
11900   for (unsigned i = 0; i != NumVars; ++i)
11901     Vars.push_back(Record.readSubExpr());
11902   C->setAssignmentOps(Vars);
11903 }
11904 
11905 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
11906   C->setLParenLoc(Record.readSourceLocation());
11907   unsigned NumVars = C->varlist_size();
11908   SmallVector<Expr *, 16> Vars;
11909   Vars.reserve(NumVars);
11910   for (unsigned i = 0; i != NumVars; ++i)
11911     Vars.push_back(Record.readSubExpr());
11912   C->setVarRefs(Vars);
11913 }
11914 
11915 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
11916   VisitOMPClauseWithPostUpdate(C);
11917   C->setLParenLoc(Record.readSourceLocation());
11918   C->setColonLoc(Record.readSourceLocation());
11919   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11920   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11921   C->setQualifierLoc(NNSL);
11922   C->setNameInfo(DNI);
11923 
11924   unsigned NumVars = C->varlist_size();
11925   SmallVector<Expr *, 16> Vars;
11926   Vars.reserve(NumVars);
11927   for (unsigned i = 0; i != NumVars; ++i)
11928     Vars.push_back(Record.readSubExpr());
11929   C->setVarRefs(Vars);
11930   Vars.clear();
11931   for (unsigned i = 0; i != NumVars; ++i)
11932     Vars.push_back(Record.readSubExpr());
11933   C->setPrivates(Vars);
11934   Vars.clear();
11935   for (unsigned i = 0; i != NumVars; ++i)
11936     Vars.push_back(Record.readSubExpr());
11937   C->setLHSExprs(Vars);
11938   Vars.clear();
11939   for (unsigned i = 0; i != NumVars; ++i)
11940     Vars.push_back(Record.readSubExpr());
11941   C->setRHSExprs(Vars);
11942   Vars.clear();
11943   for (unsigned i = 0; i != NumVars; ++i)
11944     Vars.push_back(Record.readSubExpr());
11945   C->setReductionOps(Vars);
11946 }
11947 
11948 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
11949   VisitOMPClauseWithPostUpdate(C);
11950   C->setLParenLoc(Record.readSourceLocation());
11951   C->setColonLoc(Record.readSourceLocation());
11952   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11953   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11954   C->setQualifierLoc(NNSL);
11955   C->setNameInfo(DNI);
11956 
11957   unsigned NumVars = C->varlist_size();
11958   SmallVector<Expr *, 16> Vars;
11959   Vars.reserve(NumVars);
11960   for (unsigned I = 0; I != NumVars; ++I)
11961     Vars.push_back(Record.readSubExpr());
11962   C->setVarRefs(Vars);
11963   Vars.clear();
11964   for (unsigned I = 0; I != NumVars; ++I)
11965     Vars.push_back(Record.readSubExpr());
11966   C->setPrivates(Vars);
11967   Vars.clear();
11968   for (unsigned I = 0; I != NumVars; ++I)
11969     Vars.push_back(Record.readSubExpr());
11970   C->setLHSExprs(Vars);
11971   Vars.clear();
11972   for (unsigned I = 0; I != NumVars; ++I)
11973     Vars.push_back(Record.readSubExpr());
11974   C->setRHSExprs(Vars);
11975   Vars.clear();
11976   for (unsigned I = 0; I != NumVars; ++I)
11977     Vars.push_back(Record.readSubExpr());
11978   C->setReductionOps(Vars);
11979 }
11980 
11981 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
11982   VisitOMPClauseWithPostUpdate(C);
11983   C->setLParenLoc(Record.readSourceLocation());
11984   C->setColonLoc(Record.readSourceLocation());
11985   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11986   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11987   C->setQualifierLoc(NNSL);
11988   C->setNameInfo(DNI);
11989 
11990   unsigned NumVars = C->varlist_size();
11991   SmallVector<Expr *, 16> Vars;
11992   Vars.reserve(NumVars);
11993   for (unsigned I = 0; I != NumVars; ++I)
11994     Vars.push_back(Record.readSubExpr());
11995   C->setVarRefs(Vars);
11996   Vars.clear();
11997   for (unsigned I = 0; I != NumVars; ++I)
11998     Vars.push_back(Record.readSubExpr());
11999   C->setPrivates(Vars);
12000   Vars.clear();
12001   for (unsigned I = 0; I != NumVars; ++I)
12002     Vars.push_back(Record.readSubExpr());
12003   C->setLHSExprs(Vars);
12004   Vars.clear();
12005   for (unsigned I = 0; I != NumVars; ++I)
12006     Vars.push_back(Record.readSubExpr());
12007   C->setRHSExprs(Vars);
12008   Vars.clear();
12009   for (unsigned I = 0; I != NumVars; ++I)
12010     Vars.push_back(Record.readSubExpr());
12011   C->setReductionOps(Vars);
12012   Vars.clear();
12013   for (unsigned I = 0; I != NumVars; ++I)
12014     Vars.push_back(Record.readSubExpr());
12015   C->setTaskgroupDescriptors(Vars);
12016 }
12017 
12018 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12019   VisitOMPClauseWithPostUpdate(C);
12020   C->setLParenLoc(Record.readSourceLocation());
12021   C->setColonLoc(Record.readSourceLocation());
12022   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12023   C->setModifierLoc(Record.readSourceLocation());
12024   unsigned NumVars = C->varlist_size();
12025   SmallVector<Expr *, 16> Vars;
12026   Vars.reserve(NumVars);
12027   for (unsigned i = 0; i != NumVars; ++i)
12028     Vars.push_back(Record.readSubExpr());
12029   C->setVarRefs(Vars);
12030   Vars.clear();
12031   for (unsigned i = 0; i != NumVars; ++i)
12032     Vars.push_back(Record.readSubExpr());
12033   C->setPrivates(Vars);
12034   Vars.clear();
12035   for (unsigned i = 0; i != NumVars; ++i)
12036     Vars.push_back(Record.readSubExpr());
12037   C->setInits(Vars);
12038   Vars.clear();
12039   for (unsigned i = 0; i != NumVars; ++i)
12040     Vars.push_back(Record.readSubExpr());
12041   C->setUpdates(Vars);
12042   Vars.clear();
12043   for (unsigned i = 0; i != NumVars; ++i)
12044     Vars.push_back(Record.readSubExpr());
12045   C->setFinals(Vars);
12046   C->setStep(Record.readSubExpr());
12047   C->setCalcStep(Record.readSubExpr());
12048   Vars.clear();
12049   for (unsigned I = 0; I != NumVars + 1; ++I)
12050     Vars.push_back(Record.readSubExpr());
12051   C->setUsedExprs(Vars);
12052 }
12053 
12054 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12055   C->setLParenLoc(Record.readSourceLocation());
12056   C->setColonLoc(Record.readSourceLocation());
12057   unsigned NumVars = C->varlist_size();
12058   SmallVector<Expr *, 16> Vars;
12059   Vars.reserve(NumVars);
12060   for (unsigned i = 0; i != NumVars; ++i)
12061     Vars.push_back(Record.readSubExpr());
12062   C->setVarRefs(Vars);
12063   C->setAlignment(Record.readSubExpr());
12064 }
12065 
12066 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12067   C->setLParenLoc(Record.readSourceLocation());
12068   unsigned NumVars = C->varlist_size();
12069   SmallVector<Expr *, 16> Exprs;
12070   Exprs.reserve(NumVars);
12071   for (unsigned i = 0; i != NumVars; ++i)
12072     Exprs.push_back(Record.readSubExpr());
12073   C->setVarRefs(Exprs);
12074   Exprs.clear();
12075   for (unsigned i = 0; i != NumVars; ++i)
12076     Exprs.push_back(Record.readSubExpr());
12077   C->setSourceExprs(Exprs);
12078   Exprs.clear();
12079   for (unsigned i = 0; i != NumVars; ++i)
12080     Exprs.push_back(Record.readSubExpr());
12081   C->setDestinationExprs(Exprs);
12082   Exprs.clear();
12083   for (unsigned i = 0; i != NumVars; ++i)
12084     Exprs.push_back(Record.readSubExpr());
12085   C->setAssignmentOps(Exprs);
12086 }
12087 
12088 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12089   C->setLParenLoc(Record.readSourceLocation());
12090   unsigned NumVars = C->varlist_size();
12091   SmallVector<Expr *, 16> Exprs;
12092   Exprs.reserve(NumVars);
12093   for (unsigned i = 0; i != NumVars; ++i)
12094     Exprs.push_back(Record.readSubExpr());
12095   C->setVarRefs(Exprs);
12096   Exprs.clear();
12097   for (unsigned i = 0; i != NumVars; ++i)
12098     Exprs.push_back(Record.readSubExpr());
12099   C->setSourceExprs(Exprs);
12100   Exprs.clear();
12101   for (unsigned i = 0; i != NumVars; ++i)
12102     Exprs.push_back(Record.readSubExpr());
12103   C->setDestinationExprs(Exprs);
12104   Exprs.clear();
12105   for (unsigned i = 0; i != NumVars; ++i)
12106     Exprs.push_back(Record.readSubExpr());
12107   C->setAssignmentOps(Exprs);
12108 }
12109 
12110 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12111   C->setLParenLoc(Record.readSourceLocation());
12112   unsigned NumVars = C->varlist_size();
12113   SmallVector<Expr *, 16> Vars;
12114   Vars.reserve(NumVars);
12115   for (unsigned i = 0; i != NumVars; ++i)
12116     Vars.push_back(Record.readSubExpr());
12117   C->setVarRefs(Vars);
12118 }
12119 
12120 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12121   C->setLParenLoc(Record.readSourceLocation());
12122   C->setDependencyKind(
12123       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12124   C->setDependencyLoc(Record.readSourceLocation());
12125   C->setColonLoc(Record.readSourceLocation());
12126   unsigned NumVars = C->varlist_size();
12127   SmallVector<Expr *, 16> Vars;
12128   Vars.reserve(NumVars);
12129   for (unsigned I = 0; I != NumVars; ++I)
12130     Vars.push_back(Record.readSubExpr());
12131   C->setVarRefs(Vars);
12132   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12133     C->setLoopData(I, Record.readSubExpr());
12134 }
12135 
12136 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12137   VisitOMPClauseWithPreInit(C);
12138   C->setDevice(Record.readSubExpr());
12139   C->setLParenLoc(Record.readSourceLocation());
12140 }
12141 
12142 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12143   C->setLParenLoc(Record.readSourceLocation());
12144   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12145     C->setMapTypeModifier(
12146         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12147     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12148   }
12149   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12150   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12151   C->setMapType(
12152      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12153   C->setMapLoc(Record.readSourceLocation());
12154   C->setColonLoc(Record.readSourceLocation());
12155   auto NumVars = C->varlist_size();
12156   auto UniqueDecls = C->getUniqueDeclarationsNum();
12157   auto TotalLists = C->getTotalComponentListNum();
12158   auto TotalComponents = C->getTotalComponentsNum();
12159 
12160   SmallVector<Expr *, 16> Vars;
12161   Vars.reserve(NumVars);
12162   for (unsigned i = 0; i != NumVars; ++i)
12163     Vars.push_back(Record.readExpr());
12164   C->setVarRefs(Vars);
12165 
12166   SmallVector<Expr *, 16> UDMappers;
12167   UDMappers.reserve(NumVars);
12168   for (unsigned I = 0; I < NumVars; ++I)
12169     UDMappers.push_back(Record.readExpr());
12170   C->setUDMapperRefs(UDMappers);
12171 
12172   SmallVector<ValueDecl *, 16> Decls;
12173   Decls.reserve(UniqueDecls);
12174   for (unsigned i = 0; i < UniqueDecls; ++i)
12175     Decls.push_back(Record.readDeclAs<ValueDecl>());
12176   C->setUniqueDecls(Decls);
12177 
12178   SmallVector<unsigned, 16> ListsPerDecl;
12179   ListsPerDecl.reserve(UniqueDecls);
12180   for (unsigned i = 0; i < UniqueDecls; ++i)
12181     ListsPerDecl.push_back(Record.readInt());
12182   C->setDeclNumLists(ListsPerDecl);
12183 
12184   SmallVector<unsigned, 32> ListSizes;
12185   ListSizes.reserve(TotalLists);
12186   for (unsigned i = 0; i < TotalLists; ++i)
12187     ListSizes.push_back(Record.readInt());
12188   C->setComponentListSizes(ListSizes);
12189 
12190   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12191   Components.reserve(TotalComponents);
12192   for (unsigned i = 0; i < TotalComponents; ++i) {
12193     Expr *AssociatedExpr = Record.readExpr();
12194     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12195     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12196         AssociatedExpr, AssociatedDecl));
12197   }
12198   C->setComponents(Components, ListSizes);
12199 }
12200 
12201 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12202   C->setLParenLoc(Record.readSourceLocation());
12203   C->setColonLoc(Record.readSourceLocation());
12204   C->setAllocator(Record.readSubExpr());
12205   unsigned NumVars = C->varlist_size();
12206   SmallVector<Expr *, 16> Vars;
12207   Vars.reserve(NumVars);
12208   for (unsigned i = 0; i != NumVars; ++i)
12209     Vars.push_back(Record.readSubExpr());
12210   C->setVarRefs(Vars);
12211 }
12212 
12213 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12214   VisitOMPClauseWithPreInit(C);
12215   C->setNumTeams(Record.readSubExpr());
12216   C->setLParenLoc(Record.readSourceLocation());
12217 }
12218 
12219 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12220   VisitOMPClauseWithPreInit(C);
12221   C->setThreadLimit(Record.readSubExpr());
12222   C->setLParenLoc(Record.readSourceLocation());
12223 }
12224 
12225 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12226   VisitOMPClauseWithPreInit(C);
12227   C->setPriority(Record.readSubExpr());
12228   C->setLParenLoc(Record.readSourceLocation());
12229 }
12230 
12231 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12232   VisitOMPClauseWithPreInit(C);
12233   C->setGrainsize(Record.readSubExpr());
12234   C->setLParenLoc(Record.readSourceLocation());
12235 }
12236 
12237 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12238   VisitOMPClauseWithPreInit(C);
12239   C->setNumTasks(Record.readSubExpr());
12240   C->setLParenLoc(Record.readSourceLocation());
12241 }
12242 
12243 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12244   C->setHint(Record.readSubExpr());
12245   C->setLParenLoc(Record.readSourceLocation());
12246 }
12247 
12248 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12249   VisitOMPClauseWithPreInit(C);
12250   C->setDistScheduleKind(
12251       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12252   C->setChunkSize(Record.readSubExpr());
12253   C->setLParenLoc(Record.readSourceLocation());
12254   C->setDistScheduleKindLoc(Record.readSourceLocation());
12255   C->setCommaLoc(Record.readSourceLocation());
12256 }
12257 
12258 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12259   C->setDefaultmapKind(
12260        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12261   C->setDefaultmapModifier(
12262       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12263   C->setLParenLoc(Record.readSourceLocation());
12264   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12265   C->setDefaultmapKindLoc(Record.readSourceLocation());
12266 }
12267 
12268 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12269   C->setLParenLoc(Record.readSourceLocation());
12270   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12271   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12272   auto NumVars = C->varlist_size();
12273   auto UniqueDecls = C->getUniqueDeclarationsNum();
12274   auto TotalLists = C->getTotalComponentListNum();
12275   auto TotalComponents = C->getTotalComponentsNum();
12276 
12277   SmallVector<Expr *, 16> Vars;
12278   Vars.reserve(NumVars);
12279   for (unsigned i = 0; i != NumVars; ++i)
12280     Vars.push_back(Record.readSubExpr());
12281   C->setVarRefs(Vars);
12282 
12283   SmallVector<Expr *, 16> UDMappers;
12284   UDMappers.reserve(NumVars);
12285   for (unsigned I = 0; I < NumVars; ++I)
12286     UDMappers.push_back(Record.readSubExpr());
12287   C->setUDMapperRefs(UDMappers);
12288 
12289   SmallVector<ValueDecl *, 16> Decls;
12290   Decls.reserve(UniqueDecls);
12291   for (unsigned i = 0; i < UniqueDecls; ++i)
12292     Decls.push_back(Record.readDeclAs<ValueDecl>());
12293   C->setUniqueDecls(Decls);
12294 
12295   SmallVector<unsigned, 16> ListsPerDecl;
12296   ListsPerDecl.reserve(UniqueDecls);
12297   for (unsigned i = 0; i < UniqueDecls; ++i)
12298     ListsPerDecl.push_back(Record.readInt());
12299   C->setDeclNumLists(ListsPerDecl);
12300 
12301   SmallVector<unsigned, 32> ListSizes;
12302   ListSizes.reserve(TotalLists);
12303   for (unsigned i = 0; i < TotalLists; ++i)
12304     ListSizes.push_back(Record.readInt());
12305   C->setComponentListSizes(ListSizes);
12306 
12307   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12308   Components.reserve(TotalComponents);
12309   for (unsigned i = 0; i < TotalComponents; ++i) {
12310     Expr *AssociatedExpr = Record.readSubExpr();
12311     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12312     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12313         AssociatedExpr, AssociatedDecl));
12314   }
12315   C->setComponents(Components, ListSizes);
12316 }
12317 
12318 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12319   C->setLParenLoc(Record.readSourceLocation());
12320   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12321   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12322   auto NumVars = C->varlist_size();
12323   auto UniqueDecls = C->getUniqueDeclarationsNum();
12324   auto TotalLists = C->getTotalComponentListNum();
12325   auto TotalComponents = C->getTotalComponentsNum();
12326 
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   SmallVector<Expr *, 16> UDMappers;
12334   UDMappers.reserve(NumVars);
12335   for (unsigned I = 0; I < NumVars; ++I)
12336     UDMappers.push_back(Record.readSubExpr());
12337   C->setUDMapperRefs(UDMappers);
12338 
12339   SmallVector<ValueDecl *, 16> Decls;
12340   Decls.reserve(UniqueDecls);
12341   for (unsigned i = 0; i < UniqueDecls; ++i)
12342     Decls.push_back(Record.readDeclAs<ValueDecl>());
12343   C->setUniqueDecls(Decls);
12344 
12345   SmallVector<unsigned, 16> ListsPerDecl;
12346   ListsPerDecl.reserve(UniqueDecls);
12347   for (unsigned i = 0; i < UniqueDecls; ++i)
12348     ListsPerDecl.push_back(Record.readInt());
12349   C->setDeclNumLists(ListsPerDecl);
12350 
12351   SmallVector<unsigned, 32> ListSizes;
12352   ListSizes.reserve(TotalLists);
12353   for (unsigned i = 0; i < TotalLists; ++i)
12354     ListSizes.push_back(Record.readInt());
12355   C->setComponentListSizes(ListSizes);
12356 
12357   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12358   Components.reserve(TotalComponents);
12359   for (unsigned i = 0; i < TotalComponents; ++i) {
12360     Expr *AssociatedExpr = Record.readSubExpr();
12361     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12362     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12363         AssociatedExpr, AssociatedDecl));
12364   }
12365   C->setComponents(Components, ListSizes);
12366 }
12367 
12368 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12369   C->setLParenLoc(Record.readSourceLocation());
12370   auto NumVars = C->varlist_size();
12371   auto UniqueDecls = C->getUniqueDeclarationsNum();
12372   auto TotalLists = C->getTotalComponentListNum();
12373   auto TotalComponents = C->getTotalComponentsNum();
12374 
12375   SmallVector<Expr *, 16> Vars;
12376   Vars.reserve(NumVars);
12377   for (unsigned i = 0; i != NumVars; ++i)
12378     Vars.push_back(Record.readSubExpr());
12379   C->setVarRefs(Vars);
12380   Vars.clear();
12381   for (unsigned i = 0; i != NumVars; ++i)
12382     Vars.push_back(Record.readSubExpr());
12383   C->setPrivateCopies(Vars);
12384   Vars.clear();
12385   for (unsigned i = 0; i != NumVars; ++i)
12386     Vars.push_back(Record.readSubExpr());
12387   C->setInits(Vars);
12388 
12389   SmallVector<ValueDecl *, 16> Decls;
12390   Decls.reserve(UniqueDecls);
12391   for (unsigned i = 0; i < UniqueDecls; ++i)
12392     Decls.push_back(Record.readDeclAs<ValueDecl>());
12393   C->setUniqueDecls(Decls);
12394 
12395   SmallVector<unsigned, 16> ListsPerDecl;
12396   ListsPerDecl.reserve(UniqueDecls);
12397   for (unsigned i = 0; i < UniqueDecls; ++i)
12398     ListsPerDecl.push_back(Record.readInt());
12399   C->setDeclNumLists(ListsPerDecl);
12400 
12401   SmallVector<unsigned, 32> ListSizes;
12402   ListSizes.reserve(TotalLists);
12403   for (unsigned i = 0; i < TotalLists; ++i)
12404     ListSizes.push_back(Record.readInt());
12405   C->setComponentListSizes(ListSizes);
12406 
12407   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12408   Components.reserve(TotalComponents);
12409   for (unsigned i = 0; i < TotalComponents; ++i) {
12410     Expr *AssociatedExpr = Record.readSubExpr();
12411     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12412     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12413         AssociatedExpr, AssociatedDecl));
12414   }
12415   C->setComponents(Components, ListSizes);
12416 }
12417 
12418 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12419   C->setLParenLoc(Record.readSourceLocation());
12420   auto NumVars = C->varlist_size();
12421   auto UniqueDecls = C->getUniqueDeclarationsNum();
12422   auto TotalLists = C->getTotalComponentListNum();
12423   auto TotalComponents = C->getTotalComponentsNum();
12424 
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 
12432   SmallVector<ValueDecl *, 16> Decls;
12433   Decls.reserve(UniqueDecls);
12434   for (unsigned i = 0; i < UniqueDecls; ++i)
12435     Decls.push_back(Record.readDeclAs<ValueDecl>());
12436   C->setUniqueDecls(Decls);
12437 
12438   SmallVector<unsigned, 16> ListsPerDecl;
12439   ListsPerDecl.reserve(UniqueDecls);
12440   for (unsigned i = 0; i < UniqueDecls; ++i)
12441     ListsPerDecl.push_back(Record.readInt());
12442   C->setDeclNumLists(ListsPerDecl);
12443 
12444   SmallVector<unsigned, 32> ListSizes;
12445   ListSizes.reserve(TotalLists);
12446   for (unsigned i = 0; i < TotalLists; ++i)
12447     ListSizes.push_back(Record.readInt());
12448   C->setComponentListSizes(ListSizes);
12449 
12450   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12451   Components.reserve(TotalComponents);
12452   for (unsigned i = 0; i < TotalComponents; ++i) {
12453     Expr *AssociatedExpr = Record.readSubExpr();
12454     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12455     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12456         AssociatedExpr, AssociatedDecl));
12457   }
12458   C->setComponents(Components, ListSizes);
12459 }
12460 
12461 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12462   C->setLParenLoc(Record.readSourceLocation());
12463   unsigned NumVars = C->varlist_size();
12464   SmallVector<Expr *, 16> Vars;
12465   Vars.reserve(NumVars);
12466   for (unsigned i = 0; i != NumVars; ++i)
12467     Vars.push_back(Record.readSubExpr());
12468   C->setVarRefs(Vars);
12469 }
12470