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