xref: /freebsd-src/contrib/llvm-project/clang/lib/Serialization/ASTReader.cpp (revision 972a253a57b6f144b0e4a3e2080a2a0076ec55a0)
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 static unsigned computeODRHash(QualType Ty) {
9449   ODRHash Hasher;
9450   Hasher.AddQualType(Ty);
9451   return Hasher.CalculateHash();
9452 }
9453 
9454 static unsigned computeODRHash(const Stmt *S) {
9455   ODRHash Hasher;
9456   Hasher.AddStmt(S);
9457   return Hasher.CalculateHash();
9458 }
9459 
9460 static unsigned computeODRHash(const Decl *D) {
9461   assert(D);
9462   ODRHash Hasher;
9463   Hasher.AddSubDecl(D);
9464   return Hasher.CalculateHash();
9465 }
9466 
9467 static unsigned computeODRHash(const TemplateArgument &TA) {
9468   ODRHash Hasher;
9469   Hasher.AddTemplateArgument(TA);
9470   return Hasher.CalculateHash();
9471 }
9472 
9473 void ASTReader::diagnoseOdrViolations() {
9474   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9475       PendingFunctionOdrMergeFailures.empty() &&
9476       PendingEnumOdrMergeFailures.empty())
9477     return;
9478 
9479   // Trigger the import of the full definition of each class that had any
9480   // odr-merging problems, so we can produce better diagnostics for them.
9481   // These updates may in turn find and diagnose some ODR failures, so take
9482   // ownership of the set first.
9483   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9484   PendingOdrMergeFailures.clear();
9485   for (auto &Merge : OdrMergeFailures) {
9486     Merge.first->buildLookup();
9487     Merge.first->decls_begin();
9488     Merge.first->bases_begin();
9489     Merge.first->vbases_begin();
9490     for (auto &RecordPair : Merge.second) {
9491       auto *RD = RecordPair.first;
9492       RD->decls_begin();
9493       RD->bases_begin();
9494       RD->vbases_begin();
9495     }
9496   }
9497 
9498   // Trigger the import of functions.
9499   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9500   PendingFunctionOdrMergeFailures.clear();
9501   for (auto &Merge : FunctionOdrMergeFailures) {
9502     Merge.first->buildLookup();
9503     Merge.first->decls_begin();
9504     Merge.first->getBody();
9505     for (auto &FD : Merge.second) {
9506       FD->buildLookup();
9507       FD->decls_begin();
9508       FD->getBody();
9509     }
9510   }
9511 
9512   // Trigger the import of enums.
9513   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9514   PendingEnumOdrMergeFailures.clear();
9515   for (auto &Merge : EnumOdrMergeFailures) {
9516     Merge.first->decls_begin();
9517     for (auto &Enum : Merge.second) {
9518       Enum->decls_begin();
9519     }
9520   }
9521 
9522   // For each declaration from a merged context, check that the canonical
9523   // definition of that context also contains a declaration of the same
9524   // entity.
9525   //
9526   // Caution: this loop does things that might invalidate iterators into
9527   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9528   while (!PendingOdrMergeChecks.empty()) {
9529     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9530 
9531     // FIXME: Skip over implicit declarations for now. This matters for things
9532     // like implicitly-declared special member functions. This isn't entirely
9533     // correct; we can end up with multiple unmerged declarations of the same
9534     // implicit entity.
9535     if (D->isImplicit())
9536       continue;
9537 
9538     DeclContext *CanonDef = D->getDeclContext();
9539 
9540     bool Found = false;
9541     const Decl *DCanon = D->getCanonicalDecl();
9542 
9543     for (auto RI : D->redecls()) {
9544       if (RI->getLexicalDeclContext() == CanonDef) {
9545         Found = true;
9546         break;
9547       }
9548     }
9549     if (Found)
9550       continue;
9551 
9552     // Quick check failed, time to do the slow thing. Note, we can't just
9553     // look up the name of D in CanonDef here, because the member that is
9554     // in CanonDef might not be found by name lookup (it might have been
9555     // replaced by a more recent declaration in the lookup table), and we
9556     // can't necessarily find it in the redeclaration chain because it might
9557     // be merely mergeable, not redeclarable.
9558     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9559     for (auto *CanonMember : CanonDef->decls()) {
9560       if (CanonMember->getCanonicalDecl() == DCanon) {
9561         // This can happen if the declaration is merely mergeable and not
9562         // actually redeclarable (we looked for redeclarations earlier).
9563         //
9564         // FIXME: We should be able to detect this more efficiently, without
9565         // pulling in all of the members of CanonDef.
9566         Found = true;
9567         break;
9568       }
9569       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9570         if (ND->getDeclName() == D->getDeclName())
9571           Candidates.push_back(ND);
9572     }
9573 
9574     if (!Found) {
9575       // The AST doesn't like TagDecls becoming invalid after they've been
9576       // completed. We only really need to mark FieldDecls as invalid here.
9577       if (!isa<TagDecl>(D))
9578         D->setInvalidDecl();
9579 
9580       // Ensure we don't accidentally recursively enter deserialization while
9581       // we're producing our diagnostic.
9582       Deserializing RecursionGuard(this);
9583 
9584       std::string CanonDefModule =
9585           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9586       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9587         << D << getOwningModuleNameForDiagnostic(D)
9588         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9589 
9590       if (Candidates.empty())
9591         Diag(cast<Decl>(CanonDef)->getLocation(),
9592              diag::note_module_odr_violation_no_possible_decls) << D;
9593       else {
9594         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9595           Diag(Candidates[I]->getLocation(),
9596                diag::note_module_odr_violation_possible_decl)
9597             << Candidates[I];
9598       }
9599 
9600       DiagnosedOdrMergeFailures.insert(CanonDef);
9601     }
9602   }
9603 
9604   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9605       EnumOdrMergeFailures.empty())
9606     return;
9607 
9608   // Ensure we don't accidentally recursively enter deserialization while
9609   // we're producing our diagnostics.
9610   Deserializing RecursionGuard(this);
9611 
9612   // Used with err_module_odr_violation_mismatch_decl and
9613   // note_module_odr_violation_mismatch_decl
9614   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9615   enum ODRMismatchDecl {
9616     EndOfClass,
9617     PublicSpecifer,
9618     PrivateSpecifer,
9619     ProtectedSpecifer,
9620     StaticAssert,
9621     Field,
9622     CXXMethod,
9623     TypeAlias,
9624     TypeDef,
9625     Var,
9626     Friend,
9627     FunctionTemplate,
9628     Other
9629   };
9630 
9631   // These lambdas have the common portions of the ODR diagnostics.  This
9632   // has the same return as Diag(), so addition parameters can be passed
9633   // in with operator<<
9634   auto ODRDiagField = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9635                              StringRef SecondModule,
9636                              const FieldDecl *FirstField,
9637                              const FieldDecl *SecondField) {
9638     enum ODRFieldDifference {
9639       FieldName,
9640       FieldTypeName,
9641       FieldSingleBitField,
9642       FieldDifferentWidthBitField,
9643       FieldSingleMutable,
9644       FieldSingleInitializer,
9645       FieldDifferentInitializers,
9646     };
9647 
9648     auto DiagError = [FirstRecord, FirstField, FirstModule,
9649                       this](ODRFieldDifference DiffType) {
9650       return Diag(FirstField->getLocation(),
9651                   diag::err_module_odr_violation_field)
9652              << FirstRecord << FirstModule.empty() << FirstModule
9653              << FirstField->getSourceRange() << DiffType;
9654     };
9655     auto DiagNote = [SecondField, SecondModule,
9656                      this](ODRFieldDifference DiffType) {
9657       return Diag(SecondField->getLocation(),
9658                   diag::note_module_odr_violation_field)
9659              << SecondModule << SecondField->getSourceRange() << DiffType;
9660     };
9661 
9662     IdentifierInfo *FirstII = FirstField->getIdentifier();
9663     IdentifierInfo *SecondII = SecondField->getIdentifier();
9664     if (FirstII->getName() != SecondII->getName()) {
9665       DiagError(FieldName) << FirstII;
9666       DiagNote(FieldName) << SecondII;
9667       return true;
9668     }
9669 
9670     assert(getContext().hasSameType(FirstField->getType(),
9671                                     SecondField->getType()));
9672 
9673     QualType FirstType = FirstField->getType();
9674     QualType SecondType = SecondField->getType();
9675     if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
9676       DiagError(FieldTypeName) << FirstII << FirstType;
9677       DiagNote(FieldTypeName) << SecondII << SecondType;
9678       return true;
9679     }
9680 
9681     const bool IsFirstBitField = FirstField->isBitField();
9682     const bool IsSecondBitField = SecondField->isBitField();
9683     if (IsFirstBitField != IsSecondBitField) {
9684       DiagError(FieldSingleBitField) << FirstII << IsFirstBitField;
9685       DiagNote(FieldSingleBitField) << SecondII << IsSecondBitField;
9686       return true;
9687     }
9688 
9689     if (IsFirstBitField && IsSecondBitField) {
9690       unsigned FirstBitWidthHash = computeODRHash(FirstField->getBitWidth());
9691       unsigned SecondBitWidthHash = computeODRHash(SecondField->getBitWidth());
9692       if (FirstBitWidthHash != SecondBitWidthHash) {
9693         DiagError(FieldDifferentWidthBitField)
9694             << FirstII << FirstField->getBitWidth()->getSourceRange();
9695         DiagNote(FieldDifferentWidthBitField)
9696             << SecondII << SecondField->getBitWidth()->getSourceRange();
9697         return true;
9698       }
9699     }
9700 
9701     if (!PP.getLangOpts().CPlusPlus)
9702       return false;
9703 
9704     const bool IsFirstMutable = FirstField->isMutable();
9705     const bool IsSecondMutable = SecondField->isMutable();
9706     if (IsFirstMutable != IsSecondMutable) {
9707       DiagError(FieldSingleMutable) << FirstII << IsFirstMutable;
9708       DiagNote(FieldSingleMutable) << SecondII << IsSecondMutable;
9709       return true;
9710     }
9711 
9712     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9713     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9714     if ((!FirstInitializer && SecondInitializer) ||
9715         (FirstInitializer && !SecondInitializer)) {
9716       DiagError(FieldSingleInitializer)
9717           << FirstII << (FirstInitializer != nullptr);
9718       DiagNote(FieldSingleInitializer)
9719           << SecondII << (SecondInitializer != nullptr);
9720       return true;
9721     }
9722 
9723     if (FirstInitializer && SecondInitializer) {
9724       unsigned FirstInitHash = computeODRHash(FirstInitializer);
9725       unsigned SecondInitHash = computeODRHash(SecondInitializer);
9726       if (FirstInitHash != SecondInitHash) {
9727         DiagError(FieldDifferentInitializers)
9728             << FirstII << FirstInitializer->getSourceRange();
9729         DiagNote(FieldDifferentInitializers)
9730             << SecondII << SecondInitializer->getSourceRange();
9731         return true;
9732       }
9733     }
9734 
9735     return false;
9736   };
9737 
9738   auto ODRDiagTypeDefOrAlias =
9739       [this](NamedDecl *FirstRecord, StringRef FirstModule,
9740              StringRef SecondModule, const TypedefNameDecl *FirstTD,
9741              const TypedefNameDecl *SecondTD, bool IsTypeAlias) {
9742         enum ODRTypedefDifference {
9743           TypedefName,
9744           TypedefType,
9745         };
9746 
9747         auto DiagError = [FirstRecord, FirstTD, FirstModule,
9748                           this](ODRTypedefDifference DiffType) {
9749           return Diag(FirstTD->getLocation(),
9750                       diag::err_module_odr_violation_typedef)
9751                  << FirstRecord << FirstModule.empty() << FirstModule
9752                  << FirstTD->getSourceRange() << DiffType;
9753         };
9754         auto DiagNote = [SecondTD, SecondModule,
9755                          this](ODRTypedefDifference DiffType) {
9756           return Diag(SecondTD->getLocation(),
9757                       diag::note_module_odr_violation_typedef)
9758                  << SecondModule << SecondTD->getSourceRange() << DiffType;
9759         };
9760 
9761         DeclarationName FirstName = FirstTD->getDeclName();
9762         DeclarationName SecondName = SecondTD->getDeclName();
9763         if (FirstName != SecondName) {
9764           DiagError(TypedefName) << IsTypeAlias << FirstName;
9765           DiagNote(TypedefName) << IsTypeAlias << SecondName;
9766           return true;
9767         }
9768 
9769         QualType FirstType = FirstTD->getUnderlyingType();
9770         QualType SecondType = SecondTD->getUnderlyingType();
9771         if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
9772           DiagError(TypedefType) << IsTypeAlias << FirstName << FirstType;
9773           DiagNote(TypedefType) << IsTypeAlias << SecondName << SecondType;
9774           return true;
9775         }
9776 
9777         return false;
9778       };
9779 
9780   auto ODRDiagVar = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9781                            StringRef SecondModule, const VarDecl *FirstVD,
9782                            const VarDecl *SecondVD) {
9783     enum ODRVarDifference {
9784       VarName,
9785       VarType,
9786       VarSingleInitializer,
9787       VarDifferentInitializer,
9788       VarConstexpr,
9789     };
9790 
9791     auto DiagError = [FirstRecord, FirstVD, FirstModule,
9792                       this](ODRVarDifference DiffType) {
9793       return Diag(FirstVD->getLocation(),
9794                   diag::err_module_odr_violation_variable)
9795              << FirstRecord << FirstModule.empty() << FirstModule
9796              << FirstVD->getSourceRange() << DiffType;
9797     };
9798     auto DiagNote = [SecondVD, SecondModule, this](ODRVarDifference DiffType) {
9799       return Diag(SecondVD->getLocation(),
9800                   diag::note_module_odr_violation_variable)
9801              << SecondModule << SecondVD->getSourceRange() << DiffType;
9802     };
9803 
9804     DeclarationName FirstName = FirstVD->getDeclName();
9805     DeclarationName SecondName = SecondVD->getDeclName();
9806     if (FirstName != SecondName) {
9807       DiagError(VarName) << FirstName;
9808       DiagNote(VarName) << SecondName;
9809       return true;
9810     }
9811 
9812     QualType FirstType = FirstVD->getType();
9813     QualType SecondType = SecondVD->getType();
9814     if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
9815       DiagError(VarType) << FirstName << FirstType;
9816       DiagNote(VarType) << SecondName << SecondType;
9817       return true;
9818     }
9819 
9820     if (!PP.getLangOpts().CPlusPlus)
9821       return false;
9822 
9823     const Expr *FirstInit = FirstVD->getInit();
9824     const Expr *SecondInit = SecondVD->getInit();
9825     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9826       DiagError(VarSingleInitializer)
9827           << FirstName << (FirstInit == nullptr)
9828           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9829       DiagNote(VarSingleInitializer)
9830           << SecondName << (SecondInit == nullptr)
9831           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9832       return true;
9833     }
9834 
9835     if (FirstInit && SecondInit &&
9836         computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
9837       DiagError(VarDifferentInitializer)
9838           << FirstName << FirstInit->getSourceRange();
9839       DiagNote(VarDifferentInitializer)
9840           << SecondName << SecondInit->getSourceRange();
9841       return true;
9842     }
9843 
9844     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9845     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9846     if (FirstIsConstexpr != SecondIsConstexpr) {
9847       DiagError(VarConstexpr) << FirstName << FirstIsConstexpr;
9848       DiagNote(VarConstexpr) << SecondName << SecondIsConstexpr;
9849       return true;
9850     }
9851     return false;
9852   };
9853 
9854   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9855   auto PopulateHashes = [](DeclHashes &Hashes, RecordDecl *Record,
9856                            const DeclContext *DC) {
9857     for (auto *D : Record->decls()) {
9858       if (!ODRHash::isDeclToBeProcessed(D, DC))
9859         continue;
9860       Hashes.emplace_back(D, computeODRHash(D));
9861     }
9862   };
9863 
9864   struct DiffResult {
9865     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9866     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9867   };
9868 
9869   // If there is a diagnoseable difference, FirstDiffType and
9870   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9871   // filled in if not EndOfClass.
9872   auto FindTypeDiffs = [](DeclHashes &FirstHashes, DeclHashes &SecondHashes) {
9873     auto DifferenceSelector = [](Decl *D) {
9874       assert(D && "valid Decl required");
9875       switch (D->getKind()) {
9876       default:
9877         return Other;
9878       case Decl::AccessSpec:
9879         switch (D->getAccess()) {
9880         case AS_public:
9881           return PublicSpecifer;
9882         case AS_private:
9883           return PrivateSpecifer;
9884         case AS_protected:
9885           return ProtectedSpecifer;
9886         case AS_none:
9887           break;
9888         }
9889         llvm_unreachable("Invalid access specifier");
9890       case Decl::StaticAssert:
9891         return StaticAssert;
9892       case Decl::Field:
9893         return Field;
9894       case Decl::CXXMethod:
9895       case Decl::CXXConstructor:
9896       case Decl::CXXDestructor:
9897         return CXXMethod;
9898       case Decl::TypeAlias:
9899         return TypeAlias;
9900       case Decl::Typedef:
9901         return TypeDef;
9902       case Decl::Var:
9903         return Var;
9904       case Decl::Friend:
9905         return Friend;
9906       case Decl::FunctionTemplate:
9907         return FunctionTemplate;
9908       }
9909     };
9910 
9911     DiffResult DR;
9912     auto FirstIt = FirstHashes.begin();
9913     auto SecondIt = SecondHashes.begin();
9914     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9915       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9916           FirstIt->second == SecondIt->second) {
9917         ++FirstIt;
9918         ++SecondIt;
9919         continue;
9920       }
9921 
9922       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9923       DR.SecondDecl =
9924           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9925 
9926       DR.FirstDiffType =
9927           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9928       DR.SecondDiffType =
9929           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9930       return DR;
9931     }
9932     return DR;
9933   };
9934 
9935   // Use this to diagnose that an unexpected Decl was encountered
9936   // or no difference was detected. This causes a generic error
9937   // message to be emitted.
9938   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9939                                       StringRef FirstModule,
9940                                       NamedDecl *SecondRecord,
9941                                       StringRef SecondModule) {
9942     Diag(FirstRecord->getLocation(),
9943          diag::err_module_odr_violation_different_definitions)
9944         << FirstRecord << FirstModule.empty() << FirstModule;
9945 
9946     if (DR.FirstDecl) {
9947       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9948           << FirstRecord << DR.FirstDecl->getSourceRange();
9949     }
9950 
9951     Diag(SecondRecord->getLocation(),
9952          diag::note_module_odr_violation_different_definitions)
9953         << SecondModule;
9954 
9955     if (DR.SecondDecl) {
9956       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9957           << DR.SecondDecl->getSourceRange();
9958     }
9959   };
9960 
9961   auto DiagnoseODRMismatch = [this](DiffResult &DR, NamedDecl *FirstRecord,
9962                                     StringRef FirstModule,
9963                                     NamedDecl *SecondRecord,
9964                                     StringRef SecondModule) {
9965     auto GetMismatchedDeclLoc = [](const NamedDecl *Container,
9966                                    ODRMismatchDecl DiffType, const Decl *D) {
9967       SourceLocation Loc;
9968       SourceRange Range;
9969       auto *Tag = dyn_cast<TagDecl>(Container);
9970       if (DiffType == EndOfClass && Tag) {
9971         Loc = Tag->getBraceRange().getEnd();
9972       } else {
9973         Loc = D->getLocation();
9974         Range = D->getSourceRange();
9975       }
9976       return std::make_pair(Loc, Range);
9977     };
9978 
9979     auto FirstDiagInfo =
9980         GetMismatchedDeclLoc(FirstRecord, DR.FirstDiffType, DR.FirstDecl);
9981     Diag(FirstDiagInfo.first, diag::err_module_odr_violation_mismatch_decl)
9982         << FirstRecord << FirstModule.empty() << FirstModule
9983         << FirstDiagInfo.second << DR.FirstDiffType;
9984 
9985     auto SecondDiagInfo =
9986         GetMismatchedDeclLoc(SecondRecord, DR.SecondDiffType, DR.SecondDecl);
9987     Diag(SecondDiagInfo.first, diag::note_module_odr_violation_mismatch_decl)
9988         << SecondModule << SecondDiagInfo.second << DR.SecondDiffType;
9989   };
9990 
9991   // Issue any pending ODR-failure diagnostics.
9992   for (auto &Merge : OdrMergeFailures) {
9993     // If we've already pointed out a specific problem with this class, don't
9994     // bother issuing a general "something's different" diagnostic.
9995     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9996       continue;
9997 
9998     bool Diagnosed = false;
9999     CXXRecordDecl *FirstRecord = Merge.first;
10000     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10001     for (auto &RecordPair : Merge.second) {
10002       CXXRecordDecl *SecondRecord = RecordPair.first;
10003       // Multiple different declarations got merged together; tell the user
10004       // where they came from.
10005       if (FirstRecord == SecondRecord)
10006         continue;
10007 
10008       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10009 
10010       auto *FirstDD = FirstRecord->DefinitionData;
10011       auto *SecondDD = RecordPair.second;
10012 
10013       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10014 
10015       // Diagnostics from DefinitionData are emitted here.
10016       if (FirstDD != SecondDD) {
10017         enum ODRDefinitionDataDifference {
10018           NumBases,
10019           NumVBases,
10020           BaseType,
10021           BaseVirtual,
10022           BaseAccess,
10023         };
10024         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10025                                  this](SourceLocation Loc, SourceRange Range,
10026                                        ODRDefinitionDataDifference DiffType) {
10027           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10028                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10029                  << DiffType;
10030         };
10031         auto ODRDiagBaseNote = [&SecondModule,
10032                                 this](SourceLocation Loc, SourceRange Range,
10033                                       ODRDefinitionDataDifference DiffType) {
10034           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10035                  << SecondModule << Range << DiffType;
10036         };
10037         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10038           unsigned NumBases = DD->NumBases;
10039           if (NumBases == 0) return SourceRange();
10040           ArrayRef<CXXBaseSpecifier> bases = DD->bases();
10041           return SourceRange(bases[0].getBeginLoc(),
10042                              bases[NumBases - 1].getEndLoc());
10043         };
10044 
10045         unsigned FirstNumBases = FirstDD->NumBases;
10046         unsigned FirstNumVBases = FirstDD->NumVBases;
10047         unsigned SecondNumBases = SecondDD->NumBases;
10048         unsigned SecondNumVBases = SecondDD->NumVBases;
10049         if (FirstNumBases != SecondNumBases) {
10050           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10051                            NumBases)
10052               << FirstNumBases;
10053           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10054                           NumBases)
10055               << SecondNumBases;
10056           Diagnosed = true;
10057           break;
10058         }
10059 
10060         if (FirstNumVBases != SecondNumVBases) {
10061           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10062                            NumVBases)
10063               << FirstNumVBases;
10064           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10065                           NumVBases)
10066               << SecondNumVBases;
10067           Diagnosed = true;
10068           break;
10069         }
10070 
10071         ArrayRef<CXXBaseSpecifier> FirstBases = FirstDD->bases();
10072         ArrayRef<CXXBaseSpecifier> SecondBases = SecondDD->bases();
10073         unsigned I = 0;
10074         for (I = 0; I < FirstNumBases; ++I) {
10075           const CXXBaseSpecifier FirstBase = FirstBases[I];
10076           const CXXBaseSpecifier SecondBase = SecondBases[I];
10077           if (computeODRHash(FirstBase.getType()) !=
10078               computeODRHash(SecondBase.getType())) {
10079             ODRDiagBaseError(FirstRecord->getLocation(),
10080                              FirstBase.getSourceRange(), BaseType)
10081                 << (I + 1) << FirstBase.getType();
10082             ODRDiagBaseNote(SecondRecord->getLocation(),
10083                             SecondBase.getSourceRange(), BaseType)
10084                 << (I + 1) << SecondBase.getType();
10085             break;
10086           }
10087 
10088           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10089             ODRDiagBaseError(FirstRecord->getLocation(),
10090                              FirstBase.getSourceRange(), BaseVirtual)
10091                 << (I + 1) << FirstBase.isVirtual() << FirstBase.getType();
10092             ODRDiagBaseNote(SecondRecord->getLocation(),
10093                             SecondBase.getSourceRange(), BaseVirtual)
10094                 << (I + 1) << SecondBase.isVirtual() << SecondBase.getType();
10095             break;
10096           }
10097 
10098           if (FirstBase.getAccessSpecifierAsWritten() !=
10099               SecondBase.getAccessSpecifierAsWritten()) {
10100             ODRDiagBaseError(FirstRecord->getLocation(),
10101                              FirstBase.getSourceRange(), BaseAccess)
10102                 << (I + 1) << FirstBase.getType()
10103                 << (int)FirstBase.getAccessSpecifierAsWritten();
10104             ODRDiagBaseNote(SecondRecord->getLocation(),
10105                             SecondBase.getSourceRange(), BaseAccess)
10106                 << (I + 1) << SecondBase.getType()
10107                 << (int)SecondBase.getAccessSpecifierAsWritten();
10108             break;
10109           }
10110         }
10111 
10112         if (I != FirstNumBases) {
10113           Diagnosed = true;
10114           break;
10115         }
10116       }
10117 
10118       const ClassTemplateDecl *FirstTemplate =
10119           FirstRecord->getDescribedClassTemplate();
10120       const ClassTemplateDecl *SecondTemplate =
10121           SecondRecord->getDescribedClassTemplate();
10122 
10123       assert(!FirstTemplate == !SecondTemplate &&
10124              "Both pointers should be null or non-null");
10125 
10126       if (FirstTemplate && SecondTemplate) {
10127         DeclHashes FirstTemplateHashes;
10128         DeclHashes SecondTemplateHashes;
10129 
10130         auto PopulateTemplateParameterHashs = [](DeclHashes &Hashes,
10131                                                  const ClassTemplateDecl *TD) {
10132           for (auto *D : TD->getTemplateParameters()->asArray()) {
10133             Hashes.emplace_back(D, computeODRHash(D));
10134           }
10135         };
10136 
10137         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10138         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10139 
10140         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10141                "Number of template parameters should be equal.");
10142 
10143         auto FirstIt = FirstTemplateHashes.begin();
10144         auto FirstEnd = FirstTemplateHashes.end();
10145         auto SecondIt = SecondTemplateHashes.begin();
10146         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10147           if (FirstIt->second == SecondIt->second)
10148             continue;
10149 
10150           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10151           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10152 
10153           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10154                  "Parameter Decl's should be the same kind.");
10155 
10156           enum ODRTemplateDifference {
10157             ParamEmptyName,
10158             ParamName,
10159             ParamSingleDefaultArgument,
10160             ParamDifferentDefaultArgument,
10161           };
10162 
10163           auto hasDefaultArg = [](const NamedDecl *D) {
10164             if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
10165               return TTP->hasDefaultArgument() &&
10166                       !TTP->defaultArgumentWasInherited();
10167             if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
10168               return NTTP->hasDefaultArgument() &&
10169                       !NTTP->defaultArgumentWasInherited();
10170             auto *TTP = cast<TemplateTemplateParmDecl>(D);
10171             return TTP->hasDefaultArgument() &&
10172                     !TTP->defaultArgumentWasInherited();
10173           };
10174           bool hasFirstArg = hasDefaultArg(FirstDecl);
10175           bool hasSecondArg = hasDefaultArg(SecondDecl);
10176 
10177           ODRTemplateDifference ErrDiffType;
10178           ODRTemplateDifference NoteDiffType;
10179 
10180           DeclarationName FirstName = FirstDecl->getDeclName();
10181           DeclarationName SecondName = SecondDecl->getDeclName();
10182 
10183           if (FirstName != SecondName) {
10184             bool FirstNameEmpty =
10185                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10186             bool SecondNameEmpty = SecondName.isIdentifier() &&
10187                                     !SecondName.getAsIdentifierInfo();
10188             ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName;
10189             NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName;
10190           } else if (hasFirstArg == hasSecondArg)
10191             ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument;
10192           else
10193             ErrDiffType = NoteDiffType = ParamSingleDefaultArgument;
10194 
10195           Diag(FirstDecl->getLocation(),
10196                 diag::err_module_odr_violation_template_parameter)
10197               << FirstRecord << FirstModule.empty() << FirstModule
10198               << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg
10199               << FirstName;
10200           Diag(SecondDecl->getLocation(),
10201                 diag::note_module_odr_violation_template_parameter)
10202               << SecondModule << SecondDecl->getSourceRange() << NoteDiffType
10203               << hasSecondArg << SecondName;
10204           break;
10205         }
10206 
10207         if (FirstIt != FirstEnd) {
10208           Diagnosed = true;
10209           break;
10210         }
10211       }
10212 
10213       DeclHashes FirstHashes;
10214       DeclHashes SecondHashes;
10215       const DeclContext *DC = FirstRecord;
10216       PopulateHashes(FirstHashes, FirstRecord, DC);
10217       PopulateHashes(SecondHashes, SecondRecord, DC);
10218 
10219       DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes);
10220       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10221       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10222       const Decl *FirstDecl = DR.FirstDecl;
10223       const Decl *SecondDecl = DR.SecondDecl;
10224 
10225       if (FirstDiffType == Other || SecondDiffType == Other) {
10226         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10227                               SecondModule);
10228         Diagnosed = true;
10229         break;
10230       }
10231 
10232       if (FirstDiffType != SecondDiffType) {
10233         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10234                             SecondModule);
10235         Diagnosed = true;
10236         break;
10237       }
10238 
10239       // Used with err_module_odr_violation_record and
10240       // note_module_odr_violation_record
10241       enum ODRCXXRecordDifference {
10242         StaticAssertCondition,
10243         StaticAssertMessage,
10244         StaticAssertOnlyMessage,
10245         MethodName,
10246         MethodDeleted,
10247         MethodDefaulted,
10248         MethodVirtual,
10249         MethodStatic,
10250         MethodVolatile,
10251         MethodConst,
10252         MethodInline,
10253         MethodNumberParameters,
10254         MethodParameterType,
10255         MethodParameterName,
10256         MethodParameterSingleDefaultArgument,
10257         MethodParameterDifferentDefaultArgument,
10258         MethodNoTemplateArguments,
10259         MethodDifferentNumberTemplateArguments,
10260         MethodDifferentTemplateArgument,
10261         MethodSingleBody,
10262         MethodDifferentBody,
10263         FriendTypeFunction,
10264         FriendType,
10265         FriendFunction,
10266         FunctionTemplateDifferentNumberParameters,
10267         FunctionTemplateParameterDifferentKind,
10268         FunctionTemplateParameterName,
10269         FunctionTemplateParameterSingleDefaultArgument,
10270         FunctionTemplateParameterDifferentDefaultArgument,
10271         FunctionTemplateParameterDifferentType,
10272         FunctionTemplatePackParameter,
10273       };
10274       auto ODRDiagDeclError = [FirstRecord, &FirstModule,
10275                                this](SourceLocation Loc, SourceRange Range,
10276                                      ODRCXXRecordDifference DiffType) {
10277         return Diag(Loc, diag::err_module_odr_violation_record)
10278                << FirstRecord << FirstModule.empty() << FirstModule << Range
10279                << DiffType;
10280       };
10281       auto ODRDiagDeclNote = [&SecondModule,
10282                               this](SourceLocation Loc, SourceRange Range,
10283                                     ODRCXXRecordDifference DiffType) {
10284         return Diag(Loc, diag::note_module_odr_violation_record)
10285                << SecondModule << Range << DiffType;
10286       };
10287 
10288       assert(FirstDiffType == SecondDiffType);
10289       switch (FirstDiffType) {
10290       case Other:
10291       case EndOfClass:
10292       case PublicSpecifer:
10293       case PrivateSpecifer:
10294       case ProtectedSpecifer:
10295         llvm_unreachable("Invalid diff type");
10296 
10297       case StaticAssert: {
10298         const StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10299         const StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10300 
10301         const Expr *FirstExpr = FirstSA->getAssertExpr();
10302         const Expr *SecondExpr = SecondSA->getAssertExpr();
10303         unsigned FirstODRHash = computeODRHash(FirstExpr);
10304         unsigned SecondODRHash = computeODRHash(SecondExpr);
10305         if (FirstODRHash != SecondODRHash) {
10306           ODRDiagDeclError(FirstExpr->getBeginLoc(),
10307                            FirstExpr->getSourceRange(), StaticAssertCondition);
10308           ODRDiagDeclNote(SecondExpr->getBeginLoc(),
10309                           SecondExpr->getSourceRange(), StaticAssertCondition);
10310           Diagnosed = true;
10311           break;
10312         }
10313 
10314         const StringLiteral *FirstStr = FirstSA->getMessage();
10315         const StringLiteral *SecondStr = SecondSA->getMessage();
10316         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10317         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10318           SourceLocation FirstLoc, SecondLoc;
10319           SourceRange FirstRange, SecondRange;
10320           if (FirstStr) {
10321             FirstLoc = FirstStr->getBeginLoc();
10322             FirstRange = FirstStr->getSourceRange();
10323           } else {
10324             FirstLoc = FirstSA->getBeginLoc();
10325             FirstRange = FirstSA->getSourceRange();
10326           }
10327           if (SecondStr) {
10328             SecondLoc = SecondStr->getBeginLoc();
10329             SecondRange = SecondStr->getSourceRange();
10330           } else {
10331             SecondLoc = SecondSA->getBeginLoc();
10332             SecondRange = SecondSA->getSourceRange();
10333           }
10334           ODRDiagDeclError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10335               << (FirstStr == nullptr);
10336           ODRDiagDeclNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10337               << (SecondStr == nullptr);
10338           Diagnosed = true;
10339           break;
10340         }
10341 
10342         if (FirstStr && SecondStr &&
10343             FirstStr->getString() != SecondStr->getString()) {
10344           ODRDiagDeclError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10345                            StaticAssertMessage);
10346           ODRDiagDeclNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10347                           StaticAssertMessage);
10348           Diagnosed = true;
10349           break;
10350         }
10351         break;
10352       }
10353       case Field: {
10354         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10355                                  cast<FieldDecl>(FirstDecl),
10356                                  cast<FieldDecl>(SecondDecl));
10357         break;
10358       }
10359       case CXXMethod: {
10360         enum {
10361           DiagMethod,
10362           DiagConstructor,
10363           DiagDestructor,
10364         } FirstMethodType,
10365             SecondMethodType;
10366         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10367           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10368           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10369           return DiagMethod;
10370         };
10371         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10372         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10373         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10374         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10375         DeclarationName FirstName = FirstMethod->getDeclName();
10376         DeclarationName SecondName = SecondMethod->getDeclName();
10377         auto DiagMethodError = [&ODRDiagDeclError, FirstMethod, FirstMethodType,
10378                                 FirstName](ODRCXXRecordDifference DiffType) {
10379           return ODRDiagDeclError(FirstMethod->getLocation(),
10380                                   FirstMethod->getSourceRange(), DiffType)
10381                  << FirstMethodType << FirstName;
10382         };
10383         auto DiagMethodNote = [&ODRDiagDeclNote, SecondMethod, SecondMethodType,
10384                                SecondName](ODRCXXRecordDifference DiffType) {
10385           return ODRDiagDeclNote(SecondMethod->getLocation(),
10386                                  SecondMethod->getSourceRange(), DiffType)
10387                  << SecondMethodType << SecondName;
10388         };
10389 
10390         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10391           DiagMethodError(MethodName);
10392           DiagMethodNote(MethodName);
10393           Diagnosed = true;
10394           break;
10395         }
10396 
10397         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10398         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10399         if (FirstDeleted != SecondDeleted) {
10400           DiagMethodError(MethodDeleted) << FirstDeleted;
10401           DiagMethodNote(MethodDeleted) << SecondDeleted;
10402           Diagnosed = true;
10403           break;
10404         }
10405 
10406         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10407         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10408         if (FirstDefaulted != SecondDefaulted) {
10409           DiagMethodError(MethodDefaulted) << FirstDefaulted;
10410           DiagMethodNote(MethodDefaulted) << SecondDefaulted;
10411           Diagnosed = true;
10412           break;
10413         }
10414 
10415         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10416         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10417         const bool FirstPure = FirstMethod->isPure();
10418         const bool SecondPure = SecondMethod->isPure();
10419         if ((FirstVirtual || SecondVirtual) &&
10420             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10421           DiagMethodError(MethodVirtual) << FirstPure << FirstVirtual;
10422           DiagMethodNote(MethodVirtual) << SecondPure << SecondVirtual;
10423           Diagnosed = true;
10424           break;
10425         }
10426 
10427         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10428         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10429         // class needs to be checked instead.
10430         StorageClass FirstStorage = FirstMethod->getStorageClass();
10431         StorageClass SecondStorage = SecondMethod->getStorageClass();
10432         const bool FirstStatic = FirstStorage == SC_Static;
10433         const bool SecondStatic = SecondStorage == SC_Static;
10434         if (FirstStatic != SecondStatic) {
10435           DiagMethodError(MethodStatic) << FirstStatic;
10436           DiagMethodNote(MethodStatic) << SecondStatic;
10437           Diagnosed = true;
10438           break;
10439         }
10440 
10441         const bool FirstVolatile = FirstMethod->isVolatile();
10442         const bool SecondVolatile = SecondMethod->isVolatile();
10443         if (FirstVolatile != SecondVolatile) {
10444           DiagMethodError(MethodVolatile) << FirstVolatile;
10445           DiagMethodNote(MethodVolatile) << SecondVolatile;
10446           Diagnosed = true;
10447           break;
10448         }
10449 
10450         const bool FirstConst = FirstMethod->isConst();
10451         const bool SecondConst = SecondMethod->isConst();
10452         if (FirstConst != SecondConst) {
10453           DiagMethodError(MethodConst) << FirstConst;
10454           DiagMethodNote(MethodConst) << SecondConst;
10455           Diagnosed = true;
10456           break;
10457         }
10458 
10459         const bool FirstInline = FirstMethod->isInlineSpecified();
10460         const bool SecondInline = SecondMethod->isInlineSpecified();
10461         if (FirstInline != SecondInline) {
10462           DiagMethodError(MethodInline) << FirstInline;
10463           DiagMethodNote(MethodInline) << SecondInline;
10464           Diagnosed = true;
10465           break;
10466         }
10467 
10468         const unsigned FirstNumParameters = FirstMethod->param_size();
10469         const unsigned SecondNumParameters = SecondMethod->param_size();
10470         if (FirstNumParameters != SecondNumParameters) {
10471           DiagMethodError(MethodNumberParameters) << FirstNumParameters;
10472           DiagMethodNote(MethodNumberParameters) << SecondNumParameters;
10473           Diagnosed = true;
10474           break;
10475         }
10476 
10477         // Need this status boolean to know when break out of the switch.
10478         bool ParameterMismatch = false;
10479         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10480           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10481           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10482 
10483           QualType FirstParamType = FirstParam->getType();
10484           QualType SecondParamType = SecondParam->getType();
10485           if (FirstParamType != SecondParamType &&
10486               computeODRHash(FirstParamType) !=
10487                   computeODRHash(SecondParamType)) {
10488             if (const DecayedType *ParamDecayedType =
10489                     FirstParamType->getAs<DecayedType>()) {
10490               DiagMethodError(MethodParameterType)
10491                   << (I + 1) << FirstParamType << true
10492                   << ParamDecayedType->getOriginalType();
10493             } else {
10494               DiagMethodError(MethodParameterType)
10495                   << (I + 1) << FirstParamType << false;
10496             }
10497 
10498             if (const DecayedType *ParamDecayedType =
10499                     SecondParamType->getAs<DecayedType>()) {
10500               DiagMethodNote(MethodParameterType)
10501                   << (I + 1) << SecondParamType << true
10502                   << ParamDecayedType->getOriginalType();
10503             } else {
10504               DiagMethodNote(MethodParameterType)
10505                   << (I + 1) << SecondParamType << false;
10506             }
10507             ParameterMismatch = true;
10508             break;
10509           }
10510 
10511           DeclarationName FirstParamName = FirstParam->getDeclName();
10512           DeclarationName SecondParamName = SecondParam->getDeclName();
10513           if (FirstParamName != SecondParamName) {
10514             DiagMethodError(MethodParameterName) << (I + 1) << FirstParamName;
10515             DiagMethodNote(MethodParameterName) << (I + 1) << SecondParamName;
10516             ParameterMismatch = true;
10517             break;
10518           }
10519 
10520           const Expr *FirstInit = FirstParam->getInit();
10521           const Expr *SecondInit = SecondParam->getInit();
10522           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10523             DiagMethodError(MethodParameterSingleDefaultArgument)
10524                 << (I + 1) << (FirstInit == nullptr)
10525                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10526             DiagMethodNote(MethodParameterSingleDefaultArgument)
10527                 << (I + 1) << (SecondInit == nullptr)
10528                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10529             ParameterMismatch = true;
10530             break;
10531           }
10532 
10533           if (FirstInit && SecondInit &&
10534               computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
10535             DiagMethodError(MethodParameterDifferentDefaultArgument)
10536                 << (I + 1) << FirstInit->getSourceRange();
10537             DiagMethodNote(MethodParameterDifferentDefaultArgument)
10538                 << (I + 1) << SecondInit->getSourceRange();
10539             ParameterMismatch = true;
10540             break;
10541           }
10542         }
10543 
10544         if (ParameterMismatch) {
10545           Diagnosed = true;
10546           break;
10547         }
10548 
10549         const TemplateArgumentList *FirstTemplateArgs =
10550             FirstMethod->getTemplateSpecializationArgs();
10551         const TemplateArgumentList *SecondTemplateArgs =
10552             SecondMethod->getTemplateSpecializationArgs();
10553 
10554         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10555             (!FirstTemplateArgs && SecondTemplateArgs)) {
10556           DiagMethodError(MethodNoTemplateArguments)
10557               << (FirstTemplateArgs != nullptr);
10558           DiagMethodNote(MethodNoTemplateArguments)
10559               << (SecondTemplateArgs != nullptr);
10560           Diagnosed = true;
10561           break;
10562         }
10563 
10564         if (FirstTemplateArgs && SecondTemplateArgs) {
10565           // Remove pack expansions from argument list.
10566           auto ExpandTemplateArgumentList =
10567               [](const TemplateArgumentList *TAL) {
10568                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10569                 for (const TemplateArgument &TA : TAL->asArray()) {
10570                   if (TA.getKind() != TemplateArgument::Pack) {
10571                     ExpandedList.push_back(&TA);
10572                     continue;
10573                   }
10574                   llvm::append_range(ExpandedList, llvm::make_pointer_range(
10575                                                        TA.getPackAsArray()));
10576                 }
10577                 return ExpandedList;
10578               };
10579           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10580               ExpandTemplateArgumentList(FirstTemplateArgs);
10581           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10582               ExpandTemplateArgumentList(SecondTemplateArgs);
10583 
10584           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10585             DiagMethodError(MethodDifferentNumberTemplateArguments)
10586                 << (unsigned)FirstExpandedList.size();
10587             DiagMethodNote(MethodDifferentNumberTemplateArguments)
10588                 << (unsigned)SecondExpandedList.size();
10589             Diagnosed = true;
10590             break;
10591           }
10592 
10593           bool TemplateArgumentMismatch = false;
10594           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10595             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10596                                    &SecondTA = *SecondExpandedList[i];
10597             if (computeODRHash(FirstTA) == computeODRHash(SecondTA)) {
10598               continue;
10599             }
10600 
10601             DiagMethodError(MethodDifferentTemplateArgument)
10602                 << FirstTA << i + 1;
10603             DiagMethodNote(MethodDifferentTemplateArgument)
10604                 << SecondTA << i + 1;
10605             TemplateArgumentMismatch = true;
10606             break;
10607           }
10608 
10609           if (TemplateArgumentMismatch) {
10610             Diagnosed = true;
10611             break;
10612           }
10613         }
10614 
10615         // Compute the hash of the method as if it has no body.
10616         auto ComputeCXXMethodODRHash = [](const CXXMethodDecl *D) {
10617           ODRHash Hasher;
10618           Hasher.AddFunctionDecl(D, true /*SkipBody*/);
10619           return Hasher.CalculateHash();
10620         };
10621 
10622         // Compare the hash generated to the hash stored.  A difference means
10623         // that a body was present in the original source.  Due to merging,
10624         // the standard way of detecting a body will not work.
10625         const bool HasFirstBody =
10626             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10627         const bool HasSecondBody =
10628             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10629 
10630         if (HasFirstBody != HasSecondBody) {
10631           DiagMethodError(MethodSingleBody) << HasFirstBody;
10632           DiagMethodNote(MethodSingleBody) << HasSecondBody;
10633           Diagnosed = true;
10634           break;
10635         }
10636 
10637         if (HasFirstBody && HasSecondBody) {
10638           DiagMethodError(MethodDifferentBody);
10639           DiagMethodNote(MethodDifferentBody);
10640           Diagnosed = true;
10641           break;
10642         }
10643 
10644         break;
10645       }
10646       case TypeAlias:
10647       case TypeDef: {
10648         Diagnosed = ODRDiagTypeDefOrAlias(
10649             FirstRecord, FirstModule, SecondModule,
10650             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10651             FirstDiffType == TypeAlias);
10652         break;
10653       }
10654       case Var: {
10655         Diagnosed =
10656             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10657                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10658         break;
10659       }
10660       case Friend: {
10661         const FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10662         const FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10663 
10664         const NamedDecl *FirstND = FirstFriend->getFriendDecl();
10665         const NamedDecl *SecondND = SecondFriend->getFriendDecl();
10666 
10667         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10668         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10669 
10670         if (FirstND && SecondND) {
10671           ODRDiagDeclError(FirstFriend->getFriendLoc(),
10672                            FirstFriend->getSourceRange(), FriendFunction)
10673               << FirstND;
10674           ODRDiagDeclNote(SecondFriend->getFriendLoc(),
10675                           SecondFriend->getSourceRange(), FriendFunction)
10676               << SecondND;
10677           Diagnosed = true;
10678           break;
10679         }
10680 
10681         if (FirstTSI && SecondTSI) {
10682           QualType FirstFriendType = FirstTSI->getType();
10683           QualType SecondFriendType = SecondTSI->getType();
10684           assert(computeODRHash(FirstFriendType) !=
10685                  computeODRHash(SecondFriendType));
10686           ODRDiagDeclError(FirstFriend->getFriendLoc(),
10687                            FirstFriend->getSourceRange(), FriendType)
10688               << FirstFriendType;
10689           ODRDiagDeclNote(SecondFriend->getFriendLoc(),
10690                           SecondFriend->getSourceRange(), FriendType)
10691               << SecondFriendType;
10692           Diagnosed = true;
10693           break;
10694         }
10695 
10696         ODRDiagDeclError(FirstFriend->getFriendLoc(),
10697                          FirstFriend->getSourceRange(), FriendTypeFunction)
10698             << (FirstTSI == nullptr);
10699         ODRDiagDeclNote(SecondFriend->getFriendLoc(),
10700                         SecondFriend->getSourceRange(), FriendTypeFunction)
10701             << (SecondTSI == nullptr);
10702         Diagnosed = true;
10703         break;
10704       }
10705       case FunctionTemplate: {
10706         const FunctionTemplateDecl *FirstTemplate =
10707             cast<FunctionTemplateDecl>(FirstDecl);
10708         const FunctionTemplateDecl *SecondTemplate =
10709             cast<FunctionTemplateDecl>(SecondDecl);
10710 
10711         TemplateParameterList *FirstTPL =
10712             FirstTemplate->getTemplateParameters();
10713         TemplateParameterList *SecondTPL =
10714             SecondTemplate->getTemplateParameters();
10715 
10716         auto DiagTemplateError = [&ODRDiagDeclError, FirstTemplate](
10717                                      ODRCXXRecordDifference DiffType) {
10718           return ODRDiagDeclError(FirstTemplate->getLocation(),
10719                                   FirstTemplate->getSourceRange(), DiffType)
10720                  << FirstTemplate;
10721         };
10722         auto DiagTemplateNote = [&ODRDiagDeclNote, SecondTemplate](
10723                                     ODRCXXRecordDifference DiffType) {
10724           return ODRDiagDeclNote(SecondTemplate->getLocation(),
10725                                  SecondTemplate->getSourceRange(), DiffType)
10726                  << SecondTemplate;
10727         };
10728 
10729         if (FirstTPL->size() != SecondTPL->size()) {
10730           DiagTemplateError(FunctionTemplateDifferentNumberParameters)
10731               << FirstTPL->size();
10732           DiagTemplateNote(FunctionTemplateDifferentNumberParameters)
10733               << SecondTPL->size();
10734           Diagnosed = true;
10735           break;
10736         }
10737 
10738         bool ParameterMismatch = false;
10739         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10740           NamedDecl *FirstParam = FirstTPL->getParam(i);
10741           NamedDecl *SecondParam = SecondTPL->getParam(i);
10742 
10743           if (FirstParam->getKind() != SecondParam->getKind()) {
10744             enum {
10745               TemplateTypeParameter,
10746               NonTypeTemplateParameter,
10747               TemplateTemplateParameter,
10748             };
10749             auto GetParamType = [](NamedDecl *D) {
10750               switch (D->getKind()) {
10751                 default:
10752                   llvm_unreachable("Unexpected template parameter type");
10753                 case Decl::TemplateTypeParm:
10754                   return TemplateTypeParameter;
10755                 case Decl::NonTypeTemplateParm:
10756                   return NonTypeTemplateParameter;
10757                 case Decl::TemplateTemplateParm:
10758                   return TemplateTemplateParameter;
10759               }
10760             };
10761 
10762             DiagTemplateError(FunctionTemplateParameterDifferentKind)
10763                 << (i + 1) << GetParamType(FirstParam);
10764             DiagTemplateNote(FunctionTemplateParameterDifferentKind)
10765                 << (i + 1) << GetParamType(SecondParam);
10766             ParameterMismatch = true;
10767             break;
10768           }
10769 
10770           if (FirstParam->getName() != SecondParam->getName()) {
10771             DiagTemplateError(FunctionTemplateParameterName)
10772                 << (i + 1) << (bool)FirstParam->getIdentifier() << FirstParam;
10773             DiagTemplateNote(FunctionTemplateParameterName)
10774                 << (i + 1) << (bool)SecondParam->getIdentifier() << SecondParam;
10775             ParameterMismatch = true;
10776             break;
10777           }
10778 
10779           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10780               isa<TemplateTypeParmDecl>(SecondParam)) {
10781             TemplateTypeParmDecl *FirstTTPD =
10782                 cast<TemplateTypeParmDecl>(FirstParam);
10783             TemplateTypeParmDecl *SecondTTPD =
10784                 cast<TemplateTypeParmDecl>(SecondParam);
10785             bool HasFirstDefaultArgument =
10786                 FirstTTPD->hasDefaultArgument() &&
10787                 !FirstTTPD->defaultArgumentWasInherited();
10788             bool HasSecondDefaultArgument =
10789                 SecondTTPD->hasDefaultArgument() &&
10790                 !SecondTTPD->defaultArgumentWasInherited();
10791             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10792               DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
10793                   << (i + 1) << HasFirstDefaultArgument;
10794               DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
10795                   << (i + 1) << HasSecondDefaultArgument;
10796               ParameterMismatch = true;
10797               break;
10798             }
10799 
10800             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10801               QualType FirstType = FirstTTPD->getDefaultArgument();
10802               QualType SecondType = SecondTTPD->getDefaultArgument();
10803               if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
10804                 DiagTemplateError(
10805                     FunctionTemplateParameterDifferentDefaultArgument)
10806                     << (i + 1) << FirstType;
10807                 DiagTemplateNote(
10808                     FunctionTemplateParameterDifferentDefaultArgument)
10809                     << (i + 1) << SecondType;
10810                 ParameterMismatch = true;
10811                 break;
10812               }
10813             }
10814 
10815             if (FirstTTPD->isParameterPack() !=
10816                 SecondTTPD->isParameterPack()) {
10817               DiagTemplateError(FunctionTemplatePackParameter)
10818                   << (i + 1) << FirstTTPD->isParameterPack();
10819               DiagTemplateNote(FunctionTemplatePackParameter)
10820                   << (i + 1) << SecondTTPD->isParameterPack();
10821               ParameterMismatch = true;
10822               break;
10823             }
10824           }
10825 
10826           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10827               isa<TemplateTemplateParmDecl>(SecondParam)) {
10828             TemplateTemplateParmDecl *FirstTTPD =
10829                 cast<TemplateTemplateParmDecl>(FirstParam);
10830             TemplateTemplateParmDecl *SecondTTPD =
10831                 cast<TemplateTemplateParmDecl>(SecondParam);
10832 
10833             TemplateParameterList *FirstTPL =
10834                 FirstTTPD->getTemplateParameters();
10835             TemplateParameterList *SecondTPL =
10836                 SecondTTPD->getTemplateParameters();
10837 
10838             auto ComputeTemplateParameterListODRHash =
10839                 [](const TemplateParameterList *TPL) {
10840                   assert(TPL);
10841                   ODRHash Hasher;
10842                   Hasher.AddTemplateParameterList(TPL);
10843                   return Hasher.CalculateHash();
10844                 };
10845 
10846             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10847                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10848               DiagTemplateError(FunctionTemplateParameterDifferentType)
10849                   << (i + 1);
10850               DiagTemplateNote(FunctionTemplateParameterDifferentType)
10851                   << (i + 1);
10852               ParameterMismatch = true;
10853               break;
10854             }
10855 
10856             bool HasFirstDefaultArgument =
10857                 FirstTTPD->hasDefaultArgument() &&
10858                 !FirstTTPD->defaultArgumentWasInherited();
10859             bool HasSecondDefaultArgument =
10860                 SecondTTPD->hasDefaultArgument() &&
10861                 !SecondTTPD->defaultArgumentWasInherited();
10862             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10863               DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
10864                   << (i + 1) << HasFirstDefaultArgument;
10865               DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
10866                   << (i + 1) << HasSecondDefaultArgument;
10867               ParameterMismatch = true;
10868               break;
10869             }
10870 
10871             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10872               TemplateArgument FirstTA =
10873                   FirstTTPD->getDefaultArgument().getArgument();
10874               TemplateArgument SecondTA =
10875                   SecondTTPD->getDefaultArgument().getArgument();
10876               if (computeODRHash(FirstTA) != computeODRHash(SecondTA)) {
10877                 DiagTemplateError(
10878                     FunctionTemplateParameterDifferentDefaultArgument)
10879                     << (i + 1) << FirstTA;
10880                 DiagTemplateNote(
10881                     FunctionTemplateParameterDifferentDefaultArgument)
10882                     << (i + 1) << SecondTA;
10883                 ParameterMismatch = true;
10884                 break;
10885               }
10886             }
10887 
10888             if (FirstTTPD->isParameterPack() !=
10889                 SecondTTPD->isParameterPack()) {
10890               DiagTemplateError(FunctionTemplatePackParameter)
10891                   << (i + 1) << FirstTTPD->isParameterPack();
10892               DiagTemplateNote(FunctionTemplatePackParameter)
10893                   << (i + 1) << SecondTTPD->isParameterPack();
10894               ParameterMismatch = true;
10895               break;
10896             }
10897           }
10898 
10899           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10900               isa<NonTypeTemplateParmDecl>(SecondParam)) {
10901             NonTypeTemplateParmDecl *FirstNTTPD =
10902                 cast<NonTypeTemplateParmDecl>(FirstParam);
10903             NonTypeTemplateParmDecl *SecondNTTPD =
10904                 cast<NonTypeTemplateParmDecl>(SecondParam);
10905 
10906             QualType FirstType = FirstNTTPD->getType();
10907             QualType SecondType = SecondNTTPD->getType();
10908             if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
10909               DiagTemplateError(FunctionTemplateParameterDifferentType)
10910                   << (i + 1);
10911               DiagTemplateNote(FunctionTemplateParameterDifferentType)
10912                   << (i + 1);
10913               ParameterMismatch = true;
10914               break;
10915             }
10916 
10917             bool HasFirstDefaultArgument =
10918                 FirstNTTPD->hasDefaultArgument() &&
10919                 !FirstNTTPD->defaultArgumentWasInherited();
10920             bool HasSecondDefaultArgument =
10921                 SecondNTTPD->hasDefaultArgument() &&
10922                 !SecondNTTPD->defaultArgumentWasInherited();
10923             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10924               DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
10925                   << (i + 1) << HasFirstDefaultArgument;
10926               DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
10927                   << (i + 1) << HasSecondDefaultArgument;
10928               ParameterMismatch = true;
10929               break;
10930             }
10931 
10932             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10933               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
10934               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
10935               if (computeODRHash(FirstDefaultArgument) !=
10936                   computeODRHash(SecondDefaultArgument)) {
10937                 DiagTemplateError(
10938                     FunctionTemplateParameterDifferentDefaultArgument)
10939                     << (i + 1) << FirstDefaultArgument;
10940                 DiagTemplateNote(
10941                     FunctionTemplateParameterDifferentDefaultArgument)
10942                     << (i + 1) << SecondDefaultArgument;
10943                 ParameterMismatch = true;
10944                 break;
10945               }
10946             }
10947 
10948             if (FirstNTTPD->isParameterPack() !=
10949                 SecondNTTPD->isParameterPack()) {
10950               DiagTemplateError(FunctionTemplatePackParameter)
10951                   << (i + 1) << FirstNTTPD->isParameterPack();
10952               DiagTemplateNote(FunctionTemplatePackParameter)
10953                   << (i + 1) << SecondNTTPD->isParameterPack();
10954               ParameterMismatch = true;
10955               break;
10956             }
10957           }
10958         }
10959 
10960         if (ParameterMismatch) {
10961           Diagnosed = true;
10962           break;
10963         }
10964 
10965         break;
10966       }
10967       }
10968 
10969       if (Diagnosed)
10970         continue;
10971 
10972       Diag(FirstDecl->getLocation(),
10973            diag::err_module_odr_violation_mismatch_decl_unknown)
10974           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
10975           << FirstDecl->getSourceRange();
10976       Diag(SecondDecl->getLocation(),
10977            diag::note_module_odr_violation_mismatch_decl_unknown)
10978           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
10979       Diagnosed = true;
10980     }
10981 
10982     if (!Diagnosed) {
10983       // All definitions are updates to the same declaration. This happens if a
10984       // module instantiates the declaration of a class template specialization
10985       // and two or more other modules instantiate its definition.
10986       //
10987       // FIXME: Indicate which modules had instantiations of this definition.
10988       // FIXME: How can this even happen?
10989       Diag(Merge.first->getLocation(),
10990            diag::err_module_odr_violation_different_instantiations)
10991         << Merge.first;
10992     }
10993   }
10994 
10995   // Issue ODR failures diagnostics for functions.
10996   for (auto &Merge : FunctionOdrMergeFailures) {
10997     enum ODRFunctionDifference {
10998       ReturnType,
10999       ParameterName,
11000       ParameterType,
11001       ParameterSingleDefaultArgument,
11002       ParameterDifferentDefaultArgument,
11003       FunctionBody,
11004     };
11005 
11006     FunctionDecl *FirstFunction = Merge.first;
11007     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11008 
11009     bool Diagnosed = false;
11010     for (auto &SecondFunction : Merge.second) {
11011 
11012       if (FirstFunction == SecondFunction)
11013         continue;
11014 
11015       std::string SecondModule =
11016           getOwningModuleNameForDiagnostic(SecondFunction);
11017 
11018       auto ODRDiagError = [FirstFunction, &FirstModule,
11019                            this](SourceLocation Loc, SourceRange Range,
11020                                  ODRFunctionDifference DiffType) {
11021         return Diag(Loc, diag::err_module_odr_violation_function)
11022                << FirstFunction << FirstModule.empty() << FirstModule << Range
11023                << DiffType;
11024       };
11025       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11026                                                SourceRange Range,
11027                                                ODRFunctionDifference DiffType) {
11028         return Diag(Loc, diag::note_module_odr_violation_function)
11029                << SecondModule << Range << DiffType;
11030       };
11031 
11032       if (computeODRHash(FirstFunction->getReturnType()) !=
11033           computeODRHash(SecondFunction->getReturnType())) {
11034         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11035                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11036             << FirstFunction->getReturnType();
11037         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11038                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11039             << SecondFunction->getReturnType();
11040         Diagnosed = true;
11041         break;
11042       }
11043 
11044       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11045              "Merged functions with different number of parameters");
11046 
11047       size_t ParamSize = FirstFunction->param_size();
11048       bool ParameterMismatch = false;
11049       for (unsigned I = 0; I < ParamSize; ++I) {
11050         const ParmVarDecl *FirstParam = FirstFunction->getParamDecl(I);
11051         const ParmVarDecl *SecondParam = SecondFunction->getParamDecl(I);
11052 
11053         assert(getContext().hasSameType(FirstParam->getType(),
11054                                       SecondParam->getType()) &&
11055                "Merged function has different parameter types.");
11056 
11057         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11058           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11059                        ParameterName)
11060               << I + 1 << FirstParam->getDeclName();
11061           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11062                       ParameterName)
11063               << I + 1 << SecondParam->getDeclName();
11064           ParameterMismatch = true;
11065           break;
11066         };
11067 
11068         QualType FirstParamType = FirstParam->getType();
11069         QualType SecondParamType = SecondParam->getType();
11070         if (FirstParamType != SecondParamType &&
11071             computeODRHash(FirstParamType) != computeODRHash(SecondParamType)) {
11072           if (const DecayedType *ParamDecayedType =
11073                   FirstParamType->getAs<DecayedType>()) {
11074             ODRDiagError(FirstParam->getLocation(),
11075                          FirstParam->getSourceRange(), ParameterType)
11076                 << (I + 1) << FirstParamType << true
11077                 << ParamDecayedType->getOriginalType();
11078           } else {
11079             ODRDiagError(FirstParam->getLocation(),
11080                          FirstParam->getSourceRange(), ParameterType)
11081                 << (I + 1) << FirstParamType << false;
11082           }
11083 
11084           if (const DecayedType *ParamDecayedType =
11085                   SecondParamType->getAs<DecayedType>()) {
11086             ODRDiagNote(SecondParam->getLocation(),
11087                         SecondParam->getSourceRange(), ParameterType)
11088                 << (I + 1) << SecondParamType << true
11089                 << ParamDecayedType->getOriginalType();
11090           } else {
11091             ODRDiagNote(SecondParam->getLocation(),
11092                         SecondParam->getSourceRange(), ParameterType)
11093                 << (I + 1) << SecondParamType << false;
11094           }
11095           ParameterMismatch = true;
11096           break;
11097         }
11098 
11099         const Expr *FirstInit = FirstParam->getInit();
11100         const Expr *SecondInit = SecondParam->getInit();
11101         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11102           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11103                        ParameterSingleDefaultArgument)
11104               << (I + 1) << (FirstInit == nullptr)
11105               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11106           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11107                       ParameterSingleDefaultArgument)
11108               << (I + 1) << (SecondInit == nullptr)
11109               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11110           ParameterMismatch = true;
11111           break;
11112         }
11113 
11114         if (FirstInit && SecondInit &&
11115             computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
11116           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11117                        ParameterDifferentDefaultArgument)
11118               << (I + 1) << FirstInit->getSourceRange();
11119           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11120                       ParameterDifferentDefaultArgument)
11121               << (I + 1) << SecondInit->getSourceRange();
11122           ParameterMismatch = true;
11123           break;
11124         }
11125 
11126         assert(computeODRHash(FirstParam) == computeODRHash(SecondParam) &&
11127                "Undiagnosed parameter difference.");
11128       }
11129 
11130       if (ParameterMismatch) {
11131         Diagnosed = true;
11132         break;
11133       }
11134 
11135       // If no error has been generated before now, assume the problem is in
11136       // the body and generate a message.
11137       ODRDiagError(FirstFunction->getLocation(),
11138                    FirstFunction->getSourceRange(), FunctionBody);
11139       ODRDiagNote(SecondFunction->getLocation(),
11140                   SecondFunction->getSourceRange(), FunctionBody);
11141       Diagnosed = true;
11142       break;
11143     }
11144     (void)Diagnosed;
11145     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11146   }
11147 
11148   // Issue ODR failures diagnostics for enums.
11149   for (auto &Merge : EnumOdrMergeFailures) {
11150     enum ODREnumDifference {
11151       SingleScopedEnum,
11152       EnumTagKeywordMismatch,
11153       SingleSpecifiedType,
11154       DifferentSpecifiedTypes,
11155       DifferentNumberEnumConstants,
11156       EnumConstantName,
11157       EnumConstantSingleInitializer,
11158       EnumConstantDifferentInitializer,
11159     };
11160 
11161     // If we've already pointed out a specific problem with this enum, don't
11162     // bother issuing a general "something's different" diagnostic.
11163     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11164       continue;
11165 
11166     EnumDecl *FirstEnum = Merge.first;
11167     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11168 
11169     using DeclHashes =
11170         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11171     auto PopulateHashes = [FirstEnum](DeclHashes &Hashes, EnumDecl *Enum) {
11172       for (auto *D : Enum->decls()) {
11173         // Due to decl merging, the first EnumDecl is the parent of
11174         // Decls in both records.
11175         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11176           continue;
11177         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11178         Hashes.emplace_back(cast<EnumConstantDecl>(D), computeODRHash(D));
11179       }
11180     };
11181     DeclHashes FirstHashes;
11182     PopulateHashes(FirstHashes, FirstEnum);
11183     bool Diagnosed = false;
11184     for (auto &SecondEnum : Merge.second) {
11185 
11186       if (FirstEnum == SecondEnum)
11187         continue;
11188 
11189       std::string SecondModule =
11190           getOwningModuleNameForDiagnostic(SecondEnum);
11191 
11192       auto ODRDiagError = [FirstEnum, &FirstModule,
11193                            this](const auto *DiagAnchor,
11194                                  ODREnumDifference DiffType) {
11195         return Diag(DiagAnchor->getLocation(),
11196                     diag::err_module_odr_violation_enum)
11197                << FirstEnum << FirstModule.empty() << FirstModule
11198                << DiagAnchor->getSourceRange() << DiffType;
11199       };
11200       auto ODRDiagNote = [&SecondModule, this](const auto *DiagAnchor,
11201                                                ODREnumDifference DiffType) {
11202         return Diag(DiagAnchor->getLocation(),
11203                     diag::note_module_odr_violation_enum)
11204                << SecondModule << DiagAnchor->getSourceRange() << DiffType;
11205       };
11206 
11207       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11208         ODRDiagError(FirstEnum, SingleScopedEnum) << FirstEnum->isScoped();
11209         ODRDiagNote(SecondEnum, SingleScopedEnum) << SecondEnum->isScoped();
11210         Diagnosed = true;
11211         continue;
11212       }
11213 
11214       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11215         if (FirstEnum->isScopedUsingClassTag() !=
11216             SecondEnum->isScopedUsingClassTag()) {
11217           ODRDiagError(FirstEnum, EnumTagKeywordMismatch)
11218               << FirstEnum->isScopedUsingClassTag();
11219           ODRDiagNote(SecondEnum, EnumTagKeywordMismatch)
11220               << SecondEnum->isScopedUsingClassTag();
11221           Diagnosed = true;
11222           continue;
11223         }
11224       }
11225 
11226       QualType FirstUnderlyingType =
11227           FirstEnum->getIntegerTypeSourceInfo()
11228               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11229               : QualType();
11230       QualType SecondUnderlyingType =
11231           SecondEnum->getIntegerTypeSourceInfo()
11232               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11233               : QualType();
11234       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11235         ODRDiagError(FirstEnum, SingleSpecifiedType)
11236             << !FirstUnderlyingType.isNull();
11237         ODRDiagNote(SecondEnum, SingleSpecifiedType)
11238             << !SecondUnderlyingType.isNull();
11239         Diagnosed = true;
11240         continue;
11241       }
11242 
11243       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11244         if (computeODRHash(FirstUnderlyingType) !=
11245             computeODRHash(SecondUnderlyingType)) {
11246           ODRDiagError(FirstEnum, DifferentSpecifiedTypes)
11247               << FirstUnderlyingType;
11248           ODRDiagNote(SecondEnum, DifferentSpecifiedTypes)
11249               << SecondUnderlyingType;
11250           Diagnosed = true;
11251           continue;
11252         }
11253       }
11254 
11255       DeclHashes SecondHashes;
11256       PopulateHashes(SecondHashes, SecondEnum);
11257 
11258       if (FirstHashes.size() != SecondHashes.size()) {
11259         ODRDiagError(FirstEnum, DifferentNumberEnumConstants)
11260             << (int)FirstHashes.size();
11261         ODRDiagNote(SecondEnum, DifferentNumberEnumConstants)
11262             << (int)SecondHashes.size();
11263         Diagnosed = true;
11264         continue;
11265       }
11266 
11267       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11268         if (FirstHashes[I].second == SecondHashes[I].second)
11269           continue;
11270         const EnumConstantDecl *FirstConstant = FirstHashes[I].first;
11271         const EnumConstantDecl *SecondConstant = SecondHashes[I].first;
11272 
11273         if (FirstConstant->getDeclName() != SecondConstant->getDeclName()) {
11274 
11275           ODRDiagError(FirstConstant, EnumConstantName)
11276               << I + 1 << FirstConstant;
11277           ODRDiagNote(SecondConstant, EnumConstantName)
11278               << I + 1 << SecondConstant;
11279           Diagnosed = true;
11280           break;
11281         }
11282 
11283         const Expr *FirstInit = FirstConstant->getInitExpr();
11284         const Expr *SecondInit = SecondConstant->getInitExpr();
11285         if (!FirstInit && !SecondInit)
11286           continue;
11287 
11288         if (!FirstInit || !SecondInit) {
11289           ODRDiagError(FirstConstant, EnumConstantSingleInitializer)
11290               << I + 1 << FirstConstant << (FirstInit != nullptr);
11291           ODRDiagNote(SecondConstant, EnumConstantSingleInitializer)
11292               << I + 1 << SecondConstant << (SecondInit != nullptr);
11293           Diagnosed = true;
11294           break;
11295         }
11296 
11297         if (computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
11298           ODRDiagError(FirstConstant, EnumConstantDifferentInitializer)
11299               << I + 1 << FirstConstant;
11300           ODRDiagNote(SecondConstant, EnumConstantDifferentInitializer)
11301               << I + 1 << SecondConstant;
11302           Diagnosed = true;
11303           break;
11304         }
11305       }
11306     }
11307 
11308     (void)Diagnosed;
11309     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11310   }
11311 }
11312 
11313 void ASTReader::StartedDeserializing() {
11314   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11315     ReadTimer->startTimer();
11316 }
11317 
11318 void ASTReader::FinishedDeserializing() {
11319   assert(NumCurrentElementsDeserializing &&
11320          "FinishedDeserializing not paired with StartedDeserializing");
11321   if (NumCurrentElementsDeserializing == 1) {
11322     // We decrease NumCurrentElementsDeserializing only after pending actions
11323     // are finished, to avoid recursively re-calling finishPendingActions().
11324     finishPendingActions();
11325   }
11326   --NumCurrentElementsDeserializing;
11327 
11328   if (NumCurrentElementsDeserializing == 0) {
11329     // Propagate exception specification and deduced type updates along
11330     // redeclaration chains.
11331     //
11332     // We do this now rather than in finishPendingActions because we want to
11333     // be able to walk the complete redeclaration chains of the updated decls.
11334     while (!PendingExceptionSpecUpdates.empty() ||
11335            !PendingDeducedTypeUpdates.empty()) {
11336       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11337       PendingExceptionSpecUpdates.clear();
11338       for (auto Update : ESUpdates) {
11339         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11340         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11341         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11342         if (auto *Listener = getContext().getASTMutationListener())
11343           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11344         for (auto *Redecl : Update.second->redecls())
11345           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11346       }
11347 
11348       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11349       PendingDeducedTypeUpdates.clear();
11350       for (auto Update : DTUpdates) {
11351         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11352         // FIXME: If the return type is already deduced, check that it matches.
11353         getContext().adjustDeducedFunctionResultType(Update.first,
11354                                                      Update.second);
11355       }
11356     }
11357 
11358     if (ReadTimer)
11359       ReadTimer->stopTimer();
11360 
11361     diagnoseOdrViolations();
11362 
11363     // We are not in recursive loading, so it's safe to pass the "interesting"
11364     // decls to the consumer.
11365     if (Consumer)
11366       PassInterestingDeclsToConsumer();
11367   }
11368 }
11369 
11370 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11371   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11372     // Remove any fake results before adding any real ones.
11373     auto It = PendingFakeLookupResults.find(II);
11374     if (It != PendingFakeLookupResults.end()) {
11375       for (auto *ND : It->second)
11376         SemaObj->IdResolver.RemoveDecl(ND);
11377       // FIXME: this works around module+PCH performance issue.
11378       // Rather than erase the result from the map, which is O(n), just clear
11379       // the vector of NamedDecls.
11380       It->second.clear();
11381     }
11382   }
11383 
11384   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11385     SemaObj->TUScope->AddDecl(D);
11386   } else if (SemaObj->TUScope) {
11387     // Adding the decl to IdResolver may have failed because it was already in
11388     // (even though it was not added in scope). If it is already in, make sure
11389     // it gets in the scope as well.
11390     if (std::find(SemaObj->IdResolver.begin(Name),
11391                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11392       SemaObj->TUScope->AddDecl(D);
11393   }
11394 }
11395 
11396 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11397                      ASTContext *Context,
11398                      const PCHContainerReader &PCHContainerRdr,
11399                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11400                      StringRef isysroot,
11401                      DisableValidationForModuleKind DisableValidationKind,
11402                      bool AllowASTWithCompilerErrors,
11403                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11404                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11405                      std::unique_ptr<llvm::Timer> ReadTimer)
11406     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11407                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11408                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11409       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11410       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11411       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11412                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11413       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11414       DisableValidationKind(DisableValidationKind),
11415       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11416       AllowConfigurationMismatch(AllowConfigurationMismatch),
11417       ValidateSystemInputs(ValidateSystemInputs),
11418       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11419       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11420   SourceMgr.setExternalSLocEntrySource(this);
11421 
11422   for (const auto &Ext : Extensions) {
11423     auto BlockName = Ext->getExtensionMetadata().BlockName;
11424     auto Known = ModuleFileExtensions.find(BlockName);
11425     if (Known != ModuleFileExtensions.end()) {
11426       Diags.Report(diag::warn_duplicate_module_file_extension)
11427         << BlockName;
11428       continue;
11429     }
11430 
11431     ModuleFileExtensions.insert({BlockName, Ext});
11432   }
11433 }
11434 
11435 ASTReader::~ASTReader() {
11436   if (OwnsDeserializationListener)
11437     delete DeserializationListener;
11438 }
11439 
11440 IdentifierResolver &ASTReader::getIdResolver() {
11441   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11442 }
11443 
11444 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11445                                                unsigned AbbrevID) {
11446   Idx = 0;
11447   Record.clear();
11448   return Cursor.readRecord(AbbrevID, Record);
11449 }
11450 //===----------------------------------------------------------------------===//
11451 //// OMPClauseReader implementation
11452 ////===----------------------------------------------------------------------===//
11453 
11454 // This has to be in namespace clang because it's friended by all
11455 // of the OMP clauses.
11456 namespace clang {
11457 
11458 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11459   ASTRecordReader &Record;
11460   ASTContext &Context;
11461 
11462 public:
11463   OMPClauseReader(ASTRecordReader &Record)
11464       : Record(Record), Context(Record.getContext()) {}
11465 #define GEN_CLANG_CLAUSE_CLASS
11466 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11467 #include "llvm/Frontend/OpenMP/OMP.inc"
11468   OMPClause *readClause();
11469   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11470   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11471 };
11472 
11473 } // end namespace clang
11474 
11475 OMPClause *ASTRecordReader::readOMPClause() {
11476   return OMPClauseReader(*this).readClause();
11477 }
11478 
11479 OMPClause *OMPClauseReader::readClause() {
11480   OMPClause *C = nullptr;
11481   switch (llvm::omp::Clause(Record.readInt())) {
11482   case llvm::omp::OMPC_if:
11483     C = new (Context) OMPIfClause();
11484     break;
11485   case llvm::omp::OMPC_final:
11486     C = new (Context) OMPFinalClause();
11487     break;
11488   case llvm::omp::OMPC_num_threads:
11489     C = new (Context) OMPNumThreadsClause();
11490     break;
11491   case llvm::omp::OMPC_safelen:
11492     C = new (Context) OMPSafelenClause();
11493     break;
11494   case llvm::omp::OMPC_simdlen:
11495     C = new (Context) OMPSimdlenClause();
11496     break;
11497   case llvm::omp::OMPC_sizes: {
11498     unsigned NumSizes = Record.readInt();
11499     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11500     break;
11501   }
11502   case llvm::omp::OMPC_full:
11503     C = OMPFullClause::CreateEmpty(Context);
11504     break;
11505   case llvm::omp::OMPC_partial:
11506     C = OMPPartialClause::CreateEmpty(Context);
11507     break;
11508   case llvm::omp::OMPC_allocator:
11509     C = new (Context) OMPAllocatorClause();
11510     break;
11511   case llvm::omp::OMPC_collapse:
11512     C = new (Context) OMPCollapseClause();
11513     break;
11514   case llvm::omp::OMPC_default:
11515     C = new (Context) OMPDefaultClause();
11516     break;
11517   case llvm::omp::OMPC_proc_bind:
11518     C = new (Context) OMPProcBindClause();
11519     break;
11520   case llvm::omp::OMPC_schedule:
11521     C = new (Context) OMPScheduleClause();
11522     break;
11523   case llvm::omp::OMPC_ordered:
11524     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11525     break;
11526   case llvm::omp::OMPC_nowait:
11527     C = new (Context) OMPNowaitClause();
11528     break;
11529   case llvm::omp::OMPC_untied:
11530     C = new (Context) OMPUntiedClause();
11531     break;
11532   case llvm::omp::OMPC_mergeable:
11533     C = new (Context) OMPMergeableClause();
11534     break;
11535   case llvm::omp::OMPC_read:
11536     C = new (Context) OMPReadClause();
11537     break;
11538   case llvm::omp::OMPC_write:
11539     C = new (Context) OMPWriteClause();
11540     break;
11541   case llvm::omp::OMPC_update:
11542     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11543     break;
11544   case llvm::omp::OMPC_capture:
11545     C = new (Context) OMPCaptureClause();
11546     break;
11547   case llvm::omp::OMPC_compare:
11548     C = new (Context) OMPCompareClause();
11549     break;
11550   case llvm::omp::OMPC_seq_cst:
11551     C = new (Context) OMPSeqCstClause();
11552     break;
11553   case llvm::omp::OMPC_acq_rel:
11554     C = new (Context) OMPAcqRelClause();
11555     break;
11556   case llvm::omp::OMPC_acquire:
11557     C = new (Context) OMPAcquireClause();
11558     break;
11559   case llvm::omp::OMPC_release:
11560     C = new (Context) OMPReleaseClause();
11561     break;
11562   case llvm::omp::OMPC_relaxed:
11563     C = new (Context) OMPRelaxedClause();
11564     break;
11565   case llvm::omp::OMPC_threads:
11566     C = new (Context) OMPThreadsClause();
11567     break;
11568   case llvm::omp::OMPC_simd:
11569     C = new (Context) OMPSIMDClause();
11570     break;
11571   case llvm::omp::OMPC_nogroup:
11572     C = new (Context) OMPNogroupClause();
11573     break;
11574   case llvm::omp::OMPC_unified_address:
11575     C = new (Context) OMPUnifiedAddressClause();
11576     break;
11577   case llvm::omp::OMPC_unified_shared_memory:
11578     C = new (Context) OMPUnifiedSharedMemoryClause();
11579     break;
11580   case llvm::omp::OMPC_reverse_offload:
11581     C = new (Context) OMPReverseOffloadClause();
11582     break;
11583   case llvm::omp::OMPC_dynamic_allocators:
11584     C = new (Context) OMPDynamicAllocatorsClause();
11585     break;
11586   case llvm::omp::OMPC_atomic_default_mem_order:
11587     C = new (Context) OMPAtomicDefaultMemOrderClause();
11588     break;
11589  case llvm::omp::OMPC_private:
11590     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11591     break;
11592   case llvm::omp::OMPC_firstprivate:
11593     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11594     break;
11595   case llvm::omp::OMPC_lastprivate:
11596     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11597     break;
11598   case llvm::omp::OMPC_shared:
11599     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11600     break;
11601   case llvm::omp::OMPC_reduction: {
11602     unsigned N = Record.readInt();
11603     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11604     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11605     break;
11606   }
11607   case llvm::omp::OMPC_task_reduction:
11608     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11609     break;
11610   case llvm::omp::OMPC_in_reduction:
11611     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11612     break;
11613   case llvm::omp::OMPC_linear:
11614     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11615     break;
11616   case llvm::omp::OMPC_aligned:
11617     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11618     break;
11619   case llvm::omp::OMPC_copyin:
11620     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11621     break;
11622   case llvm::omp::OMPC_copyprivate:
11623     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11624     break;
11625   case llvm::omp::OMPC_flush:
11626     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11627     break;
11628   case llvm::omp::OMPC_depobj:
11629     C = OMPDepobjClause::CreateEmpty(Context);
11630     break;
11631   case llvm::omp::OMPC_depend: {
11632     unsigned NumVars = Record.readInt();
11633     unsigned NumLoops = Record.readInt();
11634     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11635     break;
11636   }
11637   case llvm::omp::OMPC_device:
11638     C = new (Context) OMPDeviceClause();
11639     break;
11640   case llvm::omp::OMPC_map: {
11641     OMPMappableExprListSizeTy Sizes;
11642     Sizes.NumVars = Record.readInt();
11643     Sizes.NumUniqueDeclarations = Record.readInt();
11644     Sizes.NumComponentLists = Record.readInt();
11645     Sizes.NumComponents = Record.readInt();
11646     C = OMPMapClause::CreateEmpty(Context, Sizes);
11647     break;
11648   }
11649   case llvm::omp::OMPC_num_teams:
11650     C = new (Context) OMPNumTeamsClause();
11651     break;
11652   case llvm::omp::OMPC_thread_limit:
11653     C = new (Context) OMPThreadLimitClause();
11654     break;
11655   case llvm::omp::OMPC_priority:
11656     C = new (Context) OMPPriorityClause();
11657     break;
11658   case llvm::omp::OMPC_grainsize:
11659     C = new (Context) OMPGrainsizeClause();
11660     break;
11661   case llvm::omp::OMPC_num_tasks:
11662     C = new (Context) OMPNumTasksClause();
11663     break;
11664   case llvm::omp::OMPC_hint:
11665     C = new (Context) OMPHintClause();
11666     break;
11667   case llvm::omp::OMPC_dist_schedule:
11668     C = new (Context) OMPDistScheduleClause();
11669     break;
11670   case llvm::omp::OMPC_defaultmap:
11671     C = new (Context) OMPDefaultmapClause();
11672     break;
11673   case llvm::omp::OMPC_to: {
11674     OMPMappableExprListSizeTy Sizes;
11675     Sizes.NumVars = Record.readInt();
11676     Sizes.NumUniqueDeclarations = Record.readInt();
11677     Sizes.NumComponentLists = Record.readInt();
11678     Sizes.NumComponents = Record.readInt();
11679     C = OMPToClause::CreateEmpty(Context, Sizes);
11680     break;
11681   }
11682   case llvm::omp::OMPC_from: {
11683     OMPMappableExprListSizeTy Sizes;
11684     Sizes.NumVars = Record.readInt();
11685     Sizes.NumUniqueDeclarations = Record.readInt();
11686     Sizes.NumComponentLists = Record.readInt();
11687     Sizes.NumComponents = Record.readInt();
11688     C = OMPFromClause::CreateEmpty(Context, Sizes);
11689     break;
11690   }
11691   case llvm::omp::OMPC_use_device_ptr: {
11692     OMPMappableExprListSizeTy Sizes;
11693     Sizes.NumVars = Record.readInt();
11694     Sizes.NumUniqueDeclarations = Record.readInt();
11695     Sizes.NumComponentLists = Record.readInt();
11696     Sizes.NumComponents = Record.readInt();
11697     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11698     break;
11699   }
11700   case llvm::omp::OMPC_use_device_addr: {
11701     OMPMappableExprListSizeTy Sizes;
11702     Sizes.NumVars = Record.readInt();
11703     Sizes.NumUniqueDeclarations = Record.readInt();
11704     Sizes.NumComponentLists = Record.readInt();
11705     Sizes.NumComponents = Record.readInt();
11706     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11707     break;
11708   }
11709   case llvm::omp::OMPC_is_device_ptr: {
11710     OMPMappableExprListSizeTy Sizes;
11711     Sizes.NumVars = Record.readInt();
11712     Sizes.NumUniqueDeclarations = Record.readInt();
11713     Sizes.NumComponentLists = Record.readInt();
11714     Sizes.NumComponents = Record.readInt();
11715     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11716     break;
11717   }
11718   case llvm::omp::OMPC_has_device_addr: {
11719     OMPMappableExprListSizeTy Sizes;
11720     Sizes.NumVars = Record.readInt();
11721     Sizes.NumUniqueDeclarations = Record.readInt();
11722     Sizes.NumComponentLists = Record.readInt();
11723     Sizes.NumComponents = Record.readInt();
11724     C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);
11725     break;
11726   }
11727   case llvm::omp::OMPC_allocate:
11728     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11729     break;
11730   case llvm::omp::OMPC_nontemporal:
11731     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11732     break;
11733   case llvm::omp::OMPC_inclusive:
11734     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11735     break;
11736   case llvm::omp::OMPC_exclusive:
11737     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11738     break;
11739   case llvm::omp::OMPC_order:
11740     C = new (Context) OMPOrderClause();
11741     break;
11742   case llvm::omp::OMPC_init:
11743     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11744     break;
11745   case llvm::omp::OMPC_use:
11746     C = new (Context) OMPUseClause();
11747     break;
11748   case llvm::omp::OMPC_destroy:
11749     C = new (Context) OMPDestroyClause();
11750     break;
11751   case llvm::omp::OMPC_novariants:
11752     C = new (Context) OMPNovariantsClause();
11753     break;
11754   case llvm::omp::OMPC_nocontext:
11755     C = new (Context) OMPNocontextClause();
11756     break;
11757   case llvm::omp::OMPC_detach:
11758     C = new (Context) OMPDetachClause();
11759     break;
11760   case llvm::omp::OMPC_uses_allocators:
11761     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11762     break;
11763   case llvm::omp::OMPC_affinity:
11764     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11765     break;
11766   case llvm::omp::OMPC_filter:
11767     C = new (Context) OMPFilterClause();
11768     break;
11769   case llvm::omp::OMPC_bind:
11770     C = OMPBindClause::CreateEmpty(Context);
11771     break;
11772   case llvm::omp::OMPC_align:
11773     C = new (Context) OMPAlignClause();
11774     break;
11775 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11776   case llvm::omp::Enum:                                                        \
11777     break;
11778 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11779   default:
11780     break;
11781   }
11782   assert(C && "Unknown OMPClause type");
11783 
11784   Visit(C);
11785   C->setLocStart(Record.readSourceLocation());
11786   C->setLocEnd(Record.readSourceLocation());
11787 
11788   return C;
11789 }
11790 
11791 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11792   C->setPreInitStmt(Record.readSubStmt(),
11793                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11794 }
11795 
11796 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11797   VisitOMPClauseWithPreInit(C);
11798   C->setPostUpdateExpr(Record.readSubExpr());
11799 }
11800 
11801 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11802   VisitOMPClauseWithPreInit(C);
11803   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11804   C->setNameModifierLoc(Record.readSourceLocation());
11805   C->setColonLoc(Record.readSourceLocation());
11806   C->setCondition(Record.readSubExpr());
11807   C->setLParenLoc(Record.readSourceLocation());
11808 }
11809 
11810 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11811   VisitOMPClauseWithPreInit(C);
11812   C->setCondition(Record.readSubExpr());
11813   C->setLParenLoc(Record.readSourceLocation());
11814 }
11815 
11816 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11817   VisitOMPClauseWithPreInit(C);
11818   C->setNumThreads(Record.readSubExpr());
11819   C->setLParenLoc(Record.readSourceLocation());
11820 }
11821 
11822 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11823   C->setSafelen(Record.readSubExpr());
11824   C->setLParenLoc(Record.readSourceLocation());
11825 }
11826 
11827 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11828   C->setSimdlen(Record.readSubExpr());
11829   C->setLParenLoc(Record.readSourceLocation());
11830 }
11831 
11832 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11833   for (Expr *&E : C->getSizesRefs())
11834     E = Record.readSubExpr();
11835   C->setLParenLoc(Record.readSourceLocation());
11836 }
11837 
11838 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11839 
11840 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11841   C->setFactor(Record.readSubExpr());
11842   C->setLParenLoc(Record.readSourceLocation());
11843 }
11844 
11845 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11846   C->setAllocator(Record.readExpr());
11847   C->setLParenLoc(Record.readSourceLocation());
11848 }
11849 
11850 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11851   C->setNumForLoops(Record.readSubExpr());
11852   C->setLParenLoc(Record.readSourceLocation());
11853 }
11854 
11855 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11856   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11857   C->setLParenLoc(Record.readSourceLocation());
11858   C->setDefaultKindKwLoc(Record.readSourceLocation());
11859 }
11860 
11861 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11862   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11863   C->setLParenLoc(Record.readSourceLocation());
11864   C->setProcBindKindKwLoc(Record.readSourceLocation());
11865 }
11866 
11867 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11868   VisitOMPClauseWithPreInit(C);
11869   C->setScheduleKind(
11870        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11871   C->setFirstScheduleModifier(
11872       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11873   C->setSecondScheduleModifier(
11874       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11875   C->setChunkSize(Record.readSubExpr());
11876   C->setLParenLoc(Record.readSourceLocation());
11877   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11878   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11879   C->setScheduleKindLoc(Record.readSourceLocation());
11880   C->setCommaLoc(Record.readSourceLocation());
11881 }
11882 
11883 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11884   C->setNumForLoops(Record.readSubExpr());
11885   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11886     C->setLoopNumIterations(I, Record.readSubExpr());
11887   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11888     C->setLoopCounter(I, Record.readSubExpr());
11889   C->setLParenLoc(Record.readSourceLocation());
11890 }
11891 
11892 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11893   C->setEventHandler(Record.readSubExpr());
11894   C->setLParenLoc(Record.readSourceLocation());
11895 }
11896 
11897 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11898 
11899 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11900 
11901 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11902 
11903 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11904 
11905 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11906 
11907 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11908   if (C->isExtended()) {
11909     C->setLParenLoc(Record.readSourceLocation());
11910     C->setArgumentLoc(Record.readSourceLocation());
11911     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11912   }
11913 }
11914 
11915 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11916 
11917 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11918 
11919 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11920 
11921 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11922 
11923 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11924 
11925 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11926 
11927 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11928 
11929 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11930 
11931 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11932 
11933 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11934 
11935 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
11936   unsigned NumVars = C->varlist_size();
11937   SmallVector<Expr *, 16> Vars;
11938   Vars.reserve(NumVars);
11939   for (unsigned I = 0; I != NumVars; ++I)
11940     Vars.push_back(Record.readSubExpr());
11941   C->setVarRefs(Vars);
11942   C->setIsTarget(Record.readBool());
11943   C->setIsTargetSync(Record.readBool());
11944   C->setLParenLoc(Record.readSourceLocation());
11945   C->setVarLoc(Record.readSourceLocation());
11946 }
11947 
11948 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
11949   C->setInteropVar(Record.readSubExpr());
11950   C->setLParenLoc(Record.readSourceLocation());
11951   C->setVarLoc(Record.readSourceLocation());
11952 }
11953 
11954 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
11955   C->setInteropVar(Record.readSubExpr());
11956   C->setLParenLoc(Record.readSourceLocation());
11957   C->setVarLoc(Record.readSourceLocation());
11958 }
11959 
11960 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
11961   VisitOMPClauseWithPreInit(C);
11962   C->setCondition(Record.readSubExpr());
11963   C->setLParenLoc(Record.readSourceLocation());
11964 }
11965 
11966 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
11967   VisitOMPClauseWithPreInit(C);
11968   C->setCondition(Record.readSubExpr());
11969   C->setLParenLoc(Record.readSourceLocation());
11970 }
11971 
11972 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11973 
11974 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11975     OMPUnifiedSharedMemoryClause *) {}
11976 
11977 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11978 
11979 void
11980 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11981 }
11982 
11983 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11984     OMPAtomicDefaultMemOrderClause *C) {
11985   C->setAtomicDefaultMemOrderKind(
11986       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11987   C->setLParenLoc(Record.readSourceLocation());
11988   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11989 }
11990 
11991 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11992   C->setLParenLoc(Record.readSourceLocation());
11993   unsigned NumVars = C->varlist_size();
11994   SmallVector<Expr *, 16> Vars;
11995   Vars.reserve(NumVars);
11996   for (unsigned i = 0; i != NumVars; ++i)
11997     Vars.push_back(Record.readSubExpr());
11998   C->setVarRefs(Vars);
11999   Vars.clear();
12000   for (unsigned i = 0; i != NumVars; ++i)
12001     Vars.push_back(Record.readSubExpr());
12002   C->setPrivateCopies(Vars);
12003 }
12004 
12005 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12006   VisitOMPClauseWithPreInit(C);
12007   C->setLParenLoc(Record.readSourceLocation());
12008   unsigned NumVars = C->varlist_size();
12009   SmallVector<Expr *, 16> Vars;
12010   Vars.reserve(NumVars);
12011   for (unsigned i = 0; i != NumVars; ++i)
12012     Vars.push_back(Record.readSubExpr());
12013   C->setVarRefs(Vars);
12014   Vars.clear();
12015   for (unsigned i = 0; i != NumVars; ++i)
12016     Vars.push_back(Record.readSubExpr());
12017   C->setPrivateCopies(Vars);
12018   Vars.clear();
12019   for (unsigned i = 0; i != NumVars; ++i)
12020     Vars.push_back(Record.readSubExpr());
12021   C->setInits(Vars);
12022 }
12023 
12024 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12025   VisitOMPClauseWithPostUpdate(C);
12026   C->setLParenLoc(Record.readSourceLocation());
12027   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12028   C->setKindLoc(Record.readSourceLocation());
12029   C->setColonLoc(Record.readSourceLocation());
12030   unsigned NumVars = C->varlist_size();
12031   SmallVector<Expr *, 16> Vars;
12032   Vars.reserve(NumVars);
12033   for (unsigned i = 0; i != NumVars; ++i)
12034     Vars.push_back(Record.readSubExpr());
12035   C->setVarRefs(Vars);
12036   Vars.clear();
12037   for (unsigned i = 0; i != NumVars; ++i)
12038     Vars.push_back(Record.readSubExpr());
12039   C->setPrivateCopies(Vars);
12040   Vars.clear();
12041   for (unsigned i = 0; i != NumVars; ++i)
12042     Vars.push_back(Record.readSubExpr());
12043   C->setSourceExprs(Vars);
12044   Vars.clear();
12045   for (unsigned i = 0; i != NumVars; ++i)
12046     Vars.push_back(Record.readSubExpr());
12047   C->setDestinationExprs(Vars);
12048   Vars.clear();
12049   for (unsigned i = 0; i != NumVars; ++i)
12050     Vars.push_back(Record.readSubExpr());
12051   C->setAssignmentOps(Vars);
12052 }
12053 
12054 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12055   C->setLParenLoc(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 }
12063 
12064 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12065   VisitOMPClauseWithPostUpdate(C);
12066   C->setLParenLoc(Record.readSourceLocation());
12067   C->setModifierLoc(Record.readSourceLocation());
12068   C->setColonLoc(Record.readSourceLocation());
12069   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12070   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12071   C->setQualifierLoc(NNSL);
12072   C->setNameInfo(DNI);
12073 
12074   unsigned NumVars = C->varlist_size();
12075   SmallVector<Expr *, 16> Vars;
12076   Vars.reserve(NumVars);
12077   for (unsigned i = 0; i != NumVars; ++i)
12078     Vars.push_back(Record.readSubExpr());
12079   C->setVarRefs(Vars);
12080   Vars.clear();
12081   for (unsigned i = 0; i != NumVars; ++i)
12082     Vars.push_back(Record.readSubExpr());
12083   C->setPrivates(Vars);
12084   Vars.clear();
12085   for (unsigned i = 0; i != NumVars; ++i)
12086     Vars.push_back(Record.readSubExpr());
12087   C->setLHSExprs(Vars);
12088   Vars.clear();
12089   for (unsigned i = 0; i != NumVars; ++i)
12090     Vars.push_back(Record.readSubExpr());
12091   C->setRHSExprs(Vars);
12092   Vars.clear();
12093   for (unsigned i = 0; i != NumVars; ++i)
12094     Vars.push_back(Record.readSubExpr());
12095   C->setReductionOps(Vars);
12096   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12097     Vars.clear();
12098     for (unsigned i = 0; i != NumVars; ++i)
12099       Vars.push_back(Record.readSubExpr());
12100     C->setInscanCopyOps(Vars);
12101     Vars.clear();
12102     for (unsigned i = 0; i != NumVars; ++i)
12103       Vars.push_back(Record.readSubExpr());
12104     C->setInscanCopyArrayTemps(Vars);
12105     Vars.clear();
12106     for (unsigned i = 0; i != NumVars; ++i)
12107       Vars.push_back(Record.readSubExpr());
12108     C->setInscanCopyArrayElems(Vars);
12109   }
12110 }
12111 
12112 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12113   VisitOMPClauseWithPostUpdate(C);
12114   C->setLParenLoc(Record.readSourceLocation());
12115   C->setColonLoc(Record.readSourceLocation());
12116   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12117   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12118   C->setQualifierLoc(NNSL);
12119   C->setNameInfo(DNI);
12120 
12121   unsigned NumVars = C->varlist_size();
12122   SmallVector<Expr *, 16> Vars;
12123   Vars.reserve(NumVars);
12124   for (unsigned I = 0; I != NumVars; ++I)
12125     Vars.push_back(Record.readSubExpr());
12126   C->setVarRefs(Vars);
12127   Vars.clear();
12128   for (unsigned I = 0; I != NumVars; ++I)
12129     Vars.push_back(Record.readSubExpr());
12130   C->setPrivates(Vars);
12131   Vars.clear();
12132   for (unsigned I = 0; I != NumVars; ++I)
12133     Vars.push_back(Record.readSubExpr());
12134   C->setLHSExprs(Vars);
12135   Vars.clear();
12136   for (unsigned I = 0; I != NumVars; ++I)
12137     Vars.push_back(Record.readSubExpr());
12138   C->setRHSExprs(Vars);
12139   Vars.clear();
12140   for (unsigned I = 0; I != NumVars; ++I)
12141     Vars.push_back(Record.readSubExpr());
12142   C->setReductionOps(Vars);
12143 }
12144 
12145 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12146   VisitOMPClauseWithPostUpdate(C);
12147   C->setLParenLoc(Record.readSourceLocation());
12148   C->setColonLoc(Record.readSourceLocation());
12149   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12150   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12151   C->setQualifierLoc(NNSL);
12152   C->setNameInfo(DNI);
12153 
12154   unsigned NumVars = C->varlist_size();
12155   SmallVector<Expr *, 16> Vars;
12156   Vars.reserve(NumVars);
12157   for (unsigned I = 0; I != NumVars; ++I)
12158     Vars.push_back(Record.readSubExpr());
12159   C->setVarRefs(Vars);
12160   Vars.clear();
12161   for (unsigned I = 0; I != NumVars; ++I)
12162     Vars.push_back(Record.readSubExpr());
12163   C->setPrivates(Vars);
12164   Vars.clear();
12165   for (unsigned I = 0; I != NumVars; ++I)
12166     Vars.push_back(Record.readSubExpr());
12167   C->setLHSExprs(Vars);
12168   Vars.clear();
12169   for (unsigned I = 0; I != NumVars; ++I)
12170     Vars.push_back(Record.readSubExpr());
12171   C->setRHSExprs(Vars);
12172   Vars.clear();
12173   for (unsigned I = 0; I != NumVars; ++I)
12174     Vars.push_back(Record.readSubExpr());
12175   C->setReductionOps(Vars);
12176   Vars.clear();
12177   for (unsigned I = 0; I != NumVars; ++I)
12178     Vars.push_back(Record.readSubExpr());
12179   C->setTaskgroupDescriptors(Vars);
12180 }
12181 
12182 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12183   VisitOMPClauseWithPostUpdate(C);
12184   C->setLParenLoc(Record.readSourceLocation());
12185   C->setColonLoc(Record.readSourceLocation());
12186   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12187   C->setModifierLoc(Record.readSourceLocation());
12188   unsigned NumVars = C->varlist_size();
12189   SmallVector<Expr *, 16> Vars;
12190   Vars.reserve(NumVars);
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setVarRefs(Vars);
12194   Vars.clear();
12195   for (unsigned i = 0; i != NumVars; ++i)
12196     Vars.push_back(Record.readSubExpr());
12197   C->setPrivates(Vars);
12198   Vars.clear();
12199   for (unsigned i = 0; i != NumVars; ++i)
12200     Vars.push_back(Record.readSubExpr());
12201   C->setInits(Vars);
12202   Vars.clear();
12203   for (unsigned i = 0; i != NumVars; ++i)
12204     Vars.push_back(Record.readSubExpr());
12205   C->setUpdates(Vars);
12206   Vars.clear();
12207   for (unsigned i = 0; i != NumVars; ++i)
12208     Vars.push_back(Record.readSubExpr());
12209   C->setFinals(Vars);
12210   C->setStep(Record.readSubExpr());
12211   C->setCalcStep(Record.readSubExpr());
12212   Vars.clear();
12213   for (unsigned I = 0; I != NumVars + 1; ++I)
12214     Vars.push_back(Record.readSubExpr());
12215   C->setUsedExprs(Vars);
12216 }
12217 
12218 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12219   C->setLParenLoc(Record.readSourceLocation());
12220   C->setColonLoc(Record.readSourceLocation());
12221   unsigned NumVars = C->varlist_size();
12222   SmallVector<Expr *, 16> Vars;
12223   Vars.reserve(NumVars);
12224   for (unsigned i = 0; i != NumVars; ++i)
12225     Vars.push_back(Record.readSubExpr());
12226   C->setVarRefs(Vars);
12227   C->setAlignment(Record.readSubExpr());
12228 }
12229 
12230 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12231   C->setLParenLoc(Record.readSourceLocation());
12232   unsigned NumVars = C->varlist_size();
12233   SmallVector<Expr *, 16> Exprs;
12234   Exprs.reserve(NumVars);
12235   for (unsigned i = 0; i != NumVars; ++i)
12236     Exprs.push_back(Record.readSubExpr());
12237   C->setVarRefs(Exprs);
12238   Exprs.clear();
12239   for (unsigned i = 0; i != NumVars; ++i)
12240     Exprs.push_back(Record.readSubExpr());
12241   C->setSourceExprs(Exprs);
12242   Exprs.clear();
12243   for (unsigned i = 0; i != NumVars; ++i)
12244     Exprs.push_back(Record.readSubExpr());
12245   C->setDestinationExprs(Exprs);
12246   Exprs.clear();
12247   for (unsigned i = 0; i != NumVars; ++i)
12248     Exprs.push_back(Record.readSubExpr());
12249   C->setAssignmentOps(Exprs);
12250 }
12251 
12252 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12253   C->setLParenLoc(Record.readSourceLocation());
12254   unsigned NumVars = C->varlist_size();
12255   SmallVector<Expr *, 16> Exprs;
12256   Exprs.reserve(NumVars);
12257   for (unsigned i = 0; i != NumVars; ++i)
12258     Exprs.push_back(Record.readSubExpr());
12259   C->setVarRefs(Exprs);
12260   Exprs.clear();
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Exprs.push_back(Record.readSubExpr());
12263   C->setSourceExprs(Exprs);
12264   Exprs.clear();
12265   for (unsigned i = 0; i != NumVars; ++i)
12266     Exprs.push_back(Record.readSubExpr());
12267   C->setDestinationExprs(Exprs);
12268   Exprs.clear();
12269   for (unsigned i = 0; i != NumVars; ++i)
12270     Exprs.push_back(Record.readSubExpr());
12271   C->setAssignmentOps(Exprs);
12272 }
12273 
12274 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12275   C->setLParenLoc(Record.readSourceLocation());
12276   unsigned NumVars = C->varlist_size();
12277   SmallVector<Expr *, 16> Vars;
12278   Vars.reserve(NumVars);
12279   for (unsigned i = 0; i != NumVars; ++i)
12280     Vars.push_back(Record.readSubExpr());
12281   C->setVarRefs(Vars);
12282 }
12283 
12284 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12285   C->setDepobj(Record.readSubExpr());
12286   C->setLParenLoc(Record.readSourceLocation());
12287 }
12288 
12289 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12290   C->setLParenLoc(Record.readSourceLocation());
12291   C->setModifier(Record.readSubExpr());
12292   C->setDependencyKind(
12293       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12294   C->setDependencyLoc(Record.readSourceLocation());
12295   C->setColonLoc(Record.readSourceLocation());
12296   C->setOmpAllMemoryLoc(Record.readSourceLocation());
12297   unsigned NumVars = C->varlist_size();
12298   SmallVector<Expr *, 16> Vars;
12299   Vars.reserve(NumVars);
12300   for (unsigned I = 0; I != NumVars; ++I)
12301     Vars.push_back(Record.readSubExpr());
12302   C->setVarRefs(Vars);
12303   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12304     C->setLoopData(I, Record.readSubExpr());
12305 }
12306 
12307 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12308   VisitOMPClauseWithPreInit(C);
12309   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12310   C->setDevice(Record.readSubExpr());
12311   C->setModifierLoc(Record.readSourceLocation());
12312   C->setLParenLoc(Record.readSourceLocation());
12313 }
12314 
12315 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12316   C->setLParenLoc(Record.readSourceLocation());
12317   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12318     C->setMapTypeModifier(
12319         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12320     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12321   }
12322   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12323   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12324   C->setMapType(
12325      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12326   C->setMapLoc(Record.readSourceLocation());
12327   C->setColonLoc(Record.readSourceLocation());
12328   auto NumVars = C->varlist_size();
12329   auto UniqueDecls = C->getUniqueDeclarationsNum();
12330   auto TotalLists = C->getTotalComponentListNum();
12331   auto TotalComponents = C->getTotalComponentsNum();
12332 
12333   SmallVector<Expr *, 16> Vars;
12334   Vars.reserve(NumVars);
12335   for (unsigned i = 0; i != NumVars; ++i)
12336     Vars.push_back(Record.readExpr());
12337   C->setVarRefs(Vars);
12338 
12339   SmallVector<Expr *, 16> UDMappers;
12340   UDMappers.reserve(NumVars);
12341   for (unsigned I = 0; I < NumVars; ++I)
12342     UDMappers.push_back(Record.readExpr());
12343   C->setUDMapperRefs(UDMappers);
12344 
12345   SmallVector<ValueDecl *, 16> Decls;
12346   Decls.reserve(UniqueDecls);
12347   for (unsigned i = 0; i < UniqueDecls; ++i)
12348     Decls.push_back(Record.readDeclAs<ValueDecl>());
12349   C->setUniqueDecls(Decls);
12350 
12351   SmallVector<unsigned, 16> ListsPerDecl;
12352   ListsPerDecl.reserve(UniqueDecls);
12353   for (unsigned i = 0; i < UniqueDecls; ++i)
12354     ListsPerDecl.push_back(Record.readInt());
12355   C->setDeclNumLists(ListsPerDecl);
12356 
12357   SmallVector<unsigned, 32> ListSizes;
12358   ListSizes.reserve(TotalLists);
12359   for (unsigned i = 0; i < TotalLists; ++i)
12360     ListSizes.push_back(Record.readInt());
12361   C->setComponentListSizes(ListSizes);
12362 
12363   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12364   Components.reserve(TotalComponents);
12365   for (unsigned i = 0; i < TotalComponents; ++i) {
12366     Expr *AssociatedExprPr = Record.readExpr();
12367     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12368     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12369                             /*IsNonContiguous=*/false);
12370   }
12371   C->setComponents(Components, ListSizes);
12372 }
12373 
12374 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12375   C->setLParenLoc(Record.readSourceLocation());
12376   C->setColonLoc(Record.readSourceLocation());
12377   C->setAllocator(Record.readSubExpr());
12378   unsigned NumVars = C->varlist_size();
12379   SmallVector<Expr *, 16> Vars;
12380   Vars.reserve(NumVars);
12381   for (unsigned i = 0; i != NumVars; ++i)
12382     Vars.push_back(Record.readSubExpr());
12383   C->setVarRefs(Vars);
12384 }
12385 
12386 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12387   VisitOMPClauseWithPreInit(C);
12388   C->setNumTeams(Record.readSubExpr());
12389   C->setLParenLoc(Record.readSourceLocation());
12390 }
12391 
12392 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12393   VisitOMPClauseWithPreInit(C);
12394   C->setThreadLimit(Record.readSubExpr());
12395   C->setLParenLoc(Record.readSourceLocation());
12396 }
12397 
12398 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12399   VisitOMPClauseWithPreInit(C);
12400   C->setPriority(Record.readSubExpr());
12401   C->setLParenLoc(Record.readSourceLocation());
12402 }
12403 
12404 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12405   VisitOMPClauseWithPreInit(C);
12406   C->setGrainsize(Record.readSubExpr());
12407   C->setLParenLoc(Record.readSourceLocation());
12408 }
12409 
12410 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12411   VisitOMPClauseWithPreInit(C);
12412   C->setNumTasks(Record.readSubExpr());
12413   C->setLParenLoc(Record.readSourceLocation());
12414 }
12415 
12416 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12417   C->setHint(Record.readSubExpr());
12418   C->setLParenLoc(Record.readSourceLocation());
12419 }
12420 
12421 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12422   VisitOMPClauseWithPreInit(C);
12423   C->setDistScheduleKind(
12424       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12425   C->setChunkSize(Record.readSubExpr());
12426   C->setLParenLoc(Record.readSourceLocation());
12427   C->setDistScheduleKindLoc(Record.readSourceLocation());
12428   C->setCommaLoc(Record.readSourceLocation());
12429 }
12430 
12431 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12432   C->setDefaultmapKind(
12433        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12434   C->setDefaultmapModifier(
12435       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12436   C->setLParenLoc(Record.readSourceLocation());
12437   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12438   C->setDefaultmapKindLoc(Record.readSourceLocation());
12439 }
12440 
12441 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12442   C->setLParenLoc(Record.readSourceLocation());
12443   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12444     C->setMotionModifier(
12445         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12446     C->setMotionModifierLoc(I, Record.readSourceLocation());
12447   }
12448   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12449   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12450   C->setColonLoc(Record.readSourceLocation());
12451   auto NumVars = C->varlist_size();
12452   auto UniqueDecls = C->getUniqueDeclarationsNum();
12453   auto TotalLists = C->getTotalComponentListNum();
12454   auto TotalComponents = C->getTotalComponentsNum();
12455 
12456   SmallVector<Expr *, 16> Vars;
12457   Vars.reserve(NumVars);
12458   for (unsigned i = 0; i != NumVars; ++i)
12459     Vars.push_back(Record.readSubExpr());
12460   C->setVarRefs(Vars);
12461 
12462   SmallVector<Expr *, 16> UDMappers;
12463   UDMappers.reserve(NumVars);
12464   for (unsigned I = 0; I < NumVars; ++I)
12465     UDMappers.push_back(Record.readSubExpr());
12466   C->setUDMapperRefs(UDMappers);
12467 
12468   SmallVector<ValueDecl *, 16> Decls;
12469   Decls.reserve(UniqueDecls);
12470   for (unsigned i = 0; i < UniqueDecls; ++i)
12471     Decls.push_back(Record.readDeclAs<ValueDecl>());
12472   C->setUniqueDecls(Decls);
12473 
12474   SmallVector<unsigned, 16> ListsPerDecl;
12475   ListsPerDecl.reserve(UniqueDecls);
12476   for (unsigned i = 0; i < UniqueDecls; ++i)
12477     ListsPerDecl.push_back(Record.readInt());
12478   C->setDeclNumLists(ListsPerDecl);
12479 
12480   SmallVector<unsigned, 32> ListSizes;
12481   ListSizes.reserve(TotalLists);
12482   for (unsigned i = 0; i < TotalLists; ++i)
12483     ListSizes.push_back(Record.readInt());
12484   C->setComponentListSizes(ListSizes);
12485 
12486   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12487   Components.reserve(TotalComponents);
12488   for (unsigned i = 0; i < TotalComponents; ++i) {
12489     Expr *AssociatedExprPr = Record.readSubExpr();
12490     bool IsNonContiguous = Record.readBool();
12491     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12492     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12493   }
12494   C->setComponents(Components, ListSizes);
12495 }
12496 
12497 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12498   C->setLParenLoc(Record.readSourceLocation());
12499   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12500     C->setMotionModifier(
12501         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12502     C->setMotionModifierLoc(I, Record.readSourceLocation());
12503   }
12504   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12505   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12506   C->setColonLoc(Record.readSourceLocation());
12507   auto NumVars = C->varlist_size();
12508   auto UniqueDecls = C->getUniqueDeclarationsNum();
12509   auto TotalLists = C->getTotalComponentListNum();
12510   auto TotalComponents = C->getTotalComponentsNum();
12511 
12512   SmallVector<Expr *, 16> Vars;
12513   Vars.reserve(NumVars);
12514   for (unsigned i = 0; i != NumVars; ++i)
12515     Vars.push_back(Record.readSubExpr());
12516   C->setVarRefs(Vars);
12517 
12518   SmallVector<Expr *, 16> UDMappers;
12519   UDMappers.reserve(NumVars);
12520   for (unsigned I = 0; I < NumVars; ++I)
12521     UDMappers.push_back(Record.readSubExpr());
12522   C->setUDMapperRefs(UDMappers);
12523 
12524   SmallVector<ValueDecl *, 16> Decls;
12525   Decls.reserve(UniqueDecls);
12526   for (unsigned i = 0; i < UniqueDecls; ++i)
12527     Decls.push_back(Record.readDeclAs<ValueDecl>());
12528   C->setUniqueDecls(Decls);
12529 
12530   SmallVector<unsigned, 16> ListsPerDecl;
12531   ListsPerDecl.reserve(UniqueDecls);
12532   for (unsigned i = 0; i < UniqueDecls; ++i)
12533     ListsPerDecl.push_back(Record.readInt());
12534   C->setDeclNumLists(ListsPerDecl);
12535 
12536   SmallVector<unsigned, 32> ListSizes;
12537   ListSizes.reserve(TotalLists);
12538   for (unsigned i = 0; i < TotalLists; ++i)
12539     ListSizes.push_back(Record.readInt());
12540   C->setComponentListSizes(ListSizes);
12541 
12542   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12543   Components.reserve(TotalComponents);
12544   for (unsigned i = 0; i < TotalComponents; ++i) {
12545     Expr *AssociatedExprPr = Record.readSubExpr();
12546     bool IsNonContiguous = Record.readBool();
12547     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12548     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12549   }
12550   C->setComponents(Components, ListSizes);
12551 }
12552 
12553 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12554   C->setLParenLoc(Record.readSourceLocation());
12555   auto NumVars = C->varlist_size();
12556   auto UniqueDecls = C->getUniqueDeclarationsNum();
12557   auto TotalLists = C->getTotalComponentListNum();
12558   auto TotalComponents = C->getTotalComponentsNum();
12559 
12560   SmallVector<Expr *, 16> Vars;
12561   Vars.reserve(NumVars);
12562   for (unsigned i = 0; i != NumVars; ++i)
12563     Vars.push_back(Record.readSubExpr());
12564   C->setVarRefs(Vars);
12565   Vars.clear();
12566   for (unsigned i = 0; i != NumVars; ++i)
12567     Vars.push_back(Record.readSubExpr());
12568   C->setPrivateCopies(Vars);
12569   Vars.clear();
12570   for (unsigned i = 0; i != NumVars; ++i)
12571     Vars.push_back(Record.readSubExpr());
12572   C->setInits(Vars);
12573 
12574   SmallVector<ValueDecl *, 16> Decls;
12575   Decls.reserve(UniqueDecls);
12576   for (unsigned i = 0; i < UniqueDecls; ++i)
12577     Decls.push_back(Record.readDeclAs<ValueDecl>());
12578   C->setUniqueDecls(Decls);
12579 
12580   SmallVector<unsigned, 16> ListsPerDecl;
12581   ListsPerDecl.reserve(UniqueDecls);
12582   for (unsigned i = 0; i < UniqueDecls; ++i)
12583     ListsPerDecl.push_back(Record.readInt());
12584   C->setDeclNumLists(ListsPerDecl);
12585 
12586   SmallVector<unsigned, 32> ListSizes;
12587   ListSizes.reserve(TotalLists);
12588   for (unsigned i = 0; i < TotalLists; ++i)
12589     ListSizes.push_back(Record.readInt());
12590   C->setComponentListSizes(ListSizes);
12591 
12592   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12593   Components.reserve(TotalComponents);
12594   for (unsigned i = 0; i < TotalComponents; ++i) {
12595     auto *AssociatedExprPr = Record.readSubExpr();
12596     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12597     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12598                             /*IsNonContiguous=*/false);
12599   }
12600   C->setComponents(Components, ListSizes);
12601 }
12602 
12603 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12604   C->setLParenLoc(Record.readSourceLocation());
12605   auto NumVars = C->varlist_size();
12606   auto UniqueDecls = C->getUniqueDeclarationsNum();
12607   auto TotalLists = C->getTotalComponentListNum();
12608   auto TotalComponents = C->getTotalComponentsNum();
12609 
12610   SmallVector<Expr *, 16> Vars;
12611   Vars.reserve(NumVars);
12612   for (unsigned i = 0; i != NumVars; ++i)
12613     Vars.push_back(Record.readSubExpr());
12614   C->setVarRefs(Vars);
12615 
12616   SmallVector<ValueDecl *, 16> Decls;
12617   Decls.reserve(UniqueDecls);
12618   for (unsigned i = 0; i < UniqueDecls; ++i)
12619     Decls.push_back(Record.readDeclAs<ValueDecl>());
12620   C->setUniqueDecls(Decls);
12621 
12622   SmallVector<unsigned, 16> ListsPerDecl;
12623   ListsPerDecl.reserve(UniqueDecls);
12624   for (unsigned i = 0; i < UniqueDecls; ++i)
12625     ListsPerDecl.push_back(Record.readInt());
12626   C->setDeclNumLists(ListsPerDecl);
12627 
12628   SmallVector<unsigned, 32> ListSizes;
12629   ListSizes.reserve(TotalLists);
12630   for (unsigned i = 0; i < TotalLists; ++i)
12631     ListSizes.push_back(Record.readInt());
12632   C->setComponentListSizes(ListSizes);
12633 
12634   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12635   Components.reserve(TotalComponents);
12636   for (unsigned i = 0; i < TotalComponents; ++i) {
12637     Expr *AssociatedExpr = Record.readSubExpr();
12638     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12639     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12640                             /*IsNonContiguous*/ false);
12641   }
12642   C->setComponents(Components, ListSizes);
12643 }
12644 
12645 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12646   C->setLParenLoc(Record.readSourceLocation());
12647   auto NumVars = C->varlist_size();
12648   auto UniqueDecls = C->getUniqueDeclarationsNum();
12649   auto TotalLists = C->getTotalComponentListNum();
12650   auto TotalComponents = C->getTotalComponentsNum();
12651 
12652   SmallVector<Expr *, 16> Vars;
12653   Vars.reserve(NumVars);
12654   for (unsigned i = 0; i != NumVars; ++i)
12655     Vars.push_back(Record.readSubExpr());
12656   C->setVarRefs(Vars);
12657   Vars.clear();
12658 
12659   SmallVector<ValueDecl *, 16> Decls;
12660   Decls.reserve(UniqueDecls);
12661   for (unsigned i = 0; i < UniqueDecls; ++i)
12662     Decls.push_back(Record.readDeclAs<ValueDecl>());
12663   C->setUniqueDecls(Decls);
12664 
12665   SmallVector<unsigned, 16> ListsPerDecl;
12666   ListsPerDecl.reserve(UniqueDecls);
12667   for (unsigned i = 0; i < UniqueDecls; ++i)
12668     ListsPerDecl.push_back(Record.readInt());
12669   C->setDeclNumLists(ListsPerDecl);
12670 
12671   SmallVector<unsigned, 32> ListSizes;
12672   ListSizes.reserve(TotalLists);
12673   for (unsigned i = 0; i < TotalLists; ++i)
12674     ListSizes.push_back(Record.readInt());
12675   C->setComponentListSizes(ListSizes);
12676 
12677   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12678   Components.reserve(TotalComponents);
12679   for (unsigned i = 0; i < TotalComponents; ++i) {
12680     Expr *AssociatedExpr = Record.readSubExpr();
12681     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12682     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12683                             /*IsNonContiguous=*/false);
12684   }
12685   C->setComponents(Components, ListSizes);
12686 }
12687 
12688 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12689   C->setLParenLoc(Record.readSourceLocation());
12690   auto NumVars = C->varlist_size();
12691   auto UniqueDecls = C->getUniqueDeclarationsNum();
12692   auto TotalLists = C->getTotalComponentListNum();
12693   auto TotalComponents = C->getTotalComponentsNum();
12694 
12695   SmallVector<Expr *, 16> Vars;
12696   Vars.reserve(NumVars);
12697   for (unsigned I = 0; I != NumVars; ++I)
12698     Vars.push_back(Record.readSubExpr());
12699   C->setVarRefs(Vars);
12700   Vars.clear();
12701 
12702   SmallVector<ValueDecl *, 16> Decls;
12703   Decls.reserve(UniqueDecls);
12704   for (unsigned I = 0; I < UniqueDecls; ++I)
12705     Decls.push_back(Record.readDeclAs<ValueDecl>());
12706   C->setUniqueDecls(Decls);
12707 
12708   SmallVector<unsigned, 16> ListsPerDecl;
12709   ListsPerDecl.reserve(UniqueDecls);
12710   for (unsigned I = 0; I < UniqueDecls; ++I)
12711     ListsPerDecl.push_back(Record.readInt());
12712   C->setDeclNumLists(ListsPerDecl);
12713 
12714   SmallVector<unsigned, 32> ListSizes;
12715   ListSizes.reserve(TotalLists);
12716   for (unsigned i = 0; i < TotalLists; ++i)
12717     ListSizes.push_back(Record.readInt());
12718   C->setComponentListSizes(ListSizes);
12719 
12720   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12721   Components.reserve(TotalComponents);
12722   for (unsigned I = 0; I < TotalComponents; ++I) {
12723     Expr *AssociatedExpr = Record.readSubExpr();
12724     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12725     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12726                             /*IsNonContiguous=*/false);
12727   }
12728   C->setComponents(Components, ListSizes);
12729 }
12730 
12731 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12732   C->setLParenLoc(Record.readSourceLocation());
12733   unsigned NumVars = C->varlist_size();
12734   SmallVector<Expr *, 16> Vars;
12735   Vars.reserve(NumVars);
12736   for (unsigned i = 0; i != NumVars; ++i)
12737     Vars.push_back(Record.readSubExpr());
12738   C->setVarRefs(Vars);
12739   Vars.clear();
12740   Vars.reserve(NumVars);
12741   for (unsigned i = 0; i != NumVars; ++i)
12742     Vars.push_back(Record.readSubExpr());
12743   C->setPrivateRefs(Vars);
12744 }
12745 
12746 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12747   C->setLParenLoc(Record.readSourceLocation());
12748   unsigned NumVars = C->varlist_size();
12749   SmallVector<Expr *, 16> Vars;
12750   Vars.reserve(NumVars);
12751   for (unsigned i = 0; i != NumVars; ++i)
12752     Vars.push_back(Record.readSubExpr());
12753   C->setVarRefs(Vars);
12754 }
12755 
12756 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12757   C->setLParenLoc(Record.readSourceLocation());
12758   unsigned NumVars = C->varlist_size();
12759   SmallVector<Expr *, 16> Vars;
12760   Vars.reserve(NumVars);
12761   for (unsigned i = 0; i != NumVars; ++i)
12762     Vars.push_back(Record.readSubExpr());
12763   C->setVarRefs(Vars);
12764 }
12765 
12766 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12767   C->setLParenLoc(Record.readSourceLocation());
12768   unsigned NumOfAllocators = C->getNumberOfAllocators();
12769   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12770   Data.reserve(NumOfAllocators);
12771   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12772     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12773     D.Allocator = Record.readSubExpr();
12774     D.AllocatorTraits = Record.readSubExpr();
12775     D.LParenLoc = Record.readSourceLocation();
12776     D.RParenLoc = Record.readSourceLocation();
12777   }
12778   C->setAllocatorsData(Data);
12779 }
12780 
12781 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12782   C->setLParenLoc(Record.readSourceLocation());
12783   C->setModifier(Record.readSubExpr());
12784   C->setColonLoc(Record.readSourceLocation());
12785   unsigned NumOfLocators = C->varlist_size();
12786   SmallVector<Expr *, 4> Locators;
12787   Locators.reserve(NumOfLocators);
12788   for (unsigned I = 0; I != NumOfLocators; ++I)
12789     Locators.push_back(Record.readSubExpr());
12790   C->setVarRefs(Locators);
12791 }
12792 
12793 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12794   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12795   C->setLParenLoc(Record.readSourceLocation());
12796   C->setKindKwLoc(Record.readSourceLocation());
12797 }
12798 
12799 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12800   VisitOMPClauseWithPreInit(C);
12801   C->setThreadID(Record.readSubExpr());
12802   C->setLParenLoc(Record.readSourceLocation());
12803 }
12804 
12805 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12806   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12807   C->setLParenLoc(Record.readSourceLocation());
12808   C->setBindKindLoc(Record.readSourceLocation());
12809 }
12810 
12811 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12812   C->setAlignment(Record.readExpr());
12813   C->setLParenLoc(Record.readSourceLocation());
12814 }
12815 
12816 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12817   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12818   TI.Sets.resize(readUInt32());
12819   for (auto &Set : TI.Sets) {
12820     Set.Kind = readEnum<llvm::omp::TraitSet>();
12821     Set.Selectors.resize(readUInt32());
12822     for (auto &Selector : Set.Selectors) {
12823       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12824       Selector.ScoreOrCondition = nullptr;
12825       if (readBool())
12826         Selector.ScoreOrCondition = readExprRef();
12827       Selector.Properties.resize(readUInt32());
12828       for (auto &Property : Selector.Properties)
12829         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12830     }
12831   }
12832   return &TI;
12833 }
12834 
12835 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12836   if (!Data)
12837     return;
12838   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12839     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12840     skipInts(3);
12841   }
12842   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12843   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12844     Clauses[I] = readOMPClause();
12845   Data->setClauses(Clauses);
12846   if (Data->hasAssociatedStmt())
12847     Data->setAssociatedStmt(readStmt());
12848   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12849     Data->getChildren()[I] = readStmt();
12850 }
12851