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           getContext().getLangOpts().BuildingPCHWithObjectFile)
3229         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3230           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3231       break;
3232 
3233     case SPECIAL_TYPES:
3234       if (SpecialTypes.empty()) {
3235         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3236           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3237         break;
3238       }
3239 
3240       if (SpecialTypes.size() != Record.size()) {
3241         Error("invalid special-types record");
3242         return Failure;
3243       }
3244 
3245       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3246         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3247         if (!SpecialTypes[I])
3248           SpecialTypes[I] = ID;
3249         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3250         // merge step?
3251       }
3252       break;
3253 
3254     case STATISTICS:
3255       TotalNumStatements += Record[0];
3256       TotalNumMacros += Record[1];
3257       TotalLexicalDeclContexts += Record[2];
3258       TotalVisibleDeclContexts += Record[3];
3259       break;
3260 
3261     case UNUSED_FILESCOPED_DECLS:
3262       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3263         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3264       break;
3265 
3266     case DELEGATING_CTORS:
3267       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3268         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3269       break;
3270 
3271     case WEAK_UNDECLARED_IDENTIFIERS:
3272       if (Record.size() % 4 != 0) {
3273         Error("invalid weak identifiers record");
3274         return Failure;
3275       }
3276 
3277       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3278       // files. This isn't the way to do it :)
3279       WeakUndeclaredIdentifiers.clear();
3280 
3281       // Translate the weak, undeclared identifiers into global IDs.
3282       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3283         WeakUndeclaredIdentifiers.push_back(
3284           getGlobalIdentifierID(F, Record[I++]));
3285         WeakUndeclaredIdentifiers.push_back(
3286           getGlobalIdentifierID(F, Record[I++]));
3287         WeakUndeclaredIdentifiers.push_back(
3288           ReadSourceLocation(F, Record, I).getRawEncoding());
3289         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3290       }
3291       break;
3292 
3293     case SELECTOR_OFFSETS: {
3294       F.SelectorOffsets = (const uint32_t *)Blob.data();
3295       F.LocalNumSelectors = Record[0];
3296       unsigned LocalBaseSelectorID = Record[1];
3297       F.BaseSelectorID = getTotalNumSelectors();
3298 
3299       if (F.LocalNumSelectors > 0) {
3300         // Introduce the global -> local mapping for selectors within this
3301         // module.
3302         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3303 
3304         // Introduce the local -> global mapping for selectors within this
3305         // module.
3306         F.SelectorRemap.insertOrReplace(
3307           std::make_pair(LocalBaseSelectorID,
3308                          F.BaseSelectorID - LocalBaseSelectorID));
3309 
3310         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3311       }
3312       break;
3313     }
3314 
3315     case METHOD_POOL:
3316       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3317       if (Record[0])
3318         F.SelectorLookupTable
3319           = ASTSelectorLookupTable::Create(
3320                         F.SelectorLookupTableData + Record[0],
3321                         F.SelectorLookupTableData,
3322                         ASTSelectorLookupTrait(*this, F));
3323       TotalNumMethodPoolEntries += Record[1];
3324       break;
3325 
3326     case REFERENCED_SELECTOR_POOL:
3327       if (!Record.empty()) {
3328         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3329           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3330                                                                 Record[Idx++]));
3331           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3332                                               getRawEncoding());
3333         }
3334       }
3335       break;
3336 
3337     case PP_CONDITIONAL_STACK:
3338       if (!Record.empty()) {
3339         unsigned Idx = 0, End = Record.size() - 1;
3340         bool ReachedEOFWhileSkipping = Record[Idx++];
3341         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3342         if (ReachedEOFWhileSkipping) {
3343           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3344           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3345           bool FoundNonSkipPortion = Record[Idx++];
3346           bool FoundElse = Record[Idx++];
3347           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3348           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3349                            FoundElse, ElseLoc);
3350         }
3351         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3352         while (Idx < End) {
3353           auto Loc = ReadSourceLocation(F, Record, Idx);
3354           bool WasSkipping = Record[Idx++];
3355           bool FoundNonSkip = Record[Idx++];
3356           bool FoundElse = Record[Idx++];
3357           ConditionalStack.push_back(
3358               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3359         }
3360         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3361       }
3362       break;
3363 
3364     case PP_COUNTER_VALUE:
3365       if (!Record.empty() && Listener)
3366         Listener->ReadCounter(F, Record[0]);
3367       break;
3368 
3369     case FILE_SORTED_DECLS:
3370       F.FileSortedDecls = (const DeclID *)Blob.data();
3371       F.NumFileSortedDecls = Record[0];
3372       break;
3373 
3374     case SOURCE_LOCATION_OFFSETS: {
3375       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3376       F.LocalNumSLocEntries = Record[0];
3377       unsigned SLocSpaceSize = Record[1];
3378       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3379           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3380                                               SLocSpaceSize);
3381       if (!F.SLocEntryBaseID) {
3382         Error("ran out of source locations");
3383         break;
3384       }
3385       // Make our entry in the range map. BaseID is negative and growing, so
3386       // we invert it. Because we invert it, though, we need the other end of
3387       // the range.
3388       unsigned RangeStart =
3389           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3390       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3391       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3392 
3393       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3394       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3395       GlobalSLocOffsetMap.insert(
3396           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3397                            - SLocSpaceSize,&F));
3398 
3399       // Initialize the remapping table.
3400       // Invalid stays invalid.
3401       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3402       // This module. Base was 2 when being compiled.
3403       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3404                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3405 
3406       TotalNumSLocEntries += F.LocalNumSLocEntries;
3407       break;
3408     }
3409 
3410     case MODULE_OFFSET_MAP:
3411       F.ModuleOffsetMap = Blob;
3412       break;
3413 
3414     case SOURCE_MANAGER_LINE_TABLE:
3415       if (ParseLineTable(F, Record)) {
3416         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3417         return Failure;
3418       }
3419       break;
3420 
3421     case SOURCE_LOCATION_PRELOADS: {
3422       // Need to transform from the local view (1-based IDs) to the global view,
3423       // which is based off F.SLocEntryBaseID.
3424       if (!F.PreloadSLocEntries.empty()) {
3425         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3426         return Failure;
3427       }
3428 
3429       F.PreloadSLocEntries.swap(Record);
3430       break;
3431     }
3432 
3433     case EXT_VECTOR_DECLS:
3434       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3435         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3436       break;
3437 
3438     case VTABLE_USES:
3439       if (Record.size() % 3 != 0) {
3440         Error("Invalid VTABLE_USES record");
3441         return Failure;
3442       }
3443 
3444       // Later tables overwrite earlier ones.
3445       // FIXME: Modules will have some trouble with this. This is clearly not
3446       // the right way to do this.
3447       VTableUses.clear();
3448 
3449       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3450         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3451         VTableUses.push_back(
3452           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3453         VTableUses.push_back(Record[Idx++]);
3454       }
3455       break;
3456 
3457     case PENDING_IMPLICIT_INSTANTIATIONS:
3458       if (PendingInstantiations.size() % 2 != 0) {
3459         Error("Invalid existing PendingInstantiations");
3460         return Failure;
3461       }
3462 
3463       if (Record.size() % 2 != 0) {
3464         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3465         return Failure;
3466       }
3467 
3468       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3469         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3470         PendingInstantiations.push_back(
3471           ReadSourceLocation(F, Record, I).getRawEncoding());
3472       }
3473       break;
3474 
3475     case SEMA_DECL_REFS:
3476       if (Record.size() != 3) {
3477         Error("Invalid SEMA_DECL_REFS block");
3478         return Failure;
3479       }
3480       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3481         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3482       break;
3483 
3484     case PPD_ENTITIES_OFFSETS: {
3485       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3486       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3487       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3488 
3489       unsigned LocalBasePreprocessedEntityID = Record[0];
3490 
3491       unsigned StartingID;
3492       if (!PP.getPreprocessingRecord())
3493         PP.createPreprocessingRecord();
3494       if (!PP.getPreprocessingRecord()->getExternalSource())
3495         PP.getPreprocessingRecord()->SetExternalSource(*this);
3496       StartingID
3497         = PP.getPreprocessingRecord()
3498             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3499       F.BasePreprocessedEntityID = StartingID;
3500 
3501       if (F.NumPreprocessedEntities > 0) {
3502         // Introduce the global -> local mapping for preprocessed entities in
3503         // this module.
3504         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3505 
3506         // Introduce the local -> global mapping for preprocessed entities in
3507         // this module.
3508         F.PreprocessedEntityRemap.insertOrReplace(
3509           std::make_pair(LocalBasePreprocessedEntityID,
3510             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3511       }
3512 
3513       break;
3514     }
3515 
3516     case PPD_SKIPPED_RANGES: {
3517       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3518       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3519       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3520 
3521       if (!PP.getPreprocessingRecord())
3522         PP.createPreprocessingRecord();
3523       if (!PP.getPreprocessingRecord()->getExternalSource())
3524         PP.getPreprocessingRecord()->SetExternalSource(*this);
3525       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3526           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3527 
3528       if (F.NumPreprocessedSkippedRanges > 0)
3529         GlobalSkippedRangeMap.insert(
3530             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3531       break;
3532     }
3533 
3534     case DECL_UPDATE_OFFSETS:
3535       if (Record.size() % 2 != 0) {
3536         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3537         return Failure;
3538       }
3539       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3540         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3541         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3542 
3543         // If we've already loaded the decl, perform the updates when we finish
3544         // loading this block.
3545         if (Decl *D = GetExistingDecl(ID))
3546           PendingUpdateRecords.push_back(
3547               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3548       }
3549       break;
3550 
3551     case OBJC_CATEGORIES_MAP:
3552       if (F.LocalNumObjCCategoriesInMap != 0) {
3553         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3554         return Failure;
3555       }
3556 
3557       F.LocalNumObjCCategoriesInMap = Record[0];
3558       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3559       break;
3560 
3561     case OBJC_CATEGORIES:
3562       F.ObjCCategories.swap(Record);
3563       break;
3564 
3565     case CUDA_SPECIAL_DECL_REFS:
3566       // Later tables overwrite earlier ones.
3567       // FIXME: Modules will have trouble with this.
3568       CUDASpecialDeclRefs.clear();
3569       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3570         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3571       break;
3572 
3573     case HEADER_SEARCH_TABLE:
3574       F.HeaderFileInfoTableData = Blob.data();
3575       F.LocalNumHeaderFileInfos = Record[1];
3576       if (Record[0]) {
3577         F.HeaderFileInfoTable
3578           = HeaderFileInfoLookupTable::Create(
3579                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3580                    (const unsigned char *)F.HeaderFileInfoTableData,
3581                    HeaderFileInfoTrait(*this, F,
3582                                        &PP.getHeaderSearchInfo(),
3583                                        Blob.data() + Record[2]));
3584 
3585         PP.getHeaderSearchInfo().SetExternalSource(this);
3586         if (!PP.getHeaderSearchInfo().getExternalLookup())
3587           PP.getHeaderSearchInfo().SetExternalLookup(this);
3588       }
3589       break;
3590 
3591     case FP_PRAGMA_OPTIONS:
3592       // Later tables overwrite earlier ones.
3593       FPPragmaOptions.swap(Record);
3594       break;
3595 
3596     case OPENCL_EXTENSIONS:
3597       for (unsigned I = 0, E = Record.size(); I != E; ) {
3598         auto Name = ReadString(Record, I);
3599         auto &Opt = OpenCLExtensions.OptMap[Name];
3600         Opt.Supported = Record[I++] != 0;
3601         Opt.Enabled = Record[I++] != 0;
3602         Opt.Avail = Record[I++];
3603         Opt.Core = Record[I++];
3604       }
3605       break;
3606 
3607     case OPENCL_EXTENSION_TYPES:
3608       for (unsigned I = 0, E = Record.size(); I != E;) {
3609         auto TypeID = static_cast<::TypeID>(Record[I++]);
3610         auto *Type = GetType(TypeID).getTypePtr();
3611         auto NumExt = static_cast<unsigned>(Record[I++]);
3612         for (unsigned II = 0; II != NumExt; ++II) {
3613           auto Ext = ReadString(Record, I);
3614           OpenCLTypeExtMap[Type].insert(Ext);
3615         }
3616       }
3617       break;
3618 
3619     case OPENCL_EXTENSION_DECLS:
3620       for (unsigned I = 0, E = Record.size(); I != E;) {
3621         auto DeclID = static_cast<::DeclID>(Record[I++]);
3622         auto *Decl = GetDecl(DeclID);
3623         auto NumExt = static_cast<unsigned>(Record[I++]);
3624         for (unsigned II = 0; II != NumExt; ++II) {
3625           auto Ext = ReadString(Record, I);
3626           OpenCLDeclExtMap[Decl].insert(Ext);
3627         }
3628       }
3629       break;
3630 
3631     case TENTATIVE_DEFINITIONS:
3632       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3633         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3634       break;
3635 
3636     case KNOWN_NAMESPACES:
3637       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3638         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3639       break;
3640 
3641     case UNDEFINED_BUT_USED:
3642       if (UndefinedButUsed.size() % 2 != 0) {
3643         Error("Invalid existing UndefinedButUsed");
3644         return Failure;
3645       }
3646 
3647       if (Record.size() % 2 != 0) {
3648         Error("invalid undefined-but-used record");
3649         return Failure;
3650       }
3651       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3652         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3653         UndefinedButUsed.push_back(
3654             ReadSourceLocation(F, Record, I).getRawEncoding());
3655       }
3656       break;
3657 
3658     case DELETE_EXPRS_TO_ANALYZE:
3659       for (unsigned I = 0, N = Record.size(); I != N;) {
3660         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3661         const uint64_t Count = Record[I++];
3662         DelayedDeleteExprs.push_back(Count);
3663         for (uint64_t C = 0; C < Count; ++C) {
3664           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3665           bool IsArrayForm = Record[I++] == 1;
3666           DelayedDeleteExprs.push_back(IsArrayForm);
3667         }
3668       }
3669       break;
3670 
3671     case IMPORTED_MODULES:
3672       if (!F.isModule()) {
3673         // If we aren't loading a module (which has its own exports), make
3674         // all of the imported modules visible.
3675         // FIXME: Deal with macros-only imports.
3676         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3677           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3678           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3679           if (GlobalID) {
3680             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3681             if (DeserializationListener)
3682               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3683           }
3684         }
3685       }
3686       break;
3687 
3688     case MACRO_OFFSET: {
3689       if (F.LocalNumMacros != 0) {
3690         Error("duplicate MACRO_OFFSET record in AST file");
3691         return Failure;
3692       }
3693       F.MacroOffsets = (const uint32_t *)Blob.data();
3694       F.LocalNumMacros = Record[0];
3695       unsigned LocalBaseMacroID = Record[1];
3696       F.BaseMacroID = getTotalNumMacros();
3697 
3698       if (F.LocalNumMacros > 0) {
3699         // Introduce the global -> local mapping for macros within this module.
3700         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3701 
3702         // Introduce the local -> global mapping for macros within this module.
3703         F.MacroRemap.insertOrReplace(
3704           std::make_pair(LocalBaseMacroID,
3705                          F.BaseMacroID - LocalBaseMacroID));
3706 
3707         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3708       }
3709       break;
3710     }
3711 
3712     case LATE_PARSED_TEMPLATE:
3713       LateParsedTemplates.append(Record.begin(), Record.end());
3714       break;
3715 
3716     case OPTIMIZE_PRAGMA_OPTIONS:
3717       if (Record.size() != 1) {
3718         Error("invalid pragma optimize record");
3719         return Failure;
3720       }
3721       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3722       break;
3723 
3724     case MSSTRUCT_PRAGMA_OPTIONS:
3725       if (Record.size() != 1) {
3726         Error("invalid pragma ms_struct record");
3727         return Failure;
3728       }
3729       PragmaMSStructState = Record[0];
3730       break;
3731 
3732     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3733       if (Record.size() != 2) {
3734         Error("invalid pragma ms_struct record");
3735         return Failure;
3736       }
3737       PragmaMSPointersToMembersState = Record[0];
3738       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3739       break;
3740 
3741     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3742       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3743         UnusedLocalTypedefNameCandidates.push_back(
3744             getGlobalDeclID(F, Record[I]));
3745       break;
3746 
3747     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3748       if (Record.size() != 1) {
3749         Error("invalid cuda pragma options record");
3750         return Failure;
3751       }
3752       ForceCUDAHostDeviceDepth = Record[0];
3753       break;
3754 
3755     case PACK_PRAGMA_OPTIONS: {
3756       if (Record.size() < 3) {
3757         Error("invalid pragma pack record");
3758         return Failure;
3759       }
3760       PragmaPackCurrentValue = Record[0];
3761       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3762       unsigned NumStackEntries = Record[2];
3763       unsigned Idx = 3;
3764       // Reset the stack when importing a new module.
3765       PragmaPackStack.clear();
3766       for (unsigned I = 0; I < NumStackEntries; ++I) {
3767         PragmaPackStackEntry Entry;
3768         Entry.Value = Record[Idx++];
3769         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3770         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3771         PragmaPackStrings.push_back(ReadString(Record, Idx));
3772         Entry.SlotLabel = PragmaPackStrings.back();
3773         PragmaPackStack.push_back(Entry);
3774       }
3775       break;
3776     }
3777     }
3778   }
3779 }
3780 
3781 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3782   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3783 
3784   // Additional remapping information.
3785   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3786   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3787   F.ModuleOffsetMap = StringRef();
3788 
3789   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3790   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3791     F.SLocRemap.insert(std::make_pair(0U, 0));
3792     F.SLocRemap.insert(std::make_pair(2U, 1));
3793   }
3794 
3795   // Continuous range maps we may be updating in our module.
3796   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3797   RemapBuilder SLocRemap(F.SLocRemap);
3798   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3799   RemapBuilder MacroRemap(F.MacroRemap);
3800   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3801   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3802   RemapBuilder SelectorRemap(F.SelectorRemap);
3803   RemapBuilder DeclRemap(F.DeclRemap);
3804   RemapBuilder TypeRemap(F.TypeRemap);
3805 
3806   while (Data < DataEnd) {
3807     // FIXME: Looking up dependency modules by filename is horrible. Let's
3808     // start fixing this with prebuilt and explicit modules and see how it
3809     // goes...
3810     using namespace llvm::support;
3811     ModuleKind Kind = static_cast<ModuleKind>(
3812       endian::readNext<uint8_t, little, unaligned>(Data));
3813     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3814     StringRef Name = StringRef((const char*)Data, Len);
3815     Data += Len;
3816     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3817                       ? ModuleMgr.lookupByModuleName(Name)
3818                       : ModuleMgr.lookupByFileName(Name));
3819     if (!OM) {
3820       std::string Msg =
3821           "SourceLocation remap refers to unknown module, cannot find ";
3822       Msg.append(std::string(Name));
3823       Error(Msg);
3824       return;
3825     }
3826 
3827     uint32_t SLocOffset =
3828         endian::readNext<uint32_t, little, unaligned>(Data);
3829     uint32_t IdentifierIDOffset =
3830         endian::readNext<uint32_t, little, unaligned>(Data);
3831     uint32_t MacroIDOffset =
3832         endian::readNext<uint32_t, little, unaligned>(Data);
3833     uint32_t PreprocessedEntityIDOffset =
3834         endian::readNext<uint32_t, little, unaligned>(Data);
3835     uint32_t SubmoduleIDOffset =
3836         endian::readNext<uint32_t, little, unaligned>(Data);
3837     uint32_t SelectorIDOffset =
3838         endian::readNext<uint32_t, little, unaligned>(Data);
3839     uint32_t DeclIDOffset =
3840         endian::readNext<uint32_t, little, unaligned>(Data);
3841     uint32_t TypeIndexOffset =
3842         endian::readNext<uint32_t, little, unaligned>(Data);
3843 
3844     uint32_t None = std::numeric_limits<uint32_t>::max();
3845 
3846     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3847                          RemapBuilder &Remap) {
3848       if (Offset != None)
3849         Remap.insert(std::make_pair(Offset,
3850                                     static_cast<int>(BaseOffset - Offset)));
3851     };
3852     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3853     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3854     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3855     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3856               PreprocessedEntityRemap);
3857     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3858     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3859     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3860     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3861 
3862     // Global -> local mappings.
3863     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3864   }
3865 }
3866 
3867 ASTReader::ASTReadResult
3868 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3869                                   const ModuleFile *ImportedBy,
3870                                   unsigned ClientLoadCapabilities) {
3871   unsigned Idx = 0;
3872   F.ModuleMapPath = ReadPath(F, Record, Idx);
3873 
3874   // Try to resolve ModuleName in the current header search context and
3875   // verify that it is found in the same module map file as we saved. If the
3876   // top-level AST file is a main file, skip this check because there is no
3877   // usable header search context.
3878   assert(!F.ModuleName.empty() &&
3879          "MODULE_NAME should come before MODULE_MAP_FILE");
3880   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3881     // An implicitly-loaded module file should have its module listed in some
3882     // module map file that we've already loaded.
3883     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3884     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3885     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3886     // Don't emit module relocation error if we have -fno-validate-pch
3887     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3888       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3889         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3890           // This module was defined by an imported (explicit) module.
3891           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3892                                                << ASTFE->getName();
3893         } else {
3894           // This module was built with a different module map.
3895           Diag(diag::err_imported_module_not_found)
3896               << F.ModuleName << F.FileName
3897               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3898               << !ImportedBy;
3899           // In case it was imported by a PCH, there's a chance the user is
3900           // just missing to include the search path to the directory containing
3901           // the modulemap.
3902           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3903             Diag(diag::note_imported_by_pch_module_not_found)
3904                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3905         }
3906       }
3907       return OutOfDate;
3908     }
3909 
3910     assert(M->Name == F.ModuleName && "found module with different name");
3911 
3912     // Check the primary module map file.
3913     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3914     if (!StoredModMap || *StoredModMap != ModMap) {
3915       assert(ModMap && "found module is missing module map file");
3916       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3917              "top-level import should be verified");
3918       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3919       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3920         Diag(diag::err_imported_module_modmap_changed)
3921             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3922             << ModMap->getName() << F.ModuleMapPath << NotImported;
3923       return OutOfDate;
3924     }
3925 
3926     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3927     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3928       // FIXME: we should use input files rather than storing names.
3929       std::string Filename = ReadPath(F, Record, Idx);
3930       auto F = FileMgr.getFile(Filename, false, false);
3931       if (!F) {
3932         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3933           Error("could not find file '" + Filename +"' referenced by AST file");
3934         return OutOfDate;
3935       }
3936       AdditionalStoredMaps.insert(*F);
3937     }
3938 
3939     // Check any additional module map files (e.g. module.private.modulemap)
3940     // that are not in the pcm.
3941     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3942       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3943         // Remove files that match
3944         // Note: SmallPtrSet::erase is really remove
3945         if (!AdditionalStoredMaps.erase(ModMap)) {
3946           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3947             Diag(diag::err_module_different_modmap)
3948               << F.ModuleName << /*new*/0 << ModMap->getName();
3949           return OutOfDate;
3950         }
3951       }
3952     }
3953 
3954     // Check any additional module map files that are in the pcm, but not
3955     // found in header search. Cases that match are already removed.
3956     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3957       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3958         Diag(diag::err_module_different_modmap)
3959           << F.ModuleName << /*not new*/1 << ModMap->getName();
3960       return OutOfDate;
3961     }
3962   }
3963 
3964   if (Listener)
3965     Listener->ReadModuleMapFile(F.ModuleMapPath);
3966   return Success;
3967 }
3968 
3969 /// Move the given method to the back of the global list of methods.
3970 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3971   // Find the entry for this selector in the method pool.
3972   Sema::GlobalMethodPool::iterator Known
3973     = S.MethodPool.find(Method->getSelector());
3974   if (Known == S.MethodPool.end())
3975     return;
3976 
3977   // Retrieve the appropriate method list.
3978   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3979                                                     : Known->second.second;
3980   bool Found = false;
3981   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3982     if (!Found) {
3983       if (List->getMethod() == Method) {
3984         Found = true;
3985       } else {
3986         // Keep searching.
3987         continue;
3988       }
3989     }
3990 
3991     if (List->getNext())
3992       List->setMethod(List->getNext()->getMethod());
3993     else
3994       List->setMethod(Method);
3995   }
3996 }
3997 
3998 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3999   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4000   for (Decl *D : Names) {
4001     bool wasHidden = D->isHidden();
4002     D->setVisibleDespiteOwningModule();
4003 
4004     if (wasHidden && SemaObj) {
4005       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4006         moveMethodToBackOfGlobalList(*SemaObj, Method);
4007       }
4008     }
4009   }
4010 }
4011 
4012 void ASTReader::makeModuleVisible(Module *Mod,
4013                                   Module::NameVisibilityKind NameVisibility,
4014                                   SourceLocation ImportLoc) {
4015   llvm::SmallPtrSet<Module *, 4> Visited;
4016   SmallVector<Module *, 4> Stack;
4017   Stack.push_back(Mod);
4018   while (!Stack.empty()) {
4019     Mod = Stack.pop_back_val();
4020 
4021     if (NameVisibility <= Mod->NameVisibility) {
4022       // This module already has this level of visibility (or greater), so
4023       // there is nothing more to do.
4024       continue;
4025     }
4026 
4027     if (!Mod->isAvailable()) {
4028       // Modules that aren't available cannot be made visible.
4029       continue;
4030     }
4031 
4032     // Update the module's name visibility.
4033     Mod->NameVisibility = NameVisibility;
4034 
4035     // If we've already deserialized any names from this module,
4036     // mark them as visible.
4037     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4038     if (Hidden != HiddenNamesMap.end()) {
4039       auto HiddenNames = std::move(*Hidden);
4040       HiddenNamesMap.erase(Hidden);
4041       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4042       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4043              "making names visible added hidden names");
4044     }
4045 
4046     // Push any exported modules onto the stack to be marked as visible.
4047     SmallVector<Module *, 16> Exports;
4048     Mod->getExportedModules(Exports);
4049     for (SmallVectorImpl<Module *>::iterator
4050            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4051       Module *Exported = *I;
4052       if (Visited.insert(Exported).second)
4053         Stack.push_back(Exported);
4054     }
4055   }
4056 }
4057 
4058 /// We've merged the definition \p MergedDef into the existing definition
4059 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4060 /// visible.
4061 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4062                                           NamedDecl *MergedDef) {
4063   if (Def->isHidden()) {
4064     // If MergedDef is visible or becomes visible, make the definition visible.
4065     if (!MergedDef->isHidden())
4066       Def->setVisibleDespiteOwningModule();
4067     else {
4068       getContext().mergeDefinitionIntoModule(
4069           Def, MergedDef->getImportedOwningModule(),
4070           /*NotifyListeners*/ false);
4071       PendingMergedDefinitionsToDeduplicate.insert(Def);
4072     }
4073   }
4074 }
4075 
4076 bool ASTReader::loadGlobalIndex() {
4077   if (GlobalIndex)
4078     return false;
4079 
4080   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4081       !PP.getLangOpts().Modules)
4082     return true;
4083 
4084   // Try to load the global index.
4085   TriedLoadingGlobalIndex = true;
4086   StringRef ModuleCachePath
4087     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4088   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4089       GlobalModuleIndex::readIndex(ModuleCachePath);
4090   if (llvm::Error Err = std::move(Result.second)) {
4091     assert(!Result.first);
4092     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4093     return true;
4094   }
4095 
4096   GlobalIndex.reset(Result.first);
4097   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4098   return false;
4099 }
4100 
4101 bool ASTReader::isGlobalIndexUnavailable() const {
4102   return PP.getLangOpts().Modules && UseGlobalIndex &&
4103          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4104 }
4105 
4106 static void updateModuleTimestamp(ModuleFile &MF) {
4107   // Overwrite the timestamp file contents so that file's mtime changes.
4108   std::string TimestampFilename = MF.getTimestampFilename();
4109   std::error_code EC;
4110   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4111   if (EC)
4112     return;
4113   OS << "Timestamp file\n";
4114   OS.close();
4115   OS.clear_error(); // Avoid triggering a fatal error.
4116 }
4117 
4118 /// Given a cursor at the start of an AST file, scan ahead and drop the
4119 /// cursor into the start of the given block ID, returning false on success and
4120 /// true on failure.
4121 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4122   while (true) {
4123     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4124     if (!MaybeEntry) {
4125       // FIXME this drops errors on the floor.
4126       consumeError(MaybeEntry.takeError());
4127       return true;
4128     }
4129     llvm::BitstreamEntry Entry = MaybeEntry.get();
4130 
4131     switch (Entry.Kind) {
4132     case llvm::BitstreamEntry::Error:
4133     case llvm::BitstreamEntry::EndBlock:
4134       return true;
4135 
4136     case llvm::BitstreamEntry::Record:
4137       // Ignore top-level records.
4138       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4139         break;
4140       else {
4141         // FIXME this drops errors on the floor.
4142         consumeError(Skipped.takeError());
4143         return true;
4144       }
4145 
4146     case llvm::BitstreamEntry::SubBlock:
4147       if (Entry.ID == BlockID) {
4148         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4149           // FIXME this drops the error on the floor.
4150           consumeError(std::move(Err));
4151           return true;
4152         }
4153         // Found it!
4154         return false;
4155       }
4156 
4157       if (llvm::Error Err = Cursor.SkipBlock()) {
4158         // FIXME this drops the error on the floor.
4159         consumeError(std::move(Err));
4160         return true;
4161       }
4162     }
4163   }
4164 }
4165 
4166 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4167                                             ModuleKind Type,
4168                                             SourceLocation ImportLoc,
4169                                             unsigned ClientLoadCapabilities,
4170                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4171   llvm::SaveAndRestore<SourceLocation>
4172     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4173 
4174   // Defer any pending actions until we get to the end of reading the AST file.
4175   Deserializing AnASTFile(this);
4176 
4177   // Bump the generation number.
4178   unsigned PreviousGeneration = 0;
4179   if (ContextObj)
4180     PreviousGeneration = incrementGeneration(*ContextObj);
4181 
4182   unsigned NumModules = ModuleMgr.size();
4183   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4184     assert(ReadResult && "expected to return error");
4185     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4186                             PP.getLangOpts().Modules
4187                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4188                                 : nullptr);
4189 
4190     // If we find that any modules are unusable, the global index is going
4191     // to be out-of-date. Just remove it.
4192     GlobalIndex.reset();
4193     ModuleMgr.setGlobalIndex(nullptr);
4194     return ReadResult;
4195   };
4196 
4197   SmallVector<ImportedModule, 4> Loaded;
4198   switch (ASTReadResult ReadResult =
4199               ReadASTCore(FileName, Type, ImportLoc,
4200                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4201                           ASTFileSignature(), ClientLoadCapabilities)) {
4202   case Failure:
4203   case Missing:
4204   case OutOfDate:
4205   case VersionMismatch:
4206   case ConfigurationMismatch:
4207   case HadErrors:
4208     return removeModulesAndReturn(ReadResult);
4209   case Success:
4210     break;
4211   }
4212 
4213   // Here comes stuff that we only do once the entire chain is loaded.
4214 
4215   // Load the AST blocks of all of the modules that we loaded.  We can still
4216   // hit errors parsing the ASTs at this point.
4217   for (ImportedModule &M : Loaded) {
4218     ModuleFile &F = *M.Mod;
4219 
4220     // Read the AST block.
4221     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4222       return removeModulesAndReturn(Result);
4223 
4224     // The AST block should always have a definition for the main module.
4225     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4226       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4227       return removeModulesAndReturn(Failure);
4228     }
4229 
4230     // Read the extension blocks.
4231     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4232       if (ASTReadResult Result = ReadExtensionBlock(F))
4233         return removeModulesAndReturn(Result);
4234     }
4235 
4236     // Once read, set the ModuleFile bit base offset and update the size in
4237     // bits of all files we've seen.
4238     F.GlobalBitOffset = TotalModulesSizeInBits;
4239     TotalModulesSizeInBits += F.SizeInBits;
4240     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4241   }
4242 
4243   // Preload source locations and interesting indentifiers.
4244   for (ImportedModule &M : Loaded) {
4245     ModuleFile &F = *M.Mod;
4246 
4247     // Preload SLocEntries.
4248     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4249       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4250       // Load it through the SourceManager and don't call ReadSLocEntry()
4251       // directly because the entry may have already been loaded in which case
4252       // calling ReadSLocEntry() directly would trigger an assertion in
4253       // SourceManager.
4254       SourceMgr.getLoadedSLocEntryByID(Index);
4255     }
4256 
4257     // Map the original source file ID into the ID space of the current
4258     // compilation.
4259     if (F.OriginalSourceFileID.isValid()) {
4260       F.OriginalSourceFileID = FileID::get(
4261           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4262     }
4263 
4264     // Preload all the pending interesting identifiers by marking them out of
4265     // date.
4266     for (auto Offset : F.PreloadIdentifierOffsets) {
4267       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4268           F.IdentifierTableData + Offset);
4269 
4270       ASTIdentifierLookupTrait Trait(*this, F);
4271       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4272       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4273       auto &II = PP.getIdentifierTable().getOwn(Key);
4274       II.setOutOfDate(true);
4275 
4276       // Mark this identifier as being from an AST file so that we can track
4277       // whether we need to serialize it.
4278       markIdentifierFromAST(*this, II);
4279 
4280       // Associate the ID with the identifier so that the writer can reuse it.
4281       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4282       SetIdentifierInfo(ID, &II);
4283     }
4284   }
4285 
4286   // Setup the import locations and notify the module manager that we've
4287   // committed to these module files.
4288   for (ImportedModule &M : Loaded) {
4289     ModuleFile &F = *M.Mod;
4290 
4291     ModuleMgr.moduleFileAccepted(&F);
4292 
4293     // Set the import location.
4294     F.DirectImportLoc = ImportLoc;
4295     // FIXME: We assume that locations from PCH / preamble do not need
4296     // any translation.
4297     if (!M.ImportedBy)
4298       F.ImportLoc = M.ImportLoc;
4299     else
4300       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4301   }
4302 
4303   if (!PP.getLangOpts().CPlusPlus ||
4304       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4305        Type != MK_PrebuiltModule)) {
4306     // Mark all of the identifiers in the identifier table as being out of date,
4307     // so that various accessors know to check the loaded modules when the
4308     // identifier is used.
4309     //
4310     // For C++ modules, we don't need information on many identifiers (just
4311     // those that provide macros or are poisoned), so we mark all of
4312     // the interesting ones via PreloadIdentifierOffsets.
4313     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4314                                 IdEnd = PP.getIdentifierTable().end();
4315          Id != IdEnd; ++Id)
4316       Id->second->setOutOfDate(true);
4317   }
4318   // Mark selectors as out of date.
4319   for (auto Sel : SelectorGeneration)
4320     SelectorOutOfDate[Sel.first] = true;
4321 
4322   // Resolve any unresolved module exports.
4323   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4324     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4325     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4326     Module *ResolvedMod = getSubmodule(GlobalID);
4327 
4328     switch (Unresolved.Kind) {
4329     case UnresolvedModuleRef::Conflict:
4330       if (ResolvedMod) {
4331         Module::Conflict Conflict;
4332         Conflict.Other = ResolvedMod;
4333         Conflict.Message = Unresolved.String.str();
4334         Unresolved.Mod->Conflicts.push_back(Conflict);
4335       }
4336       continue;
4337 
4338     case UnresolvedModuleRef::Import:
4339       if (ResolvedMod)
4340         Unresolved.Mod->Imports.insert(ResolvedMod);
4341       continue;
4342 
4343     case UnresolvedModuleRef::Export:
4344       if (ResolvedMod || Unresolved.IsWildcard)
4345         Unresolved.Mod->Exports.push_back(
4346           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4347       continue;
4348     }
4349   }
4350   UnresolvedModuleRefs.clear();
4351 
4352   if (Imported)
4353     Imported->append(ImportedModules.begin(),
4354                      ImportedModules.end());
4355 
4356   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4357   // Might be unnecessary as use declarations are only used to build the
4358   // module itself.
4359 
4360   if (ContextObj)
4361     InitializeContext();
4362 
4363   if (SemaObj)
4364     UpdateSema();
4365 
4366   if (DeserializationListener)
4367     DeserializationListener->ReaderInitialized(this);
4368 
4369   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4370   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4371     // If this AST file is a precompiled preamble, then set the
4372     // preamble file ID of the source manager to the file source file
4373     // from which the preamble was built.
4374     if (Type == MK_Preamble) {
4375       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4376     } else if (Type == MK_MainFile) {
4377       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4378     }
4379   }
4380 
4381   // For any Objective-C class definitions we have already loaded, make sure
4382   // that we load any additional categories.
4383   if (ContextObj) {
4384     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4385       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4386                          ObjCClassesLoaded[I],
4387                          PreviousGeneration);
4388     }
4389   }
4390 
4391   if (PP.getHeaderSearchInfo()
4392           .getHeaderSearchOpts()
4393           .ModulesValidateOncePerBuildSession) {
4394     // Now we are certain that the module and all modules it depends on are
4395     // up to date.  Create or update timestamp files for modules that are
4396     // located in the module cache (not for PCH files that could be anywhere
4397     // in the filesystem).
4398     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4399       ImportedModule &M = Loaded[I];
4400       if (M.Mod->Kind == MK_ImplicitModule) {
4401         updateModuleTimestamp(*M.Mod);
4402       }
4403     }
4404   }
4405 
4406   return Success;
4407 }
4408 
4409 static ASTFileSignature readASTFileSignature(StringRef PCH);
4410 
4411 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4412 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4413   // FIXME checking magic headers is done in other places such as
4414   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4415   // always done the same. Unify it all with a helper.
4416   if (!Stream.canSkipToPos(4))
4417     return llvm::createStringError(std::errc::illegal_byte_sequence,
4418                                    "file too small to contain AST file magic");
4419   for (unsigned C : {'C', 'P', 'C', 'H'})
4420     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4421       if (Res.get() != C)
4422         return llvm::createStringError(
4423             std::errc::illegal_byte_sequence,
4424             "file doesn't start with AST file magic");
4425     } else
4426       return Res.takeError();
4427   return llvm::Error::success();
4428 }
4429 
4430 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4431   switch (Kind) {
4432   case MK_PCH:
4433     return 0; // PCH
4434   case MK_ImplicitModule:
4435   case MK_ExplicitModule:
4436   case MK_PrebuiltModule:
4437     return 1; // module
4438   case MK_MainFile:
4439   case MK_Preamble:
4440     return 2; // main source file
4441   }
4442   llvm_unreachable("unknown module kind");
4443 }
4444 
4445 ASTReader::ASTReadResult
4446 ASTReader::ReadASTCore(StringRef FileName,
4447                        ModuleKind Type,
4448                        SourceLocation ImportLoc,
4449                        ModuleFile *ImportedBy,
4450                        SmallVectorImpl<ImportedModule> &Loaded,
4451                        off_t ExpectedSize, time_t ExpectedModTime,
4452                        ASTFileSignature ExpectedSignature,
4453                        unsigned ClientLoadCapabilities) {
4454   ModuleFile *M;
4455   std::string ErrorStr;
4456   ModuleManager::AddModuleResult AddResult
4457     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4458                           getGeneration(), ExpectedSize, ExpectedModTime,
4459                           ExpectedSignature, readASTFileSignature,
4460                           M, ErrorStr);
4461 
4462   switch (AddResult) {
4463   case ModuleManager::AlreadyLoaded:
4464     Diag(diag::remark_module_import)
4465         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4466         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4467     return Success;
4468 
4469   case ModuleManager::NewlyLoaded:
4470     // Load module file below.
4471     break;
4472 
4473   case ModuleManager::Missing:
4474     // The module file was missing; if the client can handle that, return
4475     // it.
4476     if (ClientLoadCapabilities & ARR_Missing)
4477       return Missing;
4478 
4479     // Otherwise, return an error.
4480     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4481                                           << FileName << !ErrorStr.empty()
4482                                           << ErrorStr;
4483     return Failure;
4484 
4485   case ModuleManager::OutOfDate:
4486     // We couldn't load the module file because it is out-of-date. If the
4487     // client can handle out-of-date, return it.
4488     if (ClientLoadCapabilities & ARR_OutOfDate)
4489       return OutOfDate;
4490 
4491     // Otherwise, return an error.
4492     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4493                                             << FileName << !ErrorStr.empty()
4494                                             << ErrorStr;
4495     return Failure;
4496   }
4497 
4498   assert(M && "Missing module file");
4499 
4500   bool ShouldFinalizePCM = false;
4501   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4502     auto &MC = getModuleManager().getModuleCache();
4503     if (ShouldFinalizePCM)
4504       MC.finalizePCM(FileName);
4505     else
4506       MC.tryToRemovePCM(FileName);
4507   });
4508   ModuleFile &F = *M;
4509   BitstreamCursor &Stream = F.Stream;
4510   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4511   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4512 
4513   // Sniff for the signature.
4514   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4515     Diag(diag::err_module_file_invalid)
4516         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4517     return Failure;
4518   }
4519 
4520   // This is used for compatibility with older PCH formats.
4521   bool HaveReadControlBlock = false;
4522   while (true) {
4523     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4524     if (!MaybeEntry) {
4525       Error(MaybeEntry.takeError());
4526       return Failure;
4527     }
4528     llvm::BitstreamEntry Entry = MaybeEntry.get();
4529 
4530     switch (Entry.Kind) {
4531     case llvm::BitstreamEntry::Error:
4532     case llvm::BitstreamEntry::Record:
4533     case llvm::BitstreamEntry::EndBlock:
4534       Error("invalid record at top-level of AST file");
4535       return Failure;
4536 
4537     case llvm::BitstreamEntry::SubBlock:
4538       break;
4539     }
4540 
4541     switch (Entry.ID) {
4542     case CONTROL_BLOCK_ID:
4543       HaveReadControlBlock = true;
4544       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4545       case Success:
4546         // Check that we didn't try to load a non-module AST file as a module.
4547         //
4548         // FIXME: Should we also perform the converse check? Loading a module as
4549         // a PCH file sort of works, but it's a bit wonky.
4550         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4551              Type == MK_PrebuiltModule) &&
4552             F.ModuleName.empty()) {
4553           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4554           if (Result != OutOfDate ||
4555               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4556             Diag(diag::err_module_file_not_module) << FileName;
4557           return Result;
4558         }
4559         break;
4560 
4561       case Failure: return Failure;
4562       case Missing: return Missing;
4563       case OutOfDate: return OutOfDate;
4564       case VersionMismatch: return VersionMismatch;
4565       case ConfigurationMismatch: return ConfigurationMismatch;
4566       case HadErrors: return HadErrors;
4567       }
4568       break;
4569 
4570     case AST_BLOCK_ID:
4571       if (!HaveReadControlBlock) {
4572         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4573           Diag(diag::err_pch_version_too_old);
4574         return VersionMismatch;
4575       }
4576 
4577       // Record that we've loaded this module.
4578       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4579       ShouldFinalizePCM = true;
4580       return Success;
4581 
4582     case UNHASHED_CONTROL_BLOCK_ID:
4583       // This block is handled using look-ahead during ReadControlBlock.  We
4584       // shouldn't get here!
4585       Error("malformed block record in AST file");
4586       return Failure;
4587 
4588     default:
4589       if (llvm::Error Err = Stream.SkipBlock()) {
4590         Error(std::move(Err));
4591         return Failure;
4592       }
4593       break;
4594     }
4595   }
4596 
4597   llvm_unreachable("unexpected break; expected return");
4598 }
4599 
4600 ASTReader::ASTReadResult
4601 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4602                                     unsigned ClientLoadCapabilities) {
4603   const HeaderSearchOptions &HSOpts =
4604       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4605   bool AllowCompatibleConfigurationMismatch =
4606       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4607 
4608   ASTReadResult Result = readUnhashedControlBlockImpl(
4609       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4610       Listener.get(),
4611       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4612 
4613   // If F was directly imported by another module, it's implicitly validated by
4614   // the importing module.
4615   if (DisableValidation || WasImportedBy ||
4616       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4617     return Success;
4618 
4619   if (Result == Failure) {
4620     Error("malformed block record in AST file");
4621     return Failure;
4622   }
4623 
4624   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4625     // If this module has already been finalized in the ModuleCache, we're stuck
4626     // with it; we can only load a single version of each module.
4627     //
4628     // This can happen when a module is imported in two contexts: in one, as a
4629     // user module; in another, as a system module (due to an import from
4630     // another module marked with the [system] flag).  It usually indicates a
4631     // bug in the module map: this module should also be marked with [system].
4632     //
4633     // If -Wno-system-headers (the default), and the first import is as a
4634     // system module, then validation will fail during the as-user import,
4635     // since -Werror flags won't have been validated.  However, it's reasonable
4636     // to treat this consistently as a system module.
4637     //
4638     // If -Wsystem-headers, the PCM on disk was built with
4639     // -Wno-system-headers, and the first import is as a user module, then
4640     // validation will fail during the as-system import since the PCM on disk
4641     // doesn't guarantee that -Werror was respected.  However, the -Werror
4642     // flags were checked during the initial as-user import.
4643     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4644       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4645       return Success;
4646     }
4647   }
4648 
4649   return Result;
4650 }
4651 
4652 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4653     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4654     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4655     bool ValidateDiagnosticOptions) {
4656   // Initialize a stream.
4657   BitstreamCursor Stream(StreamData);
4658 
4659   // Sniff for the signature.
4660   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4661     // FIXME this drops the error on the floor.
4662     consumeError(std::move(Err));
4663     return Failure;
4664   }
4665 
4666   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4667   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4668     return Failure;
4669 
4670   // Read all of the records in the options block.
4671   RecordData Record;
4672   ASTReadResult Result = Success;
4673   while (true) {
4674     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4675     if (!MaybeEntry) {
4676       // FIXME this drops the error on the floor.
4677       consumeError(MaybeEntry.takeError());
4678       return Failure;
4679     }
4680     llvm::BitstreamEntry Entry = MaybeEntry.get();
4681 
4682     switch (Entry.Kind) {
4683     case llvm::BitstreamEntry::Error:
4684     case llvm::BitstreamEntry::SubBlock:
4685       return Failure;
4686 
4687     case llvm::BitstreamEntry::EndBlock:
4688       return Result;
4689 
4690     case llvm::BitstreamEntry::Record:
4691       // The interesting case.
4692       break;
4693     }
4694 
4695     // Read and process a record.
4696     Record.clear();
4697     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4698     if (!MaybeRecordType) {
4699       // FIXME this drops the error.
4700       return Failure;
4701     }
4702     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4703     case SIGNATURE:
4704       if (F)
4705         std::copy(Record.begin(), Record.end(), F->Signature.data());
4706       break;
4707     case DIAGNOSTIC_OPTIONS: {
4708       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4709       if (Listener && ValidateDiagnosticOptions &&
4710           !AllowCompatibleConfigurationMismatch &&
4711           ParseDiagnosticOptions(Record, Complain, *Listener))
4712         Result = OutOfDate; // Don't return early.  Read the signature.
4713       break;
4714     }
4715     case DIAG_PRAGMA_MAPPINGS:
4716       if (!F)
4717         break;
4718       if (F->PragmaDiagMappings.empty())
4719         F->PragmaDiagMappings.swap(Record);
4720       else
4721         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4722                                      Record.begin(), Record.end());
4723       break;
4724     }
4725   }
4726 }
4727 
4728 /// Parse a record and blob containing module file extension metadata.
4729 static bool parseModuleFileExtensionMetadata(
4730               const SmallVectorImpl<uint64_t> &Record,
4731               StringRef Blob,
4732               ModuleFileExtensionMetadata &Metadata) {
4733   if (Record.size() < 4) return true;
4734 
4735   Metadata.MajorVersion = Record[0];
4736   Metadata.MinorVersion = Record[1];
4737 
4738   unsigned BlockNameLen = Record[2];
4739   unsigned UserInfoLen = Record[3];
4740 
4741   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4742 
4743   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4744   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4745                                   Blob.data() + BlockNameLen + UserInfoLen);
4746   return false;
4747 }
4748 
4749 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4750   BitstreamCursor &Stream = F.Stream;
4751 
4752   RecordData Record;
4753   while (true) {
4754     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4755     if (!MaybeEntry) {
4756       Error(MaybeEntry.takeError());
4757       return Failure;
4758     }
4759     llvm::BitstreamEntry Entry = MaybeEntry.get();
4760 
4761     switch (Entry.Kind) {
4762     case llvm::BitstreamEntry::SubBlock:
4763       if (llvm::Error Err = Stream.SkipBlock()) {
4764         Error(std::move(Err));
4765         return Failure;
4766       }
4767       continue;
4768 
4769     case llvm::BitstreamEntry::EndBlock:
4770       return Success;
4771 
4772     case llvm::BitstreamEntry::Error:
4773       return HadErrors;
4774 
4775     case llvm::BitstreamEntry::Record:
4776       break;
4777     }
4778 
4779     Record.clear();
4780     StringRef Blob;
4781     Expected<unsigned> MaybeRecCode =
4782         Stream.readRecord(Entry.ID, Record, &Blob);
4783     if (!MaybeRecCode) {
4784       Error(MaybeRecCode.takeError());
4785       return Failure;
4786     }
4787     switch (MaybeRecCode.get()) {
4788     case EXTENSION_METADATA: {
4789       ModuleFileExtensionMetadata Metadata;
4790       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4791         Error("malformed EXTENSION_METADATA in AST file");
4792         return Failure;
4793       }
4794 
4795       // Find a module file extension with this block name.
4796       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4797       if (Known == ModuleFileExtensions.end()) break;
4798 
4799       // Form a reader.
4800       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4801                                                              F, Stream)) {
4802         F.ExtensionReaders.push_back(std::move(Reader));
4803       }
4804 
4805       break;
4806     }
4807     }
4808   }
4809 
4810   return Success;
4811 }
4812 
4813 void ASTReader::InitializeContext() {
4814   assert(ContextObj && "no context to initialize");
4815   ASTContext &Context = *ContextObj;
4816 
4817   // If there's a listener, notify them that we "read" the translation unit.
4818   if (DeserializationListener)
4819     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4820                                       Context.getTranslationUnitDecl());
4821 
4822   // FIXME: Find a better way to deal with collisions between these
4823   // built-in types. Right now, we just ignore the problem.
4824 
4825   // Load the special types.
4826   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4827     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4828       if (!Context.CFConstantStringTypeDecl)
4829         Context.setCFConstantStringType(GetType(String));
4830     }
4831 
4832     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4833       QualType FileType = GetType(File);
4834       if (FileType.isNull()) {
4835         Error("FILE type is NULL");
4836         return;
4837       }
4838 
4839       if (!Context.FILEDecl) {
4840         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4841           Context.setFILEDecl(Typedef->getDecl());
4842         else {
4843           const TagType *Tag = FileType->getAs<TagType>();
4844           if (!Tag) {
4845             Error("Invalid FILE type in AST file");
4846             return;
4847           }
4848           Context.setFILEDecl(Tag->getDecl());
4849         }
4850       }
4851     }
4852 
4853     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4854       QualType Jmp_bufType = GetType(Jmp_buf);
4855       if (Jmp_bufType.isNull()) {
4856         Error("jmp_buf type is NULL");
4857         return;
4858       }
4859 
4860       if (!Context.jmp_bufDecl) {
4861         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4862           Context.setjmp_bufDecl(Typedef->getDecl());
4863         else {
4864           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4865           if (!Tag) {
4866             Error("Invalid jmp_buf type in AST file");
4867             return;
4868           }
4869           Context.setjmp_bufDecl(Tag->getDecl());
4870         }
4871       }
4872     }
4873 
4874     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4875       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4876       if (Sigjmp_bufType.isNull()) {
4877         Error("sigjmp_buf type is NULL");
4878         return;
4879       }
4880 
4881       if (!Context.sigjmp_bufDecl) {
4882         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4883           Context.setsigjmp_bufDecl(Typedef->getDecl());
4884         else {
4885           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4886           assert(Tag && "Invalid sigjmp_buf type in AST file");
4887           Context.setsigjmp_bufDecl(Tag->getDecl());
4888         }
4889       }
4890     }
4891 
4892     if (unsigned ObjCIdRedef
4893           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4894       if (Context.ObjCIdRedefinitionType.isNull())
4895         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4896     }
4897 
4898     if (unsigned ObjCClassRedef
4899           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4900       if (Context.ObjCClassRedefinitionType.isNull())
4901         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4902     }
4903 
4904     if (unsigned ObjCSelRedef
4905           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4906       if (Context.ObjCSelRedefinitionType.isNull())
4907         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4908     }
4909 
4910     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4911       QualType Ucontext_tType = GetType(Ucontext_t);
4912       if (Ucontext_tType.isNull()) {
4913         Error("ucontext_t type is NULL");
4914         return;
4915       }
4916 
4917       if (!Context.ucontext_tDecl) {
4918         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4919           Context.setucontext_tDecl(Typedef->getDecl());
4920         else {
4921           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4922           assert(Tag && "Invalid ucontext_t type in AST file");
4923           Context.setucontext_tDecl(Tag->getDecl());
4924         }
4925       }
4926     }
4927   }
4928 
4929   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4930 
4931   // If there were any CUDA special declarations, deserialize them.
4932   if (!CUDASpecialDeclRefs.empty()) {
4933     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4934     Context.setcudaConfigureCallDecl(
4935                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4936   }
4937 
4938   // Re-export any modules that were imported by a non-module AST file.
4939   // FIXME: This does not make macro-only imports visible again.
4940   for (auto &Import : ImportedModules) {
4941     if (Module *Imported = getSubmodule(Import.ID)) {
4942       makeModuleVisible(Imported, Module::AllVisible,
4943                         /*ImportLoc=*/Import.ImportLoc);
4944       if (Import.ImportLoc.isValid())
4945         PP.makeModuleVisible(Imported, Import.ImportLoc);
4946       // FIXME: should we tell Sema to make the module visible too?
4947     }
4948   }
4949   ImportedModules.clear();
4950 }
4951 
4952 void ASTReader::finalizeForWriting() {
4953   // Nothing to do for now.
4954 }
4955 
4956 /// Reads and return the signature record from \p PCH's control block, or
4957 /// else returns 0.
4958 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4959   BitstreamCursor Stream(PCH);
4960   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4961     // FIXME this drops the error on the floor.
4962     consumeError(std::move(Err));
4963     return ASTFileSignature();
4964   }
4965 
4966   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4967   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4968     return ASTFileSignature();
4969 
4970   // Scan for SIGNATURE inside the diagnostic options block.
4971   ASTReader::RecordData Record;
4972   while (true) {
4973     Expected<llvm::BitstreamEntry> MaybeEntry =
4974         Stream.advanceSkippingSubblocks();
4975     if (!MaybeEntry) {
4976       // FIXME this drops the error on the floor.
4977       consumeError(MaybeEntry.takeError());
4978       return ASTFileSignature();
4979     }
4980     llvm::BitstreamEntry Entry = MaybeEntry.get();
4981 
4982     if (Entry.Kind != llvm::BitstreamEntry::Record)
4983       return ASTFileSignature();
4984 
4985     Record.clear();
4986     StringRef Blob;
4987     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4988     if (!MaybeRecord) {
4989       // FIXME this drops the error on the floor.
4990       consumeError(MaybeRecord.takeError());
4991       return ASTFileSignature();
4992     }
4993     if (SIGNATURE == MaybeRecord.get())
4994       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4995                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4996   }
4997 }
4998 
4999 /// Retrieve the name of the original source file name
5000 /// directly from the AST file, without actually loading the AST
5001 /// file.
5002 std::string ASTReader::getOriginalSourceFile(
5003     const std::string &ASTFileName, FileManager &FileMgr,
5004     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5005   // Open the AST file.
5006   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5007   if (!Buffer) {
5008     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5009         << ASTFileName << Buffer.getError().message();
5010     return std::string();
5011   }
5012 
5013   // Initialize the stream
5014   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5015 
5016   // Sniff for the signature.
5017   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5018     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5019     return std::string();
5020   }
5021 
5022   // Scan for the CONTROL_BLOCK_ID block.
5023   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5024     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5025     return std::string();
5026   }
5027 
5028   // Scan for ORIGINAL_FILE inside the control block.
5029   RecordData Record;
5030   while (true) {
5031     Expected<llvm::BitstreamEntry> MaybeEntry =
5032         Stream.advanceSkippingSubblocks();
5033     if (!MaybeEntry) {
5034       // FIXME this drops errors on the floor.
5035       consumeError(MaybeEntry.takeError());
5036       return std::string();
5037     }
5038     llvm::BitstreamEntry Entry = MaybeEntry.get();
5039 
5040     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5041       return std::string();
5042 
5043     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5044       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5045       return std::string();
5046     }
5047 
5048     Record.clear();
5049     StringRef Blob;
5050     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5051     if (!MaybeRecord) {
5052       // FIXME this drops the errors on the floor.
5053       consumeError(MaybeRecord.takeError());
5054       return std::string();
5055     }
5056     if (ORIGINAL_FILE == MaybeRecord.get())
5057       return Blob.str();
5058   }
5059 }
5060 
5061 namespace {
5062 
5063   class SimplePCHValidator : public ASTReaderListener {
5064     const LangOptions &ExistingLangOpts;
5065     const TargetOptions &ExistingTargetOpts;
5066     const PreprocessorOptions &ExistingPPOpts;
5067     std::string ExistingModuleCachePath;
5068     FileManager &FileMgr;
5069 
5070   public:
5071     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5072                        const TargetOptions &ExistingTargetOpts,
5073                        const PreprocessorOptions &ExistingPPOpts,
5074                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5075         : ExistingLangOpts(ExistingLangOpts),
5076           ExistingTargetOpts(ExistingTargetOpts),
5077           ExistingPPOpts(ExistingPPOpts),
5078           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5079 
5080     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5081                              bool AllowCompatibleDifferences) override {
5082       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5083                                   AllowCompatibleDifferences);
5084     }
5085 
5086     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5087                            bool AllowCompatibleDifferences) override {
5088       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5089                                 AllowCompatibleDifferences);
5090     }
5091 
5092     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5093                                  StringRef SpecificModuleCachePath,
5094                                  bool Complain) override {
5095       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5096                                       ExistingModuleCachePath,
5097                                       nullptr, ExistingLangOpts);
5098     }
5099 
5100     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5101                                  bool Complain,
5102                                  std::string &SuggestedPredefines) override {
5103       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5104                                       SuggestedPredefines, ExistingLangOpts);
5105     }
5106   };
5107 
5108 } // namespace
5109 
5110 bool ASTReader::readASTFileControlBlock(
5111     StringRef Filename, FileManager &FileMgr,
5112     const PCHContainerReader &PCHContainerRdr,
5113     bool FindModuleFileExtensions,
5114     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5115   // Open the AST file.
5116   // FIXME: This allows use of the VFS; we do not allow use of the
5117   // VFS when actually loading a module.
5118   auto Buffer = FileMgr.getBufferForFile(Filename);
5119   if (!Buffer) {
5120     return true;
5121   }
5122 
5123   // Initialize the stream
5124   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5125   BitstreamCursor Stream(Bytes);
5126 
5127   // Sniff for the signature.
5128   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5129     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5130     return true;
5131   }
5132 
5133   // Scan for the CONTROL_BLOCK_ID block.
5134   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5135     return true;
5136 
5137   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5138   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5139   bool NeedsImports = Listener.needsImportVisitation();
5140   BitstreamCursor InputFilesCursor;
5141 
5142   RecordData Record;
5143   std::string ModuleDir;
5144   bool DoneWithControlBlock = false;
5145   while (!DoneWithControlBlock) {
5146     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5147     if (!MaybeEntry) {
5148       // FIXME this drops the error on the floor.
5149       consumeError(MaybeEntry.takeError());
5150       return true;
5151     }
5152     llvm::BitstreamEntry Entry = MaybeEntry.get();
5153 
5154     switch (Entry.Kind) {
5155     case llvm::BitstreamEntry::SubBlock: {
5156       switch (Entry.ID) {
5157       case OPTIONS_BLOCK_ID: {
5158         std::string IgnoredSuggestedPredefines;
5159         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5160                              /*AllowCompatibleConfigurationMismatch*/ false,
5161                              Listener, IgnoredSuggestedPredefines) != Success)
5162           return true;
5163         break;
5164       }
5165 
5166       case INPUT_FILES_BLOCK_ID:
5167         InputFilesCursor = Stream;
5168         if (llvm::Error Err = Stream.SkipBlock()) {
5169           // FIXME this drops the error on the floor.
5170           consumeError(std::move(Err));
5171           return true;
5172         }
5173         if (NeedsInputFiles &&
5174             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5175           return true;
5176         break;
5177 
5178       default:
5179         if (llvm::Error Err = Stream.SkipBlock()) {
5180           // FIXME this drops the error on the floor.
5181           consumeError(std::move(Err));
5182           return true;
5183         }
5184         break;
5185       }
5186 
5187       continue;
5188     }
5189 
5190     case llvm::BitstreamEntry::EndBlock:
5191       DoneWithControlBlock = true;
5192       break;
5193 
5194     case llvm::BitstreamEntry::Error:
5195       return true;
5196 
5197     case llvm::BitstreamEntry::Record:
5198       break;
5199     }
5200 
5201     if (DoneWithControlBlock) break;
5202 
5203     Record.clear();
5204     StringRef Blob;
5205     Expected<unsigned> MaybeRecCode =
5206         Stream.readRecord(Entry.ID, Record, &Blob);
5207     if (!MaybeRecCode) {
5208       // FIXME this drops the error.
5209       return Failure;
5210     }
5211     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5212     case METADATA:
5213       if (Record[0] != VERSION_MAJOR)
5214         return true;
5215       if (Listener.ReadFullVersionInformation(Blob))
5216         return true;
5217       break;
5218     case MODULE_NAME:
5219       Listener.ReadModuleName(Blob);
5220       break;
5221     case MODULE_DIRECTORY:
5222       ModuleDir = std::string(Blob);
5223       break;
5224     case MODULE_MAP_FILE: {
5225       unsigned Idx = 0;
5226       auto Path = ReadString(Record, Idx);
5227       ResolveImportedPath(Path, ModuleDir);
5228       Listener.ReadModuleMapFile(Path);
5229       break;
5230     }
5231     case INPUT_FILE_OFFSETS: {
5232       if (!NeedsInputFiles)
5233         break;
5234 
5235       unsigned NumInputFiles = Record[0];
5236       unsigned NumUserFiles = Record[1];
5237       const llvm::support::unaligned_uint64_t *InputFileOffs =
5238           (const llvm::support::unaligned_uint64_t *)Blob.data();
5239       for (unsigned I = 0; I != NumInputFiles; ++I) {
5240         // Go find this input file.
5241         bool isSystemFile = I >= NumUserFiles;
5242 
5243         if (isSystemFile && !NeedsSystemInputFiles)
5244           break; // the rest are system input files
5245 
5246         BitstreamCursor &Cursor = InputFilesCursor;
5247         SavedStreamPosition SavedPosition(Cursor);
5248         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5249           // FIXME this drops errors on the floor.
5250           consumeError(std::move(Err));
5251         }
5252 
5253         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5254         if (!MaybeCode) {
5255           // FIXME this drops errors on the floor.
5256           consumeError(MaybeCode.takeError());
5257         }
5258         unsigned Code = MaybeCode.get();
5259 
5260         RecordData Record;
5261         StringRef Blob;
5262         bool shouldContinue = false;
5263         Expected<unsigned> MaybeRecordType =
5264             Cursor.readRecord(Code, Record, &Blob);
5265         if (!MaybeRecordType) {
5266           // FIXME this drops errors on the floor.
5267           consumeError(MaybeRecordType.takeError());
5268         }
5269         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5270         case INPUT_FILE_HASH:
5271           break;
5272         case INPUT_FILE:
5273           bool Overridden = static_cast<bool>(Record[3]);
5274           std::string Filename = std::string(Blob);
5275           ResolveImportedPath(Filename, ModuleDir);
5276           shouldContinue = Listener.visitInputFile(
5277               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5278           break;
5279         }
5280         if (!shouldContinue)
5281           break;
5282       }
5283       break;
5284     }
5285 
5286     case IMPORTS: {
5287       if (!NeedsImports)
5288         break;
5289 
5290       unsigned Idx = 0, N = Record.size();
5291       while (Idx < N) {
5292         // Read information about the AST file.
5293         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5294         std::string ModuleName = ReadString(Record, Idx);
5295         std::string Filename = ReadString(Record, Idx);
5296         ResolveImportedPath(Filename, ModuleDir);
5297         Listener.visitImport(ModuleName, Filename);
5298       }
5299       break;
5300     }
5301 
5302     default:
5303       // No other validation to perform.
5304       break;
5305     }
5306   }
5307 
5308   // Look for module file extension blocks, if requested.
5309   if (FindModuleFileExtensions) {
5310     BitstreamCursor SavedStream = Stream;
5311     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5312       bool DoneWithExtensionBlock = false;
5313       while (!DoneWithExtensionBlock) {
5314         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5315         if (!MaybeEntry) {
5316           // FIXME this drops the error.
5317           return true;
5318         }
5319         llvm::BitstreamEntry Entry = MaybeEntry.get();
5320 
5321         switch (Entry.Kind) {
5322         case llvm::BitstreamEntry::SubBlock:
5323           if (llvm::Error Err = Stream.SkipBlock()) {
5324             // FIXME this drops the error on the floor.
5325             consumeError(std::move(Err));
5326             return true;
5327           }
5328           continue;
5329 
5330         case llvm::BitstreamEntry::EndBlock:
5331           DoneWithExtensionBlock = true;
5332           continue;
5333 
5334         case llvm::BitstreamEntry::Error:
5335           return true;
5336 
5337         case llvm::BitstreamEntry::Record:
5338           break;
5339         }
5340 
5341        Record.clear();
5342        StringRef Blob;
5343        Expected<unsigned> MaybeRecCode =
5344            Stream.readRecord(Entry.ID, Record, &Blob);
5345        if (!MaybeRecCode) {
5346          // FIXME this drops the error.
5347          return true;
5348        }
5349        switch (MaybeRecCode.get()) {
5350        case EXTENSION_METADATA: {
5351          ModuleFileExtensionMetadata Metadata;
5352          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5353            return true;
5354 
5355          Listener.readModuleFileExtension(Metadata);
5356          break;
5357        }
5358        }
5359       }
5360     }
5361     Stream = SavedStream;
5362   }
5363 
5364   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5365   if (readUnhashedControlBlockImpl(
5366           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5367           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5368           ValidateDiagnosticOptions) != Success)
5369     return true;
5370 
5371   return false;
5372 }
5373 
5374 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5375                                     const PCHContainerReader &PCHContainerRdr,
5376                                     const LangOptions &LangOpts,
5377                                     const TargetOptions &TargetOpts,
5378                                     const PreprocessorOptions &PPOpts,
5379                                     StringRef ExistingModuleCachePath) {
5380   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5381                                ExistingModuleCachePath, FileMgr);
5382   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5383                                   /*FindModuleFileExtensions=*/false,
5384                                   validator,
5385                                   /*ValidateDiagnosticOptions=*/true);
5386 }
5387 
5388 ASTReader::ASTReadResult
5389 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5390   // Enter the submodule block.
5391   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5392     Error(std::move(Err));
5393     return Failure;
5394   }
5395 
5396   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5397   bool First = true;
5398   Module *CurrentModule = nullptr;
5399   RecordData Record;
5400   while (true) {
5401     Expected<llvm::BitstreamEntry> MaybeEntry =
5402         F.Stream.advanceSkippingSubblocks();
5403     if (!MaybeEntry) {
5404       Error(MaybeEntry.takeError());
5405       return Failure;
5406     }
5407     llvm::BitstreamEntry Entry = MaybeEntry.get();
5408 
5409     switch (Entry.Kind) {
5410     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5411     case llvm::BitstreamEntry::Error:
5412       Error("malformed block record in AST file");
5413       return Failure;
5414     case llvm::BitstreamEntry::EndBlock:
5415       return Success;
5416     case llvm::BitstreamEntry::Record:
5417       // The interesting case.
5418       break;
5419     }
5420 
5421     // Read a record.
5422     StringRef Blob;
5423     Record.clear();
5424     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5425     if (!MaybeKind) {
5426       Error(MaybeKind.takeError());
5427       return Failure;
5428     }
5429     unsigned Kind = MaybeKind.get();
5430 
5431     if ((Kind == SUBMODULE_METADATA) != First) {
5432       Error("submodule metadata record should be at beginning of block");
5433       return Failure;
5434     }
5435     First = false;
5436 
5437     // Submodule information is only valid if we have a current module.
5438     // FIXME: Should we error on these cases?
5439     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5440         Kind != SUBMODULE_DEFINITION)
5441       continue;
5442 
5443     switch (Kind) {
5444     default:  // Default behavior: ignore.
5445       break;
5446 
5447     case SUBMODULE_DEFINITION: {
5448       if (Record.size() < 12) {
5449         Error("malformed module definition");
5450         return Failure;
5451       }
5452 
5453       StringRef Name = Blob;
5454       unsigned Idx = 0;
5455       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5456       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5457       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5458       bool IsFramework = Record[Idx++];
5459       bool IsExplicit = Record[Idx++];
5460       bool IsSystem = Record[Idx++];
5461       bool IsExternC = Record[Idx++];
5462       bool InferSubmodules = Record[Idx++];
5463       bool InferExplicitSubmodules = Record[Idx++];
5464       bool InferExportWildcard = Record[Idx++];
5465       bool ConfigMacrosExhaustive = Record[Idx++];
5466       bool ModuleMapIsPrivate = Record[Idx++];
5467 
5468       Module *ParentModule = nullptr;
5469       if (Parent)
5470         ParentModule = getSubmodule(Parent);
5471 
5472       // Retrieve this (sub)module from the module map, creating it if
5473       // necessary.
5474       CurrentModule =
5475           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5476               .first;
5477 
5478       // FIXME: set the definition loc for CurrentModule, or call
5479       // ModMap.setInferredModuleAllowedBy()
5480 
5481       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5482       if (GlobalIndex >= SubmodulesLoaded.size() ||
5483           SubmodulesLoaded[GlobalIndex]) {
5484         Error("too many submodules");
5485         return Failure;
5486       }
5487 
5488       if (!ParentModule) {
5489         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5490           // Don't emit module relocation error if we have -fno-validate-pch
5491           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5492               CurFile != F.File) {
5493             Error(diag::err_module_file_conflict,
5494                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5495                   F.File->getName());
5496             return Failure;
5497           }
5498         }
5499 
5500         F.DidReadTopLevelSubmodule = true;
5501         CurrentModule->setASTFile(F.File);
5502         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5503       }
5504 
5505       CurrentModule->Kind = Kind;
5506       CurrentModule->Signature = F.Signature;
5507       CurrentModule->IsFromModuleFile = true;
5508       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5509       CurrentModule->IsExternC = IsExternC;
5510       CurrentModule->InferSubmodules = InferSubmodules;
5511       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5512       CurrentModule->InferExportWildcard = InferExportWildcard;
5513       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5514       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5515       if (DeserializationListener)
5516         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5517 
5518       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5519 
5520       // Clear out data that will be replaced by what is in the module file.
5521       CurrentModule->LinkLibraries.clear();
5522       CurrentModule->ConfigMacros.clear();
5523       CurrentModule->UnresolvedConflicts.clear();
5524       CurrentModule->Conflicts.clear();
5525 
5526       // The module is available unless it's missing a requirement; relevant
5527       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5528       // Missing headers that were present when the module was built do not
5529       // make it unavailable -- if we got this far, this must be an explicitly
5530       // imported module file.
5531       CurrentModule->Requirements.clear();
5532       CurrentModule->MissingHeaders.clear();
5533       CurrentModule->IsMissingRequirement =
5534           ParentModule && ParentModule->IsMissingRequirement;
5535       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5536       break;
5537     }
5538 
5539     case SUBMODULE_UMBRELLA_HEADER: {
5540       std::string Filename = std::string(Blob);
5541       ResolveImportedPath(F, Filename);
5542       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5543         if (!CurrentModule->getUmbrellaHeader())
5544           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5545         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5546           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5547             Error("mismatched umbrella headers in submodule");
5548           return OutOfDate;
5549         }
5550       }
5551       break;
5552     }
5553 
5554     case SUBMODULE_HEADER:
5555     case SUBMODULE_EXCLUDED_HEADER:
5556     case SUBMODULE_PRIVATE_HEADER:
5557       // We lazily associate headers with their modules via the HeaderInfo table.
5558       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5559       // of complete filenames or remove it entirely.
5560       break;
5561 
5562     case SUBMODULE_TEXTUAL_HEADER:
5563     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5564       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5565       // them here.
5566       break;
5567 
5568     case SUBMODULE_TOPHEADER:
5569       CurrentModule->addTopHeaderFilename(Blob);
5570       break;
5571 
5572     case SUBMODULE_UMBRELLA_DIR: {
5573       std::string Dirname = std::string(Blob);
5574       ResolveImportedPath(F, Dirname);
5575       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5576         if (!CurrentModule->getUmbrellaDir())
5577           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5578         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5579           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5580             Error("mismatched umbrella directories in submodule");
5581           return OutOfDate;
5582         }
5583       }
5584       break;
5585     }
5586 
5587     case SUBMODULE_METADATA: {
5588       F.BaseSubmoduleID = getTotalNumSubmodules();
5589       F.LocalNumSubmodules = Record[0];
5590       unsigned LocalBaseSubmoduleID = Record[1];
5591       if (F.LocalNumSubmodules > 0) {
5592         // Introduce the global -> local mapping for submodules within this
5593         // module.
5594         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5595 
5596         // Introduce the local -> global mapping for submodules within this
5597         // module.
5598         F.SubmoduleRemap.insertOrReplace(
5599           std::make_pair(LocalBaseSubmoduleID,
5600                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5601 
5602         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5603       }
5604       break;
5605     }
5606 
5607     case SUBMODULE_IMPORTS:
5608       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5609         UnresolvedModuleRef Unresolved;
5610         Unresolved.File = &F;
5611         Unresolved.Mod = CurrentModule;
5612         Unresolved.ID = Record[Idx];
5613         Unresolved.Kind = UnresolvedModuleRef::Import;
5614         Unresolved.IsWildcard = false;
5615         UnresolvedModuleRefs.push_back(Unresolved);
5616       }
5617       break;
5618 
5619     case SUBMODULE_EXPORTS:
5620       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5621         UnresolvedModuleRef Unresolved;
5622         Unresolved.File = &F;
5623         Unresolved.Mod = CurrentModule;
5624         Unresolved.ID = Record[Idx];
5625         Unresolved.Kind = UnresolvedModuleRef::Export;
5626         Unresolved.IsWildcard = Record[Idx + 1];
5627         UnresolvedModuleRefs.push_back(Unresolved);
5628       }
5629 
5630       // Once we've loaded the set of exports, there's no reason to keep
5631       // the parsed, unresolved exports around.
5632       CurrentModule->UnresolvedExports.clear();
5633       break;
5634 
5635     case SUBMODULE_REQUIRES:
5636       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5637                                     PP.getTargetInfo());
5638       break;
5639 
5640     case SUBMODULE_LINK_LIBRARY:
5641       ModMap.resolveLinkAsDependencies(CurrentModule);
5642       CurrentModule->LinkLibraries.push_back(
5643           Module::LinkLibrary(std::string(Blob), Record[0]));
5644       break;
5645 
5646     case SUBMODULE_CONFIG_MACRO:
5647       CurrentModule->ConfigMacros.push_back(Blob.str());
5648       break;
5649 
5650     case SUBMODULE_CONFLICT: {
5651       UnresolvedModuleRef Unresolved;
5652       Unresolved.File = &F;
5653       Unresolved.Mod = CurrentModule;
5654       Unresolved.ID = Record[0];
5655       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5656       Unresolved.IsWildcard = false;
5657       Unresolved.String = Blob;
5658       UnresolvedModuleRefs.push_back(Unresolved);
5659       break;
5660     }
5661 
5662     case SUBMODULE_INITIALIZERS: {
5663       if (!ContextObj)
5664         break;
5665       SmallVector<uint32_t, 16> Inits;
5666       for (auto &ID : Record)
5667         Inits.push_back(getGlobalDeclID(F, ID));
5668       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5669       break;
5670     }
5671 
5672     case SUBMODULE_EXPORT_AS:
5673       CurrentModule->ExportAsModule = Blob.str();
5674       ModMap.addLinkAsDependency(CurrentModule);
5675       break;
5676     }
5677   }
5678 }
5679 
5680 /// Parse the record that corresponds to a LangOptions data
5681 /// structure.
5682 ///
5683 /// This routine parses the language options from the AST file and then gives
5684 /// them to the AST listener if one is set.
5685 ///
5686 /// \returns true if the listener deems the file unacceptable, false otherwise.
5687 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5688                                      bool Complain,
5689                                      ASTReaderListener &Listener,
5690                                      bool AllowCompatibleDifferences) {
5691   LangOptions LangOpts;
5692   unsigned Idx = 0;
5693 #define LANGOPT(Name, Bits, Default, Description) \
5694   LangOpts.Name = Record[Idx++];
5695 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5696   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5697 #include "clang/Basic/LangOptions.def"
5698 #define SANITIZER(NAME, ID)                                                    \
5699   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5700 #include "clang/Basic/Sanitizers.def"
5701 
5702   for (unsigned N = Record[Idx++]; N; --N)
5703     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5704 
5705   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5706   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5707   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5708 
5709   LangOpts.CurrentModule = ReadString(Record, Idx);
5710 
5711   // Comment options.
5712   for (unsigned N = Record[Idx++]; N; --N) {
5713     LangOpts.CommentOpts.BlockCommandNames.push_back(
5714       ReadString(Record, Idx));
5715   }
5716   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5717 
5718   // OpenMP offloading options.
5719   for (unsigned N = Record[Idx++]; N; --N) {
5720     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5721   }
5722 
5723   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5724 
5725   return Listener.ReadLanguageOptions(LangOpts, Complain,
5726                                       AllowCompatibleDifferences);
5727 }
5728 
5729 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5730                                    ASTReaderListener &Listener,
5731                                    bool AllowCompatibleDifferences) {
5732   unsigned Idx = 0;
5733   TargetOptions TargetOpts;
5734   TargetOpts.Triple = ReadString(Record, Idx);
5735   TargetOpts.CPU = ReadString(Record, Idx);
5736   TargetOpts.ABI = ReadString(Record, Idx);
5737   for (unsigned N = Record[Idx++]; N; --N) {
5738     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5739   }
5740   for (unsigned N = Record[Idx++]; N; --N) {
5741     TargetOpts.Features.push_back(ReadString(Record, Idx));
5742   }
5743 
5744   return Listener.ReadTargetOptions(TargetOpts, Complain,
5745                                     AllowCompatibleDifferences);
5746 }
5747 
5748 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5749                                        ASTReaderListener &Listener) {
5750   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5751   unsigned Idx = 0;
5752 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5753 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5754   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5755 #include "clang/Basic/DiagnosticOptions.def"
5756 
5757   for (unsigned N = Record[Idx++]; N; --N)
5758     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5759   for (unsigned N = Record[Idx++]; N; --N)
5760     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5761 
5762   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5763 }
5764 
5765 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5766                                        ASTReaderListener &Listener) {
5767   FileSystemOptions FSOpts;
5768   unsigned Idx = 0;
5769   FSOpts.WorkingDir = ReadString(Record, Idx);
5770   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5771 }
5772 
5773 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5774                                          bool Complain,
5775                                          ASTReaderListener &Listener) {
5776   HeaderSearchOptions HSOpts;
5777   unsigned Idx = 0;
5778   HSOpts.Sysroot = ReadString(Record, Idx);
5779 
5780   // Include entries.
5781   for (unsigned N = Record[Idx++]; N; --N) {
5782     std::string Path = ReadString(Record, Idx);
5783     frontend::IncludeDirGroup Group
5784       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5785     bool IsFramework = Record[Idx++];
5786     bool IgnoreSysRoot = Record[Idx++];
5787     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5788                                     IgnoreSysRoot);
5789   }
5790 
5791   // System header prefixes.
5792   for (unsigned N = Record[Idx++]; N; --N) {
5793     std::string Prefix = ReadString(Record, Idx);
5794     bool IsSystemHeader = Record[Idx++];
5795     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5796   }
5797 
5798   HSOpts.ResourceDir = ReadString(Record, Idx);
5799   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5800   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5801   HSOpts.DisableModuleHash = Record[Idx++];
5802   HSOpts.ImplicitModuleMaps = Record[Idx++];
5803   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5804   HSOpts.UseBuiltinIncludes = Record[Idx++];
5805   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5806   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5807   HSOpts.UseLibcxx = Record[Idx++];
5808   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5809 
5810   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5811                                           Complain);
5812 }
5813 
5814 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5815                                          bool Complain,
5816                                          ASTReaderListener &Listener,
5817                                          std::string &SuggestedPredefines) {
5818   PreprocessorOptions PPOpts;
5819   unsigned Idx = 0;
5820 
5821   // Macro definitions/undefs
5822   for (unsigned N = Record[Idx++]; N; --N) {
5823     std::string Macro = ReadString(Record, Idx);
5824     bool IsUndef = Record[Idx++];
5825     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5826   }
5827 
5828   // Includes
5829   for (unsigned N = Record[Idx++]; N; --N) {
5830     PPOpts.Includes.push_back(ReadString(Record, Idx));
5831   }
5832 
5833   // Macro Includes
5834   for (unsigned N = Record[Idx++]; N; --N) {
5835     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5836   }
5837 
5838   PPOpts.UsePredefines = Record[Idx++];
5839   PPOpts.DetailedRecord = Record[Idx++];
5840   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5841   PPOpts.ObjCXXARCStandardLibrary =
5842     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5843   SuggestedPredefines.clear();
5844   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5845                                           SuggestedPredefines);
5846 }
5847 
5848 std::pair<ModuleFile *, unsigned>
5849 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5850   GlobalPreprocessedEntityMapType::iterator
5851   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5852   assert(I != GlobalPreprocessedEntityMap.end() &&
5853          "Corrupted global preprocessed entity map");
5854   ModuleFile *M = I->second;
5855   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5856   return std::make_pair(M, LocalIndex);
5857 }
5858 
5859 llvm::iterator_range<PreprocessingRecord::iterator>
5860 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5861   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5862     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5863                                              Mod.NumPreprocessedEntities);
5864 
5865   return llvm::make_range(PreprocessingRecord::iterator(),
5866                           PreprocessingRecord::iterator());
5867 }
5868 
5869 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5870 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5871   return llvm::make_range(
5872       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5873       ModuleDeclIterator(this, &Mod,
5874                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5875 }
5876 
5877 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5878   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5879   assert(I != GlobalSkippedRangeMap.end() &&
5880     "Corrupted global skipped range map");
5881   ModuleFile *M = I->second;
5882   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5883   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5884   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5885   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5886                     TranslateSourceLocation(*M, RawRange.getEnd()));
5887   assert(Range.isValid());
5888   return Range;
5889 }
5890 
5891 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5892   PreprocessedEntityID PPID = Index+1;
5893   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5894   ModuleFile &M = *PPInfo.first;
5895   unsigned LocalIndex = PPInfo.second;
5896   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5897 
5898   if (!PP.getPreprocessingRecord()) {
5899     Error("no preprocessing record");
5900     return nullptr;
5901   }
5902 
5903   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5904   if (llvm::Error Err =
5905           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5906     Error(std::move(Err));
5907     return nullptr;
5908   }
5909 
5910   Expected<llvm::BitstreamEntry> MaybeEntry =
5911       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5912   if (!MaybeEntry) {
5913     Error(MaybeEntry.takeError());
5914     return nullptr;
5915   }
5916   llvm::BitstreamEntry Entry = MaybeEntry.get();
5917 
5918   if (Entry.Kind != llvm::BitstreamEntry::Record)
5919     return nullptr;
5920 
5921   // Read the record.
5922   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5923                     TranslateSourceLocation(M, PPOffs.getEnd()));
5924   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5925   StringRef Blob;
5926   RecordData Record;
5927   Expected<unsigned> MaybeRecType =
5928       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5929   if (!MaybeRecType) {
5930     Error(MaybeRecType.takeError());
5931     return nullptr;
5932   }
5933   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5934   case PPD_MACRO_EXPANSION: {
5935     bool isBuiltin = Record[0];
5936     IdentifierInfo *Name = nullptr;
5937     MacroDefinitionRecord *Def = nullptr;
5938     if (isBuiltin)
5939       Name = getLocalIdentifier(M, Record[1]);
5940     else {
5941       PreprocessedEntityID GlobalID =
5942           getGlobalPreprocessedEntityID(M, Record[1]);
5943       Def = cast<MacroDefinitionRecord>(
5944           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5945     }
5946 
5947     MacroExpansion *ME;
5948     if (isBuiltin)
5949       ME = new (PPRec) MacroExpansion(Name, Range);
5950     else
5951       ME = new (PPRec) MacroExpansion(Def, Range);
5952 
5953     return ME;
5954   }
5955 
5956   case PPD_MACRO_DEFINITION: {
5957     // Decode the identifier info and then check again; if the macro is
5958     // still defined and associated with the identifier,
5959     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5960     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5961 
5962     if (DeserializationListener)
5963       DeserializationListener->MacroDefinitionRead(PPID, MD);
5964 
5965     return MD;
5966   }
5967 
5968   case PPD_INCLUSION_DIRECTIVE: {
5969     const char *FullFileNameStart = Blob.data() + Record[0];
5970     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5971     const FileEntry *File = nullptr;
5972     if (!FullFileName.empty())
5973       if (auto FE = PP.getFileManager().getFile(FullFileName))
5974         File = *FE;
5975 
5976     // FIXME: Stable encoding
5977     InclusionDirective::InclusionKind Kind
5978       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5979     InclusionDirective *ID
5980       = new (PPRec) InclusionDirective(PPRec, Kind,
5981                                        StringRef(Blob.data(), Record[0]),
5982                                        Record[1], Record[3],
5983                                        File,
5984                                        Range);
5985     return ID;
5986   }
5987   }
5988 
5989   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5990 }
5991 
5992 /// Find the next module that contains entities and return the ID
5993 /// of the first entry.
5994 ///
5995 /// \param SLocMapI points at a chunk of a module that contains no
5996 /// preprocessed entities or the entities it contains are not the ones we are
5997 /// looking for.
5998 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5999                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6000   ++SLocMapI;
6001   for (GlobalSLocOffsetMapType::const_iterator
6002          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6003     ModuleFile &M = *SLocMapI->second;
6004     if (M.NumPreprocessedEntities)
6005       return M.BasePreprocessedEntityID;
6006   }
6007 
6008   return getTotalNumPreprocessedEntities();
6009 }
6010 
6011 namespace {
6012 
6013 struct PPEntityComp {
6014   const ASTReader &Reader;
6015   ModuleFile &M;
6016 
6017   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6018 
6019   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6020     SourceLocation LHS = getLoc(L);
6021     SourceLocation RHS = getLoc(R);
6022     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6023   }
6024 
6025   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6026     SourceLocation LHS = getLoc(L);
6027     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6028   }
6029 
6030   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6031     SourceLocation RHS = getLoc(R);
6032     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6033   }
6034 
6035   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6036     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6037   }
6038 };
6039 
6040 } // namespace
6041 
6042 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6043                                                        bool EndsAfter) const {
6044   if (SourceMgr.isLocalSourceLocation(Loc))
6045     return getTotalNumPreprocessedEntities();
6046 
6047   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6048       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6049   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6050          "Corrupted global sloc offset map");
6051 
6052   if (SLocMapI->second->NumPreprocessedEntities == 0)
6053     return findNextPreprocessedEntity(SLocMapI);
6054 
6055   ModuleFile &M = *SLocMapI->second;
6056 
6057   using pp_iterator = const PPEntityOffset *;
6058 
6059   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6060   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6061 
6062   size_t Count = M.NumPreprocessedEntities;
6063   size_t Half;
6064   pp_iterator First = pp_begin;
6065   pp_iterator PPI;
6066 
6067   if (EndsAfter) {
6068     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6069                            PPEntityComp(*this, M));
6070   } else {
6071     // Do a binary search manually instead of using std::lower_bound because
6072     // The end locations of entities may be unordered (when a macro expansion
6073     // is inside another macro argument), but for this case it is not important
6074     // whether we get the first macro expansion or its containing macro.
6075     while (Count > 0) {
6076       Half = Count / 2;
6077       PPI = First;
6078       std::advance(PPI, Half);
6079       if (SourceMgr.isBeforeInTranslationUnit(
6080               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6081         First = PPI;
6082         ++First;
6083         Count = Count - Half - 1;
6084       } else
6085         Count = Half;
6086     }
6087   }
6088 
6089   if (PPI == pp_end)
6090     return findNextPreprocessedEntity(SLocMapI);
6091 
6092   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6093 }
6094 
6095 /// Returns a pair of [Begin, End) indices of preallocated
6096 /// preprocessed entities that \arg Range encompasses.
6097 std::pair<unsigned, unsigned>
6098     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6099   if (Range.isInvalid())
6100     return std::make_pair(0,0);
6101   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6102 
6103   PreprocessedEntityID BeginID =
6104       findPreprocessedEntity(Range.getBegin(), false);
6105   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6106   return std::make_pair(BeginID, EndID);
6107 }
6108 
6109 /// Optionally returns true or false if the preallocated preprocessed
6110 /// entity with index \arg Index came from file \arg FID.
6111 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6112                                                              FileID FID) {
6113   if (FID.isInvalid())
6114     return false;
6115 
6116   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6117   ModuleFile &M = *PPInfo.first;
6118   unsigned LocalIndex = PPInfo.second;
6119   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6120 
6121   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6122   if (Loc.isInvalid())
6123     return false;
6124 
6125   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6126     return true;
6127   else
6128     return false;
6129 }
6130 
6131 namespace {
6132 
6133   /// Visitor used to search for information about a header file.
6134   class HeaderFileInfoVisitor {
6135     const FileEntry *FE;
6136     Optional<HeaderFileInfo> HFI;
6137 
6138   public:
6139     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6140 
6141     bool operator()(ModuleFile &M) {
6142       HeaderFileInfoLookupTable *Table
6143         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6144       if (!Table)
6145         return false;
6146 
6147       // Look in the on-disk hash table for an entry for this file name.
6148       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6149       if (Pos == Table->end())
6150         return false;
6151 
6152       HFI = *Pos;
6153       return true;
6154     }
6155 
6156     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6157   };
6158 
6159 } // namespace
6160 
6161 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6162   HeaderFileInfoVisitor Visitor(FE);
6163   ModuleMgr.visit(Visitor);
6164   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6165     return *HFI;
6166 
6167   return HeaderFileInfo();
6168 }
6169 
6170 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6171   using DiagState = DiagnosticsEngine::DiagState;
6172   SmallVector<DiagState *, 32> DiagStates;
6173 
6174   for (ModuleFile &F : ModuleMgr) {
6175     unsigned Idx = 0;
6176     auto &Record = F.PragmaDiagMappings;
6177     if (Record.empty())
6178       continue;
6179 
6180     DiagStates.clear();
6181 
6182     auto ReadDiagState =
6183         [&](const DiagState &BasedOn, SourceLocation Loc,
6184             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6185       unsigned BackrefID = Record[Idx++];
6186       if (BackrefID != 0)
6187         return DiagStates[BackrefID - 1];
6188 
6189       // A new DiagState was created here.
6190       Diag.DiagStates.push_back(BasedOn);
6191       DiagState *NewState = &Diag.DiagStates.back();
6192       DiagStates.push_back(NewState);
6193       unsigned Size = Record[Idx++];
6194       assert(Idx + Size * 2 <= Record.size() &&
6195              "Invalid data, not enough diag/map pairs");
6196       while (Size--) {
6197         unsigned DiagID = Record[Idx++];
6198         DiagnosticMapping NewMapping =
6199             DiagnosticMapping::deserialize(Record[Idx++]);
6200         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6201           continue;
6202 
6203         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6204 
6205         // If this mapping was specified as a warning but the severity was
6206         // upgraded due to diagnostic settings, simulate the current diagnostic
6207         // settings (and use a warning).
6208         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6209           NewMapping.setSeverity(diag::Severity::Warning);
6210           NewMapping.setUpgradedFromWarning(false);
6211         }
6212 
6213         Mapping = NewMapping;
6214       }
6215       return NewState;
6216     };
6217 
6218     // Read the first state.
6219     DiagState *FirstState;
6220     if (F.Kind == MK_ImplicitModule) {
6221       // Implicitly-built modules are reused with different diagnostic
6222       // settings.  Use the initial diagnostic state from Diag to simulate this
6223       // compilation's diagnostic settings.
6224       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6225       DiagStates.push_back(FirstState);
6226 
6227       // Skip the initial diagnostic state from the serialized module.
6228       assert(Record[1] == 0 &&
6229              "Invalid data, unexpected backref in initial state");
6230       Idx = 3 + Record[2] * 2;
6231       assert(Idx < Record.size() &&
6232              "Invalid data, not enough state change pairs in initial state");
6233     } else if (F.isModule()) {
6234       // For an explicit module, preserve the flags from the module build
6235       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6236       // -Wblah flags.
6237       unsigned Flags = Record[Idx++];
6238       DiagState Initial;
6239       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6240       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6241       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6242       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6243       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6244       Initial.ExtBehavior = (diag::Severity)Flags;
6245       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6246 
6247       assert(F.OriginalSourceFileID.isValid());
6248 
6249       // Set up the root buffer of the module to start with the initial
6250       // diagnostic state of the module itself, to cover files that contain no
6251       // explicit transitions (for which we did not serialize anything).
6252       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6253           .StateTransitions.push_back({FirstState, 0});
6254     } else {
6255       // For prefix ASTs, start with whatever the user configured on the
6256       // command line.
6257       Idx++; // Skip flags.
6258       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6259                                  SourceLocation(), false);
6260     }
6261 
6262     // Read the state transitions.
6263     unsigned NumLocations = Record[Idx++];
6264     while (NumLocations--) {
6265       assert(Idx < Record.size() &&
6266              "Invalid data, missing pragma diagnostic states");
6267       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6268       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6269       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6270       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6271       unsigned Transitions = Record[Idx++];
6272 
6273       // Note that we don't need to set up Parent/ParentOffset here, because
6274       // we won't be changing the diagnostic state within imported FileIDs
6275       // (other than perhaps appending to the main source file, which has no
6276       // parent).
6277       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6278       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6279       for (unsigned I = 0; I != Transitions; ++I) {
6280         unsigned Offset = Record[Idx++];
6281         auto *State =
6282             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6283         F.StateTransitions.push_back({State, Offset});
6284       }
6285     }
6286 
6287     // Read the final state.
6288     assert(Idx < Record.size() &&
6289            "Invalid data, missing final pragma diagnostic state");
6290     SourceLocation CurStateLoc =
6291         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6292     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6293 
6294     if (!F.isModule()) {
6295       Diag.DiagStatesByLoc.CurDiagState = CurState;
6296       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6297 
6298       // Preserve the property that the imaginary root file describes the
6299       // current state.
6300       FileID NullFile;
6301       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6302       if (T.empty())
6303         T.push_back({CurState, 0});
6304       else
6305         T[0].State = CurState;
6306     }
6307 
6308     // Don't try to read these mappings again.
6309     Record.clear();
6310   }
6311 }
6312 
6313 /// Get the correct cursor and offset for loading a type.
6314 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6315   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6316   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6317   ModuleFile *M = I->second;
6318   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6319 }
6320 
6321 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6322   switch (code) {
6323 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6324   case TYPE_##CODE_ID: return Type::CLASS_ID;
6325 #include "clang/Serialization/TypeBitCodes.def"
6326   default: return llvm::None;
6327   }
6328 }
6329 
6330 /// Read and return the type with the given index..
6331 ///
6332 /// The index is the type ID, shifted and minus the number of predefs. This
6333 /// routine actually reads the record corresponding to the type at the given
6334 /// location. It is a helper routine for GetType, which deals with reading type
6335 /// IDs.
6336 QualType ASTReader::readTypeRecord(unsigned Index) {
6337   assert(ContextObj && "reading type with no AST context");
6338   ASTContext &Context = *ContextObj;
6339   RecordLocation Loc = TypeCursorForIndex(Index);
6340   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6341 
6342   // Keep track of where we are in the stream, then jump back there
6343   // after reading this type.
6344   SavedStreamPosition SavedPosition(DeclsCursor);
6345 
6346   ReadingKindTracker ReadingKind(Read_Type, *this);
6347 
6348   // Note that we are loading a type record.
6349   Deserializing AType(this);
6350 
6351   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6352     Error(std::move(Err));
6353     return QualType();
6354   }
6355   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6356   if (!RawCode) {
6357     Error(RawCode.takeError());
6358     return QualType();
6359   }
6360 
6361   ASTRecordReader Record(*this, *Loc.F);
6362   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6363   if (!Code) {
6364     Error(Code.takeError());
6365     return QualType();
6366   }
6367   if (Code.get() == TYPE_EXT_QUAL) {
6368     QualType baseType = Record.readQualType();
6369     Qualifiers quals = Record.readQualifiers();
6370     return Context.getQualifiedType(baseType, quals);
6371   }
6372 
6373   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6374   if (!maybeClass) {
6375     Error("Unexpected code for type");
6376     return QualType();
6377   }
6378 
6379   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6380   return TypeReader.read(*maybeClass);
6381 }
6382 
6383 namespace clang {
6384 
6385 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6386   ASTRecordReader &Reader;
6387 
6388   SourceLocation readSourceLocation() {
6389     return Reader.readSourceLocation();
6390   }
6391 
6392   TypeSourceInfo *GetTypeSourceInfo() {
6393     return Reader.readTypeSourceInfo();
6394   }
6395 
6396   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6397     return Reader.readNestedNameSpecifierLoc();
6398   }
6399 
6400   Attr *ReadAttr() {
6401     return Reader.readAttr();
6402   }
6403 
6404 public:
6405   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6406 
6407   // We want compile-time assurance that we've enumerated all of
6408   // these, so unfortunately we have to declare them first, then
6409   // define them out-of-line.
6410 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6411 #define TYPELOC(CLASS, PARENT) \
6412   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6413 #include "clang/AST/TypeLocNodes.def"
6414 
6415   void VisitFunctionTypeLoc(FunctionTypeLoc);
6416   void VisitArrayTypeLoc(ArrayTypeLoc);
6417 };
6418 
6419 } // namespace clang
6420 
6421 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6422   // nothing to do
6423 }
6424 
6425 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6426   TL.setBuiltinLoc(readSourceLocation());
6427   if (TL.needsExtraLocalData()) {
6428     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6429     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6430     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6431     TL.setModeAttr(Reader.readInt());
6432   }
6433 }
6434 
6435 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6436   TL.setNameLoc(readSourceLocation());
6437 }
6438 
6439 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6440   TL.setStarLoc(readSourceLocation());
6441 }
6442 
6443 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6444   // nothing to do
6445 }
6446 
6447 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6448   // nothing to do
6449 }
6450 
6451 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6452   TL.setExpansionLoc(readSourceLocation());
6453 }
6454 
6455 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6456   TL.setCaretLoc(readSourceLocation());
6457 }
6458 
6459 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6460   TL.setAmpLoc(readSourceLocation());
6461 }
6462 
6463 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6464   TL.setAmpAmpLoc(readSourceLocation());
6465 }
6466 
6467 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6468   TL.setStarLoc(readSourceLocation());
6469   TL.setClassTInfo(GetTypeSourceInfo());
6470 }
6471 
6472 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6473   TL.setLBracketLoc(readSourceLocation());
6474   TL.setRBracketLoc(readSourceLocation());
6475   if (Reader.readBool())
6476     TL.setSizeExpr(Reader.readExpr());
6477   else
6478     TL.setSizeExpr(nullptr);
6479 }
6480 
6481 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6482   VisitArrayTypeLoc(TL);
6483 }
6484 
6485 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6486   VisitArrayTypeLoc(TL);
6487 }
6488 
6489 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6490   VisitArrayTypeLoc(TL);
6491 }
6492 
6493 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6494                                             DependentSizedArrayTypeLoc TL) {
6495   VisitArrayTypeLoc(TL);
6496 }
6497 
6498 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6499     DependentAddressSpaceTypeLoc TL) {
6500 
6501     TL.setAttrNameLoc(readSourceLocation());
6502     TL.setAttrOperandParensRange(Reader.readSourceRange());
6503     TL.setAttrExprOperand(Reader.readExpr());
6504 }
6505 
6506 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6507                                         DependentSizedExtVectorTypeLoc TL) {
6508   TL.setNameLoc(readSourceLocation());
6509 }
6510 
6511 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6512   TL.setNameLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitDependentVectorTypeLoc(
6516     DependentVectorTypeLoc TL) {
6517   TL.setNameLoc(readSourceLocation());
6518 }
6519 
6520 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6521   TL.setNameLoc(readSourceLocation());
6522 }
6523 
6524 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6525   TL.setLocalRangeBegin(readSourceLocation());
6526   TL.setLParenLoc(readSourceLocation());
6527   TL.setRParenLoc(readSourceLocation());
6528   TL.setExceptionSpecRange(Reader.readSourceRange());
6529   TL.setLocalRangeEnd(readSourceLocation());
6530   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6531     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6532   }
6533 }
6534 
6535 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6536   VisitFunctionTypeLoc(TL);
6537 }
6538 
6539 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6540   VisitFunctionTypeLoc(TL);
6541 }
6542 
6543 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6544   TL.setNameLoc(readSourceLocation());
6545 }
6546 
6547 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6548   TL.setNameLoc(readSourceLocation());
6549 }
6550 
6551 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6552   TL.setTypeofLoc(readSourceLocation());
6553   TL.setLParenLoc(readSourceLocation());
6554   TL.setRParenLoc(readSourceLocation());
6555 }
6556 
6557 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6558   TL.setTypeofLoc(readSourceLocation());
6559   TL.setLParenLoc(readSourceLocation());
6560   TL.setRParenLoc(readSourceLocation());
6561   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6562 }
6563 
6564 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6569   TL.setKWLoc(readSourceLocation());
6570   TL.setLParenLoc(readSourceLocation());
6571   TL.setRParenLoc(readSourceLocation());
6572   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6573 }
6574 
6575 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6576   TL.setNameLoc(readSourceLocation());
6577   if (Reader.readBool()) {
6578     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6579     TL.setTemplateKWLoc(readSourceLocation());
6580     TL.setConceptNameLoc(readSourceLocation());
6581     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6582     TL.setLAngleLoc(readSourceLocation());
6583     TL.setRAngleLoc(readSourceLocation());
6584     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6585       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6586                               TL.getTypePtr()->getArg(i).getKind()));
6587   }
6588 }
6589 
6590 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6591     DeducedTemplateSpecializationTypeLoc TL) {
6592   TL.setTemplateNameLoc(readSourceLocation());
6593 }
6594 
6595 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6596   TL.setNameLoc(readSourceLocation());
6597 }
6598 
6599 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6600   TL.setNameLoc(readSourceLocation());
6601 }
6602 
6603 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6604   TL.setAttr(ReadAttr());
6605 }
6606 
6607 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6608   TL.setNameLoc(readSourceLocation());
6609 }
6610 
6611 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6612                                             SubstTemplateTypeParmTypeLoc TL) {
6613   TL.setNameLoc(readSourceLocation());
6614 }
6615 
6616 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6617                                           SubstTemplateTypeParmPackTypeLoc TL) {
6618   TL.setNameLoc(readSourceLocation());
6619 }
6620 
6621 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6622                                            TemplateSpecializationTypeLoc TL) {
6623   TL.setTemplateKeywordLoc(readSourceLocation());
6624   TL.setTemplateNameLoc(readSourceLocation());
6625   TL.setLAngleLoc(readSourceLocation());
6626   TL.setRAngleLoc(readSourceLocation());
6627   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6628     TL.setArgLocInfo(
6629         i,
6630         Reader.readTemplateArgumentLocInfo(
6631           TL.getTypePtr()->getArg(i).getKind()));
6632 }
6633 
6634 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6635   TL.setLParenLoc(readSourceLocation());
6636   TL.setRParenLoc(readSourceLocation());
6637 }
6638 
6639 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6640   TL.setElaboratedKeywordLoc(readSourceLocation());
6641   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6642 }
6643 
6644 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6645   TL.setNameLoc(readSourceLocation());
6646 }
6647 
6648 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6649   TL.setElaboratedKeywordLoc(readSourceLocation());
6650   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6651   TL.setNameLoc(readSourceLocation());
6652 }
6653 
6654 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6655        DependentTemplateSpecializationTypeLoc TL) {
6656   TL.setElaboratedKeywordLoc(readSourceLocation());
6657   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6658   TL.setTemplateKeywordLoc(readSourceLocation());
6659   TL.setTemplateNameLoc(readSourceLocation());
6660   TL.setLAngleLoc(readSourceLocation());
6661   TL.setRAngleLoc(readSourceLocation());
6662   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6663     TL.setArgLocInfo(
6664         I,
6665         Reader.readTemplateArgumentLocInfo(
6666             TL.getTypePtr()->getArg(I).getKind()));
6667 }
6668 
6669 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6670   TL.setEllipsisLoc(readSourceLocation());
6671 }
6672 
6673 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6674   TL.setNameLoc(readSourceLocation());
6675 }
6676 
6677 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6678   if (TL.getNumProtocols()) {
6679     TL.setProtocolLAngleLoc(readSourceLocation());
6680     TL.setProtocolRAngleLoc(readSourceLocation());
6681   }
6682   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6683     TL.setProtocolLoc(i, readSourceLocation());
6684 }
6685 
6686 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6687   TL.setHasBaseTypeAsWritten(Reader.readBool());
6688   TL.setTypeArgsLAngleLoc(readSourceLocation());
6689   TL.setTypeArgsRAngleLoc(readSourceLocation());
6690   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6691     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6692   TL.setProtocolLAngleLoc(readSourceLocation());
6693   TL.setProtocolRAngleLoc(readSourceLocation());
6694   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6695     TL.setProtocolLoc(i, readSourceLocation());
6696 }
6697 
6698 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6699   TL.setStarLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6703   TL.setKWLoc(readSourceLocation());
6704   TL.setLParenLoc(readSourceLocation());
6705   TL.setRParenLoc(readSourceLocation());
6706 }
6707 
6708 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6709   TL.setKWLoc(readSourceLocation());
6710 }
6711 
6712 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6713   TypeLocReader TLR(*this);
6714   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6715     TLR.Visit(TL);
6716 }
6717 
6718 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6719   QualType InfoTy = readType();
6720   if (InfoTy.isNull())
6721     return nullptr;
6722 
6723   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6724   readTypeLoc(TInfo->getTypeLoc());
6725   return TInfo;
6726 }
6727 
6728 QualType ASTReader::GetType(TypeID ID) {
6729   assert(ContextObj && "reading type with no AST context");
6730   ASTContext &Context = *ContextObj;
6731 
6732   unsigned FastQuals = ID & Qualifiers::FastMask;
6733   unsigned Index = ID >> Qualifiers::FastWidth;
6734 
6735   if (Index < NUM_PREDEF_TYPE_IDS) {
6736     QualType T;
6737     switch ((PredefinedTypeIDs)Index) {
6738     case PREDEF_TYPE_NULL_ID:
6739       return QualType();
6740     case PREDEF_TYPE_VOID_ID:
6741       T = Context.VoidTy;
6742       break;
6743     case PREDEF_TYPE_BOOL_ID:
6744       T = Context.BoolTy;
6745       break;
6746     case PREDEF_TYPE_CHAR_U_ID:
6747     case PREDEF_TYPE_CHAR_S_ID:
6748       // FIXME: Check that the signedness of CharTy is correct!
6749       T = Context.CharTy;
6750       break;
6751     case PREDEF_TYPE_UCHAR_ID:
6752       T = Context.UnsignedCharTy;
6753       break;
6754     case PREDEF_TYPE_USHORT_ID:
6755       T = Context.UnsignedShortTy;
6756       break;
6757     case PREDEF_TYPE_UINT_ID:
6758       T = Context.UnsignedIntTy;
6759       break;
6760     case PREDEF_TYPE_ULONG_ID:
6761       T = Context.UnsignedLongTy;
6762       break;
6763     case PREDEF_TYPE_ULONGLONG_ID:
6764       T = Context.UnsignedLongLongTy;
6765       break;
6766     case PREDEF_TYPE_UINT128_ID:
6767       T = Context.UnsignedInt128Ty;
6768       break;
6769     case PREDEF_TYPE_SCHAR_ID:
6770       T = Context.SignedCharTy;
6771       break;
6772     case PREDEF_TYPE_WCHAR_ID:
6773       T = Context.WCharTy;
6774       break;
6775     case PREDEF_TYPE_SHORT_ID:
6776       T = Context.ShortTy;
6777       break;
6778     case PREDEF_TYPE_INT_ID:
6779       T = Context.IntTy;
6780       break;
6781     case PREDEF_TYPE_LONG_ID:
6782       T = Context.LongTy;
6783       break;
6784     case PREDEF_TYPE_LONGLONG_ID:
6785       T = Context.LongLongTy;
6786       break;
6787     case PREDEF_TYPE_INT128_ID:
6788       T = Context.Int128Ty;
6789       break;
6790     case PREDEF_TYPE_HALF_ID:
6791       T = Context.HalfTy;
6792       break;
6793     case PREDEF_TYPE_FLOAT_ID:
6794       T = Context.FloatTy;
6795       break;
6796     case PREDEF_TYPE_DOUBLE_ID:
6797       T = Context.DoubleTy;
6798       break;
6799     case PREDEF_TYPE_LONGDOUBLE_ID:
6800       T = Context.LongDoubleTy;
6801       break;
6802     case PREDEF_TYPE_SHORT_ACCUM_ID:
6803       T = Context.ShortAccumTy;
6804       break;
6805     case PREDEF_TYPE_ACCUM_ID:
6806       T = Context.AccumTy;
6807       break;
6808     case PREDEF_TYPE_LONG_ACCUM_ID:
6809       T = Context.LongAccumTy;
6810       break;
6811     case PREDEF_TYPE_USHORT_ACCUM_ID:
6812       T = Context.UnsignedShortAccumTy;
6813       break;
6814     case PREDEF_TYPE_UACCUM_ID:
6815       T = Context.UnsignedAccumTy;
6816       break;
6817     case PREDEF_TYPE_ULONG_ACCUM_ID:
6818       T = Context.UnsignedLongAccumTy;
6819       break;
6820     case PREDEF_TYPE_SHORT_FRACT_ID:
6821       T = Context.ShortFractTy;
6822       break;
6823     case PREDEF_TYPE_FRACT_ID:
6824       T = Context.FractTy;
6825       break;
6826     case PREDEF_TYPE_LONG_FRACT_ID:
6827       T = Context.LongFractTy;
6828       break;
6829     case PREDEF_TYPE_USHORT_FRACT_ID:
6830       T = Context.UnsignedShortFractTy;
6831       break;
6832     case PREDEF_TYPE_UFRACT_ID:
6833       T = Context.UnsignedFractTy;
6834       break;
6835     case PREDEF_TYPE_ULONG_FRACT_ID:
6836       T = Context.UnsignedLongFractTy;
6837       break;
6838     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6839       T = Context.SatShortAccumTy;
6840       break;
6841     case PREDEF_TYPE_SAT_ACCUM_ID:
6842       T = Context.SatAccumTy;
6843       break;
6844     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6845       T = Context.SatLongAccumTy;
6846       break;
6847     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6848       T = Context.SatUnsignedShortAccumTy;
6849       break;
6850     case PREDEF_TYPE_SAT_UACCUM_ID:
6851       T = Context.SatUnsignedAccumTy;
6852       break;
6853     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6854       T = Context.SatUnsignedLongAccumTy;
6855       break;
6856     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6857       T = Context.SatShortFractTy;
6858       break;
6859     case PREDEF_TYPE_SAT_FRACT_ID:
6860       T = Context.SatFractTy;
6861       break;
6862     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6863       T = Context.SatLongFractTy;
6864       break;
6865     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6866       T = Context.SatUnsignedShortFractTy;
6867       break;
6868     case PREDEF_TYPE_SAT_UFRACT_ID:
6869       T = Context.SatUnsignedFractTy;
6870       break;
6871     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6872       T = Context.SatUnsignedLongFractTy;
6873       break;
6874     case PREDEF_TYPE_FLOAT16_ID:
6875       T = Context.Float16Ty;
6876       break;
6877     case PREDEF_TYPE_FLOAT128_ID:
6878       T = Context.Float128Ty;
6879       break;
6880     case PREDEF_TYPE_OVERLOAD_ID:
6881       T = Context.OverloadTy;
6882       break;
6883     case PREDEF_TYPE_BOUND_MEMBER:
6884       T = Context.BoundMemberTy;
6885       break;
6886     case PREDEF_TYPE_PSEUDO_OBJECT:
6887       T = Context.PseudoObjectTy;
6888       break;
6889     case PREDEF_TYPE_DEPENDENT_ID:
6890       T = Context.DependentTy;
6891       break;
6892     case PREDEF_TYPE_UNKNOWN_ANY:
6893       T = Context.UnknownAnyTy;
6894       break;
6895     case PREDEF_TYPE_NULLPTR_ID:
6896       T = Context.NullPtrTy;
6897       break;
6898     case PREDEF_TYPE_CHAR8_ID:
6899       T = Context.Char8Ty;
6900       break;
6901     case PREDEF_TYPE_CHAR16_ID:
6902       T = Context.Char16Ty;
6903       break;
6904     case PREDEF_TYPE_CHAR32_ID:
6905       T = Context.Char32Ty;
6906       break;
6907     case PREDEF_TYPE_OBJC_ID:
6908       T = Context.ObjCBuiltinIdTy;
6909       break;
6910     case PREDEF_TYPE_OBJC_CLASS:
6911       T = Context.ObjCBuiltinClassTy;
6912       break;
6913     case PREDEF_TYPE_OBJC_SEL:
6914       T = Context.ObjCBuiltinSelTy;
6915       break;
6916 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6917     case PREDEF_TYPE_##Id##_ID: \
6918       T = Context.SingletonId; \
6919       break;
6920 #include "clang/Basic/OpenCLImageTypes.def"
6921 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6922     case PREDEF_TYPE_##Id##_ID: \
6923       T = Context.Id##Ty; \
6924       break;
6925 #include "clang/Basic/OpenCLExtensionTypes.def"
6926     case PREDEF_TYPE_SAMPLER_ID:
6927       T = Context.OCLSamplerTy;
6928       break;
6929     case PREDEF_TYPE_EVENT_ID:
6930       T = Context.OCLEventTy;
6931       break;
6932     case PREDEF_TYPE_CLK_EVENT_ID:
6933       T = Context.OCLClkEventTy;
6934       break;
6935     case PREDEF_TYPE_QUEUE_ID:
6936       T = Context.OCLQueueTy;
6937       break;
6938     case PREDEF_TYPE_RESERVE_ID_ID:
6939       T = Context.OCLReserveIDTy;
6940       break;
6941     case PREDEF_TYPE_AUTO_DEDUCT:
6942       T = Context.getAutoDeductType();
6943       break;
6944     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6945       T = Context.getAutoRRefDeductType();
6946       break;
6947     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6948       T = Context.ARCUnbridgedCastTy;
6949       break;
6950     case PREDEF_TYPE_BUILTIN_FN:
6951       T = Context.BuiltinFnTy;
6952       break;
6953     case PREDEF_TYPE_OMP_ARRAY_SECTION:
6954       T = Context.OMPArraySectionTy;
6955       break;
6956 #define SVE_TYPE(Name, Id, SingletonId) \
6957     case PREDEF_TYPE_##Id##_ID: \
6958       T = Context.SingletonId; \
6959       break;
6960 #include "clang/Basic/AArch64SVEACLETypes.def"
6961     }
6962 
6963     assert(!T.isNull() && "Unknown predefined type");
6964     return T.withFastQualifiers(FastQuals);
6965   }
6966 
6967   Index -= NUM_PREDEF_TYPE_IDS;
6968   assert(Index < TypesLoaded.size() && "Type index out-of-range");
6969   if (TypesLoaded[Index].isNull()) {
6970     TypesLoaded[Index] = readTypeRecord(Index);
6971     if (TypesLoaded[Index].isNull())
6972       return QualType();
6973 
6974     TypesLoaded[Index]->setFromAST();
6975     if (DeserializationListener)
6976       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6977                                         TypesLoaded[Index]);
6978   }
6979 
6980   return TypesLoaded[Index].withFastQualifiers(FastQuals);
6981 }
6982 
6983 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6984   return GetType(getGlobalTypeID(F, LocalID));
6985 }
6986 
6987 serialization::TypeID
6988 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6989   unsigned FastQuals = LocalID & Qualifiers::FastMask;
6990   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6991 
6992   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6993     return LocalID;
6994 
6995   if (!F.ModuleOffsetMap.empty())
6996     ReadModuleOffsetMap(F);
6997 
6998   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6999     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7000   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7001 
7002   unsigned GlobalIndex = LocalIndex + I->second;
7003   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7004 }
7005 
7006 TemplateArgumentLocInfo
7007 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7008   switch (Kind) {
7009   case TemplateArgument::Expression:
7010     return readExpr();
7011   case TemplateArgument::Type:
7012     return readTypeSourceInfo();
7013   case TemplateArgument::Template: {
7014     NestedNameSpecifierLoc QualifierLoc =
7015       readNestedNameSpecifierLoc();
7016     SourceLocation TemplateNameLoc = readSourceLocation();
7017     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7018                                    SourceLocation());
7019   }
7020   case TemplateArgument::TemplateExpansion: {
7021     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7022     SourceLocation TemplateNameLoc = readSourceLocation();
7023     SourceLocation EllipsisLoc = readSourceLocation();
7024     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7025                                    EllipsisLoc);
7026   }
7027   case TemplateArgument::Null:
7028   case TemplateArgument::Integral:
7029   case TemplateArgument::Declaration:
7030   case TemplateArgument::NullPtr:
7031   case TemplateArgument::Pack:
7032     // FIXME: Is this right?
7033     return TemplateArgumentLocInfo();
7034   }
7035   llvm_unreachable("unexpected template argument loc");
7036 }
7037 
7038 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7039   TemplateArgument Arg = readTemplateArgument();
7040 
7041   if (Arg.getKind() == TemplateArgument::Expression) {
7042     if (readBool()) // bool InfoHasSameExpr.
7043       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7044   }
7045   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7046 }
7047 
7048 const ASTTemplateArgumentListInfo *
7049 ASTRecordReader::readASTTemplateArgumentListInfo() {
7050   SourceLocation LAngleLoc = readSourceLocation();
7051   SourceLocation RAngleLoc = readSourceLocation();
7052   unsigned NumArgsAsWritten = readInt();
7053   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7054   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7055     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7056   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7057 }
7058 
7059 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7060   return GetDecl(ID);
7061 }
7062 
7063 void ASTReader::CompleteRedeclChain(const Decl *D) {
7064   if (NumCurrentElementsDeserializing) {
7065     // We arrange to not care about the complete redeclaration chain while we're
7066     // deserializing. Just remember that the AST has marked this one as complete
7067     // but that it's not actually complete yet, so we know we still need to
7068     // complete it later.
7069     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7070     return;
7071   }
7072 
7073   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7074 
7075   // If this is a named declaration, complete it by looking it up
7076   // within its context.
7077   //
7078   // FIXME: Merging a function definition should merge
7079   // all mergeable entities within it.
7080   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7081       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7082     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7083       if (!getContext().getLangOpts().CPlusPlus &&
7084           isa<TranslationUnitDecl>(DC)) {
7085         // Outside of C++, we don't have a lookup table for the TU, so update
7086         // the identifier instead. (For C++ modules, we don't store decls
7087         // in the serialized identifier table, so we do the lookup in the TU.)
7088         auto *II = Name.getAsIdentifierInfo();
7089         assert(II && "non-identifier name in C?");
7090         if (II->isOutOfDate())
7091           updateOutOfDateIdentifier(*II);
7092       } else
7093         DC->lookup(Name);
7094     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7095       // Find all declarations of this kind from the relevant context.
7096       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7097         auto *DC = cast<DeclContext>(DCDecl);
7098         SmallVector<Decl*, 8> Decls;
7099         FindExternalLexicalDecls(
7100             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7101       }
7102     }
7103   }
7104 
7105   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7106     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7107   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7108     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7109   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7110     if (auto *Template = FD->getPrimaryTemplate())
7111       Template->LoadLazySpecializations();
7112   }
7113 }
7114 
7115 CXXCtorInitializer **
7116 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7117   RecordLocation Loc = getLocalBitOffset(Offset);
7118   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7119   SavedStreamPosition SavedPosition(Cursor);
7120   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7121     Error(std::move(Err));
7122     return nullptr;
7123   }
7124   ReadingKindTracker ReadingKind(Read_Decl, *this);
7125 
7126   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7127   if (!MaybeCode) {
7128     Error(MaybeCode.takeError());
7129     return nullptr;
7130   }
7131   unsigned Code = MaybeCode.get();
7132 
7133   ASTRecordReader Record(*this, *Loc.F);
7134   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7135   if (!MaybeRecCode) {
7136     Error(MaybeRecCode.takeError());
7137     return nullptr;
7138   }
7139   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7140     Error("malformed AST file: missing C++ ctor initializers");
7141     return nullptr;
7142   }
7143 
7144   return Record.readCXXCtorInitializers();
7145 }
7146 
7147 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7148   assert(ContextObj && "reading base specifiers with no AST context");
7149   ASTContext &Context = *ContextObj;
7150 
7151   RecordLocation Loc = getLocalBitOffset(Offset);
7152   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7153   SavedStreamPosition SavedPosition(Cursor);
7154   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7155     Error(std::move(Err));
7156     return nullptr;
7157   }
7158   ReadingKindTracker ReadingKind(Read_Decl, *this);
7159 
7160   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7161   if (!MaybeCode) {
7162     Error(MaybeCode.takeError());
7163     return nullptr;
7164   }
7165   unsigned Code = MaybeCode.get();
7166 
7167   ASTRecordReader Record(*this, *Loc.F);
7168   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7169   if (!MaybeRecCode) {
7170     Error(MaybeCode.takeError());
7171     return nullptr;
7172   }
7173   unsigned RecCode = MaybeRecCode.get();
7174 
7175   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7176     Error("malformed AST file: missing C++ base specifiers");
7177     return nullptr;
7178   }
7179 
7180   unsigned NumBases = Record.readInt();
7181   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7182   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7183   for (unsigned I = 0; I != NumBases; ++I)
7184     Bases[I] = Record.readCXXBaseSpecifier();
7185   return Bases;
7186 }
7187 
7188 serialization::DeclID
7189 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7190   if (LocalID < NUM_PREDEF_DECL_IDS)
7191     return LocalID;
7192 
7193   if (!F.ModuleOffsetMap.empty())
7194     ReadModuleOffsetMap(F);
7195 
7196   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7197     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7198   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7199 
7200   return LocalID + I->second;
7201 }
7202 
7203 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7204                                    ModuleFile &M) const {
7205   // Predefined decls aren't from any module.
7206   if (ID < NUM_PREDEF_DECL_IDS)
7207     return false;
7208 
7209   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7210          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7211 }
7212 
7213 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7214   if (!D->isFromASTFile())
7215     return nullptr;
7216   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7217   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7218   return I->second;
7219 }
7220 
7221 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7222   if (ID < NUM_PREDEF_DECL_IDS)
7223     return SourceLocation();
7224 
7225   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7226 
7227   if (Index > DeclsLoaded.size()) {
7228     Error("declaration ID out-of-range for AST file");
7229     return SourceLocation();
7230   }
7231 
7232   if (Decl *D = DeclsLoaded[Index])
7233     return D->getLocation();
7234 
7235   SourceLocation Loc;
7236   DeclCursorForID(ID, Loc);
7237   return Loc;
7238 }
7239 
7240 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7241   switch (ID) {
7242   case PREDEF_DECL_NULL_ID:
7243     return nullptr;
7244 
7245   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7246     return Context.getTranslationUnitDecl();
7247 
7248   case PREDEF_DECL_OBJC_ID_ID:
7249     return Context.getObjCIdDecl();
7250 
7251   case PREDEF_DECL_OBJC_SEL_ID:
7252     return Context.getObjCSelDecl();
7253 
7254   case PREDEF_DECL_OBJC_CLASS_ID:
7255     return Context.getObjCClassDecl();
7256 
7257   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7258     return Context.getObjCProtocolDecl();
7259 
7260   case PREDEF_DECL_INT_128_ID:
7261     return Context.getInt128Decl();
7262 
7263   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7264     return Context.getUInt128Decl();
7265 
7266   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7267     return Context.getObjCInstanceTypeDecl();
7268 
7269   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7270     return Context.getBuiltinVaListDecl();
7271 
7272   case PREDEF_DECL_VA_LIST_TAG:
7273     return Context.getVaListTagDecl();
7274 
7275   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7276     return Context.getBuiltinMSVaListDecl();
7277 
7278   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7279     return Context.getExternCContextDecl();
7280 
7281   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7282     return Context.getMakeIntegerSeqDecl();
7283 
7284   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7285     return Context.getCFConstantStringDecl();
7286 
7287   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7288     return Context.getCFConstantStringTagDecl();
7289 
7290   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7291     return Context.getTypePackElementDecl();
7292   }
7293   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7294 }
7295 
7296 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7297   assert(ContextObj && "reading decl with no AST context");
7298   if (ID < NUM_PREDEF_DECL_IDS) {
7299     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7300     if (D) {
7301       // Track that we have merged the declaration with ID \p ID into the
7302       // pre-existing predefined declaration \p D.
7303       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7304       if (Merged.empty())
7305         Merged.push_back(ID);
7306     }
7307     return D;
7308   }
7309 
7310   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7311 
7312   if (Index >= DeclsLoaded.size()) {
7313     assert(0 && "declaration ID out-of-range for AST file");
7314     Error("declaration ID out-of-range for AST file");
7315     return nullptr;
7316   }
7317 
7318   return DeclsLoaded[Index];
7319 }
7320 
7321 Decl *ASTReader::GetDecl(DeclID ID) {
7322   if (ID < NUM_PREDEF_DECL_IDS)
7323     return GetExistingDecl(ID);
7324 
7325   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7326 
7327   if (Index >= DeclsLoaded.size()) {
7328     assert(0 && "declaration ID out-of-range for AST file");
7329     Error("declaration ID out-of-range for AST file");
7330     return nullptr;
7331   }
7332 
7333   if (!DeclsLoaded[Index]) {
7334     ReadDeclRecord(ID);
7335     if (DeserializationListener)
7336       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7337   }
7338 
7339   return DeclsLoaded[Index];
7340 }
7341 
7342 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7343                                                   DeclID GlobalID) {
7344   if (GlobalID < NUM_PREDEF_DECL_IDS)
7345     return GlobalID;
7346 
7347   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7348   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7349   ModuleFile *Owner = I->second;
7350 
7351   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7352     = M.GlobalToLocalDeclIDs.find(Owner);
7353   if (Pos == M.GlobalToLocalDeclIDs.end())
7354     return 0;
7355 
7356   return GlobalID - Owner->BaseDeclID + Pos->second;
7357 }
7358 
7359 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7360                                             const RecordData &Record,
7361                                             unsigned &Idx) {
7362   if (Idx >= Record.size()) {
7363     Error("Corrupted AST file");
7364     return 0;
7365   }
7366 
7367   return getGlobalDeclID(F, Record[Idx++]);
7368 }
7369 
7370 /// Resolve the offset of a statement into a statement.
7371 ///
7372 /// This operation will read a new statement from the external
7373 /// source each time it is called, and is meant to be used via a
7374 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7375 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7376   // Switch case IDs are per Decl.
7377   ClearSwitchCaseIDs();
7378 
7379   // Offset here is a global offset across the entire chain.
7380   RecordLocation Loc = getLocalBitOffset(Offset);
7381   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7382     Error(std::move(Err));
7383     return nullptr;
7384   }
7385   assert(NumCurrentElementsDeserializing == 0 &&
7386          "should not be called while already deserializing");
7387   Deserializing D(this);
7388   return ReadStmtFromStream(*Loc.F);
7389 }
7390 
7391 void ASTReader::FindExternalLexicalDecls(
7392     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7393     SmallVectorImpl<Decl *> &Decls) {
7394   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7395 
7396   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7397     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7398     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7399       auto K = (Decl::Kind)+LexicalDecls[I];
7400       if (!IsKindWeWant(K))
7401         continue;
7402 
7403       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7404 
7405       // Don't add predefined declarations to the lexical context more
7406       // than once.
7407       if (ID < NUM_PREDEF_DECL_IDS) {
7408         if (PredefsVisited[ID])
7409           continue;
7410 
7411         PredefsVisited[ID] = true;
7412       }
7413 
7414       if (Decl *D = GetLocalDecl(*M, ID)) {
7415         assert(D->getKind() == K && "wrong kind for lexical decl");
7416         if (!DC->isDeclInLexicalTraversal(D))
7417           Decls.push_back(D);
7418       }
7419     }
7420   };
7421 
7422   if (isa<TranslationUnitDecl>(DC)) {
7423     for (auto Lexical : TULexicalDecls)
7424       Visit(Lexical.first, Lexical.second);
7425   } else {
7426     auto I = LexicalDecls.find(DC);
7427     if (I != LexicalDecls.end())
7428       Visit(I->second.first, I->second.second);
7429   }
7430 
7431   ++NumLexicalDeclContextsRead;
7432 }
7433 
7434 namespace {
7435 
7436 class DeclIDComp {
7437   ASTReader &Reader;
7438   ModuleFile &Mod;
7439 
7440 public:
7441   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7442 
7443   bool operator()(LocalDeclID L, LocalDeclID R) const {
7444     SourceLocation LHS = getLocation(L);
7445     SourceLocation RHS = getLocation(R);
7446     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7447   }
7448 
7449   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7450     SourceLocation RHS = getLocation(R);
7451     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7452   }
7453 
7454   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7455     SourceLocation LHS = getLocation(L);
7456     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7457   }
7458 
7459   SourceLocation getLocation(LocalDeclID ID) const {
7460     return Reader.getSourceManager().getFileLoc(
7461             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7462   }
7463 };
7464 
7465 } // namespace
7466 
7467 void ASTReader::FindFileRegionDecls(FileID File,
7468                                     unsigned Offset, unsigned Length,
7469                                     SmallVectorImpl<Decl *> &Decls) {
7470   SourceManager &SM = getSourceManager();
7471 
7472   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7473   if (I == FileDeclIDs.end())
7474     return;
7475 
7476   FileDeclsInfo &DInfo = I->second;
7477   if (DInfo.Decls.empty())
7478     return;
7479 
7480   SourceLocation
7481     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7482   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7483 
7484   DeclIDComp DIDComp(*this, *DInfo.Mod);
7485   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7486       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7487   if (BeginIt != DInfo.Decls.begin())
7488     --BeginIt;
7489 
7490   // If we are pointing at a top-level decl inside an objc container, we need
7491   // to backtrack until we find it otherwise we will fail to report that the
7492   // region overlaps with an objc container.
7493   while (BeginIt != DInfo.Decls.begin() &&
7494          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7495              ->isTopLevelDeclInObjCContainer())
7496     --BeginIt;
7497 
7498   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7499       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7500   if (EndIt != DInfo.Decls.end())
7501     ++EndIt;
7502 
7503   for (ArrayRef<serialization::LocalDeclID>::iterator
7504          DIt = BeginIt; DIt != EndIt; ++DIt)
7505     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7506 }
7507 
7508 bool
7509 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7510                                           DeclarationName Name) {
7511   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7512          "DeclContext has no visible decls in storage");
7513   if (!Name)
7514     return false;
7515 
7516   auto It = Lookups.find(DC);
7517   if (It == Lookups.end())
7518     return false;
7519 
7520   Deserializing LookupResults(this);
7521 
7522   // Load the list of declarations.
7523   SmallVector<NamedDecl *, 64> Decls;
7524   for (DeclID ID : It->second.Table.find(Name)) {
7525     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7526     if (ND->getDeclName() == Name)
7527       Decls.push_back(ND);
7528   }
7529 
7530   ++NumVisibleDeclContextsRead;
7531   SetExternalVisibleDeclsForName(DC, Name, Decls);
7532   return !Decls.empty();
7533 }
7534 
7535 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7536   if (!DC->hasExternalVisibleStorage())
7537     return;
7538 
7539   auto It = Lookups.find(DC);
7540   assert(It != Lookups.end() &&
7541          "have external visible storage but no lookup tables");
7542 
7543   DeclsMap Decls;
7544 
7545   for (DeclID ID : It->second.Table.findAll()) {
7546     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7547     Decls[ND->getDeclName()].push_back(ND);
7548   }
7549 
7550   ++NumVisibleDeclContextsRead;
7551 
7552   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7553     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7554   }
7555   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7556 }
7557 
7558 const serialization::reader::DeclContextLookupTable *
7559 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7560   auto I = Lookups.find(Primary);
7561   return I == Lookups.end() ? nullptr : &I->second;
7562 }
7563 
7564 /// Under non-PCH compilation the consumer receives the objc methods
7565 /// before receiving the implementation, and codegen depends on this.
7566 /// We simulate this by deserializing and passing to consumer the methods of the
7567 /// implementation before passing the deserialized implementation decl.
7568 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7569                                        ASTConsumer *Consumer) {
7570   assert(ImplD && Consumer);
7571 
7572   for (auto *I : ImplD->methods())
7573     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7574 
7575   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7576 }
7577 
7578 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7579   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7580     PassObjCImplDeclToConsumer(ImplD, Consumer);
7581   else
7582     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7583 }
7584 
7585 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7586   this->Consumer = Consumer;
7587 
7588   if (Consumer)
7589     PassInterestingDeclsToConsumer();
7590 
7591   if (DeserializationListener)
7592     DeserializationListener->ReaderInitialized(this);
7593 }
7594 
7595 void ASTReader::PrintStats() {
7596   std::fprintf(stderr, "*** AST File Statistics:\n");
7597 
7598   unsigned NumTypesLoaded
7599     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7600                                       QualType());
7601   unsigned NumDeclsLoaded
7602     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7603                                       (Decl *)nullptr);
7604   unsigned NumIdentifiersLoaded
7605     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7606                                             IdentifiersLoaded.end(),
7607                                             (IdentifierInfo *)nullptr);
7608   unsigned NumMacrosLoaded
7609     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7610                                        MacrosLoaded.end(),
7611                                        (MacroInfo *)nullptr);
7612   unsigned NumSelectorsLoaded
7613     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7614                                           SelectorsLoaded.end(),
7615                                           Selector());
7616 
7617   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7618     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7619                  NumSLocEntriesRead, TotalNumSLocEntries,
7620                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7621   if (!TypesLoaded.empty())
7622     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7623                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7624                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7625   if (!DeclsLoaded.empty())
7626     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7627                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7628                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7629   if (!IdentifiersLoaded.empty())
7630     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7631                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7632                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7633   if (!MacrosLoaded.empty())
7634     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7635                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7636                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7637   if (!SelectorsLoaded.empty())
7638     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7639                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7640                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7641   if (TotalNumStatements)
7642     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7643                  NumStatementsRead, TotalNumStatements,
7644                  ((float)NumStatementsRead/TotalNumStatements * 100));
7645   if (TotalNumMacros)
7646     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7647                  NumMacrosRead, TotalNumMacros,
7648                  ((float)NumMacrosRead/TotalNumMacros * 100));
7649   if (TotalLexicalDeclContexts)
7650     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7651                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7652                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7653                   * 100));
7654   if (TotalVisibleDeclContexts)
7655     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7656                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7657                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7658                   * 100));
7659   if (TotalNumMethodPoolEntries)
7660     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7661                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7662                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7663                   * 100));
7664   if (NumMethodPoolLookups)
7665     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7666                  NumMethodPoolHits, NumMethodPoolLookups,
7667                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7668   if (NumMethodPoolTableLookups)
7669     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7670                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7671                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7672                   * 100.0));
7673   if (NumIdentifierLookupHits)
7674     std::fprintf(stderr,
7675                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7676                  NumIdentifierLookupHits, NumIdentifierLookups,
7677                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7678 
7679   if (GlobalIndex) {
7680     std::fprintf(stderr, "\n");
7681     GlobalIndex->printStats();
7682   }
7683 
7684   std::fprintf(stderr, "\n");
7685   dump();
7686   std::fprintf(stderr, "\n");
7687 }
7688 
7689 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7690 LLVM_DUMP_METHOD static void
7691 dumpModuleIDMap(StringRef Name,
7692                 const ContinuousRangeMap<Key, ModuleFile *,
7693                                          InitialCapacity> &Map) {
7694   if (Map.begin() == Map.end())
7695     return;
7696 
7697   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7698 
7699   llvm::errs() << Name << ":\n";
7700   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7701        I != IEnd; ++I) {
7702     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7703       << "\n";
7704   }
7705 }
7706 
7707 LLVM_DUMP_METHOD void ASTReader::dump() {
7708   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7709   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7710   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7711   dumpModuleIDMap("Global type map", GlobalTypeMap);
7712   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7713   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7714   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7715   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7716   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7717   dumpModuleIDMap("Global preprocessed entity map",
7718                   GlobalPreprocessedEntityMap);
7719 
7720   llvm::errs() << "\n*** PCH/Modules Loaded:";
7721   for (ModuleFile &M : ModuleMgr)
7722     M.dump();
7723 }
7724 
7725 /// Return the amount of memory used by memory buffers, breaking down
7726 /// by heap-backed versus mmap'ed memory.
7727 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7728   for (ModuleFile &I : ModuleMgr) {
7729     if (llvm::MemoryBuffer *buf = I.Buffer) {
7730       size_t bytes = buf->getBufferSize();
7731       switch (buf->getBufferKind()) {
7732         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7733           sizes.malloc_bytes += bytes;
7734           break;
7735         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7736           sizes.mmap_bytes += bytes;
7737           break;
7738       }
7739     }
7740   }
7741 }
7742 
7743 void ASTReader::InitializeSema(Sema &S) {
7744   SemaObj = &S;
7745   S.addExternalSource(this);
7746 
7747   // Makes sure any declarations that were deserialized "too early"
7748   // still get added to the identifier's declaration chains.
7749   for (uint64_t ID : PreloadedDeclIDs) {
7750     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7751     pushExternalDeclIntoScope(D, D->getDeclName());
7752   }
7753   PreloadedDeclIDs.clear();
7754 
7755   // FIXME: What happens if these are changed by a module import?
7756   if (!FPPragmaOptions.empty()) {
7757     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7758     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7759   }
7760 
7761   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7762   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7763   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7764 
7765   UpdateSema();
7766 }
7767 
7768 void ASTReader::UpdateSema() {
7769   assert(SemaObj && "no Sema to update");
7770 
7771   // Load the offsets of the declarations that Sema references.
7772   // They will be lazily deserialized when needed.
7773   if (!SemaDeclRefs.empty()) {
7774     assert(SemaDeclRefs.size() % 3 == 0);
7775     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7776       if (!SemaObj->StdNamespace)
7777         SemaObj->StdNamespace = SemaDeclRefs[I];
7778       if (!SemaObj->StdBadAlloc)
7779         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7780       if (!SemaObj->StdAlignValT)
7781         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7782     }
7783     SemaDeclRefs.clear();
7784   }
7785 
7786   // Update the state of pragmas. Use the same API as if we had encountered the
7787   // pragma in the source.
7788   if(OptimizeOffPragmaLocation.isValid())
7789     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7790   if (PragmaMSStructState != -1)
7791     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7792   if (PointersToMembersPragmaLocation.isValid()) {
7793     SemaObj->ActOnPragmaMSPointersToMembers(
7794         (LangOptions::PragmaMSPointersToMembersKind)
7795             PragmaMSPointersToMembersState,
7796         PointersToMembersPragmaLocation);
7797   }
7798   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7799 
7800   if (PragmaPackCurrentValue) {
7801     // The bottom of the stack might have a default value. It must be adjusted
7802     // to the current value to ensure that the packing state is preserved after
7803     // popping entries that were included/imported from a PCH/module.
7804     bool DropFirst = false;
7805     if (!PragmaPackStack.empty() &&
7806         PragmaPackStack.front().Location.isInvalid()) {
7807       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7808              "Expected a default alignment value");
7809       SemaObj->PackStack.Stack.emplace_back(
7810           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7811           SemaObj->PackStack.CurrentPragmaLocation,
7812           PragmaPackStack.front().PushLocation);
7813       DropFirst = true;
7814     }
7815     for (const auto &Entry :
7816          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7817       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7818                                             Entry.Location, Entry.PushLocation);
7819     if (PragmaPackCurrentLocation.isInvalid()) {
7820       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7821              "Expected a default alignment value");
7822       // Keep the current values.
7823     } else {
7824       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7825       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7826     }
7827   }
7828 }
7829 
7830 IdentifierInfo *ASTReader::get(StringRef Name) {
7831   // Note that we are loading an identifier.
7832   Deserializing AnIdentifier(this);
7833 
7834   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7835                                   NumIdentifierLookups,
7836                                   NumIdentifierLookupHits);
7837 
7838   // We don't need to do identifier table lookups in C++ modules (we preload
7839   // all interesting declarations, and don't need to use the scope for name
7840   // lookups). Perform the lookup in PCH files, though, since we don't build
7841   // a complete initial identifier table if we're carrying on from a PCH.
7842   if (PP.getLangOpts().CPlusPlus) {
7843     for (auto F : ModuleMgr.pch_modules())
7844       if (Visitor(*F))
7845         break;
7846   } else {
7847     // If there is a global index, look there first to determine which modules
7848     // provably do not have any results for this identifier.
7849     GlobalModuleIndex::HitSet Hits;
7850     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7851     if (!loadGlobalIndex()) {
7852       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7853         HitsPtr = &Hits;
7854       }
7855     }
7856 
7857     ModuleMgr.visit(Visitor, HitsPtr);
7858   }
7859 
7860   IdentifierInfo *II = Visitor.getIdentifierInfo();
7861   markIdentifierUpToDate(II);
7862   return II;
7863 }
7864 
7865 namespace clang {
7866 
7867   /// An identifier-lookup iterator that enumerates all of the
7868   /// identifiers stored within a set of AST files.
7869   class ASTIdentifierIterator : public IdentifierIterator {
7870     /// The AST reader whose identifiers are being enumerated.
7871     const ASTReader &Reader;
7872 
7873     /// The current index into the chain of AST files stored in
7874     /// the AST reader.
7875     unsigned Index;
7876 
7877     /// The current position within the identifier lookup table
7878     /// of the current AST file.
7879     ASTIdentifierLookupTable::key_iterator Current;
7880 
7881     /// The end position within the identifier lookup table of
7882     /// the current AST file.
7883     ASTIdentifierLookupTable::key_iterator End;
7884 
7885     /// Whether to skip any modules in the ASTReader.
7886     bool SkipModules;
7887 
7888   public:
7889     explicit ASTIdentifierIterator(const ASTReader &Reader,
7890                                    bool SkipModules = false);
7891 
7892     StringRef Next() override;
7893   };
7894 
7895 } // namespace clang
7896 
7897 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7898                                              bool SkipModules)
7899     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7900 }
7901 
7902 StringRef ASTIdentifierIterator::Next() {
7903   while (Current == End) {
7904     // If we have exhausted all of our AST files, we're done.
7905     if (Index == 0)
7906       return StringRef();
7907 
7908     --Index;
7909     ModuleFile &F = Reader.ModuleMgr[Index];
7910     if (SkipModules && F.isModule())
7911       continue;
7912 
7913     ASTIdentifierLookupTable *IdTable =
7914         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7915     Current = IdTable->key_begin();
7916     End = IdTable->key_end();
7917   }
7918 
7919   // We have any identifiers remaining in the current AST file; return
7920   // the next one.
7921   StringRef Result = *Current;
7922   ++Current;
7923   return Result;
7924 }
7925 
7926 namespace {
7927 
7928 /// A utility for appending two IdentifierIterators.
7929 class ChainedIdentifierIterator : public IdentifierIterator {
7930   std::unique_ptr<IdentifierIterator> Current;
7931   std::unique_ptr<IdentifierIterator> Queued;
7932 
7933 public:
7934   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7935                             std::unique_ptr<IdentifierIterator> Second)
7936       : Current(std::move(First)), Queued(std::move(Second)) {}
7937 
7938   StringRef Next() override {
7939     if (!Current)
7940       return StringRef();
7941 
7942     StringRef result = Current->Next();
7943     if (!result.empty())
7944       return result;
7945 
7946     // Try the queued iterator, which may itself be empty.
7947     Current.reset();
7948     std::swap(Current, Queued);
7949     return Next();
7950   }
7951 };
7952 
7953 } // namespace
7954 
7955 IdentifierIterator *ASTReader::getIdentifiers() {
7956   if (!loadGlobalIndex()) {
7957     std::unique_ptr<IdentifierIterator> ReaderIter(
7958         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7959     std::unique_ptr<IdentifierIterator> ModulesIter(
7960         GlobalIndex->createIdentifierIterator());
7961     return new ChainedIdentifierIterator(std::move(ReaderIter),
7962                                          std::move(ModulesIter));
7963   }
7964 
7965   return new ASTIdentifierIterator(*this);
7966 }
7967 
7968 namespace clang {
7969 namespace serialization {
7970 
7971   class ReadMethodPoolVisitor {
7972     ASTReader &Reader;
7973     Selector Sel;
7974     unsigned PriorGeneration;
7975     unsigned InstanceBits = 0;
7976     unsigned FactoryBits = 0;
7977     bool InstanceHasMoreThanOneDecl = false;
7978     bool FactoryHasMoreThanOneDecl = false;
7979     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7980     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7981 
7982   public:
7983     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7984                           unsigned PriorGeneration)
7985         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
7986 
7987     bool operator()(ModuleFile &M) {
7988       if (!M.SelectorLookupTable)
7989         return false;
7990 
7991       // If we've already searched this module file, skip it now.
7992       if (M.Generation <= PriorGeneration)
7993         return true;
7994 
7995       ++Reader.NumMethodPoolTableLookups;
7996       ASTSelectorLookupTable *PoolTable
7997         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7998       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
7999       if (Pos == PoolTable->end())
8000         return false;
8001 
8002       ++Reader.NumMethodPoolTableHits;
8003       ++Reader.NumSelectorsRead;
8004       // FIXME: Not quite happy with the statistics here. We probably should
8005       // disable this tracking when called via LoadSelector.
8006       // Also, should entries without methods count as misses?
8007       ++Reader.NumMethodPoolEntriesRead;
8008       ASTSelectorLookupTrait::data_type Data = *Pos;
8009       if (Reader.DeserializationListener)
8010         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8011 
8012       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8013       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8014       InstanceBits = Data.InstanceBits;
8015       FactoryBits = Data.FactoryBits;
8016       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8017       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8018       return true;
8019     }
8020 
8021     /// Retrieve the instance methods found by this visitor.
8022     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8023       return InstanceMethods;
8024     }
8025 
8026     /// Retrieve the instance methods found by this visitor.
8027     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8028       return FactoryMethods;
8029     }
8030 
8031     unsigned getInstanceBits() const { return InstanceBits; }
8032     unsigned getFactoryBits() const { return FactoryBits; }
8033 
8034     bool instanceHasMoreThanOneDecl() const {
8035       return InstanceHasMoreThanOneDecl;
8036     }
8037 
8038     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8039   };
8040 
8041 } // namespace serialization
8042 } // namespace clang
8043 
8044 /// Add the given set of methods to the method list.
8045 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8046                              ObjCMethodList &List) {
8047   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8048     S.addMethodToGlobalList(&List, Methods[I]);
8049   }
8050 }
8051 
8052 void ASTReader::ReadMethodPool(Selector Sel) {
8053   // Get the selector generation and update it to the current generation.
8054   unsigned &Generation = SelectorGeneration[Sel];
8055   unsigned PriorGeneration = Generation;
8056   Generation = getGeneration();
8057   SelectorOutOfDate[Sel] = false;
8058 
8059   // Search for methods defined with this selector.
8060   ++NumMethodPoolLookups;
8061   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8062   ModuleMgr.visit(Visitor);
8063 
8064   if (Visitor.getInstanceMethods().empty() &&
8065       Visitor.getFactoryMethods().empty())
8066     return;
8067 
8068   ++NumMethodPoolHits;
8069 
8070   if (!getSema())
8071     return;
8072 
8073   Sema &S = *getSema();
8074   Sema::GlobalMethodPool::iterator Pos
8075     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8076 
8077   Pos->second.first.setBits(Visitor.getInstanceBits());
8078   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8079   Pos->second.second.setBits(Visitor.getFactoryBits());
8080   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8081 
8082   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8083   // when building a module we keep every method individually and may need to
8084   // update hasMoreThanOneDecl as we add the methods.
8085   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8086   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8087 }
8088 
8089 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8090   if (SelectorOutOfDate[Sel])
8091     ReadMethodPool(Sel);
8092 }
8093 
8094 void ASTReader::ReadKnownNamespaces(
8095                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8096   Namespaces.clear();
8097 
8098   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8099     if (NamespaceDecl *Namespace
8100                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8101       Namespaces.push_back(Namespace);
8102   }
8103 }
8104 
8105 void ASTReader::ReadUndefinedButUsed(
8106     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8107   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8108     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8109     SourceLocation Loc =
8110         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8111     Undefined.insert(std::make_pair(D, Loc));
8112   }
8113 }
8114 
8115 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8116     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8117                                                      Exprs) {
8118   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8119     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8120     uint64_t Count = DelayedDeleteExprs[Idx++];
8121     for (uint64_t C = 0; C < Count; ++C) {
8122       SourceLocation DeleteLoc =
8123           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8124       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8125       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8126     }
8127   }
8128 }
8129 
8130 void ASTReader::ReadTentativeDefinitions(
8131                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8132   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8133     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8134     if (Var)
8135       TentativeDefs.push_back(Var);
8136   }
8137   TentativeDefinitions.clear();
8138 }
8139 
8140 void ASTReader::ReadUnusedFileScopedDecls(
8141                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8142   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8143     DeclaratorDecl *D
8144       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8145     if (D)
8146       Decls.push_back(D);
8147   }
8148   UnusedFileScopedDecls.clear();
8149 }
8150 
8151 void ASTReader::ReadDelegatingConstructors(
8152                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8153   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8154     CXXConstructorDecl *D
8155       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8156     if (D)
8157       Decls.push_back(D);
8158   }
8159   DelegatingCtorDecls.clear();
8160 }
8161 
8162 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8163   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8164     TypedefNameDecl *D
8165       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8166     if (D)
8167       Decls.push_back(D);
8168   }
8169   ExtVectorDecls.clear();
8170 }
8171 
8172 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8173     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8174   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8175        ++I) {
8176     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8177         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8178     if (D)
8179       Decls.insert(D);
8180   }
8181   UnusedLocalTypedefNameCandidates.clear();
8182 }
8183 
8184 void ASTReader::ReadReferencedSelectors(
8185        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8186   if (ReferencedSelectorsData.empty())
8187     return;
8188 
8189   // If there are @selector references added them to its pool. This is for
8190   // implementation of -Wselector.
8191   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8192   unsigned I = 0;
8193   while (I < DataSize) {
8194     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8195     SourceLocation SelLoc
8196       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8197     Sels.push_back(std::make_pair(Sel, SelLoc));
8198   }
8199   ReferencedSelectorsData.clear();
8200 }
8201 
8202 void ASTReader::ReadWeakUndeclaredIdentifiers(
8203        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8204   if (WeakUndeclaredIdentifiers.empty())
8205     return;
8206 
8207   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8208     IdentifierInfo *WeakId
8209       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8210     IdentifierInfo *AliasId
8211       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8212     SourceLocation Loc
8213       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8214     bool Used = WeakUndeclaredIdentifiers[I++];
8215     WeakInfo WI(AliasId, Loc);
8216     WI.setUsed(Used);
8217     WeakIDs.push_back(std::make_pair(WeakId, WI));
8218   }
8219   WeakUndeclaredIdentifiers.clear();
8220 }
8221 
8222 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8223   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8224     ExternalVTableUse VT;
8225     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8226     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8227     VT.DefinitionRequired = VTableUses[Idx++];
8228     VTables.push_back(VT);
8229   }
8230 
8231   VTableUses.clear();
8232 }
8233 
8234 void ASTReader::ReadPendingInstantiations(
8235        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8236   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8237     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8238     SourceLocation Loc
8239       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8240 
8241     Pending.push_back(std::make_pair(D, Loc));
8242   }
8243   PendingInstantiations.clear();
8244 }
8245 
8246 void ASTReader::ReadLateParsedTemplates(
8247     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8248         &LPTMap) {
8249   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8250        /* In loop */) {
8251     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8252 
8253     auto LT = std::make_unique<LateParsedTemplate>();
8254     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8255 
8256     ModuleFile *F = getOwningModuleFile(LT->D);
8257     assert(F && "No module");
8258 
8259     unsigned TokN = LateParsedTemplates[Idx++];
8260     LT->Toks.reserve(TokN);
8261     for (unsigned T = 0; T < TokN; ++T)
8262       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8263 
8264     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8265   }
8266 
8267   LateParsedTemplates.clear();
8268 }
8269 
8270 void ASTReader::LoadSelector(Selector Sel) {
8271   // It would be complicated to avoid reading the methods anyway. So don't.
8272   ReadMethodPool(Sel);
8273 }
8274 
8275 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8276   assert(ID && "Non-zero identifier ID required");
8277   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8278   IdentifiersLoaded[ID - 1] = II;
8279   if (DeserializationListener)
8280     DeserializationListener->IdentifierRead(ID, II);
8281 }
8282 
8283 /// Set the globally-visible declarations associated with the given
8284 /// identifier.
8285 ///
8286 /// If the AST reader is currently in a state where the given declaration IDs
8287 /// cannot safely be resolved, they are queued until it is safe to resolve
8288 /// them.
8289 ///
8290 /// \param II an IdentifierInfo that refers to one or more globally-visible
8291 /// declarations.
8292 ///
8293 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8294 /// visible at global scope.
8295 ///
8296 /// \param Decls if non-null, this vector will be populated with the set of
8297 /// deserialized declarations. These declarations will not be pushed into
8298 /// scope.
8299 void
8300 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8301                               const SmallVectorImpl<uint32_t> &DeclIDs,
8302                                    SmallVectorImpl<Decl *> *Decls) {
8303   if (NumCurrentElementsDeserializing && !Decls) {
8304     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8305     return;
8306   }
8307 
8308   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8309     if (!SemaObj) {
8310       // Queue this declaration so that it will be added to the
8311       // translation unit scope and identifier's declaration chain
8312       // once a Sema object is known.
8313       PreloadedDeclIDs.push_back(DeclIDs[I]);
8314       continue;
8315     }
8316 
8317     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8318 
8319     // If we're simply supposed to record the declarations, do so now.
8320     if (Decls) {
8321       Decls->push_back(D);
8322       continue;
8323     }
8324 
8325     // Introduce this declaration into the translation-unit scope
8326     // and add it to the declaration chain for this identifier, so
8327     // that (unqualified) name lookup will find it.
8328     pushExternalDeclIntoScope(D, II);
8329   }
8330 }
8331 
8332 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8333   if (ID == 0)
8334     return nullptr;
8335 
8336   if (IdentifiersLoaded.empty()) {
8337     Error("no identifier table in AST file");
8338     return nullptr;
8339   }
8340 
8341   ID -= 1;
8342   if (!IdentifiersLoaded[ID]) {
8343     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8344     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8345     ModuleFile *M = I->second;
8346     unsigned Index = ID - M->BaseIdentifierID;
8347     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8348 
8349     // All of the strings in the AST file are preceded by a 16-bit length.
8350     // Extract that 16-bit length to avoid having to execute strlen().
8351     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8352     //  unsigned integers.  This is important to avoid integer overflow when
8353     //  we cast them to 'unsigned'.
8354     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8355     unsigned StrLen = (((unsigned) StrLenPtr[0])
8356                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8357     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8358     IdentifiersLoaded[ID] = &II;
8359     markIdentifierFromAST(*this,  II);
8360     if (DeserializationListener)
8361       DeserializationListener->IdentifierRead(ID + 1, &II);
8362   }
8363 
8364   return IdentifiersLoaded[ID];
8365 }
8366 
8367 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8368   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8369 }
8370 
8371 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8372   if (LocalID < NUM_PREDEF_IDENT_IDS)
8373     return LocalID;
8374 
8375   if (!M.ModuleOffsetMap.empty())
8376     ReadModuleOffsetMap(M);
8377 
8378   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8379     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8380   assert(I != M.IdentifierRemap.end()
8381          && "Invalid index into identifier index remap");
8382 
8383   return LocalID + I->second;
8384 }
8385 
8386 MacroInfo *ASTReader::getMacro(MacroID ID) {
8387   if (ID == 0)
8388     return nullptr;
8389 
8390   if (MacrosLoaded.empty()) {
8391     Error("no macro table in AST file");
8392     return nullptr;
8393   }
8394 
8395   ID -= NUM_PREDEF_MACRO_IDS;
8396   if (!MacrosLoaded[ID]) {
8397     GlobalMacroMapType::iterator I
8398       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8399     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8400     ModuleFile *M = I->second;
8401     unsigned Index = ID - M->BaseMacroID;
8402     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8403 
8404     if (DeserializationListener)
8405       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8406                                          MacrosLoaded[ID]);
8407   }
8408 
8409   return MacrosLoaded[ID];
8410 }
8411 
8412 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8413   if (LocalID < NUM_PREDEF_MACRO_IDS)
8414     return LocalID;
8415 
8416   if (!M.ModuleOffsetMap.empty())
8417     ReadModuleOffsetMap(M);
8418 
8419   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8420     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8421   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8422 
8423   return LocalID + I->second;
8424 }
8425 
8426 serialization::SubmoduleID
8427 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8428   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8429     return LocalID;
8430 
8431   if (!M.ModuleOffsetMap.empty())
8432     ReadModuleOffsetMap(M);
8433 
8434   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8435     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8436   assert(I != M.SubmoduleRemap.end()
8437          && "Invalid index into submodule index remap");
8438 
8439   return LocalID + I->second;
8440 }
8441 
8442 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8443   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8444     assert(GlobalID == 0 && "Unhandled global submodule ID");
8445     return nullptr;
8446   }
8447 
8448   if (GlobalID > SubmodulesLoaded.size()) {
8449     Error("submodule ID out of range in AST file");
8450     return nullptr;
8451   }
8452 
8453   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8454 }
8455 
8456 Module *ASTReader::getModule(unsigned ID) {
8457   return getSubmodule(ID);
8458 }
8459 
8460 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8461   ModuleFile *MF = getOwningModuleFile(D);
8462   return MF && MF->PCHHasObjectFile;
8463 }
8464 
8465 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8466   if (ID & 1) {
8467     // It's a module, look it up by submodule ID.
8468     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8469     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8470   } else {
8471     // It's a prefix (preamble, PCH, ...). Look it up by index.
8472     unsigned IndexFromEnd = ID >> 1;
8473     assert(IndexFromEnd && "got reference to unknown module file");
8474     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8475   }
8476 }
8477 
8478 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8479   if (!F)
8480     return 1;
8481 
8482   // For a file representing a module, use the submodule ID of the top-level
8483   // module as the file ID. For any other kind of file, the number of such
8484   // files loaded beforehand will be the same on reload.
8485   // FIXME: Is this true even if we have an explicit module file and a PCH?
8486   if (F->isModule())
8487     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8488 
8489   auto PCHModules = getModuleManager().pch_modules();
8490   auto I = llvm::find(PCHModules, F);
8491   assert(I != PCHModules.end() && "emitting reference to unknown file");
8492   return (I - PCHModules.end()) << 1;
8493 }
8494 
8495 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8496 ASTReader::getSourceDescriptor(unsigned ID) {
8497   if (const Module *M = getSubmodule(ID))
8498     return ExternalASTSource::ASTSourceDescriptor(*M);
8499 
8500   // If there is only a single PCH, return it instead.
8501   // Chained PCH are not supported.
8502   const auto &PCHChain = ModuleMgr.pch_modules();
8503   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8504     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8505     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8506     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8507     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8508                                           MF.Signature);
8509   }
8510   return None;
8511 }
8512 
8513 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8514   auto I = DefinitionSource.find(FD);
8515   if (I == DefinitionSource.end())
8516     return EK_ReplyHazy;
8517   return I->second ? EK_Never : EK_Always;
8518 }
8519 
8520 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8521   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8522 }
8523 
8524 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8525   if (ID == 0)
8526     return Selector();
8527 
8528   if (ID > SelectorsLoaded.size()) {
8529     Error("selector ID out of range in AST file");
8530     return Selector();
8531   }
8532 
8533   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8534     // Load this selector from the selector table.
8535     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8536     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8537     ModuleFile &M = *I->second;
8538     ASTSelectorLookupTrait Trait(*this, M);
8539     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8540     SelectorsLoaded[ID - 1] =
8541       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8542     if (DeserializationListener)
8543       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8544   }
8545 
8546   return SelectorsLoaded[ID - 1];
8547 }
8548 
8549 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8550   return DecodeSelector(ID);
8551 }
8552 
8553 uint32_t ASTReader::GetNumExternalSelectors() {
8554   // ID 0 (the null selector) is considered an external selector.
8555   return getTotalNumSelectors() + 1;
8556 }
8557 
8558 serialization::SelectorID
8559 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8560   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8561     return LocalID;
8562 
8563   if (!M.ModuleOffsetMap.empty())
8564     ReadModuleOffsetMap(M);
8565 
8566   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8567     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8568   assert(I != M.SelectorRemap.end()
8569          && "Invalid index into selector index remap");
8570 
8571   return LocalID + I->second;
8572 }
8573 
8574 DeclarationNameLoc
8575 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8576   DeclarationNameLoc DNLoc;
8577   switch (Name.getNameKind()) {
8578   case DeclarationName::CXXConstructorName:
8579   case DeclarationName::CXXDestructorName:
8580   case DeclarationName::CXXConversionFunctionName:
8581     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8582     break;
8583 
8584   case DeclarationName::CXXOperatorName:
8585     DNLoc.CXXOperatorName.BeginOpNameLoc
8586       = readSourceLocation().getRawEncoding();
8587     DNLoc.CXXOperatorName.EndOpNameLoc
8588       = readSourceLocation().getRawEncoding();
8589     break;
8590 
8591   case DeclarationName::CXXLiteralOperatorName:
8592     DNLoc.CXXLiteralOperatorName.OpNameLoc
8593       = readSourceLocation().getRawEncoding();
8594     break;
8595 
8596   case DeclarationName::Identifier:
8597   case DeclarationName::ObjCZeroArgSelector:
8598   case DeclarationName::ObjCOneArgSelector:
8599   case DeclarationName::ObjCMultiArgSelector:
8600   case DeclarationName::CXXUsingDirective:
8601   case DeclarationName::CXXDeductionGuideName:
8602     break;
8603   }
8604   return DNLoc;
8605 }
8606 
8607 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8608   DeclarationNameInfo NameInfo;
8609   NameInfo.setName(readDeclarationName());
8610   NameInfo.setLoc(readSourceLocation());
8611   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8612   return NameInfo;
8613 }
8614 
8615 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8616   Info.QualifierLoc = readNestedNameSpecifierLoc();
8617   unsigned NumTPLists = readInt();
8618   Info.NumTemplParamLists = NumTPLists;
8619   if (NumTPLists) {
8620     Info.TemplParamLists =
8621         new (getContext()) TemplateParameterList *[NumTPLists];
8622     for (unsigned i = 0; i != NumTPLists; ++i)
8623       Info.TemplParamLists[i] = readTemplateParameterList();
8624   }
8625 }
8626 
8627 TemplateParameterList *
8628 ASTRecordReader::readTemplateParameterList() {
8629   SourceLocation TemplateLoc = readSourceLocation();
8630   SourceLocation LAngleLoc = readSourceLocation();
8631   SourceLocation RAngleLoc = readSourceLocation();
8632 
8633   unsigned NumParams = readInt();
8634   SmallVector<NamedDecl *, 16> Params;
8635   Params.reserve(NumParams);
8636   while (NumParams--)
8637     Params.push_back(readDeclAs<NamedDecl>());
8638 
8639   bool HasRequiresClause = readBool();
8640   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8641 
8642   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8643       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8644   return TemplateParams;
8645 }
8646 
8647 void ASTRecordReader::readTemplateArgumentList(
8648                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8649                         bool Canonicalize) {
8650   unsigned NumTemplateArgs = readInt();
8651   TemplArgs.reserve(NumTemplateArgs);
8652   while (NumTemplateArgs--)
8653     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8654 }
8655 
8656 /// Read a UnresolvedSet structure.
8657 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8658   unsigned NumDecls = readInt();
8659   Set.reserve(getContext(), NumDecls);
8660   while (NumDecls--) {
8661     DeclID ID = readDeclID();
8662     AccessSpecifier AS = (AccessSpecifier) readInt();
8663     Set.addLazyDecl(getContext(), ID, AS);
8664   }
8665 }
8666 
8667 CXXBaseSpecifier
8668 ASTRecordReader::readCXXBaseSpecifier() {
8669   bool isVirtual = readBool();
8670   bool isBaseOfClass = readBool();
8671   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8672   bool inheritConstructors = readBool();
8673   TypeSourceInfo *TInfo = readTypeSourceInfo();
8674   SourceRange Range = readSourceRange();
8675   SourceLocation EllipsisLoc = readSourceLocation();
8676   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8677                           EllipsisLoc);
8678   Result.setInheritConstructors(inheritConstructors);
8679   return Result;
8680 }
8681 
8682 CXXCtorInitializer **
8683 ASTRecordReader::readCXXCtorInitializers() {
8684   ASTContext &Context = getContext();
8685   unsigned NumInitializers = readInt();
8686   assert(NumInitializers && "wrote ctor initializers but have no inits");
8687   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8688   for (unsigned i = 0; i != NumInitializers; ++i) {
8689     TypeSourceInfo *TInfo = nullptr;
8690     bool IsBaseVirtual = false;
8691     FieldDecl *Member = nullptr;
8692     IndirectFieldDecl *IndirectMember = nullptr;
8693 
8694     CtorInitializerType Type = (CtorInitializerType) readInt();
8695     switch (Type) {
8696     case CTOR_INITIALIZER_BASE:
8697       TInfo = readTypeSourceInfo();
8698       IsBaseVirtual = readBool();
8699       break;
8700 
8701     case CTOR_INITIALIZER_DELEGATING:
8702       TInfo = readTypeSourceInfo();
8703       break;
8704 
8705      case CTOR_INITIALIZER_MEMBER:
8706       Member = readDeclAs<FieldDecl>();
8707       break;
8708 
8709      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8710       IndirectMember = readDeclAs<IndirectFieldDecl>();
8711       break;
8712     }
8713 
8714     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8715     Expr *Init = readExpr();
8716     SourceLocation LParenLoc = readSourceLocation();
8717     SourceLocation RParenLoc = readSourceLocation();
8718 
8719     CXXCtorInitializer *BOMInit;
8720     if (Type == CTOR_INITIALIZER_BASE)
8721       BOMInit = new (Context)
8722           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8723                              RParenLoc, MemberOrEllipsisLoc);
8724     else if (Type == CTOR_INITIALIZER_DELEGATING)
8725       BOMInit = new (Context)
8726           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8727     else if (Member)
8728       BOMInit = new (Context)
8729           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8730                              Init, RParenLoc);
8731     else
8732       BOMInit = new (Context)
8733           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8734                              LParenLoc, Init, RParenLoc);
8735 
8736     if (/*IsWritten*/readBool()) {
8737       unsigned SourceOrder = readInt();
8738       BOMInit->setSourceOrder(SourceOrder);
8739     }
8740 
8741     CtorInitializers[i] = BOMInit;
8742   }
8743 
8744   return CtorInitializers;
8745 }
8746 
8747 NestedNameSpecifierLoc
8748 ASTRecordReader::readNestedNameSpecifierLoc() {
8749   ASTContext &Context = getContext();
8750   unsigned N = readInt();
8751   NestedNameSpecifierLocBuilder Builder;
8752   for (unsigned I = 0; I != N; ++I) {
8753     auto Kind = readNestedNameSpecifierKind();
8754     switch (Kind) {
8755     case NestedNameSpecifier::Identifier: {
8756       IdentifierInfo *II = readIdentifier();
8757       SourceRange Range = readSourceRange();
8758       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8759       break;
8760     }
8761 
8762     case NestedNameSpecifier::Namespace: {
8763       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8764       SourceRange Range = readSourceRange();
8765       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8766       break;
8767     }
8768 
8769     case NestedNameSpecifier::NamespaceAlias: {
8770       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8771       SourceRange Range = readSourceRange();
8772       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8773       break;
8774     }
8775 
8776     case NestedNameSpecifier::TypeSpec:
8777     case NestedNameSpecifier::TypeSpecWithTemplate: {
8778       bool Template = readBool();
8779       TypeSourceInfo *T = readTypeSourceInfo();
8780       if (!T)
8781         return NestedNameSpecifierLoc();
8782       SourceLocation ColonColonLoc = readSourceLocation();
8783 
8784       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8785       Builder.Extend(Context,
8786                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8787                      T->getTypeLoc(), ColonColonLoc);
8788       break;
8789     }
8790 
8791     case NestedNameSpecifier::Global: {
8792       SourceLocation ColonColonLoc = readSourceLocation();
8793       Builder.MakeGlobal(Context, ColonColonLoc);
8794       break;
8795     }
8796 
8797     case NestedNameSpecifier::Super: {
8798       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8799       SourceRange Range = readSourceRange();
8800       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8801       break;
8802     }
8803     }
8804   }
8805 
8806   return Builder.getWithLocInContext(Context);
8807 }
8808 
8809 SourceRange
8810 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8811                            unsigned &Idx) {
8812   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8813   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8814   return SourceRange(beg, end);
8815 }
8816 
8817 static FixedPointSemantics
8818 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8819                         unsigned &Idx) {
8820   unsigned Width = Record[Idx++];
8821   unsigned Scale = Record[Idx++];
8822   uint64_t Tmp = Record[Idx++];
8823   bool IsSigned = Tmp & 0x1;
8824   bool IsSaturated = Tmp & 0x2;
8825   bool HasUnsignedPadding = Tmp & 0x4;
8826   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8827                              HasUnsignedPadding);
8828 }
8829 
8830 static const llvm::fltSemantics &
8831 readAPFloatSemantics(ASTRecordReader &reader) {
8832   return llvm::APFloatBase::EnumToSemantics(
8833     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8834 }
8835 
8836 APValue ASTRecordReader::readAPValue() {
8837   unsigned Kind = readInt();
8838   switch ((APValue::ValueKind) Kind) {
8839   case APValue::None:
8840     return APValue();
8841   case APValue::Indeterminate:
8842     return APValue::IndeterminateValue();
8843   case APValue::Int:
8844     return APValue(readAPSInt());
8845   case APValue::Float: {
8846     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8847     return APValue(readAPFloat(FloatSema));
8848   }
8849   case APValue::FixedPoint: {
8850     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8851     return APValue(APFixedPoint(readAPInt(), FPSema));
8852   }
8853   case APValue::ComplexInt: {
8854     llvm::APSInt First = readAPSInt();
8855     return APValue(std::move(First), readAPSInt());
8856   }
8857   case APValue::ComplexFloat: {
8858     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8859     llvm::APFloat First = readAPFloat(FloatSema1);
8860     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8861     return APValue(std::move(First), readAPFloat(FloatSema2));
8862   }
8863   case APValue::LValue:
8864   case APValue::Vector:
8865   case APValue::Array:
8866   case APValue::Struct:
8867   case APValue::Union:
8868   case APValue::MemberPointer:
8869   case APValue::AddrLabelDiff:
8870     // TODO : Handle all these APValue::ValueKind.
8871     return APValue();
8872   }
8873   llvm_unreachable("Invalid APValue::ValueKind");
8874 }
8875 
8876 /// Read a floating-point value
8877 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8878   return llvm::APFloat(Sem, readAPInt());
8879 }
8880 
8881 // Read a string
8882 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8883   unsigned Len = Record[Idx++];
8884   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8885   Idx += Len;
8886   return Result;
8887 }
8888 
8889 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8890                                 unsigned &Idx) {
8891   std::string Filename = ReadString(Record, Idx);
8892   ResolveImportedPath(F, Filename);
8893   return Filename;
8894 }
8895 
8896 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8897                                 const RecordData &Record, unsigned &Idx) {
8898   std::string Filename = ReadString(Record, Idx);
8899   if (!BaseDirectory.empty())
8900     ResolveImportedPath(Filename, BaseDirectory);
8901   return Filename;
8902 }
8903 
8904 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8905                                          unsigned &Idx) {
8906   unsigned Major = Record[Idx++];
8907   unsigned Minor = Record[Idx++];
8908   unsigned Subminor = Record[Idx++];
8909   if (Minor == 0)
8910     return VersionTuple(Major);
8911   if (Subminor == 0)
8912     return VersionTuple(Major, Minor - 1);
8913   return VersionTuple(Major, Minor - 1, Subminor - 1);
8914 }
8915 
8916 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8917                                           const RecordData &Record,
8918                                           unsigned &Idx) {
8919   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8920   return CXXTemporary::Create(getContext(), Decl);
8921 }
8922 
8923 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
8924   return Diag(CurrentImportLoc, DiagID);
8925 }
8926 
8927 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
8928   return Diags.Report(Loc, DiagID);
8929 }
8930 
8931 /// Retrieve the identifier table associated with the
8932 /// preprocessor.
8933 IdentifierTable &ASTReader::getIdentifierTable() {
8934   return PP.getIdentifierTable();
8935 }
8936 
8937 /// Record that the given ID maps to the given switch-case
8938 /// statement.
8939 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8940   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8941          "Already have a SwitchCase with this ID");
8942   (*CurrSwitchCaseStmts)[ID] = SC;
8943 }
8944 
8945 /// Retrieve the switch-case statement with the given ID.
8946 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8947   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8948   return (*CurrSwitchCaseStmts)[ID];
8949 }
8950 
8951 void ASTReader::ClearSwitchCaseIDs() {
8952   CurrSwitchCaseStmts->clear();
8953 }
8954 
8955 void ASTReader::ReadComments() {
8956   ASTContext &Context = getContext();
8957   std::vector<RawComment *> Comments;
8958   for (SmallVectorImpl<std::pair<BitstreamCursor,
8959                                  serialization::ModuleFile *>>::iterator
8960        I = CommentsCursors.begin(),
8961        E = CommentsCursors.end();
8962        I != E; ++I) {
8963     Comments.clear();
8964     BitstreamCursor &Cursor = I->first;
8965     serialization::ModuleFile &F = *I->second;
8966     SavedStreamPosition SavedPosition(Cursor);
8967 
8968     RecordData Record;
8969     while (true) {
8970       Expected<llvm::BitstreamEntry> MaybeEntry =
8971           Cursor.advanceSkippingSubblocks(
8972               BitstreamCursor::AF_DontPopBlockAtEnd);
8973       if (!MaybeEntry) {
8974         Error(MaybeEntry.takeError());
8975         return;
8976       }
8977       llvm::BitstreamEntry Entry = MaybeEntry.get();
8978 
8979       switch (Entry.Kind) {
8980       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8981       case llvm::BitstreamEntry::Error:
8982         Error("malformed block record in AST file");
8983         return;
8984       case llvm::BitstreamEntry::EndBlock:
8985         goto NextCursor;
8986       case llvm::BitstreamEntry::Record:
8987         // The interesting case.
8988         break;
8989       }
8990 
8991       // Read a record.
8992       Record.clear();
8993       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
8994       if (!MaybeComment) {
8995         Error(MaybeComment.takeError());
8996         return;
8997       }
8998       switch ((CommentRecordTypes)MaybeComment.get()) {
8999       case COMMENTS_RAW_COMMENT: {
9000         unsigned Idx = 0;
9001         SourceRange SR = ReadSourceRange(F, Record, Idx);
9002         RawComment::CommentKind Kind =
9003             (RawComment::CommentKind) Record[Idx++];
9004         bool IsTrailingComment = Record[Idx++];
9005         bool IsAlmostTrailingComment = Record[Idx++];
9006         Comments.push_back(new (Context) RawComment(
9007             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9008         break;
9009       }
9010       }
9011     }
9012   NextCursor:
9013     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9014         FileToOffsetToComment;
9015     for (RawComment *C : Comments) {
9016       SourceLocation CommentLoc = C->getBeginLoc();
9017       if (CommentLoc.isValid()) {
9018         std::pair<FileID, unsigned> Loc =
9019             SourceMgr.getDecomposedLoc(CommentLoc);
9020         if (Loc.first.isValid())
9021           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9022       }
9023     }
9024   }
9025 }
9026 
9027 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9028                                 bool IncludeSystem, bool Complain,
9029                     llvm::function_ref<void(const serialization::InputFile &IF,
9030                                             bool isSystem)> Visitor) {
9031   unsigned NumUserInputs = MF.NumUserInputFiles;
9032   unsigned NumInputs = MF.InputFilesLoaded.size();
9033   assert(NumUserInputs <= NumInputs);
9034   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9035   for (unsigned I = 0; I < N; ++I) {
9036     bool IsSystem = I >= NumUserInputs;
9037     InputFile IF = getInputFile(MF, I+1, Complain);
9038     Visitor(IF, IsSystem);
9039   }
9040 }
9041 
9042 void ASTReader::visitTopLevelModuleMaps(
9043     serialization::ModuleFile &MF,
9044     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9045   unsigned NumInputs = MF.InputFilesLoaded.size();
9046   for (unsigned I = 0; I < NumInputs; ++I) {
9047     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9048     if (IFI.TopLevelModuleMap)
9049       // FIXME: This unnecessarily re-reads the InputFileInfo.
9050       if (auto *FE = getInputFile(MF, I + 1).getFile())
9051         Visitor(FE);
9052   }
9053 }
9054 
9055 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9056   // If we know the owning module, use it.
9057   if (Module *M = D->getImportedOwningModule())
9058     return M->getFullModuleName();
9059 
9060   // Otherwise, use the name of the top-level module the decl is within.
9061   if (ModuleFile *M = getOwningModuleFile(D))
9062     return M->ModuleName;
9063 
9064   // Not from a module.
9065   return {};
9066 }
9067 
9068 void ASTReader::finishPendingActions() {
9069   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9070          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9071          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9072          !PendingUpdateRecords.empty()) {
9073     // If any identifiers with corresponding top-level declarations have
9074     // been loaded, load those declarations now.
9075     using TopLevelDeclsMap =
9076         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9077     TopLevelDeclsMap TopLevelDecls;
9078 
9079     while (!PendingIdentifierInfos.empty()) {
9080       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9081       SmallVector<uint32_t, 4> DeclIDs =
9082           std::move(PendingIdentifierInfos.back().second);
9083       PendingIdentifierInfos.pop_back();
9084 
9085       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9086     }
9087 
9088     // Load each function type that we deferred loading because it was a
9089     // deduced type that might refer to a local type declared within itself.
9090     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9091       auto *FD = PendingFunctionTypes[I].first;
9092       FD->setType(GetType(PendingFunctionTypes[I].second));
9093 
9094       // If we gave a function a deduced return type, remember that we need to
9095       // propagate that along the redeclaration chain.
9096       auto *DT = FD->getReturnType()->getContainedDeducedType();
9097       if (DT && DT->isDeduced())
9098         PendingDeducedTypeUpdates.insert(
9099             {FD->getCanonicalDecl(), FD->getReturnType()});
9100     }
9101     PendingFunctionTypes.clear();
9102 
9103     // For each decl chain that we wanted to complete while deserializing, mark
9104     // it as "still needs to be completed".
9105     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9106       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9107     }
9108     PendingIncompleteDeclChains.clear();
9109 
9110     // Load pending declaration chains.
9111     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9112       loadPendingDeclChain(PendingDeclChains[I].first,
9113                            PendingDeclChains[I].second);
9114     PendingDeclChains.clear();
9115 
9116     // Make the most recent of the top-level declarations visible.
9117     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9118            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9119       IdentifierInfo *II = TLD->first;
9120       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9121         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9122       }
9123     }
9124 
9125     // Load any pending macro definitions.
9126     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9127       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9128       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9129       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9130       // Initialize the macro history from chained-PCHs ahead of module imports.
9131       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9132            ++IDIdx) {
9133         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9134         if (!Info.M->isModule())
9135           resolvePendingMacro(II, Info);
9136       }
9137       // Handle module imports.
9138       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9139            ++IDIdx) {
9140         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9141         if (Info.M->isModule())
9142           resolvePendingMacro(II, Info);
9143       }
9144     }
9145     PendingMacroIDs.clear();
9146 
9147     // Wire up the DeclContexts for Decls that we delayed setting until
9148     // recursive loading is completed.
9149     while (!PendingDeclContextInfos.empty()) {
9150       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9151       PendingDeclContextInfos.pop_front();
9152       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9153       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9154       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9155     }
9156 
9157     // Perform any pending declaration updates.
9158     while (!PendingUpdateRecords.empty()) {
9159       auto Update = PendingUpdateRecords.pop_back_val();
9160       ReadingKindTracker ReadingKind(Read_Decl, *this);
9161       loadDeclUpdateRecords(Update);
9162     }
9163   }
9164 
9165   // At this point, all update records for loaded decls are in place, so any
9166   // fake class definitions should have become real.
9167   assert(PendingFakeDefinitionData.empty() &&
9168          "faked up a class definition but never saw the real one");
9169 
9170   // If we deserialized any C++ or Objective-C class definitions, any
9171   // Objective-C protocol definitions, or any redeclarable templates, make sure
9172   // that all redeclarations point to the definitions. Note that this can only
9173   // happen now, after the redeclaration chains have been fully wired.
9174   for (Decl *D : PendingDefinitions) {
9175     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9176       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9177         // Make sure that the TagType points at the definition.
9178         const_cast<TagType*>(TagT)->decl = TD;
9179       }
9180 
9181       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9182         for (auto *R = getMostRecentExistingDecl(RD); R;
9183              R = R->getPreviousDecl()) {
9184           assert((R == D) ==
9185                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9186                  "declaration thinks it's the definition but it isn't");
9187           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9188         }
9189       }
9190 
9191       continue;
9192     }
9193 
9194     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9195       // Make sure that the ObjCInterfaceType points at the definition.
9196       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9197         ->Decl = ID;
9198 
9199       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9200         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9201 
9202       continue;
9203     }
9204 
9205     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9206       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9207         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9208 
9209       continue;
9210     }
9211 
9212     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9213     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9214       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9215   }
9216   PendingDefinitions.clear();
9217 
9218   // Load the bodies of any functions or methods we've encountered. We do
9219   // this now (delayed) so that we can be sure that the declaration chains
9220   // have been fully wired up (hasBody relies on this).
9221   // FIXME: We shouldn't require complete redeclaration chains here.
9222   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9223                                PBEnd = PendingBodies.end();
9224        PB != PBEnd; ++PB) {
9225     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9226       // For a function defined inline within a class template, force the
9227       // canonical definition to be the one inside the canonical definition of
9228       // the template. This ensures that we instantiate from a correct view
9229       // of the template.
9230       //
9231       // Sadly we can't do this more generally: we can't be sure that all
9232       // copies of an arbitrary class definition will have the same members
9233       // defined (eg, some member functions may not be instantiated, and some
9234       // special members may or may not have been implicitly defined).
9235       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9236         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9237           continue;
9238 
9239       // FIXME: Check for =delete/=default?
9240       // FIXME: Complain about ODR violations here?
9241       const FunctionDecl *Defn = nullptr;
9242       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9243         FD->setLazyBody(PB->second);
9244       } else {
9245         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9246         mergeDefinitionVisibility(NonConstDefn, FD);
9247 
9248         if (!FD->isLateTemplateParsed() &&
9249             !NonConstDefn->isLateTemplateParsed() &&
9250             FD->getODRHash() != NonConstDefn->getODRHash()) {
9251           if (!isa<CXXMethodDecl>(FD)) {
9252             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9253           } else if (FD->getLexicalParent()->isFileContext() &&
9254                      NonConstDefn->getLexicalParent()->isFileContext()) {
9255             // Only diagnose out-of-line method definitions.  If they are
9256             // in class definitions, then an error will be generated when
9257             // processing the class bodies.
9258             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9259           }
9260         }
9261       }
9262       continue;
9263     }
9264 
9265     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9266     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9267       MD->setLazyBody(PB->second);
9268   }
9269   PendingBodies.clear();
9270 
9271   // Do some cleanup.
9272   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9273     getContext().deduplicateMergedDefinitonsFor(ND);
9274   PendingMergedDefinitionsToDeduplicate.clear();
9275 }
9276 
9277 void ASTReader::diagnoseOdrViolations() {
9278   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9279       PendingFunctionOdrMergeFailures.empty() &&
9280       PendingEnumOdrMergeFailures.empty())
9281     return;
9282 
9283   // Trigger the import of the full definition of each class that had any
9284   // odr-merging problems, so we can produce better diagnostics for them.
9285   // These updates may in turn find and diagnose some ODR failures, so take
9286   // ownership of the set first.
9287   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9288   PendingOdrMergeFailures.clear();
9289   for (auto &Merge : OdrMergeFailures) {
9290     Merge.first->buildLookup();
9291     Merge.first->decls_begin();
9292     Merge.first->bases_begin();
9293     Merge.first->vbases_begin();
9294     for (auto &RecordPair : Merge.second) {
9295       auto *RD = RecordPair.first;
9296       RD->decls_begin();
9297       RD->bases_begin();
9298       RD->vbases_begin();
9299     }
9300   }
9301 
9302   // Trigger the import of functions.
9303   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9304   PendingFunctionOdrMergeFailures.clear();
9305   for (auto &Merge : FunctionOdrMergeFailures) {
9306     Merge.first->buildLookup();
9307     Merge.first->decls_begin();
9308     Merge.first->getBody();
9309     for (auto &FD : Merge.second) {
9310       FD->buildLookup();
9311       FD->decls_begin();
9312       FD->getBody();
9313     }
9314   }
9315 
9316   // Trigger the import of enums.
9317   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9318   PendingEnumOdrMergeFailures.clear();
9319   for (auto &Merge : EnumOdrMergeFailures) {
9320     Merge.first->decls_begin();
9321     for (auto &Enum : Merge.second) {
9322       Enum->decls_begin();
9323     }
9324   }
9325 
9326   // For each declaration from a merged context, check that the canonical
9327   // definition of that context also contains a declaration of the same
9328   // entity.
9329   //
9330   // Caution: this loop does things that might invalidate iterators into
9331   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9332   while (!PendingOdrMergeChecks.empty()) {
9333     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9334 
9335     // FIXME: Skip over implicit declarations for now. This matters for things
9336     // like implicitly-declared special member functions. This isn't entirely
9337     // correct; we can end up with multiple unmerged declarations of the same
9338     // implicit entity.
9339     if (D->isImplicit())
9340       continue;
9341 
9342     DeclContext *CanonDef = D->getDeclContext();
9343 
9344     bool Found = false;
9345     const Decl *DCanon = D->getCanonicalDecl();
9346 
9347     for (auto RI : D->redecls()) {
9348       if (RI->getLexicalDeclContext() == CanonDef) {
9349         Found = true;
9350         break;
9351       }
9352     }
9353     if (Found)
9354       continue;
9355 
9356     // Quick check failed, time to do the slow thing. Note, we can't just
9357     // look up the name of D in CanonDef here, because the member that is
9358     // in CanonDef might not be found by name lookup (it might have been
9359     // replaced by a more recent declaration in the lookup table), and we
9360     // can't necessarily find it in the redeclaration chain because it might
9361     // be merely mergeable, not redeclarable.
9362     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9363     for (auto *CanonMember : CanonDef->decls()) {
9364       if (CanonMember->getCanonicalDecl() == DCanon) {
9365         // This can happen if the declaration is merely mergeable and not
9366         // actually redeclarable (we looked for redeclarations earlier).
9367         //
9368         // FIXME: We should be able to detect this more efficiently, without
9369         // pulling in all of the members of CanonDef.
9370         Found = true;
9371         break;
9372       }
9373       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9374         if (ND->getDeclName() == D->getDeclName())
9375           Candidates.push_back(ND);
9376     }
9377 
9378     if (!Found) {
9379       // The AST doesn't like TagDecls becoming invalid after they've been
9380       // completed. We only really need to mark FieldDecls as invalid here.
9381       if (!isa<TagDecl>(D))
9382         D->setInvalidDecl();
9383 
9384       // Ensure we don't accidentally recursively enter deserialization while
9385       // we're producing our diagnostic.
9386       Deserializing RecursionGuard(this);
9387 
9388       std::string CanonDefModule =
9389           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9390       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9391         << D << getOwningModuleNameForDiagnostic(D)
9392         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9393 
9394       if (Candidates.empty())
9395         Diag(cast<Decl>(CanonDef)->getLocation(),
9396              diag::note_module_odr_violation_no_possible_decls) << D;
9397       else {
9398         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9399           Diag(Candidates[I]->getLocation(),
9400                diag::note_module_odr_violation_possible_decl)
9401             << Candidates[I];
9402       }
9403 
9404       DiagnosedOdrMergeFailures.insert(CanonDef);
9405     }
9406   }
9407 
9408   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9409       EnumOdrMergeFailures.empty())
9410     return;
9411 
9412   // Ensure we don't accidentally recursively enter deserialization while
9413   // we're producing our diagnostics.
9414   Deserializing RecursionGuard(this);
9415 
9416   // Common code for hashing helpers.
9417   ODRHash Hash;
9418   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9419     Hash.clear();
9420     Hash.AddQualType(Ty);
9421     return Hash.CalculateHash();
9422   };
9423 
9424   auto ComputeODRHash = [&Hash](const Stmt *S) {
9425     assert(S);
9426     Hash.clear();
9427     Hash.AddStmt(S);
9428     return Hash.CalculateHash();
9429   };
9430 
9431   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9432     assert(D);
9433     Hash.clear();
9434     Hash.AddSubDecl(D);
9435     return Hash.CalculateHash();
9436   };
9437 
9438   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9439     Hash.clear();
9440     Hash.AddTemplateArgument(TA);
9441     return Hash.CalculateHash();
9442   };
9443 
9444   auto ComputeTemplateParameterListODRHash =
9445       [&Hash](const TemplateParameterList *TPL) {
9446         assert(TPL);
9447         Hash.clear();
9448         Hash.AddTemplateParameterList(TPL);
9449         return Hash.CalculateHash();
9450       };
9451 
9452   // Used with err_module_odr_violation_mismatch_decl and
9453   // note_module_odr_violation_mismatch_decl
9454   // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9455   enum ODRMismatchDecl {
9456     EndOfClass,
9457     PublicSpecifer,
9458     PrivateSpecifer,
9459     ProtectedSpecifer,
9460     StaticAssert,
9461     Field,
9462     CXXMethod,
9463     TypeAlias,
9464     TypeDef,
9465     Var,
9466     Friend,
9467     FunctionTemplate,
9468     Other
9469   };
9470 
9471   // Used with err_module_odr_violation_mismatch_decl_diff and
9472   // note_module_odr_violation_mismatch_decl_diff
9473   enum ODRMismatchDeclDifference {
9474     StaticAssertCondition,
9475     StaticAssertMessage,
9476     StaticAssertOnlyMessage,
9477     FieldName,
9478     FieldTypeName,
9479     FieldSingleBitField,
9480     FieldDifferentWidthBitField,
9481     FieldSingleMutable,
9482     FieldSingleInitializer,
9483     FieldDifferentInitializers,
9484     MethodName,
9485     MethodDeleted,
9486     MethodDefaulted,
9487     MethodVirtual,
9488     MethodStatic,
9489     MethodVolatile,
9490     MethodConst,
9491     MethodInline,
9492     MethodNumberParameters,
9493     MethodParameterType,
9494     MethodParameterName,
9495     MethodParameterSingleDefaultArgument,
9496     MethodParameterDifferentDefaultArgument,
9497     MethodNoTemplateArguments,
9498     MethodDifferentNumberTemplateArguments,
9499     MethodDifferentTemplateArgument,
9500     MethodSingleBody,
9501     MethodDifferentBody,
9502     TypedefName,
9503     TypedefType,
9504     VarName,
9505     VarType,
9506     VarSingleInitializer,
9507     VarDifferentInitializer,
9508     VarConstexpr,
9509     FriendTypeFunction,
9510     FriendType,
9511     FriendFunction,
9512     FunctionTemplateDifferentNumberParameters,
9513     FunctionTemplateParameterDifferentKind,
9514     FunctionTemplateParameterName,
9515     FunctionTemplateParameterSingleDefaultArgument,
9516     FunctionTemplateParameterDifferentDefaultArgument,
9517     FunctionTemplateParameterDifferentType,
9518     FunctionTemplatePackParameter,
9519   };
9520 
9521   // These lambdas have the common portions of the ODR diagnostics.  This
9522   // has the same return as Diag(), so addition parameters can be passed
9523   // in with operator<<
9524   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9525                                  SourceLocation Loc, SourceRange Range,
9526                                  ODRMismatchDeclDifference DiffType) {
9527     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9528            << FirstRecord << FirstModule.empty() << FirstModule << Range
9529            << DiffType;
9530   };
9531   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9532                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9533     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9534            << SecondModule << Range << DiffType;
9535   };
9536 
9537   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9538                        &ComputeQualTypeODRHash, &ComputeODRHash](
9539                           NamedDecl *FirstRecord, StringRef FirstModule,
9540                           StringRef SecondModule, FieldDecl *FirstField,
9541                           FieldDecl *SecondField) {
9542     IdentifierInfo *FirstII = FirstField->getIdentifier();
9543     IdentifierInfo *SecondII = SecondField->getIdentifier();
9544     if (FirstII->getName() != SecondII->getName()) {
9545       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9546                        FirstField->getSourceRange(), FieldName)
9547           << FirstII;
9548       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9549                       SecondField->getSourceRange(), FieldName)
9550           << SecondII;
9551 
9552       return true;
9553     }
9554 
9555     assert(getContext().hasSameType(FirstField->getType(),
9556                                     SecondField->getType()));
9557 
9558     QualType FirstType = FirstField->getType();
9559     QualType SecondType = SecondField->getType();
9560     if (ComputeQualTypeODRHash(FirstType) !=
9561         ComputeQualTypeODRHash(SecondType)) {
9562       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9563                        FirstField->getSourceRange(), FieldTypeName)
9564           << FirstII << FirstType;
9565       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9566                       SecondField->getSourceRange(), FieldTypeName)
9567           << SecondII << SecondType;
9568 
9569       return true;
9570     }
9571 
9572     const bool IsFirstBitField = FirstField->isBitField();
9573     const bool IsSecondBitField = SecondField->isBitField();
9574     if (IsFirstBitField != IsSecondBitField) {
9575       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9576                        FirstField->getSourceRange(), FieldSingleBitField)
9577           << FirstII << IsFirstBitField;
9578       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9579                       SecondField->getSourceRange(), FieldSingleBitField)
9580           << SecondII << IsSecondBitField;
9581       return true;
9582     }
9583 
9584     if (IsFirstBitField && IsSecondBitField) {
9585       unsigned FirstBitWidthHash =
9586           ComputeODRHash(FirstField->getBitWidth());
9587       unsigned SecondBitWidthHash =
9588           ComputeODRHash(SecondField->getBitWidth());
9589       if (FirstBitWidthHash != SecondBitWidthHash) {
9590         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9591                          FirstField->getSourceRange(),
9592                          FieldDifferentWidthBitField)
9593             << FirstII << FirstField->getBitWidth()->getSourceRange();
9594         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9595                         SecondField->getSourceRange(),
9596                         FieldDifferentWidthBitField)
9597             << SecondII << SecondField->getBitWidth()->getSourceRange();
9598         return true;
9599       }
9600     }
9601 
9602     if (!PP.getLangOpts().CPlusPlus)
9603       return false;
9604 
9605     const bool IsFirstMutable = FirstField->isMutable();
9606     const bool IsSecondMutable = SecondField->isMutable();
9607     if (IsFirstMutable != IsSecondMutable) {
9608       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9609                        FirstField->getSourceRange(), FieldSingleMutable)
9610           << FirstII << IsFirstMutable;
9611       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9612                       SecondField->getSourceRange(), FieldSingleMutable)
9613           << SecondII << IsSecondMutable;
9614       return true;
9615     }
9616 
9617     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9618     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9619     if ((!FirstInitializer && SecondInitializer) ||
9620         (FirstInitializer && !SecondInitializer)) {
9621       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9622                        FirstField->getSourceRange(), FieldSingleInitializer)
9623           << FirstII << (FirstInitializer != nullptr);
9624       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9625                       SecondField->getSourceRange(), FieldSingleInitializer)
9626           << SecondII << (SecondInitializer != nullptr);
9627       return true;
9628     }
9629 
9630     if (FirstInitializer && SecondInitializer) {
9631       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9632       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9633       if (FirstInitHash != SecondInitHash) {
9634         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9635                          FirstField->getSourceRange(),
9636                          FieldDifferentInitializers)
9637             << FirstII << FirstInitializer->getSourceRange();
9638         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9639                         SecondField->getSourceRange(),
9640                         FieldDifferentInitializers)
9641             << SecondII << SecondInitializer->getSourceRange();
9642         return true;
9643       }
9644     }
9645 
9646     return false;
9647   };
9648 
9649   auto ODRDiagTypeDefOrAlias =
9650       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9651           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9652           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9653           bool IsTypeAlias) {
9654         auto FirstName = FirstTD->getDeclName();
9655         auto SecondName = SecondTD->getDeclName();
9656         if (FirstName != SecondName) {
9657           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9658                            FirstTD->getSourceRange(), TypedefName)
9659               << IsTypeAlias << FirstName;
9660           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9661                           SecondTD->getSourceRange(), TypedefName)
9662               << IsTypeAlias << SecondName;
9663           return true;
9664         }
9665 
9666         QualType FirstType = FirstTD->getUnderlyingType();
9667         QualType SecondType = SecondTD->getUnderlyingType();
9668         if (ComputeQualTypeODRHash(FirstType) !=
9669             ComputeQualTypeODRHash(SecondType)) {
9670           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9671                            FirstTD->getSourceRange(), TypedefType)
9672               << IsTypeAlias << FirstName << FirstType;
9673           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9674                           SecondTD->getSourceRange(), TypedefType)
9675               << IsTypeAlias << SecondName << SecondType;
9676           return true;
9677         }
9678 
9679         return false;
9680   };
9681 
9682   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9683                      &ComputeQualTypeODRHash, &ComputeODRHash,
9684                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9685                            StringRef SecondModule, VarDecl *FirstVD,
9686                            VarDecl *SecondVD) {
9687     auto FirstName = FirstVD->getDeclName();
9688     auto SecondName = SecondVD->getDeclName();
9689     if (FirstName != SecondName) {
9690       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9691                        FirstVD->getSourceRange(), VarName)
9692           << FirstName;
9693       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9694                       SecondVD->getSourceRange(), VarName)
9695           << SecondName;
9696       return true;
9697     }
9698 
9699     QualType FirstType = FirstVD->getType();
9700     QualType SecondType = SecondVD->getType();
9701     if (ComputeQualTypeODRHash(FirstType) !=
9702         ComputeQualTypeODRHash(SecondType)) {
9703       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9704                        FirstVD->getSourceRange(), VarType)
9705           << FirstName << FirstType;
9706       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9707                       SecondVD->getSourceRange(), VarType)
9708           << SecondName << SecondType;
9709       return true;
9710     }
9711 
9712     if (!PP.getLangOpts().CPlusPlus)
9713       return false;
9714 
9715     const Expr *FirstInit = FirstVD->getInit();
9716     const Expr *SecondInit = SecondVD->getInit();
9717     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9718       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9719                        FirstVD->getSourceRange(), VarSingleInitializer)
9720           << FirstName << (FirstInit == nullptr)
9721           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9722       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9723                       SecondVD->getSourceRange(), VarSingleInitializer)
9724           << SecondName << (SecondInit == nullptr)
9725           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9726       return true;
9727     }
9728 
9729     if (FirstInit && SecondInit &&
9730         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9731       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9732                        FirstVD->getSourceRange(), VarDifferentInitializer)
9733           << FirstName << FirstInit->getSourceRange();
9734       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9735                       SecondVD->getSourceRange(), VarDifferentInitializer)
9736           << SecondName << SecondInit->getSourceRange();
9737       return true;
9738     }
9739 
9740     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9741     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9742     if (FirstIsConstexpr != SecondIsConstexpr) {
9743       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9744                        FirstVD->getSourceRange(), VarConstexpr)
9745           << FirstName << FirstIsConstexpr;
9746       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9747                       SecondVD->getSourceRange(), VarConstexpr)
9748           << SecondName << SecondIsConstexpr;
9749       return true;
9750     }
9751     return false;
9752   };
9753 
9754   auto DifferenceSelector = [](Decl *D) {
9755     assert(D && "valid Decl required");
9756     switch (D->getKind()) {
9757     default:
9758       return Other;
9759     case Decl::AccessSpec:
9760       switch (D->getAccess()) {
9761       case AS_public:
9762         return PublicSpecifer;
9763       case AS_private:
9764         return PrivateSpecifer;
9765       case AS_protected:
9766         return ProtectedSpecifer;
9767       case AS_none:
9768         break;
9769       }
9770       llvm_unreachable("Invalid access specifier");
9771     case Decl::StaticAssert:
9772       return StaticAssert;
9773     case Decl::Field:
9774       return Field;
9775     case Decl::CXXMethod:
9776     case Decl::CXXConstructor:
9777     case Decl::CXXDestructor:
9778       return CXXMethod;
9779     case Decl::TypeAlias:
9780       return TypeAlias;
9781     case Decl::Typedef:
9782       return TypeDef;
9783     case Decl::Var:
9784       return Var;
9785     case Decl::Friend:
9786       return Friend;
9787     case Decl::FunctionTemplate:
9788       return FunctionTemplate;
9789     }
9790   };
9791 
9792   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9793   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9794                                                  RecordDecl *Record,
9795                                                  const DeclContext *DC) {
9796     for (auto *D : Record->decls()) {
9797       if (!ODRHash::isWhitelistedDecl(D, DC))
9798         continue;
9799       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9800     }
9801   };
9802 
9803   struct DiffResult {
9804     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9805     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9806   };
9807 
9808   // If there is a diagnoseable difference, FirstDiffType and
9809   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9810   // filled in if not EndOfClass.
9811   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9812                                              DeclHashes &SecondHashes) {
9813     DiffResult DR;
9814     auto FirstIt = FirstHashes.begin();
9815     auto SecondIt = SecondHashes.begin();
9816     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9817       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9818           FirstIt->second == SecondIt->second) {
9819         ++FirstIt;
9820         ++SecondIt;
9821         continue;
9822       }
9823 
9824       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9825       DR.SecondDecl =
9826           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9827 
9828       DR.FirstDiffType =
9829           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9830       DR.SecondDiffType =
9831           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9832       return DR;
9833     }
9834     return DR;
9835   };
9836 
9837   // Use this to diagnose that an unexpected Decl was encountered
9838   // or no difference was detected. This causes a generic error
9839   // message to be emitted.
9840   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9841                                       StringRef FirstModule,
9842                                       NamedDecl *SecondRecord,
9843                                       StringRef SecondModule) {
9844     Diag(FirstRecord->getLocation(),
9845          diag::err_module_odr_violation_different_definitions)
9846         << FirstRecord << FirstModule.empty() << FirstModule;
9847 
9848     if (DR.FirstDecl) {
9849       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9850           << FirstRecord << DR.FirstDecl->getSourceRange();
9851     }
9852 
9853     Diag(SecondRecord->getLocation(),
9854          diag::note_module_odr_violation_different_definitions)
9855         << SecondModule;
9856 
9857     if (DR.SecondDecl) {
9858       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9859           << DR.SecondDecl->getSourceRange();
9860     }
9861   };
9862 
9863   auto DiagnoseODRMismatch =
9864       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9865              NamedDecl *SecondRecord, StringRef SecondModule) {
9866         SourceLocation FirstLoc;
9867         SourceRange FirstRange;
9868         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9869         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9870           FirstLoc = FirstTag->getBraceRange().getEnd();
9871         } else {
9872           FirstLoc = DR.FirstDecl->getLocation();
9873           FirstRange = DR.FirstDecl->getSourceRange();
9874         }
9875         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9876             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9877             << DR.FirstDiffType;
9878 
9879         SourceLocation SecondLoc;
9880         SourceRange SecondRange;
9881         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9882         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9883           SecondLoc = SecondTag->getBraceRange().getEnd();
9884         } else {
9885           SecondLoc = DR.SecondDecl->getLocation();
9886           SecondRange = DR.SecondDecl->getSourceRange();
9887         }
9888         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9889             << SecondModule << SecondRange << DR.SecondDiffType;
9890       };
9891 
9892   // Issue any pending ODR-failure diagnostics.
9893   for (auto &Merge : OdrMergeFailures) {
9894     // If we've already pointed out a specific problem with this class, don't
9895     // bother issuing a general "something's different" diagnostic.
9896     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9897       continue;
9898 
9899     bool Diagnosed = false;
9900     CXXRecordDecl *FirstRecord = Merge.first;
9901     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9902     for (auto &RecordPair : Merge.second) {
9903       CXXRecordDecl *SecondRecord = RecordPair.first;
9904       // Multiple different declarations got merged together; tell the user
9905       // where they came from.
9906       if (FirstRecord == SecondRecord)
9907         continue;
9908 
9909       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9910 
9911       auto *FirstDD = FirstRecord->DefinitionData;
9912       auto *SecondDD = RecordPair.second;
9913 
9914       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9915 
9916       // Diagnostics from DefinitionData are emitted here.
9917       if (FirstDD != SecondDD) {
9918         enum ODRDefinitionDataDifference {
9919           NumBases,
9920           NumVBases,
9921           BaseType,
9922           BaseVirtual,
9923           BaseAccess,
9924         };
9925         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
9926                                  this](SourceLocation Loc, SourceRange Range,
9927                                        ODRDefinitionDataDifference DiffType) {
9928           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9929                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9930                  << DiffType;
9931         };
9932         auto ODRDiagBaseNote = [&SecondModule,
9933                                 this](SourceLocation Loc, SourceRange Range,
9934                                       ODRDefinitionDataDifference DiffType) {
9935           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9936                  << SecondModule << Range << DiffType;
9937         };
9938 
9939         unsigned FirstNumBases = FirstDD->NumBases;
9940         unsigned FirstNumVBases = FirstDD->NumVBases;
9941         unsigned SecondNumBases = SecondDD->NumBases;
9942         unsigned SecondNumVBases = SecondDD->NumVBases;
9943 
9944         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9945           unsigned NumBases = DD->NumBases;
9946           if (NumBases == 0) return SourceRange();
9947           auto bases = DD->bases();
9948           return SourceRange(bases[0].getBeginLoc(),
9949                              bases[NumBases - 1].getEndLoc());
9950         };
9951 
9952         if (FirstNumBases != SecondNumBases) {
9953           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9954                            NumBases)
9955               << FirstNumBases;
9956           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9957                           NumBases)
9958               << SecondNumBases;
9959           Diagnosed = true;
9960           break;
9961         }
9962 
9963         if (FirstNumVBases != SecondNumVBases) {
9964           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9965                            NumVBases)
9966               << FirstNumVBases;
9967           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9968                           NumVBases)
9969               << SecondNumVBases;
9970           Diagnosed = true;
9971           break;
9972         }
9973 
9974         auto FirstBases = FirstDD->bases();
9975         auto SecondBases = SecondDD->bases();
9976         unsigned i = 0;
9977         for (i = 0; i < FirstNumBases; ++i) {
9978           auto FirstBase = FirstBases[i];
9979           auto SecondBase = SecondBases[i];
9980           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9981               ComputeQualTypeODRHash(SecondBase.getType())) {
9982             ODRDiagBaseError(FirstRecord->getLocation(),
9983                              FirstBase.getSourceRange(), BaseType)
9984                 << (i + 1) << FirstBase.getType();
9985             ODRDiagBaseNote(SecondRecord->getLocation(),
9986                             SecondBase.getSourceRange(), BaseType)
9987                 << (i + 1) << SecondBase.getType();
9988             break;
9989           }
9990 
9991           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9992             ODRDiagBaseError(FirstRecord->getLocation(),
9993                              FirstBase.getSourceRange(), BaseVirtual)
9994                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9995             ODRDiagBaseNote(SecondRecord->getLocation(),
9996                             SecondBase.getSourceRange(), BaseVirtual)
9997                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9998             break;
9999           }
10000 
10001           if (FirstBase.getAccessSpecifierAsWritten() !=
10002               SecondBase.getAccessSpecifierAsWritten()) {
10003             ODRDiagBaseError(FirstRecord->getLocation(),
10004                              FirstBase.getSourceRange(), BaseAccess)
10005                 << (i + 1) << FirstBase.getType()
10006                 << (int)FirstBase.getAccessSpecifierAsWritten();
10007             ODRDiagBaseNote(SecondRecord->getLocation(),
10008                             SecondBase.getSourceRange(), BaseAccess)
10009                 << (i + 1) << SecondBase.getType()
10010                 << (int)SecondBase.getAccessSpecifierAsWritten();
10011             break;
10012           }
10013         }
10014 
10015         if (i != FirstNumBases) {
10016           Diagnosed = true;
10017           break;
10018         }
10019       }
10020 
10021       const ClassTemplateDecl *FirstTemplate =
10022           FirstRecord->getDescribedClassTemplate();
10023       const ClassTemplateDecl *SecondTemplate =
10024           SecondRecord->getDescribedClassTemplate();
10025 
10026       assert(!FirstTemplate == !SecondTemplate &&
10027              "Both pointers should be null or non-null");
10028 
10029       enum ODRTemplateDifference {
10030         ParamEmptyName,
10031         ParamName,
10032         ParamSingleDefaultArgument,
10033         ParamDifferentDefaultArgument,
10034       };
10035 
10036       if (FirstTemplate && SecondTemplate) {
10037         DeclHashes FirstTemplateHashes;
10038         DeclHashes SecondTemplateHashes;
10039 
10040         auto PopulateTemplateParameterHashs =
10041             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10042                                      const ClassTemplateDecl *TD) {
10043               for (auto *D : TD->getTemplateParameters()->asArray()) {
10044                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10045               }
10046             };
10047 
10048         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10049         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10050 
10051         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10052                "Number of template parameters should be equal.");
10053 
10054         auto FirstIt = FirstTemplateHashes.begin();
10055         auto FirstEnd = FirstTemplateHashes.end();
10056         auto SecondIt = SecondTemplateHashes.begin();
10057         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10058           if (FirstIt->second == SecondIt->second)
10059             continue;
10060 
10061           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10062                                           SourceLocation Loc, SourceRange Range,
10063                                           ODRTemplateDifference DiffType) {
10064             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10065                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10066                    << DiffType;
10067           };
10068           auto ODRDiagTemplateNote = [&SecondModule, this](
10069                                          SourceLocation Loc, SourceRange Range,
10070                                          ODRTemplateDifference DiffType) {
10071             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10072                    << SecondModule << Range << DiffType;
10073           };
10074 
10075           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10076           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10077 
10078           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10079                  "Parameter Decl's should be the same kind.");
10080 
10081           DeclarationName FirstName = FirstDecl->getDeclName();
10082           DeclarationName SecondName = SecondDecl->getDeclName();
10083 
10084           if (FirstName != SecondName) {
10085             const bool FirstNameEmpty =
10086                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10087             const bool SecondNameEmpty =
10088                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10089             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10090                    "Both template parameters cannot be unnamed.");
10091             ODRDiagTemplateError(FirstDecl->getLocation(),
10092                                  FirstDecl->getSourceRange(),
10093                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10094                 << FirstName;
10095             ODRDiagTemplateNote(SecondDecl->getLocation(),
10096                                 SecondDecl->getSourceRange(),
10097                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10098                 << SecondName;
10099             break;
10100           }
10101 
10102           switch (FirstDecl->getKind()) {
10103           default:
10104             llvm_unreachable("Invalid template parameter type.");
10105           case Decl::TemplateTypeParm: {
10106             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10107             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10108             const bool HasFirstDefaultArgument =
10109                 FirstParam->hasDefaultArgument() &&
10110                 !FirstParam->defaultArgumentWasInherited();
10111             const bool HasSecondDefaultArgument =
10112                 SecondParam->hasDefaultArgument() &&
10113                 !SecondParam->defaultArgumentWasInherited();
10114 
10115             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10116               ODRDiagTemplateError(FirstDecl->getLocation(),
10117                                    FirstDecl->getSourceRange(),
10118                                    ParamSingleDefaultArgument)
10119                   << HasFirstDefaultArgument;
10120               ODRDiagTemplateNote(SecondDecl->getLocation(),
10121                                   SecondDecl->getSourceRange(),
10122                                   ParamSingleDefaultArgument)
10123                   << HasSecondDefaultArgument;
10124               break;
10125             }
10126 
10127             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10128                    "Expecting default arguments.");
10129 
10130             ODRDiagTemplateError(FirstDecl->getLocation(),
10131                                  FirstDecl->getSourceRange(),
10132                                  ParamDifferentDefaultArgument);
10133             ODRDiagTemplateNote(SecondDecl->getLocation(),
10134                                 SecondDecl->getSourceRange(),
10135                                 ParamDifferentDefaultArgument);
10136 
10137             break;
10138           }
10139           case Decl::NonTypeTemplateParm: {
10140             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10141             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10142             const bool HasFirstDefaultArgument =
10143                 FirstParam->hasDefaultArgument() &&
10144                 !FirstParam->defaultArgumentWasInherited();
10145             const bool HasSecondDefaultArgument =
10146                 SecondParam->hasDefaultArgument() &&
10147                 !SecondParam->defaultArgumentWasInherited();
10148 
10149             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10150               ODRDiagTemplateError(FirstDecl->getLocation(),
10151                                    FirstDecl->getSourceRange(),
10152                                    ParamSingleDefaultArgument)
10153                   << HasFirstDefaultArgument;
10154               ODRDiagTemplateNote(SecondDecl->getLocation(),
10155                                   SecondDecl->getSourceRange(),
10156                                   ParamSingleDefaultArgument)
10157                   << HasSecondDefaultArgument;
10158               break;
10159             }
10160 
10161             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10162                    "Expecting default arguments.");
10163 
10164             ODRDiagTemplateError(FirstDecl->getLocation(),
10165                                  FirstDecl->getSourceRange(),
10166                                  ParamDifferentDefaultArgument);
10167             ODRDiagTemplateNote(SecondDecl->getLocation(),
10168                                 SecondDecl->getSourceRange(),
10169                                 ParamDifferentDefaultArgument);
10170 
10171             break;
10172           }
10173           case Decl::TemplateTemplateParm: {
10174             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10175             const auto *SecondParam =
10176                 cast<TemplateTemplateParmDecl>(SecondDecl);
10177             const bool HasFirstDefaultArgument =
10178                 FirstParam->hasDefaultArgument() &&
10179                 !FirstParam->defaultArgumentWasInherited();
10180             const bool HasSecondDefaultArgument =
10181                 SecondParam->hasDefaultArgument() &&
10182                 !SecondParam->defaultArgumentWasInherited();
10183 
10184             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10185               ODRDiagTemplateError(FirstDecl->getLocation(),
10186                                    FirstDecl->getSourceRange(),
10187                                    ParamSingleDefaultArgument)
10188                   << HasFirstDefaultArgument;
10189               ODRDiagTemplateNote(SecondDecl->getLocation(),
10190                                   SecondDecl->getSourceRange(),
10191                                   ParamSingleDefaultArgument)
10192                   << HasSecondDefaultArgument;
10193               break;
10194             }
10195 
10196             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10197                    "Expecting default arguments.");
10198 
10199             ODRDiagTemplateError(FirstDecl->getLocation(),
10200                                  FirstDecl->getSourceRange(),
10201                                  ParamDifferentDefaultArgument);
10202             ODRDiagTemplateNote(SecondDecl->getLocation(),
10203                                 SecondDecl->getSourceRange(),
10204                                 ParamDifferentDefaultArgument);
10205 
10206             break;
10207           }
10208           }
10209 
10210           break;
10211         }
10212 
10213         if (FirstIt != FirstEnd) {
10214           Diagnosed = true;
10215           break;
10216         }
10217       }
10218 
10219       DeclHashes FirstHashes;
10220       DeclHashes SecondHashes;
10221       const DeclContext *DC = FirstRecord;
10222       PopulateHashes(FirstHashes, FirstRecord, DC);
10223       PopulateHashes(SecondHashes, SecondRecord, DC);
10224 
10225       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10226       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10227       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10228       Decl *FirstDecl = DR.FirstDecl;
10229       Decl *SecondDecl = DR.SecondDecl;
10230 
10231       if (FirstDiffType == Other || SecondDiffType == Other) {
10232         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10233                               SecondModule);
10234         Diagnosed = true;
10235         break;
10236       }
10237 
10238       if (FirstDiffType != SecondDiffType) {
10239         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10240                             SecondModule);
10241         Diagnosed = true;
10242         break;
10243       }
10244 
10245       assert(FirstDiffType == SecondDiffType);
10246 
10247       switch (FirstDiffType) {
10248       case Other:
10249       case EndOfClass:
10250       case PublicSpecifer:
10251       case PrivateSpecifer:
10252       case ProtectedSpecifer:
10253         llvm_unreachable("Invalid diff type");
10254 
10255       case StaticAssert: {
10256         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10257         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10258 
10259         Expr *FirstExpr = FirstSA->getAssertExpr();
10260         Expr *SecondExpr = SecondSA->getAssertExpr();
10261         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10262         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10263         if (FirstODRHash != SecondODRHash) {
10264           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10265                            FirstExpr->getSourceRange(), StaticAssertCondition);
10266           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10267                           SecondExpr->getSourceRange(), StaticAssertCondition);
10268           Diagnosed = true;
10269           break;
10270         }
10271 
10272         StringLiteral *FirstStr = FirstSA->getMessage();
10273         StringLiteral *SecondStr = SecondSA->getMessage();
10274         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10275         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10276           SourceLocation FirstLoc, SecondLoc;
10277           SourceRange FirstRange, SecondRange;
10278           if (FirstStr) {
10279             FirstLoc = FirstStr->getBeginLoc();
10280             FirstRange = FirstStr->getSourceRange();
10281           } else {
10282             FirstLoc = FirstSA->getBeginLoc();
10283             FirstRange = FirstSA->getSourceRange();
10284           }
10285           if (SecondStr) {
10286             SecondLoc = SecondStr->getBeginLoc();
10287             SecondRange = SecondStr->getSourceRange();
10288           } else {
10289             SecondLoc = SecondSA->getBeginLoc();
10290             SecondRange = SecondSA->getSourceRange();
10291           }
10292           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10293                            StaticAssertOnlyMessage)
10294               << (FirstStr == nullptr);
10295           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10296                           StaticAssertOnlyMessage)
10297               << (SecondStr == nullptr);
10298           Diagnosed = true;
10299           break;
10300         }
10301 
10302         if (FirstStr && SecondStr &&
10303             FirstStr->getString() != SecondStr->getString()) {
10304           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10305                            FirstStr->getSourceRange(), StaticAssertMessage);
10306           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10307                           SecondStr->getSourceRange(), StaticAssertMessage);
10308           Diagnosed = true;
10309           break;
10310         }
10311         break;
10312       }
10313       case Field: {
10314         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10315                                  cast<FieldDecl>(FirstDecl),
10316                                  cast<FieldDecl>(SecondDecl));
10317         break;
10318       }
10319       case CXXMethod: {
10320         enum {
10321           DiagMethod,
10322           DiagConstructor,
10323           DiagDestructor,
10324         } FirstMethodType,
10325             SecondMethodType;
10326         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10327           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10328           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10329           return DiagMethod;
10330         };
10331         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10332         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10333         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10334         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10335         auto FirstName = FirstMethod->getDeclName();
10336         auto SecondName = SecondMethod->getDeclName();
10337         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10338           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10339                            FirstMethod->getSourceRange(), MethodName)
10340               << FirstMethodType << FirstName;
10341           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10342                           SecondMethod->getSourceRange(), MethodName)
10343               << SecondMethodType << SecondName;
10344 
10345           Diagnosed = true;
10346           break;
10347         }
10348 
10349         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10350         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10351         if (FirstDeleted != SecondDeleted) {
10352           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10353                            FirstMethod->getSourceRange(), MethodDeleted)
10354               << FirstMethodType << FirstName << FirstDeleted;
10355 
10356           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10357                           SecondMethod->getSourceRange(), MethodDeleted)
10358               << SecondMethodType << SecondName << SecondDeleted;
10359           Diagnosed = true;
10360           break;
10361         }
10362 
10363         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10364         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10365         if (FirstDefaulted != SecondDefaulted) {
10366           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10367                            FirstMethod->getSourceRange(), MethodDefaulted)
10368               << FirstMethodType << FirstName << FirstDefaulted;
10369 
10370           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10371                           SecondMethod->getSourceRange(), MethodDefaulted)
10372               << SecondMethodType << SecondName << SecondDefaulted;
10373           Diagnosed = true;
10374           break;
10375         }
10376 
10377         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10378         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10379         const bool FirstPure = FirstMethod->isPure();
10380         const bool SecondPure = SecondMethod->isPure();
10381         if ((FirstVirtual || SecondVirtual) &&
10382             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10383           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10384                            FirstMethod->getSourceRange(), MethodVirtual)
10385               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10386           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10387                           SecondMethod->getSourceRange(), MethodVirtual)
10388               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10389           Diagnosed = true;
10390           break;
10391         }
10392 
10393         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10394         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10395         // class needs to be checked instead.
10396         const auto FirstStorage = FirstMethod->getStorageClass();
10397         const auto SecondStorage = SecondMethod->getStorageClass();
10398         const bool FirstStatic = FirstStorage == SC_Static;
10399         const bool SecondStatic = SecondStorage == SC_Static;
10400         if (FirstStatic != SecondStatic) {
10401           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10402                            FirstMethod->getSourceRange(), MethodStatic)
10403               << FirstMethodType << FirstName << FirstStatic;
10404           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10405                           SecondMethod->getSourceRange(), MethodStatic)
10406               << SecondMethodType << SecondName << SecondStatic;
10407           Diagnosed = true;
10408           break;
10409         }
10410 
10411         const bool FirstVolatile = FirstMethod->isVolatile();
10412         const bool SecondVolatile = SecondMethod->isVolatile();
10413         if (FirstVolatile != SecondVolatile) {
10414           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10415                            FirstMethod->getSourceRange(), MethodVolatile)
10416               << FirstMethodType << FirstName << FirstVolatile;
10417           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10418                           SecondMethod->getSourceRange(), MethodVolatile)
10419               << SecondMethodType << SecondName << SecondVolatile;
10420           Diagnosed = true;
10421           break;
10422         }
10423 
10424         const bool FirstConst = FirstMethod->isConst();
10425         const bool SecondConst = SecondMethod->isConst();
10426         if (FirstConst != SecondConst) {
10427           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10428                            FirstMethod->getSourceRange(), MethodConst)
10429               << FirstMethodType << FirstName << FirstConst;
10430           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10431                           SecondMethod->getSourceRange(), MethodConst)
10432               << SecondMethodType << SecondName << SecondConst;
10433           Diagnosed = true;
10434           break;
10435         }
10436 
10437         const bool FirstInline = FirstMethod->isInlineSpecified();
10438         const bool SecondInline = SecondMethod->isInlineSpecified();
10439         if (FirstInline != SecondInline) {
10440           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10441                            FirstMethod->getSourceRange(), MethodInline)
10442               << FirstMethodType << FirstName << FirstInline;
10443           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10444                           SecondMethod->getSourceRange(), MethodInline)
10445               << SecondMethodType << SecondName << SecondInline;
10446           Diagnosed = true;
10447           break;
10448         }
10449 
10450         const unsigned FirstNumParameters = FirstMethod->param_size();
10451         const unsigned SecondNumParameters = SecondMethod->param_size();
10452         if (FirstNumParameters != SecondNumParameters) {
10453           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10454                            FirstMethod->getSourceRange(),
10455                            MethodNumberParameters)
10456               << FirstMethodType << FirstName << FirstNumParameters;
10457           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10458                           SecondMethod->getSourceRange(),
10459                           MethodNumberParameters)
10460               << SecondMethodType << SecondName << SecondNumParameters;
10461           Diagnosed = true;
10462           break;
10463         }
10464 
10465         // Need this status boolean to know when break out of the switch.
10466         bool ParameterMismatch = false;
10467         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10468           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10469           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10470 
10471           QualType FirstParamType = FirstParam->getType();
10472           QualType SecondParamType = SecondParam->getType();
10473           if (FirstParamType != SecondParamType &&
10474               ComputeQualTypeODRHash(FirstParamType) !=
10475                   ComputeQualTypeODRHash(SecondParamType)) {
10476             if (const DecayedType *ParamDecayedType =
10477                     FirstParamType->getAs<DecayedType>()) {
10478               ODRDiagDeclError(
10479                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10480                   FirstMethod->getSourceRange(), MethodParameterType)
10481                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10482                   << true << ParamDecayedType->getOriginalType();
10483             } else {
10484               ODRDiagDeclError(
10485                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10486                   FirstMethod->getSourceRange(), MethodParameterType)
10487                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10488                   << false;
10489             }
10490 
10491             if (const DecayedType *ParamDecayedType =
10492                     SecondParamType->getAs<DecayedType>()) {
10493               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10494                               SecondMethod->getSourceRange(),
10495                               MethodParameterType)
10496                   << SecondMethodType << SecondName << (I + 1)
10497                   << SecondParamType << true
10498                   << ParamDecayedType->getOriginalType();
10499             } else {
10500               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10501                               SecondMethod->getSourceRange(),
10502                               MethodParameterType)
10503                   << SecondMethodType << SecondName << (I + 1)
10504                   << SecondParamType << false;
10505             }
10506             ParameterMismatch = true;
10507             break;
10508           }
10509 
10510           DeclarationName FirstParamName = FirstParam->getDeclName();
10511           DeclarationName SecondParamName = SecondParam->getDeclName();
10512           if (FirstParamName != SecondParamName) {
10513             ODRDiagDeclError(FirstRecord, FirstModule,
10514                              FirstMethod->getLocation(),
10515                              FirstMethod->getSourceRange(), MethodParameterName)
10516                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10517             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10518                             SecondMethod->getSourceRange(), MethodParameterName)
10519                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10520             ParameterMismatch = true;
10521             break;
10522           }
10523 
10524           const Expr *FirstInit = FirstParam->getInit();
10525           const Expr *SecondInit = SecondParam->getInit();
10526           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10527             ODRDiagDeclError(FirstRecord, FirstModule,
10528                              FirstMethod->getLocation(),
10529                              FirstMethod->getSourceRange(),
10530                              MethodParameterSingleDefaultArgument)
10531                 << FirstMethodType << FirstName << (I + 1)
10532                 << (FirstInit == nullptr)
10533                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10534             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10535                             SecondMethod->getSourceRange(),
10536                             MethodParameterSingleDefaultArgument)
10537                 << SecondMethodType << SecondName << (I + 1)
10538                 << (SecondInit == nullptr)
10539                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10540             ParameterMismatch = true;
10541             break;
10542           }
10543 
10544           if (FirstInit && SecondInit &&
10545               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10546             ODRDiagDeclError(FirstRecord, FirstModule,
10547                              FirstMethod->getLocation(),
10548                              FirstMethod->getSourceRange(),
10549                              MethodParameterDifferentDefaultArgument)
10550                 << FirstMethodType << FirstName << (I + 1)
10551                 << FirstInit->getSourceRange();
10552             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10553                             SecondMethod->getSourceRange(),
10554                             MethodParameterDifferentDefaultArgument)
10555                 << SecondMethodType << SecondName << (I + 1)
10556                 << SecondInit->getSourceRange();
10557             ParameterMismatch = true;
10558             break;
10559 
10560           }
10561         }
10562 
10563         if (ParameterMismatch) {
10564           Diagnosed = true;
10565           break;
10566         }
10567 
10568         const auto *FirstTemplateArgs =
10569             FirstMethod->getTemplateSpecializationArgs();
10570         const auto *SecondTemplateArgs =
10571             SecondMethod->getTemplateSpecializationArgs();
10572 
10573         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10574             (!FirstTemplateArgs && SecondTemplateArgs)) {
10575           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10576                            FirstMethod->getSourceRange(),
10577                            MethodNoTemplateArguments)
10578               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10579           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10580                           SecondMethod->getSourceRange(),
10581                           MethodNoTemplateArguments)
10582               << SecondMethodType << SecondName
10583               << (SecondTemplateArgs != nullptr);
10584 
10585           Diagnosed = true;
10586           break;
10587         }
10588 
10589         if (FirstTemplateArgs && SecondTemplateArgs) {
10590           // Remove pack expansions from argument list.
10591           auto ExpandTemplateArgumentList =
10592               [](const TemplateArgumentList *TAL) {
10593                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10594                 for (const TemplateArgument &TA : TAL->asArray()) {
10595                   if (TA.getKind() != TemplateArgument::Pack) {
10596                     ExpandedList.push_back(&TA);
10597                     continue;
10598                   }
10599                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10600                     ExpandedList.push_back(&PackTA);
10601                   }
10602                 }
10603                 return ExpandedList;
10604               };
10605           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10606               ExpandTemplateArgumentList(FirstTemplateArgs);
10607           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10608               ExpandTemplateArgumentList(SecondTemplateArgs);
10609 
10610           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10611             ODRDiagDeclError(FirstRecord, FirstModule,
10612                              FirstMethod->getLocation(),
10613                              FirstMethod->getSourceRange(),
10614                              MethodDifferentNumberTemplateArguments)
10615                 << FirstMethodType << FirstName
10616                 << (unsigned)FirstExpandedList.size();
10617             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10618                             SecondMethod->getSourceRange(),
10619                             MethodDifferentNumberTemplateArguments)
10620                 << SecondMethodType << SecondName
10621                 << (unsigned)SecondExpandedList.size();
10622 
10623             Diagnosed = true;
10624             break;
10625           }
10626 
10627           bool TemplateArgumentMismatch = false;
10628           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10629             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10630                                    &SecondTA = *SecondExpandedList[i];
10631             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10632                 ComputeTemplateArgumentODRHash(SecondTA)) {
10633               continue;
10634             }
10635 
10636             ODRDiagDeclError(
10637                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10638                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10639                 << FirstMethodType << FirstName << FirstTA << i + 1;
10640             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10641                             SecondMethod->getSourceRange(),
10642                             MethodDifferentTemplateArgument)
10643                 << SecondMethodType << SecondName << SecondTA << i + 1;
10644 
10645             TemplateArgumentMismatch = true;
10646             break;
10647           }
10648 
10649           if (TemplateArgumentMismatch) {
10650             Diagnosed = true;
10651             break;
10652           }
10653         }
10654 
10655         // Compute the hash of the method as if it has no body.
10656         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10657           Hash.clear();
10658           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10659           return Hash.CalculateHash();
10660         };
10661 
10662         // Compare the hash generated to the hash stored.  A difference means
10663         // that a body was present in the original source.  Due to merging,
10664         // the stardard way of detecting a body will not work.
10665         const bool HasFirstBody =
10666             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10667         const bool HasSecondBody =
10668             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10669 
10670         if (HasFirstBody != HasSecondBody) {
10671           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10672                            FirstMethod->getSourceRange(), MethodSingleBody)
10673               << FirstMethodType << FirstName << HasFirstBody;
10674           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10675                           SecondMethod->getSourceRange(), MethodSingleBody)
10676               << SecondMethodType << SecondName << HasSecondBody;
10677           Diagnosed = true;
10678           break;
10679         }
10680 
10681         if (HasFirstBody && HasSecondBody) {
10682           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10683                            FirstMethod->getSourceRange(), MethodDifferentBody)
10684               << FirstMethodType << FirstName;
10685           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10686                           SecondMethod->getSourceRange(), MethodDifferentBody)
10687               << SecondMethodType << SecondName;
10688           Diagnosed = true;
10689           break;
10690         }
10691 
10692         break;
10693       }
10694       case TypeAlias:
10695       case TypeDef: {
10696         Diagnosed = ODRDiagTypeDefOrAlias(
10697             FirstRecord, FirstModule, SecondModule,
10698             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10699             FirstDiffType == TypeAlias);
10700         break;
10701       }
10702       case Var: {
10703         Diagnosed =
10704             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10705                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10706         break;
10707       }
10708       case Friend: {
10709         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10710         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10711 
10712         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10713         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10714 
10715         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10716         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10717 
10718         if (FirstND && SecondND) {
10719           ODRDiagDeclError(FirstRecord, FirstModule,
10720                            FirstFriend->getFriendLoc(),
10721                            FirstFriend->getSourceRange(), FriendFunction)
10722               << FirstND;
10723           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10724                           SecondFriend->getSourceRange(), FriendFunction)
10725               << SecondND;
10726 
10727           Diagnosed = true;
10728           break;
10729         }
10730 
10731         if (FirstTSI && SecondTSI) {
10732           QualType FirstFriendType = FirstTSI->getType();
10733           QualType SecondFriendType = SecondTSI->getType();
10734           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10735                  ComputeQualTypeODRHash(SecondFriendType));
10736           ODRDiagDeclError(FirstRecord, FirstModule,
10737                            FirstFriend->getFriendLoc(),
10738                            FirstFriend->getSourceRange(), FriendType)
10739               << FirstFriendType;
10740           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10741                           SecondFriend->getSourceRange(), FriendType)
10742               << SecondFriendType;
10743           Diagnosed = true;
10744           break;
10745         }
10746 
10747         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10748                          FirstFriend->getSourceRange(), FriendTypeFunction)
10749             << (FirstTSI == nullptr);
10750         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10751                         SecondFriend->getSourceRange(), FriendTypeFunction)
10752             << (SecondTSI == nullptr);
10753 
10754         Diagnosed = true;
10755         break;
10756       }
10757       case FunctionTemplate: {
10758         FunctionTemplateDecl *FirstTemplate =
10759             cast<FunctionTemplateDecl>(FirstDecl);
10760         FunctionTemplateDecl *SecondTemplate =
10761             cast<FunctionTemplateDecl>(SecondDecl);
10762 
10763         TemplateParameterList *FirstTPL =
10764             FirstTemplate->getTemplateParameters();
10765         TemplateParameterList *SecondTPL =
10766             SecondTemplate->getTemplateParameters();
10767 
10768         if (FirstTPL->size() != SecondTPL->size()) {
10769           ODRDiagDeclError(FirstRecord, FirstModule,
10770                            FirstTemplate->getLocation(),
10771                            FirstTemplate->getSourceRange(),
10772                            FunctionTemplateDifferentNumberParameters)
10773               << FirstTemplate << FirstTPL->size();
10774           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10775                           SecondTemplate->getSourceRange(),
10776                           FunctionTemplateDifferentNumberParameters)
10777               << SecondTemplate << SecondTPL->size();
10778 
10779           Diagnosed = true;
10780           break;
10781         }
10782 
10783         bool ParameterMismatch = false;
10784         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10785           NamedDecl *FirstParam = FirstTPL->getParam(i);
10786           NamedDecl *SecondParam = SecondTPL->getParam(i);
10787 
10788           if (FirstParam->getKind() != SecondParam->getKind()) {
10789             enum {
10790               TemplateTypeParameter,
10791               NonTypeTemplateParameter,
10792               TemplateTemplateParameter,
10793             };
10794             auto GetParamType = [](NamedDecl *D) {
10795               switch (D->getKind()) {
10796                 default:
10797                   llvm_unreachable("Unexpected template parameter type");
10798                 case Decl::TemplateTypeParm:
10799                   return TemplateTypeParameter;
10800                 case Decl::NonTypeTemplateParm:
10801                   return NonTypeTemplateParameter;
10802                 case Decl::TemplateTemplateParm:
10803                   return TemplateTemplateParameter;
10804               }
10805             };
10806 
10807             ODRDiagDeclError(FirstRecord, FirstModule,
10808                              FirstTemplate->getLocation(),
10809                              FirstTemplate->getSourceRange(),
10810                              FunctionTemplateParameterDifferentKind)
10811                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10812             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10813                             SecondTemplate->getSourceRange(),
10814                             FunctionTemplateParameterDifferentKind)
10815                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10816 
10817             ParameterMismatch = true;
10818             break;
10819           }
10820 
10821           if (FirstParam->getName() != SecondParam->getName()) {
10822             ODRDiagDeclError(
10823                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10824                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10825                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10826                 << FirstParam;
10827             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10828                             SecondTemplate->getSourceRange(),
10829                             FunctionTemplateParameterName)
10830                 << SecondTemplate << (i + 1)
10831                 << (bool)SecondParam->getIdentifier() << SecondParam;
10832             ParameterMismatch = true;
10833             break;
10834           }
10835 
10836           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10837               isa<TemplateTypeParmDecl>(SecondParam)) {
10838             TemplateTypeParmDecl *FirstTTPD =
10839                 cast<TemplateTypeParmDecl>(FirstParam);
10840             TemplateTypeParmDecl *SecondTTPD =
10841                 cast<TemplateTypeParmDecl>(SecondParam);
10842             bool HasFirstDefaultArgument =
10843                 FirstTTPD->hasDefaultArgument() &&
10844                 !FirstTTPD->defaultArgumentWasInherited();
10845             bool HasSecondDefaultArgument =
10846                 SecondTTPD->hasDefaultArgument() &&
10847                 !SecondTTPD->defaultArgumentWasInherited();
10848             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10849               ODRDiagDeclError(FirstRecord, FirstModule,
10850                                FirstTemplate->getLocation(),
10851                                FirstTemplate->getSourceRange(),
10852                                FunctionTemplateParameterSingleDefaultArgument)
10853                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10854               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10855                               SecondTemplate->getSourceRange(),
10856                               FunctionTemplateParameterSingleDefaultArgument)
10857                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10858               ParameterMismatch = true;
10859               break;
10860             }
10861 
10862             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10863               QualType FirstType = FirstTTPD->getDefaultArgument();
10864               QualType SecondType = SecondTTPD->getDefaultArgument();
10865               if (ComputeQualTypeODRHash(FirstType) !=
10866                   ComputeQualTypeODRHash(SecondType)) {
10867                 ODRDiagDeclError(
10868                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10869                     FirstTemplate->getSourceRange(),
10870                     FunctionTemplateParameterDifferentDefaultArgument)
10871                     << FirstTemplate << (i + 1) << FirstType;
10872                 ODRDiagDeclNote(
10873                     SecondModule, SecondTemplate->getLocation(),
10874                     SecondTemplate->getSourceRange(),
10875                     FunctionTemplateParameterDifferentDefaultArgument)
10876                     << SecondTemplate << (i + 1) << SecondType;
10877                 ParameterMismatch = true;
10878                 break;
10879               }
10880             }
10881 
10882             if (FirstTTPD->isParameterPack() !=
10883                 SecondTTPD->isParameterPack()) {
10884               ODRDiagDeclError(FirstRecord, FirstModule,
10885                                FirstTemplate->getLocation(),
10886                                FirstTemplate->getSourceRange(),
10887                                FunctionTemplatePackParameter)
10888                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10889               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10890                               SecondTemplate->getSourceRange(),
10891                               FunctionTemplatePackParameter)
10892                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10893               ParameterMismatch = true;
10894               break;
10895             }
10896           }
10897 
10898           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10899               isa<TemplateTemplateParmDecl>(SecondParam)) {
10900             TemplateTemplateParmDecl *FirstTTPD =
10901                 cast<TemplateTemplateParmDecl>(FirstParam);
10902             TemplateTemplateParmDecl *SecondTTPD =
10903                 cast<TemplateTemplateParmDecl>(SecondParam);
10904 
10905             TemplateParameterList *FirstTPL =
10906                 FirstTTPD->getTemplateParameters();
10907             TemplateParameterList *SecondTPL =
10908                 SecondTTPD->getTemplateParameters();
10909 
10910             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10911                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10912               ODRDiagDeclError(FirstRecord, FirstModule,
10913                                FirstTemplate->getLocation(),
10914                                FirstTemplate->getSourceRange(),
10915                                FunctionTemplateParameterDifferentType)
10916                   << FirstTemplate << (i + 1);
10917               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10918                               SecondTemplate->getSourceRange(),
10919                               FunctionTemplateParameterDifferentType)
10920                   << SecondTemplate << (i + 1);
10921               ParameterMismatch = true;
10922               break;
10923             }
10924 
10925             bool HasFirstDefaultArgument =
10926                 FirstTTPD->hasDefaultArgument() &&
10927                 !FirstTTPD->defaultArgumentWasInherited();
10928             bool HasSecondDefaultArgument =
10929                 SecondTTPD->hasDefaultArgument() &&
10930                 !SecondTTPD->defaultArgumentWasInherited();
10931             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10932               ODRDiagDeclError(FirstRecord, FirstModule,
10933                                FirstTemplate->getLocation(),
10934                                FirstTemplate->getSourceRange(),
10935                                FunctionTemplateParameterSingleDefaultArgument)
10936                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10937               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10938                               SecondTemplate->getSourceRange(),
10939                               FunctionTemplateParameterSingleDefaultArgument)
10940                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10941               ParameterMismatch = true;
10942               break;
10943             }
10944 
10945             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10946               TemplateArgument FirstTA =
10947                   FirstTTPD->getDefaultArgument().getArgument();
10948               TemplateArgument SecondTA =
10949                   SecondTTPD->getDefaultArgument().getArgument();
10950               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10951                   ComputeTemplateArgumentODRHash(SecondTA)) {
10952                 ODRDiagDeclError(
10953                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10954                     FirstTemplate->getSourceRange(),
10955                     FunctionTemplateParameterDifferentDefaultArgument)
10956                     << FirstTemplate << (i + 1) << FirstTA;
10957                 ODRDiagDeclNote(
10958                     SecondModule, SecondTemplate->getLocation(),
10959                     SecondTemplate->getSourceRange(),
10960                     FunctionTemplateParameterDifferentDefaultArgument)
10961                     << SecondTemplate << (i + 1) << SecondTA;
10962                 ParameterMismatch = true;
10963                 break;
10964               }
10965             }
10966 
10967             if (FirstTTPD->isParameterPack() !=
10968                 SecondTTPD->isParameterPack()) {
10969               ODRDiagDeclError(FirstRecord, FirstModule,
10970                                FirstTemplate->getLocation(),
10971                                FirstTemplate->getSourceRange(),
10972                                FunctionTemplatePackParameter)
10973                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10974               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10975                               SecondTemplate->getSourceRange(),
10976                               FunctionTemplatePackParameter)
10977                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10978               ParameterMismatch = true;
10979               break;
10980             }
10981           }
10982 
10983           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10984               isa<NonTypeTemplateParmDecl>(SecondParam)) {
10985             NonTypeTemplateParmDecl *FirstNTTPD =
10986                 cast<NonTypeTemplateParmDecl>(FirstParam);
10987             NonTypeTemplateParmDecl *SecondNTTPD =
10988                 cast<NonTypeTemplateParmDecl>(SecondParam);
10989 
10990             QualType FirstType = FirstNTTPD->getType();
10991             QualType SecondType = SecondNTTPD->getType();
10992             if (ComputeQualTypeODRHash(FirstType) !=
10993                 ComputeQualTypeODRHash(SecondType)) {
10994               ODRDiagDeclError(FirstRecord, FirstModule,
10995                                FirstTemplate->getLocation(),
10996                                FirstTemplate->getSourceRange(),
10997                                FunctionTemplateParameterDifferentType)
10998                   << FirstTemplate << (i + 1);
10999               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11000                               SecondTemplate->getSourceRange(),
11001                               FunctionTemplateParameterDifferentType)
11002                   << SecondTemplate << (i + 1);
11003               ParameterMismatch = true;
11004               break;
11005             }
11006 
11007             bool HasFirstDefaultArgument =
11008                 FirstNTTPD->hasDefaultArgument() &&
11009                 !FirstNTTPD->defaultArgumentWasInherited();
11010             bool HasSecondDefaultArgument =
11011                 SecondNTTPD->hasDefaultArgument() &&
11012                 !SecondNTTPD->defaultArgumentWasInherited();
11013             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11014               ODRDiagDeclError(FirstRecord, FirstModule,
11015                                FirstTemplate->getLocation(),
11016                                FirstTemplate->getSourceRange(),
11017                                FunctionTemplateParameterSingleDefaultArgument)
11018                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11019               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11020                               SecondTemplate->getSourceRange(),
11021                               FunctionTemplateParameterSingleDefaultArgument)
11022                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11023               ParameterMismatch = true;
11024               break;
11025             }
11026 
11027             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11028               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11029               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11030               if (ComputeODRHash(FirstDefaultArgument) !=
11031                   ComputeODRHash(SecondDefaultArgument)) {
11032                 ODRDiagDeclError(
11033                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11034                     FirstTemplate->getSourceRange(),
11035                     FunctionTemplateParameterDifferentDefaultArgument)
11036                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11037                 ODRDiagDeclNote(
11038                     SecondModule, SecondTemplate->getLocation(),
11039                     SecondTemplate->getSourceRange(),
11040                     FunctionTemplateParameterDifferentDefaultArgument)
11041                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11042                 ParameterMismatch = true;
11043                 break;
11044               }
11045             }
11046 
11047             if (FirstNTTPD->isParameterPack() !=
11048                 SecondNTTPD->isParameterPack()) {
11049               ODRDiagDeclError(FirstRecord, FirstModule,
11050                                FirstTemplate->getLocation(),
11051                                FirstTemplate->getSourceRange(),
11052                                FunctionTemplatePackParameter)
11053                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11054               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11055                               SecondTemplate->getSourceRange(),
11056                               FunctionTemplatePackParameter)
11057                   << SecondTemplate << (i + 1)
11058                   << SecondNTTPD->isParameterPack();
11059               ParameterMismatch = true;
11060               break;
11061             }
11062           }
11063         }
11064 
11065         if (ParameterMismatch) {
11066           Diagnosed = true;
11067           break;
11068         }
11069 
11070         break;
11071       }
11072       }
11073 
11074       if (Diagnosed)
11075         continue;
11076 
11077       Diag(FirstDecl->getLocation(),
11078            diag::err_module_odr_violation_mismatch_decl_unknown)
11079           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11080           << FirstDecl->getSourceRange();
11081       Diag(SecondDecl->getLocation(),
11082            diag::note_module_odr_violation_mismatch_decl_unknown)
11083           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11084       Diagnosed = true;
11085     }
11086 
11087     if (!Diagnosed) {
11088       // All definitions are updates to the same declaration. This happens if a
11089       // module instantiates the declaration of a class template specialization
11090       // and two or more other modules instantiate its definition.
11091       //
11092       // FIXME: Indicate which modules had instantiations of this definition.
11093       // FIXME: How can this even happen?
11094       Diag(Merge.first->getLocation(),
11095            diag::err_module_odr_violation_different_instantiations)
11096         << Merge.first;
11097     }
11098   }
11099 
11100   // Issue ODR failures diagnostics for functions.
11101   for (auto &Merge : FunctionOdrMergeFailures) {
11102     enum ODRFunctionDifference {
11103       ReturnType,
11104       ParameterName,
11105       ParameterType,
11106       ParameterSingleDefaultArgument,
11107       ParameterDifferentDefaultArgument,
11108       FunctionBody,
11109     };
11110 
11111     FunctionDecl *FirstFunction = Merge.first;
11112     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11113 
11114     bool Diagnosed = false;
11115     for (auto &SecondFunction : Merge.second) {
11116 
11117       if (FirstFunction == SecondFunction)
11118         continue;
11119 
11120       std::string SecondModule =
11121           getOwningModuleNameForDiagnostic(SecondFunction);
11122 
11123       auto ODRDiagError = [FirstFunction, &FirstModule,
11124                            this](SourceLocation Loc, SourceRange Range,
11125                                  ODRFunctionDifference DiffType) {
11126         return Diag(Loc, diag::err_module_odr_violation_function)
11127                << FirstFunction << FirstModule.empty() << FirstModule << Range
11128                << DiffType;
11129       };
11130       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11131                                                SourceRange Range,
11132                                                ODRFunctionDifference DiffType) {
11133         return Diag(Loc, diag::note_module_odr_violation_function)
11134                << SecondModule << Range << DiffType;
11135       };
11136 
11137       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11138           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11139         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11140                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11141             << FirstFunction->getReturnType();
11142         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11143                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11144             << SecondFunction->getReturnType();
11145         Diagnosed = true;
11146         break;
11147       }
11148 
11149       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11150              "Merged functions with different number of parameters");
11151 
11152       auto ParamSize = FirstFunction->param_size();
11153       bool ParameterMismatch = false;
11154       for (unsigned I = 0; I < ParamSize; ++I) {
11155         auto *FirstParam = FirstFunction->getParamDecl(I);
11156         auto *SecondParam = SecondFunction->getParamDecl(I);
11157 
11158         assert(getContext().hasSameType(FirstParam->getType(),
11159                                       SecondParam->getType()) &&
11160                "Merged function has different parameter types.");
11161 
11162         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11163           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11164                        ParameterName)
11165               << I + 1 << FirstParam->getDeclName();
11166           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11167                       ParameterName)
11168               << I + 1 << SecondParam->getDeclName();
11169           ParameterMismatch = true;
11170           break;
11171         };
11172 
11173         QualType FirstParamType = FirstParam->getType();
11174         QualType SecondParamType = SecondParam->getType();
11175         if (FirstParamType != SecondParamType &&
11176             ComputeQualTypeODRHash(FirstParamType) !=
11177                 ComputeQualTypeODRHash(SecondParamType)) {
11178           if (const DecayedType *ParamDecayedType =
11179                   FirstParamType->getAs<DecayedType>()) {
11180             ODRDiagError(FirstParam->getLocation(),
11181                          FirstParam->getSourceRange(), ParameterType)
11182                 << (I + 1) << FirstParamType << true
11183                 << ParamDecayedType->getOriginalType();
11184           } else {
11185             ODRDiagError(FirstParam->getLocation(),
11186                          FirstParam->getSourceRange(), ParameterType)
11187                 << (I + 1) << FirstParamType << false;
11188           }
11189 
11190           if (const DecayedType *ParamDecayedType =
11191                   SecondParamType->getAs<DecayedType>()) {
11192             ODRDiagNote(SecondParam->getLocation(),
11193                         SecondParam->getSourceRange(), ParameterType)
11194                 << (I + 1) << SecondParamType << true
11195                 << ParamDecayedType->getOriginalType();
11196           } else {
11197             ODRDiagNote(SecondParam->getLocation(),
11198                         SecondParam->getSourceRange(), ParameterType)
11199                 << (I + 1) << SecondParamType << false;
11200           }
11201           ParameterMismatch = true;
11202           break;
11203         }
11204 
11205         const Expr *FirstInit = FirstParam->getInit();
11206         const Expr *SecondInit = SecondParam->getInit();
11207         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11208           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11209                        ParameterSingleDefaultArgument)
11210               << (I + 1) << (FirstInit == nullptr)
11211               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11212           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11213                       ParameterSingleDefaultArgument)
11214               << (I + 1) << (SecondInit == nullptr)
11215               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11216           ParameterMismatch = true;
11217           break;
11218         }
11219 
11220         if (FirstInit && SecondInit &&
11221             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11222           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11223                        ParameterDifferentDefaultArgument)
11224               << (I + 1) << FirstInit->getSourceRange();
11225           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11226                       ParameterDifferentDefaultArgument)
11227               << (I + 1) << SecondInit->getSourceRange();
11228           ParameterMismatch = true;
11229           break;
11230         }
11231 
11232         assert(ComputeSubDeclODRHash(FirstParam) ==
11233                    ComputeSubDeclODRHash(SecondParam) &&
11234                "Undiagnosed parameter difference.");
11235       }
11236 
11237       if (ParameterMismatch) {
11238         Diagnosed = true;
11239         break;
11240       }
11241 
11242       // If no error has been generated before now, assume the problem is in
11243       // the body and generate a message.
11244       ODRDiagError(FirstFunction->getLocation(),
11245                    FirstFunction->getSourceRange(), FunctionBody);
11246       ODRDiagNote(SecondFunction->getLocation(),
11247                   SecondFunction->getSourceRange(), FunctionBody);
11248       Diagnosed = true;
11249       break;
11250     }
11251     (void)Diagnosed;
11252     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11253   }
11254 
11255   // Issue ODR failures diagnostics for enums.
11256   for (auto &Merge : EnumOdrMergeFailures) {
11257     enum ODREnumDifference {
11258       SingleScopedEnum,
11259       EnumTagKeywordMismatch,
11260       SingleSpecifiedType,
11261       DifferentSpecifiedTypes,
11262       DifferentNumberEnumConstants,
11263       EnumConstantName,
11264       EnumConstantSingleInitilizer,
11265       EnumConstantDifferentInitilizer,
11266     };
11267 
11268     // If we've already pointed out a specific problem with this enum, don't
11269     // bother issuing a general "something's different" diagnostic.
11270     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11271       continue;
11272 
11273     EnumDecl *FirstEnum = Merge.first;
11274     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11275 
11276     using DeclHashes =
11277         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11278     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11279                               DeclHashes &Hashes, EnumDecl *Enum) {
11280       for (auto *D : Enum->decls()) {
11281         // Due to decl merging, the first EnumDecl is the parent of
11282         // Decls in both records.
11283         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11284           continue;
11285         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11286         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11287                             ComputeSubDeclODRHash(D));
11288       }
11289     };
11290     DeclHashes FirstHashes;
11291     PopulateHashes(FirstHashes, FirstEnum);
11292     bool Diagnosed = false;
11293     for (auto &SecondEnum : Merge.second) {
11294 
11295       if (FirstEnum == SecondEnum)
11296         continue;
11297 
11298       std::string SecondModule =
11299           getOwningModuleNameForDiagnostic(SecondEnum);
11300 
11301       auto ODRDiagError = [FirstEnum, &FirstModule,
11302                            this](SourceLocation Loc, SourceRange Range,
11303                                  ODREnumDifference DiffType) {
11304         return Diag(Loc, diag::err_module_odr_violation_enum)
11305                << FirstEnum << FirstModule.empty() << FirstModule << Range
11306                << DiffType;
11307       };
11308       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11309                                                SourceRange Range,
11310                                                ODREnumDifference DiffType) {
11311         return Diag(Loc, diag::note_module_odr_violation_enum)
11312                << SecondModule << Range << DiffType;
11313       };
11314 
11315       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11316         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11317                      SingleScopedEnum)
11318             << FirstEnum->isScoped();
11319         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11320                     SingleScopedEnum)
11321             << SecondEnum->isScoped();
11322         Diagnosed = true;
11323         continue;
11324       }
11325 
11326       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11327         if (FirstEnum->isScopedUsingClassTag() !=
11328             SecondEnum->isScopedUsingClassTag()) {
11329           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11330                        EnumTagKeywordMismatch)
11331               << FirstEnum->isScopedUsingClassTag();
11332           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11333                       EnumTagKeywordMismatch)
11334               << SecondEnum->isScopedUsingClassTag();
11335           Diagnosed = true;
11336           continue;
11337         }
11338       }
11339 
11340       QualType FirstUnderlyingType =
11341           FirstEnum->getIntegerTypeSourceInfo()
11342               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11343               : QualType();
11344       QualType SecondUnderlyingType =
11345           SecondEnum->getIntegerTypeSourceInfo()
11346               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11347               : QualType();
11348       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11349           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11350                        SingleSpecifiedType)
11351               << !FirstUnderlyingType.isNull();
11352           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11353                       SingleSpecifiedType)
11354               << !SecondUnderlyingType.isNull();
11355           Diagnosed = true;
11356           continue;
11357       }
11358 
11359       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11360         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11361             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11362           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11363                        DifferentSpecifiedTypes)
11364               << FirstUnderlyingType;
11365           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11366                       DifferentSpecifiedTypes)
11367               << SecondUnderlyingType;
11368           Diagnosed = true;
11369           continue;
11370         }
11371       }
11372 
11373       DeclHashes SecondHashes;
11374       PopulateHashes(SecondHashes, SecondEnum);
11375 
11376       if (FirstHashes.size() != SecondHashes.size()) {
11377         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11378                      DifferentNumberEnumConstants)
11379             << (int)FirstHashes.size();
11380         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11381                     DifferentNumberEnumConstants)
11382             << (int)SecondHashes.size();
11383         Diagnosed = true;
11384         continue;
11385       }
11386 
11387       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11388         if (FirstHashes[I].second == SecondHashes[I].second)
11389           continue;
11390         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11391         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11392 
11393         if (FirstEnumConstant->getDeclName() !=
11394             SecondEnumConstant->getDeclName()) {
11395 
11396           ODRDiagError(FirstEnumConstant->getLocation(),
11397                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11398               << I + 1 << FirstEnumConstant;
11399           ODRDiagNote(SecondEnumConstant->getLocation(),
11400                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11401               << I + 1 << SecondEnumConstant;
11402           Diagnosed = true;
11403           break;
11404         }
11405 
11406         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11407         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11408         if (!FirstInit && !SecondInit)
11409           continue;
11410 
11411         if (!FirstInit || !SecondInit) {
11412           ODRDiagError(FirstEnumConstant->getLocation(),
11413                        FirstEnumConstant->getSourceRange(),
11414                        EnumConstantSingleInitilizer)
11415               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11416           ODRDiagNote(SecondEnumConstant->getLocation(),
11417                       SecondEnumConstant->getSourceRange(),
11418                       EnumConstantSingleInitilizer)
11419               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11420           Diagnosed = true;
11421           break;
11422         }
11423 
11424         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11425           ODRDiagError(FirstEnumConstant->getLocation(),
11426                        FirstEnumConstant->getSourceRange(),
11427                        EnumConstantDifferentInitilizer)
11428               << I + 1 << FirstEnumConstant;
11429           ODRDiagNote(SecondEnumConstant->getLocation(),
11430                       SecondEnumConstant->getSourceRange(),
11431                       EnumConstantDifferentInitilizer)
11432               << I + 1 << SecondEnumConstant;
11433           Diagnosed = true;
11434           break;
11435         }
11436       }
11437     }
11438 
11439     (void)Diagnosed;
11440     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11441   }
11442 }
11443 
11444 void ASTReader::StartedDeserializing() {
11445   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11446     ReadTimer->startTimer();
11447 }
11448 
11449 void ASTReader::FinishedDeserializing() {
11450   assert(NumCurrentElementsDeserializing &&
11451          "FinishedDeserializing not paired with StartedDeserializing");
11452   if (NumCurrentElementsDeserializing == 1) {
11453     // We decrease NumCurrentElementsDeserializing only after pending actions
11454     // are finished, to avoid recursively re-calling finishPendingActions().
11455     finishPendingActions();
11456   }
11457   --NumCurrentElementsDeserializing;
11458 
11459   if (NumCurrentElementsDeserializing == 0) {
11460     // Propagate exception specification and deduced type updates along
11461     // redeclaration chains.
11462     //
11463     // We do this now rather than in finishPendingActions because we want to
11464     // be able to walk the complete redeclaration chains of the updated decls.
11465     while (!PendingExceptionSpecUpdates.empty() ||
11466            !PendingDeducedTypeUpdates.empty()) {
11467       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11468       PendingExceptionSpecUpdates.clear();
11469       for (auto Update : ESUpdates) {
11470         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11471         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11472         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11473         if (auto *Listener = getContext().getASTMutationListener())
11474           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11475         for (auto *Redecl : Update.second->redecls())
11476           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11477       }
11478 
11479       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11480       PendingDeducedTypeUpdates.clear();
11481       for (auto Update : DTUpdates) {
11482         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11483         // FIXME: If the return type is already deduced, check that it matches.
11484         getContext().adjustDeducedFunctionResultType(Update.first,
11485                                                      Update.second);
11486       }
11487     }
11488 
11489     if (ReadTimer)
11490       ReadTimer->stopTimer();
11491 
11492     diagnoseOdrViolations();
11493 
11494     // We are not in recursive loading, so it's safe to pass the "interesting"
11495     // decls to the consumer.
11496     if (Consumer)
11497       PassInterestingDeclsToConsumer();
11498   }
11499 }
11500 
11501 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11502   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11503     // Remove any fake results before adding any real ones.
11504     auto It = PendingFakeLookupResults.find(II);
11505     if (It != PendingFakeLookupResults.end()) {
11506       for (auto *ND : It->second)
11507         SemaObj->IdResolver.RemoveDecl(ND);
11508       // FIXME: this works around module+PCH performance issue.
11509       // Rather than erase the result from the map, which is O(n), just clear
11510       // the vector of NamedDecls.
11511       It->second.clear();
11512     }
11513   }
11514 
11515   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11516     SemaObj->TUScope->AddDecl(D);
11517   } else if (SemaObj->TUScope) {
11518     // Adding the decl to IdResolver may have failed because it was already in
11519     // (even though it was not added in scope). If it is already in, make sure
11520     // it gets in the scope as well.
11521     if (std::find(SemaObj->IdResolver.begin(Name),
11522                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11523       SemaObj->TUScope->AddDecl(D);
11524   }
11525 }
11526 
11527 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11528                      ASTContext *Context,
11529                      const PCHContainerReader &PCHContainerRdr,
11530                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11531                      StringRef isysroot, bool DisableValidation,
11532                      bool AllowASTWithCompilerErrors,
11533                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11534                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11535                      std::unique_ptr<llvm::Timer> ReadTimer)
11536     : Listener(DisableValidation
11537                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11538                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11539       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11540       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11541       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11542                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11543       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11544       DisableValidation(DisableValidation),
11545       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11546       AllowConfigurationMismatch(AllowConfigurationMismatch),
11547       ValidateSystemInputs(ValidateSystemInputs),
11548       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11549       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11550   SourceMgr.setExternalSLocEntrySource(this);
11551 
11552   for (const auto &Ext : Extensions) {
11553     auto BlockName = Ext->getExtensionMetadata().BlockName;
11554     auto Known = ModuleFileExtensions.find(BlockName);
11555     if (Known != ModuleFileExtensions.end()) {
11556       Diags.Report(diag::warn_duplicate_module_file_extension)
11557         << BlockName;
11558       continue;
11559     }
11560 
11561     ModuleFileExtensions.insert({BlockName, Ext});
11562   }
11563 }
11564 
11565 ASTReader::~ASTReader() {
11566   if (OwnsDeserializationListener)
11567     delete DeserializationListener;
11568 }
11569 
11570 IdentifierResolver &ASTReader::getIdResolver() {
11571   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11572 }
11573 
11574 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11575                                                unsigned AbbrevID) {
11576   Idx = 0;
11577   Record.clear();
11578   return Cursor.readRecord(AbbrevID, Record);
11579 }
11580 //===----------------------------------------------------------------------===//
11581 //// OMPClauseReader implementation
11582 ////===----------------------------------------------------------------------===//
11583 
11584 // This has to be in namespace clang because it's friended by all
11585 // of the OMP clauses.
11586 namespace clang {
11587 
11588 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11589   ASTRecordReader &Record;
11590   ASTContext &Context;
11591 
11592 public:
11593   OMPClauseReader(ASTRecordReader &Record)
11594       : Record(Record), Context(Record.getContext()) {}
11595 
11596 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C);
11597 #include "clang/Basic/OpenMPKinds.def"
11598   OMPClause *readClause();
11599   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11600   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11601 };
11602 
11603 } // end namespace clang
11604 
11605 OMPClause *ASTRecordReader::readOMPClause() {
11606   return OMPClauseReader(*this).readClause();
11607 }
11608 
11609 OMPClause *OMPClauseReader::readClause() {
11610   OMPClause *C = nullptr;
11611   switch (Record.readInt()) {
11612   case OMPC_if:
11613     C = new (Context) OMPIfClause();
11614     break;
11615   case OMPC_final:
11616     C = new (Context) OMPFinalClause();
11617     break;
11618   case OMPC_num_threads:
11619     C = new (Context) OMPNumThreadsClause();
11620     break;
11621   case OMPC_safelen:
11622     C = new (Context) OMPSafelenClause();
11623     break;
11624   case OMPC_simdlen:
11625     C = new (Context) OMPSimdlenClause();
11626     break;
11627   case OMPC_allocator:
11628     C = new (Context) OMPAllocatorClause();
11629     break;
11630   case OMPC_collapse:
11631     C = new (Context) OMPCollapseClause();
11632     break;
11633   case OMPC_default:
11634     C = new (Context) OMPDefaultClause();
11635     break;
11636   case OMPC_proc_bind:
11637     C = new (Context) OMPProcBindClause();
11638     break;
11639   case OMPC_schedule:
11640     C = new (Context) OMPScheduleClause();
11641     break;
11642   case OMPC_ordered:
11643     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11644     break;
11645   case OMPC_nowait:
11646     C = new (Context) OMPNowaitClause();
11647     break;
11648   case OMPC_untied:
11649     C = new (Context) OMPUntiedClause();
11650     break;
11651   case OMPC_mergeable:
11652     C = new (Context) OMPMergeableClause();
11653     break;
11654   case OMPC_read:
11655     C = new (Context) OMPReadClause();
11656     break;
11657   case OMPC_write:
11658     C = new (Context) OMPWriteClause();
11659     break;
11660   case OMPC_update:
11661     C = new (Context) OMPUpdateClause();
11662     break;
11663   case OMPC_capture:
11664     C = new (Context) OMPCaptureClause();
11665     break;
11666   case OMPC_seq_cst:
11667     C = new (Context) OMPSeqCstClause();
11668     break;
11669   case OMPC_acq_rel:
11670     C = new (Context) OMPAcqRelClause();
11671     break;
11672   case OMPC_acquire:
11673     C = new (Context) OMPAcquireClause();
11674     break;
11675   case OMPC_release:
11676     C = new (Context) OMPReleaseClause();
11677     break;
11678   case OMPC_relaxed:
11679     C = new (Context) OMPRelaxedClause();
11680     break;
11681   case OMPC_threads:
11682     C = new (Context) OMPThreadsClause();
11683     break;
11684   case OMPC_simd:
11685     C = new (Context) OMPSIMDClause();
11686     break;
11687   case OMPC_nogroup:
11688     C = new (Context) OMPNogroupClause();
11689     break;
11690   case OMPC_unified_address:
11691     C = new (Context) OMPUnifiedAddressClause();
11692     break;
11693   case OMPC_unified_shared_memory:
11694     C = new (Context) OMPUnifiedSharedMemoryClause();
11695     break;
11696   case OMPC_reverse_offload:
11697     C = new (Context) OMPReverseOffloadClause();
11698     break;
11699   case OMPC_dynamic_allocators:
11700     C = new (Context) OMPDynamicAllocatorsClause();
11701     break;
11702   case OMPC_atomic_default_mem_order:
11703     C = new (Context) OMPAtomicDefaultMemOrderClause();
11704     break;
11705  case OMPC_private:
11706     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11707     break;
11708   case OMPC_firstprivate:
11709     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11710     break;
11711   case OMPC_lastprivate:
11712     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11713     break;
11714   case OMPC_shared:
11715     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11716     break;
11717   case OMPC_reduction:
11718     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11719     break;
11720   case OMPC_task_reduction:
11721     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11722     break;
11723   case OMPC_in_reduction:
11724     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11725     break;
11726   case OMPC_linear:
11727     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11728     break;
11729   case OMPC_aligned:
11730     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11731     break;
11732   case OMPC_copyin:
11733     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11734     break;
11735   case OMPC_copyprivate:
11736     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11737     break;
11738   case OMPC_flush:
11739     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11740     break;
11741   case OMPC_depend: {
11742     unsigned NumVars = Record.readInt();
11743     unsigned NumLoops = Record.readInt();
11744     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11745     break;
11746   }
11747   case OMPC_device:
11748     C = new (Context) OMPDeviceClause();
11749     break;
11750   case OMPC_map: {
11751     OMPMappableExprListSizeTy Sizes;
11752     Sizes.NumVars = Record.readInt();
11753     Sizes.NumUniqueDeclarations = Record.readInt();
11754     Sizes.NumComponentLists = Record.readInt();
11755     Sizes.NumComponents = Record.readInt();
11756     C = OMPMapClause::CreateEmpty(Context, Sizes);
11757     break;
11758   }
11759   case OMPC_num_teams:
11760     C = new (Context) OMPNumTeamsClause();
11761     break;
11762   case OMPC_thread_limit:
11763     C = new (Context) OMPThreadLimitClause();
11764     break;
11765   case OMPC_priority:
11766     C = new (Context) OMPPriorityClause();
11767     break;
11768   case OMPC_grainsize:
11769     C = new (Context) OMPGrainsizeClause();
11770     break;
11771   case OMPC_num_tasks:
11772     C = new (Context) OMPNumTasksClause();
11773     break;
11774   case OMPC_hint:
11775     C = new (Context) OMPHintClause();
11776     break;
11777   case OMPC_dist_schedule:
11778     C = new (Context) OMPDistScheduleClause();
11779     break;
11780   case OMPC_defaultmap:
11781     C = new (Context) OMPDefaultmapClause();
11782     break;
11783   case OMPC_to: {
11784     OMPMappableExprListSizeTy Sizes;
11785     Sizes.NumVars = Record.readInt();
11786     Sizes.NumUniqueDeclarations = Record.readInt();
11787     Sizes.NumComponentLists = Record.readInt();
11788     Sizes.NumComponents = Record.readInt();
11789     C = OMPToClause::CreateEmpty(Context, Sizes);
11790     break;
11791   }
11792   case OMPC_from: {
11793     OMPMappableExprListSizeTy Sizes;
11794     Sizes.NumVars = Record.readInt();
11795     Sizes.NumUniqueDeclarations = Record.readInt();
11796     Sizes.NumComponentLists = Record.readInt();
11797     Sizes.NumComponents = Record.readInt();
11798     C = OMPFromClause::CreateEmpty(Context, Sizes);
11799     break;
11800   }
11801   case OMPC_use_device_ptr: {
11802     OMPMappableExprListSizeTy Sizes;
11803     Sizes.NumVars = Record.readInt();
11804     Sizes.NumUniqueDeclarations = Record.readInt();
11805     Sizes.NumComponentLists = Record.readInt();
11806     Sizes.NumComponents = Record.readInt();
11807     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11808     break;
11809   }
11810   case OMPC_is_device_ptr: {
11811     OMPMappableExprListSizeTy Sizes;
11812     Sizes.NumVars = Record.readInt();
11813     Sizes.NumUniqueDeclarations = Record.readInt();
11814     Sizes.NumComponentLists = Record.readInt();
11815     Sizes.NumComponents = Record.readInt();
11816     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11817     break;
11818   }
11819   case OMPC_allocate:
11820     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11821     break;
11822   case OMPC_nontemporal:
11823     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11824     break;
11825   case OMPC_order:
11826     C = new (Context) OMPOrderClause();
11827     break;
11828   }
11829   assert(C && "Unknown OMPClause type");
11830 
11831   Visit(C);
11832   C->setLocStart(Record.readSourceLocation());
11833   C->setLocEnd(Record.readSourceLocation());
11834 
11835   return C;
11836 }
11837 
11838 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11839   C->setPreInitStmt(Record.readSubStmt(),
11840                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11841 }
11842 
11843 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11844   VisitOMPClauseWithPreInit(C);
11845   C->setPostUpdateExpr(Record.readSubExpr());
11846 }
11847 
11848 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11849   VisitOMPClauseWithPreInit(C);
11850   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11851   C->setNameModifierLoc(Record.readSourceLocation());
11852   C->setColonLoc(Record.readSourceLocation());
11853   C->setCondition(Record.readSubExpr());
11854   C->setLParenLoc(Record.readSourceLocation());
11855 }
11856 
11857 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11858   VisitOMPClauseWithPreInit(C);
11859   C->setCondition(Record.readSubExpr());
11860   C->setLParenLoc(Record.readSourceLocation());
11861 }
11862 
11863 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11864   VisitOMPClauseWithPreInit(C);
11865   C->setNumThreads(Record.readSubExpr());
11866   C->setLParenLoc(Record.readSourceLocation());
11867 }
11868 
11869 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11870   C->setSafelen(Record.readSubExpr());
11871   C->setLParenLoc(Record.readSourceLocation());
11872 }
11873 
11874 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11875   C->setSimdlen(Record.readSubExpr());
11876   C->setLParenLoc(Record.readSourceLocation());
11877 }
11878 
11879 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11880   C->setAllocator(Record.readExpr());
11881   C->setLParenLoc(Record.readSourceLocation());
11882 }
11883 
11884 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11885   C->setNumForLoops(Record.readSubExpr());
11886   C->setLParenLoc(Record.readSourceLocation());
11887 }
11888 
11889 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11890   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11891   C->setLParenLoc(Record.readSourceLocation());
11892   C->setDefaultKindKwLoc(Record.readSourceLocation());
11893 }
11894 
11895 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11896   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11897   C->setLParenLoc(Record.readSourceLocation());
11898   C->setProcBindKindKwLoc(Record.readSourceLocation());
11899 }
11900 
11901 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11902   VisitOMPClauseWithPreInit(C);
11903   C->setScheduleKind(
11904        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11905   C->setFirstScheduleModifier(
11906       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11907   C->setSecondScheduleModifier(
11908       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11909   C->setChunkSize(Record.readSubExpr());
11910   C->setLParenLoc(Record.readSourceLocation());
11911   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11912   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11913   C->setScheduleKindLoc(Record.readSourceLocation());
11914   C->setCommaLoc(Record.readSourceLocation());
11915 }
11916 
11917 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11918   C->setNumForLoops(Record.readSubExpr());
11919   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11920     C->setLoopNumIterations(I, Record.readSubExpr());
11921   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11922     C->setLoopCounter(I, Record.readSubExpr());
11923   C->setLParenLoc(Record.readSourceLocation());
11924 }
11925 
11926 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11927 
11928 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11929 
11930 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11931 
11932 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11933 
11934 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11935 
11936 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
11937 
11938 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11939 
11940 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11941 
11942 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11943 
11944 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11945 
11946 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11947 
11948 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11949 
11950 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11951 
11952 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11953 
11954 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11955 
11956 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11957 
11958 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11959     OMPUnifiedSharedMemoryClause *) {}
11960 
11961 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11962 
11963 void
11964 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11965 }
11966 
11967 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11968     OMPAtomicDefaultMemOrderClause *C) {
11969   C->setAtomicDefaultMemOrderKind(
11970       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11971   C->setLParenLoc(Record.readSourceLocation());
11972   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11973 }
11974 
11975 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11976   C->setLParenLoc(Record.readSourceLocation());
11977   unsigned NumVars = C->varlist_size();
11978   SmallVector<Expr *, 16> Vars;
11979   Vars.reserve(NumVars);
11980   for (unsigned i = 0; i != NumVars; ++i)
11981     Vars.push_back(Record.readSubExpr());
11982   C->setVarRefs(Vars);
11983   Vars.clear();
11984   for (unsigned i = 0; i != NumVars; ++i)
11985     Vars.push_back(Record.readSubExpr());
11986   C->setPrivateCopies(Vars);
11987 }
11988 
11989 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
11990   VisitOMPClauseWithPreInit(C);
11991   C->setLParenLoc(Record.readSourceLocation());
11992   unsigned NumVars = C->varlist_size();
11993   SmallVector<Expr *, 16> Vars;
11994   Vars.reserve(NumVars);
11995   for (unsigned i = 0; i != NumVars; ++i)
11996     Vars.push_back(Record.readSubExpr());
11997   C->setVarRefs(Vars);
11998   Vars.clear();
11999   for (unsigned i = 0; i != NumVars; ++i)
12000     Vars.push_back(Record.readSubExpr());
12001   C->setPrivateCopies(Vars);
12002   Vars.clear();
12003   for (unsigned i = 0; i != NumVars; ++i)
12004     Vars.push_back(Record.readSubExpr());
12005   C->setInits(Vars);
12006 }
12007 
12008 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12009   VisitOMPClauseWithPostUpdate(C);
12010   C->setLParenLoc(Record.readSourceLocation());
12011   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12012   C->setKindLoc(Record.readSourceLocation());
12013   C->setColonLoc(Record.readSourceLocation());
12014   unsigned NumVars = C->varlist_size();
12015   SmallVector<Expr *, 16> Vars;
12016   Vars.reserve(NumVars);
12017   for (unsigned i = 0; i != NumVars; ++i)
12018     Vars.push_back(Record.readSubExpr());
12019   C->setVarRefs(Vars);
12020   Vars.clear();
12021   for (unsigned i = 0; i != NumVars; ++i)
12022     Vars.push_back(Record.readSubExpr());
12023   C->setPrivateCopies(Vars);
12024   Vars.clear();
12025   for (unsigned i = 0; i != NumVars; ++i)
12026     Vars.push_back(Record.readSubExpr());
12027   C->setSourceExprs(Vars);
12028   Vars.clear();
12029   for (unsigned i = 0; i != NumVars; ++i)
12030     Vars.push_back(Record.readSubExpr());
12031   C->setDestinationExprs(Vars);
12032   Vars.clear();
12033   for (unsigned i = 0; i != NumVars; ++i)
12034     Vars.push_back(Record.readSubExpr());
12035   C->setAssignmentOps(Vars);
12036 }
12037 
12038 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12039   C->setLParenLoc(Record.readSourceLocation());
12040   unsigned NumVars = C->varlist_size();
12041   SmallVector<Expr *, 16> Vars;
12042   Vars.reserve(NumVars);
12043   for (unsigned i = 0; i != NumVars; ++i)
12044     Vars.push_back(Record.readSubExpr());
12045   C->setVarRefs(Vars);
12046 }
12047 
12048 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12049   VisitOMPClauseWithPostUpdate(C);
12050   C->setLParenLoc(Record.readSourceLocation());
12051   C->setColonLoc(Record.readSourceLocation());
12052   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12053   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12054   C->setQualifierLoc(NNSL);
12055   C->setNameInfo(DNI);
12056 
12057   unsigned NumVars = C->varlist_size();
12058   SmallVector<Expr *, 16> Vars;
12059   Vars.reserve(NumVars);
12060   for (unsigned i = 0; i != NumVars; ++i)
12061     Vars.push_back(Record.readSubExpr());
12062   C->setVarRefs(Vars);
12063   Vars.clear();
12064   for (unsigned i = 0; i != NumVars; ++i)
12065     Vars.push_back(Record.readSubExpr());
12066   C->setPrivates(Vars);
12067   Vars.clear();
12068   for (unsigned i = 0; i != NumVars; ++i)
12069     Vars.push_back(Record.readSubExpr());
12070   C->setLHSExprs(Vars);
12071   Vars.clear();
12072   for (unsigned i = 0; i != NumVars; ++i)
12073     Vars.push_back(Record.readSubExpr());
12074   C->setRHSExprs(Vars);
12075   Vars.clear();
12076   for (unsigned i = 0; i != NumVars; ++i)
12077     Vars.push_back(Record.readSubExpr());
12078   C->setReductionOps(Vars);
12079 }
12080 
12081 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12082   VisitOMPClauseWithPostUpdate(C);
12083   C->setLParenLoc(Record.readSourceLocation());
12084   C->setColonLoc(Record.readSourceLocation());
12085   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12086   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12087   C->setQualifierLoc(NNSL);
12088   C->setNameInfo(DNI);
12089 
12090   unsigned NumVars = C->varlist_size();
12091   SmallVector<Expr *, 16> Vars;
12092   Vars.reserve(NumVars);
12093   for (unsigned I = 0; I != NumVars; ++I)
12094     Vars.push_back(Record.readSubExpr());
12095   C->setVarRefs(Vars);
12096   Vars.clear();
12097   for (unsigned I = 0; I != NumVars; ++I)
12098     Vars.push_back(Record.readSubExpr());
12099   C->setPrivates(Vars);
12100   Vars.clear();
12101   for (unsigned I = 0; I != NumVars; ++I)
12102     Vars.push_back(Record.readSubExpr());
12103   C->setLHSExprs(Vars);
12104   Vars.clear();
12105   for (unsigned I = 0; I != NumVars; ++I)
12106     Vars.push_back(Record.readSubExpr());
12107   C->setRHSExprs(Vars);
12108   Vars.clear();
12109   for (unsigned I = 0; I != NumVars; ++I)
12110     Vars.push_back(Record.readSubExpr());
12111   C->setReductionOps(Vars);
12112 }
12113 
12114 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12115   VisitOMPClauseWithPostUpdate(C);
12116   C->setLParenLoc(Record.readSourceLocation());
12117   C->setColonLoc(Record.readSourceLocation());
12118   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12119   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12120   C->setQualifierLoc(NNSL);
12121   C->setNameInfo(DNI);
12122 
12123   unsigned NumVars = C->varlist_size();
12124   SmallVector<Expr *, 16> Vars;
12125   Vars.reserve(NumVars);
12126   for (unsigned I = 0; I != NumVars; ++I)
12127     Vars.push_back(Record.readSubExpr());
12128   C->setVarRefs(Vars);
12129   Vars.clear();
12130   for (unsigned I = 0; I != NumVars; ++I)
12131     Vars.push_back(Record.readSubExpr());
12132   C->setPrivates(Vars);
12133   Vars.clear();
12134   for (unsigned I = 0; I != NumVars; ++I)
12135     Vars.push_back(Record.readSubExpr());
12136   C->setLHSExprs(Vars);
12137   Vars.clear();
12138   for (unsigned I = 0; I != NumVars; ++I)
12139     Vars.push_back(Record.readSubExpr());
12140   C->setRHSExprs(Vars);
12141   Vars.clear();
12142   for (unsigned I = 0; I != NumVars; ++I)
12143     Vars.push_back(Record.readSubExpr());
12144   C->setReductionOps(Vars);
12145   Vars.clear();
12146   for (unsigned I = 0; I != NumVars; ++I)
12147     Vars.push_back(Record.readSubExpr());
12148   C->setTaskgroupDescriptors(Vars);
12149 }
12150 
12151 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12152   VisitOMPClauseWithPostUpdate(C);
12153   C->setLParenLoc(Record.readSourceLocation());
12154   C->setColonLoc(Record.readSourceLocation());
12155   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12156   C->setModifierLoc(Record.readSourceLocation());
12157   unsigned NumVars = C->varlist_size();
12158   SmallVector<Expr *, 16> Vars;
12159   Vars.reserve(NumVars);
12160   for (unsigned i = 0; i != NumVars; ++i)
12161     Vars.push_back(Record.readSubExpr());
12162   C->setVarRefs(Vars);
12163   Vars.clear();
12164   for (unsigned i = 0; i != NumVars; ++i)
12165     Vars.push_back(Record.readSubExpr());
12166   C->setPrivates(Vars);
12167   Vars.clear();
12168   for (unsigned i = 0; i != NumVars; ++i)
12169     Vars.push_back(Record.readSubExpr());
12170   C->setInits(Vars);
12171   Vars.clear();
12172   for (unsigned i = 0; i != NumVars; ++i)
12173     Vars.push_back(Record.readSubExpr());
12174   C->setUpdates(Vars);
12175   Vars.clear();
12176   for (unsigned i = 0; i != NumVars; ++i)
12177     Vars.push_back(Record.readSubExpr());
12178   C->setFinals(Vars);
12179   C->setStep(Record.readSubExpr());
12180   C->setCalcStep(Record.readSubExpr());
12181   Vars.clear();
12182   for (unsigned I = 0; I != NumVars + 1; ++I)
12183     Vars.push_back(Record.readSubExpr());
12184   C->setUsedExprs(Vars);
12185 }
12186 
12187 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12188   C->setLParenLoc(Record.readSourceLocation());
12189   C->setColonLoc(Record.readSourceLocation());
12190   unsigned NumVars = C->varlist_size();
12191   SmallVector<Expr *, 16> Vars;
12192   Vars.reserve(NumVars);
12193   for (unsigned i = 0; i != NumVars; ++i)
12194     Vars.push_back(Record.readSubExpr());
12195   C->setVarRefs(Vars);
12196   C->setAlignment(Record.readSubExpr());
12197 }
12198 
12199 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12200   C->setLParenLoc(Record.readSourceLocation());
12201   unsigned NumVars = C->varlist_size();
12202   SmallVector<Expr *, 16> Exprs;
12203   Exprs.reserve(NumVars);
12204   for (unsigned i = 0; i != NumVars; ++i)
12205     Exprs.push_back(Record.readSubExpr());
12206   C->setVarRefs(Exprs);
12207   Exprs.clear();
12208   for (unsigned i = 0; i != NumVars; ++i)
12209     Exprs.push_back(Record.readSubExpr());
12210   C->setSourceExprs(Exprs);
12211   Exprs.clear();
12212   for (unsigned i = 0; i != NumVars; ++i)
12213     Exprs.push_back(Record.readSubExpr());
12214   C->setDestinationExprs(Exprs);
12215   Exprs.clear();
12216   for (unsigned i = 0; i != NumVars; ++i)
12217     Exprs.push_back(Record.readSubExpr());
12218   C->setAssignmentOps(Exprs);
12219 }
12220 
12221 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12222   C->setLParenLoc(Record.readSourceLocation());
12223   unsigned NumVars = C->varlist_size();
12224   SmallVector<Expr *, 16> Exprs;
12225   Exprs.reserve(NumVars);
12226   for (unsigned i = 0; i != NumVars; ++i)
12227     Exprs.push_back(Record.readSubExpr());
12228   C->setVarRefs(Exprs);
12229   Exprs.clear();
12230   for (unsigned i = 0; i != NumVars; ++i)
12231     Exprs.push_back(Record.readSubExpr());
12232   C->setSourceExprs(Exprs);
12233   Exprs.clear();
12234   for (unsigned i = 0; i != NumVars; ++i)
12235     Exprs.push_back(Record.readSubExpr());
12236   C->setDestinationExprs(Exprs);
12237   Exprs.clear();
12238   for (unsigned i = 0; i != NumVars; ++i)
12239     Exprs.push_back(Record.readSubExpr());
12240   C->setAssignmentOps(Exprs);
12241 }
12242 
12243 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12244   C->setLParenLoc(Record.readSourceLocation());
12245   unsigned NumVars = C->varlist_size();
12246   SmallVector<Expr *, 16> Vars;
12247   Vars.reserve(NumVars);
12248   for (unsigned i = 0; i != NumVars; ++i)
12249     Vars.push_back(Record.readSubExpr());
12250   C->setVarRefs(Vars);
12251 }
12252 
12253 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12254   C->setLParenLoc(Record.readSourceLocation());
12255   C->setDependencyKind(
12256       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12257   C->setDependencyLoc(Record.readSourceLocation());
12258   C->setColonLoc(Record.readSourceLocation());
12259   unsigned NumVars = C->varlist_size();
12260   SmallVector<Expr *, 16> Vars;
12261   Vars.reserve(NumVars);
12262   for (unsigned I = 0; I != NumVars; ++I)
12263     Vars.push_back(Record.readSubExpr());
12264   C->setVarRefs(Vars);
12265   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12266     C->setLoopData(I, Record.readSubExpr());
12267 }
12268 
12269 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12270   VisitOMPClauseWithPreInit(C);
12271   C->setDevice(Record.readSubExpr());
12272   C->setLParenLoc(Record.readSourceLocation());
12273 }
12274 
12275 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12276   C->setLParenLoc(Record.readSourceLocation());
12277   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12278     C->setMapTypeModifier(
12279         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12280     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12281   }
12282   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12283   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12284   C->setMapType(
12285      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12286   C->setMapLoc(Record.readSourceLocation());
12287   C->setColonLoc(Record.readSourceLocation());
12288   auto NumVars = C->varlist_size();
12289   auto UniqueDecls = C->getUniqueDeclarationsNum();
12290   auto TotalLists = C->getTotalComponentListNum();
12291   auto TotalComponents = C->getTotalComponentsNum();
12292 
12293   SmallVector<Expr *, 16> Vars;
12294   Vars.reserve(NumVars);
12295   for (unsigned i = 0; i != NumVars; ++i)
12296     Vars.push_back(Record.readExpr());
12297   C->setVarRefs(Vars);
12298 
12299   SmallVector<Expr *, 16> UDMappers;
12300   UDMappers.reserve(NumVars);
12301   for (unsigned I = 0; I < NumVars; ++I)
12302     UDMappers.push_back(Record.readExpr());
12303   C->setUDMapperRefs(UDMappers);
12304 
12305   SmallVector<ValueDecl *, 16> Decls;
12306   Decls.reserve(UniqueDecls);
12307   for (unsigned i = 0; i < UniqueDecls; ++i)
12308     Decls.push_back(Record.readDeclAs<ValueDecl>());
12309   C->setUniqueDecls(Decls);
12310 
12311   SmallVector<unsigned, 16> ListsPerDecl;
12312   ListsPerDecl.reserve(UniqueDecls);
12313   for (unsigned i = 0; i < UniqueDecls; ++i)
12314     ListsPerDecl.push_back(Record.readInt());
12315   C->setDeclNumLists(ListsPerDecl);
12316 
12317   SmallVector<unsigned, 32> ListSizes;
12318   ListSizes.reserve(TotalLists);
12319   for (unsigned i = 0; i < TotalLists; ++i)
12320     ListSizes.push_back(Record.readInt());
12321   C->setComponentListSizes(ListSizes);
12322 
12323   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12324   Components.reserve(TotalComponents);
12325   for (unsigned i = 0; i < TotalComponents; ++i) {
12326     Expr *AssociatedExpr = Record.readExpr();
12327     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12328     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12329         AssociatedExpr, AssociatedDecl));
12330   }
12331   C->setComponents(Components, ListSizes);
12332 }
12333 
12334 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12335   C->setLParenLoc(Record.readSourceLocation());
12336   C->setColonLoc(Record.readSourceLocation());
12337   C->setAllocator(Record.readSubExpr());
12338   unsigned NumVars = C->varlist_size();
12339   SmallVector<Expr *, 16> Vars;
12340   Vars.reserve(NumVars);
12341   for (unsigned i = 0; i != NumVars; ++i)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setVarRefs(Vars);
12344 }
12345 
12346 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12347   VisitOMPClauseWithPreInit(C);
12348   C->setNumTeams(Record.readSubExpr());
12349   C->setLParenLoc(Record.readSourceLocation());
12350 }
12351 
12352 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12353   VisitOMPClauseWithPreInit(C);
12354   C->setThreadLimit(Record.readSubExpr());
12355   C->setLParenLoc(Record.readSourceLocation());
12356 }
12357 
12358 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12359   VisitOMPClauseWithPreInit(C);
12360   C->setPriority(Record.readSubExpr());
12361   C->setLParenLoc(Record.readSourceLocation());
12362 }
12363 
12364 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12365   VisitOMPClauseWithPreInit(C);
12366   C->setGrainsize(Record.readSubExpr());
12367   C->setLParenLoc(Record.readSourceLocation());
12368 }
12369 
12370 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12371   VisitOMPClauseWithPreInit(C);
12372   C->setNumTasks(Record.readSubExpr());
12373   C->setLParenLoc(Record.readSourceLocation());
12374 }
12375 
12376 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12377   C->setHint(Record.readSubExpr());
12378   C->setLParenLoc(Record.readSourceLocation());
12379 }
12380 
12381 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12382   VisitOMPClauseWithPreInit(C);
12383   C->setDistScheduleKind(
12384       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12385   C->setChunkSize(Record.readSubExpr());
12386   C->setLParenLoc(Record.readSourceLocation());
12387   C->setDistScheduleKindLoc(Record.readSourceLocation());
12388   C->setCommaLoc(Record.readSourceLocation());
12389 }
12390 
12391 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12392   C->setDefaultmapKind(
12393        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12394   C->setDefaultmapModifier(
12395       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12396   C->setLParenLoc(Record.readSourceLocation());
12397   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12398   C->setDefaultmapKindLoc(Record.readSourceLocation());
12399 }
12400 
12401 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12402   C->setLParenLoc(Record.readSourceLocation());
12403   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12404   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12405   auto NumVars = C->varlist_size();
12406   auto UniqueDecls = C->getUniqueDeclarationsNum();
12407   auto TotalLists = C->getTotalComponentListNum();
12408   auto TotalComponents = C->getTotalComponentsNum();
12409 
12410   SmallVector<Expr *, 16> Vars;
12411   Vars.reserve(NumVars);
12412   for (unsigned i = 0; i != NumVars; ++i)
12413     Vars.push_back(Record.readSubExpr());
12414   C->setVarRefs(Vars);
12415 
12416   SmallVector<Expr *, 16> UDMappers;
12417   UDMappers.reserve(NumVars);
12418   for (unsigned I = 0; I < NumVars; ++I)
12419     UDMappers.push_back(Record.readSubExpr());
12420   C->setUDMapperRefs(UDMappers);
12421 
12422   SmallVector<ValueDecl *, 16> Decls;
12423   Decls.reserve(UniqueDecls);
12424   for (unsigned i = 0; i < UniqueDecls; ++i)
12425     Decls.push_back(Record.readDeclAs<ValueDecl>());
12426   C->setUniqueDecls(Decls);
12427 
12428   SmallVector<unsigned, 16> ListsPerDecl;
12429   ListsPerDecl.reserve(UniqueDecls);
12430   for (unsigned i = 0; i < UniqueDecls; ++i)
12431     ListsPerDecl.push_back(Record.readInt());
12432   C->setDeclNumLists(ListsPerDecl);
12433 
12434   SmallVector<unsigned, 32> ListSizes;
12435   ListSizes.reserve(TotalLists);
12436   for (unsigned i = 0; i < TotalLists; ++i)
12437     ListSizes.push_back(Record.readInt());
12438   C->setComponentListSizes(ListSizes);
12439 
12440   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12441   Components.reserve(TotalComponents);
12442   for (unsigned i = 0; i < TotalComponents; ++i) {
12443     Expr *AssociatedExpr = Record.readSubExpr();
12444     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12445     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12446         AssociatedExpr, AssociatedDecl));
12447   }
12448   C->setComponents(Components, ListSizes);
12449 }
12450 
12451 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12452   C->setLParenLoc(Record.readSourceLocation());
12453   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12454   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12455   auto NumVars = C->varlist_size();
12456   auto UniqueDecls = C->getUniqueDeclarationsNum();
12457   auto TotalLists = C->getTotalComponentListNum();
12458   auto TotalComponents = C->getTotalComponentsNum();
12459 
12460   SmallVector<Expr *, 16> Vars;
12461   Vars.reserve(NumVars);
12462   for (unsigned i = 0; i != NumVars; ++i)
12463     Vars.push_back(Record.readSubExpr());
12464   C->setVarRefs(Vars);
12465 
12466   SmallVector<Expr *, 16> UDMappers;
12467   UDMappers.reserve(NumVars);
12468   for (unsigned I = 0; I < NumVars; ++I)
12469     UDMappers.push_back(Record.readSubExpr());
12470   C->setUDMapperRefs(UDMappers);
12471 
12472   SmallVector<ValueDecl *, 16> Decls;
12473   Decls.reserve(UniqueDecls);
12474   for (unsigned i = 0; i < UniqueDecls; ++i)
12475     Decls.push_back(Record.readDeclAs<ValueDecl>());
12476   C->setUniqueDecls(Decls);
12477 
12478   SmallVector<unsigned, 16> ListsPerDecl;
12479   ListsPerDecl.reserve(UniqueDecls);
12480   for (unsigned i = 0; i < UniqueDecls; ++i)
12481     ListsPerDecl.push_back(Record.readInt());
12482   C->setDeclNumLists(ListsPerDecl);
12483 
12484   SmallVector<unsigned, 32> ListSizes;
12485   ListSizes.reserve(TotalLists);
12486   for (unsigned i = 0; i < TotalLists; ++i)
12487     ListSizes.push_back(Record.readInt());
12488   C->setComponentListSizes(ListSizes);
12489 
12490   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12491   Components.reserve(TotalComponents);
12492   for (unsigned i = 0; i < TotalComponents; ++i) {
12493     Expr *AssociatedExpr = Record.readSubExpr();
12494     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12495     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12496         AssociatedExpr, AssociatedDecl));
12497   }
12498   C->setComponents(Components, ListSizes);
12499 }
12500 
12501 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12502   C->setLParenLoc(Record.readSourceLocation());
12503   auto NumVars = C->varlist_size();
12504   auto UniqueDecls = C->getUniqueDeclarationsNum();
12505   auto TotalLists = C->getTotalComponentListNum();
12506   auto TotalComponents = C->getTotalComponentsNum();
12507 
12508   SmallVector<Expr *, 16> Vars;
12509   Vars.reserve(NumVars);
12510   for (unsigned i = 0; i != NumVars; ++i)
12511     Vars.push_back(Record.readSubExpr());
12512   C->setVarRefs(Vars);
12513   Vars.clear();
12514   for (unsigned i = 0; i != NumVars; ++i)
12515     Vars.push_back(Record.readSubExpr());
12516   C->setPrivateCopies(Vars);
12517   Vars.clear();
12518   for (unsigned i = 0; i != NumVars; ++i)
12519     Vars.push_back(Record.readSubExpr());
12520   C->setInits(Vars);
12521 
12522   SmallVector<ValueDecl *, 16> Decls;
12523   Decls.reserve(UniqueDecls);
12524   for (unsigned i = 0; i < UniqueDecls; ++i)
12525     Decls.push_back(Record.readDeclAs<ValueDecl>());
12526   C->setUniqueDecls(Decls);
12527 
12528   SmallVector<unsigned, 16> ListsPerDecl;
12529   ListsPerDecl.reserve(UniqueDecls);
12530   for (unsigned i = 0; i < UniqueDecls; ++i)
12531     ListsPerDecl.push_back(Record.readInt());
12532   C->setDeclNumLists(ListsPerDecl);
12533 
12534   SmallVector<unsigned, 32> ListSizes;
12535   ListSizes.reserve(TotalLists);
12536   for (unsigned i = 0; i < TotalLists; ++i)
12537     ListSizes.push_back(Record.readInt());
12538   C->setComponentListSizes(ListSizes);
12539 
12540   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12541   Components.reserve(TotalComponents);
12542   for (unsigned i = 0; i < TotalComponents; ++i) {
12543     Expr *AssociatedExpr = Record.readSubExpr();
12544     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12545     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12546         AssociatedExpr, AssociatedDecl));
12547   }
12548   C->setComponents(Components, ListSizes);
12549 }
12550 
12551 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12552   C->setLParenLoc(Record.readSourceLocation());
12553   auto NumVars = C->varlist_size();
12554   auto UniqueDecls = C->getUniqueDeclarationsNum();
12555   auto TotalLists = C->getTotalComponentListNum();
12556   auto TotalComponents = C->getTotalComponentsNum();
12557 
12558   SmallVector<Expr *, 16> Vars;
12559   Vars.reserve(NumVars);
12560   for (unsigned i = 0; i != NumVars; ++i)
12561     Vars.push_back(Record.readSubExpr());
12562   C->setVarRefs(Vars);
12563   Vars.clear();
12564 
12565   SmallVector<ValueDecl *, 16> Decls;
12566   Decls.reserve(UniqueDecls);
12567   for (unsigned i = 0; i < UniqueDecls; ++i)
12568     Decls.push_back(Record.readDeclAs<ValueDecl>());
12569   C->setUniqueDecls(Decls);
12570 
12571   SmallVector<unsigned, 16> ListsPerDecl;
12572   ListsPerDecl.reserve(UniqueDecls);
12573   for (unsigned i = 0; i < UniqueDecls; ++i)
12574     ListsPerDecl.push_back(Record.readInt());
12575   C->setDeclNumLists(ListsPerDecl);
12576 
12577   SmallVector<unsigned, 32> ListSizes;
12578   ListSizes.reserve(TotalLists);
12579   for (unsigned i = 0; i < TotalLists; ++i)
12580     ListSizes.push_back(Record.readInt());
12581   C->setComponentListSizes(ListSizes);
12582 
12583   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12584   Components.reserve(TotalComponents);
12585   for (unsigned i = 0; i < TotalComponents; ++i) {
12586     Expr *AssociatedExpr = Record.readSubExpr();
12587     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12588     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12589         AssociatedExpr, AssociatedDecl));
12590   }
12591   C->setComponents(Components, ListSizes);
12592 }
12593 
12594 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12595   C->setLParenLoc(Record.readSourceLocation());
12596   unsigned NumVars = C->varlist_size();
12597   SmallVector<Expr *, 16> Vars;
12598   Vars.reserve(NumVars);
12599   for (unsigned i = 0; i != NumVars; ++i)
12600     Vars.push_back(Record.readSubExpr());
12601   C->setVarRefs(Vars);
12602   Vars.clear();
12603   Vars.reserve(NumVars);
12604   for (unsigned i = 0; i != NumVars; ++i)
12605     Vars.push_back(Record.readSubExpr());
12606   C->setPrivateRefs(Vars);
12607 }
12608 
12609 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12610   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12611   C->setLParenLoc(Record.readSourceLocation());
12612   C->setKindKwLoc(Record.readSourceLocation());
12613 }
12614 
12615 OMPTraitInfo ASTRecordReader::readOMPTraitInfo() {
12616   OMPTraitInfo TI;
12617   TI.Sets.resize(readUInt32());
12618   for (auto &Set : TI.Sets) {
12619     Set.Kind = readEnum<llvm::omp::TraitSet>();
12620     Set.Selectors.resize(readUInt32());
12621     for (auto &Selector : Set.Selectors) {
12622       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12623       Selector.ScoreOrCondition = nullptr;
12624       if (readBool())
12625         Selector.ScoreOrCondition = readExprRef();
12626       Selector.Properties.resize(readUInt32());
12627       for (auto &Property : Selector.Properties)
12628         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12629     }
12630   }
12631   return TI;
12632 }
12633