1 //===--- ASTReader.cpp - AST File Reader ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ASTReader class, which reads AST files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Serialization/ASTReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/Type.h"
24 #include "clang/AST/TypeLocVisitor.h"
25 #include "clang/Basic/DiagnosticOptions.h"
26 #include "clang/Basic/FileManager.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "clang/Basic/SourceManagerInternals.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/TargetOptions.h"
31 #include "clang/Basic/Version.h"
32 #include "clang/Basic/VersionTuple.h"
33 #include "clang/Frontend/Utils.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/HeaderSearchOptions.h"
36 #include "clang/Lex/MacroInfo.h"
37 #include "clang/Lex/PreprocessingRecord.h"
38 #include "clang/Lex/Preprocessor.h"
39 #include "clang/Lex/PreprocessorOptions.h"
40 #include "clang/Sema/Scope.h"
41 #include "clang/Sema/Sema.h"
42 #include "clang/Serialization/ASTDeserializationListener.h"
43 #include "clang/Serialization/GlobalModuleIndex.h"
44 #include "clang/Serialization/ModuleManager.h"
45 #include "clang/Serialization/SerializationDiagnostic.h"
46 #include "llvm/ADT/Hashing.h"
47 #include "llvm/ADT/StringExtras.h"
48 #include "llvm/Bitcode/BitstreamReader.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/FileSystem.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/Path.h"
53 #include "llvm/Support/SaveAndRestore.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include <algorithm>
56 #include <cstdio>
57 #include <iterator>
58 #include <system_error>
59 
60 using namespace clang;
61 using namespace clang::serialization;
62 using namespace clang::serialization::reader;
63 using llvm::BitstreamCursor;
64 
65 
66 //===----------------------------------------------------------------------===//
67 // ChainedASTReaderListener implementation
68 //===----------------------------------------------------------------------===//
69 
70 bool
ReadFullVersionInformation(StringRef FullVersion)71 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72   return First->ReadFullVersionInformation(FullVersion) ||
73          Second->ReadFullVersionInformation(FullVersion);
74 }
ReadModuleName(StringRef ModuleName)75 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76   First->ReadModuleName(ModuleName);
77   Second->ReadModuleName(ModuleName);
78 }
ReadModuleMapFile(StringRef ModuleMapPath)79 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80   First->ReadModuleMapFile(ModuleMapPath);
81   Second->ReadModuleMapFile(ModuleMapPath);
82 }
83 bool
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)84 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
85                                               bool Complain,
86                                               bool AllowCompatibleDifferences) {
87   return First->ReadLanguageOptions(LangOpts, Complain,
88                                     AllowCompatibleDifferences) ||
89          Second->ReadLanguageOptions(LangOpts, Complain,
90                                      AllowCompatibleDifferences);
91 }
92 bool
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain)93 ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
94                                             bool Complain) {
95   return First->ReadTargetOptions(TargetOpts, Complain) ||
96          Second->ReadTargetOptions(TargetOpts, Complain);
97 }
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)98 bool ChainedASTReaderListener::ReadDiagnosticOptions(
99     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
100   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
101          Second->ReadDiagnosticOptions(DiagOpts, Complain);
102 }
103 bool
ReadFileSystemOptions(const FileSystemOptions & FSOpts,bool Complain)104 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
105                                                 bool Complain) {
106   return First->ReadFileSystemOptions(FSOpts, Complain) ||
107          Second->ReadFileSystemOptions(FSOpts, Complain);
108 }
109 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,bool Complain)110 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
111     const HeaderSearchOptions &HSOpts, bool Complain) {
112   return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
113          Second->ReadHeaderSearchOptions(HSOpts, Complain);
114 }
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)115 bool ChainedASTReaderListener::ReadPreprocessorOptions(
116     const PreprocessorOptions &PPOpts, bool Complain,
117     std::string &SuggestedPredefines) {
118   return First->ReadPreprocessorOptions(PPOpts, Complain,
119                                         SuggestedPredefines) ||
120          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
121 }
ReadCounter(const serialization::ModuleFile & M,unsigned Value)122 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
123                                            unsigned Value) {
124   First->ReadCounter(M, Value);
125   Second->ReadCounter(M, Value);
126 }
needsInputFileVisitation()127 bool ChainedASTReaderListener::needsInputFileVisitation() {
128   return First->needsInputFileVisitation() ||
129          Second->needsInputFileVisitation();
130 }
needsSystemInputFileVisitation()131 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
132   return First->needsSystemInputFileVisitation() ||
133   Second->needsSystemInputFileVisitation();
134 }
visitModuleFile(StringRef Filename)135 void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
136   First->visitModuleFile(Filename);
137   Second->visitModuleFile(Filename);
138 }
visitInputFile(StringRef Filename,bool isSystem,bool isOverridden)139 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
140                                               bool isSystem,
141                                               bool isOverridden) {
142   bool Continue = false;
143   if (First->needsInputFileVisitation() &&
144       (!isSystem || First->needsSystemInputFileVisitation()))
145     Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
146   if (Second->needsInputFileVisitation() &&
147       (!isSystem || Second->needsSystemInputFileVisitation()))
148     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
149   return Continue;
150 }
151 
152 //===----------------------------------------------------------------------===//
153 // PCH validator implementation
154 //===----------------------------------------------------------------------===//
155 
~ASTReaderListener()156 ASTReaderListener::~ASTReaderListener() {}
157 
158 /// \brief Compare the given set of language options against an existing set of
159 /// language options.
160 ///
161 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
162 /// \param AllowCompatibleDifferences If true, differences between compatible
163 ///        language options will be permitted.
164 ///
165 /// \returns true if the languagae options mis-match, false otherwise.
checkLanguageOptions(const LangOptions & LangOpts,const LangOptions & ExistingLangOpts,DiagnosticsEngine * Diags,bool AllowCompatibleDifferences=true)166 static bool checkLanguageOptions(const LangOptions &LangOpts,
167                                  const LangOptions &ExistingLangOpts,
168                                  DiagnosticsEngine *Diags,
169                                  bool AllowCompatibleDifferences = true) {
170 #define LANGOPT(Name, Bits, Default, Description)                 \
171   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
172     if (Diags)                                                    \
173       Diags->Report(diag::err_pch_langopt_mismatch)               \
174         << Description << LangOpts.Name << ExistingLangOpts.Name; \
175     return true;                                                  \
176   }
177 
178 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
179   if (ExistingLangOpts.Name != LangOpts.Name) {           \
180     if (Diags)                                            \
181       Diags->Report(diag::err_pch_langopt_value_mismatch) \
182         << Description;                                   \
183     return true;                                          \
184   }
185 
186 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
187   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
188     if (Diags)                                                 \
189       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
190         << Description;                                        \
191     return true;                                               \
192   }
193 
194 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
195   if (!AllowCompatibleDifferences)                            \
196     LANGOPT(Name, Bits, Default, Description)
197 
198 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
199   if (!AllowCompatibleDifferences)                                 \
200     ENUM_LANGOPT(Name, Bits, Default, Description)
201 
202 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
203 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
204 #include "clang/Basic/LangOptions.def"
205 
206   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
207     if (Diags)
208       Diags->Report(diag::err_pch_langopt_value_mismatch)
209       << "target Objective-C runtime";
210     return true;
211   }
212 
213   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
214       LangOpts.CommentOpts.BlockCommandNames) {
215     if (Diags)
216       Diags->Report(diag::err_pch_langopt_value_mismatch)
217         << "block command names";
218     return true;
219   }
220 
221   return false;
222 }
223 
224 /// \brief Compare the given set of target options against an existing set of
225 /// target options.
226 ///
227 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
228 ///
229 /// \returns true if the target options mis-match, false otherwise.
checkTargetOptions(const TargetOptions & TargetOpts,const TargetOptions & ExistingTargetOpts,DiagnosticsEngine * Diags)230 static bool checkTargetOptions(const TargetOptions &TargetOpts,
231                                const TargetOptions &ExistingTargetOpts,
232                                DiagnosticsEngine *Diags) {
233 #define CHECK_TARGET_OPT(Field, Name)                             \
234   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
235     if (Diags)                                                    \
236       Diags->Report(diag::err_pch_targetopt_mismatch)             \
237         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
238     return true;                                                  \
239   }
240 
241   CHECK_TARGET_OPT(Triple, "target");
242   CHECK_TARGET_OPT(CPU, "target CPU");
243   CHECK_TARGET_OPT(ABI, "target ABI");
244 #undef CHECK_TARGET_OPT
245 
246   // Compare feature sets.
247   SmallVector<StringRef, 4> ExistingFeatures(
248                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
249                                              ExistingTargetOpts.FeaturesAsWritten.end());
250   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
251                                          TargetOpts.FeaturesAsWritten.end());
252   std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
253   std::sort(ReadFeatures.begin(), ReadFeatures.end());
254 
255   unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
256   unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
257   while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
258     if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
259       ++ExistingIdx;
260       ++ReadIdx;
261       continue;
262     }
263 
264     if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
265       if (Diags)
266         Diags->Report(diag::err_pch_targetopt_feature_mismatch)
267           << false << ReadFeatures[ReadIdx];
268       return true;
269     }
270 
271     if (Diags)
272       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
273         << true << ExistingFeatures[ExistingIdx];
274     return true;
275   }
276 
277   if (ExistingIdx < ExistingN) {
278     if (Diags)
279       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
280         << true << ExistingFeatures[ExistingIdx];
281     return true;
282   }
283 
284   if (ReadIdx < ReadN) {
285     if (Diags)
286       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
287         << false << ReadFeatures[ReadIdx];
288     return true;
289   }
290 
291   return false;
292 }
293 
294 bool
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)295 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
296                                   bool Complain,
297                                   bool AllowCompatibleDifferences) {
298   const LangOptions &ExistingLangOpts = PP.getLangOpts();
299   return checkLanguageOptions(LangOpts, ExistingLangOpts,
300                               Complain ? &Reader.Diags : nullptr,
301                               AllowCompatibleDifferences);
302 }
303 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain)304 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
305                                      bool Complain) {
306   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
307   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
308                             Complain? &Reader.Diags : nullptr);
309 }
310 
311 namespace {
312   typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
313     MacroDefinitionsMap;
314   typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
315     DeclsMap;
316 }
317 
checkDiagnosticGroupMappings(DiagnosticsEngine & StoredDiags,DiagnosticsEngine & Diags,bool Complain)318 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
319                                          DiagnosticsEngine &Diags,
320                                          bool Complain) {
321   typedef DiagnosticsEngine::Level Level;
322 
323   // Check current mappings for new -Werror mappings, and the stored mappings
324   // for cases that were explicitly mapped to *not* be errors that are now
325   // errors because of options like -Werror.
326   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
327 
328   for (DiagnosticsEngine *MappingSource : MappingSources) {
329     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
330       diag::kind DiagID = DiagIDMappingPair.first;
331       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
332       if (CurLevel < DiagnosticsEngine::Error)
333         continue; // not significant
334       Level StoredLevel =
335           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
336       if (StoredLevel < DiagnosticsEngine::Error) {
337         if (Complain)
338           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
339               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
340         return true;
341       }
342     }
343   }
344 
345   return false;
346 }
347 
isExtHandlingFromDiagsError(DiagnosticsEngine & Diags)348 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
349   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
350   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
351     return true;
352   return Ext >= diag::Severity::Error;
353 }
354 
checkDiagnosticMappings(DiagnosticsEngine & StoredDiags,DiagnosticsEngine & Diags,bool IsSystem,bool Complain)355 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
356                                     DiagnosticsEngine &Diags,
357                                     bool IsSystem, bool Complain) {
358   // Top-level options
359   if (IsSystem) {
360     if (Diags.getSuppressSystemWarnings())
361       return false;
362     // If -Wsystem-headers was not enabled before, be conservative
363     if (StoredDiags.getSuppressSystemWarnings()) {
364       if (Complain)
365         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
366       return true;
367     }
368   }
369 
370   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
371     if (Complain)
372       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
373     return true;
374   }
375 
376   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
377       !StoredDiags.getEnableAllWarnings()) {
378     if (Complain)
379       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
380     return true;
381   }
382 
383   if (isExtHandlingFromDiagsError(Diags) &&
384       !isExtHandlingFromDiagsError(StoredDiags)) {
385     if (Complain)
386       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
387     return true;
388   }
389 
390   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
391 }
392 
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)393 bool PCHValidator::ReadDiagnosticOptions(
394     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
395   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
396   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
397   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
398       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
399   // This should never fail, because we would have processed these options
400   // before writing them to an ASTFile.
401   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
402 
403   ModuleManager &ModuleMgr = Reader.getModuleManager();
404   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
405 
406   // If the original import came from a file explicitly generated by the user,
407   // don't check the diagnostic mappings.
408   // FIXME: currently this is approximated by checking whether this is not a
409   // module import of an implicitly-loaded module file.
410   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
411   // the transitive closure of its imports, since unrelated modules cannot be
412   // imported until after this module finishes validation.
413   ModuleFile *TopImport = *ModuleMgr.rbegin();
414   while (!TopImport->ImportedBy.empty())
415     TopImport = TopImport->ImportedBy[0];
416   if (TopImport->Kind != MK_ImplicitModule)
417     return false;
418 
419   StringRef ModuleName = TopImport->ModuleName;
420   assert(!ModuleName.empty() && "diagnostic options read before module name");
421 
422   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
423   assert(M && "missing module");
424 
425   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
426   // contains the union of their flags.
427   return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
428 }
429 
430 /// \brief Collect the macro definitions provided by the given preprocessor
431 /// options.
432 static void
collectMacroDefinitions(const PreprocessorOptions & PPOpts,MacroDefinitionsMap & Macros,SmallVectorImpl<StringRef> * MacroNames=nullptr)433 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
434                         MacroDefinitionsMap &Macros,
435                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
436   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
437     StringRef Macro = PPOpts.Macros[I].first;
438     bool IsUndef = PPOpts.Macros[I].second;
439 
440     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
441     StringRef MacroName = MacroPair.first;
442     StringRef MacroBody = MacroPair.second;
443 
444     // For an #undef'd macro, we only care about the name.
445     if (IsUndef) {
446       if (MacroNames && !Macros.count(MacroName))
447         MacroNames->push_back(MacroName);
448 
449       Macros[MacroName] = std::make_pair("", true);
450       continue;
451     }
452 
453     // For a #define'd macro, figure out the actual definition.
454     if (MacroName.size() == Macro.size())
455       MacroBody = "1";
456     else {
457       // Note: GCC drops anything following an end-of-line character.
458       StringRef::size_type End = MacroBody.find_first_of("\n\r");
459       MacroBody = MacroBody.substr(0, End);
460     }
461 
462     if (MacroNames && !Macros.count(MacroName))
463       MacroNames->push_back(MacroName);
464     Macros[MacroName] = std::make_pair(MacroBody, false);
465   }
466 }
467 
468 /// \brief Check the preprocessor options deserialized from the control block
469 /// against the preprocessor options in an existing preprocessor.
470 ///
471 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
checkPreprocessorOptions(const PreprocessorOptions & PPOpts,const PreprocessorOptions & ExistingPPOpts,DiagnosticsEngine * Diags,FileManager & FileMgr,std::string & SuggestedPredefines,const LangOptions & LangOpts)472 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
473                                      const PreprocessorOptions &ExistingPPOpts,
474                                      DiagnosticsEngine *Diags,
475                                      FileManager &FileMgr,
476                                      std::string &SuggestedPredefines,
477                                      const LangOptions &LangOpts) {
478   // Check macro definitions.
479   MacroDefinitionsMap ASTFileMacros;
480   collectMacroDefinitions(PPOpts, ASTFileMacros);
481   MacroDefinitionsMap ExistingMacros;
482   SmallVector<StringRef, 4> ExistingMacroNames;
483   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
484 
485   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
486     // Dig out the macro definition in the existing preprocessor options.
487     StringRef MacroName = ExistingMacroNames[I];
488     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
489 
490     // Check whether we know anything about this macro name or not.
491     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
492       = ASTFileMacros.find(MacroName);
493     if (Known == ASTFileMacros.end()) {
494       // FIXME: Check whether this identifier was referenced anywhere in the
495       // AST file. If so, we should reject the AST file. Unfortunately, this
496       // information isn't in the control block. What shall we do about it?
497 
498       if (Existing.second) {
499         SuggestedPredefines += "#undef ";
500         SuggestedPredefines += MacroName.str();
501         SuggestedPredefines += '\n';
502       } else {
503         SuggestedPredefines += "#define ";
504         SuggestedPredefines += MacroName.str();
505         SuggestedPredefines += ' ';
506         SuggestedPredefines += Existing.first.str();
507         SuggestedPredefines += '\n';
508       }
509       continue;
510     }
511 
512     // If the macro was defined in one but undef'd in the other, we have a
513     // conflict.
514     if (Existing.second != Known->second.second) {
515       if (Diags) {
516         Diags->Report(diag::err_pch_macro_def_undef)
517           << MacroName << Known->second.second;
518       }
519       return true;
520     }
521 
522     // If the macro was #undef'd in both, or if the macro bodies are identical,
523     // it's fine.
524     if (Existing.second || Existing.first == Known->second.first)
525       continue;
526 
527     // The macro bodies differ; complain.
528     if (Diags) {
529       Diags->Report(diag::err_pch_macro_def_conflict)
530         << MacroName << Known->second.first << Existing.first;
531     }
532     return true;
533   }
534 
535   // Check whether we're using predefines.
536   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
537     if (Diags) {
538       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
539     }
540     return true;
541   }
542 
543   // Detailed record is important since it is used for the module cache hash.
544   if (LangOpts.Modules &&
545       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
546     if (Diags) {
547       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
548     }
549     return true;
550   }
551 
552   // Compute the #include and #include_macros lines we need.
553   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
554     StringRef File = ExistingPPOpts.Includes[I];
555     if (File == ExistingPPOpts.ImplicitPCHInclude)
556       continue;
557 
558     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
559           != PPOpts.Includes.end())
560       continue;
561 
562     SuggestedPredefines += "#include \"";
563     SuggestedPredefines += File;
564     SuggestedPredefines += "\"\n";
565   }
566 
567   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
568     StringRef File = ExistingPPOpts.MacroIncludes[I];
569     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
570                   File)
571         != PPOpts.MacroIncludes.end())
572       continue;
573 
574     SuggestedPredefines += "#__include_macros \"";
575     SuggestedPredefines += File;
576     SuggestedPredefines += "\"\n##\n";
577   }
578 
579   return false;
580 }
581 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)582 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
583                                            bool Complain,
584                                            std::string &SuggestedPredefines) {
585   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
586 
587   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
588                                   Complain? &Reader.Diags : nullptr,
589                                   PP.getFileManager(),
590                                   SuggestedPredefines,
591                                   PP.getLangOpts());
592 }
593 
ReadCounter(const ModuleFile & M,unsigned Value)594 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
595   PP.setCounterValue(Value);
596 }
597 
598 //===----------------------------------------------------------------------===//
599 // AST reader implementation
600 //===----------------------------------------------------------------------===//
601 
setDeserializationListener(ASTDeserializationListener * Listener,bool TakeOwnership)602 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
603                                            bool TakeOwnership) {
604   DeserializationListener = Listener;
605   OwnsDeserializationListener = TakeOwnership;
606 }
607 
608 
609 
ComputeHash(Selector Sel)610 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
611   return serialization::ComputeHash(Sel);
612 }
613 
614 
615 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)616 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
617   using namespace llvm::support;
618   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
619   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
620   return std::make_pair(KeyLen, DataLen);
621 }
622 
623 ASTSelectorLookupTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)624 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
625   using namespace llvm::support;
626   SelectorTable &SelTable = Reader.getContext().Selectors;
627   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
628   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
629       F, endian::readNext<uint32_t, little, unaligned>(d));
630   if (N == 0)
631     return SelTable.getNullarySelector(FirstII);
632   else if (N == 1)
633     return SelTable.getUnarySelector(FirstII);
634 
635   SmallVector<IdentifierInfo *, 16> Args;
636   Args.push_back(FirstII);
637   for (unsigned I = 1; I != N; ++I)
638     Args.push_back(Reader.getLocalIdentifier(
639         F, endian::readNext<uint32_t, little, unaligned>(d)));
640 
641   return SelTable.getSelector(N, Args.data());
642 }
643 
644 ASTSelectorLookupTrait::data_type
ReadData(Selector,const unsigned char * d,unsigned DataLen)645 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
646                                  unsigned DataLen) {
647   using namespace llvm::support;
648 
649   data_type Result;
650 
651   Result.ID = Reader.getGlobalSelectorID(
652       F, endian::readNext<uint32_t, little, unaligned>(d));
653   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
654   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
655   Result.InstanceBits = FullInstanceBits & 0x3;
656   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
657   Result.FactoryBits = FullFactoryBits & 0x3;
658   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
659   unsigned NumInstanceMethods = FullInstanceBits >> 3;
660   unsigned NumFactoryMethods = FullFactoryBits >> 3;
661 
662   // Load instance methods
663   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
664     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
665             F, endian::readNext<uint32_t, little, unaligned>(d)))
666       Result.Instance.push_back(Method);
667   }
668 
669   // Load factory methods
670   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
671     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
672             F, endian::readNext<uint32_t, little, unaligned>(d)))
673       Result.Factory.push_back(Method);
674   }
675 
676   return Result;
677 }
678 
ComputeHash(const internal_key_type & a)679 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
680   return llvm::HashString(a);
681 }
682 
683 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)684 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
685   using namespace llvm::support;
686   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
687   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
688   return std::make_pair(KeyLen, DataLen);
689 }
690 
691 ASTIdentifierLookupTraitBase::internal_key_type
ReadKey(const unsigned char * d,unsigned n)692 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
693   assert(n >= 2 && d[n-1] == '\0');
694   return StringRef((const char*) d, n-1);
695 }
696 
697 /// \brief Whether the given identifier is "interesting".
isInterestingIdentifier(IdentifierInfo & II)698 static bool isInterestingIdentifier(IdentifierInfo &II) {
699   return II.isPoisoned() ||
700          II.isExtensionToken() ||
701          II.getObjCOrBuiltinID() ||
702          II.hasRevertedTokenIDToIdentifier() ||
703          II.hadMacroDefinition() ||
704          II.getFETokenInfo<void>();
705 }
706 
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)707 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
708                                                    const unsigned char* d,
709                                                    unsigned DataLen) {
710   using namespace llvm::support;
711   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
712   bool IsInteresting = RawID & 0x01;
713 
714   // Wipe out the "is interesting" bit.
715   RawID = RawID >> 1;
716 
717   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
718   if (!IsInteresting) {
719     // For uninteresting identifiers, just build the IdentifierInfo
720     // and associate it with the persistent ID.
721     IdentifierInfo *II = KnownII;
722     if (!II) {
723       II = &Reader.getIdentifierTable().getOwn(k);
724       KnownII = II;
725     }
726     Reader.SetIdentifierInfo(ID, II);
727     if (!II->isFromAST()) {
728       bool WasInteresting = isInterestingIdentifier(*II);
729       II->setIsFromAST();
730       if (WasInteresting)
731         II->setChangedSinceDeserialization();
732     }
733     Reader.markIdentifierUpToDate(II);
734     return II;
735   }
736 
737   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
738   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
739   bool CPlusPlusOperatorKeyword = Bits & 0x01;
740   Bits >>= 1;
741   bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
742   Bits >>= 1;
743   bool Poisoned = Bits & 0x01;
744   Bits >>= 1;
745   bool ExtensionToken = Bits & 0x01;
746   Bits >>= 1;
747   bool hasSubmoduleMacros = Bits & 0x01;
748   Bits >>= 1;
749   bool hadMacroDefinition = Bits & 0x01;
750   Bits >>= 1;
751 
752   assert(Bits == 0 && "Extra bits in the identifier?");
753   DataLen -= 8;
754 
755   // Build the IdentifierInfo itself and link the identifier ID with
756   // the new IdentifierInfo.
757   IdentifierInfo *II = KnownII;
758   if (!II) {
759     II = &Reader.getIdentifierTable().getOwn(StringRef(k));
760     KnownII = II;
761   }
762   Reader.markIdentifierUpToDate(II);
763   if (!II->isFromAST()) {
764     bool WasInteresting = isInterestingIdentifier(*II);
765     II->setIsFromAST();
766     if (WasInteresting)
767       II->setChangedSinceDeserialization();
768   }
769 
770   // Set or check the various bits in the IdentifierInfo structure.
771   // Token IDs are read-only.
772   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
773     II->RevertTokenIDToIdentifier();
774   II->setObjCOrBuiltinID(ObjCOrBuiltinID);
775   assert(II->isExtensionToken() == ExtensionToken &&
776          "Incorrect extension token flag");
777   (void)ExtensionToken;
778   if (Poisoned)
779     II->setIsPoisoned(true);
780   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
781          "Incorrect C++ operator keyword flag");
782   (void)CPlusPlusOperatorKeyword;
783 
784   // If this identifier is a macro, deserialize the macro
785   // definition.
786   if (hadMacroDefinition) {
787     uint32_t MacroDirectivesOffset =
788         endian::readNext<uint32_t, little, unaligned>(d);
789     DataLen -= 4;
790     SmallVector<uint32_t, 8> LocalMacroIDs;
791     if (hasSubmoduleMacros) {
792       while (true) {
793         uint32_t LocalMacroID =
794             endian::readNext<uint32_t, little, unaligned>(d);
795         DataLen -= 4;
796         if (LocalMacroID == 0xdeadbeef) break;
797         LocalMacroIDs.push_back(LocalMacroID);
798       }
799     }
800 
801     if (F.Kind == MK_ImplicitModule || F.Kind == MK_ExplicitModule) {
802       // Macro definitions are stored from newest to oldest, so reverse them
803       // before registering them.
804       llvm::SmallVector<unsigned, 8> MacroSizes;
805       for (SmallVectorImpl<uint32_t>::iterator
806              I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
807         unsigned Size = 1;
808 
809         static const uint32_t HasOverridesFlag = 0x80000000U;
810         if (I + 1 != E && (I[1] & HasOverridesFlag))
811           Size += 1 + (I[1] & ~HasOverridesFlag);
812 
813         MacroSizes.push_back(Size);
814         I += Size;
815       }
816 
817       SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
818       for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
819                                                        SE = MacroSizes.rend();
820            SI != SE; ++SI) {
821         I -= *SI;
822 
823         uint32_t LocalMacroID = *I;
824         ArrayRef<uint32_t> Overrides;
825         if (*SI != 1)
826           Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
827         Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
828       }
829       assert(I == LocalMacroIDs.begin());
830     } else {
831       Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
832     }
833   }
834 
835   Reader.SetIdentifierInfo(ID, II);
836 
837   // Read all of the declarations visible at global scope with this
838   // name.
839   if (DataLen > 0) {
840     SmallVector<uint32_t, 4> DeclIDs;
841     for (; DataLen > 0; DataLen -= 4)
842       DeclIDs.push_back(Reader.getGlobalDeclID(
843           F, endian::readNext<uint32_t, little, unaligned>(d)));
844     Reader.SetGloballyVisibleDecls(II, DeclIDs);
845   }
846 
847   return II;
848 }
849 
850 unsigned
ComputeHash(const DeclNameKey & Key) const851 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
852   llvm::FoldingSetNodeID ID;
853   ID.AddInteger(Key.Kind);
854 
855   switch (Key.Kind) {
856   case DeclarationName::Identifier:
857   case DeclarationName::CXXLiteralOperatorName:
858     ID.AddString(((IdentifierInfo*)Key.Data)->getName());
859     break;
860   case DeclarationName::ObjCZeroArgSelector:
861   case DeclarationName::ObjCOneArgSelector:
862   case DeclarationName::ObjCMultiArgSelector:
863     ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
864     break;
865   case DeclarationName::CXXOperatorName:
866     ID.AddInteger((OverloadedOperatorKind)Key.Data);
867     break;
868   case DeclarationName::CXXConstructorName:
869   case DeclarationName::CXXDestructorName:
870   case DeclarationName::CXXConversionFunctionName:
871   case DeclarationName::CXXUsingDirective:
872     break;
873   }
874 
875   return ID.ComputeHash();
876 }
877 
878 ASTDeclContextNameLookupTrait::internal_key_type
GetInternalKey(const external_key_type & Name) const879 ASTDeclContextNameLookupTrait::GetInternalKey(
880                                           const external_key_type& Name) const {
881   DeclNameKey Key;
882   Key.Kind = Name.getNameKind();
883   switch (Name.getNameKind()) {
884   case DeclarationName::Identifier:
885     Key.Data = (uint64_t)Name.getAsIdentifierInfo();
886     break;
887   case DeclarationName::ObjCZeroArgSelector:
888   case DeclarationName::ObjCOneArgSelector:
889   case DeclarationName::ObjCMultiArgSelector:
890     Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
891     break;
892   case DeclarationName::CXXOperatorName:
893     Key.Data = Name.getCXXOverloadedOperator();
894     break;
895   case DeclarationName::CXXLiteralOperatorName:
896     Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
897     break;
898   case DeclarationName::CXXConstructorName:
899   case DeclarationName::CXXDestructorName:
900   case DeclarationName::CXXConversionFunctionName:
901   case DeclarationName::CXXUsingDirective:
902     Key.Data = 0;
903     break;
904   }
905 
906   return Key;
907 }
908 
909 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)910 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
911   using namespace llvm::support;
912   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
913   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
914   return std::make_pair(KeyLen, DataLen);
915 }
916 
917 ASTDeclContextNameLookupTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)918 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
919   using namespace llvm::support;
920 
921   DeclNameKey Key;
922   Key.Kind = (DeclarationName::NameKind)*d++;
923   switch (Key.Kind) {
924   case DeclarationName::Identifier:
925     Key.Data = (uint64_t)Reader.getLocalIdentifier(
926         F, endian::readNext<uint32_t, little, unaligned>(d));
927     break;
928   case DeclarationName::ObjCZeroArgSelector:
929   case DeclarationName::ObjCOneArgSelector:
930   case DeclarationName::ObjCMultiArgSelector:
931     Key.Data =
932         (uint64_t)Reader.getLocalSelector(
933                              F, endian::readNext<uint32_t, little, unaligned>(
934                                     d)).getAsOpaquePtr();
935     break;
936   case DeclarationName::CXXOperatorName:
937     Key.Data = *d++; // OverloadedOperatorKind
938     break;
939   case DeclarationName::CXXLiteralOperatorName:
940     Key.Data = (uint64_t)Reader.getLocalIdentifier(
941         F, endian::readNext<uint32_t, little, unaligned>(d));
942     break;
943   case DeclarationName::CXXConstructorName:
944   case DeclarationName::CXXDestructorName:
945   case DeclarationName::CXXConversionFunctionName:
946   case DeclarationName::CXXUsingDirective:
947     Key.Data = 0;
948     break;
949   }
950 
951   return Key;
952 }
953 
954 ASTDeclContextNameLookupTrait::data_type
ReadData(internal_key_type,const unsigned char * d,unsigned DataLen)955 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
956                                         const unsigned char* d,
957                                         unsigned DataLen) {
958   using namespace llvm::support;
959   unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
960   LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
961                         const_cast<unsigned char *>(d));
962   return std::make_pair(Start, Start + NumDecls);
963 }
964 
ReadDeclContextStorage(ModuleFile & M,BitstreamCursor & Cursor,const std::pair<uint64_t,uint64_t> & Offsets,DeclContextInfo & Info)965 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
966                                        BitstreamCursor &Cursor,
967                                    const std::pair<uint64_t, uint64_t> &Offsets,
968                                        DeclContextInfo &Info) {
969   SavedStreamPosition SavedPosition(Cursor);
970   // First the lexical decls.
971   if (Offsets.first != 0) {
972     Cursor.JumpToBit(Offsets.first);
973 
974     RecordData Record;
975     StringRef Blob;
976     unsigned Code = Cursor.ReadCode();
977     unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
978     if (RecCode != DECL_CONTEXT_LEXICAL) {
979       Error("Expected lexical block");
980       return true;
981     }
982 
983     Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
984     Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
985   }
986 
987   // Now the lookup table.
988   if (Offsets.second != 0) {
989     Cursor.JumpToBit(Offsets.second);
990 
991     RecordData Record;
992     StringRef Blob;
993     unsigned Code = Cursor.ReadCode();
994     unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
995     if (RecCode != DECL_CONTEXT_VISIBLE) {
996       Error("Expected visible lookup table block");
997       return true;
998     }
999     Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
1000         (const unsigned char *)Blob.data() + Record[0],
1001         (const unsigned char *)Blob.data() + sizeof(uint32_t),
1002         (const unsigned char *)Blob.data(),
1003         ASTDeclContextNameLookupTrait(*this, M));
1004   }
1005 
1006   return false;
1007 }
1008 
Error(StringRef Msg)1009 void ASTReader::Error(StringRef Msg) {
1010   Error(diag::err_fe_pch_malformed, Msg);
1011   if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1012     Diag(diag::note_module_cache_path)
1013       << PP.getHeaderSearchInfo().getModuleCachePath();
1014   }
1015 }
1016 
Error(unsigned DiagID,StringRef Arg1,StringRef Arg2)1017 void ASTReader::Error(unsigned DiagID,
1018                       StringRef Arg1, StringRef Arg2) {
1019   if (Diags.isDiagnosticInFlight())
1020     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1021   else
1022     Diag(DiagID) << Arg1 << Arg2;
1023 }
1024 
1025 //===----------------------------------------------------------------------===//
1026 // Source Manager Deserialization
1027 //===----------------------------------------------------------------------===//
1028 
1029 /// \brief Read the line table in the source manager block.
1030 /// \returns true if there was an error.
ParseLineTable(ModuleFile & F,const RecordData & Record)1031 bool ASTReader::ParseLineTable(ModuleFile &F,
1032                                const RecordData &Record) {
1033   unsigned Idx = 0;
1034   LineTableInfo &LineTable = SourceMgr.getLineTable();
1035 
1036   // Parse the file names
1037   std::map<int, int> FileIDs;
1038   for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1039     // Extract the file name
1040     auto Filename = ReadPath(F, Record, Idx);
1041     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1042   }
1043 
1044   // Parse the line entries
1045   std::vector<LineEntry> Entries;
1046   while (Idx < Record.size()) {
1047     int FID = Record[Idx++];
1048     assert(FID >= 0 && "Serialized line entries for non-local file.");
1049     // Remap FileID from 1-based old view.
1050     FID += F.SLocEntryBaseID - 1;
1051 
1052     // Extract the line entries
1053     unsigned NumEntries = Record[Idx++];
1054     assert(NumEntries && "Numentries is 00000");
1055     Entries.clear();
1056     Entries.reserve(NumEntries);
1057     for (unsigned I = 0; I != NumEntries; ++I) {
1058       unsigned FileOffset = Record[Idx++];
1059       unsigned LineNo = Record[Idx++];
1060       int FilenameID = FileIDs[Record[Idx++]];
1061       SrcMgr::CharacteristicKind FileKind
1062         = (SrcMgr::CharacteristicKind)Record[Idx++];
1063       unsigned IncludeOffset = Record[Idx++];
1064       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1065                                        FileKind, IncludeOffset));
1066     }
1067     LineTable.AddEntry(FileID::get(FID), Entries);
1068   }
1069 
1070   return false;
1071 }
1072 
1073 /// \brief Read a source manager block
ReadSourceManagerBlock(ModuleFile & F)1074 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1075   using namespace SrcMgr;
1076 
1077   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1078 
1079   // Set the source-location entry cursor to the current position in
1080   // the stream. This cursor will be used to read the contents of the
1081   // source manager block initially, and then lazily read
1082   // source-location entries as needed.
1083   SLocEntryCursor = F.Stream;
1084 
1085   // The stream itself is going to skip over the source manager block.
1086   if (F.Stream.SkipBlock()) {
1087     Error("malformed block record in AST file");
1088     return true;
1089   }
1090 
1091   // Enter the source manager block.
1092   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1093     Error("malformed source manager block record in AST file");
1094     return true;
1095   }
1096 
1097   RecordData Record;
1098   while (true) {
1099     llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1100 
1101     switch (E.Kind) {
1102     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1103     case llvm::BitstreamEntry::Error:
1104       Error("malformed block record in AST file");
1105       return true;
1106     case llvm::BitstreamEntry::EndBlock:
1107       return false;
1108     case llvm::BitstreamEntry::Record:
1109       // The interesting case.
1110       break;
1111     }
1112 
1113     // Read a record.
1114     Record.clear();
1115     StringRef Blob;
1116     switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1117     default:  // Default behavior: ignore.
1118       break;
1119 
1120     case SM_SLOC_FILE_ENTRY:
1121     case SM_SLOC_BUFFER_ENTRY:
1122     case SM_SLOC_EXPANSION_ENTRY:
1123       // Once we hit one of the source location entries, we're done.
1124       return false;
1125     }
1126   }
1127 }
1128 
1129 /// \brief If a header file is not found at the path that we expect it to be
1130 /// and the PCH file was moved from its original location, try to resolve the
1131 /// file by assuming that header+PCH were moved together and the header is in
1132 /// the same place relative to the PCH.
1133 static std::string
resolveFileRelativeToOriginalDir(const std::string & Filename,const std::string & OriginalDir,const std::string & CurrDir)1134 resolveFileRelativeToOriginalDir(const std::string &Filename,
1135                                  const std::string &OriginalDir,
1136                                  const std::string &CurrDir) {
1137   assert(OriginalDir != CurrDir &&
1138          "No point trying to resolve the file if the PCH dir didn't change");
1139   using namespace llvm::sys;
1140   SmallString<128> filePath(Filename);
1141   fs::make_absolute(filePath);
1142   assert(path::is_absolute(OriginalDir));
1143   SmallString<128> currPCHPath(CurrDir);
1144 
1145   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1146                        fileDirE = path::end(path::parent_path(filePath));
1147   path::const_iterator origDirI = path::begin(OriginalDir),
1148                        origDirE = path::end(OriginalDir);
1149   // Skip the common path components from filePath and OriginalDir.
1150   while (fileDirI != fileDirE && origDirI != origDirE &&
1151          *fileDirI == *origDirI) {
1152     ++fileDirI;
1153     ++origDirI;
1154   }
1155   for (; origDirI != origDirE; ++origDirI)
1156     path::append(currPCHPath, "..");
1157   path::append(currPCHPath, fileDirI, fileDirE);
1158   path::append(currPCHPath, path::filename(Filename));
1159   return currPCHPath.str();
1160 }
1161 
ReadSLocEntry(int ID)1162 bool ASTReader::ReadSLocEntry(int ID) {
1163   if (ID == 0)
1164     return false;
1165 
1166   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1167     Error("source location entry ID out-of-range for AST file");
1168     return true;
1169   }
1170 
1171   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1172   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1173   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1174   unsigned BaseOffset = F->SLocEntryBaseOffset;
1175 
1176   ++NumSLocEntriesRead;
1177   llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1178   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1179     Error("incorrectly-formatted source location entry in AST file");
1180     return true;
1181   }
1182 
1183   RecordData Record;
1184   StringRef Blob;
1185   switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1186   default:
1187     Error("incorrectly-formatted source location entry in AST file");
1188     return true;
1189 
1190   case SM_SLOC_FILE_ENTRY: {
1191     // We will detect whether a file changed and return 'Failure' for it, but
1192     // we will also try to fail gracefully by setting up the SLocEntry.
1193     unsigned InputID = Record[4];
1194     InputFile IF = getInputFile(*F, InputID);
1195     const FileEntry *File = IF.getFile();
1196     bool OverriddenBuffer = IF.isOverridden();
1197 
1198     // Note that we only check if a File was returned. If it was out-of-date
1199     // we have complained but we will continue creating a FileID to recover
1200     // gracefully.
1201     if (!File)
1202       return true;
1203 
1204     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1205     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1206       // This is the module's main file.
1207       IncludeLoc = getImportLocation(F);
1208     }
1209     SrcMgr::CharacteristicKind
1210       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1211     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1212                                         ID, BaseOffset + Record[0]);
1213     SrcMgr::FileInfo &FileInfo =
1214           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1215     FileInfo.NumCreatedFIDs = Record[5];
1216     if (Record[3])
1217       FileInfo.setHasLineDirectives();
1218 
1219     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1220     unsigned NumFileDecls = Record[7];
1221     if (NumFileDecls) {
1222       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1223       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1224                                                              NumFileDecls));
1225     }
1226 
1227     const SrcMgr::ContentCache *ContentCache
1228       = SourceMgr.getOrCreateContentCache(File,
1229                               /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1230     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1231         ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1232       unsigned Code = SLocEntryCursor.ReadCode();
1233       Record.clear();
1234       unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1235 
1236       if (RecCode != SM_SLOC_BUFFER_BLOB) {
1237         Error("AST record has invalid code");
1238         return true;
1239       }
1240 
1241       std::unique_ptr<llvm::MemoryBuffer> Buffer
1242         = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
1243       SourceMgr.overrideFileContents(File, std::move(Buffer));
1244     }
1245 
1246     break;
1247   }
1248 
1249   case SM_SLOC_BUFFER_ENTRY: {
1250     const char *Name = Blob.data();
1251     unsigned Offset = Record[0];
1252     SrcMgr::CharacteristicKind
1253       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1254     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1255     if (IncludeLoc.isInvalid() &&
1256         (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
1257       IncludeLoc = getImportLocation(F);
1258     }
1259     unsigned Code = SLocEntryCursor.ReadCode();
1260     Record.clear();
1261     unsigned RecCode
1262       = SLocEntryCursor.readRecord(Code, Record, &Blob);
1263 
1264     if (RecCode != SM_SLOC_BUFFER_BLOB) {
1265       Error("AST record has invalid code");
1266       return true;
1267     }
1268 
1269     std::unique_ptr<llvm::MemoryBuffer> Buffer =
1270         llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1271     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1272                            BaseOffset + Offset, IncludeLoc);
1273     break;
1274   }
1275 
1276   case SM_SLOC_EXPANSION_ENTRY: {
1277     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1278     SourceMgr.createExpansionLoc(SpellingLoc,
1279                                      ReadSourceLocation(*F, Record[2]),
1280                                      ReadSourceLocation(*F, Record[3]),
1281                                      Record[4],
1282                                      ID,
1283                                      BaseOffset + Record[0]);
1284     break;
1285   }
1286   }
1287 
1288   return false;
1289 }
1290 
getModuleImportLoc(int ID)1291 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1292   if (ID == 0)
1293     return std::make_pair(SourceLocation(), "");
1294 
1295   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1296     Error("source location entry ID out-of-range for AST file");
1297     return std::make_pair(SourceLocation(), "");
1298   }
1299 
1300   // Find which module file this entry lands in.
1301   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1302   if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
1303     return std::make_pair(SourceLocation(), "");
1304 
1305   // FIXME: Can we map this down to a particular submodule? That would be
1306   // ideal.
1307   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1308 }
1309 
1310 /// \brief Find the location where the module F is imported.
getImportLocation(ModuleFile * F)1311 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1312   if (F->ImportLoc.isValid())
1313     return F->ImportLoc;
1314 
1315   // Otherwise we have a PCH. It's considered to be "imported" at the first
1316   // location of its includer.
1317   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1318     // Main file is the importer.
1319     assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1320     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1321   }
1322   return F->ImportedBy[0]->FirstLoc;
1323 }
1324 
1325 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1326 /// specified cursor.  Read the abbreviations that are at the top of the block
1327 /// and then leave the cursor pointing into the block.
ReadBlockAbbrevs(BitstreamCursor & Cursor,unsigned BlockID)1328 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1329   if (Cursor.EnterSubBlock(BlockID)) {
1330     Error("malformed block record in AST file");
1331     return Failure;
1332   }
1333 
1334   while (true) {
1335     uint64_t Offset = Cursor.GetCurrentBitNo();
1336     unsigned Code = Cursor.ReadCode();
1337 
1338     // We expect all abbrevs to be at the start of the block.
1339     if (Code != llvm::bitc::DEFINE_ABBREV) {
1340       Cursor.JumpToBit(Offset);
1341       return false;
1342     }
1343     Cursor.ReadAbbrevRecord();
1344   }
1345 }
1346 
ReadToken(ModuleFile & F,const RecordDataImpl & Record,unsigned & Idx)1347 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1348                            unsigned &Idx) {
1349   Token Tok;
1350   Tok.startToken();
1351   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1352   Tok.setLength(Record[Idx++]);
1353   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1354     Tok.setIdentifierInfo(II);
1355   Tok.setKind((tok::TokenKind)Record[Idx++]);
1356   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1357   return Tok;
1358 }
1359 
ReadMacroRecord(ModuleFile & F,uint64_t Offset)1360 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1361   BitstreamCursor &Stream = F.MacroCursor;
1362 
1363   // Keep track of where we are in the stream, then jump back there
1364   // after reading this macro.
1365   SavedStreamPosition SavedPosition(Stream);
1366 
1367   Stream.JumpToBit(Offset);
1368   RecordData Record;
1369   SmallVector<IdentifierInfo*, 16> MacroArgs;
1370   MacroInfo *Macro = nullptr;
1371 
1372   while (true) {
1373     // Advance to the next record, but if we get to the end of the block, don't
1374     // pop it (removing all the abbreviations from the cursor) since we want to
1375     // be able to reseek within the block and read entries.
1376     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1377     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1378 
1379     switch (Entry.Kind) {
1380     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1381     case llvm::BitstreamEntry::Error:
1382       Error("malformed block record in AST file");
1383       return Macro;
1384     case llvm::BitstreamEntry::EndBlock:
1385       return Macro;
1386     case llvm::BitstreamEntry::Record:
1387       // The interesting case.
1388       break;
1389     }
1390 
1391     // Read a record.
1392     Record.clear();
1393     PreprocessorRecordTypes RecType =
1394       (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1395     switch (RecType) {
1396     case PP_MACRO_DIRECTIVE_HISTORY:
1397       return Macro;
1398 
1399     case PP_MACRO_OBJECT_LIKE:
1400     case PP_MACRO_FUNCTION_LIKE: {
1401       // If we already have a macro, that means that we've hit the end
1402       // of the definition of the macro we were looking for. We're
1403       // done.
1404       if (Macro)
1405         return Macro;
1406 
1407       unsigned NextIndex = 1; // Skip identifier ID.
1408       SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1409       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1410       MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1411       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1412       MI->setIsUsed(Record[NextIndex++]);
1413       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1414 
1415       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1416         // Decode function-like macro info.
1417         bool isC99VarArgs = Record[NextIndex++];
1418         bool isGNUVarArgs = Record[NextIndex++];
1419         bool hasCommaPasting = Record[NextIndex++];
1420         MacroArgs.clear();
1421         unsigned NumArgs = Record[NextIndex++];
1422         for (unsigned i = 0; i != NumArgs; ++i)
1423           MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1424 
1425         // Install function-like macro info.
1426         MI->setIsFunctionLike();
1427         if (isC99VarArgs) MI->setIsC99Varargs();
1428         if (isGNUVarArgs) MI->setIsGNUVarargs();
1429         if (hasCommaPasting) MI->setHasCommaPasting();
1430         MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1431                             PP.getPreprocessorAllocator());
1432       }
1433 
1434       // Remember that we saw this macro last so that we add the tokens that
1435       // form its body to it.
1436       Macro = MI;
1437 
1438       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1439           Record[NextIndex]) {
1440         // We have a macro definition. Register the association
1441         PreprocessedEntityID
1442             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1443         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1444         PreprocessingRecord::PPEntityID
1445           PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1446         MacroDefinition *PPDef =
1447           cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1448         if (PPDef)
1449           PPRec.RegisterMacroDefinition(Macro, PPDef);
1450       }
1451 
1452       ++NumMacrosRead;
1453       break;
1454     }
1455 
1456     case PP_TOKEN: {
1457       // If we see a TOKEN before a PP_MACRO_*, then the file is
1458       // erroneous, just pretend we didn't see this.
1459       if (!Macro) break;
1460 
1461       unsigned Idx = 0;
1462       Token Tok = ReadToken(F, Record, Idx);
1463       Macro->AddTokenToBody(Tok);
1464       break;
1465     }
1466     }
1467   }
1468 }
1469 
1470 PreprocessedEntityID
getGlobalPreprocessedEntityID(ModuleFile & M,unsigned LocalID) const1471 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1472   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1473     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1474   assert(I != M.PreprocessedEntityRemap.end()
1475          && "Invalid index into preprocessed entity index remap");
1476 
1477   return LocalID + I->second;
1478 }
1479 
ComputeHash(internal_key_ref ikey)1480 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1481   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1482 }
1483 
1484 HeaderFileInfoTrait::internal_key_type
GetInternalKey(const FileEntry * FE)1485 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1486   internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1487                              FE->getName(), /*Imported*/false };
1488   return ikey;
1489 }
1490 
EqualKey(internal_key_ref a,internal_key_ref b)1491 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1492   if (a.Size != b.Size || a.ModTime != b.ModTime)
1493     return false;
1494 
1495   if (llvm::sys::path::is_absolute(a.Filename) &&
1496       strcmp(a.Filename, b.Filename) == 0)
1497     return true;
1498 
1499   // Determine whether the actual files are equivalent.
1500   FileManager &FileMgr = Reader.getFileManager();
1501   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1502     if (!Key.Imported)
1503       return FileMgr.getFile(Key.Filename);
1504 
1505     std::string Resolved = Key.Filename;
1506     Reader.ResolveImportedPath(M, Resolved);
1507     return FileMgr.getFile(Resolved);
1508   };
1509 
1510   const FileEntry *FEA = GetFile(a);
1511   const FileEntry *FEB = GetFile(b);
1512   return FEA && FEA == FEB;
1513 }
1514 
1515 std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)1516 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1517   using namespace llvm::support;
1518   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1519   unsigned DataLen = (unsigned) *d++;
1520   return std::make_pair(KeyLen, DataLen);
1521 }
1522 
1523 HeaderFileInfoTrait::internal_key_type
ReadKey(const unsigned char * d,unsigned)1524 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1525   using namespace llvm::support;
1526   internal_key_type ikey;
1527   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1528   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1529   ikey.Filename = (const char *)d;
1530   ikey.Imported = true;
1531   return ikey;
1532 }
1533 
1534 HeaderFileInfoTrait::data_type
ReadData(internal_key_ref key,const unsigned char * d,unsigned DataLen)1535 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1536                               unsigned DataLen) {
1537   const unsigned char *End = d + DataLen;
1538   using namespace llvm::support;
1539   HeaderFileInfo HFI;
1540   unsigned Flags = *d++;
1541   HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1542                    ((Flags >> 6) & 0x03);
1543   HFI.isImport = (Flags >> 5) & 0x01;
1544   HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1545   HFI.DirInfo = (Flags >> 2) & 0x03;
1546   HFI.Resolved = (Flags >> 1) & 0x01;
1547   HFI.IndexHeaderMapHeader = Flags & 0x01;
1548   HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1549   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1550       M, endian::readNext<uint32_t, little, unaligned>(d));
1551   if (unsigned FrameworkOffset =
1552           endian::readNext<uint32_t, little, unaligned>(d)) {
1553     // The framework offset is 1 greater than the actual offset,
1554     // since 0 is used as an indicator for "no framework name".
1555     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1556     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1557   }
1558 
1559   if (d != End) {
1560     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1561     if (LocalSMID) {
1562       // This header is part of a module. Associate it with the module to enable
1563       // implicit module import.
1564       SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1565       Module *Mod = Reader.getSubmodule(GlobalSMID);
1566       HFI.isModuleHeader = true;
1567       FileManager &FileMgr = Reader.getFileManager();
1568       ModuleMap &ModMap =
1569           Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1570       // FIXME: This information should be propagated through the
1571       // SUBMODULE_HEADER etc records rather than from here.
1572       // FIXME: We don't ever mark excluded headers.
1573       std::string Filename = key.Filename;
1574       if (key.Imported)
1575         Reader.ResolveImportedPath(M, Filename);
1576       Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1577       ModMap.addHeader(Mod, H, HFI.getHeaderRole());
1578     }
1579   }
1580 
1581   assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1582   (void)End;
1583 
1584   // This HeaderFileInfo was externally loaded.
1585   HFI.External = true;
1586   return HFI;
1587 }
1588 
1589 void
addPendingMacroFromModule(IdentifierInfo * II,ModuleFile * M,GlobalMacroID GMacID,ArrayRef<SubmoduleID> Overrides)1590 ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1591                                      GlobalMacroID GMacID,
1592                                      ArrayRef<SubmoduleID> Overrides) {
1593   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1594   SubmoduleID *OverrideData = nullptr;
1595   if (!Overrides.empty()) {
1596     OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1597     OverrideData[0] = Overrides.size();
1598     for (unsigned I = 0; I != Overrides.size(); ++I)
1599       OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1600   }
1601   PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
1602 }
1603 
addPendingMacroFromPCH(IdentifierInfo * II,ModuleFile * M,uint64_t MacroDirectivesOffset)1604 void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1605                                        ModuleFile *M,
1606                                        uint64_t MacroDirectivesOffset) {
1607   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1608   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1609 }
1610 
ReadDefinedMacros()1611 void ASTReader::ReadDefinedMacros() {
1612   // Note that we are loading defined macros.
1613   Deserializing Macros(this);
1614 
1615   for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1616       E = ModuleMgr.rend(); I != E; ++I) {
1617     BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1618 
1619     // If there was no preprocessor block, skip this file.
1620     if (!MacroCursor.getBitStreamReader())
1621       continue;
1622 
1623     BitstreamCursor Cursor = MacroCursor;
1624     Cursor.JumpToBit((*I)->MacroStartOffset);
1625 
1626     RecordData Record;
1627     while (true) {
1628       llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1629 
1630       switch (E.Kind) {
1631       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1632       case llvm::BitstreamEntry::Error:
1633         Error("malformed block record in AST file");
1634         return;
1635       case llvm::BitstreamEntry::EndBlock:
1636         goto NextCursor;
1637 
1638       case llvm::BitstreamEntry::Record:
1639         Record.clear();
1640         switch (Cursor.readRecord(E.ID, Record)) {
1641         default:  // Default behavior: ignore.
1642           break;
1643 
1644         case PP_MACRO_OBJECT_LIKE:
1645         case PP_MACRO_FUNCTION_LIKE:
1646           getLocalIdentifier(**I, Record[0]);
1647           break;
1648 
1649         case PP_TOKEN:
1650           // Ignore tokens.
1651           break;
1652         }
1653         break;
1654       }
1655     }
1656     NextCursor:  ;
1657   }
1658 }
1659 
1660 namespace {
1661   /// \brief Visitor class used to look up identifirs in an AST file.
1662   class IdentifierLookupVisitor {
1663     StringRef Name;
1664     unsigned PriorGeneration;
1665     unsigned &NumIdentifierLookups;
1666     unsigned &NumIdentifierLookupHits;
1667     IdentifierInfo *Found;
1668 
1669   public:
IdentifierLookupVisitor(StringRef Name,unsigned PriorGeneration,unsigned & NumIdentifierLookups,unsigned & NumIdentifierLookupHits)1670     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1671                             unsigned &NumIdentifierLookups,
1672                             unsigned &NumIdentifierLookupHits)
1673       : Name(Name), PriorGeneration(PriorGeneration),
1674         NumIdentifierLookups(NumIdentifierLookups),
1675         NumIdentifierLookupHits(NumIdentifierLookupHits),
1676         Found()
1677     {
1678     }
1679 
visit(ModuleFile & M,void * UserData)1680     static bool visit(ModuleFile &M, void *UserData) {
1681       IdentifierLookupVisitor *This
1682         = static_cast<IdentifierLookupVisitor *>(UserData);
1683 
1684       // If we've already searched this module file, skip it now.
1685       if (M.Generation <= This->PriorGeneration)
1686         return true;
1687 
1688       ASTIdentifierLookupTable *IdTable
1689         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1690       if (!IdTable)
1691         return false;
1692 
1693       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1694                                      M, This->Found);
1695       ++This->NumIdentifierLookups;
1696       ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1697       if (Pos == IdTable->end())
1698         return false;
1699 
1700       // Dereferencing the iterator has the effect of building the
1701       // IdentifierInfo node and populating it with the various
1702       // declarations it needs.
1703       ++This->NumIdentifierLookupHits;
1704       This->Found = *Pos;
1705       return true;
1706     }
1707 
1708     // \brief Retrieve the identifier info found within the module
1709     // files.
getIdentifierInfo() const1710     IdentifierInfo *getIdentifierInfo() const { return Found; }
1711   };
1712 }
1713 
updateOutOfDateIdentifier(IdentifierInfo & II)1714 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1715   // Note that we are loading an identifier.
1716   Deserializing AnIdentifier(this);
1717 
1718   unsigned PriorGeneration = 0;
1719   if (getContext().getLangOpts().Modules)
1720     PriorGeneration = IdentifierGeneration[&II];
1721 
1722   // If there is a global index, look there first to determine which modules
1723   // provably do not have any results for this identifier.
1724   GlobalModuleIndex::HitSet Hits;
1725   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1726   if (!loadGlobalIndex()) {
1727     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1728       HitsPtr = &Hits;
1729     }
1730   }
1731 
1732   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1733                                   NumIdentifierLookups,
1734                                   NumIdentifierLookupHits);
1735   ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1736   markIdentifierUpToDate(&II);
1737 }
1738 
markIdentifierUpToDate(IdentifierInfo * II)1739 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1740   if (!II)
1741     return;
1742 
1743   II->setOutOfDate(false);
1744 
1745   // Update the generation for this identifier.
1746   if (getContext().getLangOpts().Modules)
1747     IdentifierGeneration[II] = getGeneration();
1748 }
1749 
1750 struct ASTReader::ModuleMacroInfo {
1751   SubmoduleID SubModID;
1752   MacroInfo *MI;
1753   SubmoduleID *Overrides;
1754   // FIXME: Remove this.
1755   ModuleFile *F;
1756 
isDefineASTReader::ModuleMacroInfo1757   bool isDefine() const { return MI; }
1758 
getSubmoduleIDASTReader::ModuleMacroInfo1759   SubmoduleID getSubmoduleID() const { return SubModID; }
1760 
getOverriddenSubmodulesASTReader::ModuleMacroInfo1761   ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1762     if (!Overrides)
1763       return None;
1764     return llvm::makeArrayRef(Overrides + 1, *Overrides);
1765   }
1766 
importASTReader::ModuleMacroInfo1767   MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1768     if (!MI)
1769       return PP.AllocateUndefMacroDirective(ImportLoc, SubModID,
1770                                             getOverriddenSubmodules());
1771     return PP.AllocateDefMacroDirective(MI, ImportLoc, SubModID,
1772                                         getOverriddenSubmodules());
1773   }
1774 };
1775 
1776 ASTReader::ModuleMacroInfo *
getModuleMacro(const PendingMacroInfo & PMInfo)1777 ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1778   ModuleMacroInfo Info;
1779 
1780   uint32_t ID = PMInfo.ModuleMacroData.MacID;
1781   if (ID & 1) {
1782     // Macro undefinition.
1783     Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1784     Info.MI = nullptr;
1785   } else {
1786     // Macro definition.
1787     GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1788     assert(GMacID);
1789 
1790     // If this macro has already been loaded, don't do so again.
1791     // FIXME: This is highly dubious. Multiple macro definitions can have the
1792     // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1793     if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1794       return nullptr;
1795 
1796     Info.MI = getMacro(GMacID);
1797     Info.SubModID = Info.MI->getOwningModuleID();
1798   }
1799   Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1800   Info.F = PMInfo.M;
1801 
1802   return new (Context) ModuleMacroInfo(Info);
1803 }
1804 
resolvePendingMacro(IdentifierInfo * II,const PendingMacroInfo & PMInfo)1805 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1806                                     const PendingMacroInfo &PMInfo) {
1807   assert(II);
1808 
1809   if (PMInfo.M->Kind != MK_ImplicitModule &&
1810       PMInfo.M->Kind != MK_ExplicitModule) {
1811     installPCHMacroDirectives(II, *PMInfo.M,
1812                               PMInfo.PCHMacroData.MacroDirectivesOffset);
1813     return;
1814   }
1815 
1816   // Module Macro.
1817 
1818   ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1819   if (!MMI)
1820     return;
1821 
1822   Module *Owner = getSubmodule(MMI->getSubmoduleID());
1823   if (Owner && Owner->NameVisibility == Module::Hidden) {
1824     // Macros in the owning module are hidden. Just remember this macro to
1825     // install if we make this module visible.
1826     HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1827   } else {
1828     installImportedMacro(II, MMI, Owner);
1829   }
1830 }
1831 
installPCHMacroDirectives(IdentifierInfo * II,ModuleFile & M,uint64_t Offset)1832 void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1833                                           ModuleFile &M, uint64_t Offset) {
1834   assert(M.Kind != MK_ImplicitModule && M.Kind != MK_ExplicitModule);
1835 
1836   BitstreamCursor &Cursor = M.MacroCursor;
1837   SavedStreamPosition SavedPosition(Cursor);
1838   Cursor.JumpToBit(Offset);
1839 
1840   llvm::BitstreamEntry Entry =
1841       Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1842   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1843     Error("malformed block record in AST file");
1844     return;
1845   }
1846 
1847   RecordData Record;
1848   PreprocessorRecordTypes RecType =
1849     (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1850   if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1851     Error("malformed block record in AST file");
1852     return;
1853   }
1854 
1855   // Deserialize the macro directives history in reverse source-order.
1856   MacroDirective *Latest = nullptr, *Earliest = nullptr;
1857   unsigned Idx = 0, N = Record.size();
1858   while (Idx < N) {
1859     MacroDirective *MD = nullptr;
1860     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1861     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1862     switch (K) {
1863     case MacroDirective::MD_Define: {
1864       GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1865       MacroInfo *MI = getMacro(GMacID);
1866       SubmoduleID ImportedFrom = Record[Idx++];
1867       bool IsAmbiguous = Record[Idx++];
1868       llvm::SmallVector<unsigned, 4> Overrides;
1869       if (ImportedFrom) {
1870         Overrides.insert(Overrides.end(),
1871                          &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1872         Idx += Overrides.size() + 1;
1873       }
1874       DefMacroDirective *DefMD =
1875           PP.AllocateDefMacroDirective(MI, Loc, ImportedFrom, Overrides);
1876       DefMD->setAmbiguous(IsAmbiguous);
1877       MD = DefMD;
1878       break;
1879     }
1880     case MacroDirective::MD_Undefine: {
1881       SubmoduleID ImportedFrom = Record[Idx++];
1882       llvm::SmallVector<unsigned, 4> Overrides;
1883       if (ImportedFrom) {
1884         Overrides.insert(Overrides.end(),
1885                          &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1886         Idx += Overrides.size() + 1;
1887       }
1888       MD = PP.AllocateUndefMacroDirective(Loc, ImportedFrom, Overrides);
1889       break;
1890     }
1891     case MacroDirective::MD_Visibility:
1892       bool isPublic = Record[Idx++];
1893       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1894       break;
1895     }
1896 
1897     if (!Latest)
1898       Latest = MD;
1899     if (Earliest)
1900       Earliest->setPrevious(MD);
1901     Earliest = MD;
1902   }
1903 
1904   PP.setLoadedMacroDirective(II, Latest);
1905 }
1906 
1907 /// \brief For the given macro definitions, check if they are both in system
1908 /// modules.
areDefinedInSystemModules(MacroInfo * PrevMI,MacroInfo * NewMI,Module * NewOwner,ASTReader & Reader)1909 static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1910                                       Module *NewOwner, ASTReader &Reader) {
1911   assert(PrevMI && NewMI);
1912   Module *PrevOwner = nullptr;
1913   if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1914     PrevOwner = Reader.getSubmodule(PrevModID);
1915   SourceManager &SrcMgr = Reader.getSourceManager();
1916   bool PrevInSystem
1917     = PrevOwner? PrevOwner->IsSystem
1918                : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1919   bool NewInSystem
1920     = NewOwner? NewOwner->IsSystem
1921               : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1922   if (PrevOwner && PrevOwner == NewOwner)
1923     return false;
1924   return PrevInSystem && NewInSystem;
1925 }
1926 
removeOverriddenMacros(IdentifierInfo * II,SourceLocation ImportLoc,AmbiguousMacros & Ambig,ArrayRef<SubmoduleID> Overrides)1927 void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1928                                        SourceLocation ImportLoc,
1929                                        AmbiguousMacros &Ambig,
1930                                        ArrayRef<SubmoduleID> Overrides) {
1931   for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1932     SubmoduleID OwnerID = Overrides[OI];
1933 
1934     // If this macro is not yet visible, remove it from the hidden names list.
1935     // It won't be there if we're in the middle of making the owner visible.
1936     Module *Owner = getSubmodule(OwnerID);
1937     auto HiddenIt = HiddenNamesMap.find(Owner);
1938     if (HiddenIt != HiddenNamesMap.end()) {
1939       HiddenNames &Hidden = HiddenIt->second;
1940       HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1941       if (HI != Hidden.HiddenMacros.end()) {
1942         // Register the macro now so we don't lose it when we re-export.
1943         PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
1944 
1945         auto SubOverrides = HI->second->getOverriddenSubmodules();
1946         Hidden.HiddenMacros.erase(HI);
1947         removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
1948       }
1949     }
1950 
1951     // If this macro is already in our list of conflicts, remove it from there.
1952     Ambig.erase(
1953         std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1954           return MD->getInfo()->getOwningModuleID() == OwnerID;
1955         }),
1956         Ambig.end());
1957   }
1958 }
1959 
1960 ASTReader::AmbiguousMacros *
removeOverriddenMacros(IdentifierInfo * II,SourceLocation ImportLoc,ArrayRef<SubmoduleID> Overrides)1961 ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1962                                   SourceLocation ImportLoc,
1963                                   ArrayRef<SubmoduleID> Overrides) {
1964   MacroDirective *Prev = PP.getMacroDirective(II);
1965   if (!Prev && Overrides.empty())
1966     return nullptr;
1967 
1968   DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1969                                     : nullptr;
1970   if (PrevDef && PrevDef->isAmbiguous()) {
1971     // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1972     AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1973     Ambig.push_back(PrevDef);
1974 
1975     removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
1976 
1977     if (!Ambig.empty())
1978       return &Ambig;
1979 
1980     AmbiguousMacroDefs.erase(II);
1981   } else {
1982     // There's no ambiguity yet. Maybe we're introducing one.
1983     AmbiguousMacros Ambig;
1984     if (PrevDef)
1985       Ambig.push_back(PrevDef);
1986 
1987     removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
1988 
1989     if (!Ambig.empty()) {
1990       AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1991       std::swap(Result, Ambig);
1992       return &Result;
1993     }
1994   }
1995 
1996   // We ended up with no ambiguity.
1997   return nullptr;
1998 }
1999 
installImportedMacro(IdentifierInfo * II,ModuleMacroInfo * MMI,Module * Owner)2000 void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
2001                                      Module *Owner) {
2002   assert(II && Owner);
2003 
2004   SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
2005   if (ImportLoc.isInvalid()) {
2006     // FIXME: If we made macros from this module visible but didn't provide a
2007     // source location for the import, we don't have a location for the macro.
2008     // Use the location at which the containing module file was first imported
2009     // for now.
2010     ImportLoc = MMI->F->DirectImportLoc;
2011     assert(ImportLoc.isValid() && "no import location for a visible macro?");
2012   }
2013 
2014   AmbiguousMacros *Prev =
2015       removeOverriddenMacros(II, ImportLoc, MMI->getOverriddenSubmodules());
2016 
2017   // Create a synthetic macro definition corresponding to the import (or null
2018   // if this was an undefinition of the macro).
2019   MacroDirective *Imported = MMI->import(PP, ImportLoc);
2020   DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
2021 
2022   // If there's no ambiguity, just install the macro.
2023   if (!Prev) {
2024     PP.appendMacroDirective(II, Imported);
2025     return;
2026   }
2027   assert(!Prev->empty());
2028 
2029   if (!MD) {
2030     // We imported a #undef that didn't remove all prior definitions. The most
2031     // recent prior definition remains, and we install it in the place of the
2032     // imported directive, as if by a local #pragma pop_macro.
2033     MacroInfo *NewMI = Prev->back()->getInfo();
2034     Prev->pop_back();
2035     MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
2036 
2037     // Install our #undef first so that we don't lose track of it. We'll replace
2038     // this with whichever macro definition ends up winning.
2039     PP.appendMacroDirective(II, Imported);
2040   }
2041 
2042   // We're introducing a macro definition that creates or adds to an ambiguity.
2043   // We can resolve that ambiguity if this macro is token-for-token identical to
2044   // all of the existing definitions.
2045   MacroInfo *NewMI = MD->getInfo();
2046   assert(NewMI && "macro definition with no MacroInfo?");
2047   while (!Prev->empty()) {
2048     MacroInfo *PrevMI = Prev->back()->getInfo();
2049     assert(PrevMI && "macro definition with no MacroInfo?");
2050 
2051     // Before marking the macros as ambiguous, check if this is a case where
2052     // both macros are in system headers. If so, we trust that the system
2053     // did not get it wrong. This also handles cases where Clang's own
2054     // headers have a different spelling of certain system macros:
2055     //   #define LONG_MAX __LONG_MAX__ (clang's limits.h)
2056     //   #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2057     //
2058     // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2059     // overrides the system limits.h's macros, so there's no conflict here.
2060     if (NewMI != PrevMI &&
2061         !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2062         !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2063       break;
2064 
2065     // The previous definition is the same as this one (or both are defined in
2066     // system modules so we can assume they're equivalent); we don't need to
2067     // track it any more.
2068     Prev->pop_back();
2069   }
2070 
2071   if (!Prev->empty())
2072     MD->setAmbiguous(true);
2073 
2074   PP.appendMacroDirective(II, MD);
2075 }
2076 
2077 ASTReader::InputFileInfo
readInputFileInfo(ModuleFile & F,unsigned ID)2078 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2079   // Go find this input file.
2080   BitstreamCursor &Cursor = F.InputFilesCursor;
2081   SavedStreamPosition SavedPosition(Cursor);
2082   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2083 
2084   unsigned Code = Cursor.ReadCode();
2085   RecordData Record;
2086   StringRef Blob;
2087 
2088   unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2089   assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2090          "invalid record type for input file");
2091   (void)Result;
2092 
2093   std::string Filename;
2094   off_t StoredSize;
2095   time_t StoredTime;
2096   bool Overridden;
2097 
2098   assert(Record[0] == ID && "Bogus stored ID or offset");
2099   StoredSize = static_cast<off_t>(Record[1]);
2100   StoredTime = static_cast<time_t>(Record[2]);
2101   Overridden = static_cast<bool>(Record[3]);
2102   Filename = Blob;
2103   ResolveImportedPath(F, Filename);
2104 
2105   InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2106   return R;
2107 }
2108 
getInputFileName(ModuleFile & F,unsigned int ID)2109 std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
2110   return readInputFileInfo(F, ID).Filename;
2111 }
2112 
getInputFile(ModuleFile & F,unsigned ID,bool Complain)2113 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2114   // If this ID is bogus, just return an empty input file.
2115   if (ID == 0 || ID > F.InputFilesLoaded.size())
2116     return InputFile();
2117 
2118   // If we've already loaded this input file, return it.
2119   if (F.InputFilesLoaded[ID-1].getFile())
2120     return F.InputFilesLoaded[ID-1];
2121 
2122   if (F.InputFilesLoaded[ID-1].isNotFound())
2123     return InputFile();
2124 
2125   // Go find this input file.
2126   BitstreamCursor &Cursor = F.InputFilesCursor;
2127   SavedStreamPosition SavedPosition(Cursor);
2128   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2129 
2130   InputFileInfo FI = readInputFileInfo(F, ID);
2131   off_t StoredSize = FI.StoredSize;
2132   time_t StoredTime = FI.StoredTime;
2133   bool Overridden = FI.Overridden;
2134   StringRef Filename = FI.Filename;
2135 
2136   const FileEntry *File
2137     = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2138                 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2139 
2140   // If we didn't find the file, resolve it relative to the
2141   // original directory from which this AST file was created.
2142   if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
2143       F.OriginalDir != CurrentDir) {
2144     std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2145                                                             F.OriginalDir,
2146                                                             CurrentDir);
2147     if (!Resolved.empty())
2148       File = FileMgr.getFile(Resolved);
2149   }
2150 
2151   // For an overridden file, create a virtual file with the stored
2152   // size/timestamp.
2153   if (Overridden && File == nullptr) {
2154     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2155   }
2156 
2157   if (File == nullptr) {
2158     if (Complain) {
2159       std::string ErrorStr = "could not find file '";
2160       ErrorStr += Filename;
2161       ErrorStr += "' referenced by AST file";
2162       Error(ErrorStr.c_str());
2163     }
2164     // Record that we didn't find the file.
2165     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2166     return InputFile();
2167   }
2168 
2169   // Check if there was a request to override the contents of the file
2170   // that was part of the precompiled header. Overridding such a file
2171   // can lead to problems when lexing using the source locations from the
2172   // PCH.
2173   SourceManager &SM = getSourceManager();
2174   if (!Overridden && SM.isFileOverridden(File)) {
2175     if (Complain)
2176       Error(diag::err_fe_pch_file_overridden, Filename);
2177     // After emitting the diagnostic, recover by disabling the override so
2178     // that the original file will be used.
2179     SM.disableFileContentsOverride(File);
2180     // The FileEntry is a virtual file entry with the size of the contents
2181     // that would override the original contents. Set it to the original's
2182     // size/time.
2183     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2184                             StoredSize, StoredTime);
2185   }
2186 
2187   bool IsOutOfDate = false;
2188 
2189   // For an overridden file, there is nothing to validate.
2190   if (!Overridden && //
2191       (StoredSize != File->getSize() ||
2192 #if defined(LLVM_ON_WIN32)
2193        false
2194 #else
2195        // In our regression testing, the Windows file system seems to
2196        // have inconsistent modification times that sometimes
2197        // erroneously trigger this error-handling path.
2198        //
2199        // This also happens in networked file systems, so disable this
2200        // check if validation is disabled or if we have an explicitly
2201        // built PCM file.
2202        //
2203        // FIXME: Should we also do this for PCH files? They could also
2204        // reasonably get shared across a network during a distributed build.
2205        (StoredTime != File->getModificationTime() && !DisableValidation &&
2206         F.Kind != MK_ExplicitModule)
2207 #endif
2208        )) {
2209     if (Complain) {
2210       // Build a list of the PCH imports that got us here (in reverse).
2211       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2212       while (ImportStack.back()->ImportedBy.size() > 0)
2213         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2214 
2215       // The top-level PCH is stale.
2216       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2217       Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2218 
2219       // Print the import stack.
2220       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2221         Diag(diag::note_pch_required_by)
2222           << Filename << ImportStack[0]->FileName;
2223         for (unsigned I = 1; I < ImportStack.size(); ++I)
2224           Diag(diag::note_pch_required_by)
2225             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2226       }
2227 
2228       if (!Diags.isDiagnosticInFlight())
2229         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2230     }
2231 
2232     IsOutOfDate = true;
2233   }
2234 
2235   InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2236 
2237   // Note that we've loaded this input file.
2238   F.InputFilesLoaded[ID-1] = IF;
2239   return IF;
2240 }
2241 
2242 /// \brief If we are loading a relocatable PCH or module file, and the filename
2243 /// is not an absolute path, add the system or module root to the beginning of
2244 /// the file name.
ResolveImportedPath(ModuleFile & M,std::string & Filename)2245 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2246   // Resolve relative to the base directory, if we have one.
2247   if (!M.BaseDirectory.empty())
2248     return ResolveImportedPath(Filename, M.BaseDirectory);
2249 }
2250 
ResolveImportedPath(std::string & Filename,StringRef Prefix)2251 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2252   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2253     return;
2254 
2255   SmallString<128> Buffer;
2256   llvm::sys::path::append(Buffer, Prefix, Filename);
2257   Filename.assign(Buffer.begin(), Buffer.end());
2258 }
2259 
2260 ASTReader::ASTReadResult
ReadControlBlock(ModuleFile & F,SmallVectorImpl<ImportedModule> & Loaded,const ModuleFile * ImportedBy,unsigned ClientLoadCapabilities)2261 ASTReader::ReadControlBlock(ModuleFile &F,
2262                             SmallVectorImpl<ImportedModule> &Loaded,
2263                             const ModuleFile *ImportedBy,
2264                             unsigned ClientLoadCapabilities) {
2265   BitstreamCursor &Stream = F.Stream;
2266 
2267   if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2268     Error("malformed block record in AST file");
2269     return Failure;
2270   }
2271 
2272   // Should we allow the configuration of the module file to differ from the
2273   // configuration of the current translation unit in a compatible way?
2274   //
2275   // FIXME: Allow this for files explicitly specified with -include-pch too.
2276   bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2277 
2278   // Read all of the records and blocks in the control block.
2279   RecordData Record;
2280   unsigned NumInputs = 0;
2281   unsigned NumUserInputs = 0;
2282   while (1) {
2283     llvm::BitstreamEntry Entry = Stream.advance();
2284 
2285     switch (Entry.Kind) {
2286     case llvm::BitstreamEntry::Error:
2287       Error("malformed block record in AST file");
2288       return Failure;
2289     case llvm::BitstreamEntry::EndBlock: {
2290       // Validate input files.
2291       const HeaderSearchOptions &HSOpts =
2292           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2293 
2294       // All user input files reside at the index range [0, NumUserInputs), and
2295       // system input files reside at [NumUserInputs, NumInputs).
2296       if (!DisableValidation) {
2297         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2298 
2299         // If we are reading a module, we will create a verification timestamp,
2300         // so we verify all input files.  Otherwise, verify only user input
2301         // files.
2302 
2303         unsigned N = NumUserInputs;
2304         if (ValidateSystemInputs ||
2305             (HSOpts.ModulesValidateOncePerBuildSession &&
2306              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2307              F.Kind == MK_ImplicitModule))
2308           N = NumInputs;
2309 
2310         for (unsigned I = 0; I < N; ++I) {
2311           InputFile IF = getInputFile(F, I+1, Complain);
2312           if (!IF.getFile() || IF.isOutOfDate())
2313             return OutOfDate;
2314         }
2315       }
2316 
2317       if (Listener)
2318         Listener->visitModuleFile(F.FileName);
2319 
2320       if (Listener && Listener->needsInputFileVisitation()) {
2321         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2322                                                                 : NumUserInputs;
2323         for (unsigned I = 0; I < N; ++I) {
2324           bool IsSystem = I >= NumUserInputs;
2325           InputFileInfo FI = readInputFileInfo(F, I+1);
2326           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2327         }
2328       }
2329 
2330       return Success;
2331     }
2332 
2333     case llvm::BitstreamEntry::SubBlock:
2334       switch (Entry.ID) {
2335       case INPUT_FILES_BLOCK_ID:
2336         F.InputFilesCursor = Stream;
2337         if (Stream.SkipBlock() || // Skip with the main cursor
2338             // Read the abbreviations
2339             ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2340           Error("malformed block record in AST file");
2341           return Failure;
2342         }
2343         continue;
2344 
2345       default:
2346         if (Stream.SkipBlock()) {
2347           Error("malformed block record in AST file");
2348           return Failure;
2349         }
2350         continue;
2351       }
2352 
2353     case llvm::BitstreamEntry::Record:
2354       // The interesting case.
2355       break;
2356     }
2357 
2358     // Read and process a record.
2359     Record.clear();
2360     StringRef Blob;
2361     switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2362     case METADATA: {
2363       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2364         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2365           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2366                                         : diag::err_pch_version_too_new);
2367         return VersionMismatch;
2368       }
2369 
2370       bool hasErrors = Record[5];
2371       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2372         Diag(diag::err_pch_with_compiler_errors);
2373         return HadErrors;
2374       }
2375 
2376       F.RelocatablePCH = Record[4];
2377       // Relative paths in a relocatable PCH are relative to our sysroot.
2378       if (F.RelocatablePCH)
2379         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2380 
2381       const std::string &CurBranch = getClangFullRepositoryVersion();
2382       StringRef ASTBranch = Blob;
2383       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2384         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2385           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2386         return VersionMismatch;
2387       }
2388       break;
2389     }
2390 
2391     case SIGNATURE:
2392       assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2393       F.Signature = Record[0];
2394       break;
2395 
2396     case IMPORTS: {
2397       // Load each of the imported PCH files.
2398       unsigned Idx = 0, N = Record.size();
2399       while (Idx < N) {
2400         // Read information about the AST file.
2401         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2402         // The import location will be the local one for now; we will adjust
2403         // all import locations of module imports after the global source
2404         // location info are setup.
2405         SourceLocation ImportLoc =
2406             SourceLocation::getFromRawEncoding(Record[Idx++]);
2407         off_t StoredSize = (off_t)Record[Idx++];
2408         time_t StoredModTime = (time_t)Record[Idx++];
2409         ASTFileSignature StoredSignature = Record[Idx++];
2410         auto ImportedFile = ReadPath(F, Record, Idx);
2411 
2412         // Load the AST file.
2413         switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
2414                            StoredSize, StoredModTime, StoredSignature,
2415                            ClientLoadCapabilities)) {
2416         case Failure: return Failure;
2417           // If we have to ignore the dependency, we'll have to ignore this too.
2418         case Missing:
2419         case OutOfDate: return OutOfDate;
2420         case VersionMismatch: return VersionMismatch;
2421         case ConfigurationMismatch: return ConfigurationMismatch;
2422         case HadErrors: return HadErrors;
2423         case Success: break;
2424         }
2425       }
2426       break;
2427     }
2428 
2429     case LANGUAGE_OPTIONS: {
2430       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2431       // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
2432       if (Listener && &F == *ModuleMgr.begin() &&
2433           ParseLanguageOptions(Record, Complain, *Listener,
2434                                AllowCompatibleConfigurationMismatch) &&
2435           !DisableValidation && !AllowConfigurationMismatch)
2436         return ConfigurationMismatch;
2437       break;
2438     }
2439 
2440     case TARGET_OPTIONS: {
2441       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2442       if (Listener && &F == *ModuleMgr.begin() &&
2443           ParseTargetOptions(Record, Complain, *Listener) &&
2444           !DisableValidation && !AllowConfigurationMismatch)
2445         return ConfigurationMismatch;
2446       break;
2447     }
2448 
2449     case DIAGNOSTIC_OPTIONS: {
2450       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
2451       if (Listener && &F == *ModuleMgr.begin() &&
2452           !AllowCompatibleConfigurationMismatch &&
2453           ParseDiagnosticOptions(Record, Complain, *Listener) &&
2454           !DisableValidation)
2455         return OutOfDate;
2456       break;
2457     }
2458 
2459     case FILE_SYSTEM_OPTIONS: {
2460       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2461       if (Listener && &F == *ModuleMgr.begin() &&
2462           !AllowCompatibleConfigurationMismatch &&
2463           ParseFileSystemOptions(Record, Complain, *Listener) &&
2464           !DisableValidation && !AllowConfigurationMismatch)
2465         return ConfigurationMismatch;
2466       break;
2467     }
2468 
2469     case HEADER_SEARCH_OPTIONS: {
2470       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2471       if (Listener && &F == *ModuleMgr.begin() &&
2472           !AllowCompatibleConfigurationMismatch &&
2473           ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2474           !DisableValidation && !AllowConfigurationMismatch)
2475         return ConfigurationMismatch;
2476       break;
2477     }
2478 
2479     case PREPROCESSOR_OPTIONS: {
2480       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2481       if (Listener && &F == *ModuleMgr.begin() &&
2482           !AllowCompatibleConfigurationMismatch &&
2483           ParsePreprocessorOptions(Record, Complain, *Listener,
2484                                    SuggestedPredefines) &&
2485           !DisableValidation && !AllowConfigurationMismatch)
2486         return ConfigurationMismatch;
2487       break;
2488     }
2489 
2490     case ORIGINAL_FILE:
2491       F.OriginalSourceFileID = FileID::get(Record[0]);
2492       F.ActualOriginalSourceFileName = Blob;
2493       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2494       ResolveImportedPath(F, F.OriginalSourceFileName);
2495       break;
2496 
2497     case ORIGINAL_FILE_ID:
2498       F.OriginalSourceFileID = FileID::get(Record[0]);
2499       break;
2500 
2501     case ORIGINAL_PCH_DIR:
2502       F.OriginalDir = Blob;
2503       break;
2504 
2505     case MODULE_NAME:
2506       F.ModuleName = Blob;
2507       if (Listener)
2508         Listener->ReadModuleName(F.ModuleName);
2509       break;
2510 
2511     case MODULE_DIRECTORY: {
2512       assert(!F.ModuleName.empty() &&
2513              "MODULE_DIRECTORY found before MODULE_NAME");
2514       // If we've already loaded a module map file covering this module, we may
2515       // have a better path for it (relative to the current build).
2516       Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2517       if (M && M->Directory) {
2518         // If we're implicitly loading a module, the base directory can't
2519         // change between the build and use.
2520         if (F.Kind != MK_ExplicitModule) {
2521           const DirectoryEntry *BuildDir =
2522               PP.getFileManager().getDirectory(Blob);
2523           if (!BuildDir || BuildDir != M->Directory) {
2524             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2525               Diag(diag::err_imported_module_relocated)
2526                   << F.ModuleName << Blob << M->Directory->getName();
2527             return OutOfDate;
2528           }
2529         }
2530         F.BaseDirectory = M->Directory->getName();
2531       } else {
2532         F.BaseDirectory = Blob;
2533       }
2534       break;
2535     }
2536 
2537     case MODULE_MAP_FILE:
2538       if (ASTReadResult Result =
2539               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2540         return Result;
2541       break;
2542 
2543     case INPUT_FILE_OFFSETS:
2544       NumInputs = Record[0];
2545       NumUserInputs = Record[1];
2546       F.InputFileOffsets = (const uint32_t *)Blob.data();
2547       F.InputFilesLoaded.resize(NumInputs);
2548       break;
2549     }
2550   }
2551 }
2552 
2553 ASTReader::ASTReadResult
ReadASTBlock(ModuleFile & F,unsigned ClientLoadCapabilities)2554 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2555   BitstreamCursor &Stream = F.Stream;
2556 
2557   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2558     Error("malformed block record in AST file");
2559     return Failure;
2560   }
2561 
2562   // Read all of the records and blocks for the AST file.
2563   RecordData Record;
2564   while (1) {
2565     llvm::BitstreamEntry Entry = Stream.advance();
2566 
2567     switch (Entry.Kind) {
2568     case llvm::BitstreamEntry::Error:
2569       Error("error at end of module block in AST file");
2570       return Failure;
2571     case llvm::BitstreamEntry::EndBlock: {
2572       // Outside of C++, we do not store a lookup map for the translation unit.
2573       // Instead, mark it as needing a lookup map to be built if this module
2574       // contains any declarations lexically within it (which it always does!).
2575       // This usually has no cost, since we very rarely need the lookup map for
2576       // the translation unit outside C++.
2577       DeclContext *DC = Context.getTranslationUnitDecl();
2578       if (DC->hasExternalLexicalStorage() &&
2579           !getContext().getLangOpts().CPlusPlus)
2580         DC->setMustBuildLookupTable();
2581 
2582       return Success;
2583     }
2584     case llvm::BitstreamEntry::SubBlock:
2585       switch (Entry.ID) {
2586       case DECLTYPES_BLOCK_ID:
2587         // We lazily load the decls block, but we want to set up the
2588         // DeclsCursor cursor to point into it.  Clone our current bitcode
2589         // cursor to it, enter the block and read the abbrevs in that block.
2590         // With the main cursor, we just skip over it.
2591         F.DeclsCursor = Stream;
2592         if (Stream.SkipBlock() ||  // Skip with the main cursor.
2593             // Read the abbrevs.
2594             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2595           Error("malformed block record in AST file");
2596           return Failure;
2597         }
2598         break;
2599 
2600       case PREPROCESSOR_BLOCK_ID:
2601         F.MacroCursor = Stream;
2602         if (!PP.getExternalSource())
2603           PP.setExternalSource(this);
2604 
2605         if (Stream.SkipBlock() ||
2606             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2607           Error("malformed block record in AST file");
2608           return Failure;
2609         }
2610         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2611         break;
2612 
2613       case PREPROCESSOR_DETAIL_BLOCK_ID:
2614         F.PreprocessorDetailCursor = Stream;
2615         if (Stream.SkipBlock() ||
2616             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2617                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2618               Error("malformed preprocessor detail record in AST file");
2619               return Failure;
2620             }
2621         F.PreprocessorDetailStartOffset
2622         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2623 
2624         if (!PP.getPreprocessingRecord())
2625           PP.createPreprocessingRecord();
2626         if (!PP.getPreprocessingRecord()->getExternalSource())
2627           PP.getPreprocessingRecord()->SetExternalSource(*this);
2628         break;
2629 
2630       case SOURCE_MANAGER_BLOCK_ID:
2631         if (ReadSourceManagerBlock(F))
2632           return Failure;
2633         break;
2634 
2635       case SUBMODULE_BLOCK_ID:
2636         if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2637           return Result;
2638         break;
2639 
2640       case COMMENTS_BLOCK_ID: {
2641         BitstreamCursor C = Stream;
2642         if (Stream.SkipBlock() ||
2643             ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2644           Error("malformed comments block in AST file");
2645           return Failure;
2646         }
2647         CommentsCursors.push_back(std::make_pair(C, &F));
2648         break;
2649       }
2650 
2651       default:
2652         if (Stream.SkipBlock()) {
2653           Error("malformed block record in AST file");
2654           return Failure;
2655         }
2656         break;
2657       }
2658       continue;
2659 
2660     case llvm::BitstreamEntry::Record:
2661       // The interesting case.
2662       break;
2663     }
2664 
2665     // Read and process a record.
2666     Record.clear();
2667     StringRef Blob;
2668     switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2669     default:  // Default behavior: ignore.
2670       break;
2671 
2672     case TYPE_OFFSET: {
2673       if (F.LocalNumTypes != 0) {
2674         Error("duplicate TYPE_OFFSET record in AST file");
2675         return Failure;
2676       }
2677       F.TypeOffsets = (const uint32_t *)Blob.data();
2678       F.LocalNumTypes = Record[0];
2679       unsigned LocalBaseTypeIndex = Record[1];
2680       F.BaseTypeIndex = getTotalNumTypes();
2681 
2682       if (F.LocalNumTypes > 0) {
2683         // Introduce the global -> local mapping for types within this module.
2684         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2685 
2686         // Introduce the local -> global mapping for types within this module.
2687         F.TypeRemap.insertOrReplace(
2688           std::make_pair(LocalBaseTypeIndex,
2689                          F.BaseTypeIndex - LocalBaseTypeIndex));
2690 
2691         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2692       }
2693       break;
2694     }
2695 
2696     case DECL_OFFSET: {
2697       if (F.LocalNumDecls != 0) {
2698         Error("duplicate DECL_OFFSET record in AST file");
2699         return Failure;
2700       }
2701       F.DeclOffsets = (const DeclOffset *)Blob.data();
2702       F.LocalNumDecls = Record[0];
2703       unsigned LocalBaseDeclID = Record[1];
2704       F.BaseDeclID = getTotalNumDecls();
2705 
2706       if (F.LocalNumDecls > 0) {
2707         // Introduce the global -> local mapping for declarations within this
2708         // module.
2709         GlobalDeclMap.insert(
2710           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2711 
2712         // Introduce the local -> global mapping for declarations within this
2713         // module.
2714         F.DeclRemap.insertOrReplace(
2715           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2716 
2717         // Introduce the global -> local mapping for declarations within this
2718         // module.
2719         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2720 
2721         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2722       }
2723       break;
2724     }
2725 
2726     case TU_UPDATE_LEXICAL: {
2727       DeclContext *TU = Context.getTranslationUnitDecl();
2728       DeclContextInfo &Info = F.DeclContextInfos[TU];
2729       Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2730       Info.NumLexicalDecls
2731         = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2732       TU->setHasExternalLexicalStorage(true);
2733       break;
2734     }
2735 
2736     case UPDATE_VISIBLE: {
2737       unsigned Idx = 0;
2738       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2739       ASTDeclContextNameLookupTable *Table =
2740           ASTDeclContextNameLookupTable::Create(
2741               (const unsigned char *)Blob.data() + Record[Idx++],
2742               (const unsigned char *)Blob.data() + sizeof(uint32_t),
2743               (const unsigned char *)Blob.data(),
2744               ASTDeclContextNameLookupTrait(*this, F));
2745       if (Decl *D = GetExistingDecl(ID)) {
2746         auto *DC = cast<DeclContext>(D);
2747         DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2748         auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2749         delete LookupTable;
2750         LookupTable = Table;
2751       } else
2752         PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2753       break;
2754     }
2755 
2756     case IDENTIFIER_TABLE:
2757       F.IdentifierTableData = Blob.data();
2758       if (Record[0]) {
2759         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2760             (const unsigned char *)F.IdentifierTableData + Record[0],
2761             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2762             (const unsigned char *)F.IdentifierTableData,
2763             ASTIdentifierLookupTrait(*this, F));
2764 
2765         PP.getIdentifierTable().setExternalIdentifierLookup(this);
2766       }
2767       break;
2768 
2769     case IDENTIFIER_OFFSET: {
2770       if (F.LocalNumIdentifiers != 0) {
2771         Error("duplicate IDENTIFIER_OFFSET record in AST file");
2772         return Failure;
2773       }
2774       F.IdentifierOffsets = (const uint32_t *)Blob.data();
2775       F.LocalNumIdentifiers = Record[0];
2776       unsigned LocalBaseIdentifierID = Record[1];
2777       F.BaseIdentifierID = getTotalNumIdentifiers();
2778 
2779       if (F.LocalNumIdentifiers > 0) {
2780         // Introduce the global -> local mapping for identifiers within this
2781         // module.
2782         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2783                                                   &F));
2784 
2785         // Introduce the local -> global mapping for identifiers within this
2786         // module.
2787         F.IdentifierRemap.insertOrReplace(
2788           std::make_pair(LocalBaseIdentifierID,
2789                          F.BaseIdentifierID - LocalBaseIdentifierID));
2790 
2791         IdentifiersLoaded.resize(IdentifiersLoaded.size()
2792                                  + F.LocalNumIdentifiers);
2793       }
2794       break;
2795     }
2796 
2797     case EAGERLY_DESERIALIZED_DECLS:
2798       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2799         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2800       break;
2801 
2802     case SPECIAL_TYPES:
2803       if (SpecialTypes.empty()) {
2804         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2805           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2806         break;
2807       }
2808 
2809       if (SpecialTypes.size() != Record.size()) {
2810         Error("invalid special-types record");
2811         return Failure;
2812       }
2813 
2814       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2815         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2816         if (!SpecialTypes[I])
2817           SpecialTypes[I] = ID;
2818         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2819         // merge step?
2820       }
2821       break;
2822 
2823     case STATISTICS:
2824       TotalNumStatements += Record[0];
2825       TotalNumMacros += Record[1];
2826       TotalLexicalDeclContexts += Record[2];
2827       TotalVisibleDeclContexts += Record[3];
2828       break;
2829 
2830     case UNUSED_FILESCOPED_DECLS:
2831       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2832         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2833       break;
2834 
2835     case DELEGATING_CTORS:
2836       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2837         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2838       break;
2839 
2840     case WEAK_UNDECLARED_IDENTIFIERS:
2841       if (Record.size() % 4 != 0) {
2842         Error("invalid weak identifiers record");
2843         return Failure;
2844       }
2845 
2846       // FIXME: Ignore weak undeclared identifiers from non-original PCH
2847       // files. This isn't the way to do it :)
2848       WeakUndeclaredIdentifiers.clear();
2849 
2850       // Translate the weak, undeclared identifiers into global IDs.
2851       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2852         WeakUndeclaredIdentifiers.push_back(
2853           getGlobalIdentifierID(F, Record[I++]));
2854         WeakUndeclaredIdentifiers.push_back(
2855           getGlobalIdentifierID(F, Record[I++]));
2856         WeakUndeclaredIdentifiers.push_back(
2857           ReadSourceLocation(F, Record, I).getRawEncoding());
2858         WeakUndeclaredIdentifiers.push_back(Record[I++]);
2859       }
2860       break;
2861 
2862     case LOCALLY_SCOPED_EXTERN_C_DECLS:
2863       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2864         LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2865       break;
2866 
2867     case SELECTOR_OFFSETS: {
2868       F.SelectorOffsets = (const uint32_t *)Blob.data();
2869       F.LocalNumSelectors = Record[0];
2870       unsigned LocalBaseSelectorID = Record[1];
2871       F.BaseSelectorID = getTotalNumSelectors();
2872 
2873       if (F.LocalNumSelectors > 0) {
2874         // Introduce the global -> local mapping for selectors within this
2875         // module.
2876         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2877 
2878         // Introduce the local -> global mapping for selectors within this
2879         // module.
2880         F.SelectorRemap.insertOrReplace(
2881           std::make_pair(LocalBaseSelectorID,
2882                          F.BaseSelectorID - LocalBaseSelectorID));
2883 
2884         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2885       }
2886       break;
2887     }
2888 
2889     case METHOD_POOL:
2890       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2891       if (Record[0])
2892         F.SelectorLookupTable
2893           = ASTSelectorLookupTable::Create(
2894                         F.SelectorLookupTableData + Record[0],
2895                         F.SelectorLookupTableData,
2896                         ASTSelectorLookupTrait(*this, F));
2897       TotalNumMethodPoolEntries += Record[1];
2898       break;
2899 
2900     case REFERENCED_SELECTOR_POOL:
2901       if (!Record.empty()) {
2902         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2903           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2904                                                                 Record[Idx++]));
2905           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2906                                               getRawEncoding());
2907         }
2908       }
2909       break;
2910 
2911     case PP_COUNTER_VALUE:
2912       if (!Record.empty() && Listener)
2913         Listener->ReadCounter(F, Record[0]);
2914       break;
2915 
2916     case FILE_SORTED_DECLS:
2917       F.FileSortedDecls = (const DeclID *)Blob.data();
2918       F.NumFileSortedDecls = Record[0];
2919       break;
2920 
2921     case SOURCE_LOCATION_OFFSETS: {
2922       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2923       F.LocalNumSLocEntries = Record[0];
2924       unsigned SLocSpaceSize = Record[1];
2925       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2926           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2927                                               SLocSpaceSize);
2928       // Make our entry in the range map. BaseID is negative and growing, so
2929       // we invert it. Because we invert it, though, we need the other end of
2930       // the range.
2931       unsigned RangeStart =
2932           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2933       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2934       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2935 
2936       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2937       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2938       GlobalSLocOffsetMap.insert(
2939           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2940                            - SLocSpaceSize,&F));
2941 
2942       // Initialize the remapping table.
2943       // Invalid stays invalid.
2944       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2945       // This module. Base was 2 when being compiled.
2946       F.SLocRemap.insertOrReplace(std::make_pair(2U,
2947                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
2948 
2949       TotalNumSLocEntries += F.LocalNumSLocEntries;
2950       break;
2951     }
2952 
2953     case MODULE_OFFSET_MAP: {
2954       // Additional remapping information.
2955       const unsigned char *Data = (const unsigned char*)Blob.data();
2956       const unsigned char *DataEnd = Data + Blob.size();
2957 
2958       // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2959       if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2960         F.SLocRemap.insert(std::make_pair(0U, 0));
2961         F.SLocRemap.insert(std::make_pair(2U, 1));
2962       }
2963 
2964       // Continuous range maps we may be updating in our module.
2965       typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2966           RemapBuilder;
2967       RemapBuilder SLocRemap(F.SLocRemap);
2968       RemapBuilder IdentifierRemap(F.IdentifierRemap);
2969       RemapBuilder MacroRemap(F.MacroRemap);
2970       RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2971       RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2972       RemapBuilder SelectorRemap(F.SelectorRemap);
2973       RemapBuilder DeclRemap(F.DeclRemap);
2974       RemapBuilder TypeRemap(F.TypeRemap);
2975 
2976       while(Data < DataEnd) {
2977         using namespace llvm::support;
2978         uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2979         StringRef Name = StringRef((const char*)Data, Len);
2980         Data += Len;
2981         ModuleFile *OM = ModuleMgr.lookup(Name);
2982         if (!OM) {
2983           Error("SourceLocation remap refers to unknown module");
2984           return Failure;
2985         }
2986 
2987         uint32_t SLocOffset =
2988             endian::readNext<uint32_t, little, unaligned>(Data);
2989         uint32_t IdentifierIDOffset =
2990             endian::readNext<uint32_t, little, unaligned>(Data);
2991         uint32_t MacroIDOffset =
2992             endian::readNext<uint32_t, little, unaligned>(Data);
2993         uint32_t PreprocessedEntityIDOffset =
2994             endian::readNext<uint32_t, little, unaligned>(Data);
2995         uint32_t SubmoduleIDOffset =
2996             endian::readNext<uint32_t, little, unaligned>(Data);
2997         uint32_t SelectorIDOffset =
2998             endian::readNext<uint32_t, little, unaligned>(Data);
2999         uint32_t DeclIDOffset =
3000             endian::readNext<uint32_t, little, unaligned>(Data);
3001         uint32_t TypeIndexOffset =
3002             endian::readNext<uint32_t, little, unaligned>(Data);
3003 
3004         uint32_t None = std::numeric_limits<uint32_t>::max();
3005 
3006         auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3007                              RemapBuilder &Remap) {
3008           if (Offset != None)
3009             Remap.insert(std::make_pair(Offset,
3010                                         static_cast<int>(BaseOffset - Offset)));
3011         };
3012         mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3013         mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3014         mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3015         mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3016                   PreprocessedEntityRemap);
3017         mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3018         mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3019         mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3020         mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3021 
3022         // Global -> local mappings.
3023         F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3024       }
3025       break;
3026     }
3027 
3028     case SOURCE_MANAGER_LINE_TABLE:
3029       if (ParseLineTable(F, Record))
3030         return Failure;
3031       break;
3032 
3033     case SOURCE_LOCATION_PRELOADS: {
3034       // Need to transform from the local view (1-based IDs) to the global view,
3035       // which is based off F.SLocEntryBaseID.
3036       if (!F.PreloadSLocEntries.empty()) {
3037         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3038         return Failure;
3039       }
3040 
3041       F.PreloadSLocEntries.swap(Record);
3042       break;
3043     }
3044 
3045     case EXT_VECTOR_DECLS:
3046       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3047         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3048       break;
3049 
3050     case VTABLE_USES:
3051       if (Record.size() % 3 != 0) {
3052         Error("Invalid VTABLE_USES record");
3053         return Failure;
3054       }
3055 
3056       // Later tables overwrite earlier ones.
3057       // FIXME: Modules will have some trouble with this. This is clearly not
3058       // the right way to do this.
3059       VTableUses.clear();
3060 
3061       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3062         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3063         VTableUses.push_back(
3064           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3065         VTableUses.push_back(Record[Idx++]);
3066       }
3067       break;
3068 
3069     case DYNAMIC_CLASSES:
3070       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3071         DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3072       break;
3073 
3074     case PENDING_IMPLICIT_INSTANTIATIONS:
3075       if (PendingInstantiations.size() % 2 != 0) {
3076         Error("Invalid existing PendingInstantiations");
3077         return Failure;
3078       }
3079 
3080       if (Record.size() % 2 != 0) {
3081         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3082         return Failure;
3083       }
3084 
3085       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3086         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3087         PendingInstantiations.push_back(
3088           ReadSourceLocation(F, Record, I).getRawEncoding());
3089       }
3090       break;
3091 
3092     case SEMA_DECL_REFS:
3093       if (Record.size() != 2) {
3094         Error("Invalid SEMA_DECL_REFS block");
3095         return Failure;
3096       }
3097       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3098         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3099       break;
3100 
3101     case PPD_ENTITIES_OFFSETS: {
3102       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3103       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3104       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3105 
3106       unsigned LocalBasePreprocessedEntityID = Record[0];
3107 
3108       unsigned StartingID;
3109       if (!PP.getPreprocessingRecord())
3110         PP.createPreprocessingRecord();
3111       if (!PP.getPreprocessingRecord()->getExternalSource())
3112         PP.getPreprocessingRecord()->SetExternalSource(*this);
3113       StartingID
3114         = PP.getPreprocessingRecord()
3115             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3116       F.BasePreprocessedEntityID = StartingID;
3117 
3118       if (F.NumPreprocessedEntities > 0) {
3119         // Introduce the global -> local mapping for preprocessed entities in
3120         // this module.
3121         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3122 
3123         // Introduce the local -> global mapping for preprocessed entities in
3124         // this module.
3125         F.PreprocessedEntityRemap.insertOrReplace(
3126           std::make_pair(LocalBasePreprocessedEntityID,
3127             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3128       }
3129 
3130       break;
3131     }
3132 
3133     case DECL_UPDATE_OFFSETS: {
3134       if (Record.size() % 2 != 0) {
3135         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3136         return Failure;
3137       }
3138       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3139         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3140         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3141 
3142         // If we've already loaded the decl, perform the updates when we finish
3143         // loading this block.
3144         if (Decl *D = GetExistingDecl(ID))
3145           PendingUpdateRecords.push_back(std::make_pair(ID, D));
3146       }
3147       break;
3148     }
3149 
3150     case DECL_REPLACEMENTS: {
3151       if (Record.size() % 3 != 0) {
3152         Error("invalid DECL_REPLACEMENTS block in AST file");
3153         return Failure;
3154       }
3155       for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3156         ReplacedDecls[getGlobalDeclID(F, Record[I])]
3157           = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3158       break;
3159     }
3160 
3161     case OBJC_CATEGORIES_MAP: {
3162       if (F.LocalNumObjCCategoriesInMap != 0) {
3163         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3164         return Failure;
3165       }
3166 
3167       F.LocalNumObjCCategoriesInMap = Record[0];
3168       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3169       break;
3170     }
3171 
3172     case OBJC_CATEGORIES:
3173       F.ObjCCategories.swap(Record);
3174       break;
3175 
3176     case CXX_BASE_SPECIFIER_OFFSETS: {
3177       if (F.LocalNumCXXBaseSpecifiers != 0) {
3178         Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
3179         return Failure;
3180       }
3181 
3182       F.LocalNumCXXBaseSpecifiers = Record[0];
3183       F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
3184       NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3185       break;
3186     }
3187 
3188     case DIAG_PRAGMA_MAPPINGS:
3189       if (F.PragmaDiagMappings.empty())
3190         F.PragmaDiagMappings.swap(Record);
3191       else
3192         F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3193                                     Record.begin(), Record.end());
3194       break;
3195 
3196     case CUDA_SPECIAL_DECL_REFS:
3197       // Later tables overwrite earlier ones.
3198       // FIXME: Modules will have trouble with this.
3199       CUDASpecialDeclRefs.clear();
3200       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3201         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3202       break;
3203 
3204     case HEADER_SEARCH_TABLE: {
3205       F.HeaderFileInfoTableData = Blob.data();
3206       F.LocalNumHeaderFileInfos = Record[1];
3207       if (Record[0]) {
3208         F.HeaderFileInfoTable
3209           = HeaderFileInfoLookupTable::Create(
3210                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3211                    (const unsigned char *)F.HeaderFileInfoTableData,
3212                    HeaderFileInfoTrait(*this, F,
3213                                        &PP.getHeaderSearchInfo(),
3214                                        Blob.data() + Record[2]));
3215 
3216         PP.getHeaderSearchInfo().SetExternalSource(this);
3217         if (!PP.getHeaderSearchInfo().getExternalLookup())
3218           PP.getHeaderSearchInfo().SetExternalLookup(this);
3219       }
3220       break;
3221     }
3222 
3223     case FP_PRAGMA_OPTIONS:
3224       // Later tables overwrite earlier ones.
3225       FPPragmaOptions.swap(Record);
3226       break;
3227 
3228     case OPENCL_EXTENSIONS:
3229       // Later tables overwrite earlier ones.
3230       OpenCLExtensions.swap(Record);
3231       break;
3232 
3233     case TENTATIVE_DEFINITIONS:
3234       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3235         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3236       break;
3237 
3238     case KNOWN_NAMESPACES:
3239       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3240         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3241       break;
3242 
3243     case UNDEFINED_BUT_USED:
3244       if (UndefinedButUsed.size() % 2 != 0) {
3245         Error("Invalid existing UndefinedButUsed");
3246         return Failure;
3247       }
3248 
3249       if (Record.size() % 2 != 0) {
3250         Error("invalid undefined-but-used record");
3251         return Failure;
3252       }
3253       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3254         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3255         UndefinedButUsed.push_back(
3256             ReadSourceLocation(F, Record, I).getRawEncoding());
3257       }
3258       break;
3259 
3260     case IMPORTED_MODULES: {
3261       if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
3262         // If we aren't loading a module (which has its own exports), make
3263         // all of the imported modules visible.
3264         // FIXME: Deal with macros-only imports.
3265         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3266           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3267           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3268           if (GlobalID)
3269             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3270         }
3271       }
3272       break;
3273     }
3274 
3275     case LOCAL_REDECLARATIONS: {
3276       F.RedeclarationChains.swap(Record);
3277       break;
3278     }
3279 
3280     case LOCAL_REDECLARATIONS_MAP: {
3281       if (F.LocalNumRedeclarationsInMap != 0) {
3282         Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3283         return Failure;
3284       }
3285 
3286       F.LocalNumRedeclarationsInMap = Record[0];
3287       F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
3288       break;
3289     }
3290 
3291     case MERGED_DECLARATIONS: {
3292       for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3293         GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3294         SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3295         for (unsigned N = Record[Idx++]; N > 0; --N)
3296           Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3297       }
3298       break;
3299     }
3300 
3301     case MACRO_OFFSET: {
3302       if (F.LocalNumMacros != 0) {
3303         Error("duplicate MACRO_OFFSET record in AST file");
3304         return Failure;
3305       }
3306       F.MacroOffsets = (const uint32_t *)Blob.data();
3307       F.LocalNumMacros = Record[0];
3308       unsigned LocalBaseMacroID = Record[1];
3309       F.BaseMacroID = getTotalNumMacros();
3310 
3311       if (F.LocalNumMacros > 0) {
3312         // Introduce the global -> local mapping for macros within this module.
3313         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3314 
3315         // Introduce the local -> global mapping for macros within this module.
3316         F.MacroRemap.insertOrReplace(
3317           std::make_pair(LocalBaseMacroID,
3318                          F.BaseMacroID - LocalBaseMacroID));
3319 
3320         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3321       }
3322       break;
3323     }
3324 
3325     case MACRO_TABLE: {
3326       // FIXME: Not used yet.
3327       break;
3328     }
3329 
3330     case LATE_PARSED_TEMPLATE: {
3331       LateParsedTemplates.append(Record.begin(), Record.end());
3332       break;
3333     }
3334 
3335     case OPTIMIZE_PRAGMA_OPTIONS:
3336       if (Record.size() != 1) {
3337         Error("invalid pragma optimize record");
3338         return Failure;
3339       }
3340       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3341       break;
3342 
3343     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3344       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3345         UnusedLocalTypedefNameCandidates.push_back(
3346             getGlobalDeclID(F, Record[I]));
3347       break;
3348     }
3349   }
3350 }
3351 
3352 ASTReader::ASTReadResult
ReadModuleMapFileBlock(RecordData & Record,ModuleFile & F,const ModuleFile * ImportedBy,unsigned ClientLoadCapabilities)3353 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3354                                   const ModuleFile *ImportedBy,
3355                                   unsigned ClientLoadCapabilities) {
3356   unsigned Idx = 0;
3357   F.ModuleMapPath = ReadPath(F, Record, Idx);
3358 
3359   if (F.Kind == MK_ExplicitModule) {
3360     // For an explicitly-loaded module, we don't care whether the original
3361     // module map file exists or matches.
3362     return Success;
3363   }
3364 
3365   // Try to resolve ModuleName in the current header search context and
3366   // verify that it is found in the same module map file as we saved. If the
3367   // top-level AST file is a main file, skip this check because there is no
3368   // usable header search context.
3369   assert(!F.ModuleName.empty() &&
3370          "MODULE_NAME should come before MODULE_MAP_FILE");
3371   if (F.Kind == MK_ImplicitModule &&
3372       (*ModuleMgr.begin())->Kind != MK_MainFile) {
3373     // An implicitly-loaded module file should have its module listed in some
3374     // module map file that we've already loaded.
3375     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3376     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3377     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3378     if (!ModMap) {
3379       assert(ImportedBy && "top-level import should be verified");
3380       if ((ClientLoadCapabilities & ARR_Missing) == 0)
3381         Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3382                                                   << ImportedBy->FileName
3383                                                   << F.ModuleMapPath;
3384       return Missing;
3385     }
3386 
3387     assert(M->Name == F.ModuleName && "found module with different name");
3388 
3389     // Check the primary module map file.
3390     const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3391     if (StoredModMap == nullptr || StoredModMap != ModMap) {
3392       assert(ModMap && "found module is missing module map file");
3393       assert(ImportedBy && "top-level import should be verified");
3394       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3395         Diag(diag::err_imported_module_modmap_changed)
3396           << F.ModuleName << ImportedBy->FileName
3397           << ModMap->getName() << F.ModuleMapPath;
3398       return OutOfDate;
3399     }
3400 
3401     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3402     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3403       // FIXME: we should use input files rather than storing names.
3404       std::string Filename = ReadPath(F, Record, Idx);
3405       const FileEntry *F =
3406           FileMgr.getFile(Filename, false, false);
3407       if (F == nullptr) {
3408         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3409           Error("could not find file '" + Filename +"' referenced by AST file");
3410         return OutOfDate;
3411       }
3412       AdditionalStoredMaps.insert(F);
3413     }
3414 
3415     // Check any additional module map files (e.g. module.private.modulemap)
3416     // that are not in the pcm.
3417     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3418       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3419         // Remove files that match
3420         // Note: SmallPtrSet::erase is really remove
3421         if (!AdditionalStoredMaps.erase(ModMap)) {
3422           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3423             Diag(diag::err_module_different_modmap)
3424               << F.ModuleName << /*new*/0 << ModMap->getName();
3425           return OutOfDate;
3426         }
3427       }
3428     }
3429 
3430     // Check any additional module map files that are in the pcm, but not
3431     // found in header search. Cases that match are already removed.
3432     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3433       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3434         Diag(diag::err_module_different_modmap)
3435           << F.ModuleName << /*not new*/1 << ModMap->getName();
3436       return OutOfDate;
3437     }
3438   }
3439 
3440   if (Listener)
3441     Listener->ReadModuleMapFile(F.ModuleMapPath);
3442   return Success;
3443 }
3444 
3445 
3446 /// \brief Move the given method to the back of the global list of methods.
moveMethodToBackOfGlobalList(Sema & S,ObjCMethodDecl * Method)3447 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3448   // Find the entry for this selector in the method pool.
3449   Sema::GlobalMethodPool::iterator Known
3450     = S.MethodPool.find(Method->getSelector());
3451   if (Known == S.MethodPool.end())
3452     return;
3453 
3454   // Retrieve the appropriate method list.
3455   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3456                                                     : Known->second.second;
3457   bool Found = false;
3458   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3459     if (!Found) {
3460       if (List->getMethod() == Method) {
3461         Found = true;
3462       } else {
3463         // Keep searching.
3464         continue;
3465       }
3466     }
3467 
3468     if (List->getNext())
3469       List->setMethod(List->getNext()->getMethod());
3470     else
3471       List->setMethod(Method);
3472   }
3473 }
3474 
makeNamesVisible(const HiddenNames & Names,Module * Owner,bool FromFinalization)3475 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
3476                                  bool FromFinalization) {
3477   // FIXME: Only do this if Owner->NameVisibility == AllVisible.
3478   for (Decl *D : Names.HiddenDecls) {
3479     bool wasHidden = D->Hidden;
3480     D->Hidden = false;
3481 
3482     if (wasHidden && SemaObj) {
3483       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3484         moveMethodToBackOfGlobalList(*SemaObj, Method);
3485       }
3486     }
3487   }
3488 
3489   assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
3490          "nothing to make visible?");
3491   for (const auto &Macro : Names.HiddenMacros) {
3492     if (FromFinalization)
3493       PP.appendMacroDirective(Macro.first,
3494                               Macro.second->import(PP, SourceLocation()));
3495     else
3496       installImportedMacro(Macro.first, Macro.second, Owner);
3497   }
3498 }
3499 
makeModuleVisible(Module * Mod,Module::NameVisibilityKind NameVisibility,SourceLocation ImportLoc,bool Complain)3500 void ASTReader::makeModuleVisible(Module *Mod,
3501                                   Module::NameVisibilityKind NameVisibility,
3502                                   SourceLocation ImportLoc,
3503                                   bool Complain) {
3504   llvm::SmallPtrSet<Module *, 4> Visited;
3505   SmallVector<Module *, 4> Stack;
3506   Stack.push_back(Mod);
3507   while (!Stack.empty()) {
3508     Mod = Stack.pop_back_val();
3509 
3510     if (NameVisibility <= Mod->NameVisibility) {
3511       // This module already has this level of visibility (or greater), so
3512       // there is nothing more to do.
3513       continue;
3514     }
3515 
3516     if (!Mod->isAvailable()) {
3517       // Modules that aren't available cannot be made visible.
3518       continue;
3519     }
3520 
3521     // Update the module's name visibility.
3522     if (NameVisibility >= Module::MacrosVisible &&
3523         Mod->NameVisibility < Module::MacrosVisible)
3524       Mod->MacroVisibilityLoc = ImportLoc;
3525     Mod->NameVisibility = NameVisibility;
3526 
3527     // If we've already deserialized any names from this module,
3528     // mark them as visible.
3529     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3530     if (Hidden != HiddenNamesMap.end()) {
3531       auto HiddenNames = std::move(*Hidden);
3532       HiddenNamesMap.erase(Hidden);
3533       makeNamesVisible(HiddenNames.second, HiddenNames.first,
3534                        /*FromFinalization*/false);
3535       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3536              "making names visible added hidden names");
3537     }
3538 
3539     // Push any exported modules onto the stack to be marked as visible.
3540     SmallVector<Module *, 16> Exports;
3541     Mod->getExportedModules(Exports);
3542     for (SmallVectorImpl<Module *>::iterator
3543            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3544       Module *Exported = *I;
3545       if (Visited.insert(Exported).second)
3546         Stack.push_back(Exported);
3547     }
3548 
3549     // Detect any conflicts.
3550     if (Complain) {
3551       assert(ImportLoc.isValid() && "Missing import location");
3552       for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3553         if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3554           Diag(ImportLoc, diag::warn_module_conflict)
3555             << Mod->getFullModuleName()
3556             << Mod->Conflicts[I].Other->getFullModuleName()
3557             << Mod->Conflicts[I].Message;
3558           // FIXME: Need note where the other module was imported.
3559         }
3560       }
3561     }
3562   }
3563 }
3564 
loadGlobalIndex()3565 bool ASTReader::loadGlobalIndex() {
3566   if (GlobalIndex)
3567     return false;
3568 
3569   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3570       !Context.getLangOpts().Modules)
3571     return true;
3572 
3573   // Try to load the global index.
3574   TriedLoadingGlobalIndex = true;
3575   StringRef ModuleCachePath
3576     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3577   std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3578     = GlobalModuleIndex::readIndex(ModuleCachePath);
3579   if (!Result.first)
3580     return true;
3581 
3582   GlobalIndex.reset(Result.first);
3583   ModuleMgr.setGlobalIndex(GlobalIndex.get());
3584   return false;
3585 }
3586 
isGlobalIndexUnavailable() const3587 bool ASTReader::isGlobalIndexUnavailable() const {
3588   return Context.getLangOpts().Modules && UseGlobalIndex &&
3589          !hasGlobalIndex() && TriedLoadingGlobalIndex;
3590 }
3591 
updateModuleTimestamp(ModuleFile & MF)3592 static void updateModuleTimestamp(ModuleFile &MF) {
3593   // Overwrite the timestamp file contents so that file's mtime changes.
3594   std::string TimestampFilename = MF.getTimestampFilename();
3595   std::error_code EC;
3596   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3597   if (EC)
3598     return;
3599   OS << "Timestamp file\n";
3600 }
3601 
ReadAST(const std::string & FileName,ModuleKind Type,SourceLocation ImportLoc,unsigned ClientLoadCapabilities)3602 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3603                                             ModuleKind Type,
3604                                             SourceLocation ImportLoc,
3605                                             unsigned ClientLoadCapabilities) {
3606   llvm::SaveAndRestore<SourceLocation>
3607     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3608 
3609   // Defer any pending actions until we get to the end of reading the AST file.
3610   Deserializing AnASTFile(this);
3611 
3612   // Bump the generation number.
3613   unsigned PreviousGeneration = incrementGeneration(Context);
3614 
3615   unsigned NumModules = ModuleMgr.size();
3616   SmallVector<ImportedModule, 4> Loaded;
3617   switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3618                                                 /*ImportedBy=*/nullptr, Loaded,
3619                                                 0, 0, 0,
3620                                                 ClientLoadCapabilities)) {
3621   case Failure:
3622   case Missing:
3623   case OutOfDate:
3624   case VersionMismatch:
3625   case ConfigurationMismatch:
3626   case HadErrors: {
3627     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3628     for (const ImportedModule &IM : Loaded)
3629       LoadedSet.insert(IM.Mod);
3630 
3631     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3632                             LoadedSet,
3633                             Context.getLangOpts().Modules
3634                               ? &PP.getHeaderSearchInfo().getModuleMap()
3635                               : nullptr);
3636 
3637     // If we find that any modules are unusable, the global index is going
3638     // to be out-of-date. Just remove it.
3639     GlobalIndex.reset();
3640     ModuleMgr.setGlobalIndex(nullptr);
3641     return ReadResult;
3642   }
3643   case Success:
3644     break;
3645   }
3646 
3647   // Here comes stuff that we only do once the entire chain is loaded.
3648 
3649   // Load the AST blocks of all of the modules that we loaded.
3650   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3651                                               MEnd = Loaded.end();
3652        M != MEnd; ++M) {
3653     ModuleFile &F = *M->Mod;
3654 
3655     // Read the AST block.
3656     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3657       return Result;
3658 
3659     // Once read, set the ModuleFile bit base offset and update the size in
3660     // bits of all files we've seen.
3661     F.GlobalBitOffset = TotalModulesSizeInBits;
3662     TotalModulesSizeInBits += F.SizeInBits;
3663     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3664 
3665     // Preload SLocEntries.
3666     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3667       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3668       // Load it through the SourceManager and don't call ReadSLocEntry()
3669       // directly because the entry may have already been loaded in which case
3670       // calling ReadSLocEntry() directly would trigger an assertion in
3671       // SourceManager.
3672       SourceMgr.getLoadedSLocEntryByID(Index);
3673     }
3674   }
3675 
3676   // Setup the import locations and notify the module manager that we've
3677   // committed to these module files.
3678   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3679                                               MEnd = Loaded.end();
3680        M != MEnd; ++M) {
3681     ModuleFile &F = *M->Mod;
3682 
3683     ModuleMgr.moduleFileAccepted(&F);
3684 
3685     // Set the import location.
3686     F.DirectImportLoc = ImportLoc;
3687     if (!M->ImportedBy)
3688       F.ImportLoc = M->ImportLoc;
3689     else
3690       F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3691                                        M->ImportLoc.getRawEncoding());
3692   }
3693 
3694   // Mark all of the identifiers in the identifier table as being out of date,
3695   // so that various accessors know to check the loaded modules when the
3696   // identifier is used.
3697   for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3698                               IdEnd = PP.getIdentifierTable().end();
3699        Id != IdEnd; ++Id)
3700     Id->second->setOutOfDate(true);
3701 
3702   // Resolve any unresolved module exports.
3703   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3704     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3705     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3706     Module *ResolvedMod = getSubmodule(GlobalID);
3707 
3708     switch (Unresolved.Kind) {
3709     case UnresolvedModuleRef::Conflict:
3710       if (ResolvedMod) {
3711         Module::Conflict Conflict;
3712         Conflict.Other = ResolvedMod;
3713         Conflict.Message = Unresolved.String.str();
3714         Unresolved.Mod->Conflicts.push_back(Conflict);
3715       }
3716       continue;
3717 
3718     case UnresolvedModuleRef::Import:
3719       if (ResolvedMod)
3720         Unresolved.Mod->Imports.push_back(ResolvedMod);
3721       continue;
3722 
3723     case UnresolvedModuleRef::Export:
3724       if (ResolvedMod || Unresolved.IsWildcard)
3725         Unresolved.Mod->Exports.push_back(
3726           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3727       continue;
3728     }
3729   }
3730   UnresolvedModuleRefs.clear();
3731 
3732   // FIXME: How do we load the 'use'd modules? They may not be submodules.
3733   // Might be unnecessary as use declarations are only used to build the
3734   // module itself.
3735 
3736   InitializeContext();
3737 
3738   if (SemaObj)
3739     UpdateSema();
3740 
3741   if (DeserializationListener)
3742     DeserializationListener->ReaderInitialized(this);
3743 
3744   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3745   if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3746     PrimaryModule.OriginalSourceFileID
3747       = FileID::get(PrimaryModule.SLocEntryBaseID
3748                     + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3749 
3750     // If this AST file is a precompiled preamble, then set the
3751     // preamble file ID of the source manager to the file source file
3752     // from which the preamble was built.
3753     if (Type == MK_Preamble) {
3754       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3755     } else if (Type == MK_MainFile) {
3756       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3757     }
3758   }
3759 
3760   // For any Objective-C class definitions we have already loaded, make sure
3761   // that we load any additional categories.
3762   for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3763     loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3764                        ObjCClassesLoaded[I],
3765                        PreviousGeneration);
3766   }
3767 
3768   if (PP.getHeaderSearchInfo()
3769           .getHeaderSearchOpts()
3770           .ModulesValidateOncePerBuildSession) {
3771     // Now we are certain that the module and all modules it depends on are
3772     // up to date.  Create or update timestamp files for modules that are
3773     // located in the module cache (not for PCH files that could be anywhere
3774     // in the filesystem).
3775     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3776       ImportedModule &M = Loaded[I];
3777       if (M.Mod->Kind == MK_ImplicitModule) {
3778         updateModuleTimestamp(*M.Mod);
3779       }
3780     }
3781   }
3782 
3783   return Success;
3784 }
3785 
3786 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3787 
3788 ASTReader::ASTReadResult
ReadASTCore(StringRef FileName,ModuleKind Type,SourceLocation ImportLoc,ModuleFile * ImportedBy,SmallVectorImpl<ImportedModule> & Loaded,off_t ExpectedSize,time_t ExpectedModTime,ASTFileSignature ExpectedSignature,unsigned ClientLoadCapabilities)3789 ASTReader::ReadASTCore(StringRef FileName,
3790                        ModuleKind Type,
3791                        SourceLocation ImportLoc,
3792                        ModuleFile *ImportedBy,
3793                        SmallVectorImpl<ImportedModule> &Loaded,
3794                        off_t ExpectedSize, time_t ExpectedModTime,
3795                        ASTFileSignature ExpectedSignature,
3796                        unsigned ClientLoadCapabilities) {
3797   ModuleFile *M;
3798   std::string ErrorStr;
3799   ModuleManager::AddModuleResult AddResult
3800     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3801                           getGeneration(), ExpectedSize, ExpectedModTime,
3802                           ExpectedSignature, readASTFileSignature,
3803                           M, ErrorStr);
3804 
3805   switch (AddResult) {
3806   case ModuleManager::AlreadyLoaded:
3807     return Success;
3808 
3809   case ModuleManager::NewlyLoaded:
3810     // Load module file below.
3811     break;
3812 
3813   case ModuleManager::Missing:
3814     // The module file was missing; if the client can handle that, return
3815     // it.
3816     if (ClientLoadCapabilities & ARR_Missing)
3817       return Missing;
3818 
3819     // Otherwise, return an error.
3820     {
3821       std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3822                       + ErrorStr;
3823       Error(Msg);
3824     }
3825     return Failure;
3826 
3827   case ModuleManager::OutOfDate:
3828     // We couldn't load the module file because it is out-of-date. If the
3829     // client can handle out-of-date, return it.
3830     if (ClientLoadCapabilities & ARR_OutOfDate)
3831       return OutOfDate;
3832 
3833     // Otherwise, return an error.
3834     {
3835       std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3836                       + ErrorStr;
3837       Error(Msg);
3838     }
3839     return Failure;
3840   }
3841 
3842   assert(M && "Missing module file");
3843 
3844   // FIXME: This seems rather a hack. Should CurrentDir be part of the
3845   // module?
3846   if (FileName != "-") {
3847     CurrentDir = llvm::sys::path::parent_path(FileName);
3848     if (CurrentDir.empty()) CurrentDir = ".";
3849   }
3850 
3851   ModuleFile &F = *M;
3852   BitstreamCursor &Stream = F.Stream;
3853   Stream.init(&F.StreamFile);
3854   F.SizeInBits = F.Buffer->getBufferSize() * 8;
3855 
3856   // Sniff for the signature.
3857   if (Stream.Read(8) != 'C' ||
3858       Stream.Read(8) != 'P' ||
3859       Stream.Read(8) != 'C' ||
3860       Stream.Read(8) != 'H') {
3861     Diag(diag::err_not_a_pch_file) << FileName;
3862     return Failure;
3863   }
3864 
3865   // This is used for compatibility with older PCH formats.
3866   bool HaveReadControlBlock = false;
3867 
3868   while (1) {
3869     llvm::BitstreamEntry Entry = Stream.advance();
3870 
3871     switch (Entry.Kind) {
3872     case llvm::BitstreamEntry::Error:
3873     case llvm::BitstreamEntry::EndBlock:
3874     case llvm::BitstreamEntry::Record:
3875       Error("invalid record at top-level of AST file");
3876       return Failure;
3877 
3878     case llvm::BitstreamEntry::SubBlock:
3879       break;
3880     }
3881 
3882     // We only know the control subblock ID.
3883     switch (Entry.ID) {
3884     case llvm::bitc::BLOCKINFO_BLOCK_ID:
3885       if (Stream.ReadBlockInfoBlock()) {
3886         Error("malformed BlockInfoBlock in AST file");
3887         return Failure;
3888       }
3889       break;
3890     case CONTROL_BLOCK_ID:
3891       HaveReadControlBlock = true;
3892       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3893       case Success:
3894         break;
3895 
3896       case Failure: return Failure;
3897       case Missing: return Missing;
3898       case OutOfDate: return OutOfDate;
3899       case VersionMismatch: return VersionMismatch;
3900       case ConfigurationMismatch: return ConfigurationMismatch;
3901       case HadErrors: return HadErrors;
3902       }
3903       break;
3904     case AST_BLOCK_ID:
3905       if (!HaveReadControlBlock) {
3906         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3907           Diag(diag::err_pch_version_too_old);
3908         return VersionMismatch;
3909       }
3910 
3911       // Record that we've loaded this module.
3912       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3913       return Success;
3914 
3915     default:
3916       if (Stream.SkipBlock()) {
3917         Error("malformed block record in AST file");
3918         return Failure;
3919       }
3920       break;
3921     }
3922   }
3923 
3924   return Success;
3925 }
3926 
InitializeContext()3927 void ASTReader::InitializeContext() {
3928   // If there's a listener, notify them that we "read" the translation unit.
3929   if (DeserializationListener)
3930     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3931                                       Context.getTranslationUnitDecl());
3932 
3933   // FIXME: Find a better way to deal with collisions between these
3934   // built-in types. Right now, we just ignore the problem.
3935 
3936   // Load the special types.
3937   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3938     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3939       if (!Context.CFConstantStringTypeDecl)
3940         Context.setCFConstantStringType(GetType(String));
3941     }
3942 
3943     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3944       QualType FileType = GetType(File);
3945       if (FileType.isNull()) {
3946         Error("FILE type is NULL");
3947         return;
3948       }
3949 
3950       if (!Context.FILEDecl) {
3951         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3952           Context.setFILEDecl(Typedef->getDecl());
3953         else {
3954           const TagType *Tag = FileType->getAs<TagType>();
3955           if (!Tag) {
3956             Error("Invalid FILE type in AST file");
3957             return;
3958           }
3959           Context.setFILEDecl(Tag->getDecl());
3960         }
3961       }
3962     }
3963 
3964     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3965       QualType Jmp_bufType = GetType(Jmp_buf);
3966       if (Jmp_bufType.isNull()) {
3967         Error("jmp_buf type is NULL");
3968         return;
3969       }
3970 
3971       if (!Context.jmp_bufDecl) {
3972         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3973           Context.setjmp_bufDecl(Typedef->getDecl());
3974         else {
3975           const TagType *Tag = Jmp_bufType->getAs<TagType>();
3976           if (!Tag) {
3977             Error("Invalid jmp_buf type in AST file");
3978             return;
3979           }
3980           Context.setjmp_bufDecl(Tag->getDecl());
3981         }
3982       }
3983     }
3984 
3985     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3986       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3987       if (Sigjmp_bufType.isNull()) {
3988         Error("sigjmp_buf type is NULL");
3989         return;
3990       }
3991 
3992       if (!Context.sigjmp_bufDecl) {
3993         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3994           Context.setsigjmp_bufDecl(Typedef->getDecl());
3995         else {
3996           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3997           assert(Tag && "Invalid sigjmp_buf type in AST file");
3998           Context.setsigjmp_bufDecl(Tag->getDecl());
3999         }
4000       }
4001     }
4002 
4003     if (unsigned ObjCIdRedef
4004           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4005       if (Context.ObjCIdRedefinitionType.isNull())
4006         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4007     }
4008 
4009     if (unsigned ObjCClassRedef
4010           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4011       if (Context.ObjCClassRedefinitionType.isNull())
4012         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4013     }
4014 
4015     if (unsigned ObjCSelRedef
4016           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4017       if (Context.ObjCSelRedefinitionType.isNull())
4018         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4019     }
4020 
4021     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4022       QualType Ucontext_tType = GetType(Ucontext_t);
4023       if (Ucontext_tType.isNull()) {
4024         Error("ucontext_t type is NULL");
4025         return;
4026       }
4027 
4028       if (!Context.ucontext_tDecl) {
4029         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4030           Context.setucontext_tDecl(Typedef->getDecl());
4031         else {
4032           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4033           assert(Tag && "Invalid ucontext_t type in AST file");
4034           Context.setucontext_tDecl(Tag->getDecl());
4035         }
4036       }
4037     }
4038   }
4039 
4040   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4041 
4042   // If there were any CUDA special declarations, deserialize them.
4043   if (!CUDASpecialDeclRefs.empty()) {
4044     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4045     Context.setcudaConfigureCallDecl(
4046                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4047   }
4048 
4049   // Re-export any modules that were imported by a non-module AST file.
4050   // FIXME: This does not make macro-only imports visible again. It also doesn't
4051   // make #includes mapped to module imports visible.
4052   for (auto &Import : ImportedModules) {
4053     if (Module *Imported = getSubmodule(Import.ID))
4054       makeModuleVisible(Imported, Module::AllVisible,
4055                         /*ImportLoc=*/Import.ImportLoc,
4056                         /*Complain=*/false);
4057   }
4058   ImportedModules.clear();
4059 }
4060 
finalizeForWriting()4061 void ASTReader::finalizeForWriting() {
4062   while (!HiddenNamesMap.empty()) {
4063     auto HiddenNames = std::move(*HiddenNamesMap.begin());
4064     HiddenNamesMap.erase(HiddenNamesMap.begin());
4065     makeNamesVisible(HiddenNames.second, HiddenNames.first,
4066                      /*FromFinalization*/true);
4067   }
4068 }
4069 
4070 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
4071 /// cursor into the start of the given block ID, returning false on success and
4072 /// true on failure.
SkipCursorToBlock(BitstreamCursor & Cursor,unsigned BlockID)4073 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4074   while (1) {
4075     llvm::BitstreamEntry Entry = Cursor.advance();
4076     switch (Entry.Kind) {
4077     case llvm::BitstreamEntry::Error:
4078     case llvm::BitstreamEntry::EndBlock:
4079       return true;
4080 
4081     case llvm::BitstreamEntry::Record:
4082       // Ignore top-level records.
4083       Cursor.skipRecord(Entry.ID);
4084       break;
4085 
4086     case llvm::BitstreamEntry::SubBlock:
4087       if (Entry.ID == BlockID) {
4088         if (Cursor.EnterSubBlock(BlockID))
4089           return true;
4090         // Found it!
4091         return false;
4092       }
4093 
4094       if (Cursor.SkipBlock())
4095         return true;
4096     }
4097   }
4098 }
4099 
readASTFileSignature(llvm::BitstreamReader & StreamFile)4100 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4101   BitstreamCursor Stream(StreamFile);
4102   if (Stream.Read(8) != 'C' ||
4103       Stream.Read(8) != 'P' ||
4104       Stream.Read(8) != 'C' ||
4105       Stream.Read(8) != 'H') {
4106     return 0;
4107   }
4108 
4109   // Scan for the CONTROL_BLOCK_ID block.
4110   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4111     return 0;
4112 
4113   // Scan for SIGNATURE inside the control block.
4114   ASTReader::RecordData Record;
4115   while (1) {
4116     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4117     if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4118         Entry.Kind != llvm::BitstreamEntry::Record)
4119       return 0;
4120 
4121     Record.clear();
4122     StringRef Blob;
4123     if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4124       return Record[0];
4125   }
4126 }
4127 
4128 /// \brief Retrieve the name of the original source file name
4129 /// directly from the AST file, without actually loading the AST
4130 /// file.
getOriginalSourceFile(const std::string & ASTFileName,FileManager & FileMgr,DiagnosticsEngine & Diags)4131 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
4132                                              FileManager &FileMgr,
4133                                              DiagnosticsEngine &Diags) {
4134   // Open the AST file.
4135   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4136   if (!Buffer) {
4137     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4138         << ASTFileName << Buffer.getError().message();
4139     return std::string();
4140   }
4141 
4142   // Initialize the stream
4143   llvm::BitstreamReader StreamFile;
4144   StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4145                   (const unsigned char *)(*Buffer)->getBufferEnd());
4146   BitstreamCursor Stream(StreamFile);
4147 
4148   // Sniff for the signature.
4149   if (Stream.Read(8) != 'C' ||
4150       Stream.Read(8) != 'P' ||
4151       Stream.Read(8) != 'C' ||
4152       Stream.Read(8) != 'H') {
4153     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4154     return std::string();
4155   }
4156 
4157   // Scan for the CONTROL_BLOCK_ID block.
4158   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4159     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4160     return std::string();
4161   }
4162 
4163   // Scan for ORIGINAL_FILE inside the control block.
4164   RecordData Record;
4165   while (1) {
4166     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4167     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4168       return std::string();
4169 
4170     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4171       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4172       return std::string();
4173     }
4174 
4175     Record.clear();
4176     StringRef Blob;
4177     if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4178       return Blob.str();
4179   }
4180 }
4181 
4182 namespace {
4183   class SimplePCHValidator : public ASTReaderListener {
4184     const LangOptions &ExistingLangOpts;
4185     const TargetOptions &ExistingTargetOpts;
4186     const PreprocessorOptions &ExistingPPOpts;
4187     FileManager &FileMgr;
4188 
4189   public:
SimplePCHValidator(const LangOptions & ExistingLangOpts,const TargetOptions & ExistingTargetOpts,const PreprocessorOptions & ExistingPPOpts,FileManager & FileMgr)4190     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4191                        const TargetOptions &ExistingTargetOpts,
4192                        const PreprocessorOptions &ExistingPPOpts,
4193                        FileManager &FileMgr)
4194       : ExistingLangOpts(ExistingLangOpts),
4195         ExistingTargetOpts(ExistingTargetOpts),
4196         ExistingPPOpts(ExistingPPOpts),
4197         FileMgr(FileMgr)
4198     {
4199     }
4200 
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)4201     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4202                              bool AllowCompatibleDifferences) override {
4203       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4204                                   AllowCompatibleDifferences);
4205     }
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain)4206     bool ReadTargetOptions(const TargetOptions &TargetOpts,
4207                            bool Complain) override {
4208       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
4209     }
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)4210     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4211                                  bool Complain,
4212                                  std::string &SuggestedPredefines) override {
4213       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4214                                       SuggestedPredefines, ExistingLangOpts);
4215     }
4216   };
4217 }
4218 
readASTFileControlBlock(StringRef Filename,FileManager & FileMgr,ASTReaderListener & Listener)4219 bool ASTReader::readASTFileControlBlock(StringRef Filename,
4220                                         FileManager &FileMgr,
4221                                         ASTReaderListener &Listener) {
4222   // Open the AST file.
4223   auto Buffer = FileMgr.getBufferForFile(Filename);
4224   if (!Buffer) {
4225     return true;
4226   }
4227 
4228   // Initialize the stream
4229   llvm::BitstreamReader StreamFile;
4230   StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4231                   (const unsigned char *)(*Buffer)->getBufferEnd());
4232   BitstreamCursor Stream(StreamFile);
4233 
4234   // Sniff for the signature.
4235   if (Stream.Read(8) != 'C' ||
4236       Stream.Read(8) != 'P' ||
4237       Stream.Read(8) != 'C' ||
4238       Stream.Read(8) != 'H') {
4239     return true;
4240   }
4241 
4242   // Scan for the CONTROL_BLOCK_ID block.
4243   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4244     return true;
4245 
4246   bool NeedsInputFiles = Listener.needsInputFileVisitation();
4247   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4248   bool NeedsImports = Listener.needsImportVisitation();
4249   BitstreamCursor InputFilesCursor;
4250   if (NeedsInputFiles) {
4251     InputFilesCursor = Stream;
4252     if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4253       return true;
4254 
4255     // Read the abbreviations
4256     while (true) {
4257       uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4258       unsigned Code = InputFilesCursor.ReadCode();
4259 
4260       // We expect all abbrevs to be at the start of the block.
4261       if (Code != llvm::bitc::DEFINE_ABBREV) {
4262         InputFilesCursor.JumpToBit(Offset);
4263         break;
4264       }
4265       InputFilesCursor.ReadAbbrevRecord();
4266     }
4267   }
4268 
4269   // Scan for ORIGINAL_FILE inside the control block.
4270   RecordData Record;
4271   std::string ModuleDir;
4272   while (1) {
4273     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4274     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4275       return false;
4276 
4277     if (Entry.Kind != llvm::BitstreamEntry::Record)
4278       return true;
4279 
4280     Record.clear();
4281     StringRef Blob;
4282     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4283     switch ((ControlRecordTypes)RecCode) {
4284     case METADATA: {
4285       if (Record[0] != VERSION_MAJOR)
4286         return true;
4287 
4288       if (Listener.ReadFullVersionInformation(Blob))
4289         return true;
4290 
4291       break;
4292     }
4293     case MODULE_NAME:
4294       Listener.ReadModuleName(Blob);
4295       break;
4296     case MODULE_DIRECTORY:
4297       ModuleDir = Blob;
4298       break;
4299     case MODULE_MAP_FILE: {
4300       unsigned Idx = 0;
4301       auto Path = ReadString(Record, Idx);
4302       ResolveImportedPath(Path, ModuleDir);
4303       Listener.ReadModuleMapFile(Path);
4304       break;
4305     }
4306     case LANGUAGE_OPTIONS:
4307       if (ParseLanguageOptions(Record, false, Listener,
4308                                /*AllowCompatibleConfigurationMismatch*/false))
4309         return true;
4310       break;
4311 
4312     case TARGET_OPTIONS:
4313       if (ParseTargetOptions(Record, false, Listener))
4314         return true;
4315       break;
4316 
4317     case DIAGNOSTIC_OPTIONS:
4318       if (ParseDiagnosticOptions(Record, false, Listener))
4319         return true;
4320       break;
4321 
4322     case FILE_SYSTEM_OPTIONS:
4323       if (ParseFileSystemOptions(Record, false, Listener))
4324         return true;
4325       break;
4326 
4327     case HEADER_SEARCH_OPTIONS:
4328       if (ParseHeaderSearchOptions(Record, false, Listener))
4329         return true;
4330       break;
4331 
4332     case PREPROCESSOR_OPTIONS: {
4333       std::string IgnoredSuggestedPredefines;
4334       if (ParsePreprocessorOptions(Record, false, Listener,
4335                                    IgnoredSuggestedPredefines))
4336         return true;
4337       break;
4338     }
4339 
4340     case INPUT_FILE_OFFSETS: {
4341       if (!NeedsInputFiles)
4342         break;
4343 
4344       unsigned NumInputFiles = Record[0];
4345       unsigned NumUserFiles = Record[1];
4346       const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4347       for (unsigned I = 0; I != NumInputFiles; ++I) {
4348         // Go find this input file.
4349         bool isSystemFile = I >= NumUserFiles;
4350 
4351         if (isSystemFile && !NeedsSystemInputFiles)
4352           break; // the rest are system input files
4353 
4354         BitstreamCursor &Cursor = InputFilesCursor;
4355         SavedStreamPosition SavedPosition(Cursor);
4356         Cursor.JumpToBit(InputFileOffs[I]);
4357 
4358         unsigned Code = Cursor.ReadCode();
4359         RecordData Record;
4360         StringRef Blob;
4361         bool shouldContinue = false;
4362         switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4363         case INPUT_FILE:
4364           bool Overridden = static_cast<bool>(Record[3]);
4365           std::string Filename = Blob;
4366           ResolveImportedPath(Filename, ModuleDir);
4367           shouldContinue =
4368               Listener.visitInputFile(Filename, isSystemFile, Overridden);
4369           break;
4370         }
4371         if (!shouldContinue)
4372           break;
4373       }
4374       break;
4375     }
4376 
4377     case IMPORTS: {
4378       if (!NeedsImports)
4379         break;
4380 
4381       unsigned Idx = 0, N = Record.size();
4382       while (Idx < N) {
4383         // Read information about the AST file.
4384         Idx += 5; // ImportLoc, Size, ModTime, Signature
4385         std::string Filename = ReadString(Record, Idx);
4386         ResolveImportedPath(Filename, ModuleDir);
4387         Listener.visitImport(Filename);
4388       }
4389       break;
4390     }
4391 
4392     default:
4393       // No other validation to perform.
4394       break;
4395     }
4396   }
4397 }
4398 
4399 
isAcceptableASTFile(StringRef Filename,FileManager & FileMgr,const LangOptions & LangOpts,const TargetOptions & TargetOpts,const PreprocessorOptions & PPOpts)4400 bool ASTReader::isAcceptableASTFile(StringRef Filename,
4401                                     FileManager &FileMgr,
4402                                     const LangOptions &LangOpts,
4403                                     const TargetOptions &TargetOpts,
4404                                     const PreprocessorOptions &PPOpts) {
4405   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4406   return !readASTFileControlBlock(Filename, FileMgr, validator);
4407 }
4408 
4409 ASTReader::ASTReadResult
ReadSubmoduleBlock(ModuleFile & F,unsigned ClientLoadCapabilities)4410 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4411   // Enter the submodule block.
4412   if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4413     Error("malformed submodule block record in AST file");
4414     return Failure;
4415   }
4416 
4417   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4418   bool First = true;
4419   Module *CurrentModule = nullptr;
4420   RecordData Record;
4421   while (true) {
4422     llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4423 
4424     switch (Entry.Kind) {
4425     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4426     case llvm::BitstreamEntry::Error:
4427       Error("malformed block record in AST file");
4428       return Failure;
4429     case llvm::BitstreamEntry::EndBlock:
4430       return Success;
4431     case llvm::BitstreamEntry::Record:
4432       // The interesting case.
4433       break;
4434     }
4435 
4436     // Read a record.
4437     StringRef Blob;
4438     Record.clear();
4439     auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4440 
4441     if ((Kind == SUBMODULE_METADATA) != First) {
4442       Error("submodule metadata record should be at beginning of block");
4443       return Failure;
4444     }
4445     First = false;
4446 
4447     // Submodule information is only valid if we have a current module.
4448     // FIXME: Should we error on these cases?
4449     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4450         Kind != SUBMODULE_DEFINITION)
4451       continue;
4452 
4453     switch (Kind) {
4454     default:  // Default behavior: ignore.
4455       break;
4456 
4457     case SUBMODULE_DEFINITION: {
4458       if (Record.size() < 8) {
4459         Error("malformed module definition");
4460         return Failure;
4461       }
4462 
4463       StringRef Name = Blob;
4464       unsigned Idx = 0;
4465       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4466       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4467       bool IsFramework = Record[Idx++];
4468       bool IsExplicit = Record[Idx++];
4469       bool IsSystem = Record[Idx++];
4470       bool IsExternC = Record[Idx++];
4471       bool InferSubmodules = Record[Idx++];
4472       bool InferExplicitSubmodules = Record[Idx++];
4473       bool InferExportWildcard = Record[Idx++];
4474       bool ConfigMacrosExhaustive = Record[Idx++];
4475 
4476       Module *ParentModule = nullptr;
4477       if (Parent)
4478         ParentModule = getSubmodule(Parent);
4479 
4480       // Retrieve this (sub)module from the module map, creating it if
4481       // necessary.
4482       CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
4483                                                 IsExplicit).first;
4484 
4485       // FIXME: set the definition loc for CurrentModule, or call
4486       // ModMap.setInferredModuleAllowedBy()
4487 
4488       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4489       if (GlobalIndex >= SubmodulesLoaded.size() ||
4490           SubmodulesLoaded[GlobalIndex]) {
4491         Error("too many submodules");
4492         return Failure;
4493       }
4494 
4495       if (!ParentModule) {
4496         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4497           if (CurFile != F.File) {
4498             if (!Diags.isDiagnosticInFlight()) {
4499               Diag(diag::err_module_file_conflict)
4500                 << CurrentModule->getTopLevelModuleName()
4501                 << CurFile->getName()
4502                 << F.File->getName();
4503             }
4504             return Failure;
4505           }
4506         }
4507 
4508         CurrentModule->setASTFile(F.File);
4509       }
4510 
4511       CurrentModule->IsFromModuleFile = true;
4512       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4513       CurrentModule->IsExternC = IsExternC;
4514       CurrentModule->InferSubmodules = InferSubmodules;
4515       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4516       CurrentModule->InferExportWildcard = InferExportWildcard;
4517       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4518       if (DeserializationListener)
4519         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4520 
4521       SubmodulesLoaded[GlobalIndex] = CurrentModule;
4522 
4523       // Clear out data that will be replaced by what is the module file.
4524       CurrentModule->LinkLibraries.clear();
4525       CurrentModule->ConfigMacros.clear();
4526       CurrentModule->UnresolvedConflicts.clear();
4527       CurrentModule->Conflicts.clear();
4528       break;
4529     }
4530 
4531     case SUBMODULE_UMBRELLA_HEADER: {
4532       if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
4533         if (!CurrentModule->getUmbrellaHeader())
4534           ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4535         else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4536           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4537             Error("mismatched umbrella headers in submodule");
4538           return OutOfDate;
4539         }
4540       }
4541       break;
4542     }
4543 
4544     case SUBMODULE_HEADER:
4545     case SUBMODULE_EXCLUDED_HEADER:
4546     case SUBMODULE_PRIVATE_HEADER:
4547       // We lazily associate headers with their modules via the HeaderInfo table.
4548       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4549       // of complete filenames or remove it entirely.
4550       break;
4551 
4552     case SUBMODULE_TEXTUAL_HEADER:
4553     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4554       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4555       // them here.
4556       break;
4557 
4558     case SUBMODULE_TOPHEADER: {
4559       CurrentModule->addTopHeaderFilename(Blob);
4560       break;
4561     }
4562 
4563     case SUBMODULE_UMBRELLA_DIR: {
4564       if (const DirectoryEntry *Umbrella
4565                                   = PP.getFileManager().getDirectory(Blob)) {
4566         if (!CurrentModule->getUmbrellaDir())
4567           ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4568         else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4569           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4570             Error("mismatched umbrella directories in submodule");
4571           return OutOfDate;
4572         }
4573       }
4574       break;
4575     }
4576 
4577     case SUBMODULE_METADATA: {
4578       F.BaseSubmoduleID = getTotalNumSubmodules();
4579       F.LocalNumSubmodules = Record[0];
4580       unsigned LocalBaseSubmoduleID = Record[1];
4581       if (F.LocalNumSubmodules > 0) {
4582         // Introduce the global -> local mapping for submodules within this
4583         // module.
4584         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4585 
4586         // Introduce the local -> global mapping for submodules within this
4587         // module.
4588         F.SubmoduleRemap.insertOrReplace(
4589           std::make_pair(LocalBaseSubmoduleID,
4590                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
4591 
4592         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4593       }
4594       break;
4595     }
4596 
4597     case SUBMODULE_IMPORTS: {
4598       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4599         UnresolvedModuleRef Unresolved;
4600         Unresolved.File = &F;
4601         Unresolved.Mod = CurrentModule;
4602         Unresolved.ID = Record[Idx];
4603         Unresolved.Kind = UnresolvedModuleRef::Import;
4604         Unresolved.IsWildcard = false;
4605         UnresolvedModuleRefs.push_back(Unresolved);
4606       }
4607       break;
4608     }
4609 
4610     case SUBMODULE_EXPORTS: {
4611       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4612         UnresolvedModuleRef Unresolved;
4613         Unresolved.File = &F;
4614         Unresolved.Mod = CurrentModule;
4615         Unresolved.ID = Record[Idx];
4616         Unresolved.Kind = UnresolvedModuleRef::Export;
4617         Unresolved.IsWildcard = Record[Idx + 1];
4618         UnresolvedModuleRefs.push_back(Unresolved);
4619       }
4620 
4621       // Once we've loaded the set of exports, there's no reason to keep
4622       // the parsed, unresolved exports around.
4623       CurrentModule->UnresolvedExports.clear();
4624       break;
4625     }
4626     case SUBMODULE_REQUIRES: {
4627       CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4628                                     Context.getTargetInfo());
4629       break;
4630     }
4631 
4632     case SUBMODULE_LINK_LIBRARY:
4633       CurrentModule->LinkLibraries.push_back(
4634                                          Module::LinkLibrary(Blob, Record[0]));
4635       break;
4636 
4637     case SUBMODULE_CONFIG_MACRO:
4638       CurrentModule->ConfigMacros.push_back(Blob.str());
4639       break;
4640 
4641     case SUBMODULE_CONFLICT: {
4642       UnresolvedModuleRef Unresolved;
4643       Unresolved.File = &F;
4644       Unresolved.Mod = CurrentModule;
4645       Unresolved.ID = Record[0];
4646       Unresolved.Kind = UnresolvedModuleRef::Conflict;
4647       Unresolved.IsWildcard = false;
4648       Unresolved.String = Blob;
4649       UnresolvedModuleRefs.push_back(Unresolved);
4650       break;
4651     }
4652     }
4653   }
4654 }
4655 
4656 /// \brief Parse the record that corresponds to a LangOptions data
4657 /// structure.
4658 ///
4659 /// This routine parses the language options from the AST file and then gives
4660 /// them to the AST listener if one is set.
4661 ///
4662 /// \returns true if the listener deems the file unacceptable, false otherwise.
ParseLanguageOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener,bool AllowCompatibleDifferences)4663 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4664                                      bool Complain,
4665                                      ASTReaderListener &Listener,
4666                                      bool AllowCompatibleDifferences) {
4667   LangOptions LangOpts;
4668   unsigned Idx = 0;
4669 #define LANGOPT(Name, Bits, Default, Description) \
4670   LangOpts.Name = Record[Idx++];
4671 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4672   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4673 #include "clang/Basic/LangOptions.def"
4674 #define SANITIZER(NAME, ID)                                                    \
4675   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
4676 #include "clang/Basic/Sanitizers.def"
4677 
4678   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4679   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4680   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4681 
4682   unsigned Length = Record[Idx++];
4683   LangOpts.CurrentModule.assign(Record.begin() + Idx,
4684                                 Record.begin() + Idx + Length);
4685 
4686   Idx += Length;
4687 
4688   // Comment options.
4689   for (unsigned N = Record[Idx++]; N; --N) {
4690     LangOpts.CommentOpts.BlockCommandNames.push_back(
4691       ReadString(Record, Idx));
4692   }
4693   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4694 
4695   return Listener.ReadLanguageOptions(LangOpts, Complain,
4696                                       AllowCompatibleDifferences);
4697 }
4698 
ParseTargetOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)4699 bool ASTReader::ParseTargetOptions(const RecordData &Record,
4700                                    bool Complain,
4701                                    ASTReaderListener &Listener) {
4702   unsigned Idx = 0;
4703   TargetOptions TargetOpts;
4704   TargetOpts.Triple = ReadString(Record, Idx);
4705   TargetOpts.CPU = ReadString(Record, Idx);
4706   TargetOpts.ABI = ReadString(Record, Idx);
4707   for (unsigned N = Record[Idx++]; N; --N) {
4708     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4709   }
4710   for (unsigned N = Record[Idx++]; N; --N) {
4711     TargetOpts.Features.push_back(ReadString(Record, Idx));
4712   }
4713 
4714   return Listener.ReadTargetOptions(TargetOpts, Complain);
4715 }
4716 
ParseDiagnosticOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)4717 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4718                                        ASTReaderListener &Listener) {
4719   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4720   unsigned Idx = 0;
4721 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4722 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4723   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4724 #include "clang/Basic/DiagnosticOptions.def"
4725 
4726   for (unsigned N = Record[Idx++]; N; --N)
4727     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4728   for (unsigned N = Record[Idx++]; N; --N)
4729     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
4730 
4731   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4732 }
4733 
ParseFileSystemOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)4734 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4735                                        ASTReaderListener &Listener) {
4736   FileSystemOptions FSOpts;
4737   unsigned Idx = 0;
4738   FSOpts.WorkingDir = ReadString(Record, Idx);
4739   return Listener.ReadFileSystemOptions(FSOpts, Complain);
4740 }
4741 
ParseHeaderSearchOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener)4742 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4743                                          bool Complain,
4744                                          ASTReaderListener &Listener) {
4745   HeaderSearchOptions HSOpts;
4746   unsigned Idx = 0;
4747   HSOpts.Sysroot = ReadString(Record, Idx);
4748 
4749   // Include entries.
4750   for (unsigned N = Record[Idx++]; N; --N) {
4751     std::string Path = ReadString(Record, Idx);
4752     frontend::IncludeDirGroup Group
4753       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4754     bool IsFramework = Record[Idx++];
4755     bool IgnoreSysRoot = Record[Idx++];
4756     HSOpts.UserEntries.push_back(
4757       HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4758   }
4759 
4760   // System header prefixes.
4761   for (unsigned N = Record[Idx++]; N; --N) {
4762     std::string Prefix = ReadString(Record, Idx);
4763     bool IsSystemHeader = Record[Idx++];
4764     HSOpts.SystemHeaderPrefixes.push_back(
4765       HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4766   }
4767 
4768   HSOpts.ResourceDir = ReadString(Record, Idx);
4769   HSOpts.ModuleCachePath = ReadString(Record, Idx);
4770   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4771   HSOpts.DisableModuleHash = Record[Idx++];
4772   HSOpts.UseBuiltinIncludes = Record[Idx++];
4773   HSOpts.UseStandardSystemIncludes = Record[Idx++];
4774   HSOpts.UseStandardCXXIncludes = Record[Idx++];
4775   HSOpts.UseLibcxx = Record[Idx++];
4776 
4777   return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4778 }
4779 
ParsePreprocessorOptions(const RecordData & Record,bool Complain,ASTReaderListener & Listener,std::string & SuggestedPredefines)4780 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4781                                          bool Complain,
4782                                          ASTReaderListener &Listener,
4783                                          std::string &SuggestedPredefines) {
4784   PreprocessorOptions PPOpts;
4785   unsigned Idx = 0;
4786 
4787   // Macro definitions/undefs
4788   for (unsigned N = Record[Idx++]; N; --N) {
4789     std::string Macro = ReadString(Record, Idx);
4790     bool IsUndef = Record[Idx++];
4791     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4792   }
4793 
4794   // Includes
4795   for (unsigned N = Record[Idx++]; N; --N) {
4796     PPOpts.Includes.push_back(ReadString(Record, Idx));
4797   }
4798 
4799   // Macro Includes
4800   for (unsigned N = Record[Idx++]; N; --N) {
4801     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4802   }
4803 
4804   PPOpts.UsePredefines = Record[Idx++];
4805   PPOpts.DetailedRecord = Record[Idx++];
4806   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4807   PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4808   PPOpts.ObjCXXARCStandardLibrary =
4809     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4810   SuggestedPredefines.clear();
4811   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4812                                           SuggestedPredefines);
4813 }
4814 
4815 std::pair<ModuleFile *, unsigned>
getModulePreprocessedEntity(unsigned GlobalIndex)4816 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4817   GlobalPreprocessedEntityMapType::iterator
4818   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4819   assert(I != GlobalPreprocessedEntityMap.end() &&
4820          "Corrupted global preprocessed entity map");
4821   ModuleFile *M = I->second;
4822   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4823   return std::make_pair(M, LocalIndex);
4824 }
4825 
4826 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
getModulePreprocessedEntities(ModuleFile & Mod) const4827 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4828   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4829     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4830                                              Mod.NumPreprocessedEntities);
4831 
4832   return std::make_pair(PreprocessingRecord::iterator(),
4833                         PreprocessingRecord::iterator());
4834 }
4835 
4836 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
getModuleFileLevelDecls(ModuleFile & Mod)4837 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4838   return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4839                         ModuleDeclIterator(this, &Mod,
4840                                  Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4841 }
4842 
ReadPreprocessedEntity(unsigned Index)4843 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4844   PreprocessedEntityID PPID = Index+1;
4845   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4846   ModuleFile &M = *PPInfo.first;
4847   unsigned LocalIndex = PPInfo.second;
4848   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4849 
4850   if (!PP.getPreprocessingRecord()) {
4851     Error("no preprocessing record");
4852     return nullptr;
4853   }
4854 
4855   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4856   M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4857 
4858   llvm::BitstreamEntry Entry =
4859     M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4860   if (Entry.Kind != llvm::BitstreamEntry::Record)
4861     return nullptr;
4862 
4863   // Read the record.
4864   SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4865                     ReadSourceLocation(M, PPOffs.End));
4866   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4867   StringRef Blob;
4868   RecordData Record;
4869   PreprocessorDetailRecordTypes RecType =
4870     (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4871                                           Entry.ID, Record, &Blob);
4872   switch (RecType) {
4873   case PPD_MACRO_EXPANSION: {
4874     bool isBuiltin = Record[0];
4875     IdentifierInfo *Name = nullptr;
4876     MacroDefinition *Def = nullptr;
4877     if (isBuiltin)
4878       Name = getLocalIdentifier(M, Record[1]);
4879     else {
4880       PreprocessedEntityID
4881           GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4882       Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4883     }
4884 
4885     MacroExpansion *ME;
4886     if (isBuiltin)
4887       ME = new (PPRec) MacroExpansion(Name, Range);
4888     else
4889       ME = new (PPRec) MacroExpansion(Def, Range);
4890 
4891     return ME;
4892   }
4893 
4894   case PPD_MACRO_DEFINITION: {
4895     // Decode the identifier info and then check again; if the macro is
4896     // still defined and associated with the identifier,
4897     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4898     MacroDefinition *MD
4899       = new (PPRec) MacroDefinition(II, Range);
4900 
4901     if (DeserializationListener)
4902       DeserializationListener->MacroDefinitionRead(PPID, MD);
4903 
4904     return MD;
4905   }
4906 
4907   case PPD_INCLUSION_DIRECTIVE: {
4908     const char *FullFileNameStart = Blob.data() + Record[0];
4909     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4910     const FileEntry *File = nullptr;
4911     if (!FullFileName.empty())
4912       File = PP.getFileManager().getFile(FullFileName);
4913 
4914     // FIXME: Stable encoding
4915     InclusionDirective::InclusionKind Kind
4916       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4917     InclusionDirective *ID
4918       = new (PPRec) InclusionDirective(PPRec, Kind,
4919                                        StringRef(Blob.data(), Record[0]),
4920                                        Record[1], Record[3],
4921                                        File,
4922                                        Range);
4923     return ID;
4924   }
4925   }
4926 
4927   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4928 }
4929 
4930 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4931 /// preprocessed entities or the entities it contains are not the ones we are
4932 /// looking for. Find the next module that contains entities and return the ID
4933 /// of the first entry.
findNextPreprocessedEntity(GlobalSLocOffsetMapType::const_iterator SLocMapI) const4934 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4935                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4936   ++SLocMapI;
4937   for (GlobalSLocOffsetMapType::const_iterator
4938          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4939     ModuleFile &M = *SLocMapI->second;
4940     if (M.NumPreprocessedEntities)
4941       return M.BasePreprocessedEntityID;
4942   }
4943 
4944   return getTotalNumPreprocessedEntities();
4945 }
4946 
4947 namespace {
4948 
4949 template <unsigned PPEntityOffset::*PPLoc>
4950 struct PPEntityComp {
4951   const ASTReader &Reader;
4952   ModuleFile &M;
4953 
PPEntityComp__anon3f311b4d0711::PPEntityComp4954   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4955 
operator ()__anon3f311b4d0711::PPEntityComp4956   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4957     SourceLocation LHS = getLoc(L);
4958     SourceLocation RHS = getLoc(R);
4959     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4960   }
4961 
operator ()__anon3f311b4d0711::PPEntityComp4962   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4963     SourceLocation LHS = getLoc(L);
4964     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4965   }
4966 
operator ()__anon3f311b4d0711::PPEntityComp4967   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4968     SourceLocation RHS = getLoc(R);
4969     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4970   }
4971 
getLoc__anon3f311b4d0711::PPEntityComp4972   SourceLocation getLoc(const PPEntityOffset &PPE) const {
4973     return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4974   }
4975 };
4976 
4977 }
4978 
findPreprocessedEntity(SourceLocation Loc,bool EndsAfter) const4979 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4980                                                        bool EndsAfter) const {
4981   if (SourceMgr.isLocalSourceLocation(Loc))
4982     return getTotalNumPreprocessedEntities();
4983 
4984   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4985       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
4986   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4987          "Corrupted global sloc offset map");
4988 
4989   if (SLocMapI->second->NumPreprocessedEntities == 0)
4990     return findNextPreprocessedEntity(SLocMapI);
4991 
4992   ModuleFile &M = *SLocMapI->second;
4993   typedef const PPEntityOffset *pp_iterator;
4994   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4995   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4996 
4997   size_t Count = M.NumPreprocessedEntities;
4998   size_t Half;
4999   pp_iterator First = pp_begin;
5000   pp_iterator PPI;
5001 
5002   if (EndsAfter) {
5003     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5004                            PPEntityComp<&PPEntityOffset::Begin>(*this, M));
5005   } else {
5006     // Do a binary search manually instead of using std::lower_bound because
5007     // The end locations of entities may be unordered (when a macro expansion
5008     // is inside another macro argument), but for this case it is not important
5009     // whether we get the first macro expansion or its containing macro.
5010     while (Count > 0) {
5011       Half = Count / 2;
5012       PPI = First;
5013       std::advance(PPI, Half);
5014       if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
5015                                               Loc)) {
5016         First = PPI;
5017         ++First;
5018         Count = Count - Half - 1;
5019       } else
5020         Count = Half;
5021     }
5022   }
5023 
5024   if (PPI == pp_end)
5025     return findNextPreprocessedEntity(SLocMapI);
5026 
5027   return M.BasePreprocessedEntityID + (PPI - pp_begin);
5028 }
5029 
5030 /// \brief Returns a pair of [Begin, End) indices of preallocated
5031 /// preprocessed entities that \arg Range encompasses.
5032 std::pair<unsigned, unsigned>
findPreprocessedEntitiesInRange(SourceRange Range)5033     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5034   if (Range.isInvalid())
5035     return std::make_pair(0,0);
5036   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5037 
5038   PreprocessedEntityID BeginID =
5039       findPreprocessedEntity(Range.getBegin(), false);
5040   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5041   return std::make_pair(BeginID, EndID);
5042 }
5043 
5044 /// \brief Optionally returns true or false if the preallocated preprocessed
5045 /// entity with index \arg Index came from file \arg FID.
isPreprocessedEntityInFileID(unsigned Index,FileID FID)5046 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5047                                                              FileID FID) {
5048   if (FID.isInvalid())
5049     return false;
5050 
5051   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5052   ModuleFile &M = *PPInfo.first;
5053   unsigned LocalIndex = PPInfo.second;
5054   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5055 
5056   SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5057   if (Loc.isInvalid())
5058     return false;
5059 
5060   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5061     return true;
5062   else
5063     return false;
5064 }
5065 
5066 namespace {
5067   /// \brief Visitor used to search for information about a header file.
5068   class HeaderFileInfoVisitor {
5069     const FileEntry *FE;
5070 
5071     Optional<HeaderFileInfo> HFI;
5072 
5073   public:
HeaderFileInfoVisitor(const FileEntry * FE)5074     explicit HeaderFileInfoVisitor(const FileEntry *FE)
5075       : FE(FE) { }
5076 
visit(ModuleFile & M,void * UserData)5077     static bool visit(ModuleFile &M, void *UserData) {
5078       HeaderFileInfoVisitor *This
5079         = static_cast<HeaderFileInfoVisitor *>(UserData);
5080 
5081       HeaderFileInfoLookupTable *Table
5082         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5083       if (!Table)
5084         return false;
5085 
5086       // Look in the on-disk hash table for an entry for this file name.
5087       HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
5088       if (Pos == Table->end())
5089         return false;
5090 
5091       This->HFI = *Pos;
5092       return true;
5093     }
5094 
getHeaderFileInfo() const5095     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5096   };
5097 }
5098 
GetHeaderFileInfo(const FileEntry * FE)5099 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5100   HeaderFileInfoVisitor Visitor(FE);
5101   ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
5102   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5103     return *HFI;
5104 
5105   return HeaderFileInfo();
5106 }
5107 
ReadPragmaDiagnosticMappings(DiagnosticsEngine & Diag)5108 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5109   // FIXME: Make it work properly with modules.
5110   SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
5111   for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5112     ModuleFile &F = *(*I);
5113     unsigned Idx = 0;
5114     DiagStates.clear();
5115     assert(!Diag.DiagStates.empty());
5116     DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5117     while (Idx < F.PragmaDiagMappings.size()) {
5118       SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5119       unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5120       if (DiagStateID != 0) {
5121         Diag.DiagStatePoints.push_back(
5122                     DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5123                     FullSourceLoc(Loc, SourceMgr)));
5124         continue;
5125       }
5126 
5127       assert(DiagStateID == 0);
5128       // A new DiagState was created here.
5129       Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5130       DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5131       DiagStates.push_back(NewState);
5132       Diag.DiagStatePoints.push_back(
5133           DiagnosticsEngine::DiagStatePoint(NewState,
5134                                             FullSourceLoc(Loc, SourceMgr)));
5135       while (1) {
5136         assert(Idx < F.PragmaDiagMappings.size() &&
5137                "Invalid data, didn't find '-1' marking end of diag/map pairs");
5138         if (Idx >= F.PragmaDiagMappings.size()) {
5139           break; // Something is messed up but at least avoid infinite loop in
5140                  // release build.
5141         }
5142         unsigned DiagID = F.PragmaDiagMappings[Idx++];
5143         if (DiagID == (unsigned)-1) {
5144           break; // no more diag/map pairs for this location.
5145         }
5146         diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5147         DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5148         Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
5149       }
5150     }
5151   }
5152 }
5153 
5154 /// \brief Get the correct cursor and offset for loading a type.
TypeCursorForIndex(unsigned Index)5155 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5156   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5157   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5158   ModuleFile *M = I->second;
5159   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5160 }
5161 
5162 /// \brief Read and return the type with the given index..
5163 ///
5164 /// The index is the type ID, shifted and minus the number of predefs. This
5165 /// routine actually reads the record corresponding to the type at the given
5166 /// location. It is a helper routine for GetType, which deals with reading type
5167 /// IDs.
readTypeRecord(unsigned Index)5168 QualType ASTReader::readTypeRecord(unsigned Index) {
5169   RecordLocation Loc = TypeCursorForIndex(Index);
5170   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5171 
5172   // Keep track of where we are in the stream, then jump back there
5173   // after reading this type.
5174   SavedStreamPosition SavedPosition(DeclsCursor);
5175 
5176   ReadingKindTracker ReadingKind(Read_Type, *this);
5177 
5178   // Note that we are loading a type record.
5179   Deserializing AType(this);
5180 
5181   unsigned Idx = 0;
5182   DeclsCursor.JumpToBit(Loc.Offset);
5183   RecordData Record;
5184   unsigned Code = DeclsCursor.ReadCode();
5185   switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5186   case TYPE_EXT_QUAL: {
5187     if (Record.size() != 2) {
5188       Error("Incorrect encoding of extended qualifier type");
5189       return QualType();
5190     }
5191     QualType Base = readType(*Loc.F, Record, Idx);
5192     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5193     return Context.getQualifiedType(Base, Quals);
5194   }
5195 
5196   case TYPE_COMPLEX: {
5197     if (Record.size() != 1) {
5198       Error("Incorrect encoding of complex type");
5199       return QualType();
5200     }
5201     QualType ElemType = readType(*Loc.F, Record, Idx);
5202     return Context.getComplexType(ElemType);
5203   }
5204 
5205   case TYPE_POINTER: {
5206     if (Record.size() != 1) {
5207       Error("Incorrect encoding of pointer type");
5208       return QualType();
5209     }
5210     QualType PointeeType = readType(*Loc.F, Record, Idx);
5211     return Context.getPointerType(PointeeType);
5212   }
5213 
5214   case TYPE_DECAYED: {
5215     if (Record.size() != 1) {
5216       Error("Incorrect encoding of decayed type");
5217       return QualType();
5218     }
5219     QualType OriginalType = readType(*Loc.F, Record, Idx);
5220     QualType DT = Context.getAdjustedParameterType(OriginalType);
5221     if (!isa<DecayedType>(DT))
5222       Error("Decayed type does not decay");
5223     return DT;
5224   }
5225 
5226   case TYPE_ADJUSTED: {
5227     if (Record.size() != 2) {
5228       Error("Incorrect encoding of adjusted type");
5229       return QualType();
5230     }
5231     QualType OriginalTy = readType(*Loc.F, Record, Idx);
5232     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5233     return Context.getAdjustedType(OriginalTy, AdjustedTy);
5234   }
5235 
5236   case TYPE_BLOCK_POINTER: {
5237     if (Record.size() != 1) {
5238       Error("Incorrect encoding of block pointer type");
5239       return QualType();
5240     }
5241     QualType PointeeType = readType(*Loc.F, Record, Idx);
5242     return Context.getBlockPointerType(PointeeType);
5243   }
5244 
5245   case TYPE_LVALUE_REFERENCE: {
5246     if (Record.size() != 2) {
5247       Error("Incorrect encoding of lvalue reference type");
5248       return QualType();
5249     }
5250     QualType PointeeType = readType(*Loc.F, Record, Idx);
5251     return Context.getLValueReferenceType(PointeeType, Record[1]);
5252   }
5253 
5254   case TYPE_RVALUE_REFERENCE: {
5255     if (Record.size() != 1) {
5256       Error("Incorrect encoding of rvalue reference type");
5257       return QualType();
5258     }
5259     QualType PointeeType = readType(*Loc.F, Record, Idx);
5260     return Context.getRValueReferenceType(PointeeType);
5261   }
5262 
5263   case TYPE_MEMBER_POINTER: {
5264     if (Record.size() != 2) {
5265       Error("Incorrect encoding of member pointer type");
5266       return QualType();
5267     }
5268     QualType PointeeType = readType(*Loc.F, Record, Idx);
5269     QualType ClassType = readType(*Loc.F, Record, Idx);
5270     if (PointeeType.isNull() || ClassType.isNull())
5271       return QualType();
5272 
5273     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5274   }
5275 
5276   case TYPE_CONSTANT_ARRAY: {
5277     QualType ElementType = readType(*Loc.F, Record, Idx);
5278     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5279     unsigned IndexTypeQuals = Record[2];
5280     unsigned Idx = 3;
5281     llvm::APInt Size = ReadAPInt(Record, Idx);
5282     return Context.getConstantArrayType(ElementType, Size,
5283                                          ASM, IndexTypeQuals);
5284   }
5285 
5286   case TYPE_INCOMPLETE_ARRAY: {
5287     QualType ElementType = readType(*Loc.F, Record, Idx);
5288     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5289     unsigned IndexTypeQuals = Record[2];
5290     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5291   }
5292 
5293   case TYPE_VARIABLE_ARRAY: {
5294     QualType ElementType = readType(*Loc.F, Record, Idx);
5295     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5296     unsigned IndexTypeQuals = Record[2];
5297     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5298     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5299     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5300                                          ASM, IndexTypeQuals,
5301                                          SourceRange(LBLoc, RBLoc));
5302   }
5303 
5304   case TYPE_VECTOR: {
5305     if (Record.size() != 3) {
5306       Error("incorrect encoding of vector type in AST file");
5307       return QualType();
5308     }
5309 
5310     QualType ElementType = readType(*Loc.F, Record, Idx);
5311     unsigned NumElements = Record[1];
5312     unsigned VecKind = Record[2];
5313     return Context.getVectorType(ElementType, NumElements,
5314                                   (VectorType::VectorKind)VecKind);
5315   }
5316 
5317   case TYPE_EXT_VECTOR: {
5318     if (Record.size() != 3) {
5319       Error("incorrect encoding of extended vector type in AST file");
5320       return QualType();
5321     }
5322 
5323     QualType ElementType = readType(*Loc.F, Record, Idx);
5324     unsigned NumElements = Record[1];
5325     return Context.getExtVectorType(ElementType, NumElements);
5326   }
5327 
5328   case TYPE_FUNCTION_NO_PROTO: {
5329     if (Record.size() != 6) {
5330       Error("incorrect encoding of no-proto function type");
5331       return QualType();
5332     }
5333     QualType ResultType = readType(*Loc.F, Record, Idx);
5334     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5335                                (CallingConv)Record[4], Record[5]);
5336     return Context.getFunctionNoProtoType(ResultType, Info);
5337   }
5338 
5339   case TYPE_FUNCTION_PROTO: {
5340     QualType ResultType = readType(*Loc.F, Record, Idx);
5341 
5342     FunctionProtoType::ExtProtoInfo EPI;
5343     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5344                                         /*hasregparm*/ Record[2],
5345                                         /*regparm*/ Record[3],
5346                                         static_cast<CallingConv>(Record[4]),
5347                                         /*produces*/ Record[5]);
5348 
5349     unsigned Idx = 6;
5350 
5351     EPI.Variadic = Record[Idx++];
5352     EPI.HasTrailingReturn = Record[Idx++];
5353     EPI.TypeQuals = Record[Idx++];
5354     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5355     SmallVector<QualType, 8> ExceptionStorage;
5356     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5357 
5358     unsigned NumParams = Record[Idx++];
5359     SmallVector<QualType, 16> ParamTypes;
5360     for (unsigned I = 0; I != NumParams; ++I)
5361       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5362 
5363     return Context.getFunctionType(ResultType, ParamTypes, EPI);
5364   }
5365 
5366   case TYPE_UNRESOLVED_USING: {
5367     unsigned Idx = 0;
5368     return Context.getTypeDeclType(
5369                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5370   }
5371 
5372   case TYPE_TYPEDEF: {
5373     if (Record.size() != 2) {
5374       Error("incorrect encoding of typedef type");
5375       return QualType();
5376     }
5377     unsigned Idx = 0;
5378     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5379     QualType Canonical = readType(*Loc.F, Record, Idx);
5380     if (!Canonical.isNull())
5381       Canonical = Context.getCanonicalType(Canonical);
5382     return Context.getTypedefType(Decl, Canonical);
5383   }
5384 
5385   case TYPE_TYPEOF_EXPR:
5386     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5387 
5388   case TYPE_TYPEOF: {
5389     if (Record.size() != 1) {
5390       Error("incorrect encoding of typeof(type) in AST file");
5391       return QualType();
5392     }
5393     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5394     return Context.getTypeOfType(UnderlyingType);
5395   }
5396 
5397   case TYPE_DECLTYPE: {
5398     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5399     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5400   }
5401 
5402   case TYPE_UNARY_TRANSFORM: {
5403     QualType BaseType = readType(*Loc.F, Record, Idx);
5404     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5405     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5406     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5407   }
5408 
5409   case TYPE_AUTO: {
5410     QualType Deduced = readType(*Loc.F, Record, Idx);
5411     bool IsDecltypeAuto = Record[Idx++];
5412     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5413     return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
5414   }
5415 
5416   case TYPE_RECORD: {
5417     if (Record.size() != 2) {
5418       Error("incorrect encoding of record type");
5419       return QualType();
5420     }
5421     unsigned Idx = 0;
5422     bool IsDependent = Record[Idx++];
5423     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5424     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5425     QualType T = Context.getRecordType(RD);
5426     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5427     return T;
5428   }
5429 
5430   case TYPE_ENUM: {
5431     if (Record.size() != 2) {
5432       Error("incorrect encoding of enum type");
5433       return QualType();
5434     }
5435     unsigned Idx = 0;
5436     bool IsDependent = Record[Idx++];
5437     QualType T
5438       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5439     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5440     return T;
5441   }
5442 
5443   case TYPE_ATTRIBUTED: {
5444     if (Record.size() != 3) {
5445       Error("incorrect encoding of attributed type");
5446       return QualType();
5447     }
5448     QualType modifiedType = readType(*Loc.F, Record, Idx);
5449     QualType equivalentType = readType(*Loc.F, Record, Idx);
5450     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5451     return Context.getAttributedType(kind, modifiedType, equivalentType);
5452   }
5453 
5454   case TYPE_PAREN: {
5455     if (Record.size() != 1) {
5456       Error("incorrect encoding of paren type");
5457       return QualType();
5458     }
5459     QualType InnerType = readType(*Loc.F, Record, Idx);
5460     return Context.getParenType(InnerType);
5461   }
5462 
5463   case TYPE_PACK_EXPANSION: {
5464     if (Record.size() != 2) {
5465       Error("incorrect encoding of pack expansion type");
5466       return QualType();
5467     }
5468     QualType Pattern = readType(*Loc.F, Record, Idx);
5469     if (Pattern.isNull())
5470       return QualType();
5471     Optional<unsigned> NumExpansions;
5472     if (Record[1])
5473       NumExpansions = Record[1] - 1;
5474     return Context.getPackExpansionType(Pattern, NumExpansions);
5475   }
5476 
5477   case TYPE_ELABORATED: {
5478     unsigned Idx = 0;
5479     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5480     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5481     QualType NamedType = readType(*Loc.F, Record, Idx);
5482     return Context.getElaboratedType(Keyword, NNS, NamedType);
5483   }
5484 
5485   case TYPE_OBJC_INTERFACE: {
5486     unsigned Idx = 0;
5487     ObjCInterfaceDecl *ItfD
5488       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5489     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5490   }
5491 
5492   case TYPE_OBJC_OBJECT: {
5493     unsigned Idx = 0;
5494     QualType Base = readType(*Loc.F, Record, Idx);
5495     unsigned NumProtos = Record[Idx++];
5496     SmallVector<ObjCProtocolDecl*, 4> Protos;
5497     for (unsigned I = 0; I != NumProtos; ++I)
5498       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5499     return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5500   }
5501 
5502   case TYPE_OBJC_OBJECT_POINTER: {
5503     unsigned Idx = 0;
5504     QualType Pointee = readType(*Loc.F, Record, Idx);
5505     return Context.getObjCObjectPointerType(Pointee);
5506   }
5507 
5508   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5509     unsigned Idx = 0;
5510     QualType Parm = readType(*Loc.F, Record, Idx);
5511     QualType Replacement = readType(*Loc.F, Record, Idx);
5512     return Context.getSubstTemplateTypeParmType(
5513         cast<TemplateTypeParmType>(Parm),
5514         Context.getCanonicalType(Replacement));
5515   }
5516 
5517   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5518     unsigned Idx = 0;
5519     QualType Parm = readType(*Loc.F, Record, Idx);
5520     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5521     return Context.getSubstTemplateTypeParmPackType(
5522                                                cast<TemplateTypeParmType>(Parm),
5523                                                      ArgPack);
5524   }
5525 
5526   case TYPE_INJECTED_CLASS_NAME: {
5527     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5528     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5529     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5530     // for AST reading, too much interdependencies.
5531     const Type *T = nullptr;
5532     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5533       if (const Type *Existing = DI->getTypeForDecl()) {
5534         T = Existing;
5535         break;
5536       }
5537     }
5538     if (!T) {
5539       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5540       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5541         DI->setTypeForDecl(T);
5542     }
5543     return QualType(T, 0);
5544   }
5545 
5546   case TYPE_TEMPLATE_TYPE_PARM: {
5547     unsigned Idx = 0;
5548     unsigned Depth = Record[Idx++];
5549     unsigned Index = Record[Idx++];
5550     bool Pack = Record[Idx++];
5551     TemplateTypeParmDecl *D
5552       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5553     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5554   }
5555 
5556   case TYPE_DEPENDENT_NAME: {
5557     unsigned Idx = 0;
5558     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5559     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5560     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5561     QualType Canon = readType(*Loc.F, Record, Idx);
5562     if (!Canon.isNull())
5563       Canon = Context.getCanonicalType(Canon);
5564     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5565   }
5566 
5567   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5568     unsigned Idx = 0;
5569     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5570     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5571     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5572     unsigned NumArgs = Record[Idx++];
5573     SmallVector<TemplateArgument, 8> Args;
5574     Args.reserve(NumArgs);
5575     while (NumArgs--)
5576       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5577     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5578                                                       Args.size(), Args.data());
5579   }
5580 
5581   case TYPE_DEPENDENT_SIZED_ARRAY: {
5582     unsigned Idx = 0;
5583 
5584     // ArrayType
5585     QualType ElementType = readType(*Loc.F, Record, Idx);
5586     ArrayType::ArraySizeModifier ASM
5587       = (ArrayType::ArraySizeModifier)Record[Idx++];
5588     unsigned IndexTypeQuals = Record[Idx++];
5589 
5590     // DependentSizedArrayType
5591     Expr *NumElts = ReadExpr(*Loc.F);
5592     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5593 
5594     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5595                                                IndexTypeQuals, Brackets);
5596   }
5597 
5598   case TYPE_TEMPLATE_SPECIALIZATION: {
5599     unsigned Idx = 0;
5600     bool IsDependent = Record[Idx++];
5601     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5602     SmallVector<TemplateArgument, 8> Args;
5603     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5604     QualType Underlying = readType(*Loc.F, Record, Idx);
5605     QualType T;
5606     if (Underlying.isNull())
5607       T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5608                                                           Args.size());
5609     else
5610       T = Context.getTemplateSpecializationType(Name, Args.data(),
5611                                                  Args.size(), Underlying);
5612     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5613     return T;
5614   }
5615 
5616   case TYPE_ATOMIC: {
5617     if (Record.size() != 1) {
5618       Error("Incorrect encoding of atomic type");
5619       return QualType();
5620     }
5621     QualType ValueType = readType(*Loc.F, Record, Idx);
5622     return Context.getAtomicType(ValueType);
5623   }
5624   }
5625   llvm_unreachable("Invalid TypeCode!");
5626 }
5627 
readExceptionSpec(ModuleFile & ModuleFile,SmallVectorImpl<QualType> & Exceptions,FunctionProtoType::ExceptionSpecInfo & ESI,const RecordData & Record,unsigned & Idx)5628 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5629                                   SmallVectorImpl<QualType> &Exceptions,
5630                                   FunctionProtoType::ExceptionSpecInfo &ESI,
5631                                   const RecordData &Record, unsigned &Idx) {
5632   ExceptionSpecificationType EST =
5633       static_cast<ExceptionSpecificationType>(Record[Idx++]);
5634   ESI.Type = EST;
5635   if (EST == EST_Dynamic) {
5636     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
5637       Exceptions.push_back(readType(ModuleFile, Record, Idx));
5638     ESI.Exceptions = Exceptions;
5639   } else if (EST == EST_ComputedNoexcept) {
5640     ESI.NoexceptExpr = ReadExpr(ModuleFile);
5641   } else if (EST == EST_Uninstantiated) {
5642     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5643     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5644   } else if (EST == EST_Unevaluated) {
5645     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5646   }
5647 }
5648 
5649 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5650   ASTReader &Reader;
5651   ModuleFile &F;
5652   const ASTReader::RecordData &Record;
5653   unsigned &Idx;
5654 
ReadSourceLocation(const ASTReader::RecordData & R,unsigned & I)5655   SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5656                                     unsigned &I) {
5657     return Reader.ReadSourceLocation(F, R, I);
5658   }
5659 
5660   template<typename T>
ReadDeclAs(const ASTReader::RecordData & Record,unsigned & Idx)5661   T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5662     return Reader.ReadDeclAs<T>(F, Record, Idx);
5663   }
5664 
5665 public:
TypeLocReader(ASTReader & Reader,ModuleFile & F,const ASTReader::RecordData & Record,unsigned & Idx)5666   TypeLocReader(ASTReader &Reader, ModuleFile &F,
5667                 const ASTReader::RecordData &Record, unsigned &Idx)
5668     : Reader(Reader), F(F), Record(Record), Idx(Idx)
5669   { }
5670 
5671   // We want compile-time assurance that we've enumerated all of
5672   // these, so unfortunately we have to declare them first, then
5673   // define them out-of-line.
5674 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5675 #define TYPELOC(CLASS, PARENT) \
5676   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5677 #include "clang/AST/TypeLocNodes.def"
5678 
5679   void VisitFunctionTypeLoc(FunctionTypeLoc);
5680   void VisitArrayTypeLoc(ArrayTypeLoc);
5681 };
5682 
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)5683 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5684   // nothing to do
5685 }
VisitBuiltinTypeLoc(BuiltinTypeLoc TL)5686 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5687   TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5688   if (TL.needsExtraLocalData()) {
5689     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5690     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5691     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5692     TL.setModeAttr(Record[Idx++]);
5693   }
5694 }
VisitComplexTypeLoc(ComplexTypeLoc TL)5695 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5696   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5697 }
VisitPointerTypeLoc(PointerTypeLoc TL)5698 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5699   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5700 }
VisitDecayedTypeLoc(DecayedTypeLoc TL)5701 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5702   // nothing to do
5703 }
VisitAdjustedTypeLoc(AdjustedTypeLoc TL)5704 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5705   // nothing to do
5706 }
VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL)5707 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5708   TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5709 }
VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL)5710 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5711   TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5712 }
VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL)5713 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5714   TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5715 }
VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL)5716 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5717   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5718   TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5719 }
VisitArrayTypeLoc(ArrayTypeLoc TL)5720 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5721   TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5722   TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5723   if (Record[Idx++])
5724     TL.setSizeExpr(Reader.ReadExpr(F));
5725   else
5726     TL.setSizeExpr(nullptr);
5727 }
VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL)5728 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5729   VisitArrayTypeLoc(TL);
5730 }
VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL)5731 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5732   VisitArrayTypeLoc(TL);
5733 }
VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL)5734 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5735   VisitArrayTypeLoc(TL);
5736 }
VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL)5737 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5738                                             DependentSizedArrayTypeLoc TL) {
5739   VisitArrayTypeLoc(TL);
5740 }
VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL)5741 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5742                                         DependentSizedExtVectorTypeLoc TL) {
5743   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5744 }
VisitVectorTypeLoc(VectorTypeLoc TL)5745 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5746   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5747 }
VisitExtVectorTypeLoc(ExtVectorTypeLoc TL)5748 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5749   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5750 }
VisitFunctionTypeLoc(FunctionTypeLoc TL)5751 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5752   TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5753   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5754   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5755   TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5756   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5757     TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5758   }
5759 }
VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL)5760 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5761   VisitFunctionTypeLoc(TL);
5762 }
VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL)5763 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5764   VisitFunctionTypeLoc(TL);
5765 }
VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL)5766 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5767   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5768 }
VisitTypedefTypeLoc(TypedefTypeLoc TL)5769 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5770   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5771 }
VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL)5772 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5773   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5774   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5775   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5776 }
VisitTypeOfTypeLoc(TypeOfTypeLoc TL)5777 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5778   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5779   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5780   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5781   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5782 }
VisitDecltypeTypeLoc(DecltypeTypeLoc TL)5783 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5784   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5785 }
VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL)5786 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5787   TL.setKWLoc(ReadSourceLocation(Record, Idx));
5788   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5789   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5790   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5791 }
VisitAutoTypeLoc(AutoTypeLoc TL)5792 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5793   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5794 }
VisitRecordTypeLoc(RecordTypeLoc TL)5795 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5796   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5797 }
VisitEnumTypeLoc(EnumTypeLoc TL)5798 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5799   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5800 }
VisitAttributedTypeLoc(AttributedTypeLoc TL)5801 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5802   TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5803   if (TL.hasAttrOperand()) {
5804     SourceRange range;
5805     range.setBegin(ReadSourceLocation(Record, Idx));
5806     range.setEnd(ReadSourceLocation(Record, Idx));
5807     TL.setAttrOperandParensRange(range);
5808   }
5809   if (TL.hasAttrExprOperand()) {
5810     if (Record[Idx++])
5811       TL.setAttrExprOperand(Reader.ReadExpr(F));
5812     else
5813       TL.setAttrExprOperand(nullptr);
5814   } else if (TL.hasAttrEnumOperand())
5815     TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5816 }
VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)5817 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5818   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5819 }
VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL)5820 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5821                                             SubstTemplateTypeParmTypeLoc TL) {
5822   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5823 }
VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL)5824 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5825                                           SubstTemplateTypeParmPackTypeLoc TL) {
5826   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5827 }
VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL)5828 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5829                                            TemplateSpecializationTypeLoc TL) {
5830   TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5831   TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5832   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5833   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5834   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5835     TL.setArgLocInfo(i,
5836         Reader.GetTemplateArgumentLocInfo(F,
5837                                           TL.getTypePtr()->getArg(i).getKind(),
5838                                           Record, Idx));
5839 }
VisitParenTypeLoc(ParenTypeLoc TL)5840 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5841   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5842   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5843 }
VisitElaboratedTypeLoc(ElaboratedTypeLoc TL)5844 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5845   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5846   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5847 }
VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL)5848 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5849   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5850 }
VisitDependentNameTypeLoc(DependentNameTypeLoc TL)5851 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5852   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5853   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5854   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5855 }
VisitDependentTemplateSpecializationTypeLoc(DependentTemplateSpecializationTypeLoc TL)5856 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5857        DependentTemplateSpecializationTypeLoc TL) {
5858   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5859   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5860   TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5861   TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5862   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5863   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5864   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5865     TL.setArgLocInfo(I,
5866         Reader.GetTemplateArgumentLocInfo(F,
5867                                           TL.getTypePtr()->getArg(I).getKind(),
5868                                           Record, Idx));
5869 }
VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL)5870 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5871   TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5872 }
VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)5873 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5874   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5875 }
VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL)5876 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5877   TL.setHasBaseTypeAsWritten(Record[Idx++]);
5878   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5879   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5880   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5881     TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5882 }
VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL)5883 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5884   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5885 }
VisitAtomicTypeLoc(AtomicTypeLoc TL)5886 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5887   TL.setKWLoc(ReadSourceLocation(Record, Idx));
5888   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5889   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5890 }
5891 
GetTypeSourceInfo(ModuleFile & F,const RecordData & Record,unsigned & Idx)5892 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5893                                              const RecordData &Record,
5894                                              unsigned &Idx) {
5895   QualType InfoTy = readType(F, Record, Idx);
5896   if (InfoTy.isNull())
5897     return nullptr;
5898 
5899   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5900   TypeLocReader TLR(*this, F, Record, Idx);
5901   for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5902     TLR.Visit(TL);
5903   return TInfo;
5904 }
5905 
GetType(TypeID ID)5906 QualType ASTReader::GetType(TypeID ID) {
5907   unsigned FastQuals = ID & Qualifiers::FastMask;
5908   unsigned Index = ID >> Qualifiers::FastWidth;
5909 
5910   if (Index < NUM_PREDEF_TYPE_IDS) {
5911     QualType T;
5912     switch ((PredefinedTypeIDs)Index) {
5913     case PREDEF_TYPE_NULL_ID: return QualType();
5914     case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5915     case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5916 
5917     case PREDEF_TYPE_CHAR_U_ID:
5918     case PREDEF_TYPE_CHAR_S_ID:
5919       // FIXME: Check that the signedness of CharTy is correct!
5920       T = Context.CharTy;
5921       break;
5922 
5923     case PREDEF_TYPE_UCHAR_ID:      T = Context.UnsignedCharTy;     break;
5924     case PREDEF_TYPE_USHORT_ID:     T = Context.UnsignedShortTy;    break;
5925     case PREDEF_TYPE_UINT_ID:       T = Context.UnsignedIntTy;      break;
5926     case PREDEF_TYPE_ULONG_ID:      T = Context.UnsignedLongTy;     break;
5927     case PREDEF_TYPE_ULONGLONG_ID:  T = Context.UnsignedLongLongTy; break;
5928     case PREDEF_TYPE_UINT128_ID:    T = Context.UnsignedInt128Ty;   break;
5929     case PREDEF_TYPE_SCHAR_ID:      T = Context.SignedCharTy;       break;
5930     case PREDEF_TYPE_WCHAR_ID:      T = Context.WCharTy;            break;
5931     case PREDEF_TYPE_SHORT_ID:      T = Context.ShortTy;            break;
5932     case PREDEF_TYPE_INT_ID:        T = Context.IntTy;              break;
5933     case PREDEF_TYPE_LONG_ID:       T = Context.LongTy;             break;
5934     case PREDEF_TYPE_LONGLONG_ID:   T = Context.LongLongTy;         break;
5935     case PREDEF_TYPE_INT128_ID:     T = Context.Int128Ty;           break;
5936     case PREDEF_TYPE_HALF_ID:       T = Context.HalfTy;             break;
5937     case PREDEF_TYPE_FLOAT_ID:      T = Context.FloatTy;            break;
5938     case PREDEF_TYPE_DOUBLE_ID:     T = Context.DoubleTy;           break;
5939     case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy;       break;
5940     case PREDEF_TYPE_OVERLOAD_ID:   T = Context.OverloadTy;         break;
5941     case PREDEF_TYPE_BOUND_MEMBER:  T = Context.BoundMemberTy;      break;
5942     case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy;     break;
5943     case PREDEF_TYPE_DEPENDENT_ID:  T = Context.DependentTy;        break;
5944     case PREDEF_TYPE_UNKNOWN_ANY:   T = Context.UnknownAnyTy;       break;
5945     case PREDEF_TYPE_NULLPTR_ID:    T = Context.NullPtrTy;          break;
5946     case PREDEF_TYPE_CHAR16_ID:     T = Context.Char16Ty;           break;
5947     case PREDEF_TYPE_CHAR32_ID:     T = Context.Char32Ty;           break;
5948     case PREDEF_TYPE_OBJC_ID:       T = Context.ObjCBuiltinIdTy;    break;
5949     case PREDEF_TYPE_OBJC_CLASS:    T = Context.ObjCBuiltinClassTy; break;
5950     case PREDEF_TYPE_OBJC_SEL:      T = Context.ObjCBuiltinSelTy;   break;
5951     case PREDEF_TYPE_IMAGE1D_ID:    T = Context.OCLImage1dTy;       break;
5952     case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5953     case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5954     case PREDEF_TYPE_IMAGE2D_ID:    T = Context.OCLImage2dTy;       break;
5955     case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5956     case PREDEF_TYPE_IMAGE3D_ID:    T = Context.OCLImage3dTy;       break;
5957     case PREDEF_TYPE_SAMPLER_ID:    T = Context.OCLSamplerTy;       break;
5958     case PREDEF_TYPE_EVENT_ID:      T = Context.OCLEventTy;         break;
5959     case PREDEF_TYPE_AUTO_DEDUCT:   T = Context.getAutoDeductType(); break;
5960 
5961     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5962       T = Context.getAutoRRefDeductType();
5963       break;
5964 
5965     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5966       T = Context.ARCUnbridgedCastTy;
5967       break;
5968 
5969     case PREDEF_TYPE_VA_LIST_TAG:
5970       T = Context.getVaListTagType();
5971       break;
5972 
5973     case PREDEF_TYPE_BUILTIN_FN:
5974       T = Context.BuiltinFnTy;
5975       break;
5976     }
5977 
5978     assert(!T.isNull() && "Unknown predefined type");
5979     return T.withFastQualifiers(FastQuals);
5980   }
5981 
5982   Index -= NUM_PREDEF_TYPE_IDS;
5983   assert(Index < TypesLoaded.size() && "Type index out-of-range");
5984   if (TypesLoaded[Index].isNull()) {
5985     TypesLoaded[Index] = readTypeRecord(Index);
5986     if (TypesLoaded[Index].isNull())
5987       return QualType();
5988 
5989     TypesLoaded[Index]->setFromAST();
5990     if (DeserializationListener)
5991       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5992                                         TypesLoaded[Index]);
5993   }
5994 
5995   return TypesLoaded[Index].withFastQualifiers(FastQuals);
5996 }
5997 
getLocalType(ModuleFile & F,unsigned LocalID)5998 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5999   return GetType(getGlobalTypeID(F, LocalID));
6000 }
6001 
6002 serialization::TypeID
getGlobalTypeID(ModuleFile & F,unsigned LocalID) const6003 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6004   unsigned FastQuals = LocalID & Qualifiers::FastMask;
6005   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6006 
6007   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6008     return LocalID;
6009 
6010   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6011     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6012   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6013 
6014   unsigned GlobalIndex = LocalIndex + I->second;
6015   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6016 }
6017 
6018 TemplateArgumentLocInfo
GetTemplateArgumentLocInfo(ModuleFile & F,TemplateArgument::ArgKind Kind,const RecordData & Record,unsigned & Index)6019 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6020                                       TemplateArgument::ArgKind Kind,
6021                                       const RecordData &Record,
6022                                       unsigned &Index) {
6023   switch (Kind) {
6024   case TemplateArgument::Expression:
6025     return ReadExpr(F);
6026   case TemplateArgument::Type:
6027     return GetTypeSourceInfo(F, Record, Index);
6028   case TemplateArgument::Template: {
6029     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6030                                                                      Index);
6031     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6032     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6033                                    SourceLocation());
6034   }
6035   case TemplateArgument::TemplateExpansion: {
6036     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6037                                                                      Index);
6038     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6039     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6040     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6041                                    EllipsisLoc);
6042   }
6043   case TemplateArgument::Null:
6044   case TemplateArgument::Integral:
6045   case TemplateArgument::Declaration:
6046   case TemplateArgument::NullPtr:
6047   case TemplateArgument::Pack:
6048     // FIXME: Is this right?
6049     return TemplateArgumentLocInfo();
6050   }
6051   llvm_unreachable("unexpected template argument loc");
6052 }
6053 
6054 TemplateArgumentLoc
ReadTemplateArgumentLoc(ModuleFile & F,const RecordData & Record,unsigned & Index)6055 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6056                                    const RecordData &Record, unsigned &Index) {
6057   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6058 
6059   if (Arg.getKind() == TemplateArgument::Expression) {
6060     if (Record[Index++]) // bool InfoHasSameExpr.
6061       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6062   }
6063   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6064                                                              Record, Index));
6065 }
6066 
6067 const ASTTemplateArgumentListInfo*
ReadASTTemplateArgumentListInfo(ModuleFile & F,const RecordData & Record,unsigned & Index)6068 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6069                                            const RecordData &Record,
6070                                            unsigned &Index) {
6071   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6072   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6073   unsigned NumArgsAsWritten = Record[Index++];
6074   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6075   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6076     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6077   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6078 }
6079 
GetExternalDecl(uint32_t ID)6080 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6081   return GetDecl(ID);
6082 }
6083 
CompleteRedeclChain(const Decl * D)6084 void ASTReader::CompleteRedeclChain(const Decl *D) {
6085   if (NumCurrentElementsDeserializing) {
6086     // We arrange to not care about the complete redeclaration chain while we're
6087     // deserializing. Just remember that the AST has marked this one as complete
6088     // but that it's not actually complete yet, so we know we still need to
6089     // complete it later.
6090     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6091     return;
6092   }
6093 
6094   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6095 
6096   // If this is a named declaration, complete it by looking it up
6097   // within its context.
6098   //
6099   // FIXME: Merging a function definition should merge
6100   // all mergeable entities within it.
6101   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6102       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6103     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6104       auto *II = Name.getAsIdentifierInfo();
6105       if (isa<TranslationUnitDecl>(DC) && II) {
6106         // Outside of C++, we don't have a lookup table for the TU, so update
6107         // the identifier instead. In C++, either way should work fine.
6108         if (II->isOutOfDate())
6109           updateOutOfDateIdentifier(*II);
6110       } else
6111         DC->lookup(Name);
6112     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
6113       // FIXME: It'd be nice to do something a bit more targeted here.
6114       D->getDeclContext()->decls_begin();
6115     }
6116   }
6117 }
6118 
readCXXBaseSpecifiers(ModuleFile & M,const RecordData & Record,unsigned & Idx)6119 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6120                                           const RecordData &Record,
6121                                           unsigned &Idx) {
6122   if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6123     Error("malformed AST file: missing C++ base specifier");
6124     return 0;
6125   }
6126 
6127   unsigned LocalID = Record[Idx++];
6128   return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6129 }
6130 
GetExternalCXXBaseSpecifiers(uint64_t Offset)6131 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6132   RecordLocation Loc = getLocalBitOffset(Offset);
6133   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6134   SavedStreamPosition SavedPosition(Cursor);
6135   Cursor.JumpToBit(Loc.Offset);
6136   ReadingKindTracker ReadingKind(Read_Decl, *this);
6137   RecordData Record;
6138   unsigned Code = Cursor.ReadCode();
6139   unsigned RecCode = Cursor.readRecord(Code, Record);
6140   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
6141     Error("malformed AST file: missing C++ base specifiers");
6142     return nullptr;
6143   }
6144 
6145   unsigned Idx = 0;
6146   unsigned NumBases = Record[Idx++];
6147   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6148   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6149   for (unsigned I = 0; I != NumBases; ++I)
6150     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6151   return Bases;
6152 }
6153 
6154 serialization::DeclID
getGlobalDeclID(ModuleFile & F,LocalDeclID LocalID) const6155 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6156   if (LocalID < NUM_PREDEF_DECL_IDS)
6157     return LocalID;
6158 
6159   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6160     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6161   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6162 
6163   return LocalID + I->second;
6164 }
6165 
isDeclIDFromModule(serialization::GlobalDeclID ID,ModuleFile & M) const6166 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6167                                    ModuleFile &M) const {
6168   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6169   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6170   return &M == I->second;
6171 }
6172 
getOwningModuleFile(const Decl * D)6173 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
6174   if (!D->isFromASTFile())
6175     return nullptr;
6176   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6177   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6178   return I->second;
6179 }
6180 
getSourceLocationForDeclID(GlobalDeclID ID)6181 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6182   if (ID < NUM_PREDEF_DECL_IDS)
6183     return SourceLocation();
6184 
6185   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6186 
6187   if (Index > DeclsLoaded.size()) {
6188     Error("declaration ID out-of-range for AST file");
6189     return SourceLocation();
6190   }
6191 
6192   if (Decl *D = DeclsLoaded[Index])
6193     return D->getLocation();
6194 
6195   unsigned RawLocation = 0;
6196   RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6197   return ReadSourceLocation(*Rec.F, RawLocation);
6198 }
6199 
GetExistingDecl(DeclID ID)6200 Decl *ASTReader::GetExistingDecl(DeclID ID) {
6201   if (ID < NUM_PREDEF_DECL_IDS) {
6202     switch ((PredefinedDeclIDs)ID) {
6203     case PREDEF_DECL_NULL_ID:
6204       return nullptr;
6205 
6206     case PREDEF_DECL_TRANSLATION_UNIT_ID:
6207       return Context.getTranslationUnitDecl();
6208 
6209     case PREDEF_DECL_OBJC_ID_ID:
6210       return Context.getObjCIdDecl();
6211 
6212     case PREDEF_DECL_OBJC_SEL_ID:
6213       return Context.getObjCSelDecl();
6214 
6215     case PREDEF_DECL_OBJC_CLASS_ID:
6216       return Context.getObjCClassDecl();
6217 
6218     case PREDEF_DECL_OBJC_PROTOCOL_ID:
6219       return Context.getObjCProtocolDecl();
6220 
6221     case PREDEF_DECL_INT_128_ID:
6222       return Context.getInt128Decl();
6223 
6224     case PREDEF_DECL_UNSIGNED_INT_128_ID:
6225       return Context.getUInt128Decl();
6226 
6227     case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6228       return Context.getObjCInstanceTypeDecl();
6229 
6230     case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6231       return Context.getBuiltinVaListDecl();
6232     }
6233   }
6234 
6235   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6236 
6237   if (Index >= DeclsLoaded.size()) {
6238     assert(0 && "declaration ID out-of-range for AST file");
6239     Error("declaration ID out-of-range for AST file");
6240     return nullptr;
6241   }
6242 
6243   return DeclsLoaded[Index];
6244 }
6245 
GetDecl(DeclID ID)6246 Decl *ASTReader::GetDecl(DeclID ID) {
6247   if (ID < NUM_PREDEF_DECL_IDS)
6248     return GetExistingDecl(ID);
6249 
6250   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6251 
6252   if (Index >= DeclsLoaded.size()) {
6253     assert(0 && "declaration ID out-of-range for AST file");
6254     Error("declaration ID out-of-range for AST file");
6255     return nullptr;
6256   }
6257 
6258   if (!DeclsLoaded[Index]) {
6259     ReadDeclRecord(ID);
6260     if (DeserializationListener)
6261       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6262   }
6263 
6264   return DeclsLoaded[Index];
6265 }
6266 
mapGlobalIDToModuleFileGlobalID(ModuleFile & M,DeclID GlobalID)6267 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6268                                                   DeclID GlobalID) {
6269   if (GlobalID < NUM_PREDEF_DECL_IDS)
6270     return GlobalID;
6271 
6272   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6273   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6274   ModuleFile *Owner = I->second;
6275 
6276   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6277     = M.GlobalToLocalDeclIDs.find(Owner);
6278   if (Pos == M.GlobalToLocalDeclIDs.end())
6279     return 0;
6280 
6281   return GlobalID - Owner->BaseDeclID + Pos->second;
6282 }
6283 
ReadDeclID(ModuleFile & F,const RecordData & Record,unsigned & Idx)6284 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6285                                             const RecordData &Record,
6286                                             unsigned &Idx) {
6287   if (Idx >= Record.size()) {
6288     Error("Corrupted AST file");
6289     return 0;
6290   }
6291 
6292   return getGlobalDeclID(F, Record[Idx++]);
6293 }
6294 
6295 /// \brief Resolve the offset of a statement into a statement.
6296 ///
6297 /// This operation will read a new statement from the external
6298 /// source each time it is called, and is meant to be used via a
6299 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
GetExternalDeclStmt(uint64_t Offset)6300 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6301   // Switch case IDs are per Decl.
6302   ClearSwitchCaseIDs();
6303 
6304   // Offset here is a global offset across the entire chain.
6305   RecordLocation Loc = getLocalBitOffset(Offset);
6306   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6307   return ReadStmtFromStream(*Loc.F);
6308 }
6309 
6310 namespace {
6311   class FindExternalLexicalDeclsVisitor {
6312     ASTReader &Reader;
6313     const DeclContext *DC;
6314     bool (*isKindWeWant)(Decl::Kind);
6315 
6316     SmallVectorImpl<Decl*> &Decls;
6317     bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6318 
6319   public:
FindExternalLexicalDeclsVisitor(ASTReader & Reader,const DeclContext * DC,bool (* isKindWeWant)(Decl::Kind),SmallVectorImpl<Decl * > & Decls)6320     FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6321                                     bool (*isKindWeWant)(Decl::Kind),
6322                                     SmallVectorImpl<Decl*> &Decls)
6323       : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6324     {
6325       for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6326         PredefsVisited[I] = false;
6327     }
6328 
visit(ModuleFile & M,bool Preorder,void * UserData)6329     static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6330       if (Preorder)
6331         return false;
6332 
6333       FindExternalLexicalDeclsVisitor *This
6334         = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6335 
6336       ModuleFile::DeclContextInfosMap::iterator Info
6337         = M.DeclContextInfos.find(This->DC);
6338       if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6339         return false;
6340 
6341       // Load all of the declaration IDs
6342       for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6343                                *IDE = ID + Info->second.NumLexicalDecls;
6344            ID != IDE; ++ID) {
6345         if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6346           continue;
6347 
6348         // Don't add predefined declarations to the lexical context more
6349         // than once.
6350         if (ID->second < NUM_PREDEF_DECL_IDS) {
6351           if (This->PredefsVisited[ID->second])
6352             continue;
6353 
6354           This->PredefsVisited[ID->second] = true;
6355         }
6356 
6357         if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6358           if (!This->DC->isDeclInLexicalTraversal(D))
6359             This->Decls.push_back(D);
6360         }
6361       }
6362 
6363       return false;
6364     }
6365   };
6366 }
6367 
FindExternalLexicalDecls(const DeclContext * DC,bool (* isKindWeWant)(Decl::Kind),SmallVectorImpl<Decl * > & Decls)6368 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6369                                          bool (*isKindWeWant)(Decl::Kind),
6370                                          SmallVectorImpl<Decl*> &Decls) {
6371   // There might be lexical decls in multiple modules, for the TU at
6372   // least. Walk all of the modules in the order they were loaded.
6373   FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6374   ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6375   ++NumLexicalDeclContextsRead;
6376   return ELR_Success;
6377 }
6378 
6379 namespace {
6380 
6381 class DeclIDComp {
6382   ASTReader &Reader;
6383   ModuleFile &Mod;
6384 
6385 public:
DeclIDComp(ASTReader & Reader,ModuleFile & M)6386   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6387 
operator ()(LocalDeclID L,LocalDeclID R) const6388   bool operator()(LocalDeclID L, LocalDeclID R) const {
6389     SourceLocation LHS = getLocation(L);
6390     SourceLocation RHS = getLocation(R);
6391     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6392   }
6393 
operator ()(SourceLocation LHS,LocalDeclID R) const6394   bool operator()(SourceLocation LHS, LocalDeclID R) const {
6395     SourceLocation RHS = getLocation(R);
6396     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6397   }
6398 
operator ()(LocalDeclID L,SourceLocation RHS) const6399   bool operator()(LocalDeclID L, SourceLocation RHS) const {
6400     SourceLocation LHS = getLocation(L);
6401     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6402   }
6403 
getLocation(LocalDeclID ID) const6404   SourceLocation getLocation(LocalDeclID ID) const {
6405     return Reader.getSourceManager().getFileLoc(
6406             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6407   }
6408 };
6409 
6410 }
6411 
FindFileRegionDecls(FileID File,unsigned Offset,unsigned Length,SmallVectorImpl<Decl * > & Decls)6412 void ASTReader::FindFileRegionDecls(FileID File,
6413                                     unsigned Offset, unsigned Length,
6414                                     SmallVectorImpl<Decl *> &Decls) {
6415   SourceManager &SM = getSourceManager();
6416 
6417   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6418   if (I == FileDeclIDs.end())
6419     return;
6420 
6421   FileDeclsInfo &DInfo = I->second;
6422   if (DInfo.Decls.empty())
6423     return;
6424 
6425   SourceLocation
6426     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6427   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6428 
6429   DeclIDComp DIDComp(*this, *DInfo.Mod);
6430   ArrayRef<serialization::LocalDeclID>::iterator
6431     BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6432                                BeginLoc, DIDComp);
6433   if (BeginIt != DInfo.Decls.begin())
6434     --BeginIt;
6435 
6436   // If we are pointing at a top-level decl inside an objc container, we need
6437   // to backtrack until we find it otherwise we will fail to report that the
6438   // region overlaps with an objc container.
6439   while (BeginIt != DInfo.Decls.begin() &&
6440          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6441              ->isTopLevelDeclInObjCContainer())
6442     --BeginIt;
6443 
6444   ArrayRef<serialization::LocalDeclID>::iterator
6445     EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6446                              EndLoc, DIDComp);
6447   if (EndIt != DInfo.Decls.end())
6448     ++EndIt;
6449 
6450   for (ArrayRef<serialization::LocalDeclID>::iterator
6451          DIt = BeginIt; DIt != EndIt; ++DIt)
6452     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6453 }
6454 
6455 namespace {
6456   /// \brief ModuleFile visitor used to perform name lookup into a
6457   /// declaration context.
6458   class DeclContextNameLookupVisitor {
6459     ASTReader &Reader;
6460     ArrayRef<const DeclContext *> Contexts;
6461     DeclarationName Name;
6462     SmallVectorImpl<NamedDecl *> &Decls;
6463 
6464   public:
DeclContextNameLookupVisitor(ASTReader & Reader,ArrayRef<const DeclContext * > Contexts,DeclarationName Name,SmallVectorImpl<NamedDecl * > & Decls)6465     DeclContextNameLookupVisitor(ASTReader &Reader,
6466                                  ArrayRef<const DeclContext *> Contexts,
6467                                  DeclarationName Name,
6468                                  SmallVectorImpl<NamedDecl *> &Decls)
6469       : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6470 
visit(ModuleFile & M,void * UserData)6471     static bool visit(ModuleFile &M, void *UserData) {
6472       DeclContextNameLookupVisitor *This
6473         = static_cast<DeclContextNameLookupVisitor *>(UserData);
6474 
6475       // Check whether we have any visible declaration information for
6476       // this context in this module.
6477       ModuleFile::DeclContextInfosMap::iterator Info;
6478       bool FoundInfo = false;
6479       for (auto *DC : This->Contexts) {
6480         Info = M.DeclContextInfos.find(DC);
6481         if (Info != M.DeclContextInfos.end() &&
6482             Info->second.NameLookupTableData) {
6483           FoundInfo = true;
6484           break;
6485         }
6486       }
6487 
6488       if (!FoundInfo)
6489         return false;
6490 
6491       // Look for this name within this module.
6492       ASTDeclContextNameLookupTable *LookupTable =
6493         Info->second.NameLookupTableData;
6494       ASTDeclContextNameLookupTable::iterator Pos
6495         = LookupTable->find(This->Name);
6496       if (Pos == LookupTable->end())
6497         return false;
6498 
6499       bool FoundAnything = false;
6500       ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6501       for (; Data.first != Data.second; ++Data.first) {
6502         NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6503         if (!ND)
6504           continue;
6505 
6506         if (ND->getDeclName() != This->Name) {
6507           // A name might be null because the decl's redeclarable part is
6508           // currently read before reading its name. The lookup is triggered by
6509           // building that decl (likely indirectly), and so it is later in the
6510           // sense of "already existing" and can be ignored here.
6511           // FIXME: This should not happen; deserializing declarations should
6512           // not perform lookups since that can lead to deserialization cycles.
6513           continue;
6514         }
6515 
6516         // Record this declaration.
6517         FoundAnything = true;
6518         This->Decls.push_back(ND);
6519       }
6520 
6521       return FoundAnything;
6522     }
6523   };
6524 }
6525 
6526 /// \brief Retrieve the "definitive" module file for the definition of the
6527 /// given declaration context, if there is one.
6528 ///
6529 /// The "definitive" module file is the only place where we need to look to
6530 /// find information about the declarations within the given declaration
6531 /// context. For example, C++ and Objective-C classes, C structs/unions, and
6532 /// Objective-C protocols, categories, and extensions are all defined in a
6533 /// single place in the source code, so they have definitive module files
6534 /// associated with them. C++ namespaces, on the other hand, can have
6535 /// definitions in multiple different module files.
6536 ///
6537 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6538 /// NDEBUG checking.
getDefinitiveModuleFileFor(const DeclContext * DC,ASTReader & Reader)6539 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6540                                               ASTReader &Reader) {
6541   if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6542     return Reader.getOwningModuleFile(cast<Decl>(DefDC));
6543 
6544   return nullptr;
6545 }
6546 
6547 bool
FindExternalVisibleDeclsByName(const DeclContext * DC,DeclarationName Name)6548 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6549                                           DeclarationName Name) {
6550   assert(DC->hasExternalVisibleStorage() &&
6551          "DeclContext has no visible decls in storage");
6552   if (!Name)
6553     return false;
6554 
6555   Deserializing LookupResults(this);
6556 
6557   SmallVector<NamedDecl *, 64> Decls;
6558 
6559   // Compute the declaration contexts we need to look into. Multiple such
6560   // declaration contexts occur when two declaration contexts from disjoint
6561   // modules get merged, e.g., when two namespaces with the same name are
6562   // independently defined in separate modules.
6563   SmallVector<const DeclContext *, 2> Contexts;
6564   Contexts.push_back(DC);
6565 
6566   if (DC->isNamespace()) {
6567     auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6568     if (Merged != MergedDecls.end()) {
6569       for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6570         Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6571     }
6572   }
6573 
6574   auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
6575     DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
6576 
6577     // If we can definitively determine which module file to look into,
6578     // only look there. Otherwise, look in all module files.
6579     ModuleFile *Definitive;
6580     if (Contexts.size() == 1 &&
6581         (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6582       DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6583     } else {
6584       ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6585     }
6586   };
6587 
6588   LookUpInContexts(Contexts);
6589 
6590   // If this might be an implicit special member function, then also search
6591   // all merged definitions of the surrounding class. We need to search them
6592   // individually, because finding an entity in one of them doesn't imply that
6593   // we can't find a different entity in another one.
6594   if (isa<CXXRecordDecl>(DC)) {
6595     auto Kind = Name.getNameKind();
6596     if (Kind == DeclarationName::CXXConstructorName ||
6597         Kind == DeclarationName::CXXDestructorName ||
6598         (Kind == DeclarationName::CXXOperatorName &&
6599          Name.getCXXOverloadedOperator() == OO_Equal)) {
6600       auto Merged = MergedLookups.find(DC);
6601       if (Merged != MergedLookups.end()) {
6602         for (unsigned I = 0; I != Merged->second.size(); ++I) {
6603           LookUpInContexts(Merged->second[I]);
6604           // We might have just added some more merged lookups. If so, our
6605           // iterator is now invalid, so grab a fresh one before continuing.
6606           Merged = MergedLookups.find(DC);
6607         }
6608       }
6609     }
6610   }
6611 
6612   ++NumVisibleDeclContextsRead;
6613   SetExternalVisibleDeclsForName(DC, Name, Decls);
6614   return !Decls.empty();
6615 }
6616 
6617 namespace {
6618   /// \brief ModuleFile visitor used to retrieve all visible names in a
6619   /// declaration context.
6620   class DeclContextAllNamesVisitor {
6621     ASTReader &Reader;
6622     SmallVectorImpl<const DeclContext *> &Contexts;
6623     DeclsMap &Decls;
6624     bool VisitAll;
6625 
6626   public:
DeclContextAllNamesVisitor(ASTReader & Reader,SmallVectorImpl<const DeclContext * > & Contexts,DeclsMap & Decls,bool VisitAll)6627     DeclContextAllNamesVisitor(ASTReader &Reader,
6628                                SmallVectorImpl<const DeclContext *> &Contexts,
6629                                DeclsMap &Decls, bool VisitAll)
6630       : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
6631 
visit(ModuleFile & M,void * UserData)6632     static bool visit(ModuleFile &M, void *UserData) {
6633       DeclContextAllNamesVisitor *This
6634         = static_cast<DeclContextAllNamesVisitor *>(UserData);
6635 
6636       // Check whether we have any visible declaration information for
6637       // this context in this module.
6638       ModuleFile::DeclContextInfosMap::iterator Info;
6639       bool FoundInfo = false;
6640       for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6641         Info = M.DeclContextInfos.find(This->Contexts[I]);
6642         if (Info != M.DeclContextInfos.end() &&
6643             Info->second.NameLookupTableData) {
6644           FoundInfo = true;
6645           break;
6646         }
6647       }
6648 
6649       if (!FoundInfo)
6650         return false;
6651 
6652       ASTDeclContextNameLookupTable *LookupTable =
6653         Info->second.NameLookupTableData;
6654       bool FoundAnything = false;
6655       for (ASTDeclContextNameLookupTable::data_iterator
6656              I = LookupTable->data_begin(), E = LookupTable->data_end();
6657            I != E;
6658            ++I) {
6659         ASTDeclContextNameLookupTrait::data_type Data = *I;
6660         for (; Data.first != Data.second; ++Data.first) {
6661           NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6662                                                                  *Data.first);
6663           if (!ND)
6664             continue;
6665 
6666           // Record this declaration.
6667           FoundAnything = true;
6668           This->Decls[ND->getDeclName()].push_back(ND);
6669         }
6670       }
6671 
6672       return FoundAnything && !This->VisitAll;
6673     }
6674   };
6675 }
6676 
completeVisibleDeclsMap(const DeclContext * DC)6677 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6678   if (!DC->hasExternalVisibleStorage())
6679     return;
6680   DeclsMap Decls;
6681 
6682   // Compute the declaration contexts we need to look into. Multiple such
6683   // declaration contexts occur when two declaration contexts from disjoint
6684   // modules get merged, e.g., when two namespaces with the same name are
6685   // independently defined in separate modules.
6686   SmallVector<const DeclContext *, 2> Contexts;
6687   Contexts.push_back(DC);
6688 
6689   if (DC->isNamespace()) {
6690     MergedDeclsMap::iterator Merged
6691       = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6692     if (Merged != MergedDecls.end()) {
6693       for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6694         Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6695     }
6696   }
6697 
6698   DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6699                                      /*VisitAll=*/DC->isFileContext());
6700   ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6701   ++NumVisibleDeclContextsRead;
6702 
6703   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
6704     SetExternalVisibleDeclsForName(DC, I->first, I->second);
6705   }
6706   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6707 }
6708 
6709 /// \brief Under non-PCH compilation the consumer receives the objc methods
6710 /// before receiving the implementation, and codegen depends on this.
6711 /// We simulate this by deserializing and passing to consumer the methods of the
6712 /// implementation before passing the deserialized implementation decl.
PassObjCImplDeclToConsumer(ObjCImplDecl * ImplD,ASTConsumer * Consumer)6713 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6714                                        ASTConsumer *Consumer) {
6715   assert(ImplD && Consumer);
6716 
6717   for (auto *I : ImplD->methods())
6718     Consumer->HandleInterestingDecl(DeclGroupRef(I));
6719 
6720   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6721 }
6722 
PassInterestingDeclsToConsumer()6723 void ASTReader::PassInterestingDeclsToConsumer() {
6724   assert(Consumer);
6725 
6726   if (PassingDeclsToConsumer)
6727     return;
6728 
6729   // Guard variable to avoid recursively redoing the process of passing
6730   // decls to consumer.
6731   SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6732                                                    true);
6733 
6734   while (!InterestingDecls.empty()) {
6735     Decl *D = InterestingDecls.front();
6736     InterestingDecls.pop_front();
6737 
6738     PassInterestingDeclToConsumer(D);
6739   }
6740 }
6741 
PassInterestingDeclToConsumer(Decl * D)6742 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6743   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6744     PassObjCImplDeclToConsumer(ImplD, Consumer);
6745   else
6746     Consumer->HandleInterestingDecl(DeclGroupRef(D));
6747 }
6748 
StartTranslationUnit(ASTConsumer * Consumer)6749 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6750   this->Consumer = Consumer;
6751 
6752   if (!Consumer)
6753     return;
6754 
6755   for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
6756     // Force deserialization of this decl, which will cause it to be queued for
6757     // passing to the consumer.
6758     GetDecl(EagerlyDeserializedDecls[I]);
6759   }
6760   EagerlyDeserializedDecls.clear();
6761 
6762   PassInterestingDeclsToConsumer();
6763 }
6764 
PrintStats()6765 void ASTReader::PrintStats() {
6766   std::fprintf(stderr, "*** AST File Statistics:\n");
6767 
6768   unsigned NumTypesLoaded
6769     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6770                                       QualType());
6771   unsigned NumDeclsLoaded
6772     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6773                                       (Decl *)nullptr);
6774   unsigned NumIdentifiersLoaded
6775     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6776                                             IdentifiersLoaded.end(),
6777                                             (IdentifierInfo *)nullptr);
6778   unsigned NumMacrosLoaded
6779     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6780                                        MacrosLoaded.end(),
6781                                        (MacroInfo *)nullptr);
6782   unsigned NumSelectorsLoaded
6783     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6784                                           SelectorsLoaded.end(),
6785                                           Selector());
6786 
6787   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6788     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
6789                  NumSLocEntriesRead, TotalNumSLocEntries,
6790                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6791   if (!TypesLoaded.empty())
6792     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
6793                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
6794                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6795   if (!DeclsLoaded.empty())
6796     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
6797                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6798                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6799   if (!IdentifiersLoaded.empty())
6800     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
6801                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6802                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6803   if (!MacrosLoaded.empty())
6804     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
6805                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6806                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6807   if (!SelectorsLoaded.empty())
6808     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
6809                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6810                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6811   if (TotalNumStatements)
6812     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
6813                  NumStatementsRead, TotalNumStatements,
6814                  ((float)NumStatementsRead/TotalNumStatements * 100));
6815   if (TotalNumMacros)
6816     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
6817                  NumMacrosRead, TotalNumMacros,
6818                  ((float)NumMacrosRead/TotalNumMacros * 100));
6819   if (TotalLexicalDeclContexts)
6820     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
6821                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6822                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6823                   * 100));
6824   if (TotalVisibleDeclContexts)
6825     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
6826                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6827                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6828                   * 100));
6829   if (TotalNumMethodPoolEntries) {
6830     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
6831                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6832                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6833                   * 100));
6834   }
6835   if (NumMethodPoolLookups) {
6836     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
6837                  NumMethodPoolHits, NumMethodPoolLookups,
6838                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6839   }
6840   if (NumMethodPoolTableLookups) {
6841     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
6842                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
6843                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6844                   * 100.0));
6845   }
6846 
6847   if (NumIdentifierLookupHits) {
6848     std::fprintf(stderr,
6849                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
6850                  NumIdentifierLookupHits, NumIdentifierLookups,
6851                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6852   }
6853 
6854   if (GlobalIndex) {
6855     std::fprintf(stderr, "\n");
6856     GlobalIndex->printStats();
6857   }
6858 
6859   std::fprintf(stderr, "\n");
6860   dump();
6861   std::fprintf(stderr, "\n");
6862 }
6863 
6864 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6865 static void
dumpModuleIDMap(StringRef Name,const ContinuousRangeMap<Key,ModuleFile *,InitialCapacity> & Map)6866 dumpModuleIDMap(StringRef Name,
6867                 const ContinuousRangeMap<Key, ModuleFile *,
6868                                          InitialCapacity> &Map) {
6869   if (Map.begin() == Map.end())
6870     return;
6871 
6872   typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6873   llvm::errs() << Name << ":\n";
6874   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6875        I != IEnd; ++I) {
6876     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
6877       << "\n";
6878   }
6879 }
6880 
dump()6881 void ASTReader::dump() {
6882   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6883   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6884   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6885   dumpModuleIDMap("Global type map", GlobalTypeMap);
6886   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6887   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6888   dumpModuleIDMap("Global macro map", GlobalMacroMap);
6889   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6890   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6891   dumpModuleIDMap("Global preprocessed entity map",
6892                   GlobalPreprocessedEntityMap);
6893 
6894   llvm::errs() << "\n*** PCH/Modules Loaded:";
6895   for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6896                                        MEnd = ModuleMgr.end();
6897        M != MEnd; ++M)
6898     (*M)->dump();
6899 }
6900 
6901 /// Return the amount of memory used by memory buffers, breaking down
6902 /// by heap-backed versus mmap'ed memory.
getMemoryBufferSizes(MemoryBufferSizes & sizes) const6903 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6904   for (ModuleConstIterator I = ModuleMgr.begin(),
6905       E = ModuleMgr.end(); I != E; ++I) {
6906     if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6907       size_t bytes = buf->getBufferSize();
6908       switch (buf->getBufferKind()) {
6909         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6910           sizes.malloc_bytes += bytes;
6911           break;
6912         case llvm::MemoryBuffer::MemoryBuffer_MMap:
6913           sizes.mmap_bytes += bytes;
6914           break;
6915       }
6916     }
6917   }
6918 }
6919 
InitializeSema(Sema & S)6920 void ASTReader::InitializeSema(Sema &S) {
6921   SemaObj = &S;
6922   S.addExternalSource(this);
6923 
6924   // Makes sure any declarations that were deserialized "too early"
6925   // still get added to the identifier's declaration chains.
6926   for (uint64_t ID : PreloadedDeclIDs) {
6927     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6928     pushExternalDeclIntoScope(D, D->getDeclName());
6929   }
6930   PreloadedDeclIDs.clear();
6931 
6932   // FIXME: What happens if these are changed by a module import?
6933   if (!FPPragmaOptions.empty()) {
6934     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6935     SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6936   }
6937 
6938   // FIXME: What happens if these are changed by a module import?
6939   if (!OpenCLExtensions.empty()) {
6940     unsigned I = 0;
6941 #define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6942 #include "clang/Basic/OpenCLExtensions.def"
6943 
6944     assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6945   }
6946 
6947   UpdateSema();
6948 }
6949 
UpdateSema()6950 void ASTReader::UpdateSema() {
6951   assert(SemaObj && "no Sema to update");
6952 
6953   // Load the offsets of the declarations that Sema references.
6954   // They will be lazily deserialized when needed.
6955   if (!SemaDeclRefs.empty()) {
6956     assert(SemaDeclRefs.size() % 2 == 0);
6957     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6958       if (!SemaObj->StdNamespace)
6959         SemaObj->StdNamespace = SemaDeclRefs[I];
6960       if (!SemaObj->StdBadAlloc)
6961         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6962     }
6963     SemaDeclRefs.clear();
6964   }
6965 
6966   // Update the state of 'pragma clang optimize'. Use the same API as if we had
6967   // encountered the pragma in the source.
6968   if(OptimizeOffPragmaLocation.isValid())
6969     SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
6970 }
6971 
get(const char * NameStart,const char * NameEnd)6972 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6973   // Note that we are loading an identifier.
6974   Deserializing AnIdentifier(this);
6975   StringRef Name(NameStart, NameEnd - NameStart);
6976 
6977   // If there is a global index, look there first to determine which modules
6978   // provably do not have any results for this identifier.
6979   GlobalModuleIndex::HitSet Hits;
6980   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6981   if (!loadGlobalIndex()) {
6982     if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6983       HitsPtr = &Hits;
6984     }
6985   }
6986   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6987                                   NumIdentifierLookups,
6988                                   NumIdentifierLookupHits);
6989   ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6990   IdentifierInfo *II = Visitor.getIdentifierInfo();
6991   markIdentifierUpToDate(II);
6992   return II;
6993 }
6994 
6995 namespace clang {
6996   /// \brief An identifier-lookup iterator that enumerates all of the
6997   /// identifiers stored within a set of AST files.
6998   class ASTIdentifierIterator : public IdentifierIterator {
6999     /// \brief The AST reader whose identifiers are being enumerated.
7000     const ASTReader &Reader;
7001 
7002     /// \brief The current index into the chain of AST files stored in
7003     /// the AST reader.
7004     unsigned Index;
7005 
7006     /// \brief The current position within the identifier lookup table
7007     /// of the current AST file.
7008     ASTIdentifierLookupTable::key_iterator Current;
7009 
7010     /// \brief The end position within the identifier lookup table of
7011     /// the current AST file.
7012     ASTIdentifierLookupTable::key_iterator End;
7013 
7014   public:
7015     explicit ASTIdentifierIterator(const ASTReader &Reader);
7016 
7017     StringRef Next() override;
7018   };
7019 }
7020 
ASTIdentifierIterator(const ASTReader & Reader)7021 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
7022   : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
7023   ASTIdentifierLookupTable *IdTable
7024     = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7025   Current = IdTable->key_begin();
7026   End = IdTable->key_end();
7027 }
7028 
Next()7029 StringRef ASTIdentifierIterator::Next() {
7030   while (Current == End) {
7031     // If we have exhausted all of our AST files, we're done.
7032     if (Index == 0)
7033       return StringRef();
7034 
7035     --Index;
7036     ASTIdentifierLookupTable *IdTable
7037       = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7038         IdentifierLookupTable;
7039     Current = IdTable->key_begin();
7040     End = IdTable->key_end();
7041   }
7042 
7043   // We have any identifiers remaining in the current AST file; return
7044   // the next one.
7045   StringRef Result = *Current;
7046   ++Current;
7047   return Result;
7048 }
7049 
getIdentifiers()7050 IdentifierIterator *ASTReader::getIdentifiers() {
7051   if (!loadGlobalIndex())
7052     return GlobalIndex->createIdentifierIterator();
7053 
7054   return new ASTIdentifierIterator(*this);
7055 }
7056 
7057 namespace clang { namespace serialization {
7058   class ReadMethodPoolVisitor {
7059     ASTReader &Reader;
7060     Selector Sel;
7061     unsigned PriorGeneration;
7062     unsigned InstanceBits;
7063     unsigned FactoryBits;
7064     bool InstanceHasMoreThanOneDecl;
7065     bool FactoryHasMoreThanOneDecl;
7066     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7067     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7068 
7069   public:
ReadMethodPoolVisitor(ASTReader & Reader,Selector Sel,unsigned PriorGeneration)7070     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7071                           unsigned PriorGeneration)
7072         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
7073           InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7074           FactoryHasMoreThanOneDecl(false) {}
7075 
visit(ModuleFile & M,void * UserData)7076     static bool visit(ModuleFile &M, void *UserData) {
7077       ReadMethodPoolVisitor *This
7078         = static_cast<ReadMethodPoolVisitor *>(UserData);
7079 
7080       if (!M.SelectorLookupTable)
7081         return false;
7082 
7083       // If we've already searched this module file, skip it now.
7084       if (M.Generation <= This->PriorGeneration)
7085         return true;
7086 
7087       ++This->Reader.NumMethodPoolTableLookups;
7088       ASTSelectorLookupTable *PoolTable
7089         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7090       ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
7091       if (Pos == PoolTable->end())
7092         return false;
7093 
7094       ++This->Reader.NumMethodPoolTableHits;
7095       ++This->Reader.NumSelectorsRead;
7096       // FIXME: Not quite happy with the statistics here. We probably should
7097       // disable this tracking when called via LoadSelector.
7098       // Also, should entries without methods count as misses?
7099       ++This->Reader.NumMethodPoolEntriesRead;
7100       ASTSelectorLookupTrait::data_type Data = *Pos;
7101       if (This->Reader.DeserializationListener)
7102         This->Reader.DeserializationListener->SelectorRead(Data.ID,
7103                                                            This->Sel);
7104 
7105       This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7106       This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7107       This->InstanceBits = Data.InstanceBits;
7108       This->FactoryBits = Data.FactoryBits;
7109       This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7110       This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
7111       return true;
7112     }
7113 
7114     /// \brief Retrieve the instance methods found by this visitor.
getInstanceMethods() const7115     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7116       return InstanceMethods;
7117     }
7118 
7119     /// \brief Retrieve the instance methods found by this visitor.
getFactoryMethods() const7120     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7121       return FactoryMethods;
7122     }
7123 
getInstanceBits() const7124     unsigned getInstanceBits() const { return InstanceBits; }
getFactoryBits() const7125     unsigned getFactoryBits() const { return FactoryBits; }
instanceHasMoreThanOneDecl() const7126     bool instanceHasMoreThanOneDecl() const {
7127       return InstanceHasMoreThanOneDecl;
7128     }
factoryHasMoreThanOneDecl() const7129     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
7130   };
7131 } } // end namespace clang::serialization
7132 
7133 /// \brief Add the given set of methods to the method list.
addMethodsToPool(Sema & S,ArrayRef<ObjCMethodDecl * > Methods,ObjCMethodList & List)7134 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7135                              ObjCMethodList &List) {
7136   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7137     S.addMethodToGlobalList(&List, Methods[I]);
7138   }
7139 }
7140 
ReadMethodPool(Selector Sel)7141 void ASTReader::ReadMethodPool(Selector Sel) {
7142   // Get the selector generation and update it to the current generation.
7143   unsigned &Generation = SelectorGeneration[Sel];
7144   unsigned PriorGeneration = Generation;
7145   Generation = getGeneration();
7146 
7147   // Search for methods defined with this selector.
7148   ++NumMethodPoolLookups;
7149   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7150   ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7151 
7152   if (Visitor.getInstanceMethods().empty() &&
7153       Visitor.getFactoryMethods().empty())
7154     return;
7155 
7156   ++NumMethodPoolHits;
7157 
7158   if (!getSema())
7159     return;
7160 
7161   Sema &S = *getSema();
7162   Sema::GlobalMethodPool::iterator Pos
7163     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7164 
7165   Pos->second.first.setBits(Visitor.getInstanceBits());
7166   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
7167   Pos->second.second.setBits(Visitor.getFactoryBits());
7168   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
7169 
7170   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7171   // when building a module we keep every method individually and may need to
7172   // update hasMoreThanOneDecl as we add the methods.
7173   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7174   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7175 }
7176 
ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl * > & Namespaces)7177 void ASTReader::ReadKnownNamespaces(
7178                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7179   Namespaces.clear();
7180 
7181   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7182     if (NamespaceDecl *Namespace
7183                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7184       Namespaces.push_back(Namespace);
7185   }
7186 }
7187 
ReadUndefinedButUsed(llvm::DenseMap<NamedDecl *,SourceLocation> & Undefined)7188 void ASTReader::ReadUndefinedButUsed(
7189                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
7190   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7191     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
7192     SourceLocation Loc =
7193         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
7194     Undefined.insert(std::make_pair(D, Loc));
7195   }
7196 }
7197 
ReadTentativeDefinitions(SmallVectorImpl<VarDecl * > & TentativeDefs)7198 void ASTReader::ReadTentativeDefinitions(
7199                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
7200   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7201     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7202     if (Var)
7203       TentativeDefs.push_back(Var);
7204   }
7205   TentativeDefinitions.clear();
7206 }
7207 
ReadUnusedFileScopedDecls(SmallVectorImpl<const DeclaratorDecl * > & Decls)7208 void ASTReader::ReadUnusedFileScopedDecls(
7209                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7210   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7211     DeclaratorDecl *D
7212       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7213     if (D)
7214       Decls.push_back(D);
7215   }
7216   UnusedFileScopedDecls.clear();
7217 }
7218 
ReadDelegatingConstructors(SmallVectorImpl<CXXConstructorDecl * > & Decls)7219 void ASTReader::ReadDelegatingConstructors(
7220                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7221   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7222     CXXConstructorDecl *D
7223       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7224     if (D)
7225       Decls.push_back(D);
7226   }
7227   DelegatingCtorDecls.clear();
7228 }
7229 
ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl * > & Decls)7230 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7231   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7232     TypedefNameDecl *D
7233       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7234     if (D)
7235       Decls.push_back(D);
7236   }
7237   ExtVectorDecls.clear();
7238 }
7239 
ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl * > & Decls)7240 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7241   for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7242     CXXRecordDecl *D
7243       = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7244     if (D)
7245       Decls.push_back(D);
7246   }
7247   DynamicClasses.clear();
7248 }
7249 
ReadUnusedLocalTypedefNameCandidates(llvm::SmallSetVector<const TypedefNameDecl *,4> & Decls)7250 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7251     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7252   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7253        ++I) {
7254     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7255         GetDecl(UnusedLocalTypedefNameCandidates[I]));
7256     if (D)
7257       Decls.insert(D);
7258   }
7259   UnusedLocalTypedefNameCandidates.clear();
7260 }
7261 
7262 void
ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl * > & Decls)7263 ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7264   for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7265     NamedDecl *D
7266       = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
7267     if (D)
7268       Decls.push_back(D);
7269   }
7270   LocallyScopedExternCDecls.clear();
7271 }
7272 
ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,SourceLocation>> & Sels)7273 void ASTReader::ReadReferencedSelectors(
7274        SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7275   if (ReferencedSelectorsData.empty())
7276     return;
7277 
7278   // If there are @selector references added them to its pool. This is for
7279   // implementation of -Wselector.
7280   unsigned int DataSize = ReferencedSelectorsData.size()-1;
7281   unsigned I = 0;
7282   while (I < DataSize) {
7283     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7284     SourceLocation SelLoc
7285       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7286     Sels.push_back(std::make_pair(Sel, SelLoc));
7287   }
7288   ReferencedSelectorsData.clear();
7289 }
7290 
ReadWeakUndeclaredIdentifiers(SmallVectorImpl<std::pair<IdentifierInfo *,WeakInfo>> & WeakIDs)7291 void ASTReader::ReadWeakUndeclaredIdentifiers(
7292        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7293   if (WeakUndeclaredIdentifiers.empty())
7294     return;
7295 
7296   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7297     IdentifierInfo *WeakId
7298       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7299     IdentifierInfo *AliasId
7300       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7301     SourceLocation Loc
7302       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7303     bool Used = WeakUndeclaredIdentifiers[I++];
7304     WeakInfo WI(AliasId, Loc);
7305     WI.setUsed(Used);
7306     WeakIDs.push_back(std::make_pair(WeakId, WI));
7307   }
7308   WeakUndeclaredIdentifiers.clear();
7309 }
7310 
ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> & VTables)7311 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7312   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7313     ExternalVTableUse VT;
7314     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7315     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7316     VT.DefinitionRequired = VTableUses[Idx++];
7317     VTables.push_back(VT);
7318   }
7319 
7320   VTableUses.clear();
7321 }
7322 
ReadPendingInstantiations(SmallVectorImpl<std::pair<ValueDecl *,SourceLocation>> & Pending)7323 void ASTReader::ReadPendingInstantiations(
7324        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7325   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7326     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7327     SourceLocation Loc
7328       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7329 
7330     Pending.push_back(std::make_pair(D, Loc));
7331   }
7332   PendingInstantiations.clear();
7333 }
7334 
ReadLateParsedTemplates(llvm::DenseMap<const FunctionDecl *,LateParsedTemplate * > & LPTMap)7335 void ASTReader::ReadLateParsedTemplates(
7336     llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7337   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7338        /* In loop */) {
7339     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7340 
7341     LateParsedTemplate *LT = new LateParsedTemplate;
7342     LT->D = GetDecl(LateParsedTemplates[Idx++]);
7343 
7344     ModuleFile *F = getOwningModuleFile(LT->D);
7345     assert(F && "No module");
7346 
7347     unsigned TokN = LateParsedTemplates[Idx++];
7348     LT->Toks.reserve(TokN);
7349     for (unsigned T = 0; T < TokN; ++T)
7350       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7351 
7352     LPTMap[FD] = LT;
7353   }
7354 
7355   LateParsedTemplates.clear();
7356 }
7357 
LoadSelector(Selector Sel)7358 void ASTReader::LoadSelector(Selector Sel) {
7359   // It would be complicated to avoid reading the methods anyway. So don't.
7360   ReadMethodPool(Sel);
7361 }
7362 
SetIdentifierInfo(IdentifierID ID,IdentifierInfo * II)7363 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7364   assert(ID && "Non-zero identifier ID required");
7365   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7366   IdentifiersLoaded[ID - 1] = II;
7367   if (DeserializationListener)
7368     DeserializationListener->IdentifierRead(ID, II);
7369 }
7370 
7371 /// \brief Set the globally-visible declarations associated with the given
7372 /// identifier.
7373 ///
7374 /// If the AST reader is currently in a state where the given declaration IDs
7375 /// cannot safely be resolved, they are queued until it is safe to resolve
7376 /// them.
7377 ///
7378 /// \param II an IdentifierInfo that refers to one or more globally-visible
7379 /// declarations.
7380 ///
7381 /// \param DeclIDs the set of declaration IDs with the name @p II that are
7382 /// visible at global scope.
7383 ///
7384 /// \param Decls if non-null, this vector will be populated with the set of
7385 /// deserialized declarations. These declarations will not be pushed into
7386 /// scope.
7387 void
SetGloballyVisibleDecls(IdentifierInfo * II,const SmallVectorImpl<uint32_t> & DeclIDs,SmallVectorImpl<Decl * > * Decls)7388 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7389                               const SmallVectorImpl<uint32_t> &DeclIDs,
7390                                    SmallVectorImpl<Decl *> *Decls) {
7391   if (NumCurrentElementsDeserializing && !Decls) {
7392     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
7393     return;
7394   }
7395 
7396   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7397     if (!SemaObj) {
7398       // Queue this declaration so that it will be added to the
7399       // translation unit scope and identifier's declaration chain
7400       // once a Sema object is known.
7401       PreloadedDeclIDs.push_back(DeclIDs[I]);
7402       continue;
7403     }
7404 
7405     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7406 
7407     // If we're simply supposed to record the declarations, do so now.
7408     if (Decls) {
7409       Decls->push_back(D);
7410       continue;
7411     }
7412 
7413     // Introduce this declaration into the translation-unit scope
7414     // and add it to the declaration chain for this identifier, so
7415     // that (unqualified) name lookup will find it.
7416     pushExternalDeclIntoScope(D, II);
7417   }
7418 }
7419 
DecodeIdentifierInfo(IdentifierID ID)7420 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
7421   if (ID == 0)
7422     return nullptr;
7423 
7424   if (IdentifiersLoaded.empty()) {
7425     Error("no identifier table in AST file");
7426     return nullptr;
7427   }
7428 
7429   ID -= 1;
7430   if (!IdentifiersLoaded[ID]) {
7431     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7432     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7433     ModuleFile *M = I->second;
7434     unsigned Index = ID - M->BaseIdentifierID;
7435     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7436 
7437     // All of the strings in the AST file are preceded by a 16-bit length.
7438     // Extract that 16-bit length to avoid having to execute strlen().
7439     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7440     //  unsigned integers.  This is important to avoid integer overflow when
7441     //  we cast them to 'unsigned'.
7442     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7443     unsigned StrLen = (((unsigned) StrLenPtr[0])
7444                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
7445     IdentifiersLoaded[ID]
7446       = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
7447     if (DeserializationListener)
7448       DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7449   }
7450 
7451   return IdentifiersLoaded[ID];
7452 }
7453 
getLocalIdentifier(ModuleFile & M,unsigned LocalID)7454 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7455   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
7456 }
7457 
getGlobalIdentifierID(ModuleFile & M,unsigned LocalID)7458 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7459   if (LocalID < NUM_PREDEF_IDENT_IDS)
7460     return LocalID;
7461 
7462   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7463     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7464   assert(I != M.IdentifierRemap.end()
7465          && "Invalid index into identifier index remap");
7466 
7467   return LocalID + I->second;
7468 }
7469 
getMacro(MacroID ID)7470 MacroInfo *ASTReader::getMacro(MacroID ID) {
7471   if (ID == 0)
7472     return nullptr;
7473 
7474   if (MacrosLoaded.empty()) {
7475     Error("no macro table in AST file");
7476     return nullptr;
7477   }
7478 
7479   ID -= NUM_PREDEF_MACRO_IDS;
7480   if (!MacrosLoaded[ID]) {
7481     GlobalMacroMapType::iterator I
7482       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7483     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7484     ModuleFile *M = I->second;
7485     unsigned Index = ID - M->BaseMacroID;
7486     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7487 
7488     if (DeserializationListener)
7489       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7490                                          MacrosLoaded[ID]);
7491   }
7492 
7493   return MacrosLoaded[ID];
7494 }
7495 
getGlobalMacroID(ModuleFile & M,unsigned LocalID)7496 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7497   if (LocalID < NUM_PREDEF_MACRO_IDS)
7498     return LocalID;
7499 
7500   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7501     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7502   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7503 
7504   return LocalID + I->second;
7505 }
7506 
7507 serialization::SubmoduleID
getGlobalSubmoduleID(ModuleFile & M,unsigned LocalID)7508 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7509   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7510     return LocalID;
7511 
7512   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7513     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7514   assert(I != M.SubmoduleRemap.end()
7515          && "Invalid index into submodule index remap");
7516 
7517   return LocalID + I->second;
7518 }
7519 
getSubmodule(SubmoduleID GlobalID)7520 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7521   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7522     assert(GlobalID == 0 && "Unhandled global submodule ID");
7523     return nullptr;
7524   }
7525 
7526   if (GlobalID > SubmodulesLoaded.size()) {
7527     Error("submodule ID out of range in AST file");
7528     return nullptr;
7529   }
7530 
7531   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7532 }
7533 
getModule(unsigned ID)7534 Module *ASTReader::getModule(unsigned ID) {
7535   return getSubmodule(ID);
7536 }
7537 
getLocalSelector(ModuleFile & M,unsigned LocalID)7538 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7539   return DecodeSelector(getGlobalSelectorID(M, LocalID));
7540 }
7541 
DecodeSelector(serialization::SelectorID ID)7542 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7543   if (ID == 0)
7544     return Selector();
7545 
7546   if (ID > SelectorsLoaded.size()) {
7547     Error("selector ID out of range in AST file");
7548     return Selector();
7549   }
7550 
7551   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
7552     // Load this selector from the selector table.
7553     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7554     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7555     ModuleFile &M = *I->second;
7556     ASTSelectorLookupTrait Trait(*this, M);
7557     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7558     SelectorsLoaded[ID - 1] =
7559       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7560     if (DeserializationListener)
7561       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7562   }
7563 
7564   return SelectorsLoaded[ID - 1];
7565 }
7566 
GetExternalSelector(serialization::SelectorID ID)7567 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7568   return DecodeSelector(ID);
7569 }
7570 
GetNumExternalSelectors()7571 uint32_t ASTReader::GetNumExternalSelectors() {
7572   // ID 0 (the null selector) is considered an external selector.
7573   return getTotalNumSelectors() + 1;
7574 }
7575 
7576 serialization::SelectorID
getGlobalSelectorID(ModuleFile & M,unsigned LocalID) const7577 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7578   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7579     return LocalID;
7580 
7581   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7582     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7583   assert(I != M.SelectorRemap.end()
7584          && "Invalid index into selector index remap");
7585 
7586   return LocalID + I->second;
7587 }
7588 
7589 DeclarationName
ReadDeclarationName(ModuleFile & F,const RecordData & Record,unsigned & Idx)7590 ASTReader::ReadDeclarationName(ModuleFile &F,
7591                                const RecordData &Record, unsigned &Idx) {
7592   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7593   switch (Kind) {
7594   case DeclarationName::Identifier:
7595     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
7596 
7597   case DeclarationName::ObjCZeroArgSelector:
7598   case DeclarationName::ObjCOneArgSelector:
7599   case DeclarationName::ObjCMultiArgSelector:
7600     return DeclarationName(ReadSelector(F, Record, Idx));
7601 
7602   case DeclarationName::CXXConstructorName:
7603     return Context.DeclarationNames.getCXXConstructorName(
7604                           Context.getCanonicalType(readType(F, Record, Idx)));
7605 
7606   case DeclarationName::CXXDestructorName:
7607     return Context.DeclarationNames.getCXXDestructorName(
7608                           Context.getCanonicalType(readType(F, Record, Idx)));
7609 
7610   case DeclarationName::CXXConversionFunctionName:
7611     return Context.DeclarationNames.getCXXConversionFunctionName(
7612                           Context.getCanonicalType(readType(F, Record, Idx)));
7613 
7614   case DeclarationName::CXXOperatorName:
7615     return Context.DeclarationNames.getCXXOperatorName(
7616                                        (OverloadedOperatorKind)Record[Idx++]);
7617 
7618   case DeclarationName::CXXLiteralOperatorName:
7619     return Context.DeclarationNames.getCXXLiteralOperatorName(
7620                                        GetIdentifierInfo(F, Record, Idx));
7621 
7622   case DeclarationName::CXXUsingDirective:
7623     return DeclarationName::getUsingDirectiveName();
7624   }
7625 
7626   llvm_unreachable("Invalid NameKind!");
7627 }
7628 
ReadDeclarationNameLoc(ModuleFile & F,DeclarationNameLoc & DNLoc,DeclarationName Name,const RecordData & Record,unsigned & Idx)7629 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7630                                        DeclarationNameLoc &DNLoc,
7631                                        DeclarationName Name,
7632                                       const RecordData &Record, unsigned &Idx) {
7633   switch (Name.getNameKind()) {
7634   case DeclarationName::CXXConstructorName:
7635   case DeclarationName::CXXDestructorName:
7636   case DeclarationName::CXXConversionFunctionName:
7637     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7638     break;
7639 
7640   case DeclarationName::CXXOperatorName:
7641     DNLoc.CXXOperatorName.BeginOpNameLoc
7642         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7643     DNLoc.CXXOperatorName.EndOpNameLoc
7644         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7645     break;
7646 
7647   case DeclarationName::CXXLiteralOperatorName:
7648     DNLoc.CXXLiteralOperatorName.OpNameLoc
7649         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7650     break;
7651 
7652   case DeclarationName::Identifier:
7653   case DeclarationName::ObjCZeroArgSelector:
7654   case DeclarationName::ObjCOneArgSelector:
7655   case DeclarationName::ObjCMultiArgSelector:
7656   case DeclarationName::CXXUsingDirective:
7657     break;
7658   }
7659 }
7660 
ReadDeclarationNameInfo(ModuleFile & F,DeclarationNameInfo & NameInfo,const RecordData & Record,unsigned & Idx)7661 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7662                                         DeclarationNameInfo &NameInfo,
7663                                       const RecordData &Record, unsigned &Idx) {
7664   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7665   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7666   DeclarationNameLoc DNLoc;
7667   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7668   NameInfo.setInfo(DNLoc);
7669 }
7670 
ReadQualifierInfo(ModuleFile & F,QualifierInfo & Info,const RecordData & Record,unsigned & Idx)7671 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7672                                   const RecordData &Record, unsigned &Idx) {
7673   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7674   unsigned NumTPLists = Record[Idx++];
7675   Info.NumTemplParamLists = NumTPLists;
7676   if (NumTPLists) {
7677     Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7678     for (unsigned i=0; i != NumTPLists; ++i)
7679       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7680   }
7681 }
7682 
7683 TemplateName
ReadTemplateName(ModuleFile & F,const RecordData & Record,unsigned & Idx)7684 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7685                             unsigned &Idx) {
7686   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7687   switch (Kind) {
7688   case TemplateName::Template:
7689       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7690 
7691   case TemplateName::OverloadedTemplate: {
7692     unsigned size = Record[Idx++];
7693     UnresolvedSet<8> Decls;
7694     while (size--)
7695       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7696 
7697     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7698   }
7699 
7700   case TemplateName::QualifiedTemplate: {
7701     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7702     bool hasTemplKeyword = Record[Idx++];
7703     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7704     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7705   }
7706 
7707   case TemplateName::DependentTemplate: {
7708     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7709     if (Record[Idx++])  // isIdentifier
7710       return Context.getDependentTemplateName(NNS,
7711                                                GetIdentifierInfo(F, Record,
7712                                                                  Idx));
7713     return Context.getDependentTemplateName(NNS,
7714                                          (OverloadedOperatorKind)Record[Idx++]);
7715   }
7716 
7717   case TemplateName::SubstTemplateTemplateParm: {
7718     TemplateTemplateParmDecl *param
7719       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7720     if (!param) return TemplateName();
7721     TemplateName replacement = ReadTemplateName(F, Record, Idx);
7722     return Context.getSubstTemplateTemplateParm(param, replacement);
7723   }
7724 
7725   case TemplateName::SubstTemplateTemplateParmPack: {
7726     TemplateTemplateParmDecl *Param
7727       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7728     if (!Param)
7729       return TemplateName();
7730 
7731     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7732     if (ArgPack.getKind() != TemplateArgument::Pack)
7733       return TemplateName();
7734 
7735     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7736   }
7737   }
7738 
7739   llvm_unreachable("Unhandled template name kind!");
7740 }
7741 
7742 TemplateArgument
ReadTemplateArgument(ModuleFile & F,const RecordData & Record,unsigned & Idx)7743 ASTReader::ReadTemplateArgument(ModuleFile &F,
7744                                 const RecordData &Record, unsigned &Idx) {
7745   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7746   switch (Kind) {
7747   case TemplateArgument::Null:
7748     return TemplateArgument();
7749   case TemplateArgument::Type:
7750     return TemplateArgument(readType(F, Record, Idx));
7751   case TemplateArgument::Declaration: {
7752     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7753     return TemplateArgument(D, readType(F, Record, Idx));
7754   }
7755   case TemplateArgument::NullPtr:
7756     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7757   case TemplateArgument::Integral: {
7758     llvm::APSInt Value = ReadAPSInt(Record, Idx);
7759     QualType T = readType(F, Record, Idx);
7760     return TemplateArgument(Context, Value, T);
7761   }
7762   case TemplateArgument::Template:
7763     return TemplateArgument(ReadTemplateName(F, Record, Idx));
7764   case TemplateArgument::TemplateExpansion: {
7765     TemplateName Name = ReadTemplateName(F, Record, Idx);
7766     Optional<unsigned> NumTemplateExpansions;
7767     if (unsigned NumExpansions = Record[Idx++])
7768       NumTemplateExpansions = NumExpansions - 1;
7769     return TemplateArgument(Name, NumTemplateExpansions);
7770   }
7771   case TemplateArgument::Expression:
7772     return TemplateArgument(ReadExpr(F));
7773   case TemplateArgument::Pack: {
7774     unsigned NumArgs = Record[Idx++];
7775     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7776     for (unsigned I = 0; I != NumArgs; ++I)
7777       Args[I] = ReadTemplateArgument(F, Record, Idx);
7778     return TemplateArgument(Args, NumArgs);
7779   }
7780   }
7781 
7782   llvm_unreachable("Unhandled template argument kind!");
7783 }
7784 
7785 TemplateParameterList *
ReadTemplateParameterList(ModuleFile & F,const RecordData & Record,unsigned & Idx)7786 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7787                                      const RecordData &Record, unsigned &Idx) {
7788   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7789   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7790   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7791 
7792   unsigned NumParams = Record[Idx++];
7793   SmallVector<NamedDecl *, 16> Params;
7794   Params.reserve(NumParams);
7795   while (NumParams--)
7796     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7797 
7798   TemplateParameterList* TemplateParams =
7799     TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7800                                   Params.data(), Params.size(), RAngleLoc);
7801   return TemplateParams;
7802 }
7803 
7804 void
7805 ASTReader::
ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> & TemplArgs,ModuleFile & F,const RecordData & Record,unsigned & Idx)7806 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7807                          ModuleFile &F, const RecordData &Record,
7808                          unsigned &Idx) {
7809   unsigned NumTemplateArgs = Record[Idx++];
7810   TemplArgs.reserve(NumTemplateArgs);
7811   while (NumTemplateArgs--)
7812     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7813 }
7814 
7815 /// \brief Read a UnresolvedSet structure.
ReadUnresolvedSet(ModuleFile & F,LazyASTUnresolvedSet & Set,const RecordData & Record,unsigned & Idx)7816 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7817                                   const RecordData &Record, unsigned &Idx) {
7818   unsigned NumDecls = Record[Idx++];
7819   Set.reserve(Context, NumDecls);
7820   while (NumDecls--) {
7821     DeclID ID = ReadDeclID(F, Record, Idx);
7822     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7823     Set.addLazyDecl(Context, ID, AS);
7824   }
7825 }
7826 
7827 CXXBaseSpecifier
ReadCXXBaseSpecifier(ModuleFile & F,const RecordData & Record,unsigned & Idx)7828 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7829                                 const RecordData &Record, unsigned &Idx) {
7830   bool isVirtual = static_cast<bool>(Record[Idx++]);
7831   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7832   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7833   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7834   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7835   SourceRange Range = ReadSourceRange(F, Record, Idx);
7836   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7837   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7838                           EllipsisLoc);
7839   Result.setInheritConstructors(inheritConstructors);
7840   return Result;
7841 }
7842 
7843 std::pair<CXXCtorInitializer **, unsigned>
ReadCXXCtorInitializers(ModuleFile & F,const RecordData & Record,unsigned & Idx)7844 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7845                                    unsigned &Idx) {
7846   CXXCtorInitializer **CtorInitializers = nullptr;
7847   unsigned NumInitializers = Record[Idx++];
7848   if (NumInitializers) {
7849     CtorInitializers
7850         = new (Context) CXXCtorInitializer*[NumInitializers];
7851     for (unsigned i=0; i != NumInitializers; ++i) {
7852       TypeSourceInfo *TInfo = nullptr;
7853       bool IsBaseVirtual = false;
7854       FieldDecl *Member = nullptr;
7855       IndirectFieldDecl *IndirectMember = nullptr;
7856 
7857       CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7858       switch (Type) {
7859       case CTOR_INITIALIZER_BASE:
7860         TInfo = GetTypeSourceInfo(F, Record, Idx);
7861         IsBaseVirtual = Record[Idx++];
7862         break;
7863 
7864       case CTOR_INITIALIZER_DELEGATING:
7865         TInfo = GetTypeSourceInfo(F, Record, Idx);
7866         break;
7867 
7868        case CTOR_INITIALIZER_MEMBER:
7869         Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7870         break;
7871 
7872        case CTOR_INITIALIZER_INDIRECT_MEMBER:
7873         IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7874         break;
7875       }
7876 
7877       SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7878       Expr *Init = ReadExpr(F);
7879       SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7880       SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7881       bool IsWritten = Record[Idx++];
7882       unsigned SourceOrderOrNumArrayIndices;
7883       SmallVector<VarDecl *, 8> Indices;
7884       if (IsWritten) {
7885         SourceOrderOrNumArrayIndices = Record[Idx++];
7886       } else {
7887         SourceOrderOrNumArrayIndices = Record[Idx++];
7888         Indices.reserve(SourceOrderOrNumArrayIndices);
7889         for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7890           Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7891       }
7892 
7893       CXXCtorInitializer *BOMInit;
7894       if (Type == CTOR_INITIALIZER_BASE) {
7895         BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7896                                              LParenLoc, Init, RParenLoc,
7897                                              MemberOrEllipsisLoc);
7898       } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7899         BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7900                                                    Init, RParenLoc);
7901       } else if (IsWritten) {
7902         if (Member)
7903           BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7904                                                LParenLoc, Init, RParenLoc);
7905         else
7906           BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7907                                                MemberOrEllipsisLoc, LParenLoc,
7908                                                Init, RParenLoc);
7909       } else {
7910         if (IndirectMember) {
7911           assert(Indices.empty() && "Indirect field improperly initialized");
7912           BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7913                                                      MemberOrEllipsisLoc, LParenLoc,
7914                                                      Init, RParenLoc);
7915         } else {
7916           BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7917                                                LParenLoc, Init, RParenLoc,
7918                                                Indices.data(), Indices.size());
7919         }
7920       }
7921 
7922       if (IsWritten)
7923         BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7924       CtorInitializers[i] = BOMInit;
7925     }
7926   }
7927 
7928   return std::make_pair(CtorInitializers, NumInitializers);
7929 }
7930 
7931 NestedNameSpecifier *
ReadNestedNameSpecifier(ModuleFile & F,const RecordData & Record,unsigned & Idx)7932 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7933                                    const RecordData &Record, unsigned &Idx) {
7934   unsigned N = Record[Idx++];
7935   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
7936   for (unsigned I = 0; I != N; ++I) {
7937     NestedNameSpecifier::SpecifierKind Kind
7938       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7939     switch (Kind) {
7940     case NestedNameSpecifier::Identifier: {
7941       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7942       NNS = NestedNameSpecifier::Create(Context, Prev, II);
7943       break;
7944     }
7945 
7946     case NestedNameSpecifier::Namespace: {
7947       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7948       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7949       break;
7950     }
7951 
7952     case NestedNameSpecifier::NamespaceAlias: {
7953       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7954       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7955       break;
7956     }
7957 
7958     case NestedNameSpecifier::TypeSpec:
7959     case NestedNameSpecifier::TypeSpecWithTemplate: {
7960       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7961       if (!T)
7962         return nullptr;
7963 
7964       bool Template = Record[Idx++];
7965       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7966       break;
7967     }
7968 
7969     case NestedNameSpecifier::Global: {
7970       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7971       // No associated value, and there can't be a prefix.
7972       break;
7973     }
7974 
7975     case NestedNameSpecifier::Super: {
7976       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7977       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
7978       break;
7979     }
7980     }
7981     Prev = NNS;
7982   }
7983   return NNS;
7984 }
7985 
7986 NestedNameSpecifierLoc
ReadNestedNameSpecifierLoc(ModuleFile & F,const RecordData & Record,unsigned & Idx)7987 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7988                                       unsigned &Idx) {
7989   unsigned N = Record[Idx++];
7990   NestedNameSpecifierLocBuilder Builder;
7991   for (unsigned I = 0; I != N; ++I) {
7992     NestedNameSpecifier::SpecifierKind Kind
7993       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7994     switch (Kind) {
7995     case NestedNameSpecifier::Identifier: {
7996       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7997       SourceRange Range = ReadSourceRange(F, Record, Idx);
7998       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7999       break;
8000     }
8001 
8002     case NestedNameSpecifier::Namespace: {
8003       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8004       SourceRange Range = ReadSourceRange(F, Record, Idx);
8005       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8006       break;
8007     }
8008 
8009     case NestedNameSpecifier::NamespaceAlias: {
8010       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8011       SourceRange Range = ReadSourceRange(F, Record, Idx);
8012       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8013       break;
8014     }
8015 
8016     case NestedNameSpecifier::TypeSpec:
8017     case NestedNameSpecifier::TypeSpecWithTemplate: {
8018       bool Template = Record[Idx++];
8019       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8020       if (!T)
8021         return NestedNameSpecifierLoc();
8022       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8023 
8024       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8025       Builder.Extend(Context,
8026                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8027                      T->getTypeLoc(), ColonColonLoc);
8028       break;
8029     }
8030 
8031     case NestedNameSpecifier::Global: {
8032       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8033       Builder.MakeGlobal(Context, ColonColonLoc);
8034       break;
8035     }
8036 
8037     case NestedNameSpecifier::Super: {
8038       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8039       SourceRange Range = ReadSourceRange(F, Record, Idx);
8040       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8041       break;
8042     }
8043     }
8044   }
8045 
8046   return Builder.getWithLocInContext(Context);
8047 }
8048 
8049 SourceRange
ReadSourceRange(ModuleFile & F,const RecordData & Record,unsigned & Idx)8050 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8051                            unsigned &Idx) {
8052   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8053   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8054   return SourceRange(beg, end);
8055 }
8056 
8057 /// \brief Read an integral value
ReadAPInt(const RecordData & Record,unsigned & Idx)8058 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8059   unsigned BitWidth = Record[Idx++];
8060   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8061   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8062   Idx += NumWords;
8063   return Result;
8064 }
8065 
8066 /// \brief Read a signed integral value
ReadAPSInt(const RecordData & Record,unsigned & Idx)8067 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8068   bool isUnsigned = Record[Idx++];
8069   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8070 }
8071 
8072 /// \brief Read a floating-point value
ReadAPFloat(const RecordData & Record,const llvm::fltSemantics & Sem,unsigned & Idx)8073 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8074                                      const llvm::fltSemantics &Sem,
8075                                      unsigned &Idx) {
8076   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
8077 }
8078 
8079 // \brief Read a string
ReadString(const RecordData & Record,unsigned & Idx)8080 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8081   unsigned Len = Record[Idx++];
8082   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8083   Idx += Len;
8084   return Result;
8085 }
8086 
ReadPath(ModuleFile & F,const RecordData & Record,unsigned & Idx)8087 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8088                                 unsigned &Idx) {
8089   std::string Filename = ReadString(Record, Idx);
8090   ResolveImportedPath(F, Filename);
8091   return Filename;
8092 }
8093 
ReadVersionTuple(const RecordData & Record,unsigned & Idx)8094 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8095                                          unsigned &Idx) {
8096   unsigned Major = Record[Idx++];
8097   unsigned Minor = Record[Idx++];
8098   unsigned Subminor = Record[Idx++];
8099   if (Minor == 0)
8100     return VersionTuple(Major);
8101   if (Subminor == 0)
8102     return VersionTuple(Major, Minor - 1);
8103   return VersionTuple(Major, Minor - 1, Subminor - 1);
8104 }
8105 
ReadCXXTemporary(ModuleFile & F,const RecordData & Record,unsigned & Idx)8106 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8107                                           const RecordData &Record,
8108                                           unsigned &Idx) {
8109   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8110   return CXXTemporary::Create(Context, Decl);
8111 }
8112 
Diag(unsigned DiagID)8113 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
8114   return Diag(CurrentImportLoc, DiagID);
8115 }
8116 
Diag(SourceLocation Loc,unsigned DiagID)8117 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8118   return Diags.Report(Loc, DiagID);
8119 }
8120 
8121 /// \brief Retrieve the identifier table associated with the
8122 /// preprocessor.
getIdentifierTable()8123 IdentifierTable &ASTReader::getIdentifierTable() {
8124   return PP.getIdentifierTable();
8125 }
8126 
8127 /// \brief Record that the given ID maps to the given switch-case
8128 /// statement.
RecordSwitchCaseID(SwitchCase * SC,unsigned ID)8129 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8130   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8131          "Already have a SwitchCase with this ID");
8132   (*CurrSwitchCaseStmts)[ID] = SC;
8133 }
8134 
8135 /// \brief Retrieve the switch-case statement with the given ID.
getSwitchCaseWithID(unsigned ID)8136 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8137   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8138   return (*CurrSwitchCaseStmts)[ID];
8139 }
8140 
ClearSwitchCaseIDs()8141 void ASTReader::ClearSwitchCaseIDs() {
8142   CurrSwitchCaseStmts->clear();
8143 }
8144 
ReadComments()8145 void ASTReader::ReadComments() {
8146   std::vector<RawComment *> Comments;
8147   for (SmallVectorImpl<std::pair<BitstreamCursor,
8148                                  serialization::ModuleFile *> >::iterator
8149        I = CommentsCursors.begin(),
8150        E = CommentsCursors.end();
8151        I != E; ++I) {
8152     Comments.clear();
8153     BitstreamCursor &Cursor = I->first;
8154     serialization::ModuleFile &F = *I->second;
8155     SavedStreamPosition SavedPosition(Cursor);
8156 
8157     RecordData Record;
8158     while (true) {
8159       llvm::BitstreamEntry Entry =
8160         Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
8161 
8162       switch (Entry.Kind) {
8163       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8164       case llvm::BitstreamEntry::Error:
8165         Error("malformed block record in AST file");
8166         return;
8167       case llvm::BitstreamEntry::EndBlock:
8168         goto NextCursor;
8169       case llvm::BitstreamEntry::Record:
8170         // The interesting case.
8171         break;
8172       }
8173 
8174       // Read a record.
8175       Record.clear();
8176       switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
8177       case COMMENTS_RAW_COMMENT: {
8178         unsigned Idx = 0;
8179         SourceRange SR = ReadSourceRange(F, Record, Idx);
8180         RawComment::CommentKind Kind =
8181             (RawComment::CommentKind) Record[Idx++];
8182         bool IsTrailingComment = Record[Idx++];
8183         bool IsAlmostTrailingComment = Record[Idx++];
8184         Comments.push_back(new (Context) RawComment(
8185             SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8186             Context.getLangOpts().CommentOpts.ParseAllComments));
8187         break;
8188       }
8189       }
8190     }
8191   NextCursor:
8192     Context.Comments.addDeserializedComments(Comments);
8193   }
8194 }
8195 
getInputFiles(ModuleFile & F,SmallVectorImpl<serialization::InputFile> & Files)8196 void ASTReader::getInputFiles(ModuleFile &F,
8197                              SmallVectorImpl<serialization::InputFile> &Files) {
8198   for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8199     unsigned ID = I+1;
8200     Files.push_back(getInputFile(F, ID));
8201   }
8202 }
8203 
getOwningModuleNameForDiagnostic(const Decl * D)8204 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8205   // If we know the owning module, use it.
8206   if (Module *M = D->getOwningModule())
8207     return M->getFullModuleName();
8208 
8209   // Otherwise, use the name of the top-level module the decl is within.
8210   if (ModuleFile *M = getOwningModuleFile(D))
8211     return M->ModuleName;
8212 
8213   // Not from a module.
8214   return "";
8215 }
8216 
finishPendingActions()8217 void ASTReader::finishPendingActions() {
8218   while (!PendingIdentifierInfos.empty() ||
8219          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
8220          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
8221          !PendingUpdateRecords.empty()) {
8222     // If any identifiers with corresponding top-level declarations have
8223     // been loaded, load those declarations now.
8224     typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8225       TopLevelDeclsMap;
8226     TopLevelDeclsMap TopLevelDecls;
8227 
8228     while (!PendingIdentifierInfos.empty()) {
8229       IdentifierInfo *II = PendingIdentifierInfos.back().first;
8230       SmallVector<uint32_t, 4> DeclIDs =
8231           std::move(PendingIdentifierInfos.back().second);
8232       PendingIdentifierInfos.pop_back();
8233 
8234       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
8235     }
8236 
8237     // For each decl chain that we wanted to complete while deserializing, mark
8238     // it as "still needs to be completed".
8239     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8240       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8241     }
8242     PendingIncompleteDeclChains.clear();
8243 
8244     // Load pending declaration chains.
8245     for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8246       loadPendingDeclChain(PendingDeclChains[I]);
8247       PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8248     }
8249     PendingDeclChains.clear();
8250 
8251     // Make the most recent of the top-level declarations visible.
8252     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8253            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
8254       IdentifierInfo *II = TLD->first;
8255       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
8256         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
8257       }
8258     }
8259 
8260     // Load any pending macro definitions.
8261     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
8262       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8263       SmallVector<PendingMacroInfo, 2> GlobalIDs;
8264       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8265       // Initialize the macro history from chained-PCHs ahead of module imports.
8266       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8267            ++IDIdx) {
8268         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8269         if (Info.M->Kind != MK_ImplicitModule &&
8270             Info.M->Kind != MK_ExplicitModule)
8271           resolvePendingMacro(II, Info);
8272       }
8273       // Handle module imports.
8274       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8275            ++IDIdx) {
8276         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8277         if (Info.M->Kind == MK_ImplicitModule ||
8278             Info.M->Kind == MK_ExplicitModule)
8279           resolvePendingMacro(II, Info);
8280       }
8281     }
8282     PendingMacroIDs.clear();
8283 
8284     // Wire up the DeclContexts for Decls that we delayed setting until
8285     // recursive loading is completed.
8286     while (!PendingDeclContextInfos.empty()) {
8287       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8288       PendingDeclContextInfos.pop_front();
8289       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8290       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8291       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8292     }
8293 
8294     // Perform any pending declaration updates.
8295     while (!PendingUpdateRecords.empty()) {
8296       auto Update = PendingUpdateRecords.pop_back_val();
8297       ReadingKindTracker ReadingKind(Read_Decl, *this);
8298       loadDeclUpdateRecords(Update.first, Update.second);
8299     }
8300   }
8301 
8302   // If we deserialized any C++ or Objective-C class definitions, any
8303   // Objective-C protocol definitions, or any redeclarable templates, make sure
8304   // that all redeclarations point to the definitions. Note that this can only
8305   // happen now, after the redeclaration chains have been fully wired.
8306   for (Decl *D : PendingDefinitions) {
8307     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
8308       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
8309         // Make sure that the TagType points at the definition.
8310         const_cast<TagType*>(TagT)->decl = TD;
8311       }
8312 
8313       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
8314         for (auto R : RD->redecls()) {
8315           assert((R == D) == R->isThisDeclarationADefinition() &&
8316                  "declaration thinks it's the definition but it isn't");
8317           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
8318         }
8319       }
8320 
8321       continue;
8322     }
8323 
8324     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
8325       // Make sure that the ObjCInterfaceType points at the definition.
8326       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8327         ->Decl = ID;
8328 
8329       for (auto R : ID->redecls())
8330         R->Data = ID->Data;
8331 
8332       continue;
8333     }
8334 
8335     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
8336       for (auto R : PD->redecls())
8337         R->Data = PD->Data;
8338 
8339       continue;
8340     }
8341 
8342     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
8343     for (auto R : RTD->redecls())
8344       R->Common = RTD->Common;
8345   }
8346   PendingDefinitions.clear();
8347 
8348   // Load the bodies of any functions or methods we've encountered. We do
8349   // this now (delayed) so that we can be sure that the declaration chains
8350   // have been fully wired up.
8351   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8352                                PBEnd = PendingBodies.end();
8353        PB != PBEnd; ++PB) {
8354     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8355       // FIXME: Check for =delete/=default?
8356       // FIXME: Complain about ODR violations here?
8357       if (!getContext().getLangOpts().Modules || !FD->hasBody())
8358         FD->setLazyBody(PB->second);
8359       continue;
8360     }
8361 
8362     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8363     if (!getContext().getLangOpts().Modules || !MD->hasBody())
8364       MD->setLazyBody(PB->second);
8365   }
8366   PendingBodies.clear();
8367 }
8368 
diagnoseOdrViolations()8369 void ASTReader::diagnoseOdrViolations() {
8370   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8371     return;
8372 
8373   // Trigger the import of the full definition of each class that had any
8374   // odr-merging problems, so we can produce better diagnostics for them.
8375   // These updates may in turn find and diagnose some ODR failures, so take
8376   // ownership of the set first.
8377   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8378   PendingOdrMergeFailures.clear();
8379   for (auto &Merge : OdrMergeFailures) {
8380     Merge.first->buildLookup();
8381     Merge.first->decls_begin();
8382     Merge.first->bases_begin();
8383     Merge.first->vbases_begin();
8384     for (auto *RD : Merge.second) {
8385       RD->decls_begin();
8386       RD->bases_begin();
8387       RD->vbases_begin();
8388     }
8389   }
8390 
8391   // For each declaration from a merged context, check that the canonical
8392   // definition of that context also contains a declaration of the same
8393   // entity.
8394   //
8395   // Caution: this loop does things that might invalidate iterators into
8396   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8397   while (!PendingOdrMergeChecks.empty()) {
8398     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8399 
8400     // FIXME: Skip over implicit declarations for now. This matters for things
8401     // like implicitly-declared special member functions. This isn't entirely
8402     // correct; we can end up with multiple unmerged declarations of the same
8403     // implicit entity.
8404     if (D->isImplicit())
8405       continue;
8406 
8407     DeclContext *CanonDef = D->getDeclContext();
8408 
8409     bool Found = false;
8410     const Decl *DCanon = D->getCanonicalDecl();
8411 
8412     for (auto RI : D->redecls()) {
8413       if (RI->getLexicalDeclContext() == CanonDef) {
8414         Found = true;
8415         break;
8416       }
8417     }
8418     if (Found)
8419       continue;
8420 
8421     llvm::SmallVector<const NamedDecl*, 4> Candidates;
8422     DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8423     for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8424          !Found && I != E; ++I) {
8425       for (auto RI : (*I)->redecls()) {
8426         if (RI->getLexicalDeclContext() == CanonDef) {
8427           // This declaration is present in the canonical definition. If it's
8428           // in the same redecl chain, it's the one we're looking for.
8429           if (RI->getCanonicalDecl() == DCanon)
8430             Found = true;
8431           else
8432             Candidates.push_back(cast<NamedDecl>(RI));
8433           break;
8434         }
8435       }
8436     }
8437 
8438     if (!Found) {
8439       // The AST doesn't like TagDecls becoming invalid after they've been
8440       // completed. We only really need to mark FieldDecls as invalid here.
8441       if (!isa<TagDecl>(D))
8442         D->setInvalidDecl();
8443 
8444       std::string CanonDefModule =
8445           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8446       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8447         << D << getOwningModuleNameForDiagnostic(D)
8448         << CanonDef << CanonDefModule.empty() << CanonDefModule;
8449 
8450       if (Candidates.empty())
8451         Diag(cast<Decl>(CanonDef)->getLocation(),
8452              diag::note_module_odr_violation_no_possible_decls) << D;
8453       else {
8454         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8455           Diag(Candidates[I]->getLocation(),
8456                diag::note_module_odr_violation_possible_decl)
8457             << Candidates[I];
8458       }
8459 
8460       DiagnosedOdrMergeFailures.insert(CanonDef);
8461     }
8462   }
8463 
8464   // Issue any pending ODR-failure diagnostics.
8465   for (auto &Merge : OdrMergeFailures) {
8466     // If we've already pointed out a specific problem with this class, don't
8467     // bother issuing a general "something's different" diagnostic.
8468     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
8469       continue;
8470 
8471     bool Diagnosed = false;
8472     for (auto *RD : Merge.second) {
8473       // Multiple different declarations got merged together; tell the user
8474       // where they came from.
8475       if (Merge.first != RD) {
8476         // FIXME: Walk the definition, figure out what's different,
8477         // and diagnose that.
8478         if (!Diagnosed) {
8479           std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8480           Diag(Merge.first->getLocation(),
8481                diag::err_module_odr_violation_different_definitions)
8482             << Merge.first << Module.empty() << Module;
8483           Diagnosed = true;
8484         }
8485 
8486         Diag(RD->getLocation(),
8487              diag::note_module_odr_violation_different_definitions)
8488           << getOwningModuleNameForDiagnostic(RD);
8489       }
8490     }
8491 
8492     if (!Diagnosed) {
8493       // All definitions are updates to the same declaration. This happens if a
8494       // module instantiates the declaration of a class template specialization
8495       // and two or more other modules instantiate its definition.
8496       //
8497       // FIXME: Indicate which modules had instantiations of this definition.
8498       // FIXME: How can this even happen?
8499       Diag(Merge.first->getLocation(),
8500            diag::err_module_odr_violation_different_instantiations)
8501         << Merge.first;
8502     }
8503   }
8504 }
8505 
FinishedDeserializing()8506 void ASTReader::FinishedDeserializing() {
8507   assert(NumCurrentElementsDeserializing &&
8508          "FinishedDeserializing not paired with StartedDeserializing");
8509   if (NumCurrentElementsDeserializing == 1) {
8510     // We decrease NumCurrentElementsDeserializing only after pending actions
8511     // are finished, to avoid recursively re-calling finishPendingActions().
8512     finishPendingActions();
8513   }
8514   --NumCurrentElementsDeserializing;
8515 
8516   if (NumCurrentElementsDeserializing == 0) {
8517     diagnoseOdrViolations();
8518 
8519     // We are not in recursive loading, so it's safe to pass the "interesting"
8520     // decls to the consumer.
8521     if (Consumer)
8522       PassInterestingDeclsToConsumer();
8523   }
8524 }
8525 
pushExternalDeclIntoScope(NamedDecl * D,DeclarationName Name)8526 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
8527   D = D->getMostRecentDecl();
8528 
8529   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8530     SemaObj->TUScope->AddDecl(D);
8531   } else if (SemaObj->TUScope) {
8532     // Adding the decl to IdResolver may have failed because it was already in
8533     // (even though it was not added in scope). If it is already in, make sure
8534     // it gets in the scope as well.
8535     if (std::find(SemaObj->IdResolver.begin(Name),
8536                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8537       SemaObj->TUScope->AddDecl(D);
8538   }
8539 }
8540 
ASTReader(Preprocessor & PP,ASTContext & Context,StringRef isysroot,bool DisableValidation,bool AllowASTWithCompilerErrors,bool AllowConfigurationMismatch,bool ValidateSystemInputs,bool UseGlobalIndex)8541 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8542                      bool DisableValidation, bool AllowASTWithCompilerErrors,
8543                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8544                      bool UseGlobalIndex)
8545     : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8546       OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8547       FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8548       SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8549       ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8550       DisableValidation(DisableValidation),
8551       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8552       AllowConfigurationMismatch(AllowConfigurationMismatch),
8553       ValidateSystemInputs(ValidateSystemInputs),
8554       UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8555       CurrSwitchCaseStmts(&SwitchCaseStmts),
8556       NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8557       TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8558       NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8559       NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8560       NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8561       NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8562       NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8563       NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8564       TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8565       PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8566       ReadingKind(Read_None) {
8567   SourceMgr.setExternalSLocEntrySource(this);
8568 }
8569 
~ASTReader()8570 ASTReader::~ASTReader() {
8571   if (OwnsDeserializationListener)
8572     delete DeserializationListener;
8573 
8574   for (DeclContextVisibleUpdatesPending::iterator
8575            I = PendingVisibleUpdates.begin(),
8576            E = PendingVisibleUpdates.end();
8577        I != E; ++I) {
8578     for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8579                                              F = I->second.end();
8580          J != F; ++J)
8581       delete J->first;
8582   }
8583 }
8584