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 "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTStructuralEquivalence.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/AbstractTypeReader.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/ODRHash.h"
34 #include "clang/AST/OpenMPClause.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/DiagnosticError.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/DiagnosticSema.h"
47 #include "clang/Basic/ExceptionSpecificationType.h"
48 #include "clang/Basic/FileManager.h"
49 #include "clang/Basic/FileSystemOptions.h"
50 #include "clang/Basic/IdentifierTable.h"
51 #include "clang/Basic/LLVM.h"
52 #include "clang/Basic/LangOptions.h"
53 #include "clang/Basic/Module.h"
54 #include "clang/Basic/ObjCRuntime.h"
55 #include "clang/Basic/OpenMPKinds.h"
56 #include "clang/Basic/OperatorKinds.h"
57 #include "clang/Basic/PragmaKinds.h"
58 #include "clang/Basic/Sanitizers.h"
59 #include "clang/Basic/SourceLocation.h"
60 #include "clang/Basic/SourceManager.h"
61 #include "clang/Basic/SourceManagerInternals.h"
62 #include "clang/Basic/Specifiers.h"
63 #include "clang/Basic/TargetInfo.h"
64 #include "clang/Basic/TargetOptions.h"
65 #include "clang/Basic/TokenKinds.h"
66 #include "clang/Basic/Version.h"
67 #include "clang/Lex/HeaderSearch.h"
68 #include "clang/Lex/HeaderSearchOptions.h"
69 #include "clang/Lex/MacroInfo.h"
70 #include "clang/Lex/ModuleMap.h"
71 #include "clang/Lex/PreprocessingRecord.h"
72 #include "clang/Lex/Preprocessor.h"
73 #include "clang/Lex/PreprocessorOptions.h"
74 #include "clang/Lex/Token.h"
75 #include "clang/Sema/ObjCMethodList.h"
76 #include "clang/Sema/Scope.h"
77 #include "clang/Sema/Sema.h"
78 #include "clang/Sema/Weak.h"
79 #include "clang/Serialization/ASTBitCodes.h"
80 #include "clang/Serialization/ASTDeserializationListener.h"
81 #include "clang/Serialization/ASTRecordReader.h"
82 #include "clang/Serialization/ContinuousRangeMap.h"
83 #include "clang/Serialization/GlobalModuleIndex.h"
84 #include "clang/Serialization/InMemoryModuleCache.h"
85 #include "clang/Serialization/ModuleFile.h"
86 #include "clang/Serialization/ModuleFileExtension.h"
87 #include "clang/Serialization/ModuleManager.h"
88 #include "clang/Serialization/PCHContainerOperations.h"
89 #include "clang/Serialization/SerializationDiagnostic.h"
90 #include "llvm/ADT/APFloat.h"
91 #include "llvm/ADT/APInt.h"
92 #include "llvm/ADT/APSInt.h"
93 #include "llvm/ADT/ArrayRef.h"
94 #include "llvm/ADT/DenseMap.h"
95 #include "llvm/ADT/FloatingPointMode.h"
96 #include "llvm/ADT/FoldingSet.h"
97 #include "llvm/ADT/Hashing.h"
98 #include "llvm/ADT/IntrusiveRefCntPtr.h"
99 #include "llvm/ADT/None.h"
100 #include "llvm/ADT/Optional.h"
101 #include "llvm/ADT/STLExtras.h"
102 #include "llvm/ADT/ScopeExit.h"
103 #include "llvm/ADT/SmallPtrSet.h"
104 #include "llvm/ADT/SmallString.h"
105 #include "llvm/ADT/SmallVector.h"
106 #include "llvm/ADT/StringExtras.h"
107 #include "llvm/ADT/StringMap.h"
108 #include "llvm/ADT/StringRef.h"
109 #include "llvm/ADT/Triple.h"
110 #include "llvm/ADT/iterator_range.h"
111 #include "llvm/Bitstream/BitstreamReader.h"
112 #include "llvm/Support/Casting.h"
113 #include "llvm/Support/Compiler.h"
114 #include "llvm/Support/Compression.h"
115 #include "llvm/Support/DJB.h"
116 #include "llvm/Support/Endian.h"
117 #include "llvm/Support/Error.h"
118 #include "llvm/Support/ErrorHandling.h"
119 #include "llvm/Support/FileSystem.h"
120 #include "llvm/Support/LEB128.h"
121 #include "llvm/Support/MemoryBuffer.h"
122 #include "llvm/Support/Path.h"
123 #include "llvm/Support/SaveAndRestore.h"
124 #include "llvm/Support/Timer.h"
125 #include "llvm/Support/VersionTuple.h"
126 #include "llvm/Support/raw_ostream.h"
127 #include <algorithm>
128 #include <cassert>
129 #include <cstddef>
130 #include <cstdint>
131 #include <cstdio>
132 #include <ctime>
133 #include <iterator>
134 #include <limits>
135 #include <map>
136 #include <memory>
137 #include <string>
138 #include <system_error>
139 #include <tuple>
140 #include <utility>
141 #include <vector>
142 
143 using namespace clang;
144 using namespace clang::serialization;
145 using namespace clang::serialization::reader;
146 using llvm::BitstreamCursor;
147 
148 //===----------------------------------------------------------------------===//
149 // ChainedASTReaderListener implementation
150 //===----------------------------------------------------------------------===//
151 
152 bool
153 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
154   return First->ReadFullVersionInformation(FullVersion) ||
155          Second->ReadFullVersionInformation(FullVersion);
156 }
157 
158 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
159   First->ReadModuleName(ModuleName);
160   Second->ReadModuleName(ModuleName);
161 }
162 
163 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
164   First->ReadModuleMapFile(ModuleMapPath);
165   Second->ReadModuleMapFile(ModuleMapPath);
166 }
167 
168 bool
169 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
170                                               bool Complain,
171                                               bool AllowCompatibleDifferences) {
172   return First->ReadLanguageOptions(LangOpts, Complain,
173                                     AllowCompatibleDifferences) ||
174          Second->ReadLanguageOptions(LangOpts, Complain,
175                                      AllowCompatibleDifferences);
176 }
177 
178 bool ChainedASTReaderListener::ReadTargetOptions(
179     const TargetOptions &TargetOpts, bool Complain,
180     bool AllowCompatibleDifferences) {
181   return First->ReadTargetOptions(TargetOpts, Complain,
182                                   AllowCompatibleDifferences) ||
183          Second->ReadTargetOptions(TargetOpts, Complain,
184                                    AllowCompatibleDifferences);
185 }
186 
187 bool ChainedASTReaderListener::ReadDiagnosticOptions(
188     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
189   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
190          Second->ReadDiagnosticOptions(DiagOpts, Complain);
191 }
192 
193 bool
194 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
195                                                 bool Complain) {
196   return First->ReadFileSystemOptions(FSOpts, Complain) ||
197          Second->ReadFileSystemOptions(FSOpts, Complain);
198 }
199 
200 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
201     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
202     bool Complain) {
203   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                         Complain) ||
205          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
206                                          Complain);
207 }
208 
209 bool ChainedASTReaderListener::ReadPreprocessorOptions(
210     const PreprocessorOptions &PPOpts, bool Complain,
211     std::string &SuggestedPredefines) {
212   return First->ReadPreprocessorOptions(PPOpts, Complain,
213                                         SuggestedPredefines) ||
214          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
215 }
216 
217 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
218                                            unsigned Value) {
219   First->ReadCounter(M, Value);
220   Second->ReadCounter(M, Value);
221 }
222 
223 bool ChainedASTReaderListener::needsInputFileVisitation() {
224   return First->needsInputFileVisitation() ||
225          Second->needsInputFileVisitation();
226 }
227 
228 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
229   return First->needsSystemInputFileVisitation() ||
230   Second->needsSystemInputFileVisitation();
231 }
232 
233 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
234                                                ModuleKind Kind) {
235   First->visitModuleFile(Filename, Kind);
236   Second->visitModuleFile(Filename, Kind);
237 }
238 
239 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
240                                               bool isSystem,
241                                               bool isOverridden,
242                                               bool isExplicitModule) {
243   bool Continue = false;
244   if (First->needsInputFileVisitation() &&
245       (!isSystem || First->needsSystemInputFileVisitation()))
246     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
247                                       isExplicitModule);
248   if (Second->needsInputFileVisitation() &&
249       (!isSystem || Second->needsSystemInputFileVisitation()))
250     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
251                                        isExplicitModule);
252   return Continue;
253 }
254 
255 void ChainedASTReaderListener::readModuleFileExtension(
256        const ModuleFileExtensionMetadata &Metadata) {
257   First->readModuleFileExtension(Metadata);
258   Second->readModuleFileExtension(Metadata);
259 }
260 
261 //===----------------------------------------------------------------------===//
262 // PCH validator implementation
263 //===----------------------------------------------------------------------===//
264 
265 ASTReaderListener::~ASTReaderListener() = default;
266 
267 /// Compare the given set of language options against an existing set of
268 /// language options.
269 ///
270 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
271 /// \param AllowCompatibleDifferences If true, differences between compatible
272 ///        language options will be permitted.
273 ///
274 /// \returns true if the languagae options mis-match, false otherwise.
275 static bool checkLanguageOptions(const LangOptions &LangOpts,
276                                  const LangOptions &ExistingLangOpts,
277                                  DiagnosticsEngine *Diags,
278                                  bool AllowCompatibleDifferences = true) {
279 #define LANGOPT(Name, Bits, Default, Description)                 \
280   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
281     if (Diags)                                                    \
282       Diags->Report(diag::err_pch_langopt_mismatch)               \
283         << Description << LangOpts.Name << ExistingLangOpts.Name; \
284     return true;                                                  \
285   }
286 
287 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
288   if (ExistingLangOpts.Name != LangOpts.Name) {           \
289     if (Diags)                                            \
290       Diags->Report(diag::err_pch_langopt_value_mismatch) \
291         << Description;                                   \
292     return true;                                          \
293   }
294 
295 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
296   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
297     if (Diags)                                                 \
298       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
299         << Description;                                        \
300     return true;                                               \
301   }
302 
303 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
304   if (!AllowCompatibleDifferences)                            \
305     LANGOPT(Name, Bits, Default, Description)
306 
307 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
308   if (!AllowCompatibleDifferences)                                 \
309     ENUM_LANGOPT(Name, Bits, Default, Description)
310 
311 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
312   if (!AllowCompatibleDifferences)                                 \
313     VALUE_LANGOPT(Name, Bits, Default, Description)
314 
315 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
316 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
317 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
318 #include "clang/Basic/LangOptions.def"
319 
320   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
321     if (Diags)
322       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
323     return true;
324   }
325 
326   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
327     if (Diags)
328       Diags->Report(diag::err_pch_langopt_value_mismatch)
329       << "target Objective-C runtime";
330     return true;
331   }
332 
333   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
334       LangOpts.CommentOpts.BlockCommandNames) {
335     if (Diags)
336       Diags->Report(diag::err_pch_langopt_value_mismatch)
337         << "block command names";
338     return true;
339   }
340 
341   // Sanitizer feature mismatches are treated as compatible differences. If
342   // compatible differences aren't allowed, we still only want to check for
343   // mismatches of non-modular sanitizers (the only ones which can affect AST
344   // generation).
345   if (!AllowCompatibleDifferences) {
346     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
347     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
348     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
349     ExistingSanitizers.clear(ModularSanitizers);
350     ImportedSanitizers.clear(ModularSanitizers);
351     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
352       const std::string Flag = "-fsanitize=";
353       if (Diags) {
354 #define SANITIZER(NAME, ID)                                                    \
355   {                                                                            \
356     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
357     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
358     if (InExistingModule != InImportedModule)                                  \
359       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
360           << InExistingModule << (Flag + NAME);                                \
361   }
362 #include "clang/Basic/Sanitizers.def"
363       }
364       return true;
365     }
366   }
367 
368   return false;
369 }
370 
371 /// Compare the given set of target options against an existing set of
372 /// target options.
373 ///
374 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
375 ///
376 /// \returns true if the target options mis-match, false otherwise.
377 static bool checkTargetOptions(const TargetOptions &TargetOpts,
378                                const TargetOptions &ExistingTargetOpts,
379                                DiagnosticsEngine *Diags,
380                                bool AllowCompatibleDifferences = true) {
381 #define CHECK_TARGET_OPT(Field, Name)                             \
382   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
383     if (Diags)                                                    \
384       Diags->Report(diag::err_pch_targetopt_mismatch)             \
385         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
386     return true;                                                  \
387   }
388 
389   // The triple and ABI must match exactly.
390   CHECK_TARGET_OPT(Triple, "target");
391   CHECK_TARGET_OPT(ABI, "target ABI");
392 
393   // We can tolerate different CPUs in many cases, notably when one CPU
394   // supports a strict superset of another. When allowing compatible
395   // differences skip this check.
396   if (!AllowCompatibleDifferences) {
397     CHECK_TARGET_OPT(CPU, "target CPU");
398     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
399   }
400 
401 #undef CHECK_TARGET_OPT
402 
403   // Compare feature sets.
404   SmallVector<StringRef, 4> ExistingFeatures(
405                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
406                                              ExistingTargetOpts.FeaturesAsWritten.end());
407   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
408                                          TargetOpts.FeaturesAsWritten.end());
409   llvm::sort(ExistingFeatures);
410   llvm::sort(ReadFeatures);
411 
412   // We compute the set difference in both directions explicitly so that we can
413   // diagnose the differences differently.
414   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
415   std::set_difference(
416       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
417       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
418   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
419                       ExistingFeatures.begin(), ExistingFeatures.end(),
420                       std::back_inserter(UnmatchedReadFeatures));
421 
422   // If we are allowing compatible differences and the read feature set is
423   // a strict subset of the existing feature set, there is nothing to diagnose.
424   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
425     return false;
426 
427   if (Diags) {
428     for (StringRef Feature : UnmatchedReadFeatures)
429       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
430           << /* is-existing-feature */ false << Feature;
431     for (StringRef Feature : UnmatchedExistingFeatures)
432       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
433           << /* is-existing-feature */ true << Feature;
434   }
435 
436   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
437 }
438 
439 bool
440 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
441                                   bool Complain,
442                                   bool AllowCompatibleDifferences) {
443   const LangOptions &ExistingLangOpts = PP.getLangOpts();
444   return checkLanguageOptions(LangOpts, ExistingLangOpts,
445                               Complain ? &Reader.Diags : nullptr,
446                               AllowCompatibleDifferences);
447 }
448 
449 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
450                                      bool Complain,
451                                      bool AllowCompatibleDifferences) {
452   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
453   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
454                             Complain ? &Reader.Diags : nullptr,
455                             AllowCompatibleDifferences);
456 }
457 
458 namespace {
459 
460 using MacroDefinitionsMap =
461     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
462 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
463 
464 } // namespace
465 
466 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
467                                          DiagnosticsEngine &Diags,
468                                          bool Complain) {
469   using Level = DiagnosticsEngine::Level;
470 
471   // Check current mappings for new -Werror mappings, and the stored mappings
472   // for cases that were explicitly mapped to *not* be errors that are now
473   // errors because of options like -Werror.
474   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
475 
476   for (DiagnosticsEngine *MappingSource : MappingSources) {
477     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
478       diag::kind DiagID = DiagIDMappingPair.first;
479       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
480       if (CurLevel < DiagnosticsEngine::Error)
481         continue; // not significant
482       Level StoredLevel =
483           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
484       if (StoredLevel < DiagnosticsEngine::Error) {
485         if (Complain)
486           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
487               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
488         return true;
489       }
490     }
491   }
492 
493   return false;
494 }
495 
496 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
497   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
498   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
499     return true;
500   return Ext >= diag::Severity::Error;
501 }
502 
503 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
504                                     DiagnosticsEngine &Diags,
505                                     bool IsSystem, bool Complain) {
506   // Top-level options
507   if (IsSystem) {
508     if (Diags.getSuppressSystemWarnings())
509       return false;
510     // If -Wsystem-headers was not enabled before, be conservative
511     if (StoredDiags.getSuppressSystemWarnings()) {
512       if (Complain)
513         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
514       return true;
515     }
516   }
517 
518   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
519     if (Complain)
520       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
521     return true;
522   }
523 
524   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
525       !StoredDiags.getEnableAllWarnings()) {
526     if (Complain)
527       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
528     return true;
529   }
530 
531   if (isExtHandlingFromDiagsError(Diags) &&
532       !isExtHandlingFromDiagsError(StoredDiags)) {
533     if (Complain)
534       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
535     return true;
536   }
537 
538   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
539 }
540 
541 /// Return the top import module if it is implicit, nullptr otherwise.
542 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
543                                           Preprocessor &PP) {
544   // If the original import came from a file explicitly generated by the user,
545   // don't check the diagnostic mappings.
546   // FIXME: currently this is approximated by checking whether this is not a
547   // module import of an implicitly-loaded module file.
548   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
549   // the transitive closure of its imports, since unrelated modules cannot be
550   // imported until after this module finishes validation.
551   ModuleFile *TopImport = &*ModuleMgr.rbegin();
552   while (!TopImport->ImportedBy.empty())
553     TopImport = TopImport->ImportedBy[0];
554   if (TopImport->Kind != MK_ImplicitModule)
555     return nullptr;
556 
557   StringRef ModuleName = TopImport->ModuleName;
558   assert(!ModuleName.empty() && "diagnostic options read before module name");
559 
560   Module *M =
561       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
562   assert(M && "missing module");
563   return M;
564 }
565 
566 bool PCHValidator::ReadDiagnosticOptions(
567     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
568   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
569   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
570   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
571       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
572   // This should never fail, because we would have processed these options
573   // before writing them to an ASTFile.
574   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
575 
576   ModuleManager &ModuleMgr = Reader.getModuleManager();
577   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
578 
579   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
580   if (!TopM)
581     return false;
582 
583   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
584   // contains the union of their flags.
585   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
586                                  Complain);
587 }
588 
589 /// Collect the macro definitions provided by the given preprocessor
590 /// options.
591 static void
592 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
593                         MacroDefinitionsMap &Macros,
594                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
595   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
596     StringRef Macro = PPOpts.Macros[I].first;
597     bool IsUndef = PPOpts.Macros[I].second;
598 
599     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
600     StringRef MacroName = MacroPair.first;
601     StringRef MacroBody = MacroPair.second;
602 
603     // For an #undef'd macro, we only care about the name.
604     if (IsUndef) {
605       if (MacroNames && !Macros.count(MacroName))
606         MacroNames->push_back(MacroName);
607 
608       Macros[MacroName] = std::make_pair("", true);
609       continue;
610     }
611 
612     // For a #define'd macro, figure out the actual definition.
613     if (MacroName.size() == Macro.size())
614       MacroBody = "1";
615     else {
616       // Note: GCC drops anything following an end-of-line character.
617       StringRef::size_type End = MacroBody.find_first_of("\n\r");
618       MacroBody = MacroBody.substr(0, End);
619     }
620 
621     if (MacroNames && !Macros.count(MacroName))
622       MacroNames->push_back(MacroName);
623     Macros[MacroName] = std::make_pair(MacroBody, false);
624   }
625 }
626 
627 /// Check the preprocessor options deserialized from the control block
628 /// against the preprocessor options in an existing preprocessor.
629 ///
630 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
631 /// \param Validate If true, validate preprocessor options. If false, allow
632 ///        macros defined by \p ExistingPPOpts to override those defined by
633 ///        \p PPOpts in SuggestedPredefines.
634 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
635                                      const PreprocessorOptions &ExistingPPOpts,
636                                      DiagnosticsEngine *Diags,
637                                      FileManager &FileMgr,
638                                      std::string &SuggestedPredefines,
639                                      const LangOptions &LangOpts,
640                                      bool Validate = true) {
641   // Check macro definitions.
642   MacroDefinitionsMap ASTFileMacros;
643   collectMacroDefinitions(PPOpts, ASTFileMacros);
644   MacroDefinitionsMap ExistingMacros;
645   SmallVector<StringRef, 4> ExistingMacroNames;
646   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
647 
648   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
649     // Dig out the macro definition in the existing preprocessor options.
650     StringRef MacroName = ExistingMacroNames[I];
651     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
652 
653     // Check whether we know anything about this macro name or not.
654     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
655         ASTFileMacros.find(MacroName);
656     if (!Validate || Known == ASTFileMacros.end()) {
657       // FIXME: Check whether this identifier was referenced anywhere in the
658       // AST file. If so, we should reject the AST file. Unfortunately, this
659       // information isn't in the control block. What shall we do about it?
660 
661       if (Existing.second) {
662         SuggestedPredefines += "#undef ";
663         SuggestedPredefines += MacroName.str();
664         SuggestedPredefines += '\n';
665       } else {
666         SuggestedPredefines += "#define ";
667         SuggestedPredefines += MacroName.str();
668         SuggestedPredefines += ' ';
669         SuggestedPredefines += Existing.first.str();
670         SuggestedPredefines += '\n';
671       }
672       continue;
673     }
674 
675     // If the macro was defined in one but undef'd in the other, we have a
676     // conflict.
677     if (Existing.second != Known->second.second) {
678       if (Diags) {
679         Diags->Report(diag::err_pch_macro_def_undef)
680           << MacroName << Known->second.second;
681       }
682       return true;
683     }
684 
685     // If the macro was #undef'd in both, or if the macro bodies are identical,
686     // it's fine.
687     if (Existing.second || Existing.first == Known->second.first)
688       continue;
689 
690     // The macro bodies differ; complain.
691     if (Diags) {
692       Diags->Report(diag::err_pch_macro_def_conflict)
693         << MacroName << Known->second.first << Existing.first;
694     }
695     return true;
696   }
697 
698   // Check whether we're using predefines.
699   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
700     if (Diags) {
701       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
702     }
703     return true;
704   }
705 
706   // Detailed record is important since it is used for the module cache hash.
707   if (LangOpts.Modules &&
708       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
709     if (Diags) {
710       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
711     }
712     return true;
713   }
714 
715   // Compute the #include and #include_macros lines we need.
716   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
717     StringRef File = ExistingPPOpts.Includes[I];
718 
719     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
720         !ExistingPPOpts.PCHThroughHeader.empty()) {
721       // In case the through header is an include, we must add all the includes
722       // to the predefines so the start point can be determined.
723       SuggestedPredefines += "#include \"";
724       SuggestedPredefines += File;
725       SuggestedPredefines += "\"\n";
726       continue;
727     }
728 
729     if (File == ExistingPPOpts.ImplicitPCHInclude)
730       continue;
731 
732     if (llvm::is_contained(PPOpts.Includes, File))
733       continue;
734 
735     SuggestedPredefines += "#include \"";
736     SuggestedPredefines += File;
737     SuggestedPredefines += "\"\n";
738   }
739 
740   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
741     StringRef File = ExistingPPOpts.MacroIncludes[I];
742     if (llvm::is_contained(PPOpts.MacroIncludes, File))
743       continue;
744 
745     SuggestedPredefines += "#__include_macros \"";
746     SuggestedPredefines += File;
747     SuggestedPredefines += "\"\n##\n";
748   }
749 
750   return false;
751 }
752 
753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
754                                            bool Complain,
755                                            std::string &SuggestedPredefines) {
756   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
757 
758   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
759                                   Complain? &Reader.Diags : nullptr,
760                                   PP.getFileManager(),
761                                   SuggestedPredefines,
762                                   PP.getLangOpts());
763 }
764 
765 bool SimpleASTReaderListener::ReadPreprocessorOptions(
766                                   const PreprocessorOptions &PPOpts,
767                                   bool Complain,
768                                   std::string &SuggestedPredefines) {
769   return checkPreprocessorOptions(PPOpts,
770                                   PP.getPreprocessorOpts(),
771                                   nullptr,
772                                   PP.getFileManager(),
773                                   SuggestedPredefines,
774                                   PP.getLangOpts(),
775                                   false);
776 }
777 
778 /// Check the header search options deserialized from the control block
779 /// against the header search options in an existing preprocessor.
780 ///
781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
783                                      StringRef SpecificModuleCachePath,
784                                      StringRef ExistingModuleCachePath,
785                                      DiagnosticsEngine *Diags,
786                                      const LangOptions &LangOpts,
787                                      const PreprocessorOptions &PPOpts) {
788   if (LangOpts.Modules) {
789     if (SpecificModuleCachePath != ExistingModuleCachePath &&
790         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
791       if (Diags)
792         Diags->Report(diag::err_pch_modulecache_mismatch)
793           << SpecificModuleCachePath << ExistingModuleCachePath;
794       return true;
795     }
796   }
797 
798   return false;
799 }
800 
801 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
802                                            StringRef SpecificModuleCachePath,
803                                            bool Complain) {
804   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
805                                   PP.getHeaderSearchInfo().getModuleCachePath(),
806                                   Complain ? &Reader.Diags : nullptr,
807                                   PP.getLangOpts(), PP.getPreprocessorOpts());
808 }
809 
810 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
811   PP.setCounterValue(Value);
812 }
813 
814 //===----------------------------------------------------------------------===//
815 // AST reader implementation
816 //===----------------------------------------------------------------------===//
817 
818 static uint64_t readULEB(const unsigned char *&P) {
819   unsigned Length = 0;
820   const char *Error = nullptr;
821 
822   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
823   if (Error)
824     llvm::report_fatal_error(Error);
825   P += Length;
826   return Val;
827 }
828 
829 /// Read ULEB-encoded key length and data length.
830 static std::pair<unsigned, unsigned>
831 readULEBKeyDataLength(const unsigned char *&P) {
832   unsigned KeyLen = readULEB(P);
833   if ((unsigned)KeyLen != KeyLen)
834     llvm::report_fatal_error("key too large");
835 
836   unsigned DataLen = readULEB(P);
837   if ((unsigned)DataLen != DataLen)
838     llvm::report_fatal_error("data too large");
839 
840   return std::make_pair(KeyLen, DataLen);
841 }
842 
843 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
844                                            bool TakeOwnership) {
845   DeserializationListener = Listener;
846   OwnsDeserializationListener = TakeOwnership;
847 }
848 
849 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
850   return serialization::ComputeHash(Sel);
851 }
852 
853 std::pair<unsigned, unsigned>
854 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
855   return readULEBKeyDataLength(d);
856 }
857 
858 ASTSelectorLookupTrait::internal_key_type
859 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
860   using namespace llvm::support;
861 
862   SelectorTable &SelTable = Reader.getContext().Selectors;
863   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
864   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
865       F, endian::readNext<uint32_t, little, unaligned>(d));
866   if (N == 0)
867     return SelTable.getNullarySelector(FirstII);
868   else if (N == 1)
869     return SelTable.getUnarySelector(FirstII);
870 
871   SmallVector<IdentifierInfo *, 16> Args;
872   Args.push_back(FirstII);
873   for (unsigned I = 1; I != N; ++I)
874     Args.push_back(Reader.getLocalIdentifier(
875         F, endian::readNext<uint32_t, little, unaligned>(d)));
876 
877   return SelTable.getSelector(N, Args.data());
878 }
879 
880 ASTSelectorLookupTrait::data_type
881 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
882                                  unsigned DataLen) {
883   using namespace llvm::support;
884 
885   data_type Result;
886 
887   Result.ID = Reader.getGlobalSelectorID(
888       F, endian::readNext<uint32_t, little, unaligned>(d));
889   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
890   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
891   Result.InstanceBits = FullInstanceBits & 0x3;
892   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
893   Result.FactoryBits = FullFactoryBits & 0x3;
894   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
895   unsigned NumInstanceMethods = FullInstanceBits >> 3;
896   unsigned NumFactoryMethods = FullFactoryBits >> 3;
897 
898   // Load instance methods
899   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
900     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
901             F, endian::readNext<uint32_t, little, unaligned>(d)))
902       Result.Instance.push_back(Method);
903   }
904 
905   // Load factory methods
906   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
907     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
908             F, endian::readNext<uint32_t, little, unaligned>(d)))
909       Result.Factory.push_back(Method);
910   }
911 
912   return Result;
913 }
914 
915 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
916   return llvm::djbHash(a);
917 }
918 
919 std::pair<unsigned, unsigned>
920 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
921   return readULEBKeyDataLength(d);
922 }
923 
924 ASTIdentifierLookupTraitBase::internal_key_type
925 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
926   assert(n >= 2 && d[n-1] == '\0');
927   return StringRef((const char*) d, n-1);
928 }
929 
930 /// Whether the given identifier is "interesting".
931 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
932                                     bool IsModule) {
933   return II.hadMacroDefinition() || II.isPoisoned() ||
934          (!IsModule && II.getObjCOrBuiltinID()) ||
935          II.hasRevertedTokenIDToIdentifier() ||
936          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
937           II.getFETokenInfo());
938 }
939 
940 static bool readBit(unsigned &Bits) {
941   bool Value = Bits & 0x1;
942   Bits >>= 1;
943   return Value;
944 }
945 
946 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
947   using namespace llvm::support;
948 
949   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
950   return Reader.getGlobalIdentifierID(F, RawID >> 1);
951 }
952 
953 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
954   if (!II.isFromAST()) {
955     II.setIsFromAST();
956     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
957     if (isInterestingIdentifier(Reader, II, IsModule))
958       II.setChangedSinceDeserialization();
959   }
960 }
961 
962 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
963                                                    const unsigned char* d,
964                                                    unsigned DataLen) {
965   using namespace llvm::support;
966 
967   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
968   bool IsInteresting = RawID & 0x01;
969 
970   // Wipe out the "is interesting" bit.
971   RawID = RawID >> 1;
972 
973   // Build the IdentifierInfo and link the identifier ID with it.
974   IdentifierInfo *II = KnownII;
975   if (!II) {
976     II = &Reader.getIdentifierTable().getOwn(k);
977     KnownII = II;
978   }
979   markIdentifierFromAST(Reader, *II);
980   Reader.markIdentifierUpToDate(II);
981 
982   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
983   if (!IsInteresting) {
984     // For uninteresting identifiers, there's nothing else to do. Just notify
985     // the reader that we've finished loading this identifier.
986     Reader.SetIdentifierInfo(ID, II);
987     return II;
988   }
989 
990   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
991   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
992   bool CPlusPlusOperatorKeyword = readBit(Bits);
993   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
994   bool Poisoned = readBit(Bits);
995   bool ExtensionToken = readBit(Bits);
996   bool HadMacroDefinition = readBit(Bits);
997 
998   assert(Bits == 0 && "Extra bits in the identifier?");
999   DataLen -= 8;
1000 
1001   // Set or check the various bits in the IdentifierInfo structure.
1002   // Token IDs are read-only.
1003   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1004     II->revertTokenIDToIdentifier();
1005   if (!F.isModule())
1006     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1007   assert(II->isExtensionToken() == ExtensionToken &&
1008          "Incorrect extension token flag");
1009   (void)ExtensionToken;
1010   if (Poisoned)
1011     II->setIsPoisoned(true);
1012   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1013          "Incorrect C++ operator keyword flag");
1014   (void)CPlusPlusOperatorKeyword;
1015 
1016   // If this identifier is a macro, deserialize the macro
1017   // definition.
1018   if (HadMacroDefinition) {
1019     uint32_t MacroDirectivesOffset =
1020         endian::readNext<uint32_t, little, unaligned>(d);
1021     DataLen -= 4;
1022 
1023     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1024   }
1025 
1026   Reader.SetIdentifierInfo(ID, II);
1027 
1028   // Read all of the declarations visible at global scope with this
1029   // name.
1030   if (DataLen > 0) {
1031     SmallVector<uint32_t, 4> DeclIDs;
1032     for (; DataLen > 0; DataLen -= 4)
1033       DeclIDs.push_back(Reader.getGlobalDeclID(
1034           F, endian::readNext<uint32_t, little, unaligned>(d)));
1035     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1036   }
1037 
1038   return II;
1039 }
1040 
1041 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1042     : Kind(Name.getNameKind()) {
1043   switch (Kind) {
1044   case DeclarationName::Identifier:
1045     Data = (uint64_t)Name.getAsIdentifierInfo();
1046     break;
1047   case DeclarationName::ObjCZeroArgSelector:
1048   case DeclarationName::ObjCOneArgSelector:
1049   case DeclarationName::ObjCMultiArgSelector:
1050     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1051     break;
1052   case DeclarationName::CXXOperatorName:
1053     Data = Name.getCXXOverloadedOperator();
1054     break;
1055   case DeclarationName::CXXLiteralOperatorName:
1056     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1057     break;
1058   case DeclarationName::CXXDeductionGuideName:
1059     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1060                ->getDeclName().getAsIdentifierInfo();
1061     break;
1062   case DeclarationName::CXXConstructorName:
1063   case DeclarationName::CXXDestructorName:
1064   case DeclarationName::CXXConversionFunctionName:
1065   case DeclarationName::CXXUsingDirective:
1066     Data = 0;
1067     break;
1068   }
1069 }
1070 
1071 unsigned DeclarationNameKey::getHash() const {
1072   llvm::FoldingSetNodeID ID;
1073   ID.AddInteger(Kind);
1074 
1075   switch (Kind) {
1076   case DeclarationName::Identifier:
1077   case DeclarationName::CXXLiteralOperatorName:
1078   case DeclarationName::CXXDeductionGuideName:
1079     ID.AddString(((IdentifierInfo*)Data)->getName());
1080     break;
1081   case DeclarationName::ObjCZeroArgSelector:
1082   case DeclarationName::ObjCOneArgSelector:
1083   case DeclarationName::ObjCMultiArgSelector:
1084     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1085     break;
1086   case DeclarationName::CXXOperatorName:
1087     ID.AddInteger((OverloadedOperatorKind)Data);
1088     break;
1089   case DeclarationName::CXXConstructorName:
1090   case DeclarationName::CXXDestructorName:
1091   case DeclarationName::CXXConversionFunctionName:
1092   case DeclarationName::CXXUsingDirective:
1093     break;
1094   }
1095 
1096   return ID.ComputeHash();
1097 }
1098 
1099 ModuleFile *
1100 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1101   using namespace llvm::support;
1102 
1103   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1104   return Reader.getLocalModuleFile(F, ModuleFileID);
1105 }
1106 
1107 std::pair<unsigned, unsigned>
1108 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1109   return readULEBKeyDataLength(d);
1110 }
1111 
1112 ASTDeclContextNameLookupTrait::internal_key_type
1113 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1114   using namespace llvm::support;
1115 
1116   auto Kind = (DeclarationName::NameKind)*d++;
1117   uint64_t Data;
1118   switch (Kind) {
1119   case DeclarationName::Identifier:
1120   case DeclarationName::CXXLiteralOperatorName:
1121   case DeclarationName::CXXDeductionGuideName:
1122     Data = (uint64_t)Reader.getLocalIdentifier(
1123         F, endian::readNext<uint32_t, little, unaligned>(d));
1124     break;
1125   case DeclarationName::ObjCZeroArgSelector:
1126   case DeclarationName::ObjCOneArgSelector:
1127   case DeclarationName::ObjCMultiArgSelector:
1128     Data =
1129         (uint64_t)Reader.getLocalSelector(
1130                              F, endian::readNext<uint32_t, little, unaligned>(
1131                                     d)).getAsOpaquePtr();
1132     break;
1133   case DeclarationName::CXXOperatorName:
1134     Data = *d++; // OverloadedOperatorKind
1135     break;
1136   case DeclarationName::CXXConstructorName:
1137   case DeclarationName::CXXDestructorName:
1138   case DeclarationName::CXXConversionFunctionName:
1139   case DeclarationName::CXXUsingDirective:
1140     Data = 0;
1141     break;
1142   }
1143 
1144   return DeclarationNameKey(Kind, Data);
1145 }
1146 
1147 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1148                                                  const unsigned char *d,
1149                                                  unsigned DataLen,
1150                                                  data_type_builder &Val) {
1151   using namespace llvm::support;
1152 
1153   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1154     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1155     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1156   }
1157 }
1158 
1159 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1160                                               BitstreamCursor &Cursor,
1161                                               uint64_t Offset,
1162                                               DeclContext *DC) {
1163   assert(Offset != 0);
1164 
1165   SavedStreamPosition SavedPosition(Cursor);
1166   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1167     Error(std::move(Err));
1168     return true;
1169   }
1170 
1171   RecordData Record;
1172   StringRef Blob;
1173   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1174   if (!MaybeCode) {
1175     Error(MaybeCode.takeError());
1176     return true;
1177   }
1178   unsigned Code = MaybeCode.get();
1179 
1180   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1181   if (!MaybeRecCode) {
1182     Error(MaybeRecCode.takeError());
1183     return true;
1184   }
1185   unsigned RecCode = MaybeRecCode.get();
1186   if (RecCode != DECL_CONTEXT_LEXICAL) {
1187     Error("Expected lexical block");
1188     return true;
1189   }
1190 
1191   assert(!isa<TranslationUnitDecl>(DC) &&
1192          "expected a TU_UPDATE_LEXICAL record for TU");
1193   // If we are handling a C++ class template instantiation, we can see multiple
1194   // lexical updates for the same record. It's important that we select only one
1195   // of them, so that field numbering works properly. Just pick the first one we
1196   // see.
1197   auto &Lex = LexicalDecls[DC];
1198   if (!Lex.first) {
1199     Lex = std::make_pair(
1200         &M, llvm::makeArrayRef(
1201                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1202                     Blob.data()),
1203                 Blob.size() / 4));
1204   }
1205   DC->setHasExternalLexicalStorage(true);
1206   return false;
1207 }
1208 
1209 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1210                                               BitstreamCursor &Cursor,
1211                                               uint64_t Offset,
1212                                               DeclID ID) {
1213   assert(Offset != 0);
1214 
1215   SavedStreamPosition SavedPosition(Cursor);
1216   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1217     Error(std::move(Err));
1218     return true;
1219   }
1220 
1221   RecordData Record;
1222   StringRef Blob;
1223   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1224   if (!MaybeCode) {
1225     Error(MaybeCode.takeError());
1226     return true;
1227   }
1228   unsigned Code = MaybeCode.get();
1229 
1230   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1231   if (!MaybeRecCode) {
1232     Error(MaybeRecCode.takeError());
1233     return true;
1234   }
1235   unsigned RecCode = MaybeRecCode.get();
1236   if (RecCode != DECL_CONTEXT_VISIBLE) {
1237     Error("Expected visible lookup table block");
1238     return true;
1239   }
1240 
1241   // We can't safely determine the primary context yet, so delay attaching the
1242   // lookup table until we're done with recursive deserialization.
1243   auto *Data = (const unsigned char*)Blob.data();
1244   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1245   return false;
1246 }
1247 
1248 void ASTReader::Error(StringRef Msg) const {
1249   Error(diag::err_fe_pch_malformed, Msg);
1250   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1251       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1252     Diag(diag::note_module_cache_path)
1253       << PP.getHeaderSearchInfo().getModuleCachePath();
1254   }
1255 }
1256 
1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1258                       StringRef Arg3) const {
1259   if (Diags.isDiagnosticInFlight())
1260     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1261   else
1262     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1263 }
1264 
1265 void ASTReader::Error(llvm::Error &&Err) const {
1266   llvm::Error RemainingErr =
1267       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1268         auto Diag = E.getDiagnostic().second;
1269 
1270         // Ideally we'd just emit it, but have to handle a possible in-flight
1271         // diagnostic. Note that the location is currently ignored as well.
1272         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1273         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1274         StringRef Arg1, Arg2, Arg3;
1275         switch (NumArgs) {
1276         case 3:
1277           Arg3 = Diag.getStringArg(2);
1278           LLVM_FALLTHROUGH;
1279         case 2:
1280           Arg2 = Diag.getStringArg(1);
1281           LLVM_FALLTHROUGH;
1282         case 1:
1283           Arg1 = Diag.getStringArg(0);
1284         }
1285         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1286       });
1287   if (RemainingErr)
1288     Error(toString(std::move(RemainingErr)));
1289 }
1290 
1291 //===----------------------------------------------------------------------===//
1292 // Source Manager Deserialization
1293 //===----------------------------------------------------------------------===//
1294 
1295 /// Read the line table in the source manager block.
1296 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1297   unsigned Idx = 0;
1298   LineTableInfo &LineTable = SourceMgr.getLineTable();
1299 
1300   // Parse the file names
1301   std::map<int, int> FileIDs;
1302   FileIDs[-1] = -1; // For unspecified filenames.
1303   for (unsigned I = 0; Record[Idx]; ++I) {
1304     // Extract the file name
1305     auto Filename = ReadPath(F, Record, Idx);
1306     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1307   }
1308   ++Idx;
1309 
1310   // Parse the line entries
1311   std::vector<LineEntry> Entries;
1312   while (Idx < Record.size()) {
1313     int FID = Record[Idx++];
1314     assert(FID >= 0 && "Serialized line entries for non-local file.");
1315     // Remap FileID from 1-based old view.
1316     FID += F.SLocEntryBaseID - 1;
1317 
1318     // Extract the line entries
1319     unsigned NumEntries = Record[Idx++];
1320     assert(NumEntries && "no line entries for file ID");
1321     Entries.clear();
1322     Entries.reserve(NumEntries);
1323     for (unsigned I = 0; I != NumEntries; ++I) {
1324       unsigned FileOffset = Record[Idx++];
1325       unsigned LineNo = Record[Idx++];
1326       int FilenameID = FileIDs[Record[Idx++]];
1327       SrcMgr::CharacteristicKind FileKind
1328         = (SrcMgr::CharacteristicKind)Record[Idx++];
1329       unsigned IncludeOffset = Record[Idx++];
1330       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1331                                        FileKind, IncludeOffset));
1332     }
1333     LineTable.AddEntry(FileID::get(FID), Entries);
1334   }
1335 }
1336 
1337 /// Read a source manager block
1338 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1339   using namespace SrcMgr;
1340 
1341   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1342 
1343   // Set the source-location entry cursor to the current position in
1344   // the stream. This cursor will be used to read the contents of the
1345   // source manager block initially, and then lazily read
1346   // source-location entries as needed.
1347   SLocEntryCursor = F.Stream;
1348 
1349   // The stream itself is going to skip over the source manager block.
1350   if (llvm::Error Err = F.Stream.SkipBlock())
1351     return Err;
1352 
1353   // Enter the source manager block.
1354   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1355     return Err;
1356   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1357 
1358   RecordData Record;
1359   while (true) {
1360     Expected<llvm::BitstreamEntry> MaybeE =
1361         SLocEntryCursor.advanceSkippingSubblocks();
1362     if (!MaybeE)
1363       return MaybeE.takeError();
1364     llvm::BitstreamEntry E = MaybeE.get();
1365 
1366     switch (E.Kind) {
1367     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1368     case llvm::BitstreamEntry::Error:
1369       return llvm::createStringError(std::errc::illegal_byte_sequence,
1370                                      "malformed block record in AST file");
1371     case llvm::BitstreamEntry::EndBlock:
1372       return llvm::Error::success();
1373     case llvm::BitstreamEntry::Record:
1374       // The interesting case.
1375       break;
1376     }
1377 
1378     // Read a record.
1379     Record.clear();
1380     StringRef Blob;
1381     Expected<unsigned> MaybeRecord =
1382         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1383     if (!MaybeRecord)
1384       return MaybeRecord.takeError();
1385     switch (MaybeRecord.get()) {
1386     default:  // Default behavior: ignore.
1387       break;
1388 
1389     case SM_SLOC_FILE_ENTRY:
1390     case SM_SLOC_BUFFER_ENTRY:
1391     case SM_SLOC_EXPANSION_ENTRY:
1392       // Once we hit one of the source location entries, we're done.
1393       return llvm::Error::success();
1394     }
1395   }
1396 }
1397 
1398 /// If a header file is not found at the path that we expect it to be
1399 /// and the PCH file was moved from its original location, try to resolve the
1400 /// file by assuming that header+PCH were moved together and the header is in
1401 /// the same place relative to the PCH.
1402 static std::string
1403 resolveFileRelativeToOriginalDir(const std::string &Filename,
1404                                  const std::string &OriginalDir,
1405                                  const std::string &CurrDir) {
1406   assert(OriginalDir != CurrDir &&
1407          "No point trying to resolve the file if the PCH dir didn't change");
1408 
1409   using namespace llvm::sys;
1410 
1411   SmallString<128> filePath(Filename);
1412   fs::make_absolute(filePath);
1413   assert(path::is_absolute(OriginalDir));
1414   SmallString<128> currPCHPath(CurrDir);
1415 
1416   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1417                        fileDirE = path::end(path::parent_path(filePath));
1418   path::const_iterator origDirI = path::begin(OriginalDir),
1419                        origDirE = path::end(OriginalDir);
1420   // Skip the common path components from filePath and OriginalDir.
1421   while (fileDirI != fileDirE && origDirI != origDirE &&
1422          *fileDirI == *origDirI) {
1423     ++fileDirI;
1424     ++origDirI;
1425   }
1426   for (; origDirI != origDirE; ++origDirI)
1427     path::append(currPCHPath, "..");
1428   path::append(currPCHPath, fileDirI, fileDirE);
1429   path::append(currPCHPath, path::filename(Filename));
1430   return std::string(currPCHPath.str());
1431 }
1432 
1433 bool ASTReader::ReadSLocEntry(int ID) {
1434   if (ID == 0)
1435     return false;
1436 
1437   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1438     Error("source location entry ID out-of-range for AST file");
1439     return true;
1440   }
1441 
1442   // Local helper to read the (possibly-compressed) buffer data following the
1443   // entry record.
1444   auto ReadBuffer = [this](
1445       BitstreamCursor &SLocEntryCursor,
1446       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1447     RecordData Record;
1448     StringRef Blob;
1449     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1450     if (!MaybeCode) {
1451       Error(MaybeCode.takeError());
1452       return nullptr;
1453     }
1454     unsigned Code = MaybeCode.get();
1455 
1456     Expected<unsigned> MaybeRecCode =
1457         SLocEntryCursor.readRecord(Code, Record, &Blob);
1458     if (!MaybeRecCode) {
1459       Error(MaybeRecCode.takeError());
1460       return nullptr;
1461     }
1462     unsigned RecCode = MaybeRecCode.get();
1463 
1464     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1465       if (!llvm::zlib::isAvailable()) {
1466         Error("zlib is not available");
1467         return nullptr;
1468       }
1469       SmallString<0> Uncompressed;
1470       if (llvm::Error E =
1471               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1472         Error("could not decompress embedded file contents: " +
1473               llvm::toString(std::move(E)));
1474         return nullptr;
1475       }
1476       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1477     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1478       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1479     } else {
1480       Error("AST record has invalid code");
1481       return nullptr;
1482     }
1483   };
1484 
1485   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1486   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1487           F->SLocEntryOffsetsBase +
1488           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1489     Error(std::move(Err));
1490     return true;
1491   }
1492 
1493   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1494   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1495 
1496   ++NumSLocEntriesRead;
1497   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1498   if (!MaybeEntry) {
1499     Error(MaybeEntry.takeError());
1500     return true;
1501   }
1502   llvm::BitstreamEntry Entry = MaybeEntry.get();
1503 
1504   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1505     Error("incorrectly-formatted source location entry in AST file");
1506     return true;
1507   }
1508 
1509   RecordData Record;
1510   StringRef Blob;
1511   Expected<unsigned> MaybeSLOC =
1512       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1513   if (!MaybeSLOC) {
1514     Error(MaybeSLOC.takeError());
1515     return true;
1516   }
1517   switch (MaybeSLOC.get()) {
1518   default:
1519     Error("incorrectly-formatted source location entry in AST file");
1520     return true;
1521 
1522   case SM_SLOC_FILE_ENTRY: {
1523     // We will detect whether a file changed and return 'Failure' for it, but
1524     // we will also try to fail gracefully by setting up the SLocEntry.
1525     unsigned InputID = Record[4];
1526     InputFile IF = getInputFile(*F, InputID);
1527     Optional<FileEntryRef> File = IF.getFile();
1528     bool OverriddenBuffer = IF.isOverridden();
1529 
1530     // Note that we only check if a File was returned. If it was out-of-date
1531     // we have complained but we will continue creating a FileID to recover
1532     // gracefully.
1533     if (!File)
1534       return true;
1535 
1536     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1537     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1538       // This is the module's main file.
1539       IncludeLoc = getImportLocation(F);
1540     }
1541     SrcMgr::CharacteristicKind
1542       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1543     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1544                                         BaseOffset + Record[0]);
1545     SrcMgr::FileInfo &FileInfo =
1546           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1547     FileInfo.NumCreatedFIDs = Record[5];
1548     if (Record[3])
1549       FileInfo.setHasLineDirectives();
1550 
1551     unsigned NumFileDecls = Record[7];
1552     if (NumFileDecls && ContextObj) {
1553       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1554       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1555       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1556                                                              NumFileDecls));
1557     }
1558 
1559     const SrcMgr::ContentCache &ContentCache =
1560         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1561     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1562         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1563         !ContentCache.getBufferIfLoaded()) {
1564       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1565       if (!Buffer)
1566         return true;
1567       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1568     }
1569 
1570     break;
1571   }
1572 
1573   case SM_SLOC_BUFFER_ENTRY: {
1574     const char *Name = Blob.data();
1575     unsigned Offset = Record[0];
1576     SrcMgr::CharacteristicKind
1577       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1578     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1579     if (IncludeLoc.isInvalid() && F->isModule()) {
1580       IncludeLoc = getImportLocation(F);
1581     }
1582 
1583     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1584     if (!Buffer)
1585       return true;
1586     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1587                            BaseOffset + Offset, IncludeLoc);
1588     break;
1589   }
1590 
1591   case SM_SLOC_EXPANSION_ENTRY: {
1592     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1593     SourceMgr.createExpansionLoc(SpellingLoc,
1594                                      ReadSourceLocation(*F, Record[2]),
1595                                      ReadSourceLocation(*F, Record[3]),
1596                                      Record[5],
1597                                      Record[4],
1598                                      ID,
1599                                      BaseOffset + Record[0]);
1600     break;
1601   }
1602   }
1603 
1604   return false;
1605 }
1606 
1607 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1608   if (ID == 0)
1609     return std::make_pair(SourceLocation(), "");
1610 
1611   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1612     Error("source location entry ID out-of-range for AST file");
1613     return std::make_pair(SourceLocation(), "");
1614   }
1615 
1616   // Find which module file this entry lands in.
1617   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1618   if (!M->isModule())
1619     return std::make_pair(SourceLocation(), "");
1620 
1621   // FIXME: Can we map this down to a particular submodule? That would be
1622   // ideal.
1623   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1624 }
1625 
1626 /// Find the location where the module F is imported.
1627 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1628   if (F->ImportLoc.isValid())
1629     return F->ImportLoc;
1630 
1631   // Otherwise we have a PCH. It's considered to be "imported" at the first
1632   // location of its includer.
1633   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1634     // Main file is the importer.
1635     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1636     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1637   }
1638   return F->ImportedBy[0]->FirstLoc;
1639 }
1640 
1641 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1642 /// the abbreviations that are at the top of the block and then leave the cursor
1643 /// pointing into the block.
1644 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1645                                         unsigned BlockID,
1646                                         uint64_t *StartOfBlockOffset) {
1647   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1648     return Err;
1649 
1650   if (StartOfBlockOffset)
1651     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1652 
1653   while (true) {
1654     uint64_t Offset = Cursor.GetCurrentBitNo();
1655     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1656     if (!MaybeCode)
1657       return MaybeCode.takeError();
1658     unsigned Code = MaybeCode.get();
1659 
1660     // We expect all abbrevs to be at the start of the block.
1661     if (Code != llvm::bitc::DEFINE_ABBREV) {
1662       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1663         return Err;
1664       return llvm::Error::success();
1665     }
1666     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1667       return Err;
1668   }
1669 }
1670 
1671 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1672                            unsigned &Idx) {
1673   Token Tok;
1674   Tok.startToken();
1675   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1676   Tok.setLength(Record[Idx++]);
1677   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1678     Tok.setIdentifierInfo(II);
1679   Tok.setKind((tok::TokenKind)Record[Idx++]);
1680   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1681   return Tok;
1682 }
1683 
1684 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1685   BitstreamCursor &Stream = F.MacroCursor;
1686 
1687   // Keep track of where we are in the stream, then jump back there
1688   // after reading this macro.
1689   SavedStreamPosition SavedPosition(Stream);
1690 
1691   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1692     // FIXME this drops errors on the floor.
1693     consumeError(std::move(Err));
1694     return nullptr;
1695   }
1696   RecordData Record;
1697   SmallVector<IdentifierInfo*, 16> MacroParams;
1698   MacroInfo *Macro = nullptr;
1699   llvm::MutableArrayRef<Token> MacroTokens;
1700 
1701   while (true) {
1702     // Advance to the next record, but if we get to the end of the block, don't
1703     // pop it (removing all the abbreviations from the cursor) since we want to
1704     // be able to reseek within the block and read entries.
1705     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1706     Expected<llvm::BitstreamEntry> MaybeEntry =
1707         Stream.advanceSkippingSubblocks(Flags);
1708     if (!MaybeEntry) {
1709       Error(MaybeEntry.takeError());
1710       return Macro;
1711     }
1712     llvm::BitstreamEntry Entry = MaybeEntry.get();
1713 
1714     switch (Entry.Kind) {
1715     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1716     case llvm::BitstreamEntry::Error:
1717       Error("malformed block record in AST file");
1718       return Macro;
1719     case llvm::BitstreamEntry::EndBlock:
1720       return Macro;
1721     case llvm::BitstreamEntry::Record:
1722       // The interesting case.
1723       break;
1724     }
1725 
1726     // Read a record.
1727     Record.clear();
1728     PreprocessorRecordTypes RecType;
1729     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1730       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1731     else {
1732       Error(MaybeRecType.takeError());
1733       return Macro;
1734     }
1735     switch (RecType) {
1736     case PP_MODULE_MACRO:
1737     case PP_MACRO_DIRECTIVE_HISTORY:
1738       return Macro;
1739 
1740     case PP_MACRO_OBJECT_LIKE:
1741     case PP_MACRO_FUNCTION_LIKE: {
1742       // If we already have a macro, that means that we've hit the end
1743       // of the definition of the macro we were looking for. We're
1744       // done.
1745       if (Macro)
1746         return Macro;
1747 
1748       unsigned NextIndex = 1; // Skip identifier ID.
1749       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1750       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1751       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1752       MI->setIsUsed(Record[NextIndex++]);
1753       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1754       MacroTokens = MI->allocateTokens(Record[NextIndex++],
1755                                        PP.getPreprocessorAllocator());
1756       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1757         // Decode function-like macro info.
1758         bool isC99VarArgs = Record[NextIndex++];
1759         bool isGNUVarArgs = Record[NextIndex++];
1760         bool hasCommaPasting = Record[NextIndex++];
1761         MacroParams.clear();
1762         unsigned NumArgs = Record[NextIndex++];
1763         for (unsigned i = 0; i != NumArgs; ++i)
1764           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1765 
1766         // Install function-like macro info.
1767         MI->setIsFunctionLike();
1768         if (isC99VarArgs) MI->setIsC99Varargs();
1769         if (isGNUVarArgs) MI->setIsGNUVarargs();
1770         if (hasCommaPasting) MI->setHasCommaPasting();
1771         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1772       }
1773 
1774       // Remember that we saw this macro last so that we add the tokens that
1775       // form its body to it.
1776       Macro = MI;
1777 
1778       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1779           Record[NextIndex]) {
1780         // We have a macro definition. Register the association
1781         PreprocessedEntityID
1782             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1783         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1784         PreprocessingRecord::PPEntityID PPID =
1785             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1786         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1787             PPRec.getPreprocessedEntity(PPID));
1788         if (PPDef)
1789           PPRec.RegisterMacroDefinition(Macro, PPDef);
1790       }
1791 
1792       ++NumMacrosRead;
1793       break;
1794     }
1795 
1796     case PP_TOKEN: {
1797       // If we see a TOKEN before a PP_MACRO_*, then the file is
1798       // erroneous, just pretend we didn't see this.
1799       if (!Macro) break;
1800       if (MacroTokens.empty()) {
1801         Error("unexpected number of macro tokens for a macro in AST file");
1802         return Macro;
1803       }
1804 
1805       unsigned Idx = 0;
1806       MacroTokens[0] = ReadToken(F, Record, Idx);
1807       MacroTokens = MacroTokens.drop_front();
1808       break;
1809     }
1810     }
1811   }
1812 }
1813 
1814 PreprocessedEntityID
1815 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1816                                          unsigned LocalID) const {
1817   if (!M.ModuleOffsetMap.empty())
1818     ReadModuleOffsetMap(M);
1819 
1820   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1821     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1822   assert(I != M.PreprocessedEntityRemap.end()
1823          && "Invalid index into preprocessed entity index remap");
1824 
1825   return LocalID + I->second;
1826 }
1827 
1828 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1829   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1830 }
1831 
1832 HeaderFileInfoTrait::internal_key_type
1833 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1834   internal_key_type ikey = {FE->getSize(),
1835                             M.HasTimestamps ? FE->getModificationTime() : 0,
1836                             FE->getName(), /*Imported*/ false};
1837   return ikey;
1838 }
1839 
1840 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1841   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1842     return false;
1843 
1844   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1845     return true;
1846 
1847   // Determine whether the actual files are equivalent.
1848   FileManager &FileMgr = Reader.getFileManager();
1849   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1850     if (!Key.Imported) {
1851       if (auto File = FileMgr.getFile(Key.Filename))
1852         return *File;
1853       return nullptr;
1854     }
1855 
1856     std::string Resolved = std::string(Key.Filename);
1857     Reader.ResolveImportedPath(M, Resolved);
1858     if (auto File = FileMgr.getFile(Resolved))
1859       return *File;
1860     return nullptr;
1861   };
1862 
1863   const FileEntry *FEA = GetFile(a);
1864   const FileEntry *FEB = GetFile(b);
1865   return FEA && FEA == FEB;
1866 }
1867 
1868 std::pair<unsigned, unsigned>
1869 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1870   return readULEBKeyDataLength(d);
1871 }
1872 
1873 HeaderFileInfoTrait::internal_key_type
1874 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1875   using namespace llvm::support;
1876 
1877   internal_key_type ikey;
1878   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1879   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1880   ikey.Filename = (const char *)d;
1881   ikey.Imported = true;
1882   return ikey;
1883 }
1884 
1885 HeaderFileInfoTrait::data_type
1886 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1887                               unsigned DataLen) {
1888   using namespace llvm::support;
1889 
1890   const unsigned char *End = d + DataLen;
1891   HeaderFileInfo HFI;
1892   unsigned Flags = *d++;
1893   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1894   HFI.isImport |= (Flags >> 5) & 0x01;
1895   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1896   HFI.DirInfo = (Flags >> 1) & 0x07;
1897   HFI.IndexHeaderMapHeader = Flags & 0x01;
1898   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1899       M, endian::readNext<uint32_t, little, unaligned>(d));
1900   if (unsigned FrameworkOffset =
1901           endian::readNext<uint32_t, little, unaligned>(d)) {
1902     // The framework offset is 1 greater than the actual offset,
1903     // since 0 is used as an indicator for "no framework name".
1904     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1905     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1906   }
1907 
1908   assert((End - d) % 4 == 0 &&
1909          "Wrong data length in HeaderFileInfo deserialization");
1910   while (d != End) {
1911     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1912     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1913     LocalSMID >>= 2;
1914 
1915     // This header is part of a module. Associate it with the module to enable
1916     // implicit module import.
1917     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1918     Module *Mod = Reader.getSubmodule(GlobalSMID);
1919     FileManager &FileMgr = Reader.getFileManager();
1920     ModuleMap &ModMap =
1921         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1922 
1923     std::string Filename = std::string(key.Filename);
1924     if (key.Imported)
1925       Reader.ResolveImportedPath(M, Filename);
1926     // FIXME: NameAsWritten
1927     Module::Header H = {std::string(key.Filename), "",
1928                         *FileMgr.getFile(Filename)};
1929     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1930     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1931   }
1932 
1933   // This HeaderFileInfo was externally loaded.
1934   HFI.External = true;
1935   HFI.IsValid = true;
1936   return HFI;
1937 }
1938 
1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1940                                 uint32_t MacroDirectivesOffset) {
1941   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1942   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1943 }
1944 
1945 void ASTReader::ReadDefinedMacros() {
1946   // Note that we are loading defined macros.
1947   Deserializing Macros(this);
1948 
1949   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1950     BitstreamCursor &MacroCursor = I.MacroCursor;
1951 
1952     // If there was no preprocessor block, skip this file.
1953     if (MacroCursor.getBitcodeBytes().empty())
1954       continue;
1955 
1956     BitstreamCursor Cursor = MacroCursor;
1957     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1958       Error(std::move(Err));
1959       return;
1960     }
1961 
1962     RecordData Record;
1963     while (true) {
1964       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1965       if (!MaybeE) {
1966         Error(MaybeE.takeError());
1967         return;
1968       }
1969       llvm::BitstreamEntry E = MaybeE.get();
1970 
1971       switch (E.Kind) {
1972       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1973       case llvm::BitstreamEntry::Error:
1974         Error("malformed block record in AST file");
1975         return;
1976       case llvm::BitstreamEntry::EndBlock:
1977         goto NextCursor;
1978 
1979       case llvm::BitstreamEntry::Record: {
1980         Record.clear();
1981         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1982         if (!MaybeRecord) {
1983           Error(MaybeRecord.takeError());
1984           return;
1985         }
1986         switch (MaybeRecord.get()) {
1987         default:  // Default behavior: ignore.
1988           break;
1989 
1990         case PP_MACRO_OBJECT_LIKE:
1991         case PP_MACRO_FUNCTION_LIKE: {
1992           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1993           if (II->isOutOfDate())
1994             updateOutOfDateIdentifier(*II);
1995           break;
1996         }
1997 
1998         case PP_TOKEN:
1999           // Ignore tokens.
2000           break;
2001         }
2002         break;
2003       }
2004       }
2005     }
2006     NextCursor:  ;
2007   }
2008 }
2009 
2010 namespace {
2011 
2012   /// Visitor class used to look up identifirs in an AST file.
2013   class IdentifierLookupVisitor {
2014     StringRef Name;
2015     unsigned NameHash;
2016     unsigned PriorGeneration;
2017     unsigned &NumIdentifierLookups;
2018     unsigned &NumIdentifierLookupHits;
2019     IdentifierInfo *Found = nullptr;
2020 
2021   public:
2022     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2023                             unsigned &NumIdentifierLookups,
2024                             unsigned &NumIdentifierLookupHits)
2025       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2026         PriorGeneration(PriorGeneration),
2027         NumIdentifierLookups(NumIdentifierLookups),
2028         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2029 
2030     bool operator()(ModuleFile &M) {
2031       // If we've already searched this module file, skip it now.
2032       if (M.Generation <= PriorGeneration)
2033         return true;
2034 
2035       ASTIdentifierLookupTable *IdTable
2036         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2037       if (!IdTable)
2038         return false;
2039 
2040       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2041                                      Found);
2042       ++NumIdentifierLookups;
2043       ASTIdentifierLookupTable::iterator Pos =
2044           IdTable->find_hashed(Name, NameHash, &Trait);
2045       if (Pos == IdTable->end())
2046         return false;
2047 
2048       // Dereferencing the iterator has the effect of building the
2049       // IdentifierInfo node and populating it with the various
2050       // declarations it needs.
2051       ++NumIdentifierLookupHits;
2052       Found = *Pos;
2053       return true;
2054     }
2055 
2056     // Retrieve the identifier info found within the module
2057     // files.
2058     IdentifierInfo *getIdentifierInfo() const { return Found; }
2059   };
2060 
2061 } // namespace
2062 
2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2064   // Note that we are loading an identifier.
2065   Deserializing AnIdentifier(this);
2066 
2067   unsigned PriorGeneration = 0;
2068   if (getContext().getLangOpts().Modules)
2069     PriorGeneration = IdentifierGeneration[&II];
2070 
2071   // If there is a global index, look there first to determine which modules
2072   // provably do not have any results for this identifier.
2073   GlobalModuleIndex::HitSet Hits;
2074   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2075   if (!loadGlobalIndex()) {
2076     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2077       HitsPtr = &Hits;
2078     }
2079   }
2080 
2081   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2082                                   NumIdentifierLookups,
2083                                   NumIdentifierLookupHits);
2084   ModuleMgr.visit(Visitor, HitsPtr);
2085   markIdentifierUpToDate(&II);
2086 }
2087 
2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2089   if (!II)
2090     return;
2091 
2092   II->setOutOfDate(false);
2093 
2094   // Update the generation for this identifier.
2095   if (getContext().getLangOpts().Modules)
2096     IdentifierGeneration[II] = getGeneration();
2097 }
2098 
2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2100                                     const PendingMacroInfo &PMInfo) {
2101   ModuleFile &M = *PMInfo.M;
2102 
2103   BitstreamCursor &Cursor = M.MacroCursor;
2104   SavedStreamPosition SavedPosition(Cursor);
2105   if (llvm::Error Err =
2106           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2107     Error(std::move(Err));
2108     return;
2109   }
2110 
2111   struct ModuleMacroRecord {
2112     SubmoduleID SubModID;
2113     MacroInfo *MI;
2114     SmallVector<SubmoduleID, 8> Overrides;
2115   };
2116   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2117 
2118   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2119   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2120   // macro histroy.
2121   RecordData Record;
2122   while (true) {
2123     Expected<llvm::BitstreamEntry> MaybeEntry =
2124         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2125     if (!MaybeEntry) {
2126       Error(MaybeEntry.takeError());
2127       return;
2128     }
2129     llvm::BitstreamEntry Entry = MaybeEntry.get();
2130 
2131     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2132       Error("malformed block record in AST file");
2133       return;
2134     }
2135 
2136     Record.clear();
2137     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2138     if (!MaybePP) {
2139       Error(MaybePP.takeError());
2140       return;
2141     }
2142     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2143     case PP_MACRO_DIRECTIVE_HISTORY:
2144       break;
2145 
2146     case PP_MODULE_MACRO: {
2147       ModuleMacros.push_back(ModuleMacroRecord());
2148       auto &Info = ModuleMacros.back();
2149       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2150       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2151       for (int I = 2, N = Record.size(); I != N; ++I)
2152         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2153       continue;
2154     }
2155 
2156     default:
2157       Error("malformed block record in AST file");
2158       return;
2159     }
2160 
2161     // We found the macro directive history; that's the last record
2162     // for this macro.
2163     break;
2164   }
2165 
2166   // Module macros are listed in reverse dependency order.
2167   {
2168     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2169     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2170     for (auto &MMR : ModuleMacros) {
2171       Overrides.clear();
2172       for (unsigned ModID : MMR.Overrides) {
2173         Module *Mod = getSubmodule(ModID);
2174         auto *Macro = PP.getModuleMacro(Mod, II);
2175         assert(Macro && "missing definition for overridden macro");
2176         Overrides.push_back(Macro);
2177       }
2178 
2179       bool Inserted = false;
2180       Module *Owner = getSubmodule(MMR.SubModID);
2181       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2182     }
2183   }
2184 
2185   // Don't read the directive history for a module; we don't have anywhere
2186   // to put it.
2187   if (M.isModule())
2188     return;
2189 
2190   // Deserialize the macro directives history in reverse source-order.
2191   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2192   unsigned Idx = 0, N = Record.size();
2193   while (Idx < N) {
2194     MacroDirective *MD = nullptr;
2195     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2196     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2197     switch (K) {
2198     case MacroDirective::MD_Define: {
2199       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2200       MD = PP.AllocateDefMacroDirective(MI, Loc);
2201       break;
2202     }
2203     case MacroDirective::MD_Undefine:
2204       MD = PP.AllocateUndefMacroDirective(Loc);
2205       break;
2206     case MacroDirective::MD_Visibility:
2207       bool isPublic = Record[Idx++];
2208       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2209       break;
2210     }
2211 
2212     if (!Latest)
2213       Latest = MD;
2214     if (Earliest)
2215       Earliest->setPrevious(MD);
2216     Earliest = MD;
2217   }
2218 
2219   if (Latest)
2220     PP.setLoadedMacroDirective(II, Earliest, Latest);
2221 }
2222 
2223 bool ASTReader::shouldDisableValidationForFile(
2224     const serialization::ModuleFile &M) const {
2225   if (DisableValidationKind == DisableValidationForModuleKind::None)
2226     return false;
2227 
2228   // If a PCH is loaded and validation is disabled for PCH then disable
2229   // validation for the PCH and the modules it loads.
2230   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2231 
2232   switch (K) {
2233   case MK_MainFile:
2234   case MK_Preamble:
2235   case MK_PCH:
2236     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2237   case MK_ImplicitModule:
2238   case MK_ExplicitModule:
2239   case MK_PrebuiltModule:
2240     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2241   }
2242 
2243   return false;
2244 }
2245 
2246 ASTReader::InputFileInfo
2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2248   // Go find this input file.
2249   BitstreamCursor &Cursor = F.InputFilesCursor;
2250   SavedStreamPosition SavedPosition(Cursor);
2251   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2252     // FIXME this drops errors on the floor.
2253     consumeError(std::move(Err));
2254   }
2255 
2256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2257   if (!MaybeCode) {
2258     // FIXME this drops errors on the floor.
2259     consumeError(MaybeCode.takeError());
2260   }
2261   unsigned Code = MaybeCode.get();
2262   RecordData Record;
2263   StringRef Blob;
2264 
2265   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2266     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2267            "invalid record type for input file");
2268   else {
2269     // FIXME this drops errors on the floor.
2270     consumeError(Maybe.takeError());
2271   }
2272 
2273   assert(Record[0] == ID && "Bogus stored ID or offset");
2274   InputFileInfo R;
2275   R.StoredSize = static_cast<off_t>(Record[1]);
2276   R.StoredTime = static_cast<time_t>(Record[2]);
2277   R.Overridden = static_cast<bool>(Record[3]);
2278   R.Transient = static_cast<bool>(Record[4]);
2279   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2280   R.Filename = std::string(Blob);
2281   ResolveImportedPath(F, R.Filename);
2282 
2283   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2284   if (!MaybeEntry) // FIXME this drops errors on the floor.
2285     consumeError(MaybeEntry.takeError());
2286   llvm::BitstreamEntry Entry = MaybeEntry.get();
2287   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2288          "expected record type for input file hash");
2289 
2290   Record.clear();
2291   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2292     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2293            "invalid record type for input file hash");
2294   else {
2295     // FIXME this drops errors on the floor.
2296     consumeError(Maybe.takeError());
2297   }
2298   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2299                   static_cast<uint64_t>(Record[0]);
2300   return R;
2301 }
2302 
2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2305   // If this ID is bogus, just return an empty input file.
2306   if (ID == 0 || ID > F.InputFilesLoaded.size())
2307     return InputFile();
2308 
2309   // If we've already loaded this input file, return it.
2310   if (F.InputFilesLoaded[ID-1].getFile())
2311     return F.InputFilesLoaded[ID-1];
2312 
2313   if (F.InputFilesLoaded[ID-1].isNotFound())
2314     return InputFile();
2315 
2316   // Go find this input file.
2317   BitstreamCursor &Cursor = F.InputFilesCursor;
2318   SavedStreamPosition SavedPosition(Cursor);
2319   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2320     // FIXME this drops errors on the floor.
2321     consumeError(std::move(Err));
2322   }
2323 
2324   InputFileInfo FI = readInputFileInfo(F, ID);
2325   off_t StoredSize = FI.StoredSize;
2326   time_t StoredTime = FI.StoredTime;
2327   bool Overridden = FI.Overridden;
2328   bool Transient = FI.Transient;
2329   StringRef Filename = FI.Filename;
2330   uint64_t StoredContentHash = FI.ContentHash;
2331 
2332   OptionalFileEntryRefDegradesToFileEntryPtr File =
2333       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2334 
2335   // If we didn't find the file, resolve it relative to the
2336   // original directory from which this AST file was created.
2337   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2338       F.OriginalDir != F.BaseDirectory) {
2339     std::string Resolved = resolveFileRelativeToOriginalDir(
2340         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2341     if (!Resolved.empty())
2342       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2343   }
2344 
2345   // For an overridden file, create a virtual file with the stored
2346   // size/timestamp.
2347   if ((Overridden || Transient) && !File)
2348     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2349 
2350   if (!File) {
2351     if (Complain) {
2352       std::string ErrorStr = "could not find file '";
2353       ErrorStr += Filename;
2354       ErrorStr += "' referenced by AST file '";
2355       ErrorStr += F.FileName;
2356       ErrorStr += "'";
2357       Error(ErrorStr);
2358     }
2359     // Record that we didn't find the file.
2360     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2361     return InputFile();
2362   }
2363 
2364   // Check if there was a request to override the contents of the file
2365   // that was part of the precompiled header. Overriding such a file
2366   // can lead to problems when lexing using the source locations from the
2367   // PCH.
2368   SourceManager &SM = getSourceManager();
2369   // FIXME: Reject if the overrides are different.
2370   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2371     if (Complain)
2372       Error(diag::err_fe_pch_file_overridden, Filename);
2373 
2374     // After emitting the diagnostic, bypass the overriding file to recover
2375     // (this creates a separate FileEntry).
2376     File = SM.bypassFileContentsOverride(*File);
2377     if (!File) {
2378       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2379       return InputFile();
2380     }
2381   }
2382 
2383   struct Change {
2384     enum ModificationKind {
2385       Size,
2386       ModTime,
2387       Content,
2388       None,
2389     } Kind;
2390     llvm::Optional<int64_t> Old = llvm::None;
2391     llvm::Optional<int64_t> New = llvm::None;
2392   };
2393   auto HasInputFileChanged = [&]() {
2394     if (StoredSize != File->getSize())
2395       return Change{Change::Size, StoredSize, File->getSize()};
2396     if (!shouldDisableValidationForFile(F) && StoredTime &&
2397         StoredTime != File->getModificationTime()) {
2398       Change MTimeChange = {Change::ModTime, StoredTime,
2399                             File->getModificationTime()};
2400 
2401       // In case the modification time changes but not the content,
2402       // accept the cached file as legit.
2403       if (ValidateASTInputFilesContent &&
2404           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2405         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2406         if (!MemBuffOrError) {
2407           if (!Complain)
2408             return MTimeChange;
2409           std::string ErrorStr = "could not get buffer for file '";
2410           ErrorStr += File->getName();
2411           ErrorStr += "'";
2412           Error(ErrorStr);
2413           return MTimeChange;
2414         }
2415 
2416         // FIXME: hash_value is not guaranteed to be stable!
2417         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2418         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2419           return Change{Change::None};
2420 
2421         return Change{Change::Content};
2422       }
2423       return MTimeChange;
2424     }
2425     return Change{Change::None};
2426   };
2427 
2428   bool IsOutOfDate = false;
2429   auto FileChange = HasInputFileChanged();
2430   // For an overridden file, there is nothing to validate.
2431   if (!Overridden && FileChange.Kind != Change::None) {
2432     if (Complain && !Diags.isDiagnosticInFlight()) {
2433       // Build a list of the PCH imports that got us here (in reverse).
2434       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2435       while (!ImportStack.back()->ImportedBy.empty())
2436         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2437 
2438       // The top-level PCH is stale.
2439       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2440       Diag(diag::err_fe_ast_file_modified)
2441           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2442           << TopLevelPCHName << FileChange.Kind
2443           << (FileChange.Old && FileChange.New)
2444           << llvm::itostr(FileChange.Old.getValueOr(0))
2445           << llvm::itostr(FileChange.New.getValueOr(0));
2446 
2447       // Print the import stack.
2448       if (ImportStack.size() > 1) {
2449         Diag(diag::note_pch_required_by)
2450           << Filename << ImportStack[0]->FileName;
2451         for (unsigned I = 1; I < ImportStack.size(); ++I)
2452           Diag(diag::note_pch_required_by)
2453             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2454       }
2455 
2456       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2457     }
2458 
2459     IsOutOfDate = true;
2460   }
2461   // FIXME: If the file is overridden and we've already opened it,
2462   // issue an error (or split it into a separate FileEntry).
2463 
2464   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2465 
2466   // Note that we've loaded this input file.
2467   F.InputFilesLoaded[ID-1] = IF;
2468   return IF;
2469 }
2470 
2471 /// If we are loading a relocatable PCH or module file, and the filename
2472 /// is not an absolute path, add the system or module root to the beginning of
2473 /// the file name.
2474 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2475   // Resolve relative to the base directory, if we have one.
2476   if (!M.BaseDirectory.empty())
2477     return ResolveImportedPath(Filename, M.BaseDirectory);
2478 }
2479 
2480 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2481   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2482     return;
2483 
2484   SmallString<128> Buffer;
2485   llvm::sys::path::append(Buffer, Prefix, Filename);
2486   Filename.assign(Buffer.begin(), Buffer.end());
2487 }
2488 
2489 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2490   switch (ARR) {
2491   case ASTReader::Failure: return true;
2492   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2493   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2494   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2495   case ASTReader::ConfigurationMismatch:
2496     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2497   case ASTReader::HadErrors: return true;
2498   case ASTReader::Success: return false;
2499   }
2500 
2501   llvm_unreachable("unknown ASTReadResult");
2502 }
2503 
2504 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2505     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2506     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2507     std::string &SuggestedPredefines) {
2508   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2509     // FIXME this drops errors on the floor.
2510     consumeError(std::move(Err));
2511     return Failure;
2512   }
2513 
2514   // Read all of the records in the options block.
2515   RecordData Record;
2516   ASTReadResult Result = Success;
2517   while (true) {
2518     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2519     if (!MaybeEntry) {
2520       // FIXME this drops errors on the floor.
2521       consumeError(MaybeEntry.takeError());
2522       return Failure;
2523     }
2524     llvm::BitstreamEntry Entry = MaybeEntry.get();
2525 
2526     switch (Entry.Kind) {
2527     case llvm::BitstreamEntry::Error:
2528     case llvm::BitstreamEntry::SubBlock:
2529       return Failure;
2530 
2531     case llvm::BitstreamEntry::EndBlock:
2532       return Result;
2533 
2534     case llvm::BitstreamEntry::Record:
2535       // The interesting case.
2536       break;
2537     }
2538 
2539     // Read and process a record.
2540     Record.clear();
2541     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2542     if (!MaybeRecordType) {
2543       // FIXME this drops errors on the floor.
2544       consumeError(MaybeRecordType.takeError());
2545       return Failure;
2546     }
2547     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2548     case LANGUAGE_OPTIONS: {
2549       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2550       if (ParseLanguageOptions(Record, Complain, Listener,
2551                                AllowCompatibleConfigurationMismatch))
2552         Result = ConfigurationMismatch;
2553       break;
2554     }
2555 
2556     case TARGET_OPTIONS: {
2557       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2558       if (ParseTargetOptions(Record, Complain, Listener,
2559                              AllowCompatibleConfigurationMismatch))
2560         Result = ConfigurationMismatch;
2561       break;
2562     }
2563 
2564     case FILE_SYSTEM_OPTIONS: {
2565       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2566       if (!AllowCompatibleConfigurationMismatch &&
2567           ParseFileSystemOptions(Record, Complain, Listener))
2568         Result = ConfigurationMismatch;
2569       break;
2570     }
2571 
2572     case HEADER_SEARCH_OPTIONS: {
2573       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2574       if (!AllowCompatibleConfigurationMismatch &&
2575           ParseHeaderSearchOptions(Record, Complain, Listener))
2576         Result = ConfigurationMismatch;
2577       break;
2578     }
2579 
2580     case PREPROCESSOR_OPTIONS:
2581       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2582       if (!AllowCompatibleConfigurationMismatch &&
2583           ParsePreprocessorOptions(Record, Complain, Listener,
2584                                    SuggestedPredefines))
2585         Result = ConfigurationMismatch;
2586       break;
2587     }
2588   }
2589 }
2590 
2591 ASTReader::ASTReadResult
2592 ASTReader::ReadControlBlock(ModuleFile &F,
2593                             SmallVectorImpl<ImportedModule> &Loaded,
2594                             const ModuleFile *ImportedBy,
2595                             unsigned ClientLoadCapabilities) {
2596   BitstreamCursor &Stream = F.Stream;
2597 
2598   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2599     Error(std::move(Err));
2600     return Failure;
2601   }
2602 
2603   // Lambda to read the unhashed control block the first time it's called.
2604   //
2605   // For PCM files, the unhashed control block cannot be read until after the
2606   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2607   // need to look ahead before reading the IMPORTS record.  For consistency,
2608   // this block is always read somehow (see BitstreamEntry::EndBlock).
2609   bool HasReadUnhashedControlBlock = false;
2610   auto readUnhashedControlBlockOnce = [&]() {
2611     if (!HasReadUnhashedControlBlock) {
2612       HasReadUnhashedControlBlock = true;
2613       if (ASTReadResult Result =
2614               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2615         return Result;
2616     }
2617     return Success;
2618   };
2619 
2620   bool DisableValidation = shouldDisableValidationForFile(F);
2621 
2622   // Read all of the records and blocks in the control block.
2623   RecordData Record;
2624   unsigned NumInputs = 0;
2625   unsigned NumUserInputs = 0;
2626   StringRef BaseDirectoryAsWritten;
2627   while (true) {
2628     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2629     if (!MaybeEntry) {
2630       Error(MaybeEntry.takeError());
2631       return Failure;
2632     }
2633     llvm::BitstreamEntry Entry = MaybeEntry.get();
2634 
2635     switch (Entry.Kind) {
2636     case llvm::BitstreamEntry::Error:
2637       Error("malformed block record in AST file");
2638       return Failure;
2639     case llvm::BitstreamEntry::EndBlock: {
2640       // Validate the module before returning.  This call catches an AST with
2641       // no module name and no imports.
2642       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2643         return Result;
2644 
2645       // Validate input files.
2646       const HeaderSearchOptions &HSOpts =
2647           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2648 
2649       // All user input files reside at the index range [0, NumUserInputs), and
2650       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2651       // loaded module files, ignore missing inputs.
2652       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2653           F.Kind != MK_PrebuiltModule) {
2654         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2655 
2656         // If we are reading a module, we will create a verification timestamp,
2657         // so we verify all input files.  Otherwise, verify only user input
2658         // files.
2659 
2660         unsigned N = NumUserInputs;
2661         if (ValidateSystemInputs ||
2662             (HSOpts.ModulesValidateOncePerBuildSession &&
2663              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2664              F.Kind == MK_ImplicitModule))
2665           N = NumInputs;
2666 
2667         for (unsigned I = 0; I < N; ++I) {
2668           InputFile IF = getInputFile(F, I+1, Complain);
2669           if (!IF.getFile() || IF.isOutOfDate())
2670             return OutOfDate;
2671         }
2672       }
2673 
2674       if (Listener)
2675         Listener->visitModuleFile(F.FileName, F.Kind);
2676 
2677       if (Listener && Listener->needsInputFileVisitation()) {
2678         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2679                                                                 : NumUserInputs;
2680         for (unsigned I = 0; I < N; ++I) {
2681           bool IsSystem = I >= NumUserInputs;
2682           InputFileInfo FI = readInputFileInfo(F, I+1);
2683           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2684                                    F.Kind == MK_ExplicitModule ||
2685                                    F.Kind == MK_PrebuiltModule);
2686         }
2687       }
2688 
2689       return Success;
2690     }
2691 
2692     case llvm::BitstreamEntry::SubBlock:
2693       switch (Entry.ID) {
2694       case INPUT_FILES_BLOCK_ID:
2695         F.InputFilesCursor = Stream;
2696         if (llvm::Error Err = Stream.SkipBlock()) {
2697           Error(std::move(Err));
2698           return Failure;
2699         }
2700         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2701           Error("malformed block record in AST file");
2702           return Failure;
2703         }
2704         continue;
2705 
2706       case OPTIONS_BLOCK_ID:
2707         // If we're reading the first module for this group, check its options
2708         // are compatible with ours. For modules it imports, no further checking
2709         // is required, because we checked them when we built it.
2710         if (Listener && !ImportedBy) {
2711           // Should we allow the configuration of the module file to differ from
2712           // the configuration of the current translation unit in a compatible
2713           // way?
2714           //
2715           // FIXME: Allow this for files explicitly specified with -include-pch.
2716           bool AllowCompatibleConfigurationMismatch =
2717               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2718 
2719           ASTReadResult Result =
2720               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2721                                AllowCompatibleConfigurationMismatch, *Listener,
2722                                SuggestedPredefines);
2723           if (Result == Failure) {
2724             Error("malformed block record in AST file");
2725             return Result;
2726           }
2727 
2728           if (DisableValidation ||
2729               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2730             Result = Success;
2731 
2732           // If we can't load the module, exit early since we likely
2733           // will rebuild the module anyway. The stream may be in the
2734           // middle of a block.
2735           if (Result != Success)
2736             return Result;
2737         } else if (llvm::Error Err = Stream.SkipBlock()) {
2738           Error(std::move(Err));
2739           return Failure;
2740         }
2741         continue;
2742 
2743       default:
2744         if (llvm::Error Err = Stream.SkipBlock()) {
2745           Error(std::move(Err));
2746           return Failure;
2747         }
2748         continue;
2749       }
2750 
2751     case llvm::BitstreamEntry::Record:
2752       // The interesting case.
2753       break;
2754     }
2755 
2756     // Read and process a record.
2757     Record.clear();
2758     StringRef Blob;
2759     Expected<unsigned> MaybeRecordType =
2760         Stream.readRecord(Entry.ID, Record, &Blob);
2761     if (!MaybeRecordType) {
2762       Error(MaybeRecordType.takeError());
2763       return Failure;
2764     }
2765     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2766     case METADATA: {
2767       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2768         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2769           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2770                                         : diag::err_pch_version_too_new);
2771         return VersionMismatch;
2772       }
2773 
2774       bool hasErrors = Record[6];
2775       if (hasErrors && !DisableValidation) {
2776         // If requested by the caller and the module hasn't already been read
2777         // or compiled, mark modules on error as out-of-date.
2778         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2779             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2780           return OutOfDate;
2781 
2782         if (!AllowASTWithCompilerErrors) {
2783           Diag(diag::err_pch_with_compiler_errors);
2784           return HadErrors;
2785         }
2786       }
2787       if (hasErrors) {
2788         Diags.ErrorOccurred = true;
2789         Diags.UncompilableErrorOccurred = true;
2790         Diags.UnrecoverableErrorOccurred = true;
2791       }
2792 
2793       F.RelocatablePCH = Record[4];
2794       // Relative paths in a relocatable PCH are relative to our sysroot.
2795       if (F.RelocatablePCH)
2796         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2797 
2798       F.HasTimestamps = Record[5];
2799 
2800       const std::string &CurBranch = getClangFullRepositoryVersion();
2801       StringRef ASTBranch = Blob;
2802       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2803         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2804           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2805         return VersionMismatch;
2806       }
2807       break;
2808     }
2809 
2810     case IMPORTS: {
2811       // Validate the AST before processing any imports (otherwise, untangling
2812       // them can be error-prone and expensive).  A module will have a name and
2813       // will already have been validated, but this catches the PCH case.
2814       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2815         return Result;
2816 
2817       // Load each of the imported PCH files.
2818       unsigned Idx = 0, N = Record.size();
2819       while (Idx < N) {
2820         // Read information about the AST file.
2821         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2822         // The import location will be the local one for now; we will adjust
2823         // all import locations of module imports after the global source
2824         // location info are setup, in ReadAST.
2825         SourceLocation ImportLoc =
2826             ReadUntranslatedSourceLocation(Record[Idx++]);
2827         off_t StoredSize = (off_t)Record[Idx++];
2828         time_t StoredModTime = (time_t)Record[Idx++];
2829         auto FirstSignatureByte = Record.begin() + Idx;
2830         ASTFileSignature StoredSignature = ASTFileSignature::create(
2831             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2832         Idx += ASTFileSignature::size;
2833 
2834         std::string ImportedName = ReadString(Record, Idx);
2835         std::string ImportedFile;
2836 
2837         // For prebuilt and explicit modules first consult the file map for
2838         // an override. Note that here we don't search prebuilt module
2839         // directories, only the explicit name to file mappings. Also, we will
2840         // still verify the size/signature making sure it is essentially the
2841         // same file but perhaps in a different location.
2842         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2843           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2844             ImportedName, /*FileMapOnly*/ true);
2845 
2846         if (ImportedFile.empty())
2847           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2848           // ModuleCache as when writing.
2849           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2850         else
2851           SkipPath(Record, Idx);
2852 
2853         // If our client can't cope with us being out of date, we can't cope with
2854         // our dependency being missing.
2855         unsigned Capabilities = ClientLoadCapabilities;
2856         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2857           Capabilities &= ~ARR_Missing;
2858 
2859         // Load the AST file.
2860         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2861                                   Loaded, StoredSize, StoredModTime,
2862                                   StoredSignature, Capabilities);
2863 
2864         // If we diagnosed a problem, produce a backtrace.
2865         bool recompilingFinalized =
2866             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2867             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2868         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2869           Diag(diag::note_module_file_imported_by)
2870               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2871         if (recompilingFinalized)
2872           Diag(diag::note_module_file_conflict);
2873 
2874         switch (Result) {
2875         case Failure: return Failure;
2876           // If we have to ignore the dependency, we'll have to ignore this too.
2877         case Missing:
2878         case OutOfDate: return OutOfDate;
2879         case VersionMismatch: return VersionMismatch;
2880         case ConfigurationMismatch: return ConfigurationMismatch;
2881         case HadErrors: return HadErrors;
2882         case Success: break;
2883         }
2884       }
2885       break;
2886     }
2887 
2888     case ORIGINAL_FILE:
2889       F.OriginalSourceFileID = FileID::get(Record[0]);
2890       F.ActualOriginalSourceFileName = std::string(Blob);
2891       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2892       ResolveImportedPath(F, F.OriginalSourceFileName);
2893       break;
2894 
2895     case ORIGINAL_FILE_ID:
2896       F.OriginalSourceFileID = FileID::get(Record[0]);
2897       break;
2898 
2899     case ORIGINAL_PCH_DIR:
2900       F.OriginalDir = std::string(Blob);
2901       ResolveImportedPath(F, F.OriginalDir);
2902       break;
2903 
2904     case MODULE_NAME:
2905       F.ModuleName = std::string(Blob);
2906       Diag(diag::remark_module_import)
2907           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2908           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2909       if (Listener)
2910         Listener->ReadModuleName(F.ModuleName);
2911 
2912       // Validate the AST as soon as we have a name so we can exit early on
2913       // failure.
2914       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2915         return Result;
2916 
2917       break;
2918 
2919     case MODULE_DIRECTORY: {
2920       // Save the BaseDirectory as written in the PCM for computing the module
2921       // filename for the ModuleCache.
2922       BaseDirectoryAsWritten = Blob;
2923       assert(!F.ModuleName.empty() &&
2924              "MODULE_DIRECTORY found before MODULE_NAME");
2925       // If we've already loaded a module map file covering this module, we may
2926       // have a better path for it (relative to the current build).
2927       Module *M = PP.getHeaderSearchInfo().lookupModule(
2928           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2929           /*AllowExtraModuleMapSearch*/ true);
2930       if (M && M->Directory) {
2931         // If we're implicitly loading a module, the base directory can't
2932         // change between the build and use.
2933         // Don't emit module relocation error if we have -fno-validate-pch
2934         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2935                   DisableValidationForModuleKind::Module) &&
2936             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2937           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2938           if (!BuildDir || *BuildDir != M->Directory) {
2939             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2940               Diag(diag::err_imported_module_relocated)
2941                   << F.ModuleName << Blob << M->Directory->getName();
2942             return OutOfDate;
2943           }
2944         }
2945         F.BaseDirectory = std::string(M->Directory->getName());
2946       } else {
2947         F.BaseDirectory = std::string(Blob);
2948       }
2949       break;
2950     }
2951 
2952     case MODULE_MAP_FILE:
2953       if (ASTReadResult Result =
2954               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2955         return Result;
2956       break;
2957 
2958     case INPUT_FILE_OFFSETS:
2959       NumInputs = Record[0];
2960       NumUserInputs = Record[1];
2961       F.InputFileOffsets =
2962           (const llvm::support::unaligned_uint64_t *)Blob.data();
2963       F.InputFilesLoaded.resize(NumInputs);
2964       F.NumUserInputFiles = NumUserInputs;
2965       break;
2966     }
2967   }
2968 }
2969 
2970 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob,
2971                                   Preprocessor &PP) {
2972   using namespace llvm::support;
2973 
2974   const unsigned char *D = (const unsigned char *)Blob.data();
2975   unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D);
2976 
2977   for (unsigned I = 0; I < FileCount; ++I) {
2978     size_t ID = endian::readNext<uint32_t, little, unaligned>(D);
2979     InputFileInfo IFI = readInputFileInfo(F, ID);
2980     if (llvm::ErrorOr<const FileEntry *> File =
2981             PP.getFileManager().getFile(IFI.Filename))
2982       PP.getIncludedFiles().insert(*File);
2983   }
2984 }
2985 
2986 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2987                                     unsigned ClientLoadCapabilities) {
2988   BitstreamCursor &Stream = F.Stream;
2989 
2990   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2991     return Err;
2992   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2993 
2994   // Read all of the records and blocks for the AST file.
2995   RecordData Record;
2996   while (true) {
2997     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2998     if (!MaybeEntry)
2999       return MaybeEntry.takeError();
3000     llvm::BitstreamEntry Entry = MaybeEntry.get();
3001 
3002     switch (Entry.Kind) {
3003     case llvm::BitstreamEntry::Error:
3004       return llvm::createStringError(
3005           std::errc::illegal_byte_sequence,
3006           "error at end of module block in AST file");
3007     case llvm::BitstreamEntry::EndBlock:
3008       // Outside of C++, we do not store a lookup map for the translation unit.
3009       // Instead, mark it as needing a lookup map to be built if this module
3010       // contains any declarations lexically within it (which it always does!).
3011       // This usually has no cost, since we very rarely need the lookup map for
3012       // the translation unit outside C++.
3013       if (ASTContext *Ctx = ContextObj) {
3014         DeclContext *DC = Ctx->getTranslationUnitDecl();
3015         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3016           DC->setMustBuildLookupTable();
3017       }
3018 
3019       return llvm::Error::success();
3020     case llvm::BitstreamEntry::SubBlock:
3021       switch (Entry.ID) {
3022       case DECLTYPES_BLOCK_ID:
3023         // We lazily load the decls block, but we want to set up the
3024         // DeclsCursor cursor to point into it.  Clone our current bitcode
3025         // cursor to it, enter the block and read the abbrevs in that block.
3026         // With the main cursor, we just skip over it.
3027         F.DeclsCursor = Stream;
3028         if (llvm::Error Err = Stream.SkipBlock())
3029           return Err;
3030         if (llvm::Error Err = ReadBlockAbbrevs(
3031                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3032           return Err;
3033         break;
3034 
3035       case PREPROCESSOR_BLOCK_ID:
3036         F.MacroCursor = Stream;
3037         if (!PP.getExternalSource())
3038           PP.setExternalSource(this);
3039 
3040         if (llvm::Error Err = Stream.SkipBlock())
3041           return Err;
3042         if (llvm::Error Err =
3043                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3044           return Err;
3045         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3046         break;
3047 
3048       case PREPROCESSOR_DETAIL_BLOCK_ID:
3049         F.PreprocessorDetailCursor = Stream;
3050 
3051         if (llvm::Error Err = Stream.SkipBlock()) {
3052           return Err;
3053         }
3054         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3055                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3056           return Err;
3057         F.PreprocessorDetailStartOffset
3058         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3059 
3060         if (!PP.getPreprocessingRecord())
3061           PP.createPreprocessingRecord();
3062         if (!PP.getPreprocessingRecord()->getExternalSource())
3063           PP.getPreprocessingRecord()->SetExternalSource(*this);
3064         break;
3065 
3066       case SOURCE_MANAGER_BLOCK_ID:
3067         if (llvm::Error Err = ReadSourceManagerBlock(F))
3068           return Err;
3069         break;
3070 
3071       case SUBMODULE_BLOCK_ID:
3072         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3073           return Err;
3074         break;
3075 
3076       case COMMENTS_BLOCK_ID: {
3077         BitstreamCursor C = Stream;
3078 
3079         if (llvm::Error Err = Stream.SkipBlock())
3080           return Err;
3081         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3082           return Err;
3083         CommentsCursors.push_back(std::make_pair(C, &F));
3084         break;
3085       }
3086 
3087       default:
3088         if (llvm::Error Err = Stream.SkipBlock())
3089           return Err;
3090         break;
3091       }
3092       continue;
3093 
3094     case llvm::BitstreamEntry::Record:
3095       // The interesting case.
3096       break;
3097     }
3098 
3099     // Read and process a record.
3100     Record.clear();
3101     StringRef Blob;
3102     Expected<unsigned> MaybeRecordType =
3103         Stream.readRecord(Entry.ID, Record, &Blob);
3104     if (!MaybeRecordType)
3105       return MaybeRecordType.takeError();
3106     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3107 
3108     // If we're not loading an AST context, we don't care about most records.
3109     if (!ContextObj) {
3110       switch (RecordType) {
3111       case IDENTIFIER_TABLE:
3112       case IDENTIFIER_OFFSET:
3113       case INTERESTING_IDENTIFIERS:
3114       case STATISTICS:
3115       case PP_ASSUME_NONNULL_LOC:
3116       case PP_CONDITIONAL_STACK:
3117       case PP_COUNTER_VALUE:
3118       case SOURCE_LOCATION_OFFSETS:
3119       case MODULE_OFFSET_MAP:
3120       case SOURCE_MANAGER_LINE_TABLE:
3121       case SOURCE_LOCATION_PRELOADS:
3122       case PPD_ENTITIES_OFFSETS:
3123       case HEADER_SEARCH_TABLE:
3124       case IMPORTED_MODULES:
3125       case MACRO_OFFSET:
3126         break;
3127       default:
3128         continue;
3129       }
3130     }
3131 
3132     switch (RecordType) {
3133     default:  // Default behavior: ignore.
3134       break;
3135 
3136     case TYPE_OFFSET: {
3137       if (F.LocalNumTypes != 0)
3138         return llvm::createStringError(
3139             std::errc::illegal_byte_sequence,
3140             "duplicate TYPE_OFFSET record in AST file");
3141       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3142       F.LocalNumTypes = Record[0];
3143       unsigned LocalBaseTypeIndex = Record[1];
3144       F.BaseTypeIndex = getTotalNumTypes();
3145 
3146       if (F.LocalNumTypes > 0) {
3147         // Introduce the global -> local mapping for types within this module.
3148         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3149 
3150         // Introduce the local -> global mapping for types within this module.
3151         F.TypeRemap.insertOrReplace(
3152           std::make_pair(LocalBaseTypeIndex,
3153                          F.BaseTypeIndex - LocalBaseTypeIndex));
3154 
3155         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3156       }
3157       break;
3158     }
3159 
3160     case DECL_OFFSET: {
3161       if (F.LocalNumDecls != 0)
3162         return llvm::createStringError(
3163             std::errc::illegal_byte_sequence,
3164             "duplicate DECL_OFFSET record in AST file");
3165       F.DeclOffsets = (const DeclOffset *)Blob.data();
3166       F.LocalNumDecls = Record[0];
3167       unsigned LocalBaseDeclID = Record[1];
3168       F.BaseDeclID = getTotalNumDecls();
3169 
3170       if (F.LocalNumDecls > 0) {
3171         // Introduce the global -> local mapping for declarations within this
3172         // module.
3173         GlobalDeclMap.insert(
3174           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3175 
3176         // Introduce the local -> global mapping for declarations within this
3177         // module.
3178         F.DeclRemap.insertOrReplace(
3179           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3180 
3181         // Introduce the global -> local mapping for declarations within this
3182         // module.
3183         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3184 
3185         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3186       }
3187       break;
3188     }
3189 
3190     case TU_UPDATE_LEXICAL: {
3191       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3192       LexicalContents Contents(
3193           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3194               Blob.data()),
3195           static_cast<unsigned int>(Blob.size() / 4));
3196       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3197       TU->setHasExternalLexicalStorage(true);
3198       break;
3199     }
3200 
3201     case UPDATE_VISIBLE: {
3202       unsigned Idx = 0;
3203       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3204       auto *Data = (const unsigned char*)Blob.data();
3205       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3206       // If we've already loaded the decl, perform the updates when we finish
3207       // loading this block.
3208       if (Decl *D = GetExistingDecl(ID))
3209         PendingUpdateRecords.push_back(
3210             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3211       break;
3212     }
3213 
3214     case IDENTIFIER_TABLE:
3215       F.IdentifierTableData =
3216           reinterpret_cast<const unsigned char *>(Blob.data());
3217       if (Record[0]) {
3218         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3219             F.IdentifierTableData + Record[0],
3220             F.IdentifierTableData + sizeof(uint32_t),
3221             F.IdentifierTableData,
3222             ASTIdentifierLookupTrait(*this, F));
3223 
3224         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3225       }
3226       break;
3227 
3228     case IDENTIFIER_OFFSET: {
3229       if (F.LocalNumIdentifiers != 0)
3230         return llvm::createStringError(
3231             std::errc::illegal_byte_sequence,
3232             "duplicate IDENTIFIER_OFFSET record in AST file");
3233       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3234       F.LocalNumIdentifiers = Record[0];
3235       unsigned LocalBaseIdentifierID = Record[1];
3236       F.BaseIdentifierID = getTotalNumIdentifiers();
3237 
3238       if (F.LocalNumIdentifiers > 0) {
3239         // Introduce the global -> local mapping for identifiers within this
3240         // module.
3241         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3242                                                   &F));
3243 
3244         // Introduce the local -> global mapping for identifiers within this
3245         // module.
3246         F.IdentifierRemap.insertOrReplace(
3247           std::make_pair(LocalBaseIdentifierID,
3248                          F.BaseIdentifierID - LocalBaseIdentifierID));
3249 
3250         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3251                                  + F.LocalNumIdentifiers);
3252       }
3253       break;
3254     }
3255 
3256     case INTERESTING_IDENTIFIERS:
3257       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3258       break;
3259 
3260     case EAGERLY_DESERIALIZED_DECLS:
3261       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3262       // about "interesting" decls (for instance, if we're building a module).
3263       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3264         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3265       break;
3266 
3267     case MODULAR_CODEGEN_DECLS:
3268       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3269       // them (ie: if we're not codegenerating this module).
3270       if (F.Kind == MK_MainFile ||
3271           getContext().getLangOpts().BuildingPCHWithObjectFile)
3272         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3273           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3274       break;
3275 
3276     case SPECIAL_TYPES:
3277       if (SpecialTypes.empty()) {
3278         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3279           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3280         break;
3281       }
3282 
3283       if (SpecialTypes.size() != Record.size())
3284         return llvm::createStringError(std::errc::illegal_byte_sequence,
3285                                        "invalid special-types record");
3286 
3287       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3288         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3289         if (!SpecialTypes[I])
3290           SpecialTypes[I] = ID;
3291         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3292         // merge step?
3293       }
3294       break;
3295 
3296     case STATISTICS:
3297       TotalNumStatements += Record[0];
3298       TotalNumMacros += Record[1];
3299       TotalLexicalDeclContexts += Record[2];
3300       TotalVisibleDeclContexts += Record[3];
3301       break;
3302 
3303     case UNUSED_FILESCOPED_DECLS:
3304       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3305         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3306       break;
3307 
3308     case DELEGATING_CTORS:
3309       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3310         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3311       break;
3312 
3313     case WEAK_UNDECLARED_IDENTIFIERS:
3314       if (Record.size() % 3 != 0)
3315         return llvm::createStringError(std::errc::illegal_byte_sequence,
3316                                        "invalid weak identifiers record");
3317 
3318       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3319       // files. This isn't the way to do it :)
3320       WeakUndeclaredIdentifiers.clear();
3321 
3322       // Translate the weak, undeclared identifiers into global IDs.
3323       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3324         WeakUndeclaredIdentifiers.push_back(
3325           getGlobalIdentifierID(F, Record[I++]));
3326         WeakUndeclaredIdentifiers.push_back(
3327           getGlobalIdentifierID(F, Record[I++]));
3328         WeakUndeclaredIdentifiers.push_back(
3329             ReadSourceLocation(F, Record, I).getRawEncoding());
3330       }
3331       break;
3332 
3333     case SELECTOR_OFFSETS: {
3334       F.SelectorOffsets = (const uint32_t *)Blob.data();
3335       F.LocalNumSelectors = Record[0];
3336       unsigned LocalBaseSelectorID = Record[1];
3337       F.BaseSelectorID = getTotalNumSelectors();
3338 
3339       if (F.LocalNumSelectors > 0) {
3340         // Introduce the global -> local mapping for selectors within this
3341         // module.
3342         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3343 
3344         // Introduce the local -> global mapping for selectors within this
3345         // module.
3346         F.SelectorRemap.insertOrReplace(
3347           std::make_pair(LocalBaseSelectorID,
3348                          F.BaseSelectorID - LocalBaseSelectorID));
3349 
3350         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3351       }
3352       break;
3353     }
3354 
3355     case METHOD_POOL:
3356       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3357       if (Record[0])
3358         F.SelectorLookupTable
3359           = ASTSelectorLookupTable::Create(
3360                         F.SelectorLookupTableData + Record[0],
3361                         F.SelectorLookupTableData,
3362                         ASTSelectorLookupTrait(*this, F));
3363       TotalNumMethodPoolEntries += Record[1];
3364       break;
3365 
3366     case REFERENCED_SELECTOR_POOL:
3367       if (!Record.empty()) {
3368         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3369           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3370                                                                 Record[Idx++]));
3371           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3372                                               getRawEncoding());
3373         }
3374       }
3375       break;
3376 
3377     case PP_ASSUME_NONNULL_LOC: {
3378       unsigned Idx = 0;
3379       if (!Record.empty())
3380         PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3381             ReadSourceLocation(F, Record, Idx));
3382       break;
3383     }
3384 
3385     case PP_CONDITIONAL_STACK:
3386       if (!Record.empty()) {
3387         unsigned Idx = 0, End = Record.size() - 1;
3388         bool ReachedEOFWhileSkipping = Record[Idx++];
3389         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3390         if (ReachedEOFWhileSkipping) {
3391           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3392           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3393           bool FoundNonSkipPortion = Record[Idx++];
3394           bool FoundElse = Record[Idx++];
3395           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3396           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3397                            FoundElse, ElseLoc);
3398         }
3399         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3400         while (Idx < End) {
3401           auto Loc = ReadSourceLocation(F, Record, Idx);
3402           bool WasSkipping = Record[Idx++];
3403           bool FoundNonSkip = Record[Idx++];
3404           bool FoundElse = Record[Idx++];
3405           ConditionalStack.push_back(
3406               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3407         }
3408         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3409       }
3410       break;
3411 
3412     case PP_COUNTER_VALUE:
3413       if (!Record.empty() && Listener)
3414         Listener->ReadCounter(F, Record[0]);
3415       break;
3416 
3417     case FILE_SORTED_DECLS:
3418       F.FileSortedDecls = (const DeclID *)Blob.data();
3419       F.NumFileSortedDecls = Record[0];
3420       break;
3421 
3422     case SOURCE_LOCATION_OFFSETS: {
3423       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3424       F.LocalNumSLocEntries = Record[0];
3425       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3426       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3427       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3428           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3429                                               SLocSpaceSize);
3430       if (!F.SLocEntryBaseID)
3431         return llvm::createStringError(std::errc::invalid_argument,
3432                                        "ran out of source locations");
3433       // Make our entry in the range map. BaseID is negative and growing, so
3434       // we invert it. Because we invert it, though, we need the other end of
3435       // the range.
3436       unsigned RangeStart =
3437           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3438       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3439       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3440 
3441       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3442       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3443       GlobalSLocOffsetMap.insert(
3444           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3445                            - SLocSpaceSize,&F));
3446 
3447       // Initialize the remapping table.
3448       // Invalid stays invalid.
3449       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3450       // This module. Base was 2 when being compiled.
3451       F.SLocRemap.insertOrReplace(std::make_pair(
3452           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3453 
3454       TotalNumSLocEntries += F.LocalNumSLocEntries;
3455       break;
3456     }
3457 
3458     case MODULE_OFFSET_MAP:
3459       F.ModuleOffsetMap = Blob;
3460       break;
3461 
3462     case SOURCE_MANAGER_LINE_TABLE:
3463       ParseLineTable(F, Record);
3464       break;
3465 
3466     case SOURCE_LOCATION_PRELOADS: {
3467       // Need to transform from the local view (1-based IDs) to the global view,
3468       // which is based off F.SLocEntryBaseID.
3469       if (!F.PreloadSLocEntries.empty())
3470         return llvm::createStringError(
3471             std::errc::illegal_byte_sequence,
3472             "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3473 
3474       F.PreloadSLocEntries.swap(Record);
3475       break;
3476     }
3477 
3478     case EXT_VECTOR_DECLS:
3479       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3480         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3481       break;
3482 
3483     case VTABLE_USES:
3484       if (Record.size() % 3 != 0)
3485         return llvm::createStringError(std::errc::illegal_byte_sequence,
3486                                        "Invalid VTABLE_USES record");
3487 
3488       // Later tables overwrite earlier ones.
3489       // FIXME: Modules will have some trouble with this. This is clearly not
3490       // the right way to do this.
3491       VTableUses.clear();
3492 
3493       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3494         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3495         VTableUses.push_back(
3496           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3497         VTableUses.push_back(Record[Idx++]);
3498       }
3499       break;
3500 
3501     case PENDING_IMPLICIT_INSTANTIATIONS:
3502       if (PendingInstantiations.size() % 2 != 0)
3503         return llvm::createStringError(
3504             std::errc::illegal_byte_sequence,
3505             "Invalid existing PendingInstantiations");
3506 
3507       if (Record.size() % 2 != 0)
3508         return llvm::createStringError(
3509             std::errc::illegal_byte_sequence,
3510             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3511 
3512       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3513         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3514         PendingInstantiations.push_back(
3515           ReadSourceLocation(F, Record, I).getRawEncoding());
3516       }
3517       break;
3518 
3519     case SEMA_DECL_REFS:
3520       if (Record.size() != 3)
3521         return llvm::createStringError(std::errc::illegal_byte_sequence,
3522                                        "Invalid SEMA_DECL_REFS block");
3523       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3524         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3525       break;
3526 
3527     case PPD_ENTITIES_OFFSETS: {
3528       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3529       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3530       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3531 
3532       unsigned LocalBasePreprocessedEntityID = Record[0];
3533 
3534       unsigned StartingID;
3535       if (!PP.getPreprocessingRecord())
3536         PP.createPreprocessingRecord();
3537       if (!PP.getPreprocessingRecord()->getExternalSource())
3538         PP.getPreprocessingRecord()->SetExternalSource(*this);
3539       StartingID
3540         = PP.getPreprocessingRecord()
3541             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3542       F.BasePreprocessedEntityID = StartingID;
3543 
3544       if (F.NumPreprocessedEntities > 0) {
3545         // Introduce the global -> local mapping for preprocessed entities in
3546         // this module.
3547         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3548 
3549         // Introduce the local -> global mapping for preprocessed entities in
3550         // this module.
3551         F.PreprocessedEntityRemap.insertOrReplace(
3552           std::make_pair(LocalBasePreprocessedEntityID,
3553             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3554       }
3555 
3556       break;
3557     }
3558 
3559     case PPD_SKIPPED_RANGES: {
3560       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3561       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3562       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3563 
3564       if (!PP.getPreprocessingRecord())
3565         PP.createPreprocessingRecord();
3566       if (!PP.getPreprocessingRecord()->getExternalSource())
3567         PP.getPreprocessingRecord()->SetExternalSource(*this);
3568       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3569           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3570 
3571       if (F.NumPreprocessedSkippedRanges > 0)
3572         GlobalSkippedRangeMap.insert(
3573             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3574       break;
3575     }
3576 
3577     case DECL_UPDATE_OFFSETS:
3578       if (Record.size() % 2 != 0)
3579         return llvm::createStringError(
3580             std::errc::illegal_byte_sequence,
3581             "invalid DECL_UPDATE_OFFSETS block in AST file");
3582       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3583         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3584         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3585 
3586         // If we've already loaded the decl, perform the updates when we finish
3587         // loading this block.
3588         if (Decl *D = GetExistingDecl(ID))
3589           PendingUpdateRecords.push_back(
3590               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3591       }
3592       break;
3593 
3594     case OBJC_CATEGORIES_MAP:
3595       if (F.LocalNumObjCCategoriesInMap != 0)
3596         return llvm::createStringError(
3597             std::errc::illegal_byte_sequence,
3598             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3599 
3600       F.LocalNumObjCCategoriesInMap = Record[0];
3601       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3602       break;
3603 
3604     case OBJC_CATEGORIES:
3605       F.ObjCCategories.swap(Record);
3606       break;
3607 
3608     case CUDA_SPECIAL_DECL_REFS:
3609       // Later tables overwrite earlier ones.
3610       // FIXME: Modules will have trouble with this.
3611       CUDASpecialDeclRefs.clear();
3612       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3613         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3614       break;
3615 
3616     case HEADER_SEARCH_TABLE:
3617       F.HeaderFileInfoTableData = Blob.data();
3618       F.LocalNumHeaderFileInfos = Record[1];
3619       if (Record[0]) {
3620         F.HeaderFileInfoTable
3621           = HeaderFileInfoLookupTable::Create(
3622                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3623                    (const unsigned char *)F.HeaderFileInfoTableData,
3624                    HeaderFileInfoTrait(*this, F,
3625                                        &PP.getHeaderSearchInfo(),
3626                                        Blob.data() + Record[2]));
3627 
3628         PP.getHeaderSearchInfo().SetExternalSource(this);
3629         if (!PP.getHeaderSearchInfo().getExternalLookup())
3630           PP.getHeaderSearchInfo().SetExternalLookup(this);
3631       }
3632       break;
3633 
3634     case FP_PRAGMA_OPTIONS:
3635       // Later tables overwrite earlier ones.
3636       FPPragmaOptions.swap(Record);
3637       break;
3638 
3639     case OPENCL_EXTENSIONS:
3640       for (unsigned I = 0, E = Record.size(); I != E; ) {
3641         auto Name = ReadString(Record, I);
3642         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3643         OptInfo.Supported = Record[I++] != 0;
3644         OptInfo.Enabled = Record[I++] != 0;
3645         OptInfo.WithPragma = Record[I++] != 0;
3646         OptInfo.Avail = Record[I++];
3647         OptInfo.Core = Record[I++];
3648         OptInfo.Opt = Record[I++];
3649       }
3650       break;
3651 
3652     case TENTATIVE_DEFINITIONS:
3653       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3654         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3655       break;
3656 
3657     case KNOWN_NAMESPACES:
3658       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3659         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3660       break;
3661 
3662     case UNDEFINED_BUT_USED:
3663       if (UndefinedButUsed.size() % 2 != 0)
3664         return llvm::createStringError(std::errc::illegal_byte_sequence,
3665                                        "Invalid existing UndefinedButUsed");
3666 
3667       if (Record.size() % 2 != 0)
3668         return llvm::createStringError(std::errc::illegal_byte_sequence,
3669                                        "invalid undefined-but-used record");
3670       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3671         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3672         UndefinedButUsed.push_back(
3673             ReadSourceLocation(F, Record, I).getRawEncoding());
3674       }
3675       break;
3676 
3677     case DELETE_EXPRS_TO_ANALYZE:
3678       for (unsigned I = 0, N = Record.size(); I != N;) {
3679         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3680         const uint64_t Count = Record[I++];
3681         DelayedDeleteExprs.push_back(Count);
3682         for (uint64_t C = 0; C < Count; ++C) {
3683           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3684           bool IsArrayForm = Record[I++] == 1;
3685           DelayedDeleteExprs.push_back(IsArrayForm);
3686         }
3687       }
3688       break;
3689 
3690     case IMPORTED_MODULES:
3691       if (!F.isModule()) {
3692         // If we aren't loading a module (which has its own exports), make
3693         // all of the imported modules visible.
3694         // FIXME: Deal with macros-only imports.
3695         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3696           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3697           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3698           if (GlobalID) {
3699             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3700             if (DeserializationListener)
3701               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3702           }
3703         }
3704       }
3705       break;
3706 
3707     case MACRO_OFFSET: {
3708       if (F.LocalNumMacros != 0)
3709         return llvm::createStringError(
3710             std::errc::illegal_byte_sequence,
3711             "duplicate MACRO_OFFSET record in AST file");
3712       F.MacroOffsets = (const uint32_t *)Blob.data();
3713       F.LocalNumMacros = Record[0];
3714       unsigned LocalBaseMacroID = Record[1];
3715       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3716       F.BaseMacroID = getTotalNumMacros();
3717 
3718       if (F.LocalNumMacros > 0) {
3719         // Introduce the global -> local mapping for macros within this module.
3720         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3721 
3722         // Introduce the local -> global mapping for macros within this module.
3723         F.MacroRemap.insertOrReplace(
3724           std::make_pair(LocalBaseMacroID,
3725                          F.BaseMacroID - LocalBaseMacroID));
3726 
3727         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3728       }
3729       break;
3730     }
3731 
3732     case PP_INCLUDED_FILES:
3733       readIncludedFiles(F, Blob, PP);
3734       break;
3735 
3736     case LATE_PARSED_TEMPLATE:
3737       LateParsedTemplates.emplace_back(
3738           std::piecewise_construct, std::forward_as_tuple(&F),
3739           std::forward_as_tuple(Record.begin(), Record.end()));
3740       break;
3741 
3742     case OPTIMIZE_PRAGMA_OPTIONS:
3743       if (Record.size() != 1)
3744         return llvm::createStringError(std::errc::illegal_byte_sequence,
3745                                        "invalid pragma optimize record");
3746       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3747       break;
3748 
3749     case MSSTRUCT_PRAGMA_OPTIONS:
3750       if (Record.size() != 1)
3751         return llvm::createStringError(std::errc::illegal_byte_sequence,
3752                                        "invalid pragma ms_struct record");
3753       PragmaMSStructState = Record[0];
3754       break;
3755 
3756     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3757       if (Record.size() != 2)
3758         return llvm::createStringError(
3759             std::errc::illegal_byte_sequence,
3760             "invalid pragma pointers to members record");
3761       PragmaMSPointersToMembersState = Record[0];
3762       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3763       break;
3764 
3765     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3766       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3767         UnusedLocalTypedefNameCandidates.push_back(
3768             getGlobalDeclID(F, Record[I]));
3769       break;
3770 
3771     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3772       if (Record.size() != 1)
3773         return llvm::createStringError(std::errc::illegal_byte_sequence,
3774                                        "invalid cuda pragma options record");
3775       ForceCUDAHostDeviceDepth = Record[0];
3776       break;
3777 
3778     case ALIGN_PACK_PRAGMA_OPTIONS: {
3779       if (Record.size() < 3)
3780         return llvm::createStringError(std::errc::illegal_byte_sequence,
3781                                        "invalid pragma pack record");
3782       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3783       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3784       unsigned NumStackEntries = Record[2];
3785       unsigned Idx = 3;
3786       // Reset the stack when importing a new module.
3787       PragmaAlignPackStack.clear();
3788       for (unsigned I = 0; I < NumStackEntries; ++I) {
3789         PragmaAlignPackStackEntry Entry;
3790         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3791         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3792         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3793         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3794         Entry.SlotLabel = PragmaAlignPackStrings.back();
3795         PragmaAlignPackStack.push_back(Entry);
3796       }
3797       break;
3798     }
3799 
3800     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3801       if (Record.size() < 3)
3802         return llvm::createStringError(std::errc::illegal_byte_sequence,
3803                                        "invalid pragma float control record");
3804       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3805       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3806       unsigned NumStackEntries = Record[2];
3807       unsigned Idx = 3;
3808       // Reset the stack when importing a new module.
3809       FpPragmaStack.clear();
3810       for (unsigned I = 0; I < NumStackEntries; ++I) {
3811         FpPragmaStackEntry Entry;
3812         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3813         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3814         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3815         FpPragmaStrings.push_back(ReadString(Record, Idx));
3816         Entry.SlotLabel = FpPragmaStrings.back();
3817         FpPragmaStack.push_back(Entry);
3818       }
3819       break;
3820     }
3821 
3822     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3823       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3824         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3825       break;
3826     }
3827   }
3828 }
3829 
3830 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3831   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3832 
3833   // Additional remapping information.
3834   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3835   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3836   F.ModuleOffsetMap = StringRef();
3837 
3838   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3839   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3840     F.SLocRemap.insert(std::make_pair(0U, 0));
3841     F.SLocRemap.insert(std::make_pair(2U, 1));
3842   }
3843 
3844   // Continuous range maps we may be updating in our module.
3845   using SLocRemapBuilder =
3846       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3847                          2>::Builder;
3848   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3849   SLocRemapBuilder SLocRemap(F.SLocRemap);
3850   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3851   RemapBuilder MacroRemap(F.MacroRemap);
3852   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3853   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3854   RemapBuilder SelectorRemap(F.SelectorRemap);
3855   RemapBuilder DeclRemap(F.DeclRemap);
3856   RemapBuilder TypeRemap(F.TypeRemap);
3857 
3858   while (Data < DataEnd) {
3859     // FIXME: Looking up dependency modules by filename is horrible. Let's
3860     // start fixing this with prebuilt, explicit and implicit modules and see
3861     // how it goes...
3862     using namespace llvm::support;
3863     ModuleKind Kind = static_cast<ModuleKind>(
3864       endian::readNext<uint8_t, little, unaligned>(Data));
3865     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3866     StringRef Name = StringRef((const char*)Data, Len);
3867     Data += Len;
3868     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3869                               Kind == MK_ImplicitModule
3870                           ? ModuleMgr.lookupByModuleName(Name)
3871                           : ModuleMgr.lookupByFileName(Name));
3872     if (!OM) {
3873       std::string Msg =
3874           "SourceLocation remap refers to unknown module, cannot find ";
3875       Msg.append(std::string(Name));
3876       Error(Msg);
3877       return;
3878     }
3879 
3880     SourceLocation::UIntTy SLocOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882     uint32_t IdentifierIDOffset =
3883         endian::readNext<uint32_t, little, unaligned>(Data);
3884     uint32_t MacroIDOffset =
3885         endian::readNext<uint32_t, little, unaligned>(Data);
3886     uint32_t PreprocessedEntityIDOffset =
3887         endian::readNext<uint32_t, little, unaligned>(Data);
3888     uint32_t SubmoduleIDOffset =
3889         endian::readNext<uint32_t, little, unaligned>(Data);
3890     uint32_t SelectorIDOffset =
3891         endian::readNext<uint32_t, little, unaligned>(Data);
3892     uint32_t DeclIDOffset =
3893         endian::readNext<uint32_t, little, unaligned>(Data);
3894     uint32_t TypeIndexOffset =
3895         endian::readNext<uint32_t, little, unaligned>(Data);
3896 
3897     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3898                          RemapBuilder &Remap) {
3899       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3900       if (Offset != None)
3901         Remap.insert(std::make_pair(Offset,
3902                                     static_cast<int>(BaseOffset - Offset)));
3903     };
3904 
3905     constexpr SourceLocation::UIntTy SLocNone =
3906         std::numeric_limits<SourceLocation::UIntTy>::max();
3907     if (SLocOffset != SLocNone)
3908       SLocRemap.insert(std::make_pair(
3909           SLocOffset, static_cast<SourceLocation::IntTy>(
3910                           OM->SLocEntryBaseOffset - SLocOffset)));
3911 
3912     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3913     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3914     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3915               PreprocessedEntityRemap);
3916     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3917     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3918     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3919     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3920 
3921     // Global -> local mappings.
3922     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3923   }
3924 }
3925 
3926 ASTReader::ASTReadResult
3927 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3928                                   const ModuleFile *ImportedBy,
3929                                   unsigned ClientLoadCapabilities) {
3930   unsigned Idx = 0;
3931   F.ModuleMapPath = ReadPath(F, Record, Idx);
3932 
3933   // Try to resolve ModuleName in the current header search context and
3934   // verify that it is found in the same module map file as we saved. If the
3935   // top-level AST file is a main file, skip this check because there is no
3936   // usable header search context.
3937   assert(!F.ModuleName.empty() &&
3938          "MODULE_NAME should come before MODULE_MAP_FILE");
3939   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3940     // An implicitly-loaded module file should have its module listed in some
3941     // module map file that we've already loaded.
3942     Module *M =
3943         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
3944     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3945     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3946     // Don't emit module relocation error if we have -fno-validate-pch
3947     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3948               DisableValidationForModuleKind::Module) &&
3949         !ModMap) {
3950       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3951         if (auto ASTFE = M ? M->getASTFile() : None) {
3952           // This module was defined by an imported (explicit) module.
3953           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3954                                                << ASTFE->getName();
3955         } else {
3956           // This module was built with a different module map.
3957           Diag(diag::err_imported_module_not_found)
3958               << F.ModuleName << F.FileName
3959               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3960               << !ImportedBy;
3961           // In case it was imported by a PCH, there's a chance the user is
3962           // just missing to include the search path to the directory containing
3963           // the modulemap.
3964           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3965             Diag(diag::note_imported_by_pch_module_not_found)
3966                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3967         }
3968       }
3969       return OutOfDate;
3970     }
3971 
3972     assert(M && M->Name == F.ModuleName && "found module with different name");
3973 
3974     // Check the primary module map file.
3975     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3976     if (!StoredModMap || *StoredModMap != ModMap) {
3977       assert(ModMap && "found module is missing module map file");
3978       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3979              "top-level import should be verified");
3980       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3981       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3982         Diag(diag::err_imported_module_modmap_changed)
3983             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3984             << ModMap->getName() << F.ModuleMapPath << NotImported;
3985       return OutOfDate;
3986     }
3987 
3988     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3989     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3990       // FIXME: we should use input files rather than storing names.
3991       std::string Filename = ReadPath(F, Record, Idx);
3992       auto SF = FileMgr.getFile(Filename, false, false);
3993       if (!SF) {
3994         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3995           Error("could not find file '" + Filename +"' referenced by AST file");
3996         return OutOfDate;
3997       }
3998       AdditionalStoredMaps.insert(*SF);
3999     }
4000 
4001     // Check any additional module map files (e.g. module.private.modulemap)
4002     // that are not in the pcm.
4003     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4004       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
4005         // Remove files that match
4006         // Note: SmallPtrSet::erase is really remove
4007         if (!AdditionalStoredMaps.erase(ModMap)) {
4008           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4009             Diag(diag::err_module_different_modmap)
4010               << F.ModuleName << /*new*/0 << ModMap->getName();
4011           return OutOfDate;
4012         }
4013       }
4014     }
4015 
4016     // Check any additional module map files that are in the pcm, but not
4017     // found in header search. Cases that match are already removed.
4018     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4019       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4020         Diag(diag::err_module_different_modmap)
4021           << F.ModuleName << /*not new*/1 << ModMap->getName();
4022       return OutOfDate;
4023     }
4024   }
4025 
4026   if (Listener)
4027     Listener->ReadModuleMapFile(F.ModuleMapPath);
4028   return Success;
4029 }
4030 
4031 /// Move the given method to the back of the global list of methods.
4032 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4033   // Find the entry for this selector in the method pool.
4034   Sema::GlobalMethodPool::iterator Known
4035     = S.MethodPool.find(Method->getSelector());
4036   if (Known == S.MethodPool.end())
4037     return;
4038 
4039   // Retrieve the appropriate method list.
4040   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4041                                                     : Known->second.second;
4042   bool Found = false;
4043   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4044     if (!Found) {
4045       if (List->getMethod() == Method) {
4046         Found = true;
4047       } else {
4048         // Keep searching.
4049         continue;
4050       }
4051     }
4052 
4053     if (List->getNext())
4054       List->setMethod(List->getNext()->getMethod());
4055     else
4056       List->setMethod(Method);
4057   }
4058 }
4059 
4060 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4061   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4062   for (Decl *D : Names) {
4063     bool wasHidden = !D->isUnconditionallyVisible();
4064     D->setVisibleDespiteOwningModule();
4065 
4066     if (wasHidden && SemaObj) {
4067       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4068         moveMethodToBackOfGlobalList(*SemaObj, Method);
4069       }
4070     }
4071   }
4072 }
4073 
4074 void ASTReader::makeModuleVisible(Module *Mod,
4075                                   Module::NameVisibilityKind NameVisibility,
4076                                   SourceLocation ImportLoc) {
4077   llvm::SmallPtrSet<Module *, 4> Visited;
4078   SmallVector<Module *, 4> Stack;
4079   Stack.push_back(Mod);
4080   while (!Stack.empty()) {
4081     Mod = Stack.pop_back_val();
4082 
4083     if (NameVisibility <= Mod->NameVisibility) {
4084       // This module already has this level of visibility (or greater), so
4085       // there is nothing more to do.
4086       continue;
4087     }
4088 
4089     if (Mod->isUnimportable()) {
4090       // Modules that aren't importable cannot be made visible.
4091       continue;
4092     }
4093 
4094     // Update the module's name visibility.
4095     Mod->NameVisibility = NameVisibility;
4096 
4097     // If we've already deserialized any names from this module,
4098     // mark them as visible.
4099     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4100     if (Hidden != HiddenNamesMap.end()) {
4101       auto HiddenNames = std::move(*Hidden);
4102       HiddenNamesMap.erase(Hidden);
4103       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4104       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4105              "making names visible added hidden names");
4106     }
4107 
4108     // Push any exported modules onto the stack to be marked as visible.
4109     SmallVector<Module *, 16> Exports;
4110     Mod->getExportedModules(Exports);
4111     for (SmallVectorImpl<Module *>::iterator
4112            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4113       Module *Exported = *I;
4114       if (Visited.insert(Exported).second)
4115         Stack.push_back(Exported);
4116     }
4117   }
4118 }
4119 
4120 /// We've merged the definition \p MergedDef into the existing definition
4121 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4122 /// visible.
4123 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4124                                           NamedDecl *MergedDef) {
4125   if (!Def->isUnconditionallyVisible()) {
4126     // If MergedDef is visible or becomes visible, make the definition visible.
4127     if (MergedDef->isUnconditionallyVisible())
4128       Def->setVisibleDespiteOwningModule();
4129     else {
4130       getContext().mergeDefinitionIntoModule(
4131           Def, MergedDef->getImportedOwningModule(),
4132           /*NotifyListeners*/ false);
4133       PendingMergedDefinitionsToDeduplicate.insert(Def);
4134     }
4135   }
4136 }
4137 
4138 bool ASTReader::loadGlobalIndex() {
4139   if (GlobalIndex)
4140     return false;
4141 
4142   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4143       !PP.getLangOpts().Modules)
4144     return true;
4145 
4146   // Try to load the global index.
4147   TriedLoadingGlobalIndex = true;
4148   StringRef ModuleCachePath
4149     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4150   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4151       GlobalModuleIndex::readIndex(ModuleCachePath);
4152   if (llvm::Error Err = std::move(Result.second)) {
4153     assert(!Result.first);
4154     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4155     return true;
4156   }
4157 
4158   GlobalIndex.reset(Result.first);
4159   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4160   return false;
4161 }
4162 
4163 bool ASTReader::isGlobalIndexUnavailable() const {
4164   return PP.getLangOpts().Modules && UseGlobalIndex &&
4165          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4166 }
4167 
4168 static void updateModuleTimestamp(ModuleFile &MF) {
4169   // Overwrite the timestamp file contents so that file's mtime changes.
4170   std::string TimestampFilename = MF.getTimestampFilename();
4171   std::error_code EC;
4172   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4173                           llvm::sys::fs::OF_TextWithCRLF);
4174   if (EC)
4175     return;
4176   OS << "Timestamp file\n";
4177   OS.close();
4178   OS.clear_error(); // Avoid triggering a fatal error.
4179 }
4180 
4181 /// Given a cursor at the start of an AST file, scan ahead and drop the
4182 /// cursor into the start of the given block ID, returning false on success and
4183 /// true on failure.
4184 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4185   while (true) {
4186     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4187     if (!MaybeEntry) {
4188       // FIXME this drops errors on the floor.
4189       consumeError(MaybeEntry.takeError());
4190       return true;
4191     }
4192     llvm::BitstreamEntry Entry = MaybeEntry.get();
4193 
4194     switch (Entry.Kind) {
4195     case llvm::BitstreamEntry::Error:
4196     case llvm::BitstreamEntry::EndBlock:
4197       return true;
4198 
4199     case llvm::BitstreamEntry::Record:
4200       // Ignore top-level records.
4201       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4202         break;
4203       else {
4204         // FIXME this drops errors on the floor.
4205         consumeError(Skipped.takeError());
4206         return true;
4207       }
4208 
4209     case llvm::BitstreamEntry::SubBlock:
4210       if (Entry.ID == BlockID) {
4211         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4212           // FIXME this drops the error on the floor.
4213           consumeError(std::move(Err));
4214           return true;
4215         }
4216         // Found it!
4217         return false;
4218       }
4219 
4220       if (llvm::Error Err = Cursor.SkipBlock()) {
4221         // FIXME this drops the error on the floor.
4222         consumeError(std::move(Err));
4223         return true;
4224       }
4225     }
4226   }
4227 }
4228 
4229 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4230                                             ModuleKind Type,
4231                                             SourceLocation ImportLoc,
4232                                             unsigned ClientLoadCapabilities,
4233                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4234   llvm::SaveAndRestore<SourceLocation>
4235     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4236   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4237       CurrentDeserializingModuleKind, Type);
4238 
4239   // Defer any pending actions until we get to the end of reading the AST file.
4240   Deserializing AnASTFile(this);
4241 
4242   // Bump the generation number.
4243   unsigned PreviousGeneration = 0;
4244   if (ContextObj)
4245     PreviousGeneration = incrementGeneration(*ContextObj);
4246 
4247   unsigned NumModules = ModuleMgr.size();
4248   SmallVector<ImportedModule, 4> Loaded;
4249   if (ASTReadResult ReadResult =
4250           ReadASTCore(FileName, Type, ImportLoc,
4251                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4252                       ClientLoadCapabilities)) {
4253     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4254                             PP.getLangOpts().Modules
4255                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4256                                 : nullptr);
4257 
4258     // If we find that any modules are unusable, the global index is going
4259     // to be out-of-date. Just remove it.
4260     GlobalIndex.reset();
4261     ModuleMgr.setGlobalIndex(nullptr);
4262     return ReadResult;
4263   }
4264 
4265   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4266   // remove modules from this point. Various fields are updated during reading
4267   // the AST block and removing the modules would result in dangling pointers.
4268   // They are generally only incidentally dereferenced, ie. a binary search
4269   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4270   // be dereferenced but it wouldn't actually be used.
4271 
4272   // Load the AST blocks of all of the modules that we loaded. We can still
4273   // hit errors parsing the ASTs at this point.
4274   for (ImportedModule &M : Loaded) {
4275     ModuleFile &F = *M.Mod;
4276 
4277     // Read the AST block.
4278     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4279       Error(std::move(Err));
4280       return Failure;
4281     }
4282 
4283     // The AST block should always have a definition for the main module.
4284     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4285       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4286       return Failure;
4287     }
4288 
4289     // Read the extension blocks.
4290     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4291       if (llvm::Error Err = ReadExtensionBlock(F)) {
4292         Error(std::move(Err));
4293         return Failure;
4294       }
4295     }
4296 
4297     // Once read, set the ModuleFile bit base offset and update the size in
4298     // bits of all files we've seen.
4299     F.GlobalBitOffset = TotalModulesSizeInBits;
4300     TotalModulesSizeInBits += F.SizeInBits;
4301     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4302   }
4303 
4304   // Preload source locations and interesting indentifiers.
4305   for (ImportedModule &M : Loaded) {
4306     ModuleFile &F = *M.Mod;
4307 
4308     // Preload SLocEntries.
4309     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4310       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4311       // Load it through the SourceManager and don't call ReadSLocEntry()
4312       // directly because the entry may have already been loaded in which case
4313       // calling ReadSLocEntry() directly would trigger an assertion in
4314       // SourceManager.
4315       SourceMgr.getLoadedSLocEntryByID(Index);
4316     }
4317 
4318     // Map the original source file ID into the ID space of the current
4319     // compilation.
4320     if (F.OriginalSourceFileID.isValid()) {
4321       F.OriginalSourceFileID = FileID::get(
4322           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4323     }
4324 
4325     // Preload all the pending interesting identifiers by marking them out of
4326     // date.
4327     for (auto Offset : F.PreloadIdentifierOffsets) {
4328       const unsigned char *Data = F.IdentifierTableData + Offset;
4329 
4330       ASTIdentifierLookupTrait Trait(*this, F);
4331       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4332       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4333       auto &II = PP.getIdentifierTable().getOwn(Key);
4334       II.setOutOfDate(true);
4335 
4336       // Mark this identifier as being from an AST file so that we can track
4337       // whether we need to serialize it.
4338       markIdentifierFromAST(*this, II);
4339 
4340       // Associate the ID with the identifier so that the writer can reuse it.
4341       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4342       SetIdentifierInfo(ID, &II);
4343     }
4344   }
4345 
4346   // Setup the import locations and notify the module manager that we've
4347   // committed to these module files.
4348   for (ImportedModule &M : Loaded) {
4349     ModuleFile &F = *M.Mod;
4350 
4351     ModuleMgr.moduleFileAccepted(&F);
4352 
4353     // Set the import location.
4354     F.DirectImportLoc = ImportLoc;
4355     // FIXME: We assume that locations from PCH / preamble do not need
4356     // any translation.
4357     if (!M.ImportedBy)
4358       F.ImportLoc = M.ImportLoc;
4359     else
4360       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4361   }
4362 
4363   if (!PP.getLangOpts().CPlusPlus ||
4364       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4365        Type != MK_PrebuiltModule)) {
4366     // Mark all of the identifiers in the identifier table as being out of date,
4367     // so that various accessors know to check the loaded modules when the
4368     // identifier is used.
4369     //
4370     // For C++ modules, we don't need information on many identifiers (just
4371     // those that provide macros or are poisoned), so we mark all of
4372     // the interesting ones via PreloadIdentifierOffsets.
4373     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4374                                 IdEnd = PP.getIdentifierTable().end();
4375          Id != IdEnd; ++Id)
4376       Id->second->setOutOfDate(true);
4377   }
4378   // Mark selectors as out of date.
4379   for (auto Sel : SelectorGeneration)
4380     SelectorOutOfDate[Sel.first] = true;
4381 
4382   // Resolve any unresolved module exports.
4383   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4384     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4385     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4386     Module *ResolvedMod = getSubmodule(GlobalID);
4387 
4388     switch (Unresolved.Kind) {
4389     case UnresolvedModuleRef::Conflict:
4390       if (ResolvedMod) {
4391         Module::Conflict Conflict;
4392         Conflict.Other = ResolvedMod;
4393         Conflict.Message = Unresolved.String.str();
4394         Unresolved.Mod->Conflicts.push_back(Conflict);
4395       }
4396       continue;
4397 
4398     case UnresolvedModuleRef::Import:
4399       if (ResolvedMod)
4400         Unresolved.Mod->Imports.insert(ResolvedMod);
4401       continue;
4402 
4403     case UnresolvedModuleRef::Export:
4404       if (ResolvedMod || Unresolved.IsWildcard)
4405         Unresolved.Mod->Exports.push_back(
4406           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4407       continue;
4408     }
4409   }
4410   UnresolvedModuleRefs.clear();
4411 
4412   if (Imported)
4413     Imported->append(ImportedModules.begin(),
4414                      ImportedModules.end());
4415 
4416   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4417   // Might be unnecessary as use declarations are only used to build the
4418   // module itself.
4419 
4420   if (ContextObj)
4421     InitializeContext();
4422 
4423   if (SemaObj)
4424     UpdateSema();
4425 
4426   if (DeserializationListener)
4427     DeserializationListener->ReaderInitialized(this);
4428 
4429   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4430   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4431     // If this AST file is a precompiled preamble, then set the
4432     // preamble file ID of the source manager to the file source file
4433     // from which the preamble was built.
4434     if (Type == MK_Preamble) {
4435       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4436     } else if (Type == MK_MainFile) {
4437       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4438     }
4439   }
4440 
4441   // For any Objective-C class definitions we have already loaded, make sure
4442   // that we load any additional categories.
4443   if (ContextObj) {
4444     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4445       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4446                          ObjCClassesLoaded[I],
4447                          PreviousGeneration);
4448     }
4449   }
4450 
4451   if (PP.getHeaderSearchInfo()
4452           .getHeaderSearchOpts()
4453           .ModulesValidateOncePerBuildSession) {
4454     // Now we are certain that the module and all modules it depends on are
4455     // up to date.  Create or update timestamp files for modules that are
4456     // located in the module cache (not for PCH files that could be anywhere
4457     // in the filesystem).
4458     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4459       ImportedModule &M = Loaded[I];
4460       if (M.Mod->Kind == MK_ImplicitModule) {
4461         updateModuleTimestamp(*M.Mod);
4462       }
4463     }
4464   }
4465 
4466   return Success;
4467 }
4468 
4469 static ASTFileSignature readASTFileSignature(StringRef PCH);
4470 
4471 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4472 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4473   // FIXME checking magic headers is done in other places such as
4474   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4475   // always done the same. Unify it all with a helper.
4476   if (!Stream.canSkipToPos(4))
4477     return llvm::createStringError(std::errc::illegal_byte_sequence,
4478                                    "file too small to contain AST file magic");
4479   for (unsigned C : {'C', 'P', 'C', 'H'})
4480     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4481       if (Res.get() != C)
4482         return llvm::createStringError(
4483             std::errc::illegal_byte_sequence,
4484             "file doesn't start with AST file magic");
4485     } else
4486       return Res.takeError();
4487   return llvm::Error::success();
4488 }
4489 
4490 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4491   switch (Kind) {
4492   case MK_PCH:
4493     return 0; // PCH
4494   case MK_ImplicitModule:
4495   case MK_ExplicitModule:
4496   case MK_PrebuiltModule:
4497     return 1; // module
4498   case MK_MainFile:
4499   case MK_Preamble:
4500     return 2; // main source file
4501   }
4502   llvm_unreachable("unknown module kind");
4503 }
4504 
4505 ASTReader::ASTReadResult
4506 ASTReader::ReadASTCore(StringRef FileName,
4507                        ModuleKind Type,
4508                        SourceLocation ImportLoc,
4509                        ModuleFile *ImportedBy,
4510                        SmallVectorImpl<ImportedModule> &Loaded,
4511                        off_t ExpectedSize, time_t ExpectedModTime,
4512                        ASTFileSignature ExpectedSignature,
4513                        unsigned ClientLoadCapabilities) {
4514   ModuleFile *M;
4515   std::string ErrorStr;
4516   ModuleManager::AddModuleResult AddResult
4517     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4518                           getGeneration(), ExpectedSize, ExpectedModTime,
4519                           ExpectedSignature, readASTFileSignature,
4520                           M, ErrorStr);
4521 
4522   switch (AddResult) {
4523   case ModuleManager::AlreadyLoaded:
4524     Diag(diag::remark_module_import)
4525         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4526         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4527     return Success;
4528 
4529   case ModuleManager::NewlyLoaded:
4530     // Load module file below.
4531     break;
4532 
4533   case ModuleManager::Missing:
4534     // The module file was missing; if the client can handle that, return
4535     // it.
4536     if (ClientLoadCapabilities & ARR_Missing)
4537       return Missing;
4538 
4539     // Otherwise, return an error.
4540     Diag(diag::err_ast_file_not_found)
4541         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4542         << ErrorStr;
4543     return Failure;
4544 
4545   case ModuleManager::OutOfDate:
4546     // We couldn't load the module file because it is out-of-date. If the
4547     // client can handle out-of-date, return it.
4548     if (ClientLoadCapabilities & ARR_OutOfDate)
4549       return OutOfDate;
4550 
4551     // Otherwise, return an error.
4552     Diag(diag::err_ast_file_out_of_date)
4553         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4554         << ErrorStr;
4555     return Failure;
4556   }
4557 
4558   assert(M && "Missing module file");
4559 
4560   bool ShouldFinalizePCM = false;
4561   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4562     auto &MC = getModuleManager().getModuleCache();
4563     if (ShouldFinalizePCM)
4564       MC.finalizePCM(FileName);
4565     else
4566       MC.tryToDropPCM(FileName);
4567   });
4568   ModuleFile &F = *M;
4569   BitstreamCursor &Stream = F.Stream;
4570   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4571   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4572 
4573   // Sniff for the signature.
4574   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4575     Diag(diag::err_ast_file_invalid)
4576         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4577     return Failure;
4578   }
4579 
4580   // This is used for compatibility with older PCH formats.
4581   bool HaveReadControlBlock = false;
4582   while (true) {
4583     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4584     if (!MaybeEntry) {
4585       Error(MaybeEntry.takeError());
4586       return Failure;
4587     }
4588     llvm::BitstreamEntry Entry = MaybeEntry.get();
4589 
4590     switch (Entry.Kind) {
4591     case llvm::BitstreamEntry::Error:
4592     case llvm::BitstreamEntry::Record:
4593     case llvm::BitstreamEntry::EndBlock:
4594       Error("invalid record at top-level of AST file");
4595       return Failure;
4596 
4597     case llvm::BitstreamEntry::SubBlock:
4598       break;
4599     }
4600 
4601     switch (Entry.ID) {
4602     case CONTROL_BLOCK_ID:
4603       HaveReadControlBlock = true;
4604       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4605       case Success:
4606         // Check that we didn't try to load a non-module AST file as a module.
4607         //
4608         // FIXME: Should we also perform the converse check? Loading a module as
4609         // a PCH file sort of works, but it's a bit wonky.
4610         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4611              Type == MK_PrebuiltModule) &&
4612             F.ModuleName.empty()) {
4613           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4614           if (Result != OutOfDate ||
4615               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4616             Diag(diag::err_module_file_not_module) << FileName;
4617           return Result;
4618         }
4619         break;
4620 
4621       case Failure: return Failure;
4622       case Missing: return Missing;
4623       case OutOfDate: return OutOfDate;
4624       case VersionMismatch: return VersionMismatch;
4625       case ConfigurationMismatch: return ConfigurationMismatch;
4626       case HadErrors: return HadErrors;
4627       }
4628       break;
4629 
4630     case AST_BLOCK_ID:
4631       if (!HaveReadControlBlock) {
4632         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4633           Diag(diag::err_pch_version_too_old);
4634         return VersionMismatch;
4635       }
4636 
4637       // Record that we've loaded this module.
4638       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4639       ShouldFinalizePCM = true;
4640       return Success;
4641 
4642     case UNHASHED_CONTROL_BLOCK_ID:
4643       // This block is handled using look-ahead during ReadControlBlock.  We
4644       // shouldn't get here!
4645       Error("malformed block record in AST file");
4646       return Failure;
4647 
4648     default:
4649       if (llvm::Error Err = Stream.SkipBlock()) {
4650         Error(std::move(Err));
4651         return Failure;
4652       }
4653       break;
4654     }
4655   }
4656 
4657   llvm_unreachable("unexpected break; expected return");
4658 }
4659 
4660 ASTReader::ASTReadResult
4661 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4662                                     unsigned ClientLoadCapabilities) {
4663   const HeaderSearchOptions &HSOpts =
4664       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4665   bool AllowCompatibleConfigurationMismatch =
4666       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4667   bool DisableValidation = shouldDisableValidationForFile(F);
4668 
4669   ASTReadResult Result = readUnhashedControlBlockImpl(
4670       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4671       Listener.get(),
4672       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4673 
4674   // If F was directly imported by another module, it's implicitly validated by
4675   // the importing module.
4676   if (DisableValidation || WasImportedBy ||
4677       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4678     return Success;
4679 
4680   if (Result == Failure) {
4681     Error("malformed block record in AST file");
4682     return Failure;
4683   }
4684 
4685   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4686     // If this module has already been finalized in the ModuleCache, we're stuck
4687     // with it; we can only load a single version of each module.
4688     //
4689     // This can happen when a module is imported in two contexts: in one, as a
4690     // user module; in another, as a system module (due to an import from
4691     // another module marked with the [system] flag).  It usually indicates a
4692     // bug in the module map: this module should also be marked with [system].
4693     //
4694     // If -Wno-system-headers (the default), and the first import is as a
4695     // system module, then validation will fail during the as-user import,
4696     // since -Werror flags won't have been validated.  However, it's reasonable
4697     // to treat this consistently as a system module.
4698     //
4699     // If -Wsystem-headers, the PCM on disk was built with
4700     // -Wno-system-headers, and the first import is as a user module, then
4701     // validation will fail during the as-system import since the PCM on disk
4702     // doesn't guarantee that -Werror was respected.  However, the -Werror
4703     // flags were checked during the initial as-user import.
4704     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4705       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4706       return Success;
4707     }
4708   }
4709 
4710   return Result;
4711 }
4712 
4713 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4714     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4715     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4716     bool ValidateDiagnosticOptions) {
4717   // Initialize a stream.
4718   BitstreamCursor Stream(StreamData);
4719 
4720   // Sniff for the signature.
4721   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4722     // FIXME this drops the error on the floor.
4723     consumeError(std::move(Err));
4724     return Failure;
4725   }
4726 
4727   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4728   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4729     return Failure;
4730 
4731   // Read all of the records in the options block.
4732   RecordData Record;
4733   ASTReadResult Result = Success;
4734   while (true) {
4735     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4736     if (!MaybeEntry) {
4737       // FIXME this drops the error on the floor.
4738       consumeError(MaybeEntry.takeError());
4739       return Failure;
4740     }
4741     llvm::BitstreamEntry Entry = MaybeEntry.get();
4742 
4743     switch (Entry.Kind) {
4744     case llvm::BitstreamEntry::Error:
4745     case llvm::BitstreamEntry::SubBlock:
4746       return Failure;
4747 
4748     case llvm::BitstreamEntry::EndBlock:
4749       return Result;
4750 
4751     case llvm::BitstreamEntry::Record:
4752       // The interesting case.
4753       break;
4754     }
4755 
4756     // Read and process a record.
4757     Record.clear();
4758     StringRef Blob;
4759     Expected<unsigned> MaybeRecordType =
4760         Stream.readRecord(Entry.ID, Record, &Blob);
4761     if (!MaybeRecordType) {
4762       // FIXME this drops the error.
4763       return Failure;
4764     }
4765     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4766     case SIGNATURE:
4767       if (F)
4768         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4769       break;
4770     case AST_BLOCK_HASH:
4771       if (F)
4772         F->ASTBlockHash =
4773             ASTFileSignature::create(Record.begin(), Record.end());
4774       break;
4775     case DIAGNOSTIC_OPTIONS: {
4776       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4777       if (Listener && ValidateDiagnosticOptions &&
4778           !AllowCompatibleConfigurationMismatch &&
4779           ParseDiagnosticOptions(Record, Complain, *Listener))
4780         Result = OutOfDate; // Don't return early.  Read the signature.
4781       break;
4782     }
4783     case DIAG_PRAGMA_MAPPINGS:
4784       if (!F)
4785         break;
4786       if (F->PragmaDiagMappings.empty())
4787         F->PragmaDiagMappings.swap(Record);
4788       else
4789         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4790                                      Record.begin(), Record.end());
4791       break;
4792     case HEADER_SEARCH_ENTRY_USAGE:
4793       if (!F)
4794         break;
4795       unsigned Count = Record[0];
4796       const char *Byte = Blob.data();
4797       F->SearchPathUsage = llvm::BitVector(Count, false);
4798       for (unsigned I = 0; I < Count; ++Byte)
4799         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4800           if (*Byte & (1 << Bit))
4801             F->SearchPathUsage[I] = true;
4802       break;
4803     }
4804   }
4805 }
4806 
4807 /// Parse a record and blob containing module file extension metadata.
4808 static bool parseModuleFileExtensionMetadata(
4809               const SmallVectorImpl<uint64_t> &Record,
4810               StringRef Blob,
4811               ModuleFileExtensionMetadata &Metadata) {
4812   if (Record.size() < 4) return true;
4813 
4814   Metadata.MajorVersion = Record[0];
4815   Metadata.MinorVersion = Record[1];
4816 
4817   unsigned BlockNameLen = Record[2];
4818   unsigned UserInfoLen = Record[3];
4819 
4820   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4821 
4822   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4823   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4824                                   Blob.data() + BlockNameLen + UserInfoLen);
4825   return false;
4826 }
4827 
4828 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4829   BitstreamCursor &Stream = F.Stream;
4830 
4831   RecordData Record;
4832   while (true) {
4833     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4834     if (!MaybeEntry)
4835       return MaybeEntry.takeError();
4836     llvm::BitstreamEntry Entry = MaybeEntry.get();
4837 
4838     switch (Entry.Kind) {
4839     case llvm::BitstreamEntry::SubBlock:
4840       if (llvm::Error Err = Stream.SkipBlock())
4841         return Err;
4842       continue;
4843     case llvm::BitstreamEntry::EndBlock:
4844       return llvm::Error::success();
4845     case llvm::BitstreamEntry::Error:
4846       return llvm::createStringError(std::errc::illegal_byte_sequence,
4847                                      "malformed block record in AST file");
4848     case llvm::BitstreamEntry::Record:
4849       break;
4850     }
4851 
4852     Record.clear();
4853     StringRef Blob;
4854     Expected<unsigned> MaybeRecCode =
4855         Stream.readRecord(Entry.ID, Record, &Blob);
4856     if (!MaybeRecCode)
4857       return MaybeRecCode.takeError();
4858     switch (MaybeRecCode.get()) {
4859     case EXTENSION_METADATA: {
4860       ModuleFileExtensionMetadata Metadata;
4861       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4862         return llvm::createStringError(
4863             std::errc::illegal_byte_sequence,
4864             "malformed EXTENSION_METADATA in AST file");
4865 
4866       // Find a module file extension with this block name.
4867       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4868       if (Known == ModuleFileExtensions.end()) break;
4869 
4870       // Form a reader.
4871       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4872                                                              F, Stream)) {
4873         F.ExtensionReaders.push_back(std::move(Reader));
4874       }
4875 
4876       break;
4877     }
4878     }
4879   }
4880 
4881   return llvm::Error::success();
4882 }
4883 
4884 void ASTReader::InitializeContext() {
4885   assert(ContextObj && "no context to initialize");
4886   ASTContext &Context = *ContextObj;
4887 
4888   // If there's a listener, notify them that we "read" the translation unit.
4889   if (DeserializationListener)
4890     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4891                                       Context.getTranslationUnitDecl());
4892 
4893   // FIXME: Find a better way to deal with collisions between these
4894   // built-in types. Right now, we just ignore the problem.
4895 
4896   // Load the special types.
4897   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4898     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4899       if (!Context.CFConstantStringTypeDecl)
4900         Context.setCFConstantStringType(GetType(String));
4901     }
4902 
4903     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4904       QualType FileType = GetType(File);
4905       if (FileType.isNull()) {
4906         Error("FILE type is NULL");
4907         return;
4908       }
4909 
4910       if (!Context.FILEDecl) {
4911         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4912           Context.setFILEDecl(Typedef->getDecl());
4913         else {
4914           const TagType *Tag = FileType->getAs<TagType>();
4915           if (!Tag) {
4916             Error("Invalid FILE type in AST file");
4917             return;
4918           }
4919           Context.setFILEDecl(Tag->getDecl());
4920         }
4921       }
4922     }
4923 
4924     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4925       QualType Jmp_bufType = GetType(Jmp_buf);
4926       if (Jmp_bufType.isNull()) {
4927         Error("jmp_buf type is NULL");
4928         return;
4929       }
4930 
4931       if (!Context.jmp_bufDecl) {
4932         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4933           Context.setjmp_bufDecl(Typedef->getDecl());
4934         else {
4935           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4936           if (!Tag) {
4937             Error("Invalid jmp_buf type in AST file");
4938             return;
4939           }
4940           Context.setjmp_bufDecl(Tag->getDecl());
4941         }
4942       }
4943     }
4944 
4945     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4946       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4947       if (Sigjmp_bufType.isNull()) {
4948         Error("sigjmp_buf type is NULL");
4949         return;
4950       }
4951 
4952       if (!Context.sigjmp_bufDecl) {
4953         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4954           Context.setsigjmp_bufDecl(Typedef->getDecl());
4955         else {
4956           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4957           assert(Tag && "Invalid sigjmp_buf type in AST file");
4958           Context.setsigjmp_bufDecl(Tag->getDecl());
4959         }
4960       }
4961     }
4962 
4963     if (unsigned ObjCIdRedef
4964           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4965       if (Context.ObjCIdRedefinitionType.isNull())
4966         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4967     }
4968 
4969     if (unsigned ObjCClassRedef
4970           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4971       if (Context.ObjCClassRedefinitionType.isNull())
4972         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4973     }
4974 
4975     if (unsigned ObjCSelRedef
4976           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4977       if (Context.ObjCSelRedefinitionType.isNull())
4978         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4979     }
4980 
4981     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4982       QualType Ucontext_tType = GetType(Ucontext_t);
4983       if (Ucontext_tType.isNull()) {
4984         Error("ucontext_t type is NULL");
4985         return;
4986       }
4987 
4988       if (!Context.ucontext_tDecl) {
4989         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4990           Context.setucontext_tDecl(Typedef->getDecl());
4991         else {
4992           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4993           assert(Tag && "Invalid ucontext_t type in AST file");
4994           Context.setucontext_tDecl(Tag->getDecl());
4995         }
4996       }
4997     }
4998   }
4999 
5000   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5001 
5002   // If there were any CUDA special declarations, deserialize them.
5003   if (!CUDASpecialDeclRefs.empty()) {
5004     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5005     Context.setcudaConfigureCallDecl(
5006                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5007   }
5008 
5009   // Re-export any modules that were imported by a non-module AST file.
5010   // FIXME: This does not make macro-only imports visible again.
5011   for (auto &Import : ImportedModules) {
5012     if (Module *Imported = getSubmodule(Import.ID)) {
5013       makeModuleVisible(Imported, Module::AllVisible,
5014                         /*ImportLoc=*/Import.ImportLoc);
5015       if (Import.ImportLoc.isValid())
5016         PP.makeModuleVisible(Imported, Import.ImportLoc);
5017       // This updates visibility for Preprocessor only. For Sema, which can be
5018       // nullptr here, we do the same later, in UpdateSema().
5019     }
5020   }
5021 }
5022 
5023 void ASTReader::finalizeForWriting() {
5024   // Nothing to do for now.
5025 }
5026 
5027 /// Reads and return the signature record from \p PCH's control block, or
5028 /// else returns 0.
5029 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5030   BitstreamCursor Stream(PCH);
5031   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5032     // FIXME this drops the error on the floor.
5033     consumeError(std::move(Err));
5034     return ASTFileSignature();
5035   }
5036 
5037   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5038   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5039     return ASTFileSignature();
5040 
5041   // Scan for SIGNATURE inside the diagnostic options block.
5042   ASTReader::RecordData Record;
5043   while (true) {
5044     Expected<llvm::BitstreamEntry> MaybeEntry =
5045         Stream.advanceSkippingSubblocks();
5046     if (!MaybeEntry) {
5047       // FIXME this drops the error on the floor.
5048       consumeError(MaybeEntry.takeError());
5049       return ASTFileSignature();
5050     }
5051     llvm::BitstreamEntry Entry = MaybeEntry.get();
5052 
5053     if (Entry.Kind != llvm::BitstreamEntry::Record)
5054       return ASTFileSignature();
5055 
5056     Record.clear();
5057     StringRef Blob;
5058     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5059     if (!MaybeRecord) {
5060       // FIXME this drops the error on the floor.
5061       consumeError(MaybeRecord.takeError());
5062       return ASTFileSignature();
5063     }
5064     if (SIGNATURE == MaybeRecord.get())
5065       return ASTFileSignature::create(Record.begin(),
5066                                       Record.begin() + ASTFileSignature::size);
5067   }
5068 }
5069 
5070 /// Retrieve the name of the original source file name
5071 /// directly from the AST file, without actually loading the AST
5072 /// file.
5073 std::string ASTReader::getOriginalSourceFile(
5074     const std::string &ASTFileName, FileManager &FileMgr,
5075     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5076   // Open the AST file.
5077   auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5078                                          /*RequiresNullTerminator=*/false);
5079   if (!Buffer) {
5080     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5081         << ASTFileName << Buffer.getError().message();
5082     return std::string();
5083   }
5084 
5085   // Initialize the stream
5086   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5087 
5088   // Sniff for the signature.
5089   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5090     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5091     return std::string();
5092   }
5093 
5094   // Scan for the CONTROL_BLOCK_ID block.
5095   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5096     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5097     return std::string();
5098   }
5099 
5100   // Scan for ORIGINAL_FILE inside the control block.
5101   RecordData Record;
5102   while (true) {
5103     Expected<llvm::BitstreamEntry> MaybeEntry =
5104         Stream.advanceSkippingSubblocks();
5105     if (!MaybeEntry) {
5106       // FIXME this drops errors on the floor.
5107       consumeError(MaybeEntry.takeError());
5108       return std::string();
5109     }
5110     llvm::BitstreamEntry Entry = MaybeEntry.get();
5111 
5112     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5113       return std::string();
5114 
5115     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5116       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5117       return std::string();
5118     }
5119 
5120     Record.clear();
5121     StringRef Blob;
5122     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5123     if (!MaybeRecord) {
5124       // FIXME this drops the errors on the floor.
5125       consumeError(MaybeRecord.takeError());
5126       return std::string();
5127     }
5128     if (ORIGINAL_FILE == MaybeRecord.get())
5129       return Blob.str();
5130   }
5131 }
5132 
5133 namespace {
5134 
5135   class SimplePCHValidator : public ASTReaderListener {
5136     const LangOptions &ExistingLangOpts;
5137     const TargetOptions &ExistingTargetOpts;
5138     const PreprocessorOptions &ExistingPPOpts;
5139     std::string ExistingModuleCachePath;
5140     FileManager &FileMgr;
5141 
5142   public:
5143     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5144                        const TargetOptions &ExistingTargetOpts,
5145                        const PreprocessorOptions &ExistingPPOpts,
5146                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5147         : ExistingLangOpts(ExistingLangOpts),
5148           ExistingTargetOpts(ExistingTargetOpts),
5149           ExistingPPOpts(ExistingPPOpts),
5150           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5151 
5152     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5153                              bool AllowCompatibleDifferences) override {
5154       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5155                                   AllowCompatibleDifferences);
5156     }
5157 
5158     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5159                            bool AllowCompatibleDifferences) override {
5160       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5161                                 AllowCompatibleDifferences);
5162     }
5163 
5164     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5165                                  StringRef SpecificModuleCachePath,
5166                                  bool Complain) override {
5167       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5168                                       ExistingModuleCachePath, nullptr,
5169                                       ExistingLangOpts, ExistingPPOpts);
5170     }
5171 
5172     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5173                                  bool Complain,
5174                                  std::string &SuggestedPredefines) override {
5175       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5176                                       SuggestedPredefines, ExistingLangOpts);
5177     }
5178   };
5179 
5180 } // namespace
5181 
5182 bool ASTReader::readASTFileControlBlock(
5183     StringRef Filename, FileManager &FileMgr,
5184     const PCHContainerReader &PCHContainerRdr,
5185     bool FindModuleFileExtensions,
5186     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5187   // Open the AST file.
5188   // FIXME: This allows use of the VFS; we do not allow use of the
5189   // VFS when actually loading a module.
5190   auto Buffer = FileMgr.getBufferForFile(Filename);
5191   if (!Buffer) {
5192     return true;
5193   }
5194 
5195   // Initialize the stream
5196   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5197   BitstreamCursor Stream(Bytes);
5198 
5199   // Sniff for the signature.
5200   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5201     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5202     return true;
5203   }
5204 
5205   // Scan for the CONTROL_BLOCK_ID block.
5206   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5207     return true;
5208 
5209   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5210   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5211   bool NeedsImports = Listener.needsImportVisitation();
5212   BitstreamCursor InputFilesCursor;
5213 
5214   RecordData Record;
5215   std::string ModuleDir;
5216   bool DoneWithControlBlock = false;
5217   while (!DoneWithControlBlock) {
5218     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5219     if (!MaybeEntry) {
5220       // FIXME this drops the error on the floor.
5221       consumeError(MaybeEntry.takeError());
5222       return true;
5223     }
5224     llvm::BitstreamEntry Entry = MaybeEntry.get();
5225 
5226     switch (Entry.Kind) {
5227     case llvm::BitstreamEntry::SubBlock: {
5228       switch (Entry.ID) {
5229       case OPTIONS_BLOCK_ID: {
5230         std::string IgnoredSuggestedPredefines;
5231         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5232                              /*AllowCompatibleConfigurationMismatch*/ false,
5233                              Listener, IgnoredSuggestedPredefines) != Success)
5234           return true;
5235         break;
5236       }
5237 
5238       case INPUT_FILES_BLOCK_ID:
5239         InputFilesCursor = Stream;
5240         if (llvm::Error Err = Stream.SkipBlock()) {
5241           // FIXME this drops the error on the floor.
5242           consumeError(std::move(Err));
5243           return true;
5244         }
5245         if (NeedsInputFiles &&
5246             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5247           return true;
5248         break;
5249 
5250       default:
5251         if (llvm::Error Err = Stream.SkipBlock()) {
5252           // FIXME this drops the error on the floor.
5253           consumeError(std::move(Err));
5254           return true;
5255         }
5256         break;
5257       }
5258 
5259       continue;
5260     }
5261 
5262     case llvm::BitstreamEntry::EndBlock:
5263       DoneWithControlBlock = true;
5264       break;
5265 
5266     case llvm::BitstreamEntry::Error:
5267       return true;
5268 
5269     case llvm::BitstreamEntry::Record:
5270       break;
5271     }
5272 
5273     if (DoneWithControlBlock) break;
5274 
5275     Record.clear();
5276     StringRef Blob;
5277     Expected<unsigned> MaybeRecCode =
5278         Stream.readRecord(Entry.ID, Record, &Blob);
5279     if (!MaybeRecCode) {
5280       // FIXME this drops the error.
5281       return Failure;
5282     }
5283     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5284     case METADATA:
5285       if (Record[0] != VERSION_MAJOR)
5286         return true;
5287       if (Listener.ReadFullVersionInformation(Blob))
5288         return true;
5289       break;
5290     case MODULE_NAME:
5291       Listener.ReadModuleName(Blob);
5292       break;
5293     case MODULE_DIRECTORY:
5294       ModuleDir = std::string(Blob);
5295       break;
5296     case MODULE_MAP_FILE: {
5297       unsigned Idx = 0;
5298       auto Path = ReadString(Record, Idx);
5299       ResolveImportedPath(Path, ModuleDir);
5300       Listener.ReadModuleMapFile(Path);
5301       break;
5302     }
5303     case INPUT_FILE_OFFSETS: {
5304       if (!NeedsInputFiles)
5305         break;
5306 
5307       unsigned NumInputFiles = Record[0];
5308       unsigned NumUserFiles = Record[1];
5309       const llvm::support::unaligned_uint64_t *InputFileOffs =
5310           (const llvm::support::unaligned_uint64_t *)Blob.data();
5311       for (unsigned I = 0; I != NumInputFiles; ++I) {
5312         // Go find this input file.
5313         bool isSystemFile = I >= NumUserFiles;
5314 
5315         if (isSystemFile && !NeedsSystemInputFiles)
5316           break; // the rest are system input files
5317 
5318         BitstreamCursor &Cursor = InputFilesCursor;
5319         SavedStreamPosition SavedPosition(Cursor);
5320         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5321           // FIXME this drops errors on the floor.
5322           consumeError(std::move(Err));
5323         }
5324 
5325         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5326         if (!MaybeCode) {
5327           // FIXME this drops errors on the floor.
5328           consumeError(MaybeCode.takeError());
5329         }
5330         unsigned Code = MaybeCode.get();
5331 
5332         RecordData Record;
5333         StringRef Blob;
5334         bool shouldContinue = false;
5335         Expected<unsigned> MaybeRecordType =
5336             Cursor.readRecord(Code, Record, &Blob);
5337         if (!MaybeRecordType) {
5338           // FIXME this drops errors on the floor.
5339           consumeError(MaybeRecordType.takeError());
5340         }
5341         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5342         case INPUT_FILE_HASH:
5343           break;
5344         case INPUT_FILE:
5345           bool Overridden = static_cast<bool>(Record[3]);
5346           std::string Filename = std::string(Blob);
5347           ResolveImportedPath(Filename, ModuleDir);
5348           shouldContinue = Listener.visitInputFile(
5349               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5350           break;
5351         }
5352         if (!shouldContinue)
5353           break;
5354       }
5355       break;
5356     }
5357 
5358     case IMPORTS: {
5359       if (!NeedsImports)
5360         break;
5361 
5362       unsigned Idx = 0, N = Record.size();
5363       while (Idx < N) {
5364         // Read information about the AST file.
5365         Idx +=
5366             1 + 1 + 1 + 1 +
5367             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5368         std::string ModuleName = ReadString(Record, Idx);
5369         std::string Filename = ReadString(Record, Idx);
5370         ResolveImportedPath(Filename, ModuleDir);
5371         Listener.visitImport(ModuleName, Filename);
5372       }
5373       break;
5374     }
5375 
5376     default:
5377       // No other validation to perform.
5378       break;
5379     }
5380   }
5381 
5382   // Look for module file extension blocks, if requested.
5383   if (FindModuleFileExtensions) {
5384     BitstreamCursor SavedStream = Stream;
5385     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5386       bool DoneWithExtensionBlock = false;
5387       while (!DoneWithExtensionBlock) {
5388         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5389         if (!MaybeEntry) {
5390           // FIXME this drops the error.
5391           return true;
5392         }
5393         llvm::BitstreamEntry Entry = MaybeEntry.get();
5394 
5395         switch (Entry.Kind) {
5396         case llvm::BitstreamEntry::SubBlock:
5397           if (llvm::Error Err = Stream.SkipBlock()) {
5398             // FIXME this drops the error on the floor.
5399             consumeError(std::move(Err));
5400             return true;
5401           }
5402           continue;
5403 
5404         case llvm::BitstreamEntry::EndBlock:
5405           DoneWithExtensionBlock = true;
5406           continue;
5407 
5408         case llvm::BitstreamEntry::Error:
5409           return true;
5410 
5411         case llvm::BitstreamEntry::Record:
5412           break;
5413         }
5414 
5415        Record.clear();
5416        StringRef Blob;
5417        Expected<unsigned> MaybeRecCode =
5418            Stream.readRecord(Entry.ID, Record, &Blob);
5419        if (!MaybeRecCode) {
5420          // FIXME this drops the error.
5421          return true;
5422        }
5423        switch (MaybeRecCode.get()) {
5424        case EXTENSION_METADATA: {
5425          ModuleFileExtensionMetadata Metadata;
5426          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5427            return true;
5428 
5429          Listener.readModuleFileExtension(Metadata);
5430          break;
5431        }
5432        }
5433       }
5434     }
5435     Stream = SavedStream;
5436   }
5437 
5438   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5439   if (readUnhashedControlBlockImpl(
5440           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5441           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5442           ValidateDiagnosticOptions) != Success)
5443     return true;
5444 
5445   return false;
5446 }
5447 
5448 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5449                                     const PCHContainerReader &PCHContainerRdr,
5450                                     const LangOptions &LangOpts,
5451                                     const TargetOptions &TargetOpts,
5452                                     const PreprocessorOptions &PPOpts,
5453                                     StringRef ExistingModuleCachePath) {
5454   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5455                                ExistingModuleCachePath, FileMgr);
5456   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5457                                   /*FindModuleFileExtensions=*/false,
5458                                   validator,
5459                                   /*ValidateDiagnosticOptions=*/true);
5460 }
5461 
5462 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5463                                           unsigned ClientLoadCapabilities) {
5464   // Enter the submodule block.
5465   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5466     return Err;
5467 
5468   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5469   bool First = true;
5470   Module *CurrentModule = nullptr;
5471   RecordData Record;
5472   while (true) {
5473     Expected<llvm::BitstreamEntry> MaybeEntry =
5474         F.Stream.advanceSkippingSubblocks();
5475     if (!MaybeEntry)
5476       return MaybeEntry.takeError();
5477     llvm::BitstreamEntry Entry = MaybeEntry.get();
5478 
5479     switch (Entry.Kind) {
5480     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5481     case llvm::BitstreamEntry::Error:
5482       return llvm::createStringError(std::errc::illegal_byte_sequence,
5483                                      "malformed block record in AST file");
5484     case llvm::BitstreamEntry::EndBlock:
5485       return llvm::Error::success();
5486     case llvm::BitstreamEntry::Record:
5487       // The interesting case.
5488       break;
5489     }
5490 
5491     // Read a record.
5492     StringRef Blob;
5493     Record.clear();
5494     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5495     if (!MaybeKind)
5496       return MaybeKind.takeError();
5497     unsigned Kind = MaybeKind.get();
5498 
5499     if ((Kind == SUBMODULE_METADATA) != First)
5500       return llvm::createStringError(
5501           std::errc::illegal_byte_sequence,
5502           "submodule metadata record should be at beginning of block");
5503     First = false;
5504 
5505     // Submodule information is only valid if we have a current module.
5506     // FIXME: Should we error on these cases?
5507     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5508         Kind != SUBMODULE_DEFINITION)
5509       continue;
5510 
5511     switch (Kind) {
5512     default:  // Default behavior: ignore.
5513       break;
5514 
5515     case SUBMODULE_DEFINITION: {
5516       if (Record.size() < 12)
5517         return llvm::createStringError(std::errc::illegal_byte_sequence,
5518                                        "malformed module definition");
5519 
5520       StringRef Name = Blob;
5521       unsigned Idx = 0;
5522       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5523       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5524       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5525       bool IsFramework = Record[Idx++];
5526       bool IsExplicit = Record[Idx++];
5527       bool IsSystem = Record[Idx++];
5528       bool IsExternC = Record[Idx++];
5529       bool InferSubmodules = Record[Idx++];
5530       bool InferExplicitSubmodules = Record[Idx++];
5531       bool InferExportWildcard = Record[Idx++];
5532       bool ConfigMacrosExhaustive = Record[Idx++];
5533       bool ModuleMapIsPrivate = Record[Idx++];
5534 
5535       Module *ParentModule = nullptr;
5536       if (Parent)
5537         ParentModule = getSubmodule(Parent);
5538 
5539       // Retrieve this (sub)module from the module map, creating it if
5540       // necessary.
5541       CurrentModule =
5542           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5543               .first;
5544 
5545       // FIXME: set the definition loc for CurrentModule, or call
5546       // ModMap.setInferredModuleAllowedBy()
5547 
5548       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5549       if (GlobalIndex >= SubmodulesLoaded.size() ||
5550           SubmodulesLoaded[GlobalIndex])
5551         return llvm::createStringError(std::errc::invalid_argument,
5552                                        "too many submodules");
5553 
5554       if (!ParentModule) {
5555         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5556           // Don't emit module relocation error if we have -fno-validate-pch
5557           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5558                     DisableValidationForModuleKind::Module) &&
5559               CurFile != F.File) {
5560             auto ConflictError =
5561                 PartialDiagnostic(diag::err_module_file_conflict,
5562                                   ContextObj->DiagAllocator)
5563                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5564                 << F.File->getName();
5565             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5566           }
5567         }
5568 
5569         F.DidReadTopLevelSubmodule = true;
5570         CurrentModule->setASTFile(F.File);
5571         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5572       }
5573 
5574       CurrentModule->Kind = Kind;
5575       CurrentModule->Signature = F.Signature;
5576       CurrentModule->IsFromModuleFile = true;
5577       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5578       CurrentModule->IsExternC = IsExternC;
5579       CurrentModule->InferSubmodules = InferSubmodules;
5580       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5581       CurrentModule->InferExportWildcard = InferExportWildcard;
5582       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5583       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5584       if (DeserializationListener)
5585         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5586 
5587       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5588 
5589       // Clear out data that will be replaced by what is in the module file.
5590       CurrentModule->LinkLibraries.clear();
5591       CurrentModule->ConfigMacros.clear();
5592       CurrentModule->UnresolvedConflicts.clear();
5593       CurrentModule->Conflicts.clear();
5594 
5595       // The module is available unless it's missing a requirement; relevant
5596       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5597       // Missing headers that were present when the module was built do not
5598       // make it unavailable -- if we got this far, this must be an explicitly
5599       // imported module file.
5600       CurrentModule->Requirements.clear();
5601       CurrentModule->MissingHeaders.clear();
5602       CurrentModule->IsUnimportable =
5603           ParentModule && ParentModule->IsUnimportable;
5604       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5605       break;
5606     }
5607 
5608     case SUBMODULE_UMBRELLA_HEADER: {
5609       // FIXME: This doesn't work for framework modules as `Filename` is the
5610       //        name as written in the module file and does not include
5611       //        `Headers/`, so this path will never exist.
5612       std::string Filename = std::string(Blob);
5613       ResolveImportedPath(F, Filename);
5614       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5615         if (!CurrentModule->getUmbrellaHeader()) {
5616           // FIXME: NameAsWritten
5617           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5618         }
5619         // Note that it's too late at this point to return out of date if the
5620         // name from the PCM doesn't match up with the one in the module map,
5621         // but also quite unlikely since we will have already checked the
5622         // modification time and size of the module map file itself.
5623       }
5624       break;
5625     }
5626 
5627     case SUBMODULE_HEADER:
5628     case SUBMODULE_EXCLUDED_HEADER:
5629     case SUBMODULE_PRIVATE_HEADER:
5630       // We lazily associate headers with their modules via the HeaderInfo table.
5631       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5632       // of complete filenames or remove it entirely.
5633       break;
5634 
5635     case SUBMODULE_TEXTUAL_HEADER:
5636     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5637       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5638       // them here.
5639       break;
5640 
5641     case SUBMODULE_TOPHEADER: {
5642       std::string HeaderName(Blob);
5643       ResolveImportedPath(F, HeaderName);
5644       CurrentModule->addTopHeaderFilename(HeaderName);
5645       break;
5646     }
5647 
5648     case SUBMODULE_UMBRELLA_DIR: {
5649       // See comments in SUBMODULE_UMBRELLA_HEADER
5650       std::string Dirname = std::string(Blob);
5651       ResolveImportedPath(F, Dirname);
5652       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5653         if (!CurrentModule->getUmbrellaDir()) {
5654           // FIXME: NameAsWritten
5655           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5656         }
5657       }
5658       break;
5659     }
5660 
5661     case SUBMODULE_METADATA: {
5662       F.BaseSubmoduleID = getTotalNumSubmodules();
5663       F.LocalNumSubmodules = Record[0];
5664       unsigned LocalBaseSubmoduleID = Record[1];
5665       if (F.LocalNumSubmodules > 0) {
5666         // Introduce the global -> local mapping for submodules within this
5667         // module.
5668         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5669 
5670         // Introduce the local -> global mapping for submodules within this
5671         // module.
5672         F.SubmoduleRemap.insertOrReplace(
5673           std::make_pair(LocalBaseSubmoduleID,
5674                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5675 
5676         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5677       }
5678       break;
5679     }
5680 
5681     case SUBMODULE_IMPORTS:
5682       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5683         UnresolvedModuleRef Unresolved;
5684         Unresolved.File = &F;
5685         Unresolved.Mod = CurrentModule;
5686         Unresolved.ID = Record[Idx];
5687         Unresolved.Kind = UnresolvedModuleRef::Import;
5688         Unresolved.IsWildcard = false;
5689         UnresolvedModuleRefs.push_back(Unresolved);
5690       }
5691       break;
5692 
5693     case SUBMODULE_EXPORTS:
5694       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5695         UnresolvedModuleRef Unresolved;
5696         Unresolved.File = &F;
5697         Unresolved.Mod = CurrentModule;
5698         Unresolved.ID = Record[Idx];
5699         Unresolved.Kind = UnresolvedModuleRef::Export;
5700         Unresolved.IsWildcard = Record[Idx + 1];
5701         UnresolvedModuleRefs.push_back(Unresolved);
5702       }
5703 
5704       // Once we've loaded the set of exports, there's no reason to keep
5705       // the parsed, unresolved exports around.
5706       CurrentModule->UnresolvedExports.clear();
5707       break;
5708 
5709     case SUBMODULE_REQUIRES:
5710       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5711                                     PP.getTargetInfo());
5712       break;
5713 
5714     case SUBMODULE_LINK_LIBRARY:
5715       ModMap.resolveLinkAsDependencies(CurrentModule);
5716       CurrentModule->LinkLibraries.push_back(
5717           Module::LinkLibrary(std::string(Blob), Record[0]));
5718       break;
5719 
5720     case SUBMODULE_CONFIG_MACRO:
5721       CurrentModule->ConfigMacros.push_back(Blob.str());
5722       break;
5723 
5724     case SUBMODULE_CONFLICT: {
5725       UnresolvedModuleRef Unresolved;
5726       Unresolved.File = &F;
5727       Unresolved.Mod = CurrentModule;
5728       Unresolved.ID = Record[0];
5729       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5730       Unresolved.IsWildcard = false;
5731       Unresolved.String = Blob;
5732       UnresolvedModuleRefs.push_back(Unresolved);
5733       break;
5734     }
5735 
5736     case SUBMODULE_INITIALIZERS: {
5737       if (!ContextObj)
5738         break;
5739       SmallVector<uint32_t, 16> Inits;
5740       for (auto &ID : Record)
5741         Inits.push_back(getGlobalDeclID(F, ID));
5742       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5743       break;
5744     }
5745 
5746     case SUBMODULE_EXPORT_AS:
5747       CurrentModule->ExportAsModule = Blob.str();
5748       ModMap.addLinkAsDependency(CurrentModule);
5749       break;
5750     }
5751   }
5752 }
5753 
5754 /// Parse the record that corresponds to a LangOptions data
5755 /// structure.
5756 ///
5757 /// This routine parses the language options from the AST file and then gives
5758 /// them to the AST listener if one is set.
5759 ///
5760 /// \returns true if the listener deems the file unacceptable, false otherwise.
5761 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5762                                      bool Complain,
5763                                      ASTReaderListener &Listener,
5764                                      bool AllowCompatibleDifferences) {
5765   LangOptions LangOpts;
5766   unsigned Idx = 0;
5767 #define LANGOPT(Name, Bits, Default, Description) \
5768   LangOpts.Name = Record[Idx++];
5769 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5770   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5771 #include "clang/Basic/LangOptions.def"
5772 #define SANITIZER(NAME, ID)                                                    \
5773   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5774 #include "clang/Basic/Sanitizers.def"
5775 
5776   for (unsigned N = Record[Idx++]; N; --N)
5777     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5778 
5779   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5780   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5781   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5782 
5783   LangOpts.CurrentModule = ReadString(Record, Idx);
5784 
5785   // Comment options.
5786   for (unsigned N = Record[Idx++]; N; --N) {
5787     LangOpts.CommentOpts.BlockCommandNames.push_back(
5788       ReadString(Record, Idx));
5789   }
5790   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5791 
5792   // OpenMP offloading options.
5793   for (unsigned N = Record[Idx++]; N; --N) {
5794     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5795   }
5796 
5797   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5798 
5799   return Listener.ReadLanguageOptions(LangOpts, Complain,
5800                                       AllowCompatibleDifferences);
5801 }
5802 
5803 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5804                                    ASTReaderListener &Listener,
5805                                    bool AllowCompatibleDifferences) {
5806   unsigned Idx = 0;
5807   TargetOptions TargetOpts;
5808   TargetOpts.Triple = ReadString(Record, Idx);
5809   TargetOpts.CPU = ReadString(Record, Idx);
5810   TargetOpts.TuneCPU = ReadString(Record, Idx);
5811   TargetOpts.ABI = ReadString(Record, Idx);
5812   for (unsigned N = Record[Idx++]; N; --N) {
5813     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5814   }
5815   for (unsigned N = Record[Idx++]; N; --N) {
5816     TargetOpts.Features.push_back(ReadString(Record, Idx));
5817   }
5818 
5819   return Listener.ReadTargetOptions(TargetOpts, Complain,
5820                                     AllowCompatibleDifferences);
5821 }
5822 
5823 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5824                                        ASTReaderListener &Listener) {
5825   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5826   unsigned Idx = 0;
5827 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5828 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5829   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5830 #include "clang/Basic/DiagnosticOptions.def"
5831 
5832   for (unsigned N = Record[Idx++]; N; --N)
5833     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5834   for (unsigned N = Record[Idx++]; N; --N)
5835     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5836 
5837   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5838 }
5839 
5840 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5841                                        ASTReaderListener &Listener) {
5842   FileSystemOptions FSOpts;
5843   unsigned Idx = 0;
5844   FSOpts.WorkingDir = ReadString(Record, Idx);
5845   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5846 }
5847 
5848 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5849                                          bool Complain,
5850                                          ASTReaderListener &Listener) {
5851   HeaderSearchOptions HSOpts;
5852   unsigned Idx = 0;
5853   HSOpts.Sysroot = ReadString(Record, Idx);
5854 
5855   // Include entries.
5856   for (unsigned N = Record[Idx++]; N; --N) {
5857     std::string Path = ReadString(Record, Idx);
5858     frontend::IncludeDirGroup Group
5859       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5860     bool IsFramework = Record[Idx++];
5861     bool IgnoreSysRoot = Record[Idx++];
5862     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5863                                     IgnoreSysRoot);
5864   }
5865 
5866   // System header prefixes.
5867   for (unsigned N = Record[Idx++]; N; --N) {
5868     std::string Prefix = ReadString(Record, Idx);
5869     bool IsSystemHeader = Record[Idx++];
5870     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5871   }
5872 
5873   HSOpts.ResourceDir = ReadString(Record, Idx);
5874   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5875   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5876   HSOpts.DisableModuleHash = Record[Idx++];
5877   HSOpts.ImplicitModuleMaps = Record[Idx++];
5878   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5879   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5880   HSOpts.UseBuiltinIncludes = Record[Idx++];
5881   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5882   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5883   HSOpts.UseLibcxx = Record[Idx++];
5884   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5885 
5886   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5887                                           Complain);
5888 }
5889 
5890 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5891                                          bool Complain,
5892                                          ASTReaderListener &Listener,
5893                                          std::string &SuggestedPredefines) {
5894   PreprocessorOptions PPOpts;
5895   unsigned Idx = 0;
5896 
5897   // Macro definitions/undefs
5898   for (unsigned N = Record[Idx++]; N; --N) {
5899     std::string Macro = ReadString(Record, Idx);
5900     bool IsUndef = Record[Idx++];
5901     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5902   }
5903 
5904   // Includes
5905   for (unsigned N = Record[Idx++]; N; --N) {
5906     PPOpts.Includes.push_back(ReadString(Record, Idx));
5907   }
5908 
5909   // Macro Includes
5910   for (unsigned N = Record[Idx++]; N; --N) {
5911     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5912   }
5913 
5914   PPOpts.UsePredefines = Record[Idx++];
5915   PPOpts.DetailedRecord = Record[Idx++];
5916   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5917   PPOpts.ObjCXXARCStandardLibrary =
5918     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5919   SuggestedPredefines.clear();
5920   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5921                                           SuggestedPredefines);
5922 }
5923 
5924 std::pair<ModuleFile *, unsigned>
5925 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5926   GlobalPreprocessedEntityMapType::iterator
5927   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5928   assert(I != GlobalPreprocessedEntityMap.end() &&
5929          "Corrupted global preprocessed entity map");
5930   ModuleFile *M = I->second;
5931   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5932   return std::make_pair(M, LocalIndex);
5933 }
5934 
5935 llvm::iterator_range<PreprocessingRecord::iterator>
5936 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5937   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5938     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5939                                              Mod.NumPreprocessedEntities);
5940 
5941   return llvm::make_range(PreprocessingRecord::iterator(),
5942                           PreprocessingRecord::iterator());
5943 }
5944 
5945 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5946                                         unsigned int ClientLoadCapabilities) {
5947   return ClientLoadCapabilities & ARR_OutOfDate &&
5948          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5949 }
5950 
5951 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5952 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5953   return llvm::make_range(
5954       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5955       ModuleDeclIterator(this, &Mod,
5956                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5957 }
5958 
5959 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5960   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5961   assert(I != GlobalSkippedRangeMap.end() &&
5962     "Corrupted global skipped range map");
5963   ModuleFile *M = I->second;
5964   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5965   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5966   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5967   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5968                     TranslateSourceLocation(*M, RawRange.getEnd()));
5969   assert(Range.isValid());
5970   return Range;
5971 }
5972 
5973 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5974   PreprocessedEntityID PPID = Index+1;
5975   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5976   ModuleFile &M = *PPInfo.first;
5977   unsigned LocalIndex = PPInfo.second;
5978   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5979 
5980   if (!PP.getPreprocessingRecord()) {
5981     Error("no preprocessing record");
5982     return nullptr;
5983   }
5984 
5985   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5986   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5987           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5988     Error(std::move(Err));
5989     return nullptr;
5990   }
5991 
5992   Expected<llvm::BitstreamEntry> MaybeEntry =
5993       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5994   if (!MaybeEntry) {
5995     Error(MaybeEntry.takeError());
5996     return nullptr;
5997   }
5998   llvm::BitstreamEntry Entry = MaybeEntry.get();
5999 
6000   if (Entry.Kind != llvm::BitstreamEntry::Record)
6001     return nullptr;
6002 
6003   // Read the record.
6004   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
6005                     TranslateSourceLocation(M, PPOffs.getEnd()));
6006   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6007   StringRef Blob;
6008   RecordData Record;
6009   Expected<unsigned> MaybeRecType =
6010       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
6011   if (!MaybeRecType) {
6012     Error(MaybeRecType.takeError());
6013     return nullptr;
6014   }
6015   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6016   case PPD_MACRO_EXPANSION: {
6017     bool isBuiltin = Record[0];
6018     IdentifierInfo *Name = nullptr;
6019     MacroDefinitionRecord *Def = nullptr;
6020     if (isBuiltin)
6021       Name = getLocalIdentifier(M, Record[1]);
6022     else {
6023       PreprocessedEntityID GlobalID =
6024           getGlobalPreprocessedEntityID(M, Record[1]);
6025       Def = cast<MacroDefinitionRecord>(
6026           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6027     }
6028 
6029     MacroExpansion *ME;
6030     if (isBuiltin)
6031       ME = new (PPRec) MacroExpansion(Name, Range);
6032     else
6033       ME = new (PPRec) MacroExpansion(Def, Range);
6034 
6035     return ME;
6036   }
6037 
6038   case PPD_MACRO_DEFINITION: {
6039     // Decode the identifier info and then check again; if the macro is
6040     // still defined and associated with the identifier,
6041     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6042     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6043 
6044     if (DeserializationListener)
6045       DeserializationListener->MacroDefinitionRead(PPID, MD);
6046 
6047     return MD;
6048   }
6049 
6050   case PPD_INCLUSION_DIRECTIVE: {
6051     const char *FullFileNameStart = Blob.data() + Record[0];
6052     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6053     Optional<FileEntryRef> File;
6054     if (!FullFileName.empty())
6055       File = PP.getFileManager().getOptionalFileRef(FullFileName);
6056 
6057     // FIXME: Stable encoding
6058     InclusionDirective::InclusionKind Kind
6059       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6060     InclusionDirective *ID
6061       = new (PPRec) InclusionDirective(PPRec, Kind,
6062                                        StringRef(Blob.data(), Record[0]),
6063                                        Record[1], Record[3],
6064                                        File,
6065                                        Range);
6066     return ID;
6067   }
6068   }
6069 
6070   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6071 }
6072 
6073 /// Find the next module that contains entities and return the ID
6074 /// of the first entry.
6075 ///
6076 /// \param SLocMapI points at a chunk of a module that contains no
6077 /// preprocessed entities or the entities it contains are not the ones we are
6078 /// looking for.
6079 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6080                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6081   ++SLocMapI;
6082   for (GlobalSLocOffsetMapType::const_iterator
6083          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6084     ModuleFile &M = *SLocMapI->second;
6085     if (M.NumPreprocessedEntities)
6086       return M.BasePreprocessedEntityID;
6087   }
6088 
6089   return getTotalNumPreprocessedEntities();
6090 }
6091 
6092 namespace {
6093 
6094 struct PPEntityComp {
6095   const ASTReader &Reader;
6096   ModuleFile &M;
6097 
6098   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6099 
6100   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6101     SourceLocation LHS = getLoc(L);
6102     SourceLocation RHS = getLoc(R);
6103     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6104   }
6105 
6106   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6107     SourceLocation LHS = getLoc(L);
6108     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6109   }
6110 
6111   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6112     SourceLocation RHS = getLoc(R);
6113     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6114   }
6115 
6116   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6117     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6118   }
6119 };
6120 
6121 } // namespace
6122 
6123 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6124                                                        bool EndsAfter) const {
6125   if (SourceMgr.isLocalSourceLocation(Loc))
6126     return getTotalNumPreprocessedEntities();
6127 
6128   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6129       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6130   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6131          "Corrupted global sloc offset map");
6132 
6133   if (SLocMapI->second->NumPreprocessedEntities == 0)
6134     return findNextPreprocessedEntity(SLocMapI);
6135 
6136   ModuleFile &M = *SLocMapI->second;
6137 
6138   using pp_iterator = const PPEntityOffset *;
6139 
6140   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6141   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6142 
6143   size_t Count = M.NumPreprocessedEntities;
6144   size_t Half;
6145   pp_iterator First = pp_begin;
6146   pp_iterator PPI;
6147 
6148   if (EndsAfter) {
6149     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6150                            PPEntityComp(*this, M));
6151   } else {
6152     // Do a binary search manually instead of using std::lower_bound because
6153     // The end locations of entities may be unordered (when a macro expansion
6154     // is inside another macro argument), but for this case it is not important
6155     // whether we get the first macro expansion or its containing macro.
6156     while (Count > 0) {
6157       Half = Count / 2;
6158       PPI = First;
6159       std::advance(PPI, Half);
6160       if (SourceMgr.isBeforeInTranslationUnit(
6161               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6162         First = PPI;
6163         ++First;
6164         Count = Count - Half - 1;
6165       } else
6166         Count = Half;
6167     }
6168   }
6169 
6170   if (PPI == pp_end)
6171     return findNextPreprocessedEntity(SLocMapI);
6172 
6173   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6174 }
6175 
6176 /// Returns a pair of [Begin, End) indices of preallocated
6177 /// preprocessed entities that \arg Range encompasses.
6178 std::pair<unsigned, unsigned>
6179     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6180   if (Range.isInvalid())
6181     return std::make_pair(0,0);
6182   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6183 
6184   PreprocessedEntityID BeginID =
6185       findPreprocessedEntity(Range.getBegin(), false);
6186   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6187   return std::make_pair(BeginID, EndID);
6188 }
6189 
6190 /// Optionally returns true or false if the preallocated preprocessed
6191 /// entity with index \arg Index came from file \arg FID.
6192 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6193                                                              FileID FID) {
6194   if (FID.isInvalid())
6195     return false;
6196 
6197   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6198   ModuleFile &M = *PPInfo.first;
6199   unsigned LocalIndex = PPInfo.second;
6200   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6201 
6202   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6203   if (Loc.isInvalid())
6204     return false;
6205 
6206   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6207     return true;
6208   else
6209     return false;
6210 }
6211 
6212 namespace {
6213 
6214   /// Visitor used to search for information about a header file.
6215   class HeaderFileInfoVisitor {
6216     const FileEntry *FE;
6217     Optional<HeaderFileInfo> HFI;
6218 
6219   public:
6220     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6221 
6222     bool operator()(ModuleFile &M) {
6223       HeaderFileInfoLookupTable *Table
6224         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6225       if (!Table)
6226         return false;
6227 
6228       // Look in the on-disk hash table for an entry for this file name.
6229       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6230       if (Pos == Table->end())
6231         return false;
6232 
6233       HFI = *Pos;
6234       return true;
6235     }
6236 
6237     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6238   };
6239 
6240 } // namespace
6241 
6242 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6243   HeaderFileInfoVisitor Visitor(FE);
6244   ModuleMgr.visit(Visitor);
6245   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6246     return *HFI;
6247 
6248   return HeaderFileInfo();
6249 }
6250 
6251 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6252   using DiagState = DiagnosticsEngine::DiagState;
6253   SmallVector<DiagState *, 32> DiagStates;
6254 
6255   for (ModuleFile &F : ModuleMgr) {
6256     unsigned Idx = 0;
6257     auto &Record = F.PragmaDiagMappings;
6258     if (Record.empty())
6259       continue;
6260 
6261     DiagStates.clear();
6262 
6263     auto ReadDiagState =
6264         [&](const DiagState &BasedOn, SourceLocation Loc,
6265             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6266       unsigned BackrefID = Record[Idx++];
6267       if (BackrefID != 0)
6268         return DiagStates[BackrefID - 1];
6269 
6270       // A new DiagState was created here.
6271       Diag.DiagStates.push_back(BasedOn);
6272       DiagState *NewState = &Diag.DiagStates.back();
6273       DiagStates.push_back(NewState);
6274       unsigned Size = Record[Idx++];
6275       assert(Idx + Size * 2 <= Record.size() &&
6276              "Invalid data, not enough diag/map pairs");
6277       while (Size--) {
6278         unsigned DiagID = Record[Idx++];
6279         DiagnosticMapping NewMapping =
6280             DiagnosticMapping::deserialize(Record[Idx++]);
6281         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6282           continue;
6283 
6284         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6285 
6286         // If this mapping was specified as a warning but the severity was
6287         // upgraded due to diagnostic settings, simulate the current diagnostic
6288         // settings (and use a warning).
6289         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6290           NewMapping.setSeverity(diag::Severity::Warning);
6291           NewMapping.setUpgradedFromWarning(false);
6292         }
6293 
6294         Mapping = NewMapping;
6295       }
6296       return NewState;
6297     };
6298 
6299     // Read the first state.
6300     DiagState *FirstState;
6301     if (F.Kind == MK_ImplicitModule) {
6302       // Implicitly-built modules are reused with different diagnostic
6303       // settings.  Use the initial diagnostic state from Diag to simulate this
6304       // compilation's diagnostic settings.
6305       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6306       DiagStates.push_back(FirstState);
6307 
6308       // Skip the initial diagnostic state from the serialized module.
6309       assert(Record[1] == 0 &&
6310              "Invalid data, unexpected backref in initial state");
6311       Idx = 3 + Record[2] * 2;
6312       assert(Idx < Record.size() &&
6313              "Invalid data, not enough state change pairs in initial state");
6314     } else if (F.isModule()) {
6315       // For an explicit module, preserve the flags from the module build
6316       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6317       // -Wblah flags.
6318       unsigned Flags = Record[Idx++];
6319       DiagState Initial;
6320       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6321       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6322       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6323       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6324       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6325       Initial.ExtBehavior = (diag::Severity)Flags;
6326       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6327 
6328       assert(F.OriginalSourceFileID.isValid());
6329 
6330       // Set up the root buffer of the module to start with the initial
6331       // diagnostic state of the module itself, to cover files that contain no
6332       // explicit transitions (for which we did not serialize anything).
6333       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6334           .StateTransitions.push_back({FirstState, 0});
6335     } else {
6336       // For prefix ASTs, start with whatever the user configured on the
6337       // command line.
6338       Idx++; // Skip flags.
6339       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6340                                  SourceLocation(), false);
6341     }
6342 
6343     // Read the state transitions.
6344     unsigned NumLocations = Record[Idx++];
6345     while (NumLocations--) {
6346       assert(Idx < Record.size() &&
6347              "Invalid data, missing pragma diagnostic states");
6348       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6349       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6350       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6351       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6352       unsigned Transitions = Record[Idx++];
6353 
6354       // Note that we don't need to set up Parent/ParentOffset here, because
6355       // we won't be changing the diagnostic state within imported FileIDs
6356       // (other than perhaps appending to the main source file, which has no
6357       // parent).
6358       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6359       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6360       for (unsigned I = 0; I != Transitions; ++I) {
6361         unsigned Offset = Record[Idx++];
6362         auto *State =
6363             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6364         F.StateTransitions.push_back({State, Offset});
6365       }
6366     }
6367 
6368     // Read the final state.
6369     assert(Idx < Record.size() &&
6370            "Invalid data, missing final pragma diagnostic state");
6371     SourceLocation CurStateLoc =
6372         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6373     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6374 
6375     if (!F.isModule()) {
6376       Diag.DiagStatesByLoc.CurDiagState = CurState;
6377       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6378 
6379       // Preserve the property that the imaginary root file describes the
6380       // current state.
6381       FileID NullFile;
6382       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6383       if (T.empty())
6384         T.push_back({CurState, 0});
6385       else
6386         T[0].State = CurState;
6387     }
6388 
6389     // Don't try to read these mappings again.
6390     Record.clear();
6391   }
6392 }
6393 
6394 /// Get the correct cursor and offset for loading a type.
6395 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6396   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6397   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6398   ModuleFile *M = I->second;
6399   return RecordLocation(
6400       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6401              M->DeclsBlockStartOffset);
6402 }
6403 
6404 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6405   switch (code) {
6406 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6407   case TYPE_##CODE_ID: return Type::CLASS_ID;
6408 #include "clang/Serialization/TypeBitCodes.def"
6409   default: return llvm::None;
6410   }
6411 }
6412 
6413 /// Read and return the type with the given index..
6414 ///
6415 /// The index is the type ID, shifted and minus the number of predefs. This
6416 /// routine actually reads the record corresponding to the type at the given
6417 /// location. It is a helper routine for GetType, which deals with reading type
6418 /// IDs.
6419 QualType ASTReader::readTypeRecord(unsigned Index) {
6420   assert(ContextObj && "reading type with no AST context");
6421   ASTContext &Context = *ContextObj;
6422   RecordLocation Loc = TypeCursorForIndex(Index);
6423   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6424 
6425   // Keep track of where we are in the stream, then jump back there
6426   // after reading this type.
6427   SavedStreamPosition SavedPosition(DeclsCursor);
6428 
6429   ReadingKindTracker ReadingKind(Read_Type, *this);
6430 
6431   // Note that we are loading a type record.
6432   Deserializing AType(this);
6433 
6434   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6435     Error(std::move(Err));
6436     return QualType();
6437   }
6438   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6439   if (!RawCode) {
6440     Error(RawCode.takeError());
6441     return QualType();
6442   }
6443 
6444   ASTRecordReader Record(*this, *Loc.F);
6445   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6446   if (!Code) {
6447     Error(Code.takeError());
6448     return QualType();
6449   }
6450   if (Code.get() == TYPE_EXT_QUAL) {
6451     QualType baseType = Record.readQualType();
6452     Qualifiers quals = Record.readQualifiers();
6453     return Context.getQualifiedType(baseType, quals);
6454   }
6455 
6456   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6457   if (!maybeClass) {
6458     Error("Unexpected code for type");
6459     return QualType();
6460   }
6461 
6462   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6463   return TypeReader.read(*maybeClass);
6464 }
6465 
6466 namespace clang {
6467 
6468 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6469   using LocSeq = SourceLocationSequence;
6470 
6471   ASTRecordReader &Reader;
6472   LocSeq *Seq;
6473 
6474   SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); }
6475   SourceRange readSourceRange() { return Reader.readSourceRange(Seq); }
6476 
6477   TypeSourceInfo *GetTypeSourceInfo() {
6478     return Reader.readTypeSourceInfo();
6479   }
6480 
6481   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6482     return Reader.readNestedNameSpecifierLoc();
6483   }
6484 
6485   Attr *ReadAttr() {
6486     return Reader.readAttr();
6487   }
6488 
6489 public:
6490   TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq)
6491       : Reader(Reader), Seq(Seq) {}
6492 
6493   // We want compile-time assurance that we've enumerated all of
6494   // these, so unfortunately we have to declare them first, then
6495   // define them out-of-line.
6496 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6497 #define TYPELOC(CLASS, PARENT) \
6498   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6499 #include "clang/AST/TypeLocNodes.def"
6500 
6501   void VisitFunctionTypeLoc(FunctionTypeLoc);
6502   void VisitArrayTypeLoc(ArrayTypeLoc);
6503 };
6504 
6505 } // namespace clang
6506 
6507 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6508   // nothing to do
6509 }
6510 
6511 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6512   TL.setBuiltinLoc(readSourceLocation());
6513   if (TL.needsExtraLocalData()) {
6514     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6515     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6516     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6517     TL.setModeAttr(Reader.readInt());
6518   }
6519 }
6520 
6521 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6522   TL.setNameLoc(readSourceLocation());
6523 }
6524 
6525 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6526   TL.setStarLoc(readSourceLocation());
6527 }
6528 
6529 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6530   // nothing to do
6531 }
6532 
6533 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6534   // nothing to do
6535 }
6536 
6537 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6538   TL.setExpansionLoc(readSourceLocation());
6539 }
6540 
6541 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6542   TL.setCaretLoc(readSourceLocation());
6543 }
6544 
6545 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6546   TL.setAmpLoc(readSourceLocation());
6547 }
6548 
6549 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6550   TL.setAmpAmpLoc(readSourceLocation());
6551 }
6552 
6553 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6554   TL.setStarLoc(readSourceLocation());
6555   TL.setClassTInfo(GetTypeSourceInfo());
6556 }
6557 
6558 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6559   TL.setLBracketLoc(readSourceLocation());
6560   TL.setRBracketLoc(readSourceLocation());
6561   if (Reader.readBool())
6562     TL.setSizeExpr(Reader.readExpr());
6563   else
6564     TL.setSizeExpr(nullptr);
6565 }
6566 
6567 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6568   VisitArrayTypeLoc(TL);
6569 }
6570 
6571 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6572   VisitArrayTypeLoc(TL);
6573 }
6574 
6575 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6576   VisitArrayTypeLoc(TL);
6577 }
6578 
6579 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6580                                             DependentSizedArrayTypeLoc TL) {
6581   VisitArrayTypeLoc(TL);
6582 }
6583 
6584 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6585     DependentAddressSpaceTypeLoc TL) {
6586 
6587     TL.setAttrNameLoc(readSourceLocation());
6588     TL.setAttrOperandParensRange(readSourceRange());
6589     TL.setAttrExprOperand(Reader.readExpr());
6590 }
6591 
6592 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6593                                         DependentSizedExtVectorTypeLoc TL) {
6594   TL.setNameLoc(readSourceLocation());
6595 }
6596 
6597 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6598   TL.setNameLoc(readSourceLocation());
6599 }
6600 
6601 void TypeLocReader::VisitDependentVectorTypeLoc(
6602     DependentVectorTypeLoc TL) {
6603   TL.setNameLoc(readSourceLocation());
6604 }
6605 
6606 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6607   TL.setNameLoc(readSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6611   TL.setAttrNameLoc(readSourceLocation());
6612   TL.setAttrOperandParensRange(readSourceRange());
6613   TL.setAttrRowOperand(Reader.readExpr());
6614   TL.setAttrColumnOperand(Reader.readExpr());
6615 }
6616 
6617 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6618     DependentSizedMatrixTypeLoc TL) {
6619   TL.setAttrNameLoc(readSourceLocation());
6620   TL.setAttrOperandParensRange(readSourceRange());
6621   TL.setAttrRowOperand(Reader.readExpr());
6622   TL.setAttrColumnOperand(Reader.readExpr());
6623 }
6624 
6625 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6626   TL.setLocalRangeBegin(readSourceLocation());
6627   TL.setLParenLoc(readSourceLocation());
6628   TL.setRParenLoc(readSourceLocation());
6629   TL.setExceptionSpecRange(readSourceRange());
6630   TL.setLocalRangeEnd(readSourceLocation());
6631   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6632     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6633   }
6634 }
6635 
6636 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6637   VisitFunctionTypeLoc(TL);
6638 }
6639 
6640 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6641   VisitFunctionTypeLoc(TL);
6642 }
6643 
6644 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6645   TL.setNameLoc(readSourceLocation());
6646 }
6647 
6648 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6649   TL.setNameLoc(readSourceLocation());
6650 }
6651 
6652 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6653   TL.setNameLoc(readSourceLocation());
6654 }
6655 
6656 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6657   TL.setTypeofLoc(readSourceLocation());
6658   TL.setLParenLoc(readSourceLocation());
6659   TL.setRParenLoc(readSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6663   TL.setTypeofLoc(readSourceLocation());
6664   TL.setLParenLoc(readSourceLocation());
6665   TL.setRParenLoc(readSourceLocation());
6666   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6667 }
6668 
6669 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6670   TL.setDecltypeLoc(readSourceLocation());
6671   TL.setRParenLoc(readSourceLocation());
6672 }
6673 
6674 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6675   TL.setKWLoc(readSourceLocation());
6676   TL.setLParenLoc(readSourceLocation());
6677   TL.setRParenLoc(readSourceLocation());
6678   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6679 }
6680 
6681 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6682   TL.setNameLoc(readSourceLocation());
6683   if (Reader.readBool()) {
6684     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6685     TL.setTemplateKWLoc(readSourceLocation());
6686     TL.setConceptNameLoc(readSourceLocation());
6687     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6688     TL.setLAngleLoc(readSourceLocation());
6689     TL.setRAngleLoc(readSourceLocation());
6690     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6691       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6692                               TL.getTypePtr()->getArg(i).getKind()));
6693   }
6694   if (Reader.readBool())
6695     TL.setRParenLoc(readSourceLocation());
6696 }
6697 
6698 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6699     DeducedTemplateSpecializationTypeLoc TL) {
6700   TL.setTemplateNameLoc(readSourceLocation());
6701 }
6702 
6703 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6704   TL.setNameLoc(readSourceLocation());
6705 }
6706 
6707 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6708   TL.setNameLoc(readSourceLocation());
6709 }
6710 
6711 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6712   TL.setAttr(ReadAttr());
6713 }
6714 
6715 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6716   // Nothing to do.
6717 }
6718 
6719 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6720   TL.setNameLoc(readSourceLocation());
6721 }
6722 
6723 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6724                                             SubstTemplateTypeParmTypeLoc TL) {
6725   TL.setNameLoc(readSourceLocation());
6726 }
6727 
6728 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6729                                           SubstTemplateTypeParmPackTypeLoc TL) {
6730   TL.setNameLoc(readSourceLocation());
6731 }
6732 
6733 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6734                                            TemplateSpecializationTypeLoc TL) {
6735   TL.setTemplateKeywordLoc(readSourceLocation());
6736   TL.setTemplateNameLoc(readSourceLocation());
6737   TL.setLAngleLoc(readSourceLocation());
6738   TL.setRAngleLoc(readSourceLocation());
6739   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6740     TL.setArgLocInfo(
6741         i,
6742         Reader.readTemplateArgumentLocInfo(
6743           TL.getTypePtr()->getArg(i).getKind()));
6744 }
6745 
6746 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6747   TL.setLParenLoc(readSourceLocation());
6748   TL.setRParenLoc(readSourceLocation());
6749 }
6750 
6751 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6752   TL.setElaboratedKeywordLoc(readSourceLocation());
6753   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6754 }
6755 
6756 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6757   TL.setNameLoc(readSourceLocation());
6758 }
6759 
6760 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6761   TL.setElaboratedKeywordLoc(readSourceLocation());
6762   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6763   TL.setNameLoc(readSourceLocation());
6764 }
6765 
6766 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6767        DependentTemplateSpecializationTypeLoc TL) {
6768   TL.setElaboratedKeywordLoc(readSourceLocation());
6769   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6770   TL.setTemplateKeywordLoc(readSourceLocation());
6771   TL.setTemplateNameLoc(readSourceLocation());
6772   TL.setLAngleLoc(readSourceLocation());
6773   TL.setRAngleLoc(readSourceLocation());
6774   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6775     TL.setArgLocInfo(
6776         I,
6777         Reader.readTemplateArgumentLocInfo(
6778             TL.getTypePtr()->getArg(I).getKind()));
6779 }
6780 
6781 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6782   TL.setEllipsisLoc(readSourceLocation());
6783 }
6784 
6785 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6786   TL.setNameLoc(readSourceLocation());
6787 }
6788 
6789 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6790   if (TL.getNumProtocols()) {
6791     TL.setProtocolLAngleLoc(readSourceLocation());
6792     TL.setProtocolRAngleLoc(readSourceLocation());
6793   }
6794   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6795     TL.setProtocolLoc(i, readSourceLocation());
6796 }
6797 
6798 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6799   TL.setHasBaseTypeAsWritten(Reader.readBool());
6800   TL.setTypeArgsLAngleLoc(readSourceLocation());
6801   TL.setTypeArgsRAngleLoc(readSourceLocation());
6802   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6803     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6804   TL.setProtocolLAngleLoc(readSourceLocation());
6805   TL.setProtocolRAngleLoc(readSourceLocation());
6806   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6807     TL.setProtocolLoc(i, readSourceLocation());
6808 }
6809 
6810 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6811   TL.setStarLoc(readSourceLocation());
6812 }
6813 
6814 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6815   TL.setKWLoc(readSourceLocation());
6816   TL.setLParenLoc(readSourceLocation());
6817   TL.setRParenLoc(readSourceLocation());
6818 }
6819 
6820 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6821   TL.setKWLoc(readSourceLocation());
6822 }
6823 
6824 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
6825   TL.setNameLoc(readSourceLocation());
6826 }
6827 void TypeLocReader::VisitDependentBitIntTypeLoc(
6828     clang::DependentBitIntTypeLoc TL) {
6829   TL.setNameLoc(readSourceLocation());
6830 }
6831 
6832 void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
6833   LocSeq::State Seq(ParentSeq);
6834   TypeLocReader TLR(*this, Seq);
6835   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6836     TLR.Visit(TL);
6837 }
6838 
6839 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6840   QualType InfoTy = readType();
6841   if (InfoTy.isNull())
6842     return nullptr;
6843 
6844   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6845   readTypeLoc(TInfo->getTypeLoc());
6846   return TInfo;
6847 }
6848 
6849 QualType ASTReader::GetType(TypeID ID) {
6850   assert(ContextObj && "reading type with no AST context");
6851   ASTContext &Context = *ContextObj;
6852 
6853   unsigned FastQuals = ID & Qualifiers::FastMask;
6854   unsigned Index = ID >> Qualifiers::FastWidth;
6855 
6856   if (Index < NUM_PREDEF_TYPE_IDS) {
6857     QualType T;
6858     switch ((PredefinedTypeIDs)Index) {
6859     case PREDEF_TYPE_NULL_ID:
6860       return QualType();
6861     case PREDEF_TYPE_VOID_ID:
6862       T = Context.VoidTy;
6863       break;
6864     case PREDEF_TYPE_BOOL_ID:
6865       T = Context.BoolTy;
6866       break;
6867     case PREDEF_TYPE_CHAR_U_ID:
6868     case PREDEF_TYPE_CHAR_S_ID:
6869       // FIXME: Check that the signedness of CharTy is correct!
6870       T = Context.CharTy;
6871       break;
6872     case PREDEF_TYPE_UCHAR_ID:
6873       T = Context.UnsignedCharTy;
6874       break;
6875     case PREDEF_TYPE_USHORT_ID:
6876       T = Context.UnsignedShortTy;
6877       break;
6878     case PREDEF_TYPE_UINT_ID:
6879       T = Context.UnsignedIntTy;
6880       break;
6881     case PREDEF_TYPE_ULONG_ID:
6882       T = Context.UnsignedLongTy;
6883       break;
6884     case PREDEF_TYPE_ULONGLONG_ID:
6885       T = Context.UnsignedLongLongTy;
6886       break;
6887     case PREDEF_TYPE_UINT128_ID:
6888       T = Context.UnsignedInt128Ty;
6889       break;
6890     case PREDEF_TYPE_SCHAR_ID:
6891       T = Context.SignedCharTy;
6892       break;
6893     case PREDEF_TYPE_WCHAR_ID:
6894       T = Context.WCharTy;
6895       break;
6896     case PREDEF_TYPE_SHORT_ID:
6897       T = Context.ShortTy;
6898       break;
6899     case PREDEF_TYPE_INT_ID:
6900       T = Context.IntTy;
6901       break;
6902     case PREDEF_TYPE_LONG_ID:
6903       T = Context.LongTy;
6904       break;
6905     case PREDEF_TYPE_LONGLONG_ID:
6906       T = Context.LongLongTy;
6907       break;
6908     case PREDEF_TYPE_INT128_ID:
6909       T = Context.Int128Ty;
6910       break;
6911     case PREDEF_TYPE_BFLOAT16_ID:
6912       T = Context.BFloat16Ty;
6913       break;
6914     case PREDEF_TYPE_HALF_ID:
6915       T = Context.HalfTy;
6916       break;
6917     case PREDEF_TYPE_FLOAT_ID:
6918       T = Context.FloatTy;
6919       break;
6920     case PREDEF_TYPE_DOUBLE_ID:
6921       T = Context.DoubleTy;
6922       break;
6923     case PREDEF_TYPE_LONGDOUBLE_ID:
6924       T = Context.LongDoubleTy;
6925       break;
6926     case PREDEF_TYPE_SHORT_ACCUM_ID:
6927       T = Context.ShortAccumTy;
6928       break;
6929     case PREDEF_TYPE_ACCUM_ID:
6930       T = Context.AccumTy;
6931       break;
6932     case PREDEF_TYPE_LONG_ACCUM_ID:
6933       T = Context.LongAccumTy;
6934       break;
6935     case PREDEF_TYPE_USHORT_ACCUM_ID:
6936       T = Context.UnsignedShortAccumTy;
6937       break;
6938     case PREDEF_TYPE_UACCUM_ID:
6939       T = Context.UnsignedAccumTy;
6940       break;
6941     case PREDEF_TYPE_ULONG_ACCUM_ID:
6942       T = Context.UnsignedLongAccumTy;
6943       break;
6944     case PREDEF_TYPE_SHORT_FRACT_ID:
6945       T = Context.ShortFractTy;
6946       break;
6947     case PREDEF_TYPE_FRACT_ID:
6948       T = Context.FractTy;
6949       break;
6950     case PREDEF_TYPE_LONG_FRACT_ID:
6951       T = Context.LongFractTy;
6952       break;
6953     case PREDEF_TYPE_USHORT_FRACT_ID:
6954       T = Context.UnsignedShortFractTy;
6955       break;
6956     case PREDEF_TYPE_UFRACT_ID:
6957       T = Context.UnsignedFractTy;
6958       break;
6959     case PREDEF_TYPE_ULONG_FRACT_ID:
6960       T = Context.UnsignedLongFractTy;
6961       break;
6962     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6963       T = Context.SatShortAccumTy;
6964       break;
6965     case PREDEF_TYPE_SAT_ACCUM_ID:
6966       T = Context.SatAccumTy;
6967       break;
6968     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6969       T = Context.SatLongAccumTy;
6970       break;
6971     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6972       T = Context.SatUnsignedShortAccumTy;
6973       break;
6974     case PREDEF_TYPE_SAT_UACCUM_ID:
6975       T = Context.SatUnsignedAccumTy;
6976       break;
6977     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6978       T = Context.SatUnsignedLongAccumTy;
6979       break;
6980     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6981       T = Context.SatShortFractTy;
6982       break;
6983     case PREDEF_TYPE_SAT_FRACT_ID:
6984       T = Context.SatFractTy;
6985       break;
6986     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6987       T = Context.SatLongFractTy;
6988       break;
6989     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6990       T = Context.SatUnsignedShortFractTy;
6991       break;
6992     case PREDEF_TYPE_SAT_UFRACT_ID:
6993       T = Context.SatUnsignedFractTy;
6994       break;
6995     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6996       T = Context.SatUnsignedLongFractTy;
6997       break;
6998     case PREDEF_TYPE_FLOAT16_ID:
6999       T = Context.Float16Ty;
7000       break;
7001     case PREDEF_TYPE_FLOAT128_ID:
7002       T = Context.Float128Ty;
7003       break;
7004     case PREDEF_TYPE_IBM128_ID:
7005       T = Context.Ibm128Ty;
7006       break;
7007     case PREDEF_TYPE_OVERLOAD_ID:
7008       T = Context.OverloadTy;
7009       break;
7010     case PREDEF_TYPE_BOUND_MEMBER:
7011       T = Context.BoundMemberTy;
7012       break;
7013     case PREDEF_TYPE_PSEUDO_OBJECT:
7014       T = Context.PseudoObjectTy;
7015       break;
7016     case PREDEF_TYPE_DEPENDENT_ID:
7017       T = Context.DependentTy;
7018       break;
7019     case PREDEF_TYPE_UNKNOWN_ANY:
7020       T = Context.UnknownAnyTy;
7021       break;
7022     case PREDEF_TYPE_NULLPTR_ID:
7023       T = Context.NullPtrTy;
7024       break;
7025     case PREDEF_TYPE_CHAR8_ID:
7026       T = Context.Char8Ty;
7027       break;
7028     case PREDEF_TYPE_CHAR16_ID:
7029       T = Context.Char16Ty;
7030       break;
7031     case PREDEF_TYPE_CHAR32_ID:
7032       T = Context.Char32Ty;
7033       break;
7034     case PREDEF_TYPE_OBJC_ID:
7035       T = Context.ObjCBuiltinIdTy;
7036       break;
7037     case PREDEF_TYPE_OBJC_CLASS:
7038       T = Context.ObjCBuiltinClassTy;
7039       break;
7040     case PREDEF_TYPE_OBJC_SEL:
7041       T = Context.ObjCBuiltinSelTy;
7042       break;
7043 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7044     case PREDEF_TYPE_##Id##_ID: \
7045       T = Context.SingletonId; \
7046       break;
7047 #include "clang/Basic/OpenCLImageTypes.def"
7048 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7049     case PREDEF_TYPE_##Id##_ID: \
7050       T = Context.Id##Ty; \
7051       break;
7052 #include "clang/Basic/OpenCLExtensionTypes.def"
7053     case PREDEF_TYPE_SAMPLER_ID:
7054       T = Context.OCLSamplerTy;
7055       break;
7056     case PREDEF_TYPE_EVENT_ID:
7057       T = Context.OCLEventTy;
7058       break;
7059     case PREDEF_TYPE_CLK_EVENT_ID:
7060       T = Context.OCLClkEventTy;
7061       break;
7062     case PREDEF_TYPE_QUEUE_ID:
7063       T = Context.OCLQueueTy;
7064       break;
7065     case PREDEF_TYPE_RESERVE_ID_ID:
7066       T = Context.OCLReserveIDTy;
7067       break;
7068     case PREDEF_TYPE_AUTO_DEDUCT:
7069       T = Context.getAutoDeductType();
7070       break;
7071     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7072       T = Context.getAutoRRefDeductType();
7073       break;
7074     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7075       T = Context.ARCUnbridgedCastTy;
7076       break;
7077     case PREDEF_TYPE_BUILTIN_FN:
7078       T = Context.BuiltinFnTy;
7079       break;
7080     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7081       T = Context.IncompleteMatrixIdxTy;
7082       break;
7083     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7084       T = Context.OMPArraySectionTy;
7085       break;
7086     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7087       T = Context.OMPArraySectionTy;
7088       break;
7089     case PREDEF_TYPE_OMP_ITERATOR:
7090       T = Context.OMPIteratorTy;
7091       break;
7092 #define SVE_TYPE(Name, Id, SingletonId) \
7093     case PREDEF_TYPE_##Id##_ID: \
7094       T = Context.SingletonId; \
7095       break;
7096 #include "clang/Basic/AArch64SVEACLETypes.def"
7097 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7098     case PREDEF_TYPE_##Id##_ID: \
7099       T = Context.Id##Ty; \
7100       break;
7101 #include "clang/Basic/PPCTypes.def"
7102 #define RVV_TYPE(Name, Id, SingletonId) \
7103     case PREDEF_TYPE_##Id##_ID: \
7104       T = Context.SingletonId; \
7105       break;
7106 #include "clang/Basic/RISCVVTypes.def"
7107     }
7108 
7109     assert(!T.isNull() && "Unknown predefined type");
7110     return T.withFastQualifiers(FastQuals);
7111   }
7112 
7113   Index -= NUM_PREDEF_TYPE_IDS;
7114   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7115   if (TypesLoaded[Index].isNull()) {
7116     TypesLoaded[Index] = readTypeRecord(Index);
7117     if (TypesLoaded[Index].isNull())
7118       return QualType();
7119 
7120     TypesLoaded[Index]->setFromAST();
7121     if (DeserializationListener)
7122       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7123                                         TypesLoaded[Index]);
7124   }
7125 
7126   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7127 }
7128 
7129 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7130   return GetType(getGlobalTypeID(F, LocalID));
7131 }
7132 
7133 serialization::TypeID
7134 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7135   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7136   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7137 
7138   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7139     return LocalID;
7140 
7141   if (!F.ModuleOffsetMap.empty())
7142     ReadModuleOffsetMap(F);
7143 
7144   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7145     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7146   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7147 
7148   unsigned GlobalIndex = LocalIndex + I->second;
7149   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7150 }
7151 
7152 TemplateArgumentLocInfo
7153 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7154   switch (Kind) {
7155   case TemplateArgument::Expression:
7156     return readExpr();
7157   case TemplateArgument::Type:
7158     return readTypeSourceInfo();
7159   case TemplateArgument::Template: {
7160     NestedNameSpecifierLoc QualifierLoc =
7161       readNestedNameSpecifierLoc();
7162     SourceLocation TemplateNameLoc = readSourceLocation();
7163     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7164                                    TemplateNameLoc, SourceLocation());
7165   }
7166   case TemplateArgument::TemplateExpansion: {
7167     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7168     SourceLocation TemplateNameLoc = readSourceLocation();
7169     SourceLocation EllipsisLoc = readSourceLocation();
7170     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7171                                    TemplateNameLoc, EllipsisLoc);
7172   }
7173   case TemplateArgument::Null:
7174   case TemplateArgument::Integral:
7175   case TemplateArgument::Declaration:
7176   case TemplateArgument::NullPtr:
7177   case TemplateArgument::Pack:
7178     // FIXME: Is this right?
7179     return TemplateArgumentLocInfo();
7180   }
7181   llvm_unreachable("unexpected template argument loc");
7182 }
7183 
7184 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7185   TemplateArgument Arg = readTemplateArgument();
7186 
7187   if (Arg.getKind() == TemplateArgument::Expression) {
7188     if (readBool()) // bool InfoHasSameExpr.
7189       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7190   }
7191   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7192 }
7193 
7194 const ASTTemplateArgumentListInfo *
7195 ASTRecordReader::readASTTemplateArgumentListInfo() {
7196   SourceLocation LAngleLoc = readSourceLocation();
7197   SourceLocation RAngleLoc = readSourceLocation();
7198   unsigned NumArgsAsWritten = readInt();
7199   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7200   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7201     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7202   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7203 }
7204 
7205 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7206   return GetDecl(ID);
7207 }
7208 
7209 void ASTReader::CompleteRedeclChain(const Decl *D) {
7210   if (NumCurrentElementsDeserializing) {
7211     // We arrange to not care about the complete redeclaration chain while we're
7212     // deserializing. Just remember that the AST has marked this one as complete
7213     // but that it's not actually complete yet, so we know we still need to
7214     // complete it later.
7215     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7216     return;
7217   }
7218 
7219   if (!D->getDeclContext()) {
7220     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7221     return;
7222   }
7223 
7224   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7225 
7226   // If this is a named declaration, complete it by looking it up
7227   // within its context.
7228   //
7229   // FIXME: Merging a function definition should merge
7230   // all mergeable entities within it.
7231   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7232       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7233     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7234       if (!getContext().getLangOpts().CPlusPlus &&
7235           isa<TranslationUnitDecl>(DC)) {
7236         // Outside of C++, we don't have a lookup table for the TU, so update
7237         // the identifier instead. (For C++ modules, we don't store decls
7238         // in the serialized identifier table, so we do the lookup in the TU.)
7239         auto *II = Name.getAsIdentifierInfo();
7240         assert(II && "non-identifier name in C?");
7241         if (II->isOutOfDate())
7242           updateOutOfDateIdentifier(*II);
7243       } else
7244         DC->lookup(Name);
7245     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7246       // Find all declarations of this kind from the relevant context.
7247       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7248         auto *DC = cast<DeclContext>(DCDecl);
7249         SmallVector<Decl*, 8> Decls;
7250         FindExternalLexicalDecls(
7251             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7252       }
7253     }
7254   }
7255 
7256   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7257     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7258   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7259     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7260   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7261     if (auto *Template = FD->getPrimaryTemplate())
7262       Template->LoadLazySpecializations();
7263   }
7264 }
7265 
7266 CXXCtorInitializer **
7267 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7268   RecordLocation Loc = getLocalBitOffset(Offset);
7269   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7270   SavedStreamPosition SavedPosition(Cursor);
7271   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7272     Error(std::move(Err));
7273     return nullptr;
7274   }
7275   ReadingKindTracker ReadingKind(Read_Decl, *this);
7276 
7277   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7278   if (!MaybeCode) {
7279     Error(MaybeCode.takeError());
7280     return nullptr;
7281   }
7282   unsigned Code = MaybeCode.get();
7283 
7284   ASTRecordReader Record(*this, *Loc.F);
7285   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7286   if (!MaybeRecCode) {
7287     Error(MaybeRecCode.takeError());
7288     return nullptr;
7289   }
7290   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7291     Error("malformed AST file: missing C++ ctor initializers");
7292     return nullptr;
7293   }
7294 
7295   return Record.readCXXCtorInitializers();
7296 }
7297 
7298 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7299   assert(ContextObj && "reading base specifiers with no AST context");
7300   ASTContext &Context = *ContextObj;
7301 
7302   RecordLocation Loc = getLocalBitOffset(Offset);
7303   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7304   SavedStreamPosition SavedPosition(Cursor);
7305   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7306     Error(std::move(Err));
7307     return nullptr;
7308   }
7309   ReadingKindTracker ReadingKind(Read_Decl, *this);
7310 
7311   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7312   if (!MaybeCode) {
7313     Error(MaybeCode.takeError());
7314     return nullptr;
7315   }
7316   unsigned Code = MaybeCode.get();
7317 
7318   ASTRecordReader Record(*this, *Loc.F);
7319   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7320   if (!MaybeRecCode) {
7321     Error(MaybeCode.takeError());
7322     return nullptr;
7323   }
7324   unsigned RecCode = MaybeRecCode.get();
7325 
7326   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7327     Error("malformed AST file: missing C++ base specifiers");
7328     return nullptr;
7329   }
7330 
7331   unsigned NumBases = Record.readInt();
7332   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7333   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7334   for (unsigned I = 0; I != NumBases; ++I)
7335     Bases[I] = Record.readCXXBaseSpecifier();
7336   return Bases;
7337 }
7338 
7339 serialization::DeclID
7340 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7341   if (LocalID < NUM_PREDEF_DECL_IDS)
7342     return LocalID;
7343 
7344   if (!F.ModuleOffsetMap.empty())
7345     ReadModuleOffsetMap(F);
7346 
7347   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7348     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7349   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7350 
7351   return LocalID + I->second;
7352 }
7353 
7354 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7355                                    ModuleFile &M) const {
7356   // Predefined decls aren't from any module.
7357   if (ID < NUM_PREDEF_DECL_IDS)
7358     return false;
7359 
7360   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7361          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7362 }
7363 
7364 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7365   if (!D->isFromASTFile())
7366     return nullptr;
7367   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7368   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7369   return I->second;
7370 }
7371 
7372 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7373   if (ID < NUM_PREDEF_DECL_IDS)
7374     return SourceLocation();
7375 
7376   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7377 
7378   if (Index > DeclsLoaded.size()) {
7379     Error("declaration ID out-of-range for AST file");
7380     return SourceLocation();
7381   }
7382 
7383   if (Decl *D = DeclsLoaded[Index])
7384     return D->getLocation();
7385 
7386   SourceLocation Loc;
7387   DeclCursorForID(ID, Loc);
7388   return Loc;
7389 }
7390 
7391 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7392   switch (ID) {
7393   case PREDEF_DECL_NULL_ID:
7394     return nullptr;
7395 
7396   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7397     return Context.getTranslationUnitDecl();
7398 
7399   case PREDEF_DECL_OBJC_ID_ID:
7400     return Context.getObjCIdDecl();
7401 
7402   case PREDEF_DECL_OBJC_SEL_ID:
7403     return Context.getObjCSelDecl();
7404 
7405   case PREDEF_DECL_OBJC_CLASS_ID:
7406     return Context.getObjCClassDecl();
7407 
7408   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7409     return Context.getObjCProtocolDecl();
7410 
7411   case PREDEF_DECL_INT_128_ID:
7412     return Context.getInt128Decl();
7413 
7414   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7415     return Context.getUInt128Decl();
7416 
7417   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7418     return Context.getObjCInstanceTypeDecl();
7419 
7420   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7421     return Context.getBuiltinVaListDecl();
7422 
7423   case PREDEF_DECL_VA_LIST_TAG:
7424     return Context.getVaListTagDecl();
7425 
7426   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7427     return Context.getBuiltinMSVaListDecl();
7428 
7429   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7430     return Context.getMSGuidTagDecl();
7431 
7432   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7433     return Context.getExternCContextDecl();
7434 
7435   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7436     return Context.getMakeIntegerSeqDecl();
7437 
7438   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7439     return Context.getCFConstantStringDecl();
7440 
7441   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7442     return Context.getCFConstantStringTagDecl();
7443 
7444   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7445     return Context.getTypePackElementDecl();
7446   }
7447   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7448 }
7449 
7450 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7451   assert(ContextObj && "reading decl with no AST context");
7452   if (ID < NUM_PREDEF_DECL_IDS) {
7453     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7454     if (D) {
7455       // Track that we have merged the declaration with ID \p ID into the
7456       // pre-existing predefined declaration \p D.
7457       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7458       if (Merged.empty())
7459         Merged.push_back(ID);
7460     }
7461     return D;
7462   }
7463 
7464   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7465 
7466   if (Index >= DeclsLoaded.size()) {
7467     assert(0 && "declaration ID out-of-range for AST file");
7468     Error("declaration ID out-of-range for AST file");
7469     return nullptr;
7470   }
7471 
7472   return DeclsLoaded[Index];
7473 }
7474 
7475 Decl *ASTReader::GetDecl(DeclID ID) {
7476   if (ID < NUM_PREDEF_DECL_IDS)
7477     return GetExistingDecl(ID);
7478 
7479   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7480 
7481   if (Index >= DeclsLoaded.size()) {
7482     assert(0 && "declaration ID out-of-range for AST file");
7483     Error("declaration ID out-of-range for AST file");
7484     return nullptr;
7485   }
7486 
7487   if (!DeclsLoaded[Index]) {
7488     ReadDeclRecord(ID);
7489     if (DeserializationListener)
7490       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7491   }
7492 
7493   return DeclsLoaded[Index];
7494 }
7495 
7496 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7497                                                   DeclID GlobalID) {
7498   if (GlobalID < NUM_PREDEF_DECL_IDS)
7499     return GlobalID;
7500 
7501   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7502   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7503   ModuleFile *Owner = I->second;
7504 
7505   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7506     = M.GlobalToLocalDeclIDs.find(Owner);
7507   if (Pos == M.GlobalToLocalDeclIDs.end())
7508     return 0;
7509 
7510   return GlobalID - Owner->BaseDeclID + Pos->second;
7511 }
7512 
7513 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7514                                             const RecordData &Record,
7515                                             unsigned &Idx) {
7516   if (Idx >= Record.size()) {
7517     Error("Corrupted AST file");
7518     return 0;
7519   }
7520 
7521   return getGlobalDeclID(F, Record[Idx++]);
7522 }
7523 
7524 /// Resolve the offset of a statement into a statement.
7525 ///
7526 /// This operation will read a new statement from the external
7527 /// source each time it is called, and is meant to be used via a
7528 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7529 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7530   // Switch case IDs are per Decl.
7531   ClearSwitchCaseIDs();
7532 
7533   // Offset here is a global offset across the entire chain.
7534   RecordLocation Loc = getLocalBitOffset(Offset);
7535   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7536     Error(std::move(Err));
7537     return nullptr;
7538   }
7539   assert(NumCurrentElementsDeserializing == 0 &&
7540          "should not be called while already deserializing");
7541   Deserializing D(this);
7542   return ReadStmtFromStream(*Loc.F);
7543 }
7544 
7545 void ASTReader::FindExternalLexicalDecls(
7546     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7547     SmallVectorImpl<Decl *> &Decls) {
7548   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7549 
7550   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7551     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7552     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7553       auto K = (Decl::Kind)+LexicalDecls[I];
7554       if (!IsKindWeWant(K))
7555         continue;
7556 
7557       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7558 
7559       // Don't add predefined declarations to the lexical context more
7560       // than once.
7561       if (ID < NUM_PREDEF_DECL_IDS) {
7562         if (PredefsVisited[ID])
7563           continue;
7564 
7565         PredefsVisited[ID] = true;
7566       }
7567 
7568       if (Decl *D = GetLocalDecl(*M, ID)) {
7569         assert(D->getKind() == K && "wrong kind for lexical decl");
7570         if (!DC->isDeclInLexicalTraversal(D))
7571           Decls.push_back(D);
7572       }
7573     }
7574   };
7575 
7576   if (isa<TranslationUnitDecl>(DC)) {
7577     for (auto Lexical : TULexicalDecls)
7578       Visit(Lexical.first, Lexical.second);
7579   } else {
7580     auto I = LexicalDecls.find(DC);
7581     if (I != LexicalDecls.end())
7582       Visit(I->second.first, I->second.second);
7583   }
7584 
7585   ++NumLexicalDeclContextsRead;
7586 }
7587 
7588 namespace {
7589 
7590 class DeclIDComp {
7591   ASTReader &Reader;
7592   ModuleFile &Mod;
7593 
7594 public:
7595   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7596 
7597   bool operator()(LocalDeclID L, LocalDeclID R) const {
7598     SourceLocation LHS = getLocation(L);
7599     SourceLocation RHS = getLocation(R);
7600     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7601   }
7602 
7603   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7604     SourceLocation RHS = getLocation(R);
7605     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7606   }
7607 
7608   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7609     SourceLocation LHS = getLocation(L);
7610     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7611   }
7612 
7613   SourceLocation getLocation(LocalDeclID ID) const {
7614     return Reader.getSourceManager().getFileLoc(
7615             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7616   }
7617 };
7618 
7619 } // namespace
7620 
7621 void ASTReader::FindFileRegionDecls(FileID File,
7622                                     unsigned Offset, unsigned Length,
7623                                     SmallVectorImpl<Decl *> &Decls) {
7624   SourceManager &SM = getSourceManager();
7625 
7626   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7627   if (I == FileDeclIDs.end())
7628     return;
7629 
7630   FileDeclsInfo &DInfo = I->second;
7631   if (DInfo.Decls.empty())
7632     return;
7633 
7634   SourceLocation
7635     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7636   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7637 
7638   DeclIDComp DIDComp(*this, *DInfo.Mod);
7639   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7640       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7641   if (BeginIt != DInfo.Decls.begin())
7642     --BeginIt;
7643 
7644   // If we are pointing at a top-level decl inside an objc container, we need
7645   // to backtrack until we find it otherwise we will fail to report that the
7646   // region overlaps with an objc container.
7647   while (BeginIt != DInfo.Decls.begin() &&
7648          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7649              ->isTopLevelDeclInObjCContainer())
7650     --BeginIt;
7651 
7652   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7653       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7654   if (EndIt != DInfo.Decls.end())
7655     ++EndIt;
7656 
7657   for (ArrayRef<serialization::LocalDeclID>::iterator
7658          DIt = BeginIt; DIt != EndIt; ++DIt)
7659     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7660 }
7661 
7662 bool
7663 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7664                                           DeclarationName Name) {
7665   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7666          "DeclContext has no visible decls in storage");
7667   if (!Name)
7668     return false;
7669 
7670   auto It = Lookups.find(DC);
7671   if (It == Lookups.end())
7672     return false;
7673 
7674   Deserializing LookupResults(this);
7675 
7676   // Load the list of declarations.
7677   SmallVector<NamedDecl *, 64> Decls;
7678   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7679   for (DeclID ID : It->second.Table.find(Name)) {
7680     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7681     if (ND->getDeclName() == Name && Found.insert(ND).second)
7682       Decls.push_back(ND);
7683   }
7684 
7685   ++NumVisibleDeclContextsRead;
7686   SetExternalVisibleDeclsForName(DC, Name, Decls);
7687   return !Decls.empty();
7688 }
7689 
7690 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7691   if (!DC->hasExternalVisibleStorage())
7692     return;
7693 
7694   auto It = Lookups.find(DC);
7695   assert(It != Lookups.end() &&
7696          "have external visible storage but no lookup tables");
7697 
7698   DeclsMap Decls;
7699 
7700   for (DeclID ID : It->second.Table.findAll()) {
7701     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7702     Decls[ND->getDeclName()].push_back(ND);
7703   }
7704 
7705   ++NumVisibleDeclContextsRead;
7706 
7707   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7708     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7709   }
7710   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7711 }
7712 
7713 const serialization::reader::DeclContextLookupTable *
7714 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7715   auto I = Lookups.find(Primary);
7716   return I == Lookups.end() ? nullptr : &I->second;
7717 }
7718 
7719 /// Under non-PCH compilation the consumer receives the objc methods
7720 /// before receiving the implementation, and codegen depends on this.
7721 /// We simulate this by deserializing and passing to consumer the methods of the
7722 /// implementation before passing the deserialized implementation decl.
7723 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7724                                        ASTConsumer *Consumer) {
7725   assert(ImplD && Consumer);
7726 
7727   for (auto *I : ImplD->methods())
7728     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7729 
7730   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7731 }
7732 
7733 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7734   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7735     PassObjCImplDeclToConsumer(ImplD, Consumer);
7736   else
7737     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7738 }
7739 
7740 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7741   this->Consumer = Consumer;
7742 
7743   if (Consumer)
7744     PassInterestingDeclsToConsumer();
7745 
7746   if (DeserializationListener)
7747     DeserializationListener->ReaderInitialized(this);
7748 }
7749 
7750 void ASTReader::PrintStats() {
7751   std::fprintf(stderr, "*** AST File Statistics:\n");
7752 
7753   unsigned NumTypesLoaded =
7754       TypesLoaded.size() - llvm::count(TypesLoaded, QualType());
7755   unsigned NumDeclsLoaded =
7756       DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr);
7757   unsigned NumIdentifiersLoaded =
7758       IdentifiersLoaded.size() -
7759       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
7760   unsigned NumMacrosLoaded =
7761       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
7762   unsigned NumSelectorsLoaded =
7763       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
7764 
7765   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7766     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7767                  NumSLocEntriesRead, TotalNumSLocEntries,
7768                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7769   if (!TypesLoaded.empty())
7770     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7771                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7772                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7773   if (!DeclsLoaded.empty())
7774     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7775                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7776                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7777   if (!IdentifiersLoaded.empty())
7778     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7779                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7780                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7781   if (!MacrosLoaded.empty())
7782     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7783                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7784                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7785   if (!SelectorsLoaded.empty())
7786     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7787                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7788                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7789   if (TotalNumStatements)
7790     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7791                  NumStatementsRead, TotalNumStatements,
7792                  ((float)NumStatementsRead/TotalNumStatements * 100));
7793   if (TotalNumMacros)
7794     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7795                  NumMacrosRead, TotalNumMacros,
7796                  ((float)NumMacrosRead/TotalNumMacros * 100));
7797   if (TotalLexicalDeclContexts)
7798     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7799                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7800                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7801                   * 100));
7802   if (TotalVisibleDeclContexts)
7803     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7804                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7805                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7806                   * 100));
7807   if (TotalNumMethodPoolEntries)
7808     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7809                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7810                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7811                   * 100));
7812   if (NumMethodPoolLookups)
7813     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7814                  NumMethodPoolHits, NumMethodPoolLookups,
7815                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7816   if (NumMethodPoolTableLookups)
7817     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7818                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7819                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7820                   * 100.0));
7821   if (NumIdentifierLookupHits)
7822     std::fprintf(stderr,
7823                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7824                  NumIdentifierLookupHits, NumIdentifierLookups,
7825                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7826 
7827   if (GlobalIndex) {
7828     std::fprintf(stderr, "\n");
7829     GlobalIndex->printStats();
7830   }
7831 
7832   std::fprintf(stderr, "\n");
7833   dump();
7834   std::fprintf(stderr, "\n");
7835 }
7836 
7837 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7838 LLVM_DUMP_METHOD static void
7839 dumpModuleIDMap(StringRef Name,
7840                 const ContinuousRangeMap<Key, ModuleFile *,
7841                                          InitialCapacity> &Map) {
7842   if (Map.begin() == Map.end())
7843     return;
7844 
7845   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7846 
7847   llvm::errs() << Name << ":\n";
7848   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7849        I != IEnd; ++I) {
7850     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7851       << "\n";
7852   }
7853 }
7854 
7855 LLVM_DUMP_METHOD void ASTReader::dump() {
7856   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7857   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7858   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7859   dumpModuleIDMap("Global type map", GlobalTypeMap);
7860   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7861   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7862   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7863   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7864   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7865   dumpModuleIDMap("Global preprocessed entity map",
7866                   GlobalPreprocessedEntityMap);
7867 
7868   llvm::errs() << "\n*** PCH/Modules Loaded:";
7869   for (ModuleFile &M : ModuleMgr)
7870     M.dump();
7871 }
7872 
7873 /// Return the amount of memory used by memory buffers, breaking down
7874 /// by heap-backed versus mmap'ed memory.
7875 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7876   for (ModuleFile &I : ModuleMgr) {
7877     if (llvm::MemoryBuffer *buf = I.Buffer) {
7878       size_t bytes = buf->getBufferSize();
7879       switch (buf->getBufferKind()) {
7880         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7881           sizes.malloc_bytes += bytes;
7882           break;
7883         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7884           sizes.mmap_bytes += bytes;
7885           break;
7886       }
7887     }
7888   }
7889 }
7890 
7891 void ASTReader::InitializeSema(Sema &S) {
7892   SemaObj = &S;
7893   S.addExternalSource(this);
7894 
7895   // Makes sure any declarations that were deserialized "too early"
7896   // still get added to the identifier's declaration chains.
7897   for (uint64_t ID : PreloadedDeclIDs) {
7898     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7899     pushExternalDeclIntoScope(D, D->getDeclName());
7900   }
7901   PreloadedDeclIDs.clear();
7902 
7903   // FIXME: What happens if these are changed by a module import?
7904   if (!FPPragmaOptions.empty()) {
7905     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7906     FPOptionsOverride NewOverrides =
7907         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7908     SemaObj->CurFPFeatures =
7909         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7910   }
7911 
7912   SemaObj->OpenCLFeatures = OpenCLExtensions;
7913 
7914   UpdateSema();
7915 }
7916 
7917 void ASTReader::UpdateSema() {
7918   assert(SemaObj && "no Sema to update");
7919 
7920   // Load the offsets of the declarations that Sema references.
7921   // They will be lazily deserialized when needed.
7922   if (!SemaDeclRefs.empty()) {
7923     assert(SemaDeclRefs.size() % 3 == 0);
7924     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7925       if (!SemaObj->StdNamespace)
7926         SemaObj->StdNamespace = SemaDeclRefs[I];
7927       if (!SemaObj->StdBadAlloc)
7928         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7929       if (!SemaObj->StdAlignValT)
7930         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7931     }
7932     SemaDeclRefs.clear();
7933   }
7934 
7935   // Update the state of pragmas. Use the same API as if we had encountered the
7936   // pragma in the source.
7937   if(OptimizeOffPragmaLocation.isValid())
7938     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7939   if (PragmaMSStructState != -1)
7940     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7941   if (PointersToMembersPragmaLocation.isValid()) {
7942     SemaObj->ActOnPragmaMSPointersToMembers(
7943         (LangOptions::PragmaMSPointersToMembersKind)
7944             PragmaMSPointersToMembersState,
7945         PointersToMembersPragmaLocation);
7946   }
7947   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7948 
7949   if (PragmaAlignPackCurrentValue) {
7950     // The bottom of the stack might have a default value. It must be adjusted
7951     // to the current value to ensure that the packing state is preserved after
7952     // popping entries that were included/imported from a PCH/module.
7953     bool DropFirst = false;
7954     if (!PragmaAlignPackStack.empty() &&
7955         PragmaAlignPackStack.front().Location.isInvalid()) {
7956       assert(PragmaAlignPackStack.front().Value ==
7957                  SemaObj->AlignPackStack.DefaultValue &&
7958              "Expected a default alignment value");
7959       SemaObj->AlignPackStack.Stack.emplace_back(
7960           PragmaAlignPackStack.front().SlotLabel,
7961           SemaObj->AlignPackStack.CurrentValue,
7962           SemaObj->AlignPackStack.CurrentPragmaLocation,
7963           PragmaAlignPackStack.front().PushLocation);
7964       DropFirst = true;
7965     }
7966     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7967                                  .drop_front(DropFirst ? 1 : 0)) {
7968       SemaObj->AlignPackStack.Stack.emplace_back(
7969           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7970     }
7971     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7972       assert(*PragmaAlignPackCurrentValue ==
7973                  SemaObj->AlignPackStack.DefaultValue &&
7974              "Expected a default align and pack value");
7975       // Keep the current values.
7976     } else {
7977       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7978       SemaObj->AlignPackStack.CurrentPragmaLocation =
7979           PragmaAlignPackCurrentLocation;
7980     }
7981   }
7982   if (FpPragmaCurrentValue) {
7983     // The bottom of the stack might have a default value. It must be adjusted
7984     // to the current value to ensure that fp-pragma state is preserved after
7985     // popping entries that were included/imported from a PCH/module.
7986     bool DropFirst = false;
7987     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7988       assert(FpPragmaStack.front().Value ==
7989                  SemaObj->FpPragmaStack.DefaultValue &&
7990              "Expected a default pragma float_control value");
7991       SemaObj->FpPragmaStack.Stack.emplace_back(
7992           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7993           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7994           FpPragmaStack.front().PushLocation);
7995       DropFirst = true;
7996     }
7997     for (const auto &Entry :
7998          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7999       SemaObj->FpPragmaStack.Stack.emplace_back(
8000           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8001     if (FpPragmaCurrentLocation.isInvalid()) {
8002       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8003              "Expected a default pragma float_control value");
8004       // Keep the current values.
8005     } else {
8006       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8007       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8008     }
8009   }
8010 
8011   // For non-modular AST files, restore visiblity of modules.
8012   for (auto &Import : ImportedModules) {
8013     if (Import.ImportLoc.isInvalid())
8014       continue;
8015     if (Module *Imported = getSubmodule(Import.ID)) {
8016       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8017     }
8018   }
8019 }
8020 
8021 IdentifierInfo *ASTReader::get(StringRef Name) {
8022   // Note that we are loading an identifier.
8023   Deserializing AnIdentifier(this);
8024 
8025   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8026                                   NumIdentifierLookups,
8027                                   NumIdentifierLookupHits);
8028 
8029   // We don't need to do identifier table lookups in C++ modules (we preload
8030   // all interesting declarations, and don't need to use the scope for name
8031   // lookups). Perform the lookup in PCH files, though, since we don't build
8032   // a complete initial identifier table if we're carrying on from a PCH.
8033   if (PP.getLangOpts().CPlusPlus) {
8034     for (auto F : ModuleMgr.pch_modules())
8035       if (Visitor(*F))
8036         break;
8037   } else {
8038     // If there is a global index, look there first to determine which modules
8039     // provably do not have any results for this identifier.
8040     GlobalModuleIndex::HitSet Hits;
8041     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8042     if (!loadGlobalIndex()) {
8043       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8044         HitsPtr = &Hits;
8045       }
8046     }
8047 
8048     ModuleMgr.visit(Visitor, HitsPtr);
8049   }
8050 
8051   IdentifierInfo *II = Visitor.getIdentifierInfo();
8052   markIdentifierUpToDate(II);
8053   return II;
8054 }
8055 
8056 namespace clang {
8057 
8058   /// An identifier-lookup iterator that enumerates all of the
8059   /// identifiers stored within a set of AST files.
8060   class ASTIdentifierIterator : public IdentifierIterator {
8061     /// The AST reader whose identifiers are being enumerated.
8062     const ASTReader &Reader;
8063 
8064     /// The current index into the chain of AST files stored in
8065     /// the AST reader.
8066     unsigned Index;
8067 
8068     /// The current position within the identifier lookup table
8069     /// of the current AST file.
8070     ASTIdentifierLookupTable::key_iterator Current;
8071 
8072     /// The end position within the identifier lookup table of
8073     /// the current AST file.
8074     ASTIdentifierLookupTable::key_iterator End;
8075 
8076     /// Whether to skip any modules in the ASTReader.
8077     bool SkipModules;
8078 
8079   public:
8080     explicit ASTIdentifierIterator(const ASTReader &Reader,
8081                                    bool SkipModules = false);
8082 
8083     StringRef Next() override;
8084   };
8085 
8086 } // namespace clang
8087 
8088 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8089                                              bool SkipModules)
8090     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8091 }
8092 
8093 StringRef ASTIdentifierIterator::Next() {
8094   while (Current == End) {
8095     // If we have exhausted all of our AST files, we're done.
8096     if (Index == 0)
8097       return StringRef();
8098 
8099     --Index;
8100     ModuleFile &F = Reader.ModuleMgr[Index];
8101     if (SkipModules && F.isModule())
8102       continue;
8103 
8104     ASTIdentifierLookupTable *IdTable =
8105         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8106     Current = IdTable->key_begin();
8107     End = IdTable->key_end();
8108   }
8109 
8110   // We have any identifiers remaining in the current AST file; return
8111   // the next one.
8112   StringRef Result = *Current;
8113   ++Current;
8114   return Result;
8115 }
8116 
8117 namespace {
8118 
8119 /// A utility for appending two IdentifierIterators.
8120 class ChainedIdentifierIterator : public IdentifierIterator {
8121   std::unique_ptr<IdentifierIterator> Current;
8122   std::unique_ptr<IdentifierIterator> Queued;
8123 
8124 public:
8125   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8126                             std::unique_ptr<IdentifierIterator> Second)
8127       : Current(std::move(First)), Queued(std::move(Second)) {}
8128 
8129   StringRef Next() override {
8130     if (!Current)
8131       return StringRef();
8132 
8133     StringRef result = Current->Next();
8134     if (!result.empty())
8135       return result;
8136 
8137     // Try the queued iterator, which may itself be empty.
8138     Current.reset();
8139     std::swap(Current, Queued);
8140     return Next();
8141   }
8142 };
8143 
8144 } // namespace
8145 
8146 IdentifierIterator *ASTReader::getIdentifiers() {
8147   if (!loadGlobalIndex()) {
8148     std::unique_ptr<IdentifierIterator> ReaderIter(
8149         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8150     std::unique_ptr<IdentifierIterator> ModulesIter(
8151         GlobalIndex->createIdentifierIterator());
8152     return new ChainedIdentifierIterator(std::move(ReaderIter),
8153                                          std::move(ModulesIter));
8154   }
8155 
8156   return new ASTIdentifierIterator(*this);
8157 }
8158 
8159 namespace clang {
8160 namespace serialization {
8161 
8162   class ReadMethodPoolVisitor {
8163     ASTReader &Reader;
8164     Selector Sel;
8165     unsigned PriorGeneration;
8166     unsigned InstanceBits = 0;
8167     unsigned FactoryBits = 0;
8168     bool InstanceHasMoreThanOneDecl = false;
8169     bool FactoryHasMoreThanOneDecl = false;
8170     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8171     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8172 
8173   public:
8174     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8175                           unsigned PriorGeneration)
8176         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8177 
8178     bool operator()(ModuleFile &M) {
8179       if (!M.SelectorLookupTable)
8180         return false;
8181 
8182       // If we've already searched this module file, skip it now.
8183       if (M.Generation <= PriorGeneration)
8184         return true;
8185 
8186       ++Reader.NumMethodPoolTableLookups;
8187       ASTSelectorLookupTable *PoolTable
8188         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8189       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8190       if (Pos == PoolTable->end())
8191         return false;
8192 
8193       ++Reader.NumMethodPoolTableHits;
8194       ++Reader.NumSelectorsRead;
8195       // FIXME: Not quite happy with the statistics here. We probably should
8196       // disable this tracking when called via LoadSelector.
8197       // Also, should entries without methods count as misses?
8198       ++Reader.NumMethodPoolEntriesRead;
8199       ASTSelectorLookupTrait::data_type Data = *Pos;
8200       if (Reader.DeserializationListener)
8201         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8202 
8203       // Append methods in the reverse order, so that later we can process them
8204       // in the order they appear in the source code by iterating through
8205       // the vector in the reverse order.
8206       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8207       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8208       InstanceBits = Data.InstanceBits;
8209       FactoryBits = Data.FactoryBits;
8210       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8211       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8212       return false;
8213     }
8214 
8215     /// Retrieve the instance methods found by this visitor.
8216     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8217       return InstanceMethods;
8218     }
8219 
8220     /// Retrieve the instance methods found by this visitor.
8221     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8222       return FactoryMethods;
8223     }
8224 
8225     unsigned getInstanceBits() const { return InstanceBits; }
8226     unsigned getFactoryBits() const { return FactoryBits; }
8227 
8228     bool instanceHasMoreThanOneDecl() const {
8229       return InstanceHasMoreThanOneDecl;
8230     }
8231 
8232     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8233   };
8234 
8235 } // namespace serialization
8236 } // namespace clang
8237 
8238 /// Add the given set of methods to the method list.
8239 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8240                              ObjCMethodList &List) {
8241   for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I)
8242     S.addMethodToGlobalList(&List, *I);
8243 }
8244 
8245 void ASTReader::ReadMethodPool(Selector Sel) {
8246   // Get the selector generation and update it to the current generation.
8247   unsigned &Generation = SelectorGeneration[Sel];
8248   unsigned PriorGeneration = Generation;
8249   Generation = getGeneration();
8250   SelectorOutOfDate[Sel] = false;
8251 
8252   // Search for methods defined with this selector.
8253   ++NumMethodPoolLookups;
8254   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8255   ModuleMgr.visit(Visitor);
8256 
8257   if (Visitor.getInstanceMethods().empty() &&
8258       Visitor.getFactoryMethods().empty())
8259     return;
8260 
8261   ++NumMethodPoolHits;
8262 
8263   if (!getSema())
8264     return;
8265 
8266   Sema &S = *getSema();
8267   Sema::GlobalMethodPool::iterator Pos =
8268       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8269           .first;
8270 
8271   Pos->second.first.setBits(Visitor.getInstanceBits());
8272   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8273   Pos->second.second.setBits(Visitor.getFactoryBits());
8274   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8275 
8276   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8277   // when building a module we keep every method individually and may need to
8278   // update hasMoreThanOneDecl as we add the methods.
8279   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8280   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8281 }
8282 
8283 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8284   if (SelectorOutOfDate[Sel])
8285     ReadMethodPool(Sel);
8286 }
8287 
8288 void ASTReader::ReadKnownNamespaces(
8289                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8290   Namespaces.clear();
8291 
8292   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8293     if (NamespaceDecl *Namespace
8294                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8295       Namespaces.push_back(Namespace);
8296   }
8297 }
8298 
8299 void ASTReader::ReadUndefinedButUsed(
8300     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8301   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8302     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8303     SourceLocation Loc =
8304         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8305     Undefined.insert(std::make_pair(D, Loc));
8306   }
8307 }
8308 
8309 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8310     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8311                                                      Exprs) {
8312   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8313     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8314     uint64_t Count = DelayedDeleteExprs[Idx++];
8315     for (uint64_t C = 0; C < Count; ++C) {
8316       SourceLocation DeleteLoc =
8317           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8318       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8319       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8320     }
8321   }
8322 }
8323 
8324 void ASTReader::ReadTentativeDefinitions(
8325                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8326   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8327     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8328     if (Var)
8329       TentativeDefs.push_back(Var);
8330   }
8331   TentativeDefinitions.clear();
8332 }
8333 
8334 void ASTReader::ReadUnusedFileScopedDecls(
8335                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8336   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8337     DeclaratorDecl *D
8338       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8339     if (D)
8340       Decls.push_back(D);
8341   }
8342   UnusedFileScopedDecls.clear();
8343 }
8344 
8345 void ASTReader::ReadDelegatingConstructors(
8346                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8347   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8348     CXXConstructorDecl *D
8349       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8350     if (D)
8351       Decls.push_back(D);
8352   }
8353   DelegatingCtorDecls.clear();
8354 }
8355 
8356 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8357   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8358     TypedefNameDecl *D
8359       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8360     if (D)
8361       Decls.push_back(D);
8362   }
8363   ExtVectorDecls.clear();
8364 }
8365 
8366 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8367     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8368   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8369        ++I) {
8370     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8371         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8372     if (D)
8373       Decls.insert(D);
8374   }
8375   UnusedLocalTypedefNameCandidates.clear();
8376 }
8377 
8378 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8379     llvm::SmallSetVector<Decl *, 4> &Decls) {
8380   for (auto I : DeclsToCheckForDeferredDiags) {
8381     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8382     if (D)
8383       Decls.insert(D);
8384   }
8385   DeclsToCheckForDeferredDiags.clear();
8386 }
8387 
8388 void ASTReader::ReadReferencedSelectors(
8389        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8390   if (ReferencedSelectorsData.empty())
8391     return;
8392 
8393   // If there are @selector references added them to its pool. This is for
8394   // implementation of -Wselector.
8395   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8396   unsigned I = 0;
8397   while (I < DataSize) {
8398     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8399     SourceLocation SelLoc
8400       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8401     Sels.push_back(std::make_pair(Sel, SelLoc));
8402   }
8403   ReferencedSelectorsData.clear();
8404 }
8405 
8406 void ASTReader::ReadWeakUndeclaredIdentifiers(
8407        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8408   if (WeakUndeclaredIdentifiers.empty())
8409     return;
8410 
8411   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8412     IdentifierInfo *WeakId
8413       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8414     IdentifierInfo *AliasId
8415       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8416     SourceLocation Loc =
8417         SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8418     WeakInfo WI(AliasId, Loc);
8419     WeakIDs.push_back(std::make_pair(WeakId, WI));
8420   }
8421   WeakUndeclaredIdentifiers.clear();
8422 }
8423 
8424 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8425   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8426     ExternalVTableUse VT;
8427     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8428     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8429     VT.DefinitionRequired = VTableUses[Idx++];
8430     VTables.push_back(VT);
8431   }
8432 
8433   VTableUses.clear();
8434 }
8435 
8436 void ASTReader::ReadPendingInstantiations(
8437        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8438   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8439     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8440     SourceLocation Loc
8441       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8442 
8443     Pending.push_back(std::make_pair(D, Loc));
8444   }
8445   PendingInstantiations.clear();
8446 }
8447 
8448 void ASTReader::ReadLateParsedTemplates(
8449     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8450         &LPTMap) {
8451   for (auto &LPT : LateParsedTemplates) {
8452     ModuleFile *FMod = LPT.first;
8453     RecordDataImpl &LateParsed = LPT.second;
8454     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8455          /* In loop */) {
8456       FunctionDecl *FD =
8457           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8458 
8459       auto LT = std::make_unique<LateParsedTemplate>();
8460       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8461 
8462       ModuleFile *F = getOwningModuleFile(LT->D);
8463       assert(F && "No module");
8464 
8465       unsigned TokN = LateParsed[Idx++];
8466       LT->Toks.reserve(TokN);
8467       for (unsigned T = 0; T < TokN; ++T)
8468         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8469 
8470       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8471     }
8472   }
8473 
8474   LateParsedTemplates.clear();
8475 }
8476 
8477 void ASTReader::LoadSelector(Selector Sel) {
8478   // It would be complicated to avoid reading the methods anyway. So don't.
8479   ReadMethodPool(Sel);
8480 }
8481 
8482 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8483   assert(ID && "Non-zero identifier ID required");
8484   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8485   IdentifiersLoaded[ID - 1] = II;
8486   if (DeserializationListener)
8487     DeserializationListener->IdentifierRead(ID, II);
8488 }
8489 
8490 /// Set the globally-visible declarations associated with the given
8491 /// identifier.
8492 ///
8493 /// If the AST reader is currently in a state where the given declaration IDs
8494 /// cannot safely be resolved, they are queued until it is safe to resolve
8495 /// them.
8496 ///
8497 /// \param II an IdentifierInfo that refers to one or more globally-visible
8498 /// declarations.
8499 ///
8500 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8501 /// visible at global scope.
8502 ///
8503 /// \param Decls if non-null, this vector will be populated with the set of
8504 /// deserialized declarations. These declarations will not be pushed into
8505 /// scope.
8506 void
8507 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8508                               const SmallVectorImpl<uint32_t> &DeclIDs,
8509                                    SmallVectorImpl<Decl *> *Decls) {
8510   if (NumCurrentElementsDeserializing && !Decls) {
8511     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8512     return;
8513   }
8514 
8515   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8516     if (!SemaObj) {
8517       // Queue this declaration so that it will be added to the
8518       // translation unit scope and identifier's declaration chain
8519       // once a Sema object is known.
8520       PreloadedDeclIDs.push_back(DeclIDs[I]);
8521       continue;
8522     }
8523 
8524     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8525 
8526     // If we're simply supposed to record the declarations, do so now.
8527     if (Decls) {
8528       Decls->push_back(D);
8529       continue;
8530     }
8531 
8532     // Introduce this declaration into the translation-unit scope
8533     // and add it to the declaration chain for this identifier, so
8534     // that (unqualified) name lookup will find it.
8535     pushExternalDeclIntoScope(D, II);
8536   }
8537 }
8538 
8539 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8540   if (ID == 0)
8541     return nullptr;
8542 
8543   if (IdentifiersLoaded.empty()) {
8544     Error("no identifier table in AST file");
8545     return nullptr;
8546   }
8547 
8548   ID -= 1;
8549   if (!IdentifiersLoaded[ID]) {
8550     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8551     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8552     ModuleFile *M = I->second;
8553     unsigned Index = ID - M->BaseIdentifierID;
8554     const unsigned char *Data =
8555         M->IdentifierTableData + M->IdentifierOffsets[Index];
8556 
8557     ASTIdentifierLookupTrait Trait(*this, *M);
8558     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8559     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8560     auto &II = PP.getIdentifierTable().get(Key);
8561     IdentifiersLoaded[ID] = &II;
8562     markIdentifierFromAST(*this,  II);
8563     if (DeserializationListener)
8564       DeserializationListener->IdentifierRead(ID + 1, &II);
8565   }
8566 
8567   return IdentifiersLoaded[ID];
8568 }
8569 
8570 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8571   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8572 }
8573 
8574 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8575   if (LocalID < NUM_PREDEF_IDENT_IDS)
8576     return LocalID;
8577 
8578   if (!M.ModuleOffsetMap.empty())
8579     ReadModuleOffsetMap(M);
8580 
8581   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8582     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8583   assert(I != M.IdentifierRemap.end()
8584          && "Invalid index into identifier index remap");
8585 
8586   return LocalID + I->second;
8587 }
8588 
8589 MacroInfo *ASTReader::getMacro(MacroID ID) {
8590   if (ID == 0)
8591     return nullptr;
8592 
8593   if (MacrosLoaded.empty()) {
8594     Error("no macro table in AST file");
8595     return nullptr;
8596   }
8597 
8598   ID -= NUM_PREDEF_MACRO_IDS;
8599   if (!MacrosLoaded[ID]) {
8600     GlobalMacroMapType::iterator I
8601       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8602     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8603     ModuleFile *M = I->second;
8604     unsigned Index = ID - M->BaseMacroID;
8605     MacrosLoaded[ID] =
8606         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8607 
8608     if (DeserializationListener)
8609       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8610                                          MacrosLoaded[ID]);
8611   }
8612 
8613   return MacrosLoaded[ID];
8614 }
8615 
8616 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8617   if (LocalID < NUM_PREDEF_MACRO_IDS)
8618     return LocalID;
8619 
8620   if (!M.ModuleOffsetMap.empty())
8621     ReadModuleOffsetMap(M);
8622 
8623   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8624     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8625   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8626 
8627   return LocalID + I->second;
8628 }
8629 
8630 serialization::SubmoduleID
8631 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8632   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8633     return LocalID;
8634 
8635   if (!M.ModuleOffsetMap.empty())
8636     ReadModuleOffsetMap(M);
8637 
8638   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8639     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8640   assert(I != M.SubmoduleRemap.end()
8641          && "Invalid index into submodule index remap");
8642 
8643   return LocalID + I->second;
8644 }
8645 
8646 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8647   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8648     assert(GlobalID == 0 && "Unhandled global submodule ID");
8649     return nullptr;
8650   }
8651 
8652   if (GlobalID > SubmodulesLoaded.size()) {
8653     Error("submodule ID out of range in AST file");
8654     return nullptr;
8655   }
8656 
8657   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8658 }
8659 
8660 Module *ASTReader::getModule(unsigned ID) {
8661   return getSubmodule(ID);
8662 }
8663 
8664 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8665   if (ID & 1) {
8666     // It's a module, look it up by submodule ID.
8667     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8668     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8669   } else {
8670     // It's a prefix (preamble, PCH, ...). Look it up by index.
8671     unsigned IndexFromEnd = ID >> 1;
8672     assert(IndexFromEnd && "got reference to unknown module file");
8673     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8674   }
8675 }
8676 
8677 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8678   if (!F)
8679     return 1;
8680 
8681   // For a file representing a module, use the submodule ID of the top-level
8682   // module as the file ID. For any other kind of file, the number of such
8683   // files loaded beforehand will be the same on reload.
8684   // FIXME: Is this true even if we have an explicit module file and a PCH?
8685   if (F->isModule())
8686     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8687 
8688   auto PCHModules = getModuleManager().pch_modules();
8689   auto I = llvm::find(PCHModules, F);
8690   assert(I != PCHModules.end() && "emitting reference to unknown file");
8691   return (I - PCHModules.end()) << 1;
8692 }
8693 
8694 llvm::Optional<ASTSourceDescriptor>
8695 ASTReader::getSourceDescriptor(unsigned ID) {
8696   if (Module *M = getSubmodule(ID))
8697     return ASTSourceDescriptor(*M);
8698 
8699   // If there is only a single PCH, return it instead.
8700   // Chained PCH are not supported.
8701   const auto &PCHChain = ModuleMgr.pch_modules();
8702   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8703     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8704     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8705     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8706     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8707                                MF.Signature);
8708   }
8709   return None;
8710 }
8711 
8712 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8713   auto I = DefinitionSource.find(FD);
8714   if (I == DefinitionSource.end())
8715     return EK_ReplyHazy;
8716   return I->second ? EK_Never : EK_Always;
8717 }
8718 
8719 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8720   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8721 }
8722 
8723 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8724   if (ID == 0)
8725     return Selector();
8726 
8727   if (ID > SelectorsLoaded.size()) {
8728     Error("selector ID out of range in AST file");
8729     return Selector();
8730   }
8731 
8732   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8733     // Load this selector from the selector table.
8734     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8735     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8736     ModuleFile &M = *I->second;
8737     ASTSelectorLookupTrait Trait(*this, M);
8738     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8739     SelectorsLoaded[ID - 1] =
8740       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8741     if (DeserializationListener)
8742       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8743   }
8744 
8745   return SelectorsLoaded[ID - 1];
8746 }
8747 
8748 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8749   return DecodeSelector(ID);
8750 }
8751 
8752 uint32_t ASTReader::GetNumExternalSelectors() {
8753   // ID 0 (the null selector) is considered an external selector.
8754   return getTotalNumSelectors() + 1;
8755 }
8756 
8757 serialization::SelectorID
8758 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8759   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8760     return LocalID;
8761 
8762   if (!M.ModuleOffsetMap.empty())
8763     ReadModuleOffsetMap(M);
8764 
8765   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8766     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8767   assert(I != M.SelectorRemap.end()
8768          && "Invalid index into selector index remap");
8769 
8770   return LocalID + I->second;
8771 }
8772 
8773 DeclarationNameLoc
8774 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8775   switch (Name.getNameKind()) {
8776   case DeclarationName::CXXConstructorName:
8777   case DeclarationName::CXXDestructorName:
8778   case DeclarationName::CXXConversionFunctionName:
8779     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8780 
8781   case DeclarationName::CXXOperatorName:
8782     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8783 
8784   case DeclarationName::CXXLiteralOperatorName:
8785     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8786         readSourceLocation());
8787 
8788   case DeclarationName::Identifier:
8789   case DeclarationName::ObjCZeroArgSelector:
8790   case DeclarationName::ObjCOneArgSelector:
8791   case DeclarationName::ObjCMultiArgSelector:
8792   case DeclarationName::CXXUsingDirective:
8793   case DeclarationName::CXXDeductionGuideName:
8794     break;
8795   }
8796   return DeclarationNameLoc();
8797 }
8798 
8799 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8800   DeclarationNameInfo NameInfo;
8801   NameInfo.setName(readDeclarationName());
8802   NameInfo.setLoc(readSourceLocation());
8803   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8804   return NameInfo;
8805 }
8806 
8807 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8808   Info.QualifierLoc = readNestedNameSpecifierLoc();
8809   unsigned NumTPLists = readInt();
8810   Info.NumTemplParamLists = NumTPLists;
8811   if (NumTPLists) {
8812     Info.TemplParamLists =
8813         new (getContext()) TemplateParameterList *[NumTPLists];
8814     for (unsigned i = 0; i != NumTPLists; ++i)
8815       Info.TemplParamLists[i] = readTemplateParameterList();
8816   }
8817 }
8818 
8819 TemplateParameterList *
8820 ASTRecordReader::readTemplateParameterList() {
8821   SourceLocation TemplateLoc = readSourceLocation();
8822   SourceLocation LAngleLoc = readSourceLocation();
8823   SourceLocation RAngleLoc = readSourceLocation();
8824 
8825   unsigned NumParams = readInt();
8826   SmallVector<NamedDecl *, 16> Params;
8827   Params.reserve(NumParams);
8828   while (NumParams--)
8829     Params.push_back(readDeclAs<NamedDecl>());
8830 
8831   bool HasRequiresClause = readBool();
8832   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8833 
8834   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8835       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8836   return TemplateParams;
8837 }
8838 
8839 void ASTRecordReader::readTemplateArgumentList(
8840                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8841                         bool Canonicalize) {
8842   unsigned NumTemplateArgs = readInt();
8843   TemplArgs.reserve(NumTemplateArgs);
8844   while (NumTemplateArgs--)
8845     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8846 }
8847 
8848 /// Read a UnresolvedSet structure.
8849 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8850   unsigned NumDecls = readInt();
8851   Set.reserve(getContext(), NumDecls);
8852   while (NumDecls--) {
8853     DeclID ID = readDeclID();
8854     AccessSpecifier AS = (AccessSpecifier) readInt();
8855     Set.addLazyDecl(getContext(), ID, AS);
8856   }
8857 }
8858 
8859 CXXBaseSpecifier
8860 ASTRecordReader::readCXXBaseSpecifier() {
8861   bool isVirtual = readBool();
8862   bool isBaseOfClass = readBool();
8863   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8864   bool inheritConstructors = readBool();
8865   TypeSourceInfo *TInfo = readTypeSourceInfo();
8866   SourceRange Range = readSourceRange();
8867   SourceLocation EllipsisLoc = readSourceLocation();
8868   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8869                           EllipsisLoc);
8870   Result.setInheritConstructors(inheritConstructors);
8871   return Result;
8872 }
8873 
8874 CXXCtorInitializer **
8875 ASTRecordReader::readCXXCtorInitializers() {
8876   ASTContext &Context = getContext();
8877   unsigned NumInitializers = readInt();
8878   assert(NumInitializers && "wrote ctor initializers but have no inits");
8879   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8880   for (unsigned i = 0; i != NumInitializers; ++i) {
8881     TypeSourceInfo *TInfo = nullptr;
8882     bool IsBaseVirtual = false;
8883     FieldDecl *Member = nullptr;
8884     IndirectFieldDecl *IndirectMember = nullptr;
8885 
8886     CtorInitializerType Type = (CtorInitializerType) readInt();
8887     switch (Type) {
8888     case CTOR_INITIALIZER_BASE:
8889       TInfo = readTypeSourceInfo();
8890       IsBaseVirtual = readBool();
8891       break;
8892 
8893     case CTOR_INITIALIZER_DELEGATING:
8894       TInfo = readTypeSourceInfo();
8895       break;
8896 
8897      case CTOR_INITIALIZER_MEMBER:
8898       Member = readDeclAs<FieldDecl>();
8899       break;
8900 
8901      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8902       IndirectMember = readDeclAs<IndirectFieldDecl>();
8903       break;
8904     }
8905 
8906     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8907     Expr *Init = readExpr();
8908     SourceLocation LParenLoc = readSourceLocation();
8909     SourceLocation RParenLoc = readSourceLocation();
8910 
8911     CXXCtorInitializer *BOMInit;
8912     if (Type == CTOR_INITIALIZER_BASE)
8913       BOMInit = new (Context)
8914           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8915                              RParenLoc, MemberOrEllipsisLoc);
8916     else if (Type == CTOR_INITIALIZER_DELEGATING)
8917       BOMInit = new (Context)
8918           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8919     else if (Member)
8920       BOMInit = new (Context)
8921           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8922                              Init, RParenLoc);
8923     else
8924       BOMInit = new (Context)
8925           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8926                              LParenLoc, Init, RParenLoc);
8927 
8928     if (/*IsWritten*/readBool()) {
8929       unsigned SourceOrder = readInt();
8930       BOMInit->setSourceOrder(SourceOrder);
8931     }
8932 
8933     CtorInitializers[i] = BOMInit;
8934   }
8935 
8936   return CtorInitializers;
8937 }
8938 
8939 NestedNameSpecifierLoc
8940 ASTRecordReader::readNestedNameSpecifierLoc() {
8941   ASTContext &Context = getContext();
8942   unsigned N = readInt();
8943   NestedNameSpecifierLocBuilder Builder;
8944   for (unsigned I = 0; I != N; ++I) {
8945     auto Kind = readNestedNameSpecifierKind();
8946     switch (Kind) {
8947     case NestedNameSpecifier::Identifier: {
8948       IdentifierInfo *II = readIdentifier();
8949       SourceRange Range = readSourceRange();
8950       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8951       break;
8952     }
8953 
8954     case NestedNameSpecifier::Namespace: {
8955       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8956       SourceRange Range = readSourceRange();
8957       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8958       break;
8959     }
8960 
8961     case NestedNameSpecifier::NamespaceAlias: {
8962       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8963       SourceRange Range = readSourceRange();
8964       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8965       break;
8966     }
8967 
8968     case NestedNameSpecifier::TypeSpec:
8969     case NestedNameSpecifier::TypeSpecWithTemplate: {
8970       bool Template = readBool();
8971       TypeSourceInfo *T = readTypeSourceInfo();
8972       if (!T)
8973         return NestedNameSpecifierLoc();
8974       SourceLocation ColonColonLoc = readSourceLocation();
8975 
8976       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8977       Builder.Extend(Context,
8978                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8979                      T->getTypeLoc(), ColonColonLoc);
8980       break;
8981     }
8982 
8983     case NestedNameSpecifier::Global: {
8984       SourceLocation ColonColonLoc = readSourceLocation();
8985       Builder.MakeGlobal(Context, ColonColonLoc);
8986       break;
8987     }
8988 
8989     case NestedNameSpecifier::Super: {
8990       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8991       SourceRange Range = readSourceRange();
8992       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8993       break;
8994     }
8995     }
8996   }
8997 
8998   return Builder.getWithLocInContext(Context);
8999 }
9000 
9001 SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9002                                        unsigned &Idx, LocSeq *Seq) {
9003   SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq);
9004   SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq);
9005   return SourceRange(beg, end);
9006 }
9007 
9008 /// Read a floating-point value
9009 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9010   return llvm::APFloat(Sem, readAPInt());
9011 }
9012 
9013 // Read a string
9014 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9015   unsigned Len = Record[Idx++];
9016   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9017   Idx += Len;
9018   return Result;
9019 }
9020 
9021 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9022                                 unsigned &Idx) {
9023   std::string Filename = ReadString(Record, Idx);
9024   ResolveImportedPath(F, Filename);
9025   return Filename;
9026 }
9027 
9028 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9029                                 const RecordData &Record, unsigned &Idx) {
9030   std::string Filename = ReadString(Record, Idx);
9031   if (!BaseDirectory.empty())
9032     ResolveImportedPath(Filename, BaseDirectory);
9033   return Filename;
9034 }
9035 
9036 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9037                                          unsigned &Idx) {
9038   unsigned Major = Record[Idx++];
9039   unsigned Minor = Record[Idx++];
9040   unsigned Subminor = Record[Idx++];
9041   if (Minor == 0)
9042     return VersionTuple(Major);
9043   if (Subminor == 0)
9044     return VersionTuple(Major, Minor - 1);
9045   return VersionTuple(Major, Minor - 1, Subminor - 1);
9046 }
9047 
9048 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9049                                           const RecordData &Record,
9050                                           unsigned &Idx) {
9051   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9052   return CXXTemporary::Create(getContext(), Decl);
9053 }
9054 
9055 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9056   return Diag(CurrentImportLoc, DiagID);
9057 }
9058 
9059 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9060   return Diags.Report(Loc, DiagID);
9061 }
9062 
9063 /// Retrieve the identifier table associated with the
9064 /// preprocessor.
9065 IdentifierTable &ASTReader::getIdentifierTable() {
9066   return PP.getIdentifierTable();
9067 }
9068 
9069 /// Record that the given ID maps to the given switch-case
9070 /// statement.
9071 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9072   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9073          "Already have a SwitchCase with this ID");
9074   (*CurrSwitchCaseStmts)[ID] = SC;
9075 }
9076 
9077 /// Retrieve the switch-case statement with the given ID.
9078 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9079   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9080   return (*CurrSwitchCaseStmts)[ID];
9081 }
9082 
9083 void ASTReader::ClearSwitchCaseIDs() {
9084   CurrSwitchCaseStmts->clear();
9085 }
9086 
9087 void ASTReader::ReadComments() {
9088   ASTContext &Context = getContext();
9089   std::vector<RawComment *> Comments;
9090   for (SmallVectorImpl<std::pair<BitstreamCursor,
9091                                  serialization::ModuleFile *>>::iterator
9092        I = CommentsCursors.begin(),
9093        E = CommentsCursors.end();
9094        I != E; ++I) {
9095     Comments.clear();
9096     BitstreamCursor &Cursor = I->first;
9097     serialization::ModuleFile &F = *I->second;
9098     SavedStreamPosition SavedPosition(Cursor);
9099 
9100     RecordData Record;
9101     while (true) {
9102       Expected<llvm::BitstreamEntry> MaybeEntry =
9103           Cursor.advanceSkippingSubblocks(
9104               BitstreamCursor::AF_DontPopBlockAtEnd);
9105       if (!MaybeEntry) {
9106         Error(MaybeEntry.takeError());
9107         return;
9108       }
9109       llvm::BitstreamEntry Entry = MaybeEntry.get();
9110 
9111       switch (Entry.Kind) {
9112       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9113       case llvm::BitstreamEntry::Error:
9114         Error("malformed block record in AST file");
9115         return;
9116       case llvm::BitstreamEntry::EndBlock:
9117         goto NextCursor;
9118       case llvm::BitstreamEntry::Record:
9119         // The interesting case.
9120         break;
9121       }
9122 
9123       // Read a record.
9124       Record.clear();
9125       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9126       if (!MaybeComment) {
9127         Error(MaybeComment.takeError());
9128         return;
9129       }
9130       switch ((CommentRecordTypes)MaybeComment.get()) {
9131       case COMMENTS_RAW_COMMENT: {
9132         unsigned Idx = 0;
9133         SourceRange SR = ReadSourceRange(F, Record, Idx);
9134         RawComment::CommentKind Kind =
9135             (RawComment::CommentKind) Record[Idx++];
9136         bool IsTrailingComment = Record[Idx++];
9137         bool IsAlmostTrailingComment = Record[Idx++];
9138         Comments.push_back(new (Context) RawComment(
9139             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9140         break;
9141       }
9142       }
9143     }
9144   NextCursor:
9145     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9146         FileToOffsetToComment;
9147     for (RawComment *C : Comments) {
9148       SourceLocation CommentLoc = C->getBeginLoc();
9149       if (CommentLoc.isValid()) {
9150         std::pair<FileID, unsigned> Loc =
9151             SourceMgr.getDecomposedLoc(CommentLoc);
9152         if (Loc.first.isValid())
9153           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9154       }
9155     }
9156   }
9157 }
9158 
9159 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9160                                 bool IncludeSystem, bool Complain,
9161                     llvm::function_ref<void(const serialization::InputFile &IF,
9162                                             bool isSystem)> Visitor) {
9163   unsigned NumUserInputs = MF.NumUserInputFiles;
9164   unsigned NumInputs = MF.InputFilesLoaded.size();
9165   assert(NumUserInputs <= NumInputs);
9166   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9167   for (unsigned I = 0; I < N; ++I) {
9168     bool IsSystem = I >= NumUserInputs;
9169     InputFile IF = getInputFile(MF, I+1, Complain);
9170     Visitor(IF, IsSystem);
9171   }
9172 }
9173 
9174 void ASTReader::visitTopLevelModuleMaps(
9175     serialization::ModuleFile &MF,
9176     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9177   unsigned NumInputs = MF.InputFilesLoaded.size();
9178   for (unsigned I = 0; I < NumInputs; ++I) {
9179     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9180     if (IFI.TopLevelModuleMap)
9181       // FIXME: This unnecessarily re-reads the InputFileInfo.
9182       if (auto FE = getInputFile(MF, I + 1).getFile())
9183         Visitor(FE);
9184   }
9185 }
9186 
9187 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9188   // If we know the owning module, use it.
9189   if (Module *M = D->getImportedOwningModule())
9190     return M->getFullModuleName();
9191 
9192   // Otherwise, use the name of the top-level module the decl is within.
9193   if (ModuleFile *M = getOwningModuleFile(D))
9194     return M->ModuleName;
9195 
9196   // Not from a module.
9197   return {};
9198 }
9199 
9200 void ASTReader::finishPendingActions() {
9201   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9202          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9203          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9204          !PendingUpdateRecords.empty() ||
9205          !PendingObjCExtensionIvarRedeclarations.empty()) {
9206     // If any identifiers with corresponding top-level declarations have
9207     // been loaded, load those declarations now.
9208     using TopLevelDeclsMap =
9209         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9210     TopLevelDeclsMap TopLevelDecls;
9211 
9212     while (!PendingIdentifierInfos.empty()) {
9213       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9214       SmallVector<uint32_t, 4> DeclIDs =
9215           std::move(PendingIdentifierInfos.back().second);
9216       PendingIdentifierInfos.pop_back();
9217 
9218       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9219     }
9220 
9221     // Load each function type that we deferred loading because it was a
9222     // deduced type that might refer to a local type declared within itself.
9223     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9224       auto *FD = PendingFunctionTypes[I].first;
9225       FD->setType(GetType(PendingFunctionTypes[I].second));
9226 
9227       // If we gave a function a deduced return type, remember that we need to
9228       // propagate that along the redeclaration chain.
9229       auto *DT = FD->getReturnType()->getContainedDeducedType();
9230       if (DT && DT->isDeduced())
9231         PendingDeducedTypeUpdates.insert(
9232             {FD->getCanonicalDecl(), FD->getReturnType()});
9233     }
9234     PendingFunctionTypes.clear();
9235 
9236     // For each decl chain that we wanted to complete while deserializing, mark
9237     // it as "still needs to be completed".
9238     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9239       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9240     }
9241     PendingIncompleteDeclChains.clear();
9242 
9243     // Load pending declaration chains.
9244     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9245       loadPendingDeclChain(PendingDeclChains[I].first,
9246                            PendingDeclChains[I].second);
9247     PendingDeclChains.clear();
9248 
9249     // Make the most recent of the top-level declarations visible.
9250     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9251            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9252       IdentifierInfo *II = TLD->first;
9253       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9254         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9255       }
9256     }
9257 
9258     // Load any pending macro definitions.
9259     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9260       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9261       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9262       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9263       // Initialize the macro history from chained-PCHs ahead of module imports.
9264       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9265            ++IDIdx) {
9266         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9267         if (!Info.M->isModule())
9268           resolvePendingMacro(II, Info);
9269       }
9270       // Handle module imports.
9271       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9272            ++IDIdx) {
9273         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9274         if (Info.M->isModule())
9275           resolvePendingMacro(II, Info);
9276       }
9277     }
9278     PendingMacroIDs.clear();
9279 
9280     // Wire up the DeclContexts for Decls that we delayed setting until
9281     // recursive loading is completed.
9282     while (!PendingDeclContextInfos.empty()) {
9283       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9284       PendingDeclContextInfos.pop_front();
9285       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9286       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9287       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9288     }
9289 
9290     // Perform any pending declaration updates.
9291     while (!PendingUpdateRecords.empty()) {
9292       auto Update = PendingUpdateRecords.pop_back_val();
9293       ReadingKindTracker ReadingKind(Read_Decl, *this);
9294       loadDeclUpdateRecords(Update);
9295     }
9296 
9297     while (!PendingObjCExtensionIvarRedeclarations.empty()) {
9298       auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
9299       auto DuplicateIvars =
9300           PendingObjCExtensionIvarRedeclarations.back().second;
9301       llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
9302       StructuralEquivalenceContext Ctx(
9303           ExtensionsPair.first->getASTContext(),
9304           ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
9305           StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
9306           /*Complain =*/false,
9307           /*ErrorOnTagTypeMismatch =*/true);
9308       if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {
9309         // Merge redeclared ivars with their predecessors.
9310         for (auto IvarPair : DuplicateIvars) {
9311           ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
9312           // Change semantic DeclContext but keep the lexical one.
9313           Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(),
9314                                     Ivar->getLexicalDeclContext(),
9315                                     getContext());
9316           getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());
9317         }
9318         // Invalidate duplicate extension and the cached ivar list.
9319         ExtensionsPair.first->setInvalidDecl();
9320         ExtensionsPair.second->getClassInterface()
9321             ->getDefinition()
9322             ->setIvarList(nullptr);
9323       } else {
9324         for (auto IvarPair : DuplicateIvars) {
9325           Diag(IvarPair.first->getLocation(),
9326                diag::err_duplicate_ivar_declaration)
9327               << IvarPair.first->getIdentifier();
9328           Diag(IvarPair.second->getLocation(), diag::note_previous_definition);
9329         }
9330       }
9331       PendingObjCExtensionIvarRedeclarations.pop_back();
9332     }
9333   }
9334 
9335   // At this point, all update records for loaded decls are in place, so any
9336   // fake class definitions should have become real.
9337   assert(PendingFakeDefinitionData.empty() &&
9338          "faked up a class definition but never saw the real one");
9339 
9340   // If we deserialized any C++ or Objective-C class definitions, any
9341   // Objective-C protocol definitions, or any redeclarable templates, make sure
9342   // that all redeclarations point to the definitions. Note that this can only
9343   // happen now, after the redeclaration chains have been fully wired.
9344   for (Decl *D : PendingDefinitions) {
9345     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9346       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9347         // Make sure that the TagType points at the definition.
9348         const_cast<TagType*>(TagT)->decl = TD;
9349       }
9350 
9351       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9352         for (auto *R = getMostRecentExistingDecl(RD); R;
9353              R = R->getPreviousDecl()) {
9354           assert((R == D) ==
9355                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9356                  "declaration thinks it's the definition but it isn't");
9357           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9358         }
9359       }
9360 
9361       continue;
9362     }
9363 
9364     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9365       // Make sure that the ObjCInterfaceType points at the definition.
9366       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9367         ->Decl = ID;
9368 
9369       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9370         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9371 
9372       continue;
9373     }
9374 
9375     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9376       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9377         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9378 
9379       continue;
9380     }
9381 
9382     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9383     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9384       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9385   }
9386   PendingDefinitions.clear();
9387 
9388   // Load the bodies of any functions or methods we've encountered. We do
9389   // this now (delayed) so that we can be sure that the declaration chains
9390   // have been fully wired up (hasBody relies on this).
9391   // FIXME: We shouldn't require complete redeclaration chains here.
9392   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9393                                PBEnd = PendingBodies.end();
9394        PB != PBEnd; ++PB) {
9395     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9396       // For a function defined inline within a class template, force the
9397       // canonical definition to be the one inside the canonical definition of
9398       // the template. This ensures that we instantiate from a correct view
9399       // of the template.
9400       //
9401       // Sadly we can't do this more generally: we can't be sure that all
9402       // copies of an arbitrary class definition will have the same members
9403       // defined (eg, some member functions may not be instantiated, and some
9404       // special members may or may not have been implicitly defined).
9405       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9406         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9407           continue;
9408 
9409       // FIXME: Check for =delete/=default?
9410       // FIXME: Complain about ODR violations here?
9411       const FunctionDecl *Defn = nullptr;
9412       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9413         FD->setLazyBody(PB->second);
9414       } else {
9415         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9416         mergeDefinitionVisibility(NonConstDefn, FD);
9417 
9418         if (!FD->isLateTemplateParsed() &&
9419             !NonConstDefn->isLateTemplateParsed() &&
9420             FD->getODRHash() != NonConstDefn->getODRHash()) {
9421           if (!isa<CXXMethodDecl>(FD)) {
9422             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9423           } else if (FD->getLexicalParent()->isFileContext() &&
9424                      NonConstDefn->getLexicalParent()->isFileContext()) {
9425             // Only diagnose out-of-line method definitions.  If they are
9426             // in class definitions, then an error will be generated when
9427             // processing the class bodies.
9428             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9429           }
9430         }
9431       }
9432       continue;
9433     }
9434 
9435     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9436     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9437       MD->setLazyBody(PB->second);
9438   }
9439   PendingBodies.clear();
9440 
9441   // Do some cleanup.
9442   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9443     getContext().deduplicateMergedDefinitonsFor(ND);
9444   PendingMergedDefinitionsToDeduplicate.clear();
9445 }
9446 
9447 void ASTReader::diagnoseOdrViolations() {
9448   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9449       PendingFunctionOdrMergeFailures.empty() &&
9450       PendingEnumOdrMergeFailures.empty())
9451     return;
9452 
9453   // Trigger the import of the full definition of each class that had any
9454   // odr-merging problems, so we can produce better diagnostics for them.
9455   // These updates may in turn find and diagnose some ODR failures, so take
9456   // ownership of the set first.
9457   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9458   PendingOdrMergeFailures.clear();
9459   for (auto &Merge : OdrMergeFailures) {
9460     Merge.first->buildLookup();
9461     Merge.first->decls_begin();
9462     Merge.first->bases_begin();
9463     Merge.first->vbases_begin();
9464     for (auto &RecordPair : Merge.second) {
9465       auto *RD = RecordPair.first;
9466       RD->decls_begin();
9467       RD->bases_begin();
9468       RD->vbases_begin();
9469     }
9470   }
9471 
9472   // Trigger the import of functions.
9473   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9474   PendingFunctionOdrMergeFailures.clear();
9475   for (auto &Merge : FunctionOdrMergeFailures) {
9476     Merge.first->buildLookup();
9477     Merge.first->decls_begin();
9478     Merge.first->getBody();
9479     for (auto &FD : Merge.second) {
9480       FD->buildLookup();
9481       FD->decls_begin();
9482       FD->getBody();
9483     }
9484   }
9485 
9486   // Trigger the import of enums.
9487   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9488   PendingEnumOdrMergeFailures.clear();
9489   for (auto &Merge : EnumOdrMergeFailures) {
9490     Merge.first->decls_begin();
9491     for (auto &Enum : Merge.second) {
9492       Enum->decls_begin();
9493     }
9494   }
9495 
9496   // For each declaration from a merged context, check that the canonical
9497   // definition of that context also contains a declaration of the same
9498   // entity.
9499   //
9500   // Caution: this loop does things that might invalidate iterators into
9501   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9502   while (!PendingOdrMergeChecks.empty()) {
9503     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9504 
9505     // FIXME: Skip over implicit declarations for now. This matters for things
9506     // like implicitly-declared special member functions. This isn't entirely
9507     // correct; we can end up with multiple unmerged declarations of the same
9508     // implicit entity.
9509     if (D->isImplicit())
9510       continue;
9511 
9512     DeclContext *CanonDef = D->getDeclContext();
9513 
9514     bool Found = false;
9515     const Decl *DCanon = D->getCanonicalDecl();
9516 
9517     for (auto RI : D->redecls()) {
9518       if (RI->getLexicalDeclContext() == CanonDef) {
9519         Found = true;
9520         break;
9521       }
9522     }
9523     if (Found)
9524       continue;
9525 
9526     // Quick check failed, time to do the slow thing. Note, we can't just
9527     // look up the name of D in CanonDef here, because the member that is
9528     // in CanonDef might not be found by name lookup (it might have been
9529     // replaced by a more recent declaration in the lookup table), and we
9530     // can't necessarily find it in the redeclaration chain because it might
9531     // be merely mergeable, not redeclarable.
9532     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9533     for (auto *CanonMember : CanonDef->decls()) {
9534       if (CanonMember->getCanonicalDecl() == DCanon) {
9535         // This can happen if the declaration is merely mergeable and not
9536         // actually redeclarable (we looked for redeclarations earlier).
9537         //
9538         // FIXME: We should be able to detect this more efficiently, without
9539         // pulling in all of the members of CanonDef.
9540         Found = true;
9541         break;
9542       }
9543       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9544         if (ND->getDeclName() == D->getDeclName())
9545           Candidates.push_back(ND);
9546     }
9547 
9548     if (!Found) {
9549       // The AST doesn't like TagDecls becoming invalid after they've been
9550       // completed. We only really need to mark FieldDecls as invalid here.
9551       if (!isa<TagDecl>(D))
9552         D->setInvalidDecl();
9553 
9554       // Ensure we don't accidentally recursively enter deserialization while
9555       // we're producing our diagnostic.
9556       Deserializing RecursionGuard(this);
9557 
9558       std::string CanonDefModule =
9559           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9560       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9561         << D << getOwningModuleNameForDiagnostic(D)
9562         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9563 
9564       if (Candidates.empty())
9565         Diag(cast<Decl>(CanonDef)->getLocation(),
9566              diag::note_module_odr_violation_no_possible_decls) << D;
9567       else {
9568         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9569           Diag(Candidates[I]->getLocation(),
9570                diag::note_module_odr_violation_possible_decl)
9571             << Candidates[I];
9572       }
9573 
9574       DiagnosedOdrMergeFailures.insert(CanonDef);
9575     }
9576   }
9577 
9578   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9579       EnumOdrMergeFailures.empty())
9580     return;
9581 
9582   // Ensure we don't accidentally recursively enter deserialization while
9583   // we're producing our diagnostics.
9584   Deserializing RecursionGuard(this);
9585 
9586   // Common code for hashing helpers.
9587   ODRHash Hash;
9588   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9589     Hash.clear();
9590     Hash.AddQualType(Ty);
9591     return Hash.CalculateHash();
9592   };
9593 
9594   auto ComputeODRHash = [&Hash](const Stmt *S) {
9595     assert(S);
9596     Hash.clear();
9597     Hash.AddStmt(S);
9598     return Hash.CalculateHash();
9599   };
9600 
9601   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9602     assert(D);
9603     Hash.clear();
9604     Hash.AddSubDecl(D);
9605     return Hash.CalculateHash();
9606   };
9607 
9608   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9609     Hash.clear();
9610     Hash.AddTemplateArgument(TA);
9611     return Hash.CalculateHash();
9612   };
9613 
9614   auto ComputeTemplateParameterListODRHash =
9615       [&Hash](const TemplateParameterList *TPL) {
9616         assert(TPL);
9617         Hash.clear();
9618         Hash.AddTemplateParameterList(TPL);
9619         return Hash.CalculateHash();
9620       };
9621 
9622   // Used with err_module_odr_violation_mismatch_decl and
9623   // note_module_odr_violation_mismatch_decl
9624   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9625   enum ODRMismatchDecl {
9626     EndOfClass,
9627     PublicSpecifer,
9628     PrivateSpecifer,
9629     ProtectedSpecifer,
9630     StaticAssert,
9631     Field,
9632     CXXMethod,
9633     TypeAlias,
9634     TypeDef,
9635     Var,
9636     Friend,
9637     FunctionTemplate,
9638     Other
9639   };
9640 
9641   // Used with err_module_odr_violation_mismatch_decl_diff and
9642   // note_module_odr_violation_mismatch_decl_diff
9643   enum ODRMismatchDeclDifference {
9644     StaticAssertCondition,
9645     StaticAssertMessage,
9646     StaticAssertOnlyMessage,
9647     FieldName,
9648     FieldTypeName,
9649     FieldSingleBitField,
9650     FieldDifferentWidthBitField,
9651     FieldSingleMutable,
9652     FieldSingleInitializer,
9653     FieldDifferentInitializers,
9654     MethodName,
9655     MethodDeleted,
9656     MethodDefaulted,
9657     MethodVirtual,
9658     MethodStatic,
9659     MethodVolatile,
9660     MethodConst,
9661     MethodInline,
9662     MethodNumberParameters,
9663     MethodParameterType,
9664     MethodParameterName,
9665     MethodParameterSingleDefaultArgument,
9666     MethodParameterDifferentDefaultArgument,
9667     MethodNoTemplateArguments,
9668     MethodDifferentNumberTemplateArguments,
9669     MethodDifferentTemplateArgument,
9670     MethodSingleBody,
9671     MethodDifferentBody,
9672     TypedefName,
9673     TypedefType,
9674     VarName,
9675     VarType,
9676     VarSingleInitializer,
9677     VarDifferentInitializer,
9678     VarConstexpr,
9679     FriendTypeFunction,
9680     FriendType,
9681     FriendFunction,
9682     FunctionTemplateDifferentNumberParameters,
9683     FunctionTemplateParameterDifferentKind,
9684     FunctionTemplateParameterName,
9685     FunctionTemplateParameterSingleDefaultArgument,
9686     FunctionTemplateParameterDifferentDefaultArgument,
9687     FunctionTemplateParameterDifferentType,
9688     FunctionTemplatePackParameter,
9689   };
9690 
9691   // These lambdas have the common portions of the ODR diagnostics.  This
9692   // has the same return as Diag(), so addition parameters can be passed
9693   // in with operator<<
9694   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9695                                  SourceLocation Loc, SourceRange Range,
9696                                  ODRMismatchDeclDifference DiffType) {
9697     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9698            << FirstRecord << FirstModule.empty() << FirstModule << Range
9699            << DiffType;
9700   };
9701   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9702                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9703     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9704            << SecondModule << Range << DiffType;
9705   };
9706 
9707   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9708                        &ComputeQualTypeODRHash, &ComputeODRHash](
9709                           NamedDecl *FirstRecord, StringRef FirstModule,
9710                           StringRef SecondModule, FieldDecl *FirstField,
9711                           FieldDecl *SecondField) {
9712     IdentifierInfo *FirstII = FirstField->getIdentifier();
9713     IdentifierInfo *SecondII = SecondField->getIdentifier();
9714     if (FirstII->getName() != SecondII->getName()) {
9715       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9716                        FirstField->getSourceRange(), FieldName)
9717           << FirstII;
9718       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9719                       SecondField->getSourceRange(), FieldName)
9720           << SecondII;
9721 
9722       return true;
9723     }
9724 
9725     assert(getContext().hasSameType(FirstField->getType(),
9726                                     SecondField->getType()));
9727 
9728     QualType FirstType = FirstField->getType();
9729     QualType SecondType = SecondField->getType();
9730     if (ComputeQualTypeODRHash(FirstType) !=
9731         ComputeQualTypeODRHash(SecondType)) {
9732       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9733                        FirstField->getSourceRange(), FieldTypeName)
9734           << FirstII << FirstType;
9735       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9736                       SecondField->getSourceRange(), FieldTypeName)
9737           << SecondII << SecondType;
9738 
9739       return true;
9740     }
9741 
9742     const bool IsFirstBitField = FirstField->isBitField();
9743     const bool IsSecondBitField = SecondField->isBitField();
9744     if (IsFirstBitField != IsSecondBitField) {
9745       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9746                        FirstField->getSourceRange(), FieldSingleBitField)
9747           << FirstII << IsFirstBitField;
9748       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9749                       SecondField->getSourceRange(), FieldSingleBitField)
9750           << SecondII << IsSecondBitField;
9751       return true;
9752     }
9753 
9754     if (IsFirstBitField && IsSecondBitField) {
9755       unsigned FirstBitWidthHash =
9756           ComputeODRHash(FirstField->getBitWidth());
9757       unsigned SecondBitWidthHash =
9758           ComputeODRHash(SecondField->getBitWidth());
9759       if (FirstBitWidthHash != SecondBitWidthHash) {
9760         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9761                          FirstField->getSourceRange(),
9762                          FieldDifferentWidthBitField)
9763             << FirstII << FirstField->getBitWidth()->getSourceRange();
9764         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9765                         SecondField->getSourceRange(),
9766                         FieldDifferentWidthBitField)
9767             << SecondII << SecondField->getBitWidth()->getSourceRange();
9768         return true;
9769       }
9770     }
9771 
9772     if (!PP.getLangOpts().CPlusPlus)
9773       return false;
9774 
9775     const bool IsFirstMutable = FirstField->isMutable();
9776     const bool IsSecondMutable = SecondField->isMutable();
9777     if (IsFirstMutable != IsSecondMutable) {
9778       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9779                        FirstField->getSourceRange(), FieldSingleMutable)
9780           << FirstII << IsFirstMutable;
9781       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9782                       SecondField->getSourceRange(), FieldSingleMutable)
9783           << SecondII << IsSecondMutable;
9784       return true;
9785     }
9786 
9787     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9788     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9789     if ((!FirstInitializer && SecondInitializer) ||
9790         (FirstInitializer && !SecondInitializer)) {
9791       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9792                        FirstField->getSourceRange(), FieldSingleInitializer)
9793           << FirstII << (FirstInitializer != nullptr);
9794       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9795                       SecondField->getSourceRange(), FieldSingleInitializer)
9796           << SecondII << (SecondInitializer != nullptr);
9797       return true;
9798     }
9799 
9800     if (FirstInitializer && SecondInitializer) {
9801       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9802       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9803       if (FirstInitHash != SecondInitHash) {
9804         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9805                          FirstField->getSourceRange(),
9806                          FieldDifferentInitializers)
9807             << FirstII << FirstInitializer->getSourceRange();
9808         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9809                         SecondField->getSourceRange(),
9810                         FieldDifferentInitializers)
9811             << SecondII << SecondInitializer->getSourceRange();
9812         return true;
9813       }
9814     }
9815 
9816     return false;
9817   };
9818 
9819   auto ODRDiagTypeDefOrAlias =
9820       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9821           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9822           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9823           bool IsTypeAlias) {
9824         auto FirstName = FirstTD->getDeclName();
9825         auto SecondName = SecondTD->getDeclName();
9826         if (FirstName != SecondName) {
9827           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9828                            FirstTD->getSourceRange(), TypedefName)
9829               << IsTypeAlias << FirstName;
9830           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9831                           SecondTD->getSourceRange(), TypedefName)
9832               << IsTypeAlias << SecondName;
9833           return true;
9834         }
9835 
9836         QualType FirstType = FirstTD->getUnderlyingType();
9837         QualType SecondType = SecondTD->getUnderlyingType();
9838         if (ComputeQualTypeODRHash(FirstType) !=
9839             ComputeQualTypeODRHash(SecondType)) {
9840           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9841                            FirstTD->getSourceRange(), TypedefType)
9842               << IsTypeAlias << FirstName << FirstType;
9843           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9844                           SecondTD->getSourceRange(), TypedefType)
9845               << IsTypeAlias << SecondName << SecondType;
9846           return true;
9847         }
9848 
9849         return false;
9850   };
9851 
9852   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9853                      &ComputeQualTypeODRHash, &ComputeODRHash,
9854                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9855                            StringRef SecondModule, VarDecl *FirstVD,
9856                            VarDecl *SecondVD) {
9857     auto FirstName = FirstVD->getDeclName();
9858     auto SecondName = SecondVD->getDeclName();
9859     if (FirstName != SecondName) {
9860       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9861                        FirstVD->getSourceRange(), VarName)
9862           << FirstName;
9863       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9864                       SecondVD->getSourceRange(), VarName)
9865           << SecondName;
9866       return true;
9867     }
9868 
9869     QualType FirstType = FirstVD->getType();
9870     QualType SecondType = SecondVD->getType();
9871     if (ComputeQualTypeODRHash(FirstType) !=
9872         ComputeQualTypeODRHash(SecondType)) {
9873       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9874                        FirstVD->getSourceRange(), VarType)
9875           << FirstName << FirstType;
9876       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9877                       SecondVD->getSourceRange(), VarType)
9878           << SecondName << SecondType;
9879       return true;
9880     }
9881 
9882     if (!PP.getLangOpts().CPlusPlus)
9883       return false;
9884 
9885     const Expr *FirstInit = FirstVD->getInit();
9886     const Expr *SecondInit = SecondVD->getInit();
9887     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9888       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9889                        FirstVD->getSourceRange(), VarSingleInitializer)
9890           << FirstName << (FirstInit == nullptr)
9891           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9892       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9893                       SecondVD->getSourceRange(), VarSingleInitializer)
9894           << SecondName << (SecondInit == nullptr)
9895           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9896       return true;
9897     }
9898 
9899     if (FirstInit && SecondInit &&
9900         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9901       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9902                        FirstVD->getSourceRange(), VarDifferentInitializer)
9903           << FirstName << FirstInit->getSourceRange();
9904       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9905                       SecondVD->getSourceRange(), VarDifferentInitializer)
9906           << SecondName << SecondInit->getSourceRange();
9907       return true;
9908     }
9909 
9910     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9911     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9912     if (FirstIsConstexpr != SecondIsConstexpr) {
9913       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9914                        FirstVD->getSourceRange(), VarConstexpr)
9915           << FirstName << FirstIsConstexpr;
9916       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9917                       SecondVD->getSourceRange(), VarConstexpr)
9918           << SecondName << SecondIsConstexpr;
9919       return true;
9920     }
9921     return false;
9922   };
9923 
9924   auto DifferenceSelector = [](Decl *D) {
9925     assert(D && "valid Decl required");
9926     switch (D->getKind()) {
9927     default:
9928       return Other;
9929     case Decl::AccessSpec:
9930       switch (D->getAccess()) {
9931       case AS_public:
9932         return PublicSpecifer;
9933       case AS_private:
9934         return PrivateSpecifer;
9935       case AS_protected:
9936         return ProtectedSpecifer;
9937       case AS_none:
9938         break;
9939       }
9940       llvm_unreachable("Invalid access specifier");
9941     case Decl::StaticAssert:
9942       return StaticAssert;
9943     case Decl::Field:
9944       return Field;
9945     case Decl::CXXMethod:
9946     case Decl::CXXConstructor:
9947     case Decl::CXXDestructor:
9948       return CXXMethod;
9949     case Decl::TypeAlias:
9950       return TypeAlias;
9951     case Decl::Typedef:
9952       return TypeDef;
9953     case Decl::Var:
9954       return Var;
9955     case Decl::Friend:
9956       return Friend;
9957     case Decl::FunctionTemplate:
9958       return FunctionTemplate;
9959     }
9960   };
9961 
9962   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9963   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9964                                                  RecordDecl *Record,
9965                                                  const DeclContext *DC) {
9966     for (auto *D : Record->decls()) {
9967       if (!ODRHash::isDeclToBeProcessed(D, DC))
9968         continue;
9969       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9970     }
9971   };
9972 
9973   struct DiffResult {
9974     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9975     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9976   };
9977 
9978   // If there is a diagnoseable difference, FirstDiffType and
9979   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9980   // filled in if not EndOfClass.
9981   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9982                                              DeclHashes &SecondHashes) {
9983     DiffResult DR;
9984     auto FirstIt = FirstHashes.begin();
9985     auto SecondIt = SecondHashes.begin();
9986     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9987       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9988           FirstIt->second == SecondIt->second) {
9989         ++FirstIt;
9990         ++SecondIt;
9991         continue;
9992       }
9993 
9994       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9995       DR.SecondDecl =
9996           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9997 
9998       DR.FirstDiffType =
9999           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
10000       DR.SecondDiffType =
10001           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
10002       return DR;
10003     }
10004     return DR;
10005   };
10006 
10007   // Use this to diagnose that an unexpected Decl was encountered
10008   // or no difference was detected. This causes a generic error
10009   // message to be emitted.
10010   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
10011                                       StringRef FirstModule,
10012                                       NamedDecl *SecondRecord,
10013                                       StringRef SecondModule) {
10014     Diag(FirstRecord->getLocation(),
10015          diag::err_module_odr_violation_different_definitions)
10016         << FirstRecord << FirstModule.empty() << FirstModule;
10017 
10018     if (DR.FirstDecl) {
10019       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
10020           << FirstRecord << DR.FirstDecl->getSourceRange();
10021     }
10022 
10023     Diag(SecondRecord->getLocation(),
10024          diag::note_module_odr_violation_different_definitions)
10025         << SecondModule;
10026 
10027     if (DR.SecondDecl) {
10028       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
10029           << DR.SecondDecl->getSourceRange();
10030     }
10031   };
10032 
10033   auto DiagnoseODRMismatch =
10034       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10035              NamedDecl *SecondRecord, StringRef SecondModule) {
10036         SourceLocation FirstLoc;
10037         SourceRange FirstRange;
10038         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10039         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10040           FirstLoc = FirstTag->getBraceRange().getEnd();
10041         } else {
10042           FirstLoc = DR.FirstDecl->getLocation();
10043           FirstRange = DR.FirstDecl->getSourceRange();
10044         }
10045         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10046             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10047             << DR.FirstDiffType;
10048 
10049         SourceLocation SecondLoc;
10050         SourceRange SecondRange;
10051         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10052         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10053           SecondLoc = SecondTag->getBraceRange().getEnd();
10054         } else {
10055           SecondLoc = DR.SecondDecl->getLocation();
10056           SecondRange = DR.SecondDecl->getSourceRange();
10057         }
10058         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10059             << SecondModule << SecondRange << DR.SecondDiffType;
10060       };
10061 
10062   // Issue any pending ODR-failure diagnostics.
10063   for (auto &Merge : OdrMergeFailures) {
10064     // If we've already pointed out a specific problem with this class, don't
10065     // bother issuing a general "something's different" diagnostic.
10066     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10067       continue;
10068 
10069     bool Diagnosed = false;
10070     CXXRecordDecl *FirstRecord = Merge.first;
10071     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10072     for (auto &RecordPair : Merge.second) {
10073       CXXRecordDecl *SecondRecord = RecordPair.first;
10074       // Multiple different declarations got merged together; tell the user
10075       // where they came from.
10076       if (FirstRecord == SecondRecord)
10077         continue;
10078 
10079       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10080 
10081       auto *FirstDD = FirstRecord->DefinitionData;
10082       auto *SecondDD = RecordPair.second;
10083 
10084       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10085 
10086       // Diagnostics from DefinitionData are emitted here.
10087       if (FirstDD != SecondDD) {
10088         enum ODRDefinitionDataDifference {
10089           NumBases,
10090           NumVBases,
10091           BaseType,
10092           BaseVirtual,
10093           BaseAccess,
10094         };
10095         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10096                                  this](SourceLocation Loc, SourceRange Range,
10097                                        ODRDefinitionDataDifference DiffType) {
10098           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10099                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10100                  << DiffType;
10101         };
10102         auto ODRDiagBaseNote = [&SecondModule,
10103                                 this](SourceLocation Loc, SourceRange Range,
10104                                       ODRDefinitionDataDifference DiffType) {
10105           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10106                  << SecondModule << Range << DiffType;
10107         };
10108 
10109         unsigned FirstNumBases = FirstDD->NumBases;
10110         unsigned FirstNumVBases = FirstDD->NumVBases;
10111         unsigned SecondNumBases = SecondDD->NumBases;
10112         unsigned SecondNumVBases = SecondDD->NumVBases;
10113 
10114         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10115           unsigned NumBases = DD->NumBases;
10116           if (NumBases == 0) return SourceRange();
10117           auto bases = DD->bases();
10118           return SourceRange(bases[0].getBeginLoc(),
10119                              bases[NumBases - 1].getEndLoc());
10120         };
10121 
10122         if (FirstNumBases != SecondNumBases) {
10123           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10124                            NumBases)
10125               << FirstNumBases;
10126           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10127                           NumBases)
10128               << SecondNumBases;
10129           Diagnosed = true;
10130           break;
10131         }
10132 
10133         if (FirstNumVBases != SecondNumVBases) {
10134           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10135                            NumVBases)
10136               << FirstNumVBases;
10137           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10138                           NumVBases)
10139               << SecondNumVBases;
10140           Diagnosed = true;
10141           break;
10142         }
10143 
10144         auto FirstBases = FirstDD->bases();
10145         auto SecondBases = SecondDD->bases();
10146         unsigned i = 0;
10147         for (i = 0; i < FirstNumBases; ++i) {
10148           auto FirstBase = FirstBases[i];
10149           auto SecondBase = SecondBases[i];
10150           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10151               ComputeQualTypeODRHash(SecondBase.getType())) {
10152             ODRDiagBaseError(FirstRecord->getLocation(),
10153                              FirstBase.getSourceRange(), BaseType)
10154                 << (i + 1) << FirstBase.getType();
10155             ODRDiagBaseNote(SecondRecord->getLocation(),
10156                             SecondBase.getSourceRange(), BaseType)
10157                 << (i + 1) << SecondBase.getType();
10158             break;
10159           }
10160 
10161           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10162             ODRDiagBaseError(FirstRecord->getLocation(),
10163                              FirstBase.getSourceRange(), BaseVirtual)
10164                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10165             ODRDiagBaseNote(SecondRecord->getLocation(),
10166                             SecondBase.getSourceRange(), BaseVirtual)
10167                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10168             break;
10169           }
10170 
10171           if (FirstBase.getAccessSpecifierAsWritten() !=
10172               SecondBase.getAccessSpecifierAsWritten()) {
10173             ODRDiagBaseError(FirstRecord->getLocation(),
10174                              FirstBase.getSourceRange(), BaseAccess)
10175                 << (i + 1) << FirstBase.getType()
10176                 << (int)FirstBase.getAccessSpecifierAsWritten();
10177             ODRDiagBaseNote(SecondRecord->getLocation(),
10178                             SecondBase.getSourceRange(), BaseAccess)
10179                 << (i + 1) << SecondBase.getType()
10180                 << (int)SecondBase.getAccessSpecifierAsWritten();
10181             break;
10182           }
10183         }
10184 
10185         if (i != FirstNumBases) {
10186           Diagnosed = true;
10187           break;
10188         }
10189       }
10190 
10191       const ClassTemplateDecl *FirstTemplate =
10192           FirstRecord->getDescribedClassTemplate();
10193       const ClassTemplateDecl *SecondTemplate =
10194           SecondRecord->getDescribedClassTemplate();
10195 
10196       assert(!FirstTemplate == !SecondTemplate &&
10197              "Both pointers should be null or non-null");
10198 
10199       if (FirstTemplate && SecondTemplate) {
10200         DeclHashes FirstTemplateHashes;
10201         DeclHashes SecondTemplateHashes;
10202 
10203         auto PopulateTemplateParameterHashs =
10204             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10205                                      const ClassTemplateDecl *TD) {
10206               for (auto *D : TD->getTemplateParameters()->asArray()) {
10207                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10208               }
10209             };
10210 
10211         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10212         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10213 
10214         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10215                "Number of template parameters should be equal.");
10216 
10217         auto FirstIt = FirstTemplateHashes.begin();
10218         auto FirstEnd = FirstTemplateHashes.end();
10219         auto SecondIt = SecondTemplateHashes.begin();
10220         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10221           if (FirstIt->second == SecondIt->second)
10222             continue;
10223 
10224           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10225           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10226 
10227           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10228                  "Parameter Decl's should be the same kind.");
10229 
10230           enum ODRTemplateDifference {
10231             ParamEmptyName,
10232             ParamName,
10233             ParamSingleDefaultArgument,
10234             ParamDifferentDefaultArgument,
10235           };
10236 
10237           auto hasDefaultArg = [](const NamedDecl *D) {
10238             if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
10239               return TTP->hasDefaultArgument() &&
10240                       !TTP->defaultArgumentWasInherited();
10241             if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
10242               return NTTP->hasDefaultArgument() &&
10243                       !NTTP->defaultArgumentWasInherited();
10244             auto *TTP = cast<TemplateTemplateParmDecl>(D);
10245             return TTP->hasDefaultArgument() &&
10246                     !TTP->defaultArgumentWasInherited();
10247           };
10248           bool hasFirstArg = hasDefaultArg(FirstDecl);
10249           bool hasSecondArg = hasDefaultArg(SecondDecl);
10250 
10251           ODRTemplateDifference ErrDiffType;
10252           ODRTemplateDifference NoteDiffType;
10253 
10254           DeclarationName FirstName = FirstDecl->getDeclName();
10255           DeclarationName SecondName = SecondDecl->getDeclName();
10256 
10257           if (FirstName != SecondName) {
10258             bool FirstNameEmpty =
10259                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10260             bool SecondNameEmpty = SecondName.isIdentifier() &&
10261                                     !SecondName.getAsIdentifierInfo();
10262             ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName;
10263             NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName;
10264           } else if (hasFirstArg == hasSecondArg)
10265             ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument;
10266           else
10267             ErrDiffType = NoteDiffType = ParamSingleDefaultArgument;
10268 
10269           Diag(FirstDecl->getLocation(),
10270                 diag::err_module_odr_violation_template_parameter)
10271               << FirstRecord << FirstModule.empty() << FirstModule
10272               << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg
10273               << FirstName;
10274           Diag(SecondDecl->getLocation(),
10275                 diag::note_module_odr_violation_template_parameter)
10276               << SecondModule << SecondDecl->getSourceRange() << NoteDiffType
10277               << hasSecondArg << SecondName;
10278           break;
10279         }
10280 
10281         if (FirstIt != FirstEnd) {
10282           Diagnosed = true;
10283           break;
10284         }
10285       }
10286 
10287       DeclHashes FirstHashes;
10288       DeclHashes SecondHashes;
10289       const DeclContext *DC = FirstRecord;
10290       PopulateHashes(FirstHashes, FirstRecord, DC);
10291       PopulateHashes(SecondHashes, SecondRecord, DC);
10292 
10293       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10294       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10295       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10296       Decl *FirstDecl = DR.FirstDecl;
10297       Decl *SecondDecl = DR.SecondDecl;
10298 
10299       if (FirstDiffType == Other || SecondDiffType == Other) {
10300         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10301                               SecondModule);
10302         Diagnosed = true;
10303         break;
10304       }
10305 
10306       if (FirstDiffType != SecondDiffType) {
10307         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10308                             SecondModule);
10309         Diagnosed = true;
10310         break;
10311       }
10312 
10313       assert(FirstDiffType == SecondDiffType);
10314 
10315       switch (FirstDiffType) {
10316       case Other:
10317       case EndOfClass:
10318       case PublicSpecifer:
10319       case PrivateSpecifer:
10320       case ProtectedSpecifer:
10321         llvm_unreachable("Invalid diff type");
10322 
10323       case StaticAssert: {
10324         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10325         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10326 
10327         Expr *FirstExpr = FirstSA->getAssertExpr();
10328         Expr *SecondExpr = SecondSA->getAssertExpr();
10329         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10330         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10331         if (FirstODRHash != SecondODRHash) {
10332           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10333                            FirstExpr->getSourceRange(), StaticAssertCondition);
10334           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10335                           SecondExpr->getSourceRange(), StaticAssertCondition);
10336           Diagnosed = true;
10337           break;
10338         }
10339 
10340         StringLiteral *FirstStr = FirstSA->getMessage();
10341         StringLiteral *SecondStr = SecondSA->getMessage();
10342         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10343         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10344           SourceLocation FirstLoc, SecondLoc;
10345           SourceRange FirstRange, SecondRange;
10346           if (FirstStr) {
10347             FirstLoc = FirstStr->getBeginLoc();
10348             FirstRange = FirstStr->getSourceRange();
10349           } else {
10350             FirstLoc = FirstSA->getBeginLoc();
10351             FirstRange = FirstSA->getSourceRange();
10352           }
10353           if (SecondStr) {
10354             SecondLoc = SecondStr->getBeginLoc();
10355             SecondRange = SecondStr->getSourceRange();
10356           } else {
10357             SecondLoc = SecondSA->getBeginLoc();
10358             SecondRange = SecondSA->getSourceRange();
10359           }
10360           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10361                            StaticAssertOnlyMessage)
10362               << (FirstStr == nullptr);
10363           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10364                           StaticAssertOnlyMessage)
10365               << (SecondStr == nullptr);
10366           Diagnosed = true;
10367           break;
10368         }
10369 
10370         if (FirstStr && SecondStr &&
10371             FirstStr->getString() != SecondStr->getString()) {
10372           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10373                            FirstStr->getSourceRange(), StaticAssertMessage);
10374           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10375                           SecondStr->getSourceRange(), StaticAssertMessage);
10376           Diagnosed = true;
10377           break;
10378         }
10379         break;
10380       }
10381       case Field: {
10382         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10383                                  cast<FieldDecl>(FirstDecl),
10384                                  cast<FieldDecl>(SecondDecl));
10385         break;
10386       }
10387       case CXXMethod: {
10388         enum {
10389           DiagMethod,
10390           DiagConstructor,
10391           DiagDestructor,
10392         } FirstMethodType,
10393             SecondMethodType;
10394         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10395           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10396           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10397           return DiagMethod;
10398         };
10399         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10400         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10401         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10402         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10403         auto FirstName = FirstMethod->getDeclName();
10404         auto SecondName = SecondMethod->getDeclName();
10405         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10406           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10407                            FirstMethod->getSourceRange(), MethodName)
10408               << FirstMethodType << FirstName;
10409           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10410                           SecondMethod->getSourceRange(), MethodName)
10411               << SecondMethodType << SecondName;
10412 
10413           Diagnosed = true;
10414           break;
10415         }
10416 
10417         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10418         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10419         if (FirstDeleted != SecondDeleted) {
10420           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10421                            FirstMethod->getSourceRange(), MethodDeleted)
10422               << FirstMethodType << FirstName << FirstDeleted;
10423 
10424           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10425                           SecondMethod->getSourceRange(), MethodDeleted)
10426               << SecondMethodType << SecondName << SecondDeleted;
10427           Diagnosed = true;
10428           break;
10429         }
10430 
10431         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10432         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10433         if (FirstDefaulted != SecondDefaulted) {
10434           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10435                            FirstMethod->getSourceRange(), MethodDefaulted)
10436               << FirstMethodType << FirstName << FirstDefaulted;
10437 
10438           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10439                           SecondMethod->getSourceRange(), MethodDefaulted)
10440               << SecondMethodType << SecondName << SecondDefaulted;
10441           Diagnosed = true;
10442           break;
10443         }
10444 
10445         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10446         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10447         const bool FirstPure = FirstMethod->isPure();
10448         const bool SecondPure = SecondMethod->isPure();
10449         if ((FirstVirtual || SecondVirtual) &&
10450             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10451           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10452                            FirstMethod->getSourceRange(), MethodVirtual)
10453               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10454           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10455                           SecondMethod->getSourceRange(), MethodVirtual)
10456               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10457           Diagnosed = true;
10458           break;
10459         }
10460 
10461         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10462         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10463         // class needs to be checked instead.
10464         const auto FirstStorage = FirstMethod->getStorageClass();
10465         const auto SecondStorage = SecondMethod->getStorageClass();
10466         const bool FirstStatic = FirstStorage == SC_Static;
10467         const bool SecondStatic = SecondStorage == SC_Static;
10468         if (FirstStatic != SecondStatic) {
10469           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10470                            FirstMethod->getSourceRange(), MethodStatic)
10471               << FirstMethodType << FirstName << FirstStatic;
10472           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10473                           SecondMethod->getSourceRange(), MethodStatic)
10474               << SecondMethodType << SecondName << SecondStatic;
10475           Diagnosed = true;
10476           break;
10477         }
10478 
10479         const bool FirstVolatile = FirstMethod->isVolatile();
10480         const bool SecondVolatile = SecondMethod->isVolatile();
10481         if (FirstVolatile != SecondVolatile) {
10482           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10483                            FirstMethod->getSourceRange(), MethodVolatile)
10484               << FirstMethodType << FirstName << FirstVolatile;
10485           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10486                           SecondMethod->getSourceRange(), MethodVolatile)
10487               << SecondMethodType << SecondName << SecondVolatile;
10488           Diagnosed = true;
10489           break;
10490         }
10491 
10492         const bool FirstConst = FirstMethod->isConst();
10493         const bool SecondConst = SecondMethod->isConst();
10494         if (FirstConst != SecondConst) {
10495           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10496                            FirstMethod->getSourceRange(), MethodConst)
10497               << FirstMethodType << FirstName << FirstConst;
10498           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10499                           SecondMethod->getSourceRange(), MethodConst)
10500               << SecondMethodType << SecondName << SecondConst;
10501           Diagnosed = true;
10502           break;
10503         }
10504 
10505         const bool FirstInline = FirstMethod->isInlineSpecified();
10506         const bool SecondInline = SecondMethod->isInlineSpecified();
10507         if (FirstInline != SecondInline) {
10508           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10509                            FirstMethod->getSourceRange(), MethodInline)
10510               << FirstMethodType << FirstName << FirstInline;
10511           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10512                           SecondMethod->getSourceRange(), MethodInline)
10513               << SecondMethodType << SecondName << SecondInline;
10514           Diagnosed = true;
10515           break;
10516         }
10517 
10518         const unsigned FirstNumParameters = FirstMethod->param_size();
10519         const unsigned SecondNumParameters = SecondMethod->param_size();
10520         if (FirstNumParameters != SecondNumParameters) {
10521           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10522                            FirstMethod->getSourceRange(),
10523                            MethodNumberParameters)
10524               << FirstMethodType << FirstName << FirstNumParameters;
10525           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10526                           SecondMethod->getSourceRange(),
10527                           MethodNumberParameters)
10528               << SecondMethodType << SecondName << SecondNumParameters;
10529           Diagnosed = true;
10530           break;
10531         }
10532 
10533         // Need this status boolean to know when break out of the switch.
10534         bool ParameterMismatch = false;
10535         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10536           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10537           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10538 
10539           QualType FirstParamType = FirstParam->getType();
10540           QualType SecondParamType = SecondParam->getType();
10541           if (FirstParamType != SecondParamType &&
10542               ComputeQualTypeODRHash(FirstParamType) !=
10543                   ComputeQualTypeODRHash(SecondParamType)) {
10544             if (const DecayedType *ParamDecayedType =
10545                     FirstParamType->getAs<DecayedType>()) {
10546               ODRDiagDeclError(
10547                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10548                   FirstMethod->getSourceRange(), MethodParameterType)
10549                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10550                   << true << ParamDecayedType->getOriginalType();
10551             } else {
10552               ODRDiagDeclError(
10553                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10554                   FirstMethod->getSourceRange(), MethodParameterType)
10555                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10556                   << false;
10557             }
10558 
10559             if (const DecayedType *ParamDecayedType =
10560                     SecondParamType->getAs<DecayedType>()) {
10561               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10562                               SecondMethod->getSourceRange(),
10563                               MethodParameterType)
10564                   << SecondMethodType << SecondName << (I + 1)
10565                   << SecondParamType << true
10566                   << ParamDecayedType->getOriginalType();
10567             } else {
10568               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569                               SecondMethod->getSourceRange(),
10570                               MethodParameterType)
10571                   << SecondMethodType << SecondName << (I + 1)
10572                   << SecondParamType << false;
10573             }
10574             ParameterMismatch = true;
10575             break;
10576           }
10577 
10578           DeclarationName FirstParamName = FirstParam->getDeclName();
10579           DeclarationName SecondParamName = SecondParam->getDeclName();
10580           if (FirstParamName != SecondParamName) {
10581             ODRDiagDeclError(FirstRecord, FirstModule,
10582                              FirstMethod->getLocation(),
10583                              FirstMethod->getSourceRange(), MethodParameterName)
10584                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10585             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10586                             SecondMethod->getSourceRange(), MethodParameterName)
10587                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10588             ParameterMismatch = true;
10589             break;
10590           }
10591 
10592           const Expr *FirstInit = FirstParam->getInit();
10593           const Expr *SecondInit = SecondParam->getInit();
10594           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10595             ODRDiagDeclError(FirstRecord, FirstModule,
10596                              FirstMethod->getLocation(),
10597                              FirstMethod->getSourceRange(),
10598                              MethodParameterSingleDefaultArgument)
10599                 << FirstMethodType << FirstName << (I + 1)
10600                 << (FirstInit == nullptr)
10601                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10602             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10603                             SecondMethod->getSourceRange(),
10604                             MethodParameterSingleDefaultArgument)
10605                 << SecondMethodType << SecondName << (I + 1)
10606                 << (SecondInit == nullptr)
10607                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10608             ParameterMismatch = true;
10609             break;
10610           }
10611 
10612           if (FirstInit && SecondInit &&
10613               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10614             ODRDiagDeclError(FirstRecord, FirstModule,
10615                              FirstMethod->getLocation(),
10616                              FirstMethod->getSourceRange(),
10617                              MethodParameterDifferentDefaultArgument)
10618                 << FirstMethodType << FirstName << (I + 1)
10619                 << FirstInit->getSourceRange();
10620             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10621                             SecondMethod->getSourceRange(),
10622                             MethodParameterDifferentDefaultArgument)
10623                 << SecondMethodType << SecondName << (I + 1)
10624                 << SecondInit->getSourceRange();
10625             ParameterMismatch = true;
10626             break;
10627 
10628           }
10629         }
10630 
10631         if (ParameterMismatch) {
10632           Diagnosed = true;
10633           break;
10634         }
10635 
10636         const auto *FirstTemplateArgs =
10637             FirstMethod->getTemplateSpecializationArgs();
10638         const auto *SecondTemplateArgs =
10639             SecondMethod->getTemplateSpecializationArgs();
10640 
10641         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10642             (!FirstTemplateArgs && SecondTemplateArgs)) {
10643           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10644                            FirstMethod->getSourceRange(),
10645                            MethodNoTemplateArguments)
10646               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10647           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10648                           SecondMethod->getSourceRange(),
10649                           MethodNoTemplateArguments)
10650               << SecondMethodType << SecondName
10651               << (SecondTemplateArgs != nullptr);
10652 
10653           Diagnosed = true;
10654           break;
10655         }
10656 
10657         if (FirstTemplateArgs && SecondTemplateArgs) {
10658           // Remove pack expansions from argument list.
10659           auto ExpandTemplateArgumentList =
10660               [](const TemplateArgumentList *TAL) {
10661                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10662                 for (const TemplateArgument &TA : TAL->asArray()) {
10663                   if (TA.getKind() != TemplateArgument::Pack) {
10664                     ExpandedList.push_back(&TA);
10665                     continue;
10666                   }
10667                   llvm::append_range(ExpandedList, llvm::make_pointer_range(
10668                                                        TA.getPackAsArray()));
10669                 }
10670                 return ExpandedList;
10671               };
10672           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10673               ExpandTemplateArgumentList(FirstTemplateArgs);
10674           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10675               ExpandTemplateArgumentList(SecondTemplateArgs);
10676 
10677           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10678             ODRDiagDeclError(FirstRecord, FirstModule,
10679                              FirstMethod->getLocation(),
10680                              FirstMethod->getSourceRange(),
10681                              MethodDifferentNumberTemplateArguments)
10682                 << FirstMethodType << FirstName
10683                 << (unsigned)FirstExpandedList.size();
10684             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10685                             SecondMethod->getSourceRange(),
10686                             MethodDifferentNumberTemplateArguments)
10687                 << SecondMethodType << SecondName
10688                 << (unsigned)SecondExpandedList.size();
10689 
10690             Diagnosed = true;
10691             break;
10692           }
10693 
10694           bool TemplateArgumentMismatch = false;
10695           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10696             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10697                                    &SecondTA = *SecondExpandedList[i];
10698             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10699                 ComputeTemplateArgumentODRHash(SecondTA)) {
10700               continue;
10701             }
10702 
10703             ODRDiagDeclError(
10704                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10705                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10706                 << FirstMethodType << FirstName << FirstTA << i + 1;
10707             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10708                             SecondMethod->getSourceRange(),
10709                             MethodDifferentTemplateArgument)
10710                 << SecondMethodType << SecondName << SecondTA << i + 1;
10711 
10712             TemplateArgumentMismatch = true;
10713             break;
10714           }
10715 
10716           if (TemplateArgumentMismatch) {
10717             Diagnosed = true;
10718             break;
10719           }
10720         }
10721 
10722         // Compute the hash of the method as if it has no body.
10723         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10724           Hash.clear();
10725           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10726           return Hash.CalculateHash();
10727         };
10728 
10729         // Compare the hash generated to the hash stored.  A difference means
10730         // that a body was present in the original source.  Due to merging,
10731         // the stardard way of detecting a body will not work.
10732         const bool HasFirstBody =
10733             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10734         const bool HasSecondBody =
10735             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10736 
10737         if (HasFirstBody != HasSecondBody) {
10738           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10739                            FirstMethod->getSourceRange(), MethodSingleBody)
10740               << FirstMethodType << FirstName << HasFirstBody;
10741           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10742                           SecondMethod->getSourceRange(), MethodSingleBody)
10743               << SecondMethodType << SecondName << HasSecondBody;
10744           Diagnosed = true;
10745           break;
10746         }
10747 
10748         if (HasFirstBody && HasSecondBody) {
10749           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10750                            FirstMethod->getSourceRange(), MethodDifferentBody)
10751               << FirstMethodType << FirstName;
10752           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10753                           SecondMethod->getSourceRange(), MethodDifferentBody)
10754               << SecondMethodType << SecondName;
10755           Diagnosed = true;
10756           break;
10757         }
10758 
10759         break;
10760       }
10761       case TypeAlias:
10762       case TypeDef: {
10763         Diagnosed = ODRDiagTypeDefOrAlias(
10764             FirstRecord, FirstModule, SecondModule,
10765             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10766             FirstDiffType == TypeAlias);
10767         break;
10768       }
10769       case Var: {
10770         Diagnosed =
10771             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10772                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10773         break;
10774       }
10775       case Friend: {
10776         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10777         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10778 
10779         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10780         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10781 
10782         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10783         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10784 
10785         if (FirstND && SecondND) {
10786           ODRDiagDeclError(FirstRecord, FirstModule,
10787                            FirstFriend->getFriendLoc(),
10788                            FirstFriend->getSourceRange(), FriendFunction)
10789               << FirstND;
10790           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10791                           SecondFriend->getSourceRange(), FriendFunction)
10792               << SecondND;
10793 
10794           Diagnosed = true;
10795           break;
10796         }
10797 
10798         if (FirstTSI && SecondTSI) {
10799           QualType FirstFriendType = FirstTSI->getType();
10800           QualType SecondFriendType = SecondTSI->getType();
10801           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10802                  ComputeQualTypeODRHash(SecondFriendType));
10803           ODRDiagDeclError(FirstRecord, FirstModule,
10804                            FirstFriend->getFriendLoc(),
10805                            FirstFriend->getSourceRange(), FriendType)
10806               << FirstFriendType;
10807           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10808                           SecondFriend->getSourceRange(), FriendType)
10809               << SecondFriendType;
10810           Diagnosed = true;
10811           break;
10812         }
10813 
10814         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10815                          FirstFriend->getSourceRange(), FriendTypeFunction)
10816             << (FirstTSI == nullptr);
10817         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10818                         SecondFriend->getSourceRange(), FriendTypeFunction)
10819             << (SecondTSI == nullptr);
10820 
10821         Diagnosed = true;
10822         break;
10823       }
10824       case FunctionTemplate: {
10825         FunctionTemplateDecl *FirstTemplate =
10826             cast<FunctionTemplateDecl>(FirstDecl);
10827         FunctionTemplateDecl *SecondTemplate =
10828             cast<FunctionTemplateDecl>(SecondDecl);
10829 
10830         TemplateParameterList *FirstTPL =
10831             FirstTemplate->getTemplateParameters();
10832         TemplateParameterList *SecondTPL =
10833             SecondTemplate->getTemplateParameters();
10834 
10835         if (FirstTPL->size() != SecondTPL->size()) {
10836           ODRDiagDeclError(FirstRecord, FirstModule,
10837                            FirstTemplate->getLocation(),
10838                            FirstTemplate->getSourceRange(),
10839                            FunctionTemplateDifferentNumberParameters)
10840               << FirstTemplate << FirstTPL->size();
10841           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10842                           SecondTemplate->getSourceRange(),
10843                           FunctionTemplateDifferentNumberParameters)
10844               << SecondTemplate << SecondTPL->size();
10845 
10846           Diagnosed = true;
10847           break;
10848         }
10849 
10850         bool ParameterMismatch = false;
10851         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10852           NamedDecl *FirstParam = FirstTPL->getParam(i);
10853           NamedDecl *SecondParam = SecondTPL->getParam(i);
10854 
10855           if (FirstParam->getKind() != SecondParam->getKind()) {
10856             enum {
10857               TemplateTypeParameter,
10858               NonTypeTemplateParameter,
10859               TemplateTemplateParameter,
10860             };
10861             auto GetParamType = [](NamedDecl *D) {
10862               switch (D->getKind()) {
10863                 default:
10864                   llvm_unreachable("Unexpected template parameter type");
10865                 case Decl::TemplateTypeParm:
10866                   return TemplateTypeParameter;
10867                 case Decl::NonTypeTemplateParm:
10868                   return NonTypeTemplateParameter;
10869                 case Decl::TemplateTemplateParm:
10870                   return TemplateTemplateParameter;
10871               }
10872             };
10873 
10874             ODRDiagDeclError(FirstRecord, FirstModule,
10875                              FirstTemplate->getLocation(),
10876                              FirstTemplate->getSourceRange(),
10877                              FunctionTemplateParameterDifferentKind)
10878                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10879             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10880                             SecondTemplate->getSourceRange(),
10881                             FunctionTemplateParameterDifferentKind)
10882                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10883 
10884             ParameterMismatch = true;
10885             break;
10886           }
10887 
10888           if (FirstParam->getName() != SecondParam->getName()) {
10889             ODRDiagDeclError(
10890                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10891                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10892                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10893                 << FirstParam;
10894             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10895                             SecondTemplate->getSourceRange(),
10896                             FunctionTemplateParameterName)
10897                 << SecondTemplate << (i + 1)
10898                 << (bool)SecondParam->getIdentifier() << SecondParam;
10899             ParameterMismatch = true;
10900             break;
10901           }
10902 
10903           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10904               isa<TemplateTypeParmDecl>(SecondParam)) {
10905             TemplateTypeParmDecl *FirstTTPD =
10906                 cast<TemplateTypeParmDecl>(FirstParam);
10907             TemplateTypeParmDecl *SecondTTPD =
10908                 cast<TemplateTypeParmDecl>(SecondParam);
10909             bool HasFirstDefaultArgument =
10910                 FirstTTPD->hasDefaultArgument() &&
10911                 !FirstTTPD->defaultArgumentWasInherited();
10912             bool HasSecondDefaultArgument =
10913                 SecondTTPD->hasDefaultArgument() &&
10914                 !SecondTTPD->defaultArgumentWasInherited();
10915             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10916               ODRDiagDeclError(FirstRecord, FirstModule,
10917                                FirstTemplate->getLocation(),
10918                                FirstTemplate->getSourceRange(),
10919                                FunctionTemplateParameterSingleDefaultArgument)
10920                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10921               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10922                               SecondTemplate->getSourceRange(),
10923                               FunctionTemplateParameterSingleDefaultArgument)
10924                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10925               ParameterMismatch = true;
10926               break;
10927             }
10928 
10929             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10930               QualType FirstType = FirstTTPD->getDefaultArgument();
10931               QualType SecondType = SecondTTPD->getDefaultArgument();
10932               if (ComputeQualTypeODRHash(FirstType) !=
10933                   ComputeQualTypeODRHash(SecondType)) {
10934                 ODRDiagDeclError(
10935                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10936                     FirstTemplate->getSourceRange(),
10937                     FunctionTemplateParameterDifferentDefaultArgument)
10938                     << FirstTemplate << (i + 1) << FirstType;
10939                 ODRDiagDeclNote(
10940                     SecondModule, SecondTemplate->getLocation(),
10941                     SecondTemplate->getSourceRange(),
10942                     FunctionTemplateParameterDifferentDefaultArgument)
10943                     << SecondTemplate << (i + 1) << SecondType;
10944                 ParameterMismatch = true;
10945                 break;
10946               }
10947             }
10948 
10949             if (FirstTTPD->isParameterPack() !=
10950                 SecondTTPD->isParameterPack()) {
10951               ODRDiagDeclError(FirstRecord, FirstModule,
10952                                FirstTemplate->getLocation(),
10953                                FirstTemplate->getSourceRange(),
10954                                FunctionTemplatePackParameter)
10955                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10956               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10957                               SecondTemplate->getSourceRange(),
10958                               FunctionTemplatePackParameter)
10959                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10960               ParameterMismatch = true;
10961               break;
10962             }
10963           }
10964 
10965           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10966               isa<TemplateTemplateParmDecl>(SecondParam)) {
10967             TemplateTemplateParmDecl *FirstTTPD =
10968                 cast<TemplateTemplateParmDecl>(FirstParam);
10969             TemplateTemplateParmDecl *SecondTTPD =
10970                 cast<TemplateTemplateParmDecl>(SecondParam);
10971 
10972             TemplateParameterList *FirstTPL =
10973                 FirstTTPD->getTemplateParameters();
10974             TemplateParameterList *SecondTPL =
10975                 SecondTTPD->getTemplateParameters();
10976 
10977             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10978                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10979               ODRDiagDeclError(FirstRecord, FirstModule,
10980                                FirstTemplate->getLocation(),
10981                                FirstTemplate->getSourceRange(),
10982                                FunctionTemplateParameterDifferentType)
10983                   << FirstTemplate << (i + 1);
10984               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10985                               SecondTemplate->getSourceRange(),
10986                               FunctionTemplateParameterDifferentType)
10987                   << SecondTemplate << (i + 1);
10988               ParameterMismatch = true;
10989               break;
10990             }
10991 
10992             bool HasFirstDefaultArgument =
10993                 FirstTTPD->hasDefaultArgument() &&
10994                 !FirstTTPD->defaultArgumentWasInherited();
10995             bool HasSecondDefaultArgument =
10996                 SecondTTPD->hasDefaultArgument() &&
10997                 !SecondTTPD->defaultArgumentWasInherited();
10998             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10999               ODRDiagDeclError(FirstRecord, FirstModule,
11000                                FirstTemplate->getLocation(),
11001                                FirstTemplate->getSourceRange(),
11002                                FunctionTemplateParameterSingleDefaultArgument)
11003                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11004               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11005                               SecondTemplate->getSourceRange(),
11006                               FunctionTemplateParameterSingleDefaultArgument)
11007                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11008               ParameterMismatch = true;
11009               break;
11010             }
11011 
11012             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11013               TemplateArgument FirstTA =
11014                   FirstTTPD->getDefaultArgument().getArgument();
11015               TemplateArgument SecondTA =
11016                   SecondTTPD->getDefaultArgument().getArgument();
11017               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11018                   ComputeTemplateArgumentODRHash(SecondTA)) {
11019                 ODRDiagDeclError(
11020                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11021                     FirstTemplate->getSourceRange(),
11022                     FunctionTemplateParameterDifferentDefaultArgument)
11023                     << FirstTemplate << (i + 1) << FirstTA;
11024                 ODRDiagDeclNote(
11025                     SecondModule, SecondTemplate->getLocation(),
11026                     SecondTemplate->getSourceRange(),
11027                     FunctionTemplateParameterDifferentDefaultArgument)
11028                     << SecondTemplate << (i + 1) << SecondTA;
11029                 ParameterMismatch = true;
11030                 break;
11031               }
11032             }
11033 
11034             if (FirstTTPD->isParameterPack() !=
11035                 SecondTTPD->isParameterPack()) {
11036               ODRDiagDeclError(FirstRecord, FirstModule,
11037                                FirstTemplate->getLocation(),
11038                                FirstTemplate->getSourceRange(),
11039                                FunctionTemplatePackParameter)
11040                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11041               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11042                               SecondTemplate->getSourceRange(),
11043                               FunctionTemplatePackParameter)
11044                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11045               ParameterMismatch = true;
11046               break;
11047             }
11048           }
11049 
11050           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11051               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11052             NonTypeTemplateParmDecl *FirstNTTPD =
11053                 cast<NonTypeTemplateParmDecl>(FirstParam);
11054             NonTypeTemplateParmDecl *SecondNTTPD =
11055                 cast<NonTypeTemplateParmDecl>(SecondParam);
11056 
11057             QualType FirstType = FirstNTTPD->getType();
11058             QualType SecondType = SecondNTTPD->getType();
11059             if (ComputeQualTypeODRHash(FirstType) !=
11060                 ComputeQualTypeODRHash(SecondType)) {
11061               ODRDiagDeclError(FirstRecord, FirstModule,
11062                                FirstTemplate->getLocation(),
11063                                FirstTemplate->getSourceRange(),
11064                                FunctionTemplateParameterDifferentType)
11065                   << FirstTemplate << (i + 1);
11066               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11067                               SecondTemplate->getSourceRange(),
11068                               FunctionTemplateParameterDifferentType)
11069                   << SecondTemplate << (i + 1);
11070               ParameterMismatch = true;
11071               break;
11072             }
11073 
11074             bool HasFirstDefaultArgument =
11075                 FirstNTTPD->hasDefaultArgument() &&
11076                 !FirstNTTPD->defaultArgumentWasInherited();
11077             bool HasSecondDefaultArgument =
11078                 SecondNTTPD->hasDefaultArgument() &&
11079                 !SecondNTTPD->defaultArgumentWasInherited();
11080             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11081               ODRDiagDeclError(FirstRecord, FirstModule,
11082                                FirstTemplate->getLocation(),
11083                                FirstTemplate->getSourceRange(),
11084                                FunctionTemplateParameterSingleDefaultArgument)
11085                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11086               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11087                               SecondTemplate->getSourceRange(),
11088                               FunctionTemplateParameterSingleDefaultArgument)
11089                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11090               ParameterMismatch = true;
11091               break;
11092             }
11093 
11094             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11095               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11096               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11097               if (ComputeODRHash(FirstDefaultArgument) !=
11098                   ComputeODRHash(SecondDefaultArgument)) {
11099                 ODRDiagDeclError(
11100                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11101                     FirstTemplate->getSourceRange(),
11102                     FunctionTemplateParameterDifferentDefaultArgument)
11103                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11104                 ODRDiagDeclNote(
11105                     SecondModule, SecondTemplate->getLocation(),
11106                     SecondTemplate->getSourceRange(),
11107                     FunctionTemplateParameterDifferentDefaultArgument)
11108                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11109                 ParameterMismatch = true;
11110                 break;
11111               }
11112             }
11113 
11114             if (FirstNTTPD->isParameterPack() !=
11115                 SecondNTTPD->isParameterPack()) {
11116               ODRDiagDeclError(FirstRecord, FirstModule,
11117                                FirstTemplate->getLocation(),
11118                                FirstTemplate->getSourceRange(),
11119                                FunctionTemplatePackParameter)
11120                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11121               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11122                               SecondTemplate->getSourceRange(),
11123                               FunctionTemplatePackParameter)
11124                   << SecondTemplate << (i + 1)
11125                   << SecondNTTPD->isParameterPack();
11126               ParameterMismatch = true;
11127               break;
11128             }
11129           }
11130         }
11131 
11132         if (ParameterMismatch) {
11133           Diagnosed = true;
11134           break;
11135         }
11136 
11137         break;
11138       }
11139       }
11140 
11141       if (Diagnosed)
11142         continue;
11143 
11144       Diag(FirstDecl->getLocation(),
11145            diag::err_module_odr_violation_mismatch_decl_unknown)
11146           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11147           << FirstDecl->getSourceRange();
11148       Diag(SecondDecl->getLocation(),
11149            diag::note_module_odr_violation_mismatch_decl_unknown)
11150           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11151       Diagnosed = true;
11152     }
11153 
11154     if (!Diagnosed) {
11155       // All definitions are updates to the same declaration. This happens if a
11156       // module instantiates the declaration of a class template specialization
11157       // and two or more other modules instantiate its definition.
11158       //
11159       // FIXME: Indicate which modules had instantiations of this definition.
11160       // FIXME: How can this even happen?
11161       Diag(Merge.first->getLocation(),
11162            diag::err_module_odr_violation_different_instantiations)
11163         << Merge.first;
11164     }
11165   }
11166 
11167   // Issue ODR failures diagnostics for functions.
11168   for (auto &Merge : FunctionOdrMergeFailures) {
11169     enum ODRFunctionDifference {
11170       ReturnType,
11171       ParameterName,
11172       ParameterType,
11173       ParameterSingleDefaultArgument,
11174       ParameterDifferentDefaultArgument,
11175       FunctionBody,
11176     };
11177 
11178     FunctionDecl *FirstFunction = Merge.first;
11179     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11180 
11181     bool Diagnosed = false;
11182     for (auto &SecondFunction : Merge.second) {
11183 
11184       if (FirstFunction == SecondFunction)
11185         continue;
11186 
11187       std::string SecondModule =
11188           getOwningModuleNameForDiagnostic(SecondFunction);
11189 
11190       auto ODRDiagError = [FirstFunction, &FirstModule,
11191                            this](SourceLocation Loc, SourceRange Range,
11192                                  ODRFunctionDifference DiffType) {
11193         return Diag(Loc, diag::err_module_odr_violation_function)
11194                << FirstFunction << FirstModule.empty() << FirstModule << Range
11195                << DiffType;
11196       };
11197       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11198                                                SourceRange Range,
11199                                                ODRFunctionDifference DiffType) {
11200         return Diag(Loc, diag::note_module_odr_violation_function)
11201                << SecondModule << Range << DiffType;
11202       };
11203 
11204       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11205           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11206         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11207                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11208             << FirstFunction->getReturnType();
11209         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11210                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11211             << SecondFunction->getReturnType();
11212         Diagnosed = true;
11213         break;
11214       }
11215 
11216       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11217              "Merged functions with different number of parameters");
11218 
11219       auto ParamSize = FirstFunction->param_size();
11220       bool ParameterMismatch = false;
11221       for (unsigned I = 0; I < ParamSize; ++I) {
11222         auto *FirstParam = FirstFunction->getParamDecl(I);
11223         auto *SecondParam = SecondFunction->getParamDecl(I);
11224 
11225         assert(getContext().hasSameType(FirstParam->getType(),
11226                                       SecondParam->getType()) &&
11227                "Merged function has different parameter types.");
11228 
11229         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11230           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11231                        ParameterName)
11232               << I + 1 << FirstParam->getDeclName();
11233           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11234                       ParameterName)
11235               << I + 1 << SecondParam->getDeclName();
11236           ParameterMismatch = true;
11237           break;
11238         };
11239 
11240         QualType FirstParamType = FirstParam->getType();
11241         QualType SecondParamType = SecondParam->getType();
11242         if (FirstParamType != SecondParamType &&
11243             ComputeQualTypeODRHash(FirstParamType) !=
11244                 ComputeQualTypeODRHash(SecondParamType)) {
11245           if (const DecayedType *ParamDecayedType =
11246                   FirstParamType->getAs<DecayedType>()) {
11247             ODRDiagError(FirstParam->getLocation(),
11248                          FirstParam->getSourceRange(), ParameterType)
11249                 << (I + 1) << FirstParamType << true
11250                 << ParamDecayedType->getOriginalType();
11251           } else {
11252             ODRDiagError(FirstParam->getLocation(),
11253                          FirstParam->getSourceRange(), ParameterType)
11254                 << (I + 1) << FirstParamType << false;
11255           }
11256 
11257           if (const DecayedType *ParamDecayedType =
11258                   SecondParamType->getAs<DecayedType>()) {
11259             ODRDiagNote(SecondParam->getLocation(),
11260                         SecondParam->getSourceRange(), ParameterType)
11261                 << (I + 1) << SecondParamType << true
11262                 << ParamDecayedType->getOriginalType();
11263           } else {
11264             ODRDiagNote(SecondParam->getLocation(),
11265                         SecondParam->getSourceRange(), ParameterType)
11266                 << (I + 1) << SecondParamType << false;
11267           }
11268           ParameterMismatch = true;
11269           break;
11270         }
11271 
11272         const Expr *FirstInit = FirstParam->getInit();
11273         const Expr *SecondInit = SecondParam->getInit();
11274         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11275           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11276                        ParameterSingleDefaultArgument)
11277               << (I + 1) << (FirstInit == nullptr)
11278               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11279           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11280                       ParameterSingleDefaultArgument)
11281               << (I + 1) << (SecondInit == nullptr)
11282               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11283           ParameterMismatch = true;
11284           break;
11285         }
11286 
11287         if (FirstInit && SecondInit &&
11288             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11289           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11290                        ParameterDifferentDefaultArgument)
11291               << (I + 1) << FirstInit->getSourceRange();
11292           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11293                       ParameterDifferentDefaultArgument)
11294               << (I + 1) << SecondInit->getSourceRange();
11295           ParameterMismatch = true;
11296           break;
11297         }
11298 
11299         assert(ComputeSubDeclODRHash(FirstParam) ==
11300                    ComputeSubDeclODRHash(SecondParam) &&
11301                "Undiagnosed parameter difference.");
11302       }
11303 
11304       if (ParameterMismatch) {
11305         Diagnosed = true;
11306         break;
11307       }
11308 
11309       // If no error has been generated before now, assume the problem is in
11310       // the body and generate a message.
11311       ODRDiagError(FirstFunction->getLocation(),
11312                    FirstFunction->getSourceRange(), FunctionBody);
11313       ODRDiagNote(SecondFunction->getLocation(),
11314                   SecondFunction->getSourceRange(), FunctionBody);
11315       Diagnosed = true;
11316       break;
11317     }
11318     (void)Diagnosed;
11319     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11320   }
11321 
11322   // Issue ODR failures diagnostics for enums.
11323   for (auto &Merge : EnumOdrMergeFailures) {
11324     enum ODREnumDifference {
11325       SingleScopedEnum,
11326       EnumTagKeywordMismatch,
11327       SingleSpecifiedType,
11328       DifferentSpecifiedTypes,
11329       DifferentNumberEnumConstants,
11330       EnumConstantName,
11331       EnumConstantSingleInitilizer,
11332       EnumConstantDifferentInitilizer,
11333     };
11334 
11335     // If we've already pointed out a specific problem with this enum, don't
11336     // bother issuing a general "something's different" diagnostic.
11337     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11338       continue;
11339 
11340     EnumDecl *FirstEnum = Merge.first;
11341     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11342 
11343     using DeclHashes =
11344         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11345     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11346                               DeclHashes &Hashes, EnumDecl *Enum) {
11347       for (auto *D : Enum->decls()) {
11348         // Due to decl merging, the first EnumDecl is the parent of
11349         // Decls in both records.
11350         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11351           continue;
11352         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11353         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11354                             ComputeSubDeclODRHash(D));
11355       }
11356     };
11357     DeclHashes FirstHashes;
11358     PopulateHashes(FirstHashes, FirstEnum);
11359     bool Diagnosed = false;
11360     for (auto &SecondEnum : Merge.second) {
11361 
11362       if (FirstEnum == SecondEnum)
11363         continue;
11364 
11365       std::string SecondModule =
11366           getOwningModuleNameForDiagnostic(SecondEnum);
11367 
11368       auto ODRDiagError = [FirstEnum, &FirstModule,
11369                            this](SourceLocation Loc, SourceRange Range,
11370                                  ODREnumDifference DiffType) {
11371         return Diag(Loc, diag::err_module_odr_violation_enum)
11372                << FirstEnum << FirstModule.empty() << FirstModule << Range
11373                << DiffType;
11374       };
11375       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11376                                                SourceRange Range,
11377                                                ODREnumDifference DiffType) {
11378         return Diag(Loc, diag::note_module_odr_violation_enum)
11379                << SecondModule << Range << DiffType;
11380       };
11381 
11382       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11383         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11384                      SingleScopedEnum)
11385             << FirstEnum->isScoped();
11386         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11387                     SingleScopedEnum)
11388             << SecondEnum->isScoped();
11389         Diagnosed = true;
11390         continue;
11391       }
11392 
11393       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11394         if (FirstEnum->isScopedUsingClassTag() !=
11395             SecondEnum->isScopedUsingClassTag()) {
11396           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11397                        EnumTagKeywordMismatch)
11398               << FirstEnum->isScopedUsingClassTag();
11399           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11400                       EnumTagKeywordMismatch)
11401               << SecondEnum->isScopedUsingClassTag();
11402           Diagnosed = true;
11403           continue;
11404         }
11405       }
11406 
11407       QualType FirstUnderlyingType =
11408           FirstEnum->getIntegerTypeSourceInfo()
11409               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11410               : QualType();
11411       QualType SecondUnderlyingType =
11412           SecondEnum->getIntegerTypeSourceInfo()
11413               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11414               : QualType();
11415       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11416           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11417                        SingleSpecifiedType)
11418               << !FirstUnderlyingType.isNull();
11419           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11420                       SingleSpecifiedType)
11421               << !SecondUnderlyingType.isNull();
11422           Diagnosed = true;
11423           continue;
11424       }
11425 
11426       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11427         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11428             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11429           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11430                        DifferentSpecifiedTypes)
11431               << FirstUnderlyingType;
11432           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11433                       DifferentSpecifiedTypes)
11434               << SecondUnderlyingType;
11435           Diagnosed = true;
11436           continue;
11437         }
11438       }
11439 
11440       DeclHashes SecondHashes;
11441       PopulateHashes(SecondHashes, SecondEnum);
11442 
11443       if (FirstHashes.size() != SecondHashes.size()) {
11444         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11445                      DifferentNumberEnumConstants)
11446             << (int)FirstHashes.size();
11447         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11448                     DifferentNumberEnumConstants)
11449             << (int)SecondHashes.size();
11450         Diagnosed = true;
11451         continue;
11452       }
11453 
11454       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11455         if (FirstHashes[I].second == SecondHashes[I].second)
11456           continue;
11457         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11458         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11459 
11460         if (FirstEnumConstant->getDeclName() !=
11461             SecondEnumConstant->getDeclName()) {
11462 
11463           ODRDiagError(FirstEnumConstant->getLocation(),
11464                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11465               << I + 1 << FirstEnumConstant;
11466           ODRDiagNote(SecondEnumConstant->getLocation(),
11467                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11468               << I + 1 << SecondEnumConstant;
11469           Diagnosed = true;
11470           break;
11471         }
11472 
11473         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11474         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11475         if (!FirstInit && !SecondInit)
11476           continue;
11477 
11478         if (!FirstInit || !SecondInit) {
11479           ODRDiagError(FirstEnumConstant->getLocation(),
11480                        FirstEnumConstant->getSourceRange(),
11481                        EnumConstantSingleInitilizer)
11482               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11483           ODRDiagNote(SecondEnumConstant->getLocation(),
11484                       SecondEnumConstant->getSourceRange(),
11485                       EnumConstantSingleInitilizer)
11486               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11487           Diagnosed = true;
11488           break;
11489         }
11490 
11491         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11492           ODRDiagError(FirstEnumConstant->getLocation(),
11493                        FirstEnumConstant->getSourceRange(),
11494                        EnumConstantDifferentInitilizer)
11495               << I + 1 << FirstEnumConstant;
11496           ODRDiagNote(SecondEnumConstant->getLocation(),
11497                       SecondEnumConstant->getSourceRange(),
11498                       EnumConstantDifferentInitilizer)
11499               << I + 1 << SecondEnumConstant;
11500           Diagnosed = true;
11501           break;
11502         }
11503       }
11504     }
11505 
11506     (void)Diagnosed;
11507     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11508   }
11509 }
11510 
11511 void ASTReader::StartedDeserializing() {
11512   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11513     ReadTimer->startTimer();
11514 }
11515 
11516 void ASTReader::FinishedDeserializing() {
11517   assert(NumCurrentElementsDeserializing &&
11518          "FinishedDeserializing not paired with StartedDeserializing");
11519   if (NumCurrentElementsDeserializing == 1) {
11520     // We decrease NumCurrentElementsDeserializing only after pending actions
11521     // are finished, to avoid recursively re-calling finishPendingActions().
11522     finishPendingActions();
11523   }
11524   --NumCurrentElementsDeserializing;
11525 
11526   if (NumCurrentElementsDeserializing == 0) {
11527     // Propagate exception specification and deduced type updates along
11528     // redeclaration chains.
11529     //
11530     // We do this now rather than in finishPendingActions because we want to
11531     // be able to walk the complete redeclaration chains of the updated decls.
11532     while (!PendingExceptionSpecUpdates.empty() ||
11533            !PendingDeducedTypeUpdates.empty()) {
11534       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11535       PendingExceptionSpecUpdates.clear();
11536       for (auto Update : ESUpdates) {
11537         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11538         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11539         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11540         if (auto *Listener = getContext().getASTMutationListener())
11541           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11542         for (auto *Redecl : Update.second->redecls())
11543           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11544       }
11545 
11546       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11547       PendingDeducedTypeUpdates.clear();
11548       for (auto Update : DTUpdates) {
11549         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11550         // FIXME: If the return type is already deduced, check that it matches.
11551         getContext().adjustDeducedFunctionResultType(Update.first,
11552                                                      Update.second);
11553       }
11554     }
11555 
11556     if (ReadTimer)
11557       ReadTimer->stopTimer();
11558 
11559     diagnoseOdrViolations();
11560 
11561     // We are not in recursive loading, so it's safe to pass the "interesting"
11562     // decls to the consumer.
11563     if (Consumer)
11564       PassInterestingDeclsToConsumer();
11565   }
11566 }
11567 
11568 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11569   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11570     // Remove any fake results before adding any real ones.
11571     auto It = PendingFakeLookupResults.find(II);
11572     if (It != PendingFakeLookupResults.end()) {
11573       for (auto *ND : It->second)
11574         SemaObj->IdResolver.RemoveDecl(ND);
11575       // FIXME: this works around module+PCH performance issue.
11576       // Rather than erase the result from the map, which is O(n), just clear
11577       // the vector of NamedDecls.
11578       It->second.clear();
11579     }
11580   }
11581 
11582   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11583     SemaObj->TUScope->AddDecl(D);
11584   } else if (SemaObj->TUScope) {
11585     // Adding the decl to IdResolver may have failed because it was already in
11586     // (even though it was not added in scope). If it is already in, make sure
11587     // it gets in the scope as well.
11588     if (std::find(SemaObj->IdResolver.begin(Name),
11589                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11590       SemaObj->TUScope->AddDecl(D);
11591   }
11592 }
11593 
11594 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11595                      ASTContext *Context,
11596                      const PCHContainerReader &PCHContainerRdr,
11597                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11598                      StringRef isysroot,
11599                      DisableValidationForModuleKind DisableValidationKind,
11600                      bool AllowASTWithCompilerErrors,
11601                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11602                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11603                      std::unique_ptr<llvm::Timer> ReadTimer)
11604     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11605                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11606                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11607       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11608       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11609       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11610                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11611       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11612       DisableValidationKind(DisableValidationKind),
11613       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11614       AllowConfigurationMismatch(AllowConfigurationMismatch),
11615       ValidateSystemInputs(ValidateSystemInputs),
11616       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11617       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11618   SourceMgr.setExternalSLocEntrySource(this);
11619 
11620   for (const auto &Ext : Extensions) {
11621     auto BlockName = Ext->getExtensionMetadata().BlockName;
11622     auto Known = ModuleFileExtensions.find(BlockName);
11623     if (Known != ModuleFileExtensions.end()) {
11624       Diags.Report(diag::warn_duplicate_module_file_extension)
11625         << BlockName;
11626       continue;
11627     }
11628 
11629     ModuleFileExtensions.insert({BlockName, Ext});
11630   }
11631 }
11632 
11633 ASTReader::~ASTReader() {
11634   if (OwnsDeserializationListener)
11635     delete DeserializationListener;
11636 }
11637 
11638 IdentifierResolver &ASTReader::getIdResolver() {
11639   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11640 }
11641 
11642 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11643                                                unsigned AbbrevID) {
11644   Idx = 0;
11645   Record.clear();
11646   return Cursor.readRecord(AbbrevID, Record);
11647 }
11648 //===----------------------------------------------------------------------===//
11649 //// OMPClauseReader implementation
11650 ////===----------------------------------------------------------------------===//
11651 
11652 // This has to be in namespace clang because it's friended by all
11653 // of the OMP clauses.
11654 namespace clang {
11655 
11656 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11657   ASTRecordReader &Record;
11658   ASTContext &Context;
11659 
11660 public:
11661   OMPClauseReader(ASTRecordReader &Record)
11662       : Record(Record), Context(Record.getContext()) {}
11663 #define GEN_CLANG_CLAUSE_CLASS
11664 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11665 #include "llvm/Frontend/OpenMP/OMP.inc"
11666   OMPClause *readClause();
11667   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11668   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11669 };
11670 
11671 } // end namespace clang
11672 
11673 OMPClause *ASTRecordReader::readOMPClause() {
11674   return OMPClauseReader(*this).readClause();
11675 }
11676 
11677 OMPClause *OMPClauseReader::readClause() {
11678   OMPClause *C = nullptr;
11679   switch (llvm::omp::Clause(Record.readInt())) {
11680   case llvm::omp::OMPC_if:
11681     C = new (Context) OMPIfClause();
11682     break;
11683   case llvm::omp::OMPC_final:
11684     C = new (Context) OMPFinalClause();
11685     break;
11686   case llvm::omp::OMPC_num_threads:
11687     C = new (Context) OMPNumThreadsClause();
11688     break;
11689   case llvm::omp::OMPC_safelen:
11690     C = new (Context) OMPSafelenClause();
11691     break;
11692   case llvm::omp::OMPC_simdlen:
11693     C = new (Context) OMPSimdlenClause();
11694     break;
11695   case llvm::omp::OMPC_sizes: {
11696     unsigned NumSizes = Record.readInt();
11697     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11698     break;
11699   }
11700   case llvm::omp::OMPC_full:
11701     C = OMPFullClause::CreateEmpty(Context);
11702     break;
11703   case llvm::omp::OMPC_partial:
11704     C = OMPPartialClause::CreateEmpty(Context);
11705     break;
11706   case llvm::omp::OMPC_allocator:
11707     C = new (Context) OMPAllocatorClause();
11708     break;
11709   case llvm::omp::OMPC_collapse:
11710     C = new (Context) OMPCollapseClause();
11711     break;
11712   case llvm::omp::OMPC_default:
11713     C = new (Context) OMPDefaultClause();
11714     break;
11715   case llvm::omp::OMPC_proc_bind:
11716     C = new (Context) OMPProcBindClause();
11717     break;
11718   case llvm::omp::OMPC_schedule:
11719     C = new (Context) OMPScheduleClause();
11720     break;
11721   case llvm::omp::OMPC_ordered:
11722     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11723     break;
11724   case llvm::omp::OMPC_nowait:
11725     C = new (Context) OMPNowaitClause();
11726     break;
11727   case llvm::omp::OMPC_untied:
11728     C = new (Context) OMPUntiedClause();
11729     break;
11730   case llvm::omp::OMPC_mergeable:
11731     C = new (Context) OMPMergeableClause();
11732     break;
11733   case llvm::omp::OMPC_read:
11734     C = new (Context) OMPReadClause();
11735     break;
11736   case llvm::omp::OMPC_write:
11737     C = new (Context) OMPWriteClause();
11738     break;
11739   case llvm::omp::OMPC_update:
11740     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11741     break;
11742   case llvm::omp::OMPC_capture:
11743     C = new (Context) OMPCaptureClause();
11744     break;
11745   case llvm::omp::OMPC_compare:
11746     C = new (Context) OMPCompareClause();
11747     break;
11748   case llvm::omp::OMPC_seq_cst:
11749     C = new (Context) OMPSeqCstClause();
11750     break;
11751   case llvm::omp::OMPC_acq_rel:
11752     C = new (Context) OMPAcqRelClause();
11753     break;
11754   case llvm::omp::OMPC_acquire:
11755     C = new (Context) OMPAcquireClause();
11756     break;
11757   case llvm::omp::OMPC_release:
11758     C = new (Context) OMPReleaseClause();
11759     break;
11760   case llvm::omp::OMPC_relaxed:
11761     C = new (Context) OMPRelaxedClause();
11762     break;
11763   case llvm::omp::OMPC_threads:
11764     C = new (Context) OMPThreadsClause();
11765     break;
11766   case llvm::omp::OMPC_simd:
11767     C = new (Context) OMPSIMDClause();
11768     break;
11769   case llvm::omp::OMPC_nogroup:
11770     C = new (Context) OMPNogroupClause();
11771     break;
11772   case llvm::omp::OMPC_unified_address:
11773     C = new (Context) OMPUnifiedAddressClause();
11774     break;
11775   case llvm::omp::OMPC_unified_shared_memory:
11776     C = new (Context) OMPUnifiedSharedMemoryClause();
11777     break;
11778   case llvm::omp::OMPC_reverse_offload:
11779     C = new (Context) OMPReverseOffloadClause();
11780     break;
11781   case llvm::omp::OMPC_dynamic_allocators:
11782     C = new (Context) OMPDynamicAllocatorsClause();
11783     break;
11784   case llvm::omp::OMPC_atomic_default_mem_order:
11785     C = new (Context) OMPAtomicDefaultMemOrderClause();
11786     break;
11787  case llvm::omp::OMPC_private:
11788     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11789     break;
11790   case llvm::omp::OMPC_firstprivate:
11791     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11792     break;
11793   case llvm::omp::OMPC_lastprivate:
11794     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11795     break;
11796   case llvm::omp::OMPC_shared:
11797     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11798     break;
11799   case llvm::omp::OMPC_reduction: {
11800     unsigned N = Record.readInt();
11801     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11802     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11803     break;
11804   }
11805   case llvm::omp::OMPC_task_reduction:
11806     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11807     break;
11808   case llvm::omp::OMPC_in_reduction:
11809     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11810     break;
11811   case llvm::omp::OMPC_linear:
11812     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11813     break;
11814   case llvm::omp::OMPC_aligned:
11815     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11816     break;
11817   case llvm::omp::OMPC_copyin:
11818     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11819     break;
11820   case llvm::omp::OMPC_copyprivate:
11821     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11822     break;
11823   case llvm::omp::OMPC_flush:
11824     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11825     break;
11826   case llvm::omp::OMPC_depobj:
11827     C = OMPDepobjClause::CreateEmpty(Context);
11828     break;
11829   case llvm::omp::OMPC_depend: {
11830     unsigned NumVars = Record.readInt();
11831     unsigned NumLoops = Record.readInt();
11832     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11833     break;
11834   }
11835   case llvm::omp::OMPC_device:
11836     C = new (Context) OMPDeviceClause();
11837     break;
11838   case llvm::omp::OMPC_map: {
11839     OMPMappableExprListSizeTy Sizes;
11840     Sizes.NumVars = Record.readInt();
11841     Sizes.NumUniqueDeclarations = Record.readInt();
11842     Sizes.NumComponentLists = Record.readInt();
11843     Sizes.NumComponents = Record.readInt();
11844     C = OMPMapClause::CreateEmpty(Context, Sizes);
11845     break;
11846   }
11847   case llvm::omp::OMPC_num_teams:
11848     C = new (Context) OMPNumTeamsClause();
11849     break;
11850   case llvm::omp::OMPC_thread_limit:
11851     C = new (Context) OMPThreadLimitClause();
11852     break;
11853   case llvm::omp::OMPC_priority:
11854     C = new (Context) OMPPriorityClause();
11855     break;
11856   case llvm::omp::OMPC_grainsize:
11857     C = new (Context) OMPGrainsizeClause();
11858     break;
11859   case llvm::omp::OMPC_num_tasks:
11860     C = new (Context) OMPNumTasksClause();
11861     break;
11862   case llvm::omp::OMPC_hint:
11863     C = new (Context) OMPHintClause();
11864     break;
11865   case llvm::omp::OMPC_dist_schedule:
11866     C = new (Context) OMPDistScheduleClause();
11867     break;
11868   case llvm::omp::OMPC_defaultmap:
11869     C = new (Context) OMPDefaultmapClause();
11870     break;
11871   case llvm::omp::OMPC_to: {
11872     OMPMappableExprListSizeTy Sizes;
11873     Sizes.NumVars = Record.readInt();
11874     Sizes.NumUniqueDeclarations = Record.readInt();
11875     Sizes.NumComponentLists = Record.readInt();
11876     Sizes.NumComponents = Record.readInt();
11877     C = OMPToClause::CreateEmpty(Context, Sizes);
11878     break;
11879   }
11880   case llvm::omp::OMPC_from: {
11881     OMPMappableExprListSizeTy Sizes;
11882     Sizes.NumVars = Record.readInt();
11883     Sizes.NumUniqueDeclarations = Record.readInt();
11884     Sizes.NumComponentLists = Record.readInt();
11885     Sizes.NumComponents = Record.readInt();
11886     C = OMPFromClause::CreateEmpty(Context, Sizes);
11887     break;
11888   }
11889   case llvm::omp::OMPC_use_device_ptr: {
11890     OMPMappableExprListSizeTy Sizes;
11891     Sizes.NumVars = Record.readInt();
11892     Sizes.NumUniqueDeclarations = Record.readInt();
11893     Sizes.NumComponentLists = Record.readInt();
11894     Sizes.NumComponents = Record.readInt();
11895     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11896     break;
11897   }
11898   case llvm::omp::OMPC_use_device_addr: {
11899     OMPMappableExprListSizeTy Sizes;
11900     Sizes.NumVars = Record.readInt();
11901     Sizes.NumUniqueDeclarations = Record.readInt();
11902     Sizes.NumComponentLists = Record.readInt();
11903     Sizes.NumComponents = Record.readInt();
11904     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11905     break;
11906   }
11907   case llvm::omp::OMPC_is_device_ptr: {
11908     OMPMappableExprListSizeTy Sizes;
11909     Sizes.NumVars = Record.readInt();
11910     Sizes.NumUniqueDeclarations = Record.readInt();
11911     Sizes.NumComponentLists = Record.readInt();
11912     Sizes.NumComponents = Record.readInt();
11913     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11914     break;
11915   }
11916   case llvm::omp::OMPC_has_device_addr: {
11917     OMPMappableExprListSizeTy Sizes;
11918     Sizes.NumVars = Record.readInt();
11919     Sizes.NumUniqueDeclarations = Record.readInt();
11920     Sizes.NumComponentLists = Record.readInt();
11921     Sizes.NumComponents = Record.readInt();
11922     C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);
11923     break;
11924   }
11925   case llvm::omp::OMPC_allocate:
11926     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11927     break;
11928   case llvm::omp::OMPC_nontemporal:
11929     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11930     break;
11931   case llvm::omp::OMPC_inclusive:
11932     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11933     break;
11934   case llvm::omp::OMPC_exclusive:
11935     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11936     break;
11937   case llvm::omp::OMPC_order:
11938     C = new (Context) OMPOrderClause();
11939     break;
11940   case llvm::omp::OMPC_init:
11941     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11942     break;
11943   case llvm::omp::OMPC_use:
11944     C = new (Context) OMPUseClause();
11945     break;
11946   case llvm::omp::OMPC_destroy:
11947     C = new (Context) OMPDestroyClause();
11948     break;
11949   case llvm::omp::OMPC_novariants:
11950     C = new (Context) OMPNovariantsClause();
11951     break;
11952   case llvm::omp::OMPC_nocontext:
11953     C = new (Context) OMPNocontextClause();
11954     break;
11955   case llvm::omp::OMPC_detach:
11956     C = new (Context) OMPDetachClause();
11957     break;
11958   case llvm::omp::OMPC_uses_allocators:
11959     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11960     break;
11961   case llvm::omp::OMPC_affinity:
11962     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11963     break;
11964   case llvm::omp::OMPC_filter:
11965     C = new (Context) OMPFilterClause();
11966     break;
11967   case llvm::omp::OMPC_bind:
11968     C = OMPBindClause::CreateEmpty(Context);
11969     break;
11970   case llvm::omp::OMPC_align:
11971     C = new (Context) OMPAlignClause();
11972     break;
11973 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11974   case llvm::omp::Enum:                                                        \
11975     break;
11976 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11977   default:
11978     break;
11979   }
11980   assert(C && "Unknown OMPClause type");
11981 
11982   Visit(C);
11983   C->setLocStart(Record.readSourceLocation());
11984   C->setLocEnd(Record.readSourceLocation());
11985 
11986   return C;
11987 }
11988 
11989 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11990   C->setPreInitStmt(Record.readSubStmt(),
11991                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11992 }
11993 
11994 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11995   VisitOMPClauseWithPreInit(C);
11996   C->setPostUpdateExpr(Record.readSubExpr());
11997 }
11998 
11999 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12000   VisitOMPClauseWithPreInit(C);
12001   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12002   C->setNameModifierLoc(Record.readSourceLocation());
12003   C->setColonLoc(Record.readSourceLocation());
12004   C->setCondition(Record.readSubExpr());
12005   C->setLParenLoc(Record.readSourceLocation());
12006 }
12007 
12008 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12009   VisitOMPClauseWithPreInit(C);
12010   C->setCondition(Record.readSubExpr());
12011   C->setLParenLoc(Record.readSourceLocation());
12012 }
12013 
12014 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12015   VisitOMPClauseWithPreInit(C);
12016   C->setNumThreads(Record.readSubExpr());
12017   C->setLParenLoc(Record.readSourceLocation());
12018 }
12019 
12020 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12021   C->setSafelen(Record.readSubExpr());
12022   C->setLParenLoc(Record.readSourceLocation());
12023 }
12024 
12025 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12026   C->setSimdlen(Record.readSubExpr());
12027   C->setLParenLoc(Record.readSourceLocation());
12028 }
12029 
12030 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12031   for (Expr *&E : C->getSizesRefs())
12032     E = Record.readSubExpr();
12033   C->setLParenLoc(Record.readSourceLocation());
12034 }
12035 
12036 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12037 
12038 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12039   C->setFactor(Record.readSubExpr());
12040   C->setLParenLoc(Record.readSourceLocation());
12041 }
12042 
12043 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12044   C->setAllocator(Record.readExpr());
12045   C->setLParenLoc(Record.readSourceLocation());
12046 }
12047 
12048 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12049   C->setNumForLoops(Record.readSubExpr());
12050   C->setLParenLoc(Record.readSourceLocation());
12051 }
12052 
12053 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12054   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12055   C->setLParenLoc(Record.readSourceLocation());
12056   C->setDefaultKindKwLoc(Record.readSourceLocation());
12057 }
12058 
12059 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12060   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12061   C->setLParenLoc(Record.readSourceLocation());
12062   C->setProcBindKindKwLoc(Record.readSourceLocation());
12063 }
12064 
12065 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12066   VisitOMPClauseWithPreInit(C);
12067   C->setScheduleKind(
12068        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12069   C->setFirstScheduleModifier(
12070       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12071   C->setSecondScheduleModifier(
12072       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12073   C->setChunkSize(Record.readSubExpr());
12074   C->setLParenLoc(Record.readSourceLocation());
12075   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12076   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12077   C->setScheduleKindLoc(Record.readSourceLocation());
12078   C->setCommaLoc(Record.readSourceLocation());
12079 }
12080 
12081 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12082   C->setNumForLoops(Record.readSubExpr());
12083   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12084     C->setLoopNumIterations(I, Record.readSubExpr());
12085   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12086     C->setLoopCounter(I, Record.readSubExpr());
12087   C->setLParenLoc(Record.readSourceLocation());
12088 }
12089 
12090 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12091   C->setEventHandler(Record.readSubExpr());
12092   C->setLParenLoc(Record.readSourceLocation());
12093 }
12094 
12095 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12096 
12097 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12098 
12099 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12100 
12101 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12102 
12103 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12104 
12105 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12106   if (C->isExtended()) {
12107     C->setLParenLoc(Record.readSourceLocation());
12108     C->setArgumentLoc(Record.readSourceLocation());
12109     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12110   }
12111 }
12112 
12113 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12114 
12115 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12116 
12117 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12118 
12119 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12120 
12121 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12122 
12123 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12124 
12125 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12126 
12127 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12128 
12129 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12130 
12131 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12132 
12133 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12134   unsigned NumVars = C->varlist_size();
12135   SmallVector<Expr *, 16> Vars;
12136   Vars.reserve(NumVars);
12137   for (unsigned I = 0; I != NumVars; ++I)
12138     Vars.push_back(Record.readSubExpr());
12139   C->setVarRefs(Vars);
12140   C->setIsTarget(Record.readBool());
12141   C->setIsTargetSync(Record.readBool());
12142   C->setLParenLoc(Record.readSourceLocation());
12143   C->setVarLoc(Record.readSourceLocation());
12144 }
12145 
12146 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12147   C->setInteropVar(Record.readSubExpr());
12148   C->setLParenLoc(Record.readSourceLocation());
12149   C->setVarLoc(Record.readSourceLocation());
12150 }
12151 
12152 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12153   C->setInteropVar(Record.readSubExpr());
12154   C->setLParenLoc(Record.readSourceLocation());
12155   C->setVarLoc(Record.readSourceLocation());
12156 }
12157 
12158 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12159   VisitOMPClauseWithPreInit(C);
12160   C->setCondition(Record.readSubExpr());
12161   C->setLParenLoc(Record.readSourceLocation());
12162 }
12163 
12164 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12165   VisitOMPClauseWithPreInit(C);
12166   C->setCondition(Record.readSubExpr());
12167   C->setLParenLoc(Record.readSourceLocation());
12168 }
12169 
12170 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12171 
12172 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12173     OMPUnifiedSharedMemoryClause *) {}
12174 
12175 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12176 
12177 void
12178 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12179 }
12180 
12181 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12182     OMPAtomicDefaultMemOrderClause *C) {
12183   C->setAtomicDefaultMemOrderKind(
12184       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12185   C->setLParenLoc(Record.readSourceLocation());
12186   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12187 }
12188 
12189 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12190   C->setLParenLoc(Record.readSourceLocation());
12191   unsigned NumVars = C->varlist_size();
12192   SmallVector<Expr *, 16> Vars;
12193   Vars.reserve(NumVars);
12194   for (unsigned i = 0; i != NumVars; ++i)
12195     Vars.push_back(Record.readSubExpr());
12196   C->setVarRefs(Vars);
12197   Vars.clear();
12198   for (unsigned i = 0; i != NumVars; ++i)
12199     Vars.push_back(Record.readSubExpr());
12200   C->setPrivateCopies(Vars);
12201 }
12202 
12203 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12204   VisitOMPClauseWithPreInit(C);
12205   C->setLParenLoc(Record.readSourceLocation());
12206   unsigned NumVars = C->varlist_size();
12207   SmallVector<Expr *, 16> Vars;
12208   Vars.reserve(NumVars);
12209   for (unsigned i = 0; i != NumVars; ++i)
12210     Vars.push_back(Record.readSubExpr());
12211   C->setVarRefs(Vars);
12212   Vars.clear();
12213   for (unsigned i = 0; i != NumVars; ++i)
12214     Vars.push_back(Record.readSubExpr());
12215   C->setPrivateCopies(Vars);
12216   Vars.clear();
12217   for (unsigned i = 0; i != NumVars; ++i)
12218     Vars.push_back(Record.readSubExpr());
12219   C->setInits(Vars);
12220 }
12221 
12222 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12223   VisitOMPClauseWithPostUpdate(C);
12224   C->setLParenLoc(Record.readSourceLocation());
12225   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12226   C->setKindLoc(Record.readSourceLocation());
12227   C->setColonLoc(Record.readSourceLocation());
12228   unsigned NumVars = C->varlist_size();
12229   SmallVector<Expr *, 16> Vars;
12230   Vars.reserve(NumVars);
12231   for (unsigned i = 0; i != NumVars; ++i)
12232     Vars.push_back(Record.readSubExpr());
12233   C->setVarRefs(Vars);
12234   Vars.clear();
12235   for (unsigned i = 0; i != NumVars; ++i)
12236     Vars.push_back(Record.readSubExpr());
12237   C->setPrivateCopies(Vars);
12238   Vars.clear();
12239   for (unsigned i = 0; i != NumVars; ++i)
12240     Vars.push_back(Record.readSubExpr());
12241   C->setSourceExprs(Vars);
12242   Vars.clear();
12243   for (unsigned i = 0; i != NumVars; ++i)
12244     Vars.push_back(Record.readSubExpr());
12245   C->setDestinationExprs(Vars);
12246   Vars.clear();
12247   for (unsigned i = 0; i != NumVars; ++i)
12248     Vars.push_back(Record.readSubExpr());
12249   C->setAssignmentOps(Vars);
12250 }
12251 
12252 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12253   C->setLParenLoc(Record.readSourceLocation());
12254   unsigned NumVars = C->varlist_size();
12255   SmallVector<Expr *, 16> Vars;
12256   Vars.reserve(NumVars);
12257   for (unsigned i = 0; i != NumVars; ++i)
12258     Vars.push_back(Record.readSubExpr());
12259   C->setVarRefs(Vars);
12260 }
12261 
12262 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12263   VisitOMPClauseWithPostUpdate(C);
12264   C->setLParenLoc(Record.readSourceLocation());
12265   C->setModifierLoc(Record.readSourceLocation());
12266   C->setColonLoc(Record.readSourceLocation());
12267   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12268   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12269   C->setQualifierLoc(NNSL);
12270   C->setNameInfo(DNI);
12271 
12272   unsigned NumVars = C->varlist_size();
12273   SmallVector<Expr *, 16> Vars;
12274   Vars.reserve(NumVars);
12275   for (unsigned i = 0; i != NumVars; ++i)
12276     Vars.push_back(Record.readSubExpr());
12277   C->setVarRefs(Vars);
12278   Vars.clear();
12279   for (unsigned i = 0; i != NumVars; ++i)
12280     Vars.push_back(Record.readSubExpr());
12281   C->setPrivates(Vars);
12282   Vars.clear();
12283   for (unsigned i = 0; i != NumVars; ++i)
12284     Vars.push_back(Record.readSubExpr());
12285   C->setLHSExprs(Vars);
12286   Vars.clear();
12287   for (unsigned i = 0; i != NumVars; ++i)
12288     Vars.push_back(Record.readSubExpr());
12289   C->setRHSExprs(Vars);
12290   Vars.clear();
12291   for (unsigned i = 0; i != NumVars; ++i)
12292     Vars.push_back(Record.readSubExpr());
12293   C->setReductionOps(Vars);
12294   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12295     Vars.clear();
12296     for (unsigned i = 0; i != NumVars; ++i)
12297       Vars.push_back(Record.readSubExpr());
12298     C->setInscanCopyOps(Vars);
12299     Vars.clear();
12300     for (unsigned i = 0; i != NumVars; ++i)
12301       Vars.push_back(Record.readSubExpr());
12302     C->setInscanCopyArrayTemps(Vars);
12303     Vars.clear();
12304     for (unsigned i = 0; i != NumVars; ++i)
12305       Vars.push_back(Record.readSubExpr());
12306     C->setInscanCopyArrayElems(Vars);
12307   }
12308 }
12309 
12310 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12311   VisitOMPClauseWithPostUpdate(C);
12312   C->setLParenLoc(Record.readSourceLocation());
12313   C->setColonLoc(Record.readSourceLocation());
12314   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12315   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12316   C->setQualifierLoc(NNSL);
12317   C->setNameInfo(DNI);
12318 
12319   unsigned NumVars = C->varlist_size();
12320   SmallVector<Expr *, 16> Vars;
12321   Vars.reserve(NumVars);
12322   for (unsigned I = 0; I != NumVars; ++I)
12323     Vars.push_back(Record.readSubExpr());
12324   C->setVarRefs(Vars);
12325   Vars.clear();
12326   for (unsigned I = 0; I != NumVars; ++I)
12327     Vars.push_back(Record.readSubExpr());
12328   C->setPrivates(Vars);
12329   Vars.clear();
12330   for (unsigned I = 0; I != NumVars; ++I)
12331     Vars.push_back(Record.readSubExpr());
12332   C->setLHSExprs(Vars);
12333   Vars.clear();
12334   for (unsigned I = 0; I != NumVars; ++I)
12335     Vars.push_back(Record.readSubExpr());
12336   C->setRHSExprs(Vars);
12337   Vars.clear();
12338   for (unsigned I = 0; I != NumVars; ++I)
12339     Vars.push_back(Record.readSubExpr());
12340   C->setReductionOps(Vars);
12341 }
12342 
12343 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12344   VisitOMPClauseWithPostUpdate(C);
12345   C->setLParenLoc(Record.readSourceLocation());
12346   C->setColonLoc(Record.readSourceLocation());
12347   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12348   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12349   C->setQualifierLoc(NNSL);
12350   C->setNameInfo(DNI);
12351 
12352   unsigned NumVars = C->varlist_size();
12353   SmallVector<Expr *, 16> Vars;
12354   Vars.reserve(NumVars);
12355   for (unsigned I = 0; I != NumVars; ++I)
12356     Vars.push_back(Record.readSubExpr());
12357   C->setVarRefs(Vars);
12358   Vars.clear();
12359   for (unsigned I = 0; I != NumVars; ++I)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setPrivates(Vars);
12362   Vars.clear();
12363   for (unsigned I = 0; I != NumVars; ++I)
12364     Vars.push_back(Record.readSubExpr());
12365   C->setLHSExprs(Vars);
12366   Vars.clear();
12367   for (unsigned I = 0; I != NumVars; ++I)
12368     Vars.push_back(Record.readSubExpr());
12369   C->setRHSExprs(Vars);
12370   Vars.clear();
12371   for (unsigned I = 0; I != NumVars; ++I)
12372     Vars.push_back(Record.readSubExpr());
12373   C->setReductionOps(Vars);
12374   Vars.clear();
12375   for (unsigned I = 0; I != NumVars; ++I)
12376     Vars.push_back(Record.readSubExpr());
12377   C->setTaskgroupDescriptors(Vars);
12378 }
12379 
12380 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12381   VisitOMPClauseWithPostUpdate(C);
12382   C->setLParenLoc(Record.readSourceLocation());
12383   C->setColonLoc(Record.readSourceLocation());
12384   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12385   C->setModifierLoc(Record.readSourceLocation());
12386   unsigned NumVars = C->varlist_size();
12387   SmallVector<Expr *, 16> Vars;
12388   Vars.reserve(NumVars);
12389   for (unsigned i = 0; i != NumVars; ++i)
12390     Vars.push_back(Record.readSubExpr());
12391   C->setVarRefs(Vars);
12392   Vars.clear();
12393   for (unsigned i = 0; i != NumVars; ++i)
12394     Vars.push_back(Record.readSubExpr());
12395   C->setPrivates(Vars);
12396   Vars.clear();
12397   for (unsigned i = 0; i != NumVars; ++i)
12398     Vars.push_back(Record.readSubExpr());
12399   C->setInits(Vars);
12400   Vars.clear();
12401   for (unsigned i = 0; i != NumVars; ++i)
12402     Vars.push_back(Record.readSubExpr());
12403   C->setUpdates(Vars);
12404   Vars.clear();
12405   for (unsigned i = 0; i != NumVars; ++i)
12406     Vars.push_back(Record.readSubExpr());
12407   C->setFinals(Vars);
12408   C->setStep(Record.readSubExpr());
12409   C->setCalcStep(Record.readSubExpr());
12410   Vars.clear();
12411   for (unsigned I = 0; I != NumVars + 1; ++I)
12412     Vars.push_back(Record.readSubExpr());
12413   C->setUsedExprs(Vars);
12414 }
12415 
12416 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12417   C->setLParenLoc(Record.readSourceLocation());
12418   C->setColonLoc(Record.readSourceLocation());
12419   unsigned NumVars = C->varlist_size();
12420   SmallVector<Expr *, 16> Vars;
12421   Vars.reserve(NumVars);
12422   for (unsigned i = 0; i != NumVars; ++i)
12423     Vars.push_back(Record.readSubExpr());
12424   C->setVarRefs(Vars);
12425   C->setAlignment(Record.readSubExpr());
12426 }
12427 
12428 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12429   C->setLParenLoc(Record.readSourceLocation());
12430   unsigned NumVars = C->varlist_size();
12431   SmallVector<Expr *, 16> Exprs;
12432   Exprs.reserve(NumVars);
12433   for (unsigned i = 0; i != NumVars; ++i)
12434     Exprs.push_back(Record.readSubExpr());
12435   C->setVarRefs(Exprs);
12436   Exprs.clear();
12437   for (unsigned i = 0; i != NumVars; ++i)
12438     Exprs.push_back(Record.readSubExpr());
12439   C->setSourceExprs(Exprs);
12440   Exprs.clear();
12441   for (unsigned i = 0; i != NumVars; ++i)
12442     Exprs.push_back(Record.readSubExpr());
12443   C->setDestinationExprs(Exprs);
12444   Exprs.clear();
12445   for (unsigned i = 0; i != NumVars; ++i)
12446     Exprs.push_back(Record.readSubExpr());
12447   C->setAssignmentOps(Exprs);
12448 }
12449 
12450 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12451   C->setLParenLoc(Record.readSourceLocation());
12452   unsigned NumVars = C->varlist_size();
12453   SmallVector<Expr *, 16> Exprs;
12454   Exprs.reserve(NumVars);
12455   for (unsigned i = 0; i != NumVars; ++i)
12456     Exprs.push_back(Record.readSubExpr());
12457   C->setVarRefs(Exprs);
12458   Exprs.clear();
12459   for (unsigned i = 0; i != NumVars; ++i)
12460     Exprs.push_back(Record.readSubExpr());
12461   C->setSourceExprs(Exprs);
12462   Exprs.clear();
12463   for (unsigned i = 0; i != NumVars; ++i)
12464     Exprs.push_back(Record.readSubExpr());
12465   C->setDestinationExprs(Exprs);
12466   Exprs.clear();
12467   for (unsigned i = 0; i != NumVars; ++i)
12468     Exprs.push_back(Record.readSubExpr());
12469   C->setAssignmentOps(Exprs);
12470 }
12471 
12472 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12473   C->setLParenLoc(Record.readSourceLocation());
12474   unsigned NumVars = C->varlist_size();
12475   SmallVector<Expr *, 16> Vars;
12476   Vars.reserve(NumVars);
12477   for (unsigned i = 0; i != NumVars; ++i)
12478     Vars.push_back(Record.readSubExpr());
12479   C->setVarRefs(Vars);
12480 }
12481 
12482 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12483   C->setDepobj(Record.readSubExpr());
12484   C->setLParenLoc(Record.readSourceLocation());
12485 }
12486 
12487 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12488   C->setLParenLoc(Record.readSourceLocation());
12489   C->setModifier(Record.readSubExpr());
12490   C->setDependencyKind(
12491       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12492   C->setDependencyLoc(Record.readSourceLocation());
12493   C->setColonLoc(Record.readSourceLocation());
12494   unsigned NumVars = C->varlist_size();
12495   SmallVector<Expr *, 16> Vars;
12496   Vars.reserve(NumVars);
12497   for (unsigned I = 0; I != NumVars; ++I)
12498     Vars.push_back(Record.readSubExpr());
12499   C->setVarRefs(Vars);
12500   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12501     C->setLoopData(I, Record.readSubExpr());
12502 }
12503 
12504 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12505   VisitOMPClauseWithPreInit(C);
12506   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12507   C->setDevice(Record.readSubExpr());
12508   C->setModifierLoc(Record.readSourceLocation());
12509   C->setLParenLoc(Record.readSourceLocation());
12510 }
12511 
12512 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12513   C->setLParenLoc(Record.readSourceLocation());
12514   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12515     C->setMapTypeModifier(
12516         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12517     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12518   }
12519   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12520   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12521   C->setMapType(
12522      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12523   C->setMapLoc(Record.readSourceLocation());
12524   C->setColonLoc(Record.readSourceLocation());
12525   auto NumVars = C->varlist_size();
12526   auto UniqueDecls = C->getUniqueDeclarationsNum();
12527   auto TotalLists = C->getTotalComponentListNum();
12528   auto TotalComponents = C->getTotalComponentsNum();
12529 
12530   SmallVector<Expr *, 16> Vars;
12531   Vars.reserve(NumVars);
12532   for (unsigned i = 0; i != NumVars; ++i)
12533     Vars.push_back(Record.readExpr());
12534   C->setVarRefs(Vars);
12535 
12536   SmallVector<Expr *, 16> UDMappers;
12537   UDMappers.reserve(NumVars);
12538   for (unsigned I = 0; I < NumVars; ++I)
12539     UDMappers.push_back(Record.readExpr());
12540   C->setUDMapperRefs(UDMappers);
12541 
12542   SmallVector<ValueDecl *, 16> Decls;
12543   Decls.reserve(UniqueDecls);
12544   for (unsigned i = 0; i < UniqueDecls; ++i)
12545     Decls.push_back(Record.readDeclAs<ValueDecl>());
12546   C->setUniqueDecls(Decls);
12547 
12548   SmallVector<unsigned, 16> ListsPerDecl;
12549   ListsPerDecl.reserve(UniqueDecls);
12550   for (unsigned i = 0; i < UniqueDecls; ++i)
12551     ListsPerDecl.push_back(Record.readInt());
12552   C->setDeclNumLists(ListsPerDecl);
12553 
12554   SmallVector<unsigned, 32> ListSizes;
12555   ListSizes.reserve(TotalLists);
12556   for (unsigned i = 0; i < TotalLists; ++i)
12557     ListSizes.push_back(Record.readInt());
12558   C->setComponentListSizes(ListSizes);
12559 
12560   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12561   Components.reserve(TotalComponents);
12562   for (unsigned i = 0; i < TotalComponents; ++i) {
12563     Expr *AssociatedExprPr = Record.readExpr();
12564     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12565     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12566                             /*IsNonContiguous=*/false);
12567   }
12568   C->setComponents(Components, ListSizes);
12569 }
12570 
12571 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12572   C->setLParenLoc(Record.readSourceLocation());
12573   C->setColonLoc(Record.readSourceLocation());
12574   C->setAllocator(Record.readSubExpr());
12575   unsigned NumVars = C->varlist_size();
12576   SmallVector<Expr *, 16> Vars;
12577   Vars.reserve(NumVars);
12578   for (unsigned i = 0; i != NumVars; ++i)
12579     Vars.push_back(Record.readSubExpr());
12580   C->setVarRefs(Vars);
12581 }
12582 
12583 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12584   VisitOMPClauseWithPreInit(C);
12585   C->setNumTeams(Record.readSubExpr());
12586   C->setLParenLoc(Record.readSourceLocation());
12587 }
12588 
12589 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12590   VisitOMPClauseWithPreInit(C);
12591   C->setThreadLimit(Record.readSubExpr());
12592   C->setLParenLoc(Record.readSourceLocation());
12593 }
12594 
12595 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12596   VisitOMPClauseWithPreInit(C);
12597   C->setPriority(Record.readSubExpr());
12598   C->setLParenLoc(Record.readSourceLocation());
12599 }
12600 
12601 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12602   VisitOMPClauseWithPreInit(C);
12603   C->setGrainsize(Record.readSubExpr());
12604   C->setLParenLoc(Record.readSourceLocation());
12605 }
12606 
12607 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12608   VisitOMPClauseWithPreInit(C);
12609   C->setNumTasks(Record.readSubExpr());
12610   C->setLParenLoc(Record.readSourceLocation());
12611 }
12612 
12613 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12614   C->setHint(Record.readSubExpr());
12615   C->setLParenLoc(Record.readSourceLocation());
12616 }
12617 
12618 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12619   VisitOMPClauseWithPreInit(C);
12620   C->setDistScheduleKind(
12621       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12622   C->setChunkSize(Record.readSubExpr());
12623   C->setLParenLoc(Record.readSourceLocation());
12624   C->setDistScheduleKindLoc(Record.readSourceLocation());
12625   C->setCommaLoc(Record.readSourceLocation());
12626 }
12627 
12628 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12629   C->setDefaultmapKind(
12630        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12631   C->setDefaultmapModifier(
12632       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12633   C->setLParenLoc(Record.readSourceLocation());
12634   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12635   C->setDefaultmapKindLoc(Record.readSourceLocation());
12636 }
12637 
12638 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12639   C->setLParenLoc(Record.readSourceLocation());
12640   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12641     C->setMotionModifier(
12642         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12643     C->setMotionModifierLoc(I, Record.readSourceLocation());
12644   }
12645   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12646   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12647   C->setColonLoc(Record.readSourceLocation());
12648   auto NumVars = C->varlist_size();
12649   auto UniqueDecls = C->getUniqueDeclarationsNum();
12650   auto TotalLists = C->getTotalComponentListNum();
12651   auto TotalComponents = C->getTotalComponentsNum();
12652 
12653   SmallVector<Expr *, 16> Vars;
12654   Vars.reserve(NumVars);
12655   for (unsigned i = 0; i != NumVars; ++i)
12656     Vars.push_back(Record.readSubExpr());
12657   C->setVarRefs(Vars);
12658 
12659   SmallVector<Expr *, 16> UDMappers;
12660   UDMappers.reserve(NumVars);
12661   for (unsigned I = 0; I < NumVars; ++I)
12662     UDMappers.push_back(Record.readSubExpr());
12663   C->setUDMapperRefs(UDMappers);
12664 
12665   SmallVector<ValueDecl *, 16> Decls;
12666   Decls.reserve(UniqueDecls);
12667   for (unsigned i = 0; i < UniqueDecls; ++i)
12668     Decls.push_back(Record.readDeclAs<ValueDecl>());
12669   C->setUniqueDecls(Decls);
12670 
12671   SmallVector<unsigned, 16> ListsPerDecl;
12672   ListsPerDecl.reserve(UniqueDecls);
12673   for (unsigned i = 0; i < UniqueDecls; ++i)
12674     ListsPerDecl.push_back(Record.readInt());
12675   C->setDeclNumLists(ListsPerDecl);
12676 
12677   SmallVector<unsigned, 32> ListSizes;
12678   ListSizes.reserve(TotalLists);
12679   for (unsigned i = 0; i < TotalLists; ++i)
12680     ListSizes.push_back(Record.readInt());
12681   C->setComponentListSizes(ListSizes);
12682 
12683   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12684   Components.reserve(TotalComponents);
12685   for (unsigned i = 0; i < TotalComponents; ++i) {
12686     Expr *AssociatedExprPr = Record.readSubExpr();
12687     bool IsNonContiguous = Record.readBool();
12688     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12689     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12690   }
12691   C->setComponents(Components, ListSizes);
12692 }
12693 
12694 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12695   C->setLParenLoc(Record.readSourceLocation());
12696   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12697     C->setMotionModifier(
12698         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12699     C->setMotionModifierLoc(I, Record.readSourceLocation());
12700   }
12701   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12702   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12703   C->setColonLoc(Record.readSourceLocation());
12704   auto NumVars = C->varlist_size();
12705   auto UniqueDecls = C->getUniqueDeclarationsNum();
12706   auto TotalLists = C->getTotalComponentListNum();
12707   auto TotalComponents = C->getTotalComponentsNum();
12708 
12709   SmallVector<Expr *, 16> Vars;
12710   Vars.reserve(NumVars);
12711   for (unsigned i = 0; i != NumVars; ++i)
12712     Vars.push_back(Record.readSubExpr());
12713   C->setVarRefs(Vars);
12714 
12715   SmallVector<Expr *, 16> UDMappers;
12716   UDMappers.reserve(NumVars);
12717   for (unsigned I = 0; I < NumVars; ++I)
12718     UDMappers.push_back(Record.readSubExpr());
12719   C->setUDMapperRefs(UDMappers);
12720 
12721   SmallVector<ValueDecl *, 16> Decls;
12722   Decls.reserve(UniqueDecls);
12723   for (unsigned i = 0; i < UniqueDecls; ++i)
12724     Decls.push_back(Record.readDeclAs<ValueDecl>());
12725   C->setUniqueDecls(Decls);
12726 
12727   SmallVector<unsigned, 16> ListsPerDecl;
12728   ListsPerDecl.reserve(UniqueDecls);
12729   for (unsigned i = 0; i < UniqueDecls; ++i)
12730     ListsPerDecl.push_back(Record.readInt());
12731   C->setDeclNumLists(ListsPerDecl);
12732 
12733   SmallVector<unsigned, 32> ListSizes;
12734   ListSizes.reserve(TotalLists);
12735   for (unsigned i = 0; i < TotalLists; ++i)
12736     ListSizes.push_back(Record.readInt());
12737   C->setComponentListSizes(ListSizes);
12738 
12739   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12740   Components.reserve(TotalComponents);
12741   for (unsigned i = 0; i < TotalComponents; ++i) {
12742     Expr *AssociatedExprPr = Record.readSubExpr();
12743     bool IsNonContiguous = Record.readBool();
12744     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12745     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12746   }
12747   C->setComponents(Components, ListSizes);
12748 }
12749 
12750 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12751   C->setLParenLoc(Record.readSourceLocation());
12752   auto NumVars = C->varlist_size();
12753   auto UniqueDecls = C->getUniqueDeclarationsNum();
12754   auto TotalLists = C->getTotalComponentListNum();
12755   auto TotalComponents = C->getTotalComponentsNum();
12756 
12757   SmallVector<Expr *, 16> Vars;
12758   Vars.reserve(NumVars);
12759   for (unsigned i = 0; i != NumVars; ++i)
12760     Vars.push_back(Record.readSubExpr());
12761   C->setVarRefs(Vars);
12762   Vars.clear();
12763   for (unsigned i = 0; i != NumVars; ++i)
12764     Vars.push_back(Record.readSubExpr());
12765   C->setPrivateCopies(Vars);
12766   Vars.clear();
12767   for (unsigned i = 0; i != NumVars; ++i)
12768     Vars.push_back(Record.readSubExpr());
12769   C->setInits(Vars);
12770 
12771   SmallVector<ValueDecl *, 16> Decls;
12772   Decls.reserve(UniqueDecls);
12773   for (unsigned i = 0; i < UniqueDecls; ++i)
12774     Decls.push_back(Record.readDeclAs<ValueDecl>());
12775   C->setUniqueDecls(Decls);
12776 
12777   SmallVector<unsigned, 16> ListsPerDecl;
12778   ListsPerDecl.reserve(UniqueDecls);
12779   for (unsigned i = 0; i < UniqueDecls; ++i)
12780     ListsPerDecl.push_back(Record.readInt());
12781   C->setDeclNumLists(ListsPerDecl);
12782 
12783   SmallVector<unsigned, 32> ListSizes;
12784   ListSizes.reserve(TotalLists);
12785   for (unsigned i = 0; i < TotalLists; ++i)
12786     ListSizes.push_back(Record.readInt());
12787   C->setComponentListSizes(ListSizes);
12788 
12789   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12790   Components.reserve(TotalComponents);
12791   for (unsigned i = 0; i < TotalComponents; ++i) {
12792     auto *AssociatedExprPr = Record.readSubExpr();
12793     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12794     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12795                             /*IsNonContiguous=*/false);
12796   }
12797   C->setComponents(Components, ListSizes);
12798 }
12799 
12800 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12801   C->setLParenLoc(Record.readSourceLocation());
12802   auto NumVars = C->varlist_size();
12803   auto UniqueDecls = C->getUniqueDeclarationsNum();
12804   auto TotalLists = C->getTotalComponentListNum();
12805   auto TotalComponents = C->getTotalComponentsNum();
12806 
12807   SmallVector<Expr *, 16> Vars;
12808   Vars.reserve(NumVars);
12809   for (unsigned i = 0; i != NumVars; ++i)
12810     Vars.push_back(Record.readSubExpr());
12811   C->setVarRefs(Vars);
12812 
12813   SmallVector<ValueDecl *, 16> Decls;
12814   Decls.reserve(UniqueDecls);
12815   for (unsigned i = 0; i < UniqueDecls; ++i)
12816     Decls.push_back(Record.readDeclAs<ValueDecl>());
12817   C->setUniqueDecls(Decls);
12818 
12819   SmallVector<unsigned, 16> ListsPerDecl;
12820   ListsPerDecl.reserve(UniqueDecls);
12821   for (unsigned i = 0; i < UniqueDecls; ++i)
12822     ListsPerDecl.push_back(Record.readInt());
12823   C->setDeclNumLists(ListsPerDecl);
12824 
12825   SmallVector<unsigned, 32> ListSizes;
12826   ListSizes.reserve(TotalLists);
12827   for (unsigned i = 0; i < TotalLists; ++i)
12828     ListSizes.push_back(Record.readInt());
12829   C->setComponentListSizes(ListSizes);
12830 
12831   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12832   Components.reserve(TotalComponents);
12833   for (unsigned i = 0; i < TotalComponents; ++i) {
12834     Expr *AssociatedExpr = Record.readSubExpr();
12835     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12836     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12837                             /*IsNonContiguous*/ false);
12838   }
12839   C->setComponents(Components, ListSizes);
12840 }
12841 
12842 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12843   C->setLParenLoc(Record.readSourceLocation());
12844   auto NumVars = C->varlist_size();
12845   auto UniqueDecls = C->getUniqueDeclarationsNum();
12846   auto TotalLists = C->getTotalComponentListNum();
12847   auto TotalComponents = C->getTotalComponentsNum();
12848 
12849   SmallVector<Expr *, 16> Vars;
12850   Vars.reserve(NumVars);
12851   for (unsigned i = 0; i != NumVars; ++i)
12852     Vars.push_back(Record.readSubExpr());
12853   C->setVarRefs(Vars);
12854   Vars.clear();
12855 
12856   SmallVector<ValueDecl *, 16> Decls;
12857   Decls.reserve(UniqueDecls);
12858   for (unsigned i = 0; i < UniqueDecls; ++i)
12859     Decls.push_back(Record.readDeclAs<ValueDecl>());
12860   C->setUniqueDecls(Decls);
12861 
12862   SmallVector<unsigned, 16> ListsPerDecl;
12863   ListsPerDecl.reserve(UniqueDecls);
12864   for (unsigned i = 0; i < UniqueDecls; ++i)
12865     ListsPerDecl.push_back(Record.readInt());
12866   C->setDeclNumLists(ListsPerDecl);
12867 
12868   SmallVector<unsigned, 32> ListSizes;
12869   ListSizes.reserve(TotalLists);
12870   for (unsigned i = 0; i < TotalLists; ++i)
12871     ListSizes.push_back(Record.readInt());
12872   C->setComponentListSizes(ListSizes);
12873 
12874   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12875   Components.reserve(TotalComponents);
12876   for (unsigned i = 0; i < TotalComponents; ++i) {
12877     Expr *AssociatedExpr = Record.readSubExpr();
12878     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12879     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12880                             /*IsNonContiguous=*/false);
12881   }
12882   C->setComponents(Components, ListSizes);
12883 }
12884 
12885 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12886   C->setLParenLoc(Record.readSourceLocation());
12887   auto NumVars = C->varlist_size();
12888   auto UniqueDecls = C->getUniqueDeclarationsNum();
12889   auto TotalLists = C->getTotalComponentListNum();
12890   auto TotalComponents = C->getTotalComponentsNum();
12891 
12892   SmallVector<Expr *, 16> Vars;
12893   Vars.reserve(NumVars);
12894   for (unsigned I = 0; I != NumVars; ++I)
12895     Vars.push_back(Record.readSubExpr());
12896   C->setVarRefs(Vars);
12897   Vars.clear();
12898 
12899   SmallVector<ValueDecl *, 16> Decls;
12900   Decls.reserve(UniqueDecls);
12901   for (unsigned I = 0; I < UniqueDecls; ++I)
12902     Decls.push_back(Record.readDeclAs<ValueDecl>());
12903   C->setUniqueDecls(Decls);
12904 
12905   SmallVector<unsigned, 16> ListsPerDecl;
12906   ListsPerDecl.reserve(UniqueDecls);
12907   for (unsigned I = 0; I < UniqueDecls; ++I)
12908     ListsPerDecl.push_back(Record.readInt());
12909   C->setDeclNumLists(ListsPerDecl);
12910 
12911   SmallVector<unsigned, 32> ListSizes;
12912   ListSizes.reserve(TotalLists);
12913   for (unsigned i = 0; i < TotalLists; ++i)
12914     ListSizes.push_back(Record.readInt());
12915   C->setComponentListSizes(ListSizes);
12916 
12917   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12918   Components.reserve(TotalComponents);
12919   for (unsigned I = 0; I < TotalComponents; ++I) {
12920     Expr *AssociatedExpr = Record.readSubExpr();
12921     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12922     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12923                             /*IsNonContiguous=*/false);
12924   }
12925   C->setComponents(Components, ListSizes);
12926 }
12927 
12928 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12929   C->setLParenLoc(Record.readSourceLocation());
12930   unsigned NumVars = C->varlist_size();
12931   SmallVector<Expr *, 16> Vars;
12932   Vars.reserve(NumVars);
12933   for (unsigned i = 0; i != NumVars; ++i)
12934     Vars.push_back(Record.readSubExpr());
12935   C->setVarRefs(Vars);
12936   Vars.clear();
12937   Vars.reserve(NumVars);
12938   for (unsigned i = 0; i != NumVars; ++i)
12939     Vars.push_back(Record.readSubExpr());
12940   C->setPrivateRefs(Vars);
12941 }
12942 
12943 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12944   C->setLParenLoc(Record.readSourceLocation());
12945   unsigned NumVars = C->varlist_size();
12946   SmallVector<Expr *, 16> Vars;
12947   Vars.reserve(NumVars);
12948   for (unsigned i = 0; i != NumVars; ++i)
12949     Vars.push_back(Record.readSubExpr());
12950   C->setVarRefs(Vars);
12951 }
12952 
12953 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12954   C->setLParenLoc(Record.readSourceLocation());
12955   unsigned NumVars = C->varlist_size();
12956   SmallVector<Expr *, 16> Vars;
12957   Vars.reserve(NumVars);
12958   for (unsigned i = 0; i != NumVars; ++i)
12959     Vars.push_back(Record.readSubExpr());
12960   C->setVarRefs(Vars);
12961 }
12962 
12963 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12964   C->setLParenLoc(Record.readSourceLocation());
12965   unsigned NumOfAllocators = C->getNumberOfAllocators();
12966   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12967   Data.reserve(NumOfAllocators);
12968   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12969     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12970     D.Allocator = Record.readSubExpr();
12971     D.AllocatorTraits = Record.readSubExpr();
12972     D.LParenLoc = Record.readSourceLocation();
12973     D.RParenLoc = Record.readSourceLocation();
12974   }
12975   C->setAllocatorsData(Data);
12976 }
12977 
12978 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12979   C->setLParenLoc(Record.readSourceLocation());
12980   C->setModifier(Record.readSubExpr());
12981   C->setColonLoc(Record.readSourceLocation());
12982   unsigned NumOfLocators = C->varlist_size();
12983   SmallVector<Expr *, 4> Locators;
12984   Locators.reserve(NumOfLocators);
12985   for (unsigned I = 0; I != NumOfLocators; ++I)
12986     Locators.push_back(Record.readSubExpr());
12987   C->setVarRefs(Locators);
12988 }
12989 
12990 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12991   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12992   C->setLParenLoc(Record.readSourceLocation());
12993   C->setKindKwLoc(Record.readSourceLocation());
12994 }
12995 
12996 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12997   VisitOMPClauseWithPreInit(C);
12998   C->setThreadID(Record.readSubExpr());
12999   C->setLParenLoc(Record.readSourceLocation());
13000 }
13001 
13002 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13003   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13004   C->setLParenLoc(Record.readSourceLocation());
13005   C->setBindKindLoc(Record.readSourceLocation());
13006 }
13007 
13008 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13009   C->setAlignment(Record.readExpr());
13010   C->setLParenLoc(Record.readSourceLocation());
13011 }
13012 
13013 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13014   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13015   TI.Sets.resize(readUInt32());
13016   for (auto &Set : TI.Sets) {
13017     Set.Kind = readEnum<llvm::omp::TraitSet>();
13018     Set.Selectors.resize(readUInt32());
13019     for (auto &Selector : Set.Selectors) {
13020       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13021       Selector.ScoreOrCondition = nullptr;
13022       if (readBool())
13023         Selector.ScoreOrCondition = readExprRef();
13024       Selector.Properties.resize(readUInt32());
13025       for (auto &Property : Selector.Properties)
13026         Property.Kind = readEnum<llvm::omp::TraitProperty>();
13027     }
13028   }
13029   return &TI;
13030 }
13031 
13032 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13033   if (!Data)
13034     return;
13035   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13036     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13037     skipInts(3);
13038   }
13039   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13040   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13041     Clauses[I] = readOMPClause();
13042   Data->setClauses(Clauses);
13043   if (Data->hasAssociatedStmt())
13044     Data->setAssociatedStmt(readStmt());
13045   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13046     Data->getChildren()[I] = readStmt();
13047 }
13048