blob: 0ee2b2b2215ea2311651f6ab1601b29a9392e0fe [file] [log] [blame]
Nick Lewycky995e26b2013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002//
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"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070025#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026#include "clang/Basic/FileManager.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027#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"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070033#include "clang/Frontend/Utils.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000034#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"
Douglas Gregor1a49d972013-01-25 01:03:03 +000043#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000044#include "clang/Serialization/ModuleManager.h"
45#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000047#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"
Stephen Hines651f13c2014-04-23 16:59:28 -070054#include "llvm/Support/raw_ostream.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055#include <algorithm>
Chris Lattnere4e4a882013-01-20 00:57:52 +000056#include <cstdio>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000057#include <iterator>
Stephen Hinesc568f1e2014-07-21 00:47:37 -070058#include <system_error>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000059
60using namespace clang;
61using namespace clang::serialization;
62using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000063using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064
Stephen Hines651f13c2014-04-23 16:59:28 -070065
66//===----------------------------------------------------------------------===//
67// ChainedASTReaderListener implementation
68//===----------------------------------------------------------------------===//
69
70bool
71ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
74}
Stephen Hines6bcf27b2014-05-29 04:14:42 -070075void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76 First->ReadModuleName(ModuleName);
77 Second->ReadModuleName(ModuleName);
78}
79void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80 First->ReadModuleMapFile(ModuleMapPath);
81 Second->ReadModuleMapFile(ModuleMapPath);
82}
Stephen Hines176edba2014-12-01 14:53:08 -080083bool
84ChainedASTReaderListener::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);
Stephen Hines651f13c2014-04-23 16:59:28 -070091}
92bool
93ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
94 bool Complain) {
95 return First->ReadTargetOptions(TargetOpts, Complain) ||
96 Second->ReadTargetOptions(TargetOpts, Complain);
97}
98bool ChainedASTReaderListener::ReadDiagnosticOptions(
Stephen Hines6bcf27b2014-05-29 04:14:42 -070099 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700100 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
101 Second->ReadDiagnosticOptions(DiagOpts, Complain);
102}
103bool
104ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
105 bool Complain) {
106 return First->ReadFileSystemOptions(FSOpts, Complain) ||
107 Second->ReadFileSystemOptions(FSOpts, Complain);
108}
109
110bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700111 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
112 bool Complain) {
113 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
114 Complain) ||
115 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
116 Complain);
Stephen Hines651f13c2014-04-23 16:59:28 -0700117}
118bool ChainedASTReaderListener::ReadPreprocessorOptions(
119 const PreprocessorOptions &PPOpts, bool Complain,
120 std::string &SuggestedPredefines) {
121 return First->ReadPreprocessorOptions(PPOpts, Complain,
122 SuggestedPredefines) ||
123 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
124}
125void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
126 unsigned Value) {
127 First->ReadCounter(M, Value);
128 Second->ReadCounter(M, Value);
129}
130bool ChainedASTReaderListener::needsInputFileVisitation() {
131 return First->needsInputFileVisitation() ||
132 Second->needsInputFileVisitation();
133}
134bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
135 return First->needsSystemInputFileVisitation() ||
136 Second->needsSystemInputFileVisitation();
137}
138void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
139 First->visitModuleFile(Filename);
140 Second->visitModuleFile(Filename);
141}
142bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
143 bool isSystem,
144 bool isOverridden) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700145 bool Continue = false;
146 if (First->needsInputFileVisitation() &&
147 (!isSystem || First->needsSystemInputFileVisitation()))
148 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
149 if (Second->needsInputFileVisitation() &&
150 (!isSystem || Second->needsSystemInputFileVisitation()))
151 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
152 return Continue;
Stephen Hines651f13c2014-04-23 16:59:28 -0700153}
154
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000155//===----------------------------------------------------------------------===//
156// PCH validator implementation
157//===----------------------------------------------------------------------===//
158
159ASTReaderListener::~ASTReaderListener() {}
160
161/// \brief Compare the given set of language options against an existing set of
162/// language options.
163///
164/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Stephen Hines176edba2014-12-01 14:53:08 -0800165/// \param AllowCompatibleDifferences If true, differences between compatible
166/// language options will be permitted.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000167///
168/// \returns true if the languagae options mis-match, false otherwise.
169static bool checkLanguageOptions(const LangOptions &LangOpts,
170 const LangOptions &ExistingLangOpts,
Stephen Hines176edba2014-12-01 14:53:08 -0800171 DiagnosticsEngine *Diags,
172 bool AllowCompatibleDifferences = true) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000173#define LANGOPT(Name, Bits, Default, Description) \
174 if (ExistingLangOpts.Name != LangOpts.Name) { \
175 if (Diags) \
176 Diags->Report(diag::err_pch_langopt_mismatch) \
177 << Description << LangOpts.Name << ExistingLangOpts.Name; \
178 return true; \
179 }
180
181#define VALUE_LANGOPT(Name, Bits, Default, Description) \
182 if (ExistingLangOpts.Name != LangOpts.Name) { \
183 if (Diags) \
184 Diags->Report(diag::err_pch_langopt_value_mismatch) \
185 << Description; \
186 return true; \
187 }
188
189#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
190 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
191 if (Diags) \
192 Diags->Report(diag::err_pch_langopt_value_mismatch) \
193 << Description; \
194 return true; \
195 }
196
Stephen Hines176edba2014-12-01 14:53:08 -0800197#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
198 if (!AllowCompatibleDifferences) \
199 LANGOPT(Name, Bits, Default, Description)
200
201#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
202 if (!AllowCompatibleDifferences) \
203 ENUM_LANGOPT(Name, Bits, Default, Description)
204
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000205#define BENIGN_LANGOPT(Name, Bits, Default, Description)
206#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
207#include "clang/Basic/LangOptions.def"
208
209 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
210 if (Diags)
211 Diags->Report(diag::err_pch_langopt_value_mismatch)
212 << "target Objective-C runtime";
213 return true;
214 }
215
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +0000216 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
217 LangOpts.CommentOpts.BlockCommandNames) {
218 if (Diags)
219 Diags->Report(diag::err_pch_langopt_value_mismatch)
220 << "block command names";
221 return true;
222 }
223
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000224 return false;
225}
226
227/// \brief Compare the given set of target options against an existing set of
228/// target options.
229///
230/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
231///
232/// \returns true if the target options mis-match, false otherwise.
233static bool checkTargetOptions(const TargetOptions &TargetOpts,
234 const TargetOptions &ExistingTargetOpts,
235 DiagnosticsEngine *Diags) {
236#define CHECK_TARGET_OPT(Field, Name) \
237 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
238 if (Diags) \
239 Diags->Report(diag::err_pch_targetopt_mismatch) \
240 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
241 return true; \
242 }
243
244 CHECK_TARGET_OPT(Triple, "target");
245 CHECK_TARGET_OPT(CPU, "target CPU");
246 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000247#undef CHECK_TARGET_OPT
248
249 // Compare feature sets.
250 SmallVector<StringRef, 4> ExistingFeatures(
251 ExistingTargetOpts.FeaturesAsWritten.begin(),
252 ExistingTargetOpts.FeaturesAsWritten.end());
253 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
254 TargetOpts.FeaturesAsWritten.end());
255 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
256 std::sort(ReadFeatures.begin(), ReadFeatures.end());
257
258 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
259 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
260 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
261 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
262 ++ExistingIdx;
263 ++ReadIdx;
264 continue;
265 }
266
267 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
268 if (Diags)
269 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
270 << false << ReadFeatures[ReadIdx];
271 return true;
272 }
273
274 if (Diags)
275 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
276 << true << ExistingFeatures[ExistingIdx];
277 return true;
278 }
279
280 if (ExistingIdx < ExistingN) {
281 if (Diags)
282 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
283 << true << ExistingFeatures[ExistingIdx];
284 return true;
285 }
286
287 if (ReadIdx < ReadN) {
288 if (Diags)
289 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
290 << false << ReadFeatures[ReadIdx];
291 return true;
292 }
293
294 return false;
295}
296
297bool
298PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Stephen Hines176edba2014-12-01 14:53:08 -0800299 bool Complain,
300 bool AllowCompatibleDifferences) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000301 const LangOptions &ExistingLangOpts = PP.getLangOpts();
302 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Stephen Hines176edba2014-12-01 14:53:08 -0800303 Complain ? &Reader.Diags : nullptr,
304 AllowCompatibleDifferences);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000305}
306
307bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
308 bool Complain) {
309 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
310 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700311 Complain? &Reader.Diags : nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000312}
313
314namespace {
315 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
316 MacroDefinitionsMap;
Craig Topper8ae63872013-07-05 04:43:31 +0000317 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
318 DeclsMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000319}
320
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700321static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
322 DiagnosticsEngine &Diags,
323 bool Complain) {
324 typedef DiagnosticsEngine::Level Level;
325
326 // Check current mappings for new -Werror mappings, and the stored mappings
327 // for cases that were explicitly mapped to *not* be errors that are now
328 // errors because of options like -Werror.
329 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
330
331 for (DiagnosticsEngine *MappingSource : MappingSources) {
332 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
333 diag::kind DiagID = DiagIDMappingPair.first;
334 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
335 if (CurLevel < DiagnosticsEngine::Error)
336 continue; // not significant
337 Level StoredLevel =
338 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
339 if (StoredLevel < DiagnosticsEngine::Error) {
340 if (Complain)
341 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
342 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
343 return true;
344 }
345 }
346 }
347
348 return false;
349}
350
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700351static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
352 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
353 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
354 return true;
355 return Ext >= diag::Severity::Error;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700356}
357
358static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
359 DiagnosticsEngine &Diags,
360 bool IsSystem, bool Complain) {
361 // Top-level options
362 if (IsSystem) {
363 if (Diags.getSuppressSystemWarnings())
364 return false;
365 // If -Wsystem-headers was not enabled before, be conservative
366 if (StoredDiags.getSuppressSystemWarnings()) {
367 if (Complain)
368 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
369 return true;
370 }
371 }
372
373 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
374 if (Complain)
375 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
376 return true;
377 }
378
379 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
380 !StoredDiags.getEnableAllWarnings()) {
381 if (Complain)
382 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
383 return true;
384 }
385
386 if (isExtHandlingFromDiagsError(Diags) &&
387 !isExtHandlingFromDiagsError(StoredDiags)) {
388 if (Complain)
389 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
390 return true;
391 }
392
393 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
394}
395
396bool PCHValidator::ReadDiagnosticOptions(
397 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
398 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
399 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
400 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700401 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700402 // This should never fail, because we would have processed these options
403 // before writing them to an ASTFile.
404 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
405
406 ModuleManager &ModuleMgr = Reader.getModuleManager();
407 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
408
409 // If the original import came from a file explicitly generated by the user,
410 // don't check the diagnostic mappings.
411 // FIXME: currently this is approximated by checking whether this is not a
Stephen Hines176edba2014-12-01 14:53:08 -0800412 // module import of an implicitly-loaded module file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700413 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
414 // the transitive closure of its imports, since unrelated modules cannot be
415 // imported until after this module finishes validation.
416 ModuleFile *TopImport = *ModuleMgr.rbegin();
417 while (!TopImport->ImportedBy.empty())
418 TopImport = TopImport->ImportedBy[0];
Stephen Hines176edba2014-12-01 14:53:08 -0800419 if (TopImport->Kind != MK_ImplicitModule)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700420 return false;
421
422 StringRef ModuleName = TopImport->ModuleName;
423 assert(!ModuleName.empty() && "diagnostic options read before module name");
424
425 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
426 assert(M && "missing module");
427
428 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
429 // contains the union of their flags.
430 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
431}
432
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000433/// \brief Collect the macro definitions provided by the given preprocessor
434/// options.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700435static void
436collectMacroDefinitions(const PreprocessorOptions &PPOpts,
437 MacroDefinitionsMap &Macros,
438 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000439 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
440 StringRef Macro = PPOpts.Macros[I].first;
441 bool IsUndef = PPOpts.Macros[I].second;
442
443 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
444 StringRef MacroName = MacroPair.first;
445 StringRef MacroBody = MacroPair.second;
446
447 // For an #undef'd macro, we only care about the name.
448 if (IsUndef) {
449 if (MacroNames && !Macros.count(MacroName))
450 MacroNames->push_back(MacroName);
451
452 Macros[MacroName] = std::make_pair("", true);
453 continue;
454 }
455
456 // For a #define'd macro, figure out the actual definition.
457 if (MacroName.size() == Macro.size())
458 MacroBody = "1";
459 else {
460 // Note: GCC drops anything following an end-of-line character.
461 StringRef::size_type End = MacroBody.find_first_of("\n\r");
462 MacroBody = MacroBody.substr(0, End);
463 }
464
465 if (MacroNames && !Macros.count(MacroName))
466 MacroNames->push_back(MacroName);
467 Macros[MacroName] = std::make_pair(MacroBody, false);
468 }
469}
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700470
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000471/// \brief Check the preprocessor options deserialized from the control block
472/// against the preprocessor options in an existing preprocessor.
473///
474/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
475static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
476 const PreprocessorOptions &ExistingPPOpts,
477 DiagnosticsEngine *Diags,
478 FileManager &FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000479 std::string &SuggestedPredefines,
480 const LangOptions &LangOpts) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000481 // Check macro definitions.
482 MacroDefinitionsMap ASTFileMacros;
483 collectMacroDefinitions(PPOpts, ASTFileMacros);
484 MacroDefinitionsMap ExistingMacros;
485 SmallVector<StringRef, 4> ExistingMacroNames;
486 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
487
488 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
489 // Dig out the macro definition in the existing preprocessor options.
490 StringRef MacroName = ExistingMacroNames[I];
491 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
492
493 // Check whether we know anything about this macro name or not.
494 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
495 = ASTFileMacros.find(MacroName);
496 if (Known == ASTFileMacros.end()) {
497 // FIXME: Check whether this identifier was referenced anywhere in the
498 // AST file. If so, we should reject the AST file. Unfortunately, this
499 // information isn't in the control block. What shall we do about it?
500
501 if (Existing.second) {
502 SuggestedPredefines += "#undef ";
503 SuggestedPredefines += MacroName.str();
504 SuggestedPredefines += '\n';
505 } else {
506 SuggestedPredefines += "#define ";
507 SuggestedPredefines += MacroName.str();
508 SuggestedPredefines += ' ';
509 SuggestedPredefines += Existing.first.str();
510 SuggestedPredefines += '\n';
511 }
512 continue;
513 }
514
515 // If the macro was defined in one but undef'd in the other, we have a
516 // conflict.
517 if (Existing.second != Known->second.second) {
518 if (Diags) {
519 Diags->Report(diag::err_pch_macro_def_undef)
520 << MacroName << Known->second.second;
521 }
522 return true;
523 }
524
525 // If the macro was #undef'd in both, or if the macro bodies are identical,
526 // it's fine.
527 if (Existing.second || Existing.first == Known->second.first)
528 continue;
529
530 // The macro bodies differ; complain.
531 if (Diags) {
532 Diags->Report(diag::err_pch_macro_def_conflict)
533 << MacroName << Known->second.first << Existing.first;
534 }
535 return true;
536 }
537
538 // Check whether we're using predefines.
539 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
540 if (Diags) {
541 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
542 }
543 return true;
544 }
545
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000546 // Detailed record is important since it is used for the module cache hash.
547 if (LangOpts.Modules &&
548 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
549 if (Diags) {
550 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
551 }
552 return true;
553 }
554
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000555 // Compute the #include and #include_macros lines we need.
556 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
557 StringRef File = ExistingPPOpts.Includes[I];
558 if (File == ExistingPPOpts.ImplicitPCHInclude)
559 continue;
560
561 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
562 != PPOpts.Includes.end())
563 continue;
564
565 SuggestedPredefines += "#include \"";
Stephen Hines176edba2014-12-01 14:53:08 -0800566 SuggestedPredefines += File;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000567 SuggestedPredefines += "\"\n";
568 }
569
570 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
571 StringRef File = ExistingPPOpts.MacroIncludes[I];
572 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
573 File)
574 != PPOpts.MacroIncludes.end())
575 continue;
576
577 SuggestedPredefines += "#__include_macros \"";
Stephen Hines176edba2014-12-01 14:53:08 -0800578 SuggestedPredefines += File;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000579 SuggestedPredefines += "\"\n##\n";
580 }
581
582 return false;
583}
584
585bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
586 bool Complain,
587 std::string &SuggestedPredefines) {
588 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
589
590 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700591 Complain? &Reader.Diags : nullptr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000592 PP.getFileManager(),
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000593 SuggestedPredefines,
594 PP.getLangOpts());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000595}
596
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700597/// Check the header search options deserialized from the control block
598/// against the header search options in an existing preprocessor.
599///
600/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
601static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
602 StringRef SpecificModuleCachePath,
603 StringRef ExistingModuleCachePath,
604 DiagnosticsEngine *Diags,
605 const LangOptions &LangOpts) {
606 if (LangOpts.Modules) {
607 if (SpecificModuleCachePath != ExistingModuleCachePath) {
608 if (Diags)
609 Diags->Report(diag::err_pch_modulecache_mismatch)
610 << SpecificModuleCachePath << ExistingModuleCachePath;
611 return true;
612 }
613 }
614
615 return false;
616}
617
618bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
619 StringRef SpecificModuleCachePath,
620 bool Complain) {
621 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
622 PP.getHeaderSearchInfo().getModuleCachePath(),
623 Complain ? &Reader.Diags : nullptr,
624 PP.getLangOpts());
625}
626
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000627void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
628 PP.setCounterValue(Value);
629}
630
631//===----------------------------------------------------------------------===//
632// AST reader implementation
633//===----------------------------------------------------------------------===//
634
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700635void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
636 bool TakeOwnership) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000637 DeserializationListener = Listener;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700638 OwnsDeserializationListener = TakeOwnership;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000639}
640
641
642
643unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
644 return serialization::ComputeHash(Sel);
645}
646
647
648std::pair<unsigned, unsigned>
649ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700650 using namespace llvm::support;
651 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
652 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000653 return std::make_pair(KeyLen, DataLen);
654}
655
656ASTSelectorLookupTrait::internal_key_type
657ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700658 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000659 SelectorTable &SelTable = Reader.getContext().Selectors;
Stephen Hines651f13c2014-04-23 16:59:28 -0700660 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
661 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
662 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000663 if (N == 0)
664 return SelTable.getNullarySelector(FirstII);
665 else if (N == 1)
666 return SelTable.getUnarySelector(FirstII);
667
668 SmallVector<IdentifierInfo *, 16> Args;
669 Args.push_back(FirstII);
670 for (unsigned I = 1; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -0700671 Args.push_back(Reader.getLocalIdentifier(
672 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000673
674 return SelTable.getSelector(N, Args.data());
675}
676
677ASTSelectorLookupTrait::data_type
678ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
679 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700680 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000681
682 data_type Result;
683
Stephen Hines651f13c2014-04-23 16:59:28 -0700684 Result.ID = Reader.getGlobalSelectorID(
685 F, endian::readNext<uint32_t, little, unaligned>(d));
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700686 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
687 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
688 Result.InstanceBits = FullInstanceBits & 0x3;
689 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
690 Result.FactoryBits = FullFactoryBits & 0x3;
691 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
692 unsigned NumInstanceMethods = FullInstanceBits >> 3;
693 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000694
695 // Load instance methods
696 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700697 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
698 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000699 Result.Instance.push_back(Method);
700 }
701
702 // Load factory methods
703 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700704 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
705 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000706 Result.Factory.push_back(Method);
707 }
708
709 return Result;
710}
711
Douglas Gregor479633c2013-01-23 18:53:14 +0000712unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
713 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000714}
715
716std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000717ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700718 using namespace llvm::support;
719 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
720 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000721 return std::make_pair(KeyLen, DataLen);
722}
723
Douglas Gregor479633c2013-01-23 18:53:14 +0000724ASTIdentifierLookupTraitBase::internal_key_type
725ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000726 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000727 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000728}
729
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000730/// \brief Whether the given identifier is "interesting".
731static bool isInterestingIdentifier(IdentifierInfo &II) {
732 return II.isPoisoned() ||
733 II.isExtensionToken() ||
734 II.getObjCOrBuiltinID() ||
735 II.hasRevertedTokenIDToIdentifier() ||
736 II.hadMacroDefinition() ||
737 II.getFETokenInfo<void>();
738}
739
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000740IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
741 const unsigned char* d,
742 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700743 using namespace llvm::support;
744 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000745 bool IsInteresting = RawID & 0x01;
746
747 // Wipe out the "is interesting" bit.
748 RawID = RawID >> 1;
749
750 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
751 if (!IsInteresting) {
752 // For uninteresting identifiers, just build the IdentifierInfo
753 // and associate it with the persistent ID.
754 IdentifierInfo *II = KnownII;
755 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000756 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000757 KnownII = II;
758 }
759 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000760 if (!II->isFromAST()) {
761 bool WasInteresting = isInterestingIdentifier(*II);
762 II->setIsFromAST();
763 if (WasInteresting)
764 II->setChangedSinceDeserialization();
765 }
766 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000767 return II;
768 }
769
Stephen Hines651f13c2014-04-23 16:59:28 -0700770 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
771 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000772 bool CPlusPlusOperatorKeyword = Bits & 0x01;
773 Bits >>= 1;
774 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
775 Bits >>= 1;
776 bool Poisoned = Bits & 0x01;
777 Bits >>= 1;
778 bool ExtensionToken = Bits & 0x01;
779 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000780 bool hasSubmoduleMacros = Bits & 0x01;
781 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000782 bool hadMacroDefinition = Bits & 0x01;
783 Bits >>= 1;
784
785 assert(Bits == 0 && "Extra bits in the identifier?");
786 DataLen -= 8;
787
788 // Build the IdentifierInfo itself and link the identifier ID with
789 // the new IdentifierInfo.
790 IdentifierInfo *II = KnownII;
791 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000792 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000793 KnownII = II;
794 }
795 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000796 if (!II->isFromAST()) {
797 bool WasInteresting = isInterestingIdentifier(*II);
798 II->setIsFromAST();
799 if (WasInteresting)
800 II->setChangedSinceDeserialization();
801 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000802
803 // Set or check the various bits in the IdentifierInfo structure.
804 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000805 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000806 II->RevertTokenIDToIdentifier();
807 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
808 assert(II->isExtensionToken() == ExtensionToken &&
809 "Incorrect extension token flag");
810 (void)ExtensionToken;
811 if (Poisoned)
812 II->setIsPoisoned(true);
813 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
814 "Incorrect C++ operator keyword flag");
815 (void)CPlusPlusOperatorKeyword;
816
817 // If this identifier is a macro, deserialize the macro
818 // definition.
819 if (hadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700820 uint32_t MacroDirectivesOffset =
821 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000822 DataLen -= 4;
823 SmallVector<uint32_t, 8> LocalMacroIDs;
824 if (hasSubmoduleMacros) {
Stephen Hines176edba2014-12-01 14:53:08 -0800825 while (true) {
826 uint32_t LocalMacroID =
827 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000828 DataLen -= 4;
Stephen Hines176edba2014-12-01 14:53:08 -0800829 if (LocalMacroID == 0xdeadbeef) break;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000830 LocalMacroIDs.push_back(LocalMacroID);
831 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000832 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000833
Stephen Hines176edba2014-12-01 14:53:08 -0800834 if (F.Kind == MK_ImplicitModule || F.Kind == MK_ExplicitModule) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700835 // Macro definitions are stored from newest to oldest, so reverse them
836 // before registering them.
837 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000838 for (SmallVectorImpl<uint32_t>::iterator
Stephen Hines651f13c2014-04-23 16:59:28 -0700839 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
840 unsigned Size = 1;
841
842 static const uint32_t HasOverridesFlag = 0x80000000U;
843 if (I + 1 != E && (I[1] & HasOverridesFlag))
844 Size += 1 + (I[1] & ~HasOverridesFlag);
845
846 MacroSizes.push_back(Size);
847 I += Size;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000848 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700849
850 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
851 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
852 SE = MacroSizes.rend();
853 SI != SE; ++SI) {
854 I -= *SI;
855
856 uint32_t LocalMacroID = *I;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700857 ArrayRef<uint32_t> Overrides;
Stephen Hines651f13c2014-04-23 16:59:28 -0700858 if (*SI != 1)
859 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
860 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
861 }
862 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000863 } else {
864 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
865 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000866 }
867
868 Reader.SetIdentifierInfo(ID, II);
869
870 // Read all of the declarations visible at global scope with this
871 // name.
872 if (DataLen > 0) {
873 SmallVector<uint32_t, 4> DeclIDs;
874 for (; DataLen > 0; DataLen -= 4)
Stephen Hines651f13c2014-04-23 16:59:28 -0700875 DeclIDs.push_back(Reader.getGlobalDeclID(
876 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000877 Reader.SetGloballyVisibleDecls(II, DeclIDs);
878 }
879
880 return II;
881}
882
883unsigned
884ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
885 llvm::FoldingSetNodeID ID;
886 ID.AddInteger(Key.Kind);
887
888 switch (Key.Kind) {
889 case DeclarationName::Identifier:
890 case DeclarationName::CXXLiteralOperatorName:
891 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
892 break;
893 case DeclarationName::ObjCZeroArgSelector:
894 case DeclarationName::ObjCOneArgSelector:
895 case DeclarationName::ObjCMultiArgSelector:
896 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
897 break;
898 case DeclarationName::CXXOperatorName:
899 ID.AddInteger((OverloadedOperatorKind)Key.Data);
900 break;
901 case DeclarationName::CXXConstructorName:
902 case DeclarationName::CXXDestructorName:
903 case DeclarationName::CXXConversionFunctionName:
904 case DeclarationName::CXXUsingDirective:
905 break;
906 }
907
908 return ID.ComputeHash();
909}
910
911ASTDeclContextNameLookupTrait::internal_key_type
912ASTDeclContextNameLookupTrait::GetInternalKey(
913 const external_key_type& Name) const {
914 DeclNameKey Key;
915 Key.Kind = Name.getNameKind();
916 switch (Name.getNameKind()) {
917 case DeclarationName::Identifier:
918 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
919 break;
920 case DeclarationName::ObjCZeroArgSelector:
921 case DeclarationName::ObjCOneArgSelector:
922 case DeclarationName::ObjCMultiArgSelector:
923 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
924 break;
925 case DeclarationName::CXXOperatorName:
926 Key.Data = Name.getCXXOverloadedOperator();
927 break;
928 case DeclarationName::CXXLiteralOperatorName:
929 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
930 break;
931 case DeclarationName::CXXConstructorName:
932 case DeclarationName::CXXDestructorName:
933 case DeclarationName::CXXConversionFunctionName:
934 case DeclarationName::CXXUsingDirective:
935 Key.Data = 0;
936 break;
937 }
938
939 return Key;
940}
941
942std::pair<unsigned, unsigned>
943ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700944 using namespace llvm::support;
945 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
946 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000947 return std::make_pair(KeyLen, DataLen);
948}
949
950ASTDeclContextNameLookupTrait::internal_key_type
951ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700952 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000953
954 DeclNameKey Key;
955 Key.Kind = (DeclarationName::NameKind)*d++;
956 switch (Key.Kind) {
957 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700958 Key.Data = (uint64_t)Reader.getLocalIdentifier(
959 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000960 break;
961 case DeclarationName::ObjCZeroArgSelector:
962 case DeclarationName::ObjCOneArgSelector:
963 case DeclarationName::ObjCMultiArgSelector:
964 Key.Data =
Stephen Hines651f13c2014-04-23 16:59:28 -0700965 (uint64_t)Reader.getLocalSelector(
966 F, endian::readNext<uint32_t, little, unaligned>(
967 d)).getAsOpaquePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000968 break;
969 case DeclarationName::CXXOperatorName:
970 Key.Data = *d++; // OverloadedOperatorKind
971 break;
972 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -0700973 Key.Data = (uint64_t)Reader.getLocalIdentifier(
974 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000975 break;
976 case DeclarationName::CXXConstructorName:
977 case DeclarationName::CXXDestructorName:
978 case DeclarationName::CXXConversionFunctionName:
979 case DeclarationName::CXXUsingDirective:
980 Key.Data = 0;
981 break;
982 }
983
984 return Key;
985}
986
987ASTDeclContextNameLookupTrait::data_type
988ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
989 const unsigned char* d,
990 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700991 using namespace llvm::support;
992 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000993 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
994 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000995 return std::make_pair(Start, Start + NumDecls);
996}
997
998bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000999 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001000 const std::pair<uint64_t, uint64_t> &Offsets,
1001 DeclContextInfo &Info) {
1002 SavedStreamPosition SavedPosition(Cursor);
1003 // First the lexical decls.
1004 if (Offsets.first != 0) {
1005 Cursor.JumpToBit(Offsets.first);
1006
1007 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001008 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001009 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001010 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001011 if (RecCode != DECL_CONTEXT_LEXICAL) {
1012 Error("Expected lexical block");
1013 return true;
1014 }
1015
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001016 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
1017 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001018 }
1019
1020 // Now the lookup table.
1021 if (Offsets.second != 0) {
1022 Cursor.JumpToBit(Offsets.second);
1023
1024 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001025 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001026 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001027 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001028 if (RecCode != DECL_CONTEXT_VISIBLE) {
1029 Error("Expected visible lookup table block");
1030 return true;
1031 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001032 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
1033 (const unsigned char *)Blob.data() + Record[0],
1034 (const unsigned char *)Blob.data() + sizeof(uint32_t),
1035 (const unsigned char *)Blob.data(),
1036 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001037 }
1038
1039 return false;
1040}
1041
1042void ASTReader::Error(StringRef Msg) {
1043 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregorc1478612013-05-10 22:15:13 +00001044 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1045 Diag(diag::note_module_cache_path)
1046 << PP.getHeaderSearchInfo().getModuleCachePath();
1047 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001048}
1049
1050void ASTReader::Error(unsigned DiagID,
1051 StringRef Arg1, StringRef Arg2) {
1052 if (Diags.isDiagnosticInFlight())
1053 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1054 else
1055 Diag(DiagID) << Arg1 << Arg2;
1056}
1057
1058//===----------------------------------------------------------------------===//
1059// Source Manager Deserialization
1060//===----------------------------------------------------------------------===//
1061
1062/// \brief Read the line table in the source manager block.
1063/// \returns true if there was an error.
1064bool ASTReader::ParseLineTable(ModuleFile &F,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001065 const RecordData &Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001066 unsigned Idx = 0;
1067 LineTableInfo &LineTable = SourceMgr.getLineTable();
1068
1069 // Parse the file names
1070 std::map<int, int> FileIDs;
1071 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1072 // Extract the file name
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001073 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001074 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1075 }
1076
1077 // Parse the line entries
1078 std::vector<LineEntry> Entries;
1079 while (Idx < Record.size()) {
1080 int FID = Record[Idx++];
1081 assert(FID >= 0 && "Serialized line entries for non-local file.");
1082 // Remap FileID from 1-based old view.
1083 FID += F.SLocEntryBaseID - 1;
1084
1085 // Extract the line entries
1086 unsigned NumEntries = Record[Idx++];
1087 assert(NumEntries && "Numentries is 00000");
1088 Entries.clear();
1089 Entries.reserve(NumEntries);
1090 for (unsigned I = 0; I != NumEntries; ++I) {
1091 unsigned FileOffset = Record[Idx++];
1092 unsigned LineNo = Record[Idx++];
1093 int FilenameID = FileIDs[Record[Idx++]];
1094 SrcMgr::CharacteristicKind FileKind
1095 = (SrcMgr::CharacteristicKind)Record[Idx++];
1096 unsigned IncludeOffset = Record[Idx++];
1097 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1098 FileKind, IncludeOffset));
1099 }
1100 LineTable.AddEntry(FileID::get(FID), Entries);
1101 }
1102
1103 return false;
1104}
1105
1106/// \brief Read a source manager block
1107bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1108 using namespace SrcMgr;
1109
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001110 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001111
1112 // Set the source-location entry cursor to the current position in
1113 // the stream. This cursor will be used to read the contents of the
1114 // source manager block initially, and then lazily read
1115 // source-location entries as needed.
1116 SLocEntryCursor = F.Stream;
1117
1118 // The stream itself is going to skip over the source manager block.
1119 if (F.Stream.SkipBlock()) {
1120 Error("malformed block record in AST file");
1121 return true;
1122 }
1123
1124 // Enter the source manager block.
1125 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1126 Error("malformed source manager block record in AST file");
1127 return true;
1128 }
1129
1130 RecordData Record;
1131 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001132 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1133
1134 switch (E.Kind) {
1135 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1136 case llvm::BitstreamEntry::Error:
1137 Error("malformed block record in AST file");
1138 return true;
1139 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001140 return false;
Chris Lattner88bde502013-01-19 21:39:22 +00001141 case llvm::BitstreamEntry::Record:
1142 // The interesting case.
1143 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144 }
Chris Lattner88bde502013-01-19 21:39:22 +00001145
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001146 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001147 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +00001148 StringRef Blob;
1149 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001150 default: // Default behavior: ignore.
1151 break;
1152
1153 case SM_SLOC_FILE_ENTRY:
1154 case SM_SLOC_BUFFER_ENTRY:
1155 case SM_SLOC_EXPANSION_ENTRY:
1156 // Once we hit one of the source location entries, we're done.
1157 return false;
1158 }
1159 }
1160}
1161
1162/// \brief If a header file is not found at the path that we expect it to be
1163/// and the PCH file was moved from its original location, try to resolve the
1164/// file by assuming that header+PCH were moved together and the header is in
1165/// the same place relative to the PCH.
1166static std::string
1167resolveFileRelativeToOriginalDir(const std::string &Filename,
1168 const std::string &OriginalDir,
1169 const std::string &CurrDir) {
1170 assert(OriginalDir != CurrDir &&
1171 "No point trying to resolve the file if the PCH dir didn't change");
1172 using namespace llvm::sys;
1173 SmallString<128> filePath(Filename);
1174 fs::make_absolute(filePath);
1175 assert(path::is_absolute(OriginalDir));
1176 SmallString<128> currPCHPath(CurrDir);
1177
1178 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1179 fileDirE = path::end(path::parent_path(filePath));
1180 path::const_iterator origDirI = path::begin(OriginalDir),
1181 origDirE = path::end(OriginalDir);
1182 // Skip the common path components from filePath and OriginalDir.
1183 while (fileDirI != fileDirE && origDirI != origDirE &&
1184 *fileDirI == *origDirI) {
1185 ++fileDirI;
1186 ++origDirI;
1187 }
1188 for (; origDirI != origDirE; ++origDirI)
1189 path::append(currPCHPath, "..");
1190 path::append(currPCHPath, fileDirI, fileDirE);
1191 path::append(currPCHPath, path::filename(Filename));
1192 return currPCHPath.str();
1193}
1194
1195bool ASTReader::ReadSLocEntry(int ID) {
1196 if (ID == 0)
1197 return false;
1198
1199 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1200 Error("source location entry ID out-of-range for AST file");
1201 return true;
1202 }
1203
1204 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1205 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001206 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001207 unsigned BaseOffset = F->SLocEntryBaseOffset;
1208
1209 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +00001210 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1211 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001212 Error("incorrectly-formatted source location entry in AST file");
1213 return true;
1214 }
Chris Lattner88bde502013-01-19 21:39:22 +00001215
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001216 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001217 StringRef Blob;
1218 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001219 default:
1220 Error("incorrectly-formatted source location entry in AST file");
1221 return true;
1222
1223 case SM_SLOC_FILE_ENTRY: {
1224 // We will detect whether a file changed and return 'Failure' for it, but
1225 // we will also try to fail gracefully by setting up the SLocEntry.
1226 unsigned InputID = Record[4];
1227 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001228 const FileEntry *File = IF.getFile();
1229 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001230
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001231 // Note that we only check if a File was returned. If it was out-of-date
1232 // we have complained but we will continue creating a FileID to recover
1233 // gracefully.
1234 if (!File)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001235 return true;
1236
1237 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1238 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1239 // This is the module's main file.
1240 IncludeLoc = getImportLocation(F);
1241 }
1242 SrcMgr::CharacteristicKind
1243 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1244 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1245 ID, BaseOffset + Record[0]);
1246 SrcMgr::FileInfo &FileInfo =
1247 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1248 FileInfo.NumCreatedFIDs = Record[5];
1249 if (Record[3])
1250 FileInfo.setHasLineDirectives();
1251
1252 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1253 unsigned NumFileDecls = Record[7];
1254 if (NumFileDecls) {
1255 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1256 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1257 NumFileDecls));
1258 }
1259
1260 const SrcMgr::ContentCache *ContentCache
1261 = SourceMgr.getOrCreateContentCache(File,
1262 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1263 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1264 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1265 unsigned Code = SLocEntryCursor.ReadCode();
1266 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001267 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001268
1269 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1270 Error("AST record has invalid code");
1271 return true;
1272 }
1273
Stephen Hines176edba2014-12-01 14:53:08 -08001274 std::unique_ptr<llvm::MemoryBuffer> Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001275 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Stephen Hines176edba2014-12-01 14:53:08 -08001276 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001277 }
1278
1279 break;
1280 }
1281
1282 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001283 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001284 unsigned Offset = Record[0];
1285 SrcMgr::CharacteristicKind
1286 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1287 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Stephen Hines176edba2014-12-01 14:53:08 -08001288 if (IncludeLoc.isInvalid() &&
1289 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001290 IncludeLoc = getImportLocation(F);
1291 }
1292 unsigned Code = SLocEntryCursor.ReadCode();
1293 Record.clear();
1294 unsigned RecCode
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001295 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001296
1297 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1298 Error("AST record has invalid code");
1299 return true;
1300 }
1301
Stephen Hines176edba2014-12-01 14:53:08 -08001302 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1303 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1304 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1305 BaseOffset + Offset, IncludeLoc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001306 break;
1307 }
1308
1309 case SM_SLOC_EXPANSION_ENTRY: {
1310 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1311 SourceMgr.createExpansionLoc(SpellingLoc,
1312 ReadSourceLocation(*F, Record[2]),
1313 ReadSourceLocation(*F, Record[3]),
1314 Record[4],
1315 ID,
1316 BaseOffset + Record[0]);
1317 break;
1318 }
1319 }
1320
1321 return false;
1322}
1323
1324std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1325 if (ID == 0)
1326 return std::make_pair(SourceLocation(), "");
1327
1328 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1329 Error("source location entry ID out-of-range for AST file");
1330 return std::make_pair(SourceLocation(), "");
1331 }
1332
1333 // Find which module file this entry lands in.
1334 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Stephen Hines176edba2014-12-01 14:53:08 -08001335 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001336 return std::make_pair(SourceLocation(), "");
1337
1338 // FIXME: Can we map this down to a particular submodule? That would be
1339 // ideal.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001340 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001341}
1342
1343/// \brief Find the location where the module F is imported.
1344SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1345 if (F->ImportLoc.isValid())
1346 return F->ImportLoc;
1347
1348 // Otherwise we have a PCH. It's considered to be "imported" at the first
1349 // location of its includer.
1350 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001351 // Main file is the importer.
1352 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1353 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001355 return F->ImportedBy[0]->FirstLoc;
1356}
1357
1358/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1359/// specified cursor. Read the abbreviations that are at the top of the block
1360/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001361bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001362 if (Cursor.EnterSubBlock(BlockID)) {
1363 Error("malformed block record in AST file");
1364 return Failure;
1365 }
1366
1367 while (true) {
1368 uint64_t Offset = Cursor.GetCurrentBitNo();
1369 unsigned Code = Cursor.ReadCode();
1370
1371 // We expect all abbrevs to be at the start of the block.
1372 if (Code != llvm::bitc::DEFINE_ABBREV) {
1373 Cursor.JumpToBit(Offset);
1374 return false;
1375 }
1376 Cursor.ReadAbbrevRecord();
1377 }
1378}
1379
Richard Smithac32d902013-08-07 21:41:30 +00001380Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallaeeacf72013-05-03 00:10:13 +00001381 unsigned &Idx) {
1382 Token Tok;
1383 Tok.startToken();
1384 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1385 Tok.setLength(Record[Idx++]);
1386 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1387 Tok.setIdentifierInfo(II);
1388 Tok.setKind((tok::TokenKind)Record[Idx++]);
1389 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1390 return Tok;
1391}
1392
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001393MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001394 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001395
1396 // Keep track of where we are in the stream, then jump back there
1397 // after reading this macro.
1398 SavedStreamPosition SavedPosition(Stream);
1399
1400 Stream.JumpToBit(Offset);
1401 RecordData Record;
1402 SmallVector<IdentifierInfo*, 16> MacroArgs;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001403 MacroInfo *Macro = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001404
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001406 // Advance to the next record, but if we get to the end of the block, don't
1407 // pop it (removing all the abbreviations from the cursor) since we want to
1408 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001409 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001410 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1411
1412 switch (Entry.Kind) {
1413 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1414 case llvm::BitstreamEntry::Error:
1415 Error("malformed block record in AST file");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001416 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001417 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001418 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001419 case llvm::BitstreamEntry::Record:
1420 // The interesting case.
1421 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001422 }
1423
1424 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001425 Record.clear();
1426 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001427 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001428 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001429 case PP_MACRO_DIRECTIVE_HISTORY:
1430 return Macro;
1431
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001432 case PP_MACRO_OBJECT_LIKE:
1433 case PP_MACRO_FUNCTION_LIKE: {
1434 // If we already have a macro, that means that we've hit the end
1435 // of the definition of the macro we were looking for. We're
1436 // done.
1437 if (Macro)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001438 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001439
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001440 unsigned NextIndex = 1; // Skip identifier ID.
1441 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001442 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001443 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001444 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001445 MI->setIsUsed(Record[NextIndex++]);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001446 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001447
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001448 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1449 // Decode function-like macro info.
1450 bool isC99VarArgs = Record[NextIndex++];
1451 bool isGNUVarArgs = Record[NextIndex++];
1452 bool hasCommaPasting = Record[NextIndex++];
1453 MacroArgs.clear();
1454 unsigned NumArgs = Record[NextIndex++];
1455 for (unsigned i = 0; i != NumArgs; ++i)
1456 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1457
1458 // Install function-like macro info.
1459 MI->setIsFunctionLike();
1460 if (isC99VarArgs) MI->setIsC99Varargs();
1461 if (isGNUVarArgs) MI->setIsGNUVarargs();
1462 if (hasCommaPasting) MI->setHasCommaPasting();
1463 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1464 PP.getPreprocessorAllocator());
1465 }
1466
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001467 // Remember that we saw this macro last so that we add the tokens that
1468 // form its body to it.
1469 Macro = MI;
1470
1471 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1472 Record[NextIndex]) {
1473 // We have a macro definition. Register the association
1474 PreprocessedEntityID
1475 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1476 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +00001477 PreprocessingRecord::PPEntityID
1478 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1479 MacroDefinition *PPDef =
1480 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1481 if (PPDef)
1482 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001483 }
1484
1485 ++NumMacrosRead;
1486 break;
1487 }
1488
1489 case PP_TOKEN: {
1490 // If we see a TOKEN before a PP_MACRO_*, then the file is
1491 // erroneous, just pretend we didn't see this.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001492 if (!Macro) break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001493
John McCallaeeacf72013-05-03 00:10:13 +00001494 unsigned Idx = 0;
1495 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001496 Macro->AddTokenToBody(Tok);
1497 break;
1498 }
1499 }
1500 }
1501}
1502
1503PreprocessedEntityID
1504ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1505 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1506 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1507 assert(I != M.PreprocessedEntityRemap.end()
1508 && "Invalid index into preprocessed entity index remap");
1509
1510 return LocalID + I->second;
1511}
1512
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001513unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1514 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001515}
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001516
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001517HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001518HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1519 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001520 FE->getName(), /*Imported*/false };
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001521 return ikey;
1522}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001523
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001524bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1525 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526 return false;
1527
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001528 if (llvm::sys::path::is_absolute(a.Filename) &&
1529 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001530 return true;
1531
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001532 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001533 FileManager &FileMgr = Reader.getFileManager();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001534 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1535 if (!Key.Imported)
1536 return FileMgr.getFile(Key.Filename);
1537
1538 std::string Resolved = Key.Filename;
1539 Reader.ResolveImportedPath(M, Resolved);
1540 return FileMgr.getFile(Resolved);
1541 };
1542
1543 const FileEntry *FEA = GetFile(a);
1544 const FileEntry *FEB = GetFile(b);
1545 return FEA && FEA == FEB;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001546}
1547
1548std::pair<unsigned, unsigned>
1549HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001550 using namespace llvm::support;
1551 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001552 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001553 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001554}
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001555
1556HeaderFileInfoTrait::internal_key_type
1557HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001558 using namespace llvm::support;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001559 internal_key_type ikey;
Stephen Hines651f13c2014-04-23 16:59:28 -07001560 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1561 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001562 ikey.Filename = (const char *)d;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001563 ikey.Imported = true;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001564 return ikey;
1565}
1566
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001567HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001568HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001569 unsigned DataLen) {
1570 const unsigned char *End = d + DataLen;
Stephen Hines651f13c2014-04-23 16:59:28 -07001571 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001572 HeaderFileInfo HFI;
1573 unsigned Flags = *d++;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001574 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1575 ((Flags >> 6) & 0x03);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001576 HFI.isImport = (Flags >> 5) & 0x01;
1577 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1578 HFI.DirInfo = (Flags >> 2) & 0x03;
1579 HFI.Resolved = (Flags >> 1) & 0x01;
1580 HFI.IndexHeaderMapHeader = Flags & 0x01;
Stephen Hines651f13c2014-04-23 16:59:28 -07001581 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1582 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1583 M, endian::readNext<uint32_t, little, unaligned>(d));
1584 if (unsigned FrameworkOffset =
1585 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001586 // The framework offset is 1 greater than the actual offset,
1587 // since 0 is used as an indicator for "no framework name".
1588 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1589 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1590 }
1591
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001592 if (d != End) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001593 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001594 if (LocalSMID) {
1595 // This header is part of a module. Associate it with the module to enable
1596 // implicit module import.
1597 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1598 Module *Mod = Reader.getSubmodule(GlobalSMID);
1599 HFI.isModuleHeader = true;
1600 FileManager &FileMgr = Reader.getFileManager();
1601 ModuleMap &ModMap =
1602 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001603 // FIXME: This information should be propagated through the
1604 // SUBMODULE_HEADER etc records rather than from here.
1605 // FIXME: We don't ever mark excluded headers.
1606 std::string Filename = key.Filename;
1607 if (key.Imported)
1608 Reader.ResolveImportedPath(M, Filename);
1609 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1610 ModMap.addHeader(Mod, H, HFI.getHeaderRole());
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001611 }
1612 }
1613
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001614 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1615 (void)End;
1616
1617 // This HeaderFileInfo was externally loaded.
1618 HFI.External = true;
1619 return HFI;
1620}
1621
Stephen Hines651f13c2014-04-23 16:59:28 -07001622void
1623ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1624 GlobalMacroID GMacID,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001625 ArrayRef<SubmoduleID> Overrides) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001626 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001627 SubmoduleID *OverrideData = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001628 if (!Overrides.empty()) {
1629 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1630 OverrideData[0] = Overrides.size();
1631 for (unsigned I = 0; I != Overrides.size(); ++I)
1632 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1633 }
1634 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001635}
1636
1637void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1638 ModuleFile *M,
1639 uint64_t MacroDirectivesOffset) {
1640 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1641 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001642}
1643
1644void ASTReader::ReadDefinedMacros() {
1645 // Note that we are loading defined macros.
1646 Deserializing Macros(this);
1647
1648 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1649 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001650 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001651
1652 // If there was no preprocessor block, skip this file.
1653 if (!MacroCursor.getBitStreamReader())
1654 continue;
1655
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001656 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001657 Cursor.JumpToBit((*I)->MacroStartOffset);
1658
1659 RecordData Record;
1660 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001661 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1662
1663 switch (E.Kind) {
1664 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1665 case llvm::BitstreamEntry::Error:
1666 Error("malformed block record in AST file");
1667 return;
1668 case llvm::BitstreamEntry::EndBlock:
1669 goto NextCursor;
1670
1671 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001672 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001673 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001674 default: // Default behavior: ignore.
1675 break;
1676
1677 case PP_MACRO_OBJECT_LIKE:
1678 case PP_MACRO_FUNCTION_LIKE:
1679 getLocalIdentifier(**I, Record[0]);
1680 break;
1681
1682 case PP_TOKEN:
1683 // Ignore tokens.
1684 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001685 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001686 break;
1687 }
1688 }
Chris Lattner88bde502013-01-19 21:39:22 +00001689 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001690 }
1691}
1692
1693namespace {
1694 /// \brief Visitor class used to look up identifirs in an AST file.
1695 class IdentifierLookupVisitor {
1696 StringRef Name;
1697 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001698 unsigned &NumIdentifierLookups;
1699 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001700 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001701
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001702 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001703 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1704 unsigned &NumIdentifierLookups,
1705 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001706 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001707 NumIdentifierLookups(NumIdentifierLookups),
1708 NumIdentifierLookupHits(NumIdentifierLookupHits),
1709 Found()
1710 {
1711 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001712
1713 static bool visit(ModuleFile &M, void *UserData) {
1714 IdentifierLookupVisitor *This
1715 = static_cast<IdentifierLookupVisitor *>(UserData);
1716
1717 // If we've already searched this module file, skip it now.
1718 if (M.Generation <= This->PriorGeneration)
1719 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001720
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001721 ASTIdentifierLookupTable *IdTable
1722 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1723 if (!IdTable)
1724 return false;
1725
1726 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1727 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001728 ++This->NumIdentifierLookups;
1729 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001730 if (Pos == IdTable->end())
1731 return false;
1732
1733 // Dereferencing the iterator has the effect of building the
1734 // IdentifierInfo node and populating it with the various
1735 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001736 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001737 This->Found = *Pos;
1738 return true;
1739 }
1740
1741 // \brief Retrieve the identifier info found within the module
1742 // files.
1743 IdentifierInfo *getIdentifierInfo() const { return Found; }
1744 };
1745}
1746
1747void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1748 // Note that we are loading an identifier.
1749 Deserializing AnIdentifier(this);
1750
1751 unsigned PriorGeneration = 0;
1752 if (getContext().getLangOpts().Modules)
1753 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001754
1755 // If there is a global index, look there first to determine which modules
1756 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001757 GlobalModuleIndex::HitSet Hits;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001758 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001759 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001760 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1761 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001762 }
1763 }
1764
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001765 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001766 NumIdentifierLookups,
1767 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001768 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001769 markIdentifierUpToDate(&II);
1770}
1771
1772void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1773 if (!II)
1774 return;
1775
1776 II->setOutOfDate(false);
1777
1778 // Update the generation for this identifier.
1779 if (getContext().getLangOpts().Modules)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001780 IdentifierGeneration[II] = getGeneration();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001781}
1782
Stephen Hines651f13c2014-04-23 16:59:28 -07001783struct ASTReader::ModuleMacroInfo {
1784 SubmoduleID SubModID;
1785 MacroInfo *MI;
1786 SubmoduleID *Overrides;
1787 // FIXME: Remove this.
1788 ModuleFile *F;
1789
1790 bool isDefine() const { return MI; }
1791
1792 SubmoduleID getSubmoduleID() const { return SubModID; }
1793
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001794 ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001795 if (!Overrides)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001796 return None;
Stephen Hines651f13c2014-04-23 16:59:28 -07001797 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1798 }
1799
Stephen Hines176edba2014-12-01 14:53:08 -08001800 MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001801 if (!MI)
Stephen Hines176edba2014-12-01 14:53:08 -08001802 return PP.AllocateUndefMacroDirective(ImportLoc, SubModID,
1803 getOverriddenSubmodules());
1804 return PP.AllocateDefMacroDirective(MI, ImportLoc, SubModID,
1805 getOverriddenSubmodules());
Stephen Hines651f13c2014-04-23 16:59:28 -07001806 }
1807};
1808
1809ASTReader::ModuleMacroInfo *
1810ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1811 ModuleMacroInfo Info;
1812
1813 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1814 if (ID & 1) {
1815 // Macro undefinition.
1816 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001817 Info.MI = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001818 } else {
1819 // Macro definition.
1820 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1821 assert(GMacID);
1822
1823 // If this macro has already been loaded, don't do so again.
1824 // FIXME: This is highly dubious. Multiple macro definitions can have the
1825 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1826 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001827 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001828
1829 Info.MI = getMacro(GMacID);
1830 Info.SubModID = Info.MI->getOwningModuleID();
1831 }
1832 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1833 Info.F = PMInfo.M;
1834
1835 return new (Context) ModuleMacroInfo(Info);
1836}
1837
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001838void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1839 const PendingMacroInfo &PMInfo) {
1840 assert(II);
1841
Stephen Hines176edba2014-12-01 14:53:08 -08001842 if (PMInfo.M->Kind != MK_ImplicitModule &&
1843 PMInfo.M->Kind != MK_ExplicitModule) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001844 installPCHMacroDirectives(II, *PMInfo.M,
1845 PMInfo.PCHMacroData.MacroDirectivesOffset);
1846 return;
1847 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001848
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001849 // Module Macro.
1850
Stephen Hines651f13c2014-04-23 16:59:28 -07001851 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1852 if (!MMI)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001853 return;
1854
Stephen Hines651f13c2014-04-23 16:59:28 -07001855 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1856 if (Owner && Owner->NameVisibility == Module::Hidden) {
1857 // Macros in the owning module are hidden. Just remember this macro to
1858 // install if we make this module visible.
1859 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1860 } else {
1861 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001862 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001863}
1864
1865void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1866 ModuleFile &M, uint64_t Offset) {
Stephen Hines176edba2014-12-01 14:53:08 -08001867 assert(M.Kind != MK_ImplicitModule && M.Kind != MK_ExplicitModule);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001868
1869 BitstreamCursor &Cursor = M.MacroCursor;
1870 SavedStreamPosition SavedPosition(Cursor);
1871 Cursor.JumpToBit(Offset);
1872
1873 llvm::BitstreamEntry Entry =
1874 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1875 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1876 Error("malformed block record in AST file");
1877 return;
1878 }
1879
1880 RecordData Record;
1881 PreprocessorRecordTypes RecType =
1882 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1883 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1884 Error("malformed block record in AST file");
1885 return;
1886 }
1887
1888 // Deserialize the macro directives history in reverse source-order.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001889 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001890 unsigned Idx = 0, N = Record.size();
1891 while (Idx < N) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001892 MacroDirective *MD = nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001893 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001894 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1895 switch (K) {
1896 case MacroDirective::MD_Define: {
1897 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1898 MacroInfo *MI = getMacro(GMacID);
Stephen Hines176edba2014-12-01 14:53:08 -08001899 SubmoduleID ImportedFrom = Record[Idx++];
1900 bool IsAmbiguous = Record[Idx++];
1901 llvm::SmallVector<unsigned, 4> Overrides;
1902 if (ImportedFrom) {
1903 Overrides.insert(Overrides.end(),
1904 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1905 Idx += Overrides.size() + 1;
1906 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001907 DefMacroDirective *DefMD =
Stephen Hines176edba2014-12-01 14:53:08 -08001908 PP.AllocateDefMacroDirective(MI, Loc, ImportedFrom, Overrides);
1909 DefMD->setAmbiguous(IsAmbiguous);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001910 MD = DefMD;
1911 break;
1912 }
Stephen Hines176edba2014-12-01 14:53:08 -08001913 case MacroDirective::MD_Undefine: {
1914 SubmoduleID ImportedFrom = Record[Idx++];
1915 llvm::SmallVector<unsigned, 4> Overrides;
1916 if (ImportedFrom) {
1917 Overrides.insert(Overrides.end(),
1918 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1919 Idx += Overrides.size() + 1;
1920 }
1921 MD = PP.AllocateUndefMacroDirective(Loc, ImportedFrom, Overrides);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001922 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001923 }
1924 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001925 bool isPublic = Record[Idx++];
1926 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1927 break;
1928 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001929
1930 if (!Latest)
1931 Latest = MD;
1932 if (Earliest)
1933 Earliest->setPrevious(MD);
1934 Earliest = MD;
1935 }
1936
1937 PP.setLoadedMacroDirective(II, Latest);
1938}
1939
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001940/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001941/// modules.
1942static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001943 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001944 assert(PrevMI && NewMI);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001945 Module *PrevOwner = nullptr;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001946 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1947 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001948 SourceManager &SrcMgr = Reader.getSourceManager();
1949 bool PrevInSystem
1950 = PrevOwner? PrevOwner->IsSystem
1951 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1952 bool NewInSystem
1953 = NewOwner? NewOwner->IsSystem
1954 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1955 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001956 return false;
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001957 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001958}
1959
Stephen Hines651f13c2014-04-23 16:59:28 -07001960void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Stephen Hines176edba2014-12-01 14:53:08 -08001961 SourceLocation ImportLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07001962 AmbiguousMacros &Ambig,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001963 ArrayRef<SubmoduleID> Overrides) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001964 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1965 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001966
Stephen Hines651f13c2014-04-23 16:59:28 -07001967 // If this macro is not yet visible, remove it from the hidden names list.
Stephen Hines176edba2014-12-01 14:53:08 -08001968 // It won't be there if we're in the middle of making the owner visible.
Stephen Hines651f13c2014-04-23 16:59:28 -07001969 Module *Owner = getSubmodule(OwnerID);
Stephen Hines176edba2014-12-01 14:53:08 -08001970 auto HiddenIt = HiddenNamesMap.find(Owner);
1971 if (HiddenIt != HiddenNamesMap.end()) {
1972 HiddenNames &Hidden = HiddenIt->second;
1973 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1974 if (HI != Hidden.HiddenMacros.end()) {
1975 // Register the macro now so we don't lose it when we re-export.
1976 PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
1977
1978 auto SubOverrides = HI->second->getOverriddenSubmodules();
1979 Hidden.HiddenMacros.erase(HI);
1980 removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
1981 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001982 }
1983
1984 // If this macro is already in our list of conflicts, remove it from there.
1985 Ambig.erase(
1986 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1987 return MD->getInfo()->getOwningModuleID() == OwnerID;
1988 }),
1989 Ambig.end());
1990 }
1991}
1992
1993ASTReader::AmbiguousMacros *
1994ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Stephen Hines176edba2014-12-01 14:53:08 -08001995 SourceLocation ImportLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001996 ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001997 MacroDirective *Prev = PP.getMacroDirective(II);
Stephen Hines651f13c2014-04-23 16:59:28 -07001998 if (!Prev && Overrides.empty())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001999 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07002000
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002001 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
2002 : nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07002003 if (PrevDef && PrevDef->isAmbiguous()) {
2004 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
2005 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
2006 Ambig.push_back(PrevDef);
2007
Stephen Hines176edba2014-12-01 14:53:08 -08002008 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Stephen Hines651f13c2014-04-23 16:59:28 -07002009
2010 if (!Ambig.empty())
2011 return &Ambig;
2012
2013 AmbiguousMacroDefs.erase(II);
2014 } else {
2015 // There's no ambiguity yet. Maybe we're introducing one.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002016 AmbiguousMacros Ambig;
Stephen Hines651f13c2014-04-23 16:59:28 -07002017 if (PrevDef)
2018 Ambig.push_back(PrevDef);
2019
Stephen Hines176edba2014-12-01 14:53:08 -08002020 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Stephen Hines651f13c2014-04-23 16:59:28 -07002021
2022 if (!Ambig.empty()) {
2023 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002024 std::swap(Result, Ambig);
Stephen Hines651f13c2014-04-23 16:59:28 -07002025 return &Result;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002026 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002027 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002028
2029 // We ended up with no ambiguity.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002030 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07002031}
2032
2033void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
2034 Module *Owner) {
2035 assert(II && Owner);
2036
2037 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
2038 if (ImportLoc.isInvalid()) {
2039 // FIXME: If we made macros from this module visible but didn't provide a
2040 // source location for the import, we don't have a location for the macro.
2041 // Use the location at which the containing module file was first imported
2042 // for now.
2043 ImportLoc = MMI->F->DirectImportLoc;
2044 assert(ImportLoc.isValid() && "no import location for a visible macro?");
2045 }
2046
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002047 AmbiguousMacros *Prev =
Stephen Hines176edba2014-12-01 14:53:08 -08002048 removeOverriddenMacros(II, ImportLoc, MMI->getOverriddenSubmodules());
Stephen Hines651f13c2014-04-23 16:59:28 -07002049
Stephen Hines651f13c2014-04-23 16:59:28 -07002050 // Create a synthetic macro definition corresponding to the import (or null
2051 // if this was an undefinition of the macro).
Stephen Hines176edba2014-12-01 14:53:08 -08002052 MacroDirective *Imported = MMI->import(PP, ImportLoc);
2053 DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
Stephen Hines651f13c2014-04-23 16:59:28 -07002054
2055 // If there's no ambiguity, just install the macro.
2056 if (!Prev) {
Stephen Hines176edba2014-12-01 14:53:08 -08002057 PP.appendMacroDirective(II, Imported);
Stephen Hines651f13c2014-04-23 16:59:28 -07002058 return;
2059 }
2060 assert(!Prev->empty());
2061
2062 if (!MD) {
2063 // We imported a #undef that didn't remove all prior definitions. The most
2064 // recent prior definition remains, and we install it in the place of the
Stephen Hines176edba2014-12-01 14:53:08 -08002065 // imported directive, as if by a local #pragma pop_macro.
Stephen Hines651f13c2014-04-23 16:59:28 -07002066 MacroInfo *NewMI = Prev->back()->getInfo();
2067 Prev->pop_back();
Stephen Hines176edba2014-12-01 14:53:08 -08002068 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
2069
2070 // Install our #undef first so that we don't lose track of it. We'll replace
2071 // this with whichever macro definition ends up winning.
2072 PP.appendMacroDirective(II, Imported);
Stephen Hines651f13c2014-04-23 16:59:28 -07002073 }
2074
2075 // We're introducing a macro definition that creates or adds to an ambiguity.
2076 // We can resolve that ambiguity if this macro is token-for-token identical to
2077 // all of the existing definitions.
2078 MacroInfo *NewMI = MD->getInfo();
2079 assert(NewMI && "macro definition with no MacroInfo?");
2080 while (!Prev->empty()) {
2081 MacroInfo *PrevMI = Prev->back()->getInfo();
2082 assert(PrevMI && "macro definition with no MacroInfo?");
2083
2084 // Before marking the macros as ambiguous, check if this is a case where
2085 // both macros are in system headers. If so, we trust that the system
2086 // did not get it wrong. This also handles cases where Clang's own
2087 // headers have a different spelling of certain system macros:
2088 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
2089 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2090 //
2091 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2092 // overrides the system limits.h's macros, so there's no conflict here.
2093 if (NewMI != PrevMI &&
2094 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2095 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2096 break;
2097
2098 // The previous definition is the same as this one (or both are defined in
2099 // system modules so we can assume they're equivalent); we don't need to
2100 // track it any more.
2101 Prev->pop_back();
2102 }
2103
2104 if (!Prev->empty())
2105 MD->setAmbiguous(true);
2106
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002107 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002108}
2109
Stephen Hines651f13c2014-04-23 16:59:28 -07002110ASTReader::InputFileInfo
2111ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2112 // Go find this input file.
2113 BitstreamCursor &Cursor = F.InputFilesCursor;
2114 SavedStreamPosition SavedPosition(Cursor);
2115 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2116
2117 unsigned Code = Cursor.ReadCode();
2118 RecordData Record;
2119 StringRef Blob;
2120
2121 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2122 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2123 "invalid record type for input file");
2124 (void)Result;
2125
2126 std::string Filename;
2127 off_t StoredSize;
2128 time_t StoredTime;
2129 bool Overridden;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002130
Stephen Hines651f13c2014-04-23 16:59:28 -07002131 assert(Record[0] == ID && "Bogus stored ID or offset");
2132 StoredSize = static_cast<off_t>(Record[1]);
2133 StoredTime = static_cast<time_t>(Record[2]);
2134 Overridden = static_cast<bool>(Record[3]);
2135 Filename = Blob;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002136 ResolveImportedPath(F, Filename);
2137
Stephen Hines651f13c2014-04-23 16:59:28 -07002138 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2139 return R;
2140}
2141
2142std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
2143 return readInputFileInfo(F, ID).Filename;
2144}
2145
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002146InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002147 // If this ID is bogus, just return an empty input file.
2148 if (ID == 0 || ID > F.InputFilesLoaded.size())
2149 return InputFile();
2150
2151 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002152 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002153 return F.InputFilesLoaded[ID-1];
2154
Stephen Hines651f13c2014-04-23 16:59:28 -07002155 if (F.InputFilesLoaded[ID-1].isNotFound())
2156 return InputFile();
2157
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002158 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002159 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002160 SavedStreamPosition SavedPosition(Cursor);
2161 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2162
Stephen Hines651f13c2014-04-23 16:59:28 -07002163 InputFileInfo FI = readInputFileInfo(F, ID);
2164 off_t StoredSize = FI.StoredSize;
2165 time_t StoredTime = FI.StoredTime;
2166 bool Overridden = FI.Overridden;
2167 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002168
Stephen Hines651f13c2014-04-23 16:59:28 -07002169 const FileEntry *File
2170 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2171 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2172
2173 // If we didn't find the file, resolve it relative to the
2174 // original directory from which this AST file was created.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002175 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002176 F.OriginalDir != CurrentDir) {
2177 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2178 F.OriginalDir,
2179 CurrentDir);
2180 if (!Resolved.empty())
2181 File = FileMgr.getFile(Resolved);
2182 }
2183
2184 // For an overridden file, create a virtual file with the stored
2185 // size/timestamp.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002186 if (Overridden && File == nullptr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002187 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2188 }
2189
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002190 if (File == nullptr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002191 if (Complain) {
2192 std::string ErrorStr = "could not find file '";
2193 ErrorStr += Filename;
2194 ErrorStr += "' referenced by AST file";
2195 Error(ErrorStr.c_str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002196 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002197 // Record that we didn't find the file.
2198 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2199 return InputFile();
2200 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002201
Stephen Hines651f13c2014-04-23 16:59:28 -07002202 // Check if there was a request to override the contents of the file
2203 // that was part of the precompiled header. Overridding such a file
2204 // can lead to problems when lexing using the source locations from the
2205 // PCH.
2206 SourceManager &SM = getSourceManager();
2207 if (!Overridden && SM.isFileOverridden(File)) {
2208 if (Complain)
2209 Error(diag::err_fe_pch_file_overridden, Filename);
2210 // After emitting the diagnostic, recover by disabling the override so
2211 // that the original file will be used.
2212 SM.disableFileContentsOverride(File);
2213 // The FileEntry is a virtual file entry with the size of the contents
2214 // that would override the original contents. Set it to the original's
2215 // size/time.
2216 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2217 StoredSize, StoredTime);
2218 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002219
Stephen Hines651f13c2014-04-23 16:59:28 -07002220 bool IsOutOfDate = false;
2221
2222 // For an overridden file, there is nothing to validate.
Stephen Hines176edba2014-12-01 14:53:08 -08002223 if (!Overridden && //
2224 (StoredSize != File->getSize() ||
2225#if defined(LLVM_ON_WIN32)
2226 false
2227#else
Stephen Hines651f13c2014-04-23 16:59:28 -07002228 // In our regression testing, the Windows file system seems to
2229 // have inconsistent modification times that sometimes
2230 // erroneously trigger this error-handling path.
Stephen Hines176edba2014-12-01 14:53:08 -08002231 //
2232 // This also happens in networked file systems, so disable this
2233 // check if validation is disabled or if we have an explicitly
2234 // built PCM file.
2235 //
2236 // FIXME: Should we also do this for PCH files? They could also
2237 // reasonably get shared across a network during a distributed build.
2238 (StoredTime != File->getModificationTime() && !DisableValidation &&
2239 F.Kind != MK_ExplicitModule)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002240#endif
Stephen Hines651f13c2014-04-23 16:59:28 -07002241 )) {
2242 if (Complain) {
2243 // Build a list of the PCH imports that got us here (in reverse).
2244 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2245 while (ImportStack.back()->ImportedBy.size() > 0)
2246 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2247
2248 // The top-level PCH is stale.
2249 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2250 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2251
2252 // Print the import stack.
2253 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2254 Diag(diag::note_pch_required_by)
2255 << Filename << ImportStack[0]->FileName;
2256 for (unsigned I = 1; I < ImportStack.size(); ++I)
2257 Diag(diag::note_pch_required_by)
2258 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor677e15f2013-03-19 00:28:20 +00002259 }
2260
Stephen Hines651f13c2014-04-23 16:59:28 -07002261 if (!Diags.isDiagnosticInFlight())
2262 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002263 }
2264
Stephen Hines651f13c2014-04-23 16:59:28 -07002265 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002266 }
2267
Stephen Hines651f13c2014-04-23 16:59:28 -07002268 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2269
2270 // Note that we've loaded this input file.
2271 F.InputFilesLoaded[ID-1] = IF;
2272 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002273}
2274
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002275/// \brief If we are loading a relocatable PCH or module file, and the filename
2276/// is not an absolute path, add the system or module root to the beginning of
2277/// the file name.
2278void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2279 // Resolve relative to the base directory, if we have one.
2280 if (!M.BaseDirectory.empty())
2281 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002282}
2283
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002284void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002285 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2286 return;
2287
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002288 SmallString<128> Buffer;
2289 llvm::sys::path::append(Buffer, Prefix, Filename);
2290 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002291}
2292
2293ASTReader::ASTReadResult
2294ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002295 SmallVectorImpl<ImportedModule> &Loaded,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002296 const ModuleFile *ImportedBy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002297 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002298 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002299
2300 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2301 Error("malformed block record in AST file");
2302 return Failure;
2303 }
2304
Stephen Hines176edba2014-12-01 14:53:08 -08002305 // Should we allow the configuration of the module file to differ from the
2306 // configuration of the current translation unit in a compatible way?
2307 //
2308 // FIXME: Allow this for files explicitly specified with -include-pch too.
2309 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2310
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002311 // Read all of the records and blocks in the control block.
2312 RecordData Record;
Stephen Hines176edba2014-12-01 14:53:08 -08002313 unsigned NumInputs = 0;
2314 unsigned NumUserInputs = 0;
Chris Lattner88bde502013-01-19 21:39:22 +00002315 while (1) {
2316 llvm::BitstreamEntry Entry = Stream.advance();
2317
2318 switch (Entry.Kind) {
2319 case llvm::BitstreamEntry::Error:
2320 Error("malformed block record in AST file");
2321 return Failure;
Stephen Hines651f13c2014-04-23 16:59:28 -07002322 case llvm::BitstreamEntry::EndBlock: {
2323 // Validate input files.
2324 const HeaderSearchOptions &HSOpts =
2325 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2326
Stephen Hines176edba2014-12-01 14:53:08 -08002327 // All user input files reside at the index range [0, NumUserInputs), and
2328 // system input files reside at [NumUserInputs, NumInputs).
2329 if (!DisableValidation) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002330 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002331
2332 // If we are reading a module, we will create a verification timestamp,
2333 // so we verify all input files. Otherwise, verify only user input
2334 // files.
2335
2336 unsigned N = NumUserInputs;
2337 if (ValidateSystemInputs ||
Stephen Hines176edba2014-12-01 14:53:08 -08002338 (HSOpts.ModulesValidateOncePerBuildSession &&
2339 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2340 F.Kind == MK_ImplicitModule))
Stephen Hines651f13c2014-04-23 16:59:28 -07002341 N = NumInputs;
2342
2343 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002344 InputFile IF = getInputFile(F, I+1, Complain);
2345 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002346 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002347 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002348 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002349
2350 if (Listener)
2351 Listener->visitModuleFile(F.FileName);
2352
2353 if (Listener && Listener->needsInputFileVisitation()) {
2354 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2355 : NumUserInputs;
2356 for (unsigned I = 0; I < N; ++I) {
2357 bool IsSystem = I >= NumUserInputs;
2358 InputFileInfo FI = readInputFileInfo(F, I+1);
2359 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2360 }
2361 }
2362
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002363 return Success;
Stephen Hines651f13c2014-04-23 16:59:28 -07002364 }
2365
Chris Lattner88bde502013-01-19 21:39:22 +00002366 case llvm::BitstreamEntry::SubBlock:
2367 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002368 case INPUT_FILES_BLOCK_ID:
2369 F.InputFilesCursor = Stream;
2370 if (Stream.SkipBlock() || // Skip with the main cursor
2371 // Read the abbreviations
2372 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2373 Error("malformed block record in AST file");
2374 return Failure;
2375 }
2376 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002377
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002378 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002379 if (Stream.SkipBlock()) {
2380 Error("malformed block record in AST file");
2381 return Failure;
2382 }
2383 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002384 }
Chris Lattner88bde502013-01-19 21:39:22 +00002385
2386 case llvm::BitstreamEntry::Record:
2387 // The interesting case.
2388 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002389 }
2390
2391 // Read and process a record.
2392 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002393 StringRef Blob;
2394 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002395 case METADATA: {
2396 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2397 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07002398 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2399 : diag::err_pch_version_too_new);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002400 return VersionMismatch;
2401 }
2402
2403 bool hasErrors = Record[5];
2404 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2405 Diag(diag::err_pch_with_compiler_errors);
2406 return HadErrors;
2407 }
2408
2409 F.RelocatablePCH = Record[4];
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002410 // Relative paths in a relocatable PCH are relative to our sysroot.
2411 if (F.RelocatablePCH)
2412 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002413
2414 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002415 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002416 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2417 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07002418 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002419 return VersionMismatch;
2420 }
2421 break;
2422 }
2423
Stephen Hines176edba2014-12-01 14:53:08 -08002424 case SIGNATURE:
2425 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2426 F.Signature = Record[0];
2427 break;
2428
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002429 case IMPORTS: {
2430 // Load each of the imported PCH files.
2431 unsigned Idx = 0, N = Record.size();
2432 while (Idx < N) {
2433 // Read information about the AST file.
2434 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2435 // The import location will be the local one for now; we will adjust
2436 // all import locations of module imports after the global source
2437 // location info are setup.
2438 SourceLocation ImportLoc =
2439 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00002440 off_t StoredSize = (off_t)Record[Idx++];
2441 time_t StoredModTime = (time_t)Record[Idx++];
Stephen Hines176edba2014-12-01 14:53:08 -08002442 ASTFileSignature StoredSignature = Record[Idx++];
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002443 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002444
2445 // Load the AST file.
2446 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Stephen Hines176edba2014-12-01 14:53:08 -08002447 StoredSize, StoredModTime, StoredSignature,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002448 ClientLoadCapabilities)) {
2449 case Failure: return Failure;
2450 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00002451 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002452 case OutOfDate: return OutOfDate;
2453 case VersionMismatch: return VersionMismatch;
2454 case ConfigurationMismatch: return ConfigurationMismatch;
2455 case HadErrors: return HadErrors;
2456 case Success: break;
2457 }
2458 }
2459 break;
2460 }
2461
2462 case LANGUAGE_OPTIONS: {
2463 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
Stephen Hines176edba2014-12-01 14:53:08 -08002464 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002465 if (Listener && &F == *ModuleMgr.begin() &&
Stephen Hines176edba2014-12-01 14:53:08 -08002466 ParseLanguageOptions(Record, Complain, *Listener,
2467 AllowCompatibleConfigurationMismatch) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002468 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002469 return ConfigurationMismatch;
2470 break;
2471 }
2472
2473 case TARGET_OPTIONS: {
2474 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2475 if (Listener && &F == *ModuleMgr.begin() &&
2476 ParseTargetOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002477 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002478 return ConfigurationMismatch;
2479 break;
2480 }
2481
2482 case DIAGNOSTIC_OPTIONS: {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002483 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002484 if (Listener && &F == *ModuleMgr.begin() &&
Stephen Hines176edba2014-12-01 14:53:08 -08002485 !AllowCompatibleConfigurationMismatch &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002486 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002487 !DisableValidation)
2488 return OutOfDate;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002489 break;
2490 }
2491
2492 case FILE_SYSTEM_OPTIONS: {
2493 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2494 if (Listener && &F == *ModuleMgr.begin() &&
Stephen Hines176edba2014-12-01 14:53:08 -08002495 !AllowCompatibleConfigurationMismatch &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002496 ParseFileSystemOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002497 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002498 return ConfigurationMismatch;
2499 break;
2500 }
2501
2502 case HEADER_SEARCH_OPTIONS: {
2503 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2504 if (Listener && &F == *ModuleMgr.begin() &&
Stephen Hines176edba2014-12-01 14:53:08 -08002505 !AllowCompatibleConfigurationMismatch &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002506 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002507 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002508 return ConfigurationMismatch;
2509 break;
2510 }
2511
2512 case PREPROCESSOR_OPTIONS: {
2513 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2514 if (Listener && &F == *ModuleMgr.begin() &&
Stephen Hines176edba2014-12-01 14:53:08 -08002515 !AllowCompatibleConfigurationMismatch &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002516 ParsePreprocessorOptions(Record, Complain, *Listener,
2517 SuggestedPredefines) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002518 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002519 return ConfigurationMismatch;
2520 break;
2521 }
2522
2523 case ORIGINAL_FILE:
2524 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002525 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002526 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002527 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002528 break;
2529
2530 case ORIGINAL_FILE_ID:
2531 F.OriginalSourceFileID = FileID::get(Record[0]);
2532 break;
2533
2534 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002535 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002536 break;
2537
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002538 case MODULE_NAME:
2539 F.ModuleName = Blob;
2540 if (Listener)
2541 Listener->ReadModuleName(F.ModuleName);
2542 break;
2543
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002544 case MODULE_DIRECTORY: {
2545 assert(!F.ModuleName.empty() &&
2546 "MODULE_DIRECTORY found before MODULE_NAME");
2547 // If we've already loaded a module map file covering this module, we may
2548 // have a better path for it (relative to the current build).
2549 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2550 if (M && M->Directory) {
2551 // If we're implicitly loading a module, the base directory can't
2552 // change between the build and use.
2553 if (F.Kind != MK_ExplicitModule) {
2554 const DirectoryEntry *BuildDir =
2555 PP.getFileManager().getDirectory(Blob);
2556 if (!BuildDir || BuildDir != M->Directory) {
2557 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2558 Diag(diag::err_imported_module_relocated)
2559 << F.ModuleName << Blob << M->Directory->getName();
2560 return OutOfDate;
2561 }
2562 }
2563 F.BaseDirectory = M->Directory->getName();
2564 } else {
2565 F.BaseDirectory = Blob;
2566 }
2567 break;
2568 }
2569
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002570 case MODULE_MAP_FILE:
Stephen Hines176edba2014-12-01 14:53:08 -08002571 if (ASTReadResult Result =
2572 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2573 return Result;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002574 break;
2575
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002576 case INPUT_FILE_OFFSETS:
Stephen Hines176edba2014-12-01 14:53:08 -08002577 NumInputs = Record[0];
2578 NumUserInputs = Record[1];
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002579 F.InputFileOffsets = (const uint64_t *)Blob.data();
Stephen Hines176edba2014-12-01 14:53:08 -08002580 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002581 break;
2582 }
2583 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002584}
2585
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002586ASTReader::ASTReadResult
2587ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002588 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002589
2590 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2591 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002592 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002593 }
2594
2595 // Read all of the records and blocks for the AST file.
2596 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00002597 while (1) {
2598 llvm::BitstreamEntry Entry = Stream.advance();
2599
2600 switch (Entry.Kind) {
2601 case llvm::BitstreamEntry::Error:
2602 Error("error at end of module block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002603 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002604 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00002605 // Outside of C++, we do not store a lookup map for the translation unit.
2606 // Instead, mark it as needing a lookup map to be built if this module
2607 // contains any declarations lexically within it (which it always does!).
2608 // This usually has no cost, since we very rarely need the lookup map for
2609 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002610 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00002611 if (DC->hasExternalLexicalStorage() &&
2612 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002613 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00002614
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002615 return Success;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002616 }
Chris Lattner88bde502013-01-19 21:39:22 +00002617 case llvm::BitstreamEntry::SubBlock:
2618 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002619 case DECLTYPES_BLOCK_ID:
2620 // We lazily load the decls block, but we want to set up the
2621 // DeclsCursor cursor to point into it. Clone our current bitcode
2622 // cursor to it, enter the block and read the abbrevs in that block.
2623 // With the main cursor, we just skip over it.
2624 F.DeclsCursor = Stream;
2625 if (Stream.SkipBlock() || // Skip with the main cursor.
2626 // Read the abbrevs.
2627 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2628 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002629 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002630 }
2631 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07002632
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002633 case PREPROCESSOR_BLOCK_ID:
2634 F.MacroCursor = Stream;
2635 if (!PP.getExternalSource())
2636 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002637
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002638 if (Stream.SkipBlock() ||
2639 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2640 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002641 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002642 }
2643 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2644 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002645
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002646 case PREPROCESSOR_DETAIL_BLOCK_ID:
2647 F.PreprocessorDetailCursor = Stream;
2648 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002649 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002650 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002651 Error("malformed preprocessor detail record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002652 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002653 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002654 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002655 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2656
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002657 if (!PP.getPreprocessingRecord())
2658 PP.createPreprocessingRecord();
2659 if (!PP.getPreprocessingRecord()->getExternalSource())
2660 PP.getPreprocessingRecord()->SetExternalSource(*this);
2661 break;
2662
2663 case SOURCE_MANAGER_BLOCK_ID:
2664 if (ReadSourceManagerBlock(F))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002665 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002666 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002667
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002668 case SUBMODULE_BLOCK_ID:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002669 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2670 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002671 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002672
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002673 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002674 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002675 if (Stream.SkipBlock() ||
2676 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2677 Error("malformed comments block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002678 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002679 }
2680 CommentsCursors.push_back(std::make_pair(C, &F));
2681 break;
2682 }
Chris Lattner88bde502013-01-19 21:39:22 +00002683
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002684 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002685 if (Stream.SkipBlock()) {
2686 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002687 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002688 }
2689 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002690 }
2691 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002692
2693 case llvm::BitstreamEntry::Record:
2694 // The interesting case.
2695 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002696 }
2697
2698 // Read and process a record.
2699 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002700 StringRef Blob;
2701 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002702 default: // Default behavior: ignore.
2703 break;
2704
2705 case TYPE_OFFSET: {
2706 if (F.LocalNumTypes != 0) {
2707 Error("duplicate TYPE_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002708 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002709 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002710 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002711 F.LocalNumTypes = Record[0];
2712 unsigned LocalBaseTypeIndex = Record[1];
2713 F.BaseTypeIndex = getTotalNumTypes();
2714
2715 if (F.LocalNumTypes > 0) {
2716 // Introduce the global -> local mapping for types within this module.
2717 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2718
2719 // Introduce the local -> global mapping for types within this module.
2720 F.TypeRemap.insertOrReplace(
2721 std::make_pair(LocalBaseTypeIndex,
2722 F.BaseTypeIndex - LocalBaseTypeIndex));
Stephen Hines176edba2014-12-01 14:53:08 -08002723
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002724 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2725 }
2726 break;
2727 }
2728
2729 case DECL_OFFSET: {
2730 if (F.LocalNumDecls != 0) {
2731 Error("duplicate DECL_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002732 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002733 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002734 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002735 F.LocalNumDecls = Record[0];
2736 unsigned LocalBaseDeclID = Record[1];
2737 F.BaseDeclID = getTotalNumDecls();
2738
2739 if (F.LocalNumDecls > 0) {
2740 // Introduce the global -> local mapping for declarations within this
2741 // module.
2742 GlobalDeclMap.insert(
2743 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2744
2745 // Introduce the local -> global mapping for declarations within this
2746 // module.
2747 F.DeclRemap.insertOrReplace(
2748 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2749
2750 // Introduce the global -> local mapping for declarations within this
2751 // module.
2752 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Stephen Hines176edba2014-12-01 14:53:08 -08002753
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002754 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2755 }
2756 break;
2757 }
2758
2759 case TU_UPDATE_LEXICAL: {
2760 DeclContext *TU = Context.getTranslationUnitDecl();
2761 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002762 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002763 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002764 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002765 TU->setHasExternalLexicalStorage(true);
2766 break;
2767 }
2768
2769 case UPDATE_VISIBLE: {
2770 unsigned Idx = 0;
2771 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2772 ASTDeclContextNameLookupTable *Table =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002773 ASTDeclContextNameLookupTable::Create(
2774 (const unsigned char *)Blob.data() + Record[Idx++],
2775 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2776 (const unsigned char *)Blob.data(),
2777 ASTDeclContextNameLookupTrait(*this, F));
2778 if (Decl *D = GetExistingDecl(ID)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002779 auto *DC = cast<DeclContext>(D);
2780 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2781 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2782 delete LookupTable;
2783 LookupTable = Table;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002784 } else
2785 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2786 break;
2787 }
2788
2789 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002790 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002791 if (Record[0]) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002792 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2793 (const unsigned char *)F.IdentifierTableData + Record[0],
2794 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2795 (const unsigned char *)F.IdentifierTableData,
2796 ASTIdentifierLookupTrait(*this, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002797
2798 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2799 }
2800 break;
2801
2802 case IDENTIFIER_OFFSET: {
2803 if (F.LocalNumIdentifiers != 0) {
2804 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002805 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002806 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002807 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002808 F.LocalNumIdentifiers = Record[0];
2809 unsigned LocalBaseIdentifierID = Record[1];
2810 F.BaseIdentifierID = getTotalNumIdentifiers();
2811
2812 if (F.LocalNumIdentifiers > 0) {
2813 // Introduce the global -> local mapping for identifiers within this
2814 // module.
2815 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2816 &F));
2817
2818 // Introduce the local -> global mapping for identifiers within this
2819 // module.
2820 F.IdentifierRemap.insertOrReplace(
2821 std::make_pair(LocalBaseIdentifierID,
2822 F.BaseIdentifierID - LocalBaseIdentifierID));
Stephen Hines176edba2014-12-01 14:53:08 -08002823
2824 IdentifiersLoaded.resize(IdentifiersLoaded.size()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002825 + F.LocalNumIdentifiers);
2826 }
2827 break;
2828 }
2829
Stephen Hines651f13c2014-04-23 16:59:28 -07002830 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002831 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002832 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002833 break;
2834
2835 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002836 if (SpecialTypes.empty()) {
2837 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2838 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2839 break;
2840 }
2841
2842 if (SpecialTypes.size() != Record.size()) {
2843 Error("invalid special-types record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002844 return Failure;
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002845 }
2846
2847 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2848 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2849 if (!SpecialTypes[I])
2850 SpecialTypes[I] = ID;
2851 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2852 // merge step?
2853 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002854 break;
2855
2856 case STATISTICS:
2857 TotalNumStatements += Record[0];
2858 TotalNumMacros += Record[1];
2859 TotalLexicalDeclContexts += Record[2];
2860 TotalVisibleDeclContexts += Record[3];
2861 break;
2862
2863 case UNUSED_FILESCOPED_DECLS:
2864 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2865 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2866 break;
2867
2868 case DELEGATING_CTORS:
2869 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2870 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2871 break;
2872
2873 case WEAK_UNDECLARED_IDENTIFIERS:
2874 if (Record.size() % 4 != 0) {
2875 Error("invalid weak identifiers record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002876 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002877 }
2878
2879 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2880 // files. This isn't the way to do it :)
2881 WeakUndeclaredIdentifiers.clear();
2882
2883 // Translate the weak, undeclared identifiers into global IDs.
2884 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2885 WeakUndeclaredIdentifiers.push_back(
2886 getGlobalIdentifierID(F, Record[I++]));
2887 WeakUndeclaredIdentifiers.push_back(
2888 getGlobalIdentifierID(F, Record[I++]));
2889 WeakUndeclaredIdentifiers.push_back(
2890 ReadSourceLocation(F, Record, I).getRawEncoding());
2891 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2892 }
2893 break;
2894
Richard Smith5ea6ef42013-01-10 23:43:47 +00002895 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002896 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002897 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002898 break;
2899
2900 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002901 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002902 F.LocalNumSelectors = Record[0];
2903 unsigned LocalBaseSelectorID = Record[1];
2904 F.BaseSelectorID = getTotalNumSelectors();
2905
2906 if (F.LocalNumSelectors > 0) {
2907 // Introduce the global -> local mapping for selectors within this
2908 // module.
2909 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2910
2911 // Introduce the local -> global mapping for selectors within this
2912 // module.
2913 F.SelectorRemap.insertOrReplace(
2914 std::make_pair(LocalBaseSelectorID,
2915 F.BaseSelectorID - LocalBaseSelectorID));
2916
Stephen Hines176edba2014-12-01 14:53:08 -08002917 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002918 }
2919 break;
2920 }
2921
2922 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002923 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002924 if (Record[0])
2925 F.SelectorLookupTable
2926 = ASTSelectorLookupTable::Create(
2927 F.SelectorLookupTableData + Record[0],
2928 F.SelectorLookupTableData,
2929 ASTSelectorLookupTrait(*this, F));
2930 TotalNumMethodPoolEntries += Record[1];
2931 break;
2932
2933 case REFERENCED_SELECTOR_POOL:
2934 if (!Record.empty()) {
2935 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2936 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2937 Record[Idx++]));
2938 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2939 getRawEncoding());
2940 }
2941 }
2942 break;
2943
2944 case PP_COUNTER_VALUE:
2945 if (!Record.empty() && Listener)
2946 Listener->ReadCounter(F, Record[0]);
2947 break;
2948
2949 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002950 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002951 F.NumFileSortedDecls = Record[0];
2952 break;
2953
2954 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002955 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002956 F.LocalNumSLocEntries = Record[0];
2957 unsigned SLocSpaceSize = Record[1];
Stephen Hines651f13c2014-04-23 16:59:28 -07002958 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002959 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2960 SLocSpaceSize);
2961 // Make our entry in the range map. BaseID is negative and growing, so
2962 // we invert it. Because we invert it, though, we need the other end of
2963 // the range.
2964 unsigned RangeStart =
2965 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2966 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2967 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2968
2969 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2970 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2971 GlobalSLocOffsetMap.insert(
2972 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2973 - SLocSpaceSize,&F));
2974
2975 // Initialize the remapping table.
2976 // Invalid stays invalid.
Stephen Hines651f13c2014-04-23 16:59:28 -07002977 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002978 // This module. Base was 2 when being compiled.
Stephen Hines651f13c2014-04-23 16:59:28 -07002979 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002980 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2981
2982 TotalNumSLocEntries += F.LocalNumSLocEntries;
2983 break;
2984 }
2985
2986 case MODULE_OFFSET_MAP: {
2987 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002988 const unsigned char *Data = (const unsigned char*)Blob.data();
2989 const unsigned char *DataEnd = Data + Blob.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07002990
2991 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2992 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2993 F.SLocRemap.insert(std::make_pair(0U, 0));
2994 F.SLocRemap.insert(std::make_pair(2U, 1));
2995 }
2996
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002997 // Continuous range maps we may be updating in our module.
Stephen Hines176edba2014-12-01 14:53:08 -08002998 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2999 RemapBuilder;
3000 RemapBuilder SLocRemap(F.SLocRemap);
3001 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3002 RemapBuilder MacroRemap(F.MacroRemap);
3003 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3004 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3005 RemapBuilder SelectorRemap(F.SelectorRemap);
3006 RemapBuilder DeclRemap(F.DeclRemap);
3007 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003008
3009 while(Data < DataEnd) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003010 using namespace llvm::support;
3011 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003012 StringRef Name = StringRef((const char*)Data, Len);
3013 Data += Len;
3014 ModuleFile *OM = ModuleMgr.lookup(Name);
3015 if (!OM) {
3016 Error("SourceLocation remap refers to unknown module");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003017 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003018 }
3019
Stephen Hines651f13c2014-04-23 16:59:28 -07003020 uint32_t SLocOffset =
3021 endian::readNext<uint32_t, little, unaligned>(Data);
3022 uint32_t IdentifierIDOffset =
3023 endian::readNext<uint32_t, little, unaligned>(Data);
3024 uint32_t MacroIDOffset =
3025 endian::readNext<uint32_t, little, unaligned>(Data);
3026 uint32_t PreprocessedEntityIDOffset =
3027 endian::readNext<uint32_t, little, unaligned>(Data);
3028 uint32_t SubmoduleIDOffset =
3029 endian::readNext<uint32_t, little, unaligned>(Data);
3030 uint32_t SelectorIDOffset =
3031 endian::readNext<uint32_t, little, unaligned>(Data);
3032 uint32_t DeclIDOffset =
3033 endian::readNext<uint32_t, little, unaligned>(Data);
3034 uint32_t TypeIndexOffset =
3035 endian::readNext<uint32_t, little, unaligned>(Data);
3036
Stephen Hines176edba2014-12-01 14:53:08 -08003037 uint32_t None = std::numeric_limits<uint32_t>::max();
3038
3039 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3040 RemapBuilder &Remap) {
3041 if (Offset != None)
3042 Remap.insert(std::make_pair(Offset,
3043 static_cast<int>(BaseOffset - Offset)));
3044 };
3045 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3046 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3047 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3048 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3049 PreprocessedEntityRemap);
3050 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3051 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3052 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3053 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003054
3055 // Global -> local mappings.
3056 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3057 }
3058 break;
3059 }
3060
3061 case SOURCE_MANAGER_LINE_TABLE:
3062 if (ParseLineTable(F, Record))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003063 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003064 break;
3065
3066 case SOURCE_LOCATION_PRELOADS: {
3067 // Need to transform from the local view (1-based IDs) to the global view,
3068 // which is based off F.SLocEntryBaseID.
3069 if (!F.PreloadSLocEntries.empty()) {
3070 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003071 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003072 }
3073
3074 F.PreloadSLocEntries.swap(Record);
3075 break;
3076 }
3077
3078 case EXT_VECTOR_DECLS:
3079 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3080 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3081 break;
3082
3083 case VTABLE_USES:
3084 if (Record.size() % 3 != 0) {
3085 Error("Invalid VTABLE_USES record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003086 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003087 }
3088
3089 // Later tables overwrite earlier ones.
3090 // FIXME: Modules will have some trouble with this. This is clearly not
3091 // the right way to do this.
3092 VTableUses.clear();
3093
3094 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3095 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3096 VTableUses.push_back(
3097 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3098 VTableUses.push_back(Record[Idx++]);
3099 }
3100 break;
3101
3102 case DYNAMIC_CLASSES:
3103 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3104 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3105 break;
3106
3107 case PENDING_IMPLICIT_INSTANTIATIONS:
3108 if (PendingInstantiations.size() % 2 != 0) {
3109 Error("Invalid existing PendingInstantiations");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003110 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003111 }
3112
3113 if (Record.size() % 2 != 0) {
3114 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003115 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003116 }
3117
3118 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3119 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3120 PendingInstantiations.push_back(
3121 ReadSourceLocation(F, Record, I).getRawEncoding());
3122 }
3123 break;
3124
3125 case SEMA_DECL_REFS:
Richard Smith9b671182013-10-18 06:54:39 +00003126 if (Record.size() != 2) {
3127 Error("Invalid SEMA_DECL_REFS block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003128 return Failure;
Richard Smith9b671182013-10-18 06:54:39 +00003129 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003130 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3131 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3132 break;
3133
3134 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003135 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3136 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3137 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003138
3139 unsigned LocalBasePreprocessedEntityID = Record[0];
3140
3141 unsigned StartingID;
3142 if (!PP.getPreprocessingRecord())
3143 PP.createPreprocessingRecord();
3144 if (!PP.getPreprocessingRecord()->getExternalSource())
3145 PP.getPreprocessingRecord()->SetExternalSource(*this);
3146 StartingID
3147 = PP.getPreprocessingRecord()
3148 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3149 F.BasePreprocessedEntityID = StartingID;
3150
3151 if (F.NumPreprocessedEntities > 0) {
3152 // Introduce the global -> local mapping for preprocessed entities in
3153 // this module.
3154 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3155
3156 // Introduce the local -> global mapping for preprocessed entities in
3157 // this module.
3158 F.PreprocessedEntityRemap.insertOrReplace(
3159 std::make_pair(LocalBasePreprocessedEntityID,
3160 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3161 }
3162
3163 break;
3164 }
3165
3166 case DECL_UPDATE_OFFSETS: {
3167 if (Record.size() % 2 != 0) {
3168 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003169 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003170 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003171 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3172 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3173 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3174
3175 // If we've already loaded the decl, perform the updates when we finish
3176 // loading this block.
3177 if (Decl *D = GetExistingDecl(ID))
3178 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3179 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003180 break;
3181 }
3182
3183 case DECL_REPLACEMENTS: {
3184 if (Record.size() % 3 != 0) {
3185 Error("invalid DECL_REPLACEMENTS block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003186 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003187 }
3188 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3189 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3190 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3191 break;
3192 }
3193
3194 case OBJC_CATEGORIES_MAP: {
3195 if (F.LocalNumObjCCategoriesInMap != 0) {
3196 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003197 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003198 }
3199
3200 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003201 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003202 break;
3203 }
3204
3205 case OBJC_CATEGORIES:
3206 F.ObjCCategories.swap(Record);
3207 break;
3208
3209 case CXX_BASE_SPECIFIER_OFFSETS: {
3210 if (F.LocalNumCXXBaseSpecifiers != 0) {
3211 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003212 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003213 }
3214
3215 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003216 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003217 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3218 break;
3219 }
3220
3221 case DIAG_PRAGMA_MAPPINGS:
3222 if (F.PragmaDiagMappings.empty())
3223 F.PragmaDiagMappings.swap(Record);
3224 else
3225 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3226 Record.begin(), Record.end());
3227 break;
3228
3229 case CUDA_SPECIAL_DECL_REFS:
3230 // Later tables overwrite earlier ones.
3231 // FIXME: Modules will have trouble with this.
3232 CUDASpecialDeclRefs.clear();
3233 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3234 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3235 break;
3236
3237 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003238 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003239 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003240 if (Record[0]) {
3241 F.HeaderFileInfoTable
3242 = HeaderFileInfoLookupTable::Create(
3243 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3244 (const unsigned char *)F.HeaderFileInfoTableData,
3245 HeaderFileInfoTrait(*this, F,
3246 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003247 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003248
3249 PP.getHeaderSearchInfo().SetExternalSource(this);
3250 if (!PP.getHeaderSearchInfo().getExternalLookup())
3251 PP.getHeaderSearchInfo().SetExternalLookup(this);
3252 }
3253 break;
3254 }
3255
3256 case FP_PRAGMA_OPTIONS:
3257 // Later tables overwrite earlier ones.
3258 FPPragmaOptions.swap(Record);
3259 break;
3260
3261 case OPENCL_EXTENSIONS:
3262 // Later tables overwrite earlier ones.
3263 OpenCLExtensions.swap(Record);
3264 break;
3265
3266 case TENTATIVE_DEFINITIONS:
3267 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3268 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3269 break;
3270
3271 case KNOWN_NAMESPACES:
3272 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3273 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3274 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00003275
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003276 case UNDEFINED_BUT_USED:
3277 if (UndefinedButUsed.size() % 2 != 0) {
3278 Error("Invalid existing UndefinedButUsed");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003279 return Failure;
Nick Lewycky01a41142013-01-26 00:35:08 +00003280 }
3281
3282 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003283 Error("invalid undefined-but-used record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003284 return Failure;
Nick Lewycky01a41142013-01-26 00:35:08 +00003285 }
3286 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003287 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3288 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00003289 ReadSourceLocation(F, Record, I).getRawEncoding());
3290 }
3291 break;
3292
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003293 case IMPORTED_MODULES: {
Stephen Hines176edba2014-12-01 14:53:08 -08003294 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003295 // If we aren't loading a module (which has its own exports), make
3296 // all of the imported modules visible.
3297 // FIXME: Deal with macros-only imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07003298 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3299 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3300 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3301 if (GlobalID)
3302 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003303 }
3304 }
3305 break;
3306 }
3307
3308 case LOCAL_REDECLARATIONS: {
3309 F.RedeclarationChains.swap(Record);
3310 break;
3311 }
3312
3313 case LOCAL_REDECLARATIONS_MAP: {
3314 if (F.LocalNumRedeclarationsInMap != 0) {
3315 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003316 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003317 }
3318
3319 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003320 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003321 break;
3322 }
3323
3324 case MERGED_DECLARATIONS: {
3325 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3326 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3327 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3328 for (unsigned N = Record[Idx++]; N > 0; --N)
3329 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3330 }
3331 break;
3332 }
3333
3334 case MACRO_OFFSET: {
3335 if (F.LocalNumMacros != 0) {
3336 Error("duplicate MACRO_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003337 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003338 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003339 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003340 F.LocalNumMacros = Record[0];
3341 unsigned LocalBaseMacroID = Record[1];
3342 F.BaseMacroID = getTotalNumMacros();
3343
3344 if (F.LocalNumMacros > 0) {
3345 // Introduce the global -> local mapping for macros within this module.
3346 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3347
3348 // Introduce the local -> global mapping for macros within this module.
3349 F.MacroRemap.insertOrReplace(
3350 std::make_pair(LocalBaseMacroID,
3351 F.BaseMacroID - LocalBaseMacroID));
3352
3353 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3354 }
3355 break;
3356 }
3357
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003358 case MACRO_TABLE: {
3359 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003360 break;
3361 }
Richard Smithac32d902013-08-07 21:41:30 +00003362
3363 case LATE_PARSED_TEMPLATE: {
3364 LateParsedTemplates.append(Record.begin(), Record.end());
3365 break;
3366 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003367
3368 case OPTIMIZE_PRAGMA_OPTIONS:
3369 if (Record.size() != 1) {
3370 Error("invalid pragma optimize record");
3371 return Failure;
3372 }
3373 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3374 break;
Stephen Hines176edba2014-12-01 14:53:08 -08003375
3376 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3377 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3378 UnusedLocalTypedefNameCandidates.push_back(
3379 getGlobalDeclID(F, Record[I]));
3380 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003381 }
3382 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003383}
3384
Stephen Hines176edba2014-12-01 14:53:08 -08003385ASTReader::ASTReadResult
3386ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3387 const ModuleFile *ImportedBy,
3388 unsigned ClientLoadCapabilities) {
3389 unsigned Idx = 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003390 F.ModuleMapPath = ReadPath(F, Record, Idx);
Stephen Hines176edba2014-12-01 14:53:08 -08003391
3392 if (F.Kind == MK_ExplicitModule) {
3393 // For an explicitly-loaded module, we don't care whether the original
3394 // module map file exists or matches.
3395 return Success;
3396 }
3397
3398 // Try to resolve ModuleName in the current header search context and
3399 // verify that it is found in the same module map file as we saved. If the
3400 // top-level AST file is a main file, skip this check because there is no
3401 // usable header search context.
3402 assert(!F.ModuleName.empty() &&
3403 "MODULE_NAME should come before MODULE_MAP_FILE");
3404 if (F.Kind == MK_ImplicitModule &&
3405 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3406 // An implicitly-loaded module file should have its module listed in some
3407 // module map file that we've already loaded.
3408 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3409 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3410 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3411 if (!ModMap) {
3412 assert(ImportedBy && "top-level import should be verified");
3413 if ((ClientLoadCapabilities & ARR_Missing) == 0)
3414 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3415 << ImportedBy->FileName
3416 << F.ModuleMapPath;
3417 return Missing;
3418 }
3419
3420 assert(M->Name == F.ModuleName && "found module with different name");
3421
3422 // Check the primary module map file.
3423 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3424 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3425 assert(ModMap && "found module is missing module map file");
3426 assert(ImportedBy && "top-level import should be verified");
3427 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3428 Diag(diag::err_imported_module_modmap_changed)
3429 << F.ModuleName << ImportedBy->FileName
3430 << ModMap->getName() << F.ModuleMapPath;
3431 return OutOfDate;
3432 }
3433
3434 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3435 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3436 // FIXME: we should use input files rather than storing names.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003437 std::string Filename = ReadPath(F, Record, Idx);
Stephen Hines176edba2014-12-01 14:53:08 -08003438 const FileEntry *F =
3439 FileMgr.getFile(Filename, false, false);
3440 if (F == nullptr) {
3441 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3442 Error("could not find file '" + Filename +"' referenced by AST file");
3443 return OutOfDate;
3444 }
3445 AdditionalStoredMaps.insert(F);
3446 }
3447
3448 // Check any additional module map files (e.g. module.private.modulemap)
3449 // that are not in the pcm.
3450 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3451 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3452 // Remove files that match
3453 // Note: SmallPtrSet::erase is really remove
3454 if (!AdditionalStoredMaps.erase(ModMap)) {
3455 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3456 Diag(diag::err_module_different_modmap)
3457 << F.ModuleName << /*new*/0 << ModMap->getName();
3458 return OutOfDate;
3459 }
3460 }
3461 }
3462
3463 // Check any additional module map files that are in the pcm, but not
3464 // found in header search. Cases that match are already removed.
3465 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3466 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3467 Diag(diag::err_module_different_modmap)
3468 << F.ModuleName << /*not new*/1 << ModMap->getName();
3469 return OutOfDate;
3470 }
3471 }
3472
3473 if (Listener)
3474 Listener->ReadModuleMapFile(F.ModuleMapPath);
3475 return Success;
3476}
3477
3478
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003479/// \brief Move the given method to the back of the global list of methods.
3480static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3481 // Find the entry for this selector in the method pool.
3482 Sema::GlobalMethodPool::iterator Known
3483 = S.MethodPool.find(Method->getSelector());
3484 if (Known == S.MethodPool.end())
3485 return;
3486
3487 // Retrieve the appropriate method list.
3488 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3489 : Known->second.second;
3490 bool Found = false;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00003491 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003492 if (!Found) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003493 if (List->getMethod() == Method) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003494 Found = true;
3495 } else {
3496 // Keep searching.
3497 continue;
3498 }
3499 }
3500
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00003501 if (List->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003502 List->setMethod(List->getNext()->getMethod());
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003503 else
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003504 List->setMethod(Method);
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003505 }
3506}
3507
Stephen Hines176edba2014-12-01 14:53:08 -08003508void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
3509 bool FromFinalization) {
3510 // FIXME: Only do this if Owner->NameVisibility == AllVisible.
3511 for (Decl *D : Names.HiddenDecls) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003512 bool wasHidden = D->Hidden;
3513 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003514
Stephen Hines651f13c2014-04-23 16:59:28 -07003515 if (wasHidden && SemaObj) {
3516 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3517 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003518 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003519 }
3520 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003521
Stephen Hines176edba2014-12-01 14:53:08 -08003522 assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
3523 "nothing to make visible?");
3524 for (const auto &Macro : Names.HiddenMacros) {
3525 if (FromFinalization)
3526 PP.appendMacroDirective(Macro.first,
3527 Macro.second->import(PP, SourceLocation()));
3528 else
3529 installImportedMacro(Macro.first, Macro.second, Owner);
3530 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003531}
3532
Stephen Hines651f13c2014-04-23 16:59:28 -07003533void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003534 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003535 SourceLocation ImportLoc,
3536 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003537 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003538 SmallVector<Module *, 4> Stack;
Robert Wilhelm344472e2013-08-23 16:11:15 +00003539 Stack.push_back(Mod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003540 while (!Stack.empty()) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00003541 Mod = Stack.pop_back_val();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003542
3543 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00003544 // This module already has this level of visibility (or greater), so
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003545 // there is nothing more to do.
3546 continue;
3547 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003548
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003549 if (!Mod->isAvailable()) {
3550 // Modules that aren't available cannot be made visible.
3551 continue;
3552 }
3553
3554 // Update the module's name visibility.
Stephen Hines651f13c2014-04-23 16:59:28 -07003555 if (NameVisibility >= Module::MacrosVisible &&
3556 Mod->NameVisibility < Module::MacrosVisible)
3557 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003558 Mod->NameVisibility = NameVisibility;
Stephen Hines651f13c2014-04-23 16:59:28 -07003559
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003560 // If we've already deserialized any names from this module,
3561 // mark them as visible.
3562 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3563 if (Hidden != HiddenNamesMap.end()) {
Stephen Hines176edba2014-12-01 14:53:08 -08003564 auto HiddenNames = std::move(*Hidden);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003565 HiddenNamesMap.erase(Hidden);
Stephen Hines176edba2014-12-01 14:53:08 -08003566 makeNamesVisible(HiddenNames.second, HiddenNames.first,
3567 /*FromFinalization*/false);
3568 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3569 "making names visible added hidden names");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003570 }
Dmitri Gribenko86250892013-11-04 21:51:33 +00003571
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003572 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00003573 SmallVector<Module *, 16> Exports;
3574 Mod->getExportedModules(Exports);
3575 for (SmallVectorImpl<Module *>::iterator
3576 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3577 Module *Exported = *I;
Stephen Hines176edba2014-12-01 14:53:08 -08003578 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00003579 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003580 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003581
3582 // Detect any conflicts.
3583 if (Complain) {
3584 assert(ImportLoc.isValid() && "Missing import location");
3585 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3586 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3587 Diag(ImportLoc, diag::warn_module_conflict)
3588 << Mod->getFullModuleName()
3589 << Mod->Conflicts[I].Other->getFullModuleName()
3590 << Mod->Conflicts[I].Message;
3591 // FIXME: Need note where the other module was imported.
3592 }
3593 }
3594 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003595 }
3596}
3597
Douglas Gregor1a49d972013-01-25 01:03:03 +00003598bool ASTReader::loadGlobalIndex() {
3599 if (GlobalIndex)
3600 return false;
3601
3602 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3603 !Context.getLangOpts().Modules)
3604 return true;
3605
3606 // Try to load the global index.
3607 TriedLoadingGlobalIndex = true;
3608 StringRef ModuleCachePath
3609 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3610 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00003611 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00003612 if (!Result.first)
3613 return true;
3614
3615 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00003616 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00003617 return false;
3618}
3619
3620bool ASTReader::isGlobalIndexUnavailable() const {
3621 return Context.getLangOpts().Modules && UseGlobalIndex &&
3622 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3623}
3624
Stephen Hines651f13c2014-04-23 16:59:28 -07003625static void updateModuleTimestamp(ModuleFile &MF) {
3626 // Overwrite the timestamp file contents so that file's mtime changes.
3627 std::string TimestampFilename = MF.getTimestampFilename();
Stephen Hines176edba2014-12-01 14:53:08 -08003628 std::error_code EC;
3629 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3630 if (EC)
Stephen Hines651f13c2014-04-23 16:59:28 -07003631 return;
3632 OS << "Timestamp file\n";
3633}
3634
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003635ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3636 ModuleKind Type,
3637 SourceLocation ImportLoc,
3638 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00003639 llvm::SaveAndRestore<SourceLocation>
3640 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3641
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003642 // Defer any pending actions until we get to the end of reading the AST file.
3643 Deserializing AnASTFile(this);
3644
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003645 // Bump the generation number.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003646 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003647
3648 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003649 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003650 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003651 /*ImportedBy=*/nullptr, Loaded,
Stephen Hines176edba2014-12-01 14:53:08 -08003652 0, 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003653 ClientLoadCapabilities)) {
3654 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00003655 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003656 case OutOfDate:
3657 case VersionMismatch:
3658 case ConfigurationMismatch:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003659 case HadErrors: {
3660 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3661 for (const ImportedModule &IM : Loaded)
3662 LoadedSet.insert(IM.Mod);
3663
Douglas Gregor677e15f2013-03-19 00:28:20 +00003664 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003665 LoadedSet,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003666 Context.getLangOpts().Modules
3667 ? &PP.getHeaderSearchInfo().getModuleMap()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003668 : nullptr);
Douglas Gregor1a49d972013-01-25 01:03:03 +00003669
3670 // If we find that any modules are unusable, the global index is going
3671 // to be out-of-date. Just remove it.
3672 GlobalIndex.reset();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003673 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003674 return ReadResult;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003675 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003676 case Success:
3677 break;
3678 }
3679
3680 // Here comes stuff that we only do once the entire chain is loaded.
3681
3682 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003683 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3684 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003685 M != MEnd; ++M) {
3686 ModuleFile &F = *M->Mod;
3687
3688 // Read the AST block.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003689 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3690 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003691
3692 // Once read, set the ModuleFile bit base offset and update the size in
3693 // bits of all files we've seen.
3694 F.GlobalBitOffset = TotalModulesSizeInBits;
3695 TotalModulesSizeInBits += F.SizeInBits;
3696 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3697
3698 // Preload SLocEntries.
3699 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3700 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3701 // Load it through the SourceManager and don't call ReadSLocEntry()
3702 // directly because the entry may have already been loaded in which case
3703 // calling ReadSLocEntry() directly would trigger an assertion in
3704 // SourceManager.
3705 SourceMgr.getLoadedSLocEntryByID(Index);
3706 }
3707 }
3708
Douglas Gregorfa69fc12013-03-22 18:50:14 +00003709 // Setup the import locations and notify the module manager that we've
3710 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003711 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3712 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003713 M != MEnd; ++M) {
3714 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00003715
3716 ModuleMgr.moduleFileAccepted(&F);
3717
3718 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00003719 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003720 if (!M->ImportedBy)
3721 F.ImportLoc = M->ImportLoc;
3722 else
3723 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3724 M->ImportLoc.getRawEncoding());
3725 }
3726
3727 // Mark all of the identifiers in the identifier table as being out of date,
3728 // so that various accessors know to check the loaded modules when the
3729 // identifier is used.
3730 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3731 IdEnd = PP.getIdentifierTable().end();
3732 Id != IdEnd; ++Id)
3733 Id->second->setOutOfDate(true);
3734
3735 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00003736 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3737 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003738 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3739 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00003740
3741 switch (Unresolved.Kind) {
3742 case UnresolvedModuleRef::Conflict:
3743 if (ResolvedMod) {
3744 Module::Conflict Conflict;
3745 Conflict.Other = ResolvedMod;
3746 Conflict.Message = Unresolved.String.str();
3747 Unresolved.Mod->Conflicts.push_back(Conflict);
3748 }
3749 continue;
3750
3751 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003752 if (ResolvedMod)
3753 Unresolved.Mod->Imports.push_back(ResolvedMod);
3754 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003755
Douglas Gregor906d66a2013-03-20 21:10:35 +00003756 case UnresolvedModuleRef::Export:
3757 if (ResolvedMod || Unresolved.IsWildcard)
3758 Unresolved.Mod->Exports.push_back(
3759 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3760 continue;
3761 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003762 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003763 UnresolvedModuleRefs.clear();
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003764
3765 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3766 // Might be unnecessary as use declarations are only used to build the
3767 // module itself.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003768
3769 InitializeContext();
3770
Richard Smith9b671182013-10-18 06:54:39 +00003771 if (SemaObj)
3772 UpdateSema();
3773
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003774 if (DeserializationListener)
3775 DeserializationListener->ReaderInitialized(this);
3776
3777 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3778 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3779 PrimaryModule.OriginalSourceFileID
3780 = FileID::get(PrimaryModule.SLocEntryBaseID
3781 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3782
3783 // If this AST file is a precompiled preamble, then set the
3784 // preamble file ID of the source manager to the file source file
3785 // from which the preamble was built.
3786 if (Type == MK_Preamble) {
3787 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3788 } else if (Type == MK_MainFile) {
3789 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3790 }
3791 }
3792
3793 // For any Objective-C class definitions we have already loaded, make sure
3794 // that we load any additional categories.
3795 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3796 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3797 ObjCClassesLoaded[I],
3798 PreviousGeneration);
3799 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003800
Stephen Hines651f13c2014-04-23 16:59:28 -07003801 if (PP.getHeaderSearchInfo()
3802 .getHeaderSearchOpts()
3803 .ModulesValidateOncePerBuildSession) {
3804 // Now we are certain that the module and all modules it depends on are
3805 // up to date. Create or update timestamp files for modules that are
3806 // located in the module cache (not for PCH files that could be anywhere
3807 // in the filesystem).
3808 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3809 ImportedModule &M = Loaded[I];
Stephen Hines176edba2014-12-01 14:53:08 -08003810 if (M.Mod->Kind == MK_ImplicitModule) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003811 updateModuleTimestamp(*M.Mod);
3812 }
3813 }
3814 }
3815
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003816 return Success;
3817}
3818
Stephen Hines176edba2014-12-01 14:53:08 -08003819static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3820
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003821ASTReader::ASTReadResult
3822ASTReader::ReadASTCore(StringRef FileName,
3823 ModuleKind Type,
3824 SourceLocation ImportLoc,
3825 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003826 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003827 off_t ExpectedSize, time_t ExpectedModTime,
Stephen Hines176edba2014-12-01 14:53:08 -08003828 ASTFileSignature ExpectedSignature,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003829 unsigned ClientLoadCapabilities) {
3830 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003831 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003832 ModuleManager::AddModuleResult AddResult
3833 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003834 getGeneration(), ExpectedSize, ExpectedModTime,
Stephen Hines176edba2014-12-01 14:53:08 -08003835 ExpectedSignature, readASTFileSignature,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003836 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003837
Douglas Gregor677e15f2013-03-19 00:28:20 +00003838 switch (AddResult) {
3839 case ModuleManager::AlreadyLoaded:
3840 return Success;
3841
3842 case ModuleManager::NewlyLoaded:
3843 // Load module file below.
3844 break;
3845
3846 case ModuleManager::Missing:
Stephen Hines176edba2014-12-01 14:53:08 -08003847 // The module file was missing; if the client can handle that, return
Douglas Gregor677e15f2013-03-19 00:28:20 +00003848 // it.
3849 if (ClientLoadCapabilities & ARR_Missing)
3850 return Missing;
3851
3852 // Otherwise, return an error.
3853 {
3854 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3855 + ErrorStr;
3856 Error(Msg);
3857 }
3858 return Failure;
3859
3860 case ModuleManager::OutOfDate:
3861 // We couldn't load the module file because it is out-of-date. If the
3862 // client can handle out-of-date, return it.
3863 if (ClientLoadCapabilities & ARR_OutOfDate)
3864 return OutOfDate;
3865
3866 // Otherwise, return an error.
3867 {
3868 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3869 + ErrorStr;
3870 Error(Msg);
3871 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003872 return Failure;
3873 }
3874
Douglas Gregor677e15f2013-03-19 00:28:20 +00003875 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003876
3877 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3878 // module?
3879 if (FileName != "-") {
3880 CurrentDir = llvm::sys::path::parent_path(FileName);
3881 if (CurrentDir.empty()) CurrentDir = ".";
3882 }
3883
3884 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003885 BitstreamCursor &Stream = F.Stream;
Stephen Hines176edba2014-12-01 14:53:08 -08003886 Stream.init(&F.StreamFile);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003887 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3888
3889 // Sniff for the signature.
3890 if (Stream.Read(8) != 'C' ||
3891 Stream.Read(8) != 'P' ||
3892 Stream.Read(8) != 'C' ||
3893 Stream.Read(8) != 'H') {
3894 Diag(diag::err_not_a_pch_file) << FileName;
3895 return Failure;
3896 }
3897
3898 // This is used for compatibility with older PCH formats.
3899 bool HaveReadControlBlock = false;
3900
Chris Lattner99a5af02013-01-20 00:00:22 +00003901 while (1) {
3902 llvm::BitstreamEntry Entry = Stream.advance();
3903
3904 switch (Entry.Kind) {
3905 case llvm::BitstreamEntry::Error:
3906 case llvm::BitstreamEntry::EndBlock:
3907 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003908 Error("invalid record at top-level of AST file");
3909 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003910
3911 case llvm::BitstreamEntry::SubBlock:
3912 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003913 }
3914
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003915 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003916 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003917 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3918 if (Stream.ReadBlockInfoBlock()) {
3919 Error("malformed BlockInfoBlock in AST file");
3920 return Failure;
3921 }
3922 break;
3923 case CONTROL_BLOCK_ID:
3924 HaveReadControlBlock = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003925 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003926 case Success:
3927 break;
3928
3929 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003930 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003931 case OutOfDate: return OutOfDate;
3932 case VersionMismatch: return VersionMismatch;
3933 case ConfigurationMismatch: return ConfigurationMismatch;
3934 case HadErrors: return HadErrors;
3935 }
3936 break;
3937 case AST_BLOCK_ID:
3938 if (!HaveReadControlBlock) {
3939 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07003940 Diag(diag::err_pch_version_too_old);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003941 return VersionMismatch;
3942 }
3943
3944 // Record that we've loaded this module.
3945 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3946 return Success;
3947
3948 default:
3949 if (Stream.SkipBlock()) {
3950 Error("malformed block record in AST file");
3951 return Failure;
3952 }
3953 break;
3954 }
3955 }
3956
3957 return Success;
3958}
3959
3960void ASTReader::InitializeContext() {
3961 // If there's a listener, notify them that we "read" the translation unit.
3962 if (DeserializationListener)
3963 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3964 Context.getTranslationUnitDecl());
3965
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003966 // FIXME: Find a better way to deal with collisions between these
3967 // built-in types. Right now, we just ignore the problem.
3968
3969 // Load the special types.
3970 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3971 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3972 if (!Context.CFConstantStringTypeDecl)
3973 Context.setCFConstantStringType(GetType(String));
3974 }
3975
3976 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3977 QualType FileType = GetType(File);
3978 if (FileType.isNull()) {
3979 Error("FILE type is NULL");
3980 return;
3981 }
3982
3983 if (!Context.FILEDecl) {
3984 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3985 Context.setFILEDecl(Typedef->getDecl());
3986 else {
3987 const TagType *Tag = FileType->getAs<TagType>();
3988 if (!Tag) {
3989 Error("Invalid FILE type in AST file");
3990 return;
3991 }
3992 Context.setFILEDecl(Tag->getDecl());
3993 }
3994 }
3995 }
3996
3997 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3998 QualType Jmp_bufType = GetType(Jmp_buf);
3999 if (Jmp_bufType.isNull()) {
4000 Error("jmp_buf type is NULL");
4001 return;
4002 }
4003
4004 if (!Context.jmp_bufDecl) {
4005 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4006 Context.setjmp_bufDecl(Typedef->getDecl());
4007 else {
4008 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4009 if (!Tag) {
4010 Error("Invalid jmp_buf type in AST file");
4011 return;
4012 }
4013 Context.setjmp_bufDecl(Tag->getDecl());
4014 }
4015 }
4016 }
4017
4018 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4019 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4020 if (Sigjmp_bufType.isNull()) {
4021 Error("sigjmp_buf type is NULL");
4022 return;
4023 }
4024
4025 if (!Context.sigjmp_bufDecl) {
4026 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4027 Context.setsigjmp_bufDecl(Typedef->getDecl());
4028 else {
4029 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4030 assert(Tag && "Invalid sigjmp_buf type in AST file");
4031 Context.setsigjmp_bufDecl(Tag->getDecl());
4032 }
4033 }
4034 }
4035
4036 if (unsigned ObjCIdRedef
4037 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4038 if (Context.ObjCIdRedefinitionType.isNull())
4039 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4040 }
4041
4042 if (unsigned ObjCClassRedef
4043 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4044 if (Context.ObjCClassRedefinitionType.isNull())
4045 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4046 }
4047
4048 if (unsigned ObjCSelRedef
4049 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4050 if (Context.ObjCSelRedefinitionType.isNull())
4051 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4052 }
4053
4054 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4055 QualType Ucontext_tType = GetType(Ucontext_t);
4056 if (Ucontext_tType.isNull()) {
4057 Error("ucontext_t type is NULL");
4058 return;
4059 }
4060
4061 if (!Context.ucontext_tDecl) {
4062 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4063 Context.setucontext_tDecl(Typedef->getDecl());
4064 else {
4065 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4066 assert(Tag && "Invalid ucontext_t type in AST file");
4067 Context.setucontext_tDecl(Tag->getDecl());
4068 }
4069 }
4070 }
4071 }
4072
4073 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4074
4075 // If there were any CUDA special declarations, deserialize them.
4076 if (!CUDASpecialDeclRefs.empty()) {
4077 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4078 Context.setcudaConfigureCallDecl(
4079 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4080 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004081
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004082 // Re-export any modules that were imported by a non-module AST file.
Stephen Hines651f13c2014-04-23 16:59:28 -07004083 // FIXME: This does not make macro-only imports visible again. It also doesn't
4084 // make #includes mapped to module imports visible.
4085 for (auto &Import : ImportedModules) {
4086 if (Module *Imported = getSubmodule(Import.ID))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00004087 makeModuleVisible(Imported, Module::AllVisible,
Stephen Hines651f13c2014-04-23 16:59:28 -07004088 /*ImportLoc=*/Import.ImportLoc,
Douglas Gregor906d66a2013-03-20 21:10:35 +00004089 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004090 }
4091 ImportedModules.clear();
4092}
4093
4094void ASTReader::finalizeForWriting() {
Stephen Hines176edba2014-12-01 14:53:08 -08004095 while (!HiddenNamesMap.empty()) {
4096 auto HiddenNames = std::move(*HiddenNamesMap.begin());
4097 HiddenNamesMap.erase(HiddenNamesMap.begin());
4098 makeNamesVisible(HiddenNames.second, HiddenNames.first,
4099 /*FromFinalization*/true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004100 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004101}
4102
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004103/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
4104/// cursor into the start of the given block ID, returning false on success and
4105/// true on failure.
4106static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004107 while (1) {
4108 llvm::BitstreamEntry Entry = Cursor.advance();
4109 switch (Entry.Kind) {
4110 case llvm::BitstreamEntry::Error:
4111 case llvm::BitstreamEntry::EndBlock:
4112 return true;
4113
4114 case llvm::BitstreamEntry::Record:
4115 // Ignore top-level records.
4116 Cursor.skipRecord(Entry.ID);
4117 break;
4118
4119 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004120 if (Entry.ID == BlockID) {
4121 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004122 return true;
4123 // Found it!
4124 return false;
4125 }
4126
4127 if (Cursor.SkipBlock())
4128 return true;
4129 }
4130 }
4131}
4132
Stephen Hines176edba2014-12-01 14:53:08 -08004133static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4134 BitstreamCursor Stream(StreamFile);
4135 if (Stream.Read(8) != 'C' ||
4136 Stream.Read(8) != 'P' ||
4137 Stream.Read(8) != 'C' ||
4138 Stream.Read(8) != 'H') {
4139 return 0;
4140 }
4141
4142 // Scan for the CONTROL_BLOCK_ID block.
4143 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4144 return 0;
4145
4146 // Scan for SIGNATURE inside the control block.
4147 ASTReader::RecordData Record;
4148 while (1) {
4149 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4150 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4151 Entry.Kind != llvm::BitstreamEntry::Record)
4152 return 0;
4153
4154 Record.clear();
4155 StringRef Blob;
4156 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4157 return Record[0];
4158 }
4159}
4160
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004161/// \brief Retrieve the name of the original source file name
4162/// directly from the AST file, without actually loading the AST
4163/// file.
4164std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
4165 FileManager &FileMgr,
4166 DiagnosticsEngine &Diags) {
4167 // Open the AST file.
Stephen Hines176edba2014-12-01 14:53:08 -08004168 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004169 if (!Buffer) {
Stephen Hines176edba2014-12-01 14:53:08 -08004170 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4171 << ASTFileName << Buffer.getError().message();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004172 return std::string();
4173 }
4174
4175 // Initialize the stream
4176 llvm::BitstreamReader StreamFile;
Stephen Hines176edba2014-12-01 14:53:08 -08004177 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4178 (const unsigned char *)(*Buffer)->getBufferEnd());
4179 BitstreamCursor Stream(StreamFile);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004180
4181 // Sniff for the signature.
4182 if (Stream.Read(8) != 'C' ||
4183 Stream.Read(8) != 'P' ||
4184 Stream.Read(8) != 'C' ||
4185 Stream.Read(8) != 'H') {
4186 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4187 return std::string();
4188 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004189
Chris Lattner88bde502013-01-19 21:39:22 +00004190 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004191 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004192 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4193 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00004194 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004195
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004196 // Scan for ORIGINAL_FILE inside the control block.
4197 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00004198 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004199 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00004200 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4201 return std::string();
4202
4203 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4204 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4205 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004206 }
Chris Lattner88bde502013-01-19 21:39:22 +00004207
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004208 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004209 StringRef Blob;
4210 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4211 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004212 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004213}
4214
4215namespace {
4216 class SimplePCHValidator : public ASTReaderListener {
4217 const LangOptions &ExistingLangOpts;
4218 const TargetOptions &ExistingTargetOpts;
4219 const PreprocessorOptions &ExistingPPOpts;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004220 std::string ExistingModuleCachePath;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004221 FileManager &FileMgr;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004222
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004223 public:
4224 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4225 const TargetOptions &ExistingTargetOpts,
4226 const PreprocessorOptions &ExistingPPOpts,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004227 StringRef ExistingModuleCachePath,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004228 FileManager &FileMgr)
4229 : ExistingLangOpts(ExistingLangOpts),
4230 ExistingTargetOpts(ExistingTargetOpts),
4231 ExistingPPOpts(ExistingPPOpts),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004232 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004233 FileMgr(FileMgr)
4234 {
4235 }
4236
Stephen Hines176edba2014-12-01 14:53:08 -08004237 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4238 bool AllowCompatibleDifferences) override {
4239 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4240 AllowCompatibleDifferences);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004241 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004242 bool ReadTargetOptions(const TargetOptions &TargetOpts,
4243 bool Complain) override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004244 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004245 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004246 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4247 StringRef SpecificModuleCachePath,
4248 bool Complain) override {
4249 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4250 ExistingModuleCachePath,
4251 nullptr, ExistingLangOpts);
4252 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004253 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4254 bool Complain,
4255 std::string &SuggestedPredefines) override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004256 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004257 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004258 }
4259 };
4260}
4261
4262bool ASTReader::readASTFileControlBlock(StringRef Filename,
4263 FileManager &FileMgr,
4264 ASTReaderListener &Listener) {
4265 // Open the AST file.
Stephen Hines176edba2014-12-01 14:53:08 -08004266 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004267 if (!Buffer) {
4268 return true;
4269 }
4270
4271 // Initialize the stream
4272 llvm::BitstreamReader StreamFile;
Stephen Hines176edba2014-12-01 14:53:08 -08004273 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4274 (const unsigned char *)(*Buffer)->getBufferEnd());
4275 BitstreamCursor Stream(StreamFile);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004276
4277 // Sniff for the signature.
4278 if (Stream.Read(8) != 'C' ||
4279 Stream.Read(8) != 'P' ||
4280 Stream.Read(8) != 'C' ||
4281 Stream.Read(8) != 'H') {
4282 return true;
4283 }
4284
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004285 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004286 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004287 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004288
4289 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Stephen Hines651f13c2014-04-23 16:59:28 -07004290 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Stephen Hines176edba2014-12-01 14:53:08 -08004291 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004292 BitstreamCursor InputFilesCursor;
4293 if (NeedsInputFiles) {
4294 InputFilesCursor = Stream;
4295 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4296 return true;
4297
4298 // Read the abbreviations
4299 while (true) {
4300 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4301 unsigned Code = InputFilesCursor.ReadCode();
4302
4303 // We expect all abbrevs to be at the start of the block.
4304 if (Code != llvm::bitc::DEFINE_ABBREV) {
4305 InputFilesCursor.JumpToBit(Offset);
4306 break;
4307 }
4308 InputFilesCursor.ReadAbbrevRecord();
4309 }
4310 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004311
4312 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004313 RecordData Record;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004314 std::string ModuleDir;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004315 while (1) {
4316 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4317 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4318 return false;
4319
4320 if (Entry.Kind != llvm::BitstreamEntry::Record)
4321 return true;
4322
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004323 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004324 StringRef Blob;
4325 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004326 switch ((ControlRecordTypes)RecCode) {
4327 case METADATA: {
4328 if (Record[0] != VERSION_MAJOR)
4329 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004330
Douglas Gregorc544ba02013-03-27 16:47:18 +00004331 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004332 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00004333
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004334 break;
4335 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004336 case MODULE_NAME:
4337 Listener.ReadModuleName(Blob);
4338 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004339 case MODULE_DIRECTORY:
4340 ModuleDir = Blob;
4341 break;
Stephen Hines176edba2014-12-01 14:53:08 -08004342 case MODULE_MAP_FILE: {
4343 unsigned Idx = 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004344 auto Path = ReadString(Record, Idx);
4345 ResolveImportedPath(Path, ModuleDir);
4346 Listener.ReadModuleMapFile(Path);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004347 break;
Stephen Hines176edba2014-12-01 14:53:08 -08004348 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004349 case LANGUAGE_OPTIONS:
Stephen Hines176edba2014-12-01 14:53:08 -08004350 if (ParseLanguageOptions(Record, false, Listener,
4351 /*AllowCompatibleConfigurationMismatch*/false))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004352 return true;
4353 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004354
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004355 case TARGET_OPTIONS:
4356 if (ParseTargetOptions(Record, false, Listener))
4357 return true;
4358 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004359
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004360 case DIAGNOSTIC_OPTIONS:
4361 if (ParseDiagnosticOptions(Record, false, Listener))
4362 return true;
4363 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004364
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004365 case FILE_SYSTEM_OPTIONS:
4366 if (ParseFileSystemOptions(Record, false, Listener))
4367 return true;
4368 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004369
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004370 case HEADER_SEARCH_OPTIONS:
4371 if (ParseHeaderSearchOptions(Record, false, Listener))
4372 return true;
4373 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004374
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004375 case PREPROCESSOR_OPTIONS: {
4376 std::string IgnoredSuggestedPredefines;
4377 if (ParsePreprocessorOptions(Record, false, Listener,
4378 IgnoredSuggestedPredefines))
4379 return true;
4380 break;
4381 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004382
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004383 case INPUT_FILE_OFFSETS: {
4384 if (!NeedsInputFiles)
4385 break;
4386
4387 unsigned NumInputFiles = Record[0];
4388 unsigned NumUserFiles = Record[1];
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004389 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004390 for (unsigned I = 0; I != NumInputFiles; ++I) {
4391 // Go find this input file.
4392 bool isSystemFile = I >= NumUserFiles;
Stephen Hines651f13c2014-04-23 16:59:28 -07004393
4394 if (isSystemFile && !NeedsSystemInputFiles)
4395 break; // the rest are system input files
4396
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004397 BitstreamCursor &Cursor = InputFilesCursor;
4398 SavedStreamPosition SavedPosition(Cursor);
4399 Cursor.JumpToBit(InputFileOffs[I]);
4400
4401 unsigned Code = Cursor.ReadCode();
4402 RecordData Record;
4403 StringRef Blob;
4404 bool shouldContinue = false;
4405 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4406 case INPUT_FILE:
Stephen Hines651f13c2014-04-23 16:59:28 -07004407 bool Overridden = static_cast<bool>(Record[3]);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004408 std::string Filename = Blob;
4409 ResolveImportedPath(Filename, ModuleDir);
4410 shouldContinue =
4411 Listener.visitInputFile(Filename, isSystemFile, Overridden);
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004412 break;
4413 }
4414 if (!shouldContinue)
4415 break;
4416 }
4417 break;
4418 }
4419
Stephen Hines176edba2014-12-01 14:53:08 -08004420 case IMPORTS: {
4421 if (!NeedsImports)
4422 break;
4423
4424 unsigned Idx = 0, N = Record.size();
4425 while (Idx < N) {
4426 // Read information about the AST file.
4427 Idx += 5; // ImportLoc, Size, ModTime, Signature
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004428 std::string Filename = ReadString(Record, Idx);
4429 ResolveImportedPath(Filename, ModuleDir);
4430 Listener.visitImport(Filename);
Stephen Hines176edba2014-12-01 14:53:08 -08004431 }
4432 break;
4433 }
4434
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004435 default:
4436 // No other validation to perform.
4437 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004438 }
4439 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004440}
4441
4442
4443bool ASTReader::isAcceptableASTFile(StringRef Filename,
4444 FileManager &FileMgr,
4445 const LangOptions &LangOpts,
4446 const TargetOptions &TargetOpts,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004447 const PreprocessorOptions &PPOpts,
4448 std::string ExistingModuleCachePath) {
4449 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4450 ExistingModuleCachePath, FileMgr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004451 return !readASTFileControlBlock(Filename, FileMgr, validator);
4452}
4453
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004454ASTReader::ASTReadResult
4455ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004456 // Enter the submodule block.
4457 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4458 Error("malformed submodule block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004459 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004460 }
4461
4462 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4463 bool First = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004464 Module *CurrentModule = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004465 RecordData Record;
4466 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004467 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4468
4469 switch (Entry.Kind) {
4470 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4471 case llvm::BitstreamEntry::Error:
4472 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004473 return Failure;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004474 case llvm::BitstreamEntry::EndBlock:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004475 return Success;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004476 case llvm::BitstreamEntry::Record:
4477 // The interesting case.
4478 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004479 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004480
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004481 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004482 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004483 Record.clear();
Stephen Hines176edba2014-12-01 14:53:08 -08004484 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4485
4486 if ((Kind == SUBMODULE_METADATA) != First) {
4487 Error("submodule metadata record should be at beginning of block");
4488 return Failure;
4489 }
4490 First = false;
4491
4492 // Submodule information is only valid if we have a current module.
4493 // FIXME: Should we error on these cases?
4494 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4495 Kind != SUBMODULE_DEFINITION)
4496 continue;
4497
4498 switch (Kind) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004499 default: // Default behavior: ignore.
4500 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004501
Stephen Hines176edba2014-12-01 14:53:08 -08004502 case SUBMODULE_DEFINITION: {
Douglas Gregor970e4412013-03-20 03:59:18 +00004503 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004504 Error("malformed module definition");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004505 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004506 }
Stephen Hines176edba2014-12-01 14:53:08 -08004507
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004508 StringRef Name = Blob;
Stephen Hines651f13c2014-04-23 16:59:28 -07004509 unsigned Idx = 0;
4510 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4511 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4512 bool IsFramework = Record[Idx++];
4513 bool IsExplicit = Record[Idx++];
4514 bool IsSystem = Record[Idx++];
4515 bool IsExternC = Record[Idx++];
4516 bool InferSubmodules = Record[Idx++];
4517 bool InferExplicitSubmodules = Record[Idx++];
4518 bool InferExportWildcard = Record[Idx++];
4519 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor970e4412013-03-20 03:59:18 +00004520
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004521 Module *ParentModule = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08004522 if (Parent)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004523 ParentModule = getSubmodule(Parent);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004524
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004525 // Retrieve this (sub)module from the module map, creating it if
4526 // necessary.
Stephen Hines176edba2014-12-01 14:53:08 -08004527 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004528 IsExplicit).first;
Stephen Hines176edba2014-12-01 14:53:08 -08004529
4530 // FIXME: set the definition loc for CurrentModule, or call
4531 // ModMap.setInferredModuleAllowedBy()
4532
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004533 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4534 if (GlobalIndex >= SubmodulesLoaded.size() ||
4535 SubmodulesLoaded[GlobalIndex]) {
4536 Error("too many submodules");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004537 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004538 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004539
Douglas Gregor677e15f2013-03-19 00:28:20 +00004540 if (!ParentModule) {
4541 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4542 if (CurFile != F.File) {
4543 if (!Diags.isDiagnosticInFlight()) {
4544 Diag(diag::err_module_file_conflict)
4545 << CurrentModule->getTopLevelModuleName()
4546 << CurFile->getName()
4547 << F.File->getName();
4548 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004549 return Failure;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004550 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004551 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00004552
4553 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004554 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004555
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004556 CurrentModule->IsFromModuleFile = true;
4557 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Stephen Hines651f13c2014-04-23 16:59:28 -07004558 CurrentModule->IsExternC = IsExternC;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004559 CurrentModule->InferSubmodules = InferSubmodules;
4560 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4561 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00004562 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004563 if (DeserializationListener)
4564 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4565
4566 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004567
Douglas Gregor906d66a2013-03-20 21:10:35 +00004568 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004569 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00004570 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00004571 CurrentModule->UnresolvedConflicts.clear();
4572 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004573 break;
4574 }
4575
4576 case SUBMODULE_UMBRELLA_HEADER: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004577 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004578 if (!CurrentModule->getUmbrellaHeader())
4579 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4580 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004581 // This can be a spurious difference caused by changing the VFS to
4582 // point to a different copy of the file, and it is too late to
4583 // to rebuild safely.
4584 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4585 // after input file validation only real problems would remain and we
4586 // could just error. For now, assume it's okay.
4587 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004588 }
4589 }
4590 break;
4591 }
4592
Stephen Hines176edba2014-12-01 14:53:08 -08004593 case SUBMODULE_HEADER:
4594 case SUBMODULE_EXCLUDED_HEADER:
4595 case SUBMODULE_PRIVATE_HEADER:
4596 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00004597 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4598 // of complete filenames or remove it entirely.
Stephen Hines176edba2014-12-01 14:53:08 -08004599 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004600
Stephen Hines176edba2014-12-01 14:53:08 -08004601 case SUBMODULE_TEXTUAL_HEADER:
4602 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4603 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4604 // them here.
4605 break;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00004606
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004607 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00004608 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004609 break;
4610 }
4611
4612 case SUBMODULE_UMBRELLA_DIR: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004613 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004614 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004615 if (!CurrentModule->getUmbrellaDir())
4616 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4617 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004618 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4619 Error("mismatched umbrella directories in submodule");
4620 return OutOfDate;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004621 }
4622 }
4623 break;
4624 }
4625
4626 case SUBMODULE_METADATA: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004627 F.BaseSubmoduleID = getTotalNumSubmodules();
4628 F.LocalNumSubmodules = Record[0];
4629 unsigned LocalBaseSubmoduleID = Record[1];
4630 if (F.LocalNumSubmodules > 0) {
4631 // Introduce the global -> local mapping for submodules within this
4632 // module.
4633 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4634
4635 // Introduce the local -> global mapping for submodules within this
4636 // module.
4637 F.SubmoduleRemap.insertOrReplace(
4638 std::make_pair(LocalBaseSubmoduleID,
4639 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Stephen Hines176edba2014-12-01 14:53:08 -08004640
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004641 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
Stephen Hines176edba2014-12-01 14:53:08 -08004642 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004643 break;
4644 }
4645
4646 case SUBMODULE_IMPORTS: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004647 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00004648 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004649 Unresolved.File = &F;
4650 Unresolved.Mod = CurrentModule;
4651 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004652 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004653 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00004654 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004655 }
4656 break;
4657 }
4658
4659 case SUBMODULE_EXPORTS: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004660 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00004661 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004662 Unresolved.File = &F;
4663 Unresolved.Mod = CurrentModule;
4664 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004665 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004666 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004667 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004668 }
4669
4670 // Once we've loaded the set of exports, there's no reason to keep
4671 // the parsed, unresolved exports around.
4672 CurrentModule->UnresolvedExports.clear();
4673 break;
4674 }
4675 case SUBMODULE_REQUIRES: {
Richard Smith5794b532013-10-28 22:18:19 +00004676 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004677 Context.getTargetInfo());
4678 break;
4679 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004680
4681 case SUBMODULE_LINK_LIBRARY:
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004682 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004683 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004684 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00004685
4686 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor63a72682013-03-20 00:22:05 +00004687 CurrentModule->ConfigMacros.push_back(Blob.str());
4688 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00004689
4690 case SUBMODULE_CONFLICT: {
Douglas Gregor906d66a2013-03-20 21:10:35 +00004691 UnresolvedModuleRef Unresolved;
4692 Unresolved.File = &F;
4693 Unresolved.Mod = CurrentModule;
4694 Unresolved.ID = Record[0];
4695 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4696 Unresolved.IsWildcard = false;
4697 Unresolved.String = Blob;
4698 UnresolvedModuleRefs.push_back(Unresolved);
4699 break;
4700 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004701 }
4702 }
4703}
4704
4705/// \brief Parse the record that corresponds to a LangOptions data
4706/// structure.
4707///
4708/// This routine parses the language options from the AST file and then gives
4709/// them to the AST listener if one is set.
4710///
4711/// \returns true if the listener deems the file unacceptable, false otherwise.
4712bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4713 bool Complain,
Stephen Hines176edba2014-12-01 14:53:08 -08004714 ASTReaderListener &Listener,
4715 bool AllowCompatibleDifferences) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004716 LangOptions LangOpts;
4717 unsigned Idx = 0;
4718#define LANGOPT(Name, Bits, Default, Description) \
4719 LangOpts.Name = Record[Idx++];
4720#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4721 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4722#include "clang/Basic/LangOptions.def"
Stephen Hines176edba2014-12-01 14:53:08 -08004723#define SANITIZER(NAME, ID) \
4724 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietz4f45bc02013-01-18 11:30:38 +00004725#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004726
4727 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4728 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4729 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4730
4731 unsigned Length = Record[Idx++];
4732 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4733 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004734
4735 Idx += Length;
4736
4737 // Comment options.
4738 for (unsigned N = Record[Idx++]; N; --N) {
4739 LangOpts.CommentOpts.BlockCommandNames.push_back(
4740 ReadString(Record, Idx));
4741 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00004742 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004743
Stephen Hines176edba2014-12-01 14:53:08 -08004744 return Listener.ReadLanguageOptions(LangOpts, Complain,
4745 AllowCompatibleDifferences);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004746}
4747
4748bool ASTReader::ParseTargetOptions(const RecordData &Record,
4749 bool Complain,
4750 ASTReaderListener &Listener) {
4751 unsigned Idx = 0;
4752 TargetOptions TargetOpts;
4753 TargetOpts.Triple = ReadString(Record, Idx);
4754 TargetOpts.CPU = ReadString(Record, Idx);
4755 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004756 for (unsigned N = Record[Idx++]; N; --N) {
4757 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4758 }
4759 for (unsigned N = Record[Idx++]; N; --N) {
4760 TargetOpts.Features.push_back(ReadString(Record, Idx));
4761 }
4762
4763 return Listener.ReadTargetOptions(TargetOpts, Complain);
4764}
4765
4766bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4767 ASTReaderListener &Listener) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004768 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004769 unsigned Idx = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004770#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004771#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004772 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004773#include "clang/Basic/DiagnosticOptions.def"
4774
Stephen Hines176edba2014-12-01 14:53:08 -08004775 for (unsigned N = Record[Idx++]; N; --N)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004776 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Stephen Hines176edba2014-12-01 14:53:08 -08004777 for (unsigned N = Record[Idx++]; N; --N)
4778 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004779
4780 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4781}
4782
4783bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4784 ASTReaderListener &Listener) {
4785 FileSystemOptions FSOpts;
4786 unsigned Idx = 0;
4787 FSOpts.WorkingDir = ReadString(Record, Idx);
4788 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4789}
4790
4791bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4792 bool Complain,
4793 ASTReaderListener &Listener) {
4794 HeaderSearchOptions HSOpts;
4795 unsigned Idx = 0;
4796 HSOpts.Sysroot = ReadString(Record, Idx);
4797
4798 // Include entries.
4799 for (unsigned N = Record[Idx++]; N; --N) {
4800 std::string Path = ReadString(Record, Idx);
4801 frontend::IncludeDirGroup Group
4802 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004803 bool IsFramework = Record[Idx++];
4804 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004805 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004806 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004807 }
4808
4809 // System header prefixes.
4810 for (unsigned N = Record[Idx++]; N; --N) {
4811 std::string Prefix = ReadString(Record, Idx);
4812 bool IsSystemHeader = Record[Idx++];
4813 HSOpts.SystemHeaderPrefixes.push_back(
4814 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4815 }
4816
4817 HSOpts.ResourceDir = ReadString(Record, Idx);
4818 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07004819 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004820 HSOpts.DisableModuleHash = Record[Idx++];
4821 HSOpts.UseBuiltinIncludes = Record[Idx++];
4822 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4823 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4824 HSOpts.UseLibcxx = Record[Idx++];
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004825 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004826
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004827 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4828 Complain);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004829}
4830
4831bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4832 bool Complain,
4833 ASTReaderListener &Listener,
4834 std::string &SuggestedPredefines) {
4835 PreprocessorOptions PPOpts;
4836 unsigned Idx = 0;
4837
4838 // Macro definitions/undefs
4839 for (unsigned N = Record[Idx++]; N; --N) {
4840 std::string Macro = ReadString(Record, Idx);
4841 bool IsUndef = Record[Idx++];
4842 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4843 }
4844
4845 // Includes
4846 for (unsigned N = Record[Idx++]; N; --N) {
4847 PPOpts.Includes.push_back(ReadString(Record, Idx));
4848 }
4849
4850 // Macro Includes
4851 for (unsigned N = Record[Idx++]; N; --N) {
4852 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4853 }
4854
4855 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004856 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004857 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4858 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4859 PPOpts.ObjCXXARCStandardLibrary =
4860 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4861 SuggestedPredefines.clear();
4862 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4863 SuggestedPredefines);
4864}
4865
4866std::pair<ModuleFile *, unsigned>
4867ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4868 GlobalPreprocessedEntityMapType::iterator
4869 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4870 assert(I != GlobalPreprocessedEntityMap.end() &&
4871 "Corrupted global preprocessed entity map");
4872 ModuleFile *M = I->second;
4873 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4874 return std::make_pair(M, LocalIndex);
4875}
4876
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004877llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004878ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4879 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4880 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4881 Mod.NumPreprocessedEntities);
4882
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004883 return llvm::make_range(PreprocessingRecord::iterator(),
4884 PreprocessingRecord::iterator());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004885}
4886
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004887llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004888ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004889 return llvm::make_range(
4890 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4891 ModuleDeclIterator(this, &Mod,
4892 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004893}
4894
4895PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4896 PreprocessedEntityID PPID = Index+1;
4897 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4898 ModuleFile &M = *PPInfo.first;
4899 unsigned LocalIndex = PPInfo.second;
4900 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4901
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004902 if (!PP.getPreprocessingRecord()) {
4903 Error("no preprocessing record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004904 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004905 }
4906
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004907 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4908 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4909
4910 llvm::BitstreamEntry Entry =
4911 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4912 if (Entry.Kind != llvm::BitstreamEntry::Record)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004913 return nullptr;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004914
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004915 // Read the record.
4916 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4917 ReadSourceLocation(M, PPOffs.End));
4918 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004919 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004920 RecordData Record;
4921 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004922 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4923 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004924 switch (RecType) {
4925 case PPD_MACRO_EXPANSION: {
4926 bool isBuiltin = Record[0];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004927 IdentifierInfo *Name = nullptr;
4928 MacroDefinition *Def = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004929 if (isBuiltin)
4930 Name = getLocalIdentifier(M, Record[1]);
4931 else {
4932 PreprocessedEntityID
4933 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4934 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4935 }
4936
4937 MacroExpansion *ME;
4938 if (isBuiltin)
4939 ME = new (PPRec) MacroExpansion(Name, Range);
4940 else
4941 ME = new (PPRec) MacroExpansion(Def, Range);
4942
4943 return ME;
4944 }
4945
4946 case PPD_MACRO_DEFINITION: {
4947 // Decode the identifier info and then check again; if the macro is
4948 // still defined and associated with the identifier,
4949 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4950 MacroDefinition *MD
4951 = new (PPRec) MacroDefinition(II, Range);
4952
4953 if (DeserializationListener)
4954 DeserializationListener->MacroDefinitionRead(PPID, MD);
4955
4956 return MD;
4957 }
4958
4959 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004960 const char *FullFileNameStart = Blob.data() + Record[0];
4961 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004962 const FileEntry *File = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004963 if (!FullFileName.empty())
4964 File = PP.getFileManager().getFile(FullFileName);
4965
4966 // FIXME: Stable encoding
4967 InclusionDirective::InclusionKind Kind
4968 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4969 InclusionDirective *ID
4970 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004971 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004972 Record[1], Record[3],
4973 File,
4974 Range);
4975 return ID;
4976 }
4977 }
4978
4979 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4980}
4981
4982/// \brief \arg SLocMapI points at a chunk of a module that contains no
4983/// preprocessed entities or the entities it contains are not the ones we are
4984/// looking for. Find the next module that contains entities and return the ID
4985/// of the first entry.
4986PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4987 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4988 ++SLocMapI;
4989 for (GlobalSLocOffsetMapType::const_iterator
4990 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4991 ModuleFile &M = *SLocMapI->second;
4992 if (M.NumPreprocessedEntities)
4993 return M.BasePreprocessedEntityID;
4994 }
4995
4996 return getTotalNumPreprocessedEntities();
4997}
4998
4999namespace {
5000
5001template <unsigned PPEntityOffset::*PPLoc>
5002struct PPEntityComp {
5003 const ASTReader &Reader;
5004 ModuleFile &M;
5005
5006 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
5007
5008 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5009 SourceLocation LHS = getLoc(L);
5010 SourceLocation RHS = getLoc(R);
5011 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5012 }
5013
5014 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5015 SourceLocation LHS = getLoc(L);
5016 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5017 }
5018
5019 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5020 SourceLocation RHS = getLoc(R);
5021 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5022 }
5023
5024 SourceLocation getLoc(const PPEntityOffset &PPE) const {
5025 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
5026 }
5027};
5028
5029}
5030
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005031PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5032 bool EndsAfter) const {
5033 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005034 return getTotalNumPreprocessedEntities();
5035
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005036 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5037 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005038 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5039 "Corrupted global sloc offset map");
5040
5041 if (SLocMapI->second->NumPreprocessedEntities == 0)
5042 return findNextPreprocessedEntity(SLocMapI);
5043
5044 ModuleFile &M = *SLocMapI->second;
5045 typedef const PPEntityOffset *pp_iterator;
5046 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5047 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5048
5049 size_t Count = M.NumPreprocessedEntities;
5050 size_t Half;
5051 pp_iterator First = pp_begin;
5052 pp_iterator PPI;
5053
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005054 if (EndsAfter) {
5055 PPI = std::upper_bound(pp_begin, pp_end, Loc,
5056 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
5057 } else {
5058 // Do a binary search manually instead of using std::lower_bound because
5059 // The end locations of entities may be unordered (when a macro expansion
5060 // is inside another macro argument), but for this case it is not important
5061 // whether we get the first macro expansion or its containing macro.
5062 while (Count > 0) {
5063 Half = Count / 2;
5064 PPI = First;
5065 std::advance(PPI, Half);
5066 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
5067 Loc)) {
5068 First = PPI;
5069 ++First;
5070 Count = Count - Half - 1;
5071 } else
5072 Count = Half;
5073 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005074 }
5075
5076 if (PPI == pp_end)
5077 return findNextPreprocessedEntity(SLocMapI);
5078
5079 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5080}
5081
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005082/// \brief Returns a pair of [Begin, End) indices of preallocated
5083/// preprocessed entities that \arg Range encompasses.
5084std::pair<unsigned, unsigned>
5085 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5086 if (Range.isInvalid())
5087 return std::make_pair(0,0);
5088 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5089
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005090 PreprocessedEntityID BeginID =
5091 findPreprocessedEntity(Range.getBegin(), false);
5092 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005093 return std::make_pair(BeginID, EndID);
5094}
5095
5096/// \brief Optionally returns true or false if the preallocated preprocessed
5097/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00005098Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005099 FileID FID) {
5100 if (FID.isInvalid())
5101 return false;
5102
5103 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5104 ModuleFile &M = *PPInfo.first;
5105 unsigned LocalIndex = PPInfo.second;
5106 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5107
5108 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5109 if (Loc.isInvalid())
5110 return false;
5111
5112 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5113 return true;
5114 else
5115 return false;
5116}
5117
5118namespace {
5119 /// \brief Visitor used to search for information about a header file.
5120 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005121 const FileEntry *FE;
5122
David Blaikiedc84cd52013-02-20 22:23:23 +00005123 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005124
5125 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00005126 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5127 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005128
5129 static bool visit(ModuleFile &M, void *UserData) {
5130 HeaderFileInfoVisitor *This
5131 = static_cast<HeaderFileInfoVisitor *>(UserData);
5132
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005133 HeaderFileInfoLookupTable *Table
5134 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5135 if (!Table)
5136 return false;
5137
5138 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00005139 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005140 if (Pos == Table->end())
5141 return false;
5142
5143 This->HFI = *Pos;
5144 return true;
5145 }
5146
David Blaikiedc84cd52013-02-20 22:23:23 +00005147 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005148 };
5149}
5150
5151HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00005152 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005153 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidisf9ba8512013-05-08 23:46:55 +00005154 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005155 return *HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005156
5157 return HeaderFileInfo();
5158}
5159
5160void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5161 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005162 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005163 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5164 ModuleFile &F = *(*I);
5165 unsigned Idx = 0;
5166 DiagStates.clear();
5167 assert(!Diag.DiagStates.empty());
5168 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5169 while (Idx < F.PragmaDiagMappings.size()) {
5170 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5171 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5172 if (DiagStateID != 0) {
5173 Diag.DiagStatePoints.push_back(
5174 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5175 FullSourceLoc(Loc, SourceMgr)));
5176 continue;
5177 }
5178
5179 assert(DiagStateID == 0);
5180 // A new DiagState was created here.
5181 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5182 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5183 DiagStates.push_back(NewState);
5184 Diag.DiagStatePoints.push_back(
5185 DiagnosticsEngine::DiagStatePoint(NewState,
5186 FullSourceLoc(Loc, SourceMgr)));
5187 while (1) {
5188 assert(Idx < F.PragmaDiagMappings.size() &&
5189 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5190 if (Idx >= F.PragmaDiagMappings.size()) {
5191 break; // Something is messed up but at least avoid infinite loop in
5192 // release build.
5193 }
5194 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5195 if (DiagID == (unsigned)-1) {
5196 break; // no more diag/map pairs for this location.
5197 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005198 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5199 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5200 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005201 }
5202 }
5203 }
5204}
5205
5206/// \brief Get the correct cursor and offset for loading a type.
5207ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5208 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5209 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5210 ModuleFile *M = I->second;
5211 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5212}
5213
5214/// \brief Read and return the type with the given index..
5215///
5216/// The index is the type ID, shifted and minus the number of predefs. This
5217/// routine actually reads the record corresponding to the type at the given
5218/// location. It is a helper routine for GetType, which deals with reading type
5219/// IDs.
5220QualType ASTReader::readTypeRecord(unsigned Index) {
5221 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005222 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005223
5224 // Keep track of where we are in the stream, then jump back there
5225 // after reading this type.
5226 SavedStreamPosition SavedPosition(DeclsCursor);
5227
5228 ReadingKindTracker ReadingKind(Read_Type, *this);
5229
5230 // Note that we are loading a type record.
5231 Deserializing AType(this);
5232
5233 unsigned Idx = 0;
5234 DeclsCursor.JumpToBit(Loc.Offset);
5235 RecordData Record;
5236 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005237 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005238 case TYPE_EXT_QUAL: {
5239 if (Record.size() != 2) {
5240 Error("Incorrect encoding of extended qualifier type");
5241 return QualType();
5242 }
5243 QualType Base = readType(*Loc.F, Record, Idx);
5244 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5245 return Context.getQualifiedType(Base, Quals);
5246 }
5247
5248 case TYPE_COMPLEX: {
5249 if (Record.size() != 1) {
5250 Error("Incorrect encoding of complex type");
5251 return QualType();
5252 }
5253 QualType ElemType = readType(*Loc.F, Record, Idx);
5254 return Context.getComplexType(ElemType);
5255 }
5256
5257 case TYPE_POINTER: {
5258 if (Record.size() != 1) {
5259 Error("Incorrect encoding of pointer type");
5260 return QualType();
5261 }
5262 QualType PointeeType = readType(*Loc.F, Record, Idx);
5263 return Context.getPointerType(PointeeType);
5264 }
5265
Reid Kleckner12df2462013-06-24 17:51:48 +00005266 case TYPE_DECAYED: {
5267 if (Record.size() != 1) {
5268 Error("Incorrect encoding of decayed type");
5269 return QualType();
5270 }
5271 QualType OriginalType = readType(*Loc.F, Record, Idx);
5272 QualType DT = Context.getAdjustedParameterType(OriginalType);
5273 if (!isa<DecayedType>(DT))
5274 Error("Decayed type does not decay");
5275 return DT;
5276 }
5277
Stephen Hines651f13c2014-04-23 16:59:28 -07005278 case TYPE_ADJUSTED: {
5279 if (Record.size() != 2) {
5280 Error("Incorrect encoding of adjusted type");
5281 return QualType();
5282 }
5283 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5284 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5285 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5286 }
5287
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005288 case TYPE_BLOCK_POINTER: {
5289 if (Record.size() != 1) {
5290 Error("Incorrect encoding of block pointer type");
5291 return QualType();
5292 }
5293 QualType PointeeType = readType(*Loc.F, Record, Idx);
5294 return Context.getBlockPointerType(PointeeType);
5295 }
5296
5297 case TYPE_LVALUE_REFERENCE: {
5298 if (Record.size() != 2) {
5299 Error("Incorrect encoding of lvalue reference type");
5300 return QualType();
5301 }
5302 QualType PointeeType = readType(*Loc.F, Record, Idx);
5303 return Context.getLValueReferenceType(PointeeType, Record[1]);
5304 }
5305
5306 case TYPE_RVALUE_REFERENCE: {
5307 if (Record.size() != 1) {
5308 Error("Incorrect encoding of rvalue reference type");
5309 return QualType();
5310 }
5311 QualType PointeeType = readType(*Loc.F, Record, Idx);
5312 return Context.getRValueReferenceType(PointeeType);
5313 }
5314
5315 case TYPE_MEMBER_POINTER: {
5316 if (Record.size() != 2) {
5317 Error("Incorrect encoding of member pointer type");
5318 return QualType();
5319 }
5320 QualType PointeeType = readType(*Loc.F, Record, Idx);
5321 QualType ClassType = readType(*Loc.F, Record, Idx);
5322 if (PointeeType.isNull() || ClassType.isNull())
5323 return QualType();
5324
5325 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5326 }
5327
5328 case TYPE_CONSTANT_ARRAY: {
5329 QualType ElementType = readType(*Loc.F, Record, Idx);
5330 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5331 unsigned IndexTypeQuals = Record[2];
5332 unsigned Idx = 3;
5333 llvm::APInt Size = ReadAPInt(Record, Idx);
5334 return Context.getConstantArrayType(ElementType, Size,
5335 ASM, IndexTypeQuals);
5336 }
5337
5338 case TYPE_INCOMPLETE_ARRAY: {
5339 QualType ElementType = readType(*Loc.F, Record, Idx);
5340 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5341 unsigned IndexTypeQuals = Record[2];
5342 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5343 }
5344
5345 case TYPE_VARIABLE_ARRAY: {
5346 QualType ElementType = readType(*Loc.F, Record, Idx);
5347 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5348 unsigned IndexTypeQuals = Record[2];
5349 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5350 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5351 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5352 ASM, IndexTypeQuals,
5353 SourceRange(LBLoc, RBLoc));
5354 }
5355
5356 case TYPE_VECTOR: {
5357 if (Record.size() != 3) {
5358 Error("incorrect encoding of vector type in AST file");
5359 return QualType();
5360 }
5361
5362 QualType ElementType = readType(*Loc.F, Record, Idx);
5363 unsigned NumElements = Record[1];
5364 unsigned VecKind = Record[2];
5365 return Context.getVectorType(ElementType, NumElements,
5366 (VectorType::VectorKind)VecKind);
5367 }
5368
5369 case TYPE_EXT_VECTOR: {
5370 if (Record.size() != 3) {
5371 Error("incorrect encoding of extended vector type in AST file");
5372 return QualType();
5373 }
5374
5375 QualType ElementType = readType(*Loc.F, Record, Idx);
5376 unsigned NumElements = Record[1];
5377 return Context.getExtVectorType(ElementType, NumElements);
5378 }
5379
5380 case TYPE_FUNCTION_NO_PROTO: {
5381 if (Record.size() != 6) {
5382 Error("incorrect encoding of no-proto function type");
5383 return QualType();
5384 }
5385 QualType ResultType = readType(*Loc.F, Record, Idx);
5386 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5387 (CallingConv)Record[4], Record[5]);
5388 return Context.getFunctionNoProtoType(ResultType, Info);
5389 }
5390
5391 case TYPE_FUNCTION_PROTO: {
5392 QualType ResultType = readType(*Loc.F, Record, Idx);
5393
5394 FunctionProtoType::ExtProtoInfo EPI;
5395 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5396 /*hasregparm*/ Record[2],
5397 /*regparm*/ Record[3],
5398 static_cast<CallingConv>(Record[4]),
5399 /*produces*/ Record[5]);
5400
5401 unsigned Idx = 6;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005402
5403 EPI.Variadic = Record[Idx++];
5404 EPI.HasTrailingReturn = Record[Idx++];
5405 EPI.TypeQuals = Record[Idx++];
5406 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Stephen Hines651f13c2014-04-23 16:59:28 -07005407 SmallVector<QualType, 8> ExceptionStorage;
Stephen Hines176edba2014-12-01 14:53:08 -08005408 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5409
5410 unsigned NumParams = Record[Idx++];
5411 SmallVector<QualType, 16> ParamTypes;
5412 for (unsigned I = 0; I != NumParams; ++I)
5413 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5414
Jordan Rosebea522f2013-03-08 21:51:21 +00005415 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005416 }
5417
5418 case TYPE_UNRESOLVED_USING: {
5419 unsigned Idx = 0;
5420 return Context.getTypeDeclType(
5421 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5422 }
5423
5424 case TYPE_TYPEDEF: {
5425 if (Record.size() != 2) {
5426 Error("incorrect encoding of typedef type");
5427 return QualType();
5428 }
5429 unsigned Idx = 0;
5430 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5431 QualType Canonical = readType(*Loc.F, Record, Idx);
5432 if (!Canonical.isNull())
5433 Canonical = Context.getCanonicalType(Canonical);
5434 return Context.getTypedefType(Decl, Canonical);
5435 }
5436
5437 case TYPE_TYPEOF_EXPR:
5438 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5439
5440 case TYPE_TYPEOF: {
5441 if (Record.size() != 1) {
5442 Error("incorrect encoding of typeof(type) in AST file");
5443 return QualType();
5444 }
5445 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5446 return Context.getTypeOfType(UnderlyingType);
5447 }
5448
5449 case TYPE_DECLTYPE: {
5450 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5451 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5452 }
5453
5454 case TYPE_UNARY_TRANSFORM: {
5455 QualType BaseType = readType(*Loc.F, Record, Idx);
5456 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5457 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5458 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5459 }
5460
Richard Smitha2c36462013-04-26 16:15:35 +00005461 case TYPE_AUTO: {
5462 QualType Deduced = readType(*Loc.F, Record, Idx);
5463 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00005464 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek152b4e42013-08-22 12:12:24 +00005465 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00005466 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005467
5468 case TYPE_RECORD: {
5469 if (Record.size() != 2) {
5470 Error("incorrect encoding of record type");
5471 return QualType();
5472 }
5473 unsigned Idx = 0;
5474 bool IsDependent = Record[Idx++];
5475 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5476 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5477 QualType T = Context.getRecordType(RD);
5478 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5479 return T;
5480 }
5481
5482 case TYPE_ENUM: {
5483 if (Record.size() != 2) {
5484 Error("incorrect encoding of enum type");
5485 return QualType();
5486 }
5487 unsigned Idx = 0;
5488 bool IsDependent = Record[Idx++];
5489 QualType T
5490 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5491 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5492 return T;
5493 }
5494
5495 case TYPE_ATTRIBUTED: {
5496 if (Record.size() != 3) {
5497 Error("incorrect encoding of attributed type");
5498 return QualType();
5499 }
5500 QualType modifiedType = readType(*Loc.F, Record, Idx);
5501 QualType equivalentType = readType(*Loc.F, Record, Idx);
5502 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5503 return Context.getAttributedType(kind, modifiedType, equivalentType);
5504 }
5505
5506 case TYPE_PAREN: {
5507 if (Record.size() != 1) {
5508 Error("incorrect encoding of paren type");
5509 return QualType();
5510 }
5511 QualType InnerType = readType(*Loc.F, Record, Idx);
5512 return Context.getParenType(InnerType);
5513 }
5514
5515 case TYPE_PACK_EXPANSION: {
5516 if (Record.size() != 2) {
5517 Error("incorrect encoding of pack expansion type");
5518 return QualType();
5519 }
5520 QualType Pattern = readType(*Loc.F, Record, Idx);
5521 if (Pattern.isNull())
5522 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00005523 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005524 if (Record[1])
5525 NumExpansions = Record[1] - 1;
5526 return Context.getPackExpansionType(Pattern, NumExpansions);
5527 }
5528
5529 case TYPE_ELABORATED: {
5530 unsigned Idx = 0;
5531 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5532 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5533 QualType NamedType = readType(*Loc.F, Record, Idx);
5534 return Context.getElaboratedType(Keyword, NNS, NamedType);
5535 }
5536
5537 case TYPE_OBJC_INTERFACE: {
5538 unsigned Idx = 0;
5539 ObjCInterfaceDecl *ItfD
5540 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5541 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5542 }
5543
5544 case TYPE_OBJC_OBJECT: {
5545 unsigned Idx = 0;
5546 QualType Base = readType(*Loc.F, Record, Idx);
5547 unsigned NumProtos = Record[Idx++];
5548 SmallVector<ObjCProtocolDecl*, 4> Protos;
5549 for (unsigned I = 0; I != NumProtos; ++I)
5550 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5551 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5552 }
5553
5554 case TYPE_OBJC_OBJECT_POINTER: {
5555 unsigned Idx = 0;
5556 QualType Pointee = readType(*Loc.F, Record, Idx);
5557 return Context.getObjCObjectPointerType(Pointee);
5558 }
5559
5560 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5561 unsigned Idx = 0;
5562 QualType Parm = readType(*Loc.F, Record, Idx);
5563 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07005564 return Context.getSubstTemplateTypeParmType(
5565 cast<TemplateTypeParmType>(Parm),
5566 Context.getCanonicalType(Replacement));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005567 }
5568
5569 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5570 unsigned Idx = 0;
5571 QualType Parm = readType(*Loc.F, Record, Idx);
5572 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5573 return Context.getSubstTemplateTypeParmPackType(
5574 cast<TemplateTypeParmType>(Parm),
5575 ArgPack);
5576 }
5577
5578 case TYPE_INJECTED_CLASS_NAME: {
5579 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5580 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5581 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5582 // for AST reading, too much interdependencies.
Stephen Hines176edba2014-12-01 14:53:08 -08005583 const Type *T = nullptr;
5584 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5585 if (const Type *Existing = DI->getTypeForDecl()) {
5586 T = Existing;
5587 break;
5588 }
5589 }
5590 if (!T) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005591 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Stephen Hines176edba2014-12-01 14:53:08 -08005592 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5593 DI->setTypeForDecl(T);
5594 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005595 return QualType(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005596 }
5597
5598 case TYPE_TEMPLATE_TYPE_PARM: {
5599 unsigned Idx = 0;
5600 unsigned Depth = Record[Idx++];
5601 unsigned Index = Record[Idx++];
5602 bool Pack = Record[Idx++];
5603 TemplateTypeParmDecl *D
5604 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5605 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5606 }
5607
5608 case TYPE_DEPENDENT_NAME: {
5609 unsigned Idx = 0;
5610 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5611 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5612 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5613 QualType Canon = readType(*Loc.F, Record, Idx);
5614 if (!Canon.isNull())
5615 Canon = Context.getCanonicalType(Canon);
5616 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5617 }
5618
5619 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5620 unsigned Idx = 0;
5621 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5622 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5623 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5624 unsigned NumArgs = Record[Idx++];
5625 SmallVector<TemplateArgument, 8> Args;
5626 Args.reserve(NumArgs);
5627 while (NumArgs--)
5628 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5629 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5630 Args.size(), Args.data());
5631 }
5632
5633 case TYPE_DEPENDENT_SIZED_ARRAY: {
5634 unsigned Idx = 0;
5635
5636 // ArrayType
5637 QualType ElementType = readType(*Loc.F, Record, Idx);
5638 ArrayType::ArraySizeModifier ASM
5639 = (ArrayType::ArraySizeModifier)Record[Idx++];
5640 unsigned IndexTypeQuals = Record[Idx++];
5641
5642 // DependentSizedArrayType
5643 Expr *NumElts = ReadExpr(*Loc.F);
5644 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5645
5646 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5647 IndexTypeQuals, Brackets);
5648 }
5649
5650 case TYPE_TEMPLATE_SPECIALIZATION: {
5651 unsigned Idx = 0;
5652 bool IsDependent = Record[Idx++];
5653 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5654 SmallVector<TemplateArgument, 8> Args;
5655 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5656 QualType Underlying = readType(*Loc.F, Record, Idx);
5657 QualType T;
5658 if (Underlying.isNull())
5659 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5660 Args.size());
5661 else
5662 T = Context.getTemplateSpecializationType(Name, Args.data(),
5663 Args.size(), Underlying);
5664 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5665 return T;
5666 }
5667
5668 case TYPE_ATOMIC: {
5669 if (Record.size() != 1) {
5670 Error("Incorrect encoding of atomic type");
5671 return QualType();
5672 }
5673 QualType ValueType = readType(*Loc.F, Record, Idx);
5674 return Context.getAtomicType(ValueType);
5675 }
5676 }
5677 llvm_unreachable("Invalid TypeCode!");
5678}
5679
Stephen Hines651f13c2014-04-23 16:59:28 -07005680void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5681 SmallVectorImpl<QualType> &Exceptions,
Stephen Hines176edba2014-12-01 14:53:08 -08005682 FunctionProtoType::ExceptionSpecInfo &ESI,
Stephen Hines651f13c2014-04-23 16:59:28 -07005683 const RecordData &Record, unsigned &Idx) {
5684 ExceptionSpecificationType EST =
5685 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Stephen Hines176edba2014-12-01 14:53:08 -08005686 ESI.Type = EST;
Stephen Hines651f13c2014-04-23 16:59:28 -07005687 if (EST == EST_Dynamic) {
Stephen Hines176edba2014-12-01 14:53:08 -08005688 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07005689 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Stephen Hines176edba2014-12-01 14:53:08 -08005690 ESI.Exceptions = Exceptions;
Stephen Hines651f13c2014-04-23 16:59:28 -07005691 } else if (EST == EST_ComputedNoexcept) {
Stephen Hines176edba2014-12-01 14:53:08 -08005692 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Stephen Hines651f13c2014-04-23 16:59:28 -07005693 } else if (EST == EST_Uninstantiated) {
Stephen Hines176edba2014-12-01 14:53:08 -08005694 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5695 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07005696 } else if (EST == EST_Unevaluated) {
Stephen Hines176edba2014-12-01 14:53:08 -08005697 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07005698 }
5699}
5700
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005701class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5702 ASTReader &Reader;
5703 ModuleFile &F;
5704 const ASTReader::RecordData &Record;
5705 unsigned &Idx;
5706
5707 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5708 unsigned &I) {
5709 return Reader.ReadSourceLocation(F, R, I);
5710 }
5711
5712 template<typename T>
5713 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5714 return Reader.ReadDeclAs<T>(F, Record, Idx);
5715 }
5716
5717public:
5718 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5719 const ASTReader::RecordData &Record, unsigned &Idx)
5720 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5721 { }
5722
5723 // We want compile-time assurance that we've enumerated all of
5724 // these, so unfortunately we have to declare them first, then
5725 // define them out-of-line.
5726#define ABSTRACT_TYPELOC(CLASS, PARENT)
5727#define TYPELOC(CLASS, PARENT) \
5728 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5729#include "clang/AST/TypeLocNodes.def"
5730
5731 void VisitFunctionTypeLoc(FunctionTypeLoc);
5732 void VisitArrayTypeLoc(ArrayTypeLoc);
5733};
5734
5735void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5736 // nothing to do
5737}
5738void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5739 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5740 if (TL.needsExtraLocalData()) {
5741 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5742 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5743 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5744 TL.setModeAttr(Record[Idx++]);
5745 }
5746}
5747void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5748 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5749}
5750void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5751 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5752}
Reid Kleckner12df2462013-06-24 17:51:48 +00005753void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5754 // nothing to do
5755}
Stephen Hines651f13c2014-04-23 16:59:28 -07005756void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5757 // nothing to do
5758}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005759void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5760 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5761}
5762void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5763 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5764}
5765void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5766 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5767}
5768void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5769 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5770 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5771}
5772void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5773 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5774 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5775 if (Record[Idx++])
5776 TL.setSizeExpr(Reader.ReadExpr(F));
5777 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005778 TL.setSizeExpr(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005779}
5780void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5781 VisitArrayTypeLoc(TL);
5782}
5783void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5784 VisitArrayTypeLoc(TL);
5785}
5786void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5787 VisitArrayTypeLoc(TL);
5788}
5789void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5790 DependentSizedArrayTypeLoc TL) {
5791 VisitArrayTypeLoc(TL);
5792}
5793void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5794 DependentSizedExtVectorTypeLoc TL) {
5795 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5796}
5797void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5798 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5799}
5800void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5801 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5802}
5803void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5804 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5805 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5806 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5807 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Stephen Hines651f13c2014-04-23 16:59:28 -07005808 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5809 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005810 }
5811}
5812void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5813 VisitFunctionTypeLoc(TL);
5814}
5815void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5816 VisitFunctionTypeLoc(TL);
5817}
5818void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5819 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5820}
5821void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5822 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5823}
5824void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5825 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5826 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5827 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5828}
5829void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5830 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5831 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5832 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5833 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5834}
5835void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5836 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5837}
5838void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5839 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5840 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5841 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5842 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5843}
5844void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5845 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5846}
5847void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5848 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5849}
5850void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5851 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5852}
5853void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5854 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5855 if (TL.hasAttrOperand()) {
5856 SourceRange range;
5857 range.setBegin(ReadSourceLocation(Record, Idx));
5858 range.setEnd(ReadSourceLocation(Record, Idx));
5859 TL.setAttrOperandParensRange(range);
5860 }
5861 if (TL.hasAttrExprOperand()) {
5862 if (Record[Idx++])
5863 TL.setAttrExprOperand(Reader.ReadExpr(F));
5864 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005865 TL.setAttrExprOperand(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005866 } else if (TL.hasAttrEnumOperand())
5867 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5868}
5869void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5870 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5871}
5872void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5873 SubstTemplateTypeParmTypeLoc TL) {
5874 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5875}
5876void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5877 SubstTemplateTypeParmPackTypeLoc TL) {
5878 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5879}
5880void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5881 TemplateSpecializationTypeLoc TL) {
5882 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5883 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5884 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5885 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5886 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5887 TL.setArgLocInfo(i,
5888 Reader.GetTemplateArgumentLocInfo(F,
5889 TL.getTypePtr()->getArg(i).getKind(),
5890 Record, Idx));
5891}
5892void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5893 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5894 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5895}
5896void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5897 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5898 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5899}
5900void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5901 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5902}
5903void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5904 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5905 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5906 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5907}
5908void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5909 DependentTemplateSpecializationTypeLoc TL) {
5910 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5911 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5912 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5913 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5914 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5915 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5916 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5917 TL.setArgLocInfo(I,
5918 Reader.GetTemplateArgumentLocInfo(F,
5919 TL.getTypePtr()->getArg(I).getKind(),
5920 Record, Idx));
5921}
5922void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5923 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5924}
5925void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5926 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5927}
5928void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5929 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5930 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5931 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5932 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5933 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5934}
5935void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5936 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5937}
5938void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5939 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5940 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5941 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5942}
5943
5944TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5945 const RecordData &Record,
5946 unsigned &Idx) {
5947 QualType InfoTy = readType(F, Record, Idx);
5948 if (InfoTy.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005949 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005950
5951 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5952 TypeLocReader TLR(*this, F, Record, Idx);
5953 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5954 TLR.Visit(TL);
5955 return TInfo;
5956}
5957
5958QualType ASTReader::GetType(TypeID ID) {
5959 unsigned FastQuals = ID & Qualifiers::FastMask;
5960 unsigned Index = ID >> Qualifiers::FastWidth;
5961
5962 if (Index < NUM_PREDEF_TYPE_IDS) {
5963 QualType T;
5964 switch ((PredefinedTypeIDs)Index) {
5965 case PREDEF_TYPE_NULL_ID: return QualType();
5966 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5967 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5968
5969 case PREDEF_TYPE_CHAR_U_ID:
5970 case PREDEF_TYPE_CHAR_S_ID:
5971 // FIXME: Check that the signedness of CharTy is correct!
5972 T = Context.CharTy;
5973 break;
5974
5975 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5976 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5977 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5978 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5979 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5980 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5981 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5982 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5983 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5984 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5985 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5986 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5987 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5988 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5989 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5990 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5991 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5992 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5993 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5994 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5995 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5996 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5997 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5998 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5999 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
6000 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
6001 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
6002 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00006003 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
6004 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
6005 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
6006 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
6007 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
6008 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00006009 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00006010 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006011 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
6012
6013 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6014 T = Context.getAutoRRefDeductType();
6015 break;
6016
6017 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6018 T = Context.ARCUnbridgedCastTy;
6019 break;
6020
6021 case PREDEF_TYPE_VA_LIST_TAG:
6022 T = Context.getVaListTagType();
6023 break;
6024
6025 case PREDEF_TYPE_BUILTIN_FN:
6026 T = Context.BuiltinFnTy;
6027 break;
6028 }
6029
6030 assert(!T.isNull() && "Unknown predefined type");
6031 return T.withFastQualifiers(FastQuals);
6032 }
6033
6034 Index -= NUM_PREDEF_TYPE_IDS;
6035 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6036 if (TypesLoaded[Index].isNull()) {
6037 TypesLoaded[Index] = readTypeRecord(Index);
6038 if (TypesLoaded[Index].isNull())
6039 return QualType();
6040
6041 TypesLoaded[Index]->setFromAST();
6042 if (DeserializationListener)
6043 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6044 TypesLoaded[Index]);
6045 }
6046
6047 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6048}
6049
6050QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6051 return GetType(getGlobalTypeID(F, LocalID));
6052}
6053
6054serialization::TypeID
6055ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6056 unsigned FastQuals = LocalID & Qualifiers::FastMask;
6057 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6058
6059 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6060 return LocalID;
6061
6062 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6063 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6064 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6065
6066 unsigned GlobalIndex = LocalIndex + I->second;
6067 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6068}
6069
6070TemplateArgumentLocInfo
6071ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6072 TemplateArgument::ArgKind Kind,
6073 const RecordData &Record,
6074 unsigned &Index) {
6075 switch (Kind) {
6076 case TemplateArgument::Expression:
6077 return ReadExpr(F);
6078 case TemplateArgument::Type:
6079 return GetTypeSourceInfo(F, Record, Index);
6080 case TemplateArgument::Template: {
6081 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6082 Index);
6083 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6084 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6085 SourceLocation());
6086 }
6087 case TemplateArgument::TemplateExpansion: {
6088 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6089 Index);
6090 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6091 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6092 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6093 EllipsisLoc);
6094 }
6095 case TemplateArgument::Null:
6096 case TemplateArgument::Integral:
6097 case TemplateArgument::Declaration:
6098 case TemplateArgument::NullPtr:
6099 case TemplateArgument::Pack:
6100 // FIXME: Is this right?
6101 return TemplateArgumentLocInfo();
6102 }
6103 llvm_unreachable("unexpected template argument loc");
6104}
6105
6106TemplateArgumentLoc
6107ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6108 const RecordData &Record, unsigned &Index) {
6109 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6110
6111 if (Arg.getKind() == TemplateArgument::Expression) {
6112 if (Record[Index++]) // bool InfoHasSameExpr.
6113 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6114 }
6115 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6116 Record, Index));
6117}
6118
Enea Zaffanellac1cef082013-08-10 07:24:53 +00006119const ASTTemplateArgumentListInfo*
6120ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6121 const RecordData &Record,
6122 unsigned &Index) {
6123 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6124 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6125 unsigned NumArgsAsWritten = Record[Index++];
6126 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6127 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6128 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6129 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6130}
6131
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006132Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6133 return GetDecl(ID);
6134}
6135
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006136template<typename TemplateSpecializationDecl>
6137static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6138 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6139 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6140}
6141
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006142void ASTReader::CompleteRedeclChain(const Decl *D) {
6143 if (NumCurrentElementsDeserializing) {
6144 // We arrange to not care about the complete redeclaration chain while we're
6145 // deserializing. Just remember that the AST has marked this one as complete
6146 // but that it's not actually complete yet, so we know we still need to
6147 // complete it later.
6148 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6149 return;
6150 }
6151
6152 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6153
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006154 // If this is a named declaration, complete it by looking it up
6155 // within its context.
6156 //
Stephen Hines176edba2014-12-01 14:53:08 -08006157 // FIXME: Merging a function definition should merge
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006158 // all mergeable entities within it.
6159 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6160 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6161 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6162 auto *II = Name.getAsIdentifierInfo();
6163 if (isa<TranslationUnitDecl>(DC) && II) {
6164 // Outside of C++, we don't have a lookup table for the TU, so update
6165 // the identifier instead. In C++, either way should work fine.
6166 if (II->isOutOfDate())
6167 updateOutOfDateIdentifier(*II);
6168 } else
6169 DC->lookup(Name);
Stephen Hines176edba2014-12-01 14:53:08 -08006170 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
6171 // FIXME: It'd be nice to do something a bit more targeted here.
6172 D->getDeclContext()->decls_begin();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006173 }
6174 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006175
6176 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6177 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6178 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6179 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6180 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6181 if (auto *Template = FD->getPrimaryTemplate())
6182 Template->LoadLazySpecializations();
6183 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006184}
6185
6186uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6187 const RecordData &Record,
6188 unsigned &Idx) {
6189 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6190 Error("malformed AST file: missing C++ base specifier");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006191 return 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006192 }
6193
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006194 unsigned LocalID = Record[Idx++];
6195 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6196}
6197
6198CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6199 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006200 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006201 SavedStreamPosition SavedPosition(Cursor);
6202 Cursor.JumpToBit(Loc.Offset);
6203 ReadingKindTracker ReadingKind(Read_Decl, *this);
6204 RecordData Record;
6205 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00006206 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006207 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006208 Error("malformed AST file: missing C++ base specifiers");
6209 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006210 }
6211
6212 unsigned Idx = 0;
6213 unsigned NumBases = Record[Idx++];
6214 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6215 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6216 for (unsigned I = 0; I != NumBases; ++I)
6217 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6218 return Bases;
6219}
6220
6221serialization::DeclID
6222ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6223 if (LocalID < NUM_PREDEF_DECL_IDS)
6224 return LocalID;
6225
6226 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6227 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6228 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6229
6230 return LocalID + I->second;
6231}
6232
6233bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6234 ModuleFile &M) const {
6235 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6236 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6237 return &M == I->second;
6238}
6239
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006240ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006241 if (!D->isFromASTFile())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006242 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006243 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6244 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6245 return I->second;
6246}
6247
6248SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6249 if (ID < NUM_PREDEF_DECL_IDS)
6250 return SourceLocation();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006251
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006252 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6253
6254 if (Index > DeclsLoaded.size()) {
6255 Error("declaration ID out-of-range for AST file");
6256 return SourceLocation();
6257 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006258
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006259 if (Decl *D = DeclsLoaded[Index])
6260 return D->getLocation();
6261
6262 unsigned RawLocation = 0;
6263 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6264 return ReadSourceLocation(*Rec.F, RawLocation);
6265}
6266
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006267Decl *ASTReader::GetExistingDecl(DeclID ID) {
6268 if (ID < NUM_PREDEF_DECL_IDS) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006269 switch ((PredefinedDeclIDs)ID) {
6270 case PREDEF_DECL_NULL_ID:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006271 return nullptr;
6272
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006273 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6274 return Context.getTranslationUnitDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006275
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006276 case PREDEF_DECL_OBJC_ID_ID:
6277 return Context.getObjCIdDecl();
6278
6279 case PREDEF_DECL_OBJC_SEL_ID:
6280 return Context.getObjCSelDecl();
6281
6282 case PREDEF_DECL_OBJC_CLASS_ID:
6283 return Context.getObjCClassDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006284
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006285 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6286 return Context.getObjCProtocolDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006287
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006288 case PREDEF_DECL_INT_128_ID:
6289 return Context.getInt128Decl();
6290
6291 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6292 return Context.getUInt128Decl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006293
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006294 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6295 return Context.getObjCInstanceTypeDecl();
6296
6297 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6298 return Context.getBuiltinVaListDecl();
6299 }
6300 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006301
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006302 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6303
6304 if (Index >= DeclsLoaded.size()) {
6305 assert(0 && "declaration ID out-of-range for AST file");
6306 Error("declaration ID out-of-range for AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006307 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006308 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006309
6310 return DeclsLoaded[Index];
6311}
6312
6313Decl *ASTReader::GetDecl(DeclID ID) {
6314 if (ID < NUM_PREDEF_DECL_IDS)
6315 return GetExistingDecl(ID);
6316
6317 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6318
6319 if (Index >= DeclsLoaded.size()) {
6320 assert(0 && "declaration ID out-of-range for AST file");
6321 Error("declaration ID out-of-range for AST file");
6322 return nullptr;
6323 }
6324
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006325 if (!DeclsLoaded[Index]) {
6326 ReadDeclRecord(ID);
6327 if (DeserializationListener)
6328 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6329 }
6330
6331 return DeclsLoaded[Index];
6332}
6333
6334DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6335 DeclID GlobalID) {
6336 if (GlobalID < NUM_PREDEF_DECL_IDS)
6337 return GlobalID;
6338
6339 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6340 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6341 ModuleFile *Owner = I->second;
6342
6343 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6344 = M.GlobalToLocalDeclIDs.find(Owner);
6345 if (Pos == M.GlobalToLocalDeclIDs.end())
6346 return 0;
6347
6348 return GlobalID - Owner->BaseDeclID + Pos->second;
6349}
6350
6351serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6352 const RecordData &Record,
6353 unsigned &Idx) {
6354 if (Idx >= Record.size()) {
6355 Error("Corrupted AST file");
6356 return 0;
6357 }
6358
6359 return getGlobalDeclID(F, Record[Idx++]);
6360}
6361
6362/// \brief Resolve the offset of a statement into a statement.
6363///
6364/// This operation will read a new statement from the external
6365/// source each time it is called, and is meant to be used via a
6366/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6367Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6368 // Switch case IDs are per Decl.
6369 ClearSwitchCaseIDs();
6370
6371 // Offset here is a global offset across the entire chain.
6372 RecordLocation Loc = getLocalBitOffset(Offset);
6373 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6374 return ReadStmtFromStream(*Loc.F);
6375}
6376
6377namespace {
6378 class FindExternalLexicalDeclsVisitor {
6379 ASTReader &Reader;
6380 const DeclContext *DC;
6381 bool (*isKindWeWant)(Decl::Kind);
6382
6383 SmallVectorImpl<Decl*> &Decls;
6384 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6385
6386 public:
6387 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6388 bool (*isKindWeWant)(Decl::Kind),
6389 SmallVectorImpl<Decl*> &Decls)
6390 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6391 {
6392 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6393 PredefsVisited[I] = false;
6394 }
6395
6396 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6397 if (Preorder)
6398 return false;
6399
6400 FindExternalLexicalDeclsVisitor *This
6401 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6402
6403 ModuleFile::DeclContextInfosMap::iterator Info
6404 = M.DeclContextInfos.find(This->DC);
6405 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6406 return false;
6407
6408 // Load all of the declaration IDs
6409 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6410 *IDE = ID + Info->second.NumLexicalDecls;
6411 ID != IDE; ++ID) {
6412 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6413 continue;
6414
6415 // Don't add predefined declarations to the lexical context more
6416 // than once.
6417 if (ID->second < NUM_PREDEF_DECL_IDS) {
6418 if (This->PredefsVisited[ID->second])
6419 continue;
6420
6421 This->PredefsVisited[ID->second] = true;
6422 }
6423
6424 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6425 if (!This->DC->isDeclInLexicalTraversal(D))
6426 This->Decls.push_back(D);
6427 }
6428 }
6429
6430 return false;
6431 }
6432 };
6433}
6434
6435ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6436 bool (*isKindWeWant)(Decl::Kind),
6437 SmallVectorImpl<Decl*> &Decls) {
6438 // There might be lexical decls in multiple modules, for the TU at
6439 // least. Walk all of the modules in the order they were loaded.
6440 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6441 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6442 ++NumLexicalDeclContextsRead;
6443 return ELR_Success;
6444}
6445
6446namespace {
6447
6448class DeclIDComp {
6449 ASTReader &Reader;
6450 ModuleFile &Mod;
6451
6452public:
6453 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6454
6455 bool operator()(LocalDeclID L, LocalDeclID R) const {
6456 SourceLocation LHS = getLocation(L);
6457 SourceLocation RHS = getLocation(R);
6458 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6459 }
6460
6461 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6462 SourceLocation RHS = getLocation(R);
6463 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6464 }
6465
6466 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6467 SourceLocation LHS = getLocation(L);
6468 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6469 }
6470
6471 SourceLocation getLocation(LocalDeclID ID) const {
6472 return Reader.getSourceManager().getFileLoc(
6473 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6474 }
6475};
6476
6477}
6478
6479void ASTReader::FindFileRegionDecls(FileID File,
6480 unsigned Offset, unsigned Length,
6481 SmallVectorImpl<Decl *> &Decls) {
6482 SourceManager &SM = getSourceManager();
6483
6484 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6485 if (I == FileDeclIDs.end())
6486 return;
6487
6488 FileDeclsInfo &DInfo = I->second;
6489 if (DInfo.Decls.empty())
6490 return;
6491
6492 SourceLocation
6493 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6494 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6495
6496 DeclIDComp DIDComp(*this, *DInfo.Mod);
6497 ArrayRef<serialization::LocalDeclID>::iterator
6498 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6499 BeginLoc, DIDComp);
6500 if (BeginIt != DInfo.Decls.begin())
6501 --BeginIt;
6502
6503 // If we are pointing at a top-level decl inside an objc container, we need
6504 // to backtrack until we find it otherwise we will fail to report that the
6505 // region overlaps with an objc container.
6506 while (BeginIt != DInfo.Decls.begin() &&
6507 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6508 ->isTopLevelDeclInObjCContainer())
6509 --BeginIt;
6510
6511 ArrayRef<serialization::LocalDeclID>::iterator
6512 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6513 EndLoc, DIDComp);
6514 if (EndIt != DInfo.Decls.end())
6515 ++EndIt;
6516
6517 for (ArrayRef<serialization::LocalDeclID>::iterator
6518 DIt = BeginIt; DIt != EndIt; ++DIt)
6519 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6520}
6521
6522namespace {
6523 /// \brief ModuleFile visitor used to perform name lookup into a
6524 /// declaration context.
6525 class DeclContextNameLookupVisitor {
6526 ASTReader &Reader;
Stephen Hines176edba2014-12-01 14:53:08 -08006527 ArrayRef<const DeclContext *> Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006528 DeclarationName Name;
6529 SmallVectorImpl<NamedDecl *> &Decls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006530 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006531
6532 public:
Stephen Hines176edba2014-12-01 14:53:08 -08006533 DeclContextNameLookupVisitor(ASTReader &Reader,
6534 ArrayRef<const DeclContext *> Contexts,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006535 DeclarationName Name,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006536 SmallVectorImpl<NamedDecl *> &Decls,
6537 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet)
6538 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls),
6539 DeclSet(DeclSet) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006540
6541 static bool visit(ModuleFile &M, void *UserData) {
6542 DeclContextNameLookupVisitor *This
6543 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6544
6545 // Check whether we have any visible declaration information for
6546 // this context in this module.
6547 ModuleFile::DeclContextInfosMap::iterator Info;
6548 bool FoundInfo = false;
Stephen Hines176edba2014-12-01 14:53:08 -08006549 for (auto *DC : This->Contexts) {
6550 Info = M.DeclContextInfos.find(DC);
6551 if (Info != M.DeclContextInfos.end() &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006552 Info->second.NameLookupTableData) {
6553 FoundInfo = true;
6554 break;
6555 }
6556 }
6557
6558 if (!FoundInfo)
6559 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08006560
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006561 // Look for this name within this module.
6562 ASTDeclContextNameLookupTable *LookupTable =
6563 Info->second.NameLookupTableData;
6564 ASTDeclContextNameLookupTable::iterator Pos
6565 = LookupTable->find(This->Name);
6566 if (Pos == LookupTable->end())
6567 return false;
6568
6569 bool FoundAnything = false;
6570 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6571 for (; Data.first != Data.second; ++Data.first) {
6572 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6573 if (!ND)
6574 continue;
6575
6576 if (ND->getDeclName() != This->Name) {
6577 // A name might be null because the decl's redeclarable part is
6578 // currently read before reading its name. The lookup is triggered by
6579 // building that decl (likely indirectly), and so it is later in the
6580 // sense of "already existing" and can be ignored here.
Stephen Hines176edba2014-12-01 14:53:08 -08006581 // FIXME: This should not happen; deserializing declarations should
6582 // not perform lookups since that can lead to deserialization cycles.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006583 continue;
6584 }
Stephen Hines176edba2014-12-01 14:53:08 -08006585
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006586 // Record this declaration.
6587 FoundAnything = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006588 if (This->DeclSet.insert(ND).second)
6589 This->Decls.push_back(ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006590 }
6591
6592 return FoundAnything;
6593 }
6594 };
6595}
6596
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006597/// \brief Retrieve the "definitive" module file for the definition of the
6598/// given declaration context, if there is one.
6599///
6600/// The "definitive" module file is the only place where we need to look to
6601/// find information about the declarations within the given declaration
6602/// context. For example, C++ and Objective-C classes, C structs/unions, and
6603/// Objective-C protocols, categories, and extensions are all defined in a
6604/// single place in the source code, so they have definitive module files
6605/// associated with them. C++ namespaces, on the other hand, can have
6606/// definitions in multiple different module files.
6607///
6608/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6609/// NDEBUG checking.
6610static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6611 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00006612 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6613 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006614
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006615 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006616}
6617
Richard Smith3646c682013-02-07 03:30:24 +00006618bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006619ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6620 DeclarationName Name) {
6621 assert(DC->hasExternalVisibleStorage() &&
6622 "DeclContext has no visible decls in storage");
6623 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00006624 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006625
Stephen Hines176edba2014-12-01 14:53:08 -08006626 Deserializing LookupResults(this);
6627
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006628 SmallVector<NamedDecl *, 64> Decls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006629 llvm::SmallPtrSet<NamedDecl*, 64> DeclSet;
Stephen Hines176edba2014-12-01 14:53:08 -08006630
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006631 // Compute the declaration contexts we need to look into. Multiple such
6632 // declaration contexts occur when two declaration contexts from disjoint
6633 // modules get merged, e.g., when two namespaces with the same name are
6634 // independently defined in separate modules.
6635 SmallVector<const DeclContext *, 2> Contexts;
6636 Contexts.push_back(DC);
Stephen Hines176edba2014-12-01 14:53:08 -08006637
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006638 if (DC->isNamespace()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006639 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006640 if (Merged != MergedDecls.end()) {
6641 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6642 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6643 }
6644 }
Stephen Hines176edba2014-12-01 14:53:08 -08006645
6646 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006647 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls, DeclSet);
Stephen Hines176edba2014-12-01 14:53:08 -08006648
6649 // If we can definitively determine which module file to look into,
6650 // only look there. Otherwise, look in all module files.
6651 ModuleFile *Definitive;
6652 if (Contexts.size() == 1 &&
6653 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6654 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6655 } else {
6656 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6657 }
6658 };
6659
6660 LookUpInContexts(Contexts);
6661
6662 // If this might be an implicit special member function, then also search
6663 // all merged definitions of the surrounding class. We need to search them
6664 // individually, because finding an entity in one of them doesn't imply that
6665 // we can't find a different entity in another one.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006666 if (isa<CXXRecordDecl>(DC)) {
Stephen Hines176edba2014-12-01 14:53:08 -08006667 auto Kind = Name.getNameKind();
6668 if (Kind == DeclarationName::CXXConstructorName ||
6669 Kind == DeclarationName::CXXDestructorName ||
6670 (Kind == DeclarationName::CXXOperatorName &&
6671 Name.getCXXOverloadedOperator() == OO_Equal)) {
6672 auto Merged = MergedLookups.find(DC);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006673 if (Merged != MergedLookups.end()) {
6674 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6675 const DeclContext *Context = Merged->second[I];
6676 LookUpInContexts(Context);
6677 // We might have just added some more merged lookups. If so, our
6678 // iterator is now invalid, so grab a fresh one before continuing.
6679 Merged = MergedLookups.find(DC);
6680 }
6681 }
Stephen Hines176edba2014-12-01 14:53:08 -08006682 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006683 }
6684
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006685 ++NumVisibleDeclContextsRead;
6686 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00006687 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006688}
6689
6690namespace {
6691 /// \brief ModuleFile visitor used to retrieve all visible names in a
6692 /// declaration context.
6693 class DeclContextAllNamesVisitor {
6694 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006695 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper8ae63872013-07-05 04:43:31 +00006696 DeclsMap &Decls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006697 llvm::SmallPtrSet<NamedDecl *, 256> DeclSet;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006698 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006699
6700 public:
6701 DeclContextAllNamesVisitor(ASTReader &Reader,
6702 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper8ae63872013-07-05 04:43:31 +00006703 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006704 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006705
6706 static bool visit(ModuleFile &M, void *UserData) {
6707 DeclContextAllNamesVisitor *This
6708 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6709
6710 // Check whether we have any visible declaration information for
6711 // this context in this module.
6712 ModuleFile::DeclContextInfosMap::iterator Info;
6713 bool FoundInfo = false;
6714 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6715 Info = M.DeclContextInfos.find(This->Contexts[I]);
6716 if (Info != M.DeclContextInfos.end() &&
6717 Info->second.NameLookupTableData) {
6718 FoundInfo = true;
6719 break;
6720 }
6721 }
6722
6723 if (!FoundInfo)
6724 return false;
6725
6726 ASTDeclContextNameLookupTable *LookupTable =
6727 Info->second.NameLookupTableData;
6728 bool FoundAnything = false;
6729 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00006730 I = LookupTable->data_begin(), E = LookupTable->data_end();
6731 I != E;
6732 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006733 ASTDeclContextNameLookupTrait::data_type Data = *I;
6734 for (; Data.first != Data.second; ++Data.first) {
6735 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6736 *Data.first);
6737 if (!ND)
6738 continue;
6739
6740 // Record this declaration.
6741 FoundAnything = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006742 if (This->DeclSet.insert(ND).second)
6743 This->Decls[ND->getDeclName()].push_back(ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006744 }
6745 }
6746
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006747 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006748 }
6749 };
6750}
6751
6752void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6753 if (!DC->hasExternalVisibleStorage())
6754 return;
Craig Topperee0a4792013-07-05 04:33:53 +00006755 DeclsMap Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006756
6757 // Compute the declaration contexts we need to look into. Multiple such
6758 // declaration contexts occur when two declaration contexts from disjoint
6759 // modules get merged, e.g., when two namespaces with the same name are
6760 // independently defined in separate modules.
6761 SmallVector<const DeclContext *, 2> Contexts;
6762 Contexts.push_back(DC);
6763
6764 if (DC->isNamespace()) {
6765 MergedDeclsMap::iterator Merged
6766 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6767 if (Merged != MergedDecls.end()) {
6768 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6769 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6770 }
6771 }
6772
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006773 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6774 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006775 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6776 ++NumVisibleDeclContextsRead;
6777
Craig Topperee0a4792013-07-05 04:33:53 +00006778 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006779 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6780 }
6781 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6782}
6783
6784/// \brief Under non-PCH compilation the consumer receives the objc methods
6785/// before receiving the implementation, and codegen depends on this.
6786/// We simulate this by deserializing and passing to consumer the methods of the
6787/// implementation before passing the deserialized implementation decl.
6788static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6789 ASTConsumer *Consumer) {
6790 assert(ImplD && Consumer);
6791
Stephen Hines651f13c2014-04-23 16:59:28 -07006792 for (auto *I : ImplD->methods())
6793 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006794
6795 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6796}
6797
6798void ASTReader::PassInterestingDeclsToConsumer() {
6799 assert(Consumer);
Stephen Hines651f13c2014-04-23 16:59:28 -07006800
6801 if (PassingDeclsToConsumer)
6802 return;
6803
6804 // Guard variable to avoid recursively redoing the process of passing
6805 // decls to consumer.
6806 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6807 true);
6808
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006809 while (!InterestingDecls.empty()) {
6810 Decl *D = InterestingDecls.front();
6811 InterestingDecls.pop_front();
6812
6813 PassInterestingDeclToConsumer(D);
6814 }
6815}
6816
6817void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6818 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6819 PassObjCImplDeclToConsumer(ImplD, Consumer);
6820 else
6821 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6822}
6823
6824void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6825 this->Consumer = Consumer;
6826
6827 if (!Consumer)
6828 return;
6829
Stephen Hines651f13c2014-04-23 16:59:28 -07006830 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006831 // Force deserialization of this decl, which will cause it to be queued for
6832 // passing to the consumer.
Stephen Hines651f13c2014-04-23 16:59:28 -07006833 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006834 }
Stephen Hines651f13c2014-04-23 16:59:28 -07006835 EagerlyDeserializedDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006836
6837 PassInterestingDeclsToConsumer();
6838}
6839
6840void ASTReader::PrintStats() {
6841 std::fprintf(stderr, "*** AST File Statistics:\n");
6842
6843 unsigned NumTypesLoaded
6844 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6845 QualType());
6846 unsigned NumDeclsLoaded
6847 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006848 (Decl *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006849 unsigned NumIdentifiersLoaded
6850 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6851 IdentifiersLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006852 (IdentifierInfo *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006853 unsigned NumMacrosLoaded
6854 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6855 MacrosLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006856 (MacroInfo *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006857 unsigned NumSelectorsLoaded
6858 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6859 SelectorsLoaded.end(),
6860 Selector());
6861
6862 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6863 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6864 NumSLocEntriesRead, TotalNumSLocEntries,
6865 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6866 if (!TypesLoaded.empty())
6867 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6868 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6869 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6870 if (!DeclsLoaded.empty())
6871 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6872 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6873 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6874 if (!IdentifiersLoaded.empty())
6875 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6876 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6877 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6878 if (!MacrosLoaded.empty())
6879 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6880 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6881 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6882 if (!SelectorsLoaded.empty())
6883 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6884 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6885 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6886 if (TotalNumStatements)
6887 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6888 NumStatementsRead, TotalNumStatements,
6889 ((float)NumStatementsRead/TotalNumStatements * 100));
6890 if (TotalNumMacros)
6891 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6892 NumMacrosRead, TotalNumMacros,
6893 ((float)NumMacrosRead/TotalNumMacros * 100));
6894 if (TotalLexicalDeclContexts)
6895 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6896 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6897 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6898 * 100));
6899 if (TotalVisibleDeclContexts)
6900 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6901 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6902 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6903 * 100));
6904 if (TotalNumMethodPoolEntries) {
6905 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6906 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6907 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6908 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006909 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006910 if (NumMethodPoolLookups) {
6911 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6912 NumMethodPoolHits, NumMethodPoolLookups,
6913 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6914 }
6915 if (NumMethodPoolTableLookups) {
6916 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6917 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6918 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6919 * 100.0));
6920 }
6921
Douglas Gregore1698072013-01-25 00:38:33 +00006922 if (NumIdentifierLookupHits) {
6923 std::fprintf(stderr,
6924 " %u / %u identifier table lookups succeeded (%f%%)\n",
6925 NumIdentifierLookupHits, NumIdentifierLookups,
6926 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6927 }
6928
Douglas Gregor1a49d972013-01-25 01:03:03 +00006929 if (GlobalIndex) {
6930 std::fprintf(stderr, "\n");
6931 GlobalIndex->printStats();
6932 }
6933
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006934 std::fprintf(stderr, "\n");
6935 dump();
6936 std::fprintf(stderr, "\n");
6937}
6938
6939template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6940static void
6941dumpModuleIDMap(StringRef Name,
6942 const ContinuousRangeMap<Key, ModuleFile *,
6943 InitialCapacity> &Map) {
6944 if (Map.begin() == Map.end())
6945 return;
6946
6947 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6948 llvm::errs() << Name << ":\n";
6949 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6950 I != IEnd; ++I) {
6951 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6952 << "\n";
6953 }
6954}
6955
6956void ASTReader::dump() {
6957 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6958 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6959 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6960 dumpModuleIDMap("Global type map", GlobalTypeMap);
6961 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6962 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6963 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6964 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6965 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6966 dumpModuleIDMap("Global preprocessed entity map",
6967 GlobalPreprocessedEntityMap);
6968
6969 llvm::errs() << "\n*** PCH/Modules Loaded:";
6970 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6971 MEnd = ModuleMgr.end();
6972 M != MEnd; ++M)
6973 (*M)->dump();
6974}
6975
6976/// Return the amount of memory used by memory buffers, breaking down
6977/// by heap-backed versus mmap'ed memory.
6978void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6979 for (ModuleConstIterator I = ModuleMgr.begin(),
6980 E = ModuleMgr.end(); I != E; ++I) {
6981 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6982 size_t bytes = buf->getBufferSize();
6983 switch (buf->getBufferKind()) {
6984 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6985 sizes.malloc_bytes += bytes;
6986 break;
6987 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6988 sizes.mmap_bytes += bytes;
6989 break;
6990 }
6991 }
6992 }
6993}
6994
6995void ASTReader::InitializeSema(Sema &S) {
6996 SemaObj = &S;
6997 S.addExternalSource(this);
6998
6999 // Makes sure any declarations that were deserialized "too early"
7000 // still get added to the identifier's declaration chains.
Stephen Hines176edba2014-12-01 14:53:08 -08007001 for (uint64_t ID : PreloadedDeclIDs) {
7002 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7003 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007004 }
Stephen Hines176edba2014-12-01 14:53:08 -08007005 PreloadedDeclIDs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007006
Richard Smith9b671182013-10-18 06:54:39 +00007007 // FIXME: What happens if these are changed by a module import?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007008 if (!FPPragmaOptions.empty()) {
7009 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7010 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
7011 }
7012
Richard Smith9b671182013-10-18 06:54:39 +00007013 // FIXME: What happens if these are changed by a module import?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007014 if (!OpenCLExtensions.empty()) {
7015 unsigned I = 0;
7016#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
7017#include "clang/Basic/OpenCLExtensions.def"
7018
7019 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
7020 }
Richard Smith9b671182013-10-18 06:54:39 +00007021
7022 UpdateSema();
7023}
7024
7025void ASTReader::UpdateSema() {
7026 assert(SemaObj && "no Sema to update");
7027
7028 // Load the offsets of the declarations that Sema references.
7029 // They will be lazily deserialized when needed.
7030 if (!SemaDeclRefs.empty()) {
7031 assert(SemaDeclRefs.size() % 2 == 0);
7032 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
7033 if (!SemaObj->StdNamespace)
7034 SemaObj->StdNamespace = SemaDeclRefs[I];
7035 if (!SemaObj->StdBadAlloc)
7036 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7037 }
7038 SemaDeclRefs.clear();
7039 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007040
7041 // Update the state of 'pragma clang optimize'. Use the same API as if we had
7042 // encountered the pragma in the source.
7043 if(OptimizeOffPragmaLocation.isValid())
7044 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007045}
7046
7047IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
7048 // Note that we are loading an identifier.
7049 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00007050 StringRef Name(NameStart, NameEnd - NameStart);
7051
7052 // If there is a global index, look there first to determine which modules
7053 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00007054 GlobalModuleIndex::HitSet Hits;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007055 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregor1a49d972013-01-25 01:03:03 +00007056 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00007057 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7058 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00007059 }
7060 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00007061 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00007062 NumIdentifierLookups,
7063 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00007064 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007065 IdentifierInfo *II = Visitor.getIdentifierInfo();
7066 markIdentifierUpToDate(II);
7067 return II;
7068}
7069
7070namespace clang {
7071 /// \brief An identifier-lookup iterator that enumerates all of the
7072 /// identifiers stored within a set of AST files.
7073 class ASTIdentifierIterator : public IdentifierIterator {
7074 /// \brief The AST reader whose identifiers are being enumerated.
7075 const ASTReader &Reader;
7076
7077 /// \brief The current index into the chain of AST files stored in
7078 /// the AST reader.
7079 unsigned Index;
7080
7081 /// \brief The current position within the identifier lookup table
7082 /// of the current AST file.
7083 ASTIdentifierLookupTable::key_iterator Current;
7084
7085 /// \brief The end position within the identifier lookup table of
7086 /// the current AST file.
7087 ASTIdentifierLookupTable::key_iterator End;
7088
7089 public:
7090 explicit ASTIdentifierIterator(const ASTReader &Reader);
7091
Stephen Hines651f13c2014-04-23 16:59:28 -07007092 StringRef Next() override;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007093 };
7094}
7095
7096ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
7097 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
7098 ASTIdentifierLookupTable *IdTable
7099 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7100 Current = IdTable->key_begin();
7101 End = IdTable->key_end();
7102}
7103
7104StringRef ASTIdentifierIterator::Next() {
7105 while (Current == End) {
7106 // If we have exhausted all of our AST files, we're done.
7107 if (Index == 0)
7108 return StringRef();
7109
7110 --Index;
7111 ASTIdentifierLookupTable *IdTable
7112 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7113 IdentifierLookupTable;
7114 Current = IdTable->key_begin();
7115 End = IdTable->key_end();
7116 }
7117
7118 // We have any identifiers remaining in the current AST file; return
7119 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00007120 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007121 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00007122 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007123}
7124
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00007125IdentifierIterator *ASTReader::getIdentifiers() {
7126 if (!loadGlobalIndex())
7127 return GlobalIndex->createIdentifierIterator();
7128
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007129 return new ASTIdentifierIterator(*this);
7130}
7131
7132namespace clang { namespace serialization {
7133 class ReadMethodPoolVisitor {
7134 ASTReader &Reader;
7135 Selector Sel;
7136 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00007137 unsigned InstanceBits;
7138 unsigned FactoryBits;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007139 bool InstanceHasMoreThanOneDecl;
7140 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00007141 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7142 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007143
7144 public:
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007145 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007146 unsigned PriorGeneration)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007147 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
7148 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7149 FactoryHasMoreThanOneDecl(false) {}
7150
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007151 static bool visit(ModuleFile &M, void *UserData) {
7152 ReadMethodPoolVisitor *This
7153 = static_cast<ReadMethodPoolVisitor *>(UserData);
7154
7155 if (!M.SelectorLookupTable)
7156 return false;
7157
7158 // If we've already searched this module file, skip it now.
7159 if (M.Generation <= This->PriorGeneration)
7160 return true;
7161
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007162 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007163 ASTSelectorLookupTable *PoolTable
7164 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7165 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
7166 if (Pos == PoolTable->end())
7167 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007168
7169 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007170 ++This->Reader.NumSelectorsRead;
7171 // FIXME: Not quite happy with the statistics here. We probably should
7172 // disable this tracking when called via LoadSelector.
7173 // Also, should entries without methods count as misses?
7174 ++This->Reader.NumMethodPoolEntriesRead;
7175 ASTSelectorLookupTrait::data_type Data = *Pos;
7176 if (This->Reader.DeserializationListener)
7177 This->Reader.DeserializationListener->SelectorRead(Data.ID,
7178 This->Sel);
7179
7180 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7181 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00007182 This->InstanceBits = Data.InstanceBits;
7183 This->FactoryBits = Data.FactoryBits;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007184 This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7185 This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007186 return true;
7187 }
7188
7189 /// \brief Retrieve the instance methods found by this visitor.
7190 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7191 return InstanceMethods;
7192 }
7193
7194 /// \brief Retrieve the instance methods found by this visitor.
7195 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7196 return FactoryMethods;
7197 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00007198
7199 unsigned getInstanceBits() const { return InstanceBits; }
7200 unsigned getFactoryBits() const { return FactoryBits; }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007201 bool instanceHasMoreThanOneDecl() const {
7202 return InstanceHasMoreThanOneDecl;
7203 }
7204 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007205 };
7206} } // end namespace clang::serialization
7207
7208/// \brief Add the given set of methods to the method list.
7209static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7210 ObjCMethodList &List) {
7211 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7212 S.addMethodToGlobalList(&List, Methods[I]);
7213 }
7214}
7215
7216void ASTReader::ReadMethodPool(Selector Sel) {
7217 // Get the selector generation and update it to the current generation.
7218 unsigned &Generation = SelectorGeneration[Sel];
7219 unsigned PriorGeneration = Generation;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007220 Generation = getGeneration();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007221
7222 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007223 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007224 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7225 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7226
7227 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007228 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007229 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007230
7231 ++NumMethodPoolHits;
7232
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007233 if (!getSema())
7234 return;
7235
7236 Sema &S = *getSema();
7237 Sema::GlobalMethodPool::iterator Pos
7238 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007239
7240 Pos->second.first.setBits(Visitor.getInstanceBits());
7241 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
7242 Pos->second.second.setBits(Visitor.getFactoryBits());
7243 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
7244
7245 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7246 // when building a module we keep every method individually and may need to
7247 // update hasMoreThanOneDecl as we add the methods.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007248 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7249 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7250}
7251
7252void ASTReader::ReadKnownNamespaces(
7253 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7254 Namespaces.clear();
7255
7256 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7257 if (NamespaceDecl *Namespace
7258 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7259 Namespaces.push_back(Namespace);
7260 }
7261}
7262
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007263void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00007264 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007265 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7266 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00007267 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007268 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00007269 Undefined.insert(std::make_pair(D, Loc));
7270 }
7271}
Nick Lewycky01a41142013-01-26 00:35:08 +00007272
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007273void ASTReader::ReadTentativeDefinitions(
7274 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7275 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7276 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7277 if (Var)
7278 TentativeDefs.push_back(Var);
7279 }
7280 TentativeDefinitions.clear();
7281}
7282
7283void ASTReader::ReadUnusedFileScopedDecls(
7284 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7285 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7286 DeclaratorDecl *D
7287 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7288 if (D)
7289 Decls.push_back(D);
7290 }
7291 UnusedFileScopedDecls.clear();
7292}
7293
7294void ASTReader::ReadDelegatingConstructors(
7295 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7296 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7297 CXXConstructorDecl *D
7298 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7299 if (D)
7300 Decls.push_back(D);
7301 }
7302 DelegatingCtorDecls.clear();
7303}
7304
7305void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7306 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7307 TypedefNameDecl *D
7308 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7309 if (D)
7310 Decls.push_back(D);
7311 }
7312 ExtVectorDecls.clear();
7313}
7314
7315void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7316 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7317 CXXRecordDecl *D
7318 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7319 if (D)
7320 Decls.push_back(D);
7321 }
7322 DynamicClasses.clear();
7323}
7324
Stephen Hines176edba2014-12-01 14:53:08 -08007325void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7326 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7327 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7328 ++I) {
7329 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7330 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7331 if (D)
7332 Decls.insert(D);
7333 }
7334 UnusedLocalTypedefNameCandidates.clear();
7335}
7336
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007337void
Richard Smith5ea6ef42013-01-10 23:43:47 +00007338ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7339 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7340 NamedDecl *D
7341 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007342 if (D)
7343 Decls.push_back(D);
7344 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00007345 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007346}
7347
7348void ASTReader::ReadReferencedSelectors(
7349 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7350 if (ReferencedSelectorsData.empty())
7351 return;
7352
7353 // If there are @selector references added them to its pool. This is for
7354 // implementation of -Wselector.
7355 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7356 unsigned I = 0;
7357 while (I < DataSize) {
7358 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7359 SourceLocation SelLoc
7360 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7361 Sels.push_back(std::make_pair(Sel, SelLoc));
7362 }
7363 ReferencedSelectorsData.clear();
7364}
7365
7366void ASTReader::ReadWeakUndeclaredIdentifiers(
7367 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7368 if (WeakUndeclaredIdentifiers.empty())
7369 return;
7370
7371 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7372 IdentifierInfo *WeakId
7373 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7374 IdentifierInfo *AliasId
7375 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7376 SourceLocation Loc
7377 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7378 bool Used = WeakUndeclaredIdentifiers[I++];
7379 WeakInfo WI(AliasId, Loc);
7380 WI.setUsed(Used);
7381 WeakIDs.push_back(std::make_pair(WeakId, WI));
7382 }
7383 WeakUndeclaredIdentifiers.clear();
7384}
7385
7386void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7387 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7388 ExternalVTableUse VT;
7389 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7390 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7391 VT.DefinitionRequired = VTableUses[Idx++];
7392 VTables.push_back(VT);
7393 }
7394
7395 VTableUses.clear();
7396}
7397
7398void ASTReader::ReadPendingInstantiations(
7399 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7400 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7401 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7402 SourceLocation Loc
7403 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7404
7405 Pending.push_back(std::make_pair(D, Loc));
7406 }
7407 PendingInstantiations.clear();
7408}
7409
Richard Smithac32d902013-08-07 21:41:30 +00007410void ASTReader::ReadLateParsedTemplates(
7411 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7412 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7413 /* In loop */) {
7414 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7415
7416 LateParsedTemplate *LT = new LateParsedTemplate;
7417 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7418
7419 ModuleFile *F = getOwningModuleFile(LT->D);
7420 assert(F && "No module");
7421
7422 unsigned TokN = LateParsedTemplates[Idx++];
7423 LT->Toks.reserve(TokN);
7424 for (unsigned T = 0; T < TokN; ++T)
7425 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7426
7427 LPTMap[FD] = LT;
7428 }
7429
7430 LateParsedTemplates.clear();
7431}
7432
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007433void ASTReader::LoadSelector(Selector Sel) {
7434 // It would be complicated to avoid reading the methods anyway. So don't.
7435 ReadMethodPool(Sel);
7436}
7437
7438void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7439 assert(ID && "Non-zero identifier ID required");
7440 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7441 IdentifiersLoaded[ID - 1] = II;
7442 if (DeserializationListener)
7443 DeserializationListener->IdentifierRead(ID, II);
7444}
7445
7446/// \brief Set the globally-visible declarations associated with the given
7447/// identifier.
7448///
7449/// If the AST reader is currently in a state where the given declaration IDs
7450/// cannot safely be resolved, they are queued until it is safe to resolve
7451/// them.
7452///
7453/// \param II an IdentifierInfo that refers to one or more globally-visible
7454/// declarations.
7455///
7456/// \param DeclIDs the set of declaration IDs with the name @p II that are
7457/// visible at global scope.
7458///
Douglas Gregoraa945902013-02-18 15:53:43 +00007459/// \param Decls if non-null, this vector will be populated with the set of
7460/// deserialized declarations. These declarations will not be pushed into
7461/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007462void
7463ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7464 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00007465 SmallVectorImpl<Decl *> *Decls) {
7466 if (NumCurrentElementsDeserializing && !Decls) {
7467 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007468 return;
7469 }
7470
7471 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Stephen Hines176edba2014-12-01 14:53:08 -08007472 if (!SemaObj) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007473 // Queue this declaration so that it will be added to the
7474 // translation unit scope and identifier's declaration chain
7475 // once a Sema object is known.
Stephen Hines176edba2014-12-01 14:53:08 -08007476 PreloadedDeclIDs.push_back(DeclIDs[I]);
7477 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007478 }
Stephen Hines176edba2014-12-01 14:53:08 -08007479
7480 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7481
7482 // If we're simply supposed to record the declarations, do so now.
7483 if (Decls) {
7484 Decls->push_back(D);
7485 continue;
7486 }
7487
7488 // Introduce this declaration into the translation-unit scope
7489 // and add it to the declaration chain for this identifier, so
7490 // that (unqualified) name lookup will find it.
7491 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007492 }
7493}
7494
Douglas Gregor8222b892013-01-21 16:52:34 +00007495IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007496 if (ID == 0)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007497 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007498
7499 if (IdentifiersLoaded.empty()) {
7500 Error("no identifier table in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007501 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007502 }
7503
7504 ID -= 1;
7505 if (!IdentifiersLoaded[ID]) {
7506 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7507 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7508 ModuleFile *M = I->second;
7509 unsigned Index = ID - M->BaseIdentifierID;
7510 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7511
7512 // All of the strings in the AST file are preceded by a 16-bit length.
7513 // Extract that 16-bit length to avoid having to execute strlen().
7514 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7515 // unsigned integers. This is important to avoid integer overflow when
7516 // we cast them to 'unsigned'.
7517 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7518 unsigned StrLen = (((unsigned) StrLenPtr[0])
7519 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00007520 IdentifiersLoaded[ID]
7521 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007522 if (DeserializationListener)
7523 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7524 }
7525
7526 return IdentifiersLoaded[ID];
7527}
7528
Douglas Gregor8222b892013-01-21 16:52:34 +00007529IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7530 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007531}
7532
7533IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7534 if (LocalID < NUM_PREDEF_IDENT_IDS)
7535 return LocalID;
7536
7537 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7538 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7539 assert(I != M.IdentifierRemap.end()
7540 && "Invalid index into identifier index remap");
7541
7542 return LocalID + I->second;
7543}
7544
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007545MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007546 if (ID == 0)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007547 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007548
7549 if (MacrosLoaded.empty()) {
7550 Error("no macro table in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007551 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007552 }
7553
7554 ID -= NUM_PREDEF_MACRO_IDS;
7555 if (!MacrosLoaded[ID]) {
7556 GlobalMacroMapType::iterator I
7557 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7558 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7559 ModuleFile *M = I->second;
7560 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007561 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7562
7563 if (DeserializationListener)
7564 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7565 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007566 }
7567
7568 return MacrosLoaded[ID];
7569}
7570
7571MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7572 if (LocalID < NUM_PREDEF_MACRO_IDS)
7573 return LocalID;
7574
7575 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7576 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7577 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7578
7579 return LocalID + I->second;
7580}
7581
7582serialization::SubmoduleID
7583ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7584 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7585 return LocalID;
7586
7587 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7588 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7589 assert(I != M.SubmoduleRemap.end()
7590 && "Invalid index into submodule index remap");
7591
7592 return LocalID + I->second;
7593}
7594
7595Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7596 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7597 assert(GlobalID == 0 && "Unhandled global submodule ID");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007598 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007599 }
7600
7601 if (GlobalID > SubmodulesLoaded.size()) {
7602 Error("submodule ID out of range in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007603 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007604 }
7605
7606 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7607}
Douglas Gregorca2ab452013-01-12 01:29:50 +00007608
7609Module *ASTReader::getModule(unsigned ID) {
7610 return getSubmodule(ID);
7611}
7612
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007613Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7614 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7615}
7616
7617Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7618 if (ID == 0)
7619 return Selector();
7620
7621 if (ID > SelectorsLoaded.size()) {
7622 Error("selector ID out of range in AST file");
7623 return Selector();
7624 }
7625
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007626 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007627 // Load this selector from the selector table.
7628 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7629 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7630 ModuleFile &M = *I->second;
7631 ASTSelectorLookupTrait Trait(*this, M);
7632 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7633 SelectorsLoaded[ID - 1] =
7634 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7635 if (DeserializationListener)
7636 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7637 }
7638
7639 return SelectorsLoaded[ID - 1];
7640}
7641
7642Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7643 return DecodeSelector(ID);
7644}
7645
7646uint32_t ASTReader::GetNumExternalSelectors() {
7647 // ID 0 (the null selector) is considered an external selector.
7648 return getTotalNumSelectors() + 1;
7649}
7650
7651serialization::SelectorID
7652ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7653 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7654 return LocalID;
7655
7656 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7657 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7658 assert(I != M.SelectorRemap.end()
7659 && "Invalid index into selector index remap");
7660
7661 return LocalID + I->second;
7662}
7663
7664DeclarationName
7665ASTReader::ReadDeclarationName(ModuleFile &F,
7666 const RecordData &Record, unsigned &Idx) {
7667 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7668 switch (Kind) {
7669 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00007670 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007671
7672 case DeclarationName::ObjCZeroArgSelector:
7673 case DeclarationName::ObjCOneArgSelector:
7674 case DeclarationName::ObjCMultiArgSelector:
7675 return DeclarationName(ReadSelector(F, Record, Idx));
7676
7677 case DeclarationName::CXXConstructorName:
7678 return Context.DeclarationNames.getCXXConstructorName(
7679 Context.getCanonicalType(readType(F, Record, Idx)));
7680
7681 case DeclarationName::CXXDestructorName:
7682 return Context.DeclarationNames.getCXXDestructorName(
7683 Context.getCanonicalType(readType(F, Record, Idx)));
7684
7685 case DeclarationName::CXXConversionFunctionName:
7686 return Context.DeclarationNames.getCXXConversionFunctionName(
7687 Context.getCanonicalType(readType(F, Record, Idx)));
7688
7689 case DeclarationName::CXXOperatorName:
7690 return Context.DeclarationNames.getCXXOperatorName(
7691 (OverloadedOperatorKind)Record[Idx++]);
7692
7693 case DeclarationName::CXXLiteralOperatorName:
7694 return Context.DeclarationNames.getCXXLiteralOperatorName(
7695 GetIdentifierInfo(F, Record, Idx));
7696
7697 case DeclarationName::CXXUsingDirective:
7698 return DeclarationName::getUsingDirectiveName();
7699 }
7700
7701 llvm_unreachable("Invalid NameKind!");
7702}
7703
7704void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7705 DeclarationNameLoc &DNLoc,
7706 DeclarationName Name,
7707 const RecordData &Record, unsigned &Idx) {
7708 switch (Name.getNameKind()) {
7709 case DeclarationName::CXXConstructorName:
7710 case DeclarationName::CXXDestructorName:
7711 case DeclarationName::CXXConversionFunctionName:
7712 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7713 break;
7714
7715 case DeclarationName::CXXOperatorName:
7716 DNLoc.CXXOperatorName.BeginOpNameLoc
7717 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7718 DNLoc.CXXOperatorName.EndOpNameLoc
7719 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7720 break;
7721
7722 case DeclarationName::CXXLiteralOperatorName:
7723 DNLoc.CXXLiteralOperatorName.OpNameLoc
7724 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7725 break;
7726
7727 case DeclarationName::Identifier:
7728 case DeclarationName::ObjCZeroArgSelector:
7729 case DeclarationName::ObjCOneArgSelector:
7730 case DeclarationName::ObjCMultiArgSelector:
7731 case DeclarationName::CXXUsingDirective:
7732 break;
7733 }
7734}
7735
7736void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7737 DeclarationNameInfo &NameInfo,
7738 const RecordData &Record, unsigned &Idx) {
7739 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7740 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7741 DeclarationNameLoc DNLoc;
7742 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7743 NameInfo.setInfo(DNLoc);
7744}
7745
7746void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7747 const RecordData &Record, unsigned &Idx) {
7748 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7749 unsigned NumTPLists = Record[Idx++];
7750 Info.NumTemplParamLists = NumTPLists;
7751 if (NumTPLists) {
7752 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7753 for (unsigned i=0; i != NumTPLists; ++i)
7754 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7755 }
7756}
7757
7758TemplateName
7759ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7760 unsigned &Idx) {
7761 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7762 switch (Kind) {
7763 case TemplateName::Template:
7764 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7765
7766 case TemplateName::OverloadedTemplate: {
7767 unsigned size = Record[Idx++];
7768 UnresolvedSet<8> Decls;
7769 while (size--)
7770 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7771
7772 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7773 }
7774
7775 case TemplateName::QualifiedTemplate: {
7776 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7777 bool hasTemplKeyword = Record[Idx++];
7778 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7779 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7780 }
7781
7782 case TemplateName::DependentTemplate: {
7783 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7784 if (Record[Idx++]) // isIdentifier
7785 return Context.getDependentTemplateName(NNS,
7786 GetIdentifierInfo(F, Record,
7787 Idx));
7788 return Context.getDependentTemplateName(NNS,
7789 (OverloadedOperatorKind)Record[Idx++]);
7790 }
7791
7792 case TemplateName::SubstTemplateTemplateParm: {
7793 TemplateTemplateParmDecl *param
7794 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7795 if (!param) return TemplateName();
7796 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7797 return Context.getSubstTemplateTemplateParm(param, replacement);
7798 }
7799
7800 case TemplateName::SubstTemplateTemplateParmPack: {
7801 TemplateTemplateParmDecl *Param
7802 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7803 if (!Param)
7804 return TemplateName();
7805
7806 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7807 if (ArgPack.getKind() != TemplateArgument::Pack)
7808 return TemplateName();
7809
7810 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7811 }
7812 }
7813
7814 llvm_unreachable("Unhandled template name kind!");
7815}
7816
7817TemplateArgument
7818ASTReader::ReadTemplateArgument(ModuleFile &F,
7819 const RecordData &Record, unsigned &Idx) {
7820 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7821 switch (Kind) {
7822 case TemplateArgument::Null:
7823 return TemplateArgument();
7824 case TemplateArgument::Type:
7825 return TemplateArgument(readType(F, Record, Idx));
7826 case TemplateArgument::Declaration: {
7827 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
Stephen Hines176edba2014-12-01 14:53:08 -08007828 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007829 }
7830 case TemplateArgument::NullPtr:
7831 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7832 case TemplateArgument::Integral: {
7833 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7834 QualType T = readType(F, Record, Idx);
7835 return TemplateArgument(Context, Value, T);
7836 }
7837 case TemplateArgument::Template:
7838 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7839 case TemplateArgument::TemplateExpansion: {
7840 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00007841 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007842 if (unsigned NumExpansions = Record[Idx++])
7843 NumTemplateExpansions = NumExpansions - 1;
7844 return TemplateArgument(Name, NumTemplateExpansions);
7845 }
7846 case TemplateArgument::Expression:
7847 return TemplateArgument(ReadExpr(F));
7848 case TemplateArgument::Pack: {
7849 unsigned NumArgs = Record[Idx++];
7850 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7851 for (unsigned I = 0; I != NumArgs; ++I)
7852 Args[I] = ReadTemplateArgument(F, Record, Idx);
7853 return TemplateArgument(Args, NumArgs);
7854 }
7855 }
7856
7857 llvm_unreachable("Unhandled template argument kind!");
7858}
7859
7860TemplateParameterList *
7861ASTReader::ReadTemplateParameterList(ModuleFile &F,
7862 const RecordData &Record, unsigned &Idx) {
7863 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7864 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7865 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7866
7867 unsigned NumParams = Record[Idx++];
7868 SmallVector<NamedDecl *, 16> Params;
7869 Params.reserve(NumParams);
7870 while (NumParams--)
7871 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7872
7873 TemplateParameterList* TemplateParams =
7874 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7875 Params.data(), Params.size(), RAngleLoc);
7876 return TemplateParams;
7877}
7878
7879void
7880ASTReader::
Craig Topper6b9240e2013-07-05 19:34:19 +00007881ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007882 ModuleFile &F, const RecordData &Record,
7883 unsigned &Idx) {
7884 unsigned NumTemplateArgs = Record[Idx++];
7885 TemplArgs.reserve(NumTemplateArgs);
7886 while (NumTemplateArgs--)
7887 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7888}
7889
7890/// \brief Read a UnresolvedSet structure.
Richard Smithc2d77572013-08-30 04:46:40 +00007891void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007892 const RecordData &Record, unsigned &Idx) {
7893 unsigned NumDecls = Record[Idx++];
7894 Set.reserve(Context, NumDecls);
7895 while (NumDecls--) {
Richard Smithc2d77572013-08-30 04:46:40 +00007896 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007897 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smithc2d77572013-08-30 04:46:40 +00007898 Set.addLazyDecl(Context, ID, AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007899 }
7900}
7901
7902CXXBaseSpecifier
7903ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7904 const RecordData &Record, unsigned &Idx) {
7905 bool isVirtual = static_cast<bool>(Record[Idx++]);
7906 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7907 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7908 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7909 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7910 SourceRange Range = ReadSourceRange(F, Record, Idx);
7911 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7912 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7913 EllipsisLoc);
7914 Result.setInheritConstructors(inheritConstructors);
7915 return Result;
7916}
7917
7918std::pair<CXXCtorInitializer **, unsigned>
7919ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7920 unsigned &Idx) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007921 CXXCtorInitializer **CtorInitializers = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007922 unsigned NumInitializers = Record[Idx++];
7923 if (NumInitializers) {
7924 CtorInitializers
7925 = new (Context) CXXCtorInitializer*[NumInitializers];
7926 for (unsigned i=0; i != NumInitializers; ++i) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007927 TypeSourceInfo *TInfo = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007928 bool IsBaseVirtual = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007929 FieldDecl *Member = nullptr;
7930 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007931
7932 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7933 switch (Type) {
7934 case CTOR_INITIALIZER_BASE:
7935 TInfo = GetTypeSourceInfo(F, Record, Idx);
7936 IsBaseVirtual = Record[Idx++];
7937 break;
7938
7939 case CTOR_INITIALIZER_DELEGATING:
7940 TInfo = GetTypeSourceInfo(F, Record, Idx);
7941 break;
7942
7943 case CTOR_INITIALIZER_MEMBER:
7944 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7945 break;
7946
7947 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7948 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7949 break;
7950 }
7951
7952 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7953 Expr *Init = ReadExpr(F);
7954 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7955 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7956 bool IsWritten = Record[Idx++];
7957 unsigned SourceOrderOrNumArrayIndices;
7958 SmallVector<VarDecl *, 8> Indices;
7959 if (IsWritten) {
7960 SourceOrderOrNumArrayIndices = Record[Idx++];
7961 } else {
7962 SourceOrderOrNumArrayIndices = Record[Idx++];
7963 Indices.reserve(SourceOrderOrNumArrayIndices);
7964 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7965 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7966 }
7967
7968 CXXCtorInitializer *BOMInit;
7969 if (Type == CTOR_INITIALIZER_BASE) {
7970 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7971 LParenLoc, Init, RParenLoc,
7972 MemberOrEllipsisLoc);
7973 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7974 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7975 Init, RParenLoc);
7976 } else if (IsWritten) {
7977 if (Member)
7978 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7979 LParenLoc, Init, RParenLoc);
7980 else
7981 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7982 MemberOrEllipsisLoc, LParenLoc,
7983 Init, RParenLoc);
7984 } else {
Argyrios Kyrtzidisf8f480f2013-05-30 23:59:46 +00007985 if (IndirectMember) {
7986 assert(Indices.empty() && "Indirect field improperly initialized");
7987 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7988 MemberOrEllipsisLoc, LParenLoc,
7989 Init, RParenLoc);
7990 } else {
7991 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7992 LParenLoc, Init, RParenLoc,
7993 Indices.data(), Indices.size());
7994 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007995 }
7996
7997 if (IsWritten)
7998 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7999 CtorInitializers[i] = BOMInit;
8000 }
8001 }
8002
8003 return std::make_pair(CtorInitializers, NumInitializers);
8004}
8005
8006NestedNameSpecifier *
8007ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8008 const RecordData &Record, unsigned &Idx) {
8009 unsigned N = Record[Idx++];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008010 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008011 for (unsigned I = 0; I != N; ++I) {
8012 NestedNameSpecifier::SpecifierKind Kind
8013 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8014 switch (Kind) {
8015 case NestedNameSpecifier::Identifier: {
8016 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8017 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8018 break;
8019 }
8020
8021 case NestedNameSpecifier::Namespace: {
8022 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8023 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8024 break;
8025 }
8026
8027 case NestedNameSpecifier::NamespaceAlias: {
8028 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8029 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8030 break;
8031 }
8032
8033 case NestedNameSpecifier::TypeSpec:
8034 case NestedNameSpecifier::TypeSpecWithTemplate: {
8035 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8036 if (!T)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008037 return nullptr;
8038
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008039 bool Template = Record[Idx++];
8040 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8041 break;
8042 }
8043
8044 case NestedNameSpecifier::Global: {
8045 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8046 // No associated value, and there can't be a prefix.
8047 break;
8048 }
Stephen Hines176edba2014-12-01 14:53:08 -08008049
8050 case NestedNameSpecifier::Super: {
8051 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8052 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8053 break;
8054 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008055 }
8056 Prev = NNS;
8057 }
8058 return NNS;
8059}
8060
8061NestedNameSpecifierLoc
8062ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8063 unsigned &Idx) {
8064 unsigned N = Record[Idx++];
8065 NestedNameSpecifierLocBuilder Builder;
8066 for (unsigned I = 0; I != N; ++I) {
8067 NestedNameSpecifier::SpecifierKind Kind
8068 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8069 switch (Kind) {
8070 case NestedNameSpecifier::Identifier: {
8071 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8072 SourceRange Range = ReadSourceRange(F, Record, Idx);
8073 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8074 break;
8075 }
8076
8077 case NestedNameSpecifier::Namespace: {
8078 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8079 SourceRange Range = ReadSourceRange(F, Record, Idx);
8080 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8081 break;
8082 }
8083
8084 case NestedNameSpecifier::NamespaceAlias: {
8085 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8086 SourceRange Range = ReadSourceRange(F, Record, Idx);
8087 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8088 break;
8089 }
8090
8091 case NestedNameSpecifier::TypeSpec:
8092 case NestedNameSpecifier::TypeSpecWithTemplate: {
8093 bool Template = Record[Idx++];
8094 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8095 if (!T)
8096 return NestedNameSpecifierLoc();
8097 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8098
8099 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8100 Builder.Extend(Context,
8101 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8102 T->getTypeLoc(), ColonColonLoc);
8103 break;
8104 }
8105
8106 case NestedNameSpecifier::Global: {
8107 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8108 Builder.MakeGlobal(Context, ColonColonLoc);
8109 break;
8110 }
Stephen Hines176edba2014-12-01 14:53:08 -08008111
8112 case NestedNameSpecifier::Super: {
8113 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8114 SourceRange Range = ReadSourceRange(F, Record, Idx);
8115 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8116 break;
8117 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008118 }
8119 }
Stephen Hines176edba2014-12-01 14:53:08 -08008120
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008121 return Builder.getWithLocInContext(Context);
8122}
8123
8124SourceRange
8125ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8126 unsigned &Idx) {
8127 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8128 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8129 return SourceRange(beg, end);
8130}
8131
8132/// \brief Read an integral value
8133llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8134 unsigned BitWidth = Record[Idx++];
8135 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8136 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8137 Idx += NumWords;
8138 return Result;
8139}
8140
8141/// \brief Read a signed integral value
8142llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8143 bool isUnsigned = Record[Idx++];
8144 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8145}
8146
8147/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00008148llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8149 const llvm::fltSemantics &Sem,
8150 unsigned &Idx) {
8151 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008152}
8153
8154// \brief Read a string
8155std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8156 unsigned Len = Record[Idx++];
8157 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8158 Idx += Len;
8159 return Result;
8160}
8161
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008162std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8163 unsigned &Idx) {
8164 std::string Filename = ReadString(Record, Idx);
8165 ResolveImportedPath(F, Filename);
8166 return Filename;
8167}
8168
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008169VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8170 unsigned &Idx) {
8171 unsigned Major = Record[Idx++];
8172 unsigned Minor = Record[Idx++];
8173 unsigned Subminor = Record[Idx++];
8174 if (Minor == 0)
8175 return VersionTuple(Major);
8176 if (Subminor == 0)
8177 return VersionTuple(Major, Minor - 1);
8178 return VersionTuple(Major, Minor - 1, Subminor - 1);
8179}
8180
8181CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8182 const RecordData &Record,
8183 unsigned &Idx) {
8184 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8185 return CXXTemporary::Create(Context, Decl);
8186}
8187
8188DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00008189 return Diag(CurrentImportLoc, DiagID);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008190}
8191
8192DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8193 return Diags.Report(Loc, DiagID);
8194}
8195
8196/// \brief Retrieve the identifier table associated with the
8197/// preprocessor.
8198IdentifierTable &ASTReader::getIdentifierTable() {
8199 return PP.getIdentifierTable();
8200}
8201
8202/// \brief Record that the given ID maps to the given switch-case
8203/// statement.
8204void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008205 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008206 "Already have a SwitchCase with this ID");
8207 (*CurrSwitchCaseStmts)[ID] = SC;
8208}
8209
8210/// \brief Retrieve the switch-case statement with the given ID.
8211SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008212 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008213 return (*CurrSwitchCaseStmts)[ID];
8214}
8215
8216void ASTReader::ClearSwitchCaseIDs() {
8217 CurrSwitchCaseStmts->clear();
8218}
8219
8220void ASTReader::ReadComments() {
8221 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00008222 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008223 serialization::ModuleFile *> >::iterator
8224 I = CommentsCursors.begin(),
8225 E = CommentsCursors.end();
8226 I != E; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -07008227 Comments.clear();
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00008228 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008229 serialization::ModuleFile &F = *I->second;
8230 SavedStreamPosition SavedPosition(Cursor);
8231
8232 RecordData Record;
8233 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00008234 llvm::BitstreamEntry Entry =
8235 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Stephen Hines651f13c2014-04-23 16:59:28 -07008236
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00008237 switch (Entry.Kind) {
8238 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8239 case llvm::BitstreamEntry::Error:
8240 Error("malformed block record in AST file");
8241 return;
8242 case llvm::BitstreamEntry::EndBlock:
8243 goto NextCursor;
8244 case llvm::BitstreamEntry::Record:
8245 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008246 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008247 }
8248
8249 // Read a record.
8250 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00008251 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008252 case COMMENTS_RAW_COMMENT: {
8253 unsigned Idx = 0;
8254 SourceRange SR = ReadSourceRange(F, Record, Idx);
8255 RawComment::CommentKind Kind =
8256 (RawComment::CommentKind) Record[Idx++];
8257 bool IsTrailingComment = Record[Idx++];
8258 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00008259 Comments.push_back(new (Context) RawComment(
8260 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8261 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008262 break;
8263 }
8264 }
8265 }
Stephen Hines651f13c2014-04-23 16:59:28 -07008266 NextCursor:
8267 Context.Comments.addDeserializedComments(Comments);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008268 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008269}
8270
Stephen Hines176edba2014-12-01 14:53:08 -08008271void ASTReader::getInputFiles(ModuleFile &F,
8272 SmallVectorImpl<serialization::InputFile> &Files) {
8273 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8274 unsigned ID = I+1;
8275 Files.push_back(getInputFile(F, ID));
8276 }
8277}
8278
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008279std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8280 // If we know the owning module, use it.
8281 if (Module *M = D->getOwningModule())
8282 return M->getFullModuleName();
8283
8284 // Otherwise, use the name of the top-level module the decl is within.
8285 if (ModuleFile *M = getOwningModuleFile(D))
8286 return M->ModuleName;
8287
8288 // Not from a module.
8289 return "";
8290}
8291
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008292void ASTReader::finishPendingActions() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008293 while (!PendingIdentifierInfos.empty() ||
8294 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith3c40a282013-10-18 06:05:18 +00008295 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Stephen Hines176edba2014-12-01 14:53:08 -08008296 !PendingUpdateRecords.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008297 // If any identifiers with corresponding top-level declarations have
8298 // been loaded, load those declarations now.
Craig Topperee0a4792013-07-05 04:33:53 +00008299 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8300 TopLevelDeclsMap;
8301 TopLevelDeclsMap TopLevelDecls;
8302
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008303 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00008304 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Stephen Hines651f13c2014-04-23 16:59:28 -07008305 SmallVector<uint32_t, 4> DeclIDs =
8306 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00008307 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00008308
8309 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008310 }
Stephen Hines651f13c2014-04-23 16:59:28 -07008311
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008312 // For each decl chain that we wanted to complete while deserializing, mark
8313 // it as "still needs to be completed".
8314 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8315 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8316 }
8317 PendingIncompleteDeclChains.clear();
8318
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008319 // Load pending declaration chains.
8320 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8321 loadPendingDeclChain(PendingDeclChains[I]);
8322 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8323 }
8324 PendingDeclChains.clear();
8325
Douglas Gregoraa945902013-02-18 15:53:43 +00008326 // Make the most recent of the top-level declarations visible.
Craig Topperee0a4792013-07-05 04:33:53 +00008327 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8328 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregoraa945902013-02-18 15:53:43 +00008329 IdentifierInfo *II = TLD->first;
8330 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008331 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00008332 }
8333 }
8334
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008335 // Load any pending macro definitions.
8336 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008337 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8338 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8339 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8340 // Initialize the macro history from chained-PCHs ahead of module imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07008341 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00008342 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008343 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Stephen Hines176edba2014-12-01 14:53:08 -08008344 if (Info.M->Kind != MK_ImplicitModule &&
8345 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008346 resolvePendingMacro(II, Info);
8347 }
8348 // Handle module imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07008349 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008350 ++IDIdx) {
8351 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Stephen Hines176edba2014-12-01 14:53:08 -08008352 if (Info.M->Kind == MK_ImplicitModule ||
8353 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008354 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008355 }
8356 }
8357 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00008358
8359 // Wire up the DeclContexts for Decls that we delayed setting until
8360 // recursive loading is completed.
8361 while (!PendingDeclContextInfos.empty()) {
8362 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8363 PendingDeclContextInfos.pop_front();
8364 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8365 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8366 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8367 }
Richard Smith3c40a282013-10-18 06:05:18 +00008368
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008369 // Perform any pending declaration updates.
Stephen Hines176edba2014-12-01 14:53:08 -08008370 while (!PendingUpdateRecords.empty()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008371 auto Update = PendingUpdateRecords.pop_back_val();
8372 ReadingKindTracker ReadingKind(Read_Decl, *this);
8373 loadDeclUpdateRecords(Update.first, Update.second);
8374 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008375 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008376
8377 // At this point, all update records for loaded decls are in place, so any
8378 // fake class definitions should have become real.
8379 assert(PendingFakeDefinitionData.empty() &&
8380 "faked up a class definition but never saw the real one");
8381
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008382 // If we deserialized any C++ or Objective-C class definitions, any
8383 // Objective-C protocol definitions, or any redeclarable templates, make sure
8384 // that all redeclarations point to the definitions. Note that this can only
8385 // happen now, after the redeclaration chains have been fully wired.
Stephen Hines176edba2014-12-01 14:53:08 -08008386 for (Decl *D : PendingDefinitions) {
8387 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008388 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008389 // Make sure that the TagType points at the definition.
8390 const_cast<TagType*>(TagT)->decl = TD;
8391 }
8392
Stephen Hines176edba2014-12-01 14:53:08 -08008393 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
8394 for (auto R : RD->redecls()) {
8395 assert((R == D) == R->isThisDeclarationADefinition() &&
8396 "declaration thinks it's the definition but it isn't");
Stephen Hines651f13c2014-04-23 16:59:28 -07008397 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Stephen Hines176edba2014-12-01 14:53:08 -08008398 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008399 }
8400
8401 continue;
8402 }
8403
Stephen Hines176edba2014-12-01 14:53:08 -08008404 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008405 // Make sure that the ObjCInterfaceType points at the definition.
8406 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8407 ->Decl = ID;
8408
Stephen Hines651f13c2014-04-23 16:59:28 -07008409 for (auto R : ID->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008410 R->Data = ID->Data;
8411
8412 continue;
8413 }
8414
Stephen Hines176edba2014-12-01 14:53:08 -08008415 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07008416 for (auto R : PD->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008417 R->Data = PD->Data;
8418
8419 continue;
8420 }
8421
Stephen Hines176edba2014-12-01 14:53:08 -08008422 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -07008423 for (auto R : RTD->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008424 R->Common = RTD->Common;
8425 }
8426 PendingDefinitions.clear();
8427
8428 // Load the bodies of any functions or methods we've encountered. We do
8429 // this now (delayed) so that we can be sure that the declaration chains
8430 // have been fully wired up.
8431 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8432 PBEnd = PendingBodies.end();
8433 PB != PBEnd; ++PB) {
8434 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8435 // FIXME: Check for =delete/=default?
8436 // FIXME: Complain about ODR violations here?
8437 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8438 FD->setLazyBody(PB->second);
8439 continue;
8440 }
8441
8442 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8443 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8444 MD->setLazyBody(PB->second);
8445 }
8446 PendingBodies.clear();
Stephen Hines176edba2014-12-01 14:53:08 -08008447}
8448
8449void ASTReader::diagnoseOdrViolations() {
8450 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8451 return;
8452
8453 // Trigger the import of the full definition of each class that had any
8454 // odr-merging problems, so we can produce better diagnostics for them.
8455 // These updates may in turn find and diagnose some ODR failures, so take
8456 // ownership of the set first.
8457 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8458 PendingOdrMergeFailures.clear();
8459 for (auto &Merge : OdrMergeFailures) {
8460 Merge.first->buildLookup();
8461 Merge.first->decls_begin();
8462 Merge.first->bases_begin();
8463 Merge.first->vbases_begin();
8464 for (auto *RD : Merge.second) {
8465 RD->decls_begin();
8466 RD->bases_begin();
8467 RD->vbases_begin();
8468 }
8469 }
8470
8471 // For each declaration from a merged context, check that the canonical
8472 // definition of that context also contains a declaration of the same
8473 // entity.
8474 //
8475 // Caution: this loop does things that might invalidate iterators into
8476 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8477 while (!PendingOdrMergeChecks.empty()) {
8478 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8479
8480 // FIXME: Skip over implicit declarations for now. This matters for things
8481 // like implicitly-declared special member functions. This isn't entirely
8482 // correct; we can end up with multiple unmerged declarations of the same
8483 // implicit entity.
8484 if (D->isImplicit())
8485 continue;
8486
8487 DeclContext *CanonDef = D->getDeclContext();
8488
8489 bool Found = false;
8490 const Decl *DCanon = D->getCanonicalDecl();
8491
8492 for (auto RI : D->redecls()) {
8493 if (RI->getLexicalDeclContext() == CanonDef) {
8494 Found = true;
8495 break;
8496 }
8497 }
8498 if (Found)
8499 continue;
8500
8501 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8502 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8503 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8504 !Found && I != E; ++I) {
8505 for (auto RI : (*I)->redecls()) {
8506 if (RI->getLexicalDeclContext() == CanonDef) {
8507 // This declaration is present in the canonical definition. If it's
8508 // in the same redecl chain, it's the one we're looking for.
8509 if (RI->getCanonicalDecl() == DCanon)
8510 Found = true;
8511 else
8512 Candidates.push_back(cast<NamedDecl>(RI));
8513 break;
8514 }
8515 }
8516 }
8517
8518 if (!Found) {
8519 // The AST doesn't like TagDecls becoming invalid after they've been
8520 // completed. We only really need to mark FieldDecls as invalid here.
8521 if (!isa<TagDecl>(D))
8522 D->setInvalidDecl();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008523
8524 // Ensure we don't accidentally recursively enter deserialization while
8525 // we're producing our diagnostic.
8526 Deserializing RecursionGuard(this);
Stephen Hines176edba2014-12-01 14:53:08 -08008527
8528 std::string CanonDefModule =
8529 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8530 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8531 << D << getOwningModuleNameForDiagnostic(D)
8532 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8533
8534 if (Candidates.empty())
8535 Diag(cast<Decl>(CanonDef)->getLocation(),
8536 diag::note_module_odr_violation_no_possible_decls) << D;
8537 else {
8538 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8539 Diag(Candidates[I]->getLocation(),
8540 diag::note_module_odr_violation_possible_decl)
8541 << Candidates[I];
8542 }
8543
8544 DiagnosedOdrMergeFailures.insert(CanonDef);
8545 }
8546 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008547
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008548 if (OdrMergeFailures.empty())
8549 return;
8550
8551 // Ensure we don't accidentally recursively enter deserialization while
8552 // we're producing our diagnostics.
8553 Deserializing RecursionGuard(this);
8554
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008555 // Issue any pending ODR-failure diagnostics.
Stephen Hines176edba2014-12-01 14:53:08 -08008556 for (auto &Merge : OdrMergeFailures) {
8557 // If we've already pointed out a specific problem with this class, don't
8558 // bother issuing a general "something's different" diagnostic.
8559 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008560 continue;
8561
8562 bool Diagnosed = false;
8563 for (auto *RD : Merge.second) {
8564 // Multiple different declarations got merged together; tell the user
8565 // where they came from.
8566 if (Merge.first != RD) {
8567 // FIXME: Walk the definition, figure out what's different,
8568 // and diagnose that.
8569 if (!Diagnosed) {
8570 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8571 Diag(Merge.first->getLocation(),
8572 diag::err_module_odr_violation_different_definitions)
8573 << Merge.first << Module.empty() << Module;
8574 Diagnosed = true;
8575 }
8576
8577 Diag(RD->getLocation(),
8578 diag::note_module_odr_violation_different_definitions)
8579 << getOwningModuleNameForDiagnostic(RD);
8580 }
8581 }
8582
8583 if (!Diagnosed) {
8584 // All definitions are updates to the same declaration. This happens if a
8585 // module instantiates the declaration of a class template specialization
8586 // and two or more other modules instantiate its definition.
8587 //
8588 // FIXME: Indicate which modules had instantiations of this definition.
8589 // FIXME: How can this even happen?
8590 Diag(Merge.first->getLocation(),
8591 diag::err_module_odr_violation_different_instantiations)
8592 << Merge.first;
8593 }
8594 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008595}
8596
8597void ASTReader::FinishedDeserializing() {
8598 assert(NumCurrentElementsDeserializing &&
8599 "FinishedDeserializing not paired with StartedDeserializing");
8600 if (NumCurrentElementsDeserializing == 1) {
8601 // We decrease NumCurrentElementsDeserializing only after pending actions
8602 // are finished, to avoid recursively re-calling finishPendingActions().
8603 finishPendingActions();
8604 }
8605 --NumCurrentElementsDeserializing;
8606
Stephen Hines176edba2014-12-01 14:53:08 -08008607 if (NumCurrentElementsDeserializing == 0) {
8608 diagnoseOdrViolations();
8609
Stephen Hines651f13c2014-04-23 16:59:28 -07008610 // We are not in recursive loading, so it's safe to pass the "interesting"
8611 // decls to the consumer.
Stephen Hines176edba2014-12-01 14:53:08 -08008612 if (Consumer)
8613 PassInterestingDeclsToConsumer();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008614 }
8615}
8616
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008617void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola87bcee82013-10-19 16:55:03 +00008618 D = D->getMostRecentDecl();
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008619
8620 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8621 SemaObj->TUScope->AddDecl(D);
8622 } else if (SemaObj->TUScope) {
8623 // Adding the decl to IdResolver may have failed because it was already in
8624 // (even though it was not added in scope). If it is already in, make sure
8625 // it gets in the scope as well.
8626 if (std::find(SemaObj->IdResolver.begin(Name),
8627 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8628 SemaObj->TUScope->AddDecl(D);
8629 }
8630}
8631
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008632ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8633 bool DisableValidation, bool AllowASTWithCompilerErrors,
8634 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Stephen Hines651f13c2014-04-23 16:59:28 -07008635 bool UseGlobalIndex)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008636 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8637 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8638 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8639 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8640 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8641 DisableValidation(DisableValidation),
8642 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8643 AllowConfigurationMismatch(AllowConfigurationMismatch),
8644 ValidateSystemInputs(ValidateSystemInputs),
8645 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8646 CurrSwitchCaseStmts(&SwitchCaseStmts),
8647 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8648 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8649 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8650 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8651 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8652 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8653 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8654 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8655 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8656 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8657 ReadingKind(Read_None) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008658 SourceMgr.setExternalSLocEntrySource(this);
8659}
8660
8661ASTReader::~ASTReader() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008662 if (OwnsDeserializationListener)
8663 delete DeserializationListener;
8664
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008665 for (DeclContextVisibleUpdatesPending::iterator
8666 I = PendingVisibleUpdates.begin(),
8667 E = PendingVisibleUpdates.end();
8668 I != E; ++I) {
8669 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8670 F = I->second.end();
8671 J != F; ++J)
8672 delete J->first;
8673 }
8674}