1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FoldingSet.h"
93 #include "llvm/ADT/Hashing.h"
94 #include "llvm/ADT/IntrusiveRefCntPtr.h"
95 #include "llvm/ADT/None.h"
96 #include "llvm/ADT/Optional.h"
97 #include "llvm/ADT/STLExtras.h"
98 #include "llvm/ADT/ScopeExit.h"
99 #include "llvm/ADT/SmallPtrSet.h"
100 #include "llvm/ADT/SmallString.h"
101 #include "llvm/ADT/SmallVector.h"
102 #include "llvm/ADT/StringExtras.h"
103 #include "llvm/ADT/StringMap.h"
104 #include "llvm/ADT/StringRef.h"
105 #include "llvm/ADT/Triple.h"
106 #include "llvm/ADT/iterator_range.h"
107 #include "llvm/Bitstream/BitstreamReader.h"
108 #include "llvm/Support/Casting.h"
109 #include "llvm/Support/Compiler.h"
110 #include "llvm/Support/Compression.h"
111 #include "llvm/Support/DJB.h"
112 #include "llvm/Support/Endian.h"
113 #include "llvm/Support/Error.h"
114 #include "llvm/Support/ErrorHandling.h"
115 #include "llvm/Support/FileSystem.h"
116 #include "llvm/Support/MemoryBuffer.h"
117 #include "llvm/Support/Path.h"
118 #include "llvm/Support/SaveAndRestore.h"
119 #include "llvm/Support/Timer.h"
120 #include "llvm/Support/VersionTuple.h"
121 #include "llvm/Support/raw_ostream.h"
122 #include <algorithm>
123 #include <cassert>
124 #include <cstddef>
125 #include <cstdint>
126 #include <cstdio>
127 #include <ctime>
128 #include <iterator>
129 #include <limits>
130 #include <map>
131 #include <memory>
132 #include <string>
133 #include <system_error>
134 #include <tuple>
135 #include <utility>
136 #include <vector>
137 
138 using namespace clang;
139 using namespace clang::serialization;
140 using namespace clang::serialization::reader;
141 using llvm::BitstreamCursor;
142 
143 //===----------------------------------------------------------------------===//
144 // ChainedASTReaderListener implementation
145 //===----------------------------------------------------------------------===//
146 
147 bool
148 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
149   return First->ReadFullVersionInformation(FullVersion) ||
150          Second->ReadFullVersionInformation(FullVersion);
151 }
152 
153 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
154   First->ReadModuleName(ModuleName);
155   Second->ReadModuleName(ModuleName);
156 }
157 
158 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
159   First->ReadModuleMapFile(ModuleMapPath);
160   Second->ReadModuleMapFile(ModuleMapPath);
161 }
162 
163 bool
164 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
165                                               bool Complain,
166                                               bool AllowCompatibleDifferences) {
167   return First->ReadLanguageOptions(LangOpts, Complain,
168                                     AllowCompatibleDifferences) ||
169          Second->ReadLanguageOptions(LangOpts, Complain,
170                                      AllowCompatibleDifferences);
171 }
172 
173 bool ChainedASTReaderListener::ReadTargetOptions(
174     const TargetOptions &TargetOpts, bool Complain,
175     bool AllowCompatibleDifferences) {
176   return First->ReadTargetOptions(TargetOpts, Complain,
177                                   AllowCompatibleDifferences) ||
178          Second->ReadTargetOptions(TargetOpts, Complain,
179                                    AllowCompatibleDifferences);
180 }
181 
182 bool ChainedASTReaderListener::ReadDiagnosticOptions(
183     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
184   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
185          Second->ReadDiagnosticOptions(DiagOpts, Complain);
186 }
187 
188 bool
189 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
190                                                 bool Complain) {
191   return First->ReadFileSystemOptions(FSOpts, Complain) ||
192          Second->ReadFileSystemOptions(FSOpts, Complain);
193 }
194 
195 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
196     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
197     bool Complain) {
198   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
199                                         Complain) ||
200          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                          Complain);
202 }
203 
204 bool ChainedASTReaderListener::ReadPreprocessorOptions(
205     const PreprocessorOptions &PPOpts, bool Complain,
206     std::string &SuggestedPredefines) {
207   return First->ReadPreprocessorOptions(PPOpts, Complain,
208                                         SuggestedPredefines) ||
209          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
210 }
211 
212 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
213                                            unsigned Value) {
214   First->ReadCounter(M, Value);
215   Second->ReadCounter(M, Value);
216 }
217 
218 bool ChainedASTReaderListener::needsInputFileVisitation() {
219   return First->needsInputFileVisitation() ||
220          Second->needsInputFileVisitation();
221 }
222 
223 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
224   return First->needsSystemInputFileVisitation() ||
225   Second->needsSystemInputFileVisitation();
226 }
227 
228 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
229                                                ModuleKind Kind) {
230   First->visitModuleFile(Filename, Kind);
231   Second->visitModuleFile(Filename, Kind);
232 }
233 
234 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
235                                               bool isSystem,
236                                               bool isOverridden,
237                                               bool isExplicitModule) {
238   bool Continue = false;
239   if (First->needsInputFileVisitation() &&
240       (!isSystem || First->needsSystemInputFileVisitation()))
241     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
242                                       isExplicitModule);
243   if (Second->needsInputFileVisitation() &&
244       (!isSystem || Second->needsSystemInputFileVisitation()))
245     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
246                                        isExplicitModule);
247   return Continue;
248 }
249 
250 void ChainedASTReaderListener::readModuleFileExtension(
251        const ModuleFileExtensionMetadata &Metadata) {
252   First->readModuleFileExtension(Metadata);
253   Second->readModuleFileExtension(Metadata);
254 }
255 
256 //===----------------------------------------------------------------------===//
257 // PCH validator implementation
258 //===----------------------------------------------------------------------===//
259 
260 ASTReaderListener::~ASTReaderListener() = default;
261 
262 /// Compare the given set of language options against an existing set of
263 /// language options.
264 ///
265 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
266 /// \param AllowCompatibleDifferences If true, differences between compatible
267 ///        language options will be permitted.
268 ///
269 /// \returns true if the languagae options mis-match, false otherwise.
270 static bool checkLanguageOptions(const LangOptions &LangOpts,
271                                  const LangOptions &ExistingLangOpts,
272                                  DiagnosticsEngine *Diags,
273                                  bool AllowCompatibleDifferences = true) {
274 #define LANGOPT(Name, Bits, Default, Description)                 \
275   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
276     if (Diags)                                                    \
277       Diags->Report(diag::err_pch_langopt_mismatch)               \
278         << Description << LangOpts.Name << ExistingLangOpts.Name; \
279     return true;                                                  \
280   }
281 
282 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
283   if (ExistingLangOpts.Name != LangOpts.Name) {           \
284     if (Diags)                                            \
285       Diags->Report(diag::err_pch_langopt_value_mismatch) \
286         << Description;                                   \
287     return true;                                          \
288   }
289 
290 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
291   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
292     if (Diags)                                                 \
293       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
294         << Description;                                        \
295     return true;                                               \
296   }
297 
298 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
299   if (!AllowCompatibleDifferences)                            \
300     LANGOPT(Name, Bits, Default, Description)
301 
302 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
303   if (!AllowCompatibleDifferences)                                 \
304     ENUM_LANGOPT(Name, Bits, Default, Description)
305 
306 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
307   if (!AllowCompatibleDifferences)                                 \
308     VALUE_LANGOPT(Name, Bits, Default, Description)
309 
310 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
311 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
312 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
313 #include "clang/Basic/LangOptions.def"
314 
315   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
316     if (Diags)
317       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
318     return true;
319   }
320 
321   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
322     if (Diags)
323       Diags->Report(diag::err_pch_langopt_value_mismatch)
324       << "target Objective-C runtime";
325     return true;
326   }
327 
328   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
329       LangOpts.CommentOpts.BlockCommandNames) {
330     if (Diags)
331       Diags->Report(diag::err_pch_langopt_value_mismatch)
332         << "block command names";
333     return true;
334   }
335 
336   // Sanitizer feature mismatches are treated as compatible differences. If
337   // compatible differences aren't allowed, we still only want to check for
338   // mismatches of non-modular sanitizers (the only ones which can affect AST
339   // generation).
340   if (!AllowCompatibleDifferences) {
341     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
342     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
343     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
344     ExistingSanitizers.clear(ModularSanitizers);
345     ImportedSanitizers.clear(ModularSanitizers);
346     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
347       const std::string Flag = "-fsanitize=";
348       if (Diags) {
349 #define SANITIZER(NAME, ID)                                                    \
350   {                                                                            \
351     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
352     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
353     if (InExistingModule != InImportedModule)                                  \
354       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
355           << InExistingModule << (Flag + NAME);                                \
356   }
357 #include "clang/Basic/Sanitizers.def"
358       }
359       return true;
360     }
361   }
362 
363   return false;
364 }
365 
366 /// Compare the given set of target options against an existing set of
367 /// target options.
368 ///
369 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
370 ///
371 /// \returns true if the target options mis-match, false otherwise.
372 static bool checkTargetOptions(const TargetOptions &TargetOpts,
373                                const TargetOptions &ExistingTargetOpts,
374                                DiagnosticsEngine *Diags,
375                                bool AllowCompatibleDifferences = true) {
376 #define CHECK_TARGET_OPT(Field, Name)                             \
377   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
378     if (Diags)                                                    \
379       Diags->Report(diag::err_pch_targetopt_mismatch)             \
380         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
381     return true;                                                  \
382   }
383 
384   // The triple and ABI must match exactly.
385   CHECK_TARGET_OPT(Triple, "target");
386   CHECK_TARGET_OPT(ABI, "target ABI");
387 
388   // We can tolerate different CPUs in many cases, notably when one CPU
389   // supports a strict superset of another. When allowing compatible
390   // differences skip this check.
391   if (!AllowCompatibleDifferences)
392     CHECK_TARGET_OPT(CPU, "target CPU");
393 
394 #undef CHECK_TARGET_OPT
395 
396   // Compare feature sets.
397   SmallVector<StringRef, 4> ExistingFeatures(
398                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
399                                              ExistingTargetOpts.FeaturesAsWritten.end());
400   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
401                                          TargetOpts.FeaturesAsWritten.end());
402   llvm::sort(ExistingFeatures);
403   llvm::sort(ReadFeatures);
404 
405   // We compute the set difference in both directions explicitly so that we can
406   // diagnose the differences differently.
407   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
408   std::set_difference(
409       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
410       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
411   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
412                       ExistingFeatures.begin(), ExistingFeatures.end(),
413                       std::back_inserter(UnmatchedReadFeatures));
414 
415   // If we are allowing compatible differences and the read feature set is
416   // a strict subset of the existing feature set, there is nothing to diagnose.
417   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
418     return false;
419 
420   if (Diags) {
421     for (StringRef Feature : UnmatchedReadFeatures)
422       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423           << /* is-existing-feature */ false << Feature;
424     for (StringRef Feature : UnmatchedExistingFeatures)
425       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
426           << /* is-existing-feature */ true << Feature;
427   }
428 
429   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
430 }
431 
432 bool
433 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
434                                   bool Complain,
435                                   bool AllowCompatibleDifferences) {
436   const LangOptions &ExistingLangOpts = PP.getLangOpts();
437   return checkLanguageOptions(LangOpts, ExistingLangOpts,
438                               Complain ? &Reader.Diags : nullptr,
439                               AllowCompatibleDifferences);
440 }
441 
442 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
443                                      bool Complain,
444                                      bool AllowCompatibleDifferences) {
445   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
446   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
447                             Complain ? &Reader.Diags : nullptr,
448                             AllowCompatibleDifferences);
449 }
450 
451 namespace {
452 
453 using MacroDefinitionsMap =
454     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
455 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
456 
457 } // namespace
458 
459 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
460                                          DiagnosticsEngine &Diags,
461                                          bool Complain) {
462   using Level = DiagnosticsEngine::Level;
463 
464   // Check current mappings for new -Werror mappings, and the stored mappings
465   // for cases that were explicitly mapped to *not* be errors that are now
466   // errors because of options like -Werror.
467   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
468 
469   for (DiagnosticsEngine *MappingSource : MappingSources) {
470     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
471       diag::kind DiagID = DiagIDMappingPair.first;
472       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
473       if (CurLevel < DiagnosticsEngine::Error)
474         continue; // not significant
475       Level StoredLevel =
476           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
477       if (StoredLevel < DiagnosticsEngine::Error) {
478         if (Complain)
479           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
480               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
481         return true;
482       }
483     }
484   }
485 
486   return false;
487 }
488 
489 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
490   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
491   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
492     return true;
493   return Ext >= diag::Severity::Error;
494 }
495 
496 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
497                                     DiagnosticsEngine &Diags,
498                                     bool IsSystem, bool Complain) {
499   // Top-level options
500   if (IsSystem) {
501     if (Diags.getSuppressSystemWarnings())
502       return false;
503     // If -Wsystem-headers was not enabled before, be conservative
504     if (StoredDiags.getSuppressSystemWarnings()) {
505       if (Complain)
506         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
507       return true;
508     }
509   }
510 
511   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
512     if (Complain)
513       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
514     return true;
515   }
516 
517   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
518       !StoredDiags.getEnableAllWarnings()) {
519     if (Complain)
520       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
521     return true;
522   }
523 
524   if (isExtHandlingFromDiagsError(Diags) &&
525       !isExtHandlingFromDiagsError(StoredDiags)) {
526     if (Complain)
527       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
528     return true;
529   }
530 
531   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
532 }
533 
534 /// Return the top import module if it is implicit, nullptr otherwise.
535 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
536                                           Preprocessor &PP) {
537   // If the original import came from a file explicitly generated by the user,
538   // don't check the diagnostic mappings.
539   // FIXME: currently this is approximated by checking whether this is not a
540   // module import of an implicitly-loaded module file.
541   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
542   // the transitive closure of its imports, since unrelated modules cannot be
543   // imported until after this module finishes validation.
544   ModuleFile *TopImport = &*ModuleMgr.rbegin();
545   while (!TopImport->ImportedBy.empty())
546     TopImport = TopImport->ImportedBy[0];
547   if (TopImport->Kind != MK_ImplicitModule)
548     return nullptr;
549 
550   StringRef ModuleName = TopImport->ModuleName;
551   assert(!ModuleName.empty() && "diagnostic options read before module name");
552 
553   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
554   assert(M && "missing module");
555   return M;
556 }
557 
558 bool PCHValidator::ReadDiagnosticOptions(
559     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
560   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
561   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
562   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
563       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
564   // This should never fail, because we would have processed these options
565   // before writing them to an ASTFile.
566   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
567 
568   ModuleManager &ModuleMgr = Reader.getModuleManager();
569   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
570 
571   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
572   if (!TopM)
573     return false;
574 
575   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
576   // contains the union of their flags.
577   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
578                                  Complain);
579 }
580 
581 /// Collect the macro definitions provided by the given preprocessor
582 /// options.
583 static void
584 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
585                         MacroDefinitionsMap &Macros,
586                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
587   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
588     StringRef Macro = PPOpts.Macros[I].first;
589     bool IsUndef = PPOpts.Macros[I].second;
590 
591     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
592     StringRef MacroName = MacroPair.first;
593     StringRef MacroBody = MacroPair.second;
594 
595     // For an #undef'd macro, we only care about the name.
596     if (IsUndef) {
597       if (MacroNames && !Macros.count(MacroName))
598         MacroNames->push_back(MacroName);
599 
600       Macros[MacroName] = std::make_pair("", true);
601       continue;
602     }
603 
604     // For a #define'd macro, figure out the actual definition.
605     if (MacroName.size() == Macro.size())
606       MacroBody = "1";
607     else {
608       // Note: GCC drops anything following an end-of-line character.
609       StringRef::size_type End = MacroBody.find_first_of("\n\r");
610       MacroBody = MacroBody.substr(0, End);
611     }
612 
613     if (MacroNames && !Macros.count(MacroName))
614       MacroNames->push_back(MacroName);
615     Macros[MacroName] = std::make_pair(MacroBody, false);
616   }
617 }
618 
619 /// Check the preprocessor options deserialized from the control block
620 /// against the preprocessor options in an existing preprocessor.
621 ///
622 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
623 /// \param Validate If true, validate preprocessor options. If false, allow
624 ///        macros defined by \p ExistingPPOpts to override those defined by
625 ///        \p PPOpts in SuggestedPredefines.
626 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
627                                      const PreprocessorOptions &ExistingPPOpts,
628                                      DiagnosticsEngine *Diags,
629                                      FileManager &FileMgr,
630                                      std::string &SuggestedPredefines,
631                                      const LangOptions &LangOpts,
632                                      bool Validate = true) {
633   // Check macro definitions.
634   MacroDefinitionsMap ASTFileMacros;
635   collectMacroDefinitions(PPOpts, ASTFileMacros);
636   MacroDefinitionsMap ExistingMacros;
637   SmallVector<StringRef, 4> ExistingMacroNames;
638   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
639 
640   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
641     // Dig out the macro definition in the existing preprocessor options.
642     StringRef MacroName = ExistingMacroNames[I];
643     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
644 
645     // Check whether we know anything about this macro name or not.
646     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
647         ASTFileMacros.find(MacroName);
648     if (!Validate || Known == ASTFileMacros.end()) {
649       // FIXME: Check whether this identifier was referenced anywhere in the
650       // AST file. If so, we should reject the AST file. Unfortunately, this
651       // information isn't in the control block. What shall we do about it?
652 
653       if (Existing.second) {
654         SuggestedPredefines += "#undef ";
655         SuggestedPredefines += MacroName.str();
656         SuggestedPredefines += '\n';
657       } else {
658         SuggestedPredefines += "#define ";
659         SuggestedPredefines += MacroName.str();
660         SuggestedPredefines += ' ';
661         SuggestedPredefines += Existing.first.str();
662         SuggestedPredefines += '\n';
663       }
664       continue;
665     }
666 
667     // If the macro was defined in one but undef'd in the other, we have a
668     // conflict.
669     if (Existing.second != Known->second.second) {
670       if (Diags) {
671         Diags->Report(diag::err_pch_macro_def_undef)
672           << MacroName << Known->second.second;
673       }
674       return true;
675     }
676 
677     // If the macro was #undef'd in both, or if the macro bodies are identical,
678     // it's fine.
679     if (Existing.second || Existing.first == Known->second.first)
680       continue;
681 
682     // The macro bodies differ; complain.
683     if (Diags) {
684       Diags->Report(diag::err_pch_macro_def_conflict)
685         << MacroName << Known->second.first << Existing.first;
686     }
687     return true;
688   }
689 
690   // Check whether we're using predefines.
691   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
692     if (Diags) {
693       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
694     }
695     return true;
696   }
697 
698   // Detailed record is important since it is used for the module cache hash.
699   if (LangOpts.Modules &&
700       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
701     if (Diags) {
702       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
703     }
704     return true;
705   }
706 
707   // Compute the #include and #include_macros lines we need.
708   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
709     StringRef File = ExistingPPOpts.Includes[I];
710 
711     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
712         !ExistingPPOpts.PCHThroughHeader.empty()) {
713       // In case the through header is an include, we must add all the includes
714       // to the predefines so the start point can be determined.
715       SuggestedPredefines += "#include \"";
716       SuggestedPredefines += File;
717       SuggestedPredefines += "\"\n";
718       continue;
719     }
720 
721     if (File == ExistingPPOpts.ImplicitPCHInclude)
722       continue;
723 
724     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
725           != PPOpts.Includes.end())
726       continue;
727 
728     SuggestedPredefines += "#include \"";
729     SuggestedPredefines += File;
730     SuggestedPredefines += "\"\n";
731   }
732 
733   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
734     StringRef File = ExistingPPOpts.MacroIncludes[I];
735     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
736                   File)
737         != PPOpts.MacroIncludes.end())
738       continue;
739 
740     SuggestedPredefines += "#__include_macros \"";
741     SuggestedPredefines += File;
742     SuggestedPredefines += "\"\n##\n";
743   }
744 
745   return false;
746 }
747 
748 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
749                                            bool Complain,
750                                            std::string &SuggestedPredefines) {
751   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
752 
753   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
754                                   Complain? &Reader.Diags : nullptr,
755                                   PP.getFileManager(),
756                                   SuggestedPredefines,
757                                   PP.getLangOpts());
758 }
759 
760 bool SimpleASTReaderListener::ReadPreprocessorOptions(
761                                   const PreprocessorOptions &PPOpts,
762                                   bool Complain,
763                                   std::string &SuggestedPredefines) {
764   return checkPreprocessorOptions(PPOpts,
765                                   PP.getPreprocessorOpts(),
766                                   nullptr,
767                                   PP.getFileManager(),
768                                   SuggestedPredefines,
769                                   PP.getLangOpts(),
770                                   false);
771 }
772 
773 /// Check the header search options deserialized from the control block
774 /// against the header search options in an existing preprocessor.
775 ///
776 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
777 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
778                                      StringRef SpecificModuleCachePath,
779                                      StringRef ExistingModuleCachePath,
780                                      DiagnosticsEngine *Diags,
781                                      const LangOptions &LangOpts) {
782   if (LangOpts.Modules) {
783     if (SpecificModuleCachePath != ExistingModuleCachePath) {
784       if (Diags)
785         Diags->Report(diag::err_pch_modulecache_mismatch)
786           << SpecificModuleCachePath << ExistingModuleCachePath;
787       return true;
788     }
789   }
790 
791   return false;
792 }
793 
794 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
795                                            StringRef SpecificModuleCachePath,
796                                            bool Complain) {
797   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
798                                   PP.getHeaderSearchInfo().getModuleCachePath(),
799                                   Complain ? &Reader.Diags : nullptr,
800                                   PP.getLangOpts());
801 }
802 
803 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
804   PP.setCounterValue(Value);
805 }
806 
807 //===----------------------------------------------------------------------===//
808 // AST reader implementation
809 //===----------------------------------------------------------------------===//
810 
811 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
812                                            bool TakeOwnership) {
813   DeserializationListener = Listener;
814   OwnsDeserializationListener = TakeOwnership;
815 }
816 
817 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
818   return serialization::ComputeHash(Sel);
819 }
820 
821 std::pair<unsigned, unsigned>
822 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
823   using namespace llvm::support;
824 
825   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
826   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
827   return std::make_pair(KeyLen, DataLen);
828 }
829 
830 ASTSelectorLookupTrait::internal_key_type
831 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
832   using namespace llvm::support;
833 
834   SelectorTable &SelTable = Reader.getContext().Selectors;
835   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
836   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
837       F, endian::readNext<uint32_t, little, unaligned>(d));
838   if (N == 0)
839     return SelTable.getNullarySelector(FirstII);
840   else if (N == 1)
841     return SelTable.getUnarySelector(FirstII);
842 
843   SmallVector<IdentifierInfo *, 16> Args;
844   Args.push_back(FirstII);
845   for (unsigned I = 1; I != N; ++I)
846     Args.push_back(Reader.getLocalIdentifier(
847         F, endian::readNext<uint32_t, little, unaligned>(d)));
848 
849   return SelTable.getSelector(N, Args.data());
850 }
851 
852 ASTSelectorLookupTrait::data_type
853 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
854                                  unsigned DataLen) {
855   using namespace llvm::support;
856 
857   data_type Result;
858 
859   Result.ID = Reader.getGlobalSelectorID(
860       F, endian::readNext<uint32_t, little, unaligned>(d));
861   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
862   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
863   Result.InstanceBits = FullInstanceBits & 0x3;
864   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
865   Result.FactoryBits = FullFactoryBits & 0x3;
866   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
867   unsigned NumInstanceMethods = FullInstanceBits >> 3;
868   unsigned NumFactoryMethods = FullFactoryBits >> 3;
869 
870   // Load instance methods
871   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
872     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
873             F, endian::readNext<uint32_t, little, unaligned>(d)))
874       Result.Instance.push_back(Method);
875   }
876 
877   // Load factory methods
878   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
879     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
880             F, endian::readNext<uint32_t, little, unaligned>(d)))
881       Result.Factory.push_back(Method);
882   }
883 
884   return Result;
885 }
886 
887 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
888   return llvm::djbHash(a);
889 }
890 
891 std::pair<unsigned, unsigned>
892 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
893   using namespace llvm::support;
894 
895   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
896   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
897   return std::make_pair(KeyLen, DataLen);
898 }
899 
900 ASTIdentifierLookupTraitBase::internal_key_type
901 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
902   assert(n >= 2 && d[n-1] == '\0');
903   return StringRef((const char*) d, n-1);
904 }
905 
906 /// Whether the given identifier is "interesting".
907 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
908                                     bool IsModule) {
909   return II.hadMacroDefinition() ||
910          II.isPoisoned() ||
911          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
912          II.hasRevertedTokenIDToIdentifier() ||
913          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
914           II.getFETokenInfo());
915 }
916 
917 static bool readBit(unsigned &Bits) {
918   bool Value = Bits & 0x1;
919   Bits >>= 1;
920   return Value;
921 }
922 
923 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
924   using namespace llvm::support;
925 
926   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
927   return Reader.getGlobalIdentifierID(F, RawID >> 1);
928 }
929 
930 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
931   if (!II.isFromAST()) {
932     II.setIsFromAST();
933     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
934     if (isInterestingIdentifier(Reader, II, IsModule))
935       II.setChangedSinceDeserialization();
936   }
937 }
938 
939 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
940                                                    const unsigned char* d,
941                                                    unsigned DataLen) {
942   using namespace llvm::support;
943 
944   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
945   bool IsInteresting = RawID & 0x01;
946 
947   // Wipe out the "is interesting" bit.
948   RawID = RawID >> 1;
949 
950   // Build the IdentifierInfo and link the identifier ID with it.
951   IdentifierInfo *II = KnownII;
952   if (!II) {
953     II = &Reader.getIdentifierTable().getOwn(k);
954     KnownII = II;
955   }
956   markIdentifierFromAST(Reader, *II);
957   Reader.markIdentifierUpToDate(II);
958 
959   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
960   if (!IsInteresting) {
961     // For uninteresting identifiers, there's nothing else to do. Just notify
962     // the reader that we've finished loading this identifier.
963     Reader.SetIdentifierInfo(ID, II);
964     return II;
965   }
966 
967   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
968   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
969   bool CPlusPlusOperatorKeyword = readBit(Bits);
970   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
971   bool HasRevertedBuiltin = readBit(Bits);
972   bool Poisoned = readBit(Bits);
973   bool ExtensionToken = readBit(Bits);
974   bool HadMacroDefinition = readBit(Bits);
975 
976   assert(Bits == 0 && "Extra bits in the identifier?");
977   DataLen -= 8;
978 
979   // Set or check the various bits in the IdentifierInfo structure.
980   // Token IDs are read-only.
981   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
982     II->revertTokenIDToIdentifier();
983   if (!F.isModule())
984     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
985   else if (HasRevertedBuiltin && II->getBuiltinID()) {
986     II->revertBuiltin();
987     assert((II->hasRevertedBuiltin() ||
988             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
989            "Incorrect ObjC keyword or builtin ID");
990   }
991   assert(II->isExtensionToken() == ExtensionToken &&
992          "Incorrect extension token flag");
993   (void)ExtensionToken;
994   if (Poisoned)
995     II->setIsPoisoned(true);
996   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
997          "Incorrect C++ operator keyword flag");
998   (void)CPlusPlusOperatorKeyword;
999 
1000   // If this identifier is a macro, deserialize the macro
1001   // definition.
1002   if (HadMacroDefinition) {
1003     uint32_t MacroDirectivesOffset =
1004         endian::readNext<uint32_t, little, unaligned>(d);
1005     DataLen -= 4;
1006 
1007     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1008   }
1009 
1010   Reader.SetIdentifierInfo(ID, II);
1011 
1012   // Read all of the declarations visible at global scope with this
1013   // name.
1014   if (DataLen > 0) {
1015     SmallVector<uint32_t, 4> DeclIDs;
1016     for (; DataLen > 0; DataLen -= 4)
1017       DeclIDs.push_back(Reader.getGlobalDeclID(
1018           F, endian::readNext<uint32_t, little, unaligned>(d)));
1019     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1020   }
1021 
1022   return II;
1023 }
1024 
1025 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1026     : Kind(Name.getNameKind()) {
1027   switch (Kind) {
1028   case DeclarationName::Identifier:
1029     Data = (uint64_t)Name.getAsIdentifierInfo();
1030     break;
1031   case DeclarationName::ObjCZeroArgSelector:
1032   case DeclarationName::ObjCOneArgSelector:
1033   case DeclarationName::ObjCMultiArgSelector:
1034     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1035     break;
1036   case DeclarationName::CXXOperatorName:
1037     Data = Name.getCXXOverloadedOperator();
1038     break;
1039   case DeclarationName::CXXLiteralOperatorName:
1040     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1041     break;
1042   case DeclarationName::CXXDeductionGuideName:
1043     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1044                ->getDeclName().getAsIdentifierInfo();
1045     break;
1046   case DeclarationName::CXXConstructorName:
1047   case DeclarationName::CXXDestructorName:
1048   case DeclarationName::CXXConversionFunctionName:
1049   case DeclarationName::CXXUsingDirective:
1050     Data = 0;
1051     break;
1052   }
1053 }
1054 
1055 unsigned DeclarationNameKey::getHash() const {
1056   llvm::FoldingSetNodeID ID;
1057   ID.AddInteger(Kind);
1058 
1059   switch (Kind) {
1060   case DeclarationName::Identifier:
1061   case DeclarationName::CXXLiteralOperatorName:
1062   case DeclarationName::CXXDeductionGuideName:
1063     ID.AddString(((IdentifierInfo*)Data)->getName());
1064     break;
1065   case DeclarationName::ObjCZeroArgSelector:
1066   case DeclarationName::ObjCOneArgSelector:
1067   case DeclarationName::ObjCMultiArgSelector:
1068     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1069     break;
1070   case DeclarationName::CXXOperatorName:
1071     ID.AddInteger((OverloadedOperatorKind)Data);
1072     break;
1073   case DeclarationName::CXXConstructorName:
1074   case DeclarationName::CXXDestructorName:
1075   case DeclarationName::CXXConversionFunctionName:
1076   case DeclarationName::CXXUsingDirective:
1077     break;
1078   }
1079 
1080   return ID.ComputeHash();
1081 }
1082 
1083 ModuleFile *
1084 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1085   using namespace llvm::support;
1086 
1087   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1088   return Reader.getLocalModuleFile(F, ModuleFileID);
1089 }
1090 
1091 std::pair<unsigned, unsigned>
1092 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1093   using namespace llvm::support;
1094 
1095   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1096   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1097   return std::make_pair(KeyLen, DataLen);
1098 }
1099 
1100 ASTDeclContextNameLookupTrait::internal_key_type
1101 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1102   using namespace llvm::support;
1103 
1104   auto Kind = (DeclarationName::NameKind)*d++;
1105   uint64_t Data;
1106   switch (Kind) {
1107   case DeclarationName::Identifier:
1108   case DeclarationName::CXXLiteralOperatorName:
1109   case DeclarationName::CXXDeductionGuideName:
1110     Data = (uint64_t)Reader.getLocalIdentifier(
1111         F, endian::readNext<uint32_t, little, unaligned>(d));
1112     break;
1113   case DeclarationName::ObjCZeroArgSelector:
1114   case DeclarationName::ObjCOneArgSelector:
1115   case DeclarationName::ObjCMultiArgSelector:
1116     Data =
1117         (uint64_t)Reader.getLocalSelector(
1118                              F, endian::readNext<uint32_t, little, unaligned>(
1119                                     d)).getAsOpaquePtr();
1120     break;
1121   case DeclarationName::CXXOperatorName:
1122     Data = *d++; // OverloadedOperatorKind
1123     break;
1124   case DeclarationName::CXXConstructorName:
1125   case DeclarationName::CXXDestructorName:
1126   case DeclarationName::CXXConversionFunctionName:
1127   case DeclarationName::CXXUsingDirective:
1128     Data = 0;
1129     break;
1130   }
1131 
1132   return DeclarationNameKey(Kind, Data);
1133 }
1134 
1135 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1136                                                  const unsigned char *d,
1137                                                  unsigned DataLen,
1138                                                  data_type_builder &Val) {
1139   using namespace llvm::support;
1140 
1141   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1142     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1143     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1144   }
1145 }
1146 
1147 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1148                                               BitstreamCursor &Cursor,
1149                                               uint64_t Offset,
1150                                               DeclContext *DC) {
1151   assert(Offset != 0);
1152 
1153   SavedStreamPosition SavedPosition(Cursor);
1154   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1155     Error(std::move(Err));
1156     return true;
1157   }
1158 
1159   RecordData Record;
1160   StringRef Blob;
1161   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1162   if (!MaybeCode) {
1163     Error(MaybeCode.takeError());
1164     return true;
1165   }
1166   unsigned Code = MaybeCode.get();
1167 
1168   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1169   if (!MaybeRecCode) {
1170     Error(MaybeRecCode.takeError());
1171     return true;
1172   }
1173   unsigned RecCode = MaybeRecCode.get();
1174   if (RecCode != DECL_CONTEXT_LEXICAL) {
1175     Error("Expected lexical block");
1176     return true;
1177   }
1178 
1179   assert(!isa<TranslationUnitDecl>(DC) &&
1180          "expected a TU_UPDATE_LEXICAL record for TU");
1181   // If we are handling a C++ class template instantiation, we can see multiple
1182   // lexical updates for the same record. It's important that we select only one
1183   // of them, so that field numbering works properly. Just pick the first one we
1184   // see.
1185   auto &Lex = LexicalDecls[DC];
1186   if (!Lex.first) {
1187     Lex = std::make_pair(
1188         &M, llvm::makeArrayRef(
1189                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1190                     Blob.data()),
1191                 Blob.size() / 4));
1192   }
1193   DC->setHasExternalLexicalStorage(true);
1194   return false;
1195 }
1196 
1197 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1198                                               BitstreamCursor &Cursor,
1199                                               uint64_t Offset,
1200                                               DeclID ID) {
1201   assert(Offset != 0);
1202 
1203   SavedStreamPosition SavedPosition(Cursor);
1204   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1205     Error(std::move(Err));
1206     return true;
1207   }
1208 
1209   RecordData Record;
1210   StringRef Blob;
1211   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1212   if (!MaybeCode) {
1213     Error(MaybeCode.takeError());
1214     return true;
1215   }
1216   unsigned Code = MaybeCode.get();
1217 
1218   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1219   if (!MaybeRecCode) {
1220     Error(MaybeRecCode.takeError());
1221     return true;
1222   }
1223   unsigned RecCode = MaybeRecCode.get();
1224   if (RecCode != DECL_CONTEXT_VISIBLE) {
1225     Error("Expected visible lookup table block");
1226     return true;
1227   }
1228 
1229   // We can't safely determine the primary context yet, so delay attaching the
1230   // lookup table until we're done with recursive deserialization.
1231   auto *Data = (const unsigned char*)Blob.data();
1232   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1233   return false;
1234 }
1235 
1236 void ASTReader::Error(StringRef Msg) const {
1237   Error(diag::err_fe_pch_malformed, Msg);
1238   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1239       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1240     Diag(diag::note_module_cache_path)
1241       << PP.getHeaderSearchInfo().getModuleCachePath();
1242   }
1243 }
1244 
1245 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1246                       StringRef Arg3) const {
1247   if (Diags.isDiagnosticInFlight())
1248     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1249   else
1250     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1251 }
1252 
1253 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1254                       unsigned Select) const {
1255   if (!Diags.isDiagnosticInFlight())
1256     Diag(DiagID) << Arg1 << Arg2 << Select;
1257 }
1258 
1259 void ASTReader::Error(llvm::Error &&Err) const {
1260   Error(toString(std::move(Err)));
1261 }
1262 
1263 //===----------------------------------------------------------------------===//
1264 // Source Manager Deserialization
1265 //===----------------------------------------------------------------------===//
1266 
1267 /// Read the line table in the source manager block.
1268 /// \returns true if there was an error.
1269 bool ASTReader::ParseLineTable(ModuleFile &F,
1270                                const RecordData &Record) {
1271   unsigned Idx = 0;
1272   LineTableInfo &LineTable = SourceMgr.getLineTable();
1273 
1274   // Parse the file names
1275   std::map<int, int> FileIDs;
1276   FileIDs[-1] = -1; // For unspecified filenames.
1277   for (unsigned I = 0; Record[Idx]; ++I) {
1278     // Extract the file name
1279     auto Filename = ReadPath(F, Record, Idx);
1280     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1281   }
1282   ++Idx;
1283 
1284   // Parse the line entries
1285   std::vector<LineEntry> Entries;
1286   while (Idx < Record.size()) {
1287     int FID = Record[Idx++];
1288     assert(FID >= 0 && "Serialized line entries for non-local file.");
1289     // Remap FileID from 1-based old view.
1290     FID += F.SLocEntryBaseID - 1;
1291 
1292     // Extract the line entries
1293     unsigned NumEntries = Record[Idx++];
1294     assert(NumEntries && "no line entries for file ID");
1295     Entries.clear();
1296     Entries.reserve(NumEntries);
1297     for (unsigned I = 0; I != NumEntries; ++I) {
1298       unsigned FileOffset = Record[Idx++];
1299       unsigned LineNo = Record[Idx++];
1300       int FilenameID = FileIDs[Record[Idx++]];
1301       SrcMgr::CharacteristicKind FileKind
1302         = (SrcMgr::CharacteristicKind)Record[Idx++];
1303       unsigned IncludeOffset = Record[Idx++];
1304       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1305                                        FileKind, IncludeOffset));
1306     }
1307     LineTable.AddEntry(FileID::get(FID), Entries);
1308   }
1309 
1310   return false;
1311 }
1312 
1313 /// Read a source manager block
1314 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1315   using namespace SrcMgr;
1316 
1317   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1318 
1319   // Set the source-location entry cursor to the current position in
1320   // the stream. This cursor will be used to read the contents of the
1321   // source manager block initially, and then lazily read
1322   // source-location entries as needed.
1323   SLocEntryCursor = F.Stream;
1324 
1325   // The stream itself is going to skip over the source manager block.
1326   if (llvm::Error Err = F.Stream.SkipBlock()) {
1327     Error(std::move(Err));
1328     return true;
1329   }
1330 
1331   // Enter the source manager block.
1332   if (llvm::Error Err =
1333           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1334     Error(std::move(Err));
1335     return true;
1336   }
1337 
1338   RecordData Record;
1339   while (true) {
1340     Expected<llvm::BitstreamEntry> MaybeE =
1341         SLocEntryCursor.advanceSkippingSubblocks();
1342     if (!MaybeE) {
1343       Error(MaybeE.takeError());
1344       return true;
1345     }
1346     llvm::BitstreamEntry E = MaybeE.get();
1347 
1348     switch (E.Kind) {
1349     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1350     case llvm::BitstreamEntry::Error:
1351       Error("malformed block record in AST file");
1352       return true;
1353     case llvm::BitstreamEntry::EndBlock:
1354       return false;
1355     case llvm::BitstreamEntry::Record:
1356       // The interesting case.
1357       break;
1358     }
1359 
1360     // Read a record.
1361     Record.clear();
1362     StringRef Blob;
1363     Expected<unsigned> MaybeRecord =
1364         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1365     if (!MaybeRecord) {
1366       Error(MaybeRecord.takeError());
1367       return true;
1368     }
1369     switch (MaybeRecord.get()) {
1370     default:  // Default behavior: ignore.
1371       break;
1372 
1373     case SM_SLOC_FILE_ENTRY:
1374     case SM_SLOC_BUFFER_ENTRY:
1375     case SM_SLOC_EXPANSION_ENTRY:
1376       // Once we hit one of the source location entries, we're done.
1377       return false;
1378     }
1379   }
1380 }
1381 
1382 /// If a header file is not found at the path that we expect it to be
1383 /// and the PCH file was moved from its original location, try to resolve the
1384 /// file by assuming that header+PCH were moved together and the header is in
1385 /// the same place relative to the PCH.
1386 static std::string
1387 resolveFileRelativeToOriginalDir(const std::string &Filename,
1388                                  const std::string &OriginalDir,
1389                                  const std::string &CurrDir) {
1390   assert(OriginalDir != CurrDir &&
1391          "No point trying to resolve the file if the PCH dir didn't change");
1392 
1393   using namespace llvm::sys;
1394 
1395   SmallString<128> filePath(Filename);
1396   fs::make_absolute(filePath);
1397   assert(path::is_absolute(OriginalDir));
1398   SmallString<128> currPCHPath(CurrDir);
1399 
1400   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1401                        fileDirE = path::end(path::parent_path(filePath));
1402   path::const_iterator origDirI = path::begin(OriginalDir),
1403                        origDirE = path::end(OriginalDir);
1404   // Skip the common path components from filePath and OriginalDir.
1405   while (fileDirI != fileDirE && origDirI != origDirE &&
1406          *fileDirI == *origDirI) {
1407     ++fileDirI;
1408     ++origDirI;
1409   }
1410   for (; origDirI != origDirE; ++origDirI)
1411     path::append(currPCHPath, "..");
1412   path::append(currPCHPath, fileDirI, fileDirE);
1413   path::append(currPCHPath, path::filename(Filename));
1414   return std::string(currPCHPath.str());
1415 }
1416 
1417 bool ASTReader::ReadSLocEntry(int ID) {
1418   if (ID == 0)
1419     return false;
1420 
1421   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1422     Error("source location entry ID out-of-range for AST file");
1423     return true;
1424   }
1425 
1426   // Local helper to read the (possibly-compressed) buffer data following the
1427   // entry record.
1428   auto ReadBuffer = [this](
1429       BitstreamCursor &SLocEntryCursor,
1430       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1431     RecordData Record;
1432     StringRef Blob;
1433     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1434     if (!MaybeCode) {
1435       Error(MaybeCode.takeError());
1436       return nullptr;
1437     }
1438     unsigned Code = MaybeCode.get();
1439 
1440     Expected<unsigned> MaybeRecCode =
1441         SLocEntryCursor.readRecord(Code, Record, &Blob);
1442     if (!MaybeRecCode) {
1443       Error(MaybeRecCode.takeError());
1444       return nullptr;
1445     }
1446     unsigned RecCode = MaybeRecCode.get();
1447 
1448     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1449       if (!llvm::zlib::isAvailable()) {
1450         Error("zlib is not available");
1451         return nullptr;
1452       }
1453       SmallString<0> Uncompressed;
1454       if (llvm::Error E =
1455               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1456         Error("could not decompress embedded file contents: " +
1457               llvm::toString(std::move(E)));
1458         return nullptr;
1459       }
1460       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1461     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1462       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1463     } else {
1464       Error("AST record has invalid code");
1465       return nullptr;
1466     }
1467   };
1468 
1469   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1470   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1471           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1472     Error(std::move(Err));
1473     return true;
1474   }
1475 
1476   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1477   unsigned BaseOffset = F->SLocEntryBaseOffset;
1478 
1479   ++NumSLocEntriesRead;
1480   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1481   if (!MaybeEntry) {
1482     Error(MaybeEntry.takeError());
1483     return true;
1484   }
1485   llvm::BitstreamEntry Entry = MaybeEntry.get();
1486 
1487   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1488     Error("incorrectly-formatted source location entry in AST file");
1489     return true;
1490   }
1491 
1492   RecordData Record;
1493   StringRef Blob;
1494   Expected<unsigned> MaybeSLOC =
1495       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1496   if (!MaybeSLOC) {
1497     Error(MaybeSLOC.takeError());
1498     return true;
1499   }
1500   switch (MaybeSLOC.get()) {
1501   default:
1502     Error("incorrectly-formatted source location entry in AST file");
1503     return true;
1504 
1505   case SM_SLOC_FILE_ENTRY: {
1506     // We will detect whether a file changed and return 'Failure' for it, but
1507     // we will also try to fail gracefully by setting up the SLocEntry.
1508     unsigned InputID = Record[4];
1509     InputFile IF = getInputFile(*F, InputID);
1510     const FileEntry *File = IF.getFile();
1511     bool OverriddenBuffer = IF.isOverridden();
1512 
1513     // Note that we only check if a File was returned. If it was out-of-date
1514     // we have complained but we will continue creating a FileID to recover
1515     // gracefully.
1516     if (!File)
1517       return true;
1518 
1519     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1520     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1521       // This is the module's main file.
1522       IncludeLoc = getImportLocation(F);
1523     }
1524     SrcMgr::CharacteristicKind
1525       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1526     // FIXME: The FileID should be created from the FileEntryRef.
1527     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1528                                         ID, BaseOffset + Record[0]);
1529     SrcMgr::FileInfo &FileInfo =
1530           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1531     FileInfo.NumCreatedFIDs = Record[5];
1532     if (Record[3])
1533       FileInfo.setHasLineDirectives();
1534 
1535     unsigned NumFileDecls = Record[7];
1536     if (NumFileDecls && ContextObj) {
1537       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1538       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1539       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1540                                                              NumFileDecls));
1541     }
1542 
1543     const SrcMgr::ContentCache *ContentCache
1544       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1545     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1546         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1547         !ContentCache->getRawBuffer()) {
1548       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1549       if (!Buffer)
1550         return true;
1551       SourceMgr.overrideFileContents(File, std::move(Buffer));
1552     }
1553 
1554     break;
1555   }
1556 
1557   case SM_SLOC_BUFFER_ENTRY: {
1558     const char *Name = Blob.data();
1559     unsigned Offset = Record[0];
1560     SrcMgr::CharacteristicKind
1561       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1562     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1563     if (IncludeLoc.isInvalid() && F->isModule()) {
1564       IncludeLoc = getImportLocation(F);
1565     }
1566 
1567     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1568     if (!Buffer)
1569       return true;
1570     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1571                            BaseOffset + Offset, IncludeLoc);
1572     break;
1573   }
1574 
1575   case SM_SLOC_EXPANSION_ENTRY: {
1576     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1577     SourceMgr.createExpansionLoc(SpellingLoc,
1578                                      ReadSourceLocation(*F, Record[2]),
1579                                      ReadSourceLocation(*F, Record[3]),
1580                                      Record[5],
1581                                      Record[4],
1582                                      ID,
1583                                      BaseOffset + Record[0]);
1584     break;
1585   }
1586   }
1587 
1588   return false;
1589 }
1590 
1591 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1592   if (ID == 0)
1593     return std::make_pair(SourceLocation(), "");
1594 
1595   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1596     Error("source location entry ID out-of-range for AST file");
1597     return std::make_pair(SourceLocation(), "");
1598   }
1599 
1600   // Find which module file this entry lands in.
1601   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1602   if (!M->isModule())
1603     return std::make_pair(SourceLocation(), "");
1604 
1605   // FIXME: Can we map this down to a particular submodule? That would be
1606   // ideal.
1607   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1608 }
1609 
1610 /// Find the location where the module F is imported.
1611 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1612   if (F->ImportLoc.isValid())
1613     return F->ImportLoc;
1614 
1615   // Otherwise we have a PCH. It's considered to be "imported" at the first
1616   // location of its includer.
1617   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1618     // Main file is the importer.
1619     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1620     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1621   }
1622   return F->ImportedBy[0]->FirstLoc;
1623 }
1624 
1625 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1626 /// the abbreviations that are at the top of the block and then leave the cursor
1627 /// pointing into the block.
1628 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1629   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1630     // FIXME this drops errors on the floor.
1631     consumeError(std::move(Err));
1632     return true;
1633   }
1634 
1635   while (true) {
1636     uint64_t Offset = Cursor.GetCurrentBitNo();
1637     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1638     if (!MaybeCode) {
1639       // FIXME this drops errors on the floor.
1640       consumeError(MaybeCode.takeError());
1641       return true;
1642     }
1643     unsigned Code = MaybeCode.get();
1644 
1645     // We expect all abbrevs to be at the start of the block.
1646     if (Code != llvm::bitc::DEFINE_ABBREV) {
1647       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1648         // FIXME this drops errors on the floor.
1649         consumeError(std::move(Err));
1650         return true;
1651       }
1652       return false;
1653     }
1654     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1655       // FIXME this drops errors on the floor.
1656       consumeError(std::move(Err));
1657       return true;
1658     }
1659   }
1660 }
1661 
1662 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1663                            unsigned &Idx) {
1664   Token Tok;
1665   Tok.startToken();
1666   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1667   Tok.setLength(Record[Idx++]);
1668   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1669     Tok.setIdentifierInfo(II);
1670   Tok.setKind((tok::TokenKind)Record[Idx++]);
1671   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1672   return Tok;
1673 }
1674 
1675 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1676   BitstreamCursor &Stream = F.MacroCursor;
1677 
1678   // Keep track of where we are in the stream, then jump back there
1679   // after reading this macro.
1680   SavedStreamPosition SavedPosition(Stream);
1681 
1682   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1683     // FIXME this drops errors on the floor.
1684     consumeError(std::move(Err));
1685     return nullptr;
1686   }
1687   RecordData Record;
1688   SmallVector<IdentifierInfo*, 16> MacroParams;
1689   MacroInfo *Macro = nullptr;
1690 
1691   while (true) {
1692     // Advance to the next record, but if we get to the end of the block, don't
1693     // pop it (removing all the abbreviations from the cursor) since we want to
1694     // be able to reseek within the block and read entries.
1695     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1696     Expected<llvm::BitstreamEntry> MaybeEntry =
1697         Stream.advanceSkippingSubblocks(Flags);
1698     if (!MaybeEntry) {
1699       Error(MaybeEntry.takeError());
1700       return Macro;
1701     }
1702     llvm::BitstreamEntry Entry = MaybeEntry.get();
1703 
1704     switch (Entry.Kind) {
1705     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1706     case llvm::BitstreamEntry::Error:
1707       Error("malformed block record in AST file");
1708       return Macro;
1709     case llvm::BitstreamEntry::EndBlock:
1710       return Macro;
1711     case llvm::BitstreamEntry::Record:
1712       // The interesting case.
1713       break;
1714     }
1715 
1716     // Read a record.
1717     Record.clear();
1718     PreprocessorRecordTypes RecType;
1719     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1720       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1721     else {
1722       Error(MaybeRecType.takeError());
1723       return Macro;
1724     }
1725     switch (RecType) {
1726     case PP_MODULE_MACRO:
1727     case PP_MACRO_DIRECTIVE_HISTORY:
1728       return Macro;
1729 
1730     case PP_MACRO_OBJECT_LIKE:
1731     case PP_MACRO_FUNCTION_LIKE: {
1732       // If we already have a macro, that means that we've hit the end
1733       // of the definition of the macro we were looking for. We're
1734       // done.
1735       if (Macro)
1736         return Macro;
1737 
1738       unsigned NextIndex = 1; // Skip identifier ID.
1739       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1740       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1741       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1742       MI->setIsUsed(Record[NextIndex++]);
1743       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1744 
1745       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1746         // Decode function-like macro info.
1747         bool isC99VarArgs = Record[NextIndex++];
1748         bool isGNUVarArgs = Record[NextIndex++];
1749         bool hasCommaPasting = Record[NextIndex++];
1750         MacroParams.clear();
1751         unsigned NumArgs = Record[NextIndex++];
1752         for (unsigned i = 0; i != NumArgs; ++i)
1753           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1754 
1755         // Install function-like macro info.
1756         MI->setIsFunctionLike();
1757         if (isC99VarArgs) MI->setIsC99Varargs();
1758         if (isGNUVarArgs) MI->setIsGNUVarargs();
1759         if (hasCommaPasting) MI->setHasCommaPasting();
1760         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1761       }
1762 
1763       // Remember that we saw this macro last so that we add the tokens that
1764       // form its body to it.
1765       Macro = MI;
1766 
1767       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1768           Record[NextIndex]) {
1769         // We have a macro definition. Register the association
1770         PreprocessedEntityID
1771             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1772         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1773         PreprocessingRecord::PPEntityID PPID =
1774             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1775         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1776             PPRec.getPreprocessedEntity(PPID));
1777         if (PPDef)
1778           PPRec.RegisterMacroDefinition(Macro, PPDef);
1779       }
1780 
1781       ++NumMacrosRead;
1782       break;
1783     }
1784 
1785     case PP_TOKEN: {
1786       // If we see a TOKEN before a PP_MACRO_*, then the file is
1787       // erroneous, just pretend we didn't see this.
1788       if (!Macro) break;
1789 
1790       unsigned Idx = 0;
1791       Token Tok = ReadToken(F, Record, Idx);
1792       Macro->AddTokenToBody(Tok);
1793       break;
1794     }
1795     }
1796   }
1797 }
1798 
1799 PreprocessedEntityID
1800 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1801                                          unsigned LocalID) const {
1802   if (!M.ModuleOffsetMap.empty())
1803     ReadModuleOffsetMap(M);
1804 
1805   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1806     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1807   assert(I != M.PreprocessedEntityRemap.end()
1808          && "Invalid index into preprocessed entity index remap");
1809 
1810   return LocalID + I->second;
1811 }
1812 
1813 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1814   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1815 }
1816 
1817 HeaderFileInfoTrait::internal_key_type
1818 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1819   internal_key_type ikey = {FE->getSize(),
1820                             M.HasTimestamps ? FE->getModificationTime() : 0,
1821                             FE->getName(), /*Imported*/ false};
1822   return ikey;
1823 }
1824 
1825 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1826   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1827     return false;
1828 
1829   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1830     return true;
1831 
1832   // Determine whether the actual files are equivalent.
1833   FileManager &FileMgr = Reader.getFileManager();
1834   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1835     if (!Key.Imported) {
1836       if (auto File = FileMgr.getFile(Key.Filename))
1837         return *File;
1838       return nullptr;
1839     }
1840 
1841     std::string Resolved = std::string(Key.Filename);
1842     Reader.ResolveImportedPath(M, Resolved);
1843     if (auto File = FileMgr.getFile(Resolved))
1844       return *File;
1845     return nullptr;
1846   };
1847 
1848   const FileEntry *FEA = GetFile(a);
1849   const FileEntry *FEB = GetFile(b);
1850   return FEA && FEA == FEB;
1851 }
1852 
1853 std::pair<unsigned, unsigned>
1854 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1855   using namespace llvm::support;
1856 
1857   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1858   unsigned DataLen = (unsigned) *d++;
1859   return std::make_pair(KeyLen, DataLen);
1860 }
1861 
1862 HeaderFileInfoTrait::internal_key_type
1863 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1864   using namespace llvm::support;
1865 
1866   internal_key_type ikey;
1867   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1868   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1869   ikey.Filename = (const char *)d;
1870   ikey.Imported = true;
1871   return ikey;
1872 }
1873 
1874 HeaderFileInfoTrait::data_type
1875 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1876                               unsigned DataLen) {
1877   using namespace llvm::support;
1878 
1879   const unsigned char *End = d + DataLen;
1880   HeaderFileInfo HFI;
1881   unsigned Flags = *d++;
1882   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1883   HFI.isImport |= (Flags >> 5) & 0x01;
1884   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1885   HFI.DirInfo = (Flags >> 1) & 0x07;
1886   HFI.IndexHeaderMapHeader = Flags & 0x01;
1887   // FIXME: Find a better way to handle this. Maybe just store a
1888   // "has been included" flag?
1889   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1890                              HFI.NumIncludes);
1891   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1892       M, endian::readNext<uint32_t, little, unaligned>(d));
1893   if (unsigned FrameworkOffset =
1894           endian::readNext<uint32_t, little, unaligned>(d)) {
1895     // The framework offset is 1 greater than the actual offset,
1896     // since 0 is used as an indicator for "no framework name".
1897     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1898     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1899   }
1900 
1901   assert((End - d) % 4 == 0 &&
1902          "Wrong data length in HeaderFileInfo deserialization");
1903   while (d != End) {
1904     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1905     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1906     LocalSMID >>= 2;
1907 
1908     // This header is part of a module. Associate it with the module to enable
1909     // implicit module import.
1910     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1911     Module *Mod = Reader.getSubmodule(GlobalSMID);
1912     FileManager &FileMgr = Reader.getFileManager();
1913     ModuleMap &ModMap =
1914         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1915 
1916     std::string Filename = std::string(key.Filename);
1917     if (key.Imported)
1918       Reader.ResolveImportedPath(M, Filename);
1919     // FIXME: This is not always the right filename-as-written, but we're not
1920     // going to use this information to rebuild the module, so it doesn't make
1921     // a lot of difference.
1922     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1923     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1924     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1925   }
1926 
1927   // This HeaderFileInfo was externally loaded.
1928   HFI.External = true;
1929   HFI.IsValid = true;
1930   return HFI;
1931 }
1932 
1933 void ASTReader::addPendingMacro(IdentifierInfo *II,
1934                                 ModuleFile *M,
1935                                 uint64_t MacroDirectivesOffset) {
1936   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1937   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1938 }
1939 
1940 void ASTReader::ReadDefinedMacros() {
1941   // Note that we are loading defined macros.
1942   Deserializing Macros(this);
1943 
1944   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1945     BitstreamCursor &MacroCursor = I.MacroCursor;
1946 
1947     // If there was no preprocessor block, skip this file.
1948     if (MacroCursor.getBitcodeBytes().empty())
1949       continue;
1950 
1951     BitstreamCursor Cursor = MacroCursor;
1952     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1953       Error(std::move(Err));
1954       return;
1955     }
1956 
1957     RecordData Record;
1958     while (true) {
1959       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1960       if (!MaybeE) {
1961         Error(MaybeE.takeError());
1962         return;
1963       }
1964       llvm::BitstreamEntry E = MaybeE.get();
1965 
1966       switch (E.Kind) {
1967       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1968       case llvm::BitstreamEntry::Error:
1969         Error("malformed block record in AST file");
1970         return;
1971       case llvm::BitstreamEntry::EndBlock:
1972         goto NextCursor;
1973 
1974       case llvm::BitstreamEntry::Record: {
1975         Record.clear();
1976         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1977         if (!MaybeRecord) {
1978           Error(MaybeRecord.takeError());
1979           return;
1980         }
1981         switch (MaybeRecord.get()) {
1982         default:  // Default behavior: ignore.
1983           break;
1984 
1985         case PP_MACRO_OBJECT_LIKE:
1986         case PP_MACRO_FUNCTION_LIKE: {
1987           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1988           if (II->isOutOfDate())
1989             updateOutOfDateIdentifier(*II);
1990           break;
1991         }
1992 
1993         case PP_TOKEN:
1994           // Ignore tokens.
1995           break;
1996         }
1997         break;
1998       }
1999       }
2000     }
2001     NextCursor:  ;
2002   }
2003 }
2004 
2005 namespace {
2006 
2007   /// Visitor class used to look up identifirs in an AST file.
2008   class IdentifierLookupVisitor {
2009     StringRef Name;
2010     unsigned NameHash;
2011     unsigned PriorGeneration;
2012     unsigned &NumIdentifierLookups;
2013     unsigned &NumIdentifierLookupHits;
2014     IdentifierInfo *Found = nullptr;
2015 
2016   public:
2017     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2018                             unsigned &NumIdentifierLookups,
2019                             unsigned &NumIdentifierLookupHits)
2020       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2021         PriorGeneration(PriorGeneration),
2022         NumIdentifierLookups(NumIdentifierLookups),
2023         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2024 
2025     bool operator()(ModuleFile &M) {
2026       // If we've already searched this module file, skip it now.
2027       if (M.Generation <= PriorGeneration)
2028         return true;
2029 
2030       ASTIdentifierLookupTable *IdTable
2031         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2032       if (!IdTable)
2033         return false;
2034 
2035       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2036                                      Found);
2037       ++NumIdentifierLookups;
2038       ASTIdentifierLookupTable::iterator Pos =
2039           IdTable->find_hashed(Name, NameHash, &Trait);
2040       if (Pos == IdTable->end())
2041         return false;
2042 
2043       // Dereferencing the iterator has the effect of building the
2044       // IdentifierInfo node and populating it with the various
2045       // declarations it needs.
2046       ++NumIdentifierLookupHits;
2047       Found = *Pos;
2048       return true;
2049     }
2050 
2051     // Retrieve the identifier info found within the module
2052     // files.
2053     IdentifierInfo *getIdentifierInfo() const { return Found; }
2054   };
2055 
2056 } // namespace
2057 
2058 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2059   // Note that we are loading an identifier.
2060   Deserializing AnIdentifier(this);
2061 
2062   unsigned PriorGeneration = 0;
2063   if (getContext().getLangOpts().Modules)
2064     PriorGeneration = IdentifierGeneration[&II];
2065 
2066   // If there is a global index, look there first to determine which modules
2067   // provably do not have any results for this identifier.
2068   GlobalModuleIndex::HitSet Hits;
2069   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2070   if (!loadGlobalIndex()) {
2071     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2072       HitsPtr = &Hits;
2073     }
2074   }
2075 
2076   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2077                                   NumIdentifierLookups,
2078                                   NumIdentifierLookupHits);
2079   ModuleMgr.visit(Visitor, HitsPtr);
2080   markIdentifierUpToDate(&II);
2081 }
2082 
2083 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2084   if (!II)
2085     return;
2086 
2087   II->setOutOfDate(false);
2088 
2089   // Update the generation for this identifier.
2090   if (getContext().getLangOpts().Modules)
2091     IdentifierGeneration[II] = getGeneration();
2092 }
2093 
2094 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2095                                     const PendingMacroInfo &PMInfo) {
2096   ModuleFile &M = *PMInfo.M;
2097 
2098   BitstreamCursor &Cursor = M.MacroCursor;
2099   SavedStreamPosition SavedPosition(Cursor);
2100   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2101     Error(std::move(Err));
2102     return;
2103   }
2104 
2105   struct ModuleMacroRecord {
2106     SubmoduleID SubModID;
2107     MacroInfo *MI;
2108     SmallVector<SubmoduleID, 8> Overrides;
2109   };
2110   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2111 
2112   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2113   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2114   // macro histroy.
2115   RecordData Record;
2116   while (true) {
2117     Expected<llvm::BitstreamEntry> MaybeEntry =
2118         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2119     if (!MaybeEntry) {
2120       Error(MaybeEntry.takeError());
2121       return;
2122     }
2123     llvm::BitstreamEntry Entry = MaybeEntry.get();
2124 
2125     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2126       Error("malformed block record in AST file");
2127       return;
2128     }
2129 
2130     Record.clear();
2131     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2132     if (!MaybePP) {
2133       Error(MaybePP.takeError());
2134       return;
2135     }
2136     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2137     case PP_MACRO_DIRECTIVE_HISTORY:
2138       break;
2139 
2140     case PP_MODULE_MACRO: {
2141       ModuleMacros.push_back(ModuleMacroRecord());
2142       auto &Info = ModuleMacros.back();
2143       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2144       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2145       for (int I = 2, N = Record.size(); I != N; ++I)
2146         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2147       continue;
2148     }
2149 
2150     default:
2151       Error("malformed block record in AST file");
2152       return;
2153     }
2154 
2155     // We found the macro directive history; that's the last record
2156     // for this macro.
2157     break;
2158   }
2159 
2160   // Module macros are listed in reverse dependency order.
2161   {
2162     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2163     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2164     for (auto &MMR : ModuleMacros) {
2165       Overrides.clear();
2166       for (unsigned ModID : MMR.Overrides) {
2167         Module *Mod = getSubmodule(ModID);
2168         auto *Macro = PP.getModuleMacro(Mod, II);
2169         assert(Macro && "missing definition for overridden macro");
2170         Overrides.push_back(Macro);
2171       }
2172 
2173       bool Inserted = false;
2174       Module *Owner = getSubmodule(MMR.SubModID);
2175       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2176     }
2177   }
2178 
2179   // Don't read the directive history for a module; we don't have anywhere
2180   // to put it.
2181   if (M.isModule())
2182     return;
2183 
2184   // Deserialize the macro directives history in reverse source-order.
2185   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2186   unsigned Idx = 0, N = Record.size();
2187   while (Idx < N) {
2188     MacroDirective *MD = nullptr;
2189     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2190     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2191     switch (K) {
2192     case MacroDirective::MD_Define: {
2193       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2194       MD = PP.AllocateDefMacroDirective(MI, Loc);
2195       break;
2196     }
2197     case MacroDirective::MD_Undefine:
2198       MD = PP.AllocateUndefMacroDirective(Loc);
2199       break;
2200     case MacroDirective::MD_Visibility:
2201       bool isPublic = Record[Idx++];
2202       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2203       break;
2204     }
2205 
2206     if (!Latest)
2207       Latest = MD;
2208     if (Earliest)
2209       Earliest->setPrevious(MD);
2210     Earliest = MD;
2211   }
2212 
2213   if (Latest)
2214     PP.setLoadedMacroDirective(II, Earliest, Latest);
2215 }
2216 
2217 ASTReader::InputFileInfo
2218 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2219   // Go find this input file.
2220   BitstreamCursor &Cursor = F.InputFilesCursor;
2221   SavedStreamPosition SavedPosition(Cursor);
2222   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2223     // FIXME this drops errors on the floor.
2224     consumeError(std::move(Err));
2225   }
2226 
2227   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2228   if (!MaybeCode) {
2229     // FIXME this drops errors on the floor.
2230     consumeError(MaybeCode.takeError());
2231   }
2232   unsigned Code = MaybeCode.get();
2233   RecordData Record;
2234   StringRef Blob;
2235 
2236   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2237     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2238            "invalid record type for input file");
2239   else {
2240     // FIXME this drops errors on the floor.
2241     consumeError(Maybe.takeError());
2242   }
2243 
2244   assert(Record[0] == ID && "Bogus stored ID or offset");
2245   InputFileInfo R;
2246   R.StoredSize = static_cast<off_t>(Record[1]);
2247   R.StoredTime = static_cast<time_t>(Record[2]);
2248   R.Overridden = static_cast<bool>(Record[3]);
2249   R.Transient = static_cast<bool>(Record[4]);
2250   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2251   R.Filename = std::string(Blob);
2252   ResolveImportedPath(F, R.Filename);
2253 
2254   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2255   if (!MaybeEntry) // FIXME this drops errors on the floor.
2256     consumeError(MaybeEntry.takeError());
2257   llvm::BitstreamEntry Entry = MaybeEntry.get();
2258   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2259          "expected record type for input file hash");
2260 
2261   Record.clear();
2262   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2263     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2264            "invalid record type for input file hash");
2265   else {
2266     // FIXME this drops errors on the floor.
2267     consumeError(Maybe.takeError());
2268   }
2269   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2270                   static_cast<uint64_t>(Record[0]);
2271   return R;
2272 }
2273 
2274 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2275 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2276   // If this ID is bogus, just return an empty input file.
2277   if (ID == 0 || ID > F.InputFilesLoaded.size())
2278     return InputFile();
2279 
2280   // If we've already loaded this input file, return it.
2281   if (F.InputFilesLoaded[ID-1].getFile())
2282     return F.InputFilesLoaded[ID-1];
2283 
2284   if (F.InputFilesLoaded[ID-1].isNotFound())
2285     return InputFile();
2286 
2287   // Go find this input file.
2288   BitstreamCursor &Cursor = F.InputFilesCursor;
2289   SavedStreamPosition SavedPosition(Cursor);
2290   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2291     // FIXME this drops errors on the floor.
2292     consumeError(std::move(Err));
2293   }
2294 
2295   InputFileInfo FI = readInputFileInfo(F, ID);
2296   off_t StoredSize = FI.StoredSize;
2297   time_t StoredTime = FI.StoredTime;
2298   bool Overridden = FI.Overridden;
2299   bool Transient = FI.Transient;
2300   StringRef Filename = FI.Filename;
2301   uint64_t StoredContentHash = FI.ContentHash;
2302 
2303   const FileEntry *File = nullptr;
2304   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2305     File = *FE;
2306 
2307   // If we didn't find the file, resolve it relative to the
2308   // original directory from which this AST file was created.
2309   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2310       F.OriginalDir != F.BaseDirectory) {
2311     std::string Resolved = resolveFileRelativeToOriginalDir(
2312         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2313     if (!Resolved.empty())
2314       if (auto FE = FileMgr.getFile(Resolved))
2315         File = *FE;
2316   }
2317 
2318   // For an overridden file, create a virtual file with the stored
2319   // size/timestamp.
2320   if ((Overridden || Transient) && File == nullptr)
2321     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2322 
2323   if (File == nullptr) {
2324     if (Complain) {
2325       std::string ErrorStr = "could not find file '";
2326       ErrorStr += Filename;
2327       ErrorStr += "' referenced by AST file '";
2328       ErrorStr += F.FileName;
2329       ErrorStr += "'";
2330       Error(ErrorStr);
2331     }
2332     // Record that we didn't find the file.
2333     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2334     return InputFile();
2335   }
2336 
2337   // Check if there was a request to override the contents of the file
2338   // that was part of the precompiled header. Overriding such a file
2339   // can lead to problems when lexing using the source locations from the
2340   // PCH.
2341   SourceManager &SM = getSourceManager();
2342   // FIXME: Reject if the overrides are different.
2343   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2344     if (Complain)
2345       Error(diag::err_fe_pch_file_overridden, Filename);
2346 
2347     // After emitting the diagnostic, bypass the overriding file to recover
2348     // (this creates a separate FileEntry).
2349     File = SM.bypassFileContentsOverride(*File);
2350     if (!File) {
2351       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2352       return InputFile();
2353     }
2354   }
2355 
2356   enum ModificationType {
2357     Size,
2358     ModTime,
2359     Content,
2360     None,
2361   };
2362   auto HasInputFileChanged = [&]() {
2363     if (StoredSize != File->getSize())
2364       return ModificationType::Size;
2365     if (!DisableValidation && StoredTime &&
2366         StoredTime != File->getModificationTime()) {
2367       // In case the modification time changes but not the content,
2368       // accept the cached file as legit.
2369       if (ValidateASTInputFilesContent &&
2370           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2371         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2372         if (!MemBuffOrError) {
2373           if (!Complain)
2374             return ModificationType::ModTime;
2375           std::string ErrorStr = "could not get buffer for file '";
2376           ErrorStr += File->getName();
2377           ErrorStr += "'";
2378           Error(ErrorStr);
2379           return ModificationType::ModTime;
2380         }
2381 
2382         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2383         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2384           return ModificationType::None;
2385         return ModificationType::Content;
2386       }
2387       return ModificationType::ModTime;
2388     }
2389     return ModificationType::None;
2390   };
2391 
2392   bool IsOutOfDate = false;
2393   auto FileChange = HasInputFileChanged();
2394   // For an overridden file, there is nothing to validate.
2395   if (!Overridden && FileChange != ModificationType::None) {
2396     if (Complain) {
2397       // Build a list of the PCH imports that got us here (in reverse).
2398       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2399       while (!ImportStack.back()->ImportedBy.empty())
2400         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2401 
2402       // The top-level PCH is stale.
2403       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2404       unsigned DiagnosticKind =
2405           moduleKindForDiagnostic(ImportStack.back()->Kind);
2406       if (DiagnosticKind == 0)
2407         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2408               (unsigned)FileChange);
2409       else if (DiagnosticKind == 1)
2410         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2411               (unsigned)FileChange);
2412       else
2413         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2414               (unsigned)FileChange);
2415 
2416       // Print the import stack.
2417       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2418         Diag(diag::note_pch_required_by)
2419           << Filename << ImportStack[0]->FileName;
2420         for (unsigned I = 1; I < ImportStack.size(); ++I)
2421           Diag(diag::note_pch_required_by)
2422             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2423       }
2424 
2425       if (!Diags.isDiagnosticInFlight())
2426         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2427     }
2428 
2429     IsOutOfDate = true;
2430   }
2431   // FIXME: If the file is overridden and we've already opened it,
2432   // issue an error (or split it into a separate FileEntry).
2433 
2434   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2435 
2436   // Note that we've loaded this input file.
2437   F.InputFilesLoaded[ID-1] = IF;
2438   return IF;
2439 }
2440 
2441 /// If we are loading a relocatable PCH or module file, and the filename
2442 /// is not an absolute path, add the system or module root to the beginning of
2443 /// the file name.
2444 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2445   // Resolve relative to the base directory, if we have one.
2446   if (!M.BaseDirectory.empty())
2447     return ResolveImportedPath(Filename, M.BaseDirectory);
2448 }
2449 
2450 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2451   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2452     return;
2453 
2454   SmallString<128> Buffer;
2455   llvm::sys::path::append(Buffer, Prefix, Filename);
2456   Filename.assign(Buffer.begin(), Buffer.end());
2457 }
2458 
2459 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2460   switch (ARR) {
2461   case ASTReader::Failure: return true;
2462   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2463   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2464   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2465   case ASTReader::ConfigurationMismatch:
2466     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2467   case ASTReader::HadErrors: return true;
2468   case ASTReader::Success: return false;
2469   }
2470 
2471   llvm_unreachable("unknown ASTReadResult");
2472 }
2473 
2474 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2475     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2476     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2477     std::string &SuggestedPredefines) {
2478   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2479     // FIXME this drops errors on the floor.
2480     consumeError(std::move(Err));
2481     return Failure;
2482   }
2483 
2484   // Read all of the records in the options block.
2485   RecordData Record;
2486   ASTReadResult Result = Success;
2487   while (true) {
2488     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2489     if (!MaybeEntry) {
2490       // FIXME this drops errors on the floor.
2491       consumeError(MaybeEntry.takeError());
2492       return Failure;
2493     }
2494     llvm::BitstreamEntry Entry = MaybeEntry.get();
2495 
2496     switch (Entry.Kind) {
2497     case llvm::BitstreamEntry::Error:
2498     case llvm::BitstreamEntry::SubBlock:
2499       return Failure;
2500 
2501     case llvm::BitstreamEntry::EndBlock:
2502       return Result;
2503 
2504     case llvm::BitstreamEntry::Record:
2505       // The interesting case.
2506       break;
2507     }
2508 
2509     // Read and process a record.
2510     Record.clear();
2511     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2512     if (!MaybeRecordType) {
2513       // FIXME this drops errors on the floor.
2514       consumeError(MaybeRecordType.takeError());
2515       return Failure;
2516     }
2517     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2518     case LANGUAGE_OPTIONS: {
2519       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2520       if (ParseLanguageOptions(Record, Complain, Listener,
2521                                AllowCompatibleConfigurationMismatch))
2522         Result = ConfigurationMismatch;
2523       break;
2524     }
2525 
2526     case TARGET_OPTIONS: {
2527       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528       if (ParseTargetOptions(Record, Complain, Listener,
2529                              AllowCompatibleConfigurationMismatch))
2530         Result = ConfigurationMismatch;
2531       break;
2532     }
2533 
2534     case FILE_SYSTEM_OPTIONS: {
2535       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536       if (!AllowCompatibleConfigurationMismatch &&
2537           ParseFileSystemOptions(Record, Complain, Listener))
2538         Result = ConfigurationMismatch;
2539       break;
2540     }
2541 
2542     case HEADER_SEARCH_OPTIONS: {
2543       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544       if (!AllowCompatibleConfigurationMismatch &&
2545           ParseHeaderSearchOptions(Record, Complain, Listener))
2546         Result = ConfigurationMismatch;
2547       break;
2548     }
2549 
2550     case PREPROCESSOR_OPTIONS:
2551       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552       if (!AllowCompatibleConfigurationMismatch &&
2553           ParsePreprocessorOptions(Record, Complain, Listener,
2554                                    SuggestedPredefines))
2555         Result = ConfigurationMismatch;
2556       break;
2557     }
2558   }
2559 }
2560 
2561 ASTReader::ASTReadResult
2562 ASTReader::ReadControlBlock(ModuleFile &F,
2563                             SmallVectorImpl<ImportedModule> &Loaded,
2564                             const ModuleFile *ImportedBy,
2565                             unsigned ClientLoadCapabilities) {
2566   BitstreamCursor &Stream = F.Stream;
2567 
2568   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2569     Error(std::move(Err));
2570     return Failure;
2571   }
2572 
2573   // Lambda to read the unhashed control block the first time it's called.
2574   //
2575   // For PCM files, the unhashed control block cannot be read until after the
2576   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2577   // need to look ahead before reading the IMPORTS record.  For consistency,
2578   // this block is always read somehow (see BitstreamEntry::EndBlock).
2579   bool HasReadUnhashedControlBlock = false;
2580   auto readUnhashedControlBlockOnce = [&]() {
2581     if (!HasReadUnhashedControlBlock) {
2582       HasReadUnhashedControlBlock = true;
2583       if (ASTReadResult Result =
2584               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2585         return Result;
2586     }
2587     return Success;
2588   };
2589 
2590   // Read all of the records and blocks in the control block.
2591   RecordData Record;
2592   unsigned NumInputs = 0;
2593   unsigned NumUserInputs = 0;
2594   StringRef BaseDirectoryAsWritten;
2595   while (true) {
2596     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2597     if (!MaybeEntry) {
2598       Error(MaybeEntry.takeError());
2599       return Failure;
2600     }
2601     llvm::BitstreamEntry Entry = MaybeEntry.get();
2602 
2603     switch (Entry.Kind) {
2604     case llvm::BitstreamEntry::Error:
2605       Error("malformed block record in AST file");
2606       return Failure;
2607     case llvm::BitstreamEntry::EndBlock: {
2608       // Validate the module before returning.  This call catches an AST with
2609       // no module name and no imports.
2610       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2611         return Result;
2612 
2613       // Validate input files.
2614       const HeaderSearchOptions &HSOpts =
2615           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2616 
2617       // All user input files reside at the index range [0, NumUserInputs), and
2618       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2619       // loaded module files, ignore missing inputs.
2620       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2621           F.Kind != MK_PrebuiltModule) {
2622         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2623 
2624         // If we are reading a module, we will create a verification timestamp,
2625         // so we verify all input files.  Otherwise, verify only user input
2626         // files.
2627 
2628         unsigned N = NumUserInputs;
2629         if (ValidateSystemInputs ||
2630             (HSOpts.ModulesValidateOncePerBuildSession &&
2631              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2632              F.Kind == MK_ImplicitModule))
2633           N = NumInputs;
2634 
2635         for (unsigned I = 0; I < N; ++I) {
2636           InputFile IF = getInputFile(F, I+1, Complain);
2637           if (!IF.getFile() || IF.isOutOfDate())
2638             return OutOfDate;
2639         }
2640       }
2641 
2642       if (Listener)
2643         Listener->visitModuleFile(F.FileName, F.Kind);
2644 
2645       if (Listener && Listener->needsInputFileVisitation()) {
2646         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2647                                                                 : NumUserInputs;
2648         for (unsigned I = 0; I < N; ++I) {
2649           bool IsSystem = I >= NumUserInputs;
2650           InputFileInfo FI = readInputFileInfo(F, I+1);
2651           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2652                                    F.Kind == MK_ExplicitModule ||
2653                                    F.Kind == MK_PrebuiltModule);
2654         }
2655       }
2656 
2657       return Success;
2658     }
2659 
2660     case llvm::BitstreamEntry::SubBlock:
2661       switch (Entry.ID) {
2662       case INPUT_FILES_BLOCK_ID:
2663         F.InputFilesCursor = Stream;
2664         if (llvm::Error Err = Stream.SkipBlock()) {
2665           Error(std::move(Err));
2666           return Failure;
2667         }
2668         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2669           Error("malformed block record in AST file");
2670           return Failure;
2671         }
2672         continue;
2673 
2674       case OPTIONS_BLOCK_ID:
2675         // If we're reading the first module for this group, check its options
2676         // are compatible with ours. For modules it imports, no further checking
2677         // is required, because we checked them when we built it.
2678         if (Listener && !ImportedBy) {
2679           // Should we allow the configuration of the module file to differ from
2680           // the configuration of the current translation unit in a compatible
2681           // way?
2682           //
2683           // FIXME: Allow this for files explicitly specified with -include-pch.
2684           bool AllowCompatibleConfigurationMismatch =
2685               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2686 
2687           ASTReadResult Result =
2688               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2689                                AllowCompatibleConfigurationMismatch, *Listener,
2690                                SuggestedPredefines);
2691           if (Result == Failure) {
2692             Error("malformed block record in AST file");
2693             return Result;
2694           }
2695 
2696           if (DisableValidation ||
2697               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2698             Result = Success;
2699 
2700           // If we can't load the module, exit early since we likely
2701           // will rebuild the module anyway. The stream may be in the
2702           // middle of a block.
2703           if (Result != Success)
2704             return Result;
2705         } else if (llvm::Error Err = Stream.SkipBlock()) {
2706           Error(std::move(Err));
2707           return Failure;
2708         }
2709         continue;
2710 
2711       default:
2712         if (llvm::Error Err = Stream.SkipBlock()) {
2713           Error(std::move(Err));
2714           return Failure;
2715         }
2716         continue;
2717       }
2718 
2719     case llvm::BitstreamEntry::Record:
2720       // The interesting case.
2721       break;
2722     }
2723 
2724     // Read and process a record.
2725     Record.clear();
2726     StringRef Blob;
2727     Expected<unsigned> MaybeRecordType =
2728         Stream.readRecord(Entry.ID, Record, &Blob);
2729     if (!MaybeRecordType) {
2730       Error(MaybeRecordType.takeError());
2731       return Failure;
2732     }
2733     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2734     case METADATA: {
2735       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2736         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2737           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2738                                         : diag::err_pch_version_too_new);
2739         return VersionMismatch;
2740       }
2741 
2742       bool hasErrors = Record[7];
2743       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2744         Diag(diag::err_pch_with_compiler_errors);
2745         return HadErrors;
2746       }
2747       if (hasErrors) {
2748         Diags.ErrorOccurred = true;
2749         Diags.UncompilableErrorOccurred = true;
2750         Diags.UnrecoverableErrorOccurred = true;
2751       }
2752 
2753       F.RelocatablePCH = Record[4];
2754       // Relative paths in a relocatable PCH are relative to our sysroot.
2755       if (F.RelocatablePCH)
2756         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2757 
2758       F.HasTimestamps = Record[5];
2759 
2760       F.PCHHasObjectFile = Record[6];
2761 
2762       const std::string &CurBranch = getClangFullRepositoryVersion();
2763       StringRef ASTBranch = Blob;
2764       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2767         return VersionMismatch;
2768       }
2769       break;
2770     }
2771 
2772     case IMPORTS: {
2773       // Validate the AST before processing any imports (otherwise, untangling
2774       // them can be error-prone and expensive).  A module will have a name and
2775       // will already have been validated, but this catches the PCH case.
2776       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2777         return Result;
2778 
2779       // Load each of the imported PCH files.
2780       unsigned Idx = 0, N = Record.size();
2781       while (Idx < N) {
2782         // Read information about the AST file.
2783         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2784         // The import location will be the local one for now; we will adjust
2785         // all import locations of module imports after the global source
2786         // location info are setup, in ReadAST.
2787         SourceLocation ImportLoc =
2788             ReadUntranslatedSourceLocation(Record[Idx++]);
2789         off_t StoredSize = (off_t)Record[Idx++];
2790         time_t StoredModTime = (time_t)Record[Idx++];
2791         ASTFileSignature StoredSignature = {
2792             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2793               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2794               (uint32_t)Record[Idx++]}}};
2795 
2796         std::string ImportedName = ReadString(Record, Idx);
2797         std::string ImportedFile;
2798 
2799         // For prebuilt and explicit modules first consult the file map for
2800         // an override. Note that here we don't search prebuilt module
2801         // directories, only the explicit name to file mappings. Also, we will
2802         // still verify the size/signature making sure it is essentially the
2803         // same file but perhaps in a different location.
2804         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806             ImportedName, /*FileMapOnly*/ true);
2807 
2808         if (ImportedFile.empty())
2809           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810           // ModuleCache as when writing.
2811           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2812         else
2813           SkipPath(Record, Idx);
2814 
2815         // If our client can't cope with us being out of date, we can't cope with
2816         // our dependency being missing.
2817         unsigned Capabilities = ClientLoadCapabilities;
2818         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819           Capabilities &= ~ARR_Missing;
2820 
2821         // Load the AST file.
2822         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823                                   Loaded, StoredSize, StoredModTime,
2824                                   StoredSignature, Capabilities);
2825 
2826         // If we diagnosed a problem, produce a backtrace.
2827         if (isDiagnosedResult(Result, Capabilities))
2828           Diag(diag::note_module_file_imported_by)
2829               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2830 
2831         switch (Result) {
2832         case Failure: return Failure;
2833           // If we have to ignore the dependency, we'll have to ignore this too.
2834         case Missing:
2835         case OutOfDate: return OutOfDate;
2836         case VersionMismatch: return VersionMismatch;
2837         case ConfigurationMismatch: return ConfigurationMismatch;
2838         case HadErrors: return HadErrors;
2839         case Success: break;
2840         }
2841       }
2842       break;
2843     }
2844 
2845     case ORIGINAL_FILE:
2846       F.OriginalSourceFileID = FileID::get(Record[0]);
2847       F.ActualOriginalSourceFileName = std::string(Blob);
2848       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2849       ResolveImportedPath(F, F.OriginalSourceFileName);
2850       break;
2851 
2852     case ORIGINAL_FILE_ID:
2853       F.OriginalSourceFileID = FileID::get(Record[0]);
2854       break;
2855 
2856     case ORIGINAL_PCH_DIR:
2857       F.OriginalDir = std::string(Blob);
2858       break;
2859 
2860     case MODULE_NAME:
2861       F.ModuleName = std::string(Blob);
2862       Diag(diag::remark_module_import)
2863           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2865       if (Listener)
2866         Listener->ReadModuleName(F.ModuleName);
2867 
2868       // Validate the AST as soon as we have a name so we can exit early on
2869       // failure.
2870       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2871         return Result;
2872 
2873       break;
2874 
2875     case MODULE_DIRECTORY: {
2876       // Save the BaseDirectory as written in the PCM for computing the module
2877       // filename for the ModuleCache.
2878       BaseDirectoryAsWritten = Blob;
2879       assert(!F.ModuleName.empty() &&
2880              "MODULE_DIRECTORY found before MODULE_NAME");
2881       // If we've already loaded a module map file covering this module, we may
2882       // have a better path for it (relative to the current build).
2883       Module *M = PP.getHeaderSearchInfo().lookupModule(
2884           F.ModuleName, /*AllowSearch*/ true,
2885           /*AllowExtraModuleMapSearch*/ true);
2886       if (M && M->Directory) {
2887         // If we're implicitly loading a module, the base directory can't
2888         // change between the build and use.
2889         // Don't emit module relocation error if we have -fno-validate-pch
2890         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2892           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893           if (!BuildDir || *BuildDir != M->Directory) {
2894             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895               Diag(diag::err_imported_module_relocated)
2896                   << F.ModuleName << Blob << M->Directory->getName();
2897             return OutOfDate;
2898           }
2899         }
2900         F.BaseDirectory = std::string(M->Directory->getName());
2901       } else {
2902         F.BaseDirectory = std::string(Blob);
2903       }
2904       break;
2905     }
2906 
2907     case MODULE_MAP_FILE:
2908       if (ASTReadResult Result =
2909               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910         return Result;
2911       break;
2912 
2913     case INPUT_FILE_OFFSETS:
2914       NumInputs = Record[0];
2915       NumUserInputs = Record[1];
2916       F.InputFileOffsets =
2917           (const llvm::support::unaligned_uint64_t *)Blob.data();
2918       F.InputFilesLoaded.resize(NumInputs);
2919       F.NumUserInputFiles = NumUserInputs;
2920       break;
2921     }
2922   }
2923 }
2924 
2925 ASTReader::ASTReadResult
2926 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2927   BitstreamCursor &Stream = F.Stream;
2928 
2929   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930     Error(std::move(Err));
2931     return Failure;
2932   }
2933 
2934   // Read all of the records and blocks for the AST file.
2935   RecordData Record;
2936   while (true) {
2937     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2938     if (!MaybeEntry) {
2939       Error(MaybeEntry.takeError());
2940       return Failure;
2941     }
2942     llvm::BitstreamEntry Entry = MaybeEntry.get();
2943 
2944     switch (Entry.Kind) {
2945     case llvm::BitstreamEntry::Error:
2946       Error("error at end of module block in AST file");
2947       return Failure;
2948     case llvm::BitstreamEntry::EndBlock:
2949       // Outside of C++, we do not store a lookup map for the translation unit.
2950       // Instead, mark it as needing a lookup map to be built if this module
2951       // contains any declarations lexically within it (which it always does!).
2952       // This usually has no cost, since we very rarely need the lookup map for
2953       // the translation unit outside C++.
2954       if (ASTContext *Ctx = ContextObj) {
2955         DeclContext *DC = Ctx->getTranslationUnitDecl();
2956         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2957           DC->setMustBuildLookupTable();
2958       }
2959 
2960       return Success;
2961     case llvm::BitstreamEntry::SubBlock:
2962       switch (Entry.ID) {
2963       case DECLTYPES_BLOCK_ID:
2964         // We lazily load the decls block, but we want to set up the
2965         // DeclsCursor cursor to point into it.  Clone our current bitcode
2966         // cursor to it, enter the block and read the abbrevs in that block.
2967         // With the main cursor, we just skip over it.
2968         F.DeclsCursor = Stream;
2969         if (llvm::Error Err = Stream.SkipBlock()) {
2970           Error(std::move(Err));
2971           return Failure;
2972         }
2973         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2974           Error("malformed block record in AST file");
2975           return Failure;
2976         }
2977         break;
2978 
2979       case PREPROCESSOR_BLOCK_ID:
2980         F.MacroCursor = Stream;
2981         if (!PP.getExternalSource())
2982           PP.setExternalSource(this);
2983 
2984         if (llvm::Error Err = Stream.SkipBlock()) {
2985           Error(std::move(Err));
2986           return Failure;
2987         }
2988         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2989           Error("malformed block record in AST file");
2990           return Failure;
2991         }
2992         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2993         break;
2994 
2995       case PREPROCESSOR_DETAIL_BLOCK_ID:
2996         F.PreprocessorDetailCursor = Stream;
2997 
2998         if (llvm::Error Err = Stream.SkipBlock()) {
2999           Error(std::move(Err));
3000           return Failure;
3001         }
3002         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3003                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3004           Error("malformed preprocessor detail record in AST file");
3005           return Failure;
3006         }
3007         F.PreprocessorDetailStartOffset
3008         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3009 
3010         if (!PP.getPreprocessingRecord())
3011           PP.createPreprocessingRecord();
3012         if (!PP.getPreprocessingRecord()->getExternalSource())
3013           PP.getPreprocessingRecord()->SetExternalSource(*this);
3014         break;
3015 
3016       case SOURCE_MANAGER_BLOCK_ID:
3017         if (ReadSourceManagerBlock(F))
3018           return Failure;
3019         break;
3020 
3021       case SUBMODULE_BLOCK_ID:
3022         if (ASTReadResult Result =
3023                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3024           return Result;
3025         break;
3026 
3027       case COMMENTS_BLOCK_ID: {
3028         BitstreamCursor C = Stream;
3029 
3030         if (llvm::Error Err = Stream.SkipBlock()) {
3031           Error(std::move(Err));
3032           return Failure;
3033         }
3034         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3035           Error("malformed comments block in AST file");
3036           return Failure;
3037         }
3038         CommentsCursors.push_back(std::make_pair(C, &F));
3039         break;
3040       }
3041 
3042       default:
3043         if (llvm::Error Err = Stream.SkipBlock()) {
3044           Error(std::move(Err));
3045           return Failure;
3046         }
3047         break;
3048       }
3049       continue;
3050 
3051     case llvm::BitstreamEntry::Record:
3052       // The interesting case.
3053       break;
3054     }
3055 
3056     // Read and process a record.
3057     Record.clear();
3058     StringRef Blob;
3059     Expected<unsigned> MaybeRecordType =
3060         Stream.readRecord(Entry.ID, Record, &Blob);
3061     if (!MaybeRecordType) {
3062       Error(MaybeRecordType.takeError());
3063       return Failure;
3064     }
3065     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3066 
3067     // If we're not loading an AST context, we don't care about most records.
3068     if (!ContextObj) {
3069       switch (RecordType) {
3070       case IDENTIFIER_TABLE:
3071       case IDENTIFIER_OFFSET:
3072       case INTERESTING_IDENTIFIERS:
3073       case STATISTICS:
3074       case PP_CONDITIONAL_STACK:
3075       case PP_COUNTER_VALUE:
3076       case SOURCE_LOCATION_OFFSETS:
3077       case MODULE_OFFSET_MAP:
3078       case SOURCE_MANAGER_LINE_TABLE:
3079       case SOURCE_LOCATION_PRELOADS:
3080       case PPD_ENTITIES_OFFSETS:
3081       case HEADER_SEARCH_TABLE:
3082       case IMPORTED_MODULES:
3083       case MACRO_OFFSET:
3084         break;
3085       default:
3086         continue;
3087       }
3088     }
3089 
3090     switch (RecordType) {
3091     default:  // Default behavior: ignore.
3092       break;
3093 
3094     case TYPE_OFFSET: {
3095       if (F.LocalNumTypes != 0) {
3096         Error("duplicate TYPE_OFFSET record in AST file");
3097         return Failure;
3098       }
3099       F.TypeOffsets = (const uint32_t *)Blob.data();
3100       F.LocalNumTypes = Record[0];
3101       unsigned LocalBaseTypeIndex = Record[1];
3102       F.BaseTypeIndex = getTotalNumTypes();
3103 
3104       if (F.LocalNumTypes > 0) {
3105         // Introduce the global -> local mapping for types within this module.
3106         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3107 
3108         // Introduce the local -> global mapping for types within this module.
3109         F.TypeRemap.insertOrReplace(
3110           std::make_pair(LocalBaseTypeIndex,
3111                          F.BaseTypeIndex - LocalBaseTypeIndex));
3112 
3113         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3114       }
3115       break;
3116     }
3117 
3118     case DECL_OFFSET: {
3119       if (F.LocalNumDecls != 0) {
3120         Error("duplicate DECL_OFFSET record in AST file");
3121         return Failure;
3122       }
3123       F.DeclOffsets = (const DeclOffset *)Blob.data();
3124       F.LocalNumDecls = Record[0];
3125       unsigned LocalBaseDeclID = Record[1];
3126       F.BaseDeclID = getTotalNumDecls();
3127 
3128       if (F.LocalNumDecls > 0) {
3129         // Introduce the global -> local mapping for declarations within this
3130         // module.
3131         GlobalDeclMap.insert(
3132           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3133 
3134         // Introduce the local -> global mapping for declarations within this
3135         // module.
3136         F.DeclRemap.insertOrReplace(
3137           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3138 
3139         // Introduce the global -> local mapping for declarations within this
3140         // module.
3141         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3142 
3143         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3144       }
3145       break;
3146     }
3147 
3148     case TU_UPDATE_LEXICAL: {
3149       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3150       LexicalContents Contents(
3151           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3152               Blob.data()),
3153           static_cast<unsigned int>(Blob.size() / 4));
3154       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3155       TU->setHasExternalLexicalStorage(true);
3156       break;
3157     }
3158 
3159     case UPDATE_VISIBLE: {
3160       unsigned Idx = 0;
3161       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3162       auto *Data = (const unsigned char*)Blob.data();
3163       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3164       // If we've already loaded the decl, perform the updates when we finish
3165       // loading this block.
3166       if (Decl *D = GetExistingDecl(ID))
3167         PendingUpdateRecords.push_back(
3168             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3169       break;
3170     }
3171 
3172     case IDENTIFIER_TABLE:
3173       F.IdentifierTableData = Blob.data();
3174       if (Record[0]) {
3175         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3176             (const unsigned char *)F.IdentifierTableData + Record[0],
3177             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3178             (const unsigned char *)F.IdentifierTableData,
3179             ASTIdentifierLookupTrait(*this, F));
3180 
3181         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3182       }
3183       break;
3184 
3185     case IDENTIFIER_OFFSET: {
3186       if (F.LocalNumIdentifiers != 0) {
3187         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3188         return Failure;
3189       }
3190       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3191       F.LocalNumIdentifiers = Record[0];
3192       unsigned LocalBaseIdentifierID = Record[1];
3193       F.BaseIdentifierID = getTotalNumIdentifiers();
3194 
3195       if (F.LocalNumIdentifiers > 0) {
3196         // Introduce the global -> local mapping for identifiers within this
3197         // module.
3198         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3199                                                   &F));
3200 
3201         // Introduce the local -> global mapping for identifiers within this
3202         // module.
3203         F.IdentifierRemap.insertOrReplace(
3204           std::make_pair(LocalBaseIdentifierID,
3205                          F.BaseIdentifierID - LocalBaseIdentifierID));
3206 
3207         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3208                                  + F.LocalNumIdentifiers);
3209       }
3210       break;
3211     }
3212 
3213     case INTERESTING_IDENTIFIERS:
3214       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3215       break;
3216 
3217     case EAGERLY_DESERIALIZED_DECLS:
3218       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3219       // about "interesting" decls (for instance, if we're building a module).
3220       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3221         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3222       break;
3223 
3224     case MODULAR_CODEGEN_DECLS:
3225       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3226       // them (ie: if we're not codegenerating this module).
3227       if (F.Kind == MK_MainFile)
3228         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3229           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3230       break;
3231 
3232     case SPECIAL_TYPES:
3233       if (SpecialTypes.empty()) {
3234         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3235           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3236         break;
3237       }
3238 
3239       if (SpecialTypes.size() != Record.size()) {
3240         Error("invalid special-types record");
3241         return Failure;
3242       }
3243 
3244       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3245         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3246         if (!SpecialTypes[I])
3247           SpecialTypes[I] = ID;
3248         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3249         // merge step?
3250       }
3251       break;
3252 
3253     case STATISTICS:
3254       TotalNumStatements += Record[0];
3255       TotalNumMacros += Record[1];
3256       TotalLexicalDeclContexts += Record[2];
3257       TotalVisibleDeclContexts += Record[3];
3258       break;
3259 
3260     case UNUSED_FILESCOPED_DECLS:
3261       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3262         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3263       break;
3264 
3265     case DELEGATING_CTORS:
3266       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3267         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3268       break;
3269 
3270     case WEAK_UNDECLARED_IDENTIFIERS:
3271       if (Record.size() % 4 != 0) {
3272         Error("invalid weak identifiers record");
3273         return Failure;
3274       }
3275 
3276       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3277       // files. This isn't the way to do it :)
3278       WeakUndeclaredIdentifiers.clear();
3279 
3280       // Translate the weak, undeclared identifiers into global IDs.
3281       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3282         WeakUndeclaredIdentifiers.push_back(
3283           getGlobalIdentifierID(F, Record[I++]));
3284         WeakUndeclaredIdentifiers.push_back(
3285           getGlobalIdentifierID(F, Record[I++]));
3286         WeakUndeclaredIdentifiers.push_back(
3287           ReadSourceLocation(F, Record, I).getRawEncoding());
3288         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3289       }
3290       break;
3291 
3292     case SELECTOR_OFFSETS: {
3293       F.SelectorOffsets = (const uint32_t *)Blob.data();
3294       F.LocalNumSelectors = Record[0];
3295       unsigned LocalBaseSelectorID = Record[1];
3296       F.BaseSelectorID = getTotalNumSelectors();
3297 
3298       if (F.LocalNumSelectors > 0) {
3299         // Introduce the global -> local mapping for selectors within this
3300         // module.
3301         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3302 
3303         // Introduce the local -> global mapping for selectors within this
3304         // module.
3305         F.SelectorRemap.insertOrReplace(
3306           std::make_pair(LocalBaseSelectorID,
3307                          F.BaseSelectorID - LocalBaseSelectorID));
3308 
3309         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3310       }
3311       break;
3312     }
3313 
3314     case METHOD_POOL:
3315       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3316       if (Record[0])
3317         F.SelectorLookupTable
3318           = ASTSelectorLookupTable::Create(
3319                         F.SelectorLookupTableData + Record[0],
3320                         F.SelectorLookupTableData,
3321                         ASTSelectorLookupTrait(*this, F));
3322       TotalNumMethodPoolEntries += Record[1];
3323       break;
3324 
3325     case REFERENCED_SELECTOR_POOL:
3326       if (!Record.empty()) {
3327         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3328           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3329                                                                 Record[Idx++]));
3330           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3331                                               getRawEncoding());
3332         }
3333       }
3334       break;
3335 
3336     case PP_CONDITIONAL_STACK:
3337       if (!Record.empty()) {
3338         unsigned Idx = 0, End = Record.size() - 1;
3339         bool ReachedEOFWhileSkipping = Record[Idx++];
3340         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3341         if (ReachedEOFWhileSkipping) {
3342           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3343           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3344           bool FoundNonSkipPortion = Record[Idx++];
3345           bool FoundElse = Record[Idx++];
3346           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3347           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3348                            FoundElse, ElseLoc);
3349         }
3350         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3351         while (Idx < End) {
3352           auto Loc = ReadSourceLocation(F, Record, Idx);
3353           bool WasSkipping = Record[Idx++];
3354           bool FoundNonSkip = Record[Idx++];
3355           bool FoundElse = Record[Idx++];
3356           ConditionalStack.push_back(
3357               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3358         }
3359         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3360       }
3361       break;
3362 
3363     case PP_COUNTER_VALUE:
3364       if (!Record.empty() && Listener)
3365         Listener->ReadCounter(F, Record[0]);
3366       break;
3367 
3368     case FILE_SORTED_DECLS:
3369       F.FileSortedDecls = (const DeclID *)Blob.data();
3370       F.NumFileSortedDecls = Record[0];
3371       break;
3372 
3373     case SOURCE_LOCATION_OFFSETS: {
3374       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3375       F.LocalNumSLocEntries = Record[0];
3376       unsigned SLocSpaceSize = Record[1];
3377       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3378           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3379                                               SLocSpaceSize);
3380       if (!F.SLocEntryBaseID) {
3381         Error("ran out of source locations");
3382         break;
3383       }
3384       // Make our entry in the range map. BaseID is negative and growing, so
3385       // we invert it. Because we invert it, though, we need the other end of
3386       // the range.
3387       unsigned RangeStart =
3388           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3389       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3390       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3391 
3392       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3393       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3394       GlobalSLocOffsetMap.insert(
3395           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3396                            - SLocSpaceSize,&F));
3397 
3398       // Initialize the remapping table.
3399       // Invalid stays invalid.
3400       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3401       // This module. Base was 2 when being compiled.
3402       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3403                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3404 
3405       TotalNumSLocEntries += F.LocalNumSLocEntries;
3406       break;
3407     }
3408 
3409     case MODULE_OFFSET_MAP:
3410       F.ModuleOffsetMap = Blob;
3411       break;
3412 
3413     case SOURCE_MANAGER_LINE_TABLE:
3414       if (ParseLineTable(F, Record)) {
3415         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3416         return Failure;
3417       }
3418       break;
3419 
3420     case SOURCE_LOCATION_PRELOADS: {
3421       // Need to transform from the local view (1-based IDs) to the global view,
3422       // which is based off F.SLocEntryBaseID.
3423       if (!F.PreloadSLocEntries.empty()) {
3424         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3425         return Failure;
3426       }
3427 
3428       F.PreloadSLocEntries.swap(Record);
3429       break;
3430     }
3431 
3432     case EXT_VECTOR_DECLS:
3433       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3434         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3435       break;
3436 
3437     case VTABLE_USES:
3438       if (Record.size() % 3 != 0) {
3439         Error("Invalid VTABLE_USES record");
3440         return Failure;
3441       }
3442 
3443       // Later tables overwrite earlier ones.
3444       // FIXME: Modules will have some trouble with this. This is clearly not
3445       // the right way to do this.
3446       VTableUses.clear();
3447 
3448       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3449         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3450         VTableUses.push_back(
3451           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3452         VTableUses.push_back(Record[Idx++]);
3453       }
3454       break;
3455 
3456     case PENDING_IMPLICIT_INSTANTIATIONS:
3457       if (PendingInstantiations.size() % 2 != 0) {
3458         Error("Invalid existing PendingInstantiations");
3459         return Failure;
3460       }
3461 
3462       if (Record.size() % 2 != 0) {
3463         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3464         return Failure;
3465       }
3466 
3467       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3468         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3469         PendingInstantiations.push_back(
3470           ReadSourceLocation(F, Record, I).getRawEncoding());
3471       }
3472       break;
3473 
3474     case SEMA_DECL_REFS:
3475       if (Record.size() != 3) {
3476         Error("Invalid SEMA_DECL_REFS block");
3477         return Failure;
3478       }
3479       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3480         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3481       break;
3482 
3483     case PPD_ENTITIES_OFFSETS: {
3484       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3485       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3486       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3487 
3488       unsigned LocalBasePreprocessedEntityID = Record[0];
3489 
3490       unsigned StartingID;
3491       if (!PP.getPreprocessingRecord())
3492         PP.createPreprocessingRecord();
3493       if (!PP.getPreprocessingRecord()->getExternalSource())
3494         PP.getPreprocessingRecord()->SetExternalSource(*this);
3495       StartingID
3496         = PP.getPreprocessingRecord()
3497             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3498       F.BasePreprocessedEntityID = StartingID;
3499 
3500       if (F.NumPreprocessedEntities > 0) {
3501         // Introduce the global -> local mapping for preprocessed entities in
3502         // this module.
3503         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3504 
3505         // Introduce the local -> global mapping for preprocessed entities in
3506         // this module.
3507         F.PreprocessedEntityRemap.insertOrReplace(
3508           std::make_pair(LocalBasePreprocessedEntityID,
3509             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3510       }
3511 
3512       break;
3513     }
3514 
3515     case PPD_SKIPPED_RANGES: {
3516       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3517       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3518       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3519 
3520       if (!PP.getPreprocessingRecord())
3521         PP.createPreprocessingRecord();
3522       if (!PP.getPreprocessingRecord()->getExternalSource())
3523         PP.getPreprocessingRecord()->SetExternalSource(*this);
3524       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3525           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3526 
3527       if (F.NumPreprocessedSkippedRanges > 0)
3528         GlobalSkippedRangeMap.insert(
3529             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3530       break;
3531     }
3532 
3533     case DECL_UPDATE_OFFSETS:
3534       if (Record.size() % 2 != 0) {
3535         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3536         return Failure;
3537       }
3538       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3539         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3540         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3541 
3542         // If we've already loaded the decl, perform the updates when we finish
3543         // loading this block.
3544         if (Decl *D = GetExistingDecl(ID))
3545           PendingUpdateRecords.push_back(
3546               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3547       }
3548       break;
3549 
3550     case OBJC_CATEGORIES_MAP:
3551       if (F.LocalNumObjCCategoriesInMap != 0) {
3552         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3553         return Failure;
3554       }
3555 
3556       F.LocalNumObjCCategoriesInMap = Record[0];
3557       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3558       break;
3559 
3560     case OBJC_CATEGORIES:
3561       F.ObjCCategories.swap(Record);
3562       break;
3563 
3564     case CUDA_SPECIAL_DECL_REFS:
3565       // Later tables overwrite earlier ones.
3566       // FIXME: Modules will have trouble with this.
3567       CUDASpecialDeclRefs.clear();
3568       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3569         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3570       break;
3571 
3572     case HEADER_SEARCH_TABLE:
3573       F.HeaderFileInfoTableData = Blob.data();
3574       F.LocalNumHeaderFileInfos = Record[1];
3575       if (Record[0]) {
3576         F.HeaderFileInfoTable
3577           = HeaderFileInfoLookupTable::Create(
3578                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3579                    (const unsigned char *)F.HeaderFileInfoTableData,
3580                    HeaderFileInfoTrait(*this, F,
3581                                        &PP.getHeaderSearchInfo(),
3582                                        Blob.data() + Record[2]));
3583 
3584         PP.getHeaderSearchInfo().SetExternalSource(this);
3585         if (!PP.getHeaderSearchInfo().getExternalLookup())
3586           PP.getHeaderSearchInfo().SetExternalLookup(this);
3587       }
3588       break;
3589 
3590     case FP_PRAGMA_OPTIONS:
3591       // Later tables overwrite earlier ones.
3592       FPPragmaOptions.swap(Record);
3593       break;
3594 
3595     case OPENCL_EXTENSIONS:
3596       for (unsigned I = 0, E = Record.size(); I != E; ) {
3597         auto Name = ReadString(Record, I);
3598         auto &Opt = OpenCLExtensions.OptMap[Name];
3599         Opt.Supported = Record[I++] != 0;
3600         Opt.Enabled = Record[I++] != 0;
3601         Opt.Avail = Record[I++];
3602         Opt.Core = Record[I++];
3603       }
3604       break;
3605 
3606     case OPENCL_EXTENSION_TYPES:
3607       for (unsigned I = 0, E = Record.size(); I != E;) {
3608         auto TypeID = static_cast<::TypeID>(Record[I++]);
3609         auto *Type = GetType(TypeID).getTypePtr();
3610         auto NumExt = static_cast<unsigned>(Record[I++]);
3611         for (unsigned II = 0; II != NumExt; ++II) {
3612           auto Ext = ReadString(Record, I);
3613           OpenCLTypeExtMap[Type].insert(Ext);
3614         }
3615       }
3616       break;
3617 
3618     case OPENCL_EXTENSION_DECLS:
3619       for (unsigned I = 0, E = Record.size(); I != E;) {
3620         auto DeclID = static_cast<::DeclID>(Record[I++]);
3621         auto *Decl = GetDecl(DeclID);
3622         auto NumExt = static_cast<unsigned>(Record[I++]);
3623         for (unsigned II = 0; II != NumExt; ++II) {
3624           auto Ext = ReadString(Record, I);
3625           OpenCLDeclExtMap[Decl].insert(Ext);
3626         }
3627       }
3628       break;
3629 
3630     case TENTATIVE_DEFINITIONS:
3631       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3632         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3633       break;
3634 
3635     case KNOWN_NAMESPACES:
3636       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3637         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3638       break;
3639 
3640     case UNDEFINED_BUT_USED:
3641       if (UndefinedButUsed.size() % 2 != 0) {
3642         Error("Invalid existing UndefinedButUsed");
3643         return Failure;
3644       }
3645 
3646       if (Record.size() % 2 != 0) {
3647         Error("invalid undefined-but-used record");
3648         return Failure;
3649       }
3650       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3651         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3652         UndefinedButUsed.push_back(
3653             ReadSourceLocation(F, Record, I).getRawEncoding());
3654       }
3655       break;
3656 
3657     case DELETE_EXPRS_TO_ANALYZE:
3658       for (unsigned I = 0, N = Record.size(); I != N;) {
3659         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3660         const uint64_t Count = Record[I++];
3661         DelayedDeleteExprs.push_back(Count);
3662         for (uint64_t C = 0; C < Count; ++C) {
3663           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3664           bool IsArrayForm = Record[I++] == 1;
3665           DelayedDeleteExprs.push_back(IsArrayForm);
3666         }
3667       }
3668       break;
3669 
3670     case IMPORTED_MODULES:
3671       if (!F.isModule()) {
3672         // If we aren't loading a module (which has its own exports), make
3673         // all of the imported modules visible.
3674         // FIXME: Deal with macros-only imports.
3675         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3676           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3677           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3678           if (GlobalID) {
3679             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3680             if (DeserializationListener)
3681               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3682           }
3683         }
3684       }
3685       break;
3686 
3687     case MACRO_OFFSET: {
3688       if (F.LocalNumMacros != 0) {
3689         Error("duplicate MACRO_OFFSET record in AST file");
3690         return Failure;
3691       }
3692       F.MacroOffsets = (const uint32_t *)Blob.data();
3693       F.LocalNumMacros = Record[0];
3694       unsigned LocalBaseMacroID = Record[1];
3695       F.BaseMacroID = getTotalNumMacros();
3696 
3697       if (F.LocalNumMacros > 0) {
3698         // Introduce the global -> local mapping for macros within this module.
3699         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3700 
3701         // Introduce the local -> global mapping for macros within this module.
3702         F.MacroRemap.insertOrReplace(
3703           std::make_pair(LocalBaseMacroID,
3704                          F.BaseMacroID - LocalBaseMacroID));
3705 
3706         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3707       }
3708       break;
3709     }
3710 
3711     case LATE_PARSED_TEMPLATE:
3712       LateParsedTemplates.append(Record.begin(), Record.end());
3713       break;
3714 
3715     case OPTIMIZE_PRAGMA_OPTIONS:
3716       if (Record.size() != 1) {
3717         Error("invalid pragma optimize record");
3718         return Failure;
3719       }
3720       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3721       break;
3722 
3723     case MSSTRUCT_PRAGMA_OPTIONS:
3724       if (Record.size() != 1) {
3725         Error("invalid pragma ms_struct record");
3726         return Failure;
3727       }
3728       PragmaMSStructState = Record[0];
3729       break;
3730 
3731     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3732       if (Record.size() != 2) {
3733         Error("invalid pragma ms_struct record");
3734         return Failure;
3735       }
3736       PragmaMSPointersToMembersState = Record[0];
3737       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3738       break;
3739 
3740     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3741       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3742         UnusedLocalTypedefNameCandidates.push_back(
3743             getGlobalDeclID(F, Record[I]));
3744       break;
3745 
3746     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3747       if (Record.size() != 1) {
3748         Error("invalid cuda pragma options record");
3749         return Failure;
3750       }
3751       ForceCUDAHostDeviceDepth = Record[0];
3752       break;
3753 
3754     case PACK_PRAGMA_OPTIONS: {
3755       if (Record.size() < 3) {
3756         Error("invalid pragma pack record");
3757         return Failure;
3758       }
3759       PragmaPackCurrentValue = Record[0];
3760       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3761       unsigned NumStackEntries = Record[2];
3762       unsigned Idx = 3;
3763       // Reset the stack when importing a new module.
3764       PragmaPackStack.clear();
3765       for (unsigned I = 0; I < NumStackEntries; ++I) {
3766         PragmaPackStackEntry Entry;
3767         Entry.Value = Record[Idx++];
3768         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3769         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3770         PragmaPackStrings.push_back(ReadString(Record, Idx));
3771         Entry.SlotLabel = PragmaPackStrings.back();
3772         PragmaPackStack.push_back(Entry);
3773       }
3774       break;
3775     }
3776     }
3777   }
3778 }
3779 
3780 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3781   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3782 
3783   // Additional remapping information.
3784   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3785   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3786   F.ModuleOffsetMap = StringRef();
3787 
3788   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3789   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3790     F.SLocRemap.insert(std::make_pair(0U, 0));
3791     F.SLocRemap.insert(std::make_pair(2U, 1));
3792   }
3793 
3794   // Continuous range maps we may be updating in our module.
3795   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3796   RemapBuilder SLocRemap(F.SLocRemap);
3797   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3798   RemapBuilder MacroRemap(F.MacroRemap);
3799   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3800   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3801   RemapBuilder SelectorRemap(F.SelectorRemap);
3802   RemapBuilder DeclRemap(F.DeclRemap);
3803   RemapBuilder TypeRemap(F.TypeRemap);
3804 
3805   while (Data < DataEnd) {
3806     // FIXME: Looking up dependency modules by filename is horrible. Let's
3807     // start fixing this with prebuilt and explicit modules and see how it
3808     // goes...
3809     using namespace llvm::support;
3810     ModuleKind Kind = static_cast<ModuleKind>(
3811       endian::readNext<uint8_t, little, unaligned>(Data));
3812     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3813     StringRef Name = StringRef((const char*)Data, Len);
3814     Data += Len;
3815     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3816                       ? ModuleMgr.lookupByModuleName(Name)
3817                       : ModuleMgr.lookupByFileName(Name));
3818     if (!OM) {
3819       std::string Msg =
3820           "SourceLocation remap refers to unknown module, cannot find ";
3821       Msg.append(std::string(Name));
3822       Error(Msg);
3823       return;
3824     }
3825 
3826     uint32_t SLocOffset =
3827         endian::readNext<uint32_t, little, unaligned>(Data);
3828     uint32_t IdentifierIDOffset =
3829         endian::readNext<uint32_t, little, unaligned>(Data);
3830     uint32_t MacroIDOffset =
3831         endian::readNext<uint32_t, little, unaligned>(Data);
3832     uint32_t PreprocessedEntityIDOffset =
3833         endian::readNext<uint32_t, little, unaligned>(Data);
3834     uint32_t SubmoduleIDOffset =
3835         endian::readNext<uint32_t, little, unaligned>(Data);
3836     uint32_t SelectorIDOffset =
3837         endian::readNext<uint32_t, little, unaligned>(Data);
3838     uint32_t DeclIDOffset =
3839         endian::readNext<uint32_t, little, unaligned>(Data);
3840     uint32_t TypeIndexOffset =
3841         endian::readNext<uint32_t, little, unaligned>(Data);
3842 
3843     uint32_t None = std::numeric_limits<uint32_t>::max();
3844 
3845     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3846                          RemapBuilder &Remap) {
3847       if (Offset != None)
3848         Remap.insert(std::make_pair(Offset,
3849                                     static_cast<int>(BaseOffset - Offset)));
3850     };
3851     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3852     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3853     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3854     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3855               PreprocessedEntityRemap);
3856     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3857     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3858     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3859     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3860 
3861     // Global -> local mappings.
3862     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3863   }
3864 }
3865 
3866 ASTReader::ASTReadResult
3867 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3868                                   const ModuleFile *ImportedBy,
3869                                   unsigned ClientLoadCapabilities) {
3870   unsigned Idx = 0;
3871   F.ModuleMapPath = ReadPath(F, Record, Idx);
3872 
3873   // Try to resolve ModuleName in the current header search context and
3874   // verify that it is found in the same module map file as we saved. If the
3875   // top-level AST file is a main file, skip this check because there is no
3876   // usable header search context.
3877   assert(!F.ModuleName.empty() &&
3878          "MODULE_NAME should come before MODULE_MAP_FILE");
3879   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3880     // An implicitly-loaded module file should have its module listed in some
3881     // module map file that we've already loaded.
3882     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3883     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3884     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3885     // Don't emit module relocation error if we have -fno-validate-pch
3886     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3887       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3888         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3889           // This module was defined by an imported (explicit) module.
3890           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3891                                                << ASTFE->getName();
3892         } else {
3893           // This module was built with a different module map.
3894           Diag(diag::err_imported_module_not_found)
3895               << F.ModuleName << F.FileName
3896               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3897               << !ImportedBy;
3898           // In case it was imported by a PCH, there's a chance the user is
3899           // just missing to include the search path to the directory containing
3900           // the modulemap.
3901           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3902             Diag(diag::note_imported_by_pch_module_not_found)
3903                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3904         }
3905       }
3906       return OutOfDate;
3907     }
3908 
3909     assert(M->Name == F.ModuleName && "found module with different name");
3910 
3911     // Check the primary module map file.
3912     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3913     if (!StoredModMap || *StoredModMap != ModMap) {
3914       assert(ModMap && "found module is missing module map file");
3915       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3916              "top-level import should be verified");
3917       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3918       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3919         Diag(diag::err_imported_module_modmap_changed)
3920             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3921             << ModMap->getName() << F.ModuleMapPath << NotImported;
3922       return OutOfDate;
3923     }
3924 
3925     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3926     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3927       // FIXME: we should use input files rather than storing names.
3928       std::string Filename = ReadPath(F, Record, Idx);
3929       auto F = FileMgr.getFile(Filename, false, false);
3930       if (!F) {
3931         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3932           Error("could not find file '" + Filename +"' referenced by AST file");
3933         return OutOfDate;
3934       }
3935       AdditionalStoredMaps.insert(*F);
3936     }
3937 
3938     // Check any additional module map files (e.g. module.private.modulemap)
3939     // that are not in the pcm.
3940     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3941       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3942         // Remove files that match
3943         // Note: SmallPtrSet::erase is really remove
3944         if (!AdditionalStoredMaps.erase(ModMap)) {
3945           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3946             Diag(diag::err_module_different_modmap)
3947               << F.ModuleName << /*new*/0 << ModMap->getName();
3948           return OutOfDate;
3949         }
3950       }
3951     }
3952 
3953     // Check any additional module map files that are in the pcm, but not
3954     // found in header search. Cases that match are already removed.
3955     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3956       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3957         Diag(diag::err_module_different_modmap)
3958           << F.ModuleName << /*not new*/1 << ModMap->getName();
3959       return OutOfDate;
3960     }
3961   }
3962 
3963   if (Listener)
3964     Listener->ReadModuleMapFile(F.ModuleMapPath);
3965   return Success;
3966 }
3967 
3968 /// Move the given method to the back of the global list of methods.
3969 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3970   // Find the entry for this selector in the method pool.
3971   Sema::GlobalMethodPool::iterator Known
3972     = S.MethodPool.find(Method->getSelector());
3973   if (Known == S.MethodPool.end())
3974     return;
3975 
3976   // Retrieve the appropriate method list.
3977   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3978                                                     : Known->second.second;
3979   bool Found = false;
3980   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3981     if (!Found) {
3982       if (List->getMethod() == Method) {
3983         Found = true;
3984       } else {
3985         // Keep searching.
3986         continue;
3987       }
3988     }
3989 
3990     if (List->getNext())
3991       List->setMethod(List->getNext()->getMethod());
3992     else
3993       List->setMethod(Method);
3994   }
3995 }
3996 
3997 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3998   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3999   for (Decl *D : Names) {
4000     bool wasHidden = D->isHidden();
4001     D->setVisibleDespiteOwningModule();
4002 
4003     if (wasHidden && SemaObj) {
4004       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4005         moveMethodToBackOfGlobalList(*SemaObj, Method);
4006       }
4007     }
4008   }
4009 }
4010 
4011 void ASTReader::makeModuleVisible(Module *Mod,
4012                                   Module::NameVisibilityKind NameVisibility,
4013                                   SourceLocation ImportLoc) {
4014   llvm::SmallPtrSet<Module *, 4> Visited;
4015   SmallVector<Module *, 4> Stack;
4016   Stack.push_back(Mod);
4017   while (!Stack.empty()) {
4018     Mod = Stack.pop_back_val();
4019 
4020     if (NameVisibility <= Mod->NameVisibility) {
4021       // This module already has this level of visibility (or greater), so
4022       // there is nothing more to do.
4023       continue;
4024     }
4025 
4026     if (!Mod->isAvailable()) {
4027       // Modules that aren't available cannot be made visible.
4028       continue;
4029     }
4030 
4031     // Update the module's name visibility.
4032     Mod->NameVisibility = NameVisibility;
4033 
4034     // If we've already deserialized any names from this module,
4035     // mark them as visible.
4036     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4037     if (Hidden != HiddenNamesMap.end()) {
4038       auto HiddenNames = std::move(*Hidden);
4039       HiddenNamesMap.erase(Hidden);
4040       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4041       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4042              "making names visible added hidden names");
4043     }
4044 
4045     // Push any exported modules onto the stack to be marked as visible.
4046     SmallVector<Module *, 16> Exports;
4047     Mod->getExportedModules(Exports);
4048     for (SmallVectorImpl<Module *>::iterator
4049            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4050       Module *Exported = *I;
4051       if (Visited.insert(Exported).second)
4052         Stack.push_back(Exported);
4053     }
4054   }
4055 }
4056 
4057 /// We've merged the definition \p MergedDef into the existing definition
4058 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4059 /// visible.
4060 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4061                                           NamedDecl *MergedDef) {
4062   if (Def->isHidden()) {
4063     // If MergedDef is visible or becomes visible, make the definition visible.
4064     if (!MergedDef->isHidden())
4065       Def->setVisibleDespiteOwningModule();
4066     else {
4067       getContext().mergeDefinitionIntoModule(
4068           Def, MergedDef->getImportedOwningModule(),
4069           /*NotifyListeners*/ false);
4070       PendingMergedDefinitionsToDeduplicate.insert(Def);
4071     }
4072   }
4073 }
4074 
4075 bool ASTReader::loadGlobalIndex() {
4076   if (GlobalIndex)
4077     return false;
4078 
4079   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4080       !PP.getLangOpts().Modules)
4081     return true;
4082 
4083   // Try to load the global index.
4084   TriedLoadingGlobalIndex = true;
4085   StringRef ModuleCachePath
4086     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4087   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4088       GlobalModuleIndex::readIndex(ModuleCachePath);
4089   if (llvm::Error Err = std::move(Result.second)) {
4090     assert(!Result.first);
4091     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4092     return true;
4093   }
4094 
4095   GlobalIndex.reset(Result.first);
4096   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4097   return false;
4098 }
4099 
4100 bool ASTReader::isGlobalIndexUnavailable() const {
4101   return PP.getLangOpts().Modules && UseGlobalIndex &&
4102          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4103 }
4104 
4105 static void updateModuleTimestamp(ModuleFile &MF) {
4106   // Overwrite the timestamp file contents so that file's mtime changes.
4107   std::string TimestampFilename = MF.getTimestampFilename();
4108   std::error_code EC;
4109   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4110   if (EC)
4111     return;
4112   OS << "Timestamp file\n";
4113   OS.close();
4114   OS.clear_error(); // Avoid triggering a fatal error.
4115 }
4116 
4117 /// Given a cursor at the start of an AST file, scan ahead and drop the
4118 /// cursor into the start of the given block ID, returning false on success and
4119 /// true on failure.
4120 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4121   while (true) {
4122     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4123     if (!MaybeEntry) {
4124       // FIXME this drops errors on the floor.
4125       consumeError(MaybeEntry.takeError());
4126       return true;
4127     }
4128     llvm::BitstreamEntry Entry = MaybeEntry.get();
4129 
4130     switch (Entry.Kind) {
4131     case llvm::BitstreamEntry::Error:
4132     case llvm::BitstreamEntry::EndBlock:
4133       return true;
4134 
4135     case llvm::BitstreamEntry::Record:
4136       // Ignore top-level records.
4137       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4138         break;
4139       else {
4140         // FIXME this drops errors on the floor.
4141         consumeError(Skipped.takeError());
4142         return true;
4143       }
4144 
4145     case llvm::BitstreamEntry::SubBlock:
4146       if (Entry.ID == BlockID) {
4147         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4148           // FIXME this drops the error on the floor.
4149           consumeError(std::move(Err));
4150           return true;
4151         }
4152         // Found it!
4153         return false;
4154       }
4155 
4156       if (llvm::Error Err = Cursor.SkipBlock()) {
4157         // FIXME this drops the error on the floor.
4158         consumeError(std::move(Err));
4159         return true;
4160       }
4161     }
4162   }
4163 }
4164 
4165 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4166                                             ModuleKind Type,
4167                                             SourceLocation ImportLoc,
4168                                             unsigned ClientLoadCapabilities,
4169                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4170   llvm::SaveAndRestore<SourceLocation>
4171     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4172 
4173   // Defer any pending actions until we get to the end of reading the AST file.
4174   Deserializing AnASTFile(this);
4175 
4176   // Bump the generation number.
4177   unsigned PreviousGeneration = 0;
4178   if (ContextObj)
4179     PreviousGeneration = incrementGeneration(*ContextObj);
4180 
4181   unsigned NumModules = ModuleMgr.size();
4182   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4183     assert(ReadResult && "expected to return error");
4184     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4185                             PP.getLangOpts().Modules
4186                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4187                                 : nullptr);
4188 
4189     // If we find that any modules are unusable, the global index is going
4190     // to be out-of-date. Just remove it.
4191     GlobalIndex.reset();
4192     ModuleMgr.setGlobalIndex(nullptr);
4193     return ReadResult;
4194   };
4195 
4196   SmallVector<ImportedModule, 4> Loaded;
4197   switch (ASTReadResult ReadResult =
4198               ReadASTCore(FileName, Type, ImportLoc,
4199                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4200                           ASTFileSignature(), ClientLoadCapabilities)) {
4201   case Failure:
4202   case Missing:
4203   case OutOfDate:
4204   case VersionMismatch:
4205   case ConfigurationMismatch:
4206   case HadErrors:
4207     return removeModulesAndReturn(ReadResult);
4208   case Success:
4209     break;
4210   }
4211 
4212   // Here comes stuff that we only do once the entire chain is loaded.
4213 
4214   // Load the AST blocks of all of the modules that we loaded.  We can still
4215   // hit errors parsing the ASTs at this point.
4216   for (ImportedModule &M : Loaded) {
4217     ModuleFile &F = *M.Mod;
4218 
4219     // Read the AST block.
4220     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4221       return removeModulesAndReturn(Result);
4222 
4223     // The AST block should always have a definition for the main module.
4224     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4225       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4226       return removeModulesAndReturn(Failure);
4227     }
4228 
4229     // Read the extension blocks.
4230     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4231       if (ASTReadResult Result = ReadExtensionBlock(F))
4232         return removeModulesAndReturn(Result);
4233     }
4234 
4235     // Once read, set the ModuleFile bit base offset and update the size in
4236     // bits of all files we've seen.
4237     F.GlobalBitOffset = TotalModulesSizeInBits;
4238     TotalModulesSizeInBits += F.SizeInBits;
4239     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4240   }
4241 
4242   // Preload source locations and interesting indentifiers.
4243   for (ImportedModule &M : Loaded) {
4244     ModuleFile &F = *M.Mod;
4245 
4246     // Preload SLocEntries.
4247     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4248       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4249       // Load it through the SourceManager and don't call ReadSLocEntry()
4250       // directly because the entry may have already been loaded in which case
4251       // calling ReadSLocEntry() directly would trigger an assertion in
4252       // SourceManager.
4253       SourceMgr.getLoadedSLocEntryByID(Index);
4254     }
4255 
4256     // Map the original source file ID into the ID space of the current
4257     // compilation.
4258     if (F.OriginalSourceFileID.isValid()) {
4259       F.OriginalSourceFileID = FileID::get(
4260           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4261     }
4262 
4263     // Preload all the pending interesting identifiers by marking them out of
4264     // date.
4265     for (auto Offset : F.PreloadIdentifierOffsets) {
4266       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4267           F.IdentifierTableData + Offset);
4268 
4269       ASTIdentifierLookupTrait Trait(*this, F);
4270       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4271       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4272       auto &II = PP.getIdentifierTable().getOwn(Key);
4273       II.setOutOfDate(true);
4274 
4275       // Mark this identifier as being from an AST file so that we can track
4276       // whether we need to serialize it.
4277       markIdentifierFromAST(*this, II);
4278 
4279       // Associate the ID with the identifier so that the writer can reuse it.
4280       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4281       SetIdentifierInfo(ID, &II);
4282     }
4283   }
4284 
4285   // Setup the import locations and notify the module manager that we've
4286   // committed to these module files.
4287   for (ImportedModule &M : Loaded) {
4288     ModuleFile &F = *M.Mod;
4289 
4290     ModuleMgr.moduleFileAccepted(&F);
4291 
4292     // Set the import location.
4293     F.DirectImportLoc = ImportLoc;
4294     // FIXME: We assume that locations from PCH / preamble do not need
4295     // any translation.
4296     if (!M.ImportedBy)
4297       F.ImportLoc = M.ImportLoc;
4298     else
4299       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4300   }
4301 
4302   if (!PP.getLangOpts().CPlusPlus ||
4303       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4304        Type != MK_PrebuiltModule)) {
4305     // Mark all of the identifiers in the identifier table as being out of date,
4306     // so that various accessors know to check the loaded modules when the
4307     // identifier is used.
4308     //
4309     // For C++ modules, we don't need information on many identifiers (just
4310     // those that provide macros or are poisoned), so we mark all of
4311     // the interesting ones via PreloadIdentifierOffsets.
4312     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4313                                 IdEnd = PP.getIdentifierTable().end();
4314          Id != IdEnd; ++Id)
4315       Id->second->setOutOfDate(true);
4316   }
4317   // Mark selectors as out of date.
4318   for (auto Sel : SelectorGeneration)
4319     SelectorOutOfDate[Sel.first] = true;
4320 
4321   // Resolve any unresolved module exports.
4322   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4323     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4324     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4325     Module *ResolvedMod = getSubmodule(GlobalID);
4326 
4327     switch (Unresolved.Kind) {
4328     case UnresolvedModuleRef::Conflict:
4329       if (ResolvedMod) {
4330         Module::Conflict Conflict;
4331         Conflict.Other = ResolvedMod;
4332         Conflict.Message = Unresolved.String.str();
4333         Unresolved.Mod->Conflicts.push_back(Conflict);
4334       }
4335       continue;
4336 
4337     case UnresolvedModuleRef::Import:
4338       if (ResolvedMod)
4339         Unresolved.Mod->Imports.insert(ResolvedMod);
4340       continue;
4341 
4342     case UnresolvedModuleRef::Export:
4343       if (ResolvedMod || Unresolved.IsWildcard)
4344         Unresolved.Mod->Exports.push_back(
4345           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4346       continue;
4347     }
4348   }
4349   UnresolvedModuleRefs.clear();
4350 
4351   if (Imported)
4352     Imported->append(ImportedModules.begin(),
4353                      ImportedModules.end());
4354 
4355   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4356   // Might be unnecessary as use declarations are only used to build the
4357   // module itself.
4358 
4359   if (ContextObj)
4360     InitializeContext();
4361 
4362   if (SemaObj)
4363     UpdateSema();
4364 
4365   if (DeserializationListener)
4366     DeserializationListener->ReaderInitialized(this);
4367 
4368   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4369   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4370     // If this AST file is a precompiled preamble, then set the
4371     // preamble file ID of the source manager to the file source file
4372     // from which the preamble was built.
4373     if (Type == MK_Preamble) {
4374       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4375     } else if (Type == MK_MainFile) {
4376       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4377     }
4378   }
4379 
4380   // For any Objective-C class definitions we have already loaded, make sure
4381   // that we load any additional categories.
4382   if (ContextObj) {
4383     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4384       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4385                          ObjCClassesLoaded[I],
4386                          PreviousGeneration);
4387     }
4388   }
4389 
4390   if (PP.getHeaderSearchInfo()
4391           .getHeaderSearchOpts()
4392           .ModulesValidateOncePerBuildSession) {
4393     // Now we are certain that the module and all modules it depends on are
4394     // up to date.  Create or update timestamp files for modules that are
4395     // located in the module cache (not for PCH files that could be anywhere
4396     // in the filesystem).
4397     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4398       ImportedModule &M = Loaded[I];
4399       if (M.Mod->Kind == MK_ImplicitModule) {
4400         updateModuleTimestamp(*M.Mod);
4401       }
4402     }
4403   }
4404 
4405   return Success;
4406 }
4407 
4408 static ASTFileSignature readASTFileSignature(StringRef PCH);
4409 
4410 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4411 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4412   // FIXME checking magic headers is done in other places such as
4413   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4414   // always done the same. Unify it all with a helper.
4415   if (!Stream.canSkipToPos(4))
4416     return llvm::createStringError(std::errc::illegal_byte_sequence,
4417                                    "file too small to contain AST file magic");
4418   for (unsigned C : {'C', 'P', 'C', 'H'})
4419     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4420       if (Res.get() != C)
4421         return llvm::createStringError(
4422             std::errc::illegal_byte_sequence,
4423             "file doesn't start with AST file magic");
4424     } else
4425       return Res.takeError();
4426   return llvm::Error::success();
4427 }
4428 
4429 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4430   switch (Kind) {
4431   case MK_PCH:
4432     return 0; // PCH
4433   case MK_ImplicitModule:
4434   case MK_ExplicitModule:
4435   case MK_PrebuiltModule:
4436     return 1; // module
4437   case MK_MainFile:
4438   case MK_Preamble:
4439     return 2; // main source file
4440   }
4441   llvm_unreachable("unknown module kind");
4442 }
4443 
4444 ASTReader::ASTReadResult
4445 ASTReader::ReadASTCore(StringRef FileName,
4446                        ModuleKind Type,
4447                        SourceLocation ImportLoc,
4448                        ModuleFile *ImportedBy,
4449                        SmallVectorImpl<ImportedModule> &Loaded,
4450                        off_t ExpectedSize, time_t ExpectedModTime,
4451                        ASTFileSignature ExpectedSignature,
4452                        unsigned ClientLoadCapabilities) {
4453   ModuleFile *M;
4454   std::string ErrorStr;
4455   ModuleManager::AddModuleResult AddResult
4456     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4457                           getGeneration(), ExpectedSize, ExpectedModTime,
4458                           ExpectedSignature, readASTFileSignature,
4459                           M, ErrorStr);
4460 
4461   switch (AddResult) {
4462   case ModuleManager::AlreadyLoaded:
4463     Diag(diag::remark_module_import)
4464         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4465         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4466     return Success;
4467 
4468   case ModuleManager::NewlyLoaded:
4469     // Load module file below.
4470     break;
4471 
4472   case ModuleManager::Missing:
4473     // The module file was missing; if the client can handle that, return
4474     // it.
4475     if (ClientLoadCapabilities & ARR_Missing)
4476       return Missing;
4477 
4478     // Otherwise, return an error.
4479     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4480                                           << FileName << !ErrorStr.empty()
4481                                           << ErrorStr;
4482     return Failure;
4483 
4484   case ModuleManager::OutOfDate:
4485     // We couldn't load the module file because it is out-of-date. If the
4486     // client can handle out-of-date, return it.
4487     if (ClientLoadCapabilities & ARR_OutOfDate)
4488       return OutOfDate;
4489 
4490     // Otherwise, return an error.
4491     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4492                                             << FileName << !ErrorStr.empty()
4493                                             << ErrorStr;
4494     return Failure;
4495   }
4496 
4497   assert(M && "Missing module file");
4498 
4499   bool ShouldFinalizePCM = false;
4500   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4501     auto &MC = getModuleManager().getModuleCache();
4502     if (ShouldFinalizePCM)
4503       MC.finalizePCM(FileName);
4504     else
4505       MC.tryToDropPCM(FileName);
4506   });
4507   ModuleFile &F = *M;
4508   BitstreamCursor &Stream = F.Stream;
4509   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4510   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4511 
4512   // Sniff for the signature.
4513   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4514     Diag(diag::err_module_file_invalid)
4515         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4516     return Failure;
4517   }
4518 
4519   // This is used for compatibility with older PCH formats.
4520   bool HaveReadControlBlock = false;
4521   while (true) {
4522     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4523     if (!MaybeEntry) {
4524       Error(MaybeEntry.takeError());
4525       return Failure;
4526     }
4527     llvm::BitstreamEntry Entry = MaybeEntry.get();
4528 
4529     switch (Entry.Kind) {
4530     case llvm::BitstreamEntry::Error:
4531     case llvm::BitstreamEntry::Record:
4532     case llvm::BitstreamEntry::EndBlock:
4533       Error("invalid record at top-level of AST file");
4534       return Failure;
4535 
4536     case llvm::BitstreamEntry::SubBlock:
4537       break;
4538     }
4539 
4540     switch (Entry.ID) {
4541     case CONTROL_BLOCK_ID:
4542       HaveReadControlBlock = true;
4543       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4544       case Success:
4545         // Check that we didn't try to load a non-module AST file as a module.
4546         //
4547         // FIXME: Should we also perform the converse check? Loading a module as
4548         // a PCH file sort of works, but it's a bit wonky.
4549         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4550              Type == MK_PrebuiltModule) &&
4551             F.ModuleName.empty()) {
4552           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4553           if (Result != OutOfDate ||
4554               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4555             Diag(diag::err_module_file_not_module) << FileName;
4556           return Result;
4557         }
4558         break;
4559 
4560       case Failure: return Failure;
4561       case Missing: return Missing;
4562       case OutOfDate: return OutOfDate;
4563       case VersionMismatch: return VersionMismatch;
4564       case ConfigurationMismatch: return ConfigurationMismatch;
4565       case HadErrors: return HadErrors;
4566       }
4567       break;
4568 
4569     case AST_BLOCK_ID:
4570       if (!HaveReadControlBlock) {
4571         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4572           Diag(diag::err_pch_version_too_old);
4573         return VersionMismatch;
4574       }
4575 
4576       // Record that we've loaded this module.
4577       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4578       ShouldFinalizePCM = true;
4579       return Success;
4580 
4581     case UNHASHED_CONTROL_BLOCK_ID:
4582       // This block is handled using look-ahead during ReadControlBlock.  We
4583       // shouldn't get here!
4584       Error("malformed block record in AST file");
4585       return Failure;
4586 
4587     default:
4588       if (llvm::Error Err = Stream.SkipBlock()) {
4589         Error(std::move(Err));
4590         return Failure;
4591       }
4592       break;
4593     }
4594   }
4595 
4596   llvm_unreachable("unexpected break; expected return");
4597 }
4598 
4599 ASTReader::ASTReadResult
4600 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4601                                     unsigned ClientLoadCapabilities) {
4602   const HeaderSearchOptions &HSOpts =
4603       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4604   bool AllowCompatibleConfigurationMismatch =
4605       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4606 
4607   ASTReadResult Result = readUnhashedControlBlockImpl(
4608       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4609       Listener.get(),
4610       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4611 
4612   // If F was directly imported by another module, it's implicitly validated by
4613   // the importing module.
4614   if (DisableValidation || WasImportedBy ||
4615       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4616     return Success;
4617 
4618   if (Result == Failure) {
4619     Error("malformed block record in AST file");
4620     return Failure;
4621   }
4622 
4623   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4624     // If this module has already been finalized in the ModuleCache, we're stuck
4625     // with it; we can only load a single version of each module.
4626     //
4627     // This can happen when a module is imported in two contexts: in one, as a
4628     // user module; in another, as a system module (due to an import from
4629     // another module marked with the [system] flag).  It usually indicates a
4630     // bug in the module map: this module should also be marked with [system].
4631     //
4632     // If -Wno-system-headers (the default), and the first import is as a
4633     // system module, then validation will fail during the as-user import,
4634     // since -Werror flags won't have been validated.  However, it's reasonable
4635     // to treat this consistently as a system module.
4636     //
4637     // If -Wsystem-headers, the PCM on disk was built with
4638     // -Wno-system-headers, and the first import is as a user module, then
4639     // validation will fail during the as-system import since the PCM on disk
4640     // doesn't guarantee that -Werror was respected.  However, the -Werror
4641     // flags were checked during the initial as-user import.
4642     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4643       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4644       return Success;
4645     }
4646   }
4647 
4648   return Result;
4649 }
4650 
4651 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4652     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4653     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4654     bool ValidateDiagnosticOptions) {
4655   // Initialize a stream.
4656   BitstreamCursor Stream(StreamData);
4657 
4658   // Sniff for the signature.
4659   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4660     // FIXME this drops the error on the floor.
4661     consumeError(std::move(Err));
4662     return Failure;
4663   }
4664 
4665   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4666   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4667     return Failure;
4668 
4669   // Read all of the records in the options block.
4670   RecordData Record;
4671   ASTReadResult Result = Success;
4672   while (true) {
4673     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4674     if (!MaybeEntry) {
4675       // FIXME this drops the error on the floor.
4676       consumeError(MaybeEntry.takeError());
4677       return Failure;
4678     }
4679     llvm::BitstreamEntry Entry = MaybeEntry.get();
4680 
4681     switch (Entry.Kind) {
4682     case llvm::BitstreamEntry::Error:
4683     case llvm::BitstreamEntry::SubBlock:
4684       return Failure;
4685 
4686     case llvm::BitstreamEntry::EndBlock:
4687       return Result;
4688 
4689     case llvm::BitstreamEntry::Record:
4690       // The interesting case.
4691       break;
4692     }
4693 
4694     // Read and process a record.
4695     Record.clear();
4696     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4697     if (!MaybeRecordType) {
4698       // FIXME this drops the error.
4699       return Failure;
4700     }
4701     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4702     case SIGNATURE:
4703       if (F)
4704         std::copy(Record.begin(), Record.end(), F->Signature.data());
4705       break;
4706     case DIAGNOSTIC_OPTIONS: {
4707       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4708       if (Listener && ValidateDiagnosticOptions &&
4709           !AllowCompatibleConfigurationMismatch &&
4710           ParseDiagnosticOptions(Record, Complain, *Listener))
4711         Result = OutOfDate; // Don't return early.  Read the signature.
4712       break;
4713     }
4714     case DIAG_PRAGMA_MAPPINGS:
4715       if (!F)
4716         break;
4717       if (F->PragmaDiagMappings.empty())
4718         F->PragmaDiagMappings.swap(Record);
4719       else
4720         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4721                                      Record.begin(), Record.end());
4722       break;
4723     }
4724   }
4725 }
4726 
4727 /// Parse a record and blob containing module file extension metadata.
4728 static bool parseModuleFileExtensionMetadata(
4729               const SmallVectorImpl<uint64_t> &Record,
4730               StringRef Blob,
4731               ModuleFileExtensionMetadata &Metadata) {
4732   if (Record.size() < 4) return true;
4733 
4734   Metadata.MajorVersion = Record[0];
4735   Metadata.MinorVersion = Record[1];
4736 
4737   unsigned BlockNameLen = Record[2];
4738   unsigned UserInfoLen = Record[3];
4739 
4740   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4741 
4742   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4743   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4744                                   Blob.data() + BlockNameLen + UserInfoLen);
4745   return false;
4746 }
4747 
4748 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4749   BitstreamCursor &Stream = F.Stream;
4750 
4751   RecordData Record;
4752   while (true) {
4753     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4754     if (!MaybeEntry) {
4755       Error(MaybeEntry.takeError());
4756       return Failure;
4757     }
4758     llvm::BitstreamEntry Entry = MaybeEntry.get();
4759 
4760     switch (Entry.Kind) {
4761     case llvm::BitstreamEntry::SubBlock:
4762       if (llvm::Error Err = Stream.SkipBlock()) {
4763         Error(std::move(Err));
4764         return Failure;
4765       }
4766       continue;
4767 
4768     case llvm::BitstreamEntry::EndBlock:
4769       return Success;
4770 
4771     case llvm::BitstreamEntry::Error:
4772       return HadErrors;
4773 
4774     case llvm::BitstreamEntry::Record:
4775       break;
4776     }
4777 
4778     Record.clear();
4779     StringRef Blob;
4780     Expected<unsigned> MaybeRecCode =
4781         Stream.readRecord(Entry.ID, Record, &Blob);
4782     if (!MaybeRecCode) {
4783       Error(MaybeRecCode.takeError());
4784       return Failure;
4785     }
4786     switch (MaybeRecCode.get()) {
4787     case EXTENSION_METADATA: {
4788       ModuleFileExtensionMetadata Metadata;
4789       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4790         Error("malformed EXTENSION_METADATA in AST file");
4791         return Failure;
4792       }
4793 
4794       // Find a module file extension with this block name.
4795       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4796       if (Known == ModuleFileExtensions.end()) break;
4797 
4798       // Form a reader.
4799       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4800                                                              F, Stream)) {
4801         F.ExtensionReaders.push_back(std::move(Reader));
4802       }
4803 
4804       break;
4805     }
4806     }
4807   }
4808 
4809   return Success;
4810 }
4811 
4812 void ASTReader::InitializeContext() {
4813   assert(ContextObj && "no context to initialize");
4814   ASTContext &Context = *ContextObj;
4815 
4816   // If there's a listener, notify them that we "read" the translation unit.
4817   if (DeserializationListener)
4818     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4819                                       Context.getTranslationUnitDecl());
4820 
4821   // FIXME: Find a better way to deal with collisions between these
4822   // built-in types. Right now, we just ignore the problem.
4823 
4824   // Load the special types.
4825   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4826     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4827       if (!Context.CFConstantStringTypeDecl)
4828         Context.setCFConstantStringType(GetType(String));
4829     }
4830 
4831     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4832       QualType FileType = GetType(File);
4833       if (FileType.isNull()) {
4834         Error("FILE type is NULL");
4835         return;
4836       }
4837 
4838       if (!Context.FILEDecl) {
4839         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4840           Context.setFILEDecl(Typedef->getDecl());
4841         else {
4842           const TagType *Tag = FileType->getAs<TagType>();
4843           if (!Tag) {
4844             Error("Invalid FILE type in AST file");
4845             return;
4846           }
4847           Context.setFILEDecl(Tag->getDecl());
4848         }
4849       }
4850     }
4851 
4852     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4853       QualType Jmp_bufType = GetType(Jmp_buf);
4854       if (Jmp_bufType.isNull()) {
4855         Error("jmp_buf type is NULL");
4856         return;
4857       }
4858 
4859       if (!Context.jmp_bufDecl) {
4860         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4861           Context.setjmp_bufDecl(Typedef->getDecl());
4862         else {
4863           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4864           if (!Tag) {
4865             Error("Invalid jmp_buf type in AST file");
4866             return;
4867           }
4868           Context.setjmp_bufDecl(Tag->getDecl());
4869         }
4870       }
4871     }
4872 
4873     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4874       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4875       if (Sigjmp_bufType.isNull()) {
4876         Error("sigjmp_buf type is NULL");
4877         return;
4878       }
4879 
4880       if (!Context.sigjmp_bufDecl) {
4881         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4882           Context.setsigjmp_bufDecl(Typedef->getDecl());
4883         else {
4884           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4885           assert(Tag && "Invalid sigjmp_buf type in AST file");
4886           Context.setsigjmp_bufDecl(Tag->getDecl());
4887         }
4888       }
4889     }
4890 
4891     if (unsigned ObjCIdRedef
4892           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4893       if (Context.ObjCIdRedefinitionType.isNull())
4894         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4895     }
4896 
4897     if (unsigned ObjCClassRedef
4898           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4899       if (Context.ObjCClassRedefinitionType.isNull())
4900         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4901     }
4902 
4903     if (unsigned ObjCSelRedef
4904           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4905       if (Context.ObjCSelRedefinitionType.isNull())
4906         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4907     }
4908 
4909     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4910       QualType Ucontext_tType = GetType(Ucontext_t);
4911       if (Ucontext_tType.isNull()) {
4912         Error("ucontext_t type is NULL");
4913         return;
4914       }
4915 
4916       if (!Context.ucontext_tDecl) {
4917         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4918           Context.setucontext_tDecl(Typedef->getDecl());
4919         else {
4920           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4921           assert(Tag && "Invalid ucontext_t type in AST file");
4922           Context.setucontext_tDecl(Tag->getDecl());
4923         }
4924       }
4925     }
4926   }
4927 
4928   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4929 
4930   // If there were any CUDA special declarations, deserialize them.
4931   if (!CUDASpecialDeclRefs.empty()) {
4932     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4933     Context.setcudaConfigureCallDecl(
4934                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4935   }
4936 
4937   // Re-export any modules that were imported by a non-module AST file.
4938   // FIXME: This does not make macro-only imports visible again.
4939   for (auto &Import : ImportedModules) {
4940     if (Module *Imported = getSubmodule(Import.ID)) {
4941       makeModuleVisible(Imported, Module::AllVisible,
4942                         /*ImportLoc=*/Import.ImportLoc);
4943       if (Import.ImportLoc.isValid())
4944         PP.makeModuleVisible(Imported, Import.ImportLoc);
4945       // FIXME: should we tell Sema to make the module visible too?
4946     }
4947   }
4948   ImportedModules.clear();
4949 }
4950 
4951 void ASTReader::finalizeForWriting() {
4952   // Nothing to do for now.
4953 }
4954 
4955 /// Reads and return the signature record from \p PCH's control block, or
4956 /// else returns 0.
4957 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4958   BitstreamCursor Stream(PCH);
4959   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4960     // FIXME this drops the error on the floor.
4961     consumeError(std::move(Err));
4962     return ASTFileSignature();
4963   }
4964 
4965   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4966   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4967     return ASTFileSignature();
4968 
4969   // Scan for SIGNATURE inside the diagnostic options block.
4970   ASTReader::RecordData Record;
4971   while (true) {
4972     Expected<llvm::BitstreamEntry> MaybeEntry =
4973         Stream.advanceSkippingSubblocks();
4974     if (!MaybeEntry) {
4975       // FIXME this drops the error on the floor.
4976       consumeError(MaybeEntry.takeError());
4977       return ASTFileSignature();
4978     }
4979     llvm::BitstreamEntry Entry = MaybeEntry.get();
4980 
4981     if (Entry.Kind != llvm::BitstreamEntry::Record)
4982       return ASTFileSignature();
4983 
4984     Record.clear();
4985     StringRef Blob;
4986     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4987     if (!MaybeRecord) {
4988       // FIXME this drops the error on the floor.
4989       consumeError(MaybeRecord.takeError());
4990       return ASTFileSignature();
4991     }
4992     if (SIGNATURE == MaybeRecord.get())
4993       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4994                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4995   }
4996 }
4997 
4998 /// Retrieve the name of the original source file name
4999 /// directly from the AST file, without actually loading the AST
5000 /// file.
5001 std::string ASTReader::getOriginalSourceFile(
5002     const std::string &ASTFileName, FileManager &FileMgr,
5003     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5004   // Open the AST file.
5005   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5006   if (!Buffer) {
5007     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5008         << ASTFileName << Buffer.getError().message();
5009     return std::string();
5010   }
5011 
5012   // Initialize the stream
5013   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5014 
5015   // Sniff for the signature.
5016   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5017     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5018     return std::string();
5019   }
5020 
5021   // Scan for the CONTROL_BLOCK_ID block.
5022   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5023     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5024     return std::string();
5025   }
5026 
5027   // Scan for ORIGINAL_FILE inside the control block.
5028   RecordData Record;
5029   while (true) {
5030     Expected<llvm::BitstreamEntry> MaybeEntry =
5031         Stream.advanceSkippingSubblocks();
5032     if (!MaybeEntry) {
5033       // FIXME this drops errors on the floor.
5034       consumeError(MaybeEntry.takeError());
5035       return std::string();
5036     }
5037     llvm::BitstreamEntry Entry = MaybeEntry.get();
5038 
5039     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5040       return std::string();
5041 
5042     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5043       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5044       return std::string();
5045     }
5046 
5047     Record.clear();
5048     StringRef Blob;
5049     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5050     if (!MaybeRecord) {
5051       // FIXME this drops the errors on the floor.
5052       consumeError(MaybeRecord.takeError());
5053       return std::string();
5054     }
5055     if (ORIGINAL_FILE == MaybeRecord.get())
5056       return Blob.str();
5057   }
5058 }
5059 
5060 namespace {
5061 
5062   class SimplePCHValidator : public ASTReaderListener {
5063     const LangOptions &ExistingLangOpts;
5064     const TargetOptions &ExistingTargetOpts;
5065     const PreprocessorOptions &ExistingPPOpts;
5066     std::string ExistingModuleCachePath;
5067     FileManager &FileMgr;
5068 
5069   public:
5070     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5071                        const TargetOptions &ExistingTargetOpts,
5072                        const PreprocessorOptions &ExistingPPOpts,
5073                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5074         : ExistingLangOpts(ExistingLangOpts),
5075           ExistingTargetOpts(ExistingTargetOpts),
5076           ExistingPPOpts(ExistingPPOpts),
5077           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5078 
5079     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5080                              bool AllowCompatibleDifferences) override {
5081       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5082                                   AllowCompatibleDifferences);
5083     }
5084 
5085     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5086                            bool AllowCompatibleDifferences) override {
5087       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5088                                 AllowCompatibleDifferences);
5089     }
5090 
5091     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5092                                  StringRef SpecificModuleCachePath,
5093                                  bool Complain) override {
5094       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5095                                       ExistingModuleCachePath,
5096                                       nullptr, ExistingLangOpts);
5097     }
5098 
5099     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5100                                  bool Complain,
5101                                  std::string &SuggestedPredefines) override {
5102       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5103                                       SuggestedPredefines, ExistingLangOpts);
5104     }
5105   };
5106 
5107 } // namespace
5108 
5109 bool ASTReader::readASTFileControlBlock(
5110     StringRef Filename, FileManager &FileMgr,
5111     const PCHContainerReader &PCHContainerRdr,
5112     bool FindModuleFileExtensions,
5113     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5114   // Open the AST file.
5115   // FIXME: This allows use of the VFS; we do not allow use of the
5116   // VFS when actually loading a module.
5117   auto Buffer = FileMgr.getBufferForFile(Filename);
5118   if (!Buffer) {
5119     return true;
5120   }
5121 
5122   // Initialize the stream
5123   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5124   BitstreamCursor Stream(Bytes);
5125 
5126   // Sniff for the signature.
5127   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5128     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5129     return true;
5130   }
5131 
5132   // Scan for the CONTROL_BLOCK_ID block.
5133   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5134     return true;
5135 
5136   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5137   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5138   bool NeedsImports = Listener.needsImportVisitation();
5139   BitstreamCursor InputFilesCursor;
5140 
5141   RecordData Record;
5142   std::string ModuleDir;
5143   bool DoneWithControlBlock = false;
5144   while (!DoneWithControlBlock) {
5145     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5146     if (!MaybeEntry) {
5147       // FIXME this drops the error on the floor.
5148       consumeError(MaybeEntry.takeError());
5149       return true;
5150     }
5151     llvm::BitstreamEntry Entry = MaybeEntry.get();
5152 
5153     switch (Entry.Kind) {
5154     case llvm::BitstreamEntry::SubBlock: {
5155       switch (Entry.ID) {
5156       case OPTIONS_BLOCK_ID: {
5157         std::string IgnoredSuggestedPredefines;
5158         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5159                              /*AllowCompatibleConfigurationMismatch*/ false,
5160                              Listener, IgnoredSuggestedPredefines) != Success)
5161           return true;
5162         break;
5163       }
5164 
5165       case INPUT_FILES_BLOCK_ID:
5166         InputFilesCursor = Stream;
5167         if (llvm::Error Err = Stream.SkipBlock()) {
5168           // FIXME this drops the error on the floor.
5169           consumeError(std::move(Err));
5170           return true;
5171         }
5172         if (NeedsInputFiles &&
5173             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5174           return true;
5175         break;
5176 
5177       default:
5178         if (llvm::Error Err = Stream.SkipBlock()) {
5179           // FIXME this drops the error on the floor.
5180           consumeError(std::move(Err));
5181           return true;
5182         }
5183         break;
5184       }
5185 
5186       continue;
5187     }
5188 
5189     case llvm::BitstreamEntry::EndBlock:
5190       DoneWithControlBlock = true;
5191       break;
5192 
5193     case llvm::BitstreamEntry::Error:
5194       return true;
5195 
5196     case llvm::BitstreamEntry::Record:
5197       break;
5198     }
5199 
5200     if (DoneWithControlBlock) break;
5201 
5202     Record.clear();
5203     StringRef Blob;
5204     Expected<unsigned> MaybeRecCode =
5205         Stream.readRecord(Entry.ID, Record, &Blob);
5206     if (!MaybeRecCode) {
5207       // FIXME this drops the error.
5208       return Failure;
5209     }
5210     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5211     case METADATA:
5212       if (Record[0] != VERSION_MAJOR)
5213         return true;
5214       if (Listener.ReadFullVersionInformation(Blob))
5215         return true;
5216       break;
5217     case MODULE_NAME:
5218       Listener.ReadModuleName(Blob);
5219       break;
5220     case MODULE_DIRECTORY:
5221       ModuleDir = std::string(Blob);
5222       break;
5223     case MODULE_MAP_FILE: {
5224       unsigned Idx = 0;
5225       auto Path = ReadString(Record, Idx);
5226       ResolveImportedPath(Path, ModuleDir);
5227       Listener.ReadModuleMapFile(Path);
5228       break;
5229     }
5230     case INPUT_FILE_OFFSETS: {
5231       if (!NeedsInputFiles)
5232         break;
5233 
5234       unsigned NumInputFiles = Record[0];
5235       unsigned NumUserFiles = Record[1];
5236       const llvm::support::unaligned_uint64_t *InputFileOffs =
5237           (const llvm::support::unaligned_uint64_t *)Blob.data();
5238       for (unsigned I = 0; I != NumInputFiles; ++I) {
5239         // Go find this input file.
5240         bool isSystemFile = I >= NumUserFiles;
5241 
5242         if (isSystemFile && !NeedsSystemInputFiles)
5243           break; // the rest are system input files
5244 
5245         BitstreamCursor &Cursor = InputFilesCursor;
5246         SavedStreamPosition SavedPosition(Cursor);
5247         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5248           // FIXME this drops errors on the floor.
5249           consumeError(std::move(Err));
5250         }
5251 
5252         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5253         if (!MaybeCode) {
5254           // FIXME this drops errors on the floor.
5255           consumeError(MaybeCode.takeError());
5256         }
5257         unsigned Code = MaybeCode.get();
5258 
5259         RecordData Record;
5260         StringRef Blob;
5261         bool shouldContinue = false;
5262         Expected<unsigned> MaybeRecordType =
5263             Cursor.readRecord(Code, Record, &Blob);
5264         if (!MaybeRecordType) {
5265           // FIXME this drops errors on the floor.
5266           consumeError(MaybeRecordType.takeError());
5267         }
5268         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5269         case INPUT_FILE_HASH:
5270           break;
5271         case INPUT_FILE:
5272           bool Overridden = static_cast<bool>(Record[3]);
5273           std::string Filename = std::string(Blob);
5274           ResolveImportedPath(Filename, ModuleDir);
5275           shouldContinue = Listener.visitInputFile(
5276               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5277           break;
5278         }
5279         if (!shouldContinue)
5280           break;
5281       }
5282       break;
5283     }
5284 
5285     case IMPORTS: {
5286       if (!NeedsImports)
5287         break;
5288 
5289       unsigned Idx = 0, N = Record.size();
5290       while (Idx < N) {
5291         // Read information about the AST file.
5292         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5293         std::string ModuleName = ReadString(Record, Idx);
5294         std::string Filename = ReadString(Record, Idx);
5295         ResolveImportedPath(Filename, ModuleDir);
5296         Listener.visitImport(ModuleName, Filename);
5297       }
5298       break;
5299     }
5300 
5301     default:
5302       // No other validation to perform.
5303       break;
5304     }
5305   }
5306 
5307   // Look for module file extension blocks, if requested.
5308   if (FindModuleFileExtensions) {
5309     BitstreamCursor SavedStream = Stream;
5310     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5311       bool DoneWithExtensionBlock = false;
5312       while (!DoneWithExtensionBlock) {
5313         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5314         if (!MaybeEntry) {
5315           // FIXME this drops the error.
5316           return true;
5317         }
5318         llvm::BitstreamEntry Entry = MaybeEntry.get();
5319 
5320         switch (Entry.Kind) {
5321         case llvm::BitstreamEntry::SubBlock:
5322           if (llvm::Error Err = Stream.SkipBlock()) {
5323             // FIXME this drops the error on the floor.
5324             consumeError(std::move(Err));
5325             return true;
5326           }
5327           continue;
5328 
5329         case llvm::BitstreamEntry::EndBlock:
5330           DoneWithExtensionBlock = true;
5331           continue;
5332 
5333         case llvm::BitstreamEntry::Error:
5334           return true;
5335 
5336         case llvm::BitstreamEntry::Record:
5337           break;
5338         }
5339 
5340        Record.clear();
5341        StringRef Blob;
5342        Expected<unsigned> MaybeRecCode =
5343            Stream.readRecord(Entry.ID, Record, &Blob);
5344        if (!MaybeRecCode) {
5345          // FIXME this drops the error.
5346          return true;
5347        }
5348        switch (MaybeRecCode.get()) {
5349        case EXTENSION_METADATA: {
5350          ModuleFileExtensionMetadata Metadata;
5351          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5352            return true;
5353 
5354          Listener.readModuleFileExtension(Metadata);
5355          break;
5356        }
5357        }
5358       }
5359     }
5360     Stream = SavedStream;
5361   }
5362 
5363   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5364   if (readUnhashedControlBlockImpl(
5365           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5366           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5367           ValidateDiagnosticOptions) != Success)
5368     return true;
5369 
5370   return false;
5371 }
5372 
5373 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5374                                     const PCHContainerReader &PCHContainerRdr,
5375                                     const LangOptions &LangOpts,
5376                                     const TargetOptions &TargetOpts,
5377                                     const PreprocessorOptions &PPOpts,
5378                                     StringRef ExistingModuleCachePath) {
5379   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5380                                ExistingModuleCachePath, FileMgr);
5381   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5382                                   /*FindModuleFileExtensions=*/false,
5383                                   validator,
5384                                   /*ValidateDiagnosticOptions=*/true);
5385 }
5386 
5387 ASTReader::ASTReadResult
5388 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5389   // Enter the submodule block.
5390   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5391     Error(std::move(Err));
5392     return Failure;
5393   }
5394 
5395   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5396   bool First = true;
5397   Module *CurrentModule = nullptr;
5398   RecordData Record;
5399   while (true) {
5400     Expected<llvm::BitstreamEntry> MaybeEntry =
5401         F.Stream.advanceSkippingSubblocks();
5402     if (!MaybeEntry) {
5403       Error(MaybeEntry.takeError());
5404       return Failure;
5405     }
5406     llvm::BitstreamEntry Entry = MaybeEntry.get();
5407 
5408     switch (Entry.Kind) {
5409     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5410     case llvm::BitstreamEntry::Error:
5411       Error("malformed block record in AST file");
5412       return Failure;
5413     case llvm::BitstreamEntry::EndBlock:
5414       return Success;
5415     case llvm::BitstreamEntry::Record:
5416       // The interesting case.
5417       break;
5418     }
5419 
5420     // Read a record.
5421     StringRef Blob;
5422     Record.clear();
5423     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5424     if (!MaybeKind) {
5425       Error(MaybeKind.takeError());
5426       return Failure;
5427     }
5428     unsigned Kind = MaybeKind.get();
5429 
5430     if ((Kind == SUBMODULE_METADATA) != First) {
5431       Error("submodule metadata record should be at beginning of block");
5432       return Failure;
5433     }
5434     First = false;
5435 
5436     // Submodule information is only valid if we have a current module.
5437     // FIXME: Should we error on these cases?
5438     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5439         Kind != SUBMODULE_DEFINITION)
5440       continue;
5441 
5442     switch (Kind) {
5443     default:  // Default behavior: ignore.
5444       break;
5445 
5446     case SUBMODULE_DEFINITION: {
5447       if (Record.size() < 12) {
5448         Error("malformed module definition");
5449         return Failure;
5450       }
5451 
5452       StringRef Name = Blob;
5453       unsigned Idx = 0;
5454       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5455       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5456       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5457       bool IsFramework = Record[Idx++];
5458       bool IsExplicit = Record[Idx++];
5459       bool IsSystem = Record[Idx++];
5460       bool IsExternC = Record[Idx++];
5461       bool InferSubmodules = Record[Idx++];
5462       bool InferExplicitSubmodules = Record[Idx++];
5463       bool InferExportWildcard = Record[Idx++];
5464       bool ConfigMacrosExhaustive = Record[Idx++];
5465       bool ModuleMapIsPrivate = Record[Idx++];
5466 
5467       Module *ParentModule = nullptr;
5468       if (Parent)
5469         ParentModule = getSubmodule(Parent);
5470 
5471       // Retrieve this (sub)module from the module map, creating it if
5472       // necessary.
5473       CurrentModule =
5474           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5475               .first;
5476 
5477       // FIXME: set the definition loc for CurrentModule, or call
5478       // ModMap.setInferredModuleAllowedBy()
5479 
5480       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5481       if (GlobalIndex >= SubmodulesLoaded.size() ||
5482           SubmodulesLoaded[GlobalIndex]) {
5483         Error("too many submodules");
5484         return Failure;
5485       }
5486 
5487       if (!ParentModule) {
5488         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5489           // Don't emit module relocation error if we have -fno-validate-pch
5490           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5491               CurFile != F.File) {
5492             Error(diag::err_module_file_conflict,
5493                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5494                   F.File->getName());
5495             return Failure;
5496           }
5497         }
5498 
5499         F.DidReadTopLevelSubmodule = true;
5500         CurrentModule->setASTFile(F.File);
5501         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5502       }
5503 
5504       CurrentModule->Kind = Kind;
5505       CurrentModule->Signature = F.Signature;
5506       CurrentModule->IsFromModuleFile = true;
5507       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5508       CurrentModule->IsExternC = IsExternC;
5509       CurrentModule->InferSubmodules = InferSubmodules;
5510       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5511       CurrentModule->InferExportWildcard = InferExportWildcard;
5512       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5513       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5514       if (DeserializationListener)
5515         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5516 
5517       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5518 
5519       // Clear out data that will be replaced by what is in the module file.
5520       CurrentModule->LinkLibraries.clear();
5521       CurrentModule->ConfigMacros.clear();
5522       CurrentModule->UnresolvedConflicts.clear();
5523       CurrentModule->Conflicts.clear();
5524 
5525       // The module is available unless it's missing a requirement; relevant
5526       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5527       // Missing headers that were present when the module was built do not
5528       // make it unavailable -- if we got this far, this must be an explicitly
5529       // imported module file.
5530       CurrentModule->Requirements.clear();
5531       CurrentModule->MissingHeaders.clear();
5532       CurrentModule->IsMissingRequirement =
5533           ParentModule && ParentModule->IsMissingRequirement;
5534       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5535       break;
5536     }
5537 
5538     case SUBMODULE_UMBRELLA_HEADER: {
5539       std::string Filename = std::string(Blob);
5540       ResolveImportedPath(F, Filename);
5541       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5542         if (!CurrentModule->getUmbrellaHeader())
5543           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5544         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5545           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5546             Error("mismatched umbrella headers in submodule");
5547           return OutOfDate;
5548         }
5549       }
5550       break;
5551     }
5552 
5553     case SUBMODULE_HEADER:
5554     case SUBMODULE_EXCLUDED_HEADER:
5555     case SUBMODULE_PRIVATE_HEADER:
5556       // We lazily associate headers with their modules via the HeaderInfo table.
5557       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5558       // of complete filenames or remove it entirely.
5559       break;
5560 
5561     case SUBMODULE_TEXTUAL_HEADER:
5562     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5563       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5564       // them here.
5565       break;
5566 
5567     case SUBMODULE_TOPHEADER:
5568       CurrentModule->addTopHeaderFilename(Blob);
5569       break;
5570 
5571     case SUBMODULE_UMBRELLA_DIR: {
5572       std::string Dirname = std::string(Blob);
5573       ResolveImportedPath(F, Dirname);
5574       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5575         if (!CurrentModule->getUmbrellaDir())
5576           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5577         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5578           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5579             Error("mismatched umbrella directories in submodule");
5580           return OutOfDate;
5581         }
5582       }
5583       break;
5584     }
5585 
5586     case SUBMODULE_METADATA: {
5587       F.BaseSubmoduleID = getTotalNumSubmodules();
5588       F.LocalNumSubmodules = Record[0];
5589       unsigned LocalBaseSubmoduleID = Record[1];
5590       if (F.LocalNumSubmodules > 0) {
5591         // Introduce the global -> local mapping for submodules within this
5592         // module.
5593         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5594 
5595         // Introduce the local -> global mapping for submodules within this
5596         // module.
5597         F.SubmoduleRemap.insertOrReplace(
5598           std::make_pair(LocalBaseSubmoduleID,
5599                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5600 
5601         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5602       }
5603       break;
5604     }
5605 
5606     case SUBMODULE_IMPORTS:
5607       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5608         UnresolvedModuleRef Unresolved;
5609         Unresolved.File = &F;
5610         Unresolved.Mod = CurrentModule;
5611         Unresolved.ID = Record[Idx];
5612         Unresolved.Kind = UnresolvedModuleRef::Import;
5613         Unresolved.IsWildcard = false;
5614         UnresolvedModuleRefs.push_back(Unresolved);
5615       }
5616       break;
5617 
5618     case SUBMODULE_EXPORTS:
5619       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5620         UnresolvedModuleRef Unresolved;
5621         Unresolved.File = &F;
5622         Unresolved.Mod = CurrentModule;
5623         Unresolved.ID = Record[Idx];
5624         Unresolved.Kind = UnresolvedModuleRef::Export;
5625         Unresolved.IsWildcard = Record[Idx + 1];
5626         UnresolvedModuleRefs.push_back(Unresolved);
5627       }
5628 
5629       // Once we've loaded the set of exports, there's no reason to keep
5630       // the parsed, unresolved exports around.
5631       CurrentModule->UnresolvedExports.clear();
5632       break;
5633 
5634     case SUBMODULE_REQUIRES:
5635       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5636                                     PP.getTargetInfo());
5637       break;
5638 
5639     case SUBMODULE_LINK_LIBRARY:
5640       ModMap.resolveLinkAsDependencies(CurrentModule);
5641       CurrentModule->LinkLibraries.push_back(
5642           Module::LinkLibrary(std::string(Blob), Record[0]));
5643       break;
5644 
5645     case SUBMODULE_CONFIG_MACRO:
5646       CurrentModule->ConfigMacros.push_back(Blob.str());
5647       break;
5648 
5649     case SUBMODULE_CONFLICT: {
5650       UnresolvedModuleRef Unresolved;
5651       Unresolved.File = &F;
5652       Unresolved.Mod = CurrentModule;
5653       Unresolved.ID = Record[0];
5654       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5655       Unresolved.IsWildcard = false;
5656       Unresolved.String = Blob;
5657       UnresolvedModuleRefs.push_back(Unresolved);
5658       break;
5659     }
5660 
5661     case SUBMODULE_INITIALIZERS: {
5662       if (!ContextObj)
5663         break;
5664       SmallVector<uint32_t, 16> Inits;
5665       for (auto &ID : Record)
5666         Inits.push_back(getGlobalDeclID(F, ID));
5667       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5668       break;
5669     }
5670 
5671     case SUBMODULE_EXPORT_AS:
5672       CurrentModule->ExportAsModule = Blob.str();
5673       ModMap.addLinkAsDependency(CurrentModule);
5674       break;
5675     }
5676   }
5677 }
5678 
5679 /// Parse the record that corresponds to a LangOptions data
5680 /// structure.
5681 ///
5682 /// This routine parses the language options from the AST file and then gives
5683 /// them to the AST listener if one is set.
5684 ///
5685 /// \returns true if the listener deems the file unacceptable, false otherwise.
5686 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5687                                      bool Complain,
5688                                      ASTReaderListener &Listener,
5689                                      bool AllowCompatibleDifferences) {
5690   LangOptions LangOpts;
5691   unsigned Idx = 0;
5692 #define LANGOPT(Name, Bits, Default, Description) \
5693   LangOpts.Name = Record[Idx++];
5694 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5695   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5696 #include "clang/Basic/LangOptions.def"
5697 #define SANITIZER(NAME, ID)                                                    \
5698   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5699 #include "clang/Basic/Sanitizers.def"
5700 
5701   for (unsigned N = Record[Idx++]; N; --N)
5702     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5703 
5704   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5705   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5706   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5707 
5708   LangOpts.CurrentModule = ReadString(Record, Idx);
5709 
5710   // Comment options.
5711   for (unsigned N = Record[Idx++]; N; --N) {
5712     LangOpts.CommentOpts.BlockCommandNames.push_back(
5713       ReadString(Record, Idx));
5714   }
5715   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5716 
5717   // OpenMP offloading options.
5718   for (unsigned N = Record[Idx++]; N; --N) {
5719     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5720   }
5721 
5722   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5723 
5724   return Listener.ReadLanguageOptions(LangOpts, Complain,
5725                                       AllowCompatibleDifferences);
5726 }
5727 
5728 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5729                                    ASTReaderListener &Listener,
5730                                    bool AllowCompatibleDifferences) {
5731   unsigned Idx = 0;
5732   TargetOptions TargetOpts;
5733   TargetOpts.Triple = ReadString(Record, Idx);
5734   TargetOpts.CPU = ReadString(Record, Idx);
5735   TargetOpts.ABI = ReadString(Record, Idx);
5736   for (unsigned N = Record[Idx++]; N; --N) {
5737     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5738   }
5739   for (unsigned N = Record[Idx++]; N; --N) {
5740     TargetOpts.Features.push_back(ReadString(Record, Idx));
5741   }
5742 
5743   return Listener.ReadTargetOptions(TargetOpts, Complain,
5744                                     AllowCompatibleDifferences);
5745 }
5746 
5747 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5748                                        ASTReaderListener &Listener) {
5749   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5750   unsigned Idx = 0;
5751 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5752 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5753   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5754 #include "clang/Basic/DiagnosticOptions.def"
5755 
5756   for (unsigned N = Record[Idx++]; N; --N)
5757     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5758   for (unsigned N = Record[Idx++]; N; --N)
5759     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5760 
5761   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5762 }
5763 
5764 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5765                                        ASTReaderListener &Listener) {
5766   FileSystemOptions FSOpts;
5767   unsigned Idx = 0;
5768   FSOpts.WorkingDir = ReadString(Record, Idx);
5769   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5770 }
5771 
5772 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5773                                          bool Complain,
5774                                          ASTReaderListener &Listener) {
5775   HeaderSearchOptions HSOpts;
5776   unsigned Idx = 0;
5777   HSOpts.Sysroot = ReadString(Record, Idx);
5778 
5779   // Include entries.
5780   for (unsigned N = Record[Idx++]; N; --N) {
5781     std::string Path = ReadString(Record, Idx);
5782     frontend::IncludeDirGroup Group
5783       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5784     bool IsFramework = Record[Idx++];
5785     bool IgnoreSysRoot = Record[Idx++];
5786     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5787                                     IgnoreSysRoot);
5788   }
5789 
5790   // System header prefixes.
5791   for (unsigned N = Record[Idx++]; N; --N) {
5792     std::string Prefix = ReadString(Record, Idx);
5793     bool IsSystemHeader = Record[Idx++];
5794     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5795   }
5796 
5797   HSOpts.ResourceDir = ReadString(Record, Idx);
5798   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5799   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5800   HSOpts.DisableModuleHash = Record[Idx++];
5801   HSOpts.ImplicitModuleMaps = Record[Idx++];
5802   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5803   HSOpts.UseBuiltinIncludes = Record[Idx++];
5804   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5805   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5806   HSOpts.UseLibcxx = Record[Idx++];
5807   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5808 
5809   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5810                                           Complain);
5811 }
5812 
5813 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5814                                          bool Complain,
5815                                          ASTReaderListener &Listener,
5816                                          std::string &SuggestedPredefines) {
5817   PreprocessorOptions PPOpts;
5818   unsigned Idx = 0;
5819 
5820   // Macro definitions/undefs
5821   for (unsigned N = Record[Idx++]; N; --N) {
5822     std::string Macro = ReadString(Record, Idx);
5823     bool IsUndef = Record[Idx++];
5824     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5825   }
5826 
5827   // Includes
5828   for (unsigned N = Record[Idx++]; N; --N) {
5829     PPOpts.Includes.push_back(ReadString(Record, Idx));
5830   }
5831 
5832   // Macro Includes
5833   for (unsigned N = Record[Idx++]; N; --N) {
5834     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5835   }
5836 
5837   PPOpts.UsePredefines = Record[Idx++];
5838   PPOpts.DetailedRecord = Record[Idx++];
5839   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5840   PPOpts.ObjCXXARCStandardLibrary =
5841     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5842   SuggestedPredefines.clear();
5843   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5844                                           SuggestedPredefines);
5845 }
5846 
5847 std::pair<ModuleFile *, unsigned>
5848 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5849   GlobalPreprocessedEntityMapType::iterator
5850   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5851   assert(I != GlobalPreprocessedEntityMap.end() &&
5852          "Corrupted global preprocessed entity map");
5853   ModuleFile *M = I->second;
5854   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5855   return std::make_pair(M, LocalIndex);
5856 }
5857 
5858 llvm::iterator_range<PreprocessingRecord::iterator>
5859 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5860   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5861     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5862                                              Mod.NumPreprocessedEntities);
5863 
5864   return llvm::make_range(PreprocessingRecord::iterator(),
5865                           PreprocessingRecord::iterator());
5866 }
5867 
5868 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5869 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5870   return llvm::make_range(
5871       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5872       ModuleDeclIterator(this, &Mod,
5873                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5874 }
5875 
5876 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5877   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5878   assert(I != GlobalSkippedRangeMap.end() &&
5879     "Corrupted global skipped range map");
5880   ModuleFile *M = I->second;
5881   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5882   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5883   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5884   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5885                     TranslateSourceLocation(*M, RawRange.getEnd()));
5886   assert(Range.isValid());
5887   return Range;
5888 }
5889 
5890 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5891   PreprocessedEntityID PPID = Index+1;
5892   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5893   ModuleFile &M = *PPInfo.first;
5894   unsigned LocalIndex = PPInfo.second;
5895   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5896 
5897   if (!PP.getPreprocessingRecord()) {
5898     Error("no preprocessing record");
5899     return nullptr;
5900   }
5901 
5902   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5903   if (llvm::Error Err =
5904           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5905     Error(std::move(Err));
5906     return nullptr;
5907   }
5908 
5909   Expected<llvm::BitstreamEntry> MaybeEntry =
5910       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5911   if (!MaybeEntry) {
5912     Error(MaybeEntry.takeError());
5913     return nullptr;
5914   }
5915   llvm::BitstreamEntry Entry = MaybeEntry.get();
5916 
5917   if (Entry.Kind != llvm::BitstreamEntry::Record)
5918     return nullptr;
5919 
5920   // Read the record.
5921   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5922                     TranslateSourceLocation(M, PPOffs.getEnd()));
5923   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5924   StringRef Blob;
5925   RecordData Record;
5926   Expected<unsigned> MaybeRecType =
5927       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5928   if (!MaybeRecType) {
5929     Error(MaybeRecType.takeError());
5930     return nullptr;
5931   }
5932   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5933   case PPD_MACRO_EXPANSION: {
5934     bool isBuiltin = Record[0];
5935     IdentifierInfo *Name = nullptr;
5936     MacroDefinitionRecord *Def = nullptr;
5937     if (isBuiltin)
5938       Name = getLocalIdentifier(M, Record[1]);
5939     else {
5940       PreprocessedEntityID GlobalID =
5941           getGlobalPreprocessedEntityID(M, Record[1]);
5942       Def = cast<MacroDefinitionRecord>(
5943           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5944     }
5945 
5946     MacroExpansion *ME;
5947     if (isBuiltin)
5948       ME = new (PPRec) MacroExpansion(Name, Range);
5949     else
5950       ME = new (PPRec) MacroExpansion(Def, Range);
5951 
5952     return ME;
5953   }
5954 
5955   case PPD_MACRO_DEFINITION: {
5956     // Decode the identifier info and then check again; if the macro is
5957     // still defined and associated with the identifier,
5958     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5959     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5960 
5961     if (DeserializationListener)
5962       DeserializationListener->MacroDefinitionRead(PPID, MD);
5963 
5964     return MD;
5965   }
5966 
5967   case PPD_INCLUSION_DIRECTIVE: {
5968     const char *FullFileNameStart = Blob.data() + Record[0];
5969     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5970     const FileEntry *File = nullptr;
5971     if (!FullFileName.empty())
5972       if (auto FE = PP.getFileManager().getFile(FullFileName))
5973         File = *FE;
5974 
5975     // FIXME: Stable encoding
5976     InclusionDirective::InclusionKind Kind
5977       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5978     InclusionDirective *ID
5979       = new (PPRec) InclusionDirective(PPRec, Kind,
5980                                        StringRef(Blob.data(), Record[0]),
5981                                        Record[1], Record[3],
5982                                        File,
5983                                        Range);
5984     return ID;
5985   }
5986   }
5987 
5988   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5989 }
5990 
5991 /// Find the next module that contains entities and return the ID
5992 /// of the first entry.
5993 ///
5994 /// \param SLocMapI points at a chunk of a module that contains no
5995 /// preprocessed entities or the entities it contains are not the ones we are
5996 /// looking for.
5997 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5998                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5999   ++SLocMapI;
6000   for (GlobalSLocOffsetMapType::const_iterator
6001          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6002     ModuleFile &M = *SLocMapI->second;
6003     if (M.NumPreprocessedEntities)
6004       return M.BasePreprocessedEntityID;
6005   }
6006 
6007   return getTotalNumPreprocessedEntities();
6008 }
6009 
6010 namespace {
6011 
6012 struct PPEntityComp {
6013   const ASTReader &Reader;
6014   ModuleFile &M;
6015 
6016   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6017 
6018   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6019     SourceLocation LHS = getLoc(L);
6020     SourceLocation RHS = getLoc(R);
6021     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6022   }
6023 
6024   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6025     SourceLocation LHS = getLoc(L);
6026     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6027   }
6028 
6029   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6030     SourceLocation RHS = getLoc(R);
6031     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6032   }
6033 
6034   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6035     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6036   }
6037 };
6038 
6039 } // namespace
6040 
6041 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6042                                                        bool EndsAfter) const {
6043   if (SourceMgr.isLocalSourceLocation(Loc))
6044     return getTotalNumPreprocessedEntities();
6045 
6046   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6047       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6048   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6049          "Corrupted global sloc offset map");
6050 
6051   if (SLocMapI->second->NumPreprocessedEntities == 0)
6052     return findNextPreprocessedEntity(SLocMapI);
6053 
6054   ModuleFile &M = *SLocMapI->second;
6055 
6056   using pp_iterator = const PPEntityOffset *;
6057 
6058   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6059   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6060 
6061   size_t Count = M.NumPreprocessedEntities;
6062   size_t Half;
6063   pp_iterator First = pp_begin;
6064   pp_iterator PPI;
6065 
6066   if (EndsAfter) {
6067     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6068                            PPEntityComp(*this, M));
6069   } else {
6070     // Do a binary search manually instead of using std::lower_bound because
6071     // The end locations of entities may be unordered (when a macro expansion
6072     // is inside another macro argument), but for this case it is not important
6073     // whether we get the first macro expansion or its containing macro.
6074     while (Count > 0) {
6075       Half = Count / 2;
6076       PPI = First;
6077       std::advance(PPI, Half);
6078       if (SourceMgr.isBeforeInTranslationUnit(
6079               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6080         First = PPI;
6081         ++First;
6082         Count = Count - Half - 1;
6083       } else
6084         Count = Half;
6085     }
6086   }
6087 
6088   if (PPI == pp_end)
6089     return findNextPreprocessedEntity(SLocMapI);
6090 
6091   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6092 }
6093 
6094 /// Returns a pair of [Begin, End) indices of preallocated
6095 /// preprocessed entities that \arg Range encompasses.
6096 std::pair<unsigned, unsigned>
6097     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6098   if (Range.isInvalid())
6099     return std::make_pair(0,0);
6100   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6101 
6102   PreprocessedEntityID BeginID =
6103       findPreprocessedEntity(Range.getBegin(), false);
6104   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6105   return std::make_pair(BeginID, EndID);
6106 }
6107 
6108 /// Optionally returns true or false if the preallocated preprocessed
6109 /// entity with index \arg Index came from file \arg FID.
6110 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6111                                                              FileID FID) {
6112   if (FID.isInvalid())
6113     return false;
6114 
6115   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6116   ModuleFile &M = *PPInfo.first;
6117   unsigned LocalIndex = PPInfo.second;
6118   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6119 
6120   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6121   if (Loc.isInvalid())
6122     return false;
6123 
6124   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6125     return true;
6126   else
6127     return false;
6128 }
6129 
6130 namespace {
6131 
6132   /// Visitor used to search for information about a header file.
6133   class HeaderFileInfoVisitor {
6134     const FileEntry *FE;
6135     Optional<HeaderFileInfo> HFI;
6136 
6137   public:
6138     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6139 
6140     bool operator()(ModuleFile &M) {
6141       HeaderFileInfoLookupTable *Table
6142         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6143       if (!Table)
6144         return false;
6145 
6146       // Look in the on-disk hash table for an entry for this file name.
6147       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6148       if (Pos == Table->end())
6149         return false;
6150 
6151       HFI = *Pos;
6152       return true;
6153     }
6154 
6155     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6156   };
6157 
6158 } // namespace
6159 
6160 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6161   HeaderFileInfoVisitor Visitor(FE);
6162   ModuleMgr.visit(Visitor);
6163   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6164     return *HFI;
6165 
6166   return HeaderFileInfo();
6167 }
6168 
6169 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6170   using DiagState = DiagnosticsEngine::DiagState;
6171   SmallVector<DiagState *, 32> DiagStates;
6172 
6173   for (ModuleFile &F : ModuleMgr) {
6174     unsigned Idx = 0;
6175     auto &Record = F.PragmaDiagMappings;
6176     if (Record.empty())
6177       continue;
6178 
6179     DiagStates.clear();
6180 
6181     auto ReadDiagState =
6182         [&](const DiagState &BasedOn, SourceLocation Loc,
6183             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6184       unsigned BackrefID = Record[Idx++];
6185       if (BackrefID != 0)
6186         return DiagStates[BackrefID - 1];
6187 
6188       // A new DiagState was created here.
6189       Diag.DiagStates.push_back(BasedOn);
6190       DiagState *NewState = &Diag.DiagStates.back();
6191       DiagStates.push_back(NewState);
6192       unsigned Size = Record[Idx++];
6193       assert(Idx + Size * 2 <= Record.size() &&
6194              "Invalid data, not enough diag/map pairs");
6195       while (Size--) {
6196         unsigned DiagID = Record[Idx++];
6197         DiagnosticMapping NewMapping =
6198             DiagnosticMapping::deserialize(Record[Idx++]);
6199         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6200           continue;
6201 
6202         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6203 
6204         // If this mapping was specified as a warning but the severity was
6205         // upgraded due to diagnostic settings, simulate the current diagnostic
6206         // settings (and use a warning).
6207         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6208           NewMapping.setSeverity(diag::Severity::Warning);
6209           NewMapping.setUpgradedFromWarning(false);
6210         }
6211 
6212         Mapping = NewMapping;
6213       }
6214       return NewState;
6215     };
6216 
6217     // Read the first state.
6218     DiagState *FirstState;
6219     if (F.Kind == MK_ImplicitModule) {
6220       // Implicitly-built modules are reused with different diagnostic
6221       // settings.  Use the initial diagnostic state from Diag to simulate this
6222       // compilation's diagnostic settings.
6223       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6224       DiagStates.push_back(FirstState);
6225 
6226       // Skip the initial diagnostic state from the serialized module.
6227       assert(Record[1] == 0 &&
6228              "Invalid data, unexpected backref in initial state");
6229       Idx = 3 + Record[2] * 2;
6230       assert(Idx < Record.size() &&
6231              "Invalid data, not enough state change pairs in initial state");
6232     } else if (F.isModule()) {
6233       // For an explicit module, preserve the flags from the module build
6234       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6235       // -Wblah flags.
6236       unsigned Flags = Record[Idx++];
6237       DiagState Initial;
6238       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6239       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6240       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6241       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6242       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6243       Initial.ExtBehavior = (diag::Severity)Flags;
6244       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6245 
6246       assert(F.OriginalSourceFileID.isValid());
6247 
6248       // Set up the root buffer of the module to start with the initial
6249       // diagnostic state of the module itself, to cover files that contain no
6250       // explicit transitions (for which we did not serialize anything).
6251       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6252           .StateTransitions.push_back({FirstState, 0});
6253     } else {
6254       // For prefix ASTs, start with whatever the user configured on the
6255       // command line.
6256       Idx++; // Skip flags.
6257       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6258                                  SourceLocation(), false);
6259     }
6260 
6261     // Read the state transitions.
6262     unsigned NumLocations = Record[Idx++];
6263     while (NumLocations--) {
6264       assert(Idx < Record.size() &&
6265              "Invalid data, missing pragma diagnostic states");
6266       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6267       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6268       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6269       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6270       unsigned Transitions = Record[Idx++];
6271 
6272       // Note that we don't need to set up Parent/ParentOffset here, because
6273       // we won't be changing the diagnostic state within imported FileIDs
6274       // (other than perhaps appending to the main source file, which has no
6275       // parent).
6276       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6277       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6278       for (unsigned I = 0; I != Transitions; ++I) {
6279         unsigned Offset = Record[Idx++];
6280         auto *State =
6281             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6282         F.StateTransitions.push_back({State, Offset});
6283       }
6284     }
6285 
6286     // Read the final state.
6287     assert(Idx < Record.size() &&
6288            "Invalid data, missing final pragma diagnostic state");
6289     SourceLocation CurStateLoc =
6290         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6291     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6292 
6293     if (!F.isModule()) {
6294       Diag.DiagStatesByLoc.CurDiagState = CurState;
6295       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6296 
6297       // Preserve the property that the imaginary root file describes the
6298       // current state.
6299       FileID NullFile;
6300       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6301       if (T.empty())
6302         T.push_back({CurState, 0});
6303       else
6304         T[0].State = CurState;
6305     }
6306 
6307     // Don't try to read these mappings again.
6308     Record.clear();
6309   }
6310 }
6311 
6312 /// Get the correct cursor and offset for loading a type.
6313 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6314   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6315   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6316   ModuleFile *M = I->second;
6317   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6318 }
6319 
6320 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6321   switch (code) {
6322 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6323   case TYPE_##CODE_ID: return Type::CLASS_ID;
6324 #include "clang/Serialization/TypeBitCodes.def"
6325   default: return llvm::None;
6326   }
6327 }
6328 
6329 /// Read and return the type with the given index..
6330 ///
6331 /// The index is the type ID, shifted and minus the number of predefs. This
6332 /// routine actually reads the record corresponding to the type at the given
6333 /// location. It is a helper routine for GetType, which deals with reading type
6334 /// IDs.
6335 QualType ASTReader::readTypeRecord(unsigned Index) {
6336   assert(ContextObj && "reading type with no AST context");
6337   ASTContext &Context = *ContextObj;
6338   RecordLocation Loc = TypeCursorForIndex(Index);
6339   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6340 
6341   // Keep track of where we are in the stream, then jump back there
6342   // after reading this type.
6343   SavedStreamPosition SavedPosition(DeclsCursor);
6344 
6345   ReadingKindTracker ReadingKind(Read_Type, *this);
6346 
6347   // Note that we are loading a type record.
6348   Deserializing AType(this);
6349 
6350   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6351     Error(std::move(Err));
6352     return QualType();
6353   }
6354   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6355   if (!RawCode) {
6356     Error(RawCode.takeError());
6357     return QualType();
6358   }
6359 
6360   ASTRecordReader Record(*this, *Loc.F);
6361   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6362   if (!Code) {
6363     Error(Code.takeError());
6364     return QualType();
6365   }
6366   if (Code.get() == TYPE_EXT_QUAL) {
6367     QualType baseType = Record.readQualType();
6368     Qualifiers quals = Record.readQualifiers();
6369     return Context.getQualifiedType(baseType, quals);
6370   }
6371 
6372   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6373   if (!maybeClass) {
6374     Error("Unexpected code for type");
6375     return QualType();
6376   }
6377 
6378   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6379   return TypeReader.read(*maybeClass);
6380 }
6381 
6382 namespace clang {
6383 
6384 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6385   ASTRecordReader &Reader;
6386 
6387   SourceLocation readSourceLocation() {
6388     return Reader.readSourceLocation();
6389   }
6390 
6391   TypeSourceInfo *GetTypeSourceInfo() {
6392     return Reader.readTypeSourceInfo();
6393   }
6394 
6395   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6396     return Reader.readNestedNameSpecifierLoc();
6397   }
6398 
6399   Attr *ReadAttr() {
6400     return Reader.readAttr();
6401   }
6402 
6403 public:
6404   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6405 
6406   // We want compile-time assurance that we've enumerated all of
6407   // these, so unfortunately we have to declare them first, then
6408   // define them out-of-line.
6409 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6410 #define TYPELOC(CLASS, PARENT) \
6411   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6412 #include "clang/AST/TypeLocNodes.def"
6413 
6414   void VisitFunctionTypeLoc(FunctionTypeLoc);
6415   void VisitArrayTypeLoc(ArrayTypeLoc);
6416 };
6417 
6418 } // namespace clang
6419 
6420 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6421   // nothing to do
6422 }
6423 
6424 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6425   TL.setBuiltinLoc(readSourceLocation());
6426   if (TL.needsExtraLocalData()) {
6427     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6428     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6429     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6430     TL.setModeAttr(Reader.readInt());
6431   }
6432 }
6433 
6434 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6435   TL.setNameLoc(readSourceLocation());
6436 }
6437 
6438 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6439   TL.setStarLoc(readSourceLocation());
6440 }
6441 
6442 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6443   // nothing to do
6444 }
6445 
6446 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6447   // nothing to do
6448 }
6449 
6450 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6451   TL.setExpansionLoc(readSourceLocation());
6452 }
6453 
6454 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6455   TL.setCaretLoc(readSourceLocation());
6456 }
6457 
6458 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6459   TL.setAmpLoc(readSourceLocation());
6460 }
6461 
6462 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6463   TL.setAmpAmpLoc(readSourceLocation());
6464 }
6465 
6466 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6467   TL.setStarLoc(readSourceLocation());
6468   TL.setClassTInfo(GetTypeSourceInfo());
6469 }
6470 
6471 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6472   TL.setLBracketLoc(readSourceLocation());
6473   TL.setRBracketLoc(readSourceLocation());
6474   if (Reader.readBool())
6475     TL.setSizeExpr(Reader.readExpr());
6476   else
6477     TL.setSizeExpr(nullptr);
6478 }
6479 
6480 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6481   VisitArrayTypeLoc(TL);
6482 }
6483 
6484 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6485   VisitArrayTypeLoc(TL);
6486 }
6487 
6488 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6489   VisitArrayTypeLoc(TL);
6490 }
6491 
6492 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6493                                             DependentSizedArrayTypeLoc TL) {
6494   VisitArrayTypeLoc(TL);
6495 }
6496 
6497 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6498     DependentAddressSpaceTypeLoc TL) {
6499 
6500     TL.setAttrNameLoc(readSourceLocation());
6501     TL.setAttrOperandParensRange(Reader.readSourceRange());
6502     TL.setAttrExprOperand(Reader.readExpr());
6503 }
6504 
6505 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6506                                         DependentSizedExtVectorTypeLoc TL) {
6507   TL.setNameLoc(readSourceLocation());
6508 }
6509 
6510 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6511   TL.setNameLoc(readSourceLocation());
6512 }
6513 
6514 void TypeLocReader::VisitDependentVectorTypeLoc(
6515     DependentVectorTypeLoc TL) {
6516   TL.setNameLoc(readSourceLocation());
6517 }
6518 
6519 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6520   TL.setNameLoc(readSourceLocation());
6521 }
6522 
6523 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6524   TL.setLocalRangeBegin(readSourceLocation());
6525   TL.setLParenLoc(readSourceLocation());
6526   TL.setRParenLoc(readSourceLocation());
6527   TL.setExceptionSpecRange(Reader.readSourceRange());
6528   TL.setLocalRangeEnd(readSourceLocation());
6529   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6530     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6531   }
6532 }
6533 
6534 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6535   VisitFunctionTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6539   VisitFunctionTypeLoc(TL);
6540 }
6541 
6542 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6543   TL.setNameLoc(readSourceLocation());
6544 }
6545 
6546 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6547   TL.setNameLoc(readSourceLocation());
6548 }
6549 
6550 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6551   TL.setTypeofLoc(readSourceLocation());
6552   TL.setLParenLoc(readSourceLocation());
6553   TL.setRParenLoc(readSourceLocation());
6554 }
6555 
6556 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6557   TL.setTypeofLoc(readSourceLocation());
6558   TL.setLParenLoc(readSourceLocation());
6559   TL.setRParenLoc(readSourceLocation());
6560   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6561 }
6562 
6563 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6564   TL.setNameLoc(readSourceLocation());
6565 }
6566 
6567 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6568   TL.setKWLoc(readSourceLocation());
6569   TL.setLParenLoc(readSourceLocation());
6570   TL.setRParenLoc(readSourceLocation());
6571   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6572 }
6573 
6574 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6575   TL.setNameLoc(readSourceLocation());
6576   if (Reader.readBool()) {
6577     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6578     TL.setTemplateKWLoc(readSourceLocation());
6579     TL.setConceptNameLoc(readSourceLocation());
6580     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6581     TL.setLAngleLoc(readSourceLocation());
6582     TL.setRAngleLoc(readSourceLocation());
6583     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6584       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6585                               TL.getTypePtr()->getArg(i).getKind()));
6586   }
6587 }
6588 
6589 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6590     DeducedTemplateSpecializationTypeLoc TL) {
6591   TL.setTemplateNameLoc(readSourceLocation());
6592 }
6593 
6594 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6595   TL.setNameLoc(readSourceLocation());
6596 }
6597 
6598 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6599   TL.setNameLoc(readSourceLocation());
6600 }
6601 
6602 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6603   TL.setAttr(ReadAttr());
6604 }
6605 
6606 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6607   TL.setNameLoc(readSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6611                                             SubstTemplateTypeParmTypeLoc TL) {
6612   TL.setNameLoc(readSourceLocation());
6613 }
6614 
6615 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6616                                           SubstTemplateTypeParmPackTypeLoc TL) {
6617   TL.setNameLoc(readSourceLocation());
6618 }
6619 
6620 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6621                                            TemplateSpecializationTypeLoc TL) {
6622   TL.setTemplateKeywordLoc(readSourceLocation());
6623   TL.setTemplateNameLoc(readSourceLocation());
6624   TL.setLAngleLoc(readSourceLocation());
6625   TL.setRAngleLoc(readSourceLocation());
6626   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6627     TL.setArgLocInfo(
6628         i,
6629         Reader.readTemplateArgumentLocInfo(
6630           TL.getTypePtr()->getArg(i).getKind()));
6631 }
6632 
6633 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6634   TL.setLParenLoc(readSourceLocation());
6635   TL.setRParenLoc(readSourceLocation());
6636 }
6637 
6638 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6639   TL.setElaboratedKeywordLoc(readSourceLocation());
6640   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6641 }
6642 
6643 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6644   TL.setNameLoc(readSourceLocation());
6645 }
6646 
6647 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6648   TL.setElaboratedKeywordLoc(readSourceLocation());
6649   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6650   TL.setNameLoc(readSourceLocation());
6651 }
6652 
6653 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6654        DependentTemplateSpecializationTypeLoc TL) {
6655   TL.setElaboratedKeywordLoc(readSourceLocation());
6656   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6657   TL.setTemplateKeywordLoc(readSourceLocation());
6658   TL.setTemplateNameLoc(readSourceLocation());
6659   TL.setLAngleLoc(readSourceLocation());
6660   TL.setRAngleLoc(readSourceLocation());
6661   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6662     TL.setArgLocInfo(
6663         I,
6664         Reader.readTemplateArgumentLocInfo(
6665             TL.getTypePtr()->getArg(I).getKind()));
6666 }
6667 
6668 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6669   TL.setEllipsisLoc(readSourceLocation());
6670 }
6671 
6672 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6673   TL.setNameLoc(readSourceLocation());
6674 }
6675 
6676 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6677   if (TL.getNumProtocols()) {
6678     TL.setProtocolLAngleLoc(readSourceLocation());
6679     TL.setProtocolRAngleLoc(readSourceLocation());
6680   }
6681   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6682     TL.setProtocolLoc(i, readSourceLocation());
6683 }
6684 
6685 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6686   TL.setHasBaseTypeAsWritten(Reader.readBool());
6687   TL.setTypeArgsLAngleLoc(readSourceLocation());
6688   TL.setTypeArgsRAngleLoc(readSourceLocation());
6689   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6690     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6691   TL.setProtocolLAngleLoc(readSourceLocation());
6692   TL.setProtocolRAngleLoc(readSourceLocation());
6693   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6694     TL.setProtocolLoc(i, readSourceLocation());
6695 }
6696 
6697 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6698   TL.setStarLoc(readSourceLocation());
6699 }
6700 
6701 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6702   TL.setKWLoc(readSourceLocation());
6703   TL.setLParenLoc(readSourceLocation());
6704   TL.setRParenLoc(readSourceLocation());
6705 }
6706 
6707 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6708   TL.setKWLoc(readSourceLocation());
6709 }
6710 
6711 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6712   TypeLocReader TLR(*this);
6713   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6714     TLR.Visit(TL);
6715 }
6716 
6717 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6718   QualType InfoTy = readType();
6719   if (InfoTy.isNull())
6720     return nullptr;
6721 
6722   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6723   readTypeLoc(TInfo->getTypeLoc());
6724   return TInfo;
6725 }
6726 
6727 QualType ASTReader::GetType(TypeID ID) {
6728   assert(ContextObj && "reading type with no AST context");
6729   ASTContext &Context = *ContextObj;
6730 
6731   unsigned FastQuals = ID & Qualifiers::FastMask;
6732   unsigned Index = ID >> Qualifiers::FastWidth;
6733 
6734   if (Index < NUM_PREDEF_TYPE_IDS) {
6735     QualType T;
6736     switch ((PredefinedTypeIDs)Index) {
6737     case PREDEF_TYPE_NULL_ID:
6738       return QualType();
6739     case PREDEF_TYPE_VOID_ID:
6740       T = Context.VoidTy;
6741       break;
6742     case PREDEF_TYPE_BOOL_ID:
6743       T = Context.BoolTy;
6744       break;
6745     case PREDEF_TYPE_CHAR_U_ID:
6746     case PREDEF_TYPE_CHAR_S_ID:
6747       // FIXME: Check that the signedness of CharTy is correct!
6748       T = Context.CharTy;
6749       break;
6750     case PREDEF_TYPE_UCHAR_ID:
6751       T = Context.UnsignedCharTy;
6752       break;
6753     case PREDEF_TYPE_USHORT_ID:
6754       T = Context.UnsignedShortTy;
6755       break;
6756     case PREDEF_TYPE_UINT_ID:
6757       T = Context.UnsignedIntTy;
6758       break;
6759     case PREDEF_TYPE_ULONG_ID:
6760       T = Context.UnsignedLongTy;
6761       break;
6762     case PREDEF_TYPE_ULONGLONG_ID:
6763       T = Context.UnsignedLongLongTy;
6764       break;
6765     case PREDEF_TYPE_UINT128_ID:
6766       T = Context.UnsignedInt128Ty;
6767       break;
6768     case PREDEF_TYPE_SCHAR_ID:
6769       T = Context.SignedCharTy;
6770       break;
6771     case PREDEF_TYPE_WCHAR_ID:
6772       T = Context.WCharTy;
6773       break;
6774     case PREDEF_TYPE_SHORT_ID:
6775       T = Context.ShortTy;
6776       break;
6777     case PREDEF_TYPE_INT_ID:
6778       T = Context.IntTy;
6779       break;
6780     case PREDEF_TYPE_LONG_ID:
6781       T = Context.LongTy;
6782       break;
6783     case PREDEF_TYPE_LONGLONG_ID:
6784       T = Context.LongLongTy;
6785       break;
6786     case PREDEF_TYPE_INT128_ID:
6787       T = Context.Int128Ty;
6788       break;
6789     case PREDEF_TYPE_HALF_ID:
6790       T = Context.HalfTy;
6791       break;
6792     case PREDEF_TYPE_FLOAT_ID:
6793       T = Context.FloatTy;
6794       break;
6795     case PREDEF_TYPE_DOUBLE_ID:
6796       T = Context.DoubleTy;
6797       break;
6798     case PREDEF_TYPE_LONGDOUBLE_ID:
6799       T = Context.LongDoubleTy;
6800       break;
6801     case PREDEF_TYPE_SHORT_ACCUM_ID:
6802       T = Context.ShortAccumTy;
6803       break;
6804     case PREDEF_TYPE_ACCUM_ID:
6805       T = Context.AccumTy;
6806       break;
6807     case PREDEF_TYPE_LONG_ACCUM_ID:
6808       T = Context.LongAccumTy;
6809       break;
6810     case PREDEF_TYPE_USHORT_ACCUM_ID:
6811       T = Context.UnsignedShortAccumTy;
6812       break;
6813     case PREDEF_TYPE_UACCUM_ID:
6814       T = Context.UnsignedAccumTy;
6815       break;
6816     case PREDEF_TYPE_ULONG_ACCUM_ID:
6817       T = Context.UnsignedLongAccumTy;
6818       break;
6819     case PREDEF_TYPE_SHORT_FRACT_ID:
6820       T = Context.ShortFractTy;
6821       break;
6822     case PREDEF_TYPE_FRACT_ID:
6823       T = Context.FractTy;
6824       break;
6825     case PREDEF_TYPE_LONG_FRACT_ID:
6826       T = Context.LongFractTy;
6827       break;
6828     case PREDEF_TYPE_USHORT_FRACT_ID:
6829       T = Context.UnsignedShortFractTy;
6830       break;
6831     case PREDEF_TYPE_UFRACT_ID:
6832       T = Context.UnsignedFractTy;
6833       break;
6834     case PREDEF_TYPE_ULONG_FRACT_ID:
6835       T = Context.UnsignedLongFractTy;
6836       break;
6837     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6838       T = Context.SatShortAccumTy;
6839       break;
6840     case PREDEF_TYPE_SAT_ACCUM_ID:
6841       T = Context.SatAccumTy;
6842       break;
6843     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6844       T = Context.SatLongAccumTy;
6845       break;
6846     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6847       T = Context.SatUnsignedShortAccumTy;
6848       break;
6849     case PREDEF_TYPE_SAT_UACCUM_ID:
6850       T = Context.SatUnsignedAccumTy;
6851       break;
6852     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6853       T = Context.SatUnsignedLongAccumTy;
6854       break;
6855     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6856       T = Context.SatShortFractTy;
6857       break;
6858     case PREDEF_TYPE_SAT_FRACT_ID:
6859       T = Context.SatFractTy;
6860       break;
6861     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6862       T = Context.SatLongFractTy;
6863       break;
6864     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6865       T = Context.SatUnsignedShortFractTy;
6866       break;
6867     case PREDEF_TYPE_SAT_UFRACT_ID:
6868       T = Context.SatUnsignedFractTy;
6869       break;
6870     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6871       T = Context.SatUnsignedLongFractTy;
6872       break;
6873     case PREDEF_TYPE_FLOAT16_ID:
6874       T = Context.Float16Ty;
6875       break;
6876     case PREDEF_TYPE_FLOAT128_ID:
6877       T = Context.Float128Ty;
6878       break;
6879     case PREDEF_TYPE_OVERLOAD_ID:
6880       T = Context.OverloadTy;
6881       break;
6882     case PREDEF_TYPE_BOUND_MEMBER:
6883       T = Context.BoundMemberTy;
6884       break;
6885     case PREDEF_TYPE_PSEUDO_OBJECT:
6886       T = Context.PseudoObjectTy;
6887       break;
6888     case PREDEF_TYPE_DEPENDENT_ID:
6889       T = Context.DependentTy;
6890       break;
6891     case PREDEF_TYPE_UNKNOWN_ANY:
6892       T = Context.UnknownAnyTy;
6893       break;
6894     case PREDEF_TYPE_NULLPTR_ID:
6895       T = Context.NullPtrTy;
6896       break;
6897     case PREDEF_TYPE_CHAR8_ID:
6898       T = Context.Char8Ty;
6899       break;
6900     case PREDEF_TYPE_CHAR16_ID:
6901       T = Context.Char16Ty;
6902       break;
6903     case PREDEF_TYPE_CHAR32_ID:
6904       T = Context.Char32Ty;
6905       break;
6906     case PREDEF_TYPE_OBJC_ID:
6907       T = Context.ObjCBuiltinIdTy;
6908       break;
6909     case PREDEF_TYPE_OBJC_CLASS:
6910       T = Context.ObjCBuiltinClassTy;
6911       break;
6912     case PREDEF_TYPE_OBJC_SEL:
6913       T = Context.ObjCBuiltinSelTy;
6914       break;
6915 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6916     case PREDEF_TYPE_##Id##_ID: \
6917       T = Context.SingletonId; \
6918       break;
6919 #include "clang/Basic/OpenCLImageTypes.def"
6920 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6921     case PREDEF_TYPE_##Id##_ID: \
6922       T = Context.Id##Ty; \
6923       break;
6924 #include "clang/Basic/OpenCLExtensionTypes.def"
6925     case PREDEF_TYPE_SAMPLER_ID:
6926       T = Context.OCLSamplerTy;
6927       break;
6928     case PREDEF_TYPE_EVENT_ID:
6929       T = Context.OCLEventTy;
6930       break;
6931     case PREDEF_TYPE_CLK_EVENT_ID:
6932       T = Context.OCLClkEventTy;
6933       break;
6934     case PREDEF_TYPE_QUEUE_ID:
6935       T = Context.OCLQueueTy;
6936       break;
6937     case PREDEF_TYPE_RESERVE_ID_ID:
6938       T = Context.OCLReserveIDTy;
6939       break;
6940     case PREDEF_TYPE_AUTO_DEDUCT:
6941       T = Context.getAutoDeductType();
6942       break;
6943     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6944       T = Context.getAutoRRefDeductType();
6945       break;
6946     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6947       T = Context.ARCUnbridgedCastTy;
6948       break;
6949     case PREDEF_TYPE_BUILTIN_FN:
6950       T = Context.BuiltinFnTy;
6951       break;
6952     case PREDEF_TYPE_OMP_ARRAY_SECTION:
6953       T = Context.OMPArraySectionTy;
6954       break;
6955 #define SVE_TYPE(Name, Id, SingletonId) \
6956     case PREDEF_TYPE_##Id##_ID: \
6957       T = Context.SingletonId; \
6958       break;
6959 #include "clang/Basic/AArch64SVEACLETypes.def"
6960     }
6961 
6962     assert(!T.isNull() && "Unknown predefined type");
6963     return T.withFastQualifiers(FastQuals);
6964   }
6965 
6966   Index -= NUM_PREDEF_TYPE_IDS;
6967   assert(Index < TypesLoaded.size() && "Type index out-of-range");
6968   if (TypesLoaded[Index].isNull()) {
6969     TypesLoaded[Index] = readTypeRecord(Index);
6970     if (TypesLoaded[Index].isNull())
6971       return QualType();
6972 
6973     TypesLoaded[Index]->setFromAST();
6974     if (DeserializationListener)
6975       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6976                                         TypesLoaded[Index]);
6977   }
6978 
6979   return TypesLoaded[Index].withFastQualifiers(FastQuals);
6980 }
6981 
6982 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6983   return GetType(getGlobalTypeID(F, LocalID));
6984 }
6985 
6986 serialization::TypeID
6987 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6988   unsigned FastQuals = LocalID & Qualifiers::FastMask;
6989   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6990 
6991   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6992     return LocalID;
6993 
6994   if (!F.ModuleOffsetMap.empty())
6995     ReadModuleOffsetMap(F);
6996 
6997   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6998     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6999   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7000 
7001   unsigned GlobalIndex = LocalIndex + I->second;
7002   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7003 }
7004 
7005 TemplateArgumentLocInfo
7006 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7007   switch (Kind) {
7008   case TemplateArgument::Expression:
7009     return readExpr();
7010   case TemplateArgument::Type:
7011     return readTypeSourceInfo();
7012   case TemplateArgument::Template: {
7013     NestedNameSpecifierLoc QualifierLoc =
7014       readNestedNameSpecifierLoc();
7015     SourceLocation TemplateNameLoc = readSourceLocation();
7016     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7017                                    SourceLocation());
7018   }
7019   case TemplateArgument::TemplateExpansion: {
7020     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7021     SourceLocation TemplateNameLoc = readSourceLocation();
7022     SourceLocation EllipsisLoc = readSourceLocation();
7023     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7024                                    EllipsisLoc);
7025   }
7026   case TemplateArgument::Null:
7027   case TemplateArgument::Integral:
7028   case TemplateArgument::Declaration:
7029   case TemplateArgument::NullPtr:
7030   case TemplateArgument::Pack:
7031     // FIXME: Is this right?
7032     return TemplateArgumentLocInfo();
7033   }
7034   llvm_unreachable("unexpected template argument loc");
7035 }
7036 
7037 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7038   TemplateArgument Arg = readTemplateArgument();
7039 
7040   if (Arg.getKind() == TemplateArgument::Expression) {
7041     if (readBool()) // bool InfoHasSameExpr.
7042       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7043   }
7044   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7045 }
7046 
7047 const ASTTemplateArgumentListInfo *
7048 ASTRecordReader::readASTTemplateArgumentListInfo() {
7049   SourceLocation LAngleLoc = readSourceLocation();
7050   SourceLocation RAngleLoc = readSourceLocation();
7051   unsigned NumArgsAsWritten = readInt();
7052   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7053   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7054     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7055   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7056 }
7057 
7058 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7059   return GetDecl(ID);
7060 }
7061 
7062 void ASTReader::CompleteRedeclChain(const Decl *D) {
7063   if (NumCurrentElementsDeserializing) {
7064     // We arrange to not care about the complete redeclaration chain while we're
7065     // deserializing. Just remember that the AST has marked this one as complete
7066     // but that it's not actually complete yet, so we know we still need to
7067     // complete it later.
7068     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7069     return;
7070   }
7071 
7072   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7073 
7074   // If this is a named declaration, complete it by looking it up
7075   // within its context.
7076   //
7077   // FIXME: Merging a function definition should merge
7078   // all mergeable entities within it.
7079   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7080       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7081     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7082       if (!getContext().getLangOpts().CPlusPlus &&
7083           isa<TranslationUnitDecl>(DC)) {
7084         // Outside of C++, we don't have a lookup table for the TU, so update
7085         // the identifier instead. (For C++ modules, we don't store decls
7086         // in the serialized identifier table, so we do the lookup in the TU.)
7087         auto *II = Name.getAsIdentifierInfo();
7088         assert(II && "non-identifier name in C?");
7089         if (II->isOutOfDate())
7090           updateOutOfDateIdentifier(*II);
7091       } else
7092         DC->lookup(Name);
7093     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7094       // Find all declarations of this kind from the relevant context.
7095       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7096         auto *DC = cast<DeclContext>(DCDecl);
7097         SmallVector<Decl*, 8> Decls;
7098         FindExternalLexicalDecls(
7099             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7100       }
7101     }
7102   }
7103 
7104   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7105     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7106   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7107     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7108   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7109     if (auto *Template = FD->getPrimaryTemplate())
7110       Template->LoadLazySpecializations();
7111   }
7112 }
7113 
7114 CXXCtorInitializer **
7115 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7116   RecordLocation Loc = getLocalBitOffset(Offset);
7117   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7118   SavedStreamPosition SavedPosition(Cursor);
7119   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7120     Error(std::move(Err));
7121     return nullptr;
7122   }
7123   ReadingKindTracker ReadingKind(Read_Decl, *this);
7124 
7125   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7126   if (!MaybeCode) {
7127     Error(MaybeCode.takeError());
7128     return nullptr;
7129   }
7130   unsigned Code = MaybeCode.get();
7131 
7132   ASTRecordReader Record(*this, *Loc.F);
7133   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7134   if (!MaybeRecCode) {
7135     Error(MaybeRecCode.takeError());
7136     return nullptr;
7137   }
7138   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7139     Error("malformed AST file: missing C++ ctor initializers");
7140     return nullptr;
7141   }
7142 
7143   return Record.readCXXCtorInitializers();
7144 }
7145 
7146 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7147   assert(ContextObj && "reading base specifiers with no AST context");
7148   ASTContext &Context = *ContextObj;
7149 
7150   RecordLocation Loc = getLocalBitOffset(Offset);
7151   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7152   SavedStreamPosition SavedPosition(Cursor);
7153   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7154     Error(std::move(Err));
7155     return nullptr;
7156   }
7157   ReadingKindTracker ReadingKind(Read_Decl, *this);
7158 
7159   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7160   if (!MaybeCode) {
7161     Error(MaybeCode.takeError());
7162     return nullptr;
7163   }
7164   unsigned Code = MaybeCode.get();
7165 
7166   ASTRecordReader Record(*this, *Loc.F);
7167   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7168   if (!MaybeRecCode) {
7169     Error(MaybeCode.takeError());
7170     return nullptr;
7171   }
7172   unsigned RecCode = MaybeRecCode.get();
7173 
7174   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7175     Error("malformed AST file: missing C++ base specifiers");
7176     return nullptr;
7177   }
7178 
7179   unsigned NumBases = Record.readInt();
7180   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7181   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7182   for (unsigned I = 0; I != NumBases; ++I)
7183     Bases[I] = Record.readCXXBaseSpecifier();
7184   return Bases;
7185 }
7186 
7187 serialization::DeclID
7188 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7189   if (LocalID < NUM_PREDEF_DECL_IDS)
7190     return LocalID;
7191 
7192   if (!F.ModuleOffsetMap.empty())
7193     ReadModuleOffsetMap(F);
7194 
7195   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7196     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7197   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7198 
7199   return LocalID + I->second;
7200 }
7201 
7202 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7203                                    ModuleFile &M) const {
7204   // Predefined decls aren't from any module.
7205   if (ID < NUM_PREDEF_DECL_IDS)
7206     return false;
7207 
7208   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7209          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7210 }
7211 
7212 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7213   if (!D->isFromASTFile())
7214     return nullptr;
7215   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7216   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7217   return I->second;
7218 }
7219 
7220 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7221   if (ID < NUM_PREDEF_DECL_IDS)
7222     return SourceLocation();
7223 
7224   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7225 
7226   if (Index > DeclsLoaded.size()) {
7227     Error("declaration ID out-of-range for AST file");
7228     return SourceLocation();
7229   }
7230 
7231   if (Decl *D = DeclsLoaded[Index])
7232     return D->getLocation();
7233 
7234   SourceLocation Loc;
7235   DeclCursorForID(ID, Loc);
7236   return Loc;
7237 }
7238 
7239 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7240   switch (ID) {
7241   case PREDEF_DECL_NULL_ID:
7242     return nullptr;
7243 
7244   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7245     return Context.getTranslationUnitDecl();
7246 
7247   case PREDEF_DECL_OBJC_ID_ID:
7248     return Context.getObjCIdDecl();
7249 
7250   case PREDEF_DECL_OBJC_SEL_ID:
7251     return Context.getObjCSelDecl();
7252 
7253   case PREDEF_DECL_OBJC_CLASS_ID:
7254     return Context.getObjCClassDecl();
7255 
7256   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7257     return Context.getObjCProtocolDecl();
7258 
7259   case PREDEF_DECL_INT_128_ID:
7260     return Context.getInt128Decl();
7261 
7262   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7263     return Context.getUInt128Decl();
7264 
7265   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7266     return Context.getObjCInstanceTypeDecl();
7267 
7268   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7269     return Context.getBuiltinVaListDecl();
7270 
7271   case PREDEF_DECL_VA_LIST_TAG:
7272     return Context.getVaListTagDecl();
7273 
7274   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7275     return Context.getBuiltinMSVaListDecl();
7276 
7277   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7278     return Context.getExternCContextDecl();
7279 
7280   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7281     return Context.getMakeIntegerSeqDecl();
7282 
7283   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7284     return Context.getCFConstantStringDecl();
7285 
7286   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7287     return Context.getCFConstantStringTagDecl();
7288 
7289   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7290     return Context.getTypePackElementDecl();
7291   }
7292   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7293 }
7294 
7295 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7296   assert(ContextObj && "reading decl with no AST context");
7297   if (ID < NUM_PREDEF_DECL_IDS) {
7298     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7299     if (D) {
7300       // Track that we have merged the declaration with ID \p ID into the
7301       // pre-existing predefined declaration \p D.
7302       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7303       if (Merged.empty())
7304         Merged.push_back(ID);
7305     }
7306     return D;
7307   }
7308 
7309   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7310 
7311   if (Index >= DeclsLoaded.size()) {
7312     assert(0 && "declaration ID out-of-range for AST file");
7313     Error("declaration ID out-of-range for AST file");
7314     return nullptr;
7315   }
7316 
7317   return DeclsLoaded[Index];
7318 }
7319 
7320 Decl *ASTReader::GetDecl(DeclID ID) {
7321   if (ID < NUM_PREDEF_DECL_IDS)
7322     return GetExistingDecl(ID);
7323 
7324   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7325 
7326   if (Index >= DeclsLoaded.size()) {
7327     assert(0 && "declaration ID out-of-range for AST file");
7328     Error("declaration ID out-of-range for AST file");
7329     return nullptr;
7330   }
7331 
7332   if (!DeclsLoaded[Index]) {
7333     ReadDeclRecord(ID);
7334     if (DeserializationListener)
7335       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7336   }
7337 
7338   return DeclsLoaded[Index];
7339 }
7340 
7341 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7342                                                   DeclID GlobalID) {
7343   if (GlobalID < NUM_PREDEF_DECL_IDS)
7344     return GlobalID;
7345 
7346   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7347   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7348   ModuleFile *Owner = I->second;
7349 
7350   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7351     = M.GlobalToLocalDeclIDs.find(Owner);
7352   if (Pos == M.GlobalToLocalDeclIDs.end())
7353     return 0;
7354 
7355   return GlobalID - Owner->BaseDeclID + Pos->second;
7356 }
7357 
7358 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7359                                             const RecordData &Record,
7360                                             unsigned &Idx) {
7361   if (Idx >= Record.size()) {
7362     Error("Corrupted AST file");
7363     return 0;
7364   }
7365 
7366   return getGlobalDeclID(F, Record[Idx++]);
7367 }
7368 
7369 /// Resolve the offset of a statement into a statement.
7370 ///
7371 /// This operation will read a new statement from the external
7372 /// source each time it is called, and is meant to be used via a
7373 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7374 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7375   // Switch case IDs are per Decl.
7376   ClearSwitchCaseIDs();
7377 
7378   // Offset here is a global offset across the entire chain.
7379   RecordLocation Loc = getLocalBitOffset(Offset);
7380   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7381     Error(std::move(Err));
7382     return nullptr;
7383   }
7384   assert(NumCurrentElementsDeserializing == 0 &&
7385          "should not be called while already deserializing");
7386   Deserializing D(this);
7387   return ReadStmtFromStream(*Loc.F);
7388 }
7389 
7390 void ASTReader::FindExternalLexicalDecls(
7391     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7392     SmallVectorImpl<Decl *> &Decls) {
7393   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7394 
7395   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7396     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7397     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7398       auto K = (Decl::Kind)+LexicalDecls[I];
7399       if (!IsKindWeWant(K))
7400         continue;
7401 
7402       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7403 
7404       // Don't add predefined declarations to the lexical context more
7405       // than once.
7406       if (ID < NUM_PREDEF_DECL_IDS) {
7407         if (PredefsVisited[ID])
7408           continue;
7409 
7410         PredefsVisited[ID] = true;
7411       }
7412 
7413       if (Decl *D = GetLocalDecl(*M, ID)) {
7414         assert(D->getKind() == K && "wrong kind for lexical decl");
7415         if (!DC->isDeclInLexicalTraversal(D))
7416           Decls.push_back(D);
7417       }
7418     }
7419   };
7420 
7421   if (isa<TranslationUnitDecl>(DC)) {
7422     for (auto Lexical : TULexicalDecls)
7423       Visit(Lexical.first, Lexical.second);
7424   } else {
7425     auto I = LexicalDecls.find(DC);
7426     if (I != LexicalDecls.end())
7427       Visit(I->second.first, I->second.second);
7428   }
7429 
7430   ++NumLexicalDeclContextsRead;
7431 }
7432 
7433 namespace {
7434 
7435 class DeclIDComp {
7436   ASTReader &Reader;
7437   ModuleFile &Mod;
7438 
7439 public:
7440   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7441 
7442   bool operator()(LocalDeclID L, LocalDeclID R) const {
7443     SourceLocation LHS = getLocation(L);
7444     SourceLocation RHS = getLocation(R);
7445     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7446   }
7447 
7448   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7449     SourceLocation RHS = getLocation(R);
7450     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7451   }
7452 
7453   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7454     SourceLocation LHS = getLocation(L);
7455     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7456   }
7457 
7458   SourceLocation getLocation(LocalDeclID ID) const {
7459     return Reader.getSourceManager().getFileLoc(
7460             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7461   }
7462 };
7463 
7464 } // namespace
7465 
7466 void ASTReader::FindFileRegionDecls(FileID File,
7467                                     unsigned Offset, unsigned Length,
7468                                     SmallVectorImpl<Decl *> &Decls) {
7469   SourceManager &SM = getSourceManager();
7470 
7471   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7472   if (I == FileDeclIDs.end())
7473     return;
7474 
7475   FileDeclsInfo &DInfo = I->second;
7476   if (DInfo.Decls.empty())
7477     return;
7478 
7479   SourceLocation
7480     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7481   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7482 
7483   DeclIDComp DIDComp(*this, *DInfo.Mod);
7484   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7485       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7486   if (BeginIt != DInfo.Decls.begin())
7487     --BeginIt;
7488 
7489   // If we are pointing at a top-level decl inside an objc container, we need
7490   // to backtrack until we find it otherwise we will fail to report that the
7491   // region overlaps with an objc container.
7492   while (BeginIt != DInfo.Decls.begin() &&
7493          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7494              ->isTopLevelDeclInObjCContainer())
7495     --BeginIt;
7496 
7497   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7498       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7499   if (EndIt != DInfo.Decls.end())
7500     ++EndIt;
7501 
7502   for (ArrayRef<serialization::LocalDeclID>::iterator
7503          DIt = BeginIt; DIt != EndIt; ++DIt)
7504     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7505 }
7506 
7507 bool
7508 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7509                                           DeclarationName Name) {
7510   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7511          "DeclContext has no visible decls in storage");
7512   if (!Name)
7513     return false;
7514 
7515   auto It = Lookups.find(DC);
7516   if (It == Lookups.end())
7517     return false;
7518 
7519   Deserializing LookupResults(this);
7520 
7521   // Load the list of declarations.
7522   SmallVector<NamedDecl *, 64> Decls;
7523   for (DeclID ID : It->second.Table.find(Name)) {
7524     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7525     if (ND->getDeclName() == Name)
7526       Decls.push_back(ND);
7527   }
7528 
7529   ++NumVisibleDeclContextsRead;
7530   SetExternalVisibleDeclsForName(DC, Name, Decls);
7531   return !Decls.empty();
7532 }
7533 
7534 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7535   if (!DC->hasExternalVisibleStorage())
7536     return;
7537 
7538   auto It = Lookups.find(DC);
7539   assert(It != Lookups.end() &&
7540          "have external visible storage but no lookup tables");
7541 
7542   DeclsMap Decls;
7543 
7544   for (DeclID ID : It->second.Table.findAll()) {
7545     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7546     Decls[ND->getDeclName()].push_back(ND);
7547   }
7548 
7549   ++NumVisibleDeclContextsRead;
7550 
7551   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7552     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7553   }
7554   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7555 }
7556 
7557 const serialization::reader::DeclContextLookupTable *
7558 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7559   auto I = Lookups.find(Primary);
7560   return I == Lookups.end() ? nullptr : &I->second;
7561 }
7562 
7563 /// Under non-PCH compilation the consumer receives the objc methods
7564 /// before receiving the implementation, and codegen depends on this.
7565 /// We simulate this by deserializing and passing to consumer the methods of the
7566 /// implementation before passing the deserialized implementation decl.
7567 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7568                                        ASTConsumer *Consumer) {
7569   assert(ImplD && Consumer);
7570 
7571   for (auto *I : ImplD->methods())
7572     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7573 
7574   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7575 }
7576 
7577 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7578   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7579     PassObjCImplDeclToConsumer(ImplD, Consumer);
7580   else
7581     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7582 }
7583 
7584 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7585   this->Consumer = Consumer;
7586 
7587   if (Consumer)
7588     PassInterestingDeclsToConsumer();
7589 
7590   if (DeserializationListener)
7591     DeserializationListener->ReaderInitialized(this);
7592 }
7593 
7594 void ASTReader::PrintStats() {
7595   std::fprintf(stderr, "*** AST File Statistics:\n");
7596 
7597   unsigned NumTypesLoaded
7598     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7599                                       QualType());
7600   unsigned NumDeclsLoaded
7601     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7602                                       (Decl *)nullptr);
7603   unsigned NumIdentifiersLoaded
7604     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7605                                             IdentifiersLoaded.end(),
7606                                             (IdentifierInfo *)nullptr);
7607   unsigned NumMacrosLoaded
7608     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7609                                        MacrosLoaded.end(),
7610                                        (MacroInfo *)nullptr);
7611   unsigned NumSelectorsLoaded
7612     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7613                                           SelectorsLoaded.end(),
7614                                           Selector());
7615 
7616   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7617     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7618                  NumSLocEntriesRead, TotalNumSLocEntries,
7619                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7620   if (!TypesLoaded.empty())
7621     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7622                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7623                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7624   if (!DeclsLoaded.empty())
7625     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7626                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7627                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7628   if (!IdentifiersLoaded.empty())
7629     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7630                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7631                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7632   if (!MacrosLoaded.empty())
7633     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7634                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7635                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7636   if (!SelectorsLoaded.empty())
7637     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7638                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7639                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7640   if (TotalNumStatements)
7641     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7642                  NumStatementsRead, TotalNumStatements,
7643                  ((float)NumStatementsRead/TotalNumStatements * 100));
7644   if (TotalNumMacros)
7645     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7646                  NumMacrosRead, TotalNumMacros,
7647                  ((float)NumMacrosRead/TotalNumMacros * 100));
7648   if (TotalLexicalDeclContexts)
7649     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7650                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7651                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7652                   * 100));
7653   if (TotalVisibleDeclContexts)
7654     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7655                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7656                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7657                   * 100));
7658   if (TotalNumMethodPoolEntries)
7659     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7660                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7661                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7662                   * 100));
7663   if (NumMethodPoolLookups)
7664     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7665                  NumMethodPoolHits, NumMethodPoolLookups,
7666                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7667   if (NumMethodPoolTableLookups)
7668     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7669                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7670                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7671                   * 100.0));
7672   if (NumIdentifierLookupHits)
7673     std::fprintf(stderr,
7674                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7675                  NumIdentifierLookupHits, NumIdentifierLookups,
7676                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7677 
7678   if (GlobalIndex) {
7679     std::fprintf(stderr, "\n");
7680     GlobalIndex->printStats();
7681   }
7682 
7683   std::fprintf(stderr, "\n");
7684   dump();
7685   std::fprintf(stderr, "\n");
7686 }
7687 
7688 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7689 LLVM_DUMP_METHOD static void
7690 dumpModuleIDMap(StringRef Name,
7691                 const ContinuousRangeMap<Key, ModuleFile *,
7692                                          InitialCapacity> &Map) {
7693   if (Map.begin() == Map.end())
7694     return;
7695 
7696   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7697 
7698   llvm::errs() << Name << ":\n";
7699   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7700        I != IEnd; ++I) {
7701     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7702       << "\n";
7703   }
7704 }
7705 
7706 LLVM_DUMP_METHOD void ASTReader::dump() {
7707   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7708   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7709   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7710   dumpModuleIDMap("Global type map", GlobalTypeMap);
7711   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7712   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7713   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7714   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7715   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7716   dumpModuleIDMap("Global preprocessed entity map",
7717                   GlobalPreprocessedEntityMap);
7718 
7719   llvm::errs() << "\n*** PCH/Modules Loaded:";
7720   for (ModuleFile &M : ModuleMgr)
7721     M.dump();
7722 }
7723 
7724 /// Return the amount of memory used by memory buffers, breaking down
7725 /// by heap-backed versus mmap'ed memory.
7726 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7727   for (ModuleFile &I : ModuleMgr) {
7728     if (llvm::MemoryBuffer *buf = I.Buffer) {
7729       size_t bytes = buf->getBufferSize();
7730       switch (buf->getBufferKind()) {
7731         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7732           sizes.malloc_bytes += bytes;
7733           break;
7734         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7735           sizes.mmap_bytes += bytes;
7736           break;
7737       }
7738     }
7739   }
7740 }
7741 
7742 void ASTReader::InitializeSema(Sema &S) {
7743   SemaObj = &S;
7744   S.addExternalSource(this);
7745 
7746   // Makes sure any declarations that were deserialized "too early"
7747   // still get added to the identifier's declaration chains.
7748   for (uint64_t ID : PreloadedDeclIDs) {
7749     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7750     pushExternalDeclIntoScope(D, D->getDeclName());
7751   }
7752   PreloadedDeclIDs.clear();
7753 
7754   // FIXME: What happens if these are changed by a module import?
7755   if (!FPPragmaOptions.empty()) {
7756     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7757     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7758   }
7759 
7760   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7761   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7762   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7763 
7764   UpdateSema();
7765 }
7766 
7767 void ASTReader::UpdateSema() {
7768   assert(SemaObj && "no Sema to update");
7769 
7770   // Load the offsets of the declarations that Sema references.
7771   // They will be lazily deserialized when needed.
7772   if (!SemaDeclRefs.empty()) {
7773     assert(SemaDeclRefs.size() % 3 == 0);
7774     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7775       if (!SemaObj->StdNamespace)
7776         SemaObj->StdNamespace = SemaDeclRefs[I];
7777       if (!SemaObj->StdBadAlloc)
7778         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7779       if (!SemaObj->StdAlignValT)
7780         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7781     }
7782     SemaDeclRefs.clear();
7783   }
7784 
7785   // Update the state of pragmas. Use the same API as if we had encountered the
7786   // pragma in the source.
7787   if(OptimizeOffPragmaLocation.isValid())
7788     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7789   if (PragmaMSStructState != -1)
7790     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7791   if (PointersToMembersPragmaLocation.isValid()) {
7792     SemaObj->ActOnPragmaMSPointersToMembers(
7793         (LangOptions::PragmaMSPointersToMembersKind)
7794             PragmaMSPointersToMembersState,
7795         PointersToMembersPragmaLocation);
7796   }
7797   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7798 
7799   if (PragmaPackCurrentValue) {
7800     // The bottom of the stack might have a default value. It must be adjusted
7801     // to the current value to ensure that the packing state is preserved after
7802     // popping entries that were included/imported from a PCH/module.
7803     bool DropFirst = false;
7804     if (!PragmaPackStack.empty() &&
7805         PragmaPackStack.front().Location.isInvalid()) {
7806       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7807              "Expected a default alignment value");
7808       SemaObj->PackStack.Stack.emplace_back(
7809           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7810           SemaObj->PackStack.CurrentPragmaLocation,
7811           PragmaPackStack.front().PushLocation);
7812       DropFirst = true;
7813     }
7814     for (const auto &Entry :
7815          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7816       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7817                                             Entry.Location, Entry.PushLocation);
7818     if (PragmaPackCurrentLocation.isInvalid()) {
7819       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7820              "Expected a default alignment value");
7821       // Keep the current values.
7822     } else {
7823       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7824       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7825     }
7826   }
7827 }
7828 
7829 IdentifierInfo *ASTReader::get(StringRef Name) {
7830   // Note that we are loading an identifier.
7831   Deserializing AnIdentifier(this);
7832 
7833   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7834                                   NumIdentifierLookups,
7835                                   NumIdentifierLookupHits);
7836 
7837   // We don't need to do identifier table lookups in C++ modules (we preload
7838   // all interesting declarations, and don't need to use the scope for name
7839   // lookups). Perform the lookup in PCH files, though, since we don't build
7840   // a complete initial identifier table if we're carrying on from a PCH.
7841   if (PP.getLangOpts().CPlusPlus) {
7842     for (auto F : ModuleMgr.pch_modules())
7843       if (Visitor(*F))
7844         break;
7845   } else {
7846     // If there is a global index, look there first to determine which modules
7847     // provably do not have any results for this identifier.
7848     GlobalModuleIndex::HitSet Hits;
7849     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7850     if (!loadGlobalIndex()) {
7851       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7852         HitsPtr = &Hits;
7853       }
7854     }
7855 
7856     ModuleMgr.visit(Visitor, HitsPtr);
7857   }
7858 
7859   IdentifierInfo *II = Visitor.getIdentifierInfo();
7860   markIdentifierUpToDate(II);
7861   return II;
7862 }
7863 
7864 namespace clang {
7865 
7866   /// An identifier-lookup iterator that enumerates all of the
7867   /// identifiers stored within a set of AST files.
7868   class ASTIdentifierIterator : public IdentifierIterator {
7869     /// The AST reader whose identifiers are being enumerated.
7870     const ASTReader &Reader;
7871 
7872     /// The current index into the chain of AST files stored in
7873     /// the AST reader.
7874     unsigned Index;
7875 
7876     /// The current position within the identifier lookup table
7877     /// of the current AST file.
7878     ASTIdentifierLookupTable::key_iterator Current;
7879 
7880     /// The end position within the identifier lookup table of
7881     /// the current AST file.
7882     ASTIdentifierLookupTable::key_iterator End;
7883 
7884     /// Whether to skip any modules in the ASTReader.
7885     bool SkipModules;
7886 
7887   public:
7888     explicit ASTIdentifierIterator(const ASTReader &Reader,
7889                                    bool SkipModules = false);
7890 
7891     StringRef Next() override;
7892   };
7893 
7894 } // namespace clang
7895 
7896 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7897                                              bool SkipModules)
7898     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7899 }
7900 
7901 StringRef ASTIdentifierIterator::Next() {
7902   while (Current == End) {
7903     // If we have exhausted all of our AST files, we're done.
7904     if (Index == 0)
7905       return StringRef();
7906 
7907     --Index;
7908     ModuleFile &F = Reader.ModuleMgr[Index];
7909     if (SkipModules && F.isModule())
7910       continue;
7911 
7912     ASTIdentifierLookupTable *IdTable =
7913         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7914     Current = IdTable->key_begin();
7915     End = IdTable->key_end();
7916   }
7917 
7918   // We have any identifiers remaining in the current AST file; return
7919   // the next one.
7920   StringRef Result = *Current;
7921   ++Current;
7922   return Result;
7923 }
7924 
7925 namespace {
7926 
7927 /// A utility for appending two IdentifierIterators.
7928 class ChainedIdentifierIterator : public IdentifierIterator {
7929   std::unique_ptr<IdentifierIterator> Current;
7930   std::unique_ptr<IdentifierIterator> Queued;
7931 
7932 public:
7933   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7934                             std::unique_ptr<IdentifierIterator> Second)
7935       : Current(std::move(First)), Queued(std::move(Second)) {}
7936 
7937   StringRef Next() override {
7938     if (!Current)
7939       return StringRef();
7940 
7941     StringRef result = Current->Next();
7942     if (!result.empty())
7943       return result;
7944 
7945     // Try the queued iterator, which may itself be empty.
7946     Current.reset();
7947     std::swap(Current, Queued);
7948     return Next();
7949   }
7950 };
7951 
7952 } // namespace
7953 
7954 IdentifierIterator *ASTReader::getIdentifiers() {
7955   if (!loadGlobalIndex()) {
7956     std::unique_ptr<IdentifierIterator> ReaderIter(
7957         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7958     std::unique_ptr<IdentifierIterator> ModulesIter(
7959         GlobalIndex->createIdentifierIterator());
7960     return new ChainedIdentifierIterator(std::move(ReaderIter),
7961                                          std::move(ModulesIter));
7962   }
7963 
7964   return new ASTIdentifierIterator(*this);
7965 }
7966 
7967 namespace clang {
7968 namespace serialization {
7969 
7970   class ReadMethodPoolVisitor {
7971     ASTReader &Reader;
7972     Selector Sel;
7973     unsigned PriorGeneration;
7974     unsigned InstanceBits = 0;
7975     unsigned FactoryBits = 0;
7976     bool InstanceHasMoreThanOneDecl = false;
7977     bool FactoryHasMoreThanOneDecl = false;
7978     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7979     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7980 
7981   public:
7982     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7983                           unsigned PriorGeneration)
7984         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
7985 
7986     bool operator()(ModuleFile &M) {
7987       if (!M.SelectorLookupTable)
7988         return false;
7989 
7990       // If we've already searched this module file, skip it now.
7991       if (M.Generation <= PriorGeneration)
7992         return true;
7993 
7994       ++Reader.NumMethodPoolTableLookups;
7995       ASTSelectorLookupTable *PoolTable
7996         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7997       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
7998       if (Pos == PoolTable->end())
7999         return false;
8000 
8001       ++Reader.NumMethodPoolTableHits;
8002       ++Reader.NumSelectorsRead;
8003       // FIXME: Not quite happy with the statistics here. We probably should
8004       // disable this tracking when called via LoadSelector.
8005       // Also, should entries without methods count as misses?
8006       ++Reader.NumMethodPoolEntriesRead;
8007       ASTSelectorLookupTrait::data_type Data = *Pos;
8008       if (Reader.DeserializationListener)
8009         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8010 
8011       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8012       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8013       InstanceBits = Data.InstanceBits;
8014       FactoryBits = Data.FactoryBits;
8015       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8016       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8017       return true;
8018     }
8019 
8020     /// Retrieve the instance methods found by this visitor.
8021     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8022       return InstanceMethods;
8023     }
8024 
8025     /// Retrieve the instance methods found by this visitor.
8026     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8027       return FactoryMethods;
8028     }
8029 
8030     unsigned getInstanceBits() const { return InstanceBits; }
8031     unsigned getFactoryBits() const { return FactoryBits; }
8032 
8033     bool instanceHasMoreThanOneDecl() const {
8034       return InstanceHasMoreThanOneDecl;
8035     }
8036 
8037     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8038   };
8039 
8040 } // namespace serialization
8041 } // namespace clang
8042 
8043 /// Add the given set of methods to the method list.
8044 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8045                              ObjCMethodList &List) {
8046   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8047     S.addMethodToGlobalList(&List, Methods[I]);
8048   }
8049 }
8050 
8051 void ASTReader::ReadMethodPool(Selector Sel) {
8052   // Get the selector generation and update it to the current generation.
8053   unsigned &Generation = SelectorGeneration[Sel];
8054   unsigned PriorGeneration = Generation;
8055   Generation = getGeneration();
8056   SelectorOutOfDate[Sel] = false;
8057 
8058   // Search for methods defined with this selector.
8059   ++NumMethodPoolLookups;
8060   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8061   ModuleMgr.visit(Visitor);
8062 
8063   if (Visitor.getInstanceMethods().empty() &&
8064       Visitor.getFactoryMethods().empty())
8065     return;
8066 
8067   ++NumMethodPoolHits;
8068 
8069   if (!getSema())
8070     return;
8071 
8072   Sema &S = *getSema();
8073   Sema::GlobalMethodPool::iterator Pos
8074     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8075 
8076   Pos->second.first.setBits(Visitor.getInstanceBits());
8077   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8078   Pos->second.second.setBits(Visitor.getFactoryBits());
8079   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8080 
8081   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8082   // when building a module we keep every method individually and may need to
8083   // update hasMoreThanOneDecl as we add the methods.
8084   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8085   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8086 }
8087 
8088 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8089   if (SelectorOutOfDate[Sel])
8090     ReadMethodPool(Sel);
8091 }
8092 
8093 void ASTReader::ReadKnownNamespaces(
8094                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8095   Namespaces.clear();
8096 
8097   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8098     if (NamespaceDecl *Namespace
8099                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8100       Namespaces.push_back(Namespace);
8101   }
8102 }
8103 
8104 void ASTReader::ReadUndefinedButUsed(
8105     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8106   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8107     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8108     SourceLocation Loc =
8109         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8110     Undefined.insert(std::make_pair(D, Loc));
8111   }
8112 }
8113 
8114 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8115     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8116                                                      Exprs) {
8117   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8118     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8119     uint64_t Count = DelayedDeleteExprs[Idx++];
8120     for (uint64_t C = 0; C < Count; ++C) {
8121       SourceLocation DeleteLoc =
8122           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8123       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8124       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8125     }
8126   }
8127 }
8128 
8129 void ASTReader::ReadTentativeDefinitions(
8130                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8131   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8132     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8133     if (Var)
8134       TentativeDefs.push_back(Var);
8135   }
8136   TentativeDefinitions.clear();
8137 }
8138 
8139 void ASTReader::ReadUnusedFileScopedDecls(
8140                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8141   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8142     DeclaratorDecl *D
8143       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8144     if (D)
8145       Decls.push_back(D);
8146   }
8147   UnusedFileScopedDecls.clear();
8148 }
8149 
8150 void ASTReader::ReadDelegatingConstructors(
8151                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8152   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8153     CXXConstructorDecl *D
8154       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8155     if (D)
8156       Decls.push_back(D);
8157   }
8158   DelegatingCtorDecls.clear();
8159 }
8160 
8161 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8162   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8163     TypedefNameDecl *D
8164       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8165     if (D)
8166       Decls.push_back(D);
8167   }
8168   ExtVectorDecls.clear();
8169 }
8170 
8171 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8172     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8173   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8174        ++I) {
8175     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8176         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8177     if (D)
8178       Decls.insert(D);
8179   }
8180   UnusedLocalTypedefNameCandidates.clear();
8181 }
8182 
8183 void ASTReader::ReadReferencedSelectors(
8184        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8185   if (ReferencedSelectorsData.empty())
8186     return;
8187 
8188   // If there are @selector references added them to its pool. This is for
8189   // implementation of -Wselector.
8190   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8191   unsigned I = 0;
8192   while (I < DataSize) {
8193     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8194     SourceLocation SelLoc
8195       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8196     Sels.push_back(std::make_pair(Sel, SelLoc));
8197   }
8198   ReferencedSelectorsData.clear();
8199 }
8200 
8201 void ASTReader::ReadWeakUndeclaredIdentifiers(
8202        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8203   if (WeakUndeclaredIdentifiers.empty())
8204     return;
8205 
8206   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8207     IdentifierInfo *WeakId
8208       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8209     IdentifierInfo *AliasId
8210       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8211     SourceLocation Loc
8212       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8213     bool Used = WeakUndeclaredIdentifiers[I++];
8214     WeakInfo WI(AliasId, Loc);
8215     WI.setUsed(Used);
8216     WeakIDs.push_back(std::make_pair(WeakId, WI));
8217   }
8218   WeakUndeclaredIdentifiers.clear();
8219 }
8220 
8221 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8222   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8223     ExternalVTableUse VT;
8224     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8225     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8226     VT.DefinitionRequired = VTableUses[Idx++];
8227     VTables.push_back(VT);
8228   }
8229 
8230   VTableUses.clear();
8231 }
8232 
8233 void ASTReader::ReadPendingInstantiations(
8234        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8235   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8236     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8237     SourceLocation Loc
8238       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8239 
8240     Pending.push_back(std::make_pair(D, Loc));
8241   }
8242   PendingInstantiations.clear();
8243 }
8244 
8245 void ASTReader::ReadLateParsedTemplates(
8246     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8247         &LPTMap) {
8248   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8249        /* In loop */) {
8250     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8251 
8252     auto LT = std::make_unique<LateParsedTemplate>();
8253     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8254 
8255     ModuleFile *F = getOwningModuleFile(LT->D);
8256     assert(F && "No module");
8257 
8258     unsigned TokN = LateParsedTemplates[Idx++];
8259     LT->Toks.reserve(TokN);
8260     for (unsigned T = 0; T < TokN; ++T)
8261       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8262 
8263     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8264   }
8265 
8266   LateParsedTemplates.clear();
8267 }
8268 
8269 void ASTReader::LoadSelector(Selector Sel) {
8270   // It would be complicated to avoid reading the methods anyway. So don't.
8271   ReadMethodPool(Sel);
8272 }
8273 
8274 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8275   assert(ID && "Non-zero identifier ID required");
8276   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8277   IdentifiersLoaded[ID - 1] = II;
8278   if (DeserializationListener)
8279     DeserializationListener->IdentifierRead(ID, II);
8280 }
8281 
8282 /// Set the globally-visible declarations associated with the given
8283 /// identifier.
8284 ///
8285 /// If the AST reader is currently in a state where the given declaration IDs
8286 /// cannot safely be resolved, they are queued until it is safe to resolve
8287 /// them.
8288 ///
8289 /// \param II an IdentifierInfo that refers to one or more globally-visible
8290 /// declarations.
8291 ///
8292 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8293 /// visible at global scope.
8294 ///
8295 /// \param Decls if non-null, this vector will be populated with the set of
8296 /// deserialized declarations. These declarations will not be pushed into
8297 /// scope.
8298 void
8299 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8300                               const SmallVectorImpl<uint32_t> &DeclIDs,
8301                                    SmallVectorImpl<Decl *> *Decls) {
8302   if (NumCurrentElementsDeserializing && !Decls) {
8303     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8304     return;
8305   }
8306 
8307   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8308     if (!SemaObj) {
8309       // Queue this declaration so that it will be added to the
8310       // translation unit scope and identifier's declaration chain
8311       // once a Sema object is known.
8312       PreloadedDeclIDs.push_back(DeclIDs[I]);
8313       continue;
8314     }
8315 
8316     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8317 
8318     // If we're simply supposed to record the declarations, do so now.
8319     if (Decls) {
8320       Decls->push_back(D);
8321       continue;
8322     }
8323 
8324     // Introduce this declaration into the translation-unit scope
8325     // and add it to the declaration chain for this identifier, so
8326     // that (unqualified) name lookup will find it.
8327     pushExternalDeclIntoScope(D, II);
8328   }
8329 }
8330 
8331 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8332   if (ID == 0)
8333     return nullptr;
8334 
8335   if (IdentifiersLoaded.empty()) {
8336     Error("no identifier table in AST file");
8337     return nullptr;
8338   }
8339 
8340   ID -= 1;
8341   if (!IdentifiersLoaded[ID]) {
8342     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8343     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8344     ModuleFile *M = I->second;
8345     unsigned Index = ID - M->BaseIdentifierID;
8346     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8347 
8348     // All of the strings in the AST file are preceded by a 16-bit length.
8349     // Extract that 16-bit length to avoid having to execute strlen().
8350     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8351     //  unsigned integers.  This is important to avoid integer overflow when
8352     //  we cast them to 'unsigned'.
8353     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8354     unsigned StrLen = (((unsigned) StrLenPtr[0])
8355                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8356     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8357     IdentifiersLoaded[ID] = &II;
8358     markIdentifierFromAST(*this,  II);
8359     if (DeserializationListener)
8360       DeserializationListener->IdentifierRead(ID + 1, &II);
8361   }
8362 
8363   return IdentifiersLoaded[ID];
8364 }
8365 
8366 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8367   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8368 }
8369 
8370 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8371   if (LocalID < NUM_PREDEF_IDENT_IDS)
8372     return LocalID;
8373 
8374   if (!M.ModuleOffsetMap.empty())
8375     ReadModuleOffsetMap(M);
8376 
8377   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8378     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8379   assert(I != M.IdentifierRemap.end()
8380          && "Invalid index into identifier index remap");
8381 
8382   return LocalID + I->second;
8383 }
8384 
8385 MacroInfo *ASTReader::getMacro(MacroID ID) {
8386   if (ID == 0)
8387     return nullptr;
8388 
8389   if (MacrosLoaded.empty()) {
8390     Error("no macro table in AST file");
8391     return nullptr;
8392   }
8393 
8394   ID -= NUM_PREDEF_MACRO_IDS;
8395   if (!MacrosLoaded[ID]) {
8396     GlobalMacroMapType::iterator I
8397       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8398     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8399     ModuleFile *M = I->second;
8400     unsigned Index = ID - M->BaseMacroID;
8401     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8402 
8403     if (DeserializationListener)
8404       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8405                                          MacrosLoaded[ID]);
8406   }
8407 
8408   return MacrosLoaded[ID];
8409 }
8410 
8411 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8412   if (LocalID < NUM_PREDEF_MACRO_IDS)
8413     return LocalID;
8414 
8415   if (!M.ModuleOffsetMap.empty())
8416     ReadModuleOffsetMap(M);
8417 
8418   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8419     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8420   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8421 
8422   return LocalID + I->second;
8423 }
8424 
8425 serialization::SubmoduleID
8426 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8427   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8428     return LocalID;
8429 
8430   if (!M.ModuleOffsetMap.empty())
8431     ReadModuleOffsetMap(M);
8432 
8433   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8434     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8435   assert(I != M.SubmoduleRemap.end()
8436          && "Invalid index into submodule index remap");
8437 
8438   return LocalID + I->second;
8439 }
8440 
8441 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8442   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8443     assert(GlobalID == 0 && "Unhandled global submodule ID");
8444     return nullptr;
8445   }
8446 
8447   if (GlobalID > SubmodulesLoaded.size()) {
8448     Error("submodule ID out of range in AST file");
8449     return nullptr;
8450   }
8451 
8452   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8453 }
8454 
8455 Module *ASTReader::getModule(unsigned ID) {
8456   return getSubmodule(ID);
8457 }
8458 
8459 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8460   ModuleFile *MF = getOwningModuleFile(D);
8461   return MF && MF->PCHHasObjectFile;
8462 }
8463 
8464 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8465   if (ID & 1) {
8466     // It's a module, look it up by submodule ID.
8467     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8468     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8469   } else {
8470     // It's a prefix (preamble, PCH, ...). Look it up by index.
8471     unsigned IndexFromEnd = ID >> 1;
8472     assert(IndexFromEnd && "got reference to unknown module file");
8473     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8474   }
8475 }
8476 
8477 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8478   if (!F)
8479     return 1;
8480 
8481   // For a file representing a module, use the submodule ID of the top-level
8482   // module as the file ID. For any other kind of file, the number of such
8483   // files loaded beforehand will be the same on reload.
8484   // FIXME: Is this true even if we have an explicit module file and a PCH?
8485   if (F->isModule())
8486     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8487 
8488   auto PCHModules = getModuleManager().pch_modules();
8489   auto I = llvm::find(PCHModules, F);
8490   assert(I != PCHModules.end() && "emitting reference to unknown file");
8491   return (I - PCHModules.end()) << 1;
8492 }
8493 
8494 llvm::Optional<ASTSourceDescriptor>
8495 ASTReader::getSourceDescriptor(unsigned ID) {
8496   if (const Module *M = getSubmodule(ID))
8497     return ASTSourceDescriptor(*M);
8498 
8499   // If there is only a single PCH, return it instead.
8500   // Chained PCH are not supported.
8501   const auto &PCHChain = ModuleMgr.pch_modules();
8502   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8503     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8504     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8505     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8506     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8507                                MF.Signature);
8508   }
8509   return None;
8510 }
8511 
8512 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8513   auto I = DefinitionSource.find(FD);
8514   if (I == DefinitionSource.end())
8515     return EK_ReplyHazy;
8516   return I->second ? EK_Never : EK_Always;
8517 }
8518 
8519 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8520   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8521 }
8522 
8523 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8524   if (ID == 0)
8525     return Selector();
8526 
8527   if (ID > SelectorsLoaded.size()) {
8528     Error("selector ID out of range in AST file");
8529     return Selector();
8530   }
8531 
8532   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8533     // Load this selector from the selector table.
8534     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8535     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8536     ModuleFile &M = *I->second;
8537     ASTSelectorLookupTrait Trait(*this, M);
8538     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8539     SelectorsLoaded[ID - 1] =
8540       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8541     if (DeserializationListener)
8542       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8543   }
8544 
8545   return SelectorsLoaded[ID - 1];
8546 }
8547 
8548 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8549   return DecodeSelector(ID);
8550 }
8551 
8552 uint32_t ASTReader::GetNumExternalSelectors() {
8553   // ID 0 (the null selector) is considered an external selector.
8554   return getTotalNumSelectors() + 1;
8555 }
8556 
8557 serialization::SelectorID
8558 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8559   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8560     return LocalID;
8561 
8562   if (!M.ModuleOffsetMap.empty())
8563     ReadModuleOffsetMap(M);
8564 
8565   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8566     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8567   assert(I != M.SelectorRemap.end()
8568          && "Invalid index into selector index remap");
8569 
8570   return LocalID + I->second;
8571 }
8572 
8573 DeclarationNameLoc
8574 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8575   DeclarationNameLoc DNLoc;
8576   switch (Name.getNameKind()) {
8577   case DeclarationName::CXXConstructorName:
8578   case DeclarationName::CXXDestructorName:
8579   case DeclarationName::CXXConversionFunctionName:
8580     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8581     break;
8582 
8583   case DeclarationName::CXXOperatorName:
8584     DNLoc.CXXOperatorName.BeginOpNameLoc
8585       = readSourceLocation().getRawEncoding();
8586     DNLoc.CXXOperatorName.EndOpNameLoc
8587       = readSourceLocation().getRawEncoding();
8588     break;
8589 
8590   case DeclarationName::CXXLiteralOperatorName:
8591     DNLoc.CXXLiteralOperatorName.OpNameLoc
8592       = readSourceLocation().getRawEncoding();
8593     break;
8594 
8595   case DeclarationName::Identifier:
8596   case DeclarationName::ObjCZeroArgSelector:
8597   case DeclarationName::ObjCOneArgSelector:
8598   case DeclarationName::ObjCMultiArgSelector:
8599   case DeclarationName::CXXUsingDirective:
8600   case DeclarationName::CXXDeductionGuideName:
8601     break;
8602   }
8603   return DNLoc;
8604 }
8605 
8606 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8607   DeclarationNameInfo NameInfo;
8608   NameInfo.setName(readDeclarationName());
8609   NameInfo.setLoc(readSourceLocation());
8610   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8611   return NameInfo;
8612 }
8613 
8614 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8615   Info.QualifierLoc = readNestedNameSpecifierLoc();
8616   unsigned NumTPLists = readInt();
8617   Info.NumTemplParamLists = NumTPLists;
8618   if (NumTPLists) {
8619     Info.TemplParamLists =
8620         new (getContext()) TemplateParameterList *[NumTPLists];
8621     for (unsigned i = 0; i != NumTPLists; ++i)
8622       Info.TemplParamLists[i] = readTemplateParameterList();
8623   }
8624 }
8625 
8626 TemplateParameterList *
8627 ASTRecordReader::readTemplateParameterList() {
8628   SourceLocation TemplateLoc = readSourceLocation();
8629   SourceLocation LAngleLoc = readSourceLocation();
8630   SourceLocation RAngleLoc = readSourceLocation();
8631 
8632   unsigned NumParams = readInt();
8633   SmallVector<NamedDecl *, 16> Params;
8634   Params.reserve(NumParams);
8635   while (NumParams--)
8636     Params.push_back(readDeclAs<NamedDecl>());
8637 
8638   bool HasRequiresClause = readBool();
8639   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8640 
8641   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8642       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8643   return TemplateParams;
8644 }
8645 
8646 void ASTRecordReader::readTemplateArgumentList(
8647                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8648                         bool Canonicalize) {
8649   unsigned NumTemplateArgs = readInt();
8650   TemplArgs.reserve(NumTemplateArgs);
8651   while (NumTemplateArgs--)
8652     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8653 }
8654 
8655 /// Read a UnresolvedSet structure.
8656 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8657   unsigned NumDecls = readInt();
8658   Set.reserve(getContext(), NumDecls);
8659   while (NumDecls--) {
8660     DeclID ID = readDeclID();
8661     AccessSpecifier AS = (AccessSpecifier) readInt();
8662     Set.addLazyDecl(getContext(), ID, AS);
8663   }
8664 }
8665 
8666 CXXBaseSpecifier
8667 ASTRecordReader::readCXXBaseSpecifier() {
8668   bool isVirtual = readBool();
8669   bool isBaseOfClass = readBool();
8670   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8671   bool inheritConstructors = readBool();
8672   TypeSourceInfo *TInfo = readTypeSourceInfo();
8673   SourceRange Range = readSourceRange();
8674   SourceLocation EllipsisLoc = readSourceLocation();
8675   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8676                           EllipsisLoc);
8677   Result.setInheritConstructors(inheritConstructors);
8678   return Result;
8679 }
8680 
8681 CXXCtorInitializer **
8682 ASTRecordReader::readCXXCtorInitializers() {
8683   ASTContext &Context = getContext();
8684   unsigned NumInitializers = readInt();
8685   assert(NumInitializers && "wrote ctor initializers but have no inits");
8686   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8687   for (unsigned i = 0; i != NumInitializers; ++i) {
8688     TypeSourceInfo *TInfo = nullptr;
8689     bool IsBaseVirtual = false;
8690     FieldDecl *Member = nullptr;
8691     IndirectFieldDecl *IndirectMember = nullptr;
8692 
8693     CtorInitializerType Type = (CtorInitializerType) readInt();
8694     switch (Type) {
8695     case CTOR_INITIALIZER_BASE:
8696       TInfo = readTypeSourceInfo();
8697       IsBaseVirtual = readBool();
8698       break;
8699 
8700     case CTOR_INITIALIZER_DELEGATING:
8701       TInfo = readTypeSourceInfo();
8702       break;
8703 
8704      case CTOR_INITIALIZER_MEMBER:
8705       Member = readDeclAs<FieldDecl>();
8706       break;
8707 
8708      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8709       IndirectMember = readDeclAs<IndirectFieldDecl>();
8710       break;
8711     }
8712 
8713     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8714     Expr *Init = readExpr();
8715     SourceLocation LParenLoc = readSourceLocation();
8716     SourceLocation RParenLoc = readSourceLocation();
8717 
8718     CXXCtorInitializer *BOMInit;
8719     if (Type == CTOR_INITIALIZER_BASE)
8720       BOMInit = new (Context)
8721           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8722                              RParenLoc, MemberOrEllipsisLoc);
8723     else if (Type == CTOR_INITIALIZER_DELEGATING)
8724       BOMInit = new (Context)
8725           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8726     else if (Member)
8727       BOMInit = new (Context)
8728           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8729                              Init, RParenLoc);
8730     else
8731       BOMInit = new (Context)
8732           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8733                              LParenLoc, Init, RParenLoc);
8734 
8735     if (/*IsWritten*/readBool()) {
8736       unsigned SourceOrder = readInt();
8737       BOMInit->setSourceOrder(SourceOrder);
8738     }
8739 
8740     CtorInitializers[i] = BOMInit;
8741   }
8742 
8743   return CtorInitializers;
8744 }
8745 
8746 NestedNameSpecifierLoc
8747 ASTRecordReader::readNestedNameSpecifierLoc() {
8748   ASTContext &Context = getContext();
8749   unsigned N = readInt();
8750   NestedNameSpecifierLocBuilder Builder;
8751   for (unsigned I = 0; I != N; ++I) {
8752     auto Kind = readNestedNameSpecifierKind();
8753     switch (Kind) {
8754     case NestedNameSpecifier::Identifier: {
8755       IdentifierInfo *II = readIdentifier();
8756       SourceRange Range = readSourceRange();
8757       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8758       break;
8759     }
8760 
8761     case NestedNameSpecifier::Namespace: {
8762       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8763       SourceRange Range = readSourceRange();
8764       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8765       break;
8766     }
8767 
8768     case NestedNameSpecifier::NamespaceAlias: {
8769       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8770       SourceRange Range = readSourceRange();
8771       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8772       break;
8773     }
8774 
8775     case NestedNameSpecifier::TypeSpec:
8776     case NestedNameSpecifier::TypeSpecWithTemplate: {
8777       bool Template = readBool();
8778       TypeSourceInfo *T = readTypeSourceInfo();
8779       if (!T)
8780         return NestedNameSpecifierLoc();
8781       SourceLocation ColonColonLoc = readSourceLocation();
8782 
8783       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8784       Builder.Extend(Context,
8785                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8786                      T->getTypeLoc(), ColonColonLoc);
8787       break;
8788     }
8789 
8790     case NestedNameSpecifier::Global: {
8791       SourceLocation ColonColonLoc = readSourceLocation();
8792       Builder.MakeGlobal(Context, ColonColonLoc);
8793       break;
8794     }
8795 
8796     case NestedNameSpecifier::Super: {
8797       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8798       SourceRange Range = readSourceRange();
8799       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8800       break;
8801     }
8802     }
8803   }
8804 
8805   return Builder.getWithLocInContext(Context);
8806 }
8807 
8808 SourceRange
8809 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8810                            unsigned &Idx) {
8811   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8812   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8813   return SourceRange(beg, end);
8814 }
8815 
8816 static FixedPointSemantics
8817 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8818                         unsigned &Idx) {
8819   unsigned Width = Record[Idx++];
8820   unsigned Scale = Record[Idx++];
8821   uint64_t Tmp = Record[Idx++];
8822   bool IsSigned = Tmp & 0x1;
8823   bool IsSaturated = Tmp & 0x2;
8824   bool HasUnsignedPadding = Tmp & 0x4;
8825   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8826                              HasUnsignedPadding);
8827 }
8828 
8829 static const llvm::fltSemantics &
8830 readAPFloatSemantics(ASTRecordReader &reader) {
8831   return llvm::APFloatBase::EnumToSemantics(
8832     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8833 }
8834 
8835 APValue ASTRecordReader::readAPValue() {
8836   unsigned Kind = readInt();
8837   switch ((APValue::ValueKind) Kind) {
8838   case APValue::None:
8839     return APValue();
8840   case APValue::Indeterminate:
8841     return APValue::IndeterminateValue();
8842   case APValue::Int:
8843     return APValue(readAPSInt());
8844   case APValue::Float: {
8845     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8846     return APValue(readAPFloat(FloatSema));
8847   }
8848   case APValue::FixedPoint: {
8849     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8850     return APValue(APFixedPoint(readAPInt(), FPSema));
8851   }
8852   case APValue::ComplexInt: {
8853     llvm::APSInt First = readAPSInt();
8854     return APValue(std::move(First), readAPSInt());
8855   }
8856   case APValue::ComplexFloat: {
8857     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8858     llvm::APFloat First = readAPFloat(FloatSema1);
8859     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8860     return APValue(std::move(First), readAPFloat(FloatSema2));
8861   }
8862   case APValue::LValue:
8863   case APValue::Vector:
8864   case APValue::Array:
8865   case APValue::Struct:
8866   case APValue::Union:
8867   case APValue::MemberPointer:
8868   case APValue::AddrLabelDiff:
8869     // TODO : Handle all these APValue::ValueKind.
8870     return APValue();
8871   }
8872   llvm_unreachable("Invalid APValue::ValueKind");
8873 }
8874 
8875 /// Read a floating-point value
8876 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8877   return llvm::APFloat(Sem, readAPInt());
8878 }
8879 
8880 // Read a string
8881 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8882   unsigned Len = Record[Idx++];
8883   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8884   Idx += Len;
8885   return Result;
8886 }
8887 
8888 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8889                                 unsigned &Idx) {
8890   std::string Filename = ReadString(Record, Idx);
8891   ResolveImportedPath(F, Filename);
8892   return Filename;
8893 }
8894 
8895 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8896                                 const RecordData &Record, unsigned &Idx) {
8897   std::string Filename = ReadString(Record, Idx);
8898   if (!BaseDirectory.empty())
8899     ResolveImportedPath(Filename, BaseDirectory);
8900   return Filename;
8901 }
8902 
8903 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8904                                          unsigned &Idx) {
8905   unsigned Major = Record[Idx++];
8906   unsigned Minor = Record[Idx++];
8907   unsigned Subminor = Record[Idx++];
8908   if (Minor == 0)
8909     return VersionTuple(Major);
8910   if (Subminor == 0)
8911     return VersionTuple(Major, Minor - 1);
8912   return VersionTuple(Major, Minor - 1, Subminor - 1);
8913 }
8914 
8915 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8916                                           const RecordData &Record,
8917                                           unsigned &Idx) {
8918   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8919   return CXXTemporary::Create(getContext(), Decl);
8920 }
8921 
8922 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
8923   return Diag(CurrentImportLoc, DiagID);
8924 }
8925 
8926 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
8927   return Diags.Report(Loc, DiagID);
8928 }
8929 
8930 /// Retrieve the identifier table associated with the
8931 /// preprocessor.
8932 IdentifierTable &ASTReader::getIdentifierTable() {
8933   return PP.getIdentifierTable();
8934 }
8935 
8936 /// Record that the given ID maps to the given switch-case
8937 /// statement.
8938 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8939   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8940          "Already have a SwitchCase with this ID");
8941   (*CurrSwitchCaseStmts)[ID] = SC;
8942 }
8943 
8944 /// Retrieve the switch-case statement with the given ID.
8945 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8946   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8947   return (*CurrSwitchCaseStmts)[ID];
8948 }
8949 
8950 void ASTReader::ClearSwitchCaseIDs() {
8951   CurrSwitchCaseStmts->clear();
8952 }
8953 
8954 void ASTReader::ReadComments() {
8955   ASTContext &Context = getContext();
8956   std::vector<RawComment *> Comments;
8957   for (SmallVectorImpl<std::pair<BitstreamCursor,
8958                                  serialization::ModuleFile *>>::iterator
8959        I = CommentsCursors.begin(),
8960        E = CommentsCursors.end();
8961        I != E; ++I) {
8962     Comments.clear();
8963     BitstreamCursor &Cursor = I->first;
8964     serialization::ModuleFile &F = *I->second;
8965     SavedStreamPosition SavedPosition(Cursor);
8966 
8967     RecordData Record;
8968     while (true) {
8969       Expected<llvm::BitstreamEntry> MaybeEntry =
8970           Cursor.advanceSkippingSubblocks(
8971               BitstreamCursor::AF_DontPopBlockAtEnd);
8972       if (!MaybeEntry) {
8973         Error(MaybeEntry.takeError());
8974         return;
8975       }
8976       llvm::BitstreamEntry Entry = MaybeEntry.get();
8977 
8978       switch (Entry.Kind) {
8979       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8980       case llvm::BitstreamEntry::Error:
8981         Error("malformed block record in AST file");
8982         return;
8983       case llvm::BitstreamEntry::EndBlock:
8984         goto NextCursor;
8985       case llvm::BitstreamEntry::Record:
8986         // The interesting case.
8987         break;
8988       }
8989 
8990       // Read a record.
8991       Record.clear();
8992       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
8993       if (!MaybeComment) {
8994         Error(MaybeComment.takeError());
8995         return;
8996       }
8997       switch ((CommentRecordTypes)MaybeComment.get()) {
8998       case COMMENTS_RAW_COMMENT: {
8999         unsigned Idx = 0;
9000         SourceRange SR = ReadSourceRange(F, Record, Idx);
9001         RawComment::CommentKind Kind =
9002             (RawComment::CommentKind) Record[Idx++];
9003         bool IsTrailingComment = Record[Idx++];
9004         bool IsAlmostTrailingComment = Record[Idx++];
9005         Comments.push_back(new (Context) RawComment(
9006             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9007         break;
9008       }
9009       }
9010     }
9011   NextCursor:
9012     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9013         FileToOffsetToComment;
9014     for (RawComment *C : Comments) {
9015       SourceLocation CommentLoc = C->getBeginLoc();
9016       if (CommentLoc.isValid()) {
9017         std::pair<FileID, unsigned> Loc =
9018             SourceMgr.getDecomposedLoc(CommentLoc);
9019         if (Loc.first.isValid())
9020           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9021       }
9022     }
9023   }
9024 }
9025 
9026 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9027                                 bool IncludeSystem, bool Complain,
9028                     llvm::function_ref<void(const serialization::InputFile &IF,
9029                                             bool isSystem)> Visitor) {
9030   unsigned NumUserInputs = MF.NumUserInputFiles;
9031   unsigned NumInputs = MF.InputFilesLoaded.size();
9032   assert(NumUserInputs <= NumInputs);
9033   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9034   for (unsigned I = 0; I < N; ++I) {
9035     bool IsSystem = I >= NumUserInputs;
9036     InputFile IF = getInputFile(MF, I+1, Complain);
9037     Visitor(IF, IsSystem);
9038   }
9039 }
9040 
9041 void ASTReader::visitTopLevelModuleMaps(
9042     serialization::ModuleFile &MF,
9043     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9044   unsigned NumInputs = MF.InputFilesLoaded.size();
9045   for (unsigned I = 0; I < NumInputs; ++I) {
9046     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9047     if (IFI.TopLevelModuleMap)
9048       // FIXME: This unnecessarily re-reads the InputFileInfo.
9049       if (auto *FE = getInputFile(MF, I + 1).getFile())
9050         Visitor(FE);
9051   }
9052 }
9053 
9054 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9055   // If we know the owning module, use it.
9056   if (Module *M = D->getImportedOwningModule())
9057     return M->getFullModuleName();
9058 
9059   // Otherwise, use the name of the top-level module the decl is within.
9060   if (ModuleFile *M = getOwningModuleFile(D))
9061     return M->ModuleName;
9062 
9063   // Not from a module.
9064   return {};
9065 }
9066 
9067 void ASTReader::finishPendingActions() {
9068   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9069          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9070          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9071          !PendingUpdateRecords.empty()) {
9072     // If any identifiers with corresponding top-level declarations have
9073     // been loaded, load those declarations now.
9074     using TopLevelDeclsMap =
9075         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9076     TopLevelDeclsMap TopLevelDecls;
9077 
9078     while (!PendingIdentifierInfos.empty()) {
9079       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9080       SmallVector<uint32_t, 4> DeclIDs =
9081           std::move(PendingIdentifierInfos.back().second);
9082       PendingIdentifierInfos.pop_back();
9083 
9084       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9085     }
9086 
9087     // Load each function type that we deferred loading because it was a
9088     // deduced type that might refer to a local type declared within itself.
9089     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9090       auto *FD = PendingFunctionTypes[I].first;
9091       FD->setType(GetType(PendingFunctionTypes[I].second));
9092 
9093       // If we gave a function a deduced return type, remember that we need to
9094       // propagate that along the redeclaration chain.
9095       auto *DT = FD->getReturnType()->getContainedDeducedType();
9096       if (DT && DT->isDeduced())
9097         PendingDeducedTypeUpdates.insert(
9098             {FD->getCanonicalDecl(), FD->getReturnType()});
9099     }
9100     PendingFunctionTypes.clear();
9101 
9102     // For each decl chain that we wanted to complete while deserializing, mark
9103     // it as "still needs to be completed".
9104     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9105       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9106     }
9107     PendingIncompleteDeclChains.clear();
9108 
9109     // Load pending declaration chains.
9110     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9111       loadPendingDeclChain(PendingDeclChains[I].first,
9112                            PendingDeclChains[I].second);
9113     PendingDeclChains.clear();
9114 
9115     // Make the most recent of the top-level declarations visible.
9116     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9117            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9118       IdentifierInfo *II = TLD->first;
9119       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9120         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9121       }
9122     }
9123 
9124     // Load any pending macro definitions.
9125     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9126       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9127       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9128       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9129       // Initialize the macro history from chained-PCHs ahead of module imports.
9130       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9131            ++IDIdx) {
9132         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9133         if (!Info.M->isModule())
9134           resolvePendingMacro(II, Info);
9135       }
9136       // Handle module imports.
9137       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9138            ++IDIdx) {
9139         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9140         if (Info.M->isModule())
9141           resolvePendingMacro(II, Info);
9142       }
9143     }
9144     PendingMacroIDs.clear();
9145 
9146     // Wire up the DeclContexts for Decls that we delayed setting until
9147     // recursive loading is completed.
9148     while (!PendingDeclContextInfos.empty()) {
9149       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9150       PendingDeclContextInfos.pop_front();
9151       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9152       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9153       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9154     }
9155 
9156     // Perform any pending declaration updates.
9157     while (!PendingUpdateRecords.empty()) {
9158       auto Update = PendingUpdateRecords.pop_back_val();
9159       ReadingKindTracker ReadingKind(Read_Decl, *this);
9160       loadDeclUpdateRecords(Update);
9161     }
9162   }
9163 
9164   // At this point, all update records for loaded decls are in place, so any
9165   // fake class definitions should have become real.
9166   assert(PendingFakeDefinitionData.empty() &&
9167          "faked up a class definition but never saw the real one");
9168 
9169   // If we deserialized any C++ or Objective-C class definitions, any
9170   // Objective-C protocol definitions, or any redeclarable templates, make sure
9171   // that all redeclarations point to the definitions. Note that this can only
9172   // happen now, after the redeclaration chains have been fully wired.
9173   for (Decl *D : PendingDefinitions) {
9174     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9175       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9176         // Make sure that the TagType points at the definition.
9177         const_cast<TagType*>(TagT)->decl = TD;
9178       }
9179 
9180       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9181         for (auto *R = getMostRecentExistingDecl(RD); R;
9182              R = R->getPreviousDecl()) {
9183           assert((R == D) ==
9184                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9185                  "declaration thinks it's the definition but it isn't");
9186           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9187         }
9188       }
9189 
9190       continue;
9191     }
9192 
9193     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9194       // Make sure that the ObjCInterfaceType points at the definition.
9195       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9196         ->Decl = ID;
9197 
9198       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9199         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9200 
9201       continue;
9202     }
9203 
9204     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9205       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9206         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9207 
9208       continue;
9209     }
9210 
9211     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9212     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9213       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9214   }
9215   PendingDefinitions.clear();
9216 
9217   // Load the bodies of any functions or methods we've encountered. We do
9218   // this now (delayed) so that we can be sure that the declaration chains
9219   // have been fully wired up (hasBody relies on this).
9220   // FIXME: We shouldn't require complete redeclaration chains here.
9221   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9222                                PBEnd = PendingBodies.end();
9223        PB != PBEnd; ++PB) {
9224     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9225       // For a function defined inline within a class template, force the
9226       // canonical definition to be the one inside the canonical definition of
9227       // the template. This ensures that we instantiate from a correct view
9228       // of the template.
9229       //
9230       // Sadly we can't do this more generally: we can't be sure that all
9231       // copies of an arbitrary class definition will have the same members
9232       // defined (eg, some member functions may not be instantiated, and some
9233       // special members may or may not have been implicitly defined).
9234       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9235         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9236           continue;
9237 
9238       // FIXME: Check for =delete/=default?
9239       // FIXME: Complain about ODR violations here?
9240       const FunctionDecl *Defn = nullptr;
9241       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9242         FD->setLazyBody(PB->second);
9243       } else {
9244         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9245         mergeDefinitionVisibility(NonConstDefn, FD);
9246 
9247         if (!FD->isLateTemplateParsed() &&
9248             !NonConstDefn->isLateTemplateParsed() &&
9249             FD->getODRHash() != NonConstDefn->getODRHash()) {
9250           if (!isa<CXXMethodDecl>(FD)) {
9251             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9252           } else if (FD->getLexicalParent()->isFileContext() &&
9253                      NonConstDefn->getLexicalParent()->isFileContext()) {
9254             // Only diagnose out-of-line method definitions.  If they are
9255             // in class definitions, then an error will be generated when
9256             // processing the class bodies.
9257             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9258           }
9259         }
9260       }
9261       continue;
9262     }
9263 
9264     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9265     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9266       MD->setLazyBody(PB->second);
9267   }
9268   PendingBodies.clear();
9269 
9270   // Do some cleanup.
9271   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9272     getContext().deduplicateMergedDefinitonsFor(ND);
9273   PendingMergedDefinitionsToDeduplicate.clear();
9274 }
9275 
9276 void ASTReader::diagnoseOdrViolations() {
9277   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9278       PendingFunctionOdrMergeFailures.empty() &&
9279       PendingEnumOdrMergeFailures.empty())
9280     return;
9281 
9282   // Trigger the import of the full definition of each class that had any
9283   // odr-merging problems, so we can produce better diagnostics for them.
9284   // These updates may in turn find and diagnose some ODR failures, so take
9285   // ownership of the set first.
9286   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9287   PendingOdrMergeFailures.clear();
9288   for (auto &Merge : OdrMergeFailures) {
9289     Merge.first->buildLookup();
9290     Merge.first->decls_begin();
9291     Merge.first->bases_begin();
9292     Merge.first->vbases_begin();
9293     for (auto &RecordPair : Merge.second) {
9294       auto *RD = RecordPair.first;
9295       RD->decls_begin();
9296       RD->bases_begin();
9297       RD->vbases_begin();
9298     }
9299   }
9300 
9301   // Trigger the import of functions.
9302   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9303   PendingFunctionOdrMergeFailures.clear();
9304   for (auto &Merge : FunctionOdrMergeFailures) {
9305     Merge.first->buildLookup();
9306     Merge.first->decls_begin();
9307     Merge.first->getBody();
9308     for (auto &FD : Merge.second) {
9309       FD->buildLookup();
9310       FD->decls_begin();
9311       FD->getBody();
9312     }
9313   }
9314 
9315   // Trigger the import of enums.
9316   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9317   PendingEnumOdrMergeFailures.clear();
9318   for (auto &Merge : EnumOdrMergeFailures) {
9319     Merge.first->decls_begin();
9320     for (auto &Enum : Merge.second) {
9321       Enum->decls_begin();
9322     }
9323   }
9324 
9325   // For each declaration from a merged context, check that the canonical
9326   // definition of that context also contains a declaration of the same
9327   // entity.
9328   //
9329   // Caution: this loop does things that might invalidate iterators into
9330   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9331   while (!PendingOdrMergeChecks.empty()) {
9332     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9333 
9334     // FIXME: Skip over implicit declarations for now. This matters for things
9335     // like implicitly-declared special member functions. This isn't entirely
9336     // correct; we can end up with multiple unmerged declarations of the same
9337     // implicit entity.
9338     if (D->isImplicit())
9339       continue;
9340 
9341     DeclContext *CanonDef = D->getDeclContext();
9342 
9343     bool Found = false;
9344     const Decl *DCanon = D->getCanonicalDecl();
9345 
9346     for (auto RI : D->redecls()) {
9347       if (RI->getLexicalDeclContext() == CanonDef) {
9348         Found = true;
9349         break;
9350       }
9351     }
9352     if (Found)
9353       continue;
9354 
9355     // Quick check failed, time to do the slow thing. Note, we can't just
9356     // look up the name of D in CanonDef here, because the member that is
9357     // in CanonDef might not be found by name lookup (it might have been
9358     // replaced by a more recent declaration in the lookup table), and we
9359     // can't necessarily find it in the redeclaration chain because it might
9360     // be merely mergeable, not redeclarable.
9361     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9362     for (auto *CanonMember : CanonDef->decls()) {
9363       if (CanonMember->getCanonicalDecl() == DCanon) {
9364         // This can happen if the declaration is merely mergeable and not
9365         // actually redeclarable (we looked for redeclarations earlier).
9366         //
9367         // FIXME: We should be able to detect this more efficiently, without
9368         // pulling in all of the members of CanonDef.
9369         Found = true;
9370         break;
9371       }
9372       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9373         if (ND->getDeclName() == D->getDeclName())
9374           Candidates.push_back(ND);
9375     }
9376 
9377     if (!Found) {
9378       // The AST doesn't like TagDecls becoming invalid after they've been
9379       // completed. We only really need to mark FieldDecls as invalid here.
9380       if (!isa<TagDecl>(D))
9381         D->setInvalidDecl();
9382 
9383       // Ensure we don't accidentally recursively enter deserialization while
9384       // we're producing our diagnostic.
9385       Deserializing RecursionGuard(this);
9386 
9387       std::string CanonDefModule =
9388           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9389       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9390         << D << getOwningModuleNameForDiagnostic(D)
9391         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9392 
9393       if (Candidates.empty())
9394         Diag(cast<Decl>(CanonDef)->getLocation(),
9395              diag::note_module_odr_violation_no_possible_decls) << D;
9396       else {
9397         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9398           Diag(Candidates[I]->getLocation(),
9399                diag::note_module_odr_violation_possible_decl)
9400             << Candidates[I];
9401       }
9402 
9403       DiagnosedOdrMergeFailures.insert(CanonDef);
9404     }
9405   }
9406 
9407   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9408       EnumOdrMergeFailures.empty())
9409     return;
9410 
9411   // Ensure we don't accidentally recursively enter deserialization while
9412   // we're producing our diagnostics.
9413   Deserializing RecursionGuard(this);
9414 
9415   // Common code for hashing helpers.
9416   ODRHash Hash;
9417   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9418     Hash.clear();
9419     Hash.AddQualType(Ty);
9420     return Hash.CalculateHash();
9421   };
9422 
9423   auto ComputeODRHash = [&Hash](const Stmt *S) {
9424     assert(S);
9425     Hash.clear();
9426     Hash.AddStmt(S);
9427     return Hash.CalculateHash();
9428   };
9429 
9430   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9431     assert(D);
9432     Hash.clear();
9433     Hash.AddSubDecl(D);
9434     return Hash.CalculateHash();
9435   };
9436 
9437   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9438     Hash.clear();
9439     Hash.AddTemplateArgument(TA);
9440     return Hash.CalculateHash();
9441   };
9442 
9443   auto ComputeTemplateParameterListODRHash =
9444       [&Hash](const TemplateParameterList *TPL) {
9445         assert(TPL);
9446         Hash.clear();
9447         Hash.AddTemplateParameterList(TPL);
9448         return Hash.CalculateHash();
9449       };
9450 
9451   // Used with err_module_odr_violation_mismatch_decl and
9452   // note_module_odr_violation_mismatch_decl
9453   // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9454   enum ODRMismatchDecl {
9455     EndOfClass,
9456     PublicSpecifer,
9457     PrivateSpecifer,
9458     ProtectedSpecifer,
9459     StaticAssert,
9460     Field,
9461     CXXMethod,
9462     TypeAlias,
9463     TypeDef,
9464     Var,
9465     Friend,
9466     FunctionTemplate,
9467     Other
9468   };
9469 
9470   // Used with err_module_odr_violation_mismatch_decl_diff and
9471   // note_module_odr_violation_mismatch_decl_diff
9472   enum ODRMismatchDeclDifference {
9473     StaticAssertCondition,
9474     StaticAssertMessage,
9475     StaticAssertOnlyMessage,
9476     FieldName,
9477     FieldTypeName,
9478     FieldSingleBitField,
9479     FieldDifferentWidthBitField,
9480     FieldSingleMutable,
9481     FieldSingleInitializer,
9482     FieldDifferentInitializers,
9483     MethodName,
9484     MethodDeleted,
9485     MethodDefaulted,
9486     MethodVirtual,
9487     MethodStatic,
9488     MethodVolatile,
9489     MethodConst,
9490     MethodInline,
9491     MethodNumberParameters,
9492     MethodParameterType,
9493     MethodParameterName,
9494     MethodParameterSingleDefaultArgument,
9495     MethodParameterDifferentDefaultArgument,
9496     MethodNoTemplateArguments,
9497     MethodDifferentNumberTemplateArguments,
9498     MethodDifferentTemplateArgument,
9499     MethodSingleBody,
9500     MethodDifferentBody,
9501     TypedefName,
9502     TypedefType,
9503     VarName,
9504     VarType,
9505     VarSingleInitializer,
9506     VarDifferentInitializer,
9507     VarConstexpr,
9508     FriendTypeFunction,
9509     FriendType,
9510     FriendFunction,
9511     FunctionTemplateDifferentNumberParameters,
9512     FunctionTemplateParameterDifferentKind,
9513     FunctionTemplateParameterName,
9514     FunctionTemplateParameterSingleDefaultArgument,
9515     FunctionTemplateParameterDifferentDefaultArgument,
9516     FunctionTemplateParameterDifferentType,
9517     FunctionTemplatePackParameter,
9518   };
9519 
9520   // These lambdas have the common portions of the ODR diagnostics.  This
9521   // has the same return as Diag(), so addition parameters can be passed
9522   // in with operator<<
9523   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9524                                  SourceLocation Loc, SourceRange Range,
9525                                  ODRMismatchDeclDifference DiffType) {
9526     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9527            << FirstRecord << FirstModule.empty() << FirstModule << Range
9528            << DiffType;
9529   };
9530   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9531                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9532     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9533            << SecondModule << Range << DiffType;
9534   };
9535 
9536   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9537                        &ComputeQualTypeODRHash, &ComputeODRHash](
9538                           NamedDecl *FirstRecord, StringRef FirstModule,
9539                           StringRef SecondModule, FieldDecl *FirstField,
9540                           FieldDecl *SecondField) {
9541     IdentifierInfo *FirstII = FirstField->getIdentifier();
9542     IdentifierInfo *SecondII = SecondField->getIdentifier();
9543     if (FirstII->getName() != SecondII->getName()) {
9544       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9545                        FirstField->getSourceRange(), FieldName)
9546           << FirstII;
9547       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9548                       SecondField->getSourceRange(), FieldName)
9549           << SecondII;
9550 
9551       return true;
9552     }
9553 
9554     assert(getContext().hasSameType(FirstField->getType(),
9555                                     SecondField->getType()));
9556 
9557     QualType FirstType = FirstField->getType();
9558     QualType SecondType = SecondField->getType();
9559     if (ComputeQualTypeODRHash(FirstType) !=
9560         ComputeQualTypeODRHash(SecondType)) {
9561       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9562                        FirstField->getSourceRange(), FieldTypeName)
9563           << FirstII << FirstType;
9564       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9565                       SecondField->getSourceRange(), FieldTypeName)
9566           << SecondII << SecondType;
9567 
9568       return true;
9569     }
9570 
9571     const bool IsFirstBitField = FirstField->isBitField();
9572     const bool IsSecondBitField = SecondField->isBitField();
9573     if (IsFirstBitField != IsSecondBitField) {
9574       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9575                        FirstField->getSourceRange(), FieldSingleBitField)
9576           << FirstII << IsFirstBitField;
9577       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9578                       SecondField->getSourceRange(), FieldSingleBitField)
9579           << SecondII << IsSecondBitField;
9580       return true;
9581     }
9582 
9583     if (IsFirstBitField && IsSecondBitField) {
9584       unsigned FirstBitWidthHash =
9585           ComputeODRHash(FirstField->getBitWidth());
9586       unsigned SecondBitWidthHash =
9587           ComputeODRHash(SecondField->getBitWidth());
9588       if (FirstBitWidthHash != SecondBitWidthHash) {
9589         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9590                          FirstField->getSourceRange(),
9591                          FieldDifferentWidthBitField)
9592             << FirstII << FirstField->getBitWidth()->getSourceRange();
9593         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9594                         SecondField->getSourceRange(),
9595                         FieldDifferentWidthBitField)
9596             << SecondII << SecondField->getBitWidth()->getSourceRange();
9597         return true;
9598       }
9599     }
9600 
9601     if (!PP.getLangOpts().CPlusPlus)
9602       return false;
9603 
9604     const bool IsFirstMutable = FirstField->isMutable();
9605     const bool IsSecondMutable = SecondField->isMutable();
9606     if (IsFirstMutable != IsSecondMutable) {
9607       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9608                        FirstField->getSourceRange(), FieldSingleMutable)
9609           << FirstII << IsFirstMutable;
9610       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9611                       SecondField->getSourceRange(), FieldSingleMutable)
9612           << SecondII << IsSecondMutable;
9613       return true;
9614     }
9615 
9616     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9617     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9618     if ((!FirstInitializer && SecondInitializer) ||
9619         (FirstInitializer && !SecondInitializer)) {
9620       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9621                        FirstField->getSourceRange(), FieldSingleInitializer)
9622           << FirstII << (FirstInitializer != nullptr);
9623       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9624                       SecondField->getSourceRange(), FieldSingleInitializer)
9625           << SecondII << (SecondInitializer != nullptr);
9626       return true;
9627     }
9628 
9629     if (FirstInitializer && SecondInitializer) {
9630       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9631       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9632       if (FirstInitHash != SecondInitHash) {
9633         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9634                          FirstField->getSourceRange(),
9635                          FieldDifferentInitializers)
9636             << FirstII << FirstInitializer->getSourceRange();
9637         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9638                         SecondField->getSourceRange(),
9639                         FieldDifferentInitializers)
9640             << SecondII << SecondInitializer->getSourceRange();
9641         return true;
9642       }
9643     }
9644 
9645     return false;
9646   };
9647 
9648   auto ODRDiagTypeDefOrAlias =
9649       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9650           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9651           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9652           bool IsTypeAlias) {
9653         auto FirstName = FirstTD->getDeclName();
9654         auto SecondName = SecondTD->getDeclName();
9655         if (FirstName != SecondName) {
9656           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9657                            FirstTD->getSourceRange(), TypedefName)
9658               << IsTypeAlias << FirstName;
9659           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9660                           SecondTD->getSourceRange(), TypedefName)
9661               << IsTypeAlias << SecondName;
9662           return true;
9663         }
9664 
9665         QualType FirstType = FirstTD->getUnderlyingType();
9666         QualType SecondType = SecondTD->getUnderlyingType();
9667         if (ComputeQualTypeODRHash(FirstType) !=
9668             ComputeQualTypeODRHash(SecondType)) {
9669           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9670                            FirstTD->getSourceRange(), TypedefType)
9671               << IsTypeAlias << FirstName << FirstType;
9672           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9673                           SecondTD->getSourceRange(), TypedefType)
9674               << IsTypeAlias << SecondName << SecondType;
9675           return true;
9676         }
9677 
9678         return false;
9679   };
9680 
9681   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9682                      &ComputeQualTypeODRHash, &ComputeODRHash,
9683                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9684                            StringRef SecondModule, VarDecl *FirstVD,
9685                            VarDecl *SecondVD) {
9686     auto FirstName = FirstVD->getDeclName();
9687     auto SecondName = SecondVD->getDeclName();
9688     if (FirstName != SecondName) {
9689       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9690                        FirstVD->getSourceRange(), VarName)
9691           << FirstName;
9692       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9693                       SecondVD->getSourceRange(), VarName)
9694           << SecondName;
9695       return true;
9696     }
9697 
9698     QualType FirstType = FirstVD->getType();
9699     QualType SecondType = SecondVD->getType();
9700     if (ComputeQualTypeODRHash(FirstType) !=
9701         ComputeQualTypeODRHash(SecondType)) {
9702       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9703                        FirstVD->getSourceRange(), VarType)
9704           << FirstName << FirstType;
9705       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9706                       SecondVD->getSourceRange(), VarType)
9707           << SecondName << SecondType;
9708       return true;
9709     }
9710 
9711     if (!PP.getLangOpts().CPlusPlus)
9712       return false;
9713 
9714     const Expr *FirstInit = FirstVD->getInit();
9715     const Expr *SecondInit = SecondVD->getInit();
9716     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9717       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9718                        FirstVD->getSourceRange(), VarSingleInitializer)
9719           << FirstName << (FirstInit == nullptr)
9720           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9721       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9722                       SecondVD->getSourceRange(), VarSingleInitializer)
9723           << SecondName << (SecondInit == nullptr)
9724           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9725       return true;
9726     }
9727 
9728     if (FirstInit && SecondInit &&
9729         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9730       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9731                        FirstVD->getSourceRange(), VarDifferentInitializer)
9732           << FirstName << FirstInit->getSourceRange();
9733       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9734                       SecondVD->getSourceRange(), VarDifferentInitializer)
9735           << SecondName << SecondInit->getSourceRange();
9736       return true;
9737     }
9738 
9739     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9740     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9741     if (FirstIsConstexpr != SecondIsConstexpr) {
9742       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9743                        FirstVD->getSourceRange(), VarConstexpr)
9744           << FirstName << FirstIsConstexpr;
9745       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9746                       SecondVD->getSourceRange(), VarConstexpr)
9747           << SecondName << SecondIsConstexpr;
9748       return true;
9749     }
9750     return false;
9751   };
9752 
9753   auto DifferenceSelector = [](Decl *D) {
9754     assert(D && "valid Decl required");
9755     switch (D->getKind()) {
9756     default:
9757       return Other;
9758     case Decl::AccessSpec:
9759       switch (D->getAccess()) {
9760       case AS_public:
9761         return PublicSpecifer;
9762       case AS_private:
9763         return PrivateSpecifer;
9764       case AS_protected:
9765         return ProtectedSpecifer;
9766       case AS_none:
9767         break;
9768       }
9769       llvm_unreachable("Invalid access specifier");
9770     case Decl::StaticAssert:
9771       return StaticAssert;
9772     case Decl::Field:
9773       return Field;
9774     case Decl::CXXMethod:
9775     case Decl::CXXConstructor:
9776     case Decl::CXXDestructor:
9777       return CXXMethod;
9778     case Decl::TypeAlias:
9779       return TypeAlias;
9780     case Decl::Typedef:
9781       return TypeDef;
9782     case Decl::Var:
9783       return Var;
9784     case Decl::Friend:
9785       return Friend;
9786     case Decl::FunctionTemplate:
9787       return FunctionTemplate;
9788     }
9789   };
9790 
9791   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9792   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9793                                                  RecordDecl *Record,
9794                                                  const DeclContext *DC) {
9795     for (auto *D : Record->decls()) {
9796       if (!ODRHash::isWhitelistedDecl(D, DC))
9797         continue;
9798       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9799     }
9800   };
9801 
9802   struct DiffResult {
9803     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9804     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9805   };
9806 
9807   // If there is a diagnoseable difference, FirstDiffType and
9808   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9809   // filled in if not EndOfClass.
9810   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9811                                              DeclHashes &SecondHashes) {
9812     DiffResult DR;
9813     auto FirstIt = FirstHashes.begin();
9814     auto SecondIt = SecondHashes.begin();
9815     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9816       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9817           FirstIt->second == SecondIt->second) {
9818         ++FirstIt;
9819         ++SecondIt;
9820         continue;
9821       }
9822 
9823       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9824       DR.SecondDecl =
9825           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9826 
9827       DR.FirstDiffType =
9828           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9829       DR.SecondDiffType =
9830           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9831       return DR;
9832     }
9833     return DR;
9834   };
9835 
9836   // Use this to diagnose that an unexpected Decl was encountered
9837   // or no difference was detected. This causes a generic error
9838   // message to be emitted.
9839   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9840                                       StringRef FirstModule,
9841                                       NamedDecl *SecondRecord,
9842                                       StringRef SecondModule) {
9843     Diag(FirstRecord->getLocation(),
9844          diag::err_module_odr_violation_different_definitions)
9845         << FirstRecord << FirstModule.empty() << FirstModule;
9846 
9847     if (DR.FirstDecl) {
9848       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9849           << FirstRecord << DR.FirstDecl->getSourceRange();
9850     }
9851 
9852     Diag(SecondRecord->getLocation(),
9853          diag::note_module_odr_violation_different_definitions)
9854         << SecondModule;
9855 
9856     if (DR.SecondDecl) {
9857       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9858           << DR.SecondDecl->getSourceRange();
9859     }
9860   };
9861 
9862   auto DiagnoseODRMismatch =
9863       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9864              NamedDecl *SecondRecord, StringRef SecondModule) {
9865         SourceLocation FirstLoc;
9866         SourceRange FirstRange;
9867         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9868         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9869           FirstLoc = FirstTag->getBraceRange().getEnd();
9870         } else {
9871           FirstLoc = DR.FirstDecl->getLocation();
9872           FirstRange = DR.FirstDecl->getSourceRange();
9873         }
9874         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9875             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9876             << DR.FirstDiffType;
9877 
9878         SourceLocation SecondLoc;
9879         SourceRange SecondRange;
9880         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9881         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9882           SecondLoc = SecondTag->getBraceRange().getEnd();
9883         } else {
9884           SecondLoc = DR.SecondDecl->getLocation();
9885           SecondRange = DR.SecondDecl->getSourceRange();
9886         }
9887         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9888             << SecondModule << SecondRange << DR.SecondDiffType;
9889       };
9890 
9891   // Issue any pending ODR-failure diagnostics.
9892   for (auto &Merge : OdrMergeFailures) {
9893     // If we've already pointed out a specific problem with this class, don't
9894     // bother issuing a general "something's different" diagnostic.
9895     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9896       continue;
9897 
9898     bool Diagnosed = false;
9899     CXXRecordDecl *FirstRecord = Merge.first;
9900     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9901     for (auto &RecordPair : Merge.second) {
9902       CXXRecordDecl *SecondRecord = RecordPair.first;
9903       // Multiple different declarations got merged together; tell the user
9904       // where they came from.
9905       if (FirstRecord == SecondRecord)
9906         continue;
9907 
9908       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9909 
9910       auto *FirstDD = FirstRecord->DefinitionData;
9911       auto *SecondDD = RecordPair.second;
9912 
9913       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9914 
9915       // Diagnostics from DefinitionData are emitted here.
9916       if (FirstDD != SecondDD) {
9917         enum ODRDefinitionDataDifference {
9918           NumBases,
9919           NumVBases,
9920           BaseType,
9921           BaseVirtual,
9922           BaseAccess,
9923         };
9924         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
9925                                  this](SourceLocation Loc, SourceRange Range,
9926                                        ODRDefinitionDataDifference DiffType) {
9927           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9928                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9929                  << DiffType;
9930         };
9931         auto ODRDiagBaseNote = [&SecondModule,
9932                                 this](SourceLocation Loc, SourceRange Range,
9933                                       ODRDefinitionDataDifference DiffType) {
9934           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9935                  << SecondModule << Range << DiffType;
9936         };
9937 
9938         unsigned FirstNumBases = FirstDD->NumBases;
9939         unsigned FirstNumVBases = FirstDD->NumVBases;
9940         unsigned SecondNumBases = SecondDD->NumBases;
9941         unsigned SecondNumVBases = SecondDD->NumVBases;
9942 
9943         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9944           unsigned NumBases = DD->NumBases;
9945           if (NumBases == 0) return SourceRange();
9946           auto bases = DD->bases();
9947           return SourceRange(bases[0].getBeginLoc(),
9948                              bases[NumBases - 1].getEndLoc());
9949         };
9950 
9951         if (FirstNumBases != SecondNumBases) {
9952           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9953                            NumBases)
9954               << FirstNumBases;
9955           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9956                           NumBases)
9957               << SecondNumBases;
9958           Diagnosed = true;
9959           break;
9960         }
9961 
9962         if (FirstNumVBases != SecondNumVBases) {
9963           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9964                            NumVBases)
9965               << FirstNumVBases;
9966           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9967                           NumVBases)
9968               << SecondNumVBases;
9969           Diagnosed = true;
9970           break;
9971         }
9972 
9973         auto FirstBases = FirstDD->bases();
9974         auto SecondBases = SecondDD->bases();
9975         unsigned i = 0;
9976         for (i = 0; i < FirstNumBases; ++i) {
9977           auto FirstBase = FirstBases[i];
9978           auto SecondBase = SecondBases[i];
9979           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9980               ComputeQualTypeODRHash(SecondBase.getType())) {
9981             ODRDiagBaseError(FirstRecord->getLocation(),
9982                              FirstBase.getSourceRange(), BaseType)
9983                 << (i + 1) << FirstBase.getType();
9984             ODRDiagBaseNote(SecondRecord->getLocation(),
9985                             SecondBase.getSourceRange(), BaseType)
9986                 << (i + 1) << SecondBase.getType();
9987             break;
9988           }
9989 
9990           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9991             ODRDiagBaseError(FirstRecord->getLocation(),
9992                              FirstBase.getSourceRange(), BaseVirtual)
9993                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9994             ODRDiagBaseNote(SecondRecord->getLocation(),
9995                             SecondBase.getSourceRange(), BaseVirtual)
9996                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9997             break;
9998           }
9999 
10000           if (FirstBase.getAccessSpecifierAsWritten() !=
10001               SecondBase.getAccessSpecifierAsWritten()) {
10002             ODRDiagBaseError(FirstRecord->getLocation(),
10003                              FirstBase.getSourceRange(), BaseAccess)
10004                 << (i + 1) << FirstBase.getType()
10005                 << (int)FirstBase.getAccessSpecifierAsWritten();
10006             ODRDiagBaseNote(SecondRecord->getLocation(),
10007                             SecondBase.getSourceRange(), BaseAccess)
10008                 << (i + 1) << SecondBase.getType()
10009                 << (int)SecondBase.getAccessSpecifierAsWritten();
10010             break;
10011           }
10012         }
10013 
10014         if (i != FirstNumBases) {
10015           Diagnosed = true;
10016           break;
10017         }
10018       }
10019 
10020       const ClassTemplateDecl *FirstTemplate =
10021           FirstRecord->getDescribedClassTemplate();
10022       const ClassTemplateDecl *SecondTemplate =
10023           SecondRecord->getDescribedClassTemplate();
10024 
10025       assert(!FirstTemplate == !SecondTemplate &&
10026              "Both pointers should be null or non-null");
10027 
10028       enum ODRTemplateDifference {
10029         ParamEmptyName,
10030         ParamName,
10031         ParamSingleDefaultArgument,
10032         ParamDifferentDefaultArgument,
10033       };
10034 
10035       if (FirstTemplate && SecondTemplate) {
10036         DeclHashes FirstTemplateHashes;
10037         DeclHashes SecondTemplateHashes;
10038 
10039         auto PopulateTemplateParameterHashs =
10040             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10041                                      const ClassTemplateDecl *TD) {
10042               for (auto *D : TD->getTemplateParameters()->asArray()) {
10043                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10044               }
10045             };
10046 
10047         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10048         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10049 
10050         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10051                "Number of template parameters should be equal.");
10052 
10053         auto FirstIt = FirstTemplateHashes.begin();
10054         auto FirstEnd = FirstTemplateHashes.end();
10055         auto SecondIt = SecondTemplateHashes.begin();
10056         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10057           if (FirstIt->second == SecondIt->second)
10058             continue;
10059 
10060           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10061                                           SourceLocation Loc, SourceRange Range,
10062                                           ODRTemplateDifference DiffType) {
10063             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10064                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10065                    << DiffType;
10066           };
10067           auto ODRDiagTemplateNote = [&SecondModule, this](
10068                                          SourceLocation Loc, SourceRange Range,
10069                                          ODRTemplateDifference DiffType) {
10070             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10071                    << SecondModule << Range << DiffType;
10072           };
10073 
10074           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10075           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10076 
10077           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10078                  "Parameter Decl's should be the same kind.");
10079 
10080           DeclarationName FirstName = FirstDecl->getDeclName();
10081           DeclarationName SecondName = SecondDecl->getDeclName();
10082 
10083           if (FirstName != SecondName) {
10084             const bool FirstNameEmpty =
10085                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10086             const bool SecondNameEmpty =
10087                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10088             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10089                    "Both template parameters cannot be unnamed.");
10090             ODRDiagTemplateError(FirstDecl->getLocation(),
10091                                  FirstDecl->getSourceRange(),
10092                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10093                 << FirstName;
10094             ODRDiagTemplateNote(SecondDecl->getLocation(),
10095                                 SecondDecl->getSourceRange(),
10096                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10097                 << SecondName;
10098             break;
10099           }
10100 
10101           switch (FirstDecl->getKind()) {
10102           default:
10103             llvm_unreachable("Invalid template parameter type.");
10104           case Decl::TemplateTypeParm: {
10105             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10106             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10107             const bool HasFirstDefaultArgument =
10108                 FirstParam->hasDefaultArgument() &&
10109                 !FirstParam->defaultArgumentWasInherited();
10110             const bool HasSecondDefaultArgument =
10111                 SecondParam->hasDefaultArgument() &&
10112                 !SecondParam->defaultArgumentWasInherited();
10113 
10114             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10115               ODRDiagTemplateError(FirstDecl->getLocation(),
10116                                    FirstDecl->getSourceRange(),
10117                                    ParamSingleDefaultArgument)
10118                   << HasFirstDefaultArgument;
10119               ODRDiagTemplateNote(SecondDecl->getLocation(),
10120                                   SecondDecl->getSourceRange(),
10121                                   ParamSingleDefaultArgument)
10122                   << HasSecondDefaultArgument;
10123               break;
10124             }
10125 
10126             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10127                    "Expecting default arguments.");
10128 
10129             ODRDiagTemplateError(FirstDecl->getLocation(),
10130                                  FirstDecl->getSourceRange(),
10131                                  ParamDifferentDefaultArgument);
10132             ODRDiagTemplateNote(SecondDecl->getLocation(),
10133                                 SecondDecl->getSourceRange(),
10134                                 ParamDifferentDefaultArgument);
10135 
10136             break;
10137           }
10138           case Decl::NonTypeTemplateParm: {
10139             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10140             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10141             const bool HasFirstDefaultArgument =
10142                 FirstParam->hasDefaultArgument() &&
10143                 !FirstParam->defaultArgumentWasInherited();
10144             const bool HasSecondDefaultArgument =
10145                 SecondParam->hasDefaultArgument() &&
10146                 !SecondParam->defaultArgumentWasInherited();
10147 
10148             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10149               ODRDiagTemplateError(FirstDecl->getLocation(),
10150                                    FirstDecl->getSourceRange(),
10151                                    ParamSingleDefaultArgument)
10152                   << HasFirstDefaultArgument;
10153               ODRDiagTemplateNote(SecondDecl->getLocation(),
10154                                   SecondDecl->getSourceRange(),
10155                                   ParamSingleDefaultArgument)
10156                   << HasSecondDefaultArgument;
10157               break;
10158             }
10159 
10160             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10161                    "Expecting default arguments.");
10162 
10163             ODRDiagTemplateError(FirstDecl->getLocation(),
10164                                  FirstDecl->getSourceRange(),
10165                                  ParamDifferentDefaultArgument);
10166             ODRDiagTemplateNote(SecondDecl->getLocation(),
10167                                 SecondDecl->getSourceRange(),
10168                                 ParamDifferentDefaultArgument);
10169 
10170             break;
10171           }
10172           case Decl::TemplateTemplateParm: {
10173             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10174             const auto *SecondParam =
10175                 cast<TemplateTemplateParmDecl>(SecondDecl);
10176             const bool HasFirstDefaultArgument =
10177                 FirstParam->hasDefaultArgument() &&
10178                 !FirstParam->defaultArgumentWasInherited();
10179             const bool HasSecondDefaultArgument =
10180                 SecondParam->hasDefaultArgument() &&
10181                 !SecondParam->defaultArgumentWasInherited();
10182 
10183             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10184               ODRDiagTemplateError(FirstDecl->getLocation(),
10185                                    FirstDecl->getSourceRange(),
10186                                    ParamSingleDefaultArgument)
10187                   << HasFirstDefaultArgument;
10188               ODRDiagTemplateNote(SecondDecl->getLocation(),
10189                                   SecondDecl->getSourceRange(),
10190                                   ParamSingleDefaultArgument)
10191                   << HasSecondDefaultArgument;
10192               break;
10193             }
10194 
10195             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10196                    "Expecting default arguments.");
10197 
10198             ODRDiagTemplateError(FirstDecl->getLocation(),
10199                                  FirstDecl->getSourceRange(),
10200                                  ParamDifferentDefaultArgument);
10201             ODRDiagTemplateNote(SecondDecl->getLocation(),
10202                                 SecondDecl->getSourceRange(),
10203                                 ParamDifferentDefaultArgument);
10204 
10205             break;
10206           }
10207           }
10208 
10209           break;
10210         }
10211 
10212         if (FirstIt != FirstEnd) {
10213           Diagnosed = true;
10214           break;
10215         }
10216       }
10217 
10218       DeclHashes FirstHashes;
10219       DeclHashes SecondHashes;
10220       const DeclContext *DC = FirstRecord;
10221       PopulateHashes(FirstHashes, FirstRecord, DC);
10222       PopulateHashes(SecondHashes, SecondRecord, DC);
10223 
10224       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10225       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10226       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10227       Decl *FirstDecl = DR.FirstDecl;
10228       Decl *SecondDecl = DR.SecondDecl;
10229 
10230       if (FirstDiffType == Other || SecondDiffType == Other) {
10231         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10232                               SecondModule);
10233         Diagnosed = true;
10234         break;
10235       }
10236 
10237       if (FirstDiffType != SecondDiffType) {
10238         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10239                             SecondModule);
10240         Diagnosed = true;
10241         break;
10242       }
10243 
10244       assert(FirstDiffType == SecondDiffType);
10245 
10246       switch (FirstDiffType) {
10247       case Other:
10248       case EndOfClass:
10249       case PublicSpecifer:
10250       case PrivateSpecifer:
10251       case ProtectedSpecifer:
10252         llvm_unreachable("Invalid diff type");
10253 
10254       case StaticAssert: {
10255         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10256         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10257 
10258         Expr *FirstExpr = FirstSA->getAssertExpr();
10259         Expr *SecondExpr = SecondSA->getAssertExpr();
10260         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10261         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10262         if (FirstODRHash != SecondODRHash) {
10263           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10264                            FirstExpr->getSourceRange(), StaticAssertCondition);
10265           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10266                           SecondExpr->getSourceRange(), StaticAssertCondition);
10267           Diagnosed = true;
10268           break;
10269         }
10270 
10271         StringLiteral *FirstStr = FirstSA->getMessage();
10272         StringLiteral *SecondStr = SecondSA->getMessage();
10273         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10274         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10275           SourceLocation FirstLoc, SecondLoc;
10276           SourceRange FirstRange, SecondRange;
10277           if (FirstStr) {
10278             FirstLoc = FirstStr->getBeginLoc();
10279             FirstRange = FirstStr->getSourceRange();
10280           } else {
10281             FirstLoc = FirstSA->getBeginLoc();
10282             FirstRange = FirstSA->getSourceRange();
10283           }
10284           if (SecondStr) {
10285             SecondLoc = SecondStr->getBeginLoc();
10286             SecondRange = SecondStr->getSourceRange();
10287           } else {
10288             SecondLoc = SecondSA->getBeginLoc();
10289             SecondRange = SecondSA->getSourceRange();
10290           }
10291           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10292                            StaticAssertOnlyMessage)
10293               << (FirstStr == nullptr);
10294           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10295                           StaticAssertOnlyMessage)
10296               << (SecondStr == nullptr);
10297           Diagnosed = true;
10298           break;
10299         }
10300 
10301         if (FirstStr && SecondStr &&
10302             FirstStr->getString() != SecondStr->getString()) {
10303           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10304                            FirstStr->getSourceRange(), StaticAssertMessage);
10305           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10306                           SecondStr->getSourceRange(), StaticAssertMessage);
10307           Diagnosed = true;
10308           break;
10309         }
10310         break;
10311       }
10312       case Field: {
10313         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10314                                  cast<FieldDecl>(FirstDecl),
10315                                  cast<FieldDecl>(SecondDecl));
10316         break;
10317       }
10318       case CXXMethod: {
10319         enum {
10320           DiagMethod,
10321           DiagConstructor,
10322           DiagDestructor,
10323         } FirstMethodType,
10324             SecondMethodType;
10325         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10326           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10327           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10328           return DiagMethod;
10329         };
10330         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10331         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10332         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10333         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10334         auto FirstName = FirstMethod->getDeclName();
10335         auto SecondName = SecondMethod->getDeclName();
10336         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10337           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10338                            FirstMethod->getSourceRange(), MethodName)
10339               << FirstMethodType << FirstName;
10340           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10341                           SecondMethod->getSourceRange(), MethodName)
10342               << SecondMethodType << SecondName;
10343 
10344           Diagnosed = true;
10345           break;
10346         }
10347 
10348         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10349         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10350         if (FirstDeleted != SecondDeleted) {
10351           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10352                            FirstMethod->getSourceRange(), MethodDeleted)
10353               << FirstMethodType << FirstName << FirstDeleted;
10354 
10355           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10356                           SecondMethod->getSourceRange(), MethodDeleted)
10357               << SecondMethodType << SecondName << SecondDeleted;
10358           Diagnosed = true;
10359           break;
10360         }
10361 
10362         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10363         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10364         if (FirstDefaulted != SecondDefaulted) {
10365           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10366                            FirstMethod->getSourceRange(), MethodDefaulted)
10367               << FirstMethodType << FirstName << FirstDefaulted;
10368 
10369           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10370                           SecondMethod->getSourceRange(), MethodDefaulted)
10371               << SecondMethodType << SecondName << SecondDefaulted;
10372           Diagnosed = true;
10373           break;
10374         }
10375 
10376         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10377         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10378         const bool FirstPure = FirstMethod->isPure();
10379         const bool SecondPure = SecondMethod->isPure();
10380         if ((FirstVirtual || SecondVirtual) &&
10381             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10382           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10383                            FirstMethod->getSourceRange(), MethodVirtual)
10384               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10385           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10386                           SecondMethod->getSourceRange(), MethodVirtual)
10387               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10388           Diagnosed = true;
10389           break;
10390         }
10391 
10392         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10393         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10394         // class needs to be checked instead.
10395         const auto FirstStorage = FirstMethod->getStorageClass();
10396         const auto SecondStorage = SecondMethod->getStorageClass();
10397         const bool FirstStatic = FirstStorage == SC_Static;
10398         const bool SecondStatic = SecondStorage == SC_Static;
10399         if (FirstStatic != SecondStatic) {
10400           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10401                            FirstMethod->getSourceRange(), MethodStatic)
10402               << FirstMethodType << FirstName << FirstStatic;
10403           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10404                           SecondMethod->getSourceRange(), MethodStatic)
10405               << SecondMethodType << SecondName << SecondStatic;
10406           Diagnosed = true;
10407           break;
10408         }
10409 
10410         const bool FirstVolatile = FirstMethod->isVolatile();
10411         const bool SecondVolatile = SecondMethod->isVolatile();
10412         if (FirstVolatile != SecondVolatile) {
10413           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10414                            FirstMethod->getSourceRange(), MethodVolatile)
10415               << FirstMethodType << FirstName << FirstVolatile;
10416           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10417                           SecondMethod->getSourceRange(), MethodVolatile)
10418               << SecondMethodType << SecondName << SecondVolatile;
10419           Diagnosed = true;
10420           break;
10421         }
10422 
10423         const bool FirstConst = FirstMethod->isConst();
10424         const bool SecondConst = SecondMethod->isConst();
10425         if (FirstConst != SecondConst) {
10426           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10427                            FirstMethod->getSourceRange(), MethodConst)
10428               << FirstMethodType << FirstName << FirstConst;
10429           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10430                           SecondMethod->getSourceRange(), MethodConst)
10431               << SecondMethodType << SecondName << SecondConst;
10432           Diagnosed = true;
10433           break;
10434         }
10435 
10436         const bool FirstInline = FirstMethod->isInlineSpecified();
10437         const bool SecondInline = SecondMethod->isInlineSpecified();
10438         if (FirstInline != SecondInline) {
10439           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10440                            FirstMethod->getSourceRange(), MethodInline)
10441               << FirstMethodType << FirstName << FirstInline;
10442           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10443                           SecondMethod->getSourceRange(), MethodInline)
10444               << SecondMethodType << SecondName << SecondInline;
10445           Diagnosed = true;
10446           break;
10447         }
10448 
10449         const unsigned FirstNumParameters = FirstMethod->param_size();
10450         const unsigned SecondNumParameters = SecondMethod->param_size();
10451         if (FirstNumParameters != SecondNumParameters) {
10452           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10453                            FirstMethod->getSourceRange(),
10454                            MethodNumberParameters)
10455               << FirstMethodType << FirstName << FirstNumParameters;
10456           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10457                           SecondMethod->getSourceRange(),
10458                           MethodNumberParameters)
10459               << SecondMethodType << SecondName << SecondNumParameters;
10460           Diagnosed = true;
10461           break;
10462         }
10463 
10464         // Need this status boolean to know when break out of the switch.
10465         bool ParameterMismatch = false;
10466         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10467           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10468           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10469 
10470           QualType FirstParamType = FirstParam->getType();
10471           QualType SecondParamType = SecondParam->getType();
10472           if (FirstParamType != SecondParamType &&
10473               ComputeQualTypeODRHash(FirstParamType) !=
10474                   ComputeQualTypeODRHash(SecondParamType)) {
10475             if (const DecayedType *ParamDecayedType =
10476                     FirstParamType->getAs<DecayedType>()) {
10477               ODRDiagDeclError(
10478                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10479                   FirstMethod->getSourceRange(), MethodParameterType)
10480                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10481                   << true << ParamDecayedType->getOriginalType();
10482             } else {
10483               ODRDiagDeclError(
10484                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10485                   FirstMethod->getSourceRange(), MethodParameterType)
10486                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10487                   << false;
10488             }
10489 
10490             if (const DecayedType *ParamDecayedType =
10491                     SecondParamType->getAs<DecayedType>()) {
10492               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10493                               SecondMethod->getSourceRange(),
10494                               MethodParameterType)
10495                   << SecondMethodType << SecondName << (I + 1)
10496                   << SecondParamType << true
10497                   << ParamDecayedType->getOriginalType();
10498             } else {
10499               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10500                               SecondMethod->getSourceRange(),
10501                               MethodParameterType)
10502                   << SecondMethodType << SecondName << (I + 1)
10503                   << SecondParamType << false;
10504             }
10505             ParameterMismatch = true;
10506             break;
10507           }
10508 
10509           DeclarationName FirstParamName = FirstParam->getDeclName();
10510           DeclarationName SecondParamName = SecondParam->getDeclName();
10511           if (FirstParamName != SecondParamName) {
10512             ODRDiagDeclError(FirstRecord, FirstModule,
10513                              FirstMethod->getLocation(),
10514                              FirstMethod->getSourceRange(), MethodParameterName)
10515                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10516             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10517                             SecondMethod->getSourceRange(), MethodParameterName)
10518                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10519             ParameterMismatch = true;
10520             break;
10521           }
10522 
10523           const Expr *FirstInit = FirstParam->getInit();
10524           const Expr *SecondInit = SecondParam->getInit();
10525           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10526             ODRDiagDeclError(FirstRecord, FirstModule,
10527                              FirstMethod->getLocation(),
10528                              FirstMethod->getSourceRange(),
10529                              MethodParameterSingleDefaultArgument)
10530                 << FirstMethodType << FirstName << (I + 1)
10531                 << (FirstInit == nullptr)
10532                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10533             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10534                             SecondMethod->getSourceRange(),
10535                             MethodParameterSingleDefaultArgument)
10536                 << SecondMethodType << SecondName << (I + 1)
10537                 << (SecondInit == nullptr)
10538                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10539             ParameterMismatch = true;
10540             break;
10541           }
10542 
10543           if (FirstInit && SecondInit &&
10544               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10545             ODRDiagDeclError(FirstRecord, FirstModule,
10546                              FirstMethod->getLocation(),
10547                              FirstMethod->getSourceRange(),
10548                              MethodParameterDifferentDefaultArgument)
10549                 << FirstMethodType << FirstName << (I + 1)
10550                 << FirstInit->getSourceRange();
10551             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10552                             SecondMethod->getSourceRange(),
10553                             MethodParameterDifferentDefaultArgument)
10554                 << SecondMethodType << SecondName << (I + 1)
10555                 << SecondInit->getSourceRange();
10556             ParameterMismatch = true;
10557             break;
10558 
10559           }
10560         }
10561 
10562         if (ParameterMismatch) {
10563           Diagnosed = true;
10564           break;
10565         }
10566 
10567         const auto *FirstTemplateArgs =
10568             FirstMethod->getTemplateSpecializationArgs();
10569         const auto *SecondTemplateArgs =
10570             SecondMethod->getTemplateSpecializationArgs();
10571 
10572         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10573             (!FirstTemplateArgs && SecondTemplateArgs)) {
10574           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10575                            FirstMethod->getSourceRange(),
10576                            MethodNoTemplateArguments)
10577               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10578           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10579                           SecondMethod->getSourceRange(),
10580                           MethodNoTemplateArguments)
10581               << SecondMethodType << SecondName
10582               << (SecondTemplateArgs != nullptr);
10583 
10584           Diagnosed = true;
10585           break;
10586         }
10587 
10588         if (FirstTemplateArgs && SecondTemplateArgs) {
10589           // Remove pack expansions from argument list.
10590           auto ExpandTemplateArgumentList =
10591               [](const TemplateArgumentList *TAL) {
10592                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10593                 for (const TemplateArgument &TA : TAL->asArray()) {
10594                   if (TA.getKind() != TemplateArgument::Pack) {
10595                     ExpandedList.push_back(&TA);
10596                     continue;
10597                   }
10598                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10599                     ExpandedList.push_back(&PackTA);
10600                   }
10601                 }
10602                 return ExpandedList;
10603               };
10604           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10605               ExpandTemplateArgumentList(FirstTemplateArgs);
10606           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10607               ExpandTemplateArgumentList(SecondTemplateArgs);
10608 
10609           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10610             ODRDiagDeclError(FirstRecord, FirstModule,
10611                              FirstMethod->getLocation(),
10612                              FirstMethod->getSourceRange(),
10613                              MethodDifferentNumberTemplateArguments)
10614                 << FirstMethodType << FirstName
10615                 << (unsigned)FirstExpandedList.size();
10616             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10617                             SecondMethod->getSourceRange(),
10618                             MethodDifferentNumberTemplateArguments)
10619                 << SecondMethodType << SecondName
10620                 << (unsigned)SecondExpandedList.size();
10621 
10622             Diagnosed = true;
10623             break;
10624           }
10625 
10626           bool TemplateArgumentMismatch = false;
10627           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10628             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10629                                    &SecondTA = *SecondExpandedList[i];
10630             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10631                 ComputeTemplateArgumentODRHash(SecondTA)) {
10632               continue;
10633             }
10634 
10635             ODRDiagDeclError(
10636                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10637                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10638                 << FirstMethodType << FirstName << FirstTA << i + 1;
10639             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10640                             SecondMethod->getSourceRange(),
10641                             MethodDifferentTemplateArgument)
10642                 << SecondMethodType << SecondName << SecondTA << i + 1;
10643 
10644             TemplateArgumentMismatch = true;
10645             break;
10646           }
10647 
10648           if (TemplateArgumentMismatch) {
10649             Diagnosed = true;
10650             break;
10651           }
10652         }
10653 
10654         // Compute the hash of the method as if it has no body.
10655         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10656           Hash.clear();
10657           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10658           return Hash.CalculateHash();
10659         };
10660 
10661         // Compare the hash generated to the hash stored.  A difference means
10662         // that a body was present in the original source.  Due to merging,
10663         // the stardard way of detecting a body will not work.
10664         const bool HasFirstBody =
10665             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10666         const bool HasSecondBody =
10667             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10668 
10669         if (HasFirstBody != HasSecondBody) {
10670           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10671                            FirstMethod->getSourceRange(), MethodSingleBody)
10672               << FirstMethodType << FirstName << HasFirstBody;
10673           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10674                           SecondMethod->getSourceRange(), MethodSingleBody)
10675               << SecondMethodType << SecondName << HasSecondBody;
10676           Diagnosed = true;
10677           break;
10678         }
10679 
10680         if (HasFirstBody && HasSecondBody) {
10681           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10682                            FirstMethod->getSourceRange(), MethodDifferentBody)
10683               << FirstMethodType << FirstName;
10684           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10685                           SecondMethod->getSourceRange(), MethodDifferentBody)
10686               << SecondMethodType << SecondName;
10687           Diagnosed = true;
10688           break;
10689         }
10690 
10691         break;
10692       }
10693       case TypeAlias:
10694       case TypeDef: {
10695         Diagnosed = ODRDiagTypeDefOrAlias(
10696             FirstRecord, FirstModule, SecondModule,
10697             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10698             FirstDiffType == TypeAlias);
10699         break;
10700       }
10701       case Var: {
10702         Diagnosed =
10703             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10704                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10705         break;
10706       }
10707       case Friend: {
10708         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10709         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10710 
10711         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10712         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10713 
10714         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10715         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10716 
10717         if (FirstND && SecondND) {
10718           ODRDiagDeclError(FirstRecord, FirstModule,
10719                            FirstFriend->getFriendLoc(),
10720                            FirstFriend->getSourceRange(), FriendFunction)
10721               << FirstND;
10722           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10723                           SecondFriend->getSourceRange(), FriendFunction)
10724               << SecondND;
10725 
10726           Diagnosed = true;
10727           break;
10728         }
10729 
10730         if (FirstTSI && SecondTSI) {
10731           QualType FirstFriendType = FirstTSI->getType();
10732           QualType SecondFriendType = SecondTSI->getType();
10733           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10734                  ComputeQualTypeODRHash(SecondFriendType));
10735           ODRDiagDeclError(FirstRecord, FirstModule,
10736                            FirstFriend->getFriendLoc(),
10737                            FirstFriend->getSourceRange(), FriendType)
10738               << FirstFriendType;
10739           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10740                           SecondFriend->getSourceRange(), FriendType)
10741               << SecondFriendType;
10742           Diagnosed = true;
10743           break;
10744         }
10745 
10746         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10747                          FirstFriend->getSourceRange(), FriendTypeFunction)
10748             << (FirstTSI == nullptr);
10749         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10750                         SecondFriend->getSourceRange(), FriendTypeFunction)
10751             << (SecondTSI == nullptr);
10752 
10753         Diagnosed = true;
10754         break;
10755       }
10756       case FunctionTemplate: {
10757         FunctionTemplateDecl *FirstTemplate =
10758             cast<FunctionTemplateDecl>(FirstDecl);
10759         FunctionTemplateDecl *SecondTemplate =
10760             cast<FunctionTemplateDecl>(SecondDecl);
10761 
10762         TemplateParameterList *FirstTPL =
10763             FirstTemplate->getTemplateParameters();
10764         TemplateParameterList *SecondTPL =
10765             SecondTemplate->getTemplateParameters();
10766 
10767         if (FirstTPL->size() != SecondTPL->size()) {
10768           ODRDiagDeclError(FirstRecord, FirstModule,
10769                            FirstTemplate->getLocation(),
10770                            FirstTemplate->getSourceRange(),
10771                            FunctionTemplateDifferentNumberParameters)
10772               << FirstTemplate << FirstTPL->size();
10773           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10774                           SecondTemplate->getSourceRange(),
10775                           FunctionTemplateDifferentNumberParameters)
10776               << SecondTemplate << SecondTPL->size();
10777 
10778           Diagnosed = true;
10779           break;
10780         }
10781 
10782         bool ParameterMismatch = false;
10783         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10784           NamedDecl *FirstParam = FirstTPL->getParam(i);
10785           NamedDecl *SecondParam = SecondTPL->getParam(i);
10786 
10787           if (FirstParam->getKind() != SecondParam->getKind()) {
10788             enum {
10789               TemplateTypeParameter,
10790               NonTypeTemplateParameter,
10791               TemplateTemplateParameter,
10792             };
10793             auto GetParamType = [](NamedDecl *D) {
10794               switch (D->getKind()) {
10795                 default:
10796                   llvm_unreachable("Unexpected template parameter type");
10797                 case Decl::TemplateTypeParm:
10798                   return TemplateTypeParameter;
10799                 case Decl::NonTypeTemplateParm:
10800                   return NonTypeTemplateParameter;
10801                 case Decl::TemplateTemplateParm:
10802                   return TemplateTemplateParameter;
10803               }
10804             };
10805 
10806             ODRDiagDeclError(FirstRecord, FirstModule,
10807                              FirstTemplate->getLocation(),
10808                              FirstTemplate->getSourceRange(),
10809                              FunctionTemplateParameterDifferentKind)
10810                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10811             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10812                             SecondTemplate->getSourceRange(),
10813                             FunctionTemplateParameterDifferentKind)
10814                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10815 
10816             ParameterMismatch = true;
10817             break;
10818           }
10819 
10820           if (FirstParam->getName() != SecondParam->getName()) {
10821             ODRDiagDeclError(
10822                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10823                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10824                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10825                 << FirstParam;
10826             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10827                             SecondTemplate->getSourceRange(),
10828                             FunctionTemplateParameterName)
10829                 << SecondTemplate << (i + 1)
10830                 << (bool)SecondParam->getIdentifier() << SecondParam;
10831             ParameterMismatch = true;
10832             break;
10833           }
10834 
10835           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10836               isa<TemplateTypeParmDecl>(SecondParam)) {
10837             TemplateTypeParmDecl *FirstTTPD =
10838                 cast<TemplateTypeParmDecl>(FirstParam);
10839             TemplateTypeParmDecl *SecondTTPD =
10840                 cast<TemplateTypeParmDecl>(SecondParam);
10841             bool HasFirstDefaultArgument =
10842                 FirstTTPD->hasDefaultArgument() &&
10843                 !FirstTTPD->defaultArgumentWasInherited();
10844             bool HasSecondDefaultArgument =
10845                 SecondTTPD->hasDefaultArgument() &&
10846                 !SecondTTPD->defaultArgumentWasInherited();
10847             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10848               ODRDiagDeclError(FirstRecord, FirstModule,
10849                                FirstTemplate->getLocation(),
10850                                FirstTemplate->getSourceRange(),
10851                                FunctionTemplateParameterSingleDefaultArgument)
10852                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10853               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10854                               SecondTemplate->getSourceRange(),
10855                               FunctionTemplateParameterSingleDefaultArgument)
10856                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10857               ParameterMismatch = true;
10858               break;
10859             }
10860 
10861             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10862               QualType FirstType = FirstTTPD->getDefaultArgument();
10863               QualType SecondType = SecondTTPD->getDefaultArgument();
10864               if (ComputeQualTypeODRHash(FirstType) !=
10865                   ComputeQualTypeODRHash(SecondType)) {
10866                 ODRDiagDeclError(
10867                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10868                     FirstTemplate->getSourceRange(),
10869                     FunctionTemplateParameterDifferentDefaultArgument)
10870                     << FirstTemplate << (i + 1) << FirstType;
10871                 ODRDiagDeclNote(
10872                     SecondModule, SecondTemplate->getLocation(),
10873                     SecondTemplate->getSourceRange(),
10874                     FunctionTemplateParameterDifferentDefaultArgument)
10875                     << SecondTemplate << (i + 1) << SecondType;
10876                 ParameterMismatch = true;
10877                 break;
10878               }
10879             }
10880 
10881             if (FirstTTPD->isParameterPack() !=
10882                 SecondTTPD->isParameterPack()) {
10883               ODRDiagDeclError(FirstRecord, FirstModule,
10884                                FirstTemplate->getLocation(),
10885                                FirstTemplate->getSourceRange(),
10886                                FunctionTemplatePackParameter)
10887                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10888               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10889                               SecondTemplate->getSourceRange(),
10890                               FunctionTemplatePackParameter)
10891                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10892               ParameterMismatch = true;
10893               break;
10894             }
10895           }
10896 
10897           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10898               isa<TemplateTemplateParmDecl>(SecondParam)) {
10899             TemplateTemplateParmDecl *FirstTTPD =
10900                 cast<TemplateTemplateParmDecl>(FirstParam);
10901             TemplateTemplateParmDecl *SecondTTPD =
10902                 cast<TemplateTemplateParmDecl>(SecondParam);
10903 
10904             TemplateParameterList *FirstTPL =
10905                 FirstTTPD->getTemplateParameters();
10906             TemplateParameterList *SecondTPL =
10907                 SecondTTPD->getTemplateParameters();
10908 
10909             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10910                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10911               ODRDiagDeclError(FirstRecord, FirstModule,
10912                                FirstTemplate->getLocation(),
10913                                FirstTemplate->getSourceRange(),
10914                                FunctionTemplateParameterDifferentType)
10915                   << FirstTemplate << (i + 1);
10916               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10917                               SecondTemplate->getSourceRange(),
10918                               FunctionTemplateParameterDifferentType)
10919                   << SecondTemplate << (i + 1);
10920               ParameterMismatch = true;
10921               break;
10922             }
10923 
10924             bool HasFirstDefaultArgument =
10925                 FirstTTPD->hasDefaultArgument() &&
10926                 !FirstTTPD->defaultArgumentWasInherited();
10927             bool HasSecondDefaultArgument =
10928                 SecondTTPD->hasDefaultArgument() &&
10929                 !SecondTTPD->defaultArgumentWasInherited();
10930             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10931               ODRDiagDeclError(FirstRecord, FirstModule,
10932                                FirstTemplate->getLocation(),
10933                                FirstTemplate->getSourceRange(),
10934                                FunctionTemplateParameterSingleDefaultArgument)
10935                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10936               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10937                               SecondTemplate->getSourceRange(),
10938                               FunctionTemplateParameterSingleDefaultArgument)
10939                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10940               ParameterMismatch = true;
10941               break;
10942             }
10943 
10944             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10945               TemplateArgument FirstTA =
10946                   FirstTTPD->getDefaultArgument().getArgument();
10947               TemplateArgument SecondTA =
10948                   SecondTTPD->getDefaultArgument().getArgument();
10949               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10950                   ComputeTemplateArgumentODRHash(SecondTA)) {
10951                 ODRDiagDeclError(
10952                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10953                     FirstTemplate->getSourceRange(),
10954                     FunctionTemplateParameterDifferentDefaultArgument)
10955                     << FirstTemplate << (i + 1) << FirstTA;
10956                 ODRDiagDeclNote(
10957                     SecondModule, SecondTemplate->getLocation(),
10958                     SecondTemplate->getSourceRange(),
10959                     FunctionTemplateParameterDifferentDefaultArgument)
10960                     << SecondTemplate << (i + 1) << SecondTA;
10961                 ParameterMismatch = true;
10962                 break;
10963               }
10964             }
10965 
10966             if (FirstTTPD->isParameterPack() !=
10967                 SecondTTPD->isParameterPack()) {
10968               ODRDiagDeclError(FirstRecord, FirstModule,
10969                                FirstTemplate->getLocation(),
10970                                FirstTemplate->getSourceRange(),
10971                                FunctionTemplatePackParameter)
10972                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10973               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10974                               SecondTemplate->getSourceRange(),
10975                               FunctionTemplatePackParameter)
10976                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10977               ParameterMismatch = true;
10978               break;
10979             }
10980           }
10981 
10982           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10983               isa<NonTypeTemplateParmDecl>(SecondParam)) {
10984             NonTypeTemplateParmDecl *FirstNTTPD =
10985                 cast<NonTypeTemplateParmDecl>(FirstParam);
10986             NonTypeTemplateParmDecl *SecondNTTPD =
10987                 cast<NonTypeTemplateParmDecl>(SecondParam);
10988 
10989             QualType FirstType = FirstNTTPD->getType();
10990             QualType SecondType = SecondNTTPD->getType();
10991             if (ComputeQualTypeODRHash(FirstType) !=
10992                 ComputeQualTypeODRHash(SecondType)) {
10993               ODRDiagDeclError(FirstRecord, FirstModule,
10994                                FirstTemplate->getLocation(),
10995                                FirstTemplate->getSourceRange(),
10996                                FunctionTemplateParameterDifferentType)
10997                   << FirstTemplate << (i + 1);
10998               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10999                               SecondTemplate->getSourceRange(),
11000                               FunctionTemplateParameterDifferentType)
11001                   << SecondTemplate << (i + 1);
11002               ParameterMismatch = true;
11003               break;
11004             }
11005 
11006             bool HasFirstDefaultArgument =
11007                 FirstNTTPD->hasDefaultArgument() &&
11008                 !FirstNTTPD->defaultArgumentWasInherited();
11009             bool HasSecondDefaultArgument =
11010                 SecondNTTPD->hasDefaultArgument() &&
11011                 !SecondNTTPD->defaultArgumentWasInherited();
11012             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11013               ODRDiagDeclError(FirstRecord, FirstModule,
11014                                FirstTemplate->getLocation(),
11015                                FirstTemplate->getSourceRange(),
11016                                FunctionTemplateParameterSingleDefaultArgument)
11017                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11018               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11019                               SecondTemplate->getSourceRange(),
11020                               FunctionTemplateParameterSingleDefaultArgument)
11021                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11022               ParameterMismatch = true;
11023               break;
11024             }
11025 
11026             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11027               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11028               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11029               if (ComputeODRHash(FirstDefaultArgument) !=
11030                   ComputeODRHash(SecondDefaultArgument)) {
11031                 ODRDiagDeclError(
11032                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11033                     FirstTemplate->getSourceRange(),
11034                     FunctionTemplateParameterDifferentDefaultArgument)
11035                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11036                 ODRDiagDeclNote(
11037                     SecondModule, SecondTemplate->getLocation(),
11038                     SecondTemplate->getSourceRange(),
11039                     FunctionTemplateParameterDifferentDefaultArgument)
11040                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11041                 ParameterMismatch = true;
11042                 break;
11043               }
11044             }
11045 
11046             if (FirstNTTPD->isParameterPack() !=
11047                 SecondNTTPD->isParameterPack()) {
11048               ODRDiagDeclError(FirstRecord, FirstModule,
11049                                FirstTemplate->getLocation(),
11050                                FirstTemplate->getSourceRange(),
11051                                FunctionTemplatePackParameter)
11052                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11053               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11054                               SecondTemplate->getSourceRange(),
11055                               FunctionTemplatePackParameter)
11056                   << SecondTemplate << (i + 1)
11057                   << SecondNTTPD->isParameterPack();
11058               ParameterMismatch = true;
11059               break;
11060             }
11061           }
11062         }
11063 
11064         if (ParameterMismatch) {
11065           Diagnosed = true;
11066           break;
11067         }
11068 
11069         break;
11070       }
11071       }
11072 
11073       if (Diagnosed)
11074         continue;
11075 
11076       Diag(FirstDecl->getLocation(),
11077            diag::err_module_odr_violation_mismatch_decl_unknown)
11078           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11079           << FirstDecl->getSourceRange();
11080       Diag(SecondDecl->getLocation(),
11081            diag::note_module_odr_violation_mismatch_decl_unknown)
11082           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11083       Diagnosed = true;
11084     }
11085 
11086     if (!Diagnosed) {
11087       // All definitions are updates to the same declaration. This happens if a
11088       // module instantiates the declaration of a class template specialization
11089       // and two or more other modules instantiate its definition.
11090       //
11091       // FIXME: Indicate which modules had instantiations of this definition.
11092       // FIXME: How can this even happen?
11093       Diag(Merge.first->getLocation(),
11094            diag::err_module_odr_violation_different_instantiations)
11095         << Merge.first;
11096     }
11097   }
11098 
11099   // Issue ODR failures diagnostics for functions.
11100   for (auto &Merge : FunctionOdrMergeFailures) {
11101     enum ODRFunctionDifference {
11102       ReturnType,
11103       ParameterName,
11104       ParameterType,
11105       ParameterSingleDefaultArgument,
11106       ParameterDifferentDefaultArgument,
11107       FunctionBody,
11108     };
11109 
11110     FunctionDecl *FirstFunction = Merge.first;
11111     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11112 
11113     bool Diagnosed = false;
11114     for (auto &SecondFunction : Merge.second) {
11115 
11116       if (FirstFunction == SecondFunction)
11117         continue;
11118 
11119       std::string SecondModule =
11120           getOwningModuleNameForDiagnostic(SecondFunction);
11121 
11122       auto ODRDiagError = [FirstFunction, &FirstModule,
11123                            this](SourceLocation Loc, SourceRange Range,
11124                                  ODRFunctionDifference DiffType) {
11125         return Diag(Loc, diag::err_module_odr_violation_function)
11126                << FirstFunction << FirstModule.empty() << FirstModule << Range
11127                << DiffType;
11128       };
11129       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11130                                                SourceRange Range,
11131                                                ODRFunctionDifference DiffType) {
11132         return Diag(Loc, diag::note_module_odr_violation_function)
11133                << SecondModule << Range << DiffType;
11134       };
11135 
11136       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11137           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11138         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11139                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11140             << FirstFunction->getReturnType();
11141         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11142                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11143             << SecondFunction->getReturnType();
11144         Diagnosed = true;
11145         break;
11146       }
11147 
11148       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11149              "Merged functions with different number of parameters");
11150 
11151       auto ParamSize = FirstFunction->param_size();
11152       bool ParameterMismatch = false;
11153       for (unsigned I = 0; I < ParamSize; ++I) {
11154         auto *FirstParam = FirstFunction->getParamDecl(I);
11155         auto *SecondParam = SecondFunction->getParamDecl(I);
11156 
11157         assert(getContext().hasSameType(FirstParam->getType(),
11158                                       SecondParam->getType()) &&
11159                "Merged function has different parameter types.");
11160 
11161         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11162           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11163                        ParameterName)
11164               << I + 1 << FirstParam->getDeclName();
11165           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11166                       ParameterName)
11167               << I + 1 << SecondParam->getDeclName();
11168           ParameterMismatch = true;
11169           break;
11170         };
11171 
11172         QualType FirstParamType = FirstParam->getType();
11173         QualType SecondParamType = SecondParam->getType();
11174         if (FirstParamType != SecondParamType &&
11175             ComputeQualTypeODRHash(FirstParamType) !=
11176                 ComputeQualTypeODRHash(SecondParamType)) {
11177           if (const DecayedType *ParamDecayedType =
11178                   FirstParamType->getAs<DecayedType>()) {
11179             ODRDiagError(FirstParam->getLocation(),
11180                          FirstParam->getSourceRange(), ParameterType)
11181                 << (I + 1) << FirstParamType << true
11182                 << ParamDecayedType->getOriginalType();
11183           } else {
11184             ODRDiagError(FirstParam->getLocation(),
11185                          FirstParam->getSourceRange(), ParameterType)
11186                 << (I + 1) << FirstParamType << false;
11187           }
11188 
11189           if (const DecayedType *ParamDecayedType =
11190                   SecondParamType->getAs<DecayedType>()) {
11191             ODRDiagNote(SecondParam->getLocation(),
11192                         SecondParam->getSourceRange(), ParameterType)
11193                 << (I + 1) << SecondParamType << true
11194                 << ParamDecayedType->getOriginalType();
11195           } else {
11196             ODRDiagNote(SecondParam->getLocation(),
11197                         SecondParam->getSourceRange(), ParameterType)
11198                 << (I + 1) << SecondParamType << false;
11199           }
11200           ParameterMismatch = true;
11201           break;
11202         }
11203 
11204         const Expr *FirstInit = FirstParam->getInit();
11205         const Expr *SecondInit = SecondParam->getInit();
11206         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11207           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11208                        ParameterSingleDefaultArgument)
11209               << (I + 1) << (FirstInit == nullptr)
11210               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11211           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11212                       ParameterSingleDefaultArgument)
11213               << (I + 1) << (SecondInit == nullptr)
11214               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11215           ParameterMismatch = true;
11216           break;
11217         }
11218 
11219         if (FirstInit && SecondInit &&
11220             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11221           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11222                        ParameterDifferentDefaultArgument)
11223               << (I + 1) << FirstInit->getSourceRange();
11224           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11225                       ParameterDifferentDefaultArgument)
11226               << (I + 1) << SecondInit->getSourceRange();
11227           ParameterMismatch = true;
11228           break;
11229         }
11230 
11231         assert(ComputeSubDeclODRHash(FirstParam) ==
11232                    ComputeSubDeclODRHash(SecondParam) &&
11233                "Undiagnosed parameter difference.");
11234       }
11235 
11236       if (ParameterMismatch) {
11237         Diagnosed = true;
11238         break;
11239       }
11240 
11241       // If no error has been generated before now, assume the problem is in
11242       // the body and generate a message.
11243       ODRDiagError(FirstFunction->getLocation(),
11244                    FirstFunction->getSourceRange(), FunctionBody);
11245       ODRDiagNote(SecondFunction->getLocation(),
11246                   SecondFunction->getSourceRange(), FunctionBody);
11247       Diagnosed = true;
11248       break;
11249     }
11250     (void)Diagnosed;
11251     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11252   }
11253 
11254   // Issue ODR failures diagnostics for enums.
11255   for (auto &Merge : EnumOdrMergeFailures) {
11256     enum ODREnumDifference {
11257       SingleScopedEnum,
11258       EnumTagKeywordMismatch,
11259       SingleSpecifiedType,
11260       DifferentSpecifiedTypes,
11261       DifferentNumberEnumConstants,
11262       EnumConstantName,
11263       EnumConstantSingleInitilizer,
11264       EnumConstantDifferentInitilizer,
11265     };
11266 
11267     // If we've already pointed out a specific problem with this enum, don't
11268     // bother issuing a general "something's different" diagnostic.
11269     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11270       continue;
11271 
11272     EnumDecl *FirstEnum = Merge.first;
11273     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11274 
11275     using DeclHashes =
11276         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11277     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11278                               DeclHashes &Hashes, EnumDecl *Enum) {
11279       for (auto *D : Enum->decls()) {
11280         // Due to decl merging, the first EnumDecl is the parent of
11281         // Decls in both records.
11282         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11283           continue;
11284         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11285         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11286                             ComputeSubDeclODRHash(D));
11287       }
11288     };
11289     DeclHashes FirstHashes;
11290     PopulateHashes(FirstHashes, FirstEnum);
11291     bool Diagnosed = false;
11292     for (auto &SecondEnum : Merge.second) {
11293 
11294       if (FirstEnum == SecondEnum)
11295         continue;
11296 
11297       std::string SecondModule =
11298           getOwningModuleNameForDiagnostic(SecondEnum);
11299 
11300       auto ODRDiagError = [FirstEnum, &FirstModule,
11301                            this](SourceLocation Loc, SourceRange Range,
11302                                  ODREnumDifference DiffType) {
11303         return Diag(Loc, diag::err_module_odr_violation_enum)
11304                << FirstEnum << FirstModule.empty() << FirstModule << Range
11305                << DiffType;
11306       };
11307       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11308                                                SourceRange Range,
11309                                                ODREnumDifference DiffType) {
11310         return Diag(Loc, diag::note_module_odr_violation_enum)
11311                << SecondModule << Range << DiffType;
11312       };
11313 
11314       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11315         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11316                      SingleScopedEnum)
11317             << FirstEnum->isScoped();
11318         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11319                     SingleScopedEnum)
11320             << SecondEnum->isScoped();
11321         Diagnosed = true;
11322         continue;
11323       }
11324 
11325       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11326         if (FirstEnum->isScopedUsingClassTag() !=
11327             SecondEnum->isScopedUsingClassTag()) {
11328           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11329                        EnumTagKeywordMismatch)
11330               << FirstEnum->isScopedUsingClassTag();
11331           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11332                       EnumTagKeywordMismatch)
11333               << SecondEnum->isScopedUsingClassTag();
11334           Diagnosed = true;
11335           continue;
11336         }
11337       }
11338 
11339       QualType FirstUnderlyingType =
11340           FirstEnum->getIntegerTypeSourceInfo()
11341               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11342               : QualType();
11343       QualType SecondUnderlyingType =
11344           SecondEnum->getIntegerTypeSourceInfo()
11345               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11346               : QualType();
11347       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11348           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11349                        SingleSpecifiedType)
11350               << !FirstUnderlyingType.isNull();
11351           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11352                       SingleSpecifiedType)
11353               << !SecondUnderlyingType.isNull();
11354           Diagnosed = true;
11355           continue;
11356       }
11357 
11358       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11359         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11360             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11361           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11362                        DifferentSpecifiedTypes)
11363               << FirstUnderlyingType;
11364           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11365                       DifferentSpecifiedTypes)
11366               << SecondUnderlyingType;
11367           Diagnosed = true;
11368           continue;
11369         }
11370       }
11371 
11372       DeclHashes SecondHashes;
11373       PopulateHashes(SecondHashes, SecondEnum);
11374 
11375       if (FirstHashes.size() != SecondHashes.size()) {
11376         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11377                      DifferentNumberEnumConstants)
11378             << (int)FirstHashes.size();
11379         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11380                     DifferentNumberEnumConstants)
11381             << (int)SecondHashes.size();
11382         Diagnosed = true;
11383         continue;
11384       }
11385 
11386       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11387         if (FirstHashes[I].second == SecondHashes[I].second)
11388           continue;
11389         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11390         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11391 
11392         if (FirstEnumConstant->getDeclName() !=
11393             SecondEnumConstant->getDeclName()) {
11394 
11395           ODRDiagError(FirstEnumConstant->getLocation(),
11396                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11397               << I + 1 << FirstEnumConstant;
11398           ODRDiagNote(SecondEnumConstant->getLocation(),
11399                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11400               << I + 1 << SecondEnumConstant;
11401           Diagnosed = true;
11402           break;
11403         }
11404 
11405         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11406         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11407         if (!FirstInit && !SecondInit)
11408           continue;
11409 
11410         if (!FirstInit || !SecondInit) {
11411           ODRDiagError(FirstEnumConstant->getLocation(),
11412                        FirstEnumConstant->getSourceRange(),
11413                        EnumConstantSingleInitilizer)
11414               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11415           ODRDiagNote(SecondEnumConstant->getLocation(),
11416                       SecondEnumConstant->getSourceRange(),
11417                       EnumConstantSingleInitilizer)
11418               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11419           Diagnosed = true;
11420           break;
11421         }
11422 
11423         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11424           ODRDiagError(FirstEnumConstant->getLocation(),
11425                        FirstEnumConstant->getSourceRange(),
11426                        EnumConstantDifferentInitilizer)
11427               << I + 1 << FirstEnumConstant;
11428           ODRDiagNote(SecondEnumConstant->getLocation(),
11429                       SecondEnumConstant->getSourceRange(),
11430                       EnumConstantDifferentInitilizer)
11431               << I + 1 << SecondEnumConstant;
11432           Diagnosed = true;
11433           break;
11434         }
11435       }
11436     }
11437 
11438     (void)Diagnosed;
11439     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11440   }
11441 }
11442 
11443 void ASTReader::StartedDeserializing() {
11444   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11445     ReadTimer->startTimer();
11446 }
11447 
11448 void ASTReader::FinishedDeserializing() {
11449   assert(NumCurrentElementsDeserializing &&
11450          "FinishedDeserializing not paired with StartedDeserializing");
11451   if (NumCurrentElementsDeserializing == 1) {
11452     // We decrease NumCurrentElementsDeserializing only after pending actions
11453     // are finished, to avoid recursively re-calling finishPendingActions().
11454     finishPendingActions();
11455   }
11456   --NumCurrentElementsDeserializing;
11457 
11458   if (NumCurrentElementsDeserializing == 0) {
11459     // Propagate exception specification and deduced type updates along
11460     // redeclaration chains.
11461     //
11462     // We do this now rather than in finishPendingActions because we want to
11463     // be able to walk the complete redeclaration chains of the updated decls.
11464     while (!PendingExceptionSpecUpdates.empty() ||
11465            !PendingDeducedTypeUpdates.empty()) {
11466       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11467       PendingExceptionSpecUpdates.clear();
11468       for (auto Update : ESUpdates) {
11469         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11470         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11471         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11472         if (auto *Listener = getContext().getASTMutationListener())
11473           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11474         for (auto *Redecl : Update.second->redecls())
11475           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11476       }
11477 
11478       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11479       PendingDeducedTypeUpdates.clear();
11480       for (auto Update : DTUpdates) {
11481         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11482         // FIXME: If the return type is already deduced, check that it matches.
11483         getContext().adjustDeducedFunctionResultType(Update.first,
11484                                                      Update.second);
11485       }
11486     }
11487 
11488     if (ReadTimer)
11489       ReadTimer->stopTimer();
11490 
11491     diagnoseOdrViolations();
11492 
11493     // We are not in recursive loading, so it's safe to pass the "interesting"
11494     // decls to the consumer.
11495     if (Consumer)
11496       PassInterestingDeclsToConsumer();
11497   }
11498 }
11499 
11500 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11501   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11502     // Remove any fake results before adding any real ones.
11503     auto It = PendingFakeLookupResults.find(II);
11504     if (It != PendingFakeLookupResults.end()) {
11505       for (auto *ND : It->second)
11506         SemaObj->IdResolver.RemoveDecl(ND);
11507       // FIXME: this works around module+PCH performance issue.
11508       // Rather than erase the result from the map, which is O(n), just clear
11509       // the vector of NamedDecls.
11510       It->second.clear();
11511     }
11512   }
11513 
11514   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11515     SemaObj->TUScope->AddDecl(D);
11516   } else if (SemaObj->TUScope) {
11517     // Adding the decl to IdResolver may have failed because it was already in
11518     // (even though it was not added in scope). If it is already in, make sure
11519     // it gets in the scope as well.
11520     if (std::find(SemaObj->IdResolver.begin(Name),
11521                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11522       SemaObj->TUScope->AddDecl(D);
11523   }
11524 }
11525 
11526 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11527                      ASTContext *Context,
11528                      const PCHContainerReader &PCHContainerRdr,
11529                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11530                      StringRef isysroot, bool DisableValidation,
11531                      bool AllowASTWithCompilerErrors,
11532                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11533                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11534                      std::unique_ptr<llvm::Timer> ReadTimer)
11535     : Listener(DisableValidation
11536                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11537                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11538       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11539       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11540       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11541                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11542       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11543       DisableValidation(DisableValidation),
11544       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11545       AllowConfigurationMismatch(AllowConfigurationMismatch),
11546       ValidateSystemInputs(ValidateSystemInputs),
11547       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11548       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11549   SourceMgr.setExternalSLocEntrySource(this);
11550 
11551   for (const auto &Ext : Extensions) {
11552     auto BlockName = Ext->getExtensionMetadata().BlockName;
11553     auto Known = ModuleFileExtensions.find(BlockName);
11554     if (Known != ModuleFileExtensions.end()) {
11555       Diags.Report(diag::warn_duplicate_module_file_extension)
11556         << BlockName;
11557       continue;
11558     }
11559 
11560     ModuleFileExtensions.insert({BlockName, Ext});
11561   }
11562 }
11563 
11564 ASTReader::~ASTReader() {
11565   if (OwnsDeserializationListener)
11566     delete DeserializationListener;
11567 }
11568 
11569 IdentifierResolver &ASTReader::getIdResolver() {
11570   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11571 }
11572 
11573 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11574                                                unsigned AbbrevID) {
11575   Idx = 0;
11576   Record.clear();
11577   return Cursor.readRecord(AbbrevID, Record);
11578 }
11579 //===----------------------------------------------------------------------===//
11580 //// OMPClauseReader implementation
11581 ////===----------------------------------------------------------------------===//
11582 
11583 // This has to be in namespace clang because it's friended by all
11584 // of the OMP clauses.
11585 namespace clang {
11586 
11587 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11588   ASTRecordReader &Record;
11589   ASTContext &Context;
11590 
11591 public:
11592   OMPClauseReader(ASTRecordReader &Record)
11593       : Record(Record), Context(Record.getContext()) {}
11594 
11595 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C);
11596 #include "clang/Basic/OpenMPKinds.def"
11597   OMPClause *readClause();
11598   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11599   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11600 };
11601 
11602 } // end namespace clang
11603 
11604 OMPClause *ASTRecordReader::readOMPClause() {
11605   return OMPClauseReader(*this).readClause();
11606 }
11607 
11608 OMPClause *OMPClauseReader::readClause() {
11609   OMPClause *C = nullptr;
11610   switch (Record.readInt()) {
11611   case OMPC_if:
11612     C = new (Context) OMPIfClause();
11613     break;
11614   case OMPC_final:
11615     C = new (Context) OMPFinalClause();
11616     break;
11617   case OMPC_num_threads:
11618     C = new (Context) OMPNumThreadsClause();
11619     break;
11620   case OMPC_safelen:
11621     C = new (Context) OMPSafelenClause();
11622     break;
11623   case OMPC_simdlen:
11624     C = new (Context) OMPSimdlenClause();
11625     break;
11626   case OMPC_allocator:
11627     C = new (Context) OMPAllocatorClause();
11628     break;
11629   case OMPC_collapse:
11630     C = new (Context) OMPCollapseClause();
11631     break;
11632   case OMPC_default:
11633     C = new (Context) OMPDefaultClause();
11634     break;
11635   case OMPC_proc_bind:
11636     C = new (Context) OMPProcBindClause();
11637     break;
11638   case OMPC_schedule:
11639     C = new (Context) OMPScheduleClause();
11640     break;
11641   case OMPC_ordered:
11642     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11643     break;
11644   case OMPC_nowait:
11645     C = new (Context) OMPNowaitClause();
11646     break;
11647   case OMPC_untied:
11648     C = new (Context) OMPUntiedClause();
11649     break;
11650   case OMPC_mergeable:
11651     C = new (Context) OMPMergeableClause();
11652     break;
11653   case OMPC_read:
11654     C = new (Context) OMPReadClause();
11655     break;
11656   case OMPC_write:
11657     C = new (Context) OMPWriteClause();
11658     break;
11659   case OMPC_update:
11660     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11661     break;
11662   case OMPC_capture:
11663     C = new (Context) OMPCaptureClause();
11664     break;
11665   case OMPC_seq_cst:
11666     C = new (Context) OMPSeqCstClause();
11667     break;
11668   case OMPC_acq_rel:
11669     C = new (Context) OMPAcqRelClause();
11670     break;
11671   case OMPC_acquire:
11672     C = new (Context) OMPAcquireClause();
11673     break;
11674   case OMPC_release:
11675     C = new (Context) OMPReleaseClause();
11676     break;
11677   case OMPC_relaxed:
11678     C = new (Context) OMPRelaxedClause();
11679     break;
11680   case OMPC_threads:
11681     C = new (Context) OMPThreadsClause();
11682     break;
11683   case OMPC_simd:
11684     C = new (Context) OMPSIMDClause();
11685     break;
11686   case OMPC_nogroup:
11687     C = new (Context) OMPNogroupClause();
11688     break;
11689   case OMPC_unified_address:
11690     C = new (Context) OMPUnifiedAddressClause();
11691     break;
11692   case OMPC_unified_shared_memory:
11693     C = new (Context) OMPUnifiedSharedMemoryClause();
11694     break;
11695   case OMPC_reverse_offload:
11696     C = new (Context) OMPReverseOffloadClause();
11697     break;
11698   case OMPC_dynamic_allocators:
11699     C = new (Context) OMPDynamicAllocatorsClause();
11700     break;
11701   case OMPC_atomic_default_mem_order:
11702     C = new (Context) OMPAtomicDefaultMemOrderClause();
11703     break;
11704  case OMPC_private:
11705     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11706     break;
11707   case OMPC_firstprivate:
11708     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11709     break;
11710   case OMPC_lastprivate:
11711     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11712     break;
11713   case OMPC_shared:
11714     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11715     break;
11716   case OMPC_reduction:
11717     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11718     break;
11719   case OMPC_task_reduction:
11720     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11721     break;
11722   case OMPC_in_reduction:
11723     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11724     break;
11725   case OMPC_linear:
11726     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11727     break;
11728   case OMPC_aligned:
11729     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11730     break;
11731   case OMPC_copyin:
11732     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11733     break;
11734   case OMPC_copyprivate:
11735     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11736     break;
11737   case OMPC_flush:
11738     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11739     break;
11740   case OMPC_depobj:
11741     C = OMPDepobjClause::CreateEmpty(Context);
11742     break;
11743   case OMPC_depend: {
11744     unsigned NumVars = Record.readInt();
11745     unsigned NumLoops = Record.readInt();
11746     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11747     break;
11748   }
11749   case OMPC_device:
11750     C = new (Context) OMPDeviceClause();
11751     break;
11752   case OMPC_map: {
11753     OMPMappableExprListSizeTy Sizes;
11754     Sizes.NumVars = Record.readInt();
11755     Sizes.NumUniqueDeclarations = Record.readInt();
11756     Sizes.NumComponentLists = Record.readInt();
11757     Sizes.NumComponents = Record.readInt();
11758     C = OMPMapClause::CreateEmpty(Context, Sizes);
11759     break;
11760   }
11761   case OMPC_num_teams:
11762     C = new (Context) OMPNumTeamsClause();
11763     break;
11764   case OMPC_thread_limit:
11765     C = new (Context) OMPThreadLimitClause();
11766     break;
11767   case OMPC_priority:
11768     C = new (Context) OMPPriorityClause();
11769     break;
11770   case OMPC_grainsize:
11771     C = new (Context) OMPGrainsizeClause();
11772     break;
11773   case OMPC_num_tasks:
11774     C = new (Context) OMPNumTasksClause();
11775     break;
11776   case OMPC_hint:
11777     C = new (Context) OMPHintClause();
11778     break;
11779   case OMPC_dist_schedule:
11780     C = new (Context) OMPDistScheduleClause();
11781     break;
11782   case OMPC_defaultmap:
11783     C = new (Context) OMPDefaultmapClause();
11784     break;
11785   case OMPC_to: {
11786     OMPMappableExprListSizeTy Sizes;
11787     Sizes.NumVars = Record.readInt();
11788     Sizes.NumUniqueDeclarations = Record.readInt();
11789     Sizes.NumComponentLists = Record.readInt();
11790     Sizes.NumComponents = Record.readInt();
11791     C = OMPToClause::CreateEmpty(Context, Sizes);
11792     break;
11793   }
11794   case OMPC_from: {
11795     OMPMappableExprListSizeTy Sizes;
11796     Sizes.NumVars = Record.readInt();
11797     Sizes.NumUniqueDeclarations = Record.readInt();
11798     Sizes.NumComponentLists = Record.readInt();
11799     Sizes.NumComponents = Record.readInt();
11800     C = OMPFromClause::CreateEmpty(Context, Sizes);
11801     break;
11802   }
11803   case OMPC_use_device_ptr: {
11804     OMPMappableExprListSizeTy Sizes;
11805     Sizes.NumVars = Record.readInt();
11806     Sizes.NumUniqueDeclarations = Record.readInt();
11807     Sizes.NumComponentLists = Record.readInt();
11808     Sizes.NumComponents = Record.readInt();
11809     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11810     break;
11811   }
11812   case OMPC_is_device_ptr: {
11813     OMPMappableExprListSizeTy Sizes;
11814     Sizes.NumVars = Record.readInt();
11815     Sizes.NumUniqueDeclarations = Record.readInt();
11816     Sizes.NumComponentLists = Record.readInt();
11817     Sizes.NumComponents = Record.readInt();
11818     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11819     break;
11820   }
11821   case OMPC_allocate:
11822     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11823     break;
11824   case OMPC_nontemporal:
11825     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11826     break;
11827   case OMPC_order:
11828     C = new (Context) OMPOrderClause();
11829     break;
11830   case OMPC_destroy:
11831     C = new (Context) OMPDestroyClause();
11832     break;
11833   }
11834   assert(C && "Unknown OMPClause type");
11835 
11836   Visit(C);
11837   C->setLocStart(Record.readSourceLocation());
11838   C->setLocEnd(Record.readSourceLocation());
11839 
11840   return C;
11841 }
11842 
11843 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11844   C->setPreInitStmt(Record.readSubStmt(),
11845                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11846 }
11847 
11848 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11849   VisitOMPClauseWithPreInit(C);
11850   C->setPostUpdateExpr(Record.readSubExpr());
11851 }
11852 
11853 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11854   VisitOMPClauseWithPreInit(C);
11855   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11856   C->setNameModifierLoc(Record.readSourceLocation());
11857   C->setColonLoc(Record.readSourceLocation());
11858   C->setCondition(Record.readSubExpr());
11859   C->setLParenLoc(Record.readSourceLocation());
11860 }
11861 
11862 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11863   VisitOMPClauseWithPreInit(C);
11864   C->setCondition(Record.readSubExpr());
11865   C->setLParenLoc(Record.readSourceLocation());
11866 }
11867 
11868 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11869   VisitOMPClauseWithPreInit(C);
11870   C->setNumThreads(Record.readSubExpr());
11871   C->setLParenLoc(Record.readSourceLocation());
11872 }
11873 
11874 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11875   C->setSafelen(Record.readSubExpr());
11876   C->setLParenLoc(Record.readSourceLocation());
11877 }
11878 
11879 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11880   C->setSimdlen(Record.readSubExpr());
11881   C->setLParenLoc(Record.readSourceLocation());
11882 }
11883 
11884 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11885   C->setAllocator(Record.readExpr());
11886   C->setLParenLoc(Record.readSourceLocation());
11887 }
11888 
11889 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11890   C->setNumForLoops(Record.readSubExpr());
11891   C->setLParenLoc(Record.readSourceLocation());
11892 }
11893 
11894 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11895   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11896   C->setLParenLoc(Record.readSourceLocation());
11897   C->setDefaultKindKwLoc(Record.readSourceLocation());
11898 }
11899 
11900 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11901   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11902   C->setLParenLoc(Record.readSourceLocation());
11903   C->setProcBindKindKwLoc(Record.readSourceLocation());
11904 }
11905 
11906 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11907   VisitOMPClauseWithPreInit(C);
11908   C->setScheduleKind(
11909        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11910   C->setFirstScheduleModifier(
11911       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11912   C->setSecondScheduleModifier(
11913       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11914   C->setChunkSize(Record.readSubExpr());
11915   C->setLParenLoc(Record.readSourceLocation());
11916   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11917   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11918   C->setScheduleKindLoc(Record.readSourceLocation());
11919   C->setCommaLoc(Record.readSourceLocation());
11920 }
11921 
11922 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11923   C->setNumForLoops(Record.readSubExpr());
11924   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11925     C->setLoopNumIterations(I, Record.readSubExpr());
11926   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11927     C->setLoopCounter(I, Record.readSubExpr());
11928   C->setLParenLoc(Record.readSourceLocation());
11929 }
11930 
11931 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11932 
11933 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11934 
11935 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11936 
11937 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11938 
11939 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11940 
11941 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11942   if (C->isExtended()) {
11943     C->setLParenLoc(Record.readSourceLocation());
11944     C->setArgumentLoc(Record.readSourceLocation());
11945     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11946   }
11947 }
11948 
11949 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11950 
11951 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11952 
11953 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11954 
11955 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11956 
11957 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11958 
11959 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11960 
11961 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11962 
11963 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11964 
11965 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11966 
11967 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
11968 
11969 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11970 
11971 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11972     OMPUnifiedSharedMemoryClause *) {}
11973 
11974 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11975 
11976 void
11977 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11978 }
11979 
11980 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11981     OMPAtomicDefaultMemOrderClause *C) {
11982   C->setAtomicDefaultMemOrderKind(
11983       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11984   C->setLParenLoc(Record.readSourceLocation());
11985   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11986 }
11987 
11988 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11989   C->setLParenLoc(Record.readSourceLocation());
11990   unsigned NumVars = C->varlist_size();
11991   SmallVector<Expr *, 16> Vars;
11992   Vars.reserve(NumVars);
11993   for (unsigned i = 0; i != NumVars; ++i)
11994     Vars.push_back(Record.readSubExpr());
11995   C->setVarRefs(Vars);
11996   Vars.clear();
11997   for (unsigned i = 0; i != NumVars; ++i)
11998     Vars.push_back(Record.readSubExpr());
11999   C->setPrivateCopies(Vars);
12000 }
12001 
12002 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12003   VisitOMPClauseWithPreInit(C);
12004   C->setLParenLoc(Record.readSourceLocation());
12005   unsigned NumVars = C->varlist_size();
12006   SmallVector<Expr *, 16> Vars;
12007   Vars.reserve(NumVars);
12008   for (unsigned i = 0; i != NumVars; ++i)
12009     Vars.push_back(Record.readSubExpr());
12010   C->setVarRefs(Vars);
12011   Vars.clear();
12012   for (unsigned i = 0; i != NumVars; ++i)
12013     Vars.push_back(Record.readSubExpr());
12014   C->setPrivateCopies(Vars);
12015   Vars.clear();
12016   for (unsigned i = 0; i != NumVars; ++i)
12017     Vars.push_back(Record.readSubExpr());
12018   C->setInits(Vars);
12019 }
12020 
12021 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12022   VisitOMPClauseWithPostUpdate(C);
12023   C->setLParenLoc(Record.readSourceLocation());
12024   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12025   C->setKindLoc(Record.readSourceLocation());
12026   C->setColonLoc(Record.readSourceLocation());
12027   unsigned NumVars = C->varlist_size();
12028   SmallVector<Expr *, 16> Vars;
12029   Vars.reserve(NumVars);
12030   for (unsigned i = 0; i != NumVars; ++i)
12031     Vars.push_back(Record.readSubExpr());
12032   C->setVarRefs(Vars);
12033   Vars.clear();
12034   for (unsigned i = 0; i != NumVars; ++i)
12035     Vars.push_back(Record.readSubExpr());
12036   C->setPrivateCopies(Vars);
12037   Vars.clear();
12038   for (unsigned i = 0; i != NumVars; ++i)
12039     Vars.push_back(Record.readSubExpr());
12040   C->setSourceExprs(Vars);
12041   Vars.clear();
12042   for (unsigned i = 0; i != NumVars; ++i)
12043     Vars.push_back(Record.readSubExpr());
12044   C->setDestinationExprs(Vars);
12045   Vars.clear();
12046   for (unsigned i = 0; i != NumVars; ++i)
12047     Vars.push_back(Record.readSubExpr());
12048   C->setAssignmentOps(Vars);
12049 }
12050 
12051 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12052   C->setLParenLoc(Record.readSourceLocation());
12053   unsigned NumVars = C->varlist_size();
12054   SmallVector<Expr *, 16> Vars;
12055   Vars.reserve(NumVars);
12056   for (unsigned i = 0; i != NumVars; ++i)
12057     Vars.push_back(Record.readSubExpr());
12058   C->setVarRefs(Vars);
12059 }
12060 
12061 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12062   VisitOMPClauseWithPostUpdate(C);
12063   C->setLParenLoc(Record.readSourceLocation());
12064   C->setColonLoc(Record.readSourceLocation());
12065   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12066   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12067   C->setQualifierLoc(NNSL);
12068   C->setNameInfo(DNI);
12069 
12070   unsigned NumVars = C->varlist_size();
12071   SmallVector<Expr *, 16> Vars;
12072   Vars.reserve(NumVars);
12073   for (unsigned i = 0; i != NumVars; ++i)
12074     Vars.push_back(Record.readSubExpr());
12075   C->setVarRefs(Vars);
12076   Vars.clear();
12077   for (unsigned i = 0; i != NumVars; ++i)
12078     Vars.push_back(Record.readSubExpr());
12079   C->setPrivates(Vars);
12080   Vars.clear();
12081   for (unsigned i = 0; i != NumVars; ++i)
12082     Vars.push_back(Record.readSubExpr());
12083   C->setLHSExprs(Vars);
12084   Vars.clear();
12085   for (unsigned i = 0; i != NumVars; ++i)
12086     Vars.push_back(Record.readSubExpr());
12087   C->setRHSExprs(Vars);
12088   Vars.clear();
12089   for (unsigned i = 0; i != NumVars; ++i)
12090     Vars.push_back(Record.readSubExpr());
12091   C->setReductionOps(Vars);
12092 }
12093 
12094 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12095   VisitOMPClauseWithPostUpdate(C);
12096   C->setLParenLoc(Record.readSourceLocation());
12097   C->setColonLoc(Record.readSourceLocation());
12098   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12099   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12100   C->setQualifierLoc(NNSL);
12101   C->setNameInfo(DNI);
12102 
12103   unsigned NumVars = C->varlist_size();
12104   SmallVector<Expr *, 16> Vars;
12105   Vars.reserve(NumVars);
12106   for (unsigned I = 0; I != NumVars; ++I)
12107     Vars.push_back(Record.readSubExpr());
12108   C->setVarRefs(Vars);
12109   Vars.clear();
12110   for (unsigned I = 0; I != NumVars; ++I)
12111     Vars.push_back(Record.readSubExpr());
12112   C->setPrivates(Vars);
12113   Vars.clear();
12114   for (unsigned I = 0; I != NumVars; ++I)
12115     Vars.push_back(Record.readSubExpr());
12116   C->setLHSExprs(Vars);
12117   Vars.clear();
12118   for (unsigned I = 0; I != NumVars; ++I)
12119     Vars.push_back(Record.readSubExpr());
12120   C->setRHSExprs(Vars);
12121   Vars.clear();
12122   for (unsigned I = 0; I != NumVars; ++I)
12123     Vars.push_back(Record.readSubExpr());
12124   C->setReductionOps(Vars);
12125 }
12126 
12127 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12128   VisitOMPClauseWithPostUpdate(C);
12129   C->setLParenLoc(Record.readSourceLocation());
12130   C->setColonLoc(Record.readSourceLocation());
12131   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12132   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12133   C->setQualifierLoc(NNSL);
12134   C->setNameInfo(DNI);
12135 
12136   unsigned NumVars = C->varlist_size();
12137   SmallVector<Expr *, 16> Vars;
12138   Vars.reserve(NumVars);
12139   for (unsigned I = 0; I != NumVars; ++I)
12140     Vars.push_back(Record.readSubExpr());
12141   C->setVarRefs(Vars);
12142   Vars.clear();
12143   for (unsigned I = 0; I != NumVars; ++I)
12144     Vars.push_back(Record.readSubExpr());
12145   C->setPrivates(Vars);
12146   Vars.clear();
12147   for (unsigned I = 0; I != NumVars; ++I)
12148     Vars.push_back(Record.readSubExpr());
12149   C->setLHSExprs(Vars);
12150   Vars.clear();
12151   for (unsigned I = 0; I != NumVars; ++I)
12152     Vars.push_back(Record.readSubExpr());
12153   C->setRHSExprs(Vars);
12154   Vars.clear();
12155   for (unsigned I = 0; I != NumVars; ++I)
12156     Vars.push_back(Record.readSubExpr());
12157   C->setReductionOps(Vars);
12158   Vars.clear();
12159   for (unsigned I = 0; I != NumVars; ++I)
12160     Vars.push_back(Record.readSubExpr());
12161   C->setTaskgroupDescriptors(Vars);
12162 }
12163 
12164 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12165   VisitOMPClauseWithPostUpdate(C);
12166   C->setLParenLoc(Record.readSourceLocation());
12167   C->setColonLoc(Record.readSourceLocation());
12168   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12169   C->setModifierLoc(Record.readSourceLocation());
12170   unsigned NumVars = C->varlist_size();
12171   SmallVector<Expr *, 16> Vars;
12172   Vars.reserve(NumVars);
12173   for (unsigned i = 0; i != NumVars; ++i)
12174     Vars.push_back(Record.readSubExpr());
12175   C->setVarRefs(Vars);
12176   Vars.clear();
12177   for (unsigned i = 0; i != NumVars; ++i)
12178     Vars.push_back(Record.readSubExpr());
12179   C->setPrivates(Vars);
12180   Vars.clear();
12181   for (unsigned i = 0; i != NumVars; ++i)
12182     Vars.push_back(Record.readSubExpr());
12183   C->setInits(Vars);
12184   Vars.clear();
12185   for (unsigned i = 0; i != NumVars; ++i)
12186     Vars.push_back(Record.readSubExpr());
12187   C->setUpdates(Vars);
12188   Vars.clear();
12189   for (unsigned i = 0; i != NumVars; ++i)
12190     Vars.push_back(Record.readSubExpr());
12191   C->setFinals(Vars);
12192   C->setStep(Record.readSubExpr());
12193   C->setCalcStep(Record.readSubExpr());
12194   Vars.clear();
12195   for (unsigned I = 0; I != NumVars + 1; ++I)
12196     Vars.push_back(Record.readSubExpr());
12197   C->setUsedExprs(Vars);
12198 }
12199 
12200 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12201   C->setLParenLoc(Record.readSourceLocation());
12202   C->setColonLoc(Record.readSourceLocation());
12203   unsigned NumVars = C->varlist_size();
12204   SmallVector<Expr *, 16> Vars;
12205   Vars.reserve(NumVars);
12206   for (unsigned i = 0; i != NumVars; ++i)
12207     Vars.push_back(Record.readSubExpr());
12208   C->setVarRefs(Vars);
12209   C->setAlignment(Record.readSubExpr());
12210 }
12211 
12212 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12213   C->setLParenLoc(Record.readSourceLocation());
12214   unsigned NumVars = C->varlist_size();
12215   SmallVector<Expr *, 16> Exprs;
12216   Exprs.reserve(NumVars);
12217   for (unsigned i = 0; i != NumVars; ++i)
12218     Exprs.push_back(Record.readSubExpr());
12219   C->setVarRefs(Exprs);
12220   Exprs.clear();
12221   for (unsigned i = 0; i != NumVars; ++i)
12222     Exprs.push_back(Record.readSubExpr());
12223   C->setSourceExprs(Exprs);
12224   Exprs.clear();
12225   for (unsigned i = 0; i != NumVars; ++i)
12226     Exprs.push_back(Record.readSubExpr());
12227   C->setDestinationExprs(Exprs);
12228   Exprs.clear();
12229   for (unsigned i = 0; i != NumVars; ++i)
12230     Exprs.push_back(Record.readSubExpr());
12231   C->setAssignmentOps(Exprs);
12232 }
12233 
12234 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12235   C->setLParenLoc(Record.readSourceLocation());
12236   unsigned NumVars = C->varlist_size();
12237   SmallVector<Expr *, 16> Exprs;
12238   Exprs.reserve(NumVars);
12239   for (unsigned i = 0; i != NumVars; ++i)
12240     Exprs.push_back(Record.readSubExpr());
12241   C->setVarRefs(Exprs);
12242   Exprs.clear();
12243   for (unsigned i = 0; i != NumVars; ++i)
12244     Exprs.push_back(Record.readSubExpr());
12245   C->setSourceExprs(Exprs);
12246   Exprs.clear();
12247   for (unsigned i = 0; i != NumVars; ++i)
12248     Exprs.push_back(Record.readSubExpr());
12249   C->setDestinationExprs(Exprs);
12250   Exprs.clear();
12251   for (unsigned i = 0; i != NumVars; ++i)
12252     Exprs.push_back(Record.readSubExpr());
12253   C->setAssignmentOps(Exprs);
12254 }
12255 
12256 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12257   C->setLParenLoc(Record.readSourceLocation());
12258   unsigned NumVars = C->varlist_size();
12259   SmallVector<Expr *, 16> Vars;
12260   Vars.reserve(NumVars);
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setVarRefs(Vars);
12264 }
12265 
12266 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12267   C->setDepobj(Record.readSubExpr());
12268   C->setLParenLoc(Record.readSourceLocation());
12269 }
12270 
12271 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12272   C->setLParenLoc(Record.readSourceLocation());
12273   C->setDependencyKind(
12274       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12275   C->setDependencyLoc(Record.readSourceLocation());
12276   C->setColonLoc(Record.readSourceLocation());
12277   unsigned NumVars = C->varlist_size();
12278   SmallVector<Expr *, 16> Vars;
12279   Vars.reserve(NumVars);
12280   for (unsigned I = 0; I != NumVars; ++I)
12281     Vars.push_back(Record.readSubExpr());
12282   C->setVarRefs(Vars);
12283   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12284     C->setLoopData(I, Record.readSubExpr());
12285 }
12286 
12287 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12288   VisitOMPClauseWithPreInit(C);
12289   C->setDevice(Record.readSubExpr());
12290   C->setLParenLoc(Record.readSourceLocation());
12291 }
12292 
12293 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12294   C->setLParenLoc(Record.readSourceLocation());
12295   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12296     C->setMapTypeModifier(
12297         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12298     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12299   }
12300   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12301   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12302   C->setMapType(
12303      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12304   C->setMapLoc(Record.readSourceLocation());
12305   C->setColonLoc(Record.readSourceLocation());
12306   auto NumVars = C->varlist_size();
12307   auto UniqueDecls = C->getUniqueDeclarationsNum();
12308   auto TotalLists = C->getTotalComponentListNum();
12309   auto TotalComponents = C->getTotalComponentsNum();
12310 
12311   SmallVector<Expr *, 16> Vars;
12312   Vars.reserve(NumVars);
12313   for (unsigned i = 0; i != NumVars; ++i)
12314     Vars.push_back(Record.readExpr());
12315   C->setVarRefs(Vars);
12316 
12317   SmallVector<Expr *, 16> UDMappers;
12318   UDMappers.reserve(NumVars);
12319   for (unsigned I = 0; I < NumVars; ++I)
12320     UDMappers.push_back(Record.readExpr());
12321   C->setUDMapperRefs(UDMappers);
12322 
12323   SmallVector<ValueDecl *, 16> Decls;
12324   Decls.reserve(UniqueDecls);
12325   for (unsigned i = 0; i < UniqueDecls; ++i)
12326     Decls.push_back(Record.readDeclAs<ValueDecl>());
12327   C->setUniqueDecls(Decls);
12328 
12329   SmallVector<unsigned, 16> ListsPerDecl;
12330   ListsPerDecl.reserve(UniqueDecls);
12331   for (unsigned i = 0; i < UniqueDecls; ++i)
12332     ListsPerDecl.push_back(Record.readInt());
12333   C->setDeclNumLists(ListsPerDecl);
12334 
12335   SmallVector<unsigned, 32> ListSizes;
12336   ListSizes.reserve(TotalLists);
12337   for (unsigned i = 0; i < TotalLists; ++i)
12338     ListSizes.push_back(Record.readInt());
12339   C->setComponentListSizes(ListSizes);
12340 
12341   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12342   Components.reserve(TotalComponents);
12343   for (unsigned i = 0; i < TotalComponents; ++i) {
12344     Expr *AssociatedExpr = Record.readExpr();
12345     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12346     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12347         AssociatedExpr, AssociatedDecl));
12348   }
12349   C->setComponents(Components, ListSizes);
12350 }
12351 
12352 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12353   C->setLParenLoc(Record.readSourceLocation());
12354   C->setColonLoc(Record.readSourceLocation());
12355   C->setAllocator(Record.readSubExpr());
12356   unsigned NumVars = C->varlist_size();
12357   SmallVector<Expr *, 16> Vars;
12358   Vars.reserve(NumVars);
12359   for (unsigned i = 0; i != NumVars; ++i)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setVarRefs(Vars);
12362 }
12363 
12364 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12365   VisitOMPClauseWithPreInit(C);
12366   C->setNumTeams(Record.readSubExpr());
12367   C->setLParenLoc(Record.readSourceLocation());
12368 }
12369 
12370 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12371   VisitOMPClauseWithPreInit(C);
12372   C->setThreadLimit(Record.readSubExpr());
12373   C->setLParenLoc(Record.readSourceLocation());
12374 }
12375 
12376 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12377   VisitOMPClauseWithPreInit(C);
12378   C->setPriority(Record.readSubExpr());
12379   C->setLParenLoc(Record.readSourceLocation());
12380 }
12381 
12382 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12383   VisitOMPClauseWithPreInit(C);
12384   C->setGrainsize(Record.readSubExpr());
12385   C->setLParenLoc(Record.readSourceLocation());
12386 }
12387 
12388 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12389   VisitOMPClauseWithPreInit(C);
12390   C->setNumTasks(Record.readSubExpr());
12391   C->setLParenLoc(Record.readSourceLocation());
12392 }
12393 
12394 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12395   C->setHint(Record.readSubExpr());
12396   C->setLParenLoc(Record.readSourceLocation());
12397 }
12398 
12399 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12400   VisitOMPClauseWithPreInit(C);
12401   C->setDistScheduleKind(
12402       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12403   C->setChunkSize(Record.readSubExpr());
12404   C->setLParenLoc(Record.readSourceLocation());
12405   C->setDistScheduleKindLoc(Record.readSourceLocation());
12406   C->setCommaLoc(Record.readSourceLocation());
12407 }
12408 
12409 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12410   C->setDefaultmapKind(
12411        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12412   C->setDefaultmapModifier(
12413       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12414   C->setLParenLoc(Record.readSourceLocation());
12415   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12416   C->setDefaultmapKindLoc(Record.readSourceLocation());
12417 }
12418 
12419 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12420   C->setLParenLoc(Record.readSourceLocation());
12421   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12422   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12423   auto NumVars = C->varlist_size();
12424   auto UniqueDecls = C->getUniqueDeclarationsNum();
12425   auto TotalLists = C->getTotalComponentListNum();
12426   auto TotalComponents = C->getTotalComponentsNum();
12427 
12428   SmallVector<Expr *, 16> Vars;
12429   Vars.reserve(NumVars);
12430   for (unsigned i = 0; i != NumVars; ++i)
12431     Vars.push_back(Record.readSubExpr());
12432   C->setVarRefs(Vars);
12433 
12434   SmallVector<Expr *, 16> UDMappers;
12435   UDMappers.reserve(NumVars);
12436   for (unsigned I = 0; I < NumVars; ++I)
12437     UDMappers.push_back(Record.readSubExpr());
12438   C->setUDMapperRefs(UDMappers);
12439 
12440   SmallVector<ValueDecl *, 16> Decls;
12441   Decls.reserve(UniqueDecls);
12442   for (unsigned i = 0; i < UniqueDecls; ++i)
12443     Decls.push_back(Record.readDeclAs<ValueDecl>());
12444   C->setUniqueDecls(Decls);
12445 
12446   SmallVector<unsigned, 16> ListsPerDecl;
12447   ListsPerDecl.reserve(UniqueDecls);
12448   for (unsigned i = 0; i < UniqueDecls; ++i)
12449     ListsPerDecl.push_back(Record.readInt());
12450   C->setDeclNumLists(ListsPerDecl);
12451 
12452   SmallVector<unsigned, 32> ListSizes;
12453   ListSizes.reserve(TotalLists);
12454   for (unsigned i = 0; i < TotalLists; ++i)
12455     ListSizes.push_back(Record.readInt());
12456   C->setComponentListSizes(ListSizes);
12457 
12458   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12459   Components.reserve(TotalComponents);
12460   for (unsigned i = 0; i < TotalComponents; ++i) {
12461     Expr *AssociatedExpr = Record.readSubExpr();
12462     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12463     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12464         AssociatedExpr, AssociatedDecl));
12465   }
12466   C->setComponents(Components, ListSizes);
12467 }
12468 
12469 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12470   C->setLParenLoc(Record.readSourceLocation());
12471   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12472   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12473   auto NumVars = C->varlist_size();
12474   auto UniqueDecls = C->getUniqueDeclarationsNum();
12475   auto TotalLists = C->getTotalComponentListNum();
12476   auto TotalComponents = C->getTotalComponentsNum();
12477 
12478   SmallVector<Expr *, 16> Vars;
12479   Vars.reserve(NumVars);
12480   for (unsigned i = 0; i != NumVars; ++i)
12481     Vars.push_back(Record.readSubExpr());
12482   C->setVarRefs(Vars);
12483 
12484   SmallVector<Expr *, 16> UDMappers;
12485   UDMappers.reserve(NumVars);
12486   for (unsigned I = 0; I < NumVars; ++I)
12487     UDMappers.push_back(Record.readSubExpr());
12488   C->setUDMapperRefs(UDMappers);
12489 
12490   SmallVector<ValueDecl *, 16> Decls;
12491   Decls.reserve(UniqueDecls);
12492   for (unsigned i = 0; i < UniqueDecls; ++i)
12493     Decls.push_back(Record.readDeclAs<ValueDecl>());
12494   C->setUniqueDecls(Decls);
12495 
12496   SmallVector<unsigned, 16> ListsPerDecl;
12497   ListsPerDecl.reserve(UniqueDecls);
12498   for (unsigned i = 0; i < UniqueDecls; ++i)
12499     ListsPerDecl.push_back(Record.readInt());
12500   C->setDeclNumLists(ListsPerDecl);
12501 
12502   SmallVector<unsigned, 32> ListSizes;
12503   ListSizes.reserve(TotalLists);
12504   for (unsigned i = 0; i < TotalLists; ++i)
12505     ListSizes.push_back(Record.readInt());
12506   C->setComponentListSizes(ListSizes);
12507 
12508   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12509   Components.reserve(TotalComponents);
12510   for (unsigned i = 0; i < TotalComponents; ++i) {
12511     Expr *AssociatedExpr = Record.readSubExpr();
12512     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12513     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12514         AssociatedExpr, AssociatedDecl));
12515   }
12516   C->setComponents(Components, ListSizes);
12517 }
12518 
12519 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12520   C->setLParenLoc(Record.readSourceLocation());
12521   auto NumVars = C->varlist_size();
12522   auto UniqueDecls = C->getUniqueDeclarationsNum();
12523   auto TotalLists = C->getTotalComponentListNum();
12524   auto TotalComponents = C->getTotalComponentsNum();
12525 
12526   SmallVector<Expr *, 16> Vars;
12527   Vars.reserve(NumVars);
12528   for (unsigned i = 0; i != NumVars; ++i)
12529     Vars.push_back(Record.readSubExpr());
12530   C->setVarRefs(Vars);
12531   Vars.clear();
12532   for (unsigned i = 0; i != NumVars; ++i)
12533     Vars.push_back(Record.readSubExpr());
12534   C->setPrivateCopies(Vars);
12535   Vars.clear();
12536   for (unsigned i = 0; i != NumVars; ++i)
12537     Vars.push_back(Record.readSubExpr());
12538   C->setInits(Vars);
12539 
12540   SmallVector<ValueDecl *, 16> Decls;
12541   Decls.reserve(UniqueDecls);
12542   for (unsigned i = 0; i < UniqueDecls; ++i)
12543     Decls.push_back(Record.readDeclAs<ValueDecl>());
12544   C->setUniqueDecls(Decls);
12545 
12546   SmallVector<unsigned, 16> ListsPerDecl;
12547   ListsPerDecl.reserve(UniqueDecls);
12548   for (unsigned i = 0; i < UniqueDecls; ++i)
12549     ListsPerDecl.push_back(Record.readInt());
12550   C->setDeclNumLists(ListsPerDecl);
12551 
12552   SmallVector<unsigned, 32> ListSizes;
12553   ListSizes.reserve(TotalLists);
12554   for (unsigned i = 0; i < TotalLists; ++i)
12555     ListSizes.push_back(Record.readInt());
12556   C->setComponentListSizes(ListSizes);
12557 
12558   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12559   Components.reserve(TotalComponents);
12560   for (unsigned i = 0; i < TotalComponents; ++i) {
12561     Expr *AssociatedExpr = Record.readSubExpr();
12562     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12563     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12564         AssociatedExpr, AssociatedDecl));
12565   }
12566   C->setComponents(Components, ListSizes);
12567 }
12568 
12569 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12570   C->setLParenLoc(Record.readSourceLocation());
12571   auto NumVars = C->varlist_size();
12572   auto UniqueDecls = C->getUniqueDeclarationsNum();
12573   auto TotalLists = C->getTotalComponentListNum();
12574   auto TotalComponents = C->getTotalComponentsNum();
12575 
12576   SmallVector<Expr *, 16> Vars;
12577   Vars.reserve(NumVars);
12578   for (unsigned i = 0; i != NumVars; ++i)
12579     Vars.push_back(Record.readSubExpr());
12580   C->setVarRefs(Vars);
12581   Vars.clear();
12582 
12583   SmallVector<ValueDecl *, 16> Decls;
12584   Decls.reserve(UniqueDecls);
12585   for (unsigned i = 0; i < UniqueDecls; ++i)
12586     Decls.push_back(Record.readDeclAs<ValueDecl>());
12587   C->setUniqueDecls(Decls);
12588 
12589   SmallVector<unsigned, 16> ListsPerDecl;
12590   ListsPerDecl.reserve(UniqueDecls);
12591   for (unsigned i = 0; i < UniqueDecls; ++i)
12592     ListsPerDecl.push_back(Record.readInt());
12593   C->setDeclNumLists(ListsPerDecl);
12594 
12595   SmallVector<unsigned, 32> ListSizes;
12596   ListSizes.reserve(TotalLists);
12597   for (unsigned i = 0; i < TotalLists; ++i)
12598     ListSizes.push_back(Record.readInt());
12599   C->setComponentListSizes(ListSizes);
12600 
12601   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12602   Components.reserve(TotalComponents);
12603   for (unsigned i = 0; i < TotalComponents; ++i) {
12604     Expr *AssociatedExpr = Record.readSubExpr();
12605     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12606     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12607         AssociatedExpr, AssociatedDecl));
12608   }
12609   C->setComponents(Components, ListSizes);
12610 }
12611 
12612 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12613   C->setLParenLoc(Record.readSourceLocation());
12614   unsigned NumVars = C->varlist_size();
12615   SmallVector<Expr *, 16> Vars;
12616   Vars.reserve(NumVars);
12617   for (unsigned i = 0; i != NumVars; ++i)
12618     Vars.push_back(Record.readSubExpr());
12619   C->setVarRefs(Vars);
12620   Vars.clear();
12621   Vars.reserve(NumVars);
12622   for (unsigned i = 0; i != NumVars; ++i)
12623     Vars.push_back(Record.readSubExpr());
12624   C->setPrivateRefs(Vars);
12625 }
12626 
12627 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12628   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12629   C->setLParenLoc(Record.readSourceLocation());
12630   C->setKindKwLoc(Record.readSourceLocation());
12631 }
12632 
12633 OMPTraitInfo ASTRecordReader::readOMPTraitInfo() {
12634   OMPTraitInfo TI;
12635   TI.Sets.resize(readUInt32());
12636   for (auto &Set : TI.Sets) {
12637     Set.Kind = readEnum<llvm::omp::TraitSet>();
12638     Set.Selectors.resize(readUInt32());
12639     for (auto &Selector : Set.Selectors) {
12640       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12641       Selector.ScoreOrCondition = nullptr;
12642       if (readBool())
12643         Selector.ScoreOrCondition = readExprRef();
12644       Selector.Properties.resize(readUInt32());
12645       for (auto &Property : Selector.Properties)
12646         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12647     }
12648   }
12649   return TI;
12650 }
12651