1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTStructuralEquivalence.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/AbstractTypeReader.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/DeclarationName.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
31 #include "clang/AST/ExternalASTSource.h"
32 #include "clang/AST/NestedNameSpecifier.h"
33 #include "clang/AST/ODRDiagsEmitter.h"
34 #include "clang/AST/ODRHash.h"
35 #include "clang/AST/OpenMPClause.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/DiagnosticError.h"
46 #include "clang/Basic/DiagnosticOptions.h"
47 #include "clang/Basic/DiagnosticSema.h"
48 #include "clang/Basic/ExceptionSpecificationType.h"
49 #include "clang/Basic/FileManager.h"
50 #include "clang/Basic/FileSystemOptions.h"
51 #include "clang/Basic/IdentifierTable.h"
52 #include "clang/Basic/LLVM.h"
53 #include "clang/Basic/LangOptions.h"
54 #include "clang/Basic/Module.h"
55 #include "clang/Basic/ObjCRuntime.h"
56 #include "clang/Basic/OpenMPKinds.h"
57 #include "clang/Basic/OperatorKinds.h"
58 #include "clang/Basic/PragmaKinds.h"
59 #include "clang/Basic/Sanitizers.h"
60 #include "clang/Basic/SourceLocation.h"
61 #include "clang/Basic/SourceManager.h"
62 #include "clang/Basic/SourceManagerInternals.h"
63 #include "clang/Basic/Specifiers.h"
64 #include "clang/Basic/TargetInfo.h"
65 #include "clang/Basic/TargetOptions.h"
66 #include "clang/Basic/TokenKinds.h"
67 #include "clang/Basic/Version.h"
68 #include "clang/Lex/HeaderSearch.h"
69 #include "clang/Lex/HeaderSearchOptions.h"
70 #include "clang/Lex/MacroInfo.h"
71 #include "clang/Lex/ModuleMap.h"
72 #include "clang/Lex/PreprocessingRecord.h"
73 #include "clang/Lex/Preprocessor.h"
74 #include "clang/Lex/PreprocessorOptions.h"
75 #include "clang/Lex/Token.h"
76 #include "clang/Sema/ObjCMethodList.h"
77 #include "clang/Sema/Scope.h"
78 #include "clang/Sema/Sema.h"
79 #include "clang/Sema/Weak.h"
80 #include "clang/Serialization/ASTBitCodes.h"
81 #include "clang/Serialization/ASTDeserializationListener.h"
82 #include "clang/Serialization/ASTRecordReader.h"
83 #include "clang/Serialization/ContinuousRangeMap.h"
84 #include "clang/Serialization/GlobalModuleIndex.h"
85 #include "clang/Serialization/InMemoryModuleCache.h"
86 #include "clang/Serialization/ModuleFile.h"
87 #include "clang/Serialization/ModuleFileExtension.h"
88 #include "clang/Serialization/ModuleManager.h"
89 #include "clang/Serialization/PCHContainerOperations.h"
90 #include "clang/Serialization/SerializationDiagnostic.h"
91 #include "llvm/ADT/APFloat.h"
92 #include "llvm/ADT/APInt.h"
93 #include "llvm/ADT/APSInt.h"
94 #include "llvm/ADT/ArrayRef.h"
95 #include "llvm/ADT/DenseMap.h"
96 #include "llvm/ADT/FloatingPointMode.h"
97 #include "llvm/ADT/FoldingSet.h"
98 #include "llvm/ADT/Hashing.h"
99 #include "llvm/ADT/IntrusiveRefCntPtr.h"
100 #include "llvm/ADT/STLExtras.h"
101 #include "llvm/ADT/ScopeExit.h"
102 #include "llvm/ADT/SmallPtrSet.h"
103 #include "llvm/ADT/SmallString.h"
104 #include "llvm/ADT/SmallVector.h"
105 #include "llvm/ADT/StringExtras.h"
106 #include "llvm/ADT/StringMap.h"
107 #include "llvm/ADT/StringRef.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/LEB128.h"
119 #include "llvm/Support/MemoryBuffer.h"
120 #include "llvm/Support/Path.h"
121 #include "llvm/Support/SaveAndRestore.h"
122 #include "llvm/Support/TimeProfiler.h"
123 #include "llvm/Support/Timer.h"
124 #include "llvm/Support/VersionTuple.h"
125 #include "llvm/Support/raw_ostream.h"
126 #include "llvm/TargetParser/Triple.h"
127 #include <algorithm>
128 #include <cassert>
129 #include <cstddef>
130 #include <cstdint>
131 #include <cstdio>
132 #include <ctime>
133 #include <iterator>
134 #include <limits>
135 #include <map>
136 #include <memory>
137 #include <optional>
138 #include <string>
139 #include <system_error>
140 #include <tuple>
141 #include <utility>
142 #include <vector>
143 
144 using namespace clang;
145 using namespace clang::serialization;
146 using namespace clang::serialization::reader;
147 using llvm::BitstreamCursor;
148 
149 //===----------------------------------------------------------------------===//
150 // ChainedASTReaderListener implementation
151 //===----------------------------------------------------------------------===//
152 
153 bool
ReadFullVersionInformation(StringRef FullVersion)154 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155   return First->ReadFullVersionInformation(FullVersion) ||
156          Second->ReadFullVersionInformation(FullVersion);
157 }
158 
ReadModuleName(StringRef ModuleName)159 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160   First->ReadModuleName(ModuleName);
161   Second->ReadModuleName(ModuleName);
162 }
163 
ReadModuleMapFile(StringRef ModuleMapPath)164 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165   First->ReadModuleMapFile(ModuleMapPath);
166   Second->ReadModuleMapFile(ModuleMapPath);
167 }
168 
169 bool
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)170 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
171                                               bool Complain,
172                                               bool AllowCompatibleDifferences) {
173   return First->ReadLanguageOptions(LangOpts, Complain,
174                                     AllowCompatibleDifferences) ||
175          Second->ReadLanguageOptions(LangOpts, Complain,
176                                      AllowCompatibleDifferences);
177 }
178 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain,bool AllowCompatibleDifferences)179 bool ChainedASTReaderListener::ReadTargetOptions(
180     const TargetOptions &TargetOpts, bool Complain,
181     bool AllowCompatibleDifferences) {
182   return First->ReadTargetOptions(TargetOpts, Complain,
183                                   AllowCompatibleDifferences) ||
184          Second->ReadTargetOptions(TargetOpts, Complain,
185                                    AllowCompatibleDifferences);
186 }
187 
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)188 bool ChainedASTReaderListener::ReadDiagnosticOptions(
189     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
190   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
191          Second->ReadDiagnosticOptions(DiagOpts, Complain);
192 }
193 
194 bool
ReadFileSystemOptions(const FileSystemOptions & FSOpts,bool Complain)195 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
196                                                 bool Complain) {
197   return First->ReadFileSystemOptions(FSOpts, Complain) ||
198          Second->ReadFileSystemOptions(FSOpts, Complain);
199 }
200 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,StringRef SpecificModuleCachePath,bool Complain)201 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
202     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
203     bool Complain) {
204   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
205                                         Complain) ||
206          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
207                                          Complain);
208 }
209 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool ReadMacros,bool Complain,std::string & SuggestedPredefines)210 bool ChainedASTReaderListener::ReadPreprocessorOptions(
211     const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
212     std::string &SuggestedPredefines) {
213   return First->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
214                                         SuggestedPredefines) ||
215          Second->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
216                                          SuggestedPredefines);
217 }
218 
ReadCounter(const serialization::ModuleFile & M,unsigned Value)219 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
220                                            unsigned Value) {
221   First->ReadCounter(M, Value);
222   Second->ReadCounter(M, Value);
223 }
224 
needsInputFileVisitation()225 bool ChainedASTReaderListener::needsInputFileVisitation() {
226   return First->needsInputFileVisitation() ||
227          Second->needsInputFileVisitation();
228 }
229 
needsSystemInputFileVisitation()230 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
231   return First->needsSystemInputFileVisitation() ||
232   Second->needsSystemInputFileVisitation();
233 }
234 
visitModuleFile(StringRef Filename,ModuleKind Kind)235 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
236                                                ModuleKind Kind) {
237   First->visitModuleFile(Filename, Kind);
238   Second->visitModuleFile(Filename, Kind);
239 }
240 
visitInputFile(StringRef Filename,bool isSystem,bool isOverridden,bool isExplicitModule)241 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
242                                               bool isSystem,
243                                               bool isOverridden,
244                                               bool isExplicitModule) {
245   bool Continue = false;
246   if (First->needsInputFileVisitation() &&
247       (!isSystem || First->needsSystemInputFileVisitation()))
248     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
249                                       isExplicitModule);
250   if (Second->needsInputFileVisitation() &&
251       (!isSystem || Second->needsSystemInputFileVisitation()))
252     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
253                                        isExplicitModule);
254   return Continue;
255 }
256 
readModuleFileExtension(const ModuleFileExtensionMetadata & Metadata)257 void ChainedASTReaderListener::readModuleFileExtension(
258        const ModuleFileExtensionMetadata &Metadata) {
259   First->readModuleFileExtension(Metadata);
260   Second->readModuleFileExtension(Metadata);
261 }
262 
263 //===----------------------------------------------------------------------===//
264 // PCH validator implementation
265 //===----------------------------------------------------------------------===//
266 
267 ASTReaderListener::~ASTReaderListener() = default;
268 
269 /// Compare the given set of language options against an existing set of
270 /// language options.
271 ///
272 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
273 /// \param AllowCompatibleDifferences If true, differences between compatible
274 ///        language options will be permitted.
275 ///
276 /// \returns true if the languagae options mis-match, false otherwise.
checkLanguageOptions(const LangOptions & LangOpts,const LangOptions & ExistingLangOpts,DiagnosticsEngine * Diags,bool AllowCompatibleDifferences=true)277 static bool checkLanguageOptions(const LangOptions &LangOpts,
278                                  const LangOptions &ExistingLangOpts,
279                                  DiagnosticsEngine *Diags,
280                                  bool AllowCompatibleDifferences = true) {
281 #define LANGOPT(Name, Bits, Default, Description)                   \
282   if (ExistingLangOpts.Name != LangOpts.Name) {                     \
283     if (Diags) {                                                    \
284       if (Bits == 1)                                                \
285         Diags->Report(diag::err_pch_langopt_mismatch)               \
286           << Description << LangOpts.Name << ExistingLangOpts.Name; \
287       else                                                          \
288         Diags->Report(diag::err_pch_langopt_value_mismatch)         \
289           << Description;                                           \
290     }                                                               \
291     return true;                                                    \
292   }
293 
294 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
295   if (ExistingLangOpts.Name != LangOpts.Name) {           \
296     if (Diags)                                            \
297       Diags->Report(diag::err_pch_langopt_value_mismatch) \
298         << Description;                                   \
299     return true;                                          \
300   }
301 
302 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
303   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
304     if (Diags)                                                 \
305       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
306         << Description;                                        \
307     return true;                                               \
308   }
309 
310 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
311   if (!AllowCompatibleDifferences)                            \
312     LANGOPT(Name, Bits, Default, Description)
313 
314 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
315   if (!AllowCompatibleDifferences)                                 \
316     ENUM_LANGOPT(Name, Bits, Default, Description)
317 
318 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
319   if (!AllowCompatibleDifferences)                                 \
320     VALUE_LANGOPT(Name, Bits, Default, Description)
321 
322 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
323 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
324 #define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
325 #include "clang/Basic/LangOptions.def"
326 
327   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
328     if (Diags)
329       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
330     return true;
331   }
332 
333   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
334     if (Diags)
335       Diags->Report(diag::err_pch_langopt_value_mismatch)
336       << "target Objective-C runtime";
337     return true;
338   }
339 
340   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
341       LangOpts.CommentOpts.BlockCommandNames) {
342     if (Diags)
343       Diags->Report(diag::err_pch_langopt_value_mismatch)
344         << "block command names";
345     return true;
346   }
347 
348   // Sanitizer feature mismatches are treated as compatible differences. If
349   // compatible differences aren't allowed, we still only want to check for
350   // mismatches of non-modular sanitizers (the only ones which can affect AST
351   // generation).
352   if (!AllowCompatibleDifferences) {
353     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
354     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
355     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
356     ExistingSanitizers.clear(ModularSanitizers);
357     ImportedSanitizers.clear(ModularSanitizers);
358     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
359       const std::string Flag = "-fsanitize=";
360       if (Diags) {
361 #define SANITIZER(NAME, ID)                                                    \
362   {                                                                            \
363     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
364     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
365     if (InExistingModule != InImportedModule)                                  \
366       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
367           << InExistingModule << (Flag + NAME);                                \
368   }
369 #include "clang/Basic/Sanitizers.def"
370       }
371       return true;
372     }
373   }
374 
375   return false;
376 }
377 
378 /// Compare the given set of target options against an existing set of
379 /// target options.
380 ///
381 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
382 ///
383 /// \returns true if the target options mis-match, false otherwise.
checkTargetOptions(const TargetOptions & TargetOpts,const TargetOptions & ExistingTargetOpts,DiagnosticsEngine * Diags,bool AllowCompatibleDifferences=true)384 static bool checkTargetOptions(const TargetOptions &TargetOpts,
385                                const TargetOptions &ExistingTargetOpts,
386                                DiagnosticsEngine *Diags,
387                                bool AllowCompatibleDifferences = true) {
388 #define CHECK_TARGET_OPT(Field, Name)                             \
389   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
390     if (Diags)                                                    \
391       Diags->Report(diag::err_pch_targetopt_mismatch)             \
392         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
393     return true;                                                  \
394   }
395 
396   // The triple and ABI must match exactly.
397   CHECK_TARGET_OPT(Triple, "target");
398   CHECK_TARGET_OPT(ABI, "target ABI");
399 
400   // We can tolerate different CPUs in many cases, notably when one CPU
401   // supports a strict superset of another. When allowing compatible
402   // differences skip this check.
403   if (!AllowCompatibleDifferences) {
404     CHECK_TARGET_OPT(CPU, "target CPU");
405     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
406   }
407 
408 #undef CHECK_TARGET_OPT
409 
410   // Compare feature sets.
411   SmallVector<StringRef, 4> ExistingFeatures(
412                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
413                                              ExistingTargetOpts.FeaturesAsWritten.end());
414   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
415                                          TargetOpts.FeaturesAsWritten.end());
416   llvm::sort(ExistingFeatures);
417   llvm::sort(ReadFeatures);
418 
419   // We compute the set difference in both directions explicitly so that we can
420   // diagnose the differences differently.
421   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
422   std::set_difference(
423       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
424       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
425   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
426                       ExistingFeatures.begin(), ExistingFeatures.end(),
427                       std::back_inserter(UnmatchedReadFeatures));
428 
429   // If we are allowing compatible differences and the read feature set is
430   // a strict subset of the existing feature set, there is nothing to diagnose.
431   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
432     return false;
433 
434   if (Diags) {
435     for (StringRef Feature : UnmatchedReadFeatures)
436       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
437           << /* is-existing-feature */ false << Feature;
438     for (StringRef Feature : UnmatchedExistingFeatures)
439       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
440           << /* is-existing-feature */ true << Feature;
441   }
442 
443   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
444 }
445 
446 bool
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)447 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
448                                   bool Complain,
449                                   bool AllowCompatibleDifferences) {
450   const LangOptions &ExistingLangOpts = PP.getLangOpts();
451   return checkLanguageOptions(LangOpts, ExistingLangOpts,
452                               Complain ? &Reader.Diags : nullptr,
453                               AllowCompatibleDifferences);
454 }
455 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain,bool AllowCompatibleDifferences)456 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
457                                      bool Complain,
458                                      bool AllowCompatibleDifferences) {
459   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
460   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
461                             Complain ? &Reader.Diags : nullptr,
462                             AllowCompatibleDifferences);
463 }
464 
465 namespace {
466 
467 using MacroDefinitionsMap =
468     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
469 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
470 
471 } // namespace
472 
checkDiagnosticGroupMappings(DiagnosticsEngine & StoredDiags,DiagnosticsEngine & Diags,bool Complain)473 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
474                                          DiagnosticsEngine &Diags,
475                                          bool Complain) {
476   using Level = DiagnosticsEngine::Level;
477 
478   // Check current mappings for new -Werror mappings, and the stored mappings
479   // for cases that were explicitly mapped to *not* be errors that are now
480   // errors because of options like -Werror.
481   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
482 
483   for (DiagnosticsEngine *MappingSource : MappingSources) {
484     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
485       diag::kind DiagID = DiagIDMappingPair.first;
486       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
487       if (CurLevel < DiagnosticsEngine::Error)
488         continue; // not significant
489       Level StoredLevel =
490           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
491       if (StoredLevel < DiagnosticsEngine::Error) {
492         if (Complain)
493           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
494               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
495         return true;
496       }
497     }
498   }
499 
500   return false;
501 }
502 
isExtHandlingFromDiagsError(DiagnosticsEngine & Diags)503 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
504   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
505   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
506     return true;
507   return Ext >= diag::Severity::Error;
508 }
509 
checkDiagnosticMappings(DiagnosticsEngine & StoredDiags,DiagnosticsEngine & Diags,bool IsSystem,bool SystemHeaderWarningsInModule,bool Complain)510 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
511                                     DiagnosticsEngine &Diags, bool IsSystem,
512                                     bool SystemHeaderWarningsInModule,
513                                     bool Complain) {
514   // Top-level options
515   if (IsSystem) {
516     if (Diags.getSuppressSystemWarnings())
517       return false;
518     // If -Wsystem-headers was not enabled before, and it was not explicit,
519     // be conservative
520     if (StoredDiags.getSuppressSystemWarnings() &&
521         !SystemHeaderWarningsInModule) {
522       if (Complain)
523         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
524       return true;
525     }
526   }
527 
528   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
529     if (Complain)
530       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
531     return true;
532   }
533 
534   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
535       !StoredDiags.getEnableAllWarnings()) {
536     if (Complain)
537       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
538     return true;
539   }
540 
541   if (isExtHandlingFromDiagsError(Diags) &&
542       !isExtHandlingFromDiagsError(StoredDiags)) {
543     if (Complain)
544       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
545     return true;
546   }
547 
548   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
549 }
550 
551 /// Return the top import module if it is implicit, nullptr otherwise.
getTopImportImplicitModule(ModuleManager & ModuleMgr,Preprocessor & PP)552 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
553                                           Preprocessor &PP) {
554   // If the original import came from a file explicitly generated by the user,
555   // don't check the diagnostic mappings.
556   // FIXME: currently this is approximated by checking whether this is not a
557   // module import of an implicitly-loaded module file.
558   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
559   // the transitive closure of its imports, since unrelated modules cannot be
560   // imported until after this module finishes validation.
561   ModuleFile *TopImport = &*ModuleMgr.rbegin();
562   while (!TopImport->ImportedBy.empty())
563     TopImport = TopImport->ImportedBy[0];
564   if (TopImport->Kind != MK_ImplicitModule)
565     return nullptr;
566 
567   StringRef ModuleName = TopImport->ModuleName;
568   assert(!ModuleName.empty() && "diagnostic options read before module name");
569 
570   Module *M =
571       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
572   assert(M && "missing module");
573   return M;
574 }
575 
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)576 bool PCHValidator::ReadDiagnosticOptions(
577     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
578   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
579   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
580   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
581       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
582   // This should never fail, because we would have processed these options
583   // before writing them to an ASTFile.
584   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
585 
586   ModuleManager &ModuleMgr = Reader.getModuleManager();
587   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
588 
589   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
590   if (!TopM)
591     return false;
592 
593   Module *Importer = PP.getCurrentModule();
594 
595   DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
596   bool SystemHeaderWarningsInModule =
597       Importer && llvm::is_contained(ExistingOpts.SystemHeaderWarningsModules,
598                                      Importer->Name);
599 
600   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
601   // contains the union of their flags.
602   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
603                                  SystemHeaderWarningsInModule, Complain);
604 }
605 
606 /// Collect the macro definitions provided by the given preprocessor
607 /// options.
608 static void
collectMacroDefinitions(const PreprocessorOptions & PPOpts,MacroDefinitionsMap & Macros,SmallVectorImpl<StringRef> * MacroNames=nullptr)609 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
610                         MacroDefinitionsMap &Macros,
611                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
612   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
613     StringRef Macro = PPOpts.Macros[I].first;
614     bool IsUndef = PPOpts.Macros[I].second;
615 
616     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
617     StringRef MacroName = MacroPair.first;
618     StringRef MacroBody = MacroPair.second;
619 
620     // For an #undef'd macro, we only care about the name.
621     if (IsUndef) {
622       if (MacroNames && !Macros.count(MacroName))
623         MacroNames->push_back(MacroName);
624 
625       Macros[MacroName] = std::make_pair("", true);
626       continue;
627     }
628 
629     // For a #define'd macro, figure out the actual definition.
630     if (MacroName.size() == Macro.size())
631       MacroBody = "1";
632     else {
633       // Note: GCC drops anything following an end-of-line character.
634       StringRef::size_type End = MacroBody.find_first_of("\n\r");
635       MacroBody = MacroBody.substr(0, End);
636     }
637 
638     if (MacroNames && !Macros.count(MacroName))
639       MacroNames->push_back(MacroName);
640     Macros[MacroName] = std::make_pair(MacroBody, false);
641   }
642 }
643 
644 enum OptionValidation {
645   OptionValidateNone,
646   OptionValidateContradictions,
647   OptionValidateStrictMatches,
648 };
649 
650 /// Check the preprocessor options deserialized from the control block
651 /// against the preprocessor options in an existing preprocessor.
652 ///
653 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
654 /// \param Validation If set to OptionValidateNone, ignore differences in
655 ///        preprocessor options. If set to OptionValidateContradictions,
656 ///        require that options passed both in the AST file and on the command
657 ///        line (-D or -U) match, but tolerate options missing in one or the
658 ///        other. If set to OptionValidateContradictions, require that there
659 ///        are no differences in the options between the two.
checkPreprocessorOptions(const PreprocessorOptions & PPOpts,const PreprocessorOptions & ExistingPPOpts,bool ReadMacros,DiagnosticsEngine * Diags,FileManager & FileMgr,std::string & SuggestedPredefines,const LangOptions & LangOpts,OptionValidation Validation=OptionValidateContradictions)660 static bool checkPreprocessorOptions(
661     const PreprocessorOptions &PPOpts,
662     const PreprocessorOptions &ExistingPPOpts, bool ReadMacros,
663     DiagnosticsEngine *Diags, FileManager &FileMgr,
664     std::string &SuggestedPredefines, const LangOptions &LangOpts,
665     OptionValidation Validation = OptionValidateContradictions) {
666   if (ReadMacros) {
667     // Check macro definitions.
668     MacroDefinitionsMap ASTFileMacros;
669     collectMacroDefinitions(PPOpts, ASTFileMacros);
670     MacroDefinitionsMap ExistingMacros;
671     SmallVector<StringRef, 4> ExistingMacroNames;
672     collectMacroDefinitions(ExistingPPOpts, ExistingMacros,
673                             &ExistingMacroNames);
674 
675     // Use a line marker to enter the <command line> file, as the defines and
676     // undefines here will have come from the command line.
677     SuggestedPredefines += "# 1 \"<command line>\" 1\n";
678 
679     for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
680       // Dig out the macro definition in the existing preprocessor options.
681       StringRef MacroName = ExistingMacroNames[I];
682       std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
683 
684       // Check whether we know anything about this macro name or not.
685       llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
686           ASTFileMacros.find(MacroName);
687       if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
688         if (Validation == OptionValidateStrictMatches) {
689           // If strict matches are requested, don't tolerate any extra defines
690           // on the command line that are missing in the AST file.
691           if (Diags) {
692             Diags->Report(diag::err_pch_macro_def_undef) << MacroName << true;
693           }
694           return true;
695         }
696         // FIXME: Check whether this identifier was referenced anywhere in the
697         // AST file. If so, we should reject the AST file. Unfortunately, this
698         // information isn't in the control block. What shall we do about it?
699 
700         if (Existing.second) {
701           SuggestedPredefines += "#undef ";
702           SuggestedPredefines += MacroName.str();
703           SuggestedPredefines += '\n';
704         } else {
705           SuggestedPredefines += "#define ";
706           SuggestedPredefines += MacroName.str();
707           SuggestedPredefines += ' ';
708           SuggestedPredefines += Existing.first.str();
709           SuggestedPredefines += '\n';
710         }
711         continue;
712       }
713 
714       // If the macro was defined in one but undef'd in the other, we have a
715       // conflict.
716       if (Existing.second != Known->second.second) {
717         if (Diags) {
718           Diags->Report(diag::err_pch_macro_def_undef)
719               << MacroName << Known->second.second;
720         }
721         return true;
722       }
723 
724       // If the macro was #undef'd in both, or if the macro bodies are
725       // identical, it's fine.
726       if (Existing.second || Existing.first == Known->second.first) {
727         ASTFileMacros.erase(Known);
728         continue;
729       }
730 
731       // The macro bodies differ; complain.
732       if (Diags) {
733         Diags->Report(diag::err_pch_macro_def_conflict)
734             << MacroName << Known->second.first << Existing.first;
735       }
736       return true;
737     }
738 
739     // Leave the <command line> file and return to <built-in>.
740     SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
741 
742     if (Validation == OptionValidateStrictMatches) {
743       // If strict matches are requested, don't tolerate any extra defines in
744       // the AST file that are missing on the command line.
745       for (const auto &MacroName : ASTFileMacros.keys()) {
746         if (Diags) {
747           Diags->Report(diag::err_pch_macro_def_undef) << MacroName << false;
748         }
749         return true;
750       }
751     }
752   }
753 
754   // Check whether we're using predefines.
755   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
756       Validation != OptionValidateNone) {
757     if (Diags) {
758       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
759     }
760     return true;
761   }
762 
763   // Detailed record is important since it is used for the module cache hash.
764   if (LangOpts.Modules &&
765       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
766       Validation != OptionValidateNone) {
767     if (Diags) {
768       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
769     }
770     return true;
771   }
772 
773   // Compute the #include and #include_macros lines we need.
774   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
775     StringRef File = ExistingPPOpts.Includes[I];
776 
777     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
778         !ExistingPPOpts.PCHThroughHeader.empty()) {
779       // In case the through header is an include, we must add all the includes
780       // to the predefines so the start point can be determined.
781       SuggestedPredefines += "#include \"";
782       SuggestedPredefines += File;
783       SuggestedPredefines += "\"\n";
784       continue;
785     }
786 
787     if (File == ExistingPPOpts.ImplicitPCHInclude)
788       continue;
789 
790     if (llvm::is_contained(PPOpts.Includes, File))
791       continue;
792 
793     SuggestedPredefines += "#include \"";
794     SuggestedPredefines += File;
795     SuggestedPredefines += "\"\n";
796   }
797 
798   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
799     StringRef File = ExistingPPOpts.MacroIncludes[I];
800     if (llvm::is_contained(PPOpts.MacroIncludes, File))
801       continue;
802 
803     SuggestedPredefines += "#__include_macros \"";
804     SuggestedPredefines += File;
805     SuggestedPredefines += "\"\n##\n";
806   }
807 
808   return false;
809 }
810 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool ReadMacros,bool Complain,std::string & SuggestedPredefines)811 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
812                                            bool ReadMacros, bool Complain,
813                                            std::string &SuggestedPredefines) {
814   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
815 
816   return checkPreprocessorOptions(
817       PPOpts, ExistingPPOpts, ReadMacros, Complain ? &Reader.Diags : nullptr,
818       PP.getFileManager(), SuggestedPredefines, PP.getLangOpts());
819 }
820 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool ReadMacros,bool Complain,std::string & SuggestedPredefines)821 bool SimpleASTReaderListener::ReadPreprocessorOptions(
822     const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
823     std::string &SuggestedPredefines) {
824   return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(), ReadMacros,
825                                   nullptr, PP.getFileManager(),
826                                   SuggestedPredefines, PP.getLangOpts(),
827                                   OptionValidateNone);
828 }
829 
830 /// Check the header search options deserialized from the control block
831 /// against the header search options in an existing preprocessor.
832 ///
833 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
checkHeaderSearchOptions(const HeaderSearchOptions & HSOpts,StringRef SpecificModuleCachePath,StringRef ExistingModuleCachePath,DiagnosticsEngine * Diags,const LangOptions & LangOpts,const PreprocessorOptions & PPOpts)834 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
835                                      StringRef SpecificModuleCachePath,
836                                      StringRef ExistingModuleCachePath,
837                                      DiagnosticsEngine *Diags,
838                                      const LangOptions &LangOpts,
839                                      const PreprocessorOptions &PPOpts) {
840   if (LangOpts.Modules) {
841     if (SpecificModuleCachePath != ExistingModuleCachePath &&
842         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
843       if (Diags)
844         Diags->Report(diag::err_pch_modulecache_mismatch)
845           << SpecificModuleCachePath << ExistingModuleCachePath;
846       return true;
847     }
848   }
849 
850   return false;
851 }
852 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,StringRef SpecificModuleCachePath,bool Complain)853 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
854                                            StringRef SpecificModuleCachePath,
855                                            bool Complain) {
856   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
857                                   PP.getHeaderSearchInfo().getModuleCachePath(),
858                                   Complain ? &Reader.Diags : nullptr,
859                                   PP.getLangOpts(), PP.getPreprocessorOpts());
860 }
861 
ReadCounter(const ModuleFile & M,unsigned Value)862 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
863   PP.setCounterValue(Value);
864 }
865 
866 //===----------------------------------------------------------------------===//
867 // AST reader implementation
868 //===----------------------------------------------------------------------===//
869 
readULEB(const unsigned char * & P)870 static uint64_t readULEB(const unsigned char *&P) {
871   unsigned Length = 0;
872   const char *Error = nullptr;
873 
874   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
875   if (Error)
876     llvm::report_fatal_error(Error);
877   P += Length;
878   return Val;
879 }
880 
881 /// Read ULEB-encoded key length and data length.
882 static std::pair<unsigned, unsigned>
readULEBKeyDataLength(const unsigned char * & P)883 readULEBKeyDataLength(const unsigned char *&P) {
884   unsigned KeyLen = readULEB(P);
885   if ((unsigned)KeyLen != KeyLen)
886     llvm::report_fatal_error("key too large");
887 
888   unsigned DataLen = readULEB(P);
889   if ((unsigned)DataLen != DataLen)
890     llvm::report_fatal_error("data too large");
891 
892   return std::make_pair(KeyLen, DataLen);
893 }
894 
setDeserializationListener(ASTDeserializationListener * Listener,bool TakeOwnership)895 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
896                                            bool TakeOwnership) {
897   DeserializationListener = Listener;
898   OwnsDeserializationListener = TakeOwnership;
899 }
900 
ComputeHash(Selector Sel)901 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
902   return serialization::ComputeHash(Sel);
903 }
904 
905 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)906 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
907   return readULEBKeyDataLength(d);
908 }
909 
910 ASTSelectorLookupTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)911 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
912   using namespace llvm::support;
913 
914   SelectorTable &SelTable = Reader.getContext().Selectors;
915   unsigned N =
916       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
917   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
918       F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
919   if (N == 0)
920     return SelTable.getNullarySelector(FirstII);
921   else if (N == 1)
922     return SelTable.getUnarySelector(FirstII);
923 
924   SmallVector<IdentifierInfo *, 16> Args;
925   Args.push_back(FirstII);
926   for (unsigned I = 1; I != N; ++I)
927     Args.push_back(Reader.getLocalIdentifier(
928         F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
929 
930   return SelTable.getSelector(N, Args.data());
931 }
932 
933 ASTSelectorLookupTrait::data_type
ReadData(Selector,const unsigned char * d,unsigned DataLen)934 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
935                                  unsigned DataLen) {
936   using namespace llvm::support;
937 
938   data_type Result;
939 
940   Result.ID = Reader.getGlobalSelectorID(
941       F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
942   unsigned FullInstanceBits =
943       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
944   unsigned FullFactoryBits =
945       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
946   Result.InstanceBits = FullInstanceBits & 0x3;
947   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
948   Result.FactoryBits = FullFactoryBits & 0x3;
949   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
950   unsigned NumInstanceMethods = FullInstanceBits >> 3;
951   unsigned NumFactoryMethods = FullFactoryBits >> 3;
952 
953   // Load instance methods
954   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
955     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
956             F,
957             endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
958       Result.Instance.push_back(Method);
959   }
960 
961   // Load factory methods
962   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
963     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
964             F,
965             endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
966       Result.Factory.push_back(Method);
967   }
968 
969   return Result;
970 }
971 
ComputeHash(const internal_key_type & a)972 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
973   return llvm::djbHash(a);
974 }
975 
976 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)977 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
978   return readULEBKeyDataLength(d);
979 }
980 
981 ASTIdentifierLookupTraitBase::internal_key_type
ReadKey(const unsigned char * d,unsigned n)982 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
983   assert(n >= 2 && d[n-1] == '\0');
984   return StringRef((const char*) d, n-1);
985 }
986 
987 /// Whether the given identifier is "interesting".
isInterestingIdentifier(ASTReader & Reader,IdentifierInfo & II,bool IsModule)988 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
989                                     bool IsModule) {
990   return II.hadMacroDefinition() || II.isPoisoned() ||
991          (!IsModule && II.getObjCOrBuiltinID()) ||
992          II.hasRevertedTokenIDToIdentifier() ||
993          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
994           II.getFETokenInfo());
995 }
996 
readBit(unsigned & Bits)997 static bool readBit(unsigned &Bits) {
998   bool Value = Bits & 0x1;
999   Bits >>= 1;
1000   return Value;
1001 }
1002 
ReadIdentifierID(const unsigned char * d)1003 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1004   using namespace llvm::support;
1005 
1006   unsigned RawID =
1007       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1008   return Reader.getGlobalIdentifierID(F, RawID >> 1);
1009 }
1010 
markIdentifierFromAST(ASTReader & Reader,IdentifierInfo & II)1011 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
1012   if (!II.isFromAST()) {
1013     II.setIsFromAST();
1014     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1015     if (isInterestingIdentifier(Reader, II, IsModule))
1016       II.setChangedSinceDeserialization();
1017   }
1018 }
1019 
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)1020 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1021                                                    const unsigned char* d,
1022                                                    unsigned DataLen) {
1023   using namespace llvm::support;
1024 
1025   unsigned RawID =
1026       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1027   bool IsInteresting = RawID & 0x01;
1028 
1029   // Wipe out the "is interesting" bit.
1030   RawID = RawID >> 1;
1031 
1032   // Build the IdentifierInfo and link the identifier ID with it.
1033   IdentifierInfo *II = KnownII;
1034   if (!II) {
1035     II = &Reader.getIdentifierTable().getOwn(k);
1036     KnownII = II;
1037   }
1038   markIdentifierFromAST(Reader, *II);
1039   Reader.markIdentifierUpToDate(II);
1040 
1041   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
1042   if (!IsInteresting) {
1043     // For uninteresting identifiers, there's nothing else to do. Just notify
1044     // the reader that we've finished loading this identifier.
1045     Reader.SetIdentifierInfo(ID, II);
1046     return II;
1047   }
1048 
1049   unsigned ObjCOrBuiltinID =
1050       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1051   unsigned Bits =
1052       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1053   bool CPlusPlusOperatorKeyword = readBit(Bits);
1054   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1055   bool Poisoned = readBit(Bits);
1056   bool ExtensionToken = readBit(Bits);
1057   bool HadMacroDefinition = readBit(Bits);
1058 
1059   assert(Bits == 0 && "Extra bits in the identifier?");
1060   DataLen -= 8;
1061 
1062   // Set or check the various bits in the IdentifierInfo structure.
1063   // Token IDs are read-only.
1064   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1065     II->revertTokenIDToIdentifier();
1066   if (!F.isModule())
1067     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1068   assert(II->isExtensionToken() == ExtensionToken &&
1069          "Incorrect extension token flag");
1070   (void)ExtensionToken;
1071   if (Poisoned)
1072     II->setIsPoisoned(true);
1073   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1074          "Incorrect C++ operator keyword flag");
1075   (void)CPlusPlusOperatorKeyword;
1076 
1077   // If this identifier is a macro, deserialize the macro
1078   // definition.
1079   if (HadMacroDefinition) {
1080     uint32_t MacroDirectivesOffset =
1081         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1082     DataLen -= 4;
1083 
1084     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1085   }
1086 
1087   Reader.SetIdentifierInfo(ID, II);
1088 
1089   // Read all of the declarations visible at global scope with this
1090   // name.
1091   if (DataLen > 0) {
1092     SmallVector<uint32_t, 4> DeclIDs;
1093     for (; DataLen > 0; DataLen -= 4)
1094       DeclIDs.push_back(Reader.getGlobalDeclID(
1095           F,
1096           endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
1097     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1098   }
1099 
1100   return II;
1101 }
1102 
DeclarationNameKey(DeclarationName Name)1103 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1104     : Kind(Name.getNameKind()) {
1105   switch (Kind) {
1106   case DeclarationName::Identifier:
1107     Data = (uint64_t)Name.getAsIdentifierInfo();
1108     break;
1109   case DeclarationName::ObjCZeroArgSelector:
1110   case DeclarationName::ObjCOneArgSelector:
1111   case DeclarationName::ObjCMultiArgSelector:
1112     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1113     break;
1114   case DeclarationName::CXXOperatorName:
1115     Data = Name.getCXXOverloadedOperator();
1116     break;
1117   case DeclarationName::CXXLiteralOperatorName:
1118     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1119     break;
1120   case DeclarationName::CXXDeductionGuideName:
1121     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1122                ->getDeclName().getAsIdentifierInfo();
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 
getHash() const1133 unsigned DeclarationNameKey::getHash() const {
1134   llvm::FoldingSetNodeID ID;
1135   ID.AddInteger(Kind);
1136 
1137   switch (Kind) {
1138   case DeclarationName::Identifier:
1139   case DeclarationName::CXXLiteralOperatorName:
1140   case DeclarationName::CXXDeductionGuideName:
1141     ID.AddString(((IdentifierInfo*)Data)->getName());
1142     break;
1143   case DeclarationName::ObjCZeroArgSelector:
1144   case DeclarationName::ObjCOneArgSelector:
1145   case DeclarationName::ObjCMultiArgSelector:
1146     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1147     break;
1148   case DeclarationName::CXXOperatorName:
1149     ID.AddInteger((OverloadedOperatorKind)Data);
1150     break;
1151   case DeclarationName::CXXConstructorName:
1152   case DeclarationName::CXXDestructorName:
1153   case DeclarationName::CXXConversionFunctionName:
1154   case DeclarationName::CXXUsingDirective:
1155     break;
1156   }
1157 
1158   return ID.ComputeHash();
1159 }
1160 
1161 ModuleFile *
ReadFileRef(const unsigned char * & d)1162 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1163   using namespace llvm::support;
1164 
1165   uint32_t ModuleFileID =
1166       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1167   return Reader.getLocalModuleFile(F, ModuleFileID);
1168 }
1169 
1170 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)1171 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1172   return readULEBKeyDataLength(d);
1173 }
1174 
1175 ASTDeclContextNameLookupTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)1176 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1177   using namespace llvm::support;
1178 
1179   auto Kind = (DeclarationName::NameKind)*d++;
1180   uint64_t Data;
1181   switch (Kind) {
1182   case DeclarationName::Identifier:
1183   case DeclarationName::CXXLiteralOperatorName:
1184   case DeclarationName::CXXDeductionGuideName:
1185     Data = (uint64_t)Reader.getLocalIdentifier(
1186         F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
1187     break;
1188   case DeclarationName::ObjCZeroArgSelector:
1189   case DeclarationName::ObjCOneArgSelector:
1190   case DeclarationName::ObjCMultiArgSelector:
1191     Data =
1192         (uint64_t)Reader
1193             .getLocalSelector(
1194                 F,
1195                 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(
1196                     d))
1197             .getAsOpaquePtr();
1198     break;
1199   case DeclarationName::CXXOperatorName:
1200     Data = *d++; // OverloadedOperatorKind
1201     break;
1202   case DeclarationName::CXXConstructorName:
1203   case DeclarationName::CXXDestructorName:
1204   case DeclarationName::CXXConversionFunctionName:
1205   case DeclarationName::CXXUsingDirective:
1206     Data = 0;
1207     break;
1208   }
1209 
1210   return DeclarationNameKey(Kind, Data);
1211 }
1212 
ReadDataInto(internal_key_type,const unsigned char * d,unsigned DataLen,data_type_builder & Val)1213 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1214                                                  const unsigned char *d,
1215                                                  unsigned DataLen,
1216                                                  data_type_builder &Val) {
1217   using namespace llvm::support;
1218 
1219   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1220     uint32_t LocalID =
1221         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1222     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1223   }
1224 }
1225 
ReadLexicalDeclContextStorage(ModuleFile & M,BitstreamCursor & Cursor,uint64_t Offset,DeclContext * DC)1226 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1227                                               BitstreamCursor &Cursor,
1228                                               uint64_t Offset,
1229                                               DeclContext *DC) {
1230   assert(Offset != 0);
1231 
1232   SavedStreamPosition SavedPosition(Cursor);
1233   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1234     Error(std::move(Err));
1235     return true;
1236   }
1237 
1238   RecordData Record;
1239   StringRef Blob;
1240   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1241   if (!MaybeCode) {
1242     Error(MaybeCode.takeError());
1243     return true;
1244   }
1245   unsigned Code = MaybeCode.get();
1246 
1247   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1248   if (!MaybeRecCode) {
1249     Error(MaybeRecCode.takeError());
1250     return true;
1251   }
1252   unsigned RecCode = MaybeRecCode.get();
1253   if (RecCode != DECL_CONTEXT_LEXICAL) {
1254     Error("Expected lexical block");
1255     return true;
1256   }
1257 
1258   assert(!isa<TranslationUnitDecl>(DC) &&
1259          "expected a TU_UPDATE_LEXICAL record for TU");
1260   // If we are handling a C++ class template instantiation, we can see multiple
1261   // lexical updates for the same record. It's important that we select only one
1262   // of them, so that field numbering works properly. Just pick the first one we
1263   // see.
1264   auto &Lex = LexicalDecls[DC];
1265   if (!Lex.first) {
1266     Lex = std::make_pair(
1267         &M, llvm::ArrayRef(
1268                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1269                     Blob.data()),
1270                 Blob.size() / 4));
1271   }
1272   DC->setHasExternalLexicalStorage(true);
1273   return false;
1274 }
1275 
ReadVisibleDeclContextStorage(ModuleFile & M,BitstreamCursor & Cursor,uint64_t Offset,DeclID ID)1276 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1277                                               BitstreamCursor &Cursor,
1278                                               uint64_t Offset,
1279                                               DeclID ID) {
1280   assert(Offset != 0);
1281 
1282   SavedStreamPosition SavedPosition(Cursor);
1283   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1284     Error(std::move(Err));
1285     return true;
1286   }
1287 
1288   RecordData Record;
1289   StringRef Blob;
1290   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1291   if (!MaybeCode) {
1292     Error(MaybeCode.takeError());
1293     return true;
1294   }
1295   unsigned Code = MaybeCode.get();
1296 
1297   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1298   if (!MaybeRecCode) {
1299     Error(MaybeRecCode.takeError());
1300     return true;
1301   }
1302   unsigned RecCode = MaybeRecCode.get();
1303   if (RecCode != DECL_CONTEXT_VISIBLE) {
1304     Error("Expected visible lookup table block");
1305     return true;
1306   }
1307 
1308   // We can't safely determine the primary context yet, so delay attaching the
1309   // lookup table until we're done with recursive deserialization.
1310   auto *Data = (const unsigned char*)Blob.data();
1311   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1312   return false;
1313 }
1314 
Error(StringRef Msg) const1315 void ASTReader::Error(StringRef Msg) const {
1316   Error(diag::err_fe_pch_malformed, Msg);
1317   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1318       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1319     Diag(diag::note_module_cache_path)
1320       << PP.getHeaderSearchInfo().getModuleCachePath();
1321   }
1322 }
1323 
Error(unsigned DiagID,StringRef Arg1,StringRef Arg2,StringRef Arg3) const1324 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1325                       StringRef Arg3) const {
1326   if (Diags.isDiagnosticInFlight())
1327     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1328   else
1329     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1330 }
1331 
Error(llvm::Error && Err) const1332 void ASTReader::Error(llvm::Error &&Err) const {
1333   llvm::Error RemainingErr =
1334       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1335         auto Diag = E.getDiagnostic().second;
1336 
1337         // Ideally we'd just emit it, but have to handle a possible in-flight
1338         // diagnostic. Note that the location is currently ignored as well.
1339         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1340         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1341         StringRef Arg1, Arg2, Arg3;
1342         switch (NumArgs) {
1343         case 3:
1344           Arg3 = Diag.getStringArg(2);
1345           [[fallthrough]];
1346         case 2:
1347           Arg2 = Diag.getStringArg(1);
1348           [[fallthrough]];
1349         case 1:
1350           Arg1 = Diag.getStringArg(0);
1351         }
1352         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1353       });
1354   if (RemainingErr)
1355     Error(toString(std::move(RemainingErr)));
1356 }
1357 
1358 //===----------------------------------------------------------------------===//
1359 // Source Manager Deserialization
1360 //===----------------------------------------------------------------------===//
1361 
1362 /// Read the line table in the source manager block.
ParseLineTable(ModuleFile & F,const RecordData & Record)1363 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1364   unsigned Idx = 0;
1365   LineTableInfo &LineTable = SourceMgr.getLineTable();
1366 
1367   // Parse the file names
1368   std::map<int, int> FileIDs;
1369   FileIDs[-1] = -1; // For unspecified filenames.
1370   for (unsigned I = 0; Record[Idx]; ++I) {
1371     // Extract the file name
1372     auto Filename = ReadPath(F, Record, Idx);
1373     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1374   }
1375   ++Idx;
1376 
1377   // Parse the line entries
1378   std::vector<LineEntry> Entries;
1379   while (Idx < Record.size()) {
1380     FileID FID = ReadFileID(F, Record, Idx);
1381 
1382     // Extract the line entries
1383     unsigned NumEntries = Record[Idx++];
1384     assert(NumEntries && "no line entries for file ID");
1385     Entries.clear();
1386     Entries.reserve(NumEntries);
1387     for (unsigned I = 0; I != NumEntries; ++I) {
1388       unsigned FileOffset = Record[Idx++];
1389       unsigned LineNo = Record[Idx++];
1390       int FilenameID = FileIDs[Record[Idx++]];
1391       SrcMgr::CharacteristicKind FileKind
1392         = (SrcMgr::CharacteristicKind)Record[Idx++];
1393       unsigned IncludeOffset = Record[Idx++];
1394       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1395                                        FileKind, IncludeOffset));
1396     }
1397     LineTable.AddEntry(FID, Entries);
1398   }
1399 }
1400 
1401 /// Read a source manager block
ReadSourceManagerBlock(ModuleFile & F)1402 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1403   using namespace SrcMgr;
1404 
1405   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1406 
1407   // Set the source-location entry cursor to the current position in
1408   // the stream. This cursor will be used to read the contents of the
1409   // source manager block initially, and then lazily read
1410   // source-location entries as needed.
1411   SLocEntryCursor = F.Stream;
1412 
1413   // The stream itself is going to skip over the source manager block.
1414   if (llvm::Error Err = F.Stream.SkipBlock())
1415     return Err;
1416 
1417   // Enter the source manager block.
1418   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1419     return Err;
1420   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1421 
1422   RecordData Record;
1423   while (true) {
1424     Expected<llvm::BitstreamEntry> MaybeE =
1425         SLocEntryCursor.advanceSkippingSubblocks();
1426     if (!MaybeE)
1427       return MaybeE.takeError();
1428     llvm::BitstreamEntry E = MaybeE.get();
1429 
1430     switch (E.Kind) {
1431     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1432     case llvm::BitstreamEntry::Error:
1433       return llvm::createStringError(std::errc::illegal_byte_sequence,
1434                                      "malformed block record in AST file");
1435     case llvm::BitstreamEntry::EndBlock:
1436       return llvm::Error::success();
1437     case llvm::BitstreamEntry::Record:
1438       // The interesting case.
1439       break;
1440     }
1441 
1442     // Read a record.
1443     Record.clear();
1444     StringRef Blob;
1445     Expected<unsigned> MaybeRecord =
1446         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1447     if (!MaybeRecord)
1448       return MaybeRecord.takeError();
1449     switch (MaybeRecord.get()) {
1450     default:  // Default behavior: ignore.
1451       break;
1452 
1453     case SM_SLOC_FILE_ENTRY:
1454     case SM_SLOC_BUFFER_ENTRY:
1455     case SM_SLOC_EXPANSION_ENTRY:
1456       // Once we hit one of the source location entries, we're done.
1457       return llvm::Error::success();
1458     }
1459   }
1460 }
1461 
1462 llvm::Expected<SourceLocation::UIntTy>
readSLocOffset(ModuleFile * F,unsigned Index)1463 ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1464   BitstreamCursor &Cursor = F->SLocEntryCursor;
1465   SavedStreamPosition SavedPosition(Cursor);
1466   if (llvm::Error Err = Cursor.JumpToBit(F->SLocEntryOffsetsBase +
1467                                          F->SLocEntryOffsets[Index]))
1468     return std::move(Err);
1469 
1470   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1471   if (!MaybeEntry)
1472     return MaybeEntry.takeError();
1473 
1474   llvm::BitstreamEntry Entry = MaybeEntry.get();
1475   if (Entry.Kind != llvm::BitstreamEntry::Record)
1476     return llvm::createStringError(
1477         std::errc::illegal_byte_sequence,
1478         "incorrectly-formatted source location entry in AST file");
1479 
1480   RecordData Record;
1481   StringRef Blob;
1482   Expected<unsigned> MaybeSLOC = Cursor.readRecord(Entry.ID, Record, &Blob);
1483   if (!MaybeSLOC)
1484     return MaybeSLOC.takeError();
1485 
1486   switch (MaybeSLOC.get()) {
1487   default:
1488     return llvm::createStringError(
1489         std::errc::illegal_byte_sequence,
1490         "incorrectly-formatted source location entry in AST file");
1491   case SM_SLOC_FILE_ENTRY:
1492   case SM_SLOC_BUFFER_ENTRY:
1493   case SM_SLOC_EXPANSION_ENTRY:
1494     return F->SLocEntryBaseOffset + Record[0];
1495   }
1496 }
1497 
getSLocEntryID(SourceLocation::UIntTy SLocOffset)1498 int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1499   auto SLocMapI =
1500       GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - SLocOffset - 1);
1501   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1502          "Corrupted global sloc offset map");
1503   ModuleFile *F = SLocMapI->second;
1504 
1505   bool Invalid = false;
1506 
1507   auto It = llvm::upper_bound(
1508       llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,
1509       [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1510         int ID = F->SLocEntryBaseID + LocalIndex;
1511         std::size_t Index = -ID - 2;
1512         if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1513           assert(!SourceMgr.SLocEntryLoaded[Index]);
1514           auto MaybeEntryOffset = readSLocOffset(F, LocalIndex);
1515           if (!MaybeEntryOffset) {
1516             Error(MaybeEntryOffset.takeError());
1517             Invalid = true;
1518             return true;
1519           }
1520           SourceMgr.LoadedSLocEntryTable[Index] =
1521               SrcMgr::SLocEntry::getOffsetOnly(*MaybeEntryOffset);
1522           SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1523         }
1524         return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1525       });
1526 
1527   if (Invalid)
1528     return 0;
1529 
1530   // The iterator points to the first entry with start offset greater than the
1531   // offset of interest. The previous entry must contain the offset of interest.
1532   return F->SLocEntryBaseID + *std::prev(It);
1533 }
1534 
ReadSLocEntry(int ID)1535 bool ASTReader::ReadSLocEntry(int ID) {
1536   if (ID == 0)
1537     return false;
1538 
1539   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1540     Error("source location entry ID out-of-range for AST file");
1541     return true;
1542   }
1543 
1544   // Local helper to read the (possibly-compressed) buffer data following the
1545   // entry record.
1546   auto ReadBuffer = [this](
1547       BitstreamCursor &SLocEntryCursor,
1548       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1549     RecordData Record;
1550     StringRef Blob;
1551     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1552     if (!MaybeCode) {
1553       Error(MaybeCode.takeError());
1554       return nullptr;
1555     }
1556     unsigned Code = MaybeCode.get();
1557 
1558     Expected<unsigned> MaybeRecCode =
1559         SLocEntryCursor.readRecord(Code, Record, &Blob);
1560     if (!MaybeRecCode) {
1561       Error(MaybeRecCode.takeError());
1562       return nullptr;
1563     }
1564     unsigned RecCode = MaybeRecCode.get();
1565 
1566     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1567       // Inspect the first byte to differentiate zlib (\x78) and zstd
1568       // (little-endian 0xFD2FB528).
1569       const llvm::compression::Format F =
1570           Blob.size() > 0 && Blob.data()[0] == 0x78
1571               ? llvm::compression::Format::Zlib
1572               : llvm::compression::Format::Zstd;
1573       if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1574         Error(Reason);
1575         return nullptr;
1576       }
1577       SmallVector<uint8_t, 0> Decompressed;
1578       if (llvm::Error E = llvm::compression::decompress(
1579               F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {
1580         Error("could not decompress embedded file contents: " +
1581               llvm::toString(std::move(E)));
1582         return nullptr;
1583       }
1584       return llvm::MemoryBuffer::getMemBufferCopy(
1585           llvm::toStringRef(Decompressed), Name);
1586     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1587       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1588     } else {
1589       Error("AST record has invalid code");
1590       return nullptr;
1591     }
1592   };
1593 
1594   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1595   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1596           F->SLocEntryOffsetsBase +
1597           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1598     Error(std::move(Err));
1599     return true;
1600   }
1601 
1602   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1603   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1604 
1605   ++NumSLocEntriesRead;
1606   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1607   if (!MaybeEntry) {
1608     Error(MaybeEntry.takeError());
1609     return true;
1610   }
1611   llvm::BitstreamEntry Entry = MaybeEntry.get();
1612 
1613   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1614     Error("incorrectly-formatted source location entry in AST file");
1615     return true;
1616   }
1617 
1618   RecordData Record;
1619   StringRef Blob;
1620   Expected<unsigned> MaybeSLOC =
1621       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1622   if (!MaybeSLOC) {
1623     Error(MaybeSLOC.takeError());
1624     return true;
1625   }
1626   switch (MaybeSLOC.get()) {
1627   default:
1628     Error("incorrectly-formatted source location entry in AST file");
1629     return true;
1630 
1631   case SM_SLOC_FILE_ENTRY: {
1632     // We will detect whether a file changed and return 'Failure' for it, but
1633     // we will also try to fail gracefully by setting up the SLocEntry.
1634     unsigned InputID = Record[4];
1635     InputFile IF = getInputFile(*F, InputID);
1636     OptionalFileEntryRef File = IF.getFile();
1637     bool OverriddenBuffer = IF.isOverridden();
1638 
1639     // Note that we only check if a File was returned. If it was out-of-date
1640     // we have complained but we will continue creating a FileID to recover
1641     // gracefully.
1642     if (!File)
1643       return true;
1644 
1645     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1646     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1647       // This is the module's main file.
1648       IncludeLoc = getImportLocation(F);
1649     }
1650     SrcMgr::CharacteristicKind
1651       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1652     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1653                                         BaseOffset + Record[0]);
1654     SrcMgr::FileInfo &FileInfo =
1655           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1656     FileInfo.NumCreatedFIDs = Record[5];
1657     if (Record[3])
1658       FileInfo.setHasLineDirectives();
1659 
1660     unsigned NumFileDecls = Record[7];
1661     if (NumFileDecls && ContextObj) {
1662       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1663       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1664       FileDeclIDs[FID] =
1665           FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1666     }
1667 
1668     const SrcMgr::ContentCache &ContentCache =
1669         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1670     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1671         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1672         !ContentCache.getBufferIfLoaded()) {
1673       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1674       if (!Buffer)
1675         return true;
1676       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1677     }
1678 
1679     break;
1680   }
1681 
1682   case SM_SLOC_BUFFER_ENTRY: {
1683     const char *Name = Blob.data();
1684     unsigned Offset = Record[0];
1685     SrcMgr::CharacteristicKind
1686       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1687     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1688     if (IncludeLoc.isInvalid() && F->isModule()) {
1689       IncludeLoc = getImportLocation(F);
1690     }
1691 
1692     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1693     if (!Buffer)
1694       return true;
1695     FileID FID = SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1696                                         BaseOffset + Offset, IncludeLoc);
1697     if (Record[3]) {
1698       auto &FileInfo =
1699           const_cast<SrcMgr::FileInfo &>(SourceMgr.getSLocEntry(FID).getFile());
1700       FileInfo.setHasLineDirectives();
1701     }
1702     break;
1703   }
1704 
1705   case SM_SLOC_EXPANSION_ENTRY: {
1706     LocSeq::State Seq;
1707     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq);
1708     SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq);
1709     SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq);
1710     SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,
1711                                  Record[5], Record[4], ID,
1712                                  BaseOffset + Record[0]);
1713     break;
1714   }
1715   }
1716 
1717   return false;
1718 }
1719 
getModuleImportLoc(int ID)1720 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1721   if (ID == 0)
1722     return std::make_pair(SourceLocation(), "");
1723 
1724   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1725     Error("source location entry ID out-of-range for AST file");
1726     return std::make_pair(SourceLocation(), "");
1727   }
1728 
1729   // Find which module file this entry lands in.
1730   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1731   if (!M->isModule())
1732     return std::make_pair(SourceLocation(), "");
1733 
1734   // FIXME: Can we map this down to a particular submodule? That would be
1735   // ideal.
1736   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1737 }
1738 
1739 /// Find the location where the module F is imported.
getImportLocation(ModuleFile * F)1740 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1741   if (F->ImportLoc.isValid())
1742     return F->ImportLoc;
1743 
1744   // Otherwise we have a PCH. It's considered to be "imported" at the first
1745   // location of its includer.
1746   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1747     // Main file is the importer.
1748     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1749     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1750   }
1751   return F->ImportedBy[0]->FirstLoc;
1752 }
1753 
1754 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1755 /// the abbreviations that are at the top of the block and then leave the cursor
1756 /// pointing into the block.
ReadBlockAbbrevs(BitstreamCursor & Cursor,unsigned BlockID,uint64_t * StartOfBlockOffset)1757 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1758                                         unsigned BlockID,
1759                                         uint64_t *StartOfBlockOffset) {
1760   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1761     return Err;
1762 
1763   if (StartOfBlockOffset)
1764     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1765 
1766   while (true) {
1767     uint64_t Offset = Cursor.GetCurrentBitNo();
1768     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1769     if (!MaybeCode)
1770       return MaybeCode.takeError();
1771     unsigned Code = MaybeCode.get();
1772 
1773     // We expect all abbrevs to be at the start of the block.
1774     if (Code != llvm::bitc::DEFINE_ABBREV) {
1775       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1776         return Err;
1777       return llvm::Error::success();
1778     }
1779     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1780       return Err;
1781   }
1782 }
1783 
ReadToken(ModuleFile & M,const RecordDataImpl & Record,unsigned & Idx)1784 Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
1785                            unsigned &Idx) {
1786   Token Tok;
1787   Tok.startToken();
1788   Tok.setLocation(ReadSourceLocation(M, Record, Idx));
1789   Tok.setKind((tok::TokenKind)Record[Idx++]);
1790   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1791 
1792   if (Tok.isAnnotation()) {
1793     Tok.setAnnotationEndLoc(ReadSourceLocation(M, Record, Idx));
1794     switch (Tok.getKind()) {
1795     case tok::annot_pragma_loop_hint: {
1796       auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1797       Info->PragmaName = ReadToken(M, Record, Idx);
1798       Info->Option = ReadToken(M, Record, Idx);
1799       unsigned NumTokens = Record[Idx++];
1800       SmallVector<Token, 4> Toks;
1801       Toks.reserve(NumTokens);
1802       for (unsigned I = 0; I < NumTokens; ++I)
1803         Toks.push_back(ReadToken(M, Record, Idx));
1804       Info->Toks = llvm::ArrayRef(Toks).copy(PP.getPreprocessorAllocator());
1805       Tok.setAnnotationValue(static_cast<void *>(Info));
1806       break;
1807     }
1808     case tok::annot_pragma_pack: {
1809       auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
1810       Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
1811       auto SlotLabel = ReadString(Record, Idx);
1812       Info->SlotLabel =
1813           llvm::StringRef(SlotLabel).copy(PP.getPreprocessorAllocator());
1814       Info->Alignment = ReadToken(M, Record, Idx);
1815       Tok.setAnnotationValue(static_cast<void *>(Info));
1816       break;
1817     }
1818     // Some annotation tokens do not use the PtrData field.
1819     case tok::annot_pragma_openmp:
1820     case tok::annot_pragma_openmp_end:
1821     case tok::annot_pragma_unused:
1822     case tok::annot_pragma_openacc:
1823     case tok::annot_pragma_openacc_end:
1824       break;
1825     default:
1826       llvm_unreachable("missing deserialization code for annotation token");
1827     }
1828   } else {
1829     Tok.setLength(Record[Idx++]);
1830     if (IdentifierInfo *II = getLocalIdentifier(M, Record[Idx++]))
1831       Tok.setIdentifierInfo(II);
1832   }
1833   return Tok;
1834 }
1835 
ReadMacroRecord(ModuleFile & F,uint64_t Offset)1836 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1837   BitstreamCursor &Stream = F.MacroCursor;
1838 
1839   // Keep track of where we are in the stream, then jump back there
1840   // after reading this macro.
1841   SavedStreamPosition SavedPosition(Stream);
1842 
1843   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1844     // FIXME this drops errors on the floor.
1845     consumeError(std::move(Err));
1846     return nullptr;
1847   }
1848   RecordData Record;
1849   SmallVector<IdentifierInfo*, 16> MacroParams;
1850   MacroInfo *Macro = nullptr;
1851   llvm::MutableArrayRef<Token> MacroTokens;
1852 
1853   while (true) {
1854     // Advance to the next record, but if we get to the end of the block, don't
1855     // pop it (removing all the abbreviations from the cursor) since we want to
1856     // be able to reseek within the block and read entries.
1857     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1858     Expected<llvm::BitstreamEntry> MaybeEntry =
1859         Stream.advanceSkippingSubblocks(Flags);
1860     if (!MaybeEntry) {
1861       Error(MaybeEntry.takeError());
1862       return Macro;
1863     }
1864     llvm::BitstreamEntry Entry = MaybeEntry.get();
1865 
1866     switch (Entry.Kind) {
1867     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1868     case llvm::BitstreamEntry::Error:
1869       Error("malformed block record in AST file");
1870       return Macro;
1871     case llvm::BitstreamEntry::EndBlock:
1872       return Macro;
1873     case llvm::BitstreamEntry::Record:
1874       // The interesting case.
1875       break;
1876     }
1877 
1878     // Read a record.
1879     Record.clear();
1880     PreprocessorRecordTypes RecType;
1881     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1882       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1883     else {
1884       Error(MaybeRecType.takeError());
1885       return Macro;
1886     }
1887     switch (RecType) {
1888     case PP_MODULE_MACRO:
1889     case PP_MACRO_DIRECTIVE_HISTORY:
1890       return Macro;
1891 
1892     case PP_MACRO_OBJECT_LIKE:
1893     case PP_MACRO_FUNCTION_LIKE: {
1894       // If we already have a macro, that means that we've hit the end
1895       // of the definition of the macro we were looking for. We're
1896       // done.
1897       if (Macro)
1898         return Macro;
1899 
1900       unsigned NextIndex = 1; // Skip identifier ID.
1901       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1902       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1903       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1904       MI->setIsUsed(Record[NextIndex++]);
1905       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1906       MacroTokens = MI->allocateTokens(Record[NextIndex++],
1907                                        PP.getPreprocessorAllocator());
1908       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1909         // Decode function-like macro info.
1910         bool isC99VarArgs = Record[NextIndex++];
1911         bool isGNUVarArgs = Record[NextIndex++];
1912         bool hasCommaPasting = Record[NextIndex++];
1913         MacroParams.clear();
1914         unsigned NumArgs = Record[NextIndex++];
1915         for (unsigned i = 0; i != NumArgs; ++i)
1916           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1917 
1918         // Install function-like macro info.
1919         MI->setIsFunctionLike();
1920         if (isC99VarArgs) MI->setIsC99Varargs();
1921         if (isGNUVarArgs) MI->setIsGNUVarargs();
1922         if (hasCommaPasting) MI->setHasCommaPasting();
1923         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1924       }
1925 
1926       // Remember that we saw this macro last so that we add the tokens that
1927       // form its body to it.
1928       Macro = MI;
1929 
1930       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1931           Record[NextIndex]) {
1932         // We have a macro definition. Register the association
1933         PreprocessedEntityID
1934             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1935         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1936         PreprocessingRecord::PPEntityID PPID =
1937             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1938         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1939             PPRec.getPreprocessedEntity(PPID));
1940         if (PPDef)
1941           PPRec.RegisterMacroDefinition(Macro, PPDef);
1942       }
1943 
1944       ++NumMacrosRead;
1945       break;
1946     }
1947 
1948     case PP_TOKEN: {
1949       // If we see a TOKEN before a PP_MACRO_*, then the file is
1950       // erroneous, just pretend we didn't see this.
1951       if (!Macro) break;
1952       if (MacroTokens.empty()) {
1953         Error("unexpected number of macro tokens for a macro in AST file");
1954         return Macro;
1955       }
1956 
1957       unsigned Idx = 0;
1958       MacroTokens[0] = ReadToken(F, Record, Idx);
1959       MacroTokens = MacroTokens.drop_front();
1960       break;
1961     }
1962     }
1963   }
1964 }
1965 
1966 PreprocessedEntityID
getGlobalPreprocessedEntityID(ModuleFile & M,unsigned LocalID) const1967 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1968                                          unsigned LocalID) const {
1969   if (!M.ModuleOffsetMap.empty())
1970     ReadModuleOffsetMap(M);
1971 
1972   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1973     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1974   assert(I != M.PreprocessedEntityRemap.end()
1975          && "Invalid index into preprocessed entity index remap");
1976 
1977   return LocalID + I->second;
1978 }
1979 
getFile(const internal_key_type & Key)1980 const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
1981   FileManager &FileMgr = Reader.getFileManager();
1982   if (!Key.Imported) {
1983     if (auto File = FileMgr.getFile(Key.Filename))
1984       return *File;
1985     return nullptr;
1986   }
1987 
1988   std::string Resolved = std::string(Key.Filename);
1989   Reader.ResolveImportedPath(M, Resolved);
1990   if (auto File = FileMgr.getFile(Resolved))
1991     return *File;
1992   return nullptr;
1993 }
1994 
ComputeHash(internal_key_ref ikey)1995 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1996   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1997 }
1998 
1999 HeaderFileInfoTrait::internal_key_type
GetInternalKey(external_key_type ekey)2000 HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2001   internal_key_type ikey = {ekey.getSize(),
2002                             M.HasTimestamps ? ekey.getModificationTime() : 0,
2003                             ekey.getName(), /*Imported*/ false};
2004   return ikey;
2005 }
2006 
EqualKey(internal_key_ref a,internal_key_ref b)2007 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2008   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2009     return false;
2010 
2011   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
2012     return true;
2013 
2014   // Determine whether the actual files are equivalent.
2015   const FileEntry *FEA = getFile(a);
2016   const FileEntry *FEB = getFile(b);
2017   return FEA && FEA == FEB;
2018 }
2019 
2020 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)2021 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2022   return readULEBKeyDataLength(d);
2023 }
2024 
2025 HeaderFileInfoTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)2026 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2027   using namespace llvm::support;
2028 
2029   internal_key_type ikey;
2030   ikey.Size =
2031       off_t(endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2032   ikey.ModTime = time_t(
2033       endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2034   ikey.Filename = (const char *)d;
2035   ikey.Imported = true;
2036   return ikey;
2037 }
2038 
2039 HeaderFileInfoTrait::data_type
ReadData(internal_key_ref key,const unsigned char * d,unsigned DataLen)2040 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2041                               unsigned DataLen) {
2042   using namespace llvm::support;
2043 
2044   const unsigned char *End = d + DataLen;
2045   HeaderFileInfo HFI;
2046   unsigned Flags = *d++;
2047 
2048   bool Included = (Flags >> 6) & 0x01;
2049   if (Included)
2050     if (const FileEntry *FE = getFile(key))
2051       // Not using \c Preprocessor::markIncluded(), since that would attempt to
2052       // deserialize this header file info again.
2053       Reader.getPreprocessor().getIncludedFiles().insert(FE);
2054 
2055   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2056   HFI.isImport |= (Flags >> 5) & 0x01;
2057   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2058   HFI.DirInfo = (Flags >> 1) & 0x07;
2059   HFI.IndexHeaderMapHeader = Flags & 0x01;
2060   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
2061       M, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
2062   if (unsigned FrameworkOffset =
2063           endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)) {
2064     // The framework offset is 1 greater than the actual offset,
2065     // since 0 is used as an indicator for "no framework name".
2066     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2067     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
2068   }
2069 
2070   assert((End - d) % 4 == 0 &&
2071          "Wrong data length in HeaderFileInfo deserialization");
2072   while (d != End) {
2073     uint32_t LocalSMID =
2074         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
2075     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2076     LocalSMID >>= 3;
2077 
2078     // This header is part of a module. Associate it with the module to enable
2079     // implicit module import.
2080     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
2081     Module *Mod = Reader.getSubmodule(GlobalSMID);
2082     FileManager &FileMgr = Reader.getFileManager();
2083     ModuleMap &ModMap =
2084         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2085 
2086     std::string Filename = std::string(key.Filename);
2087     if (key.Imported)
2088       Reader.ResolveImportedPath(M, Filename);
2089     if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2090       // FIXME: NameAsWritten
2091       Module::Header H = {std::string(key.Filename), "", *FE};
2092       ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);
2093     }
2094     HFI.isModuleHeader |= ModuleMap::isModular(HeaderRole);
2095   }
2096 
2097   // This HeaderFileInfo was externally loaded.
2098   HFI.External = true;
2099   HFI.IsValid = true;
2100   return HFI;
2101 }
2102 
addPendingMacro(IdentifierInfo * II,ModuleFile * M,uint32_t MacroDirectivesOffset)2103 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2104                                 uint32_t MacroDirectivesOffset) {
2105   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2106   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
2107 }
2108 
ReadDefinedMacros()2109 void ASTReader::ReadDefinedMacros() {
2110   // Note that we are loading defined macros.
2111   Deserializing Macros(this);
2112 
2113   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
2114     BitstreamCursor &MacroCursor = I.MacroCursor;
2115 
2116     // If there was no preprocessor block, skip this file.
2117     if (MacroCursor.getBitcodeBytes().empty())
2118       continue;
2119 
2120     BitstreamCursor Cursor = MacroCursor;
2121     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
2122       Error(std::move(Err));
2123       return;
2124     }
2125 
2126     RecordData Record;
2127     while (true) {
2128       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2129       if (!MaybeE) {
2130         Error(MaybeE.takeError());
2131         return;
2132       }
2133       llvm::BitstreamEntry E = MaybeE.get();
2134 
2135       switch (E.Kind) {
2136       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2137       case llvm::BitstreamEntry::Error:
2138         Error("malformed block record in AST file");
2139         return;
2140       case llvm::BitstreamEntry::EndBlock:
2141         goto NextCursor;
2142 
2143       case llvm::BitstreamEntry::Record: {
2144         Record.clear();
2145         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2146         if (!MaybeRecord) {
2147           Error(MaybeRecord.takeError());
2148           return;
2149         }
2150         switch (MaybeRecord.get()) {
2151         default:  // Default behavior: ignore.
2152           break;
2153 
2154         case PP_MACRO_OBJECT_LIKE:
2155         case PP_MACRO_FUNCTION_LIKE: {
2156           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2157           if (II->isOutOfDate())
2158             updateOutOfDateIdentifier(*II);
2159           break;
2160         }
2161 
2162         case PP_TOKEN:
2163           // Ignore tokens.
2164           break;
2165         }
2166         break;
2167       }
2168       }
2169     }
2170     NextCursor:  ;
2171   }
2172 }
2173 
2174 namespace {
2175 
2176   /// Visitor class used to look up identifirs in an AST file.
2177   class IdentifierLookupVisitor {
2178     StringRef Name;
2179     unsigned NameHash;
2180     unsigned PriorGeneration;
2181     unsigned &NumIdentifierLookups;
2182     unsigned &NumIdentifierLookupHits;
2183     IdentifierInfo *Found = nullptr;
2184 
2185   public:
IdentifierLookupVisitor(StringRef Name,unsigned PriorGeneration,unsigned & NumIdentifierLookups,unsigned & NumIdentifierLookupHits)2186     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2187                             unsigned &NumIdentifierLookups,
2188                             unsigned &NumIdentifierLookupHits)
2189       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2190         PriorGeneration(PriorGeneration),
2191         NumIdentifierLookups(NumIdentifierLookups),
2192         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2193 
operator ()(ModuleFile & M)2194     bool operator()(ModuleFile &M) {
2195       // If we've already searched this module file, skip it now.
2196       if (M.Generation <= PriorGeneration)
2197         return true;
2198 
2199       ASTIdentifierLookupTable *IdTable
2200         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2201       if (!IdTable)
2202         return false;
2203 
2204       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2205                                      Found);
2206       ++NumIdentifierLookups;
2207       ASTIdentifierLookupTable::iterator Pos =
2208           IdTable->find_hashed(Name, NameHash, &Trait);
2209       if (Pos == IdTable->end())
2210         return false;
2211 
2212       // Dereferencing the iterator has the effect of building the
2213       // IdentifierInfo node and populating it with the various
2214       // declarations it needs.
2215       ++NumIdentifierLookupHits;
2216       Found = *Pos;
2217       return true;
2218     }
2219 
2220     // Retrieve the identifier info found within the module
2221     // files.
getIdentifierInfo() const2222     IdentifierInfo *getIdentifierInfo() const { return Found; }
2223   };
2224 
2225 } // namespace
2226 
updateOutOfDateIdentifier(IdentifierInfo & II)2227 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2228   // Note that we are loading an identifier.
2229   Deserializing AnIdentifier(this);
2230 
2231   unsigned PriorGeneration = 0;
2232   if (getContext().getLangOpts().Modules)
2233     PriorGeneration = IdentifierGeneration[&II];
2234 
2235   // If there is a global index, look there first to determine which modules
2236   // provably do not have any results for this identifier.
2237   GlobalModuleIndex::HitSet Hits;
2238   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2239   if (!loadGlobalIndex()) {
2240     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2241       HitsPtr = &Hits;
2242     }
2243   }
2244 
2245   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2246                                   NumIdentifierLookups,
2247                                   NumIdentifierLookupHits);
2248   ModuleMgr.visit(Visitor, HitsPtr);
2249   markIdentifierUpToDate(&II);
2250 }
2251 
markIdentifierUpToDate(IdentifierInfo * II)2252 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2253   if (!II)
2254     return;
2255 
2256   II->setOutOfDate(false);
2257 
2258   // Update the generation for this identifier.
2259   if (getContext().getLangOpts().Modules)
2260     IdentifierGeneration[II] = getGeneration();
2261 }
2262 
resolvePendingMacro(IdentifierInfo * II,const PendingMacroInfo & PMInfo)2263 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2264                                     const PendingMacroInfo &PMInfo) {
2265   ModuleFile &M = *PMInfo.M;
2266 
2267   BitstreamCursor &Cursor = M.MacroCursor;
2268   SavedStreamPosition SavedPosition(Cursor);
2269   if (llvm::Error Err =
2270           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2271     Error(std::move(Err));
2272     return;
2273   }
2274 
2275   struct ModuleMacroRecord {
2276     SubmoduleID SubModID;
2277     MacroInfo *MI;
2278     SmallVector<SubmoduleID, 8> Overrides;
2279   };
2280   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2281 
2282   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2283   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2284   // macro histroy.
2285   RecordData Record;
2286   while (true) {
2287     Expected<llvm::BitstreamEntry> MaybeEntry =
2288         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2289     if (!MaybeEntry) {
2290       Error(MaybeEntry.takeError());
2291       return;
2292     }
2293     llvm::BitstreamEntry Entry = MaybeEntry.get();
2294 
2295     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2296       Error("malformed block record in AST file");
2297       return;
2298     }
2299 
2300     Record.clear();
2301     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2302     if (!MaybePP) {
2303       Error(MaybePP.takeError());
2304       return;
2305     }
2306     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2307     case PP_MACRO_DIRECTIVE_HISTORY:
2308       break;
2309 
2310     case PP_MODULE_MACRO: {
2311       ModuleMacros.push_back(ModuleMacroRecord());
2312       auto &Info = ModuleMacros.back();
2313       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2314       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2315       for (int I = 2, N = Record.size(); I != N; ++I)
2316         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2317       continue;
2318     }
2319 
2320     default:
2321       Error("malformed block record in AST file");
2322       return;
2323     }
2324 
2325     // We found the macro directive history; that's the last record
2326     // for this macro.
2327     break;
2328   }
2329 
2330   // Module macros are listed in reverse dependency order.
2331   {
2332     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2333     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2334     for (auto &MMR : ModuleMacros) {
2335       Overrides.clear();
2336       for (unsigned ModID : MMR.Overrides) {
2337         Module *Mod = getSubmodule(ModID);
2338         auto *Macro = PP.getModuleMacro(Mod, II);
2339         assert(Macro && "missing definition for overridden macro");
2340         Overrides.push_back(Macro);
2341       }
2342 
2343       bool Inserted = false;
2344       Module *Owner = getSubmodule(MMR.SubModID);
2345       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2346     }
2347   }
2348 
2349   // Don't read the directive history for a module; we don't have anywhere
2350   // to put it.
2351   if (M.isModule())
2352     return;
2353 
2354   // Deserialize the macro directives history in reverse source-order.
2355   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2356   unsigned Idx = 0, N = Record.size();
2357   while (Idx < N) {
2358     MacroDirective *MD = nullptr;
2359     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2360     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2361     switch (K) {
2362     case MacroDirective::MD_Define: {
2363       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2364       MD = PP.AllocateDefMacroDirective(MI, Loc);
2365       break;
2366     }
2367     case MacroDirective::MD_Undefine:
2368       MD = PP.AllocateUndefMacroDirective(Loc);
2369       break;
2370     case MacroDirective::MD_Visibility:
2371       bool isPublic = Record[Idx++];
2372       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2373       break;
2374     }
2375 
2376     if (!Latest)
2377       Latest = MD;
2378     if (Earliest)
2379       Earliest->setPrevious(MD);
2380     Earliest = MD;
2381   }
2382 
2383   if (Latest)
2384     PP.setLoadedMacroDirective(II, Earliest, Latest);
2385 }
2386 
shouldDisableValidationForFile(const serialization::ModuleFile & M) const2387 bool ASTReader::shouldDisableValidationForFile(
2388     const serialization::ModuleFile &M) const {
2389   if (DisableValidationKind == DisableValidationForModuleKind::None)
2390     return false;
2391 
2392   // If a PCH is loaded and validation is disabled for PCH then disable
2393   // validation for the PCH and the modules it loads.
2394   ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2395 
2396   switch (K) {
2397   case MK_MainFile:
2398   case MK_Preamble:
2399   case MK_PCH:
2400     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2401   case MK_ImplicitModule:
2402   case MK_ExplicitModule:
2403   case MK_PrebuiltModule:
2404     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2405   }
2406 
2407   return false;
2408 }
2409 
getInputFileInfo(ModuleFile & F,unsigned ID)2410 InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2411   // If this ID is bogus, just return an empty input file.
2412   if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2413     return InputFileInfo();
2414 
2415   // If we've already loaded this input file, return it.
2416   if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2417     return F.InputFileInfosLoaded[ID - 1];
2418 
2419   // Go find this input file.
2420   BitstreamCursor &Cursor = F.InputFilesCursor;
2421   SavedStreamPosition SavedPosition(Cursor);
2422   if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2423                                          F.InputFileOffsets[ID - 1])) {
2424     // FIXME this drops errors on the floor.
2425     consumeError(std::move(Err));
2426   }
2427 
2428   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2429   if (!MaybeCode) {
2430     // FIXME this drops errors on the floor.
2431     consumeError(MaybeCode.takeError());
2432   }
2433   unsigned Code = MaybeCode.get();
2434   RecordData Record;
2435   StringRef Blob;
2436 
2437   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2438     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2439            "invalid record type for input file");
2440   else {
2441     // FIXME this drops errors on the floor.
2442     consumeError(Maybe.takeError());
2443   }
2444 
2445   assert(Record[0] == ID && "Bogus stored ID or offset");
2446   InputFileInfo R;
2447   R.StoredSize = static_cast<off_t>(Record[1]);
2448   R.StoredTime = static_cast<time_t>(Record[2]);
2449   R.Overridden = static_cast<bool>(Record[3]);
2450   R.Transient = static_cast<bool>(Record[4]);
2451   R.TopLevel = static_cast<bool>(Record[5]);
2452   R.ModuleMap = static_cast<bool>(Record[6]);
2453   std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2454     uint16_t AsRequestedLength = Record[7];
2455 
2456     std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2457     std::string Name = Blob.substr(AsRequestedLength).str();
2458 
2459     ResolveImportedPath(F, NameAsRequested);
2460     ResolveImportedPath(F, Name);
2461 
2462     if (Name.empty())
2463       Name = NameAsRequested;
2464 
2465     return std::make_pair(std::move(NameAsRequested), std::move(Name));
2466   }();
2467 
2468   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2469   if (!MaybeEntry) // FIXME this drops errors on the floor.
2470     consumeError(MaybeEntry.takeError());
2471   llvm::BitstreamEntry Entry = MaybeEntry.get();
2472   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2473          "expected record type for input file hash");
2474 
2475   Record.clear();
2476   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2477     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2478            "invalid record type for input file hash");
2479   else {
2480     // FIXME this drops errors on the floor.
2481     consumeError(Maybe.takeError());
2482   }
2483   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2484                   static_cast<uint64_t>(Record[0]);
2485 
2486   // Note that we've loaded this input file info.
2487   F.InputFileInfosLoaded[ID - 1] = R;
2488   return R;
2489 }
2490 
2491 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
getInputFile(ModuleFile & F,unsigned ID,bool Complain)2492 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2493   // If this ID is bogus, just return an empty input file.
2494   if (ID == 0 || ID > F.InputFilesLoaded.size())
2495     return InputFile();
2496 
2497   // If we've already loaded this input file, return it.
2498   if (F.InputFilesLoaded[ID-1].getFile())
2499     return F.InputFilesLoaded[ID-1];
2500 
2501   if (F.InputFilesLoaded[ID-1].isNotFound())
2502     return InputFile();
2503 
2504   // Go find this input file.
2505   BitstreamCursor &Cursor = F.InputFilesCursor;
2506   SavedStreamPosition SavedPosition(Cursor);
2507   if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2508                                          F.InputFileOffsets[ID - 1])) {
2509     // FIXME this drops errors on the floor.
2510     consumeError(std::move(Err));
2511   }
2512 
2513   InputFileInfo FI = getInputFileInfo(F, ID);
2514   off_t StoredSize = FI.StoredSize;
2515   time_t StoredTime = FI.StoredTime;
2516   bool Overridden = FI.Overridden;
2517   bool Transient = FI.Transient;
2518   StringRef Filename = FI.FilenameAsRequested;
2519   uint64_t StoredContentHash = FI.ContentHash;
2520 
2521   // For standard C++ modules, we don't need to check the inputs.
2522   bool SkipChecks = F.StandardCXXModule;
2523 
2524   const HeaderSearchOptions &HSOpts =
2525       PP.getHeaderSearchInfo().getHeaderSearchOpts();
2526 
2527   // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2528   // modules.
2529   if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2530     SkipChecks = false;
2531     Overridden = false;
2532   }
2533 
2534   auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
2535 
2536   // For an overridden file, create a virtual file with the stored
2537   // size/timestamp.
2538   if ((Overridden || Transient || SkipChecks) && !File)
2539     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2540 
2541   if (!File) {
2542     if (Complain) {
2543       std::string ErrorStr = "could not find file '";
2544       ErrorStr += Filename;
2545       ErrorStr += "' referenced by AST file '";
2546       ErrorStr += F.FileName;
2547       ErrorStr += "'";
2548       Error(ErrorStr);
2549     }
2550     // Record that we didn't find the file.
2551     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2552     return InputFile();
2553   }
2554 
2555   // Check if there was a request to override the contents of the file
2556   // that was part of the precompiled header. Overriding such a file
2557   // can lead to problems when lexing using the source locations from the
2558   // PCH.
2559   SourceManager &SM = getSourceManager();
2560   // FIXME: Reject if the overrides are different.
2561   if ((!Overridden && !Transient) && !SkipChecks &&
2562       SM.isFileOverridden(*File)) {
2563     if (Complain)
2564       Error(diag::err_fe_pch_file_overridden, Filename);
2565 
2566     // After emitting the diagnostic, bypass the overriding file to recover
2567     // (this creates a separate FileEntry).
2568     File = SM.bypassFileContentsOverride(*File);
2569     if (!File) {
2570       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2571       return InputFile();
2572     }
2573   }
2574 
2575   struct Change {
2576     enum ModificationKind {
2577       Size,
2578       ModTime,
2579       Content,
2580       None,
2581     } Kind;
2582     std::optional<int64_t> Old = std::nullopt;
2583     std::optional<int64_t> New = std::nullopt;
2584   };
2585   auto HasInputContentChanged = [&](Change OriginalChange) {
2586     assert(ValidateASTInputFilesContent &&
2587            "We should only check the content of the inputs with "
2588            "ValidateASTInputFilesContent enabled.");
2589 
2590     if (StoredContentHash == static_cast<uint64_t>(llvm::hash_code(-1)))
2591       return OriginalChange;
2592 
2593     auto MemBuffOrError = FileMgr.getBufferForFile(*File);
2594     if (!MemBuffOrError) {
2595       if (!Complain)
2596         return OriginalChange;
2597       std::string ErrorStr = "could not get buffer for file '";
2598       ErrorStr += File->getName();
2599       ErrorStr += "'";
2600       Error(ErrorStr);
2601       return OriginalChange;
2602     }
2603 
2604     // FIXME: hash_value is not guaranteed to be stable!
2605     auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2606     if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2607       return Change{Change::None};
2608 
2609     return Change{Change::Content};
2610   };
2611   auto HasInputFileChanged = [&]() {
2612     if (StoredSize != File->getSize())
2613       return Change{Change::Size, StoredSize, File->getSize()};
2614     if (!shouldDisableValidationForFile(F) && StoredTime &&
2615         StoredTime != File->getModificationTime()) {
2616       Change MTimeChange = {Change::ModTime, StoredTime,
2617                             File->getModificationTime()};
2618 
2619       // In case the modification time changes but not the content,
2620       // accept the cached file as legit.
2621       if (ValidateASTInputFilesContent)
2622         return HasInputContentChanged(MTimeChange);
2623 
2624       return MTimeChange;
2625     }
2626     return Change{Change::None};
2627   };
2628 
2629   bool IsOutOfDate = false;
2630   auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();
2631   // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2632   // enabled, it is better to check the contents of the inputs. Since we can't
2633   // get correct modified time information for inputs from overriden inputs.
2634   if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2635       F.StandardCXXModule && FileChange.Kind == Change::None)
2636     FileChange = HasInputContentChanged(FileChange);
2637 
2638   // For an overridden file, there is nothing to validate.
2639   if (!Overridden && FileChange.Kind != Change::None) {
2640     if (Complain && !Diags.isDiagnosticInFlight()) {
2641       // Build a list of the PCH imports that got us here (in reverse).
2642       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2643       while (!ImportStack.back()->ImportedBy.empty())
2644         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2645 
2646       // The top-level PCH is stale.
2647       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2648       Diag(diag::err_fe_ast_file_modified)
2649           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2650           << TopLevelPCHName << FileChange.Kind
2651           << (FileChange.Old && FileChange.New)
2652           << llvm::itostr(FileChange.Old.value_or(0))
2653           << llvm::itostr(FileChange.New.value_or(0));
2654 
2655       // Print the import stack.
2656       if (ImportStack.size() > 1) {
2657         Diag(diag::note_pch_required_by)
2658           << Filename << ImportStack[0]->FileName;
2659         for (unsigned I = 1; I < ImportStack.size(); ++I)
2660           Diag(diag::note_pch_required_by)
2661             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2662       }
2663 
2664       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2665     }
2666 
2667     IsOutOfDate = true;
2668   }
2669   // FIXME: If the file is overridden and we've already opened it,
2670   // issue an error (or split it into a separate FileEntry).
2671 
2672   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2673 
2674   // Note that we've loaded this input file.
2675   F.InputFilesLoaded[ID-1] = IF;
2676   return IF;
2677 }
2678 
2679 /// If we are loading a relocatable PCH or module file, and the filename
2680 /// is not an absolute path, add the system or module root to the beginning of
2681 /// the file name.
ResolveImportedPath(ModuleFile & M,std::string & Filename)2682 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2683   // Resolve relative to the base directory, if we have one.
2684   if (!M.BaseDirectory.empty())
2685     return ResolveImportedPath(Filename, M.BaseDirectory);
2686 }
2687 
ResolveImportedPath(std::string & Filename,StringRef Prefix)2688 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2689   if (Filename.empty() || llvm::sys::path::is_absolute(Filename) ||
2690       Filename == "<built-in>" || Filename == "<command line>")
2691     return;
2692 
2693   SmallString<128> Buffer;
2694   llvm::sys::path::append(Buffer, Prefix, Filename);
2695   Filename.assign(Buffer.begin(), Buffer.end());
2696 }
2697 
isDiagnosedResult(ASTReader::ASTReadResult ARR,unsigned Caps)2698 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2699   switch (ARR) {
2700   case ASTReader::Failure: return true;
2701   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2702   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2703   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2704   case ASTReader::ConfigurationMismatch:
2705     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2706   case ASTReader::HadErrors: return true;
2707   case ASTReader::Success: return false;
2708   }
2709 
2710   llvm_unreachable("unknown ASTReadResult");
2711 }
2712 
ReadOptionsBlock(BitstreamCursor & Stream,unsigned ClientLoadCapabilities,bool AllowCompatibleConfigurationMismatch,ASTReaderListener & Listener,std::string & SuggestedPredefines)2713 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2714     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2715     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2716     std::string &SuggestedPredefines) {
2717   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2718     // FIXME this drops errors on the floor.
2719     consumeError(std::move(Err));
2720     return Failure;
2721   }
2722 
2723   // Read all of the records in the options block.
2724   RecordData Record;
2725   ASTReadResult Result = Success;
2726   while (true) {
2727     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2728     if (!MaybeEntry) {
2729       // FIXME this drops errors on the floor.
2730       consumeError(MaybeEntry.takeError());
2731       return Failure;
2732     }
2733     llvm::BitstreamEntry Entry = MaybeEntry.get();
2734 
2735     switch (Entry.Kind) {
2736     case llvm::BitstreamEntry::Error:
2737     case llvm::BitstreamEntry::SubBlock:
2738       return Failure;
2739 
2740     case llvm::BitstreamEntry::EndBlock:
2741       return Result;
2742 
2743     case llvm::BitstreamEntry::Record:
2744       // The interesting case.
2745       break;
2746     }
2747 
2748     // Read and process a record.
2749     Record.clear();
2750     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2751     if (!MaybeRecordType) {
2752       // FIXME this drops errors on the floor.
2753       consumeError(MaybeRecordType.takeError());
2754       return Failure;
2755     }
2756     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2757     case LANGUAGE_OPTIONS: {
2758       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2759       if (ParseLanguageOptions(Record, Complain, Listener,
2760                                AllowCompatibleConfigurationMismatch))
2761         Result = ConfigurationMismatch;
2762       break;
2763     }
2764 
2765     case TARGET_OPTIONS: {
2766       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2767       if (ParseTargetOptions(Record, Complain, Listener,
2768                              AllowCompatibleConfigurationMismatch))
2769         Result = ConfigurationMismatch;
2770       break;
2771     }
2772 
2773     case FILE_SYSTEM_OPTIONS: {
2774       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2775       if (!AllowCompatibleConfigurationMismatch &&
2776           ParseFileSystemOptions(Record, Complain, Listener))
2777         Result = ConfigurationMismatch;
2778       break;
2779     }
2780 
2781     case HEADER_SEARCH_OPTIONS: {
2782       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2783       if (!AllowCompatibleConfigurationMismatch &&
2784           ParseHeaderSearchOptions(Record, Complain, Listener))
2785         Result = ConfigurationMismatch;
2786       break;
2787     }
2788 
2789     case PREPROCESSOR_OPTIONS:
2790       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2791       if (!AllowCompatibleConfigurationMismatch &&
2792           ParsePreprocessorOptions(Record, Complain, Listener,
2793                                    SuggestedPredefines))
2794         Result = ConfigurationMismatch;
2795       break;
2796     }
2797   }
2798 }
2799 
2800 ASTReader::ASTReadResult
ReadControlBlock(ModuleFile & F,SmallVectorImpl<ImportedModule> & Loaded,const ModuleFile * ImportedBy,unsigned ClientLoadCapabilities)2801 ASTReader::ReadControlBlock(ModuleFile &F,
2802                             SmallVectorImpl<ImportedModule> &Loaded,
2803                             const ModuleFile *ImportedBy,
2804                             unsigned ClientLoadCapabilities) {
2805   BitstreamCursor &Stream = F.Stream;
2806 
2807   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2808     Error(std::move(Err));
2809     return Failure;
2810   }
2811 
2812   // Lambda to read the unhashed control block the first time it's called.
2813   //
2814   // For PCM files, the unhashed control block cannot be read until after the
2815   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2816   // need to look ahead before reading the IMPORTS record.  For consistency,
2817   // this block is always read somehow (see BitstreamEntry::EndBlock).
2818   bool HasReadUnhashedControlBlock = false;
2819   auto readUnhashedControlBlockOnce = [&]() {
2820     if (!HasReadUnhashedControlBlock) {
2821       HasReadUnhashedControlBlock = true;
2822       if (ASTReadResult Result =
2823               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2824         return Result;
2825     }
2826     return Success;
2827   };
2828 
2829   bool DisableValidation = shouldDisableValidationForFile(F);
2830 
2831   // Read all of the records and blocks in the control block.
2832   RecordData Record;
2833   unsigned NumInputs = 0;
2834   unsigned NumUserInputs = 0;
2835   StringRef BaseDirectoryAsWritten;
2836   while (true) {
2837     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2838     if (!MaybeEntry) {
2839       Error(MaybeEntry.takeError());
2840       return Failure;
2841     }
2842     llvm::BitstreamEntry Entry = MaybeEntry.get();
2843 
2844     switch (Entry.Kind) {
2845     case llvm::BitstreamEntry::Error:
2846       Error("malformed block record in AST file");
2847       return Failure;
2848     case llvm::BitstreamEntry::EndBlock: {
2849       // Validate the module before returning.  This call catches an AST with
2850       // no module name and no imports.
2851       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2852         return Result;
2853 
2854       // Validate input files.
2855       const HeaderSearchOptions &HSOpts =
2856           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2857 
2858       // All user input files reside at the index range [0, NumUserInputs), and
2859       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2860       // loaded module files, ignore missing inputs.
2861       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2862           F.Kind != MK_PrebuiltModule) {
2863         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2864 
2865         // If we are reading a module, we will create a verification timestamp,
2866         // so we verify all input files.  Otherwise, verify only user input
2867         // files.
2868 
2869         unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2870         if (HSOpts.ModulesValidateOncePerBuildSession &&
2871             F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
2872             F.Kind == MK_ImplicitModule)
2873           N = NumUserInputs;
2874 
2875         for (unsigned I = 0; I < N; ++I) {
2876           InputFile IF = getInputFile(F, I+1, Complain);
2877           if (!IF.getFile() || IF.isOutOfDate())
2878             return OutOfDate;
2879         }
2880       }
2881 
2882       if (Listener)
2883         Listener->visitModuleFile(F.FileName, F.Kind);
2884 
2885       if (Listener && Listener->needsInputFileVisitation()) {
2886         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2887                                                                 : NumUserInputs;
2888         for (unsigned I = 0; I < N; ++I) {
2889           bool IsSystem = I >= NumUserInputs;
2890           InputFileInfo FI = getInputFileInfo(F, I + 1);
2891           Listener->visitInputFile(
2892               FI.FilenameAsRequested, IsSystem, FI.Overridden,
2893               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
2894         }
2895       }
2896 
2897       return Success;
2898     }
2899 
2900     case llvm::BitstreamEntry::SubBlock:
2901       switch (Entry.ID) {
2902       case INPUT_FILES_BLOCK_ID:
2903         F.InputFilesCursor = Stream;
2904         if (llvm::Error Err = Stream.SkipBlock()) {
2905           Error(std::move(Err));
2906           return Failure;
2907         }
2908         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2909           Error("malformed block record in AST file");
2910           return Failure;
2911         }
2912         F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2913         continue;
2914 
2915       case OPTIONS_BLOCK_ID:
2916         // If we're reading the first module for this group, check its options
2917         // are compatible with ours. For modules it imports, no further checking
2918         // is required, because we checked them when we built it.
2919         if (Listener && !ImportedBy) {
2920           // Should we allow the configuration of the module file to differ from
2921           // the configuration of the current translation unit in a compatible
2922           // way?
2923           //
2924           // FIXME: Allow this for files explicitly specified with -include-pch.
2925           bool AllowCompatibleConfigurationMismatch =
2926               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2927 
2928           ASTReadResult Result =
2929               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2930                                AllowCompatibleConfigurationMismatch, *Listener,
2931                                SuggestedPredefines);
2932           if (Result == Failure) {
2933             Error("malformed block record in AST file");
2934             return Result;
2935           }
2936 
2937           if (DisableValidation ||
2938               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2939             Result = Success;
2940 
2941           // If we can't load the module, exit early since we likely
2942           // will rebuild the module anyway. The stream may be in the
2943           // middle of a block.
2944           if (Result != Success)
2945             return Result;
2946         } else if (llvm::Error Err = Stream.SkipBlock()) {
2947           Error(std::move(Err));
2948           return Failure;
2949         }
2950         continue;
2951 
2952       default:
2953         if (llvm::Error Err = Stream.SkipBlock()) {
2954           Error(std::move(Err));
2955           return Failure;
2956         }
2957         continue;
2958       }
2959 
2960     case llvm::BitstreamEntry::Record:
2961       // The interesting case.
2962       break;
2963     }
2964 
2965     // Read and process a record.
2966     Record.clear();
2967     StringRef Blob;
2968     Expected<unsigned> MaybeRecordType =
2969         Stream.readRecord(Entry.ID, Record, &Blob);
2970     if (!MaybeRecordType) {
2971       Error(MaybeRecordType.takeError());
2972       return Failure;
2973     }
2974     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2975     case METADATA: {
2976       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2977         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2978           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2979                                         : diag::err_pch_version_too_new);
2980         return VersionMismatch;
2981       }
2982 
2983       bool hasErrors = Record[7];
2984       if (hasErrors && !DisableValidation) {
2985         // If requested by the caller and the module hasn't already been read
2986         // or compiled, mark modules on error as out-of-date.
2987         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2988             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2989           return OutOfDate;
2990 
2991         if (!AllowASTWithCompilerErrors) {
2992           Diag(diag::err_pch_with_compiler_errors);
2993           return HadErrors;
2994         }
2995       }
2996       if (hasErrors) {
2997         Diags.ErrorOccurred = true;
2998         Diags.UncompilableErrorOccurred = true;
2999         Diags.UnrecoverableErrorOccurred = true;
3000       }
3001 
3002       F.RelocatablePCH = Record[4];
3003       // Relative paths in a relocatable PCH are relative to our sysroot.
3004       if (F.RelocatablePCH)
3005         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3006 
3007       F.StandardCXXModule = Record[5];
3008 
3009       F.HasTimestamps = Record[6];
3010 
3011       const std::string &CurBranch = getClangFullRepositoryVersion();
3012       StringRef ASTBranch = Blob;
3013       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3014         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3015           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
3016         return VersionMismatch;
3017       }
3018       break;
3019     }
3020 
3021     case IMPORTS: {
3022       // Validate the AST before processing any imports (otherwise, untangling
3023       // them can be error-prone and expensive).  A module will have a name and
3024       // will already have been validated, but this catches the PCH case.
3025       if (ASTReadResult Result = readUnhashedControlBlockOnce())
3026         return Result;
3027 
3028       // Load each of the imported PCH files.
3029       unsigned Idx = 0, N = Record.size();
3030       while (Idx < N) {
3031         // Read information about the AST file.
3032         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3033         // Whether we're importing a standard c++ module.
3034         bool IsImportingStdCXXModule = Record[Idx++];
3035         // The import location will be the local one for now; we will adjust
3036         // all import locations of module imports after the global source
3037         // location info are setup, in ReadAST.
3038         SourceLocation ImportLoc =
3039             ReadUntranslatedSourceLocation(Record[Idx++]);
3040         off_t StoredSize = !IsImportingStdCXXModule ? (off_t)Record[Idx++] : 0;
3041         time_t StoredModTime =
3042             !IsImportingStdCXXModule ? (time_t)Record[Idx++] : 0;
3043 
3044         ASTFileSignature StoredSignature;
3045         if (!IsImportingStdCXXModule) {
3046           auto FirstSignatureByte = Record.begin() + Idx;
3047           StoredSignature = ASTFileSignature::create(
3048               FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
3049           Idx += ASTFileSignature::size;
3050         }
3051 
3052         std::string ImportedName = ReadString(Record, Idx);
3053         std::string ImportedFile;
3054 
3055         // For prebuilt and explicit modules first consult the file map for
3056         // an override. Note that here we don't search prebuilt module
3057         // directories if we're not importing standard c++ module, only the
3058         // explicit name to file mappings. Also, we will still verify the
3059         // size/signature making sure it is essentially the same file but
3060         // perhaps in a different location.
3061         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3062           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3063               ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3064 
3065         // For C++20 Modules, we won't record the path to the imported modules
3066         // in the BMI
3067         if (!IsImportingStdCXXModule) {
3068           if (ImportedFile.empty()) {
3069             // Use BaseDirectoryAsWritten to ensure we use the same path in the
3070             // ModuleCache as when writing.
3071             ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
3072           } else
3073             SkipPath(Record, Idx);
3074         } else if (ImportedFile.empty()) {
3075           Diag(clang::diag::err_failed_to_find_module_file) << ImportedName;
3076           return Missing;
3077         }
3078 
3079         // If our client can't cope with us being out of date, we can't cope with
3080         // our dependency being missing.
3081         unsigned Capabilities = ClientLoadCapabilities;
3082         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3083           Capabilities &= ~ARR_Missing;
3084 
3085         // Load the AST file.
3086         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
3087                                   Loaded, StoredSize, StoredModTime,
3088                                   StoredSignature, Capabilities);
3089 
3090         // If we diagnosed a problem, produce a backtrace.
3091         bool recompilingFinalized =
3092             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3093             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
3094         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3095           Diag(diag::note_module_file_imported_by)
3096               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3097         if (recompilingFinalized)
3098           Diag(diag::note_module_file_conflict);
3099 
3100         switch (Result) {
3101         case Failure: return Failure;
3102           // If we have to ignore the dependency, we'll have to ignore this too.
3103         case Missing:
3104         case OutOfDate: return OutOfDate;
3105         case VersionMismatch: return VersionMismatch;
3106         case ConfigurationMismatch: return ConfigurationMismatch;
3107         case HadErrors: return HadErrors;
3108         case Success: break;
3109         }
3110       }
3111       break;
3112     }
3113 
3114     case ORIGINAL_FILE:
3115       F.OriginalSourceFileID = FileID::get(Record[0]);
3116       F.ActualOriginalSourceFileName = std::string(Blob);
3117       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
3118       ResolveImportedPath(F, F.OriginalSourceFileName);
3119       break;
3120 
3121     case ORIGINAL_FILE_ID:
3122       F.OriginalSourceFileID = FileID::get(Record[0]);
3123       break;
3124 
3125     case MODULE_NAME:
3126       F.ModuleName = std::string(Blob);
3127       Diag(diag::remark_module_import)
3128           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3129           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3130       if (Listener)
3131         Listener->ReadModuleName(F.ModuleName);
3132 
3133       // Validate the AST as soon as we have a name so we can exit early on
3134       // failure.
3135       if (ASTReadResult Result = readUnhashedControlBlockOnce())
3136         return Result;
3137 
3138       break;
3139 
3140     case MODULE_DIRECTORY: {
3141       // Save the BaseDirectory as written in the PCM for computing the module
3142       // filename for the ModuleCache.
3143       BaseDirectoryAsWritten = Blob;
3144       assert(!F.ModuleName.empty() &&
3145              "MODULE_DIRECTORY found before MODULE_NAME");
3146       F.BaseDirectory = std::string(Blob);
3147       if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3148         break;
3149       // If we've already loaded a module map file covering this module, we may
3150       // have a better path for it (relative to the current build).
3151       Module *M = PP.getHeaderSearchInfo().lookupModule(
3152           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
3153           /*AllowExtraModuleMapSearch*/ true);
3154       if (M && M->Directory) {
3155         // If we're implicitly loading a module, the base directory can't
3156         // change between the build and use.
3157         // Don't emit module relocation error if we have -fno-validate-pch
3158         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3159                   DisableValidationForModuleKind::Module) &&
3160             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3161           auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(Blob);
3162           if (!BuildDir || *BuildDir != M->Directory) {
3163             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3164               Diag(diag::err_imported_module_relocated)
3165                   << F.ModuleName << Blob << M->Directory->getName();
3166             return OutOfDate;
3167           }
3168         }
3169         F.BaseDirectory = std::string(M->Directory->getName());
3170       }
3171       break;
3172     }
3173 
3174     case MODULE_MAP_FILE:
3175       if (ASTReadResult Result =
3176               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3177         return Result;
3178       break;
3179 
3180     case INPUT_FILE_OFFSETS:
3181       NumInputs = Record[0];
3182       NumUserInputs = Record[1];
3183       F.InputFileOffsets =
3184           (const llvm::support::unaligned_uint64_t *)Blob.data();
3185       F.InputFilesLoaded.resize(NumInputs);
3186       F.InputFileInfosLoaded.resize(NumInputs);
3187       F.NumUserInputFiles = NumUserInputs;
3188       break;
3189     }
3190   }
3191 }
3192 
ReadASTBlock(ModuleFile & F,unsigned ClientLoadCapabilities)3193 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3194                                     unsigned ClientLoadCapabilities) {
3195   BitstreamCursor &Stream = F.Stream;
3196 
3197   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
3198     return Err;
3199   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3200 
3201   // Read all of the records and blocks for the AST file.
3202   RecordData Record;
3203   while (true) {
3204     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3205     if (!MaybeEntry)
3206       return MaybeEntry.takeError();
3207     llvm::BitstreamEntry Entry = MaybeEntry.get();
3208 
3209     switch (Entry.Kind) {
3210     case llvm::BitstreamEntry::Error:
3211       return llvm::createStringError(
3212           std::errc::illegal_byte_sequence,
3213           "error at end of module block in AST file");
3214     case llvm::BitstreamEntry::EndBlock:
3215       // Outside of C++, we do not store a lookup map for the translation unit.
3216       // Instead, mark it as needing a lookup map to be built if this module
3217       // contains any declarations lexically within it (which it always does!).
3218       // This usually has no cost, since we very rarely need the lookup map for
3219       // the translation unit outside C++.
3220       if (ASTContext *Ctx = ContextObj) {
3221         DeclContext *DC = Ctx->getTranslationUnitDecl();
3222         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3223           DC->setMustBuildLookupTable();
3224       }
3225 
3226       return llvm::Error::success();
3227     case llvm::BitstreamEntry::SubBlock:
3228       switch (Entry.ID) {
3229       case DECLTYPES_BLOCK_ID:
3230         // We lazily load the decls block, but we want to set up the
3231         // DeclsCursor cursor to point into it.  Clone our current bitcode
3232         // cursor to it, enter the block and read the abbrevs in that block.
3233         // With the main cursor, we just skip over it.
3234         F.DeclsCursor = Stream;
3235         if (llvm::Error Err = Stream.SkipBlock())
3236           return Err;
3237         if (llvm::Error Err = ReadBlockAbbrevs(
3238                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3239           return Err;
3240         break;
3241 
3242       case PREPROCESSOR_BLOCK_ID:
3243         F.MacroCursor = Stream;
3244         if (!PP.getExternalSource())
3245           PP.setExternalSource(this);
3246 
3247         if (llvm::Error Err = Stream.SkipBlock())
3248           return Err;
3249         if (llvm::Error Err =
3250                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3251           return Err;
3252         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3253         break;
3254 
3255       case PREPROCESSOR_DETAIL_BLOCK_ID:
3256         F.PreprocessorDetailCursor = Stream;
3257 
3258         if (llvm::Error Err = Stream.SkipBlock()) {
3259           return Err;
3260         }
3261         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3262                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3263           return Err;
3264         F.PreprocessorDetailStartOffset
3265         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3266 
3267         if (!PP.getPreprocessingRecord())
3268           PP.createPreprocessingRecord();
3269         if (!PP.getPreprocessingRecord()->getExternalSource())
3270           PP.getPreprocessingRecord()->SetExternalSource(*this);
3271         break;
3272 
3273       case SOURCE_MANAGER_BLOCK_ID:
3274         if (llvm::Error Err = ReadSourceManagerBlock(F))
3275           return Err;
3276         break;
3277 
3278       case SUBMODULE_BLOCK_ID:
3279         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3280           return Err;
3281         break;
3282 
3283       case COMMENTS_BLOCK_ID: {
3284         BitstreamCursor C = Stream;
3285 
3286         if (llvm::Error Err = Stream.SkipBlock())
3287           return Err;
3288         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3289           return Err;
3290         CommentsCursors.push_back(std::make_pair(C, &F));
3291         break;
3292       }
3293 
3294       default:
3295         if (llvm::Error Err = Stream.SkipBlock())
3296           return Err;
3297         break;
3298       }
3299       continue;
3300 
3301     case llvm::BitstreamEntry::Record:
3302       // The interesting case.
3303       break;
3304     }
3305 
3306     // Read and process a record.
3307     Record.clear();
3308     StringRef Blob;
3309     Expected<unsigned> MaybeRecordType =
3310         Stream.readRecord(Entry.ID, Record, &Blob);
3311     if (!MaybeRecordType)
3312       return MaybeRecordType.takeError();
3313     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3314 
3315     // If we're not loading an AST context, we don't care about most records.
3316     if (!ContextObj) {
3317       switch (RecordType) {
3318       case IDENTIFIER_TABLE:
3319       case IDENTIFIER_OFFSET:
3320       case INTERESTING_IDENTIFIERS:
3321       case STATISTICS:
3322       case PP_ASSUME_NONNULL_LOC:
3323       case PP_CONDITIONAL_STACK:
3324       case PP_COUNTER_VALUE:
3325       case SOURCE_LOCATION_OFFSETS:
3326       case MODULE_OFFSET_MAP:
3327       case SOURCE_MANAGER_LINE_TABLE:
3328       case PPD_ENTITIES_OFFSETS:
3329       case HEADER_SEARCH_TABLE:
3330       case IMPORTED_MODULES:
3331       case MACRO_OFFSET:
3332         break;
3333       default:
3334         continue;
3335       }
3336     }
3337 
3338     switch (RecordType) {
3339     default:  // Default behavior: ignore.
3340       break;
3341 
3342     case TYPE_OFFSET: {
3343       if (F.LocalNumTypes != 0)
3344         return llvm::createStringError(
3345             std::errc::illegal_byte_sequence,
3346             "duplicate TYPE_OFFSET record in AST file");
3347       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3348       F.LocalNumTypes = Record[0];
3349       unsigned LocalBaseTypeIndex = Record[1];
3350       F.BaseTypeIndex = getTotalNumTypes();
3351 
3352       if (F.LocalNumTypes > 0) {
3353         // Introduce the global -> local mapping for types within this module.
3354         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3355 
3356         // Introduce the local -> global mapping for types within this module.
3357         F.TypeRemap.insertOrReplace(
3358           std::make_pair(LocalBaseTypeIndex,
3359                          F.BaseTypeIndex - LocalBaseTypeIndex));
3360 
3361         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3362       }
3363       break;
3364     }
3365 
3366     case DECL_OFFSET: {
3367       if (F.LocalNumDecls != 0)
3368         return llvm::createStringError(
3369             std::errc::illegal_byte_sequence,
3370             "duplicate DECL_OFFSET record in AST file");
3371       F.DeclOffsets = (const DeclOffset *)Blob.data();
3372       F.LocalNumDecls = Record[0];
3373       unsigned LocalBaseDeclID = Record[1];
3374       F.BaseDeclID = getTotalNumDecls();
3375 
3376       if (F.LocalNumDecls > 0) {
3377         // Introduce the global -> local mapping for declarations within this
3378         // module.
3379         GlobalDeclMap.insert(
3380           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3381 
3382         // Introduce the local -> global mapping for declarations within this
3383         // module.
3384         F.DeclRemap.insertOrReplace(
3385           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3386 
3387         // Introduce the global -> local mapping for declarations within this
3388         // module.
3389         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3390 
3391         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3392       }
3393       break;
3394     }
3395 
3396     case TU_UPDATE_LEXICAL: {
3397       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3398       LexicalContents Contents(
3399           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3400               Blob.data()),
3401           static_cast<unsigned int>(Blob.size() / 4));
3402       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3403       TU->setHasExternalLexicalStorage(true);
3404       break;
3405     }
3406 
3407     case UPDATE_VISIBLE: {
3408       unsigned Idx = 0;
3409       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3410       auto *Data = (const unsigned char*)Blob.data();
3411       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3412       // If we've already loaded the decl, perform the updates when we finish
3413       // loading this block.
3414       if (Decl *D = GetExistingDecl(ID))
3415         PendingUpdateRecords.push_back(
3416             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3417       break;
3418     }
3419 
3420     case IDENTIFIER_TABLE:
3421       F.IdentifierTableData =
3422           reinterpret_cast<const unsigned char *>(Blob.data());
3423       if (Record[0]) {
3424         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3425             F.IdentifierTableData + Record[0],
3426             F.IdentifierTableData + sizeof(uint32_t),
3427             F.IdentifierTableData,
3428             ASTIdentifierLookupTrait(*this, F));
3429 
3430         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3431       }
3432       break;
3433 
3434     case IDENTIFIER_OFFSET: {
3435       if (F.LocalNumIdentifiers != 0)
3436         return llvm::createStringError(
3437             std::errc::illegal_byte_sequence,
3438             "duplicate IDENTIFIER_OFFSET record in AST file");
3439       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3440       F.LocalNumIdentifiers = Record[0];
3441       unsigned LocalBaseIdentifierID = Record[1];
3442       F.BaseIdentifierID = getTotalNumIdentifiers();
3443 
3444       if (F.LocalNumIdentifiers > 0) {
3445         // Introduce the global -> local mapping for identifiers within this
3446         // module.
3447         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3448                                                   &F));
3449 
3450         // Introduce the local -> global mapping for identifiers within this
3451         // module.
3452         F.IdentifierRemap.insertOrReplace(
3453           std::make_pair(LocalBaseIdentifierID,
3454                          F.BaseIdentifierID - LocalBaseIdentifierID));
3455 
3456         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3457                                  + F.LocalNumIdentifiers);
3458       }
3459       break;
3460     }
3461 
3462     case INTERESTING_IDENTIFIERS:
3463       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3464       break;
3465 
3466     case EAGERLY_DESERIALIZED_DECLS:
3467       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3468       // about "interesting" decls (for instance, if we're building a module).
3469       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3470         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3471       break;
3472 
3473     case MODULAR_CODEGEN_DECLS:
3474       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3475       // them (ie: if we're not codegenerating this module).
3476       if (F.Kind == MK_MainFile ||
3477           getContext().getLangOpts().BuildingPCHWithObjectFile)
3478         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3479           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3480       break;
3481 
3482     case SPECIAL_TYPES:
3483       if (SpecialTypes.empty()) {
3484         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3485           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3486         break;
3487       }
3488 
3489       if (SpecialTypes.size() != Record.size())
3490         return llvm::createStringError(std::errc::illegal_byte_sequence,
3491                                        "invalid special-types record");
3492 
3493       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3494         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3495         if (!SpecialTypes[I])
3496           SpecialTypes[I] = ID;
3497         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3498         // merge step?
3499       }
3500       break;
3501 
3502     case STATISTICS:
3503       TotalNumStatements += Record[0];
3504       TotalNumMacros += Record[1];
3505       TotalLexicalDeclContexts += Record[2];
3506       TotalVisibleDeclContexts += Record[3];
3507       break;
3508 
3509     case UNUSED_FILESCOPED_DECLS:
3510       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3511         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3512       break;
3513 
3514     case DELEGATING_CTORS:
3515       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3516         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3517       break;
3518 
3519     case WEAK_UNDECLARED_IDENTIFIERS:
3520       if (Record.size() % 3 != 0)
3521         return llvm::createStringError(std::errc::illegal_byte_sequence,
3522                                        "invalid weak identifiers record");
3523 
3524       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3525       // files. This isn't the way to do it :)
3526       WeakUndeclaredIdentifiers.clear();
3527 
3528       // Translate the weak, undeclared identifiers into global IDs.
3529       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3530         WeakUndeclaredIdentifiers.push_back(
3531           getGlobalIdentifierID(F, Record[I++]));
3532         WeakUndeclaredIdentifiers.push_back(
3533           getGlobalIdentifierID(F, Record[I++]));
3534         WeakUndeclaredIdentifiers.push_back(
3535             ReadSourceLocation(F, Record, I).getRawEncoding());
3536       }
3537       break;
3538 
3539     case SELECTOR_OFFSETS: {
3540       F.SelectorOffsets = (const uint32_t *)Blob.data();
3541       F.LocalNumSelectors = Record[0];
3542       unsigned LocalBaseSelectorID = Record[1];
3543       F.BaseSelectorID = getTotalNumSelectors();
3544 
3545       if (F.LocalNumSelectors > 0) {
3546         // Introduce the global -> local mapping for selectors within this
3547         // module.
3548         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3549 
3550         // Introduce the local -> global mapping for selectors within this
3551         // module.
3552         F.SelectorRemap.insertOrReplace(
3553           std::make_pair(LocalBaseSelectorID,
3554                          F.BaseSelectorID - LocalBaseSelectorID));
3555 
3556         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3557       }
3558       break;
3559     }
3560 
3561     case METHOD_POOL:
3562       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3563       if (Record[0])
3564         F.SelectorLookupTable
3565           = ASTSelectorLookupTable::Create(
3566                         F.SelectorLookupTableData + Record[0],
3567                         F.SelectorLookupTableData,
3568                         ASTSelectorLookupTrait(*this, F));
3569       TotalNumMethodPoolEntries += Record[1];
3570       break;
3571 
3572     case REFERENCED_SELECTOR_POOL:
3573       if (!Record.empty()) {
3574         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3575           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3576                                                                 Record[Idx++]));
3577           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3578                                               getRawEncoding());
3579         }
3580       }
3581       break;
3582 
3583     case PP_ASSUME_NONNULL_LOC: {
3584       unsigned Idx = 0;
3585       if (!Record.empty())
3586         PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3587             ReadSourceLocation(F, Record, Idx));
3588       break;
3589     }
3590 
3591     case PP_CONDITIONAL_STACK:
3592       if (!Record.empty()) {
3593         unsigned Idx = 0, End = Record.size() - 1;
3594         bool ReachedEOFWhileSkipping = Record[Idx++];
3595         std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3596         if (ReachedEOFWhileSkipping) {
3597           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3598           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3599           bool FoundNonSkipPortion = Record[Idx++];
3600           bool FoundElse = Record[Idx++];
3601           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3602           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3603                            FoundElse, ElseLoc);
3604         }
3605         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3606         while (Idx < End) {
3607           auto Loc = ReadSourceLocation(F, Record, Idx);
3608           bool WasSkipping = Record[Idx++];
3609           bool FoundNonSkip = Record[Idx++];
3610           bool FoundElse = Record[Idx++];
3611           ConditionalStack.push_back(
3612               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3613         }
3614         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3615       }
3616       break;
3617 
3618     case PP_COUNTER_VALUE:
3619       if (!Record.empty() && Listener)
3620         Listener->ReadCounter(F, Record[0]);
3621       break;
3622 
3623     case FILE_SORTED_DECLS:
3624       F.FileSortedDecls = (const DeclID *)Blob.data();
3625       F.NumFileSortedDecls = Record[0];
3626       break;
3627 
3628     case SOURCE_LOCATION_OFFSETS: {
3629       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3630       F.LocalNumSLocEntries = Record[0];
3631       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3632       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3633       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3634           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3635                                               SLocSpaceSize);
3636       if (!F.SLocEntryBaseID) {
3637         if (!Diags.isDiagnosticInFlight()) {
3638           Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3639           SourceMgr.noteSLocAddressSpaceUsage(Diags);
3640         }
3641         return llvm::createStringError(std::errc::invalid_argument,
3642                                        "ran out of source locations");
3643       }
3644       // Make our entry in the range map. BaseID is negative and growing, so
3645       // we invert it. Because we invert it, though, we need the other end of
3646       // the range.
3647       unsigned RangeStart =
3648           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3649       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3650       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3651 
3652       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3653       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3654       GlobalSLocOffsetMap.insert(
3655           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3656                            - SLocSpaceSize,&F));
3657 
3658       // Initialize the remapping table.
3659       // Invalid stays invalid.
3660       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3661       // This module. Base was 2 when being compiled.
3662       F.SLocRemap.insertOrReplace(std::make_pair(
3663           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3664 
3665       TotalNumSLocEntries += F.LocalNumSLocEntries;
3666       break;
3667     }
3668 
3669     case MODULE_OFFSET_MAP:
3670       F.ModuleOffsetMap = Blob;
3671       break;
3672 
3673     case SOURCE_MANAGER_LINE_TABLE:
3674       ParseLineTable(F, Record);
3675       break;
3676 
3677     case EXT_VECTOR_DECLS:
3678       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3679         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3680       break;
3681 
3682     case VTABLE_USES:
3683       if (Record.size() % 3 != 0)
3684         return llvm::createStringError(std::errc::illegal_byte_sequence,
3685                                        "Invalid VTABLE_USES record");
3686 
3687       // Later tables overwrite earlier ones.
3688       // FIXME: Modules will have some trouble with this. This is clearly not
3689       // the right way to do this.
3690       VTableUses.clear();
3691 
3692       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3693         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3694         VTableUses.push_back(
3695           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3696         VTableUses.push_back(Record[Idx++]);
3697       }
3698       break;
3699 
3700     case PENDING_IMPLICIT_INSTANTIATIONS:
3701       if (PendingInstantiations.size() % 2 != 0)
3702         return llvm::createStringError(
3703             std::errc::illegal_byte_sequence,
3704             "Invalid existing PendingInstantiations");
3705 
3706       if (Record.size() % 2 != 0)
3707         return llvm::createStringError(
3708             std::errc::illegal_byte_sequence,
3709             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3710 
3711       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3712         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3713         PendingInstantiations.push_back(
3714           ReadSourceLocation(F, Record, I).getRawEncoding());
3715       }
3716       break;
3717 
3718     case SEMA_DECL_REFS:
3719       if (Record.size() != 3)
3720         return llvm::createStringError(std::errc::illegal_byte_sequence,
3721                                        "Invalid SEMA_DECL_REFS block");
3722       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3723         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3724       break;
3725 
3726     case PPD_ENTITIES_OFFSETS: {
3727       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3728       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3729       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3730 
3731       unsigned LocalBasePreprocessedEntityID = Record[0];
3732 
3733       unsigned StartingID;
3734       if (!PP.getPreprocessingRecord())
3735         PP.createPreprocessingRecord();
3736       if (!PP.getPreprocessingRecord()->getExternalSource())
3737         PP.getPreprocessingRecord()->SetExternalSource(*this);
3738       StartingID
3739         = PP.getPreprocessingRecord()
3740             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3741       F.BasePreprocessedEntityID = StartingID;
3742 
3743       if (F.NumPreprocessedEntities > 0) {
3744         // Introduce the global -> local mapping for preprocessed entities in
3745         // this module.
3746         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3747 
3748         // Introduce the local -> global mapping for preprocessed entities in
3749         // this module.
3750         F.PreprocessedEntityRemap.insertOrReplace(
3751           std::make_pair(LocalBasePreprocessedEntityID,
3752             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3753       }
3754 
3755       break;
3756     }
3757 
3758     case PPD_SKIPPED_RANGES: {
3759       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3760       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3761       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3762 
3763       if (!PP.getPreprocessingRecord())
3764         PP.createPreprocessingRecord();
3765       if (!PP.getPreprocessingRecord()->getExternalSource())
3766         PP.getPreprocessingRecord()->SetExternalSource(*this);
3767       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3768           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3769 
3770       if (F.NumPreprocessedSkippedRanges > 0)
3771         GlobalSkippedRangeMap.insert(
3772             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3773       break;
3774     }
3775 
3776     case DECL_UPDATE_OFFSETS:
3777       if (Record.size() % 2 != 0)
3778         return llvm::createStringError(
3779             std::errc::illegal_byte_sequence,
3780             "invalid DECL_UPDATE_OFFSETS block in AST file");
3781       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3782         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3783         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3784 
3785         // If we've already loaded the decl, perform the updates when we finish
3786         // loading this block.
3787         if (Decl *D = GetExistingDecl(ID))
3788           PendingUpdateRecords.push_back(
3789               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3790       }
3791       break;
3792 
3793     case OBJC_CATEGORIES_MAP:
3794       if (F.LocalNumObjCCategoriesInMap != 0)
3795         return llvm::createStringError(
3796             std::errc::illegal_byte_sequence,
3797             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3798 
3799       F.LocalNumObjCCategoriesInMap = Record[0];
3800       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3801       break;
3802 
3803     case OBJC_CATEGORIES:
3804       F.ObjCCategories.swap(Record);
3805       break;
3806 
3807     case CUDA_SPECIAL_DECL_REFS:
3808       // Later tables overwrite earlier ones.
3809       // FIXME: Modules will have trouble with this.
3810       CUDASpecialDeclRefs.clear();
3811       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3812         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3813       break;
3814 
3815     case HEADER_SEARCH_TABLE:
3816       F.HeaderFileInfoTableData = Blob.data();
3817       F.LocalNumHeaderFileInfos = Record[1];
3818       if (Record[0]) {
3819         F.HeaderFileInfoTable
3820           = HeaderFileInfoLookupTable::Create(
3821                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3822                    (const unsigned char *)F.HeaderFileInfoTableData,
3823                    HeaderFileInfoTrait(*this, F,
3824                                        &PP.getHeaderSearchInfo(),
3825                                        Blob.data() + Record[2]));
3826 
3827         PP.getHeaderSearchInfo().SetExternalSource(this);
3828         if (!PP.getHeaderSearchInfo().getExternalLookup())
3829           PP.getHeaderSearchInfo().SetExternalLookup(this);
3830       }
3831       break;
3832 
3833     case FP_PRAGMA_OPTIONS:
3834       // Later tables overwrite earlier ones.
3835       FPPragmaOptions.swap(Record);
3836       break;
3837 
3838     case OPENCL_EXTENSIONS:
3839       for (unsigned I = 0, E = Record.size(); I != E; ) {
3840         auto Name = ReadString(Record, I);
3841         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3842         OptInfo.Supported = Record[I++] != 0;
3843         OptInfo.Enabled = Record[I++] != 0;
3844         OptInfo.WithPragma = Record[I++] != 0;
3845         OptInfo.Avail = Record[I++];
3846         OptInfo.Core = Record[I++];
3847         OptInfo.Opt = Record[I++];
3848       }
3849       break;
3850 
3851     case TENTATIVE_DEFINITIONS:
3852       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3853         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3854       break;
3855 
3856     case KNOWN_NAMESPACES:
3857       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3858         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3859       break;
3860 
3861     case UNDEFINED_BUT_USED:
3862       if (UndefinedButUsed.size() % 2 != 0)
3863         return llvm::createStringError(std::errc::illegal_byte_sequence,
3864                                        "Invalid existing UndefinedButUsed");
3865 
3866       if (Record.size() % 2 != 0)
3867         return llvm::createStringError(std::errc::illegal_byte_sequence,
3868                                        "invalid undefined-but-used record");
3869       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3870         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3871         UndefinedButUsed.push_back(
3872             ReadSourceLocation(F, Record, I).getRawEncoding());
3873       }
3874       break;
3875 
3876     case DELETE_EXPRS_TO_ANALYZE:
3877       for (unsigned I = 0, N = Record.size(); I != N;) {
3878         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3879         const uint64_t Count = Record[I++];
3880         DelayedDeleteExprs.push_back(Count);
3881         for (uint64_t C = 0; C < Count; ++C) {
3882           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3883           bool IsArrayForm = Record[I++] == 1;
3884           DelayedDeleteExprs.push_back(IsArrayForm);
3885         }
3886       }
3887       break;
3888 
3889     case IMPORTED_MODULES:
3890       if (!F.isModule()) {
3891         // If we aren't loading a module (which has its own exports), make
3892         // all of the imported modules visible.
3893         // FIXME: Deal with macros-only imports.
3894         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3895           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3896           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3897           if (GlobalID) {
3898             PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3899             if (DeserializationListener)
3900               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3901           }
3902         }
3903       }
3904       break;
3905 
3906     case MACRO_OFFSET: {
3907       if (F.LocalNumMacros != 0)
3908         return llvm::createStringError(
3909             std::errc::illegal_byte_sequence,
3910             "duplicate MACRO_OFFSET record in AST file");
3911       F.MacroOffsets = (const uint32_t *)Blob.data();
3912       F.LocalNumMacros = Record[0];
3913       unsigned LocalBaseMacroID = Record[1];
3914       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3915       F.BaseMacroID = getTotalNumMacros();
3916 
3917       if (F.LocalNumMacros > 0) {
3918         // Introduce the global -> local mapping for macros within this module.
3919         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3920 
3921         // Introduce the local -> global mapping for macros within this module.
3922         F.MacroRemap.insertOrReplace(
3923           std::make_pair(LocalBaseMacroID,
3924                          F.BaseMacroID - LocalBaseMacroID));
3925 
3926         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3927       }
3928       break;
3929     }
3930 
3931     case LATE_PARSED_TEMPLATE:
3932       LateParsedTemplates.emplace_back(
3933           std::piecewise_construct, std::forward_as_tuple(&F),
3934           std::forward_as_tuple(Record.begin(), Record.end()));
3935       break;
3936 
3937     case OPTIMIZE_PRAGMA_OPTIONS:
3938       if (Record.size() != 1)
3939         return llvm::createStringError(std::errc::illegal_byte_sequence,
3940                                        "invalid pragma optimize record");
3941       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3942       break;
3943 
3944     case MSSTRUCT_PRAGMA_OPTIONS:
3945       if (Record.size() != 1)
3946         return llvm::createStringError(std::errc::illegal_byte_sequence,
3947                                        "invalid pragma ms_struct record");
3948       PragmaMSStructState = Record[0];
3949       break;
3950 
3951     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3952       if (Record.size() != 2)
3953         return llvm::createStringError(
3954             std::errc::illegal_byte_sequence,
3955             "invalid pragma pointers to members record");
3956       PragmaMSPointersToMembersState = Record[0];
3957       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3958       break;
3959 
3960     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3961       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3962         UnusedLocalTypedefNameCandidates.push_back(
3963             getGlobalDeclID(F, Record[I]));
3964       break;
3965 
3966     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3967       if (Record.size() != 1)
3968         return llvm::createStringError(std::errc::illegal_byte_sequence,
3969                                        "invalid cuda pragma options record");
3970       ForceCUDAHostDeviceDepth = Record[0];
3971       break;
3972 
3973     case ALIGN_PACK_PRAGMA_OPTIONS: {
3974       if (Record.size() < 3)
3975         return llvm::createStringError(std::errc::illegal_byte_sequence,
3976                                        "invalid pragma pack record");
3977       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3978       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3979       unsigned NumStackEntries = Record[2];
3980       unsigned Idx = 3;
3981       // Reset the stack when importing a new module.
3982       PragmaAlignPackStack.clear();
3983       for (unsigned I = 0; I < NumStackEntries; ++I) {
3984         PragmaAlignPackStackEntry Entry;
3985         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3986         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3987         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3988         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3989         Entry.SlotLabel = PragmaAlignPackStrings.back();
3990         PragmaAlignPackStack.push_back(Entry);
3991       }
3992       break;
3993     }
3994 
3995     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3996       if (Record.size() < 3)
3997         return llvm::createStringError(std::errc::illegal_byte_sequence,
3998                                        "invalid pragma float control record");
3999       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
4000       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
4001       unsigned NumStackEntries = Record[2];
4002       unsigned Idx = 3;
4003       // Reset the stack when importing a new module.
4004       FpPragmaStack.clear();
4005       for (unsigned I = 0; I < NumStackEntries; ++I) {
4006         FpPragmaStackEntry Entry;
4007         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
4008         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4009         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4010         FpPragmaStrings.push_back(ReadString(Record, Idx));
4011         Entry.SlotLabel = FpPragmaStrings.back();
4012         FpPragmaStack.push_back(Entry);
4013       }
4014       break;
4015     }
4016 
4017     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4018       for (unsigned I = 0, N = Record.size(); I != N; ++I)
4019         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
4020       break;
4021     }
4022   }
4023 }
4024 
ReadModuleOffsetMap(ModuleFile & F) const4025 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4026   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4027 
4028   // Additional remapping information.
4029   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4030   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4031   F.ModuleOffsetMap = StringRef();
4032 
4033   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
4034   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
4035     F.SLocRemap.insert(std::make_pair(0U, 0));
4036     F.SLocRemap.insert(std::make_pair(2U, 1));
4037   }
4038 
4039   // Continuous range maps we may be updating in our module.
4040   using SLocRemapBuilder =
4041       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
4042                          2>::Builder;
4043   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4044   SLocRemapBuilder SLocRemap(F.SLocRemap);
4045   RemapBuilder IdentifierRemap(F.IdentifierRemap);
4046   RemapBuilder MacroRemap(F.MacroRemap);
4047   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4048   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4049   RemapBuilder SelectorRemap(F.SelectorRemap);
4050   RemapBuilder DeclRemap(F.DeclRemap);
4051   RemapBuilder TypeRemap(F.TypeRemap);
4052 
4053   while (Data < DataEnd) {
4054     // FIXME: Looking up dependency modules by filename is horrible. Let's
4055     // start fixing this with prebuilt, explicit and implicit modules and see
4056     // how it goes...
4057     using namespace llvm::support;
4058     ModuleKind Kind = static_cast<ModuleKind>(
4059         endian::readNext<uint8_t, llvm::endianness::little, unaligned>(Data));
4060     uint16_t Len =
4061         endian::readNext<uint16_t, llvm::endianness::little, unaligned>(Data);
4062     StringRef Name = StringRef((const char*)Data, Len);
4063     Data += Len;
4064     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4065                               Kind == MK_ImplicitModule
4066                           ? ModuleMgr.lookupByModuleName(Name)
4067                           : ModuleMgr.lookupByFileName(Name));
4068     if (!OM) {
4069       std::string Msg =
4070           "SourceLocation remap refers to unknown module, cannot find ";
4071       Msg.append(std::string(Name));
4072       Error(Msg);
4073       return;
4074     }
4075 
4076     SourceLocation::UIntTy SLocOffset =
4077         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4078     uint32_t IdentifierIDOffset =
4079         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4080     uint32_t MacroIDOffset =
4081         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4082     uint32_t PreprocessedEntityIDOffset =
4083         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4084     uint32_t SubmoduleIDOffset =
4085         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4086     uint32_t SelectorIDOffset =
4087         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4088     uint32_t DeclIDOffset =
4089         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4090     uint32_t TypeIndexOffset =
4091         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4092 
4093     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4094                          RemapBuilder &Remap) {
4095       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4096       if (Offset != None)
4097         Remap.insert(std::make_pair(Offset,
4098                                     static_cast<int>(BaseOffset - Offset)));
4099     };
4100 
4101     constexpr SourceLocation::UIntTy SLocNone =
4102         std::numeric_limits<SourceLocation::UIntTy>::max();
4103     if (SLocOffset != SLocNone)
4104       SLocRemap.insert(std::make_pair(
4105           SLocOffset, static_cast<SourceLocation::IntTy>(
4106                           OM->SLocEntryBaseOffset - SLocOffset)));
4107 
4108     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
4109     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4110     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4111               PreprocessedEntityRemap);
4112     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4113     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4114     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
4115     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
4116 
4117     // Global -> local mappings.
4118     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
4119   }
4120 }
4121 
4122 ASTReader::ASTReadResult
ReadModuleMapFileBlock(RecordData & Record,ModuleFile & F,const ModuleFile * ImportedBy,unsigned ClientLoadCapabilities)4123 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4124                                   const ModuleFile *ImportedBy,
4125                                   unsigned ClientLoadCapabilities) {
4126   unsigned Idx = 0;
4127   F.ModuleMapPath = ReadPath(F, Record, Idx);
4128 
4129   // Try to resolve ModuleName in the current header search context and
4130   // verify that it is found in the same module map file as we saved. If the
4131   // top-level AST file is a main file, skip this check because there is no
4132   // usable header search context.
4133   assert(!F.ModuleName.empty() &&
4134          "MODULE_NAME should come before MODULE_MAP_FILE");
4135   if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4136       F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4137     // An implicitly-loaded module file should have its module listed in some
4138     // module map file that we've already loaded.
4139     Module *M =
4140         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
4141     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4142     OptionalFileEntryRef ModMap =
4143         M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4144     // Don't emit module relocation error if we have -fno-validate-pch
4145     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4146               DisableValidationForModuleKind::Module) &&
4147         !ModMap) {
4148       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
4149         if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4150           // This module was defined by an imported (explicit) module.
4151           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4152                                                << ASTFE->getName();
4153         } else {
4154           // This module was built with a different module map.
4155           Diag(diag::err_imported_module_not_found)
4156               << F.ModuleName << F.FileName
4157               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4158               << !ImportedBy;
4159           // In case it was imported by a PCH, there's a chance the user is
4160           // just missing to include the search path to the directory containing
4161           // the modulemap.
4162           if (ImportedBy && ImportedBy->Kind == MK_PCH)
4163             Diag(diag::note_imported_by_pch_module_not_found)
4164                 << llvm::sys::path::parent_path(F.ModuleMapPath);
4165         }
4166       }
4167       return OutOfDate;
4168     }
4169 
4170     assert(M && M->Name == F.ModuleName && "found module with different name");
4171 
4172     // Check the primary module map file.
4173     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
4174     if (!StoredModMap || *StoredModMap != ModMap) {
4175       assert(ModMap && "found module is missing module map file");
4176       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4177              "top-level import should be verified");
4178       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4179       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4180         Diag(diag::err_imported_module_modmap_changed)
4181             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4182             << ModMap->getName() << F.ModuleMapPath << NotImported;
4183       return OutOfDate;
4184     }
4185 
4186     ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4187     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4188       // FIXME: we should use input files rather than storing names.
4189       std::string Filename = ReadPath(F, Record, Idx);
4190       auto SF = FileMgr.getOptionalFileRef(Filename, false, false);
4191       if (!SF) {
4192         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4193           Error("could not find file '" + Filename +"' referenced by AST file");
4194         return OutOfDate;
4195       }
4196       AdditionalStoredMaps.insert(*SF);
4197     }
4198 
4199     // Check any additional module map files (e.g. module.private.modulemap)
4200     // that are not in the pcm.
4201     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4202       for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4203         // Remove files that match
4204         // Note: SmallPtrSet::erase is really remove
4205         if (!AdditionalStoredMaps.erase(ModMap)) {
4206           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4207             Diag(diag::err_module_different_modmap)
4208               << F.ModuleName << /*new*/0 << ModMap.getName();
4209           return OutOfDate;
4210         }
4211       }
4212     }
4213 
4214     // Check any additional module map files that are in the pcm, but not
4215     // found in header search. Cases that match are already removed.
4216     for (FileEntryRef ModMap : AdditionalStoredMaps) {
4217       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4218         Diag(diag::err_module_different_modmap)
4219           << F.ModuleName << /*not new*/1 << ModMap.getName();
4220       return OutOfDate;
4221     }
4222   }
4223 
4224   if (Listener)
4225     Listener->ReadModuleMapFile(F.ModuleMapPath);
4226   return Success;
4227 }
4228 
4229 /// Move the given method to the back of the global list of methods.
moveMethodToBackOfGlobalList(Sema & S,ObjCMethodDecl * Method)4230 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4231   // Find the entry for this selector in the method pool.
4232   Sema::GlobalMethodPool::iterator Known
4233     = S.MethodPool.find(Method->getSelector());
4234   if (Known == S.MethodPool.end())
4235     return;
4236 
4237   // Retrieve the appropriate method list.
4238   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4239                                                     : Known->second.second;
4240   bool Found = false;
4241   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4242     if (!Found) {
4243       if (List->getMethod() == Method) {
4244         Found = true;
4245       } else {
4246         // Keep searching.
4247         continue;
4248       }
4249     }
4250 
4251     if (List->getNext())
4252       List->setMethod(List->getNext()->getMethod());
4253     else
4254       List->setMethod(Method);
4255   }
4256 }
4257 
makeNamesVisible(const HiddenNames & Names,Module * Owner)4258 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4259   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4260   for (Decl *D : Names) {
4261     bool wasHidden = !D->isUnconditionallyVisible();
4262     D->setVisibleDespiteOwningModule();
4263 
4264     if (wasHidden && SemaObj) {
4265       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4266         moveMethodToBackOfGlobalList(*SemaObj, Method);
4267       }
4268     }
4269   }
4270 }
4271 
makeModuleVisible(Module * Mod,Module::NameVisibilityKind NameVisibility,SourceLocation ImportLoc)4272 void ASTReader::makeModuleVisible(Module *Mod,
4273                                   Module::NameVisibilityKind NameVisibility,
4274                                   SourceLocation ImportLoc) {
4275   llvm::SmallPtrSet<Module *, 4> Visited;
4276   SmallVector<Module *, 4> Stack;
4277   Stack.push_back(Mod);
4278   while (!Stack.empty()) {
4279     Mod = Stack.pop_back_val();
4280 
4281     if (NameVisibility <= Mod->NameVisibility) {
4282       // This module already has this level of visibility (or greater), so
4283       // there is nothing more to do.
4284       continue;
4285     }
4286 
4287     if (Mod->isUnimportable()) {
4288       // Modules that aren't importable cannot be made visible.
4289       continue;
4290     }
4291 
4292     // Update the module's name visibility.
4293     Mod->NameVisibility = NameVisibility;
4294 
4295     // If we've already deserialized any names from this module,
4296     // mark them as visible.
4297     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4298     if (Hidden != HiddenNamesMap.end()) {
4299       auto HiddenNames = std::move(*Hidden);
4300       HiddenNamesMap.erase(Hidden);
4301       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4302       assert(!HiddenNamesMap.contains(Mod) &&
4303              "making names visible added hidden names");
4304     }
4305 
4306     // Push any exported modules onto the stack to be marked as visible.
4307     SmallVector<Module *, 16> Exports;
4308     Mod->getExportedModules(Exports);
4309     for (SmallVectorImpl<Module *>::iterator
4310            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4311       Module *Exported = *I;
4312       if (Visited.insert(Exported).second)
4313         Stack.push_back(Exported);
4314     }
4315   }
4316 }
4317 
4318 /// We've merged the definition \p MergedDef into the existing definition
4319 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4320 /// visible.
mergeDefinitionVisibility(NamedDecl * Def,NamedDecl * MergedDef)4321 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4322                                           NamedDecl *MergedDef) {
4323   if (!Def->isUnconditionallyVisible()) {
4324     // If MergedDef is visible or becomes visible, make the definition visible.
4325     if (MergedDef->isUnconditionallyVisible())
4326       Def->setVisibleDespiteOwningModule();
4327     else {
4328       getContext().mergeDefinitionIntoModule(
4329           Def, MergedDef->getImportedOwningModule(),
4330           /*NotifyListeners*/ false);
4331       PendingMergedDefinitionsToDeduplicate.insert(Def);
4332     }
4333   }
4334 }
4335 
loadGlobalIndex()4336 bool ASTReader::loadGlobalIndex() {
4337   if (GlobalIndex)
4338     return false;
4339 
4340   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4341       !PP.getLangOpts().Modules)
4342     return true;
4343 
4344   // Try to load the global index.
4345   TriedLoadingGlobalIndex = true;
4346   StringRef ModuleCachePath
4347     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4348   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4349       GlobalModuleIndex::readIndex(ModuleCachePath);
4350   if (llvm::Error Err = std::move(Result.second)) {
4351     assert(!Result.first);
4352     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4353     return true;
4354   }
4355 
4356   GlobalIndex.reset(Result.first);
4357   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4358   return false;
4359 }
4360 
isGlobalIndexUnavailable() const4361 bool ASTReader::isGlobalIndexUnavailable() const {
4362   return PP.getLangOpts().Modules && UseGlobalIndex &&
4363          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4364 }
4365 
updateModuleTimestamp(ModuleFile & MF)4366 static void updateModuleTimestamp(ModuleFile &MF) {
4367   // Overwrite the timestamp file contents so that file's mtime changes.
4368   std::string TimestampFilename = MF.getTimestampFilename();
4369   std::error_code EC;
4370   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4371                           llvm::sys::fs::OF_TextWithCRLF);
4372   if (EC)
4373     return;
4374   OS << "Timestamp file\n";
4375   OS.close();
4376   OS.clear_error(); // Avoid triggering a fatal error.
4377 }
4378 
4379 /// Given a cursor at the start of an AST file, scan ahead and drop the
4380 /// cursor into the start of the given block ID, returning false on success and
4381 /// true on failure.
SkipCursorToBlock(BitstreamCursor & Cursor,unsigned BlockID)4382 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4383   while (true) {
4384     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4385     if (!MaybeEntry) {
4386       // FIXME this drops errors on the floor.
4387       consumeError(MaybeEntry.takeError());
4388       return true;
4389     }
4390     llvm::BitstreamEntry Entry = MaybeEntry.get();
4391 
4392     switch (Entry.Kind) {
4393     case llvm::BitstreamEntry::Error:
4394     case llvm::BitstreamEntry::EndBlock:
4395       return true;
4396 
4397     case llvm::BitstreamEntry::Record:
4398       // Ignore top-level records.
4399       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4400         break;
4401       else {
4402         // FIXME this drops errors on the floor.
4403         consumeError(Skipped.takeError());
4404         return true;
4405       }
4406 
4407     case llvm::BitstreamEntry::SubBlock:
4408       if (Entry.ID == BlockID) {
4409         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4410           // FIXME this drops the error on the floor.
4411           consumeError(std::move(Err));
4412           return true;
4413         }
4414         // Found it!
4415         return false;
4416       }
4417 
4418       if (llvm::Error Err = Cursor.SkipBlock()) {
4419         // FIXME this drops the error on the floor.
4420         consumeError(std::move(Err));
4421         return true;
4422       }
4423     }
4424   }
4425 }
4426 
ReadAST(StringRef FileName,ModuleKind Type,SourceLocation ImportLoc,unsigned ClientLoadCapabilities,ModuleFile ** NewLoadedModuleFile)4427 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4428                                             SourceLocation ImportLoc,
4429                                             unsigned ClientLoadCapabilities,
4430                                             ModuleFile **NewLoadedModuleFile) {
4431   llvm::TimeTraceScope scope("ReadAST", FileName);
4432 
4433   llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4434   llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4435       CurrentDeserializingModuleKind, Type);
4436 
4437   // Defer any pending actions until we get to the end of reading the AST file.
4438   Deserializing AnASTFile(this);
4439 
4440   // Bump the generation number.
4441   unsigned PreviousGeneration = 0;
4442   if (ContextObj)
4443     PreviousGeneration = incrementGeneration(*ContextObj);
4444 
4445   unsigned NumModules = ModuleMgr.size();
4446   SmallVector<ImportedModule, 4> Loaded;
4447   if (ASTReadResult ReadResult =
4448           ReadASTCore(FileName, Type, ImportLoc,
4449                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4450                       ClientLoadCapabilities)) {
4451     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4452 
4453     // If we find that any modules are unusable, the global index is going
4454     // to be out-of-date. Just remove it.
4455     GlobalIndex.reset();
4456     ModuleMgr.setGlobalIndex(nullptr);
4457     return ReadResult;
4458   }
4459 
4460   if (NewLoadedModuleFile && !Loaded.empty())
4461     *NewLoadedModuleFile = Loaded.back().Mod;
4462 
4463   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4464   // remove modules from this point. Various fields are updated during reading
4465   // the AST block and removing the modules would result in dangling pointers.
4466   // They are generally only incidentally dereferenced, ie. a binary search
4467   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4468   // be dereferenced but it wouldn't actually be used.
4469 
4470   // Load the AST blocks of all of the modules that we loaded. We can still
4471   // hit errors parsing the ASTs at this point.
4472   for (ImportedModule &M : Loaded) {
4473     ModuleFile &F = *M.Mod;
4474     llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4475 
4476     // Read the AST block.
4477     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4478       Error(std::move(Err));
4479       return Failure;
4480     }
4481 
4482     // The AST block should always have a definition for the main module.
4483     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4484       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4485       return Failure;
4486     }
4487 
4488     // Read the extension blocks.
4489     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4490       if (llvm::Error Err = ReadExtensionBlock(F)) {
4491         Error(std::move(Err));
4492         return Failure;
4493       }
4494     }
4495 
4496     // Once read, set the ModuleFile bit base offset and update the size in
4497     // bits of all files we've seen.
4498     F.GlobalBitOffset = TotalModulesSizeInBits;
4499     TotalModulesSizeInBits += F.SizeInBits;
4500     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4501   }
4502 
4503   // Preload source locations and interesting indentifiers.
4504   for (ImportedModule &M : Loaded) {
4505     ModuleFile &F = *M.Mod;
4506 
4507     // Map the original source file ID into the ID space of the current
4508     // compilation.
4509     if (F.OriginalSourceFileID.isValid())
4510       F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4511 
4512     for (auto Offset : F.PreloadIdentifierOffsets) {
4513       const unsigned char *Data = F.IdentifierTableData + Offset;
4514 
4515       ASTIdentifierLookupTrait Trait(*this, F);
4516       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4517       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4518 
4519       IdentifierInfo *II;
4520       if (!PP.getLangOpts().CPlusPlus) {
4521         // Identifiers present in both the module file and the importing
4522         // instance are marked out-of-date so that they can be deserialized
4523         // on next use via ASTReader::updateOutOfDateIdentifier().
4524         // Identifiers present in the module file but not in the importing
4525         // instance are ignored for now, preventing growth of the identifier
4526         // table. They will be deserialized on first use via ASTReader::get().
4527         auto It = PP.getIdentifierTable().find(Key);
4528         if (It == PP.getIdentifierTable().end())
4529           continue;
4530         II = It->second;
4531       } else {
4532         // With C++ modules, not many identifiers are considered interesting.
4533         // All identifiers in the module file can be placed into the identifier
4534         // table of the importing instance and marked as out-of-date. This makes
4535         // ASTReader::get() a no-op, and deserialization will take place on
4536         // first/next use via ASTReader::updateOutOfDateIdentifier().
4537         II = &PP.getIdentifierTable().getOwn(Key);
4538       }
4539 
4540       II->setOutOfDate(true);
4541 
4542       // Mark this identifier as being from an AST file so that we can track
4543       // whether we need to serialize it.
4544       markIdentifierFromAST(*this, *II);
4545 
4546       // Associate the ID with the identifier so that the writer can reuse it.
4547       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4548       SetIdentifierInfo(ID, II);
4549     }
4550   }
4551 
4552   // Builtins and library builtins have already been initialized. Mark all
4553   // identifiers as out-of-date, so that they are deserialized on first use.
4554   if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4555     for (auto &Id : PP.getIdentifierTable())
4556       Id.second->setOutOfDate(true);
4557 
4558   // Mark selectors as out of date.
4559   for (const auto &Sel : SelectorGeneration)
4560     SelectorOutOfDate[Sel.first] = true;
4561 
4562   // Setup the import locations and notify the module manager that we've
4563   // committed to these module files.
4564   for (ImportedModule &M : Loaded) {
4565     ModuleFile &F = *M.Mod;
4566 
4567     ModuleMgr.moduleFileAccepted(&F);
4568 
4569     // Set the import location.
4570     F.DirectImportLoc = ImportLoc;
4571     // FIXME: We assume that locations from PCH / preamble do not need
4572     // any translation.
4573     if (!M.ImportedBy)
4574       F.ImportLoc = M.ImportLoc;
4575     else
4576       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4577   }
4578 
4579   // Resolve any unresolved module exports.
4580   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4581     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4582     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4583     Module *ResolvedMod = getSubmodule(GlobalID);
4584 
4585     switch (Unresolved.Kind) {
4586     case UnresolvedModuleRef::Conflict:
4587       if (ResolvedMod) {
4588         Module::Conflict Conflict;
4589         Conflict.Other = ResolvedMod;
4590         Conflict.Message = Unresolved.String.str();
4591         Unresolved.Mod->Conflicts.push_back(Conflict);
4592       }
4593       continue;
4594 
4595     case UnresolvedModuleRef::Import:
4596       if (ResolvedMod)
4597         Unresolved.Mod->Imports.insert(ResolvedMod);
4598       continue;
4599 
4600     case UnresolvedModuleRef::Affecting:
4601       if (ResolvedMod)
4602         Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4603       continue;
4604 
4605     case UnresolvedModuleRef::Export:
4606       if (ResolvedMod || Unresolved.IsWildcard)
4607         Unresolved.Mod->Exports.push_back(
4608           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4609       continue;
4610     }
4611   }
4612   UnresolvedModuleRefs.clear();
4613 
4614   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4615   // Might be unnecessary as use declarations are only used to build the
4616   // module itself.
4617 
4618   if (ContextObj)
4619     InitializeContext();
4620 
4621   if (SemaObj)
4622     UpdateSema();
4623 
4624   if (DeserializationListener)
4625     DeserializationListener->ReaderInitialized(this);
4626 
4627   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4628   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4629     // If this AST file is a precompiled preamble, then set the
4630     // preamble file ID of the source manager to the file source file
4631     // from which the preamble was built.
4632     if (Type == MK_Preamble) {
4633       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4634     } else if (Type == MK_MainFile) {
4635       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4636     }
4637   }
4638 
4639   // For any Objective-C class definitions we have already loaded, make sure
4640   // that we load any additional categories.
4641   if (ContextObj) {
4642     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4643       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4644                          ObjCClassesLoaded[I],
4645                          PreviousGeneration);
4646     }
4647   }
4648 
4649   HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4650   if (HSOpts.ModulesValidateOncePerBuildSession) {
4651     // Now we are certain that the module and all modules it depends on are
4652     // up-to-date. For implicitly-built module files, ensure the corresponding
4653     // timestamp files are up-to-date in this build session.
4654     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4655       ImportedModule &M = Loaded[I];
4656       if (M.Mod->Kind == MK_ImplicitModule &&
4657           M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4658         updateModuleTimestamp(*M.Mod);
4659     }
4660   }
4661 
4662   return Success;
4663 }
4664 
4665 static ASTFileSignature readASTFileSignature(StringRef PCH);
4666 
4667 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
doesntStartWithASTFileMagic(BitstreamCursor & Stream)4668 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4669   // FIXME checking magic headers is done in other places such as
4670   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4671   // always done the same. Unify it all with a helper.
4672   if (!Stream.canSkipToPos(4))
4673     return llvm::createStringError(std::errc::illegal_byte_sequence,
4674                                    "file too small to contain AST file magic");
4675   for (unsigned C : {'C', 'P', 'C', 'H'})
4676     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4677       if (Res.get() != C)
4678         return llvm::createStringError(
4679             std::errc::illegal_byte_sequence,
4680             "file doesn't start with AST file magic");
4681     } else
4682       return Res.takeError();
4683   return llvm::Error::success();
4684 }
4685 
moduleKindForDiagnostic(ModuleKind Kind)4686 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4687   switch (Kind) {
4688   case MK_PCH:
4689     return 0; // PCH
4690   case MK_ImplicitModule:
4691   case MK_ExplicitModule:
4692   case MK_PrebuiltModule:
4693     return 1; // module
4694   case MK_MainFile:
4695   case MK_Preamble:
4696     return 2; // main source file
4697   }
4698   llvm_unreachable("unknown module kind");
4699 }
4700 
4701 ASTReader::ASTReadResult
ReadASTCore(StringRef FileName,ModuleKind Type,SourceLocation ImportLoc,ModuleFile * ImportedBy,SmallVectorImpl<ImportedModule> & Loaded,off_t ExpectedSize,time_t ExpectedModTime,ASTFileSignature ExpectedSignature,unsigned ClientLoadCapabilities)4702 ASTReader::ReadASTCore(StringRef FileName,
4703                        ModuleKind Type,
4704                        SourceLocation ImportLoc,
4705                        ModuleFile *ImportedBy,
4706                        SmallVectorImpl<ImportedModule> &Loaded,
4707                        off_t ExpectedSize, time_t ExpectedModTime,
4708                        ASTFileSignature ExpectedSignature,
4709                        unsigned ClientLoadCapabilities) {
4710   ModuleFile *M;
4711   std::string ErrorStr;
4712   ModuleManager::AddModuleResult AddResult
4713     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4714                           getGeneration(), ExpectedSize, ExpectedModTime,
4715                           ExpectedSignature, readASTFileSignature,
4716                           M, ErrorStr);
4717 
4718   switch (AddResult) {
4719   case ModuleManager::AlreadyLoaded:
4720     Diag(diag::remark_module_import)
4721         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4722         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4723     return Success;
4724 
4725   case ModuleManager::NewlyLoaded:
4726     // Load module file below.
4727     break;
4728 
4729   case ModuleManager::Missing:
4730     // The module file was missing; if the client can handle that, return
4731     // it.
4732     if (ClientLoadCapabilities & ARR_Missing)
4733       return Missing;
4734 
4735     // Otherwise, return an error.
4736     Diag(diag::err_ast_file_not_found)
4737         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4738         << ErrorStr;
4739     return Failure;
4740 
4741   case ModuleManager::OutOfDate:
4742     // We couldn't load the module file because it is out-of-date. If the
4743     // client can handle out-of-date, return it.
4744     if (ClientLoadCapabilities & ARR_OutOfDate)
4745       return OutOfDate;
4746 
4747     // Otherwise, return an error.
4748     Diag(diag::err_ast_file_out_of_date)
4749         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4750         << ErrorStr;
4751     return Failure;
4752   }
4753 
4754   assert(M && "Missing module file");
4755 
4756   bool ShouldFinalizePCM = false;
4757   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4758     auto &MC = getModuleManager().getModuleCache();
4759     if (ShouldFinalizePCM)
4760       MC.finalizePCM(FileName);
4761     else
4762       MC.tryToDropPCM(FileName);
4763   });
4764   ModuleFile &F = *M;
4765   BitstreamCursor &Stream = F.Stream;
4766   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4767   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4768 
4769   // Sniff for the signature.
4770   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4771     Diag(diag::err_ast_file_invalid)
4772         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4773     return Failure;
4774   }
4775 
4776   // This is used for compatibility with older PCH formats.
4777   bool HaveReadControlBlock = false;
4778   while (true) {
4779     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4780     if (!MaybeEntry) {
4781       Error(MaybeEntry.takeError());
4782       return Failure;
4783     }
4784     llvm::BitstreamEntry Entry = MaybeEntry.get();
4785 
4786     switch (Entry.Kind) {
4787     case llvm::BitstreamEntry::Error:
4788     case llvm::BitstreamEntry::Record:
4789     case llvm::BitstreamEntry::EndBlock:
4790       Error("invalid record at top-level of AST file");
4791       return Failure;
4792 
4793     case llvm::BitstreamEntry::SubBlock:
4794       break;
4795     }
4796 
4797     switch (Entry.ID) {
4798     case CONTROL_BLOCK_ID:
4799       HaveReadControlBlock = true;
4800       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4801       case Success:
4802         // Check that we didn't try to load a non-module AST file as a module.
4803         //
4804         // FIXME: Should we also perform the converse check? Loading a module as
4805         // a PCH file sort of works, but it's a bit wonky.
4806         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4807              Type == MK_PrebuiltModule) &&
4808             F.ModuleName.empty()) {
4809           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4810           if (Result != OutOfDate ||
4811               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4812             Diag(diag::err_module_file_not_module) << FileName;
4813           return Result;
4814         }
4815         break;
4816 
4817       case Failure: return Failure;
4818       case Missing: return Missing;
4819       case OutOfDate: return OutOfDate;
4820       case VersionMismatch: return VersionMismatch;
4821       case ConfigurationMismatch: return ConfigurationMismatch;
4822       case HadErrors: return HadErrors;
4823       }
4824       break;
4825 
4826     case AST_BLOCK_ID:
4827       if (!HaveReadControlBlock) {
4828         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4829           Diag(diag::err_pch_version_too_old);
4830         return VersionMismatch;
4831       }
4832 
4833       // Record that we've loaded this module.
4834       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4835       ShouldFinalizePCM = true;
4836       return Success;
4837 
4838     default:
4839       if (llvm::Error Err = Stream.SkipBlock()) {
4840         Error(std::move(Err));
4841         return Failure;
4842       }
4843       break;
4844     }
4845   }
4846 
4847   llvm_unreachable("unexpected break; expected return");
4848 }
4849 
4850 ASTReader::ASTReadResult
readUnhashedControlBlock(ModuleFile & F,bool WasImportedBy,unsigned ClientLoadCapabilities)4851 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4852                                     unsigned ClientLoadCapabilities) {
4853   const HeaderSearchOptions &HSOpts =
4854       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4855   bool AllowCompatibleConfigurationMismatch =
4856       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4857   bool DisableValidation = shouldDisableValidationForFile(F);
4858 
4859   ASTReadResult Result = readUnhashedControlBlockImpl(
4860       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4861       Listener.get(),
4862       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4863 
4864   // If F was directly imported by another module, it's implicitly validated by
4865   // the importing module.
4866   if (DisableValidation || WasImportedBy ||
4867       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4868     return Success;
4869 
4870   if (Result == Failure) {
4871     Error("malformed block record in AST file");
4872     return Failure;
4873   }
4874 
4875   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4876     // If this module has already been finalized in the ModuleCache, we're stuck
4877     // with it; we can only load a single version of each module.
4878     //
4879     // This can happen when a module is imported in two contexts: in one, as a
4880     // user module; in another, as a system module (due to an import from
4881     // another module marked with the [system] flag).  It usually indicates a
4882     // bug in the module map: this module should also be marked with [system].
4883     //
4884     // If -Wno-system-headers (the default), and the first import is as a
4885     // system module, then validation will fail during the as-user import,
4886     // since -Werror flags won't have been validated.  However, it's reasonable
4887     // to treat this consistently as a system module.
4888     //
4889     // If -Wsystem-headers, the PCM on disk was built with
4890     // -Wno-system-headers, and the first import is as a user module, then
4891     // validation will fail during the as-system import since the PCM on disk
4892     // doesn't guarantee that -Werror was respected.  However, the -Werror
4893     // flags were checked during the initial as-user import.
4894     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4895       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4896       return Success;
4897     }
4898   }
4899 
4900   return Result;
4901 }
4902 
readUnhashedControlBlockImpl(ModuleFile * F,llvm::StringRef StreamData,unsigned ClientLoadCapabilities,bool AllowCompatibleConfigurationMismatch,ASTReaderListener * Listener,bool ValidateDiagnosticOptions)4903 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4904     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4905     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4906     bool ValidateDiagnosticOptions) {
4907   // Initialize a stream.
4908   BitstreamCursor Stream(StreamData);
4909 
4910   // Sniff for the signature.
4911   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4912     // FIXME this drops the error on the floor.
4913     consumeError(std::move(Err));
4914     return Failure;
4915   }
4916 
4917   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4918   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4919     return Failure;
4920 
4921   // Read all of the records in the options block.
4922   RecordData Record;
4923   ASTReadResult Result = Success;
4924   while (true) {
4925     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4926     if (!MaybeEntry) {
4927       // FIXME this drops the error on the floor.
4928       consumeError(MaybeEntry.takeError());
4929       return Failure;
4930     }
4931     llvm::BitstreamEntry Entry = MaybeEntry.get();
4932 
4933     switch (Entry.Kind) {
4934     case llvm::BitstreamEntry::Error:
4935     case llvm::BitstreamEntry::SubBlock:
4936       return Failure;
4937 
4938     case llvm::BitstreamEntry::EndBlock:
4939       return Result;
4940 
4941     case llvm::BitstreamEntry::Record:
4942       // The interesting case.
4943       break;
4944     }
4945 
4946     // Read and process a record.
4947     Record.clear();
4948     StringRef Blob;
4949     Expected<unsigned> MaybeRecordType =
4950         Stream.readRecord(Entry.ID, Record, &Blob);
4951     if (!MaybeRecordType) {
4952       // FIXME this drops the error.
4953       return Failure;
4954     }
4955     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4956     case SIGNATURE:
4957       if (F) {
4958         F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
4959         assert(F->Signature != ASTFileSignature::createDummy() &&
4960                "Dummy AST file signature not backpatched in ASTWriter.");
4961       }
4962       break;
4963     case AST_BLOCK_HASH:
4964       if (F) {
4965         F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());
4966         assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
4967                "Dummy AST block hash not backpatched in ASTWriter.");
4968       }
4969       break;
4970     case DIAGNOSTIC_OPTIONS: {
4971       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4972       if (Listener && ValidateDiagnosticOptions &&
4973           !AllowCompatibleConfigurationMismatch &&
4974           ParseDiagnosticOptions(Record, Complain, *Listener))
4975         Result = OutOfDate; // Don't return early.  Read the signature.
4976       break;
4977     }
4978     case HEADER_SEARCH_PATHS: {
4979       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
4980       if (!AllowCompatibleConfigurationMismatch &&
4981           ParseHeaderSearchPaths(Record, Complain, *Listener))
4982         Result = ConfigurationMismatch;
4983       break;
4984     }
4985     case DIAG_PRAGMA_MAPPINGS:
4986       if (!F)
4987         break;
4988       if (F->PragmaDiagMappings.empty())
4989         F->PragmaDiagMappings.swap(Record);
4990       else
4991         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4992                                      Record.begin(), Record.end());
4993       break;
4994     case HEADER_SEARCH_ENTRY_USAGE:
4995       if (!F)
4996         break;
4997       unsigned Count = Record[0];
4998       const char *Byte = Blob.data();
4999       F->SearchPathUsage = llvm::BitVector(Count, false);
5000       for (unsigned I = 0; I < Count; ++Byte)
5001         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
5002           if (*Byte & (1 << Bit))
5003             F->SearchPathUsage[I] = true;
5004       break;
5005     }
5006   }
5007 }
5008 
5009 /// Parse a record and blob containing module file extension metadata.
parseModuleFileExtensionMetadata(const SmallVectorImpl<uint64_t> & Record,StringRef Blob,ModuleFileExtensionMetadata & Metadata)5010 static bool parseModuleFileExtensionMetadata(
5011               const SmallVectorImpl<uint64_t> &Record,
5012               StringRef Blob,
5013               ModuleFileExtensionMetadata &Metadata) {
5014   if (Record.size() < 4) return true;
5015 
5016   Metadata.MajorVersion = Record[0];
5017   Metadata.MinorVersion = Record[1];
5018 
5019   unsigned BlockNameLen = Record[2];
5020   unsigned UserInfoLen = Record[3];
5021 
5022   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5023 
5024   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5025   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5026                                   Blob.data() + BlockNameLen + UserInfoLen);
5027   return false;
5028 }
5029 
ReadExtensionBlock(ModuleFile & F)5030 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5031   BitstreamCursor &Stream = F.Stream;
5032 
5033   RecordData Record;
5034   while (true) {
5035     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5036     if (!MaybeEntry)
5037       return MaybeEntry.takeError();
5038     llvm::BitstreamEntry Entry = MaybeEntry.get();
5039 
5040     switch (Entry.Kind) {
5041     case llvm::BitstreamEntry::SubBlock:
5042       if (llvm::Error Err = Stream.SkipBlock())
5043         return Err;
5044       continue;
5045     case llvm::BitstreamEntry::EndBlock:
5046       return llvm::Error::success();
5047     case llvm::BitstreamEntry::Error:
5048       return llvm::createStringError(std::errc::illegal_byte_sequence,
5049                                      "malformed block record in AST file");
5050     case llvm::BitstreamEntry::Record:
5051       break;
5052     }
5053 
5054     Record.clear();
5055     StringRef Blob;
5056     Expected<unsigned> MaybeRecCode =
5057         Stream.readRecord(Entry.ID, Record, &Blob);
5058     if (!MaybeRecCode)
5059       return MaybeRecCode.takeError();
5060     switch (MaybeRecCode.get()) {
5061     case EXTENSION_METADATA: {
5062       ModuleFileExtensionMetadata Metadata;
5063       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5064         return llvm::createStringError(
5065             std::errc::illegal_byte_sequence,
5066             "malformed EXTENSION_METADATA in AST file");
5067 
5068       // Find a module file extension with this block name.
5069       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
5070       if (Known == ModuleFileExtensions.end()) break;
5071 
5072       // Form a reader.
5073       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
5074                                                              F, Stream)) {
5075         F.ExtensionReaders.push_back(std::move(Reader));
5076       }
5077 
5078       break;
5079     }
5080     }
5081   }
5082 
5083   return llvm::Error::success();
5084 }
5085 
InitializeContext()5086 void ASTReader::InitializeContext() {
5087   assert(ContextObj && "no context to initialize");
5088   ASTContext &Context = *ContextObj;
5089 
5090   // If there's a listener, notify them that we "read" the translation unit.
5091   if (DeserializationListener)
5092     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
5093                                       Context.getTranslationUnitDecl());
5094 
5095   // FIXME: Find a better way to deal with collisions between these
5096   // built-in types. Right now, we just ignore the problem.
5097 
5098   // Load the special types.
5099   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5100     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5101       if (!Context.CFConstantStringTypeDecl)
5102         Context.setCFConstantStringType(GetType(String));
5103     }
5104 
5105     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5106       QualType FileType = GetType(File);
5107       if (FileType.isNull()) {
5108         Error("FILE type is NULL");
5109         return;
5110       }
5111 
5112       if (!Context.FILEDecl) {
5113         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5114           Context.setFILEDecl(Typedef->getDecl());
5115         else {
5116           const TagType *Tag = FileType->getAs<TagType>();
5117           if (!Tag) {
5118             Error("Invalid FILE type in AST file");
5119             return;
5120           }
5121           Context.setFILEDecl(Tag->getDecl());
5122         }
5123       }
5124     }
5125 
5126     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5127       QualType Jmp_bufType = GetType(Jmp_buf);
5128       if (Jmp_bufType.isNull()) {
5129         Error("jmp_buf type is NULL");
5130         return;
5131       }
5132 
5133       if (!Context.jmp_bufDecl) {
5134         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5135           Context.setjmp_bufDecl(Typedef->getDecl());
5136         else {
5137           const TagType *Tag = Jmp_bufType->getAs<TagType>();
5138           if (!Tag) {
5139             Error("Invalid jmp_buf type in AST file");
5140             return;
5141           }
5142           Context.setjmp_bufDecl(Tag->getDecl());
5143         }
5144       }
5145     }
5146 
5147     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5148       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
5149       if (Sigjmp_bufType.isNull()) {
5150         Error("sigjmp_buf type is NULL");
5151         return;
5152       }
5153 
5154       if (!Context.sigjmp_bufDecl) {
5155         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5156           Context.setsigjmp_bufDecl(Typedef->getDecl());
5157         else {
5158           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5159           assert(Tag && "Invalid sigjmp_buf type in AST file");
5160           Context.setsigjmp_bufDecl(Tag->getDecl());
5161         }
5162       }
5163     }
5164 
5165     if (unsigned ObjCIdRedef
5166           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5167       if (Context.ObjCIdRedefinitionType.isNull())
5168         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
5169     }
5170 
5171     if (unsigned ObjCClassRedef
5172           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5173       if (Context.ObjCClassRedefinitionType.isNull())
5174         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
5175     }
5176 
5177     if (unsigned ObjCSelRedef
5178           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5179       if (Context.ObjCSelRedefinitionType.isNull())
5180         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
5181     }
5182 
5183     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5184       QualType Ucontext_tType = GetType(Ucontext_t);
5185       if (Ucontext_tType.isNull()) {
5186         Error("ucontext_t type is NULL");
5187         return;
5188       }
5189 
5190       if (!Context.ucontext_tDecl) {
5191         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5192           Context.setucontext_tDecl(Typedef->getDecl());
5193         else {
5194           const TagType *Tag = Ucontext_tType->getAs<TagType>();
5195           assert(Tag && "Invalid ucontext_t type in AST file");
5196           Context.setucontext_tDecl(Tag->getDecl());
5197         }
5198       }
5199     }
5200   }
5201 
5202   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5203 
5204   // If there were any CUDA special declarations, deserialize them.
5205   if (!CUDASpecialDeclRefs.empty()) {
5206     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5207     Context.setcudaConfigureCallDecl(
5208                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5209   }
5210 
5211   // Re-export any modules that were imported by a non-module AST file.
5212   // FIXME: This does not make macro-only imports visible again.
5213   for (auto &Import : PendingImportedModules) {
5214     if (Module *Imported = getSubmodule(Import.ID)) {
5215       makeModuleVisible(Imported, Module::AllVisible,
5216                         /*ImportLoc=*/Import.ImportLoc);
5217       if (Import.ImportLoc.isValid())
5218         PP.makeModuleVisible(Imported, Import.ImportLoc);
5219       // This updates visibility for Preprocessor only. For Sema, which can be
5220       // nullptr here, we do the same later, in UpdateSema().
5221     }
5222   }
5223 
5224   // Hand off these modules to Sema.
5225   PendingImportedModulesSema.append(PendingImportedModules);
5226   PendingImportedModules.clear();
5227 }
5228 
finalizeForWriting()5229 void ASTReader::finalizeForWriting() {
5230   // Nothing to do for now.
5231 }
5232 
5233 /// Reads and return the signature record from \p PCH's control block, or
5234 /// else returns 0.
readASTFileSignature(StringRef PCH)5235 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5236   BitstreamCursor Stream(PCH);
5237   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5238     // FIXME this drops the error on the floor.
5239     consumeError(std::move(Err));
5240     return ASTFileSignature();
5241   }
5242 
5243   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5244   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5245     return ASTFileSignature();
5246 
5247   // Scan for SIGNATURE inside the diagnostic options block.
5248   ASTReader::RecordData Record;
5249   while (true) {
5250     Expected<llvm::BitstreamEntry> MaybeEntry =
5251         Stream.advanceSkippingSubblocks();
5252     if (!MaybeEntry) {
5253       // FIXME this drops the error on the floor.
5254       consumeError(MaybeEntry.takeError());
5255       return ASTFileSignature();
5256     }
5257     llvm::BitstreamEntry Entry = MaybeEntry.get();
5258 
5259     if (Entry.Kind != llvm::BitstreamEntry::Record)
5260       return ASTFileSignature();
5261 
5262     Record.clear();
5263     StringRef Blob;
5264     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5265     if (!MaybeRecord) {
5266       // FIXME this drops the error on the floor.
5267       consumeError(MaybeRecord.takeError());
5268       return ASTFileSignature();
5269     }
5270     if (SIGNATURE == MaybeRecord.get()) {
5271       auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5272       assert(Signature != ASTFileSignature::createDummy() &&
5273              "Dummy AST file signature not backpatched in ASTWriter.");
5274       return Signature;
5275     }
5276   }
5277 }
5278 
5279 /// Retrieve the name of the original source file name
5280 /// directly from the AST file, without actually loading the AST
5281 /// file.
getOriginalSourceFile(const std::string & ASTFileName,FileManager & FileMgr,const PCHContainerReader & PCHContainerRdr,DiagnosticsEngine & Diags)5282 std::string ASTReader::getOriginalSourceFile(
5283     const std::string &ASTFileName, FileManager &FileMgr,
5284     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5285   // Open the AST file.
5286   auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5287                                          /*RequiresNullTerminator=*/false);
5288   if (!Buffer) {
5289     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5290         << ASTFileName << Buffer.getError().message();
5291     return std::string();
5292   }
5293 
5294   // Initialize the stream
5295   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5296 
5297   // Sniff for the signature.
5298   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5299     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5300     return std::string();
5301   }
5302 
5303   // Scan for the CONTROL_BLOCK_ID block.
5304   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5305     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5306     return std::string();
5307   }
5308 
5309   // Scan for ORIGINAL_FILE inside the control block.
5310   RecordData Record;
5311   while (true) {
5312     Expected<llvm::BitstreamEntry> MaybeEntry =
5313         Stream.advanceSkippingSubblocks();
5314     if (!MaybeEntry) {
5315       // FIXME this drops errors on the floor.
5316       consumeError(MaybeEntry.takeError());
5317       return std::string();
5318     }
5319     llvm::BitstreamEntry Entry = MaybeEntry.get();
5320 
5321     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5322       return std::string();
5323 
5324     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5325       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5326       return std::string();
5327     }
5328 
5329     Record.clear();
5330     StringRef Blob;
5331     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5332     if (!MaybeRecord) {
5333       // FIXME this drops the errors on the floor.
5334       consumeError(MaybeRecord.takeError());
5335       return std::string();
5336     }
5337     if (ORIGINAL_FILE == MaybeRecord.get())
5338       return Blob.str();
5339   }
5340 }
5341 
5342 namespace {
5343 
5344   class SimplePCHValidator : public ASTReaderListener {
5345     const LangOptions &ExistingLangOpts;
5346     const TargetOptions &ExistingTargetOpts;
5347     const PreprocessorOptions &ExistingPPOpts;
5348     std::string ExistingModuleCachePath;
5349     FileManager &FileMgr;
5350     bool StrictOptionMatches;
5351 
5352   public:
SimplePCHValidator(const LangOptions & ExistingLangOpts,const TargetOptions & ExistingTargetOpts,const PreprocessorOptions & ExistingPPOpts,StringRef ExistingModuleCachePath,FileManager & FileMgr,bool StrictOptionMatches)5353     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5354                        const TargetOptions &ExistingTargetOpts,
5355                        const PreprocessorOptions &ExistingPPOpts,
5356                        StringRef ExistingModuleCachePath, FileManager &FileMgr,
5357                        bool StrictOptionMatches)
5358         : ExistingLangOpts(ExistingLangOpts),
5359           ExistingTargetOpts(ExistingTargetOpts),
5360           ExistingPPOpts(ExistingPPOpts),
5361           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5362           StrictOptionMatches(StrictOptionMatches) {}
5363 
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)5364     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5365                              bool AllowCompatibleDifferences) override {
5366       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5367                                   AllowCompatibleDifferences);
5368     }
5369 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain,bool AllowCompatibleDifferences)5370     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5371                            bool AllowCompatibleDifferences) override {
5372       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5373                                 AllowCompatibleDifferences);
5374     }
5375 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,StringRef SpecificModuleCachePath,bool Complain)5376     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5377                                  StringRef SpecificModuleCachePath,
5378                                  bool Complain) override {
5379       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5380                                       ExistingModuleCachePath, nullptr,
5381                                       ExistingLangOpts, ExistingPPOpts);
5382     }
5383 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool ReadMacros,bool Complain,std::string & SuggestedPredefines)5384     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5385                                  bool ReadMacros, bool Complain,
5386                                  std::string &SuggestedPredefines) override {
5387       return checkPreprocessorOptions(
5388           PPOpts, ExistingPPOpts, ReadMacros, /*Diags=*/nullptr, FileMgr,
5389           SuggestedPredefines, ExistingLangOpts,
5390           StrictOptionMatches ? OptionValidateStrictMatches
5391                               : OptionValidateContradictions);
5392     }
5393   };
5394 
5395 } // namespace
5396 
readASTFileControlBlock(StringRef Filename,FileManager & FileMgr,const InMemoryModuleCache & ModuleCache,const PCHContainerReader & PCHContainerRdr,bool FindModuleFileExtensions,ASTReaderListener & Listener,bool ValidateDiagnosticOptions)5397 bool ASTReader::readASTFileControlBlock(
5398     StringRef Filename, FileManager &FileMgr,
5399     const InMemoryModuleCache &ModuleCache,
5400     const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5401     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5402   // Open the AST file.
5403   std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5404   llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
5405   if (!Buffer) {
5406     // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5407     // read again later, but we do not have the context here to determine if it
5408     // is safe to change the result of InMemoryModuleCache::getPCMState().
5409 
5410     // FIXME: This allows use of the VFS; we do not allow use of the
5411     // VFS when actually loading a module.
5412     auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5413     if (!BufferOrErr)
5414       return true;
5415     OwnedBuffer = std::move(*BufferOrErr);
5416     Buffer = OwnedBuffer.get();
5417   }
5418 
5419   // Initialize the stream
5420   StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
5421   BitstreamCursor Stream(Bytes);
5422 
5423   // Sniff for the signature.
5424   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5425     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5426     return true;
5427   }
5428 
5429   // Scan for the CONTROL_BLOCK_ID block.
5430   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5431     return true;
5432 
5433   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5434   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5435   bool NeedsImports = Listener.needsImportVisitation();
5436   BitstreamCursor InputFilesCursor;
5437   uint64_t InputFilesOffsetBase = 0;
5438 
5439   RecordData Record;
5440   std::string ModuleDir;
5441   bool DoneWithControlBlock = false;
5442   while (!DoneWithControlBlock) {
5443     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5444     if (!MaybeEntry) {
5445       // FIXME this drops the error on the floor.
5446       consumeError(MaybeEntry.takeError());
5447       return true;
5448     }
5449     llvm::BitstreamEntry Entry = MaybeEntry.get();
5450 
5451     switch (Entry.Kind) {
5452     case llvm::BitstreamEntry::SubBlock: {
5453       switch (Entry.ID) {
5454       case OPTIONS_BLOCK_ID: {
5455         std::string IgnoredSuggestedPredefines;
5456         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5457                              /*AllowCompatibleConfigurationMismatch*/ false,
5458                              Listener, IgnoredSuggestedPredefines) != Success)
5459           return true;
5460         break;
5461       }
5462 
5463       case INPUT_FILES_BLOCK_ID:
5464         InputFilesCursor = Stream;
5465         if (llvm::Error Err = Stream.SkipBlock()) {
5466           // FIXME this drops the error on the floor.
5467           consumeError(std::move(Err));
5468           return true;
5469         }
5470         if (NeedsInputFiles &&
5471             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5472           return true;
5473         InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5474         break;
5475 
5476       default:
5477         if (llvm::Error Err = Stream.SkipBlock()) {
5478           // FIXME this drops the error on the floor.
5479           consumeError(std::move(Err));
5480           return true;
5481         }
5482         break;
5483       }
5484 
5485       continue;
5486     }
5487 
5488     case llvm::BitstreamEntry::EndBlock:
5489       DoneWithControlBlock = true;
5490       break;
5491 
5492     case llvm::BitstreamEntry::Error:
5493       return true;
5494 
5495     case llvm::BitstreamEntry::Record:
5496       break;
5497     }
5498 
5499     if (DoneWithControlBlock) break;
5500 
5501     Record.clear();
5502     StringRef Blob;
5503     Expected<unsigned> MaybeRecCode =
5504         Stream.readRecord(Entry.ID, Record, &Blob);
5505     if (!MaybeRecCode) {
5506       // FIXME this drops the error.
5507       return Failure;
5508     }
5509     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5510     case METADATA:
5511       if (Record[0] != VERSION_MAJOR)
5512         return true;
5513       if (Listener.ReadFullVersionInformation(Blob))
5514         return true;
5515       break;
5516     case MODULE_NAME:
5517       Listener.ReadModuleName(Blob);
5518       break;
5519     case MODULE_DIRECTORY:
5520       ModuleDir = std::string(Blob);
5521       break;
5522     case MODULE_MAP_FILE: {
5523       unsigned Idx = 0;
5524       auto Path = ReadString(Record, Idx);
5525       ResolveImportedPath(Path, ModuleDir);
5526       Listener.ReadModuleMapFile(Path);
5527       break;
5528     }
5529     case INPUT_FILE_OFFSETS: {
5530       if (!NeedsInputFiles)
5531         break;
5532 
5533       unsigned NumInputFiles = Record[0];
5534       unsigned NumUserFiles = Record[1];
5535       const llvm::support::unaligned_uint64_t *InputFileOffs =
5536           (const llvm::support::unaligned_uint64_t *)Blob.data();
5537       for (unsigned I = 0; I != NumInputFiles; ++I) {
5538         // Go find this input file.
5539         bool isSystemFile = I >= NumUserFiles;
5540 
5541         if (isSystemFile && !NeedsSystemInputFiles)
5542           break; // the rest are system input files
5543 
5544         BitstreamCursor &Cursor = InputFilesCursor;
5545         SavedStreamPosition SavedPosition(Cursor);
5546         if (llvm::Error Err =
5547                 Cursor.JumpToBit(InputFilesOffsetBase + InputFileOffs[I])) {
5548           // FIXME this drops errors on the floor.
5549           consumeError(std::move(Err));
5550         }
5551 
5552         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5553         if (!MaybeCode) {
5554           // FIXME this drops errors on the floor.
5555           consumeError(MaybeCode.takeError());
5556         }
5557         unsigned Code = MaybeCode.get();
5558 
5559         RecordData Record;
5560         StringRef Blob;
5561         bool shouldContinue = false;
5562         Expected<unsigned> MaybeRecordType =
5563             Cursor.readRecord(Code, Record, &Blob);
5564         if (!MaybeRecordType) {
5565           // FIXME this drops errors on the floor.
5566           consumeError(MaybeRecordType.takeError());
5567         }
5568         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5569         case INPUT_FILE_HASH:
5570           break;
5571         case INPUT_FILE:
5572           bool Overridden = static_cast<bool>(Record[3]);
5573           std::string Filename = std::string(Blob);
5574           ResolveImportedPath(Filename, ModuleDir);
5575           shouldContinue = Listener.visitInputFile(
5576               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5577           break;
5578         }
5579         if (!shouldContinue)
5580           break;
5581       }
5582       break;
5583     }
5584 
5585     case IMPORTS: {
5586       if (!NeedsImports)
5587         break;
5588 
5589       unsigned Idx = 0, N = Record.size();
5590       while (Idx < N) {
5591         // Read information about the AST file.
5592 
5593         // Skip Kind
5594         Idx++;
5595         bool IsStandardCXXModule = Record[Idx++];
5596 
5597         // Skip ImportLoc
5598         Idx++;
5599 
5600         // In C++20 Modules, we don't record the path to imported
5601         // modules in the BMI files.
5602         if (IsStandardCXXModule) {
5603           std::string ModuleName = ReadString(Record, Idx);
5604           Listener.visitImport(ModuleName, /*Filename=*/"");
5605           continue;
5606         }
5607 
5608         // Skip Size, ModTime and Signature
5609         Idx += 1 + 1 + ASTFileSignature::size;
5610         std::string ModuleName = ReadString(Record, Idx);
5611         std::string Filename = ReadString(Record, Idx);
5612         ResolveImportedPath(Filename, ModuleDir);
5613         Listener.visitImport(ModuleName, Filename);
5614       }
5615       break;
5616     }
5617 
5618     default:
5619       // No other validation to perform.
5620       break;
5621     }
5622   }
5623 
5624   // Look for module file extension blocks, if requested.
5625   if (FindModuleFileExtensions) {
5626     BitstreamCursor SavedStream = Stream;
5627     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5628       bool DoneWithExtensionBlock = false;
5629       while (!DoneWithExtensionBlock) {
5630         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5631         if (!MaybeEntry) {
5632           // FIXME this drops the error.
5633           return true;
5634         }
5635         llvm::BitstreamEntry Entry = MaybeEntry.get();
5636 
5637         switch (Entry.Kind) {
5638         case llvm::BitstreamEntry::SubBlock:
5639           if (llvm::Error Err = Stream.SkipBlock()) {
5640             // FIXME this drops the error on the floor.
5641             consumeError(std::move(Err));
5642             return true;
5643           }
5644           continue;
5645 
5646         case llvm::BitstreamEntry::EndBlock:
5647           DoneWithExtensionBlock = true;
5648           continue;
5649 
5650         case llvm::BitstreamEntry::Error:
5651           return true;
5652 
5653         case llvm::BitstreamEntry::Record:
5654           break;
5655         }
5656 
5657        Record.clear();
5658        StringRef Blob;
5659        Expected<unsigned> MaybeRecCode =
5660            Stream.readRecord(Entry.ID, Record, &Blob);
5661        if (!MaybeRecCode) {
5662          // FIXME this drops the error.
5663          return true;
5664        }
5665        switch (MaybeRecCode.get()) {
5666        case EXTENSION_METADATA: {
5667          ModuleFileExtensionMetadata Metadata;
5668          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5669            return true;
5670 
5671          Listener.readModuleFileExtension(Metadata);
5672          break;
5673        }
5674        }
5675       }
5676     }
5677     Stream = SavedStream;
5678   }
5679 
5680   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5681   if (readUnhashedControlBlockImpl(
5682           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5683           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5684           ValidateDiagnosticOptions) != Success)
5685     return true;
5686 
5687   return false;
5688 }
5689 
isAcceptableASTFile(StringRef Filename,FileManager & FileMgr,const InMemoryModuleCache & ModuleCache,const PCHContainerReader & PCHContainerRdr,const LangOptions & LangOpts,const TargetOptions & TargetOpts,const PreprocessorOptions & PPOpts,StringRef ExistingModuleCachePath,bool RequireStrictOptionMatches)5690 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5691                                     const InMemoryModuleCache &ModuleCache,
5692                                     const PCHContainerReader &PCHContainerRdr,
5693                                     const LangOptions &LangOpts,
5694                                     const TargetOptions &TargetOpts,
5695                                     const PreprocessorOptions &PPOpts,
5696                                     StringRef ExistingModuleCachePath,
5697                                     bool RequireStrictOptionMatches) {
5698   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5699                                ExistingModuleCachePath, FileMgr,
5700                                RequireStrictOptionMatches);
5701   return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5702                                   PCHContainerRdr,
5703                                   /*FindModuleFileExtensions=*/false, validator,
5704                                   /*ValidateDiagnosticOptions=*/true);
5705 }
5706 
ReadSubmoduleBlock(ModuleFile & F,unsigned ClientLoadCapabilities)5707 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5708                                           unsigned ClientLoadCapabilities) {
5709   // Enter the submodule block.
5710   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5711     return Err;
5712 
5713   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5714   bool First = true;
5715   Module *CurrentModule = nullptr;
5716   RecordData Record;
5717   while (true) {
5718     Expected<llvm::BitstreamEntry> MaybeEntry =
5719         F.Stream.advanceSkippingSubblocks();
5720     if (!MaybeEntry)
5721       return MaybeEntry.takeError();
5722     llvm::BitstreamEntry Entry = MaybeEntry.get();
5723 
5724     switch (Entry.Kind) {
5725     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5726     case llvm::BitstreamEntry::Error:
5727       return llvm::createStringError(std::errc::illegal_byte_sequence,
5728                                      "malformed block record in AST file");
5729     case llvm::BitstreamEntry::EndBlock:
5730       return llvm::Error::success();
5731     case llvm::BitstreamEntry::Record:
5732       // The interesting case.
5733       break;
5734     }
5735 
5736     // Read a record.
5737     StringRef Blob;
5738     Record.clear();
5739     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5740     if (!MaybeKind)
5741       return MaybeKind.takeError();
5742     unsigned Kind = MaybeKind.get();
5743 
5744     if ((Kind == SUBMODULE_METADATA) != First)
5745       return llvm::createStringError(
5746           std::errc::illegal_byte_sequence,
5747           "submodule metadata record should be at beginning of block");
5748     First = false;
5749 
5750     // Submodule information is only valid if we have a current module.
5751     // FIXME: Should we error on these cases?
5752     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5753         Kind != SUBMODULE_DEFINITION)
5754       continue;
5755 
5756     switch (Kind) {
5757     default:  // Default behavior: ignore.
5758       break;
5759 
5760     case SUBMODULE_DEFINITION: {
5761       if (Record.size() < 13)
5762         return llvm::createStringError(std::errc::illegal_byte_sequence,
5763                                        "malformed module definition");
5764 
5765       StringRef Name = Blob;
5766       unsigned Idx = 0;
5767       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5768       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5769       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5770       SourceLocation DefinitionLoc = ReadSourceLocation(F, Record[Idx++]);
5771       bool IsFramework = Record[Idx++];
5772       bool IsExplicit = Record[Idx++];
5773       bool IsSystem = Record[Idx++];
5774       bool IsExternC = Record[Idx++];
5775       bool InferSubmodules = Record[Idx++];
5776       bool InferExplicitSubmodules = Record[Idx++];
5777       bool InferExportWildcard = Record[Idx++];
5778       bool ConfigMacrosExhaustive = Record[Idx++];
5779       bool ModuleMapIsPrivate = Record[Idx++];
5780       bool NamedModuleHasInit = Record[Idx++];
5781 
5782       Module *ParentModule = nullptr;
5783       if (Parent)
5784         ParentModule = getSubmodule(Parent);
5785 
5786       // Retrieve this (sub)module from the module map, creating it if
5787       // necessary.
5788       CurrentModule =
5789           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5790               .first;
5791 
5792       // FIXME: Call ModMap.setInferredModuleAllowedBy()
5793 
5794       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5795       if (GlobalIndex >= SubmodulesLoaded.size() ||
5796           SubmodulesLoaded[GlobalIndex])
5797         return llvm::createStringError(std::errc::invalid_argument,
5798                                        "too many submodules");
5799 
5800       if (!ParentModule) {
5801         if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
5802           // Don't emit module relocation error if we have -fno-validate-pch
5803           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5804                     DisableValidationForModuleKind::Module) &&
5805               CurFile != F.File) {
5806             auto ConflictError =
5807                 PartialDiagnostic(diag::err_module_file_conflict,
5808                                   ContextObj->DiagAllocator)
5809                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5810                 << F.File.getName();
5811             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5812           }
5813         }
5814 
5815         F.DidReadTopLevelSubmodule = true;
5816         CurrentModule->setASTFile(F.File);
5817         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5818       }
5819 
5820       CurrentModule->Kind = Kind;
5821       CurrentModule->DefinitionLoc = DefinitionLoc;
5822       CurrentModule->Signature = F.Signature;
5823       CurrentModule->IsFromModuleFile = true;
5824       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5825       CurrentModule->IsExternC = IsExternC;
5826       CurrentModule->InferSubmodules = InferSubmodules;
5827       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5828       CurrentModule->InferExportWildcard = InferExportWildcard;
5829       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5830       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5831       CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
5832       if (DeserializationListener)
5833         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5834 
5835       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5836 
5837       // Clear out data that will be replaced by what is in the module file.
5838       CurrentModule->LinkLibraries.clear();
5839       CurrentModule->ConfigMacros.clear();
5840       CurrentModule->UnresolvedConflicts.clear();
5841       CurrentModule->Conflicts.clear();
5842 
5843       // The module is available unless it's missing a requirement; relevant
5844       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5845       // Missing headers that were present when the module was built do not
5846       // make it unavailable -- if we got this far, this must be an explicitly
5847       // imported module file.
5848       CurrentModule->Requirements.clear();
5849       CurrentModule->MissingHeaders.clear();
5850       CurrentModule->IsUnimportable =
5851           ParentModule && ParentModule->IsUnimportable;
5852       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5853       break;
5854     }
5855 
5856     case SUBMODULE_UMBRELLA_HEADER: {
5857       // FIXME: This doesn't work for framework modules as `Filename` is the
5858       //        name as written in the module file and does not include
5859       //        `Headers/`, so this path will never exist.
5860       std::string Filename = std::string(Blob);
5861       ResolveImportedPath(F, Filename);
5862       if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) {
5863         if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
5864           // FIXME: NameAsWritten
5865           ModMap.setUmbrellaHeaderAsWritten(CurrentModule, *Umbrella, Blob, "");
5866         }
5867         // Note that it's too late at this point to return out of date if the
5868         // name from the PCM doesn't match up with the one in the module map,
5869         // but also quite unlikely since we will have already checked the
5870         // modification time and size of the module map file itself.
5871       }
5872       break;
5873     }
5874 
5875     case SUBMODULE_HEADER:
5876     case SUBMODULE_EXCLUDED_HEADER:
5877     case SUBMODULE_PRIVATE_HEADER:
5878       // We lazily associate headers with their modules via the HeaderInfo table.
5879       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5880       // of complete filenames or remove it entirely.
5881       break;
5882 
5883     case SUBMODULE_TEXTUAL_HEADER:
5884     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5885       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5886       // them here.
5887       break;
5888 
5889     case SUBMODULE_TOPHEADER: {
5890       std::string HeaderName(Blob);
5891       ResolveImportedPath(F, HeaderName);
5892       CurrentModule->addTopHeaderFilename(HeaderName);
5893       break;
5894     }
5895 
5896     case SUBMODULE_UMBRELLA_DIR: {
5897       // See comments in SUBMODULE_UMBRELLA_HEADER
5898       std::string Dirname = std::string(Blob);
5899       ResolveImportedPath(F, Dirname);
5900       if (auto Umbrella =
5901               PP.getFileManager().getOptionalDirectoryRef(Dirname)) {
5902         if (!CurrentModule->getUmbrellaDirAsWritten()) {
5903           // FIXME: NameAsWritten
5904           ModMap.setUmbrellaDirAsWritten(CurrentModule, *Umbrella, Blob, "");
5905         }
5906       }
5907       break;
5908     }
5909 
5910     case SUBMODULE_METADATA: {
5911       F.BaseSubmoduleID = getTotalNumSubmodules();
5912       F.LocalNumSubmodules = Record[0];
5913       unsigned LocalBaseSubmoduleID = Record[1];
5914       if (F.LocalNumSubmodules > 0) {
5915         // Introduce the global -> local mapping for submodules within this
5916         // module.
5917         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5918 
5919         // Introduce the local -> global mapping for submodules within this
5920         // module.
5921         F.SubmoduleRemap.insertOrReplace(
5922           std::make_pair(LocalBaseSubmoduleID,
5923                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5924 
5925         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5926       }
5927       break;
5928     }
5929 
5930     case SUBMODULE_IMPORTS:
5931       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5932         UnresolvedModuleRef Unresolved;
5933         Unresolved.File = &F;
5934         Unresolved.Mod = CurrentModule;
5935         Unresolved.ID = Record[Idx];
5936         Unresolved.Kind = UnresolvedModuleRef::Import;
5937         Unresolved.IsWildcard = false;
5938         UnresolvedModuleRefs.push_back(Unresolved);
5939       }
5940       break;
5941 
5942     case SUBMODULE_AFFECTING_MODULES:
5943       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5944         UnresolvedModuleRef Unresolved;
5945         Unresolved.File = &F;
5946         Unresolved.Mod = CurrentModule;
5947         Unresolved.ID = Record[Idx];
5948         Unresolved.Kind = UnresolvedModuleRef::Affecting;
5949         Unresolved.IsWildcard = false;
5950         UnresolvedModuleRefs.push_back(Unresolved);
5951       }
5952       break;
5953 
5954     case SUBMODULE_EXPORTS:
5955       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5956         UnresolvedModuleRef Unresolved;
5957         Unresolved.File = &F;
5958         Unresolved.Mod = CurrentModule;
5959         Unresolved.ID = Record[Idx];
5960         Unresolved.Kind = UnresolvedModuleRef::Export;
5961         Unresolved.IsWildcard = Record[Idx + 1];
5962         UnresolvedModuleRefs.push_back(Unresolved);
5963       }
5964 
5965       // Once we've loaded the set of exports, there's no reason to keep
5966       // the parsed, unresolved exports around.
5967       CurrentModule->UnresolvedExports.clear();
5968       break;
5969 
5970     case SUBMODULE_REQUIRES:
5971       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5972                                     PP.getTargetInfo());
5973       break;
5974 
5975     case SUBMODULE_LINK_LIBRARY:
5976       ModMap.resolveLinkAsDependencies(CurrentModule);
5977       CurrentModule->LinkLibraries.push_back(
5978           Module::LinkLibrary(std::string(Blob), Record[0]));
5979       break;
5980 
5981     case SUBMODULE_CONFIG_MACRO:
5982       CurrentModule->ConfigMacros.push_back(Blob.str());
5983       break;
5984 
5985     case SUBMODULE_CONFLICT: {
5986       UnresolvedModuleRef Unresolved;
5987       Unresolved.File = &F;
5988       Unresolved.Mod = CurrentModule;
5989       Unresolved.ID = Record[0];
5990       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5991       Unresolved.IsWildcard = false;
5992       Unresolved.String = Blob;
5993       UnresolvedModuleRefs.push_back(Unresolved);
5994       break;
5995     }
5996 
5997     case SUBMODULE_INITIALIZERS: {
5998       if (!ContextObj)
5999         break;
6000       SmallVector<uint32_t, 16> Inits;
6001       for (auto &ID : Record)
6002         Inits.push_back(getGlobalDeclID(F, ID));
6003       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
6004       break;
6005     }
6006 
6007     case SUBMODULE_EXPORT_AS:
6008       CurrentModule->ExportAsModule = Blob.str();
6009       ModMap.addLinkAsDependency(CurrentModule);
6010       break;
6011     }
6012   }
6013 }
6014 
6015 /// Parse the record that corresponds to a LangOptions data
6016 /// structure.
6017 ///
6018 /// This routine parses the language options from the AST file and then gives
6019 /// them to the AST listener if one is set.
6020 ///
6021 /// \returns true if the listener deems the file unacceptable, false otherwise.
ParseLanguageOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener,bool AllowCompatibleDifferences)6022 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6023                                      bool Complain,
6024                                      ASTReaderListener &Listener,
6025                                      bool AllowCompatibleDifferences) {
6026   LangOptions LangOpts;
6027   unsigned Idx = 0;
6028 #define LANGOPT(Name, Bits, Default, Description) \
6029   LangOpts.Name = Record[Idx++];
6030 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
6031   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6032 #include "clang/Basic/LangOptions.def"
6033 #define SANITIZER(NAME, ID)                                                    \
6034   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6035 #include "clang/Basic/Sanitizers.def"
6036 
6037   for (unsigned N = Record[Idx++]; N; --N)
6038     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
6039 
6040   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6041   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6042   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6043 
6044   LangOpts.CurrentModule = ReadString(Record, Idx);
6045 
6046   // Comment options.
6047   for (unsigned N = Record[Idx++]; N; --N) {
6048     LangOpts.CommentOpts.BlockCommandNames.push_back(
6049       ReadString(Record, Idx));
6050   }
6051   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6052 
6053   // OpenMP offloading options.
6054   for (unsigned N = Record[Idx++]; N; --N) {
6055     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
6056   }
6057 
6058   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6059 
6060   return Listener.ReadLanguageOptions(LangOpts, Complain,
6061                                       AllowCompatibleDifferences);
6062 }
6063 
ParseTargetOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener,bool AllowCompatibleDifferences)6064 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
6065                                    ASTReaderListener &Listener,
6066                                    bool AllowCompatibleDifferences) {
6067   unsigned Idx = 0;
6068   TargetOptions TargetOpts;
6069   TargetOpts.Triple = ReadString(Record, Idx);
6070   TargetOpts.CPU = ReadString(Record, Idx);
6071   TargetOpts.TuneCPU = ReadString(Record, Idx);
6072   TargetOpts.ABI = ReadString(Record, Idx);
6073   for (unsigned N = Record[Idx++]; N; --N) {
6074     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
6075   }
6076   for (unsigned N = Record[Idx++]; N; --N) {
6077     TargetOpts.Features.push_back(ReadString(Record, Idx));
6078   }
6079 
6080   return Listener.ReadTargetOptions(TargetOpts, Complain,
6081                                     AllowCompatibleDifferences);
6082 }
6083 
ParseDiagnosticOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)6084 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
6085                                        ASTReaderListener &Listener) {
6086   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
6087   unsigned Idx = 0;
6088 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
6089 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6090   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
6091 #include "clang/Basic/DiagnosticOptions.def"
6092 
6093   for (unsigned N = Record[Idx++]; N; --N)
6094     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
6095   for (unsigned N = Record[Idx++]; N; --N)
6096     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
6097 
6098   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
6099 }
6100 
ParseFileSystemOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)6101 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6102                                        ASTReaderListener &Listener) {
6103   FileSystemOptions FSOpts;
6104   unsigned Idx = 0;
6105   FSOpts.WorkingDir = ReadString(Record, Idx);
6106   return Listener.ReadFileSystemOptions(FSOpts, Complain);
6107 }
6108 
ParseHeaderSearchOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)6109 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6110                                          bool Complain,
6111                                          ASTReaderListener &Listener) {
6112   HeaderSearchOptions HSOpts;
6113   unsigned Idx = 0;
6114   HSOpts.Sysroot = ReadString(Record, Idx);
6115 
6116   HSOpts.ResourceDir = ReadString(Record, Idx);
6117   HSOpts.ModuleCachePath = ReadString(Record, Idx);
6118   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6119   HSOpts.DisableModuleHash = Record[Idx++];
6120   HSOpts.ImplicitModuleMaps = Record[Idx++];
6121   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6122   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6123   HSOpts.UseBuiltinIncludes = Record[Idx++];
6124   HSOpts.UseStandardSystemIncludes = Record[Idx++];
6125   HSOpts.UseStandardCXXIncludes = Record[Idx++];
6126   HSOpts.UseLibcxx = Record[Idx++];
6127   std::string SpecificModuleCachePath = ReadString(Record, Idx);
6128 
6129   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
6130                                           Complain);
6131 }
6132 
ParseHeaderSearchPaths(const RecordData & Record,bool Complain,ASTReaderListener & Listener)6133 bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6134                                        ASTReaderListener &Listener) {
6135   HeaderSearchOptions HSOpts;
6136   unsigned Idx = 0;
6137 
6138   // Include entries.
6139   for (unsigned N = Record[Idx++]; N; --N) {
6140     std::string Path = ReadString(Record, Idx);
6141     frontend::IncludeDirGroup Group
6142       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6143     bool IsFramework = Record[Idx++];
6144     bool IgnoreSysRoot = Record[Idx++];
6145     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
6146                                     IgnoreSysRoot);
6147   }
6148 
6149   // System header prefixes.
6150   for (unsigned N = Record[Idx++]; N; --N) {
6151     std::string Prefix = ReadString(Record, Idx);
6152     bool IsSystemHeader = Record[Idx++];
6153     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
6154   }
6155 
6156   // VFS overlay files.
6157   for (unsigned N = Record[Idx++]; N; --N) {
6158     std::string VFSOverlayFile = ReadString(Record, Idx);
6159     HSOpts.VFSOverlayFiles.emplace_back(std::move(VFSOverlayFile));
6160   }
6161 
6162   return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6163 }
6164 
ParsePreprocessorOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener,std::string & SuggestedPredefines)6165 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6166                                          bool Complain,
6167                                          ASTReaderListener &Listener,
6168                                          std::string &SuggestedPredefines) {
6169   PreprocessorOptions PPOpts;
6170   unsigned Idx = 0;
6171 
6172   // Macro definitions/undefs
6173   bool ReadMacros = Record[Idx++];
6174   if (ReadMacros) {
6175     for (unsigned N = Record[Idx++]; N; --N) {
6176       std::string Macro = ReadString(Record, Idx);
6177       bool IsUndef = Record[Idx++];
6178       PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
6179     }
6180   }
6181 
6182   // Includes
6183   for (unsigned N = Record[Idx++]; N; --N) {
6184     PPOpts.Includes.push_back(ReadString(Record, Idx));
6185   }
6186 
6187   // Macro Includes
6188   for (unsigned N = Record[Idx++]; N; --N) {
6189     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
6190   }
6191 
6192   PPOpts.UsePredefines = Record[Idx++];
6193   PPOpts.DetailedRecord = Record[Idx++];
6194   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6195   PPOpts.ObjCXXARCStandardLibrary =
6196     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6197   SuggestedPredefines.clear();
6198   return Listener.ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
6199                                           SuggestedPredefines);
6200 }
6201 
6202 std::pair<ModuleFile *, unsigned>
getModulePreprocessedEntity(unsigned GlobalIndex)6203 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6204   GlobalPreprocessedEntityMapType::iterator
6205   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
6206   assert(I != GlobalPreprocessedEntityMap.end() &&
6207          "Corrupted global preprocessed entity map");
6208   ModuleFile *M = I->second;
6209   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6210   return std::make_pair(M, LocalIndex);
6211 }
6212 
6213 llvm::iterator_range<PreprocessingRecord::iterator>
getModulePreprocessedEntities(ModuleFile & Mod) const6214 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6215   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6216     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
6217                                              Mod.NumPreprocessedEntities);
6218 
6219   return llvm::make_range(PreprocessingRecord::iterator(),
6220                           PreprocessingRecord::iterator());
6221 }
6222 
canRecoverFromOutOfDate(StringRef ModuleFileName,unsigned int ClientLoadCapabilities)6223 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6224                                         unsigned int ClientLoadCapabilities) {
6225   return ClientLoadCapabilities & ARR_OutOfDate &&
6226          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
6227 }
6228 
6229 llvm::iterator_range<ASTReader::ModuleDeclIterator>
getModuleFileLevelDecls(ModuleFile & Mod)6230 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6231   return llvm::make_range(
6232       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6233       ModuleDeclIterator(this, &Mod,
6234                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6235 }
6236 
ReadSkippedRange(unsigned GlobalIndex)6237 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6238   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
6239   assert(I != GlobalSkippedRangeMap.end() &&
6240     "Corrupted global skipped range map");
6241   ModuleFile *M = I->second;
6242   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6243   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6244   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6245   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
6246                     TranslateSourceLocation(*M, RawRange.getEnd()));
6247   assert(Range.isValid());
6248   return Range;
6249 }
6250 
ReadPreprocessedEntity(unsigned Index)6251 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6252   PreprocessedEntityID PPID = Index+1;
6253   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6254   ModuleFile &M = *PPInfo.first;
6255   unsigned LocalIndex = PPInfo.second;
6256   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6257 
6258   if (!PP.getPreprocessingRecord()) {
6259     Error("no preprocessing record");
6260     return nullptr;
6261   }
6262 
6263   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6264   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6265           M.MacroOffsetsBase + PPOffs.BitOffset)) {
6266     Error(std::move(Err));
6267     return nullptr;
6268   }
6269 
6270   Expected<llvm::BitstreamEntry> MaybeEntry =
6271       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
6272   if (!MaybeEntry) {
6273     Error(MaybeEntry.takeError());
6274     return nullptr;
6275   }
6276   llvm::BitstreamEntry Entry = MaybeEntry.get();
6277 
6278   if (Entry.Kind != llvm::BitstreamEntry::Record)
6279     return nullptr;
6280 
6281   // Read the record.
6282   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
6283                     TranslateSourceLocation(M, PPOffs.getEnd()));
6284   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6285   StringRef Blob;
6286   RecordData Record;
6287   Expected<unsigned> MaybeRecType =
6288       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
6289   if (!MaybeRecType) {
6290     Error(MaybeRecType.takeError());
6291     return nullptr;
6292   }
6293   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6294   case PPD_MACRO_EXPANSION: {
6295     bool isBuiltin = Record[0];
6296     IdentifierInfo *Name = nullptr;
6297     MacroDefinitionRecord *Def = nullptr;
6298     if (isBuiltin)
6299       Name = getLocalIdentifier(M, Record[1]);
6300     else {
6301       PreprocessedEntityID GlobalID =
6302           getGlobalPreprocessedEntityID(M, Record[1]);
6303       Def = cast<MacroDefinitionRecord>(
6304           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6305     }
6306 
6307     MacroExpansion *ME;
6308     if (isBuiltin)
6309       ME = new (PPRec) MacroExpansion(Name, Range);
6310     else
6311       ME = new (PPRec) MacroExpansion(Def, Range);
6312 
6313     return ME;
6314   }
6315 
6316   case PPD_MACRO_DEFINITION: {
6317     // Decode the identifier info and then check again; if the macro is
6318     // still defined and associated with the identifier,
6319     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6320     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6321 
6322     if (DeserializationListener)
6323       DeserializationListener->MacroDefinitionRead(PPID, MD);
6324 
6325     return MD;
6326   }
6327 
6328   case PPD_INCLUSION_DIRECTIVE: {
6329     const char *FullFileNameStart = Blob.data() + Record[0];
6330     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6331     OptionalFileEntryRef File;
6332     if (!FullFileName.empty())
6333       File = PP.getFileManager().getOptionalFileRef(FullFileName);
6334 
6335     // FIXME: Stable encoding
6336     InclusionDirective::InclusionKind Kind
6337       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6338     InclusionDirective *ID
6339       = new (PPRec) InclusionDirective(PPRec, Kind,
6340                                        StringRef(Blob.data(), Record[0]),
6341                                        Record[1], Record[3],
6342                                        File,
6343                                        Range);
6344     return ID;
6345   }
6346   }
6347 
6348   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6349 }
6350 
6351 /// Find the next module that contains entities and return the ID
6352 /// of the first entry.
6353 ///
6354 /// \param SLocMapI points at a chunk of a module that contains no
6355 /// preprocessed entities or the entities it contains are not the ones we are
6356 /// looking for.
findNextPreprocessedEntity(GlobalSLocOffsetMapType::const_iterator SLocMapI) const6357 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6358                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6359   ++SLocMapI;
6360   for (GlobalSLocOffsetMapType::const_iterator
6361          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6362     ModuleFile &M = *SLocMapI->second;
6363     if (M.NumPreprocessedEntities)
6364       return M.BasePreprocessedEntityID;
6365   }
6366 
6367   return getTotalNumPreprocessedEntities();
6368 }
6369 
6370 namespace {
6371 
6372 struct PPEntityComp {
6373   const ASTReader &Reader;
6374   ModuleFile &M;
6375 
PPEntityComp__anon41ff57ea0d11::PPEntityComp6376   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6377 
operator ()__anon41ff57ea0d11::PPEntityComp6378   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6379     SourceLocation LHS = getLoc(L);
6380     SourceLocation RHS = getLoc(R);
6381     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6382   }
6383 
operator ()__anon41ff57ea0d11::PPEntityComp6384   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6385     SourceLocation LHS = getLoc(L);
6386     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6387   }
6388 
operator ()__anon41ff57ea0d11::PPEntityComp6389   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6390     SourceLocation RHS = getLoc(R);
6391     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6392   }
6393 
getLoc__anon41ff57ea0d11::PPEntityComp6394   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6395     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6396   }
6397 };
6398 
6399 } // namespace
6400 
findPreprocessedEntity(SourceLocation Loc,bool EndsAfter) const6401 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6402                                                        bool EndsAfter) const {
6403   if (SourceMgr.isLocalSourceLocation(Loc))
6404     return getTotalNumPreprocessedEntities();
6405 
6406   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6407       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6408   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6409          "Corrupted global sloc offset map");
6410 
6411   if (SLocMapI->second->NumPreprocessedEntities == 0)
6412     return findNextPreprocessedEntity(SLocMapI);
6413 
6414   ModuleFile &M = *SLocMapI->second;
6415 
6416   using pp_iterator = const PPEntityOffset *;
6417 
6418   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6419   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6420 
6421   size_t Count = M.NumPreprocessedEntities;
6422   size_t Half;
6423   pp_iterator First = pp_begin;
6424   pp_iterator PPI;
6425 
6426   if (EndsAfter) {
6427     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6428                            PPEntityComp(*this, M));
6429   } else {
6430     // Do a binary search manually instead of using std::lower_bound because
6431     // The end locations of entities may be unordered (when a macro expansion
6432     // is inside another macro argument), but for this case it is not important
6433     // whether we get the first macro expansion or its containing macro.
6434     while (Count > 0) {
6435       Half = Count / 2;
6436       PPI = First;
6437       std::advance(PPI, Half);
6438       if (SourceMgr.isBeforeInTranslationUnit(
6439               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6440         First = PPI;
6441         ++First;
6442         Count = Count - Half - 1;
6443       } else
6444         Count = Half;
6445     }
6446   }
6447 
6448   if (PPI == pp_end)
6449     return findNextPreprocessedEntity(SLocMapI);
6450 
6451   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6452 }
6453 
6454 /// Returns a pair of [Begin, End) indices of preallocated
6455 /// preprocessed entities that \arg Range encompasses.
6456 std::pair<unsigned, unsigned>
findPreprocessedEntitiesInRange(SourceRange Range)6457     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6458   if (Range.isInvalid())
6459     return std::make_pair(0,0);
6460   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6461 
6462   PreprocessedEntityID BeginID =
6463       findPreprocessedEntity(Range.getBegin(), false);
6464   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6465   return std::make_pair(BeginID, EndID);
6466 }
6467 
6468 /// Optionally returns true or false if the preallocated preprocessed
6469 /// entity with index \arg Index came from file \arg FID.
isPreprocessedEntityInFileID(unsigned Index,FileID FID)6470 std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6471                                                             FileID FID) {
6472   if (FID.isInvalid())
6473     return false;
6474 
6475   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6476   ModuleFile &M = *PPInfo.first;
6477   unsigned LocalIndex = PPInfo.second;
6478   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6479 
6480   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6481   if (Loc.isInvalid())
6482     return false;
6483 
6484   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6485     return true;
6486   else
6487     return false;
6488 }
6489 
6490 namespace {
6491 
6492   /// Visitor used to search for information about a header file.
6493   class HeaderFileInfoVisitor {
6494   FileEntryRef FE;
6495     std::optional<HeaderFileInfo> HFI;
6496 
6497   public:
HeaderFileInfoVisitor(FileEntryRef FE)6498     explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6499 
operator ()(ModuleFile & M)6500     bool operator()(ModuleFile &M) {
6501       HeaderFileInfoLookupTable *Table
6502         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6503       if (!Table)
6504         return false;
6505 
6506       // Look in the on-disk hash table for an entry for this file name.
6507       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6508       if (Pos == Table->end())
6509         return false;
6510 
6511       HFI = *Pos;
6512       return true;
6513     }
6514 
getHeaderFileInfo() const6515     std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6516   };
6517 
6518 } // namespace
6519 
GetHeaderFileInfo(FileEntryRef FE)6520 HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6521   HeaderFileInfoVisitor Visitor(FE);
6522   ModuleMgr.visit(Visitor);
6523   if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6524       return *HFI;
6525 
6526   return HeaderFileInfo();
6527 }
6528 
ReadPragmaDiagnosticMappings(DiagnosticsEngine & Diag)6529 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6530   using DiagState = DiagnosticsEngine::DiagState;
6531   SmallVector<DiagState *, 32> DiagStates;
6532 
6533   for (ModuleFile &F : ModuleMgr) {
6534     unsigned Idx = 0;
6535     auto &Record = F.PragmaDiagMappings;
6536     if (Record.empty())
6537       continue;
6538 
6539     DiagStates.clear();
6540 
6541     auto ReadDiagState = [&](const DiagState &BasedOn,
6542                              bool IncludeNonPragmaStates) {
6543       unsigned BackrefID = Record[Idx++];
6544       if (BackrefID != 0)
6545         return DiagStates[BackrefID - 1];
6546 
6547       // A new DiagState was created here.
6548       Diag.DiagStates.push_back(BasedOn);
6549       DiagState *NewState = &Diag.DiagStates.back();
6550       DiagStates.push_back(NewState);
6551       unsigned Size = Record[Idx++];
6552       assert(Idx + Size * 2 <= Record.size() &&
6553              "Invalid data, not enough diag/map pairs");
6554       while (Size--) {
6555         unsigned DiagID = Record[Idx++];
6556         DiagnosticMapping NewMapping =
6557             DiagnosticMapping::deserialize(Record[Idx++]);
6558         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6559           continue;
6560 
6561         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6562 
6563         // If this mapping was specified as a warning but the severity was
6564         // upgraded due to diagnostic settings, simulate the current diagnostic
6565         // settings (and use a warning).
6566         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6567           NewMapping.setSeverity(diag::Severity::Warning);
6568           NewMapping.setUpgradedFromWarning(false);
6569         }
6570 
6571         Mapping = NewMapping;
6572       }
6573       return NewState;
6574     };
6575 
6576     // Read the first state.
6577     DiagState *FirstState;
6578     if (F.Kind == MK_ImplicitModule) {
6579       // Implicitly-built modules are reused with different diagnostic
6580       // settings.  Use the initial diagnostic state from Diag to simulate this
6581       // compilation's diagnostic settings.
6582       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6583       DiagStates.push_back(FirstState);
6584 
6585       // Skip the initial diagnostic state from the serialized module.
6586       assert(Record[1] == 0 &&
6587              "Invalid data, unexpected backref in initial state");
6588       Idx = 3 + Record[2] * 2;
6589       assert(Idx < Record.size() &&
6590              "Invalid data, not enough state change pairs in initial state");
6591     } else if (F.isModule()) {
6592       // For an explicit module, preserve the flags from the module build
6593       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6594       // -Wblah flags.
6595       unsigned Flags = Record[Idx++];
6596       DiagState Initial;
6597       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6598       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6599       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6600       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6601       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6602       Initial.ExtBehavior = (diag::Severity)Flags;
6603       FirstState = ReadDiagState(Initial, true);
6604 
6605       assert(F.OriginalSourceFileID.isValid());
6606 
6607       // Set up the root buffer of the module to start with the initial
6608       // diagnostic state of the module itself, to cover files that contain no
6609       // explicit transitions (for which we did not serialize anything).
6610       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6611           .StateTransitions.push_back({FirstState, 0});
6612     } else {
6613       // For prefix ASTs, start with whatever the user configured on the
6614       // command line.
6615       Idx++; // Skip flags.
6616       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6617     }
6618 
6619     // Read the state transitions.
6620     unsigned NumLocations = Record[Idx++];
6621     while (NumLocations--) {
6622       assert(Idx < Record.size() &&
6623              "Invalid data, missing pragma diagnostic states");
6624       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6625       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6626       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6627       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6628       unsigned Transitions = Record[Idx++];
6629 
6630       // Note that we don't need to set up Parent/ParentOffset here, because
6631       // we won't be changing the diagnostic state within imported FileIDs
6632       // (other than perhaps appending to the main source file, which has no
6633       // parent).
6634       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6635       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6636       for (unsigned I = 0; I != Transitions; ++I) {
6637         unsigned Offset = Record[Idx++];
6638         auto *State = ReadDiagState(*FirstState, false);
6639         F.StateTransitions.push_back({State, Offset});
6640       }
6641     }
6642 
6643     // Read the final state.
6644     assert(Idx < Record.size() &&
6645            "Invalid data, missing final pragma diagnostic state");
6646     SourceLocation CurStateLoc = ReadSourceLocation(F, Record[Idx++]);
6647     auto *CurState = ReadDiagState(*FirstState, false);
6648 
6649     if (!F.isModule()) {
6650       Diag.DiagStatesByLoc.CurDiagState = CurState;
6651       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6652 
6653       // Preserve the property that the imaginary root file describes the
6654       // current state.
6655       FileID NullFile;
6656       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6657       if (T.empty())
6658         T.push_back({CurState, 0});
6659       else
6660         T[0].State = CurState;
6661     }
6662 
6663     // Don't try to read these mappings again.
6664     Record.clear();
6665   }
6666 }
6667 
6668 /// Get the correct cursor and offset for loading a type.
TypeCursorForIndex(unsigned Index)6669 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6670   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6671   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6672   ModuleFile *M = I->second;
6673   return RecordLocation(
6674       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6675              M->DeclsBlockStartOffset);
6676 }
6677 
getTypeClassForCode(TypeCode code)6678 static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6679   switch (code) {
6680 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6681   case TYPE_##CODE_ID: return Type::CLASS_ID;
6682 #include "clang/Serialization/TypeBitCodes.def"
6683   default:
6684     return std::nullopt;
6685   }
6686 }
6687 
6688 /// Read and return the type with the given index..
6689 ///
6690 /// The index is the type ID, shifted and minus the number of predefs. This
6691 /// routine actually reads the record corresponding to the type at the given
6692 /// location. It is a helper routine for GetType, which deals with reading type
6693 /// IDs.
readTypeRecord(unsigned Index)6694 QualType ASTReader::readTypeRecord(unsigned Index) {
6695   assert(ContextObj && "reading type with no AST context");
6696   ASTContext &Context = *ContextObj;
6697   RecordLocation Loc = TypeCursorForIndex(Index);
6698   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6699 
6700   // Keep track of where we are in the stream, then jump back there
6701   // after reading this type.
6702   SavedStreamPosition SavedPosition(DeclsCursor);
6703 
6704   ReadingKindTracker ReadingKind(Read_Type, *this);
6705 
6706   // Note that we are loading a type record.
6707   Deserializing AType(this);
6708 
6709   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6710     Error(std::move(Err));
6711     return QualType();
6712   }
6713   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6714   if (!RawCode) {
6715     Error(RawCode.takeError());
6716     return QualType();
6717   }
6718 
6719   ASTRecordReader Record(*this, *Loc.F);
6720   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6721   if (!Code) {
6722     Error(Code.takeError());
6723     return QualType();
6724   }
6725   if (Code.get() == TYPE_EXT_QUAL) {
6726     QualType baseType = Record.readQualType();
6727     Qualifiers quals = Record.readQualifiers();
6728     return Context.getQualifiedType(baseType, quals);
6729   }
6730 
6731   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6732   if (!maybeClass) {
6733     Error("Unexpected code for type");
6734     return QualType();
6735   }
6736 
6737   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6738   return TypeReader.read(*maybeClass);
6739 }
6740 
6741 namespace clang {
6742 
6743 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6744   using LocSeq = SourceLocationSequence;
6745 
6746   ASTRecordReader &Reader;
6747   LocSeq *Seq;
6748 
readSourceLocation()6749   SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); }
readSourceRange()6750   SourceRange readSourceRange() { return Reader.readSourceRange(Seq); }
6751 
GetTypeSourceInfo()6752   TypeSourceInfo *GetTypeSourceInfo() {
6753     return Reader.readTypeSourceInfo();
6754   }
6755 
ReadNestedNameSpecifierLoc()6756   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6757     return Reader.readNestedNameSpecifierLoc();
6758   }
6759 
ReadAttr()6760   Attr *ReadAttr() {
6761     return Reader.readAttr();
6762   }
6763 
6764 public:
TypeLocReader(ASTRecordReader & Reader,LocSeq * Seq)6765   TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq)
6766       : Reader(Reader), Seq(Seq) {}
6767 
6768   // We want compile-time assurance that we've enumerated all of
6769   // these, so unfortunately we have to declare them first, then
6770   // define them out-of-line.
6771 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6772 #define TYPELOC(CLASS, PARENT) \
6773   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6774 #include "clang/AST/TypeLocNodes.def"
6775 
6776   void VisitFunctionTypeLoc(FunctionTypeLoc);
6777   void VisitArrayTypeLoc(ArrayTypeLoc);
6778 };
6779 
6780 } // namespace clang
6781 
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)6782 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6783   // nothing to do
6784 }
6785 
VisitBuiltinTypeLoc(BuiltinTypeLoc TL)6786 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6787   TL.setBuiltinLoc(readSourceLocation());
6788   if (TL.needsExtraLocalData()) {
6789     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6790     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6791     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6792     TL.setModeAttr(Reader.readInt());
6793   }
6794 }
6795 
VisitComplexTypeLoc(ComplexTypeLoc TL)6796 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6797   TL.setNameLoc(readSourceLocation());
6798 }
6799 
VisitPointerTypeLoc(PointerTypeLoc TL)6800 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6801   TL.setStarLoc(readSourceLocation());
6802 }
6803 
VisitDecayedTypeLoc(DecayedTypeLoc TL)6804 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6805   // nothing to do
6806 }
6807 
VisitAdjustedTypeLoc(AdjustedTypeLoc TL)6808 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6809   // nothing to do
6810 }
6811 
VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL)6812 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6813   TL.setExpansionLoc(readSourceLocation());
6814 }
6815 
VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL)6816 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6817   TL.setCaretLoc(readSourceLocation());
6818 }
6819 
VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL)6820 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6821   TL.setAmpLoc(readSourceLocation());
6822 }
6823 
VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL)6824 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6825   TL.setAmpAmpLoc(readSourceLocation());
6826 }
6827 
VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL)6828 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6829   TL.setStarLoc(readSourceLocation());
6830   TL.setClassTInfo(GetTypeSourceInfo());
6831 }
6832 
VisitArrayTypeLoc(ArrayTypeLoc TL)6833 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6834   TL.setLBracketLoc(readSourceLocation());
6835   TL.setRBracketLoc(readSourceLocation());
6836   if (Reader.readBool())
6837     TL.setSizeExpr(Reader.readExpr());
6838   else
6839     TL.setSizeExpr(nullptr);
6840 }
6841 
VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL)6842 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6843   VisitArrayTypeLoc(TL);
6844 }
6845 
VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL)6846 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6847   VisitArrayTypeLoc(TL);
6848 }
6849 
VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL)6850 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6851   VisitArrayTypeLoc(TL);
6852 }
6853 
VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL)6854 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6855                                             DependentSizedArrayTypeLoc TL) {
6856   VisitArrayTypeLoc(TL);
6857 }
6858 
VisitDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc TL)6859 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6860     DependentAddressSpaceTypeLoc TL) {
6861 
6862     TL.setAttrNameLoc(readSourceLocation());
6863     TL.setAttrOperandParensRange(readSourceRange());
6864     TL.setAttrExprOperand(Reader.readExpr());
6865 }
6866 
VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL)6867 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6868                                         DependentSizedExtVectorTypeLoc TL) {
6869   TL.setNameLoc(readSourceLocation());
6870 }
6871 
VisitVectorTypeLoc(VectorTypeLoc TL)6872 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6873   TL.setNameLoc(readSourceLocation());
6874 }
6875 
VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL)6876 void TypeLocReader::VisitDependentVectorTypeLoc(
6877     DependentVectorTypeLoc TL) {
6878   TL.setNameLoc(readSourceLocation());
6879 }
6880 
VisitExtVectorTypeLoc(ExtVectorTypeLoc TL)6881 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6882   TL.setNameLoc(readSourceLocation());
6883 }
6884 
VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL)6885 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6886   TL.setAttrNameLoc(readSourceLocation());
6887   TL.setAttrOperandParensRange(readSourceRange());
6888   TL.setAttrRowOperand(Reader.readExpr());
6889   TL.setAttrColumnOperand(Reader.readExpr());
6890 }
6891 
VisitDependentSizedMatrixTypeLoc(DependentSizedMatrixTypeLoc TL)6892 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6893     DependentSizedMatrixTypeLoc TL) {
6894   TL.setAttrNameLoc(readSourceLocation());
6895   TL.setAttrOperandParensRange(readSourceRange());
6896   TL.setAttrRowOperand(Reader.readExpr());
6897   TL.setAttrColumnOperand(Reader.readExpr());
6898 }
6899 
VisitFunctionTypeLoc(FunctionTypeLoc TL)6900 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6901   TL.setLocalRangeBegin(readSourceLocation());
6902   TL.setLParenLoc(readSourceLocation());
6903   TL.setRParenLoc(readSourceLocation());
6904   TL.setExceptionSpecRange(readSourceRange());
6905   TL.setLocalRangeEnd(readSourceLocation());
6906   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6907     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6908   }
6909 }
6910 
VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL)6911 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6912   VisitFunctionTypeLoc(TL);
6913 }
6914 
VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL)6915 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6916   VisitFunctionTypeLoc(TL);
6917 }
6918 
VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL)6919 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6920   TL.setNameLoc(readSourceLocation());
6921 }
6922 
VisitUsingTypeLoc(UsingTypeLoc TL)6923 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6924   TL.setNameLoc(readSourceLocation());
6925 }
6926 
VisitTypedefTypeLoc(TypedefTypeLoc TL)6927 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6928   TL.setNameLoc(readSourceLocation());
6929 }
6930 
VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL)6931 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6932   TL.setTypeofLoc(readSourceLocation());
6933   TL.setLParenLoc(readSourceLocation());
6934   TL.setRParenLoc(readSourceLocation());
6935 }
6936 
VisitTypeOfTypeLoc(TypeOfTypeLoc TL)6937 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6938   TL.setTypeofLoc(readSourceLocation());
6939   TL.setLParenLoc(readSourceLocation());
6940   TL.setRParenLoc(readSourceLocation());
6941   TL.setUnmodifiedTInfo(GetTypeSourceInfo());
6942 }
6943 
VisitDecltypeTypeLoc(DecltypeTypeLoc TL)6944 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6945   TL.setDecltypeLoc(readSourceLocation());
6946   TL.setRParenLoc(readSourceLocation());
6947 }
6948 
VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL)6949 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6950   TL.setKWLoc(readSourceLocation());
6951   TL.setLParenLoc(readSourceLocation());
6952   TL.setRParenLoc(readSourceLocation());
6953   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6954 }
6955 
readConceptReference()6956 ConceptReference *ASTRecordReader::readConceptReference() {
6957   auto NNS = readNestedNameSpecifierLoc();
6958   auto TemplateKWLoc = readSourceLocation();
6959   auto ConceptNameLoc = readDeclarationNameInfo();
6960   auto FoundDecl = readDeclAs<NamedDecl>();
6961   auto NamedConcept = readDeclAs<ConceptDecl>();
6962   auto *CR = ConceptReference::Create(
6963       getContext(), NNS, TemplateKWLoc, ConceptNameLoc, FoundDecl, NamedConcept,
6964       (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
6965   return CR;
6966 }
6967 
VisitAutoTypeLoc(AutoTypeLoc TL)6968 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6969   TL.setNameLoc(readSourceLocation());
6970   if (Reader.readBool())
6971     TL.setConceptReference(Reader.readConceptReference());
6972   if (Reader.readBool())
6973     TL.setRParenLoc(readSourceLocation());
6974 }
6975 
VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL)6976 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6977     DeducedTemplateSpecializationTypeLoc TL) {
6978   TL.setTemplateNameLoc(readSourceLocation());
6979 }
6980 
VisitRecordTypeLoc(RecordTypeLoc TL)6981 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6982   TL.setNameLoc(readSourceLocation());
6983 }
6984 
VisitEnumTypeLoc(EnumTypeLoc TL)6985 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6986   TL.setNameLoc(readSourceLocation());
6987 }
6988 
VisitAttributedTypeLoc(AttributedTypeLoc TL)6989 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6990   TL.setAttr(ReadAttr());
6991 }
6992 
VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL)6993 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6994   // Nothing to do.
6995 }
6996 
VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)6997 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6998   TL.setNameLoc(readSourceLocation());
6999 }
7000 
VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL)7001 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7002                                             SubstTemplateTypeParmTypeLoc TL) {
7003   TL.setNameLoc(readSourceLocation());
7004 }
7005 
VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL)7006 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7007                                           SubstTemplateTypeParmPackTypeLoc TL) {
7008   TL.setNameLoc(readSourceLocation());
7009 }
7010 
VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL)7011 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7012                                            TemplateSpecializationTypeLoc TL) {
7013   TL.setTemplateKeywordLoc(readSourceLocation());
7014   TL.setTemplateNameLoc(readSourceLocation());
7015   TL.setLAngleLoc(readSourceLocation());
7016   TL.setRAngleLoc(readSourceLocation());
7017   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7018     TL.setArgLocInfo(i,
7019                      Reader.readTemplateArgumentLocInfo(
7020                          TL.getTypePtr()->template_arguments()[i].getKind()));
7021 }
7022 
VisitParenTypeLoc(ParenTypeLoc TL)7023 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7024   TL.setLParenLoc(readSourceLocation());
7025   TL.setRParenLoc(readSourceLocation());
7026 }
7027 
VisitElaboratedTypeLoc(ElaboratedTypeLoc TL)7028 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7029   TL.setElaboratedKeywordLoc(readSourceLocation());
7030   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7031 }
7032 
VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL)7033 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7034   TL.setNameLoc(readSourceLocation());
7035 }
7036 
VisitDependentNameTypeLoc(DependentNameTypeLoc TL)7037 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7038   TL.setElaboratedKeywordLoc(readSourceLocation());
7039   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7040   TL.setNameLoc(readSourceLocation());
7041 }
7042 
VisitDependentTemplateSpecializationTypeLoc(DependentTemplateSpecializationTypeLoc TL)7043 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7044        DependentTemplateSpecializationTypeLoc TL) {
7045   TL.setElaboratedKeywordLoc(readSourceLocation());
7046   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7047   TL.setTemplateKeywordLoc(readSourceLocation());
7048   TL.setTemplateNameLoc(readSourceLocation());
7049   TL.setLAngleLoc(readSourceLocation());
7050   TL.setRAngleLoc(readSourceLocation());
7051   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7052     TL.setArgLocInfo(I,
7053                      Reader.readTemplateArgumentLocInfo(
7054                          TL.getTypePtr()->template_arguments()[I].getKind()));
7055 }
7056 
VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL)7057 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7058   TL.setEllipsisLoc(readSourceLocation());
7059 }
7060 
VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)7061 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7062   TL.setNameLoc(readSourceLocation());
7063   TL.setNameEndLoc(readSourceLocation());
7064 }
7065 
VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL)7066 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7067   if (TL.getNumProtocols()) {
7068     TL.setProtocolLAngleLoc(readSourceLocation());
7069     TL.setProtocolRAngleLoc(readSourceLocation());
7070   }
7071   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7072     TL.setProtocolLoc(i, readSourceLocation());
7073 }
7074 
VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL)7075 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7076   TL.setHasBaseTypeAsWritten(Reader.readBool());
7077   TL.setTypeArgsLAngleLoc(readSourceLocation());
7078   TL.setTypeArgsRAngleLoc(readSourceLocation());
7079   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7080     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7081   TL.setProtocolLAngleLoc(readSourceLocation());
7082   TL.setProtocolRAngleLoc(readSourceLocation());
7083   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7084     TL.setProtocolLoc(i, readSourceLocation());
7085 }
7086 
VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL)7087 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7088   TL.setStarLoc(readSourceLocation());
7089 }
7090 
VisitAtomicTypeLoc(AtomicTypeLoc TL)7091 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7092   TL.setKWLoc(readSourceLocation());
7093   TL.setLParenLoc(readSourceLocation());
7094   TL.setRParenLoc(readSourceLocation());
7095 }
7096 
VisitPipeTypeLoc(PipeTypeLoc TL)7097 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7098   TL.setKWLoc(readSourceLocation());
7099 }
7100 
VisitBitIntTypeLoc(clang::BitIntTypeLoc TL)7101 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7102   TL.setNameLoc(readSourceLocation());
7103 }
VisitDependentBitIntTypeLoc(clang::DependentBitIntTypeLoc TL)7104 void TypeLocReader::VisitDependentBitIntTypeLoc(
7105     clang::DependentBitIntTypeLoc TL) {
7106   TL.setNameLoc(readSourceLocation());
7107 }
7108 
readTypeLoc(TypeLoc TL,LocSeq * ParentSeq)7109 void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
7110   LocSeq::State Seq(ParentSeq);
7111   TypeLocReader TLR(*this, Seq);
7112   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7113     TLR.Visit(TL);
7114 }
7115 
readTypeSourceInfo()7116 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7117   QualType InfoTy = readType();
7118   if (InfoTy.isNull())
7119     return nullptr;
7120 
7121   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7122   readTypeLoc(TInfo->getTypeLoc());
7123   return TInfo;
7124 }
7125 
GetType(TypeID ID)7126 QualType ASTReader::GetType(TypeID ID) {
7127   assert(ContextObj && "reading type with no AST context");
7128   ASTContext &Context = *ContextObj;
7129 
7130   unsigned FastQuals = ID & Qualifiers::FastMask;
7131   unsigned Index = ID >> Qualifiers::FastWidth;
7132 
7133   if (Index < NUM_PREDEF_TYPE_IDS) {
7134     QualType T;
7135     switch ((PredefinedTypeIDs)Index) {
7136     case PREDEF_TYPE_LAST_ID:
7137       // We should never use this one.
7138       llvm_unreachable("Invalid predefined type");
7139       break;
7140     case PREDEF_TYPE_NULL_ID:
7141       return QualType();
7142     case PREDEF_TYPE_VOID_ID:
7143       T = Context.VoidTy;
7144       break;
7145     case PREDEF_TYPE_BOOL_ID:
7146       T = Context.BoolTy;
7147       break;
7148     case PREDEF_TYPE_CHAR_U_ID:
7149     case PREDEF_TYPE_CHAR_S_ID:
7150       // FIXME: Check that the signedness of CharTy is correct!
7151       T = Context.CharTy;
7152       break;
7153     case PREDEF_TYPE_UCHAR_ID:
7154       T = Context.UnsignedCharTy;
7155       break;
7156     case PREDEF_TYPE_USHORT_ID:
7157       T = Context.UnsignedShortTy;
7158       break;
7159     case PREDEF_TYPE_UINT_ID:
7160       T = Context.UnsignedIntTy;
7161       break;
7162     case PREDEF_TYPE_ULONG_ID:
7163       T = Context.UnsignedLongTy;
7164       break;
7165     case PREDEF_TYPE_ULONGLONG_ID:
7166       T = Context.UnsignedLongLongTy;
7167       break;
7168     case PREDEF_TYPE_UINT128_ID:
7169       T = Context.UnsignedInt128Ty;
7170       break;
7171     case PREDEF_TYPE_SCHAR_ID:
7172       T = Context.SignedCharTy;
7173       break;
7174     case PREDEF_TYPE_WCHAR_ID:
7175       T = Context.WCharTy;
7176       break;
7177     case PREDEF_TYPE_SHORT_ID:
7178       T = Context.ShortTy;
7179       break;
7180     case PREDEF_TYPE_INT_ID:
7181       T = Context.IntTy;
7182       break;
7183     case PREDEF_TYPE_LONG_ID:
7184       T = Context.LongTy;
7185       break;
7186     case PREDEF_TYPE_LONGLONG_ID:
7187       T = Context.LongLongTy;
7188       break;
7189     case PREDEF_TYPE_INT128_ID:
7190       T = Context.Int128Ty;
7191       break;
7192     case PREDEF_TYPE_BFLOAT16_ID:
7193       T = Context.BFloat16Ty;
7194       break;
7195     case PREDEF_TYPE_HALF_ID:
7196       T = Context.HalfTy;
7197       break;
7198     case PREDEF_TYPE_FLOAT_ID:
7199       T = Context.FloatTy;
7200       break;
7201     case PREDEF_TYPE_DOUBLE_ID:
7202       T = Context.DoubleTy;
7203       break;
7204     case PREDEF_TYPE_LONGDOUBLE_ID:
7205       T = Context.LongDoubleTy;
7206       break;
7207     case PREDEF_TYPE_SHORT_ACCUM_ID:
7208       T = Context.ShortAccumTy;
7209       break;
7210     case PREDEF_TYPE_ACCUM_ID:
7211       T = Context.AccumTy;
7212       break;
7213     case PREDEF_TYPE_LONG_ACCUM_ID:
7214       T = Context.LongAccumTy;
7215       break;
7216     case PREDEF_TYPE_USHORT_ACCUM_ID:
7217       T = Context.UnsignedShortAccumTy;
7218       break;
7219     case PREDEF_TYPE_UACCUM_ID:
7220       T = Context.UnsignedAccumTy;
7221       break;
7222     case PREDEF_TYPE_ULONG_ACCUM_ID:
7223       T = Context.UnsignedLongAccumTy;
7224       break;
7225     case PREDEF_TYPE_SHORT_FRACT_ID:
7226       T = Context.ShortFractTy;
7227       break;
7228     case PREDEF_TYPE_FRACT_ID:
7229       T = Context.FractTy;
7230       break;
7231     case PREDEF_TYPE_LONG_FRACT_ID:
7232       T = Context.LongFractTy;
7233       break;
7234     case PREDEF_TYPE_USHORT_FRACT_ID:
7235       T = Context.UnsignedShortFractTy;
7236       break;
7237     case PREDEF_TYPE_UFRACT_ID:
7238       T = Context.UnsignedFractTy;
7239       break;
7240     case PREDEF_TYPE_ULONG_FRACT_ID:
7241       T = Context.UnsignedLongFractTy;
7242       break;
7243     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7244       T = Context.SatShortAccumTy;
7245       break;
7246     case PREDEF_TYPE_SAT_ACCUM_ID:
7247       T = Context.SatAccumTy;
7248       break;
7249     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7250       T = Context.SatLongAccumTy;
7251       break;
7252     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7253       T = Context.SatUnsignedShortAccumTy;
7254       break;
7255     case PREDEF_TYPE_SAT_UACCUM_ID:
7256       T = Context.SatUnsignedAccumTy;
7257       break;
7258     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7259       T = Context.SatUnsignedLongAccumTy;
7260       break;
7261     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7262       T = Context.SatShortFractTy;
7263       break;
7264     case PREDEF_TYPE_SAT_FRACT_ID:
7265       T = Context.SatFractTy;
7266       break;
7267     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7268       T = Context.SatLongFractTy;
7269       break;
7270     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7271       T = Context.SatUnsignedShortFractTy;
7272       break;
7273     case PREDEF_TYPE_SAT_UFRACT_ID:
7274       T = Context.SatUnsignedFractTy;
7275       break;
7276     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7277       T = Context.SatUnsignedLongFractTy;
7278       break;
7279     case PREDEF_TYPE_FLOAT16_ID:
7280       T = Context.Float16Ty;
7281       break;
7282     case PREDEF_TYPE_FLOAT128_ID:
7283       T = Context.Float128Ty;
7284       break;
7285     case PREDEF_TYPE_IBM128_ID:
7286       T = Context.Ibm128Ty;
7287       break;
7288     case PREDEF_TYPE_OVERLOAD_ID:
7289       T = Context.OverloadTy;
7290       break;
7291     case PREDEF_TYPE_BOUND_MEMBER:
7292       T = Context.BoundMemberTy;
7293       break;
7294     case PREDEF_TYPE_PSEUDO_OBJECT:
7295       T = Context.PseudoObjectTy;
7296       break;
7297     case PREDEF_TYPE_DEPENDENT_ID:
7298       T = Context.DependentTy;
7299       break;
7300     case PREDEF_TYPE_UNKNOWN_ANY:
7301       T = Context.UnknownAnyTy;
7302       break;
7303     case PREDEF_TYPE_NULLPTR_ID:
7304       T = Context.NullPtrTy;
7305       break;
7306     case PREDEF_TYPE_CHAR8_ID:
7307       T = Context.Char8Ty;
7308       break;
7309     case PREDEF_TYPE_CHAR16_ID:
7310       T = Context.Char16Ty;
7311       break;
7312     case PREDEF_TYPE_CHAR32_ID:
7313       T = Context.Char32Ty;
7314       break;
7315     case PREDEF_TYPE_OBJC_ID:
7316       T = Context.ObjCBuiltinIdTy;
7317       break;
7318     case PREDEF_TYPE_OBJC_CLASS:
7319       T = Context.ObjCBuiltinClassTy;
7320       break;
7321     case PREDEF_TYPE_OBJC_SEL:
7322       T = Context.ObjCBuiltinSelTy;
7323       break;
7324 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7325     case PREDEF_TYPE_##Id##_ID: \
7326       T = Context.SingletonId; \
7327       break;
7328 #include "clang/Basic/OpenCLImageTypes.def"
7329 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7330     case PREDEF_TYPE_##Id##_ID: \
7331       T = Context.Id##Ty; \
7332       break;
7333 #include "clang/Basic/OpenCLExtensionTypes.def"
7334     case PREDEF_TYPE_SAMPLER_ID:
7335       T = Context.OCLSamplerTy;
7336       break;
7337     case PREDEF_TYPE_EVENT_ID:
7338       T = Context.OCLEventTy;
7339       break;
7340     case PREDEF_TYPE_CLK_EVENT_ID:
7341       T = Context.OCLClkEventTy;
7342       break;
7343     case PREDEF_TYPE_QUEUE_ID:
7344       T = Context.OCLQueueTy;
7345       break;
7346     case PREDEF_TYPE_RESERVE_ID_ID:
7347       T = Context.OCLReserveIDTy;
7348       break;
7349     case PREDEF_TYPE_AUTO_DEDUCT:
7350       T = Context.getAutoDeductType();
7351       break;
7352     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7353       T = Context.getAutoRRefDeductType();
7354       break;
7355     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7356       T = Context.ARCUnbridgedCastTy;
7357       break;
7358     case PREDEF_TYPE_BUILTIN_FN:
7359       T = Context.BuiltinFnTy;
7360       break;
7361     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7362       T = Context.IncompleteMatrixIdxTy;
7363       break;
7364     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7365       T = Context.OMPArraySectionTy;
7366       break;
7367     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7368       T = Context.OMPArraySectionTy;
7369       break;
7370     case PREDEF_TYPE_OMP_ITERATOR:
7371       T = Context.OMPIteratorTy;
7372       break;
7373 #define SVE_TYPE(Name, Id, SingletonId) \
7374     case PREDEF_TYPE_##Id##_ID: \
7375       T = Context.SingletonId; \
7376       break;
7377 #include "clang/Basic/AArch64SVEACLETypes.def"
7378 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7379     case PREDEF_TYPE_##Id##_ID: \
7380       T = Context.Id##Ty; \
7381       break;
7382 #include "clang/Basic/PPCTypes.def"
7383 #define RVV_TYPE(Name, Id, SingletonId) \
7384     case PREDEF_TYPE_##Id##_ID: \
7385       T = Context.SingletonId; \
7386       break;
7387 #include "clang/Basic/RISCVVTypes.def"
7388 #define WASM_TYPE(Name, Id, SingletonId)                                       \
7389   case PREDEF_TYPE_##Id##_ID:                                                  \
7390     T = Context.SingletonId;                                                   \
7391     break;
7392 #include "clang/Basic/WebAssemblyReferenceTypes.def"
7393     }
7394 
7395     assert(!T.isNull() && "Unknown predefined type");
7396     return T.withFastQualifiers(FastQuals);
7397   }
7398 
7399   Index -= NUM_PREDEF_TYPE_IDS;
7400   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7401   if (TypesLoaded[Index].isNull()) {
7402     TypesLoaded[Index] = readTypeRecord(Index);
7403     if (TypesLoaded[Index].isNull())
7404       return QualType();
7405 
7406     TypesLoaded[Index]->setFromAST();
7407     if (DeserializationListener)
7408       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7409                                         TypesLoaded[Index]);
7410   }
7411 
7412   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7413 }
7414 
getLocalType(ModuleFile & F,unsigned LocalID)7415 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7416   return GetType(getGlobalTypeID(F, LocalID));
7417 }
7418 
7419 serialization::TypeID
getGlobalTypeID(ModuleFile & F,unsigned LocalID) const7420 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7421   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7422   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7423 
7424   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7425     return LocalID;
7426 
7427   if (!F.ModuleOffsetMap.empty())
7428     ReadModuleOffsetMap(F);
7429 
7430   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7431     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7432   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7433 
7434   unsigned GlobalIndex = LocalIndex + I->second;
7435   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7436 }
7437 
7438 TemplateArgumentLocInfo
readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind)7439 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7440   switch (Kind) {
7441   case TemplateArgument::Expression:
7442     return readExpr();
7443   case TemplateArgument::Type:
7444     return readTypeSourceInfo();
7445   case TemplateArgument::Template: {
7446     NestedNameSpecifierLoc QualifierLoc =
7447       readNestedNameSpecifierLoc();
7448     SourceLocation TemplateNameLoc = readSourceLocation();
7449     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7450                                    TemplateNameLoc, SourceLocation());
7451   }
7452   case TemplateArgument::TemplateExpansion: {
7453     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7454     SourceLocation TemplateNameLoc = readSourceLocation();
7455     SourceLocation EllipsisLoc = readSourceLocation();
7456     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7457                                    TemplateNameLoc, EllipsisLoc);
7458   }
7459   case TemplateArgument::Null:
7460   case TemplateArgument::Integral:
7461   case TemplateArgument::Declaration:
7462   case TemplateArgument::NullPtr:
7463   case TemplateArgument::StructuralValue:
7464   case TemplateArgument::Pack:
7465     // FIXME: Is this right?
7466     return TemplateArgumentLocInfo();
7467   }
7468   llvm_unreachable("unexpected template argument loc");
7469 }
7470 
readTemplateArgumentLoc()7471 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7472   TemplateArgument Arg = readTemplateArgument();
7473 
7474   if (Arg.getKind() == TemplateArgument::Expression) {
7475     if (readBool()) // bool InfoHasSameExpr.
7476       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7477   }
7478   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7479 }
7480 
readTemplateArgumentListInfo(TemplateArgumentListInfo & Result)7481 void ASTRecordReader::readTemplateArgumentListInfo(
7482     TemplateArgumentListInfo &Result) {
7483   Result.setLAngleLoc(readSourceLocation());
7484   Result.setRAngleLoc(readSourceLocation());
7485   unsigned NumArgsAsWritten = readInt();
7486   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7487     Result.addArgument(readTemplateArgumentLoc());
7488 }
7489 
7490 const ASTTemplateArgumentListInfo *
readASTTemplateArgumentListInfo()7491 ASTRecordReader::readASTTemplateArgumentListInfo() {
7492   TemplateArgumentListInfo Result;
7493   readTemplateArgumentListInfo(Result);
7494   return ASTTemplateArgumentListInfo::Create(getContext(), Result);
7495 }
7496 
GetExternalDecl(uint32_t ID)7497 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7498   return GetDecl(ID);
7499 }
7500 
CompleteRedeclChain(const Decl * D)7501 void ASTReader::CompleteRedeclChain(const Decl *D) {
7502   if (NumCurrentElementsDeserializing) {
7503     // We arrange to not care about the complete redeclaration chain while we're
7504     // deserializing. Just remember that the AST has marked this one as complete
7505     // but that it's not actually complete yet, so we know we still need to
7506     // complete it later.
7507     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7508     return;
7509   }
7510 
7511   if (!D->getDeclContext()) {
7512     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7513     return;
7514   }
7515 
7516   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7517 
7518   // If this is a named declaration, complete it by looking it up
7519   // within its context.
7520   //
7521   // FIXME: Merging a function definition should merge
7522   // all mergeable entities within it.
7523   if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(DC)) {
7524     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7525       if (!getContext().getLangOpts().CPlusPlus &&
7526           isa<TranslationUnitDecl>(DC)) {
7527         // Outside of C++, we don't have a lookup table for the TU, so update
7528         // the identifier instead. (For C++ modules, we don't store decls
7529         // in the serialized identifier table, so we do the lookup in the TU.)
7530         auto *II = Name.getAsIdentifierInfo();
7531         assert(II && "non-identifier name in C?");
7532         if (II->isOutOfDate())
7533           updateOutOfDateIdentifier(*II);
7534       } else
7535         DC->lookup(Name);
7536     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7537       // Find all declarations of this kind from the relevant context.
7538       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7539         auto *DC = cast<DeclContext>(DCDecl);
7540         SmallVector<Decl*, 8> Decls;
7541         FindExternalLexicalDecls(
7542             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7543       }
7544     }
7545   }
7546 
7547   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7548     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7549   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7550     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7551   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7552     if (auto *Template = FD->getPrimaryTemplate())
7553       Template->LoadLazySpecializations();
7554   }
7555 }
7556 
7557 CXXCtorInitializer **
GetExternalCXXCtorInitializers(uint64_t Offset)7558 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7559   RecordLocation Loc = getLocalBitOffset(Offset);
7560   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7561   SavedStreamPosition SavedPosition(Cursor);
7562   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7563     Error(std::move(Err));
7564     return nullptr;
7565   }
7566   ReadingKindTracker ReadingKind(Read_Decl, *this);
7567   Deserializing D(this);
7568 
7569   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7570   if (!MaybeCode) {
7571     Error(MaybeCode.takeError());
7572     return nullptr;
7573   }
7574   unsigned Code = MaybeCode.get();
7575 
7576   ASTRecordReader Record(*this, *Loc.F);
7577   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7578   if (!MaybeRecCode) {
7579     Error(MaybeRecCode.takeError());
7580     return nullptr;
7581   }
7582   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7583     Error("malformed AST file: missing C++ ctor initializers");
7584     return nullptr;
7585   }
7586 
7587   return Record.readCXXCtorInitializers();
7588 }
7589 
GetExternalCXXBaseSpecifiers(uint64_t Offset)7590 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7591   assert(ContextObj && "reading base specifiers with no AST context");
7592   ASTContext &Context = *ContextObj;
7593 
7594   RecordLocation Loc = getLocalBitOffset(Offset);
7595   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7596   SavedStreamPosition SavedPosition(Cursor);
7597   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7598     Error(std::move(Err));
7599     return nullptr;
7600   }
7601   ReadingKindTracker ReadingKind(Read_Decl, *this);
7602   Deserializing D(this);
7603 
7604   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7605   if (!MaybeCode) {
7606     Error(MaybeCode.takeError());
7607     return nullptr;
7608   }
7609   unsigned Code = MaybeCode.get();
7610 
7611   ASTRecordReader Record(*this, *Loc.F);
7612   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7613   if (!MaybeRecCode) {
7614     Error(MaybeCode.takeError());
7615     return nullptr;
7616   }
7617   unsigned RecCode = MaybeRecCode.get();
7618 
7619   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7620     Error("malformed AST file: missing C++ base specifiers");
7621     return nullptr;
7622   }
7623 
7624   unsigned NumBases = Record.readInt();
7625   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7626   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7627   for (unsigned I = 0; I != NumBases; ++I)
7628     Bases[I] = Record.readCXXBaseSpecifier();
7629   return Bases;
7630 }
7631 
7632 serialization::DeclID
getGlobalDeclID(ModuleFile & F,LocalDeclID LocalID) const7633 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7634   if (LocalID < NUM_PREDEF_DECL_IDS)
7635     return LocalID;
7636 
7637   if (!F.ModuleOffsetMap.empty())
7638     ReadModuleOffsetMap(F);
7639 
7640   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7641     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7642   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7643 
7644   return LocalID + I->second;
7645 }
7646 
isDeclIDFromModule(serialization::GlobalDeclID ID,ModuleFile & M) const7647 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7648                                    ModuleFile &M) const {
7649   // Predefined decls aren't from any module.
7650   if (ID < NUM_PREDEF_DECL_IDS)
7651     return false;
7652 
7653   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7654          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7655 }
7656 
getOwningModuleFile(const Decl * D)7657 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7658   if (!D->isFromASTFile())
7659     return nullptr;
7660   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7661   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7662   return I->second;
7663 }
7664 
getSourceLocationForDeclID(GlobalDeclID ID)7665 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7666   if (ID < NUM_PREDEF_DECL_IDS)
7667     return SourceLocation();
7668 
7669   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7670 
7671   if (Index > DeclsLoaded.size()) {
7672     Error("declaration ID out-of-range for AST file");
7673     return SourceLocation();
7674   }
7675 
7676   if (Decl *D = DeclsLoaded[Index])
7677     return D->getLocation();
7678 
7679   SourceLocation Loc;
7680   DeclCursorForID(ID, Loc);
7681   return Loc;
7682 }
7683 
getPredefinedDecl(ASTContext & Context,PredefinedDeclIDs ID)7684 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7685   switch (ID) {
7686   case PREDEF_DECL_NULL_ID:
7687     return nullptr;
7688 
7689   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7690     return Context.getTranslationUnitDecl();
7691 
7692   case PREDEF_DECL_OBJC_ID_ID:
7693     return Context.getObjCIdDecl();
7694 
7695   case PREDEF_DECL_OBJC_SEL_ID:
7696     return Context.getObjCSelDecl();
7697 
7698   case PREDEF_DECL_OBJC_CLASS_ID:
7699     return Context.getObjCClassDecl();
7700 
7701   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7702     return Context.getObjCProtocolDecl();
7703 
7704   case PREDEF_DECL_INT_128_ID:
7705     return Context.getInt128Decl();
7706 
7707   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7708     return Context.getUInt128Decl();
7709 
7710   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7711     return Context.getObjCInstanceTypeDecl();
7712 
7713   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7714     return Context.getBuiltinVaListDecl();
7715 
7716   case PREDEF_DECL_VA_LIST_TAG:
7717     return Context.getVaListTagDecl();
7718 
7719   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7720     return Context.getBuiltinMSVaListDecl();
7721 
7722   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7723     return Context.getMSGuidTagDecl();
7724 
7725   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7726     return Context.getExternCContextDecl();
7727 
7728   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7729     return Context.getMakeIntegerSeqDecl();
7730 
7731   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7732     return Context.getCFConstantStringDecl();
7733 
7734   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7735     return Context.getCFConstantStringTagDecl();
7736 
7737   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7738     return Context.getTypePackElementDecl();
7739   }
7740   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7741 }
7742 
GetExistingDecl(DeclID ID)7743 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7744   assert(ContextObj && "reading decl with no AST context");
7745   if (ID < NUM_PREDEF_DECL_IDS) {
7746     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7747     if (D) {
7748       // Track that we have merged the declaration with ID \p ID into the
7749       // pre-existing predefined declaration \p D.
7750       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7751       if (Merged.empty())
7752         Merged.push_back(ID);
7753     }
7754     return D;
7755   }
7756 
7757   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7758 
7759   if (Index >= DeclsLoaded.size()) {
7760     assert(0 && "declaration ID out-of-range for AST file");
7761     Error("declaration ID out-of-range for AST file");
7762     return nullptr;
7763   }
7764 
7765   return DeclsLoaded[Index];
7766 }
7767 
GetDecl(DeclID ID)7768 Decl *ASTReader::GetDecl(DeclID ID) {
7769   if (ID < NUM_PREDEF_DECL_IDS)
7770     return GetExistingDecl(ID);
7771 
7772   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7773 
7774   if (Index >= DeclsLoaded.size()) {
7775     assert(0 && "declaration ID out-of-range for AST file");
7776     Error("declaration ID out-of-range for AST file");
7777     return nullptr;
7778   }
7779 
7780   if (!DeclsLoaded[Index]) {
7781     ReadDeclRecord(ID);
7782     if (DeserializationListener)
7783       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7784   }
7785 
7786   return DeclsLoaded[Index];
7787 }
7788 
mapGlobalIDToModuleFileGlobalID(ModuleFile & M,DeclID GlobalID)7789 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7790                                                   DeclID GlobalID) {
7791   if (GlobalID < NUM_PREDEF_DECL_IDS)
7792     return GlobalID;
7793 
7794   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7795   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7796   ModuleFile *Owner = I->second;
7797 
7798   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7799     = M.GlobalToLocalDeclIDs.find(Owner);
7800   if (Pos == M.GlobalToLocalDeclIDs.end())
7801     return 0;
7802 
7803   return GlobalID - Owner->BaseDeclID + Pos->second;
7804 }
7805 
ReadDeclID(ModuleFile & F,const RecordData & Record,unsigned & Idx)7806 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7807                                             const RecordData &Record,
7808                                             unsigned &Idx) {
7809   if (Idx >= Record.size()) {
7810     Error("Corrupted AST file");
7811     return 0;
7812   }
7813 
7814   return getGlobalDeclID(F, Record[Idx++]);
7815 }
7816 
7817 /// Resolve the offset of a statement into a statement.
7818 ///
7819 /// This operation will read a new statement from the external
7820 /// source each time it is called, and is meant to be used via a
7821 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
GetExternalDeclStmt(uint64_t Offset)7822 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7823   // Switch case IDs are per Decl.
7824   ClearSwitchCaseIDs();
7825 
7826   // Offset here is a global offset across the entire chain.
7827   RecordLocation Loc = getLocalBitOffset(Offset);
7828   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7829     Error(std::move(Err));
7830     return nullptr;
7831   }
7832   assert(NumCurrentElementsDeserializing == 0 &&
7833          "should not be called while already deserializing");
7834   Deserializing D(this);
7835   return ReadStmtFromStream(*Loc.F);
7836 }
7837 
FindExternalLexicalDecls(const DeclContext * DC,llvm::function_ref<bool (Decl::Kind)> IsKindWeWant,SmallVectorImpl<Decl * > & Decls)7838 void ASTReader::FindExternalLexicalDecls(
7839     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7840     SmallVectorImpl<Decl *> &Decls) {
7841   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7842 
7843   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7844     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7845     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7846       auto K = (Decl::Kind)+LexicalDecls[I];
7847       if (!IsKindWeWant(K))
7848         continue;
7849 
7850       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7851 
7852       // Don't add predefined declarations to the lexical context more
7853       // than once.
7854       if (ID < NUM_PREDEF_DECL_IDS) {
7855         if (PredefsVisited[ID])
7856           continue;
7857 
7858         PredefsVisited[ID] = true;
7859       }
7860 
7861       if (Decl *D = GetLocalDecl(*M, ID)) {
7862         assert(D->getKind() == K && "wrong kind for lexical decl");
7863         if (!DC->isDeclInLexicalTraversal(D))
7864           Decls.push_back(D);
7865       }
7866     }
7867   };
7868 
7869   if (isa<TranslationUnitDecl>(DC)) {
7870     for (const auto &Lexical : TULexicalDecls)
7871       Visit(Lexical.first, Lexical.second);
7872   } else {
7873     auto I = LexicalDecls.find(DC);
7874     if (I != LexicalDecls.end())
7875       Visit(I->second.first, I->second.second);
7876   }
7877 
7878   ++NumLexicalDeclContextsRead;
7879 }
7880 
7881 namespace {
7882 
7883 class DeclIDComp {
7884   ASTReader &Reader;
7885   ModuleFile &Mod;
7886 
7887 public:
DeclIDComp(ASTReader & Reader,ModuleFile & M)7888   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7889 
operator ()(LocalDeclID L,LocalDeclID R) const7890   bool operator()(LocalDeclID L, LocalDeclID R) const {
7891     SourceLocation LHS = getLocation(L);
7892     SourceLocation RHS = getLocation(R);
7893     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7894   }
7895 
operator ()(SourceLocation LHS,LocalDeclID R) const7896   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7897     SourceLocation RHS = getLocation(R);
7898     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7899   }
7900 
operator ()(LocalDeclID L,SourceLocation RHS) const7901   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7902     SourceLocation LHS = getLocation(L);
7903     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7904   }
7905 
getLocation(LocalDeclID ID) const7906   SourceLocation getLocation(LocalDeclID ID) const {
7907     return Reader.getSourceManager().getFileLoc(
7908             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7909   }
7910 };
7911 
7912 } // namespace
7913 
FindFileRegionDecls(FileID File,unsigned Offset,unsigned Length,SmallVectorImpl<Decl * > & Decls)7914 void ASTReader::FindFileRegionDecls(FileID File,
7915                                     unsigned Offset, unsigned Length,
7916                                     SmallVectorImpl<Decl *> &Decls) {
7917   SourceManager &SM = getSourceManager();
7918 
7919   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7920   if (I == FileDeclIDs.end())
7921     return;
7922 
7923   FileDeclsInfo &DInfo = I->second;
7924   if (DInfo.Decls.empty())
7925     return;
7926 
7927   SourceLocation
7928     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7929   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7930 
7931   DeclIDComp DIDComp(*this, *DInfo.Mod);
7932   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7933       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7934   if (BeginIt != DInfo.Decls.begin())
7935     --BeginIt;
7936 
7937   // If we are pointing at a top-level decl inside an objc container, we need
7938   // to backtrack until we find it otherwise we will fail to report that the
7939   // region overlaps with an objc container.
7940   while (BeginIt != DInfo.Decls.begin() &&
7941          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7942              ->isTopLevelDeclInObjCContainer())
7943     --BeginIt;
7944 
7945   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7946       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7947   if (EndIt != DInfo.Decls.end())
7948     ++EndIt;
7949 
7950   for (ArrayRef<serialization::LocalDeclID>::iterator
7951          DIt = BeginIt; DIt != EndIt; ++DIt)
7952     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7953 }
7954 
7955 bool
FindExternalVisibleDeclsByName(const DeclContext * DC,DeclarationName Name)7956 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7957                                           DeclarationName Name) {
7958   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7959          "DeclContext has no visible decls in storage");
7960   if (!Name)
7961     return false;
7962 
7963   auto It = Lookups.find(DC);
7964   if (It == Lookups.end())
7965     return false;
7966 
7967   Deserializing LookupResults(this);
7968 
7969   // Load the list of declarations.
7970   SmallVector<NamedDecl *, 64> Decls;
7971   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7972   for (DeclID ID : It->second.Table.find(Name)) {
7973     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7974     if (ND->getDeclName() == Name && Found.insert(ND).second)
7975       Decls.push_back(ND);
7976   }
7977 
7978   ++NumVisibleDeclContextsRead;
7979   SetExternalVisibleDeclsForName(DC, Name, Decls);
7980   return !Decls.empty();
7981 }
7982 
completeVisibleDeclsMap(const DeclContext * DC)7983 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7984   if (!DC->hasExternalVisibleStorage())
7985     return;
7986 
7987   auto It = Lookups.find(DC);
7988   assert(It != Lookups.end() &&
7989          "have external visible storage but no lookup tables");
7990 
7991   DeclsMap Decls;
7992 
7993   for (DeclID ID : It->second.Table.findAll()) {
7994     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7995     Decls[ND->getDeclName()].push_back(ND);
7996   }
7997 
7998   ++NumVisibleDeclContextsRead;
7999 
8000   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8001     SetExternalVisibleDeclsForName(DC, I->first, I->second);
8002   }
8003   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8004 }
8005 
8006 const serialization::reader::DeclContextLookupTable *
getLoadedLookupTables(DeclContext * Primary) const8007 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8008   auto I = Lookups.find(Primary);
8009   return I == Lookups.end() ? nullptr : &I->second;
8010 }
8011 
8012 /// Under non-PCH compilation the consumer receives the objc methods
8013 /// before receiving the implementation, and codegen depends on this.
8014 /// We simulate this by deserializing and passing to consumer the methods of the
8015 /// implementation before passing the deserialized implementation decl.
PassObjCImplDeclToConsumer(ObjCImplDecl * ImplD,ASTConsumer * Consumer)8016 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8017                                        ASTConsumer *Consumer) {
8018   assert(ImplD && Consumer);
8019 
8020   for (auto *I : ImplD->methods())
8021     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8022 
8023   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8024 }
8025 
PassInterestingDeclToConsumer(Decl * D)8026 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8027   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8028     PassObjCImplDeclToConsumer(ImplD, Consumer);
8029   else
8030     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8031 }
8032 
StartTranslationUnit(ASTConsumer * Consumer)8033 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8034   this->Consumer = Consumer;
8035 
8036   if (Consumer)
8037     PassInterestingDeclsToConsumer();
8038 
8039   if (DeserializationListener)
8040     DeserializationListener->ReaderInitialized(this);
8041 }
8042 
PrintStats()8043 void ASTReader::PrintStats() {
8044   std::fprintf(stderr, "*** AST File Statistics:\n");
8045 
8046   unsigned NumTypesLoaded =
8047       TypesLoaded.size() - llvm::count(TypesLoaded.materialized(), QualType());
8048   unsigned NumDeclsLoaded =
8049       DeclsLoaded.size() -
8050       llvm::count(DeclsLoaded.materialized(), (Decl *)nullptr);
8051   unsigned NumIdentifiersLoaded =
8052       IdentifiersLoaded.size() -
8053       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
8054   unsigned NumMacrosLoaded =
8055       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
8056   unsigned NumSelectorsLoaded =
8057       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
8058 
8059   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8060     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8061                  NumSLocEntriesRead, TotalNumSLocEntries,
8062                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8063   if (!TypesLoaded.empty())
8064     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8065                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8066                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8067   if (!DeclsLoaded.empty())
8068     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8069                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8070                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8071   if (!IdentifiersLoaded.empty())
8072     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8073                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8074                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8075   if (!MacrosLoaded.empty())
8076     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8077                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8078                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8079   if (!SelectorsLoaded.empty())
8080     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8081                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8082                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8083   if (TotalNumStatements)
8084     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8085                  NumStatementsRead, TotalNumStatements,
8086                  ((float)NumStatementsRead/TotalNumStatements * 100));
8087   if (TotalNumMacros)
8088     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8089                  NumMacrosRead, TotalNumMacros,
8090                  ((float)NumMacrosRead/TotalNumMacros * 100));
8091   if (TotalLexicalDeclContexts)
8092     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8093                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8094                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8095                   * 100));
8096   if (TotalVisibleDeclContexts)
8097     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8098                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8099                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8100                   * 100));
8101   if (TotalNumMethodPoolEntries)
8102     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8103                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8104                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8105                   * 100));
8106   if (NumMethodPoolLookups)
8107     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8108                  NumMethodPoolHits, NumMethodPoolLookups,
8109                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8110   if (NumMethodPoolTableLookups)
8111     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8112                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8113                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8114                   * 100.0));
8115   if (NumIdentifierLookupHits)
8116     std::fprintf(stderr,
8117                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8118                  NumIdentifierLookupHits, NumIdentifierLookups,
8119                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8120 
8121   if (GlobalIndex) {
8122     std::fprintf(stderr, "\n");
8123     GlobalIndex->printStats();
8124   }
8125 
8126   std::fprintf(stderr, "\n");
8127   dump();
8128   std::fprintf(stderr, "\n");
8129 }
8130 
8131 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8132 LLVM_DUMP_METHOD static void
dumpModuleIDMap(StringRef Name,const ContinuousRangeMap<Key,ModuleFile *,InitialCapacity> & Map)8133 dumpModuleIDMap(StringRef Name,
8134                 const ContinuousRangeMap<Key, ModuleFile *,
8135                                          InitialCapacity> &Map) {
8136   if (Map.begin() == Map.end())
8137     return;
8138 
8139   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8140 
8141   llvm::errs() << Name << ":\n";
8142   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8143        I != IEnd; ++I) {
8144     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8145       << "\n";
8146   }
8147 }
8148 
dump()8149 LLVM_DUMP_METHOD void ASTReader::dump() {
8150   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8151   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8152   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8153   dumpModuleIDMap("Global type map", GlobalTypeMap);
8154   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8155   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8156   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8157   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8158   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8159   dumpModuleIDMap("Global preprocessed entity map",
8160                   GlobalPreprocessedEntityMap);
8161 
8162   llvm::errs() << "\n*** PCH/Modules Loaded:";
8163   for (ModuleFile &M : ModuleMgr)
8164     M.dump();
8165 }
8166 
8167 /// Return the amount of memory used by memory buffers, breaking down
8168 /// by heap-backed versus mmap'ed memory.
getMemoryBufferSizes(MemoryBufferSizes & sizes) const8169 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8170   for (ModuleFile &I : ModuleMgr) {
8171     if (llvm::MemoryBuffer *buf = I.Buffer) {
8172       size_t bytes = buf->getBufferSize();
8173       switch (buf->getBufferKind()) {
8174         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8175           sizes.malloc_bytes += bytes;
8176           break;
8177         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8178           sizes.mmap_bytes += bytes;
8179           break;
8180       }
8181     }
8182   }
8183 }
8184 
InitializeSema(Sema & S)8185 void ASTReader::InitializeSema(Sema &S) {
8186   SemaObj = &S;
8187   S.addExternalSource(this);
8188 
8189   // Makes sure any declarations that were deserialized "too early"
8190   // still get added to the identifier's declaration chains.
8191   for (uint64_t ID : PreloadedDeclIDs) {
8192     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8193     pushExternalDeclIntoScope(D, D->getDeclName());
8194   }
8195   PreloadedDeclIDs.clear();
8196 
8197   // FIXME: What happens if these are changed by a module import?
8198   if (!FPPragmaOptions.empty()) {
8199     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8200     FPOptionsOverride NewOverrides =
8201         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
8202     SemaObj->CurFPFeatures =
8203         NewOverrides.applyOverrides(SemaObj->getLangOpts());
8204   }
8205 
8206   SemaObj->OpenCLFeatures = OpenCLExtensions;
8207 
8208   UpdateSema();
8209 }
8210 
UpdateSema()8211 void ASTReader::UpdateSema() {
8212   assert(SemaObj && "no Sema to update");
8213 
8214   // Load the offsets of the declarations that Sema references.
8215   // They will be lazily deserialized when needed.
8216   if (!SemaDeclRefs.empty()) {
8217     assert(SemaDeclRefs.size() % 3 == 0);
8218     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8219       if (!SemaObj->StdNamespace)
8220         SemaObj->StdNamespace = SemaDeclRefs[I];
8221       if (!SemaObj->StdBadAlloc)
8222         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8223       if (!SemaObj->StdAlignValT)
8224         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8225     }
8226     SemaDeclRefs.clear();
8227   }
8228 
8229   // Update the state of pragmas. Use the same API as if we had encountered the
8230   // pragma in the source.
8231   if(OptimizeOffPragmaLocation.isValid())
8232     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8233   if (PragmaMSStructState != -1)
8234     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8235   if (PointersToMembersPragmaLocation.isValid()) {
8236     SemaObj->ActOnPragmaMSPointersToMembers(
8237         (LangOptions::PragmaMSPointersToMembersKind)
8238             PragmaMSPointersToMembersState,
8239         PointersToMembersPragmaLocation);
8240   }
8241   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8242 
8243   if (PragmaAlignPackCurrentValue) {
8244     // The bottom of the stack might have a default value. It must be adjusted
8245     // to the current value to ensure that the packing state is preserved after
8246     // popping entries that were included/imported from a PCH/module.
8247     bool DropFirst = false;
8248     if (!PragmaAlignPackStack.empty() &&
8249         PragmaAlignPackStack.front().Location.isInvalid()) {
8250       assert(PragmaAlignPackStack.front().Value ==
8251                  SemaObj->AlignPackStack.DefaultValue &&
8252              "Expected a default alignment value");
8253       SemaObj->AlignPackStack.Stack.emplace_back(
8254           PragmaAlignPackStack.front().SlotLabel,
8255           SemaObj->AlignPackStack.CurrentValue,
8256           SemaObj->AlignPackStack.CurrentPragmaLocation,
8257           PragmaAlignPackStack.front().PushLocation);
8258       DropFirst = true;
8259     }
8260     for (const auto &Entry :
8261          llvm::ArrayRef(PragmaAlignPackStack).drop_front(DropFirst ? 1 : 0)) {
8262       SemaObj->AlignPackStack.Stack.emplace_back(
8263           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8264     }
8265     if (PragmaAlignPackCurrentLocation.isInvalid()) {
8266       assert(*PragmaAlignPackCurrentValue ==
8267                  SemaObj->AlignPackStack.DefaultValue &&
8268              "Expected a default align and pack value");
8269       // Keep the current values.
8270     } else {
8271       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8272       SemaObj->AlignPackStack.CurrentPragmaLocation =
8273           PragmaAlignPackCurrentLocation;
8274     }
8275   }
8276   if (FpPragmaCurrentValue) {
8277     // The bottom of the stack might have a default value. It must be adjusted
8278     // to the current value to ensure that fp-pragma state is preserved after
8279     // popping entries that were included/imported from a PCH/module.
8280     bool DropFirst = false;
8281     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8282       assert(FpPragmaStack.front().Value ==
8283                  SemaObj->FpPragmaStack.DefaultValue &&
8284              "Expected a default pragma float_control value");
8285       SemaObj->FpPragmaStack.Stack.emplace_back(
8286           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
8287           SemaObj->FpPragmaStack.CurrentPragmaLocation,
8288           FpPragmaStack.front().PushLocation);
8289       DropFirst = true;
8290     }
8291     for (const auto &Entry :
8292          llvm::ArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
8293       SemaObj->FpPragmaStack.Stack.emplace_back(
8294           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8295     if (FpPragmaCurrentLocation.isInvalid()) {
8296       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8297              "Expected a default pragma float_control value");
8298       // Keep the current values.
8299     } else {
8300       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8301       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8302     }
8303   }
8304 
8305   // For non-modular AST files, restore visiblity of modules.
8306   for (auto &Import : PendingImportedModulesSema) {
8307     if (Import.ImportLoc.isInvalid())
8308       continue;
8309     if (Module *Imported = getSubmodule(Import.ID)) {
8310       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8311     }
8312   }
8313   PendingImportedModulesSema.clear();
8314 }
8315 
get(StringRef Name)8316 IdentifierInfo *ASTReader::get(StringRef Name) {
8317   // Note that we are loading an identifier.
8318   Deserializing AnIdentifier(this);
8319 
8320   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8321                                   NumIdentifierLookups,
8322                                   NumIdentifierLookupHits);
8323 
8324   // We don't need to do identifier table lookups in C++ modules (we preload
8325   // all interesting declarations, and don't need to use the scope for name
8326   // lookups). Perform the lookup in PCH files, though, since we don't build
8327   // a complete initial identifier table if we're carrying on from a PCH.
8328   if (PP.getLangOpts().CPlusPlus) {
8329     for (auto *F : ModuleMgr.pch_modules())
8330       if (Visitor(*F))
8331         break;
8332   } else {
8333     // If there is a global index, look there first to determine which modules
8334     // provably do not have any results for this identifier.
8335     GlobalModuleIndex::HitSet Hits;
8336     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8337     if (!loadGlobalIndex()) {
8338       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8339         HitsPtr = &Hits;
8340       }
8341     }
8342 
8343     ModuleMgr.visit(Visitor, HitsPtr);
8344   }
8345 
8346   IdentifierInfo *II = Visitor.getIdentifierInfo();
8347   markIdentifierUpToDate(II);
8348   return II;
8349 }
8350 
8351 namespace clang {
8352 
8353   /// An identifier-lookup iterator that enumerates all of the
8354   /// identifiers stored within a set of AST files.
8355   class ASTIdentifierIterator : public IdentifierIterator {
8356     /// The AST reader whose identifiers are being enumerated.
8357     const ASTReader &Reader;
8358 
8359     /// The current index into the chain of AST files stored in
8360     /// the AST reader.
8361     unsigned Index;
8362 
8363     /// The current position within the identifier lookup table
8364     /// of the current AST file.
8365     ASTIdentifierLookupTable::key_iterator Current;
8366 
8367     /// The end position within the identifier lookup table of
8368     /// the current AST file.
8369     ASTIdentifierLookupTable::key_iterator End;
8370 
8371     /// Whether to skip any modules in the ASTReader.
8372     bool SkipModules;
8373 
8374   public:
8375     explicit ASTIdentifierIterator(const ASTReader &Reader,
8376                                    bool SkipModules = false);
8377 
8378     StringRef Next() override;
8379   };
8380 
8381 } // namespace clang
8382 
ASTIdentifierIterator(const ASTReader & Reader,bool SkipModules)8383 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8384                                              bool SkipModules)
8385     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8386 }
8387 
Next()8388 StringRef ASTIdentifierIterator::Next() {
8389   while (Current == End) {
8390     // If we have exhausted all of our AST files, we're done.
8391     if (Index == 0)
8392       return StringRef();
8393 
8394     --Index;
8395     ModuleFile &F = Reader.ModuleMgr[Index];
8396     if (SkipModules && F.isModule())
8397       continue;
8398 
8399     ASTIdentifierLookupTable *IdTable =
8400         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8401     Current = IdTable->key_begin();
8402     End = IdTable->key_end();
8403   }
8404 
8405   // We have any identifiers remaining in the current AST file; return
8406   // the next one.
8407   StringRef Result = *Current;
8408   ++Current;
8409   return Result;
8410 }
8411 
8412 namespace {
8413 
8414 /// A utility for appending two IdentifierIterators.
8415 class ChainedIdentifierIterator : public IdentifierIterator {
8416   std::unique_ptr<IdentifierIterator> Current;
8417   std::unique_ptr<IdentifierIterator> Queued;
8418 
8419 public:
ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,std::unique_ptr<IdentifierIterator> Second)8420   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8421                             std::unique_ptr<IdentifierIterator> Second)
8422       : Current(std::move(First)), Queued(std::move(Second)) {}
8423 
Next()8424   StringRef Next() override {
8425     if (!Current)
8426       return StringRef();
8427 
8428     StringRef result = Current->Next();
8429     if (!result.empty())
8430       return result;
8431 
8432     // Try the queued iterator, which may itself be empty.
8433     Current.reset();
8434     std::swap(Current, Queued);
8435     return Next();
8436   }
8437 };
8438 
8439 } // namespace
8440 
getIdentifiers()8441 IdentifierIterator *ASTReader::getIdentifiers() {
8442   if (!loadGlobalIndex()) {
8443     std::unique_ptr<IdentifierIterator> ReaderIter(
8444         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8445     std::unique_ptr<IdentifierIterator> ModulesIter(
8446         GlobalIndex->createIdentifierIterator());
8447     return new ChainedIdentifierIterator(std::move(ReaderIter),
8448                                          std::move(ModulesIter));
8449   }
8450 
8451   return new ASTIdentifierIterator(*this);
8452 }
8453 
8454 namespace clang {
8455 namespace serialization {
8456 
8457   class ReadMethodPoolVisitor {
8458     ASTReader &Reader;
8459     Selector Sel;
8460     unsigned PriorGeneration;
8461     unsigned InstanceBits = 0;
8462     unsigned FactoryBits = 0;
8463     bool InstanceHasMoreThanOneDecl = false;
8464     bool FactoryHasMoreThanOneDecl = false;
8465     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8466     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8467 
8468   public:
ReadMethodPoolVisitor(ASTReader & Reader,Selector Sel,unsigned PriorGeneration)8469     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8470                           unsigned PriorGeneration)
8471         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8472 
operator ()(ModuleFile & M)8473     bool operator()(ModuleFile &M) {
8474       if (!M.SelectorLookupTable)
8475         return false;
8476 
8477       // If we've already searched this module file, skip it now.
8478       if (M.Generation <= PriorGeneration)
8479         return true;
8480 
8481       ++Reader.NumMethodPoolTableLookups;
8482       ASTSelectorLookupTable *PoolTable
8483         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8484       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8485       if (Pos == PoolTable->end())
8486         return false;
8487 
8488       ++Reader.NumMethodPoolTableHits;
8489       ++Reader.NumSelectorsRead;
8490       // FIXME: Not quite happy with the statistics here. We probably should
8491       // disable this tracking when called via LoadSelector.
8492       // Also, should entries without methods count as misses?
8493       ++Reader.NumMethodPoolEntriesRead;
8494       ASTSelectorLookupTrait::data_type Data = *Pos;
8495       if (Reader.DeserializationListener)
8496         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8497 
8498       // Append methods in the reverse order, so that later we can process them
8499       // in the order they appear in the source code by iterating through
8500       // the vector in the reverse order.
8501       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8502       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8503       InstanceBits = Data.InstanceBits;
8504       FactoryBits = Data.FactoryBits;
8505       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8506       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8507       return false;
8508     }
8509 
8510     /// Retrieve the instance methods found by this visitor.
getInstanceMethods() const8511     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8512       return InstanceMethods;
8513     }
8514 
8515     /// Retrieve the instance methods found by this visitor.
getFactoryMethods() const8516     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8517       return FactoryMethods;
8518     }
8519 
getInstanceBits() const8520     unsigned getInstanceBits() const { return InstanceBits; }
getFactoryBits() const8521     unsigned getFactoryBits() const { return FactoryBits; }
8522 
instanceHasMoreThanOneDecl() const8523     bool instanceHasMoreThanOneDecl() const {
8524       return InstanceHasMoreThanOneDecl;
8525     }
8526 
factoryHasMoreThanOneDecl() const8527     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8528   };
8529 
8530 } // namespace serialization
8531 } // namespace clang
8532 
8533 /// Add the given set of methods to the method list.
addMethodsToPool(Sema & S,ArrayRef<ObjCMethodDecl * > Methods,ObjCMethodList & List)8534 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8535                              ObjCMethodList &List) {
8536   for (ObjCMethodDecl *M : llvm::reverse(Methods))
8537     S.addMethodToGlobalList(&List, M);
8538 }
8539 
ReadMethodPool(Selector Sel)8540 void ASTReader::ReadMethodPool(Selector Sel) {
8541   // Get the selector generation and update it to the current generation.
8542   unsigned &Generation = SelectorGeneration[Sel];
8543   unsigned PriorGeneration = Generation;
8544   Generation = getGeneration();
8545   SelectorOutOfDate[Sel] = false;
8546 
8547   // Search for methods defined with this selector.
8548   ++NumMethodPoolLookups;
8549   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8550   ModuleMgr.visit(Visitor);
8551 
8552   if (Visitor.getInstanceMethods().empty() &&
8553       Visitor.getFactoryMethods().empty())
8554     return;
8555 
8556   ++NumMethodPoolHits;
8557 
8558   if (!getSema())
8559     return;
8560 
8561   Sema &S = *getSema();
8562   Sema::GlobalMethodPool::iterator Pos =
8563       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8564           .first;
8565 
8566   Pos->second.first.setBits(Visitor.getInstanceBits());
8567   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8568   Pos->second.second.setBits(Visitor.getFactoryBits());
8569   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8570 
8571   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8572   // when building a module we keep every method individually and may need to
8573   // update hasMoreThanOneDecl as we add the methods.
8574   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8575   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8576 }
8577 
updateOutOfDateSelector(Selector Sel)8578 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8579   if (SelectorOutOfDate[Sel])
8580     ReadMethodPool(Sel);
8581 }
8582 
ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl * > & Namespaces)8583 void ASTReader::ReadKnownNamespaces(
8584                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8585   Namespaces.clear();
8586 
8587   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8588     if (NamespaceDecl *Namespace
8589                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8590       Namespaces.push_back(Namespace);
8591   }
8592 }
8593 
ReadUndefinedButUsed(llvm::MapVector<NamedDecl *,SourceLocation> & Undefined)8594 void ASTReader::ReadUndefinedButUsed(
8595     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8596   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8597     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8598     SourceLocation Loc =
8599         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8600     Undefined.insert(std::make_pair(D, Loc));
8601   }
8602 }
8603 
ReadMismatchingDeleteExpressions(llvm::MapVector<FieldDecl *,llvm::SmallVector<std::pair<SourceLocation,bool>,4>> & Exprs)8604 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8605     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8606                                                      Exprs) {
8607   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8608     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8609     uint64_t Count = DelayedDeleteExprs[Idx++];
8610     for (uint64_t C = 0; C < Count; ++C) {
8611       SourceLocation DeleteLoc =
8612           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8613       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8614       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8615     }
8616   }
8617 }
8618 
ReadTentativeDefinitions(SmallVectorImpl<VarDecl * > & TentativeDefs)8619 void ASTReader::ReadTentativeDefinitions(
8620                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8621   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8622     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8623     if (Var)
8624       TentativeDefs.push_back(Var);
8625   }
8626   TentativeDefinitions.clear();
8627 }
8628 
ReadUnusedFileScopedDecls(SmallVectorImpl<const DeclaratorDecl * > & Decls)8629 void ASTReader::ReadUnusedFileScopedDecls(
8630                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8631   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8632     DeclaratorDecl *D
8633       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8634     if (D)
8635       Decls.push_back(D);
8636   }
8637   UnusedFileScopedDecls.clear();
8638 }
8639 
ReadDelegatingConstructors(SmallVectorImpl<CXXConstructorDecl * > & Decls)8640 void ASTReader::ReadDelegatingConstructors(
8641                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8642   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8643     CXXConstructorDecl *D
8644       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8645     if (D)
8646       Decls.push_back(D);
8647   }
8648   DelegatingCtorDecls.clear();
8649 }
8650 
ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl * > & Decls)8651 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8652   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8653     TypedefNameDecl *D
8654       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8655     if (D)
8656       Decls.push_back(D);
8657   }
8658   ExtVectorDecls.clear();
8659 }
8660 
ReadUnusedLocalTypedefNameCandidates(llvm::SmallSetVector<const TypedefNameDecl *,4> & Decls)8661 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8662     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8663   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8664        ++I) {
8665     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8666         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8667     if (D)
8668       Decls.insert(D);
8669   }
8670   UnusedLocalTypedefNameCandidates.clear();
8671 }
8672 
ReadDeclsToCheckForDeferredDiags(llvm::SmallSetVector<Decl *,4> & Decls)8673 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8674     llvm::SmallSetVector<Decl *, 4> &Decls) {
8675   for (auto I : DeclsToCheckForDeferredDiags) {
8676     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8677     if (D)
8678       Decls.insert(D);
8679   }
8680   DeclsToCheckForDeferredDiags.clear();
8681 }
8682 
ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,SourceLocation>> & Sels)8683 void ASTReader::ReadReferencedSelectors(
8684        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8685   if (ReferencedSelectorsData.empty())
8686     return;
8687 
8688   // If there are @selector references added them to its pool. This is for
8689   // implementation of -Wselector.
8690   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8691   unsigned I = 0;
8692   while (I < DataSize) {
8693     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8694     SourceLocation SelLoc
8695       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8696     Sels.push_back(std::make_pair(Sel, SelLoc));
8697   }
8698   ReferencedSelectorsData.clear();
8699 }
8700 
ReadWeakUndeclaredIdentifiers(SmallVectorImpl<std::pair<IdentifierInfo *,WeakInfo>> & WeakIDs)8701 void ASTReader::ReadWeakUndeclaredIdentifiers(
8702        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8703   if (WeakUndeclaredIdentifiers.empty())
8704     return;
8705 
8706   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8707     IdentifierInfo *WeakId
8708       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8709     IdentifierInfo *AliasId
8710       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8711     SourceLocation Loc =
8712         SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8713     WeakInfo WI(AliasId, Loc);
8714     WeakIDs.push_back(std::make_pair(WeakId, WI));
8715   }
8716   WeakUndeclaredIdentifiers.clear();
8717 }
8718 
ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> & VTables)8719 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8720   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8721     ExternalVTableUse VT;
8722     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8723     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8724     VT.DefinitionRequired = VTableUses[Idx++];
8725     VTables.push_back(VT);
8726   }
8727 
8728   VTableUses.clear();
8729 }
8730 
ReadPendingInstantiations(SmallVectorImpl<std::pair<ValueDecl *,SourceLocation>> & Pending)8731 void ASTReader::ReadPendingInstantiations(
8732        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8733   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8734     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8735     SourceLocation Loc
8736       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8737 
8738     Pending.push_back(std::make_pair(D, Loc));
8739   }
8740   PendingInstantiations.clear();
8741 }
8742 
ReadLateParsedTemplates(llvm::MapVector<const FunctionDecl *,std::unique_ptr<LateParsedTemplate>> & LPTMap)8743 void ASTReader::ReadLateParsedTemplates(
8744     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8745         &LPTMap) {
8746   for (auto &LPT : LateParsedTemplates) {
8747     ModuleFile *FMod = LPT.first;
8748     RecordDataImpl &LateParsed = LPT.second;
8749     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8750          /* In loop */) {
8751       FunctionDecl *FD =
8752           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8753 
8754       auto LT = std::make_unique<LateParsedTemplate>();
8755       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8756       LT->FPO = FPOptions::getFromOpaqueInt(LateParsed[Idx++]);
8757 
8758       ModuleFile *F = getOwningModuleFile(LT->D);
8759       assert(F && "No module");
8760 
8761       unsigned TokN = LateParsed[Idx++];
8762       LT->Toks.reserve(TokN);
8763       for (unsigned T = 0; T < TokN; ++T)
8764         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8765 
8766       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8767     }
8768   }
8769 
8770   LateParsedTemplates.clear();
8771 }
8772 
AssignedLambdaNumbering(const CXXRecordDecl * Lambda)8773 void ASTReader::AssignedLambdaNumbering(const CXXRecordDecl *Lambda) {
8774   if (Lambda->getLambdaContextDecl()) {
8775     // Keep track of this lambda so it can be merged with another lambda that
8776     // is loaded later.
8777     LambdaDeclarationsForMerging.insert(
8778         {{Lambda->getLambdaContextDecl()->getCanonicalDecl(),
8779           Lambda->getLambdaIndexInContext()},
8780          const_cast<CXXRecordDecl *>(Lambda)});
8781   }
8782 }
8783 
LoadSelector(Selector Sel)8784 void ASTReader::LoadSelector(Selector Sel) {
8785   // It would be complicated to avoid reading the methods anyway. So don't.
8786   ReadMethodPool(Sel);
8787 }
8788 
SetIdentifierInfo(IdentifierID ID,IdentifierInfo * II)8789 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8790   assert(ID && "Non-zero identifier ID required");
8791   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8792   IdentifiersLoaded[ID - 1] = II;
8793   if (DeserializationListener)
8794     DeserializationListener->IdentifierRead(ID, II);
8795 }
8796 
8797 /// Set the globally-visible declarations associated with the given
8798 /// identifier.
8799 ///
8800 /// If the AST reader is currently in a state where the given declaration IDs
8801 /// cannot safely be resolved, they are queued until it is safe to resolve
8802 /// them.
8803 ///
8804 /// \param II an IdentifierInfo that refers to one or more globally-visible
8805 /// declarations.
8806 ///
8807 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8808 /// visible at global scope.
8809 ///
8810 /// \param Decls if non-null, this vector will be populated with the set of
8811 /// deserialized declarations. These declarations will not be pushed into
8812 /// scope.
8813 void
SetGloballyVisibleDecls(IdentifierInfo * II,const SmallVectorImpl<uint32_t> & DeclIDs,SmallVectorImpl<Decl * > * Decls)8814 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8815                               const SmallVectorImpl<uint32_t> &DeclIDs,
8816                                    SmallVectorImpl<Decl *> *Decls) {
8817   if (NumCurrentElementsDeserializing && !Decls) {
8818     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8819     return;
8820   }
8821 
8822   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8823     if (!SemaObj) {
8824       // Queue this declaration so that it will be added to the
8825       // translation unit scope and identifier's declaration chain
8826       // once a Sema object is known.
8827       PreloadedDeclIDs.push_back(DeclIDs[I]);
8828       continue;
8829     }
8830 
8831     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8832 
8833     // If we're simply supposed to record the declarations, do so now.
8834     if (Decls) {
8835       Decls->push_back(D);
8836       continue;
8837     }
8838 
8839     // Introduce this declaration into the translation-unit scope
8840     // and add it to the declaration chain for this identifier, so
8841     // that (unqualified) name lookup will find it.
8842     pushExternalDeclIntoScope(D, II);
8843   }
8844 }
8845 
DecodeIdentifierInfo(IdentifierID ID)8846 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8847   if (ID == 0)
8848     return nullptr;
8849 
8850   if (IdentifiersLoaded.empty()) {
8851     Error("no identifier table in AST file");
8852     return nullptr;
8853   }
8854 
8855   ID -= 1;
8856   if (!IdentifiersLoaded[ID]) {
8857     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8858     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8859     ModuleFile *M = I->second;
8860     unsigned Index = ID - M->BaseIdentifierID;
8861     const unsigned char *Data =
8862         M->IdentifierTableData + M->IdentifierOffsets[Index];
8863 
8864     ASTIdentifierLookupTrait Trait(*this, *M);
8865     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8866     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8867     auto &II = PP.getIdentifierTable().get(Key);
8868     IdentifiersLoaded[ID] = &II;
8869     markIdentifierFromAST(*this,  II);
8870     if (DeserializationListener)
8871       DeserializationListener->IdentifierRead(ID + 1, &II);
8872   }
8873 
8874   return IdentifiersLoaded[ID];
8875 }
8876 
getLocalIdentifier(ModuleFile & M,unsigned LocalID)8877 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8878   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8879 }
8880 
getGlobalIdentifierID(ModuleFile & M,unsigned LocalID)8881 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8882   if (LocalID < NUM_PREDEF_IDENT_IDS)
8883     return LocalID;
8884 
8885   if (!M.ModuleOffsetMap.empty())
8886     ReadModuleOffsetMap(M);
8887 
8888   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8889     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8890   assert(I != M.IdentifierRemap.end()
8891          && "Invalid index into identifier index remap");
8892 
8893   return LocalID + I->second;
8894 }
8895 
getMacro(MacroID ID)8896 MacroInfo *ASTReader::getMacro(MacroID ID) {
8897   if (ID == 0)
8898     return nullptr;
8899 
8900   if (MacrosLoaded.empty()) {
8901     Error("no macro table in AST file");
8902     return nullptr;
8903   }
8904 
8905   ID -= NUM_PREDEF_MACRO_IDS;
8906   if (!MacrosLoaded[ID]) {
8907     GlobalMacroMapType::iterator I
8908       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8909     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8910     ModuleFile *M = I->second;
8911     unsigned Index = ID - M->BaseMacroID;
8912     MacrosLoaded[ID] =
8913         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8914 
8915     if (DeserializationListener)
8916       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8917                                          MacrosLoaded[ID]);
8918   }
8919 
8920   return MacrosLoaded[ID];
8921 }
8922 
getGlobalMacroID(ModuleFile & M,unsigned LocalID)8923 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8924   if (LocalID < NUM_PREDEF_MACRO_IDS)
8925     return LocalID;
8926 
8927   if (!M.ModuleOffsetMap.empty())
8928     ReadModuleOffsetMap(M);
8929 
8930   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8931     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8932   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8933 
8934   return LocalID + I->second;
8935 }
8936 
8937 serialization::SubmoduleID
getGlobalSubmoduleID(ModuleFile & M,unsigned LocalID)8938 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8939   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8940     return LocalID;
8941 
8942   if (!M.ModuleOffsetMap.empty())
8943     ReadModuleOffsetMap(M);
8944 
8945   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8946     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8947   assert(I != M.SubmoduleRemap.end()
8948          && "Invalid index into submodule index remap");
8949 
8950   return LocalID + I->second;
8951 }
8952 
getSubmodule(SubmoduleID GlobalID)8953 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8954   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8955     assert(GlobalID == 0 && "Unhandled global submodule ID");
8956     return nullptr;
8957   }
8958 
8959   if (GlobalID > SubmodulesLoaded.size()) {
8960     Error("submodule ID out of range in AST file");
8961     return nullptr;
8962   }
8963 
8964   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8965 }
8966 
getModule(unsigned ID)8967 Module *ASTReader::getModule(unsigned ID) {
8968   return getSubmodule(ID);
8969 }
8970 
getLocalModuleFile(ModuleFile & M,unsigned ID)8971 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) {
8972   if (ID & 1) {
8973     // It's a module, look it up by submodule ID.
8974     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(M, ID >> 1));
8975     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8976   } else {
8977     // It's a prefix (preamble, PCH, ...). Look it up by index.
8978     unsigned IndexFromEnd = ID >> 1;
8979     assert(IndexFromEnd && "got reference to unknown module file");
8980     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8981   }
8982 }
8983 
getModuleFileID(ModuleFile * M)8984 unsigned ASTReader::getModuleFileID(ModuleFile *M) {
8985   if (!M)
8986     return 1;
8987 
8988   // For a file representing a module, use the submodule ID of the top-level
8989   // module as the file ID. For any other kind of file, the number of such
8990   // files loaded beforehand will be the same on reload.
8991   // FIXME: Is this true even if we have an explicit module file and a PCH?
8992   if (M->isModule())
8993     return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8994 
8995   auto PCHModules = getModuleManager().pch_modules();
8996   auto I = llvm::find(PCHModules, M);
8997   assert(I != PCHModules.end() && "emitting reference to unknown file");
8998   return (I - PCHModules.end()) << 1;
8999 }
9000 
getSourceDescriptor(unsigned ID)9001 std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
9002   if (Module *M = getSubmodule(ID))
9003     return ASTSourceDescriptor(*M);
9004 
9005   // If there is only a single PCH, return it instead.
9006   // Chained PCH are not supported.
9007   const auto &PCHChain = ModuleMgr.pch_modules();
9008   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
9009     ModuleFile &MF = ModuleMgr.getPrimaryModule();
9010     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
9011     StringRef FileName = llvm::sys::path::filename(MF.FileName);
9012     return ASTSourceDescriptor(ModuleName,
9013                                llvm::sys::path::parent_path(MF.FileName),
9014                                FileName, MF.Signature);
9015   }
9016   return std::nullopt;
9017 }
9018 
hasExternalDefinitions(const Decl * FD)9019 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9020   auto I = DefinitionSource.find(FD);
9021   if (I == DefinitionSource.end())
9022     return EK_ReplyHazy;
9023   return I->second ? EK_Never : EK_Always;
9024 }
9025 
getLocalSelector(ModuleFile & M,unsigned LocalID)9026 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9027   return DecodeSelector(getGlobalSelectorID(M, LocalID));
9028 }
9029 
DecodeSelector(serialization::SelectorID ID)9030 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9031   if (ID == 0)
9032     return Selector();
9033 
9034   if (ID > SelectorsLoaded.size()) {
9035     Error("selector ID out of range in AST file");
9036     return Selector();
9037   }
9038 
9039   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9040     // Load this selector from the selector table.
9041     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9042     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9043     ModuleFile &M = *I->second;
9044     ASTSelectorLookupTrait Trait(*this, M);
9045     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9046     SelectorsLoaded[ID - 1] =
9047       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9048     if (DeserializationListener)
9049       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9050   }
9051 
9052   return SelectorsLoaded[ID - 1];
9053 }
9054 
GetExternalSelector(serialization::SelectorID ID)9055 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9056   return DecodeSelector(ID);
9057 }
9058 
GetNumExternalSelectors()9059 uint32_t ASTReader::GetNumExternalSelectors() {
9060   // ID 0 (the null selector) is considered an external selector.
9061   return getTotalNumSelectors() + 1;
9062 }
9063 
9064 serialization::SelectorID
getGlobalSelectorID(ModuleFile & M,unsigned LocalID) const9065 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9066   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9067     return LocalID;
9068 
9069   if (!M.ModuleOffsetMap.empty())
9070     ReadModuleOffsetMap(M);
9071 
9072   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9073     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9074   assert(I != M.SelectorRemap.end()
9075          && "Invalid index into selector index remap");
9076 
9077   return LocalID + I->second;
9078 }
9079 
9080 DeclarationNameLoc
readDeclarationNameLoc(DeclarationName Name)9081 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9082   switch (Name.getNameKind()) {
9083   case DeclarationName::CXXConstructorName:
9084   case DeclarationName::CXXDestructorName:
9085   case DeclarationName::CXXConversionFunctionName:
9086     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
9087 
9088   case DeclarationName::CXXOperatorName:
9089     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
9090 
9091   case DeclarationName::CXXLiteralOperatorName:
9092     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9093         readSourceLocation());
9094 
9095   case DeclarationName::Identifier:
9096   case DeclarationName::ObjCZeroArgSelector:
9097   case DeclarationName::ObjCOneArgSelector:
9098   case DeclarationName::ObjCMultiArgSelector:
9099   case DeclarationName::CXXUsingDirective:
9100   case DeclarationName::CXXDeductionGuideName:
9101     break;
9102   }
9103   return DeclarationNameLoc();
9104 }
9105 
readDeclarationNameInfo()9106 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9107   DeclarationNameInfo NameInfo;
9108   NameInfo.setName(readDeclarationName());
9109   NameInfo.setLoc(readSourceLocation());
9110   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
9111   return NameInfo;
9112 }
9113 
readQualifierInfo(QualifierInfo & Info)9114 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9115   Info.QualifierLoc = readNestedNameSpecifierLoc();
9116   unsigned NumTPLists = readInt();
9117   Info.NumTemplParamLists = NumTPLists;
9118   if (NumTPLists) {
9119     Info.TemplParamLists =
9120         new (getContext()) TemplateParameterList *[NumTPLists];
9121     for (unsigned i = 0; i != NumTPLists; ++i)
9122       Info.TemplParamLists[i] = readTemplateParameterList();
9123   }
9124 }
9125 
9126 TemplateParameterList *
readTemplateParameterList()9127 ASTRecordReader::readTemplateParameterList() {
9128   SourceLocation TemplateLoc = readSourceLocation();
9129   SourceLocation LAngleLoc = readSourceLocation();
9130   SourceLocation RAngleLoc = readSourceLocation();
9131 
9132   unsigned NumParams = readInt();
9133   SmallVector<NamedDecl *, 16> Params;
9134   Params.reserve(NumParams);
9135   while (NumParams--)
9136     Params.push_back(readDeclAs<NamedDecl>());
9137 
9138   bool HasRequiresClause = readBool();
9139   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9140 
9141   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9142       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9143   return TemplateParams;
9144 }
9145 
readTemplateArgumentList(SmallVectorImpl<TemplateArgument> & TemplArgs,bool Canonicalize)9146 void ASTRecordReader::readTemplateArgumentList(
9147                         SmallVectorImpl<TemplateArgument> &TemplArgs,
9148                         bool Canonicalize) {
9149   unsigned NumTemplateArgs = readInt();
9150   TemplArgs.reserve(NumTemplateArgs);
9151   while (NumTemplateArgs--)
9152     TemplArgs.push_back(readTemplateArgument(Canonicalize));
9153 }
9154 
9155 /// Read a UnresolvedSet structure.
readUnresolvedSet(LazyASTUnresolvedSet & Set)9156 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9157   unsigned NumDecls = readInt();
9158   Set.reserve(getContext(), NumDecls);
9159   while (NumDecls--) {
9160     DeclID ID = readDeclID();
9161     AccessSpecifier AS = (AccessSpecifier) readInt();
9162     Set.addLazyDecl(getContext(), ID, AS);
9163   }
9164 }
9165 
9166 CXXBaseSpecifier
readCXXBaseSpecifier()9167 ASTRecordReader::readCXXBaseSpecifier() {
9168   bool isVirtual = readBool();
9169   bool isBaseOfClass = readBool();
9170   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9171   bool inheritConstructors = readBool();
9172   TypeSourceInfo *TInfo = readTypeSourceInfo();
9173   SourceRange Range = readSourceRange();
9174   SourceLocation EllipsisLoc = readSourceLocation();
9175   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9176                           EllipsisLoc);
9177   Result.setInheritConstructors(inheritConstructors);
9178   return Result;
9179 }
9180 
9181 CXXCtorInitializer **
readCXXCtorInitializers()9182 ASTRecordReader::readCXXCtorInitializers() {
9183   ASTContext &Context = getContext();
9184   unsigned NumInitializers = readInt();
9185   assert(NumInitializers && "wrote ctor initializers but have no inits");
9186   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9187   for (unsigned i = 0; i != NumInitializers; ++i) {
9188     TypeSourceInfo *TInfo = nullptr;
9189     bool IsBaseVirtual = false;
9190     FieldDecl *Member = nullptr;
9191     IndirectFieldDecl *IndirectMember = nullptr;
9192 
9193     CtorInitializerType Type = (CtorInitializerType) readInt();
9194     switch (Type) {
9195     case CTOR_INITIALIZER_BASE:
9196       TInfo = readTypeSourceInfo();
9197       IsBaseVirtual = readBool();
9198       break;
9199 
9200     case CTOR_INITIALIZER_DELEGATING:
9201       TInfo = readTypeSourceInfo();
9202       break;
9203 
9204      case CTOR_INITIALIZER_MEMBER:
9205       Member = readDeclAs<FieldDecl>();
9206       break;
9207 
9208      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9209       IndirectMember = readDeclAs<IndirectFieldDecl>();
9210       break;
9211     }
9212 
9213     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9214     Expr *Init = readExpr();
9215     SourceLocation LParenLoc = readSourceLocation();
9216     SourceLocation RParenLoc = readSourceLocation();
9217 
9218     CXXCtorInitializer *BOMInit;
9219     if (Type == CTOR_INITIALIZER_BASE)
9220       BOMInit = new (Context)
9221           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9222                              RParenLoc, MemberOrEllipsisLoc);
9223     else if (Type == CTOR_INITIALIZER_DELEGATING)
9224       BOMInit = new (Context)
9225           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9226     else if (Member)
9227       BOMInit = new (Context)
9228           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9229                              Init, RParenLoc);
9230     else
9231       BOMInit = new (Context)
9232           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9233                              LParenLoc, Init, RParenLoc);
9234 
9235     if (/*IsWritten*/readBool()) {
9236       unsigned SourceOrder = readInt();
9237       BOMInit->setSourceOrder(SourceOrder);
9238     }
9239 
9240     CtorInitializers[i] = BOMInit;
9241   }
9242 
9243   return CtorInitializers;
9244 }
9245 
9246 NestedNameSpecifierLoc
readNestedNameSpecifierLoc()9247 ASTRecordReader::readNestedNameSpecifierLoc() {
9248   ASTContext &Context = getContext();
9249   unsigned N = readInt();
9250   NestedNameSpecifierLocBuilder Builder;
9251   for (unsigned I = 0; I != N; ++I) {
9252     auto Kind = readNestedNameSpecifierKind();
9253     switch (Kind) {
9254     case NestedNameSpecifier::Identifier: {
9255       IdentifierInfo *II = readIdentifier();
9256       SourceRange Range = readSourceRange();
9257       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9258       break;
9259     }
9260 
9261     case NestedNameSpecifier::Namespace: {
9262       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9263       SourceRange Range = readSourceRange();
9264       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9265       break;
9266     }
9267 
9268     case NestedNameSpecifier::NamespaceAlias: {
9269       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
9270       SourceRange Range = readSourceRange();
9271       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9272       break;
9273     }
9274 
9275     case NestedNameSpecifier::TypeSpec:
9276     case NestedNameSpecifier::TypeSpecWithTemplate: {
9277       bool Template = readBool();
9278       TypeSourceInfo *T = readTypeSourceInfo();
9279       if (!T)
9280         return NestedNameSpecifierLoc();
9281       SourceLocation ColonColonLoc = readSourceLocation();
9282 
9283       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9284       Builder.Extend(Context,
9285                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9286                      T->getTypeLoc(), ColonColonLoc);
9287       break;
9288     }
9289 
9290     case NestedNameSpecifier::Global: {
9291       SourceLocation ColonColonLoc = readSourceLocation();
9292       Builder.MakeGlobal(Context, ColonColonLoc);
9293       break;
9294     }
9295 
9296     case NestedNameSpecifier::Super: {
9297       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
9298       SourceRange Range = readSourceRange();
9299       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9300       break;
9301     }
9302     }
9303   }
9304 
9305   return Builder.getWithLocInContext(Context);
9306 }
9307 
ReadSourceRange(ModuleFile & F,const RecordData & Record,unsigned & Idx,LocSeq * Seq)9308 SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9309                                        unsigned &Idx, LocSeq *Seq) {
9310   SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq);
9311   SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq);
9312   return SourceRange(beg, end);
9313 }
9314 
9315 /// Read a floating-point value
readAPFloat(const llvm::fltSemantics & Sem)9316 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9317   return llvm::APFloat(Sem, readAPInt());
9318 }
9319 
9320 // Read a string
ReadString(const RecordDataImpl & Record,unsigned & Idx)9321 std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9322   unsigned Len = Record[Idx++];
9323   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9324   Idx += Len;
9325   return Result;
9326 }
9327 
ReadPath(ModuleFile & F,const RecordData & Record,unsigned & Idx)9328 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9329                                 unsigned &Idx) {
9330   std::string Filename = ReadString(Record, Idx);
9331   ResolveImportedPath(F, Filename);
9332   return Filename;
9333 }
9334 
ReadPath(StringRef BaseDirectory,const RecordData & Record,unsigned & Idx)9335 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9336                                 const RecordData &Record, unsigned &Idx) {
9337   std::string Filename = ReadString(Record, Idx);
9338   if (!BaseDirectory.empty())
9339     ResolveImportedPath(Filename, BaseDirectory);
9340   return Filename;
9341 }
9342 
ReadVersionTuple(const RecordData & Record,unsigned & Idx)9343 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9344                                          unsigned &Idx) {
9345   unsigned Major = Record[Idx++];
9346   unsigned Minor = Record[Idx++];
9347   unsigned Subminor = Record[Idx++];
9348   if (Minor == 0)
9349     return VersionTuple(Major);
9350   if (Subminor == 0)
9351     return VersionTuple(Major, Minor - 1);
9352   return VersionTuple(Major, Minor - 1, Subminor - 1);
9353 }
9354 
ReadCXXTemporary(ModuleFile & F,const RecordData & Record,unsigned & Idx)9355 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9356                                           const RecordData &Record,
9357                                           unsigned &Idx) {
9358   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9359   return CXXTemporary::Create(getContext(), Decl);
9360 }
9361 
Diag(unsigned DiagID) const9362 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9363   return Diag(CurrentImportLoc, DiagID);
9364 }
9365 
Diag(SourceLocation Loc,unsigned DiagID) const9366 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9367   return Diags.Report(Loc, DiagID);
9368 }
9369 
9370 /// Retrieve the identifier table associated with the
9371 /// preprocessor.
getIdentifierTable()9372 IdentifierTable &ASTReader::getIdentifierTable() {
9373   return PP.getIdentifierTable();
9374 }
9375 
9376 /// Record that the given ID maps to the given switch-case
9377 /// statement.
RecordSwitchCaseID(SwitchCase * SC,unsigned ID)9378 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9379   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9380          "Already have a SwitchCase with this ID");
9381   (*CurrSwitchCaseStmts)[ID] = SC;
9382 }
9383 
9384 /// Retrieve the switch-case statement with the given ID.
getSwitchCaseWithID(unsigned ID)9385 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9386   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9387   return (*CurrSwitchCaseStmts)[ID];
9388 }
9389 
ClearSwitchCaseIDs()9390 void ASTReader::ClearSwitchCaseIDs() {
9391   CurrSwitchCaseStmts->clear();
9392 }
9393 
ReadComments()9394 void ASTReader::ReadComments() {
9395   ASTContext &Context = getContext();
9396   std::vector<RawComment *> Comments;
9397   for (SmallVectorImpl<std::pair<BitstreamCursor,
9398                                  serialization::ModuleFile *>>::iterator
9399        I = CommentsCursors.begin(),
9400        E = CommentsCursors.end();
9401        I != E; ++I) {
9402     Comments.clear();
9403     BitstreamCursor &Cursor = I->first;
9404     serialization::ModuleFile &F = *I->second;
9405     SavedStreamPosition SavedPosition(Cursor);
9406 
9407     RecordData Record;
9408     while (true) {
9409       Expected<llvm::BitstreamEntry> MaybeEntry =
9410           Cursor.advanceSkippingSubblocks(
9411               BitstreamCursor::AF_DontPopBlockAtEnd);
9412       if (!MaybeEntry) {
9413         Error(MaybeEntry.takeError());
9414         return;
9415       }
9416       llvm::BitstreamEntry Entry = MaybeEntry.get();
9417 
9418       switch (Entry.Kind) {
9419       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9420       case llvm::BitstreamEntry::Error:
9421         Error("malformed block record in AST file");
9422         return;
9423       case llvm::BitstreamEntry::EndBlock:
9424         goto NextCursor;
9425       case llvm::BitstreamEntry::Record:
9426         // The interesting case.
9427         break;
9428       }
9429 
9430       // Read a record.
9431       Record.clear();
9432       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9433       if (!MaybeComment) {
9434         Error(MaybeComment.takeError());
9435         return;
9436       }
9437       switch ((CommentRecordTypes)MaybeComment.get()) {
9438       case COMMENTS_RAW_COMMENT: {
9439         unsigned Idx = 0;
9440         SourceRange SR = ReadSourceRange(F, Record, Idx);
9441         RawComment::CommentKind Kind =
9442             (RawComment::CommentKind) Record[Idx++];
9443         bool IsTrailingComment = Record[Idx++];
9444         bool IsAlmostTrailingComment = Record[Idx++];
9445         Comments.push_back(new (Context) RawComment(
9446             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9447         break;
9448       }
9449       }
9450     }
9451   NextCursor:
9452     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9453         FileToOffsetToComment;
9454     for (RawComment *C : Comments) {
9455       SourceLocation CommentLoc = C->getBeginLoc();
9456       if (CommentLoc.isValid()) {
9457         std::pair<FileID, unsigned> Loc =
9458             SourceMgr.getDecomposedLoc(CommentLoc);
9459         if (Loc.first.isValid())
9460           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9461       }
9462     }
9463   }
9464 }
9465 
visitInputFileInfos(serialization::ModuleFile & MF,bool IncludeSystem,llvm::function_ref<void (const serialization::InputFileInfo & IFI,bool IsSystem)> Visitor)9466 void ASTReader::visitInputFileInfos(
9467     serialization::ModuleFile &MF, bool IncludeSystem,
9468     llvm::function_ref<void(const serialization::InputFileInfo &IFI,
9469                             bool IsSystem)>
9470         Visitor) {
9471   unsigned NumUserInputs = MF.NumUserInputFiles;
9472   unsigned NumInputs = MF.InputFilesLoaded.size();
9473   assert(NumUserInputs <= NumInputs);
9474   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9475   for (unsigned I = 0; I < N; ++I) {
9476     bool IsSystem = I >= NumUserInputs;
9477     InputFileInfo IFI = getInputFileInfo(MF, I+1);
9478     Visitor(IFI, IsSystem);
9479   }
9480 }
9481 
visitInputFiles(serialization::ModuleFile & MF,bool IncludeSystem,bool Complain,llvm::function_ref<void (const serialization::InputFile & IF,bool isSystem)> Visitor)9482 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9483                                 bool IncludeSystem, bool Complain,
9484                     llvm::function_ref<void(const serialization::InputFile &IF,
9485                                             bool isSystem)> Visitor) {
9486   unsigned NumUserInputs = MF.NumUserInputFiles;
9487   unsigned NumInputs = MF.InputFilesLoaded.size();
9488   assert(NumUserInputs <= NumInputs);
9489   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9490   for (unsigned I = 0; I < N; ++I) {
9491     bool IsSystem = I >= NumUserInputs;
9492     InputFile IF = getInputFile(MF, I+1, Complain);
9493     Visitor(IF, IsSystem);
9494   }
9495 }
9496 
visitTopLevelModuleMaps(serialization::ModuleFile & MF,llvm::function_ref<void (FileEntryRef FE)> Visitor)9497 void ASTReader::visitTopLevelModuleMaps(
9498     serialization::ModuleFile &MF,
9499     llvm::function_ref<void(FileEntryRef FE)> Visitor) {
9500   unsigned NumInputs = MF.InputFilesLoaded.size();
9501   for (unsigned I = 0; I < NumInputs; ++I) {
9502     InputFileInfo IFI = getInputFileInfo(MF, I + 1);
9503     if (IFI.TopLevel && IFI.ModuleMap)
9504       if (auto FE = getInputFile(MF, I + 1).getFile())
9505         Visitor(*FE);
9506   }
9507 }
9508 
finishPendingActions()9509 void ASTReader::finishPendingActions() {
9510   while (
9511       !PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
9512       !PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
9513       !PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
9514       !PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
9515       !PendingObjCExtensionIvarRedeclarations.empty()) {
9516     // If any identifiers with corresponding top-level declarations have
9517     // been loaded, load those declarations now.
9518     using TopLevelDeclsMap =
9519         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9520     TopLevelDeclsMap TopLevelDecls;
9521 
9522     while (!PendingIdentifierInfos.empty()) {
9523       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9524       SmallVector<uint32_t, 4> DeclIDs =
9525           std::move(PendingIdentifierInfos.back().second);
9526       PendingIdentifierInfos.pop_back();
9527 
9528       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9529     }
9530 
9531     // Load each function type that we deferred loading because it was a
9532     // deduced type that might refer to a local type declared within itself.
9533     for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
9534       auto *FD = PendingDeducedFunctionTypes[I].first;
9535       FD->setType(GetType(PendingDeducedFunctionTypes[I].second));
9536 
9537       if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
9538         // If we gave a function a deduced return type, remember that we need to
9539         // propagate that along the redeclaration chain.
9540         if (DT->isDeduced()) {
9541           PendingDeducedTypeUpdates.insert(
9542               {FD->getCanonicalDecl(), FD->getReturnType()});
9543           continue;
9544         }
9545 
9546         // The function has undeduced DeduceType return type. We hope we can
9547         // find the deduced type by iterating the redecls in other modules
9548         // later.
9549         PendingUndeducedFunctionDecls.push_back(FD);
9550         continue;
9551       }
9552     }
9553     PendingDeducedFunctionTypes.clear();
9554 
9555     // Load each variable type that we deferred loading because it was a
9556     // deduced type that might refer to a local type declared within itself.
9557     for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
9558       auto *VD = PendingDeducedVarTypes[I].first;
9559       VD->setType(GetType(PendingDeducedVarTypes[I].second));
9560     }
9561     PendingDeducedVarTypes.clear();
9562 
9563     // For each decl chain that we wanted to complete while deserializing, mark
9564     // it as "still needs to be completed".
9565     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9566       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9567     }
9568     PendingIncompleteDeclChains.clear();
9569 
9570     // Load pending declaration chains.
9571     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9572       loadPendingDeclChain(PendingDeclChains[I].first,
9573                            PendingDeclChains[I].second);
9574     PendingDeclChains.clear();
9575 
9576     // Make the most recent of the top-level declarations visible.
9577     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9578            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9579       IdentifierInfo *II = TLD->first;
9580       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9581         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9582       }
9583     }
9584 
9585     // Load any pending macro definitions.
9586     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9587       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9588       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9589       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9590       // Initialize the macro history from chained-PCHs ahead of module imports.
9591       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9592            ++IDIdx) {
9593         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9594         if (!Info.M->isModule())
9595           resolvePendingMacro(II, Info);
9596       }
9597       // Handle module imports.
9598       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9599            ++IDIdx) {
9600         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9601         if (Info.M->isModule())
9602           resolvePendingMacro(II, Info);
9603       }
9604     }
9605     PendingMacroIDs.clear();
9606 
9607     // Wire up the DeclContexts for Decls that we delayed setting until
9608     // recursive loading is completed.
9609     while (!PendingDeclContextInfos.empty()) {
9610       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9611       PendingDeclContextInfos.pop_front();
9612       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9613       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9614       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9615     }
9616 
9617     // Perform any pending declaration updates.
9618     while (!PendingUpdateRecords.empty()) {
9619       auto Update = PendingUpdateRecords.pop_back_val();
9620       ReadingKindTracker ReadingKind(Read_Decl, *this);
9621       loadDeclUpdateRecords(Update);
9622     }
9623 
9624     while (!PendingObjCExtensionIvarRedeclarations.empty()) {
9625       auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
9626       auto DuplicateIvars =
9627           PendingObjCExtensionIvarRedeclarations.back().second;
9628       llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
9629       StructuralEquivalenceContext Ctx(
9630           ExtensionsPair.first->getASTContext(),
9631           ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
9632           StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
9633           /*Complain =*/false,
9634           /*ErrorOnTagTypeMismatch =*/true);
9635       if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {
9636         // Merge redeclared ivars with their predecessors.
9637         for (auto IvarPair : DuplicateIvars) {
9638           ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
9639           // Change semantic DeclContext but keep the lexical one.
9640           Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(),
9641                                     Ivar->getLexicalDeclContext(),
9642                                     getContext());
9643           getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());
9644         }
9645         // Invalidate duplicate extension and the cached ivar list.
9646         ExtensionsPair.first->setInvalidDecl();
9647         ExtensionsPair.second->getClassInterface()
9648             ->getDefinition()
9649             ->setIvarList(nullptr);
9650       } else {
9651         for (auto IvarPair : DuplicateIvars) {
9652           Diag(IvarPair.first->getLocation(),
9653                diag::err_duplicate_ivar_declaration)
9654               << IvarPair.first->getIdentifier();
9655           Diag(IvarPair.second->getLocation(), diag::note_previous_definition);
9656         }
9657       }
9658       PendingObjCExtensionIvarRedeclarations.pop_back();
9659     }
9660   }
9661 
9662   // At this point, all update records for loaded decls are in place, so any
9663   // fake class definitions should have become real.
9664   assert(PendingFakeDefinitionData.empty() &&
9665          "faked up a class definition but never saw the real one");
9666 
9667   // If we deserialized any C++ or Objective-C class definitions, any
9668   // Objective-C protocol definitions, or any redeclarable templates, make sure
9669   // that all redeclarations point to the definitions. Note that this can only
9670   // happen now, after the redeclaration chains have been fully wired.
9671   for (Decl *D : PendingDefinitions) {
9672     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9673       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9674         // Make sure that the TagType points at the definition.
9675         const_cast<TagType*>(TagT)->decl = TD;
9676       }
9677 
9678       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9679         for (auto *R = getMostRecentExistingDecl(RD); R;
9680              R = R->getPreviousDecl()) {
9681           assert((R == D) ==
9682                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9683                  "declaration thinks it's the definition but it isn't");
9684           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9685         }
9686       }
9687 
9688       continue;
9689     }
9690 
9691     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9692       // Make sure that the ObjCInterfaceType points at the definition.
9693       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9694         ->Decl = ID;
9695 
9696       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9697         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9698 
9699       continue;
9700     }
9701 
9702     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9703       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9704         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9705 
9706       continue;
9707     }
9708 
9709     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9710     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9711       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9712   }
9713   PendingDefinitions.clear();
9714 
9715   // Load the bodies of any functions or methods we've encountered. We do
9716   // this now (delayed) so that we can be sure that the declaration chains
9717   // have been fully wired up (hasBody relies on this).
9718   // FIXME: We shouldn't require complete redeclaration chains here.
9719   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9720                                PBEnd = PendingBodies.end();
9721        PB != PBEnd; ++PB) {
9722     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9723       // For a function defined inline within a class template, force the
9724       // canonical definition to be the one inside the canonical definition of
9725       // the template. This ensures that we instantiate from a correct view
9726       // of the template.
9727       //
9728       // Sadly we can't do this more generally: we can't be sure that all
9729       // copies of an arbitrary class definition will have the same members
9730       // defined (eg, some member functions may not be instantiated, and some
9731       // special members may or may not have been implicitly defined).
9732       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9733         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9734           continue;
9735 
9736       // FIXME: Check for =delete/=default?
9737       const FunctionDecl *Defn = nullptr;
9738       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9739         FD->setLazyBody(PB->second);
9740       } else {
9741         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9742         mergeDefinitionVisibility(NonConstDefn, FD);
9743 
9744         if (!FD->isLateTemplateParsed() &&
9745             !NonConstDefn->isLateTemplateParsed() &&
9746             // We only perform ODR checks for decls not in the explicit
9747             // global module fragment.
9748             !FD->shouldSkipCheckingODR() &&
9749             FD->getODRHash() != NonConstDefn->getODRHash()) {
9750           if (!isa<CXXMethodDecl>(FD)) {
9751             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9752           } else if (FD->getLexicalParent()->isFileContext() &&
9753                      NonConstDefn->getLexicalParent()->isFileContext()) {
9754             // Only diagnose out-of-line method definitions.  If they are
9755             // in class definitions, then an error will be generated when
9756             // processing the class bodies.
9757             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9758           }
9759         }
9760       }
9761       continue;
9762     }
9763 
9764     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9765     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9766       MD->setLazyBody(PB->second);
9767   }
9768   PendingBodies.clear();
9769 
9770   // Inform any classes that had members added that they now have more members.
9771   for (auto [RD, MD] : PendingAddedClassMembers) {
9772     RD->addedMember(MD);
9773   }
9774   PendingAddedClassMembers.clear();
9775 
9776   // Do some cleanup.
9777   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9778     getContext().deduplicateMergedDefinitonsFor(ND);
9779   PendingMergedDefinitionsToDeduplicate.clear();
9780 }
9781 
diagnoseOdrViolations()9782 void ASTReader::diagnoseOdrViolations() {
9783   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9784       PendingRecordOdrMergeFailures.empty() &&
9785       PendingFunctionOdrMergeFailures.empty() &&
9786       PendingEnumOdrMergeFailures.empty() &&
9787       PendingObjCInterfaceOdrMergeFailures.empty() &&
9788       PendingObjCProtocolOdrMergeFailures.empty())
9789     return;
9790 
9791   // Trigger the import of the full definition of each class that had any
9792   // odr-merging problems, so we can produce better diagnostics for them.
9793   // These updates may in turn find and diagnose some ODR failures, so take
9794   // ownership of the set first.
9795   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9796   PendingOdrMergeFailures.clear();
9797   for (auto &Merge : OdrMergeFailures) {
9798     Merge.first->buildLookup();
9799     Merge.first->decls_begin();
9800     Merge.first->bases_begin();
9801     Merge.first->vbases_begin();
9802     for (auto &RecordPair : Merge.second) {
9803       auto *RD = RecordPair.first;
9804       RD->decls_begin();
9805       RD->bases_begin();
9806       RD->vbases_begin();
9807     }
9808   }
9809 
9810   // Trigger the import of the full definition of each record in C/ObjC.
9811   auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
9812   PendingRecordOdrMergeFailures.clear();
9813   for (auto &Merge : RecordOdrMergeFailures) {
9814     Merge.first->decls_begin();
9815     for (auto &D : Merge.second)
9816       D->decls_begin();
9817   }
9818 
9819   // Trigger the import of the full interface definition.
9820   auto ObjCInterfaceOdrMergeFailures =
9821       std::move(PendingObjCInterfaceOdrMergeFailures);
9822   PendingObjCInterfaceOdrMergeFailures.clear();
9823   for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
9824     Merge.first->decls_begin();
9825     for (auto &InterfacePair : Merge.second)
9826       InterfacePair.first->decls_begin();
9827   }
9828 
9829   // Trigger the import of functions.
9830   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9831   PendingFunctionOdrMergeFailures.clear();
9832   for (auto &Merge : FunctionOdrMergeFailures) {
9833     Merge.first->buildLookup();
9834     Merge.first->decls_begin();
9835     Merge.first->getBody();
9836     for (auto &FD : Merge.second) {
9837       FD->buildLookup();
9838       FD->decls_begin();
9839       FD->getBody();
9840     }
9841   }
9842 
9843   // Trigger the import of enums.
9844   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9845   PendingEnumOdrMergeFailures.clear();
9846   for (auto &Merge : EnumOdrMergeFailures) {
9847     Merge.first->decls_begin();
9848     for (auto &Enum : Merge.second) {
9849       Enum->decls_begin();
9850     }
9851   }
9852 
9853   // Trigger the import of the full protocol definition.
9854   auto ObjCProtocolOdrMergeFailures =
9855       std::move(PendingObjCProtocolOdrMergeFailures);
9856   PendingObjCProtocolOdrMergeFailures.clear();
9857   for (auto &Merge : ObjCProtocolOdrMergeFailures) {
9858     Merge.first->decls_begin();
9859     for (auto &ProtocolPair : Merge.second)
9860       ProtocolPair.first->decls_begin();
9861   }
9862 
9863   // For each declaration from a merged context, check that the canonical
9864   // definition of that context also contains a declaration of the same
9865   // entity.
9866   //
9867   // Caution: this loop does things that might invalidate iterators into
9868   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9869   while (!PendingOdrMergeChecks.empty()) {
9870     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9871 
9872     // FIXME: Skip over implicit declarations for now. This matters for things
9873     // like implicitly-declared special member functions. This isn't entirely
9874     // correct; we can end up with multiple unmerged declarations of the same
9875     // implicit entity.
9876     if (D->isImplicit())
9877       continue;
9878 
9879     DeclContext *CanonDef = D->getDeclContext();
9880 
9881     bool Found = false;
9882     const Decl *DCanon = D->getCanonicalDecl();
9883 
9884     for (auto *RI : D->redecls()) {
9885       if (RI->getLexicalDeclContext() == CanonDef) {
9886         Found = true;
9887         break;
9888       }
9889     }
9890     if (Found)
9891       continue;
9892 
9893     // Quick check failed, time to do the slow thing. Note, we can't just
9894     // look up the name of D in CanonDef here, because the member that is
9895     // in CanonDef might not be found by name lookup (it might have been
9896     // replaced by a more recent declaration in the lookup table), and we
9897     // can't necessarily find it in the redeclaration chain because it might
9898     // be merely mergeable, not redeclarable.
9899     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9900     for (auto *CanonMember : CanonDef->decls()) {
9901       if (CanonMember->getCanonicalDecl() == DCanon) {
9902         // This can happen if the declaration is merely mergeable and not
9903         // actually redeclarable (we looked for redeclarations earlier).
9904         //
9905         // FIXME: We should be able to detect this more efficiently, without
9906         // pulling in all of the members of CanonDef.
9907         Found = true;
9908         break;
9909       }
9910       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9911         if (ND->getDeclName() == D->getDeclName())
9912           Candidates.push_back(ND);
9913     }
9914 
9915     if (!Found) {
9916       // The AST doesn't like TagDecls becoming invalid after they've been
9917       // completed. We only really need to mark FieldDecls as invalid here.
9918       if (!isa<TagDecl>(D))
9919         D->setInvalidDecl();
9920 
9921       // Ensure we don't accidentally recursively enter deserialization while
9922       // we're producing our diagnostic.
9923       Deserializing RecursionGuard(this);
9924 
9925       std::string CanonDefModule =
9926           ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
9927               cast<Decl>(CanonDef));
9928       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9929         << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
9930         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9931 
9932       if (Candidates.empty())
9933         Diag(cast<Decl>(CanonDef)->getLocation(),
9934              diag::note_module_odr_violation_no_possible_decls) << D;
9935       else {
9936         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9937           Diag(Candidates[I]->getLocation(),
9938                diag::note_module_odr_violation_possible_decl)
9939             << Candidates[I];
9940       }
9941 
9942       DiagnosedOdrMergeFailures.insert(CanonDef);
9943     }
9944   }
9945 
9946   if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
9947       FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
9948       ObjCInterfaceOdrMergeFailures.empty() &&
9949       ObjCProtocolOdrMergeFailures.empty())
9950     return;
9951 
9952   ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
9953                                getPreprocessor().getLangOpts());
9954 
9955   // Issue any pending ODR-failure diagnostics.
9956   for (auto &Merge : OdrMergeFailures) {
9957     // If we've already pointed out a specific problem with this class, don't
9958     // bother issuing a general "something's different" diagnostic.
9959     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9960       continue;
9961 
9962     bool Diagnosed = false;
9963     CXXRecordDecl *FirstRecord = Merge.first;
9964     for (auto &RecordPair : Merge.second) {
9965       if (DiagsEmitter.diagnoseMismatch(FirstRecord, RecordPair.first,
9966                                         RecordPair.second)) {
9967         Diagnosed = true;
9968         break;
9969       }
9970     }
9971 
9972     if (!Diagnosed) {
9973       // All definitions are updates to the same declaration. This happens if a
9974       // module instantiates the declaration of a class template specialization
9975       // and two or more other modules instantiate its definition.
9976       //
9977       // FIXME: Indicate which modules had instantiations of this definition.
9978       // FIXME: How can this even happen?
9979       Diag(Merge.first->getLocation(),
9980            diag::err_module_odr_violation_different_instantiations)
9981           << Merge.first;
9982     }
9983   }
9984 
9985   // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
9986   // that in C++ this is done as a part of CXXRecordDecl ODR checking.
9987   for (auto &Merge : RecordOdrMergeFailures) {
9988     // If we've already pointed out a specific problem with this class, don't
9989     // bother issuing a general "something's different" diagnostic.
9990     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9991       continue;
9992 
9993     RecordDecl *FirstRecord = Merge.first;
9994     bool Diagnosed = false;
9995     for (auto *SecondRecord : Merge.second) {
9996       if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
9997         Diagnosed = true;
9998         break;
9999       }
10000     }
10001     (void)Diagnosed;
10002     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10003   }
10004 
10005   // Issue ODR failures diagnostics for functions.
10006   for (auto &Merge : FunctionOdrMergeFailures) {
10007     FunctionDecl *FirstFunction = Merge.first;
10008     bool Diagnosed = false;
10009     for (auto &SecondFunction : Merge.second) {
10010       if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
10011         Diagnosed = true;
10012         break;
10013       }
10014     }
10015     (void)Diagnosed;
10016     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10017   }
10018 
10019   // Issue ODR failures diagnostics for enums.
10020   for (auto &Merge : EnumOdrMergeFailures) {
10021     // If we've already pointed out a specific problem with this enum, don't
10022     // bother issuing a general "something's different" diagnostic.
10023     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10024       continue;
10025 
10026     EnumDecl *FirstEnum = Merge.first;
10027     bool Diagnosed = false;
10028     for (auto &SecondEnum : Merge.second) {
10029       if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
10030         Diagnosed = true;
10031         break;
10032       }
10033     }
10034     (void)Diagnosed;
10035     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10036   }
10037 
10038   for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10039     // If we've already pointed out a specific problem with this interface,
10040     // don't bother issuing a general "something's different" diagnostic.
10041     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10042       continue;
10043 
10044     bool Diagnosed = false;
10045     ObjCInterfaceDecl *FirstID = Merge.first;
10046     for (auto &InterfacePair : Merge.second) {
10047       if (DiagsEmitter.diagnoseMismatch(FirstID, InterfacePair.first,
10048                                         InterfacePair.second)) {
10049         Diagnosed = true;
10050         break;
10051       }
10052     }
10053     (void)Diagnosed;
10054     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10055   }
10056 
10057   for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10058     // If we've already pointed out a specific problem with this protocol,
10059     // don't bother issuing a general "something's different" diagnostic.
10060     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10061       continue;
10062 
10063     ObjCProtocolDecl *FirstProtocol = Merge.first;
10064     bool Diagnosed = false;
10065     for (auto &ProtocolPair : Merge.second) {
10066       if (DiagsEmitter.diagnoseMismatch(FirstProtocol, ProtocolPair.first,
10067                                         ProtocolPair.second)) {
10068         Diagnosed = true;
10069         break;
10070       }
10071     }
10072     (void)Diagnosed;
10073     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10074   }
10075 }
10076 
StartedDeserializing()10077 void ASTReader::StartedDeserializing() {
10078   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10079     ReadTimer->startTimer();
10080 }
10081 
FinishedDeserializing()10082 void ASTReader::FinishedDeserializing() {
10083   assert(NumCurrentElementsDeserializing &&
10084          "FinishedDeserializing not paired with StartedDeserializing");
10085   if (NumCurrentElementsDeserializing == 1) {
10086     // We decrease NumCurrentElementsDeserializing only after pending actions
10087     // are finished, to avoid recursively re-calling finishPendingActions().
10088     finishPendingActions();
10089   }
10090   --NumCurrentElementsDeserializing;
10091 
10092   if (NumCurrentElementsDeserializing == 0) {
10093     // Propagate exception specification and deduced type updates along
10094     // redeclaration chains.
10095     //
10096     // We do this now rather than in finishPendingActions because we want to
10097     // be able to walk the complete redeclaration chains of the updated decls.
10098     while (!PendingExceptionSpecUpdates.empty() ||
10099            !PendingDeducedTypeUpdates.empty()) {
10100       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10101       PendingExceptionSpecUpdates.clear();
10102       for (auto Update : ESUpdates) {
10103         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10104         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10105         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10106         if (auto *Listener = getContext().getASTMutationListener())
10107           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
10108         for (auto *Redecl : Update.second->redecls())
10109           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10110       }
10111 
10112       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10113       PendingDeducedTypeUpdates.clear();
10114       for (auto Update : DTUpdates) {
10115         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10116         // FIXME: If the return type is already deduced, check that it matches.
10117         getContext().adjustDeducedFunctionResultType(Update.first,
10118                                                      Update.second);
10119       }
10120 
10121       auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10122       PendingUndeducedFunctionDecls.clear();
10123       // We hope we can find the deduced type for the functions by iterating
10124       // redeclarations in other modules.
10125       for (FunctionDecl *UndeducedFD : UDTUpdates)
10126         (void)UndeducedFD->getMostRecentDecl();
10127     }
10128 
10129     if (ReadTimer)
10130       ReadTimer->stopTimer();
10131 
10132     diagnoseOdrViolations();
10133 
10134     // We are not in recursive loading, so it's safe to pass the "interesting"
10135     // decls to the consumer.
10136     if (Consumer)
10137       PassInterestingDeclsToConsumer();
10138   }
10139 }
10140 
pushExternalDeclIntoScope(NamedDecl * D,DeclarationName Name)10141 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10142   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10143     // Remove any fake results before adding any real ones.
10144     auto It = PendingFakeLookupResults.find(II);
10145     if (It != PendingFakeLookupResults.end()) {
10146       for (auto *ND : It->second)
10147         SemaObj->IdResolver.RemoveDecl(ND);
10148       // FIXME: this works around module+PCH performance issue.
10149       // Rather than erase the result from the map, which is O(n), just clear
10150       // the vector of NamedDecls.
10151       It->second.clear();
10152     }
10153   }
10154 
10155   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10156     SemaObj->TUScope->AddDecl(D);
10157   } else if (SemaObj->TUScope) {
10158     // Adding the decl to IdResolver may have failed because it was already in
10159     // (even though it was not added in scope). If it is already in, make sure
10160     // it gets in the scope as well.
10161     if (llvm::is_contained(SemaObj->IdResolver.decls(Name), D))
10162       SemaObj->TUScope->AddDecl(D);
10163   }
10164 }
10165 
ASTReader(Preprocessor & PP,InMemoryModuleCache & ModuleCache,ASTContext * Context,const PCHContainerReader & PCHContainerRdr,ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,StringRef isysroot,DisableValidationForModuleKind DisableValidationKind,bool AllowASTWithCompilerErrors,bool AllowConfigurationMismatch,bool ValidateSystemInputs,bool ValidateASTInputFilesContent,bool UseGlobalIndex,std::unique_ptr<llvm::Timer> ReadTimer)10166 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10167                      ASTContext *Context,
10168                      const PCHContainerReader &PCHContainerRdr,
10169                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10170                      StringRef isysroot,
10171                      DisableValidationForModuleKind DisableValidationKind,
10172                      bool AllowASTWithCompilerErrors,
10173                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10174                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10175                      std::unique_ptr<llvm::Timer> ReadTimer)
10176     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
10177                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
10178                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
10179       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
10180       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
10181       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
10182                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
10183       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
10184       DisableValidationKind(DisableValidationKind),
10185       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
10186       AllowConfigurationMismatch(AllowConfigurationMismatch),
10187       ValidateSystemInputs(ValidateSystemInputs),
10188       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
10189       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10190   SourceMgr.setExternalSLocEntrySource(this);
10191 
10192   for (const auto &Ext : Extensions) {
10193     auto BlockName = Ext->getExtensionMetadata().BlockName;
10194     auto Known = ModuleFileExtensions.find(BlockName);
10195     if (Known != ModuleFileExtensions.end()) {
10196       Diags.Report(diag::warn_duplicate_module_file_extension)
10197         << BlockName;
10198       continue;
10199     }
10200 
10201     ModuleFileExtensions.insert({BlockName, Ext});
10202   }
10203 }
10204 
~ASTReader()10205 ASTReader::~ASTReader() {
10206   if (OwnsDeserializationListener)
10207     delete DeserializationListener;
10208 }
10209 
getIdResolver()10210 IdentifierResolver &ASTReader::getIdResolver() {
10211   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
10212 }
10213 
readRecord(llvm::BitstreamCursor & Cursor,unsigned AbbrevID)10214 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
10215                                                unsigned AbbrevID) {
10216   Idx = 0;
10217   Record.clear();
10218   return Cursor.readRecord(AbbrevID, Record);
10219 }
10220 //===----------------------------------------------------------------------===//
10221 //// OMPClauseReader implementation
10222 ////===----------------------------------------------------------------------===//
10223 
10224 // This has to be in namespace clang because it's friended by all
10225 // of the OMP clauses.
10226 namespace clang {
10227 
10228 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
10229   ASTRecordReader &Record;
10230   ASTContext &Context;
10231 
10232 public:
OMPClauseReader(ASTRecordReader & Record)10233   OMPClauseReader(ASTRecordReader &Record)
10234       : Record(Record), Context(Record.getContext()) {}
10235 #define GEN_CLANG_CLAUSE_CLASS
10236 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
10237 #include "llvm/Frontend/OpenMP/OMP.inc"
10238   OMPClause *readClause();
10239   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
10240   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
10241 };
10242 
10243 } // end namespace clang
10244 
readOMPClause()10245 OMPClause *ASTRecordReader::readOMPClause() {
10246   return OMPClauseReader(*this).readClause();
10247 }
10248 
readClause()10249 OMPClause *OMPClauseReader::readClause() {
10250   OMPClause *C = nullptr;
10251   switch (llvm::omp::Clause(Record.readInt())) {
10252   case llvm::omp::OMPC_if:
10253     C = new (Context) OMPIfClause();
10254     break;
10255   case llvm::omp::OMPC_final:
10256     C = new (Context) OMPFinalClause();
10257     break;
10258   case llvm::omp::OMPC_num_threads:
10259     C = new (Context) OMPNumThreadsClause();
10260     break;
10261   case llvm::omp::OMPC_safelen:
10262     C = new (Context) OMPSafelenClause();
10263     break;
10264   case llvm::omp::OMPC_simdlen:
10265     C = new (Context) OMPSimdlenClause();
10266     break;
10267   case llvm::omp::OMPC_sizes: {
10268     unsigned NumSizes = Record.readInt();
10269     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
10270     break;
10271   }
10272   case llvm::omp::OMPC_full:
10273     C = OMPFullClause::CreateEmpty(Context);
10274     break;
10275   case llvm::omp::OMPC_partial:
10276     C = OMPPartialClause::CreateEmpty(Context);
10277     break;
10278   case llvm::omp::OMPC_allocator:
10279     C = new (Context) OMPAllocatorClause();
10280     break;
10281   case llvm::omp::OMPC_collapse:
10282     C = new (Context) OMPCollapseClause();
10283     break;
10284   case llvm::omp::OMPC_default:
10285     C = new (Context) OMPDefaultClause();
10286     break;
10287   case llvm::omp::OMPC_proc_bind:
10288     C = new (Context) OMPProcBindClause();
10289     break;
10290   case llvm::omp::OMPC_schedule:
10291     C = new (Context) OMPScheduleClause();
10292     break;
10293   case llvm::omp::OMPC_ordered:
10294     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
10295     break;
10296   case llvm::omp::OMPC_nowait:
10297     C = new (Context) OMPNowaitClause();
10298     break;
10299   case llvm::omp::OMPC_untied:
10300     C = new (Context) OMPUntiedClause();
10301     break;
10302   case llvm::omp::OMPC_mergeable:
10303     C = new (Context) OMPMergeableClause();
10304     break;
10305   case llvm::omp::OMPC_read:
10306     C = new (Context) OMPReadClause();
10307     break;
10308   case llvm::omp::OMPC_write:
10309     C = new (Context) OMPWriteClause();
10310     break;
10311   case llvm::omp::OMPC_update:
10312     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
10313     break;
10314   case llvm::omp::OMPC_capture:
10315     C = new (Context) OMPCaptureClause();
10316     break;
10317   case llvm::omp::OMPC_compare:
10318     C = new (Context) OMPCompareClause();
10319     break;
10320   case llvm::omp::OMPC_fail:
10321     C = new (Context) OMPFailClause();
10322     break;
10323   case llvm::omp::OMPC_seq_cst:
10324     C = new (Context) OMPSeqCstClause();
10325     break;
10326   case llvm::omp::OMPC_acq_rel:
10327     C = new (Context) OMPAcqRelClause();
10328     break;
10329   case llvm::omp::OMPC_acquire:
10330     C = new (Context) OMPAcquireClause();
10331     break;
10332   case llvm::omp::OMPC_release:
10333     C = new (Context) OMPReleaseClause();
10334     break;
10335   case llvm::omp::OMPC_relaxed:
10336     C = new (Context) OMPRelaxedClause();
10337     break;
10338   case llvm::omp::OMPC_threads:
10339     C = new (Context) OMPThreadsClause();
10340     break;
10341   case llvm::omp::OMPC_simd:
10342     C = new (Context) OMPSIMDClause();
10343     break;
10344   case llvm::omp::OMPC_nogroup:
10345     C = new (Context) OMPNogroupClause();
10346     break;
10347   case llvm::omp::OMPC_unified_address:
10348     C = new (Context) OMPUnifiedAddressClause();
10349     break;
10350   case llvm::omp::OMPC_unified_shared_memory:
10351     C = new (Context) OMPUnifiedSharedMemoryClause();
10352     break;
10353   case llvm::omp::OMPC_reverse_offload:
10354     C = new (Context) OMPReverseOffloadClause();
10355     break;
10356   case llvm::omp::OMPC_dynamic_allocators:
10357     C = new (Context) OMPDynamicAllocatorsClause();
10358     break;
10359   case llvm::omp::OMPC_atomic_default_mem_order:
10360     C = new (Context) OMPAtomicDefaultMemOrderClause();
10361     break;
10362   case llvm::omp::OMPC_at:
10363     C = new (Context) OMPAtClause();
10364     break;
10365   case llvm::omp::OMPC_severity:
10366     C = new (Context) OMPSeverityClause();
10367     break;
10368   case llvm::omp::OMPC_message:
10369     C = new (Context) OMPMessageClause();
10370     break;
10371   case llvm::omp::OMPC_private:
10372     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
10373     break;
10374   case llvm::omp::OMPC_firstprivate:
10375     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
10376     break;
10377   case llvm::omp::OMPC_lastprivate:
10378     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
10379     break;
10380   case llvm::omp::OMPC_shared:
10381     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
10382     break;
10383   case llvm::omp::OMPC_reduction: {
10384     unsigned N = Record.readInt();
10385     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
10386     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
10387     break;
10388   }
10389   case llvm::omp::OMPC_task_reduction:
10390     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
10391     break;
10392   case llvm::omp::OMPC_in_reduction:
10393     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
10394     break;
10395   case llvm::omp::OMPC_linear:
10396     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
10397     break;
10398   case llvm::omp::OMPC_aligned:
10399     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
10400     break;
10401   case llvm::omp::OMPC_copyin:
10402     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
10403     break;
10404   case llvm::omp::OMPC_copyprivate:
10405     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
10406     break;
10407   case llvm::omp::OMPC_flush:
10408     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
10409     break;
10410   case llvm::omp::OMPC_depobj:
10411     C = OMPDepobjClause::CreateEmpty(Context);
10412     break;
10413   case llvm::omp::OMPC_depend: {
10414     unsigned NumVars = Record.readInt();
10415     unsigned NumLoops = Record.readInt();
10416     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
10417     break;
10418   }
10419   case llvm::omp::OMPC_device:
10420     C = new (Context) OMPDeviceClause();
10421     break;
10422   case llvm::omp::OMPC_map: {
10423     OMPMappableExprListSizeTy Sizes;
10424     Sizes.NumVars = Record.readInt();
10425     Sizes.NumUniqueDeclarations = Record.readInt();
10426     Sizes.NumComponentLists = Record.readInt();
10427     Sizes.NumComponents = Record.readInt();
10428     C = OMPMapClause::CreateEmpty(Context, Sizes);
10429     break;
10430   }
10431   case llvm::omp::OMPC_num_teams:
10432     C = new (Context) OMPNumTeamsClause();
10433     break;
10434   case llvm::omp::OMPC_thread_limit:
10435     C = new (Context) OMPThreadLimitClause();
10436     break;
10437   case llvm::omp::OMPC_priority:
10438     C = new (Context) OMPPriorityClause();
10439     break;
10440   case llvm::omp::OMPC_grainsize:
10441     C = new (Context) OMPGrainsizeClause();
10442     break;
10443   case llvm::omp::OMPC_num_tasks:
10444     C = new (Context) OMPNumTasksClause();
10445     break;
10446   case llvm::omp::OMPC_hint:
10447     C = new (Context) OMPHintClause();
10448     break;
10449   case llvm::omp::OMPC_dist_schedule:
10450     C = new (Context) OMPDistScheduleClause();
10451     break;
10452   case llvm::omp::OMPC_defaultmap:
10453     C = new (Context) OMPDefaultmapClause();
10454     break;
10455   case llvm::omp::OMPC_to: {
10456     OMPMappableExprListSizeTy Sizes;
10457     Sizes.NumVars = Record.readInt();
10458     Sizes.NumUniqueDeclarations = Record.readInt();
10459     Sizes.NumComponentLists = Record.readInt();
10460     Sizes.NumComponents = Record.readInt();
10461     C = OMPToClause::CreateEmpty(Context, Sizes);
10462     break;
10463   }
10464   case llvm::omp::OMPC_from: {
10465     OMPMappableExprListSizeTy Sizes;
10466     Sizes.NumVars = Record.readInt();
10467     Sizes.NumUniqueDeclarations = Record.readInt();
10468     Sizes.NumComponentLists = Record.readInt();
10469     Sizes.NumComponents = Record.readInt();
10470     C = OMPFromClause::CreateEmpty(Context, Sizes);
10471     break;
10472   }
10473   case llvm::omp::OMPC_use_device_ptr: {
10474     OMPMappableExprListSizeTy Sizes;
10475     Sizes.NumVars = Record.readInt();
10476     Sizes.NumUniqueDeclarations = Record.readInt();
10477     Sizes.NumComponentLists = Record.readInt();
10478     Sizes.NumComponents = Record.readInt();
10479     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
10480     break;
10481   }
10482   case llvm::omp::OMPC_use_device_addr: {
10483     OMPMappableExprListSizeTy Sizes;
10484     Sizes.NumVars = Record.readInt();
10485     Sizes.NumUniqueDeclarations = Record.readInt();
10486     Sizes.NumComponentLists = Record.readInt();
10487     Sizes.NumComponents = Record.readInt();
10488     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
10489     break;
10490   }
10491   case llvm::omp::OMPC_is_device_ptr: {
10492     OMPMappableExprListSizeTy Sizes;
10493     Sizes.NumVars = Record.readInt();
10494     Sizes.NumUniqueDeclarations = Record.readInt();
10495     Sizes.NumComponentLists = Record.readInt();
10496     Sizes.NumComponents = Record.readInt();
10497     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
10498     break;
10499   }
10500   case llvm::omp::OMPC_has_device_addr: {
10501     OMPMappableExprListSizeTy Sizes;
10502     Sizes.NumVars = Record.readInt();
10503     Sizes.NumUniqueDeclarations = Record.readInt();
10504     Sizes.NumComponentLists = Record.readInt();
10505     Sizes.NumComponents = Record.readInt();
10506     C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);
10507     break;
10508   }
10509   case llvm::omp::OMPC_allocate:
10510     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
10511     break;
10512   case llvm::omp::OMPC_nontemporal:
10513     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
10514     break;
10515   case llvm::omp::OMPC_inclusive:
10516     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
10517     break;
10518   case llvm::omp::OMPC_exclusive:
10519     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
10520     break;
10521   case llvm::omp::OMPC_order:
10522     C = new (Context) OMPOrderClause();
10523     break;
10524   case llvm::omp::OMPC_init:
10525     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
10526     break;
10527   case llvm::omp::OMPC_use:
10528     C = new (Context) OMPUseClause();
10529     break;
10530   case llvm::omp::OMPC_destroy:
10531     C = new (Context) OMPDestroyClause();
10532     break;
10533   case llvm::omp::OMPC_novariants:
10534     C = new (Context) OMPNovariantsClause();
10535     break;
10536   case llvm::omp::OMPC_nocontext:
10537     C = new (Context) OMPNocontextClause();
10538     break;
10539   case llvm::omp::OMPC_detach:
10540     C = new (Context) OMPDetachClause();
10541     break;
10542   case llvm::omp::OMPC_uses_allocators:
10543     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
10544     break;
10545   case llvm::omp::OMPC_affinity:
10546     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
10547     break;
10548   case llvm::omp::OMPC_filter:
10549     C = new (Context) OMPFilterClause();
10550     break;
10551   case llvm::omp::OMPC_bind:
10552     C = OMPBindClause::CreateEmpty(Context);
10553     break;
10554   case llvm::omp::OMPC_align:
10555     C = new (Context) OMPAlignClause();
10556     break;
10557   case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
10558     C = new (Context) OMPXDynCGroupMemClause();
10559     break;
10560   case llvm::omp::OMPC_doacross: {
10561     unsigned NumVars = Record.readInt();
10562     unsigned NumLoops = Record.readInt();
10563     C = OMPDoacrossClause::CreateEmpty(Context, NumVars, NumLoops);
10564     break;
10565   }
10566   case llvm::omp::OMPC_ompx_attribute:
10567     C = new (Context) OMPXAttributeClause();
10568     break;
10569   case llvm::omp::OMPC_ompx_bare:
10570     C = new (Context) OMPXBareClause();
10571     break;
10572 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
10573   case llvm::omp::Enum:                                                        \
10574     break;
10575 #include "llvm/Frontend/OpenMP/OMPKinds.def"
10576   default:
10577     break;
10578   }
10579   assert(C && "Unknown OMPClause type");
10580 
10581   Visit(C);
10582   C->setLocStart(Record.readSourceLocation());
10583   C->setLocEnd(Record.readSourceLocation());
10584 
10585   return C;
10586 }
10587 
VisitOMPClauseWithPreInit(OMPClauseWithPreInit * C)10588 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
10589   C->setPreInitStmt(Record.readSubStmt(),
10590                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
10591 }
10592 
VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate * C)10593 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
10594   VisitOMPClauseWithPreInit(C);
10595   C->setPostUpdateExpr(Record.readSubExpr());
10596 }
10597 
VisitOMPIfClause(OMPIfClause * C)10598 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
10599   VisitOMPClauseWithPreInit(C);
10600   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
10601   C->setNameModifierLoc(Record.readSourceLocation());
10602   C->setColonLoc(Record.readSourceLocation());
10603   C->setCondition(Record.readSubExpr());
10604   C->setLParenLoc(Record.readSourceLocation());
10605 }
10606 
VisitOMPFinalClause(OMPFinalClause * C)10607 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
10608   VisitOMPClauseWithPreInit(C);
10609   C->setCondition(Record.readSubExpr());
10610   C->setLParenLoc(Record.readSourceLocation());
10611 }
10612 
VisitOMPNumThreadsClause(OMPNumThreadsClause * C)10613 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
10614   VisitOMPClauseWithPreInit(C);
10615   C->setNumThreads(Record.readSubExpr());
10616   C->setLParenLoc(Record.readSourceLocation());
10617 }
10618 
VisitOMPSafelenClause(OMPSafelenClause * C)10619 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
10620   C->setSafelen(Record.readSubExpr());
10621   C->setLParenLoc(Record.readSourceLocation());
10622 }
10623 
VisitOMPSimdlenClause(OMPSimdlenClause * C)10624 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
10625   C->setSimdlen(Record.readSubExpr());
10626   C->setLParenLoc(Record.readSourceLocation());
10627 }
10628 
VisitOMPSizesClause(OMPSizesClause * C)10629 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
10630   for (Expr *&E : C->getSizesRefs())
10631     E = Record.readSubExpr();
10632   C->setLParenLoc(Record.readSourceLocation());
10633 }
10634 
VisitOMPFullClause(OMPFullClause * C)10635 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
10636 
VisitOMPPartialClause(OMPPartialClause * C)10637 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
10638   C->setFactor(Record.readSubExpr());
10639   C->setLParenLoc(Record.readSourceLocation());
10640 }
10641 
VisitOMPAllocatorClause(OMPAllocatorClause * C)10642 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
10643   C->setAllocator(Record.readExpr());
10644   C->setLParenLoc(Record.readSourceLocation());
10645 }
10646 
VisitOMPCollapseClause(OMPCollapseClause * C)10647 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
10648   C->setNumForLoops(Record.readSubExpr());
10649   C->setLParenLoc(Record.readSourceLocation());
10650 }
10651 
VisitOMPDefaultClause(OMPDefaultClause * C)10652 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
10653   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
10654   C->setLParenLoc(Record.readSourceLocation());
10655   C->setDefaultKindKwLoc(Record.readSourceLocation());
10656 }
10657 
VisitOMPProcBindClause(OMPProcBindClause * C)10658 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
10659   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
10660   C->setLParenLoc(Record.readSourceLocation());
10661   C->setProcBindKindKwLoc(Record.readSourceLocation());
10662 }
10663 
VisitOMPScheduleClause(OMPScheduleClause * C)10664 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
10665   VisitOMPClauseWithPreInit(C);
10666   C->setScheduleKind(
10667        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
10668   C->setFirstScheduleModifier(
10669       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10670   C->setSecondScheduleModifier(
10671       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10672   C->setChunkSize(Record.readSubExpr());
10673   C->setLParenLoc(Record.readSourceLocation());
10674   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
10675   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
10676   C->setScheduleKindLoc(Record.readSourceLocation());
10677   C->setCommaLoc(Record.readSourceLocation());
10678 }
10679 
VisitOMPOrderedClause(OMPOrderedClause * C)10680 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
10681   C->setNumForLoops(Record.readSubExpr());
10682   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10683     C->setLoopNumIterations(I, Record.readSubExpr());
10684   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10685     C->setLoopCounter(I, Record.readSubExpr());
10686   C->setLParenLoc(Record.readSourceLocation());
10687 }
10688 
VisitOMPDetachClause(OMPDetachClause * C)10689 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
10690   C->setEventHandler(Record.readSubExpr());
10691   C->setLParenLoc(Record.readSourceLocation());
10692 }
10693 
VisitOMPNowaitClause(OMPNowaitClause *)10694 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
10695 
VisitOMPUntiedClause(OMPUntiedClause *)10696 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
10697 
VisitOMPMergeableClause(OMPMergeableClause *)10698 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
10699 
VisitOMPReadClause(OMPReadClause *)10700 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
10701 
VisitOMPWriteClause(OMPWriteClause *)10702 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
10703 
VisitOMPUpdateClause(OMPUpdateClause * C)10704 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
10705   if (C->isExtended()) {
10706     C->setLParenLoc(Record.readSourceLocation());
10707     C->setArgumentLoc(Record.readSourceLocation());
10708     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
10709   }
10710 }
10711 
VisitOMPCaptureClause(OMPCaptureClause *)10712 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
10713 
VisitOMPCompareClause(OMPCompareClause *)10714 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
10715 
10716 // Read the parameter of fail clause. This will have been saved when
10717 // OMPClauseWriter is called.
VisitOMPFailClause(OMPFailClause * C)10718 void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
10719   C->setLParenLoc(Record.readSourceLocation());
10720   SourceLocation FailParameterLoc = Record.readSourceLocation();
10721   C->setFailParameterLoc(FailParameterLoc);
10722   OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
10723   C->setFailParameter(CKind);
10724 }
10725 
VisitOMPSeqCstClause(OMPSeqCstClause *)10726 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
10727 
VisitOMPAcqRelClause(OMPAcqRelClause *)10728 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
10729 
VisitOMPAcquireClause(OMPAcquireClause *)10730 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
10731 
VisitOMPReleaseClause(OMPReleaseClause *)10732 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
10733 
VisitOMPRelaxedClause(OMPRelaxedClause *)10734 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
10735 
VisitOMPThreadsClause(OMPThreadsClause *)10736 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
10737 
VisitOMPSIMDClause(OMPSIMDClause *)10738 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
10739 
VisitOMPNogroupClause(OMPNogroupClause *)10740 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
10741 
VisitOMPInitClause(OMPInitClause * C)10742 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
10743   unsigned NumVars = C->varlist_size();
10744   SmallVector<Expr *, 16> Vars;
10745   Vars.reserve(NumVars);
10746   for (unsigned I = 0; I != NumVars; ++I)
10747     Vars.push_back(Record.readSubExpr());
10748   C->setVarRefs(Vars);
10749   C->setIsTarget(Record.readBool());
10750   C->setIsTargetSync(Record.readBool());
10751   C->setLParenLoc(Record.readSourceLocation());
10752   C->setVarLoc(Record.readSourceLocation());
10753 }
10754 
VisitOMPUseClause(OMPUseClause * C)10755 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
10756   C->setInteropVar(Record.readSubExpr());
10757   C->setLParenLoc(Record.readSourceLocation());
10758   C->setVarLoc(Record.readSourceLocation());
10759 }
10760 
VisitOMPDestroyClause(OMPDestroyClause * C)10761 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
10762   C->setInteropVar(Record.readSubExpr());
10763   C->setLParenLoc(Record.readSourceLocation());
10764   C->setVarLoc(Record.readSourceLocation());
10765 }
10766 
VisitOMPNovariantsClause(OMPNovariantsClause * C)10767 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
10768   VisitOMPClauseWithPreInit(C);
10769   C->setCondition(Record.readSubExpr());
10770   C->setLParenLoc(Record.readSourceLocation());
10771 }
10772 
VisitOMPNocontextClause(OMPNocontextClause * C)10773 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
10774   VisitOMPClauseWithPreInit(C);
10775   C->setCondition(Record.readSubExpr());
10776   C->setLParenLoc(Record.readSourceLocation());
10777 }
10778 
VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *)10779 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
10780 
VisitOMPUnifiedSharedMemoryClause(OMPUnifiedSharedMemoryClause *)10781 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
10782     OMPUnifiedSharedMemoryClause *) {}
10783 
VisitOMPReverseOffloadClause(OMPReverseOffloadClause *)10784 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
10785 
10786 void
VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *)10787 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
10788 }
10789 
VisitOMPAtomicDefaultMemOrderClause(OMPAtomicDefaultMemOrderClause * C)10790 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
10791     OMPAtomicDefaultMemOrderClause *C) {
10792   C->setAtomicDefaultMemOrderKind(
10793       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
10794   C->setLParenLoc(Record.readSourceLocation());
10795   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
10796 }
10797 
VisitOMPAtClause(OMPAtClause * C)10798 void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
10799   C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
10800   C->setLParenLoc(Record.readSourceLocation());
10801   C->setAtKindKwLoc(Record.readSourceLocation());
10802 }
10803 
VisitOMPSeverityClause(OMPSeverityClause * C)10804 void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
10805   C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
10806   C->setLParenLoc(Record.readSourceLocation());
10807   C->setSeverityKindKwLoc(Record.readSourceLocation());
10808 }
10809 
VisitOMPMessageClause(OMPMessageClause * C)10810 void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
10811   C->setMessageString(Record.readSubExpr());
10812   C->setLParenLoc(Record.readSourceLocation());
10813 }
10814 
VisitOMPPrivateClause(OMPPrivateClause * C)10815 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
10816   C->setLParenLoc(Record.readSourceLocation());
10817   unsigned NumVars = C->varlist_size();
10818   SmallVector<Expr *, 16> Vars;
10819   Vars.reserve(NumVars);
10820   for (unsigned i = 0; i != NumVars; ++i)
10821     Vars.push_back(Record.readSubExpr());
10822   C->setVarRefs(Vars);
10823   Vars.clear();
10824   for (unsigned i = 0; i != NumVars; ++i)
10825     Vars.push_back(Record.readSubExpr());
10826   C->setPrivateCopies(Vars);
10827 }
10828 
VisitOMPFirstprivateClause(OMPFirstprivateClause * C)10829 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
10830   VisitOMPClauseWithPreInit(C);
10831   C->setLParenLoc(Record.readSourceLocation());
10832   unsigned NumVars = C->varlist_size();
10833   SmallVector<Expr *, 16> Vars;
10834   Vars.reserve(NumVars);
10835   for (unsigned i = 0; i != NumVars; ++i)
10836     Vars.push_back(Record.readSubExpr());
10837   C->setVarRefs(Vars);
10838   Vars.clear();
10839   for (unsigned i = 0; i != NumVars; ++i)
10840     Vars.push_back(Record.readSubExpr());
10841   C->setPrivateCopies(Vars);
10842   Vars.clear();
10843   for (unsigned i = 0; i != NumVars; ++i)
10844     Vars.push_back(Record.readSubExpr());
10845   C->setInits(Vars);
10846 }
10847 
VisitOMPLastprivateClause(OMPLastprivateClause * C)10848 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
10849   VisitOMPClauseWithPostUpdate(C);
10850   C->setLParenLoc(Record.readSourceLocation());
10851   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
10852   C->setKindLoc(Record.readSourceLocation());
10853   C->setColonLoc(Record.readSourceLocation());
10854   unsigned NumVars = C->varlist_size();
10855   SmallVector<Expr *, 16> Vars;
10856   Vars.reserve(NumVars);
10857   for (unsigned i = 0; i != NumVars; ++i)
10858     Vars.push_back(Record.readSubExpr());
10859   C->setVarRefs(Vars);
10860   Vars.clear();
10861   for (unsigned i = 0; i != NumVars; ++i)
10862     Vars.push_back(Record.readSubExpr());
10863   C->setPrivateCopies(Vars);
10864   Vars.clear();
10865   for (unsigned i = 0; i != NumVars; ++i)
10866     Vars.push_back(Record.readSubExpr());
10867   C->setSourceExprs(Vars);
10868   Vars.clear();
10869   for (unsigned i = 0; i != NumVars; ++i)
10870     Vars.push_back(Record.readSubExpr());
10871   C->setDestinationExprs(Vars);
10872   Vars.clear();
10873   for (unsigned i = 0; i != NumVars; ++i)
10874     Vars.push_back(Record.readSubExpr());
10875   C->setAssignmentOps(Vars);
10876 }
10877 
VisitOMPSharedClause(OMPSharedClause * C)10878 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
10879   C->setLParenLoc(Record.readSourceLocation());
10880   unsigned NumVars = C->varlist_size();
10881   SmallVector<Expr *, 16> Vars;
10882   Vars.reserve(NumVars);
10883   for (unsigned i = 0; i != NumVars; ++i)
10884     Vars.push_back(Record.readSubExpr());
10885   C->setVarRefs(Vars);
10886 }
10887 
VisitOMPReductionClause(OMPReductionClause * C)10888 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
10889   VisitOMPClauseWithPostUpdate(C);
10890   C->setLParenLoc(Record.readSourceLocation());
10891   C->setModifierLoc(Record.readSourceLocation());
10892   C->setColonLoc(Record.readSourceLocation());
10893   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10894   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10895   C->setQualifierLoc(NNSL);
10896   C->setNameInfo(DNI);
10897 
10898   unsigned NumVars = C->varlist_size();
10899   SmallVector<Expr *, 16> Vars;
10900   Vars.reserve(NumVars);
10901   for (unsigned i = 0; i != NumVars; ++i)
10902     Vars.push_back(Record.readSubExpr());
10903   C->setVarRefs(Vars);
10904   Vars.clear();
10905   for (unsigned i = 0; i != NumVars; ++i)
10906     Vars.push_back(Record.readSubExpr());
10907   C->setPrivates(Vars);
10908   Vars.clear();
10909   for (unsigned i = 0; i != NumVars; ++i)
10910     Vars.push_back(Record.readSubExpr());
10911   C->setLHSExprs(Vars);
10912   Vars.clear();
10913   for (unsigned i = 0; i != NumVars; ++i)
10914     Vars.push_back(Record.readSubExpr());
10915   C->setRHSExprs(Vars);
10916   Vars.clear();
10917   for (unsigned i = 0; i != NumVars; ++i)
10918     Vars.push_back(Record.readSubExpr());
10919   C->setReductionOps(Vars);
10920   if (C->getModifier() == OMPC_REDUCTION_inscan) {
10921     Vars.clear();
10922     for (unsigned i = 0; i != NumVars; ++i)
10923       Vars.push_back(Record.readSubExpr());
10924     C->setInscanCopyOps(Vars);
10925     Vars.clear();
10926     for (unsigned i = 0; i != NumVars; ++i)
10927       Vars.push_back(Record.readSubExpr());
10928     C->setInscanCopyArrayTemps(Vars);
10929     Vars.clear();
10930     for (unsigned i = 0; i != NumVars; ++i)
10931       Vars.push_back(Record.readSubExpr());
10932     C->setInscanCopyArrayElems(Vars);
10933   }
10934 }
10935 
VisitOMPTaskReductionClause(OMPTaskReductionClause * C)10936 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
10937   VisitOMPClauseWithPostUpdate(C);
10938   C->setLParenLoc(Record.readSourceLocation());
10939   C->setColonLoc(Record.readSourceLocation());
10940   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10941   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10942   C->setQualifierLoc(NNSL);
10943   C->setNameInfo(DNI);
10944 
10945   unsigned NumVars = C->varlist_size();
10946   SmallVector<Expr *, 16> Vars;
10947   Vars.reserve(NumVars);
10948   for (unsigned I = 0; I != NumVars; ++I)
10949     Vars.push_back(Record.readSubExpr());
10950   C->setVarRefs(Vars);
10951   Vars.clear();
10952   for (unsigned I = 0; I != NumVars; ++I)
10953     Vars.push_back(Record.readSubExpr());
10954   C->setPrivates(Vars);
10955   Vars.clear();
10956   for (unsigned I = 0; I != NumVars; ++I)
10957     Vars.push_back(Record.readSubExpr());
10958   C->setLHSExprs(Vars);
10959   Vars.clear();
10960   for (unsigned I = 0; I != NumVars; ++I)
10961     Vars.push_back(Record.readSubExpr());
10962   C->setRHSExprs(Vars);
10963   Vars.clear();
10964   for (unsigned I = 0; I != NumVars; ++I)
10965     Vars.push_back(Record.readSubExpr());
10966   C->setReductionOps(Vars);
10967 }
10968 
VisitOMPInReductionClause(OMPInReductionClause * C)10969 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
10970   VisitOMPClauseWithPostUpdate(C);
10971   C->setLParenLoc(Record.readSourceLocation());
10972   C->setColonLoc(Record.readSourceLocation());
10973   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10974   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10975   C->setQualifierLoc(NNSL);
10976   C->setNameInfo(DNI);
10977 
10978   unsigned NumVars = C->varlist_size();
10979   SmallVector<Expr *, 16> Vars;
10980   Vars.reserve(NumVars);
10981   for (unsigned I = 0; I != NumVars; ++I)
10982     Vars.push_back(Record.readSubExpr());
10983   C->setVarRefs(Vars);
10984   Vars.clear();
10985   for (unsigned I = 0; I != NumVars; ++I)
10986     Vars.push_back(Record.readSubExpr());
10987   C->setPrivates(Vars);
10988   Vars.clear();
10989   for (unsigned I = 0; I != NumVars; ++I)
10990     Vars.push_back(Record.readSubExpr());
10991   C->setLHSExprs(Vars);
10992   Vars.clear();
10993   for (unsigned I = 0; I != NumVars; ++I)
10994     Vars.push_back(Record.readSubExpr());
10995   C->setRHSExprs(Vars);
10996   Vars.clear();
10997   for (unsigned I = 0; I != NumVars; ++I)
10998     Vars.push_back(Record.readSubExpr());
10999   C->setReductionOps(Vars);
11000   Vars.clear();
11001   for (unsigned I = 0; I != NumVars; ++I)
11002     Vars.push_back(Record.readSubExpr());
11003   C->setTaskgroupDescriptors(Vars);
11004 }
11005 
VisitOMPLinearClause(OMPLinearClause * C)11006 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
11007   VisitOMPClauseWithPostUpdate(C);
11008   C->setLParenLoc(Record.readSourceLocation());
11009   C->setColonLoc(Record.readSourceLocation());
11010   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
11011   C->setModifierLoc(Record.readSourceLocation());
11012   unsigned NumVars = C->varlist_size();
11013   SmallVector<Expr *, 16> Vars;
11014   Vars.reserve(NumVars);
11015   for (unsigned i = 0; i != NumVars; ++i)
11016     Vars.push_back(Record.readSubExpr());
11017   C->setVarRefs(Vars);
11018   Vars.clear();
11019   for (unsigned i = 0; i != NumVars; ++i)
11020     Vars.push_back(Record.readSubExpr());
11021   C->setPrivates(Vars);
11022   Vars.clear();
11023   for (unsigned i = 0; i != NumVars; ++i)
11024     Vars.push_back(Record.readSubExpr());
11025   C->setInits(Vars);
11026   Vars.clear();
11027   for (unsigned i = 0; i != NumVars; ++i)
11028     Vars.push_back(Record.readSubExpr());
11029   C->setUpdates(Vars);
11030   Vars.clear();
11031   for (unsigned i = 0; i != NumVars; ++i)
11032     Vars.push_back(Record.readSubExpr());
11033   C->setFinals(Vars);
11034   C->setStep(Record.readSubExpr());
11035   C->setCalcStep(Record.readSubExpr());
11036   Vars.clear();
11037   for (unsigned I = 0; I != NumVars + 1; ++I)
11038     Vars.push_back(Record.readSubExpr());
11039   C->setUsedExprs(Vars);
11040 }
11041 
VisitOMPAlignedClause(OMPAlignedClause * C)11042 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
11043   C->setLParenLoc(Record.readSourceLocation());
11044   C->setColonLoc(Record.readSourceLocation());
11045   unsigned NumVars = C->varlist_size();
11046   SmallVector<Expr *, 16> Vars;
11047   Vars.reserve(NumVars);
11048   for (unsigned i = 0; i != NumVars; ++i)
11049     Vars.push_back(Record.readSubExpr());
11050   C->setVarRefs(Vars);
11051   C->setAlignment(Record.readSubExpr());
11052 }
11053 
VisitOMPCopyinClause(OMPCopyinClause * C)11054 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
11055   C->setLParenLoc(Record.readSourceLocation());
11056   unsigned NumVars = C->varlist_size();
11057   SmallVector<Expr *, 16> Exprs;
11058   Exprs.reserve(NumVars);
11059   for (unsigned i = 0; i != NumVars; ++i)
11060     Exprs.push_back(Record.readSubExpr());
11061   C->setVarRefs(Exprs);
11062   Exprs.clear();
11063   for (unsigned i = 0; i != NumVars; ++i)
11064     Exprs.push_back(Record.readSubExpr());
11065   C->setSourceExprs(Exprs);
11066   Exprs.clear();
11067   for (unsigned i = 0; i != NumVars; ++i)
11068     Exprs.push_back(Record.readSubExpr());
11069   C->setDestinationExprs(Exprs);
11070   Exprs.clear();
11071   for (unsigned i = 0; i != NumVars; ++i)
11072     Exprs.push_back(Record.readSubExpr());
11073   C->setAssignmentOps(Exprs);
11074 }
11075 
VisitOMPCopyprivateClause(OMPCopyprivateClause * C)11076 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11077   C->setLParenLoc(Record.readSourceLocation());
11078   unsigned NumVars = C->varlist_size();
11079   SmallVector<Expr *, 16> Exprs;
11080   Exprs.reserve(NumVars);
11081   for (unsigned i = 0; i != NumVars; ++i)
11082     Exprs.push_back(Record.readSubExpr());
11083   C->setVarRefs(Exprs);
11084   Exprs.clear();
11085   for (unsigned i = 0; i != NumVars; ++i)
11086     Exprs.push_back(Record.readSubExpr());
11087   C->setSourceExprs(Exprs);
11088   Exprs.clear();
11089   for (unsigned i = 0; i != NumVars; ++i)
11090     Exprs.push_back(Record.readSubExpr());
11091   C->setDestinationExprs(Exprs);
11092   Exprs.clear();
11093   for (unsigned i = 0; i != NumVars; ++i)
11094     Exprs.push_back(Record.readSubExpr());
11095   C->setAssignmentOps(Exprs);
11096 }
11097 
VisitOMPFlushClause(OMPFlushClause * C)11098 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
11099   C->setLParenLoc(Record.readSourceLocation());
11100   unsigned NumVars = C->varlist_size();
11101   SmallVector<Expr *, 16> Vars;
11102   Vars.reserve(NumVars);
11103   for (unsigned i = 0; i != NumVars; ++i)
11104     Vars.push_back(Record.readSubExpr());
11105   C->setVarRefs(Vars);
11106 }
11107 
VisitOMPDepobjClause(OMPDepobjClause * C)11108 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
11109   C->setDepobj(Record.readSubExpr());
11110   C->setLParenLoc(Record.readSourceLocation());
11111 }
11112 
VisitOMPDependClause(OMPDependClause * C)11113 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
11114   C->setLParenLoc(Record.readSourceLocation());
11115   C->setModifier(Record.readSubExpr());
11116   C->setDependencyKind(
11117       static_cast<OpenMPDependClauseKind>(Record.readInt()));
11118   C->setDependencyLoc(Record.readSourceLocation());
11119   C->setColonLoc(Record.readSourceLocation());
11120   C->setOmpAllMemoryLoc(Record.readSourceLocation());
11121   unsigned NumVars = C->varlist_size();
11122   SmallVector<Expr *, 16> Vars;
11123   Vars.reserve(NumVars);
11124   for (unsigned I = 0; I != NumVars; ++I)
11125     Vars.push_back(Record.readSubExpr());
11126   C->setVarRefs(Vars);
11127   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11128     C->setLoopData(I, Record.readSubExpr());
11129 }
11130 
VisitOMPDeviceClause(OMPDeviceClause * C)11131 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
11132   VisitOMPClauseWithPreInit(C);
11133   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
11134   C->setDevice(Record.readSubExpr());
11135   C->setModifierLoc(Record.readSourceLocation());
11136   C->setLParenLoc(Record.readSourceLocation());
11137 }
11138 
VisitOMPMapClause(OMPMapClause * C)11139 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
11140   C->setLParenLoc(Record.readSourceLocation());
11141   bool HasIteratorModifier = false;
11142   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
11143     C->setMapTypeModifier(
11144         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
11145     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
11146     if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
11147       HasIteratorModifier = true;
11148   }
11149   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11150   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11151   C->setMapType(
11152      static_cast<OpenMPMapClauseKind>(Record.readInt()));
11153   C->setMapLoc(Record.readSourceLocation());
11154   C->setColonLoc(Record.readSourceLocation());
11155   auto NumVars = C->varlist_size();
11156   auto UniqueDecls = C->getUniqueDeclarationsNum();
11157   auto TotalLists = C->getTotalComponentListNum();
11158   auto TotalComponents = C->getTotalComponentsNum();
11159 
11160   SmallVector<Expr *, 16> Vars;
11161   Vars.reserve(NumVars);
11162   for (unsigned i = 0; i != NumVars; ++i)
11163     Vars.push_back(Record.readExpr());
11164   C->setVarRefs(Vars);
11165 
11166   SmallVector<Expr *, 16> UDMappers;
11167   UDMappers.reserve(NumVars);
11168   for (unsigned I = 0; I < NumVars; ++I)
11169     UDMappers.push_back(Record.readExpr());
11170   C->setUDMapperRefs(UDMappers);
11171 
11172   if (HasIteratorModifier)
11173     C->setIteratorModifier(Record.readExpr());
11174 
11175   SmallVector<ValueDecl *, 16> Decls;
11176   Decls.reserve(UniqueDecls);
11177   for (unsigned i = 0; i < UniqueDecls; ++i)
11178     Decls.push_back(Record.readDeclAs<ValueDecl>());
11179   C->setUniqueDecls(Decls);
11180 
11181   SmallVector<unsigned, 16> ListsPerDecl;
11182   ListsPerDecl.reserve(UniqueDecls);
11183   for (unsigned i = 0; i < UniqueDecls; ++i)
11184     ListsPerDecl.push_back(Record.readInt());
11185   C->setDeclNumLists(ListsPerDecl);
11186 
11187   SmallVector<unsigned, 32> ListSizes;
11188   ListSizes.reserve(TotalLists);
11189   for (unsigned i = 0; i < TotalLists; ++i)
11190     ListSizes.push_back(Record.readInt());
11191   C->setComponentListSizes(ListSizes);
11192 
11193   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11194   Components.reserve(TotalComponents);
11195   for (unsigned i = 0; i < TotalComponents; ++i) {
11196     Expr *AssociatedExprPr = Record.readExpr();
11197     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11198     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11199                             /*IsNonContiguous=*/false);
11200   }
11201   C->setComponents(Components, ListSizes);
11202 }
11203 
VisitOMPAllocateClause(OMPAllocateClause * C)11204 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
11205   C->setLParenLoc(Record.readSourceLocation());
11206   C->setColonLoc(Record.readSourceLocation());
11207   C->setAllocator(Record.readSubExpr());
11208   unsigned NumVars = C->varlist_size();
11209   SmallVector<Expr *, 16> Vars;
11210   Vars.reserve(NumVars);
11211   for (unsigned i = 0; i != NumVars; ++i)
11212     Vars.push_back(Record.readSubExpr());
11213   C->setVarRefs(Vars);
11214 }
11215 
VisitOMPNumTeamsClause(OMPNumTeamsClause * C)11216 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
11217   VisitOMPClauseWithPreInit(C);
11218   C->setNumTeams(Record.readSubExpr());
11219   C->setLParenLoc(Record.readSourceLocation());
11220 }
11221 
VisitOMPThreadLimitClause(OMPThreadLimitClause * C)11222 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
11223   VisitOMPClauseWithPreInit(C);
11224   C->setThreadLimit(Record.readSubExpr());
11225   C->setLParenLoc(Record.readSourceLocation());
11226 }
11227 
VisitOMPPriorityClause(OMPPriorityClause * C)11228 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
11229   VisitOMPClauseWithPreInit(C);
11230   C->setPriority(Record.readSubExpr());
11231   C->setLParenLoc(Record.readSourceLocation());
11232 }
11233 
VisitOMPGrainsizeClause(OMPGrainsizeClause * C)11234 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
11235   VisitOMPClauseWithPreInit(C);
11236   C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
11237   C->setGrainsize(Record.readSubExpr());
11238   C->setModifierLoc(Record.readSourceLocation());
11239   C->setLParenLoc(Record.readSourceLocation());
11240 }
11241 
VisitOMPNumTasksClause(OMPNumTasksClause * C)11242 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
11243   VisitOMPClauseWithPreInit(C);
11244   C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
11245   C->setNumTasks(Record.readSubExpr());
11246   C->setModifierLoc(Record.readSourceLocation());
11247   C->setLParenLoc(Record.readSourceLocation());
11248 }
11249 
VisitOMPHintClause(OMPHintClause * C)11250 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
11251   C->setHint(Record.readSubExpr());
11252   C->setLParenLoc(Record.readSourceLocation());
11253 }
11254 
VisitOMPDistScheduleClause(OMPDistScheduleClause * C)11255 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
11256   VisitOMPClauseWithPreInit(C);
11257   C->setDistScheduleKind(
11258       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
11259   C->setChunkSize(Record.readSubExpr());
11260   C->setLParenLoc(Record.readSourceLocation());
11261   C->setDistScheduleKindLoc(Record.readSourceLocation());
11262   C->setCommaLoc(Record.readSourceLocation());
11263 }
11264 
VisitOMPDefaultmapClause(OMPDefaultmapClause * C)11265 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
11266   C->setDefaultmapKind(
11267        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
11268   C->setDefaultmapModifier(
11269       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
11270   C->setLParenLoc(Record.readSourceLocation());
11271   C->setDefaultmapModifierLoc(Record.readSourceLocation());
11272   C->setDefaultmapKindLoc(Record.readSourceLocation());
11273 }
11274 
VisitOMPToClause(OMPToClause * C)11275 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
11276   C->setLParenLoc(Record.readSourceLocation());
11277   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11278     C->setMotionModifier(
11279         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11280     C->setMotionModifierLoc(I, Record.readSourceLocation());
11281   }
11282   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11283   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11284   C->setColonLoc(Record.readSourceLocation());
11285   auto NumVars = C->varlist_size();
11286   auto UniqueDecls = C->getUniqueDeclarationsNum();
11287   auto TotalLists = C->getTotalComponentListNum();
11288   auto TotalComponents = C->getTotalComponentsNum();
11289 
11290   SmallVector<Expr *, 16> Vars;
11291   Vars.reserve(NumVars);
11292   for (unsigned i = 0; i != NumVars; ++i)
11293     Vars.push_back(Record.readSubExpr());
11294   C->setVarRefs(Vars);
11295 
11296   SmallVector<Expr *, 16> UDMappers;
11297   UDMappers.reserve(NumVars);
11298   for (unsigned I = 0; I < NumVars; ++I)
11299     UDMappers.push_back(Record.readSubExpr());
11300   C->setUDMapperRefs(UDMappers);
11301 
11302   SmallVector<ValueDecl *, 16> Decls;
11303   Decls.reserve(UniqueDecls);
11304   for (unsigned i = 0; i < UniqueDecls; ++i)
11305     Decls.push_back(Record.readDeclAs<ValueDecl>());
11306   C->setUniqueDecls(Decls);
11307 
11308   SmallVector<unsigned, 16> ListsPerDecl;
11309   ListsPerDecl.reserve(UniqueDecls);
11310   for (unsigned i = 0; i < UniqueDecls; ++i)
11311     ListsPerDecl.push_back(Record.readInt());
11312   C->setDeclNumLists(ListsPerDecl);
11313 
11314   SmallVector<unsigned, 32> ListSizes;
11315   ListSizes.reserve(TotalLists);
11316   for (unsigned i = 0; i < TotalLists; ++i)
11317     ListSizes.push_back(Record.readInt());
11318   C->setComponentListSizes(ListSizes);
11319 
11320   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11321   Components.reserve(TotalComponents);
11322   for (unsigned i = 0; i < TotalComponents; ++i) {
11323     Expr *AssociatedExprPr = Record.readSubExpr();
11324     bool IsNonContiguous = Record.readBool();
11325     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11326     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11327   }
11328   C->setComponents(Components, ListSizes);
11329 }
11330 
VisitOMPFromClause(OMPFromClause * C)11331 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
11332   C->setLParenLoc(Record.readSourceLocation());
11333   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11334     C->setMotionModifier(
11335         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11336     C->setMotionModifierLoc(I, Record.readSourceLocation());
11337   }
11338   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11339   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11340   C->setColonLoc(Record.readSourceLocation());
11341   auto NumVars = C->varlist_size();
11342   auto UniqueDecls = C->getUniqueDeclarationsNum();
11343   auto TotalLists = C->getTotalComponentListNum();
11344   auto TotalComponents = C->getTotalComponentsNum();
11345 
11346   SmallVector<Expr *, 16> Vars;
11347   Vars.reserve(NumVars);
11348   for (unsigned i = 0; i != NumVars; ++i)
11349     Vars.push_back(Record.readSubExpr());
11350   C->setVarRefs(Vars);
11351 
11352   SmallVector<Expr *, 16> UDMappers;
11353   UDMappers.reserve(NumVars);
11354   for (unsigned I = 0; I < NumVars; ++I)
11355     UDMappers.push_back(Record.readSubExpr());
11356   C->setUDMapperRefs(UDMappers);
11357 
11358   SmallVector<ValueDecl *, 16> Decls;
11359   Decls.reserve(UniqueDecls);
11360   for (unsigned i = 0; i < UniqueDecls; ++i)
11361     Decls.push_back(Record.readDeclAs<ValueDecl>());
11362   C->setUniqueDecls(Decls);
11363 
11364   SmallVector<unsigned, 16> ListsPerDecl;
11365   ListsPerDecl.reserve(UniqueDecls);
11366   for (unsigned i = 0; i < UniqueDecls; ++i)
11367     ListsPerDecl.push_back(Record.readInt());
11368   C->setDeclNumLists(ListsPerDecl);
11369 
11370   SmallVector<unsigned, 32> ListSizes;
11371   ListSizes.reserve(TotalLists);
11372   for (unsigned i = 0; i < TotalLists; ++i)
11373     ListSizes.push_back(Record.readInt());
11374   C->setComponentListSizes(ListSizes);
11375 
11376   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11377   Components.reserve(TotalComponents);
11378   for (unsigned i = 0; i < TotalComponents; ++i) {
11379     Expr *AssociatedExprPr = Record.readSubExpr();
11380     bool IsNonContiguous = Record.readBool();
11381     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11382     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11383   }
11384   C->setComponents(Components, ListSizes);
11385 }
11386 
VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause * C)11387 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
11388   C->setLParenLoc(Record.readSourceLocation());
11389   auto NumVars = C->varlist_size();
11390   auto UniqueDecls = C->getUniqueDeclarationsNum();
11391   auto TotalLists = C->getTotalComponentListNum();
11392   auto TotalComponents = C->getTotalComponentsNum();
11393 
11394   SmallVector<Expr *, 16> Vars;
11395   Vars.reserve(NumVars);
11396   for (unsigned i = 0; i != NumVars; ++i)
11397     Vars.push_back(Record.readSubExpr());
11398   C->setVarRefs(Vars);
11399   Vars.clear();
11400   for (unsigned i = 0; i != NumVars; ++i)
11401     Vars.push_back(Record.readSubExpr());
11402   C->setPrivateCopies(Vars);
11403   Vars.clear();
11404   for (unsigned i = 0; i != NumVars; ++i)
11405     Vars.push_back(Record.readSubExpr());
11406   C->setInits(Vars);
11407 
11408   SmallVector<ValueDecl *, 16> Decls;
11409   Decls.reserve(UniqueDecls);
11410   for (unsigned i = 0; i < UniqueDecls; ++i)
11411     Decls.push_back(Record.readDeclAs<ValueDecl>());
11412   C->setUniqueDecls(Decls);
11413 
11414   SmallVector<unsigned, 16> ListsPerDecl;
11415   ListsPerDecl.reserve(UniqueDecls);
11416   for (unsigned i = 0; i < UniqueDecls; ++i)
11417     ListsPerDecl.push_back(Record.readInt());
11418   C->setDeclNumLists(ListsPerDecl);
11419 
11420   SmallVector<unsigned, 32> ListSizes;
11421   ListSizes.reserve(TotalLists);
11422   for (unsigned i = 0; i < TotalLists; ++i)
11423     ListSizes.push_back(Record.readInt());
11424   C->setComponentListSizes(ListSizes);
11425 
11426   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11427   Components.reserve(TotalComponents);
11428   for (unsigned i = 0; i < TotalComponents; ++i) {
11429     auto *AssociatedExprPr = Record.readSubExpr();
11430     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11431     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11432                             /*IsNonContiguous=*/false);
11433   }
11434   C->setComponents(Components, ListSizes);
11435 }
11436 
VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause * C)11437 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
11438   C->setLParenLoc(Record.readSourceLocation());
11439   auto NumVars = C->varlist_size();
11440   auto UniqueDecls = C->getUniqueDeclarationsNum();
11441   auto TotalLists = C->getTotalComponentListNum();
11442   auto TotalComponents = C->getTotalComponentsNum();
11443 
11444   SmallVector<Expr *, 16> Vars;
11445   Vars.reserve(NumVars);
11446   for (unsigned i = 0; i != NumVars; ++i)
11447     Vars.push_back(Record.readSubExpr());
11448   C->setVarRefs(Vars);
11449 
11450   SmallVector<ValueDecl *, 16> Decls;
11451   Decls.reserve(UniqueDecls);
11452   for (unsigned i = 0; i < UniqueDecls; ++i)
11453     Decls.push_back(Record.readDeclAs<ValueDecl>());
11454   C->setUniqueDecls(Decls);
11455 
11456   SmallVector<unsigned, 16> ListsPerDecl;
11457   ListsPerDecl.reserve(UniqueDecls);
11458   for (unsigned i = 0; i < UniqueDecls; ++i)
11459     ListsPerDecl.push_back(Record.readInt());
11460   C->setDeclNumLists(ListsPerDecl);
11461 
11462   SmallVector<unsigned, 32> ListSizes;
11463   ListSizes.reserve(TotalLists);
11464   for (unsigned i = 0; i < TotalLists; ++i)
11465     ListSizes.push_back(Record.readInt());
11466   C->setComponentListSizes(ListSizes);
11467 
11468   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11469   Components.reserve(TotalComponents);
11470   for (unsigned i = 0; i < TotalComponents; ++i) {
11471     Expr *AssociatedExpr = Record.readSubExpr();
11472     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11473     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11474                             /*IsNonContiguous*/ false);
11475   }
11476   C->setComponents(Components, ListSizes);
11477 }
11478 
VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause * C)11479 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
11480   C->setLParenLoc(Record.readSourceLocation());
11481   auto NumVars = C->varlist_size();
11482   auto UniqueDecls = C->getUniqueDeclarationsNum();
11483   auto TotalLists = C->getTotalComponentListNum();
11484   auto TotalComponents = C->getTotalComponentsNum();
11485 
11486   SmallVector<Expr *, 16> Vars;
11487   Vars.reserve(NumVars);
11488   for (unsigned i = 0; i != NumVars; ++i)
11489     Vars.push_back(Record.readSubExpr());
11490   C->setVarRefs(Vars);
11491   Vars.clear();
11492 
11493   SmallVector<ValueDecl *, 16> Decls;
11494   Decls.reserve(UniqueDecls);
11495   for (unsigned i = 0; i < UniqueDecls; ++i)
11496     Decls.push_back(Record.readDeclAs<ValueDecl>());
11497   C->setUniqueDecls(Decls);
11498 
11499   SmallVector<unsigned, 16> ListsPerDecl;
11500   ListsPerDecl.reserve(UniqueDecls);
11501   for (unsigned i = 0; i < UniqueDecls; ++i)
11502     ListsPerDecl.push_back(Record.readInt());
11503   C->setDeclNumLists(ListsPerDecl);
11504 
11505   SmallVector<unsigned, 32> ListSizes;
11506   ListSizes.reserve(TotalLists);
11507   for (unsigned i = 0; i < TotalLists; ++i)
11508     ListSizes.push_back(Record.readInt());
11509   C->setComponentListSizes(ListSizes);
11510 
11511   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11512   Components.reserve(TotalComponents);
11513   for (unsigned i = 0; i < TotalComponents; ++i) {
11514     Expr *AssociatedExpr = Record.readSubExpr();
11515     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11516     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11517                             /*IsNonContiguous=*/false);
11518   }
11519   C->setComponents(Components, ListSizes);
11520 }
11521 
VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause * C)11522 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
11523   C->setLParenLoc(Record.readSourceLocation());
11524   auto NumVars = C->varlist_size();
11525   auto UniqueDecls = C->getUniqueDeclarationsNum();
11526   auto TotalLists = C->getTotalComponentListNum();
11527   auto TotalComponents = C->getTotalComponentsNum();
11528 
11529   SmallVector<Expr *, 16> Vars;
11530   Vars.reserve(NumVars);
11531   for (unsigned I = 0; I != NumVars; ++I)
11532     Vars.push_back(Record.readSubExpr());
11533   C->setVarRefs(Vars);
11534   Vars.clear();
11535 
11536   SmallVector<ValueDecl *, 16> Decls;
11537   Decls.reserve(UniqueDecls);
11538   for (unsigned I = 0; I < UniqueDecls; ++I)
11539     Decls.push_back(Record.readDeclAs<ValueDecl>());
11540   C->setUniqueDecls(Decls);
11541 
11542   SmallVector<unsigned, 16> ListsPerDecl;
11543   ListsPerDecl.reserve(UniqueDecls);
11544   for (unsigned I = 0; I < UniqueDecls; ++I)
11545     ListsPerDecl.push_back(Record.readInt());
11546   C->setDeclNumLists(ListsPerDecl);
11547 
11548   SmallVector<unsigned, 32> ListSizes;
11549   ListSizes.reserve(TotalLists);
11550   for (unsigned i = 0; i < TotalLists; ++i)
11551     ListSizes.push_back(Record.readInt());
11552   C->setComponentListSizes(ListSizes);
11553 
11554   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11555   Components.reserve(TotalComponents);
11556   for (unsigned I = 0; I < TotalComponents; ++I) {
11557     Expr *AssociatedExpr = Record.readSubExpr();
11558     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11559     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11560                             /*IsNonContiguous=*/false);
11561   }
11562   C->setComponents(Components, ListSizes);
11563 }
11564 
VisitOMPNontemporalClause(OMPNontemporalClause * C)11565 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
11566   C->setLParenLoc(Record.readSourceLocation());
11567   unsigned NumVars = C->varlist_size();
11568   SmallVector<Expr *, 16> Vars;
11569   Vars.reserve(NumVars);
11570   for (unsigned i = 0; i != NumVars; ++i)
11571     Vars.push_back(Record.readSubExpr());
11572   C->setVarRefs(Vars);
11573   Vars.clear();
11574   Vars.reserve(NumVars);
11575   for (unsigned i = 0; i != NumVars; ++i)
11576     Vars.push_back(Record.readSubExpr());
11577   C->setPrivateRefs(Vars);
11578 }
11579 
VisitOMPInclusiveClause(OMPInclusiveClause * C)11580 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
11581   C->setLParenLoc(Record.readSourceLocation());
11582   unsigned NumVars = C->varlist_size();
11583   SmallVector<Expr *, 16> Vars;
11584   Vars.reserve(NumVars);
11585   for (unsigned i = 0; i != NumVars; ++i)
11586     Vars.push_back(Record.readSubExpr());
11587   C->setVarRefs(Vars);
11588 }
11589 
VisitOMPExclusiveClause(OMPExclusiveClause * C)11590 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
11591   C->setLParenLoc(Record.readSourceLocation());
11592   unsigned NumVars = C->varlist_size();
11593   SmallVector<Expr *, 16> Vars;
11594   Vars.reserve(NumVars);
11595   for (unsigned i = 0; i != NumVars; ++i)
11596     Vars.push_back(Record.readSubExpr());
11597   C->setVarRefs(Vars);
11598 }
11599 
VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause * C)11600 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
11601   C->setLParenLoc(Record.readSourceLocation());
11602   unsigned NumOfAllocators = C->getNumberOfAllocators();
11603   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
11604   Data.reserve(NumOfAllocators);
11605   for (unsigned I = 0; I != NumOfAllocators; ++I) {
11606     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
11607     D.Allocator = Record.readSubExpr();
11608     D.AllocatorTraits = Record.readSubExpr();
11609     D.LParenLoc = Record.readSourceLocation();
11610     D.RParenLoc = Record.readSourceLocation();
11611   }
11612   C->setAllocatorsData(Data);
11613 }
11614 
VisitOMPAffinityClause(OMPAffinityClause * C)11615 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
11616   C->setLParenLoc(Record.readSourceLocation());
11617   C->setModifier(Record.readSubExpr());
11618   C->setColonLoc(Record.readSourceLocation());
11619   unsigned NumOfLocators = C->varlist_size();
11620   SmallVector<Expr *, 4> Locators;
11621   Locators.reserve(NumOfLocators);
11622   for (unsigned I = 0; I != NumOfLocators; ++I)
11623     Locators.push_back(Record.readSubExpr());
11624   C->setVarRefs(Locators);
11625 }
11626 
VisitOMPOrderClause(OMPOrderClause * C)11627 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
11628   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
11629   C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
11630   C->setLParenLoc(Record.readSourceLocation());
11631   C->setKindKwLoc(Record.readSourceLocation());
11632   C->setModifierKwLoc(Record.readSourceLocation());
11633 }
11634 
VisitOMPFilterClause(OMPFilterClause * C)11635 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
11636   VisitOMPClauseWithPreInit(C);
11637   C->setThreadID(Record.readSubExpr());
11638   C->setLParenLoc(Record.readSourceLocation());
11639 }
11640 
VisitOMPBindClause(OMPBindClause * C)11641 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
11642   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
11643   C->setLParenLoc(Record.readSourceLocation());
11644   C->setBindKindLoc(Record.readSourceLocation());
11645 }
11646 
VisitOMPAlignClause(OMPAlignClause * C)11647 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
11648   C->setAlignment(Record.readExpr());
11649   C->setLParenLoc(Record.readSourceLocation());
11650 }
11651 
VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause * C)11652 void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
11653   VisitOMPClauseWithPreInit(C);
11654   C->setSize(Record.readSubExpr());
11655   C->setLParenLoc(Record.readSourceLocation());
11656 }
11657 
VisitOMPDoacrossClause(OMPDoacrossClause * C)11658 void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
11659   C->setLParenLoc(Record.readSourceLocation());
11660   C->setDependenceType(
11661       static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
11662   C->setDependenceLoc(Record.readSourceLocation());
11663   C->setColonLoc(Record.readSourceLocation());
11664   unsigned NumVars = C->varlist_size();
11665   SmallVector<Expr *, 16> Vars;
11666   Vars.reserve(NumVars);
11667   for (unsigned I = 0; I != NumVars; ++I)
11668     Vars.push_back(Record.readSubExpr());
11669   C->setVarRefs(Vars);
11670   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11671     C->setLoopData(I, Record.readSubExpr());
11672 }
11673 
VisitOMPXAttributeClause(OMPXAttributeClause * C)11674 void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
11675   AttrVec Attrs;
11676   Record.readAttributes(Attrs);
11677   C->setAttrs(Attrs);
11678   C->setLocStart(Record.readSourceLocation());
11679   C->setLParenLoc(Record.readSourceLocation());
11680   C->setLocEnd(Record.readSourceLocation());
11681 }
11682 
VisitOMPXBareClause(OMPXBareClause * C)11683 void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
11684 
readOMPTraitInfo()11685 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
11686   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
11687   TI.Sets.resize(readUInt32());
11688   for (auto &Set : TI.Sets) {
11689     Set.Kind = readEnum<llvm::omp::TraitSet>();
11690     Set.Selectors.resize(readUInt32());
11691     for (auto &Selector : Set.Selectors) {
11692       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
11693       Selector.ScoreOrCondition = nullptr;
11694       if (readBool())
11695         Selector.ScoreOrCondition = readExprRef();
11696       Selector.Properties.resize(readUInt32());
11697       for (auto &Property : Selector.Properties)
11698         Property.Kind = readEnum<llvm::omp::TraitProperty>();
11699     }
11700   }
11701   return &TI;
11702 }
11703 
readOMPChildren(OMPChildren * Data)11704 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
11705   if (!Data)
11706     return;
11707   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
11708     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
11709     skipInts(3);
11710   }
11711   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
11712   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
11713     Clauses[I] = readOMPClause();
11714   Data->setClauses(Clauses);
11715   if (Data->hasAssociatedStmt())
11716     Data->setAssociatedStmt(readStmt());
11717   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
11718     Data->getChildren()[I] = readStmt();
11719 }
11720