blob: c0f22846787420acfc60cb1c649428b43603e391 [file] [log] [blame]
Nick Lewyckyf0f56162013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei11169dd2012-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"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000025#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000026#include "clang/Basic/FileManager.h"
Guy Benyei11169dd2012-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"
Ben Langmuirb92de022014-04-29 16:25:26 +000033#include "clang/Frontend/Utils.h"
Guy Benyei11169dd2012-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 Gregore060e572013-01-25 01:03:03 +000043#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000044#include "clang/Serialization/ModuleManager.h"
45#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-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"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000054#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000055#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000056#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000057#include <iterator>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000058#include <system_error>
Guy Benyei11169dd2012-12-18 14:30:41 +000059
60using namespace clang;
61using namespace clang::serialization;
62using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000063using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000064
Ben Langmuircb69b572014-03-07 06:40:32 +000065
66//===----------------------------------------------------------------------===//
67// ChainedASTReaderListener implementation
68//===----------------------------------------------------------------------===//
69
70bool
71ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
74}
Ben Langmuir4f5212a2014-04-14 22:12:44 +000075void 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}
Richard Smith1e2cf0d2014-10-31 02:28:58 +000083bool
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);
Ben Langmuircb69b572014-03-07 06:40:32 +000091}
92bool
93ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
94 bool Complain) {
95 return First->ReadTargetOptions(TargetOpts, Complain) ||
96 Second->ReadTargetOptions(TargetOpts, Complain);
97}
98bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +000099 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000100 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(
111 const HeaderSearchOptions &HSOpts, bool Complain) {
112 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
113 Second->ReadHeaderSearchOptions(HSOpts, Complain);
114}
115bool ChainedASTReaderListener::ReadPreprocessorOptions(
116 const PreprocessorOptions &PPOpts, bool Complain,
117 std::string &SuggestedPredefines) {
118 return First->ReadPreprocessorOptions(PPOpts, Complain,
119 SuggestedPredefines) ||
120 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
121}
122void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
123 unsigned Value) {
124 First->ReadCounter(M, Value);
125 Second->ReadCounter(M, Value);
126}
127bool ChainedASTReaderListener::needsInputFileVisitation() {
128 return First->needsInputFileVisitation() ||
129 Second->needsInputFileVisitation();
130}
131bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
132 return First->needsSystemInputFileVisitation() ||
133 Second->needsSystemInputFileVisitation();
134}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000135void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
136 First->visitModuleFile(Filename);
137 Second->visitModuleFile(Filename);
138}
Ben Langmuircb69b572014-03-07 06:40:32 +0000139bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000140 bool isSystem,
141 bool isOverridden) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000142 bool Continue = false;
143 if (First->needsInputFileVisitation() &&
144 (!isSystem || First->needsSystemInputFileVisitation()))
145 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
146 if (Second->needsInputFileVisitation() &&
147 (!isSystem || Second->needsSystemInputFileVisitation()))
148 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
149 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000150}
151
Guy Benyei11169dd2012-12-18 14:30:41 +0000152//===----------------------------------------------------------------------===//
153// PCH validator implementation
154//===----------------------------------------------------------------------===//
155
156ASTReaderListener::~ASTReaderListener() {}
157
158/// \brief Compare the given set of language options against an existing set of
159/// language options.
160///
161/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000162/// \param AllowCompatibleDifferences If true, differences between compatible
163/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000164///
165/// \returns true if the languagae options mis-match, false otherwise.
166static bool checkLanguageOptions(const LangOptions &LangOpts,
167 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000168 DiagnosticsEngine *Diags,
169 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000170#define LANGOPT(Name, Bits, Default, Description) \
171 if (ExistingLangOpts.Name != LangOpts.Name) { \
172 if (Diags) \
173 Diags->Report(diag::err_pch_langopt_mismatch) \
174 << Description << LangOpts.Name << ExistingLangOpts.Name; \
175 return true; \
176 }
177
178#define VALUE_LANGOPT(Name, Bits, Default, Description) \
179 if (ExistingLangOpts.Name != LangOpts.Name) { \
180 if (Diags) \
181 Diags->Report(diag::err_pch_langopt_value_mismatch) \
182 << Description; \
183 return true; \
184 }
185
186#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
187 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
188 if (Diags) \
189 Diags->Report(diag::err_pch_langopt_value_mismatch) \
190 << Description; \
191 return true; \
192 }
193
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000194#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
195 if (!AllowCompatibleDifferences) \
196 LANGOPT(Name, Bits, Default, Description)
197
198#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
199 if (!AllowCompatibleDifferences) \
200 ENUM_LANGOPT(Name, Bits, Default, Description)
201
Guy Benyei11169dd2012-12-18 14:30:41 +0000202#define BENIGN_LANGOPT(Name, Bits, Default, Description)
203#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
204#include "clang/Basic/LangOptions.def"
205
206 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
207 if (Diags)
208 Diags->Report(diag::err_pch_langopt_value_mismatch)
209 << "target Objective-C runtime";
210 return true;
211 }
212
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000213 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
214 LangOpts.CommentOpts.BlockCommandNames) {
215 if (Diags)
216 Diags->Report(diag::err_pch_langopt_value_mismatch)
217 << "block command names";
218 return true;
219 }
220
Guy Benyei11169dd2012-12-18 14:30:41 +0000221 return false;
222}
223
224/// \brief Compare the given set of target options against an existing set of
225/// target options.
226///
227/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
228///
229/// \returns true if the target options mis-match, false otherwise.
230static bool checkTargetOptions(const TargetOptions &TargetOpts,
231 const TargetOptions &ExistingTargetOpts,
232 DiagnosticsEngine *Diags) {
233#define CHECK_TARGET_OPT(Field, Name) \
234 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
235 if (Diags) \
236 Diags->Report(diag::err_pch_targetopt_mismatch) \
237 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
238 return true; \
239 }
240
241 CHECK_TARGET_OPT(Triple, "target");
242 CHECK_TARGET_OPT(CPU, "target CPU");
243 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei11169dd2012-12-18 14:30:41 +0000244#undef CHECK_TARGET_OPT
245
246 // Compare feature sets.
247 SmallVector<StringRef, 4> ExistingFeatures(
248 ExistingTargetOpts.FeaturesAsWritten.begin(),
249 ExistingTargetOpts.FeaturesAsWritten.end());
250 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
251 TargetOpts.FeaturesAsWritten.end());
252 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
253 std::sort(ReadFeatures.begin(), ReadFeatures.end());
254
255 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
256 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
257 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
258 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
259 ++ExistingIdx;
260 ++ReadIdx;
261 continue;
262 }
263
264 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
265 if (Diags)
266 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
267 << false << ReadFeatures[ReadIdx];
268 return true;
269 }
270
271 if (Diags)
272 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
273 << true << ExistingFeatures[ExistingIdx];
274 return true;
275 }
276
277 if (ExistingIdx < ExistingN) {
278 if (Diags)
279 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
280 << true << ExistingFeatures[ExistingIdx];
281 return true;
282 }
283
284 if (ReadIdx < ReadN) {
285 if (Diags)
286 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
287 << false << ReadFeatures[ReadIdx];
288 return true;
289 }
290
291 return false;
292}
293
294bool
295PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000296 bool Complain,
297 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000298 const LangOptions &ExistingLangOpts = PP.getLangOpts();
299 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000300 Complain ? &Reader.Diags : nullptr,
301 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000302}
303
304bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
305 bool Complain) {
306 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
307 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000308 Complain? &Reader.Diags : nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +0000309}
310
311namespace {
312 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
313 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000314 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
315 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000316}
317
Ben Langmuirb92de022014-04-29 16:25:26 +0000318static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
319 DiagnosticsEngine &Diags,
320 bool Complain) {
321 typedef DiagnosticsEngine::Level Level;
322
323 // Check current mappings for new -Werror mappings, and the stored mappings
324 // for cases that were explicitly mapped to *not* be errors that are now
325 // errors because of options like -Werror.
326 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
327
328 for (DiagnosticsEngine *MappingSource : MappingSources) {
329 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
330 diag::kind DiagID = DiagIDMappingPair.first;
331 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
332 if (CurLevel < DiagnosticsEngine::Error)
333 continue; // not significant
334 Level StoredLevel =
335 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
336 if (StoredLevel < DiagnosticsEngine::Error) {
337 if (Complain)
338 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
339 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
340 return true;
341 }
342 }
343 }
344
345 return false;
346}
347
Alp Tokerac4e8e52014-06-22 21:58:33 +0000348static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
349 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
350 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
351 return true;
352 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000353}
354
355static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
356 DiagnosticsEngine &Diags,
357 bool IsSystem, bool Complain) {
358 // Top-level options
359 if (IsSystem) {
360 if (Diags.getSuppressSystemWarnings())
361 return false;
362 // If -Wsystem-headers was not enabled before, be conservative
363 if (StoredDiags.getSuppressSystemWarnings()) {
364 if (Complain)
365 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
366 return true;
367 }
368 }
369
370 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
371 if (Complain)
372 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
373 return true;
374 }
375
376 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
377 !StoredDiags.getEnableAllWarnings()) {
378 if (Complain)
379 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
380 return true;
381 }
382
383 if (isExtHandlingFromDiagsError(Diags) &&
384 !isExtHandlingFromDiagsError(StoredDiags)) {
385 if (Complain)
386 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
387 return true;
388 }
389
390 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
391}
392
393bool PCHValidator::ReadDiagnosticOptions(
394 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
395 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
396 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
397 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000398 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000399 // This should never fail, because we would have processed these options
400 // before writing them to an ASTFile.
401 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
402
403 ModuleManager &ModuleMgr = Reader.getModuleManager();
404 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
405
406 // If the original import came from a file explicitly generated by the user,
407 // don't check the diagnostic mappings.
408 // FIXME: currently this is approximated by checking whether this is not a
Richard Smithe842a472014-10-22 02:05:46 +0000409 // module import of an implicitly-loaded module file.
Ben Langmuirb92de022014-04-29 16:25:26 +0000410 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
411 // the transitive closure of its imports, since unrelated modules cannot be
412 // imported until after this module finishes validation.
413 ModuleFile *TopImport = *ModuleMgr.rbegin();
414 while (!TopImport->ImportedBy.empty())
415 TopImport = TopImport->ImportedBy[0];
Richard Smithe842a472014-10-22 02:05:46 +0000416 if (TopImport->Kind != MK_ImplicitModule)
Ben Langmuirb92de022014-04-29 16:25:26 +0000417 return false;
418
419 StringRef ModuleName = TopImport->ModuleName;
420 assert(!ModuleName.empty() && "diagnostic options read before module name");
421
422 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
423 assert(M && "missing module");
424
425 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
426 // contains the union of their flags.
427 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
428}
429
Guy Benyei11169dd2012-12-18 14:30:41 +0000430/// \brief Collect the macro definitions provided by the given preprocessor
431/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000432static void
433collectMacroDefinitions(const PreprocessorOptions &PPOpts,
434 MacroDefinitionsMap &Macros,
435 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
437 StringRef Macro = PPOpts.Macros[I].first;
438 bool IsUndef = PPOpts.Macros[I].second;
439
440 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
441 StringRef MacroName = MacroPair.first;
442 StringRef MacroBody = MacroPair.second;
443
444 // For an #undef'd macro, we only care about the name.
445 if (IsUndef) {
446 if (MacroNames && !Macros.count(MacroName))
447 MacroNames->push_back(MacroName);
448
449 Macros[MacroName] = std::make_pair("", true);
450 continue;
451 }
452
453 // For a #define'd macro, figure out the actual definition.
454 if (MacroName.size() == Macro.size())
455 MacroBody = "1";
456 else {
457 // Note: GCC drops anything following an end-of-line character.
458 StringRef::size_type End = MacroBody.find_first_of("\n\r");
459 MacroBody = MacroBody.substr(0, End);
460 }
461
462 if (MacroNames && !Macros.count(MacroName))
463 MacroNames->push_back(MacroName);
464 Macros[MacroName] = std::make_pair(MacroBody, false);
465 }
466}
467
468/// \brief Check the preprocessor options deserialized from the control block
469/// against the preprocessor options in an existing preprocessor.
470///
471/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
472static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
473 const PreprocessorOptions &ExistingPPOpts,
474 DiagnosticsEngine *Diags,
475 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000476 std::string &SuggestedPredefines,
477 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000478 // Check macro definitions.
479 MacroDefinitionsMap ASTFileMacros;
480 collectMacroDefinitions(PPOpts, ASTFileMacros);
481 MacroDefinitionsMap ExistingMacros;
482 SmallVector<StringRef, 4> ExistingMacroNames;
483 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
484
485 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
486 // Dig out the macro definition in the existing preprocessor options.
487 StringRef MacroName = ExistingMacroNames[I];
488 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
489
490 // Check whether we know anything about this macro name or not.
491 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
492 = ASTFileMacros.find(MacroName);
493 if (Known == ASTFileMacros.end()) {
494 // FIXME: Check whether this identifier was referenced anywhere in the
495 // AST file. If so, we should reject the AST file. Unfortunately, this
496 // information isn't in the control block. What shall we do about it?
497
498 if (Existing.second) {
499 SuggestedPredefines += "#undef ";
500 SuggestedPredefines += MacroName.str();
501 SuggestedPredefines += '\n';
502 } else {
503 SuggestedPredefines += "#define ";
504 SuggestedPredefines += MacroName.str();
505 SuggestedPredefines += ' ';
506 SuggestedPredefines += Existing.first.str();
507 SuggestedPredefines += '\n';
508 }
509 continue;
510 }
511
512 // If the macro was defined in one but undef'd in the other, we have a
513 // conflict.
514 if (Existing.second != Known->second.second) {
515 if (Diags) {
516 Diags->Report(diag::err_pch_macro_def_undef)
517 << MacroName << Known->second.second;
518 }
519 return true;
520 }
521
522 // If the macro was #undef'd in both, or if the macro bodies are identical,
523 // it's fine.
524 if (Existing.second || Existing.first == Known->second.first)
525 continue;
526
527 // The macro bodies differ; complain.
528 if (Diags) {
529 Diags->Report(diag::err_pch_macro_def_conflict)
530 << MacroName << Known->second.first << Existing.first;
531 }
532 return true;
533 }
534
535 // Check whether we're using predefines.
536 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
537 if (Diags) {
538 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
539 }
540 return true;
541 }
542
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000543 // Detailed record is important since it is used for the module cache hash.
544 if (LangOpts.Modules &&
545 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
546 if (Diags) {
547 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
548 }
549 return true;
550 }
551
Guy Benyei11169dd2012-12-18 14:30:41 +0000552 // Compute the #include and #include_macros lines we need.
553 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
554 StringRef File = ExistingPPOpts.Includes[I];
555 if (File == ExistingPPOpts.ImplicitPCHInclude)
556 continue;
557
558 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
559 != PPOpts.Includes.end())
560 continue;
561
562 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000563 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000564 SuggestedPredefines += "\"\n";
565 }
566
567 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
568 StringRef File = ExistingPPOpts.MacroIncludes[I];
569 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
570 File)
571 != PPOpts.MacroIncludes.end())
572 continue;
573
574 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000575 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 SuggestedPredefines += "\"\n##\n";
577 }
578
579 return false;
580}
581
582bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
583 bool Complain,
584 std::string &SuggestedPredefines) {
585 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
586
587 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000588 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000590 SuggestedPredefines,
591 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000592}
593
Guy Benyei11169dd2012-12-18 14:30:41 +0000594void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
595 PP.setCounterValue(Value);
596}
597
598//===----------------------------------------------------------------------===//
599// AST reader implementation
600//===----------------------------------------------------------------------===//
601
Nico Weber824285e2014-05-08 04:26:47 +0000602void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
603 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000604 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000605 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000606}
607
608
609
610unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
611 return serialization::ComputeHash(Sel);
612}
613
614
615std::pair<unsigned, unsigned>
616ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000617 using namespace llvm::support;
618 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
619 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 return std::make_pair(KeyLen, DataLen);
621}
622
623ASTSelectorLookupTrait::internal_key_type
624ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000625 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000626 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000627 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
628 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
629 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000630 if (N == 0)
631 return SelTable.getNullarySelector(FirstII);
632 else if (N == 1)
633 return SelTable.getUnarySelector(FirstII);
634
635 SmallVector<IdentifierInfo *, 16> Args;
636 Args.push_back(FirstII);
637 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000638 Args.push_back(Reader.getLocalIdentifier(
639 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000640
641 return SelTable.getSelector(N, Args.data());
642}
643
644ASTSelectorLookupTrait::data_type
645ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
646 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000647 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000648
649 data_type Result;
650
Justin Bogner57ba0b22014-03-28 22:03:24 +0000651 Result.ID = Reader.getGlobalSelectorID(
652 F, endian::readNext<uint32_t, little, unaligned>(d));
653 unsigned NumInstanceMethodsAndBits =
654 endian::readNext<uint16_t, little, unaligned>(d);
655 unsigned NumFactoryMethodsAndBits =
656 endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000657 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
658 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
659 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
660 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000661
662 // Load instance methods
663 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000664 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
665 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000666 Result.Instance.push_back(Method);
667 }
668
669 // Load factory methods
670 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000671 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
672 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 Result.Factory.push_back(Method);
674 }
675
676 return Result;
677}
678
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000679unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
680 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000681}
682
683std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000684ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000685 using namespace llvm::support;
686 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
687 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000688 return std::make_pair(KeyLen, DataLen);
689}
690
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000691ASTIdentifierLookupTraitBase::internal_key_type
692ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000693 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000694 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000695}
696
Douglas Gregordcf25082013-02-11 18:16:18 +0000697/// \brief Whether the given identifier is "interesting".
698static bool isInterestingIdentifier(IdentifierInfo &II) {
699 return II.isPoisoned() ||
700 II.isExtensionToken() ||
701 II.getObjCOrBuiltinID() ||
702 II.hasRevertedTokenIDToIdentifier() ||
703 II.hadMacroDefinition() ||
704 II.getFETokenInfo<void>();
705}
706
Guy Benyei11169dd2012-12-18 14:30:41 +0000707IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
708 const unsigned char* d,
709 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000710 using namespace llvm::support;
711 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 bool IsInteresting = RawID & 0x01;
713
714 // Wipe out the "is interesting" bit.
715 RawID = RawID >> 1;
716
717 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
718 if (!IsInteresting) {
719 // For uninteresting identifiers, just build the IdentifierInfo
720 // and associate it with the persistent ID.
721 IdentifierInfo *II = KnownII;
722 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000723 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000724 KnownII = II;
725 }
726 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000727 if (!II->isFromAST()) {
728 bool WasInteresting = isInterestingIdentifier(*II);
729 II->setIsFromAST();
730 if (WasInteresting)
731 II->setChangedSinceDeserialization();
732 }
733 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734 return II;
735 }
736
Justin Bogner57ba0b22014-03-28 22:03:24 +0000737 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
738 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000739 bool CPlusPlusOperatorKeyword = Bits & 0x01;
740 Bits >>= 1;
741 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
742 Bits >>= 1;
743 bool Poisoned = Bits & 0x01;
744 Bits >>= 1;
745 bool ExtensionToken = Bits & 0x01;
746 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000747 bool hasSubmoduleMacros = Bits & 0x01;
748 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000749 bool hadMacroDefinition = Bits & 0x01;
750 Bits >>= 1;
751
752 assert(Bits == 0 && "Extra bits in the identifier?");
753 DataLen -= 8;
754
755 // Build the IdentifierInfo itself and link the identifier ID with
756 // the new IdentifierInfo.
757 IdentifierInfo *II = KnownII;
758 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000759 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 KnownII = II;
761 }
762 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000763 if (!II->isFromAST()) {
764 bool WasInteresting = isInterestingIdentifier(*II);
765 II->setIsFromAST();
766 if (WasInteresting)
767 II->setChangedSinceDeserialization();
768 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000769
770 // Set or check the various bits in the IdentifierInfo structure.
771 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000772 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000773 II->RevertTokenIDToIdentifier();
774 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
775 assert(II->isExtensionToken() == ExtensionToken &&
776 "Incorrect extension token flag");
777 (void)ExtensionToken;
778 if (Poisoned)
779 II->setIsPoisoned(true);
780 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
781 "Incorrect C++ operator keyword flag");
782 (void)CPlusPlusOperatorKeyword;
783
784 // If this identifier is a macro, deserialize the macro
785 // definition.
786 if (hadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000787 uint32_t MacroDirectivesOffset =
788 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000789 DataLen -= 4;
790 SmallVector<uint32_t, 8> LocalMacroIDs;
791 if (hasSubmoduleMacros) {
Richard Smithdaa69e02014-07-25 04:40:03 +0000792 while (true) {
793 uint32_t LocalMacroID =
794 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000795 DataLen -= 4;
Richard Smithdaa69e02014-07-25 04:40:03 +0000796 if (LocalMacroID == 0xdeadbeef) break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000797 LocalMacroIDs.push_back(LocalMacroID);
798 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000799 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000800
Richard Smithe842a472014-10-22 02:05:46 +0000801 if (F.Kind == MK_ImplicitModule || F.Kind == MK_ExplicitModule) {
Richard Smith49f906a2014-03-01 00:08:04 +0000802 // Macro definitions are stored from newest to oldest, so reverse them
803 // before registering them.
804 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000805 for (SmallVectorImpl<uint32_t>::iterator
Richard Smith49f906a2014-03-01 00:08:04 +0000806 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
807 unsigned Size = 1;
808
809 static const uint32_t HasOverridesFlag = 0x80000000U;
810 if (I + 1 != E && (I[1] & HasOverridesFlag))
811 Size += 1 + (I[1] & ~HasOverridesFlag);
812
813 MacroSizes.push_back(Size);
814 I += Size;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000815 }
Richard Smith49f906a2014-03-01 00:08:04 +0000816
817 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
818 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
819 SE = MacroSizes.rend();
820 SI != SE; ++SI) {
821 I -= *SI;
822
823 uint32_t LocalMacroID = *I;
Craig Topper00bbdcf2014-06-28 23:22:23 +0000824 ArrayRef<uint32_t> Overrides;
Richard Smith49f906a2014-03-01 00:08:04 +0000825 if (*SI != 1)
826 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
827 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
828 }
829 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000830 } else {
831 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
832 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000833 }
834
835 Reader.SetIdentifierInfo(ID, II);
836
837 // Read all of the declarations visible at global scope with this
838 // name.
839 if (DataLen > 0) {
840 SmallVector<uint32_t, 4> DeclIDs;
841 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000842 DeclIDs.push_back(Reader.getGlobalDeclID(
843 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000844 Reader.SetGloballyVisibleDecls(II, DeclIDs);
845 }
846
847 return II;
848}
849
850unsigned
851ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
852 llvm::FoldingSetNodeID ID;
853 ID.AddInteger(Key.Kind);
854
855 switch (Key.Kind) {
856 case DeclarationName::Identifier:
857 case DeclarationName::CXXLiteralOperatorName:
858 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
859 break;
860 case DeclarationName::ObjCZeroArgSelector:
861 case DeclarationName::ObjCOneArgSelector:
862 case DeclarationName::ObjCMultiArgSelector:
863 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
864 break;
865 case DeclarationName::CXXOperatorName:
866 ID.AddInteger((OverloadedOperatorKind)Key.Data);
867 break;
868 case DeclarationName::CXXConstructorName:
869 case DeclarationName::CXXDestructorName:
870 case DeclarationName::CXXConversionFunctionName:
871 case DeclarationName::CXXUsingDirective:
872 break;
873 }
874
875 return ID.ComputeHash();
876}
877
878ASTDeclContextNameLookupTrait::internal_key_type
879ASTDeclContextNameLookupTrait::GetInternalKey(
880 const external_key_type& Name) const {
881 DeclNameKey Key;
882 Key.Kind = Name.getNameKind();
883 switch (Name.getNameKind()) {
884 case DeclarationName::Identifier:
885 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
886 break;
887 case DeclarationName::ObjCZeroArgSelector:
888 case DeclarationName::ObjCOneArgSelector:
889 case DeclarationName::ObjCMultiArgSelector:
890 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
891 break;
892 case DeclarationName::CXXOperatorName:
893 Key.Data = Name.getCXXOverloadedOperator();
894 break;
895 case DeclarationName::CXXLiteralOperatorName:
896 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
897 break;
898 case DeclarationName::CXXConstructorName:
899 case DeclarationName::CXXDestructorName:
900 case DeclarationName::CXXConversionFunctionName:
901 case DeclarationName::CXXUsingDirective:
902 Key.Data = 0;
903 break;
904 }
905
906 return Key;
907}
908
909std::pair<unsigned, unsigned>
910ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000911 using namespace llvm::support;
912 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
913 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000914 return std::make_pair(KeyLen, DataLen);
915}
916
917ASTDeclContextNameLookupTrait::internal_key_type
918ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000919 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000920
921 DeclNameKey Key;
922 Key.Kind = (DeclarationName::NameKind)*d++;
923 switch (Key.Kind) {
924 case DeclarationName::Identifier:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000925 Key.Data = (uint64_t)Reader.getLocalIdentifier(
926 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000927 break;
928 case DeclarationName::ObjCZeroArgSelector:
929 case DeclarationName::ObjCOneArgSelector:
930 case DeclarationName::ObjCMultiArgSelector:
931 Key.Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000932 (uint64_t)Reader.getLocalSelector(
933 F, endian::readNext<uint32_t, little, unaligned>(
934 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000935 break;
936 case DeclarationName::CXXOperatorName:
937 Key.Data = *d++; // OverloadedOperatorKind
938 break;
939 case DeclarationName::CXXLiteralOperatorName:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000940 Key.Data = (uint64_t)Reader.getLocalIdentifier(
941 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000942 break;
943 case DeclarationName::CXXConstructorName:
944 case DeclarationName::CXXDestructorName:
945 case DeclarationName::CXXConversionFunctionName:
946 case DeclarationName::CXXUsingDirective:
947 Key.Data = 0;
948 break;
949 }
950
951 return Key;
952}
953
954ASTDeclContextNameLookupTrait::data_type
955ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
956 const unsigned char* d,
957 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000958 using namespace llvm::support;
959 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000960 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
961 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 return std::make_pair(Start, Start + NumDecls);
963}
964
965bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000966 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000967 const std::pair<uint64_t, uint64_t> &Offsets,
968 DeclContextInfo &Info) {
969 SavedStreamPosition SavedPosition(Cursor);
970 // First the lexical decls.
971 if (Offsets.first != 0) {
972 Cursor.JumpToBit(Offsets.first);
973
974 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000975 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000976 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000977 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 if (RecCode != DECL_CONTEXT_LEXICAL) {
979 Error("Expected lexical block");
980 return true;
981 }
982
Chris Lattner0e6c9402013-01-20 02:38:54 +0000983 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
984 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000985 }
986
987 // Now the lookup table.
988 if (Offsets.second != 0) {
989 Cursor.JumpToBit(Offsets.second);
990
991 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000992 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000993 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000994 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000995 if (RecCode != DECL_CONTEXT_VISIBLE) {
996 Error("Expected visible lookup table block");
997 return true;
998 }
Justin Bognerda4e6502014-04-14 16:34:29 +0000999 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
1000 (const unsigned char *)Blob.data() + Record[0],
1001 (const unsigned char *)Blob.data() + sizeof(uint32_t),
1002 (const unsigned char *)Blob.data(),
1003 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +00001004 }
1005
1006 return false;
1007}
1008
1009void ASTReader::Error(StringRef Msg) {
1010 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +00001011 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1012 Diag(diag::note_module_cache_path)
1013 << PP.getHeaderSearchInfo().getModuleCachePath();
1014 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001015}
1016
1017void ASTReader::Error(unsigned DiagID,
1018 StringRef Arg1, StringRef Arg2) {
1019 if (Diags.isDiagnosticInFlight())
1020 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1021 else
1022 Diag(DiagID) << Arg1 << Arg2;
1023}
1024
1025//===----------------------------------------------------------------------===//
1026// Source Manager Deserialization
1027//===----------------------------------------------------------------------===//
1028
1029/// \brief Read the line table in the source manager block.
1030/// \returns true if there was an error.
1031bool ASTReader::ParseLineTable(ModuleFile &F,
1032 SmallVectorImpl<uint64_t> &Record) {
1033 unsigned Idx = 0;
1034 LineTableInfo &LineTable = SourceMgr.getLineTable();
1035
1036 // Parse the file names
1037 std::map<int, int> FileIDs;
1038 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1039 // Extract the file name
1040 unsigned FilenameLen = Record[Idx++];
1041 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1042 Idx += FilenameLen;
1043 MaybeAddSystemRootToFilename(F, Filename);
1044 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1045 }
1046
1047 // Parse the line entries
1048 std::vector<LineEntry> Entries;
1049 while (Idx < Record.size()) {
1050 int FID = Record[Idx++];
1051 assert(FID >= 0 && "Serialized line entries for non-local file.");
1052 // Remap FileID from 1-based old view.
1053 FID += F.SLocEntryBaseID - 1;
1054
1055 // Extract the line entries
1056 unsigned NumEntries = Record[Idx++];
1057 assert(NumEntries && "Numentries is 00000");
1058 Entries.clear();
1059 Entries.reserve(NumEntries);
1060 for (unsigned I = 0; I != NumEntries; ++I) {
1061 unsigned FileOffset = Record[Idx++];
1062 unsigned LineNo = Record[Idx++];
1063 int FilenameID = FileIDs[Record[Idx++]];
1064 SrcMgr::CharacteristicKind FileKind
1065 = (SrcMgr::CharacteristicKind)Record[Idx++];
1066 unsigned IncludeOffset = Record[Idx++];
1067 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1068 FileKind, IncludeOffset));
1069 }
1070 LineTable.AddEntry(FileID::get(FID), Entries);
1071 }
1072
1073 return false;
1074}
1075
1076/// \brief Read a source manager block
1077bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1078 using namespace SrcMgr;
1079
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001080 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001081
1082 // Set the source-location entry cursor to the current position in
1083 // the stream. This cursor will be used to read the contents of the
1084 // source manager block initially, and then lazily read
1085 // source-location entries as needed.
1086 SLocEntryCursor = F.Stream;
1087
1088 // The stream itself is going to skip over the source manager block.
1089 if (F.Stream.SkipBlock()) {
1090 Error("malformed block record in AST file");
1091 return true;
1092 }
1093
1094 // Enter the source manager block.
1095 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1096 Error("malformed source manager block record in AST file");
1097 return true;
1098 }
1099
1100 RecordData Record;
1101 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001102 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1103
1104 switch (E.Kind) {
1105 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1106 case llvm::BitstreamEntry::Error:
1107 Error("malformed block record in AST file");
1108 return true;
1109 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001110 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001111 case llvm::BitstreamEntry::Record:
1112 // The interesting case.
1113 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001114 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001115
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001117 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001118 StringRef Blob;
1119 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 default: // Default behavior: ignore.
1121 break;
1122
1123 case SM_SLOC_FILE_ENTRY:
1124 case SM_SLOC_BUFFER_ENTRY:
1125 case SM_SLOC_EXPANSION_ENTRY:
1126 // Once we hit one of the source location entries, we're done.
1127 return false;
1128 }
1129 }
1130}
1131
1132/// \brief If a header file is not found at the path that we expect it to be
1133/// and the PCH file was moved from its original location, try to resolve the
1134/// file by assuming that header+PCH were moved together and the header is in
1135/// the same place relative to the PCH.
1136static std::string
1137resolveFileRelativeToOriginalDir(const std::string &Filename,
1138 const std::string &OriginalDir,
1139 const std::string &CurrDir) {
1140 assert(OriginalDir != CurrDir &&
1141 "No point trying to resolve the file if the PCH dir didn't change");
1142 using namespace llvm::sys;
1143 SmallString<128> filePath(Filename);
1144 fs::make_absolute(filePath);
1145 assert(path::is_absolute(OriginalDir));
1146 SmallString<128> currPCHPath(CurrDir);
1147
1148 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1149 fileDirE = path::end(path::parent_path(filePath));
1150 path::const_iterator origDirI = path::begin(OriginalDir),
1151 origDirE = path::end(OriginalDir);
1152 // Skip the common path components from filePath and OriginalDir.
1153 while (fileDirI != fileDirE && origDirI != origDirE &&
1154 *fileDirI == *origDirI) {
1155 ++fileDirI;
1156 ++origDirI;
1157 }
1158 for (; origDirI != origDirE; ++origDirI)
1159 path::append(currPCHPath, "..");
1160 path::append(currPCHPath, fileDirI, fileDirE);
1161 path::append(currPCHPath, path::filename(Filename));
1162 return currPCHPath.str();
1163}
1164
1165bool ASTReader::ReadSLocEntry(int ID) {
1166 if (ID == 0)
1167 return false;
1168
1169 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1170 Error("source location entry ID out-of-range for AST file");
1171 return true;
1172 }
1173
1174 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1175 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001176 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001177 unsigned BaseOffset = F->SLocEntryBaseOffset;
1178
1179 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001180 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1181 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 Error("incorrectly-formatted source location entry in AST file");
1183 return true;
1184 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001185
Guy Benyei11169dd2012-12-18 14:30:41 +00001186 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001187 StringRef Blob;
1188 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001189 default:
1190 Error("incorrectly-formatted source location entry in AST file");
1191 return true;
1192
1193 case SM_SLOC_FILE_ENTRY: {
1194 // We will detect whether a file changed and return 'Failure' for it, but
1195 // we will also try to fail gracefully by setting up the SLocEntry.
1196 unsigned InputID = Record[4];
1197 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001198 const FileEntry *File = IF.getFile();
1199 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001200
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001201 // Note that we only check if a File was returned. If it was out-of-date
1202 // we have complained but we will continue creating a FileID to recover
1203 // gracefully.
1204 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 return true;
1206
1207 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1208 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1209 // This is the module's main file.
1210 IncludeLoc = getImportLocation(F);
1211 }
1212 SrcMgr::CharacteristicKind
1213 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1214 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1215 ID, BaseOffset + Record[0]);
1216 SrcMgr::FileInfo &FileInfo =
1217 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1218 FileInfo.NumCreatedFIDs = Record[5];
1219 if (Record[3])
1220 FileInfo.setHasLineDirectives();
1221
1222 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1223 unsigned NumFileDecls = Record[7];
1224 if (NumFileDecls) {
1225 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1226 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1227 NumFileDecls));
1228 }
1229
1230 const SrcMgr::ContentCache *ContentCache
1231 = SourceMgr.getOrCreateContentCache(File,
1232 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1233 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1234 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1235 unsigned Code = SLocEntryCursor.ReadCode();
1236 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001237 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001238
1239 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1240 Error("AST record has invalid code");
1241 return true;
1242 }
1243
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001244 std::unique_ptr<llvm::MemoryBuffer> Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001245 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
David Blaikie49cc3182014-08-27 20:54:45 +00001246 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 }
1248
1249 break;
1250 }
1251
1252 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001253 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 unsigned Offset = Record[0];
1255 SrcMgr::CharacteristicKind
1256 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1257 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Richard Smithe842a472014-10-22 02:05:46 +00001258 if (IncludeLoc.isInvalid() &&
1259 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 IncludeLoc = getImportLocation(F);
1261 }
1262 unsigned Code = SLocEntryCursor.ReadCode();
1263 Record.clear();
1264 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001265 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001266
1267 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1268 Error("AST record has invalid code");
1269 return true;
1270 }
1271
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001272 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1273 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
David Blaikie50a5f972014-08-29 07:59:55 +00001274 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001275 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 break;
1277 }
1278
1279 case SM_SLOC_EXPANSION_ENTRY: {
1280 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1281 SourceMgr.createExpansionLoc(SpellingLoc,
1282 ReadSourceLocation(*F, Record[2]),
1283 ReadSourceLocation(*F, Record[3]),
1284 Record[4],
1285 ID,
1286 BaseOffset + Record[0]);
1287 break;
1288 }
1289 }
1290
1291 return false;
1292}
1293
1294std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1295 if (ID == 0)
1296 return std::make_pair(SourceLocation(), "");
1297
1298 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1299 Error("source location entry ID out-of-range for AST file");
1300 return std::make_pair(SourceLocation(), "");
1301 }
1302
1303 // Find which module file this entry lands in.
1304 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Richard Smithe842a472014-10-22 02:05:46 +00001305 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 return std::make_pair(SourceLocation(), "");
1307
1308 // FIXME: Can we map this down to a particular submodule? That would be
1309 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001310 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001311}
1312
1313/// \brief Find the location where the module F is imported.
1314SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1315 if (F->ImportLoc.isValid())
1316 return F->ImportLoc;
1317
1318 // Otherwise we have a PCH. It's considered to be "imported" at the first
1319 // location of its includer.
1320 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001321 // Main file is the importer.
1322 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1323 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001324 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001325 return F->ImportedBy[0]->FirstLoc;
1326}
1327
1328/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1329/// specified cursor. Read the abbreviations that are at the top of the block
1330/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001331bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001332 if (Cursor.EnterSubBlock(BlockID)) {
1333 Error("malformed block record in AST file");
1334 return Failure;
1335 }
1336
1337 while (true) {
1338 uint64_t Offset = Cursor.GetCurrentBitNo();
1339 unsigned Code = Cursor.ReadCode();
1340
1341 // We expect all abbrevs to be at the start of the block.
1342 if (Code != llvm::bitc::DEFINE_ABBREV) {
1343 Cursor.JumpToBit(Offset);
1344 return false;
1345 }
1346 Cursor.ReadAbbrevRecord();
1347 }
1348}
1349
Richard Smithe40f2ba2013-08-07 21:41:30 +00001350Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001351 unsigned &Idx) {
1352 Token Tok;
1353 Tok.startToken();
1354 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1355 Tok.setLength(Record[Idx++]);
1356 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1357 Tok.setIdentifierInfo(II);
1358 Tok.setKind((tok::TokenKind)Record[Idx++]);
1359 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1360 return Tok;
1361}
1362
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001363MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001364 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001365
1366 // Keep track of where we are in the stream, then jump back there
1367 // after reading this macro.
1368 SavedStreamPosition SavedPosition(Stream);
1369
1370 Stream.JumpToBit(Offset);
1371 RecordData Record;
1372 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001373 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001374
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001376 // Advance to the next record, but if we get to the end of the block, don't
1377 // pop it (removing all the abbreviations from the cursor) since we want to
1378 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001379 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001380 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1381
1382 switch (Entry.Kind) {
1383 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1384 case llvm::BitstreamEntry::Error:
1385 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001386 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001387 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001388 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001389 case llvm::BitstreamEntry::Record:
1390 // The interesting case.
1391 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 }
1393
1394 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 Record.clear();
1396 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001397 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001398 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001399 case PP_MACRO_DIRECTIVE_HISTORY:
1400 return Macro;
1401
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 case PP_MACRO_OBJECT_LIKE:
1403 case PP_MACRO_FUNCTION_LIKE: {
1404 // If we already have a macro, that means that we've hit the end
1405 // of the definition of the macro we were looking for. We're
1406 // done.
1407 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001408 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001409
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001410 unsigned NextIndex = 1; // Skip identifier ID.
1411 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001412 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001413 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001414 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001416 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001417
Guy Benyei11169dd2012-12-18 14:30:41 +00001418 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1419 // Decode function-like macro info.
1420 bool isC99VarArgs = Record[NextIndex++];
1421 bool isGNUVarArgs = Record[NextIndex++];
1422 bool hasCommaPasting = Record[NextIndex++];
1423 MacroArgs.clear();
1424 unsigned NumArgs = Record[NextIndex++];
1425 for (unsigned i = 0; i != NumArgs; ++i)
1426 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1427
1428 // Install function-like macro info.
1429 MI->setIsFunctionLike();
1430 if (isC99VarArgs) MI->setIsC99Varargs();
1431 if (isGNUVarArgs) MI->setIsGNUVarargs();
1432 if (hasCommaPasting) MI->setHasCommaPasting();
1433 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1434 PP.getPreprocessorAllocator());
1435 }
1436
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 // Remember that we saw this macro last so that we add the tokens that
1438 // form its body to it.
1439 Macro = MI;
1440
1441 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1442 Record[NextIndex]) {
1443 // We have a macro definition. Register the association
1444 PreprocessedEntityID
1445 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1446 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001447 PreprocessingRecord::PPEntityID
1448 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1449 MacroDefinition *PPDef =
1450 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1451 if (PPDef)
1452 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001453 }
1454
1455 ++NumMacrosRead;
1456 break;
1457 }
1458
1459 case PP_TOKEN: {
1460 // If we see a TOKEN before a PP_MACRO_*, then the file is
1461 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001462 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001463
John McCallf413f5e2013-05-03 00:10:13 +00001464 unsigned Idx = 0;
1465 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001466 Macro->AddTokenToBody(Tok);
1467 break;
1468 }
1469 }
1470 }
1471}
1472
1473PreprocessedEntityID
1474ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1475 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1476 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1477 assert(I != M.PreprocessedEntityRemap.end()
1478 && "Invalid index into preprocessed entity index remap");
1479
1480 return LocalID + I->second;
1481}
1482
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001483unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1484 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001485}
1486
1487HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001488HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1489 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1490 FE->getName() };
1491 return ikey;
1492}
Guy Benyei11169dd2012-12-18 14:30:41 +00001493
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001494bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1495 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001496 return false;
1497
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001498 if (strcmp(a.Filename, b.Filename) == 0)
1499 return true;
1500
Guy Benyei11169dd2012-12-18 14:30:41 +00001501 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001502 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001503 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1504 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001505 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001506}
1507
1508std::pair<unsigned, unsigned>
1509HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001510 using namespace llvm::support;
1511 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001512 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001513 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001514}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001515
1516HeaderFileInfoTrait::internal_key_type
1517HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001518 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001519 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001520 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1521 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001522 ikey.Filename = (const char *)d;
1523 return ikey;
1524}
1525
Guy Benyei11169dd2012-12-18 14:30:41 +00001526HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001527HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 unsigned DataLen) {
1529 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001530 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 HeaderFileInfo HFI;
1532 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001533 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1534 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 HFI.isImport = (Flags >> 5) & 0x01;
1536 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1537 HFI.DirInfo = (Flags >> 2) & 0x03;
1538 HFI.Resolved = (Flags >> 1) & 0x01;
1539 HFI.IndexHeaderMapHeader = Flags & 0x01;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001540 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1541 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1542 M, endian::readNext<uint32_t, little, unaligned>(d));
1543 if (unsigned FrameworkOffset =
1544 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 // The framework offset is 1 greater than the actual offset,
1546 // since 0 is used as an indicator for "no framework name".
1547 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1548 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1549 }
1550
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001551 if (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001552 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001553 if (LocalSMID) {
1554 // This header is part of a module. Associate it with the module to enable
1555 // implicit module import.
1556 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1557 Module *Mod = Reader.getSubmodule(GlobalSMID);
1558 HFI.isModuleHeader = true;
1559 FileManager &FileMgr = Reader.getFileManager();
1560 ModuleMap &ModMap =
1561 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001562 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001563 }
1564 }
1565
Guy Benyei11169dd2012-12-18 14:30:41 +00001566 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1567 (void)End;
1568
1569 // This HeaderFileInfo was externally loaded.
1570 HFI.External = true;
1571 return HFI;
1572}
1573
Richard Smith49f906a2014-03-01 00:08:04 +00001574void
1575ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1576 GlobalMacroID GMacID,
Craig Topper00bbdcf2014-06-28 23:22:23 +00001577 ArrayRef<SubmoduleID> Overrides) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001578 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Craig Toppera13603a2014-05-22 05:54:18 +00001579 SubmoduleID *OverrideData = nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001580 if (!Overrides.empty()) {
1581 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1582 OverrideData[0] = Overrides.size();
1583 for (unsigned I = 0; I != Overrides.size(); ++I)
1584 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1585 }
1586 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001587}
1588
1589void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1590 ModuleFile *M,
1591 uint64_t MacroDirectivesOffset) {
1592 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1593 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001594}
1595
1596void ASTReader::ReadDefinedMacros() {
1597 // Note that we are loading defined macros.
1598 Deserializing Macros(this);
1599
1600 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1601 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001602 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001603
1604 // If there was no preprocessor block, skip this file.
1605 if (!MacroCursor.getBitStreamReader())
1606 continue;
1607
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001608 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 Cursor.JumpToBit((*I)->MacroStartOffset);
1610
1611 RecordData Record;
1612 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001613 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1614
1615 switch (E.Kind) {
1616 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1617 case llvm::BitstreamEntry::Error:
1618 Error("malformed block record in AST file");
1619 return;
1620 case llvm::BitstreamEntry::EndBlock:
1621 goto NextCursor;
1622
1623 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001624 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001625 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001626 default: // Default behavior: ignore.
1627 break;
1628
1629 case PP_MACRO_OBJECT_LIKE:
1630 case PP_MACRO_FUNCTION_LIKE:
1631 getLocalIdentifier(**I, Record[0]);
1632 break;
1633
1634 case PP_TOKEN:
1635 // Ignore tokens.
1636 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001637 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001638 break;
1639 }
1640 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001641 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 }
1643}
1644
1645namespace {
1646 /// \brief Visitor class used to look up identifirs in an AST file.
1647 class IdentifierLookupVisitor {
1648 StringRef Name;
1649 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001650 unsigned &NumIdentifierLookups;
1651 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001652 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001653
Guy Benyei11169dd2012-12-18 14:30:41 +00001654 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001655 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1656 unsigned &NumIdentifierLookups,
1657 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001658 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001659 NumIdentifierLookups(NumIdentifierLookups),
1660 NumIdentifierLookupHits(NumIdentifierLookupHits),
1661 Found()
1662 {
1663 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001664
1665 static bool visit(ModuleFile &M, void *UserData) {
1666 IdentifierLookupVisitor *This
1667 = static_cast<IdentifierLookupVisitor *>(UserData);
1668
1669 // If we've already searched this module file, skip it now.
1670 if (M.Generation <= This->PriorGeneration)
1671 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001672
Guy Benyei11169dd2012-12-18 14:30:41 +00001673 ASTIdentifierLookupTable *IdTable
1674 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1675 if (!IdTable)
1676 return false;
1677
1678 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1679 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001680 ++This->NumIdentifierLookups;
1681 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001682 if (Pos == IdTable->end())
1683 return false;
1684
1685 // Dereferencing the iterator has the effect of building the
1686 // IdentifierInfo node and populating it with the various
1687 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001688 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001689 This->Found = *Pos;
1690 return true;
1691 }
1692
1693 // \brief Retrieve the identifier info found within the module
1694 // files.
1695 IdentifierInfo *getIdentifierInfo() const { return Found; }
1696 };
1697}
1698
1699void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1700 // Note that we are loading an identifier.
1701 Deserializing AnIdentifier(this);
1702
1703 unsigned PriorGeneration = 0;
1704 if (getContext().getLangOpts().Modules)
1705 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001706
1707 // If there is a global index, look there first to determine which modules
1708 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001709 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001710 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001711 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001712 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1713 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001714 }
1715 }
1716
Douglas Gregor7211ac12013-01-25 23:32:03 +00001717 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001718 NumIdentifierLookups,
1719 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001720 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001721 markIdentifierUpToDate(&II);
1722}
1723
1724void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1725 if (!II)
1726 return;
1727
1728 II->setOutOfDate(false);
1729
1730 // Update the generation for this identifier.
1731 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001732 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001733}
1734
Richard Smith49f906a2014-03-01 00:08:04 +00001735struct ASTReader::ModuleMacroInfo {
1736 SubmoduleID SubModID;
1737 MacroInfo *MI;
1738 SubmoduleID *Overrides;
1739 // FIXME: Remove this.
1740 ModuleFile *F;
1741
1742 bool isDefine() const { return MI; }
1743
1744 SubmoduleID getSubmoduleID() const { return SubModID; }
1745
Craig Topper00bbdcf2014-06-28 23:22:23 +00001746 ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
Richard Smith49f906a2014-03-01 00:08:04 +00001747 if (!Overrides)
Craig Topper00bbdcf2014-06-28 23:22:23 +00001748 return None;
Richard Smith49f906a2014-03-01 00:08:04 +00001749 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1750 }
1751
Richard Smithdaa69e02014-07-25 04:40:03 +00001752 MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
Richard Smith49f906a2014-03-01 00:08:04 +00001753 if (!MI)
Richard Smithdaa69e02014-07-25 04:40:03 +00001754 return PP.AllocateUndefMacroDirective(ImportLoc, SubModID,
1755 getOverriddenSubmodules());
1756 return PP.AllocateDefMacroDirective(MI, ImportLoc, SubModID,
1757 getOverriddenSubmodules());
Richard Smith49f906a2014-03-01 00:08:04 +00001758 }
1759};
1760
1761ASTReader::ModuleMacroInfo *
1762ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1763 ModuleMacroInfo Info;
1764
1765 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1766 if (ID & 1) {
1767 // Macro undefinition.
1768 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
Craig Toppera13603a2014-05-22 05:54:18 +00001769 Info.MI = nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001770 } else {
1771 // Macro definition.
1772 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1773 assert(GMacID);
1774
1775 // If this macro has already been loaded, don't do so again.
1776 // FIXME: This is highly dubious. Multiple macro definitions can have the
1777 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1778 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
Craig Toppera13603a2014-05-22 05:54:18 +00001779 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001780
1781 Info.MI = getMacro(GMacID);
1782 Info.SubModID = Info.MI->getOwningModuleID();
1783 }
1784 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1785 Info.F = PMInfo.M;
1786
1787 return new (Context) ModuleMacroInfo(Info);
1788}
1789
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001790void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1791 const PendingMacroInfo &PMInfo) {
1792 assert(II);
1793
Richard Smithe842a472014-10-22 02:05:46 +00001794 if (PMInfo.M->Kind != MK_ImplicitModule &&
1795 PMInfo.M->Kind != MK_ExplicitModule) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001796 installPCHMacroDirectives(II, *PMInfo.M,
1797 PMInfo.PCHMacroData.MacroDirectivesOffset);
1798 return;
1799 }
Richard Smith49f906a2014-03-01 00:08:04 +00001800
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001801 // Module Macro.
1802
Richard Smith49f906a2014-03-01 00:08:04 +00001803 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1804 if (!MMI)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001805 return;
1806
Richard Smith49f906a2014-03-01 00:08:04 +00001807 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1808 if (Owner && Owner->NameVisibility == Module::Hidden) {
1809 // Macros in the owning module are hidden. Just remember this macro to
1810 // install if we make this module visible.
1811 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1812 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00001813 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001814 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001815}
1816
1817void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1818 ModuleFile &M, uint64_t Offset) {
Richard Smithe842a472014-10-22 02:05:46 +00001819 assert(M.Kind != MK_ImplicitModule && M.Kind != MK_ExplicitModule);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001820
1821 BitstreamCursor &Cursor = M.MacroCursor;
1822 SavedStreamPosition SavedPosition(Cursor);
1823 Cursor.JumpToBit(Offset);
1824
1825 llvm::BitstreamEntry Entry =
1826 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1827 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1828 Error("malformed block record in AST file");
1829 return;
1830 }
1831
1832 RecordData Record;
1833 PreprocessorRecordTypes RecType =
1834 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1835 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1836 Error("malformed block record in AST file");
1837 return;
1838 }
1839
1840 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001841 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001842 unsigned Idx = 0, N = Record.size();
1843 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001844 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001845 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001846 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1847 switch (K) {
1848 case MacroDirective::MD_Define: {
1849 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1850 MacroInfo *MI = getMacro(GMacID);
Richard Smithdaa69e02014-07-25 04:40:03 +00001851 SubmoduleID ImportedFrom = Record[Idx++];
1852 bool IsAmbiguous = Record[Idx++];
1853 llvm::SmallVector<unsigned, 4> Overrides;
1854 if (ImportedFrom) {
1855 Overrides.insert(Overrides.end(),
1856 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1857 Idx += Overrides.size() + 1;
1858 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001859 DefMacroDirective *DefMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00001860 PP.AllocateDefMacroDirective(MI, Loc, ImportedFrom, Overrides);
1861 DefMD->setAmbiguous(IsAmbiguous);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001862 MD = DefMD;
1863 break;
1864 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001865 case MacroDirective::MD_Undefine: {
1866 SubmoduleID ImportedFrom = Record[Idx++];
1867 llvm::SmallVector<unsigned, 4> Overrides;
1868 if (ImportedFrom) {
1869 Overrides.insert(Overrides.end(),
1870 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1871 Idx += Overrides.size() + 1;
1872 }
1873 MD = PP.AllocateUndefMacroDirective(Loc, ImportedFrom, Overrides);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001874 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001875 }
1876 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001877 bool isPublic = Record[Idx++];
1878 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1879 break;
1880 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001881
1882 if (!Latest)
1883 Latest = MD;
1884 if (Earliest)
1885 Earliest->setPrevious(MD);
1886 Earliest = MD;
1887 }
1888
1889 PP.setLoadedMacroDirective(II, Latest);
1890}
1891
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001892/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001893/// modules.
1894static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001895 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001896 assert(PrevMI && NewMI);
Craig Toppera13603a2014-05-22 05:54:18 +00001897 Module *PrevOwner = nullptr;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001898 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1899 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001900 SourceManager &SrcMgr = Reader.getSourceManager();
1901 bool PrevInSystem
1902 = PrevOwner? PrevOwner->IsSystem
1903 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1904 bool NewInSystem
1905 = NewOwner? NewOwner->IsSystem
1906 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1907 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001908 return false;
Douglas Gregor5e461192013-06-07 22:56:11 +00001909 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001910}
1911
Richard Smith49f906a2014-03-01 00:08:04 +00001912void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Richard Smithdaa69e02014-07-25 04:40:03 +00001913 SourceLocation ImportLoc,
Richard Smith49f906a2014-03-01 00:08:04 +00001914 AmbiguousMacros &Ambig,
Craig Topper00bbdcf2014-06-28 23:22:23 +00001915 ArrayRef<SubmoduleID> Overrides) {
Richard Smith49f906a2014-03-01 00:08:04 +00001916 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1917 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001918
Richard Smith49f906a2014-03-01 00:08:04 +00001919 // If this macro is not yet visible, remove it from the hidden names list.
Richard Smithbb853c72014-08-13 01:23:33 +00001920 // It won't be there if we're in the middle of making the owner visible.
Richard Smith49f906a2014-03-01 00:08:04 +00001921 Module *Owner = getSubmodule(OwnerID);
Richard Smithbb853c72014-08-13 01:23:33 +00001922 auto HiddenIt = HiddenNamesMap.find(Owner);
1923 if (HiddenIt != HiddenNamesMap.end()) {
1924 HiddenNames &Hidden = HiddenIt->second;
1925 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1926 if (HI != Hidden.HiddenMacros.end()) {
1927 // Register the macro now so we don't lose it when we re-export.
1928 PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
Richard Smithdaa69e02014-07-25 04:40:03 +00001929
Richard Smithbb853c72014-08-13 01:23:33 +00001930 auto SubOverrides = HI->second->getOverriddenSubmodules();
1931 Hidden.HiddenMacros.erase(HI);
1932 removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
1933 }
Richard Smith49f906a2014-03-01 00:08:04 +00001934 }
1935
1936 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001937 Ambig.erase(
1938 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1939 return MD->getInfo()->getOwningModuleID() == OwnerID;
1940 }),
1941 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001942 }
1943}
1944
1945ASTReader::AmbiguousMacros *
1946ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Richard Smithdaa69e02014-07-25 04:40:03 +00001947 SourceLocation ImportLoc,
Craig Topper00bbdcf2014-06-28 23:22:23 +00001948 ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001949 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001950 if (!Prev && Overrides.empty())
Craig Toppera13603a2014-05-22 05:54:18 +00001951 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001952
Craig Toppera13603a2014-05-22 05:54:18 +00001953 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1954 : nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001955 if (PrevDef && PrevDef->isAmbiguous()) {
1956 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1957 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1958 Ambig.push_back(PrevDef);
1959
Richard Smithdaa69e02014-07-25 04:40:03 +00001960 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001961
1962 if (!Ambig.empty())
1963 return &Ambig;
1964
1965 AmbiguousMacroDefs.erase(II);
1966 } else {
1967 // There's no ambiguity yet. Maybe we're introducing one.
Benjamin Kramer834652a2014-05-03 18:44:26 +00001968 AmbiguousMacros Ambig;
Richard Smith49f906a2014-03-01 00:08:04 +00001969 if (PrevDef)
1970 Ambig.push_back(PrevDef);
1971
Richard Smithdaa69e02014-07-25 04:40:03 +00001972 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001973
1974 if (!Ambig.empty()) {
1975 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
Benjamin Kramer834652a2014-05-03 18:44:26 +00001976 std::swap(Result, Ambig);
Richard Smith49f906a2014-03-01 00:08:04 +00001977 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001978 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001979 }
Richard Smith49f906a2014-03-01 00:08:04 +00001980
1981 // We ended up with no ambiguity.
Craig Toppera13603a2014-05-22 05:54:18 +00001982 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001983}
1984
1985void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
Richard Smithdaa69e02014-07-25 04:40:03 +00001986 Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00001987 assert(II && Owner);
1988
1989 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
Richard Smithdaa69e02014-07-25 04:40:03 +00001990 if (ImportLoc.isInvalid()) {
Richard Smith49f906a2014-03-01 00:08:04 +00001991 // FIXME: If we made macros from this module visible but didn't provide a
1992 // source location for the import, we don't have a location for the macro.
1993 // Use the location at which the containing module file was first imported
1994 // for now.
1995 ImportLoc = MMI->F->DirectImportLoc;
Richard Smith56be7542014-03-21 00:33:59 +00001996 assert(ImportLoc.isValid() && "no import location for a visible macro?");
Richard Smith49f906a2014-03-01 00:08:04 +00001997 }
1998
Benjamin Kramer834652a2014-05-03 18:44:26 +00001999 AmbiguousMacros *Prev =
Richard Smithdaa69e02014-07-25 04:40:03 +00002000 removeOverriddenMacros(II, ImportLoc, MMI->getOverriddenSubmodules());
Richard Smith49f906a2014-03-01 00:08:04 +00002001
Richard Smith49f906a2014-03-01 00:08:04 +00002002 // Create a synthetic macro definition corresponding to the import (or null
2003 // if this was an undefinition of the macro).
Richard Smithdaa69e02014-07-25 04:40:03 +00002004 MacroDirective *Imported = MMI->import(PP, ImportLoc);
2005 DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00002006
2007 // If there's no ambiguity, just install the macro.
2008 if (!Prev) {
Richard Smithdaa69e02014-07-25 04:40:03 +00002009 PP.appendMacroDirective(II, Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00002010 return;
2011 }
2012 assert(!Prev->empty());
2013
2014 if (!MD) {
2015 // We imported a #undef that didn't remove all prior definitions. The most
2016 // recent prior definition remains, and we install it in the place of the
Richard Smithdaa69e02014-07-25 04:40:03 +00002017 // imported directive, as if by a local #pragma pop_macro.
Richard Smith49f906a2014-03-01 00:08:04 +00002018 MacroInfo *NewMI = Prev->back()->getInfo();
2019 Prev->pop_back();
Richard Smithdaa69e02014-07-25 04:40:03 +00002020 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
2021
2022 // Install our #undef first so that we don't lose track of it. We'll replace
2023 // this with whichever macro definition ends up winning.
2024 PP.appendMacroDirective(II, Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00002025 }
2026
2027 // We're introducing a macro definition that creates or adds to an ambiguity.
2028 // We can resolve that ambiguity if this macro is token-for-token identical to
2029 // all of the existing definitions.
2030 MacroInfo *NewMI = MD->getInfo();
2031 assert(NewMI && "macro definition with no MacroInfo?");
2032 while (!Prev->empty()) {
2033 MacroInfo *PrevMI = Prev->back()->getInfo();
2034 assert(PrevMI && "macro definition with no MacroInfo?");
2035
2036 // Before marking the macros as ambiguous, check if this is a case where
2037 // both macros are in system headers. If so, we trust that the system
2038 // did not get it wrong. This also handles cases where Clang's own
2039 // headers have a different spelling of certain system macros:
2040 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
2041 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2042 //
2043 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2044 // overrides the system limits.h's macros, so there's no conflict here.
2045 if (NewMI != PrevMI &&
2046 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2047 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2048 break;
2049
2050 // The previous definition is the same as this one (or both are defined in
2051 // system modules so we can assume they're equivalent); we don't need to
2052 // track it any more.
2053 Prev->pop_back();
2054 }
2055
2056 if (!Prev->empty())
2057 MD->setAmbiguous(true);
2058
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002059 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002060}
2061
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002062ASTReader::InputFileInfo
2063ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002064 // Go find this input file.
2065 BitstreamCursor &Cursor = F.InputFilesCursor;
2066 SavedStreamPosition SavedPosition(Cursor);
2067 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2068
2069 unsigned Code = Cursor.ReadCode();
2070 RecordData Record;
2071 StringRef Blob;
2072
2073 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2074 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2075 "invalid record type for input file");
2076 (void)Result;
2077
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002078 std::string Filename;
2079 off_t StoredSize;
2080 time_t StoredTime;
2081 bool Overridden;
2082
Ben Langmuir198c1682014-03-07 07:27:49 +00002083 assert(Record[0] == ID && "Bogus stored ID or offset");
2084 StoredSize = static_cast<off_t>(Record[1]);
2085 StoredTime = static_cast<time_t>(Record[2]);
2086 Overridden = static_cast<bool>(Record[3]);
2087 Filename = Blob;
2088 MaybeAddSystemRootToFilename(F, Filename);
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002089
Hans Wennborg73945142014-03-14 17:45:06 +00002090 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2091 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002092}
2093
2094std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002095 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00002096}
2097
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002098InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002099 // If this ID is bogus, just return an empty input file.
2100 if (ID == 0 || ID > F.InputFilesLoaded.size())
2101 return InputFile();
2102
2103 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002104 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002105 return F.InputFilesLoaded[ID-1];
2106
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002107 if (F.InputFilesLoaded[ID-1].isNotFound())
2108 return InputFile();
2109
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002111 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 SavedStreamPosition SavedPosition(Cursor);
2113 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2114
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002115 InputFileInfo FI = readInputFileInfo(F, ID);
2116 off_t StoredSize = FI.StoredSize;
2117 time_t StoredTime = FI.StoredTime;
2118 bool Overridden = FI.Overridden;
2119 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002120
Ben Langmuir198c1682014-03-07 07:27:49 +00002121 const FileEntry *File
2122 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2123 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2124
2125 // If we didn't find the file, resolve it relative to the
2126 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00002127 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00002128 F.OriginalDir != CurrentDir) {
2129 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2130 F.OriginalDir,
2131 CurrentDir);
2132 if (!Resolved.empty())
2133 File = FileMgr.getFile(Resolved);
2134 }
2135
2136 // For an overridden file, create a virtual file with the stored
2137 // size/timestamp.
Craig Toppera13603a2014-05-22 05:54:18 +00002138 if (Overridden && File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002139 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2140 }
2141
Craig Toppera13603a2014-05-22 05:54:18 +00002142 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002143 if (Complain) {
2144 std::string ErrorStr = "could not find file '";
2145 ErrorStr += Filename;
2146 ErrorStr += "' referenced by AST file";
2147 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00002148 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002149 // Record that we didn't find the file.
2150 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2151 return InputFile();
2152 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002153
Ben Langmuir198c1682014-03-07 07:27:49 +00002154 // Check if there was a request to override the contents of the file
2155 // that was part of the precompiled header. Overridding such a file
2156 // can lead to problems when lexing using the source locations from the
2157 // PCH.
2158 SourceManager &SM = getSourceManager();
2159 if (!Overridden && SM.isFileOverridden(File)) {
2160 if (Complain)
2161 Error(diag::err_fe_pch_file_overridden, Filename);
2162 // After emitting the diagnostic, recover by disabling the override so
2163 // that the original file will be used.
2164 SM.disableFileContentsOverride(File);
2165 // The FileEntry is a virtual file entry with the size of the contents
2166 // that would override the original contents. Set it to the original's
2167 // size/time.
2168 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2169 StoredSize, StoredTime);
2170 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
Ben Langmuir198c1682014-03-07 07:27:49 +00002172 bool IsOutOfDate = false;
2173
2174 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002175 if (!Overridden && //
2176 (StoredSize != File->getSize() ||
2177#if defined(LLVM_ON_WIN32)
2178 false
2179#else
Ben Langmuir198c1682014-03-07 07:27:49 +00002180 // In our regression testing, the Windows file system seems to
2181 // have inconsistent modification times that sometimes
2182 // erroneously trigger this error-handling path.
Richard Smith96fdab62014-10-28 16:24:08 +00002183 //
2184 // This also happens in networked file systems, so disable this
2185 // check if validation is disabled or if we have an explicitly
2186 // built PCM file.
2187 //
2188 // FIXME: Should we also do this for PCH files? They could also
2189 // reasonably get shared across a network during a distributed build.
2190 (StoredTime != File->getModificationTime() && !DisableValidation &&
2191 F.Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00002192#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00002193 )) {
2194 if (Complain) {
2195 // Build a list of the PCH imports that got us here (in reverse).
2196 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2197 while (ImportStack.back()->ImportedBy.size() > 0)
2198 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002199
Ben Langmuir198c1682014-03-07 07:27:49 +00002200 // The top-level PCH is stale.
2201 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2202 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002203
Ben Langmuir198c1682014-03-07 07:27:49 +00002204 // Print the import stack.
2205 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2206 Diag(diag::note_pch_required_by)
2207 << Filename << ImportStack[0]->FileName;
2208 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002209 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002210 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002211 }
2212
Ben Langmuir198c1682014-03-07 07:27:49 +00002213 if (!Diags.isDiagnosticInFlight())
2214 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 }
2216
Ben Langmuir198c1682014-03-07 07:27:49 +00002217 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 }
2219
Ben Langmuir198c1682014-03-07 07:27:49 +00002220 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2221
2222 // Note that we've loaded this input file.
2223 F.InputFilesLoaded[ID-1] = IF;
2224 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002225}
2226
2227const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2228 ModuleFile &M = ModuleMgr.getPrimaryModule();
2229 std::string Filename = filenameStrRef;
2230 MaybeAddSystemRootToFilename(M, Filename);
2231 const FileEntry *File = FileMgr.getFile(Filename);
Craig Toppera13603a2014-05-22 05:54:18 +00002232 if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002233 M.OriginalDir != CurrentDir) {
2234 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2235 M.OriginalDir,
2236 CurrentDir);
2237 if (!resolved.empty())
2238 File = FileMgr.getFile(resolved);
2239 }
2240
2241 return File;
2242}
2243
2244/// \brief If we are loading a relocatable PCH file, and the filename is
2245/// not an absolute path, add the system root to the beginning of the file
2246/// name.
2247void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2248 std::string &Filename) {
2249 // If this is not a relocatable PCH file, there's nothing to do.
2250 if (!M.RelocatablePCH)
2251 return;
2252
2253 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2254 return;
2255
2256 if (isysroot.empty()) {
2257 // If no system root was given, default to '/'
2258 Filename.insert(Filename.begin(), '/');
2259 return;
2260 }
2261
2262 unsigned Length = isysroot.size();
2263 if (isysroot[Length - 1] != '/')
2264 Filename.insert(Filename.begin(), '/');
2265
2266 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2267}
2268
2269ASTReader::ASTReadResult
2270ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002271 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002272 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002273 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002274 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002275
2276 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2277 Error("malformed block record in AST file");
2278 return Failure;
2279 }
2280
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002281 // Should we allow the configuration of the module file to differ from the
2282 // configuration of the current translation unit in a compatible way?
2283 //
2284 // FIXME: Allow this for files explicitly specified with -include-pch too.
2285 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 // Read all of the records and blocks in the control block.
2288 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002289 unsigned NumInputs = 0;
2290 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002291 while (1) {
2292 llvm::BitstreamEntry Entry = Stream.advance();
2293
2294 switch (Entry.Kind) {
2295 case llvm::BitstreamEntry::Error:
2296 Error("malformed block record in AST file");
2297 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002298 case llvm::BitstreamEntry::EndBlock: {
2299 // Validate input files.
2300 const HeaderSearchOptions &HSOpts =
2301 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002302
Richard Smitha1825302014-10-23 22:18:29 +00002303 // All user input files reside at the index range [0, NumUserInputs), and
2304 // system input files reside at [NumUserInputs, NumInputs).
Ben Langmuiracb803e2014-11-10 22:13:10 +00002305 if (!DisableValidation) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002306 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002307
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002308 // If we are reading a module, we will create a verification timestamp,
2309 // so we verify all input files. Otherwise, verify only user input
2310 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002311
2312 unsigned N = NumUserInputs;
2313 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002314 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002315 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002316 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002317 N = NumInputs;
2318
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002319 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002320 InputFile IF = getInputFile(F, I+1, Complain);
2321 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002322 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002323 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002324 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002325
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002326 if (Listener)
2327 Listener->visitModuleFile(F.FileName);
2328
Ben Langmuircb69b572014-03-07 06:40:32 +00002329 if (Listener && Listener->needsInputFileVisitation()) {
2330 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2331 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002332 for (unsigned I = 0; I < N; ++I) {
2333 bool IsSystem = I >= NumUserInputs;
2334 InputFileInfo FI = readInputFileInfo(F, I+1);
2335 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2336 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002337 }
2338
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002340 }
2341
Chris Lattnere7b154b2013-01-19 21:39:22 +00002342 case llvm::BitstreamEntry::SubBlock:
2343 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002344 case INPUT_FILES_BLOCK_ID:
2345 F.InputFilesCursor = Stream;
2346 if (Stream.SkipBlock() || // Skip with the main cursor
2347 // Read the abbreviations
2348 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2349 Error("malformed block record in AST file");
2350 return Failure;
2351 }
2352 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002353
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002355 if (Stream.SkipBlock()) {
2356 Error("malformed block record in AST file");
2357 return Failure;
2358 }
2359 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002360 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002361
2362 case llvm::BitstreamEntry::Record:
2363 // The interesting case.
2364 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002365 }
2366
2367 // Read and process a record.
2368 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002369 StringRef Blob;
2370 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002371 case METADATA: {
2372 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2373 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002374 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2375 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002376 return VersionMismatch;
2377 }
2378
2379 bool hasErrors = Record[5];
2380 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2381 Diag(diag::err_pch_with_compiler_errors);
2382 return HadErrors;
2383 }
2384
2385 F.RelocatablePCH = Record[4];
2386
2387 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002388 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002389 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2390 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002391 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002392 return VersionMismatch;
2393 }
2394 break;
2395 }
2396
Ben Langmuir487ea142014-10-23 18:05:36 +00002397 case SIGNATURE:
2398 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2399 F.Signature = Record[0];
2400 break;
2401
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 case IMPORTS: {
2403 // Load each of the imported PCH files.
2404 unsigned Idx = 0, N = Record.size();
2405 while (Idx < N) {
2406 // Read information about the AST file.
2407 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2408 // The import location will be the local one for now; we will adjust
2409 // all import locations of module imports after the global source
2410 // location info are setup.
2411 SourceLocation ImportLoc =
2412 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002413 off_t StoredSize = (off_t)Record[Idx++];
2414 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002415 ASTFileSignature StoredSignature = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002416 unsigned Length = Record[Idx++];
2417 SmallString<128> ImportedFile(Record.begin() + Idx,
2418 Record.begin() + Idx + Length);
2419 Idx += Length;
2420
2421 // Load the AST file.
2422 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00002423 StoredSize, StoredModTime, StoredSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 ClientLoadCapabilities)) {
2425 case Failure: return Failure;
2426 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002427 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002428 case OutOfDate: return OutOfDate;
2429 case VersionMismatch: return VersionMismatch;
2430 case ConfigurationMismatch: return ConfigurationMismatch;
2431 case HadErrors: return HadErrors;
2432 case Success: break;
2433 }
2434 }
2435 break;
2436 }
2437
2438 case LANGUAGE_OPTIONS: {
2439 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002440 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
Guy Benyei11169dd2012-12-18 14:30:41 +00002441 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002442 ParseLanguageOptions(Record, Complain, *Listener,
2443 AllowCompatibleConfigurationMismatch) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002444 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002445 return ConfigurationMismatch;
2446 break;
2447 }
2448
2449 case TARGET_OPTIONS: {
2450 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2451 if (Listener && &F == *ModuleMgr.begin() &&
2452 ParseTargetOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002453 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 return ConfigurationMismatch;
2455 break;
2456 }
2457
2458 case DIAGNOSTIC_OPTIONS: {
Ben Langmuirb92de022014-04-29 16:25:26 +00002459 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002460 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002461 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuirb92de022014-04-29 16:25:26 +00002463 !DisableValidation)
2464 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00002465 break;
2466 }
2467
2468 case FILE_SYSTEM_OPTIONS: {
2469 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2470 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002471 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002473 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 return ConfigurationMismatch;
2475 break;
2476 }
2477
2478 case HEADER_SEARCH_OPTIONS: {
2479 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2480 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002481 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002483 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 return ConfigurationMismatch;
2485 break;
2486 }
2487
2488 case PREPROCESSOR_OPTIONS: {
2489 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2490 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002491 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002492 ParsePreprocessorOptions(Record, Complain, *Listener,
2493 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002494 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 return ConfigurationMismatch;
2496 break;
2497 }
2498
2499 case ORIGINAL_FILE:
2500 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002501 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2503 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2504 break;
2505
2506 case ORIGINAL_FILE_ID:
2507 F.OriginalSourceFileID = FileID::get(Record[0]);
2508 break;
2509
2510 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002511 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002512 break;
2513
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002514 case MODULE_NAME:
2515 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002516 if (Listener)
2517 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002518 break;
2519
2520 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002521 if (ASTReadResult Result =
2522 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2523 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002524 break;
2525
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002527 NumInputs = Record[0];
2528 NumUserInputs = Record[1];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002529 F.InputFileOffsets = (const uint32_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002530 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 break;
2532 }
2533 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002534}
2535
Ben Langmuir2c9af442014-04-10 17:57:43 +00002536ASTReader::ASTReadResult
2537ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002538 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002539
2540 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2541 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002542 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 }
2544
2545 // Read all of the records and blocks for the AST file.
2546 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002547 while (1) {
2548 llvm::BitstreamEntry Entry = Stream.advance();
2549
2550 switch (Entry.Kind) {
2551 case llvm::BitstreamEntry::Error:
2552 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002553 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002554 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002555 // Outside of C++, we do not store a lookup map for the translation unit.
2556 // Instead, mark it as needing a lookup map to be built if this module
2557 // contains any declarations lexically within it (which it always does!).
2558 // This usually has no cost, since we very rarely need the lookup map for
2559 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002561 if (DC->hasExternalLexicalStorage() &&
2562 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002564
Ben Langmuir2c9af442014-04-10 17:57:43 +00002565 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002567 case llvm::BitstreamEntry::SubBlock:
2568 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 case DECLTYPES_BLOCK_ID:
2570 // We lazily load the decls block, but we want to set up the
2571 // DeclsCursor cursor to point into it. Clone our current bitcode
2572 // cursor to it, enter the block and read the abbrevs in that block.
2573 // With the main cursor, we just skip over it.
2574 F.DeclsCursor = Stream;
2575 if (Stream.SkipBlock() || // Skip with the main cursor.
2576 // Read the abbrevs.
2577 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2578 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002579 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 }
2581 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002582
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 case PREPROCESSOR_BLOCK_ID:
2584 F.MacroCursor = Stream;
2585 if (!PP.getExternalSource())
2586 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002587
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 if (Stream.SkipBlock() ||
2589 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2590 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002591 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 }
2593 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2594 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002595
Guy Benyei11169dd2012-12-18 14:30:41 +00002596 case PREPROCESSOR_DETAIL_BLOCK_ID:
2597 F.PreprocessorDetailCursor = Stream;
2598 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002599 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002601 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002602 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002603 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002605 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2606
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 if (!PP.getPreprocessingRecord())
2608 PP.createPreprocessingRecord();
2609 if (!PP.getPreprocessingRecord()->getExternalSource())
2610 PP.getPreprocessingRecord()->SetExternalSource(*this);
2611 break;
2612
2613 case SOURCE_MANAGER_BLOCK_ID:
2614 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002615 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002616 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002617
Guy Benyei11169dd2012-12-18 14:30:41 +00002618 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002619 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2620 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002621 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002622
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002624 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 if (Stream.SkipBlock() ||
2626 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2627 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002628 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 }
2630 CommentsCursors.push_back(std::make_pair(C, &F));
2631 break;
2632 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002633
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002635 if (Stream.SkipBlock()) {
2636 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002637 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002638 }
2639 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002640 }
2641 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002642
2643 case llvm::BitstreamEntry::Record:
2644 // The interesting case.
2645 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002646 }
2647
2648 // Read and process a record.
2649 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002650 StringRef Blob;
2651 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 default: // Default behavior: ignore.
2653 break;
2654
2655 case TYPE_OFFSET: {
2656 if (F.LocalNumTypes != 0) {
2657 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002658 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002660 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 F.LocalNumTypes = Record[0];
2662 unsigned LocalBaseTypeIndex = Record[1];
2663 F.BaseTypeIndex = getTotalNumTypes();
2664
2665 if (F.LocalNumTypes > 0) {
2666 // Introduce the global -> local mapping for types within this module.
2667 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2668
2669 // Introduce the local -> global mapping for types within this module.
2670 F.TypeRemap.insertOrReplace(
2671 std::make_pair(LocalBaseTypeIndex,
2672 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002673
2674 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 }
2676 break;
2677 }
2678
2679 case DECL_OFFSET: {
2680 if (F.LocalNumDecls != 0) {
2681 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002682 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002684 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 F.LocalNumDecls = Record[0];
2686 unsigned LocalBaseDeclID = Record[1];
2687 F.BaseDeclID = getTotalNumDecls();
2688
2689 if (F.LocalNumDecls > 0) {
2690 // Introduce the global -> local mapping for declarations within this
2691 // module.
2692 GlobalDeclMap.insert(
2693 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2694
2695 // Introduce the local -> global mapping for declarations within this
2696 // module.
2697 F.DeclRemap.insertOrReplace(
2698 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2699
2700 // Introduce the global -> local mapping for declarations within this
2701 // module.
2702 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002703
Ben Langmuir52ca6782014-10-20 16:27:32 +00002704 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2705 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 break;
2707 }
2708
2709 case TU_UPDATE_LEXICAL: {
2710 DeclContext *TU = Context.getTranslationUnitDecl();
2711 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002712 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002714 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002715 TU->setHasExternalLexicalStorage(true);
2716 break;
2717 }
2718
2719 case UPDATE_VISIBLE: {
2720 unsigned Idx = 0;
2721 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2722 ASTDeclContextNameLookupTable *Table =
Justin Bognerda4e6502014-04-14 16:34:29 +00002723 ASTDeclContextNameLookupTable::Create(
2724 (const unsigned char *)Blob.data() + Record[Idx++],
2725 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2726 (const unsigned char *)Blob.data(),
2727 ASTDeclContextNameLookupTrait(*this, F));
Richard Smithcd45dbc2014-04-19 03:48:30 +00002728 if (Decl *D = GetExistingDecl(ID)) {
Richard Smithd9174792014-03-11 03:10:46 +00002729 auto *DC = cast<DeclContext>(D);
2730 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002731 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2732 delete LookupTable;
2733 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 } else
2735 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2736 break;
2737 }
2738
2739 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002740 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002741 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002742 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2743 (const unsigned char *)F.IdentifierTableData + Record[0],
2744 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2745 (const unsigned char *)F.IdentifierTableData,
2746 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002747
2748 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2749 }
2750 break;
2751
2752 case IDENTIFIER_OFFSET: {
2753 if (F.LocalNumIdentifiers != 0) {
2754 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002755 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002757 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002758 F.LocalNumIdentifiers = Record[0];
2759 unsigned LocalBaseIdentifierID = Record[1];
2760 F.BaseIdentifierID = getTotalNumIdentifiers();
2761
2762 if (F.LocalNumIdentifiers > 0) {
2763 // Introduce the global -> local mapping for identifiers within this
2764 // module.
2765 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2766 &F));
2767
2768 // Introduce the local -> global mapping for identifiers within this
2769 // module.
2770 F.IdentifierRemap.insertOrReplace(
2771 std::make_pair(LocalBaseIdentifierID,
2772 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002773
Ben Langmuir52ca6782014-10-20 16:27:32 +00002774 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2775 + F.LocalNumIdentifiers);
2776 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 break;
2778 }
2779
Ben Langmuir332aafe2014-01-31 01:06:56 +00002780 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002782 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002783 break;
2784
2785 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002786 if (SpecialTypes.empty()) {
2787 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2788 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2789 break;
2790 }
2791
2792 if (SpecialTypes.size() != Record.size()) {
2793 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002794 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002795 }
2796
2797 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2798 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2799 if (!SpecialTypes[I])
2800 SpecialTypes[I] = ID;
2801 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2802 // merge step?
2803 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 break;
2805
2806 case STATISTICS:
2807 TotalNumStatements += Record[0];
2808 TotalNumMacros += Record[1];
2809 TotalLexicalDeclContexts += Record[2];
2810 TotalVisibleDeclContexts += Record[3];
2811 break;
2812
2813 case UNUSED_FILESCOPED_DECLS:
2814 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2815 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2816 break;
2817
2818 case DELEGATING_CTORS:
2819 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2820 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2821 break;
2822
2823 case WEAK_UNDECLARED_IDENTIFIERS:
2824 if (Record.size() % 4 != 0) {
2825 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002826 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 }
2828
2829 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2830 // files. This isn't the way to do it :)
2831 WeakUndeclaredIdentifiers.clear();
2832
2833 // Translate the weak, undeclared identifiers into global IDs.
2834 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2835 WeakUndeclaredIdentifiers.push_back(
2836 getGlobalIdentifierID(F, Record[I++]));
2837 WeakUndeclaredIdentifiers.push_back(
2838 getGlobalIdentifierID(F, Record[I++]));
2839 WeakUndeclaredIdentifiers.push_back(
2840 ReadSourceLocation(F, Record, I).getRawEncoding());
2841 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2842 }
2843 break;
2844
Richard Smith78165b52013-01-10 23:43:47 +00002845 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002847 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 break;
2849
2850 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002851 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002852 F.LocalNumSelectors = Record[0];
2853 unsigned LocalBaseSelectorID = Record[1];
2854 F.BaseSelectorID = getTotalNumSelectors();
2855
2856 if (F.LocalNumSelectors > 0) {
2857 // Introduce the global -> local mapping for selectors within this
2858 // module.
2859 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2860
2861 // Introduce the local -> global mapping for selectors within this
2862 // module.
2863 F.SelectorRemap.insertOrReplace(
2864 std::make_pair(LocalBaseSelectorID,
2865 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002866
2867 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 }
2869 break;
2870 }
2871
2872 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002873 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002874 if (Record[0])
2875 F.SelectorLookupTable
2876 = ASTSelectorLookupTable::Create(
2877 F.SelectorLookupTableData + Record[0],
2878 F.SelectorLookupTableData,
2879 ASTSelectorLookupTrait(*this, F));
2880 TotalNumMethodPoolEntries += Record[1];
2881 break;
2882
2883 case REFERENCED_SELECTOR_POOL:
2884 if (!Record.empty()) {
2885 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2886 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2887 Record[Idx++]));
2888 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2889 getRawEncoding());
2890 }
2891 }
2892 break;
2893
2894 case PP_COUNTER_VALUE:
2895 if (!Record.empty() && Listener)
2896 Listener->ReadCounter(F, Record[0]);
2897 break;
2898
2899 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002900 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 F.NumFileSortedDecls = Record[0];
2902 break;
2903
2904 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002905 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 F.LocalNumSLocEntries = Record[0];
2907 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002908 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002909 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 SLocSpaceSize);
2911 // Make our entry in the range map. BaseID is negative and growing, so
2912 // we invert it. Because we invert it, though, we need the other end of
2913 // the range.
2914 unsigned RangeStart =
2915 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2916 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2917 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2918
2919 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2920 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2921 GlobalSLocOffsetMap.insert(
2922 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2923 - SLocSpaceSize,&F));
2924
2925 // Initialize the remapping table.
2926 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002927 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002929 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2931
2932 TotalNumSLocEntries += F.LocalNumSLocEntries;
2933 break;
2934 }
2935
2936 case MODULE_OFFSET_MAP: {
2937 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002938 const unsigned char *Data = (const unsigned char*)Blob.data();
2939 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002940
2941 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2942 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2943 F.SLocRemap.insert(std::make_pair(0U, 0));
2944 F.SLocRemap.insert(std::make_pair(2U, 1));
2945 }
2946
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002948 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2949 RemapBuilder;
2950 RemapBuilder SLocRemap(F.SLocRemap);
2951 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2952 RemapBuilder MacroRemap(F.MacroRemap);
2953 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2954 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2955 RemapBuilder SelectorRemap(F.SelectorRemap);
2956 RemapBuilder DeclRemap(F.DeclRemap);
2957 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002958
2959 while(Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002960 using namespace llvm::support;
2961 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002962 StringRef Name = StringRef((const char*)Data, Len);
2963 Data += Len;
2964 ModuleFile *OM = ModuleMgr.lookup(Name);
2965 if (!OM) {
2966 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002967 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 }
2969
Justin Bogner57ba0b22014-03-28 22:03:24 +00002970 uint32_t SLocOffset =
2971 endian::readNext<uint32_t, little, unaligned>(Data);
2972 uint32_t IdentifierIDOffset =
2973 endian::readNext<uint32_t, little, unaligned>(Data);
2974 uint32_t MacroIDOffset =
2975 endian::readNext<uint32_t, little, unaligned>(Data);
2976 uint32_t PreprocessedEntityIDOffset =
2977 endian::readNext<uint32_t, little, unaligned>(Data);
2978 uint32_t SubmoduleIDOffset =
2979 endian::readNext<uint32_t, little, unaligned>(Data);
2980 uint32_t SelectorIDOffset =
2981 endian::readNext<uint32_t, little, unaligned>(Data);
2982 uint32_t DeclIDOffset =
2983 endian::readNext<uint32_t, little, unaligned>(Data);
2984 uint32_t TypeIndexOffset =
2985 endian::readNext<uint32_t, little, unaligned>(Data);
2986
Ben Langmuir785180e2014-10-20 16:27:30 +00002987 uint32_t None = std::numeric_limits<uint32_t>::max();
2988
2989 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2990 RemapBuilder &Remap) {
2991 if (Offset != None)
2992 Remap.insert(std::make_pair(Offset,
2993 static_cast<int>(BaseOffset - Offset)));
2994 };
2995 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2996 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2997 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2998 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2999 PreprocessedEntityRemap);
3000 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3001 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3002 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3003 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00003004
3005 // Global -> local mappings.
3006 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3007 }
3008 break;
3009 }
3010
3011 case SOURCE_MANAGER_LINE_TABLE:
3012 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003013 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 break;
3015
3016 case SOURCE_LOCATION_PRELOADS: {
3017 // Need to transform from the local view (1-based IDs) to the global view,
3018 // which is based off F.SLocEntryBaseID.
3019 if (!F.PreloadSLocEntries.empty()) {
3020 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003021 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 }
3023
3024 F.PreloadSLocEntries.swap(Record);
3025 break;
3026 }
3027
3028 case EXT_VECTOR_DECLS:
3029 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3030 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3031 break;
3032
3033 case VTABLE_USES:
3034 if (Record.size() % 3 != 0) {
3035 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003036 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 }
3038
3039 // Later tables overwrite earlier ones.
3040 // FIXME: Modules will have some trouble with this. This is clearly not
3041 // the right way to do this.
3042 VTableUses.clear();
3043
3044 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3045 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3046 VTableUses.push_back(
3047 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3048 VTableUses.push_back(Record[Idx++]);
3049 }
3050 break;
3051
3052 case DYNAMIC_CLASSES:
3053 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3054 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3055 break;
3056
3057 case PENDING_IMPLICIT_INSTANTIATIONS:
3058 if (PendingInstantiations.size() % 2 != 0) {
3059 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003060 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 }
3062
3063 if (Record.size() % 2 != 0) {
3064 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003065 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 }
3067
3068 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3069 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3070 PendingInstantiations.push_back(
3071 ReadSourceLocation(F, Record, I).getRawEncoding());
3072 }
3073 break;
3074
3075 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00003076 if (Record.size() != 2) {
3077 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003078 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003079 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003080 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3081 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3082 break;
3083
3084 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003085 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3086 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3087 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003088
3089 unsigned LocalBasePreprocessedEntityID = Record[0];
3090
3091 unsigned StartingID;
3092 if (!PP.getPreprocessingRecord())
3093 PP.createPreprocessingRecord();
3094 if (!PP.getPreprocessingRecord()->getExternalSource())
3095 PP.getPreprocessingRecord()->SetExternalSource(*this);
3096 StartingID
3097 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003098 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003099 F.BasePreprocessedEntityID = StartingID;
3100
3101 if (F.NumPreprocessedEntities > 0) {
3102 // Introduce the global -> local mapping for preprocessed entities in
3103 // this module.
3104 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3105
3106 // Introduce the local -> global mapping for preprocessed entities in
3107 // this module.
3108 F.PreprocessedEntityRemap.insertOrReplace(
3109 std::make_pair(LocalBasePreprocessedEntityID,
3110 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3111 }
3112
3113 break;
3114 }
3115
3116 case DECL_UPDATE_OFFSETS: {
3117 if (Record.size() % 2 != 0) {
3118 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003119 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003121 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3122 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3123 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3124
3125 // If we've already loaded the decl, perform the updates when we finish
3126 // loading this block.
3127 if (Decl *D = GetExistingDecl(ID))
3128 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3129 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 break;
3131 }
3132
3133 case DECL_REPLACEMENTS: {
3134 if (Record.size() % 3 != 0) {
3135 Error("invalid DECL_REPLACEMENTS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003136 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003137 }
3138 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3139 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3140 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3141 break;
3142 }
3143
3144 case OBJC_CATEGORIES_MAP: {
3145 if (F.LocalNumObjCCategoriesInMap != 0) {
3146 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003147 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 }
3149
3150 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003151 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003152 break;
3153 }
3154
3155 case OBJC_CATEGORIES:
3156 F.ObjCCategories.swap(Record);
3157 break;
3158
3159 case CXX_BASE_SPECIFIER_OFFSETS: {
3160 if (F.LocalNumCXXBaseSpecifiers != 0) {
3161 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003162 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 }
3164
3165 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003166 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003167 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3168 break;
3169 }
3170
3171 case DIAG_PRAGMA_MAPPINGS:
3172 if (F.PragmaDiagMappings.empty())
3173 F.PragmaDiagMappings.swap(Record);
3174 else
3175 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3176 Record.begin(), Record.end());
3177 break;
3178
3179 case CUDA_SPECIAL_DECL_REFS:
3180 // Later tables overwrite earlier ones.
3181 // FIXME: Modules will have trouble with this.
3182 CUDASpecialDeclRefs.clear();
3183 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3184 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3185 break;
3186
3187 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003188 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003189 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003190 if (Record[0]) {
3191 F.HeaderFileInfoTable
3192 = HeaderFileInfoLookupTable::Create(
3193 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3194 (const unsigned char *)F.HeaderFileInfoTableData,
3195 HeaderFileInfoTrait(*this, F,
3196 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003197 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003198
3199 PP.getHeaderSearchInfo().SetExternalSource(this);
3200 if (!PP.getHeaderSearchInfo().getExternalLookup())
3201 PP.getHeaderSearchInfo().SetExternalLookup(this);
3202 }
3203 break;
3204 }
3205
3206 case FP_PRAGMA_OPTIONS:
3207 // Later tables overwrite earlier ones.
3208 FPPragmaOptions.swap(Record);
3209 break;
3210
3211 case OPENCL_EXTENSIONS:
3212 // Later tables overwrite earlier ones.
3213 OpenCLExtensions.swap(Record);
3214 break;
3215
3216 case TENTATIVE_DEFINITIONS:
3217 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3218 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3219 break;
3220
3221 case KNOWN_NAMESPACES:
3222 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3223 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3224 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003225
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003226 case UNDEFINED_BUT_USED:
3227 if (UndefinedButUsed.size() % 2 != 0) {
3228 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003229 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003230 }
3231
3232 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003233 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003234 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003235 }
3236 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003237 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3238 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003239 ReadSourceLocation(F, Record, I).getRawEncoding());
3240 }
3241 break;
3242
Guy Benyei11169dd2012-12-18 14:30:41 +00003243 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003244 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 // If we aren't loading a module (which has its own exports), make
3246 // all of the imported modules visible.
3247 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003248 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3249 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3250 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3251 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003252 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003253 }
3254 }
3255 break;
3256 }
3257
3258 case LOCAL_REDECLARATIONS: {
3259 F.RedeclarationChains.swap(Record);
3260 break;
3261 }
3262
3263 case LOCAL_REDECLARATIONS_MAP: {
3264 if (F.LocalNumRedeclarationsInMap != 0) {
3265 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003266 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003267 }
3268
3269 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003270 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003271 break;
3272 }
3273
3274 case MERGED_DECLARATIONS: {
3275 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3276 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3277 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3278 for (unsigned N = Record[Idx++]; N > 0; --N)
3279 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3280 }
3281 break;
3282 }
3283
3284 case MACRO_OFFSET: {
3285 if (F.LocalNumMacros != 0) {
3286 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003287 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003289 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003290 F.LocalNumMacros = Record[0];
3291 unsigned LocalBaseMacroID = Record[1];
3292 F.BaseMacroID = getTotalNumMacros();
3293
3294 if (F.LocalNumMacros > 0) {
3295 // Introduce the global -> local mapping for macros within this module.
3296 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3297
3298 // Introduce the local -> global mapping for macros within this module.
3299 F.MacroRemap.insertOrReplace(
3300 std::make_pair(LocalBaseMacroID,
3301 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003302
3303 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003304 }
3305 break;
3306 }
3307
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003308 case MACRO_TABLE: {
3309 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 break;
3311 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00003312
3313 case LATE_PARSED_TEMPLATE: {
3314 LateParsedTemplates.append(Record.begin(), Record.end());
3315 break;
3316 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003317
3318 case OPTIMIZE_PRAGMA_OPTIONS:
3319 if (Record.size() != 1) {
3320 Error("invalid pragma optimize record");
3321 return Failure;
3322 }
3323 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3324 break;
Nico Weber72889432014-09-06 01:25:55 +00003325
3326 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3327 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3328 UnusedLocalTypedefNameCandidates.push_back(
3329 getGlobalDeclID(F, Record[I]));
3330 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003331 }
3332 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003333}
3334
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003335ASTReader::ASTReadResult
3336ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3337 const ModuleFile *ImportedBy,
3338 unsigned ClientLoadCapabilities) {
3339 unsigned Idx = 0;
3340 F.ModuleMapPath = ReadString(Record, Idx);
3341
Richard Smithe842a472014-10-22 02:05:46 +00003342 if (F.Kind == MK_ExplicitModule) {
3343 // For an explicitly-loaded module, we don't care whether the original
3344 // module map file exists or matches.
3345 return Success;
3346 }
3347
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003348 // Try to resolve ModuleName in the current header search context and
3349 // verify that it is found in the same module map file as we saved. If the
3350 // top-level AST file is a main file, skip this check because there is no
3351 // usable header search context.
3352 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003353 "MODULE_NAME should come before MODULE_MAP_FILE");
3354 if (F.Kind == MK_ImplicitModule &&
3355 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3356 // An implicitly-loaded module file should have its module listed in some
3357 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003358 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003359 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3360 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3361 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003362 assert(ImportedBy && "top-level import should be verified");
3363 if ((ClientLoadCapabilities & ARR_Missing) == 0)
Richard Smithe842a472014-10-22 02:05:46 +00003364 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3365 << ImportedBy->FileName
3366 << F.ModuleMapPath;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003367 return Missing;
3368 }
3369
Richard Smithe842a472014-10-22 02:05:46 +00003370 assert(M->Name == F.ModuleName && "found module with different name");
3371
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003372 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003373 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003374 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3375 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003376 assert(ImportedBy && "top-level import should be verified");
3377 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3378 Diag(diag::err_imported_module_modmap_changed)
3379 << F.ModuleName << ImportedBy->FileName
3380 << ModMap->getName() << F.ModuleMapPath;
3381 return OutOfDate;
3382 }
3383
3384 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3385 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3386 // FIXME: we should use input files rather than storing names.
3387 std::string Filename = ReadString(Record, Idx);
3388 const FileEntry *F =
3389 FileMgr.getFile(Filename, false, false);
3390 if (F == nullptr) {
3391 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3392 Error("could not find file '" + Filename +"' referenced by AST file");
3393 return OutOfDate;
3394 }
3395 AdditionalStoredMaps.insert(F);
3396 }
3397
3398 // Check any additional module map files (e.g. module.private.modulemap)
3399 // that are not in the pcm.
3400 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3401 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3402 // Remove files that match
3403 // Note: SmallPtrSet::erase is really remove
3404 if (!AdditionalStoredMaps.erase(ModMap)) {
3405 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3406 Diag(diag::err_module_different_modmap)
3407 << F.ModuleName << /*new*/0 << ModMap->getName();
3408 return OutOfDate;
3409 }
3410 }
3411 }
3412
3413 // Check any additional module map files that are in the pcm, but not
3414 // found in header search. Cases that match are already removed.
3415 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3416 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3417 Diag(diag::err_module_different_modmap)
3418 << F.ModuleName << /*not new*/1 << ModMap->getName();
3419 return OutOfDate;
3420 }
3421 }
3422
3423 if (Listener)
3424 Listener->ReadModuleMapFile(F.ModuleMapPath);
3425 return Success;
3426}
3427
3428
Douglas Gregorc1489562013-02-12 23:36:21 +00003429/// \brief Move the given method to the back of the global list of methods.
3430static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3431 // Find the entry for this selector in the method pool.
3432 Sema::GlobalMethodPool::iterator Known
3433 = S.MethodPool.find(Method->getSelector());
3434 if (Known == S.MethodPool.end())
3435 return;
3436
3437 // Retrieve the appropriate method list.
3438 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3439 : Known->second.second;
3440 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003441 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003442 if (!Found) {
3443 if (List->Method == Method) {
3444 Found = true;
3445 } else {
3446 // Keep searching.
3447 continue;
3448 }
3449 }
3450
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003451 if (List->getNext())
3452 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00003453 else
3454 List->Method = Method;
3455 }
3456}
3457
Richard Smithe657bbd2014-07-18 22:13:40 +00003458void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
3459 bool FromFinalization) {
3460 // FIXME: Only do this if Owner->NameVisibility == AllVisible.
Richard Smith57721ac2014-07-21 04:10:40 +00003461 for (Decl *D : Names.HiddenDecls) {
Richard Smith49f906a2014-03-01 00:08:04 +00003462 bool wasHidden = D->Hidden;
3463 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003464
Richard Smith49f906a2014-03-01 00:08:04 +00003465 if (wasHidden && SemaObj) {
3466 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3467 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003468 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003469 }
3470 }
Richard Smith49f906a2014-03-01 00:08:04 +00003471
Richard Smithe657bbd2014-07-18 22:13:40 +00003472 assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
3473 "nothing to make visible?");
Richard Smithdaa69e02014-07-25 04:40:03 +00003474 for (const auto &Macro : Names.HiddenMacros) {
3475 if (FromFinalization)
3476 PP.appendMacroDirective(Macro.first,
3477 Macro.second->import(PP, SourceLocation()));
3478 else
3479 installImportedMacro(Macro.first, Macro.second, Owner);
3480 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003481}
3482
Richard Smith49f906a2014-03-01 00:08:04 +00003483void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003484 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003485 SourceLocation ImportLoc,
3486 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003487 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003488 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003489 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003491 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003492
3493 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003494 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003495 // there is nothing more to do.
3496 continue;
3497 }
Richard Smith49f906a2014-03-01 00:08:04 +00003498
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 if (!Mod->isAvailable()) {
3500 // Modules that aren't available cannot be made visible.
3501 continue;
3502 }
3503
3504 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003505 if (NameVisibility >= Module::MacrosVisible &&
3506 Mod->NameVisibility < Module::MacrosVisible)
3507 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003508 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003509
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 // If we've already deserialized any names from this module,
3511 // mark them as visible.
3512 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3513 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003514 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003515 HiddenNamesMap.erase(Hidden);
Richard Smith57721ac2014-07-21 04:10:40 +00003516 makeNamesVisible(HiddenNames.second, HiddenNames.first,
3517 /*FromFinalization*/false);
3518 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3519 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003520 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003521
Guy Benyei11169dd2012-12-18 14:30:41 +00003522 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003523 SmallVector<Module *, 16> Exports;
3524 Mod->getExportedModules(Exports);
3525 for (SmallVectorImpl<Module *>::iterator
3526 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3527 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003528 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003529 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003530 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003531
3532 // Detect any conflicts.
3533 if (Complain) {
3534 assert(ImportLoc.isValid() && "Missing import location");
3535 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3536 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3537 Diag(ImportLoc, diag::warn_module_conflict)
3538 << Mod->getFullModuleName()
3539 << Mod->Conflicts[I].Other->getFullModuleName()
3540 << Mod->Conflicts[I].Message;
3541 // FIXME: Need note where the other module was imported.
3542 }
3543 }
3544 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003545 }
3546}
3547
Douglas Gregore060e572013-01-25 01:03:03 +00003548bool ASTReader::loadGlobalIndex() {
3549 if (GlobalIndex)
3550 return false;
3551
3552 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3553 !Context.getLangOpts().Modules)
3554 return true;
3555
3556 // Try to load the global index.
3557 TriedLoadingGlobalIndex = true;
3558 StringRef ModuleCachePath
3559 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3560 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003561 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003562 if (!Result.first)
3563 return true;
3564
3565 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003566 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003567 return false;
3568}
3569
3570bool ASTReader::isGlobalIndexUnavailable() const {
3571 return Context.getLangOpts().Modules && UseGlobalIndex &&
3572 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3573}
3574
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003575static void updateModuleTimestamp(ModuleFile &MF) {
3576 // Overwrite the timestamp file contents so that file's mtime changes.
3577 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003578 std::error_code EC;
3579 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3580 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003581 return;
3582 OS << "Timestamp file\n";
3583}
3584
Guy Benyei11169dd2012-12-18 14:30:41 +00003585ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3586 ModuleKind Type,
3587 SourceLocation ImportLoc,
3588 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003589 llvm::SaveAndRestore<SourceLocation>
3590 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3591
Richard Smithd1c46742014-04-30 02:24:17 +00003592 // Defer any pending actions until we get to the end of reading the AST file.
3593 Deserializing AnASTFile(this);
3594
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003596 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003597
3598 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003599 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003600 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003601 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003602 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003603 ClientLoadCapabilities)) {
3604 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003605 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003606 case OutOfDate:
3607 case VersionMismatch:
3608 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003609 case HadErrors: {
3610 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3611 for (const ImportedModule &IM : Loaded)
3612 LoadedSet.insert(IM.Mod);
3613
Douglas Gregor7029ce12013-03-19 00:28:20 +00003614 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003615 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003616 Context.getLangOpts().Modules
3617 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003618 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003619
3620 // If we find that any modules are unusable, the global index is going
3621 // to be out-of-date. Just remove it.
3622 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003623 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003624 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003625 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003626 case Success:
3627 break;
3628 }
3629
3630 // Here comes stuff that we only do once the entire chain is loaded.
3631
3632 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003633 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3634 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 M != MEnd; ++M) {
3636 ModuleFile &F = *M->Mod;
3637
3638 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003639 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3640 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003641
3642 // Once read, set the ModuleFile bit base offset and update the size in
3643 // bits of all files we've seen.
3644 F.GlobalBitOffset = TotalModulesSizeInBits;
3645 TotalModulesSizeInBits += F.SizeInBits;
3646 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3647
3648 // Preload SLocEntries.
3649 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3650 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3651 // Load it through the SourceManager and don't call ReadSLocEntry()
3652 // directly because the entry may have already been loaded in which case
3653 // calling ReadSLocEntry() directly would trigger an assertion in
3654 // SourceManager.
3655 SourceMgr.getLoadedSLocEntryByID(Index);
3656 }
3657 }
3658
Douglas Gregor603cd862013-03-22 18:50:14 +00003659 // Setup the import locations and notify the module manager that we've
3660 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003661 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3662 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003663 M != MEnd; ++M) {
3664 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003665
3666 ModuleMgr.moduleFileAccepted(&F);
3667
3668 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003669 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003670 if (!M->ImportedBy)
3671 F.ImportLoc = M->ImportLoc;
3672 else
3673 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3674 M->ImportLoc.getRawEncoding());
3675 }
3676
3677 // Mark all of the identifiers in the identifier table as being out of date,
3678 // so that various accessors know to check the loaded modules when the
3679 // identifier is used.
3680 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3681 IdEnd = PP.getIdentifierTable().end();
3682 Id != IdEnd; ++Id)
3683 Id->second->setOutOfDate(true);
3684
3685 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003686 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3687 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3689 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003690
3691 switch (Unresolved.Kind) {
3692 case UnresolvedModuleRef::Conflict:
3693 if (ResolvedMod) {
3694 Module::Conflict Conflict;
3695 Conflict.Other = ResolvedMod;
3696 Conflict.Message = Unresolved.String.str();
3697 Unresolved.Mod->Conflicts.push_back(Conflict);
3698 }
3699 continue;
3700
3701 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 if (ResolvedMod)
3703 Unresolved.Mod->Imports.push_back(ResolvedMod);
3704 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003705
Douglas Gregorfb912652013-03-20 21:10:35 +00003706 case UnresolvedModuleRef::Export:
3707 if (ResolvedMod || Unresolved.IsWildcard)
3708 Unresolved.Mod->Exports.push_back(
3709 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3710 continue;
3711 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003712 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003713 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003714
3715 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3716 // Might be unnecessary as use declarations are only used to build the
3717 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003718
3719 InitializeContext();
3720
Richard Smith3d8e97e2013-10-18 06:54:39 +00003721 if (SemaObj)
3722 UpdateSema();
3723
Guy Benyei11169dd2012-12-18 14:30:41 +00003724 if (DeserializationListener)
3725 DeserializationListener->ReaderInitialized(this);
3726
3727 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3728 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3729 PrimaryModule.OriginalSourceFileID
3730 = FileID::get(PrimaryModule.SLocEntryBaseID
3731 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3732
3733 // If this AST file is a precompiled preamble, then set the
3734 // preamble file ID of the source manager to the file source file
3735 // from which the preamble was built.
3736 if (Type == MK_Preamble) {
3737 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3738 } else if (Type == MK_MainFile) {
3739 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3740 }
3741 }
3742
3743 // For any Objective-C class definitions we have already loaded, make sure
3744 // that we load any additional categories.
3745 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3746 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3747 ObjCClassesLoaded[I],
3748 PreviousGeneration);
3749 }
Douglas Gregore060e572013-01-25 01:03:03 +00003750
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003751 if (PP.getHeaderSearchInfo()
3752 .getHeaderSearchOpts()
3753 .ModulesValidateOncePerBuildSession) {
3754 // Now we are certain that the module and all modules it depends on are
3755 // up to date. Create or update timestamp files for modules that are
3756 // located in the module cache (not for PCH files that could be anywhere
3757 // in the filesystem).
3758 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3759 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003760 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003761 updateModuleTimestamp(*M.Mod);
3762 }
3763 }
3764 }
3765
Guy Benyei11169dd2012-12-18 14:30:41 +00003766 return Success;
3767}
3768
Ben Langmuir487ea142014-10-23 18:05:36 +00003769static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3770
Guy Benyei11169dd2012-12-18 14:30:41 +00003771ASTReader::ASTReadResult
3772ASTReader::ReadASTCore(StringRef FileName,
3773 ModuleKind Type,
3774 SourceLocation ImportLoc,
3775 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003776 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003777 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003778 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003779 unsigned ClientLoadCapabilities) {
3780 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003781 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003782 ModuleManager::AddModuleResult AddResult
3783 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003784 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003785 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003786 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003787
Douglas Gregor7029ce12013-03-19 00:28:20 +00003788 switch (AddResult) {
3789 case ModuleManager::AlreadyLoaded:
3790 return Success;
3791
3792 case ModuleManager::NewlyLoaded:
3793 // Load module file below.
3794 break;
3795
3796 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003797 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003798 // it.
3799 if (ClientLoadCapabilities & ARR_Missing)
3800 return Missing;
3801
3802 // Otherwise, return an error.
3803 {
3804 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3805 + ErrorStr;
3806 Error(Msg);
3807 }
3808 return Failure;
3809
3810 case ModuleManager::OutOfDate:
3811 // We couldn't load the module file because it is out-of-date. If the
3812 // client can handle out-of-date, return it.
3813 if (ClientLoadCapabilities & ARR_OutOfDate)
3814 return OutOfDate;
3815
3816 // Otherwise, return an error.
3817 {
3818 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3819 + ErrorStr;
3820 Error(Msg);
3821 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003822 return Failure;
3823 }
3824
Douglas Gregor7029ce12013-03-19 00:28:20 +00003825 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003826
3827 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3828 // module?
3829 if (FileName != "-") {
3830 CurrentDir = llvm::sys::path::parent_path(FileName);
3831 if (CurrentDir.empty()) CurrentDir = ".";
3832 }
3833
3834 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003835 BitstreamCursor &Stream = F.Stream;
Rafael Espindolafd832392014-11-12 14:48:44 +00003836 Stream.init(&F.StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00003837 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3838
3839 // Sniff for the signature.
3840 if (Stream.Read(8) != 'C' ||
3841 Stream.Read(8) != 'P' ||
3842 Stream.Read(8) != 'C' ||
3843 Stream.Read(8) != 'H') {
3844 Diag(diag::err_not_a_pch_file) << FileName;
3845 return Failure;
3846 }
3847
3848 // This is used for compatibility with older PCH formats.
3849 bool HaveReadControlBlock = false;
3850
Chris Lattnerefa77172013-01-20 00:00:22 +00003851 while (1) {
3852 llvm::BitstreamEntry Entry = Stream.advance();
3853
3854 switch (Entry.Kind) {
3855 case llvm::BitstreamEntry::Error:
3856 case llvm::BitstreamEntry::EndBlock:
3857 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003858 Error("invalid record at top-level of AST file");
3859 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003860
3861 case llvm::BitstreamEntry::SubBlock:
3862 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003863 }
3864
Guy Benyei11169dd2012-12-18 14:30:41 +00003865 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003866 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3868 if (Stream.ReadBlockInfoBlock()) {
3869 Error("malformed BlockInfoBlock in AST file");
3870 return Failure;
3871 }
3872 break;
3873 case CONTROL_BLOCK_ID:
3874 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003875 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003876 case Success:
3877 break;
3878
3879 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003880 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003881 case OutOfDate: return OutOfDate;
3882 case VersionMismatch: return VersionMismatch;
3883 case ConfigurationMismatch: return ConfigurationMismatch;
3884 case HadErrors: return HadErrors;
3885 }
3886 break;
3887 case AST_BLOCK_ID:
3888 if (!HaveReadControlBlock) {
3889 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003890 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003891 return VersionMismatch;
3892 }
3893
3894 // Record that we've loaded this module.
3895 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3896 return Success;
3897
3898 default:
3899 if (Stream.SkipBlock()) {
3900 Error("malformed block record in AST file");
3901 return Failure;
3902 }
3903 break;
3904 }
3905 }
3906
3907 return Success;
3908}
3909
3910void ASTReader::InitializeContext() {
3911 // If there's a listener, notify them that we "read" the translation unit.
3912 if (DeserializationListener)
3913 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3914 Context.getTranslationUnitDecl());
3915
Guy Benyei11169dd2012-12-18 14:30:41 +00003916 // FIXME: Find a better way to deal with collisions between these
3917 // built-in types. Right now, we just ignore the problem.
3918
3919 // Load the special types.
3920 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3921 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3922 if (!Context.CFConstantStringTypeDecl)
3923 Context.setCFConstantStringType(GetType(String));
3924 }
3925
3926 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3927 QualType FileType = GetType(File);
3928 if (FileType.isNull()) {
3929 Error("FILE type is NULL");
3930 return;
3931 }
3932
3933 if (!Context.FILEDecl) {
3934 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3935 Context.setFILEDecl(Typedef->getDecl());
3936 else {
3937 const TagType *Tag = FileType->getAs<TagType>();
3938 if (!Tag) {
3939 Error("Invalid FILE type in AST file");
3940 return;
3941 }
3942 Context.setFILEDecl(Tag->getDecl());
3943 }
3944 }
3945 }
3946
3947 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3948 QualType Jmp_bufType = GetType(Jmp_buf);
3949 if (Jmp_bufType.isNull()) {
3950 Error("jmp_buf type is NULL");
3951 return;
3952 }
3953
3954 if (!Context.jmp_bufDecl) {
3955 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3956 Context.setjmp_bufDecl(Typedef->getDecl());
3957 else {
3958 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3959 if (!Tag) {
3960 Error("Invalid jmp_buf type in AST file");
3961 return;
3962 }
3963 Context.setjmp_bufDecl(Tag->getDecl());
3964 }
3965 }
3966 }
3967
3968 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3969 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3970 if (Sigjmp_bufType.isNull()) {
3971 Error("sigjmp_buf type is NULL");
3972 return;
3973 }
3974
3975 if (!Context.sigjmp_bufDecl) {
3976 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3977 Context.setsigjmp_bufDecl(Typedef->getDecl());
3978 else {
3979 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3980 assert(Tag && "Invalid sigjmp_buf type in AST file");
3981 Context.setsigjmp_bufDecl(Tag->getDecl());
3982 }
3983 }
3984 }
3985
3986 if (unsigned ObjCIdRedef
3987 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3988 if (Context.ObjCIdRedefinitionType.isNull())
3989 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3990 }
3991
3992 if (unsigned ObjCClassRedef
3993 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3994 if (Context.ObjCClassRedefinitionType.isNull())
3995 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3996 }
3997
3998 if (unsigned ObjCSelRedef
3999 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4000 if (Context.ObjCSelRedefinitionType.isNull())
4001 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4002 }
4003
4004 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4005 QualType Ucontext_tType = GetType(Ucontext_t);
4006 if (Ucontext_tType.isNull()) {
4007 Error("ucontext_t type is NULL");
4008 return;
4009 }
4010
4011 if (!Context.ucontext_tDecl) {
4012 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4013 Context.setucontext_tDecl(Typedef->getDecl());
4014 else {
4015 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4016 assert(Tag && "Invalid ucontext_t type in AST file");
4017 Context.setucontext_tDecl(Tag->getDecl());
4018 }
4019 }
4020 }
4021 }
4022
4023 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4024
4025 // If there were any CUDA special declarations, deserialize them.
4026 if (!CUDASpecialDeclRefs.empty()) {
4027 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4028 Context.setcudaConfigureCallDecl(
4029 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4030 }
Richard Smith56be7542014-03-21 00:33:59 +00004031
Guy Benyei11169dd2012-12-18 14:30:41 +00004032 // Re-export any modules that were imported by a non-module AST file.
Richard Smith56be7542014-03-21 00:33:59 +00004033 // FIXME: This does not make macro-only imports visible again. It also doesn't
4034 // make #includes mapped to module imports visible.
4035 for (auto &Import : ImportedModules) {
4036 if (Module *Imported = getSubmodule(Import.ID))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004037 makeModuleVisible(Imported, Module::AllVisible,
Richard Smith56be7542014-03-21 00:33:59 +00004038 /*ImportLoc=*/Import.ImportLoc,
Douglas Gregorfb912652013-03-20 21:10:35 +00004039 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00004040 }
4041 ImportedModules.clear();
4042}
4043
4044void ASTReader::finalizeForWriting() {
Richard Smith57721ac2014-07-21 04:10:40 +00004045 while (!HiddenNamesMap.empty()) {
4046 auto HiddenNames = std::move(*HiddenNamesMap.begin());
4047 HiddenNamesMap.erase(HiddenNamesMap.begin());
4048 makeNamesVisible(HiddenNames.second, HiddenNames.first,
4049 /*FromFinalization*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004050 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004051}
4052
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004053/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
4054/// cursor into the start of the given block ID, returning false on success and
4055/// true on failure.
4056static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004057 while (1) {
4058 llvm::BitstreamEntry Entry = Cursor.advance();
4059 switch (Entry.Kind) {
4060 case llvm::BitstreamEntry::Error:
4061 case llvm::BitstreamEntry::EndBlock:
4062 return true;
4063
4064 case llvm::BitstreamEntry::Record:
4065 // Ignore top-level records.
4066 Cursor.skipRecord(Entry.ID);
4067 break;
4068
4069 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004070 if (Entry.ID == BlockID) {
4071 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004072 return true;
4073 // Found it!
4074 return false;
4075 }
4076
4077 if (Cursor.SkipBlock())
4078 return true;
4079 }
4080 }
4081}
4082
Ben Langmuir487ea142014-10-23 18:05:36 +00004083static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4084 BitstreamCursor Stream(StreamFile);
4085 if (Stream.Read(8) != 'C' ||
4086 Stream.Read(8) != 'P' ||
4087 Stream.Read(8) != 'C' ||
4088 Stream.Read(8) != 'H') {
4089 return 0;
4090 }
4091
4092 // Scan for the CONTROL_BLOCK_ID block.
4093 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4094 return 0;
4095
4096 // Scan for SIGNATURE inside the control block.
4097 ASTReader::RecordData Record;
4098 while (1) {
4099 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4100 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4101 Entry.Kind != llvm::BitstreamEntry::Record)
4102 return 0;
4103
4104 Record.clear();
4105 StringRef Blob;
4106 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4107 return Record[0];
4108 }
4109}
4110
Guy Benyei11169dd2012-12-18 14:30:41 +00004111/// \brief Retrieve the name of the original source file name
4112/// directly from the AST file, without actually loading the AST
4113/// file.
4114std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
4115 FileManager &FileMgr,
4116 DiagnosticsEngine &Diags) {
4117 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004118 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004119 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004120 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4121 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00004122 return std::string();
4123 }
4124
4125 // Initialize the stream
4126 llvm::BitstreamReader StreamFile;
Benjamin Kramera8857962014-10-26 22:44:13 +00004127 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4128 (const unsigned char *)(*Buffer)->getBufferEnd());
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004129 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004130
4131 // Sniff for the signature.
4132 if (Stream.Read(8) != 'C' ||
4133 Stream.Read(8) != 'P' ||
4134 Stream.Read(8) != 'C' ||
4135 Stream.Read(8) != 'H') {
4136 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4137 return std::string();
4138 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004139
Chris Lattnere7b154b2013-01-19 21:39:22 +00004140 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004141 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004142 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4143 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004144 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004145
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004146 // Scan for ORIGINAL_FILE inside the control block.
4147 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00004148 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004149 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004150 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4151 return std::string();
4152
4153 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4154 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4155 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004156 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00004157
Guy Benyei11169dd2012-12-18 14:30:41 +00004158 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004159 StringRef Blob;
4160 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4161 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004162 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004163}
4164
4165namespace {
4166 class SimplePCHValidator : public ASTReaderListener {
4167 const LangOptions &ExistingLangOpts;
4168 const TargetOptions &ExistingTargetOpts;
4169 const PreprocessorOptions &ExistingPPOpts;
4170 FileManager &FileMgr;
4171
4172 public:
4173 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4174 const TargetOptions &ExistingTargetOpts,
4175 const PreprocessorOptions &ExistingPPOpts,
4176 FileManager &FileMgr)
4177 : ExistingLangOpts(ExistingLangOpts),
4178 ExistingTargetOpts(ExistingTargetOpts),
4179 ExistingPPOpts(ExistingPPOpts),
4180 FileMgr(FileMgr)
4181 {
4182 }
4183
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004184 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4185 bool AllowCompatibleDifferences) override {
4186 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4187 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004188 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004189 bool ReadTargetOptions(const TargetOptions &TargetOpts,
4190 bool Complain) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004191 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004192 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004193 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4194 bool Complain,
4195 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004196 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004197 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004198 }
4199 };
4200}
4201
4202bool ASTReader::readASTFileControlBlock(StringRef Filename,
4203 FileManager &FileMgr,
4204 ASTReaderListener &Listener) {
4205 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004206 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004207 if (!Buffer) {
4208 return true;
4209 }
4210
4211 // Initialize the stream
4212 llvm::BitstreamReader StreamFile;
Benjamin Kramera8857962014-10-26 22:44:13 +00004213 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4214 (const unsigned char *)(*Buffer)->getBufferEnd());
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004215 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004216
4217 // Sniff for the signature.
4218 if (Stream.Read(8) != 'C' ||
4219 Stream.Read(8) != 'P' ||
4220 Stream.Read(8) != 'C' ||
4221 Stream.Read(8) != 'H') {
4222 return true;
4223 }
4224
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004225 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004226 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004227 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004228
4229 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004230 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004231 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004232 BitstreamCursor InputFilesCursor;
4233 if (NeedsInputFiles) {
4234 InputFilesCursor = Stream;
4235 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4236 return true;
4237
4238 // Read the abbreviations
4239 while (true) {
4240 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4241 unsigned Code = InputFilesCursor.ReadCode();
4242
4243 // We expect all abbrevs to be at the start of the block.
4244 if (Code != llvm::bitc::DEFINE_ABBREV) {
4245 InputFilesCursor.JumpToBit(Offset);
4246 break;
4247 }
4248 InputFilesCursor.ReadAbbrevRecord();
4249 }
4250 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004251
4252 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00004253 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004254 while (1) {
4255 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4256 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4257 return false;
4258
4259 if (Entry.Kind != llvm::BitstreamEntry::Record)
4260 return true;
4261
Guy Benyei11169dd2012-12-18 14:30:41 +00004262 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004263 StringRef Blob;
4264 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004265 switch ((ControlRecordTypes)RecCode) {
4266 case METADATA: {
4267 if (Record[0] != VERSION_MAJOR)
4268 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004269
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004270 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004271 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004272
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004273 break;
4274 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004275 case MODULE_NAME:
4276 Listener.ReadModuleName(Blob);
4277 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004278 case MODULE_MAP_FILE: {
4279 unsigned Idx = 0;
4280 Listener.ReadModuleMapFile(ReadString(Record, Idx));
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004281 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004282 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004283 case LANGUAGE_OPTIONS:
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004284 if (ParseLanguageOptions(Record, false, Listener,
4285 /*AllowCompatibleConfigurationMismatch*/false))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004286 return true;
4287 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004288
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004289 case TARGET_OPTIONS:
4290 if (ParseTargetOptions(Record, false, Listener))
4291 return true;
4292 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004293
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004294 case DIAGNOSTIC_OPTIONS:
4295 if (ParseDiagnosticOptions(Record, false, Listener))
4296 return true;
4297 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004298
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004299 case FILE_SYSTEM_OPTIONS:
4300 if (ParseFileSystemOptions(Record, false, Listener))
4301 return true;
4302 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004303
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004304 case HEADER_SEARCH_OPTIONS:
4305 if (ParseHeaderSearchOptions(Record, false, Listener))
4306 return true;
4307 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004308
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004309 case PREPROCESSOR_OPTIONS: {
4310 std::string IgnoredSuggestedPredefines;
4311 if (ParsePreprocessorOptions(Record, false, Listener,
4312 IgnoredSuggestedPredefines))
4313 return true;
4314 break;
4315 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004316
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004317 case INPUT_FILE_OFFSETS: {
4318 if (!NeedsInputFiles)
4319 break;
4320
4321 unsigned NumInputFiles = Record[0];
4322 unsigned NumUserFiles = Record[1];
4323 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4324 for (unsigned I = 0; I != NumInputFiles; ++I) {
4325 // Go find this input file.
4326 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004327
4328 if (isSystemFile && !NeedsSystemInputFiles)
4329 break; // the rest are system input files
4330
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004331 BitstreamCursor &Cursor = InputFilesCursor;
4332 SavedStreamPosition SavedPosition(Cursor);
4333 Cursor.JumpToBit(InputFileOffs[I]);
4334
4335 unsigned Code = Cursor.ReadCode();
4336 RecordData Record;
4337 StringRef Blob;
4338 bool shouldContinue = false;
4339 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4340 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004341 bool Overridden = static_cast<bool>(Record[3]);
4342 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004343 break;
4344 }
4345 if (!shouldContinue)
4346 break;
4347 }
4348 break;
4349 }
4350
Richard Smithd4b230b2014-10-27 23:01:16 +00004351 case IMPORTS: {
4352 if (!NeedsImports)
4353 break;
4354
4355 unsigned Idx = 0, N = Record.size();
4356 while (Idx < N) {
4357 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004358 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smithd4b230b2014-10-27 23:01:16 +00004359 unsigned Length = Record[Idx++];
4360 SmallString<128> ImportedFile(Record.begin() + Idx,
4361 Record.begin() + Idx + Length);
4362 Idx += Length;
4363 Listener.visitImport(ImportedFile);
4364 }
4365 break;
4366 }
4367
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004368 default:
4369 // No other validation to perform.
4370 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004371 }
4372 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004373}
4374
4375
4376bool ASTReader::isAcceptableASTFile(StringRef Filename,
4377 FileManager &FileMgr,
4378 const LangOptions &LangOpts,
4379 const TargetOptions &TargetOpts,
4380 const PreprocessorOptions &PPOpts) {
4381 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4382 return !readASTFileControlBlock(Filename, FileMgr, validator);
4383}
4384
Ben Langmuir2c9af442014-04-10 17:57:43 +00004385ASTReader::ASTReadResult
4386ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004387 // Enter the submodule block.
4388 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4389 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004390 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004391 }
4392
4393 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4394 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004395 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004396 RecordData Record;
4397 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004398 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4399
4400 switch (Entry.Kind) {
4401 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4402 case llvm::BitstreamEntry::Error:
4403 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004404 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004405 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004406 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004407 case llvm::BitstreamEntry::Record:
4408 // The interesting case.
4409 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004410 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004411
Guy Benyei11169dd2012-12-18 14:30:41 +00004412 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004413 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004414 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004415 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4416
4417 if ((Kind == SUBMODULE_METADATA) != First) {
4418 Error("submodule metadata record should be at beginning of block");
4419 return Failure;
4420 }
4421 First = false;
4422
4423 // Submodule information is only valid if we have a current module.
4424 // FIXME: Should we error on these cases?
4425 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4426 Kind != SUBMODULE_DEFINITION)
4427 continue;
4428
4429 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004430 default: // Default behavior: ignore.
4431 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004432
Richard Smith03478d92014-10-23 22:12:14 +00004433 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004434 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004435 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004436 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004437 }
Richard Smith03478d92014-10-23 22:12:14 +00004438
Chris Lattner0e6c9402013-01-20 02:38:54 +00004439 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004440 unsigned Idx = 0;
4441 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4442 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4443 bool IsFramework = Record[Idx++];
4444 bool IsExplicit = Record[Idx++];
4445 bool IsSystem = Record[Idx++];
4446 bool IsExternC = Record[Idx++];
4447 bool InferSubmodules = Record[Idx++];
4448 bool InferExplicitSubmodules = Record[Idx++];
4449 bool InferExportWildcard = Record[Idx++];
4450 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004451
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004452 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004453 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004454 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004455
Guy Benyei11169dd2012-12-18 14:30:41 +00004456 // Retrieve this (sub)module from the module map, creating it if
4457 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004458 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004459 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004460
4461 // FIXME: set the definition loc for CurrentModule, or call
4462 // ModMap.setInferredModuleAllowedBy()
4463
Guy Benyei11169dd2012-12-18 14:30:41 +00004464 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4465 if (GlobalIndex >= SubmodulesLoaded.size() ||
4466 SubmodulesLoaded[GlobalIndex]) {
4467 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004468 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004469 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004470
Douglas Gregor7029ce12013-03-19 00:28:20 +00004471 if (!ParentModule) {
4472 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4473 if (CurFile != F.File) {
4474 if (!Diags.isDiagnosticInFlight()) {
4475 Diag(diag::err_module_file_conflict)
4476 << CurrentModule->getTopLevelModuleName()
4477 << CurFile->getName()
4478 << F.File->getName();
4479 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004480 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004481 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004482 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004483
4484 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004485 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004486
Guy Benyei11169dd2012-12-18 14:30:41 +00004487 CurrentModule->IsFromModuleFile = true;
4488 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004489 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004490 CurrentModule->InferSubmodules = InferSubmodules;
4491 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4492 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004493 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004494 if (DeserializationListener)
4495 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4496
4497 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004498
Douglas Gregorfb912652013-03-20 21:10:35 +00004499 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004500 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004501 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004502 CurrentModule->UnresolvedConflicts.clear();
4503 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004504 break;
4505 }
4506
4507 case SUBMODULE_UMBRELLA_HEADER: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004508 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004509 if (!CurrentModule->getUmbrellaHeader())
4510 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4511 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004512 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4513 Error("mismatched umbrella headers in submodule");
4514 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004515 }
4516 }
4517 break;
4518 }
4519
Richard Smith202210b2014-10-24 20:23:01 +00004520 case SUBMODULE_HEADER:
4521 case SUBMODULE_EXCLUDED_HEADER:
4522 case SUBMODULE_PRIVATE_HEADER:
4523 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004524 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4525 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004526 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004527
Richard Smith202210b2014-10-24 20:23:01 +00004528 case SUBMODULE_TEXTUAL_HEADER:
4529 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4530 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4531 // them here.
4532 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004533
Guy Benyei11169dd2012-12-18 14:30:41 +00004534 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004535 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004536 break;
4537 }
4538
4539 case SUBMODULE_UMBRELLA_DIR: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004540 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004541 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004542 if (!CurrentModule->getUmbrellaDir())
4543 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4544 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004545 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4546 Error("mismatched umbrella directories in submodule");
4547 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004548 }
4549 }
4550 break;
4551 }
4552
4553 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004554 F.BaseSubmoduleID = getTotalNumSubmodules();
4555 F.LocalNumSubmodules = Record[0];
4556 unsigned LocalBaseSubmoduleID = Record[1];
4557 if (F.LocalNumSubmodules > 0) {
4558 // Introduce the global -> local mapping for submodules within this
4559 // module.
4560 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4561
4562 // Introduce the local -> global mapping for submodules within this
4563 // module.
4564 F.SubmoduleRemap.insertOrReplace(
4565 std::make_pair(LocalBaseSubmoduleID,
4566 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004567
Ben Langmuir52ca6782014-10-20 16:27:32 +00004568 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4569 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004570 break;
4571 }
4572
4573 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004574 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004575 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004576 Unresolved.File = &F;
4577 Unresolved.Mod = CurrentModule;
4578 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004579 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004580 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004581 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004582 }
4583 break;
4584 }
4585
4586 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004587 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004588 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004589 Unresolved.File = &F;
4590 Unresolved.Mod = CurrentModule;
4591 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004592 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004593 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004594 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004595 }
4596
4597 // Once we've loaded the set of exports, there's no reason to keep
4598 // the parsed, unresolved exports around.
4599 CurrentModule->UnresolvedExports.clear();
4600 break;
4601 }
4602 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004603 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004604 Context.getTargetInfo());
4605 break;
4606 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004607
4608 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004609 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004610 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004611 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004612
4613 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004614 CurrentModule->ConfigMacros.push_back(Blob.str());
4615 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004616
4617 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004618 UnresolvedModuleRef Unresolved;
4619 Unresolved.File = &F;
4620 Unresolved.Mod = CurrentModule;
4621 Unresolved.ID = Record[0];
4622 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4623 Unresolved.IsWildcard = false;
4624 Unresolved.String = Blob;
4625 UnresolvedModuleRefs.push_back(Unresolved);
4626 break;
4627 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004628 }
4629 }
4630}
4631
4632/// \brief Parse the record that corresponds to a LangOptions data
4633/// structure.
4634///
4635/// This routine parses the language options from the AST file and then gives
4636/// them to the AST listener if one is set.
4637///
4638/// \returns true if the listener deems the file unacceptable, false otherwise.
4639bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4640 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004641 ASTReaderListener &Listener,
4642 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004643 LangOptions LangOpts;
4644 unsigned Idx = 0;
4645#define LANGOPT(Name, Bits, Default, Description) \
4646 LangOpts.Name = Record[Idx++];
4647#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4648 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4649#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004650#define SANITIZER(NAME, ID) \
4651 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004652#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004653
4654 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4655 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4656 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4657
4658 unsigned Length = Record[Idx++];
4659 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4660 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004661
4662 Idx += Length;
4663
4664 // Comment options.
4665 for (unsigned N = Record[Idx++]; N; --N) {
4666 LangOpts.CommentOpts.BlockCommandNames.push_back(
4667 ReadString(Record, Idx));
4668 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004669 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004670
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004671 return Listener.ReadLanguageOptions(LangOpts, Complain,
4672 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004673}
4674
4675bool ASTReader::ParseTargetOptions(const RecordData &Record,
4676 bool Complain,
4677 ASTReaderListener &Listener) {
4678 unsigned Idx = 0;
4679 TargetOptions TargetOpts;
4680 TargetOpts.Triple = ReadString(Record, Idx);
4681 TargetOpts.CPU = ReadString(Record, Idx);
4682 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004683 for (unsigned N = Record[Idx++]; N; --N) {
4684 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4685 }
4686 for (unsigned N = Record[Idx++]; N; --N) {
4687 TargetOpts.Features.push_back(ReadString(Record, Idx));
4688 }
4689
4690 return Listener.ReadTargetOptions(TargetOpts, Complain);
4691}
4692
4693bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4694 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004695 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004696 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004697#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004698#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004699 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004700#include "clang/Basic/DiagnosticOptions.def"
4701
Richard Smith3be1cb22014-08-07 00:24:21 +00004702 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004703 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004704 for (unsigned N = Record[Idx++]; N; --N)
4705 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004706
4707 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4708}
4709
4710bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4711 ASTReaderListener &Listener) {
4712 FileSystemOptions FSOpts;
4713 unsigned Idx = 0;
4714 FSOpts.WorkingDir = ReadString(Record, Idx);
4715 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4716}
4717
4718bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4719 bool Complain,
4720 ASTReaderListener &Listener) {
4721 HeaderSearchOptions HSOpts;
4722 unsigned Idx = 0;
4723 HSOpts.Sysroot = ReadString(Record, Idx);
4724
4725 // Include entries.
4726 for (unsigned N = Record[Idx++]; N; --N) {
4727 std::string Path = ReadString(Record, Idx);
4728 frontend::IncludeDirGroup Group
4729 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004730 bool IsFramework = Record[Idx++];
4731 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004732 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004733 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004734 }
4735
4736 // System header prefixes.
4737 for (unsigned N = Record[Idx++]; N; --N) {
4738 std::string Prefix = ReadString(Record, Idx);
4739 bool IsSystemHeader = Record[Idx++];
4740 HSOpts.SystemHeaderPrefixes.push_back(
4741 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4742 }
4743
4744 HSOpts.ResourceDir = ReadString(Record, Idx);
4745 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004746 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004747 HSOpts.DisableModuleHash = Record[Idx++];
4748 HSOpts.UseBuiltinIncludes = Record[Idx++];
4749 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4750 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4751 HSOpts.UseLibcxx = Record[Idx++];
4752
4753 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4754}
4755
4756bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4757 bool Complain,
4758 ASTReaderListener &Listener,
4759 std::string &SuggestedPredefines) {
4760 PreprocessorOptions PPOpts;
4761 unsigned Idx = 0;
4762
4763 // Macro definitions/undefs
4764 for (unsigned N = Record[Idx++]; N; --N) {
4765 std::string Macro = ReadString(Record, Idx);
4766 bool IsUndef = Record[Idx++];
4767 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4768 }
4769
4770 // Includes
4771 for (unsigned N = Record[Idx++]; N; --N) {
4772 PPOpts.Includes.push_back(ReadString(Record, Idx));
4773 }
4774
4775 // Macro Includes
4776 for (unsigned N = Record[Idx++]; N; --N) {
4777 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4778 }
4779
4780 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004781 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004782 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4783 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4784 PPOpts.ObjCXXARCStandardLibrary =
4785 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4786 SuggestedPredefines.clear();
4787 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4788 SuggestedPredefines);
4789}
4790
4791std::pair<ModuleFile *, unsigned>
4792ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4793 GlobalPreprocessedEntityMapType::iterator
4794 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4795 assert(I != GlobalPreprocessedEntityMap.end() &&
4796 "Corrupted global preprocessed entity map");
4797 ModuleFile *M = I->second;
4798 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4799 return std::make_pair(M, LocalIndex);
4800}
4801
4802std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4803ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4804 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4805 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4806 Mod.NumPreprocessedEntities);
4807
4808 return std::make_pair(PreprocessingRecord::iterator(),
4809 PreprocessingRecord::iterator());
4810}
4811
4812std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4813ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4814 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4815 ModuleDeclIterator(this, &Mod,
4816 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4817}
4818
4819PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4820 PreprocessedEntityID PPID = Index+1;
4821 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4822 ModuleFile &M = *PPInfo.first;
4823 unsigned LocalIndex = PPInfo.second;
4824 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4825
Guy Benyei11169dd2012-12-18 14:30:41 +00004826 if (!PP.getPreprocessingRecord()) {
4827 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004828 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004829 }
4830
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004831 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4832 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4833
4834 llvm::BitstreamEntry Entry =
4835 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4836 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004837 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004838
Guy Benyei11169dd2012-12-18 14:30:41 +00004839 // Read the record.
4840 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4841 ReadSourceLocation(M, PPOffs.End));
4842 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004843 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004844 RecordData Record;
4845 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004846 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4847 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004848 switch (RecType) {
4849 case PPD_MACRO_EXPANSION: {
4850 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004851 IdentifierInfo *Name = nullptr;
4852 MacroDefinition *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004853 if (isBuiltin)
4854 Name = getLocalIdentifier(M, Record[1]);
4855 else {
4856 PreprocessedEntityID
4857 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4858 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4859 }
4860
4861 MacroExpansion *ME;
4862 if (isBuiltin)
4863 ME = new (PPRec) MacroExpansion(Name, Range);
4864 else
4865 ME = new (PPRec) MacroExpansion(Def, Range);
4866
4867 return ME;
4868 }
4869
4870 case PPD_MACRO_DEFINITION: {
4871 // Decode the identifier info and then check again; if the macro is
4872 // still defined and associated with the identifier,
4873 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4874 MacroDefinition *MD
4875 = new (PPRec) MacroDefinition(II, Range);
4876
4877 if (DeserializationListener)
4878 DeserializationListener->MacroDefinitionRead(PPID, MD);
4879
4880 return MD;
4881 }
4882
4883 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004884 const char *FullFileNameStart = Blob.data() + Record[0];
4885 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004886 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004887 if (!FullFileName.empty())
4888 File = PP.getFileManager().getFile(FullFileName);
4889
4890 // FIXME: Stable encoding
4891 InclusionDirective::InclusionKind Kind
4892 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4893 InclusionDirective *ID
4894 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004895 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004896 Record[1], Record[3],
4897 File,
4898 Range);
4899 return ID;
4900 }
4901 }
4902
4903 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4904}
4905
4906/// \brief \arg SLocMapI points at a chunk of a module that contains no
4907/// preprocessed entities or the entities it contains are not the ones we are
4908/// looking for. Find the next module that contains entities and return the ID
4909/// of the first entry.
4910PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4911 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4912 ++SLocMapI;
4913 for (GlobalSLocOffsetMapType::const_iterator
4914 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4915 ModuleFile &M = *SLocMapI->second;
4916 if (M.NumPreprocessedEntities)
4917 return M.BasePreprocessedEntityID;
4918 }
4919
4920 return getTotalNumPreprocessedEntities();
4921}
4922
4923namespace {
4924
4925template <unsigned PPEntityOffset::*PPLoc>
4926struct PPEntityComp {
4927 const ASTReader &Reader;
4928 ModuleFile &M;
4929
4930 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4931
4932 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4933 SourceLocation LHS = getLoc(L);
4934 SourceLocation RHS = getLoc(R);
4935 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4936 }
4937
4938 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4939 SourceLocation LHS = getLoc(L);
4940 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4941 }
4942
4943 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4944 SourceLocation RHS = getLoc(R);
4945 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4946 }
4947
4948 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4949 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4950 }
4951};
4952
4953}
4954
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004955PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4956 bool EndsAfter) const {
4957 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00004958 return getTotalNumPreprocessedEntities();
4959
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004960 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4961 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004962 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4963 "Corrupted global sloc offset map");
4964
4965 if (SLocMapI->second->NumPreprocessedEntities == 0)
4966 return findNextPreprocessedEntity(SLocMapI);
4967
4968 ModuleFile &M = *SLocMapI->second;
4969 typedef const PPEntityOffset *pp_iterator;
4970 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4971 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4972
4973 size_t Count = M.NumPreprocessedEntities;
4974 size_t Half;
4975 pp_iterator First = pp_begin;
4976 pp_iterator PPI;
4977
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004978 if (EndsAfter) {
4979 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4980 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4981 } else {
4982 // Do a binary search manually instead of using std::lower_bound because
4983 // The end locations of entities may be unordered (when a macro expansion
4984 // is inside another macro argument), but for this case it is not important
4985 // whether we get the first macro expansion or its containing macro.
4986 while (Count > 0) {
4987 Half = Count / 2;
4988 PPI = First;
4989 std::advance(PPI, Half);
4990 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4991 Loc)) {
4992 First = PPI;
4993 ++First;
4994 Count = Count - Half - 1;
4995 } else
4996 Count = Half;
4997 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004998 }
4999
5000 if (PPI == pp_end)
5001 return findNextPreprocessedEntity(SLocMapI);
5002
5003 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5004}
5005
Guy Benyei11169dd2012-12-18 14:30:41 +00005006/// \brief Returns a pair of [Begin, End) indices of preallocated
5007/// preprocessed entities that \arg Range encompasses.
5008std::pair<unsigned, unsigned>
5009 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5010 if (Range.isInvalid())
5011 return std::make_pair(0,0);
5012 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5013
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005014 PreprocessedEntityID BeginID =
5015 findPreprocessedEntity(Range.getBegin(), false);
5016 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005017 return std::make_pair(BeginID, EndID);
5018}
5019
5020/// \brief Optionally returns true or false if the preallocated preprocessed
5021/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005022Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005023 FileID FID) {
5024 if (FID.isInvalid())
5025 return false;
5026
5027 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5028 ModuleFile &M = *PPInfo.first;
5029 unsigned LocalIndex = PPInfo.second;
5030 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5031
5032 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5033 if (Loc.isInvalid())
5034 return false;
5035
5036 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5037 return true;
5038 else
5039 return false;
5040}
5041
5042namespace {
5043 /// \brief Visitor used to search for information about a header file.
5044 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005045 const FileEntry *FE;
5046
David Blaikie05785d12013-02-20 22:23:23 +00005047 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005048
5049 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005050 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5051 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00005052
5053 static bool visit(ModuleFile &M, void *UserData) {
5054 HeaderFileInfoVisitor *This
5055 = static_cast<HeaderFileInfoVisitor *>(UserData);
5056
Guy Benyei11169dd2012-12-18 14:30:41 +00005057 HeaderFileInfoLookupTable *Table
5058 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5059 if (!Table)
5060 return false;
5061
5062 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00005063 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005064 if (Pos == Table->end())
5065 return false;
5066
5067 This->HFI = *Pos;
5068 return true;
5069 }
5070
David Blaikie05785d12013-02-20 22:23:23 +00005071 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005072 };
5073}
5074
5075HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005076 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005077 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005078 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005079 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005080
5081 return HeaderFileInfo();
5082}
5083
5084void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5085 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005086 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005087 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5088 ModuleFile &F = *(*I);
5089 unsigned Idx = 0;
5090 DiagStates.clear();
5091 assert(!Diag.DiagStates.empty());
5092 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5093 while (Idx < F.PragmaDiagMappings.size()) {
5094 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5095 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5096 if (DiagStateID != 0) {
5097 Diag.DiagStatePoints.push_back(
5098 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5099 FullSourceLoc(Loc, SourceMgr)));
5100 continue;
5101 }
5102
5103 assert(DiagStateID == 0);
5104 // A new DiagState was created here.
5105 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5106 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5107 DiagStates.push_back(NewState);
5108 Diag.DiagStatePoints.push_back(
5109 DiagnosticsEngine::DiagStatePoint(NewState,
5110 FullSourceLoc(Loc, SourceMgr)));
5111 while (1) {
5112 assert(Idx < F.PragmaDiagMappings.size() &&
5113 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5114 if (Idx >= F.PragmaDiagMappings.size()) {
5115 break; // Something is messed up but at least avoid infinite loop in
5116 // release build.
5117 }
5118 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5119 if (DiagID == (unsigned)-1) {
5120 break; // no more diag/map pairs for this location.
5121 }
Alp Tokerc726c362014-06-10 09:31:37 +00005122 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5123 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5124 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005125 }
5126 }
5127 }
5128}
5129
5130/// \brief Get the correct cursor and offset for loading a type.
5131ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5132 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5133 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5134 ModuleFile *M = I->second;
5135 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5136}
5137
5138/// \brief Read and return the type with the given index..
5139///
5140/// The index is the type ID, shifted and minus the number of predefs. This
5141/// routine actually reads the record corresponding to the type at the given
5142/// location. It is a helper routine for GetType, which deals with reading type
5143/// IDs.
5144QualType ASTReader::readTypeRecord(unsigned Index) {
5145 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005146 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005147
5148 // Keep track of where we are in the stream, then jump back there
5149 // after reading this type.
5150 SavedStreamPosition SavedPosition(DeclsCursor);
5151
5152 ReadingKindTracker ReadingKind(Read_Type, *this);
5153
5154 // Note that we are loading a type record.
5155 Deserializing AType(this);
5156
5157 unsigned Idx = 0;
5158 DeclsCursor.JumpToBit(Loc.Offset);
5159 RecordData Record;
5160 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005161 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005162 case TYPE_EXT_QUAL: {
5163 if (Record.size() != 2) {
5164 Error("Incorrect encoding of extended qualifier type");
5165 return QualType();
5166 }
5167 QualType Base = readType(*Loc.F, Record, Idx);
5168 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5169 return Context.getQualifiedType(Base, Quals);
5170 }
5171
5172 case TYPE_COMPLEX: {
5173 if (Record.size() != 1) {
5174 Error("Incorrect encoding of complex type");
5175 return QualType();
5176 }
5177 QualType ElemType = readType(*Loc.F, Record, Idx);
5178 return Context.getComplexType(ElemType);
5179 }
5180
5181 case TYPE_POINTER: {
5182 if (Record.size() != 1) {
5183 Error("Incorrect encoding of pointer type");
5184 return QualType();
5185 }
5186 QualType PointeeType = readType(*Loc.F, Record, Idx);
5187 return Context.getPointerType(PointeeType);
5188 }
5189
Reid Kleckner8a365022013-06-24 17:51:48 +00005190 case TYPE_DECAYED: {
5191 if (Record.size() != 1) {
5192 Error("Incorrect encoding of decayed type");
5193 return QualType();
5194 }
5195 QualType OriginalType = readType(*Loc.F, Record, Idx);
5196 QualType DT = Context.getAdjustedParameterType(OriginalType);
5197 if (!isa<DecayedType>(DT))
5198 Error("Decayed type does not decay");
5199 return DT;
5200 }
5201
Reid Kleckner0503a872013-12-05 01:23:43 +00005202 case TYPE_ADJUSTED: {
5203 if (Record.size() != 2) {
5204 Error("Incorrect encoding of adjusted type");
5205 return QualType();
5206 }
5207 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5208 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5209 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5210 }
5211
Guy Benyei11169dd2012-12-18 14:30:41 +00005212 case TYPE_BLOCK_POINTER: {
5213 if (Record.size() != 1) {
5214 Error("Incorrect encoding of block pointer type");
5215 return QualType();
5216 }
5217 QualType PointeeType = readType(*Loc.F, Record, Idx);
5218 return Context.getBlockPointerType(PointeeType);
5219 }
5220
5221 case TYPE_LVALUE_REFERENCE: {
5222 if (Record.size() != 2) {
5223 Error("Incorrect encoding of lvalue reference type");
5224 return QualType();
5225 }
5226 QualType PointeeType = readType(*Loc.F, Record, Idx);
5227 return Context.getLValueReferenceType(PointeeType, Record[1]);
5228 }
5229
5230 case TYPE_RVALUE_REFERENCE: {
5231 if (Record.size() != 1) {
5232 Error("Incorrect encoding of rvalue reference type");
5233 return QualType();
5234 }
5235 QualType PointeeType = readType(*Loc.F, Record, Idx);
5236 return Context.getRValueReferenceType(PointeeType);
5237 }
5238
5239 case TYPE_MEMBER_POINTER: {
5240 if (Record.size() != 2) {
5241 Error("Incorrect encoding of member pointer type");
5242 return QualType();
5243 }
5244 QualType PointeeType = readType(*Loc.F, Record, Idx);
5245 QualType ClassType = readType(*Loc.F, Record, Idx);
5246 if (PointeeType.isNull() || ClassType.isNull())
5247 return QualType();
5248
5249 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5250 }
5251
5252 case TYPE_CONSTANT_ARRAY: {
5253 QualType ElementType = readType(*Loc.F, Record, Idx);
5254 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5255 unsigned IndexTypeQuals = Record[2];
5256 unsigned Idx = 3;
5257 llvm::APInt Size = ReadAPInt(Record, Idx);
5258 return Context.getConstantArrayType(ElementType, Size,
5259 ASM, IndexTypeQuals);
5260 }
5261
5262 case TYPE_INCOMPLETE_ARRAY: {
5263 QualType ElementType = readType(*Loc.F, Record, Idx);
5264 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5265 unsigned IndexTypeQuals = Record[2];
5266 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5267 }
5268
5269 case TYPE_VARIABLE_ARRAY: {
5270 QualType ElementType = readType(*Loc.F, Record, Idx);
5271 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5272 unsigned IndexTypeQuals = Record[2];
5273 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5274 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5275 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5276 ASM, IndexTypeQuals,
5277 SourceRange(LBLoc, RBLoc));
5278 }
5279
5280 case TYPE_VECTOR: {
5281 if (Record.size() != 3) {
5282 Error("incorrect encoding of vector type in AST file");
5283 return QualType();
5284 }
5285
5286 QualType ElementType = readType(*Loc.F, Record, Idx);
5287 unsigned NumElements = Record[1];
5288 unsigned VecKind = Record[2];
5289 return Context.getVectorType(ElementType, NumElements,
5290 (VectorType::VectorKind)VecKind);
5291 }
5292
5293 case TYPE_EXT_VECTOR: {
5294 if (Record.size() != 3) {
5295 Error("incorrect encoding of extended vector type in AST file");
5296 return QualType();
5297 }
5298
5299 QualType ElementType = readType(*Loc.F, Record, Idx);
5300 unsigned NumElements = Record[1];
5301 return Context.getExtVectorType(ElementType, NumElements);
5302 }
5303
5304 case TYPE_FUNCTION_NO_PROTO: {
5305 if (Record.size() != 6) {
5306 Error("incorrect encoding of no-proto function type");
5307 return QualType();
5308 }
5309 QualType ResultType = readType(*Loc.F, Record, Idx);
5310 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5311 (CallingConv)Record[4], Record[5]);
5312 return Context.getFunctionNoProtoType(ResultType, Info);
5313 }
5314
5315 case TYPE_FUNCTION_PROTO: {
5316 QualType ResultType = readType(*Loc.F, Record, Idx);
5317
5318 FunctionProtoType::ExtProtoInfo EPI;
5319 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5320 /*hasregparm*/ Record[2],
5321 /*regparm*/ Record[3],
5322 static_cast<CallingConv>(Record[4]),
5323 /*produces*/ Record[5]);
5324
5325 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005326
5327 EPI.Variadic = Record[Idx++];
5328 EPI.HasTrailingReturn = Record[Idx++];
5329 EPI.TypeQuals = Record[Idx++];
5330 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005331 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005332 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005333
5334 unsigned NumParams = Record[Idx++];
5335 SmallVector<QualType, 16> ParamTypes;
5336 for (unsigned I = 0; I != NumParams; ++I)
5337 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5338
Jordan Rose5c382722013-03-08 21:51:21 +00005339 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005340 }
5341
5342 case TYPE_UNRESOLVED_USING: {
5343 unsigned Idx = 0;
5344 return Context.getTypeDeclType(
5345 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5346 }
5347
5348 case TYPE_TYPEDEF: {
5349 if (Record.size() != 2) {
5350 Error("incorrect encoding of typedef type");
5351 return QualType();
5352 }
5353 unsigned Idx = 0;
5354 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5355 QualType Canonical = readType(*Loc.F, Record, Idx);
5356 if (!Canonical.isNull())
5357 Canonical = Context.getCanonicalType(Canonical);
5358 return Context.getTypedefType(Decl, Canonical);
5359 }
5360
5361 case TYPE_TYPEOF_EXPR:
5362 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5363
5364 case TYPE_TYPEOF: {
5365 if (Record.size() != 1) {
5366 Error("incorrect encoding of typeof(type) in AST file");
5367 return QualType();
5368 }
5369 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5370 return Context.getTypeOfType(UnderlyingType);
5371 }
5372
5373 case TYPE_DECLTYPE: {
5374 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5375 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5376 }
5377
5378 case TYPE_UNARY_TRANSFORM: {
5379 QualType BaseType = readType(*Loc.F, Record, Idx);
5380 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5381 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5382 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5383 }
5384
Richard Smith74aeef52013-04-26 16:15:35 +00005385 case TYPE_AUTO: {
5386 QualType Deduced = readType(*Loc.F, Record, Idx);
5387 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005388 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005389 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005390 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005391
5392 case TYPE_RECORD: {
5393 if (Record.size() != 2) {
5394 Error("incorrect encoding of record type");
5395 return QualType();
5396 }
5397 unsigned Idx = 0;
5398 bool IsDependent = Record[Idx++];
5399 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5400 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5401 QualType T = Context.getRecordType(RD);
5402 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5403 return T;
5404 }
5405
5406 case TYPE_ENUM: {
5407 if (Record.size() != 2) {
5408 Error("incorrect encoding of enum type");
5409 return QualType();
5410 }
5411 unsigned Idx = 0;
5412 bool IsDependent = Record[Idx++];
5413 QualType T
5414 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5415 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5416 return T;
5417 }
5418
5419 case TYPE_ATTRIBUTED: {
5420 if (Record.size() != 3) {
5421 Error("incorrect encoding of attributed type");
5422 return QualType();
5423 }
5424 QualType modifiedType = readType(*Loc.F, Record, Idx);
5425 QualType equivalentType = readType(*Loc.F, Record, Idx);
5426 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5427 return Context.getAttributedType(kind, modifiedType, equivalentType);
5428 }
5429
5430 case TYPE_PAREN: {
5431 if (Record.size() != 1) {
5432 Error("incorrect encoding of paren type");
5433 return QualType();
5434 }
5435 QualType InnerType = readType(*Loc.F, Record, Idx);
5436 return Context.getParenType(InnerType);
5437 }
5438
5439 case TYPE_PACK_EXPANSION: {
5440 if (Record.size() != 2) {
5441 Error("incorrect encoding of pack expansion type");
5442 return QualType();
5443 }
5444 QualType Pattern = readType(*Loc.F, Record, Idx);
5445 if (Pattern.isNull())
5446 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005447 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005448 if (Record[1])
5449 NumExpansions = Record[1] - 1;
5450 return Context.getPackExpansionType(Pattern, NumExpansions);
5451 }
5452
5453 case TYPE_ELABORATED: {
5454 unsigned Idx = 0;
5455 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5456 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5457 QualType NamedType = readType(*Loc.F, Record, Idx);
5458 return Context.getElaboratedType(Keyword, NNS, NamedType);
5459 }
5460
5461 case TYPE_OBJC_INTERFACE: {
5462 unsigned Idx = 0;
5463 ObjCInterfaceDecl *ItfD
5464 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5465 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5466 }
5467
5468 case TYPE_OBJC_OBJECT: {
5469 unsigned Idx = 0;
5470 QualType Base = readType(*Loc.F, Record, Idx);
5471 unsigned NumProtos = Record[Idx++];
5472 SmallVector<ObjCProtocolDecl*, 4> Protos;
5473 for (unsigned I = 0; I != NumProtos; ++I)
5474 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5475 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5476 }
5477
5478 case TYPE_OBJC_OBJECT_POINTER: {
5479 unsigned Idx = 0;
5480 QualType Pointee = readType(*Loc.F, Record, Idx);
5481 return Context.getObjCObjectPointerType(Pointee);
5482 }
5483
5484 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5485 unsigned Idx = 0;
5486 QualType Parm = readType(*Loc.F, Record, Idx);
5487 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005488 return Context.getSubstTemplateTypeParmType(
5489 cast<TemplateTypeParmType>(Parm),
5490 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005491 }
5492
5493 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5494 unsigned Idx = 0;
5495 QualType Parm = readType(*Loc.F, Record, Idx);
5496 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5497 return Context.getSubstTemplateTypeParmPackType(
5498 cast<TemplateTypeParmType>(Parm),
5499 ArgPack);
5500 }
5501
5502 case TYPE_INJECTED_CLASS_NAME: {
5503 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5504 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5505 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5506 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005507 const Type *T = nullptr;
5508 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5509 if (const Type *Existing = DI->getTypeForDecl()) {
5510 T = Existing;
5511 break;
5512 }
5513 }
5514 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005515 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005516 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5517 DI->setTypeForDecl(T);
5518 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005519 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005520 }
5521
5522 case TYPE_TEMPLATE_TYPE_PARM: {
5523 unsigned Idx = 0;
5524 unsigned Depth = Record[Idx++];
5525 unsigned Index = Record[Idx++];
5526 bool Pack = Record[Idx++];
5527 TemplateTypeParmDecl *D
5528 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5529 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5530 }
5531
5532 case TYPE_DEPENDENT_NAME: {
5533 unsigned Idx = 0;
5534 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5535 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5536 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5537 QualType Canon = readType(*Loc.F, Record, Idx);
5538 if (!Canon.isNull())
5539 Canon = Context.getCanonicalType(Canon);
5540 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5541 }
5542
5543 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5544 unsigned Idx = 0;
5545 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5546 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5547 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5548 unsigned NumArgs = Record[Idx++];
5549 SmallVector<TemplateArgument, 8> Args;
5550 Args.reserve(NumArgs);
5551 while (NumArgs--)
5552 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5553 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5554 Args.size(), Args.data());
5555 }
5556
5557 case TYPE_DEPENDENT_SIZED_ARRAY: {
5558 unsigned Idx = 0;
5559
5560 // ArrayType
5561 QualType ElementType = readType(*Loc.F, Record, Idx);
5562 ArrayType::ArraySizeModifier ASM
5563 = (ArrayType::ArraySizeModifier)Record[Idx++];
5564 unsigned IndexTypeQuals = Record[Idx++];
5565
5566 // DependentSizedArrayType
5567 Expr *NumElts = ReadExpr(*Loc.F);
5568 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5569
5570 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5571 IndexTypeQuals, Brackets);
5572 }
5573
5574 case TYPE_TEMPLATE_SPECIALIZATION: {
5575 unsigned Idx = 0;
5576 bool IsDependent = Record[Idx++];
5577 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5578 SmallVector<TemplateArgument, 8> Args;
5579 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5580 QualType Underlying = readType(*Loc.F, Record, Idx);
5581 QualType T;
5582 if (Underlying.isNull())
5583 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5584 Args.size());
5585 else
5586 T = Context.getTemplateSpecializationType(Name, Args.data(),
5587 Args.size(), Underlying);
5588 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5589 return T;
5590 }
5591
5592 case TYPE_ATOMIC: {
5593 if (Record.size() != 1) {
5594 Error("Incorrect encoding of atomic type");
5595 return QualType();
5596 }
5597 QualType ValueType = readType(*Loc.F, Record, Idx);
5598 return Context.getAtomicType(ValueType);
5599 }
5600 }
5601 llvm_unreachable("Invalid TypeCode!");
5602}
5603
Richard Smith564417a2014-03-20 21:47:22 +00005604void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5605 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005606 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005607 const RecordData &Record, unsigned &Idx) {
5608 ExceptionSpecificationType EST =
5609 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005610 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005611 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005612 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005613 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005614 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005615 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005616 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005617 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005618 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5619 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005620 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005621 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005622 }
5623}
5624
Guy Benyei11169dd2012-12-18 14:30:41 +00005625class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5626 ASTReader &Reader;
5627 ModuleFile &F;
5628 const ASTReader::RecordData &Record;
5629 unsigned &Idx;
5630
5631 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5632 unsigned &I) {
5633 return Reader.ReadSourceLocation(F, R, I);
5634 }
5635
5636 template<typename T>
5637 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5638 return Reader.ReadDeclAs<T>(F, Record, Idx);
5639 }
5640
5641public:
5642 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5643 const ASTReader::RecordData &Record, unsigned &Idx)
5644 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5645 { }
5646
5647 // We want compile-time assurance that we've enumerated all of
5648 // these, so unfortunately we have to declare them first, then
5649 // define them out-of-line.
5650#define ABSTRACT_TYPELOC(CLASS, PARENT)
5651#define TYPELOC(CLASS, PARENT) \
5652 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5653#include "clang/AST/TypeLocNodes.def"
5654
5655 void VisitFunctionTypeLoc(FunctionTypeLoc);
5656 void VisitArrayTypeLoc(ArrayTypeLoc);
5657};
5658
5659void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5660 // nothing to do
5661}
5662void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5663 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5664 if (TL.needsExtraLocalData()) {
5665 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5666 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5667 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5668 TL.setModeAttr(Record[Idx++]);
5669 }
5670}
5671void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5672 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5673}
5674void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5675 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5676}
Reid Kleckner8a365022013-06-24 17:51:48 +00005677void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5678 // nothing to do
5679}
Reid Kleckner0503a872013-12-05 01:23:43 +00005680void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5681 // nothing to do
5682}
Guy Benyei11169dd2012-12-18 14:30:41 +00005683void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5684 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5685}
5686void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5687 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5688}
5689void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5690 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5691}
5692void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5693 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5694 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5695}
5696void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5697 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5698 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5699 if (Record[Idx++])
5700 TL.setSizeExpr(Reader.ReadExpr(F));
5701 else
Craig Toppera13603a2014-05-22 05:54:18 +00005702 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005703}
5704void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5705 VisitArrayTypeLoc(TL);
5706}
5707void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5708 VisitArrayTypeLoc(TL);
5709}
5710void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5711 VisitArrayTypeLoc(TL);
5712}
5713void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5714 DependentSizedArrayTypeLoc TL) {
5715 VisitArrayTypeLoc(TL);
5716}
5717void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5718 DependentSizedExtVectorTypeLoc TL) {
5719 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5720}
5721void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5722 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5723}
5724void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5725 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5726}
5727void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5728 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5729 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5730 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5731 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005732 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5733 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005734 }
5735}
5736void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5737 VisitFunctionTypeLoc(TL);
5738}
5739void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5740 VisitFunctionTypeLoc(TL);
5741}
5742void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5743 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5744}
5745void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5746 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5747}
5748void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5749 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5750 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5751 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5752}
5753void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5754 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5755 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5756 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5757 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5758}
5759void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5760 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5761}
5762void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5763 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5764 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5765 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5766 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5767}
5768void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5769 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5770}
5771void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5772 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5773}
5774void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5775 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5776}
5777void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5778 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5779 if (TL.hasAttrOperand()) {
5780 SourceRange range;
5781 range.setBegin(ReadSourceLocation(Record, Idx));
5782 range.setEnd(ReadSourceLocation(Record, Idx));
5783 TL.setAttrOperandParensRange(range);
5784 }
5785 if (TL.hasAttrExprOperand()) {
5786 if (Record[Idx++])
5787 TL.setAttrExprOperand(Reader.ReadExpr(F));
5788 else
Craig Toppera13603a2014-05-22 05:54:18 +00005789 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005790 } else if (TL.hasAttrEnumOperand())
5791 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5792}
5793void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5794 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5795}
5796void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5797 SubstTemplateTypeParmTypeLoc TL) {
5798 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5799}
5800void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5801 SubstTemplateTypeParmPackTypeLoc TL) {
5802 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5803}
5804void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5805 TemplateSpecializationTypeLoc TL) {
5806 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5807 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5808 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5809 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5810 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5811 TL.setArgLocInfo(i,
5812 Reader.GetTemplateArgumentLocInfo(F,
5813 TL.getTypePtr()->getArg(i).getKind(),
5814 Record, Idx));
5815}
5816void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5817 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5818 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5819}
5820void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5821 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5822 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5823}
5824void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5825 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5826}
5827void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5828 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5829 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5830 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5831}
5832void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5833 DependentTemplateSpecializationTypeLoc TL) {
5834 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5835 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5836 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5837 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5838 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5839 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5840 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5841 TL.setArgLocInfo(I,
5842 Reader.GetTemplateArgumentLocInfo(F,
5843 TL.getTypePtr()->getArg(I).getKind(),
5844 Record, Idx));
5845}
5846void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5847 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5848}
5849void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5850 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5851}
5852void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5853 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5854 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5855 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5856 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5857 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5858}
5859void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5860 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5861}
5862void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5863 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5864 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5865 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5866}
5867
5868TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5869 const RecordData &Record,
5870 unsigned &Idx) {
5871 QualType InfoTy = readType(F, Record, Idx);
5872 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005873 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005874
5875 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5876 TypeLocReader TLR(*this, F, Record, Idx);
5877 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5878 TLR.Visit(TL);
5879 return TInfo;
5880}
5881
5882QualType ASTReader::GetType(TypeID ID) {
5883 unsigned FastQuals = ID & Qualifiers::FastMask;
5884 unsigned Index = ID >> Qualifiers::FastWidth;
5885
5886 if (Index < NUM_PREDEF_TYPE_IDS) {
5887 QualType T;
5888 switch ((PredefinedTypeIDs)Index) {
5889 case PREDEF_TYPE_NULL_ID: return QualType();
5890 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5891 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5892
5893 case PREDEF_TYPE_CHAR_U_ID:
5894 case PREDEF_TYPE_CHAR_S_ID:
5895 // FIXME: Check that the signedness of CharTy is correct!
5896 T = Context.CharTy;
5897 break;
5898
5899 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5900 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5901 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5902 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5903 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5904 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5905 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5906 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5907 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5908 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5909 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5910 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5911 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5912 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5913 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5914 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5915 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5916 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5917 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5918 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5919 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5920 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5921 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5922 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5923 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5924 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5925 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5926 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005927 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5928 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5929 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5930 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5931 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5932 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005933 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005934 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005935 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5936
5937 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5938 T = Context.getAutoRRefDeductType();
5939 break;
5940
5941 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5942 T = Context.ARCUnbridgedCastTy;
5943 break;
5944
5945 case PREDEF_TYPE_VA_LIST_TAG:
5946 T = Context.getVaListTagType();
5947 break;
5948
5949 case PREDEF_TYPE_BUILTIN_FN:
5950 T = Context.BuiltinFnTy;
5951 break;
5952 }
5953
5954 assert(!T.isNull() && "Unknown predefined type");
5955 return T.withFastQualifiers(FastQuals);
5956 }
5957
5958 Index -= NUM_PREDEF_TYPE_IDS;
5959 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5960 if (TypesLoaded[Index].isNull()) {
5961 TypesLoaded[Index] = readTypeRecord(Index);
5962 if (TypesLoaded[Index].isNull())
5963 return QualType();
5964
5965 TypesLoaded[Index]->setFromAST();
5966 if (DeserializationListener)
5967 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5968 TypesLoaded[Index]);
5969 }
5970
5971 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5972}
5973
5974QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5975 return GetType(getGlobalTypeID(F, LocalID));
5976}
5977
5978serialization::TypeID
5979ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5980 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5981 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5982
5983 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5984 return LocalID;
5985
5986 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5987 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5988 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5989
5990 unsigned GlobalIndex = LocalIndex + I->second;
5991 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5992}
5993
5994TemplateArgumentLocInfo
5995ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5996 TemplateArgument::ArgKind Kind,
5997 const RecordData &Record,
5998 unsigned &Index) {
5999 switch (Kind) {
6000 case TemplateArgument::Expression:
6001 return ReadExpr(F);
6002 case TemplateArgument::Type:
6003 return GetTypeSourceInfo(F, Record, Index);
6004 case TemplateArgument::Template: {
6005 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6006 Index);
6007 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6008 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6009 SourceLocation());
6010 }
6011 case TemplateArgument::TemplateExpansion: {
6012 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6013 Index);
6014 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6015 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6016 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6017 EllipsisLoc);
6018 }
6019 case TemplateArgument::Null:
6020 case TemplateArgument::Integral:
6021 case TemplateArgument::Declaration:
6022 case TemplateArgument::NullPtr:
6023 case TemplateArgument::Pack:
6024 // FIXME: Is this right?
6025 return TemplateArgumentLocInfo();
6026 }
6027 llvm_unreachable("unexpected template argument loc");
6028}
6029
6030TemplateArgumentLoc
6031ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6032 const RecordData &Record, unsigned &Index) {
6033 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6034
6035 if (Arg.getKind() == TemplateArgument::Expression) {
6036 if (Record[Index++]) // bool InfoHasSameExpr.
6037 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6038 }
6039 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6040 Record, Index));
6041}
6042
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006043const ASTTemplateArgumentListInfo*
6044ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6045 const RecordData &Record,
6046 unsigned &Index) {
6047 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6048 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6049 unsigned NumArgsAsWritten = Record[Index++];
6050 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6051 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6052 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6053 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6054}
6055
Guy Benyei11169dd2012-12-18 14:30:41 +00006056Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6057 return GetDecl(ID);
6058}
6059
Richard Smith053f6c62014-05-16 23:01:30 +00006060void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00006061 if (NumCurrentElementsDeserializing) {
6062 // We arrange to not care about the complete redeclaration chain while we're
6063 // deserializing. Just remember that the AST has marked this one as complete
6064 // but that it's not actually complete yet, so we know we still need to
6065 // complete it later.
6066 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6067 return;
6068 }
6069
Richard Smith053f6c62014-05-16 23:01:30 +00006070 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6071
Richard Smith053f6c62014-05-16 23:01:30 +00006072 // If this is a named declaration, complete it by looking it up
6073 // within its context.
6074 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00006075 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00006076 // all mergeable entities within it.
6077 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6078 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6079 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6080 auto *II = Name.getAsIdentifierInfo();
6081 if (isa<TranslationUnitDecl>(DC) && II) {
6082 // Outside of C++, we don't have a lookup table for the TU, so update
6083 // the identifier instead. In C++, either way should work fine.
6084 if (II->isOutOfDate())
6085 updateOutOfDateIdentifier(*II);
6086 } else
6087 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00006088 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
6089 // FIXME: It'd be nice to do something a bit more targeted here.
6090 D->getDeclContext()->decls_begin();
Richard Smith053f6c62014-05-16 23:01:30 +00006091 }
6092 }
6093}
6094
Richard Smithcd45dbc2014-04-19 03:48:30 +00006095uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6096 const RecordData &Record,
6097 unsigned &Idx) {
6098 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6099 Error("malformed AST file: missing C++ base specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00006100 return 0;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006101 }
6102
Guy Benyei11169dd2012-12-18 14:30:41 +00006103 unsigned LocalID = Record[Idx++];
6104 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6105}
6106
6107CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6108 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006109 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006110 SavedStreamPosition SavedPosition(Cursor);
6111 Cursor.JumpToBit(Loc.Offset);
6112 ReadingKindTracker ReadingKind(Read_Decl, *this);
6113 RecordData Record;
6114 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006115 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006116 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006117 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006118 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006119 }
6120
6121 unsigned Idx = 0;
6122 unsigned NumBases = Record[Idx++];
6123 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6124 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6125 for (unsigned I = 0; I != NumBases; ++I)
6126 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6127 return Bases;
6128}
6129
6130serialization::DeclID
6131ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6132 if (LocalID < NUM_PREDEF_DECL_IDS)
6133 return LocalID;
6134
6135 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6136 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6137 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6138
6139 return LocalID + I->second;
6140}
6141
6142bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6143 ModuleFile &M) const {
6144 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6145 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6146 return &M == I->second;
6147}
6148
Douglas Gregor9f782892013-01-21 15:25:38 +00006149ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006150 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006151 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006152 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6153 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6154 return I->second;
6155}
6156
6157SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6158 if (ID < NUM_PREDEF_DECL_IDS)
6159 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006160
Guy Benyei11169dd2012-12-18 14:30:41 +00006161 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6162
6163 if (Index > DeclsLoaded.size()) {
6164 Error("declaration ID out-of-range for AST file");
6165 return SourceLocation();
6166 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006167
Guy Benyei11169dd2012-12-18 14:30:41 +00006168 if (Decl *D = DeclsLoaded[Index])
6169 return D->getLocation();
6170
6171 unsigned RawLocation = 0;
6172 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6173 return ReadSourceLocation(*Rec.F, RawLocation);
6174}
6175
Richard Smithcd45dbc2014-04-19 03:48:30 +00006176Decl *ASTReader::GetExistingDecl(DeclID ID) {
6177 if (ID < NUM_PREDEF_DECL_IDS) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006178 switch ((PredefinedDeclIDs)ID) {
6179 case PREDEF_DECL_NULL_ID:
Craig Toppera13603a2014-05-22 05:54:18 +00006180 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006181
Guy Benyei11169dd2012-12-18 14:30:41 +00006182 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6183 return Context.getTranslationUnitDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006184
Guy Benyei11169dd2012-12-18 14:30:41 +00006185 case PREDEF_DECL_OBJC_ID_ID:
6186 return Context.getObjCIdDecl();
6187
6188 case PREDEF_DECL_OBJC_SEL_ID:
6189 return Context.getObjCSelDecl();
6190
6191 case PREDEF_DECL_OBJC_CLASS_ID:
6192 return Context.getObjCClassDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006193
Guy Benyei11169dd2012-12-18 14:30:41 +00006194 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6195 return Context.getObjCProtocolDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006196
Guy Benyei11169dd2012-12-18 14:30:41 +00006197 case PREDEF_DECL_INT_128_ID:
6198 return Context.getInt128Decl();
6199
6200 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6201 return Context.getUInt128Decl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006202
Guy Benyei11169dd2012-12-18 14:30:41 +00006203 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6204 return Context.getObjCInstanceTypeDecl();
6205
6206 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6207 return Context.getBuiltinVaListDecl();
6208 }
6209 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006210
Guy Benyei11169dd2012-12-18 14:30:41 +00006211 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6212
6213 if (Index >= DeclsLoaded.size()) {
6214 assert(0 && "declaration ID out-of-range for AST file");
6215 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006216 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006217 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006218
6219 return DeclsLoaded[Index];
6220}
6221
6222Decl *ASTReader::GetDecl(DeclID ID) {
6223 if (ID < NUM_PREDEF_DECL_IDS)
6224 return GetExistingDecl(ID);
6225
6226 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6227
6228 if (Index >= DeclsLoaded.size()) {
6229 assert(0 && "declaration ID out-of-range for AST file");
6230 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006231 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006232 }
6233
Guy Benyei11169dd2012-12-18 14:30:41 +00006234 if (!DeclsLoaded[Index]) {
6235 ReadDeclRecord(ID);
6236 if (DeserializationListener)
6237 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6238 }
6239
6240 return DeclsLoaded[Index];
6241}
6242
6243DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6244 DeclID GlobalID) {
6245 if (GlobalID < NUM_PREDEF_DECL_IDS)
6246 return GlobalID;
6247
6248 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6249 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6250 ModuleFile *Owner = I->second;
6251
6252 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6253 = M.GlobalToLocalDeclIDs.find(Owner);
6254 if (Pos == M.GlobalToLocalDeclIDs.end())
6255 return 0;
6256
6257 return GlobalID - Owner->BaseDeclID + Pos->second;
6258}
6259
6260serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6261 const RecordData &Record,
6262 unsigned &Idx) {
6263 if (Idx >= Record.size()) {
6264 Error("Corrupted AST file");
6265 return 0;
6266 }
6267
6268 return getGlobalDeclID(F, Record[Idx++]);
6269}
6270
6271/// \brief Resolve the offset of a statement into a statement.
6272///
6273/// This operation will read a new statement from the external
6274/// source each time it is called, and is meant to be used via a
6275/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6276Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6277 // Switch case IDs are per Decl.
6278 ClearSwitchCaseIDs();
6279
6280 // Offset here is a global offset across the entire chain.
6281 RecordLocation Loc = getLocalBitOffset(Offset);
6282 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6283 return ReadStmtFromStream(*Loc.F);
6284}
6285
6286namespace {
6287 class FindExternalLexicalDeclsVisitor {
6288 ASTReader &Reader;
6289 const DeclContext *DC;
6290 bool (*isKindWeWant)(Decl::Kind);
6291
6292 SmallVectorImpl<Decl*> &Decls;
6293 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6294
6295 public:
6296 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6297 bool (*isKindWeWant)(Decl::Kind),
6298 SmallVectorImpl<Decl*> &Decls)
6299 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6300 {
6301 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6302 PredefsVisited[I] = false;
6303 }
6304
6305 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6306 if (Preorder)
6307 return false;
6308
6309 FindExternalLexicalDeclsVisitor *This
6310 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6311
6312 ModuleFile::DeclContextInfosMap::iterator Info
6313 = M.DeclContextInfos.find(This->DC);
6314 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6315 return false;
6316
6317 // Load all of the declaration IDs
6318 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6319 *IDE = ID + Info->second.NumLexicalDecls;
6320 ID != IDE; ++ID) {
6321 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6322 continue;
6323
6324 // Don't add predefined declarations to the lexical context more
6325 // than once.
6326 if (ID->second < NUM_PREDEF_DECL_IDS) {
6327 if (This->PredefsVisited[ID->second])
6328 continue;
6329
6330 This->PredefsVisited[ID->second] = true;
6331 }
6332
6333 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6334 if (!This->DC->isDeclInLexicalTraversal(D))
6335 This->Decls.push_back(D);
6336 }
6337 }
6338
6339 return false;
6340 }
6341 };
6342}
6343
6344ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6345 bool (*isKindWeWant)(Decl::Kind),
6346 SmallVectorImpl<Decl*> &Decls) {
6347 // There might be lexical decls in multiple modules, for the TU at
6348 // least. Walk all of the modules in the order they were loaded.
6349 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6350 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6351 ++NumLexicalDeclContextsRead;
6352 return ELR_Success;
6353}
6354
6355namespace {
6356
6357class DeclIDComp {
6358 ASTReader &Reader;
6359 ModuleFile &Mod;
6360
6361public:
6362 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6363
6364 bool operator()(LocalDeclID L, LocalDeclID R) const {
6365 SourceLocation LHS = getLocation(L);
6366 SourceLocation RHS = getLocation(R);
6367 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6368 }
6369
6370 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6371 SourceLocation RHS = getLocation(R);
6372 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6373 }
6374
6375 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6376 SourceLocation LHS = getLocation(L);
6377 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6378 }
6379
6380 SourceLocation getLocation(LocalDeclID ID) const {
6381 return Reader.getSourceManager().getFileLoc(
6382 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6383 }
6384};
6385
6386}
6387
6388void ASTReader::FindFileRegionDecls(FileID File,
6389 unsigned Offset, unsigned Length,
6390 SmallVectorImpl<Decl *> &Decls) {
6391 SourceManager &SM = getSourceManager();
6392
6393 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6394 if (I == FileDeclIDs.end())
6395 return;
6396
6397 FileDeclsInfo &DInfo = I->second;
6398 if (DInfo.Decls.empty())
6399 return;
6400
6401 SourceLocation
6402 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6403 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6404
6405 DeclIDComp DIDComp(*this, *DInfo.Mod);
6406 ArrayRef<serialization::LocalDeclID>::iterator
6407 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6408 BeginLoc, DIDComp);
6409 if (BeginIt != DInfo.Decls.begin())
6410 --BeginIt;
6411
6412 // If we are pointing at a top-level decl inside an objc container, we need
6413 // to backtrack until we find it otherwise we will fail to report that the
6414 // region overlaps with an objc container.
6415 while (BeginIt != DInfo.Decls.begin() &&
6416 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6417 ->isTopLevelDeclInObjCContainer())
6418 --BeginIt;
6419
6420 ArrayRef<serialization::LocalDeclID>::iterator
6421 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6422 EndLoc, DIDComp);
6423 if (EndIt != DInfo.Decls.end())
6424 ++EndIt;
6425
6426 for (ArrayRef<serialization::LocalDeclID>::iterator
6427 DIt = BeginIt; DIt != EndIt; ++DIt)
6428 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6429}
6430
6431namespace {
6432 /// \brief ModuleFile visitor used to perform name lookup into a
6433 /// declaration context.
6434 class DeclContextNameLookupVisitor {
6435 ASTReader &Reader;
Richard Smith8c913ec2014-08-14 02:21:01 +00006436 ArrayRef<const DeclContext *> Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006437 DeclarationName Name;
6438 SmallVectorImpl<NamedDecl *> &Decls;
6439
6440 public:
Richard Smith8c913ec2014-08-14 02:21:01 +00006441 DeclContextNameLookupVisitor(ASTReader &Reader,
6442 ArrayRef<const DeclContext *> Contexts,
Guy Benyei11169dd2012-12-18 14:30:41 +00006443 DeclarationName Name,
6444 SmallVectorImpl<NamedDecl *> &Decls)
6445 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6446
6447 static bool visit(ModuleFile &M, void *UserData) {
6448 DeclContextNameLookupVisitor *This
6449 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6450
6451 // Check whether we have any visible declaration information for
6452 // this context in this module.
6453 ModuleFile::DeclContextInfosMap::iterator Info;
6454 bool FoundInfo = false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006455 for (auto *DC : This->Contexts) {
6456 Info = M.DeclContextInfos.find(DC);
6457 if (Info != M.DeclContextInfos.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006458 Info->second.NameLookupTableData) {
6459 FoundInfo = true;
6460 break;
6461 }
6462 }
6463
6464 if (!FoundInfo)
6465 return false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006466
Guy Benyei11169dd2012-12-18 14:30:41 +00006467 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006468 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006469 Info->second.NameLookupTableData;
6470 ASTDeclContextNameLookupTable::iterator Pos
6471 = LookupTable->find(This->Name);
6472 if (Pos == LookupTable->end())
6473 return false;
6474
6475 bool FoundAnything = false;
6476 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6477 for (; Data.first != Data.second; ++Data.first) {
6478 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6479 if (!ND)
6480 continue;
6481
6482 if (ND->getDeclName() != This->Name) {
6483 // A name might be null because the decl's redeclarable part is
6484 // currently read before reading its name. The lookup is triggered by
6485 // building that decl (likely indirectly), and so it is later in the
6486 // sense of "already existing" and can be ignored here.
Richard Smith8c913ec2014-08-14 02:21:01 +00006487 // FIXME: This should not happen; deserializing declarations should
6488 // not perform lookups since that can lead to deserialization cycles.
Guy Benyei11169dd2012-12-18 14:30:41 +00006489 continue;
6490 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006491
Guy Benyei11169dd2012-12-18 14:30:41 +00006492 // Record this declaration.
6493 FoundAnything = true;
6494 This->Decls.push_back(ND);
6495 }
6496
6497 return FoundAnything;
6498 }
6499 };
6500}
6501
Douglas Gregor9f782892013-01-21 15:25:38 +00006502/// \brief Retrieve the "definitive" module file for the definition of the
6503/// given declaration context, if there is one.
6504///
6505/// The "definitive" module file is the only place where we need to look to
6506/// find information about the declarations within the given declaration
6507/// context. For example, C++ and Objective-C classes, C structs/unions, and
6508/// Objective-C protocols, categories, and extensions are all defined in a
6509/// single place in the source code, so they have definitive module files
6510/// associated with them. C++ namespaces, on the other hand, can have
6511/// definitions in multiple different module files.
6512///
6513/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6514/// NDEBUG checking.
6515static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6516 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006517 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6518 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006519
Craig Toppera13603a2014-05-22 05:54:18 +00006520 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +00006521}
6522
Richard Smith9ce12e32013-02-07 03:30:24 +00006523bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006524ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6525 DeclarationName Name) {
6526 assert(DC->hasExternalVisibleStorage() &&
6527 "DeclContext has no visible decls in storage");
6528 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006529 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006530
Richard Smith8c913ec2014-08-14 02:21:01 +00006531 Deserializing LookupResults(this);
6532
Guy Benyei11169dd2012-12-18 14:30:41 +00006533 SmallVector<NamedDecl *, 64> Decls;
Richard Smith8c913ec2014-08-14 02:21:01 +00006534
Guy Benyei11169dd2012-12-18 14:30:41 +00006535 // Compute the declaration contexts we need to look into. Multiple such
6536 // declaration contexts occur when two declaration contexts from disjoint
6537 // modules get merged, e.g., when two namespaces with the same name are
6538 // independently defined in separate modules.
6539 SmallVector<const DeclContext *, 2> Contexts;
6540 Contexts.push_back(DC);
Richard Smith8c913ec2014-08-14 02:21:01 +00006541
Guy Benyei11169dd2012-12-18 14:30:41 +00006542 if (DC->isNamespace()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006543 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei11169dd2012-12-18 14:30:41 +00006544 if (Merged != MergedDecls.end()) {
6545 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6546 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6547 }
6548 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006549
6550 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
6551 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
6552
6553 // If we can definitively determine which module file to look into,
6554 // only look there. Otherwise, look in all module files.
6555 ModuleFile *Definitive;
6556 if (Contexts.size() == 1 &&
6557 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6558 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6559 } else {
6560 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6561 }
6562 };
6563
6564 LookUpInContexts(Contexts);
6565
6566 // If this might be an implicit special member function, then also search
6567 // all merged definitions of the surrounding class. We need to search them
6568 // individually, because finding an entity in one of them doesn't imply that
6569 // we can't find a different entity in another one.
Richard Smithcd45dbc2014-04-19 03:48:30 +00006570 if (isa<CXXRecordDecl>(DC)) {
Richard Smith8c913ec2014-08-14 02:21:01 +00006571 auto Kind = Name.getNameKind();
6572 if (Kind == DeclarationName::CXXConstructorName ||
6573 Kind == DeclarationName::CXXDestructorName ||
6574 (Kind == DeclarationName::CXXOperatorName &&
6575 Name.getCXXOverloadedOperator() == OO_Equal)) {
6576 auto Merged = MergedLookups.find(DC);
Richard Smithe0612472014-11-21 05:16:13 +00006577 if (Merged != MergedLookups.end()) {
6578 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6579 LookUpInContexts(Merged->second[I]);
6580 // We might have just added some more merged lookups. If so, our
6581 // iterator is now invalid, so grab a fresh one before continuing.
6582 Merged = MergedLookups.find(DC);
6583 }
6584 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006585 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006586 }
6587
Guy Benyei11169dd2012-12-18 14:30:41 +00006588 ++NumVisibleDeclContextsRead;
6589 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006590 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006591}
6592
6593namespace {
6594 /// \brief ModuleFile visitor used to retrieve all visible names in a
6595 /// declaration context.
6596 class DeclContextAllNamesVisitor {
6597 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006598 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006599 DeclsMap &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006600 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006601
6602 public:
6603 DeclContextAllNamesVisitor(ASTReader &Reader,
6604 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006605 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006606 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006607
6608 static bool visit(ModuleFile &M, void *UserData) {
6609 DeclContextAllNamesVisitor *This
6610 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6611
6612 // Check whether we have any visible declaration information for
6613 // this context in this module.
6614 ModuleFile::DeclContextInfosMap::iterator Info;
6615 bool FoundInfo = false;
6616 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6617 Info = M.DeclContextInfos.find(This->Contexts[I]);
6618 if (Info != M.DeclContextInfos.end() &&
6619 Info->second.NameLookupTableData) {
6620 FoundInfo = true;
6621 break;
6622 }
6623 }
6624
6625 if (!FoundInfo)
6626 return false;
6627
Richard Smith52e3fba2014-03-11 07:17:35 +00006628 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006629 Info->second.NameLookupTableData;
6630 bool FoundAnything = false;
6631 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006632 I = LookupTable->data_begin(), E = LookupTable->data_end();
6633 I != E;
6634 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006635 ASTDeclContextNameLookupTrait::data_type Data = *I;
6636 for (; Data.first != Data.second; ++Data.first) {
6637 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6638 *Data.first);
6639 if (!ND)
6640 continue;
6641
6642 // Record this declaration.
6643 FoundAnything = true;
6644 This->Decls[ND->getDeclName()].push_back(ND);
6645 }
6646 }
6647
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006648 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006649 }
6650 };
6651}
6652
6653void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6654 if (!DC->hasExternalVisibleStorage())
6655 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006656 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006657
6658 // Compute the declaration contexts we need to look into. Multiple such
6659 // declaration contexts occur when two declaration contexts from disjoint
6660 // modules get merged, e.g., when two namespaces with the same name are
6661 // independently defined in separate modules.
6662 SmallVector<const DeclContext *, 2> Contexts;
6663 Contexts.push_back(DC);
6664
6665 if (DC->isNamespace()) {
6666 MergedDeclsMap::iterator Merged
6667 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6668 if (Merged != MergedDecls.end()) {
6669 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6670 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6671 }
6672 }
6673
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006674 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6675 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006676 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6677 ++NumVisibleDeclContextsRead;
6678
Craig Topper79be4cd2013-07-05 04:33:53 +00006679 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006680 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6681 }
6682 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6683}
6684
6685/// \brief Under non-PCH compilation the consumer receives the objc methods
6686/// before receiving the implementation, and codegen depends on this.
6687/// We simulate this by deserializing and passing to consumer the methods of the
6688/// implementation before passing the deserialized implementation decl.
6689static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6690 ASTConsumer *Consumer) {
6691 assert(ImplD && Consumer);
6692
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006693 for (auto *I : ImplD->methods())
6694 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006695
6696 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6697}
6698
6699void ASTReader::PassInterestingDeclsToConsumer() {
6700 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006701
6702 if (PassingDeclsToConsumer)
6703 return;
6704
6705 // Guard variable to avoid recursively redoing the process of passing
6706 // decls to consumer.
6707 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6708 true);
6709
Guy Benyei11169dd2012-12-18 14:30:41 +00006710 while (!InterestingDecls.empty()) {
6711 Decl *D = InterestingDecls.front();
6712 InterestingDecls.pop_front();
6713
6714 PassInterestingDeclToConsumer(D);
6715 }
6716}
6717
6718void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6719 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6720 PassObjCImplDeclToConsumer(ImplD, Consumer);
6721 else
6722 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6723}
6724
6725void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6726 this->Consumer = Consumer;
6727
6728 if (!Consumer)
6729 return;
6730
Ben Langmuir332aafe2014-01-31 01:06:56 +00006731 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006732 // Force deserialization of this decl, which will cause it to be queued for
6733 // passing to the consumer.
Ben Langmuir332aafe2014-01-31 01:06:56 +00006734 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006735 }
Ben Langmuir332aafe2014-01-31 01:06:56 +00006736 EagerlyDeserializedDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006737
6738 PassInterestingDeclsToConsumer();
6739}
6740
6741void ASTReader::PrintStats() {
6742 std::fprintf(stderr, "*** AST File Statistics:\n");
6743
6744 unsigned NumTypesLoaded
6745 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6746 QualType());
6747 unsigned NumDeclsLoaded
6748 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006749 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006750 unsigned NumIdentifiersLoaded
6751 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6752 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006753 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006754 unsigned NumMacrosLoaded
6755 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6756 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006757 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006758 unsigned NumSelectorsLoaded
6759 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6760 SelectorsLoaded.end(),
6761 Selector());
6762
6763 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6764 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6765 NumSLocEntriesRead, TotalNumSLocEntries,
6766 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6767 if (!TypesLoaded.empty())
6768 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6769 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6770 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6771 if (!DeclsLoaded.empty())
6772 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6773 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6774 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6775 if (!IdentifiersLoaded.empty())
6776 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6777 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6778 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6779 if (!MacrosLoaded.empty())
6780 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6781 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6782 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6783 if (!SelectorsLoaded.empty())
6784 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6785 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6786 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6787 if (TotalNumStatements)
6788 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6789 NumStatementsRead, TotalNumStatements,
6790 ((float)NumStatementsRead/TotalNumStatements * 100));
6791 if (TotalNumMacros)
6792 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6793 NumMacrosRead, TotalNumMacros,
6794 ((float)NumMacrosRead/TotalNumMacros * 100));
6795 if (TotalLexicalDeclContexts)
6796 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6797 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6798 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6799 * 100));
6800 if (TotalVisibleDeclContexts)
6801 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6802 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6803 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6804 * 100));
6805 if (TotalNumMethodPoolEntries) {
6806 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6807 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6808 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6809 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006810 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006811 if (NumMethodPoolLookups) {
6812 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6813 NumMethodPoolHits, NumMethodPoolLookups,
6814 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6815 }
6816 if (NumMethodPoolTableLookups) {
6817 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6818 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6819 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6820 * 100.0));
6821 }
6822
Douglas Gregor00a50f72013-01-25 00:38:33 +00006823 if (NumIdentifierLookupHits) {
6824 std::fprintf(stderr,
6825 " %u / %u identifier table lookups succeeded (%f%%)\n",
6826 NumIdentifierLookupHits, NumIdentifierLookups,
6827 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6828 }
6829
Douglas Gregore060e572013-01-25 01:03:03 +00006830 if (GlobalIndex) {
6831 std::fprintf(stderr, "\n");
6832 GlobalIndex->printStats();
6833 }
6834
Guy Benyei11169dd2012-12-18 14:30:41 +00006835 std::fprintf(stderr, "\n");
6836 dump();
6837 std::fprintf(stderr, "\n");
6838}
6839
6840template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6841static void
6842dumpModuleIDMap(StringRef Name,
6843 const ContinuousRangeMap<Key, ModuleFile *,
6844 InitialCapacity> &Map) {
6845 if (Map.begin() == Map.end())
6846 return;
6847
6848 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6849 llvm::errs() << Name << ":\n";
6850 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6851 I != IEnd; ++I) {
6852 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6853 << "\n";
6854 }
6855}
6856
6857void ASTReader::dump() {
6858 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6859 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6860 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6861 dumpModuleIDMap("Global type map", GlobalTypeMap);
6862 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6863 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6864 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6865 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6866 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6867 dumpModuleIDMap("Global preprocessed entity map",
6868 GlobalPreprocessedEntityMap);
6869
6870 llvm::errs() << "\n*** PCH/Modules Loaded:";
6871 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6872 MEnd = ModuleMgr.end();
6873 M != MEnd; ++M)
6874 (*M)->dump();
6875}
6876
6877/// Return the amount of memory used by memory buffers, breaking down
6878/// by heap-backed versus mmap'ed memory.
6879void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6880 for (ModuleConstIterator I = ModuleMgr.begin(),
6881 E = ModuleMgr.end(); I != E; ++I) {
6882 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6883 size_t bytes = buf->getBufferSize();
6884 switch (buf->getBufferKind()) {
6885 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6886 sizes.malloc_bytes += bytes;
6887 break;
6888 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6889 sizes.mmap_bytes += bytes;
6890 break;
6891 }
6892 }
6893 }
6894}
6895
6896void ASTReader::InitializeSema(Sema &S) {
6897 SemaObj = &S;
6898 S.addExternalSource(this);
6899
6900 // Makes sure any declarations that were deserialized "too early"
6901 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006902 for (uint64_t ID : PreloadedDeclIDs) {
6903 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6904 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006905 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006906 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006907
Richard Smith3d8e97e2013-10-18 06:54:39 +00006908 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006909 if (!FPPragmaOptions.empty()) {
6910 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6911 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6912 }
6913
Richard Smith3d8e97e2013-10-18 06:54:39 +00006914 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006915 if (!OpenCLExtensions.empty()) {
6916 unsigned I = 0;
6917#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6918#include "clang/Basic/OpenCLExtensions.def"
6919
6920 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6921 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006922
6923 UpdateSema();
6924}
6925
6926void ASTReader::UpdateSema() {
6927 assert(SemaObj && "no Sema to update");
6928
6929 // Load the offsets of the declarations that Sema references.
6930 // They will be lazily deserialized when needed.
6931 if (!SemaDeclRefs.empty()) {
6932 assert(SemaDeclRefs.size() % 2 == 0);
6933 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6934 if (!SemaObj->StdNamespace)
6935 SemaObj->StdNamespace = SemaDeclRefs[I];
6936 if (!SemaObj->StdBadAlloc)
6937 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6938 }
6939 SemaDeclRefs.clear();
6940 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006941
6942 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6943 // encountered the pragma in the source.
6944 if(OptimizeOffPragmaLocation.isValid())
6945 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei11169dd2012-12-18 14:30:41 +00006946}
6947
6948IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6949 // Note that we are loading an identifier.
6950 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006951 StringRef Name(NameStart, NameEnd - NameStart);
6952
6953 // If there is a global index, look there first to determine which modules
6954 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006955 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00006956 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00006957 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006958 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6959 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006960 }
6961 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006962 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006963 NumIdentifierLookups,
6964 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006965 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006966 IdentifierInfo *II = Visitor.getIdentifierInfo();
6967 markIdentifierUpToDate(II);
6968 return II;
6969}
6970
6971namespace clang {
6972 /// \brief An identifier-lookup iterator that enumerates all of the
6973 /// identifiers stored within a set of AST files.
6974 class ASTIdentifierIterator : public IdentifierIterator {
6975 /// \brief The AST reader whose identifiers are being enumerated.
6976 const ASTReader &Reader;
6977
6978 /// \brief The current index into the chain of AST files stored in
6979 /// the AST reader.
6980 unsigned Index;
6981
6982 /// \brief The current position within the identifier lookup table
6983 /// of the current AST file.
6984 ASTIdentifierLookupTable::key_iterator Current;
6985
6986 /// \brief The end position within the identifier lookup table of
6987 /// the current AST file.
6988 ASTIdentifierLookupTable::key_iterator End;
6989
6990 public:
6991 explicit ASTIdentifierIterator(const ASTReader &Reader);
6992
Craig Topper3e89dfe2014-03-13 02:13:41 +00006993 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006994 };
6995}
6996
6997ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6998 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6999 ASTIdentifierLookupTable *IdTable
7000 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7001 Current = IdTable->key_begin();
7002 End = IdTable->key_end();
7003}
7004
7005StringRef ASTIdentifierIterator::Next() {
7006 while (Current == End) {
7007 // If we have exhausted all of our AST files, we're done.
7008 if (Index == 0)
7009 return StringRef();
7010
7011 --Index;
7012 ASTIdentifierLookupTable *IdTable
7013 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7014 IdentifierLookupTable;
7015 Current = IdTable->key_begin();
7016 End = IdTable->key_end();
7017 }
7018
7019 // We have any identifiers remaining in the current AST file; return
7020 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007021 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007022 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007023 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007024}
7025
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007026IdentifierIterator *ASTReader::getIdentifiers() {
7027 if (!loadGlobalIndex())
7028 return GlobalIndex->createIdentifierIterator();
7029
Guy Benyei11169dd2012-12-18 14:30:41 +00007030 return new ASTIdentifierIterator(*this);
7031}
7032
7033namespace clang { namespace serialization {
7034 class ReadMethodPoolVisitor {
7035 ASTReader &Reader;
7036 Selector Sel;
7037 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007038 unsigned InstanceBits;
7039 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007040 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7041 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007042
7043 public:
7044 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7045 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007046 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
7047 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00007048
7049 static bool visit(ModuleFile &M, void *UserData) {
7050 ReadMethodPoolVisitor *This
7051 = static_cast<ReadMethodPoolVisitor *>(UserData);
7052
7053 if (!M.SelectorLookupTable)
7054 return false;
7055
7056 // If we've already searched this module file, skip it now.
7057 if (M.Generation <= This->PriorGeneration)
7058 return true;
7059
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007060 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007061 ASTSelectorLookupTable *PoolTable
7062 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7063 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
7064 if (Pos == PoolTable->end())
7065 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007066
7067 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00007068 ++This->Reader.NumSelectorsRead;
7069 // FIXME: Not quite happy with the statistics here. We probably should
7070 // disable this tracking when called via LoadSelector.
7071 // Also, should entries without methods count as misses?
7072 ++This->Reader.NumMethodPoolEntriesRead;
7073 ASTSelectorLookupTrait::data_type Data = *Pos;
7074 if (This->Reader.DeserializationListener)
7075 This->Reader.DeserializationListener->SelectorRead(Data.ID,
7076 This->Sel);
7077
7078 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7079 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007080 This->InstanceBits = Data.InstanceBits;
7081 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00007082 return true;
7083 }
7084
7085 /// \brief Retrieve the instance methods found by this visitor.
7086 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7087 return InstanceMethods;
7088 }
7089
7090 /// \brief Retrieve the instance methods found by this visitor.
7091 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7092 return FactoryMethods;
7093 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007094
7095 unsigned getInstanceBits() const { return InstanceBits; }
7096 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007097 };
7098} } // end namespace clang::serialization
7099
7100/// \brief Add the given set of methods to the method list.
7101static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7102 ObjCMethodList &List) {
7103 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7104 S.addMethodToGlobalList(&List, Methods[I]);
7105 }
7106}
7107
7108void ASTReader::ReadMethodPool(Selector Sel) {
7109 // Get the selector generation and update it to the current generation.
7110 unsigned &Generation = SelectorGeneration[Sel];
7111 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007112 Generation = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00007113
7114 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007115 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007116 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7117 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7118
7119 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007120 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007121 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007122
7123 ++NumMethodPoolHits;
7124
Guy Benyei11169dd2012-12-18 14:30:41 +00007125 if (!getSema())
7126 return;
7127
7128 Sema &S = *getSema();
7129 Sema::GlobalMethodPool::iterator Pos
7130 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7131
7132 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7133 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007134 Pos->second.first.setBits(Visitor.getInstanceBits());
7135 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00007136}
7137
7138void ASTReader::ReadKnownNamespaces(
7139 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7140 Namespaces.clear();
7141
7142 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7143 if (NamespaceDecl *Namespace
7144 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7145 Namespaces.push_back(Namespace);
7146 }
7147}
7148
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007149void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00007150 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007151 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7152 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007153 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007154 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007155 Undefined.insert(std::make_pair(D, Loc));
7156 }
7157}
Nick Lewycky8334af82013-01-26 00:35:08 +00007158
Guy Benyei11169dd2012-12-18 14:30:41 +00007159void ASTReader::ReadTentativeDefinitions(
7160 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7161 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7162 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7163 if (Var)
7164 TentativeDefs.push_back(Var);
7165 }
7166 TentativeDefinitions.clear();
7167}
7168
7169void ASTReader::ReadUnusedFileScopedDecls(
7170 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7171 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7172 DeclaratorDecl *D
7173 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7174 if (D)
7175 Decls.push_back(D);
7176 }
7177 UnusedFileScopedDecls.clear();
7178}
7179
7180void ASTReader::ReadDelegatingConstructors(
7181 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7182 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7183 CXXConstructorDecl *D
7184 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7185 if (D)
7186 Decls.push_back(D);
7187 }
7188 DelegatingCtorDecls.clear();
7189}
7190
7191void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7192 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7193 TypedefNameDecl *D
7194 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7195 if (D)
7196 Decls.push_back(D);
7197 }
7198 ExtVectorDecls.clear();
7199}
7200
7201void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7202 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7203 CXXRecordDecl *D
7204 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7205 if (D)
7206 Decls.push_back(D);
7207 }
7208 DynamicClasses.clear();
7209}
7210
Nico Weber72889432014-09-06 01:25:55 +00007211void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7212 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7213 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7214 ++I) {
7215 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7216 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7217 if (D)
7218 Decls.insert(D);
7219 }
7220 UnusedLocalTypedefNameCandidates.clear();
7221}
7222
Guy Benyei11169dd2012-12-18 14:30:41 +00007223void
Richard Smith78165b52013-01-10 23:43:47 +00007224ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7225 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7226 NamedDecl *D
7227 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00007228 if (D)
7229 Decls.push_back(D);
7230 }
Richard Smith78165b52013-01-10 23:43:47 +00007231 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007232}
7233
7234void ASTReader::ReadReferencedSelectors(
7235 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7236 if (ReferencedSelectorsData.empty())
7237 return;
7238
7239 // If there are @selector references added them to its pool. This is for
7240 // implementation of -Wselector.
7241 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7242 unsigned I = 0;
7243 while (I < DataSize) {
7244 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7245 SourceLocation SelLoc
7246 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7247 Sels.push_back(std::make_pair(Sel, SelLoc));
7248 }
7249 ReferencedSelectorsData.clear();
7250}
7251
7252void ASTReader::ReadWeakUndeclaredIdentifiers(
7253 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7254 if (WeakUndeclaredIdentifiers.empty())
7255 return;
7256
7257 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7258 IdentifierInfo *WeakId
7259 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7260 IdentifierInfo *AliasId
7261 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7262 SourceLocation Loc
7263 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7264 bool Used = WeakUndeclaredIdentifiers[I++];
7265 WeakInfo WI(AliasId, Loc);
7266 WI.setUsed(Used);
7267 WeakIDs.push_back(std::make_pair(WeakId, WI));
7268 }
7269 WeakUndeclaredIdentifiers.clear();
7270}
7271
7272void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7273 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7274 ExternalVTableUse VT;
7275 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7276 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7277 VT.DefinitionRequired = VTableUses[Idx++];
7278 VTables.push_back(VT);
7279 }
7280
7281 VTableUses.clear();
7282}
7283
7284void ASTReader::ReadPendingInstantiations(
7285 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7286 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7287 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7288 SourceLocation Loc
7289 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7290
7291 Pending.push_back(std::make_pair(D, Loc));
7292 }
7293 PendingInstantiations.clear();
7294}
7295
Richard Smithe40f2ba2013-08-07 21:41:30 +00007296void ASTReader::ReadLateParsedTemplates(
7297 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7298 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7299 /* In loop */) {
7300 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7301
7302 LateParsedTemplate *LT = new LateParsedTemplate;
7303 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7304
7305 ModuleFile *F = getOwningModuleFile(LT->D);
7306 assert(F && "No module");
7307
7308 unsigned TokN = LateParsedTemplates[Idx++];
7309 LT->Toks.reserve(TokN);
7310 for (unsigned T = 0; T < TokN; ++T)
7311 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7312
7313 LPTMap[FD] = LT;
7314 }
7315
7316 LateParsedTemplates.clear();
7317}
7318
Guy Benyei11169dd2012-12-18 14:30:41 +00007319void ASTReader::LoadSelector(Selector Sel) {
7320 // It would be complicated to avoid reading the methods anyway. So don't.
7321 ReadMethodPool(Sel);
7322}
7323
7324void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7325 assert(ID && "Non-zero identifier ID required");
7326 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7327 IdentifiersLoaded[ID - 1] = II;
7328 if (DeserializationListener)
7329 DeserializationListener->IdentifierRead(ID, II);
7330}
7331
7332/// \brief Set the globally-visible declarations associated with the given
7333/// identifier.
7334///
7335/// If the AST reader is currently in a state where the given declaration IDs
7336/// cannot safely be resolved, they are queued until it is safe to resolve
7337/// them.
7338///
7339/// \param II an IdentifierInfo that refers to one or more globally-visible
7340/// declarations.
7341///
7342/// \param DeclIDs the set of declaration IDs with the name @p II that are
7343/// visible at global scope.
7344///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007345/// \param Decls if non-null, this vector will be populated with the set of
7346/// deserialized declarations. These declarations will not be pushed into
7347/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007348void
7349ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7350 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007351 SmallVectorImpl<Decl *> *Decls) {
7352 if (NumCurrentElementsDeserializing && !Decls) {
7353 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007354 return;
7355 }
7356
7357 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007358 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007359 // Queue this declaration so that it will be added to the
7360 // translation unit scope and identifier's declaration chain
7361 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007362 PreloadedDeclIDs.push_back(DeclIDs[I]);
7363 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007364 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007365
7366 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7367
7368 // If we're simply supposed to record the declarations, do so now.
7369 if (Decls) {
7370 Decls->push_back(D);
7371 continue;
7372 }
7373
7374 // Introduce this declaration into the translation-unit scope
7375 // and add it to the declaration chain for this identifier, so
7376 // that (unqualified) name lookup will find it.
7377 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007378 }
7379}
7380
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007381IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007382 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007383 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007384
7385 if (IdentifiersLoaded.empty()) {
7386 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007387 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007388 }
7389
7390 ID -= 1;
7391 if (!IdentifiersLoaded[ID]) {
7392 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7393 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7394 ModuleFile *M = I->second;
7395 unsigned Index = ID - M->BaseIdentifierID;
7396 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7397
7398 // All of the strings in the AST file are preceded by a 16-bit length.
7399 // Extract that 16-bit length to avoid having to execute strlen().
7400 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7401 // unsigned integers. This is important to avoid integer overflow when
7402 // we cast them to 'unsigned'.
7403 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7404 unsigned StrLen = (((unsigned) StrLenPtr[0])
7405 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007406 IdentifiersLoaded[ID]
7407 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00007408 if (DeserializationListener)
7409 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7410 }
7411
7412 return IdentifiersLoaded[ID];
7413}
7414
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007415IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7416 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007417}
7418
7419IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7420 if (LocalID < NUM_PREDEF_IDENT_IDS)
7421 return LocalID;
7422
7423 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7424 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7425 assert(I != M.IdentifierRemap.end()
7426 && "Invalid index into identifier index remap");
7427
7428 return LocalID + I->second;
7429}
7430
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007431MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007432 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007433 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007434
7435 if (MacrosLoaded.empty()) {
7436 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007437 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007438 }
7439
7440 ID -= NUM_PREDEF_MACRO_IDS;
7441 if (!MacrosLoaded[ID]) {
7442 GlobalMacroMapType::iterator I
7443 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7444 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7445 ModuleFile *M = I->second;
7446 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007447 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7448
7449 if (DeserializationListener)
7450 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7451 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007452 }
7453
7454 return MacrosLoaded[ID];
7455}
7456
7457MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7458 if (LocalID < NUM_PREDEF_MACRO_IDS)
7459 return LocalID;
7460
7461 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7462 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7463 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7464
7465 return LocalID + I->second;
7466}
7467
7468serialization::SubmoduleID
7469ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7470 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7471 return LocalID;
7472
7473 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7474 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7475 assert(I != M.SubmoduleRemap.end()
7476 && "Invalid index into submodule index remap");
7477
7478 return LocalID + I->second;
7479}
7480
7481Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7482 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7483 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007484 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007485 }
7486
7487 if (GlobalID > SubmodulesLoaded.size()) {
7488 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007489 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007490 }
7491
7492 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7493}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007494
7495Module *ASTReader::getModule(unsigned ID) {
7496 return getSubmodule(ID);
7497}
7498
Guy Benyei11169dd2012-12-18 14:30:41 +00007499Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7500 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7501}
7502
7503Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7504 if (ID == 0)
7505 return Selector();
7506
7507 if (ID > SelectorsLoaded.size()) {
7508 Error("selector ID out of range in AST file");
7509 return Selector();
7510 }
7511
Craig Toppera13603a2014-05-22 05:54:18 +00007512 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007513 // Load this selector from the selector table.
7514 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7515 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7516 ModuleFile &M = *I->second;
7517 ASTSelectorLookupTrait Trait(*this, M);
7518 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7519 SelectorsLoaded[ID - 1] =
7520 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7521 if (DeserializationListener)
7522 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7523 }
7524
7525 return SelectorsLoaded[ID - 1];
7526}
7527
7528Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7529 return DecodeSelector(ID);
7530}
7531
7532uint32_t ASTReader::GetNumExternalSelectors() {
7533 // ID 0 (the null selector) is considered an external selector.
7534 return getTotalNumSelectors() + 1;
7535}
7536
7537serialization::SelectorID
7538ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7539 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7540 return LocalID;
7541
7542 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7543 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7544 assert(I != M.SelectorRemap.end()
7545 && "Invalid index into selector index remap");
7546
7547 return LocalID + I->second;
7548}
7549
7550DeclarationName
7551ASTReader::ReadDeclarationName(ModuleFile &F,
7552 const RecordData &Record, unsigned &Idx) {
7553 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7554 switch (Kind) {
7555 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007556 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007557
7558 case DeclarationName::ObjCZeroArgSelector:
7559 case DeclarationName::ObjCOneArgSelector:
7560 case DeclarationName::ObjCMultiArgSelector:
7561 return DeclarationName(ReadSelector(F, Record, Idx));
7562
7563 case DeclarationName::CXXConstructorName:
7564 return Context.DeclarationNames.getCXXConstructorName(
7565 Context.getCanonicalType(readType(F, Record, Idx)));
7566
7567 case DeclarationName::CXXDestructorName:
7568 return Context.DeclarationNames.getCXXDestructorName(
7569 Context.getCanonicalType(readType(F, Record, Idx)));
7570
7571 case DeclarationName::CXXConversionFunctionName:
7572 return Context.DeclarationNames.getCXXConversionFunctionName(
7573 Context.getCanonicalType(readType(F, Record, Idx)));
7574
7575 case DeclarationName::CXXOperatorName:
7576 return Context.DeclarationNames.getCXXOperatorName(
7577 (OverloadedOperatorKind)Record[Idx++]);
7578
7579 case DeclarationName::CXXLiteralOperatorName:
7580 return Context.DeclarationNames.getCXXLiteralOperatorName(
7581 GetIdentifierInfo(F, Record, Idx));
7582
7583 case DeclarationName::CXXUsingDirective:
7584 return DeclarationName::getUsingDirectiveName();
7585 }
7586
7587 llvm_unreachable("Invalid NameKind!");
7588}
7589
7590void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7591 DeclarationNameLoc &DNLoc,
7592 DeclarationName Name,
7593 const RecordData &Record, unsigned &Idx) {
7594 switch (Name.getNameKind()) {
7595 case DeclarationName::CXXConstructorName:
7596 case DeclarationName::CXXDestructorName:
7597 case DeclarationName::CXXConversionFunctionName:
7598 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7599 break;
7600
7601 case DeclarationName::CXXOperatorName:
7602 DNLoc.CXXOperatorName.BeginOpNameLoc
7603 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7604 DNLoc.CXXOperatorName.EndOpNameLoc
7605 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7606 break;
7607
7608 case DeclarationName::CXXLiteralOperatorName:
7609 DNLoc.CXXLiteralOperatorName.OpNameLoc
7610 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7611 break;
7612
7613 case DeclarationName::Identifier:
7614 case DeclarationName::ObjCZeroArgSelector:
7615 case DeclarationName::ObjCOneArgSelector:
7616 case DeclarationName::ObjCMultiArgSelector:
7617 case DeclarationName::CXXUsingDirective:
7618 break;
7619 }
7620}
7621
7622void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7623 DeclarationNameInfo &NameInfo,
7624 const RecordData &Record, unsigned &Idx) {
7625 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7626 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7627 DeclarationNameLoc DNLoc;
7628 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7629 NameInfo.setInfo(DNLoc);
7630}
7631
7632void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7633 const RecordData &Record, unsigned &Idx) {
7634 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7635 unsigned NumTPLists = Record[Idx++];
7636 Info.NumTemplParamLists = NumTPLists;
7637 if (NumTPLists) {
7638 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7639 for (unsigned i=0; i != NumTPLists; ++i)
7640 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7641 }
7642}
7643
7644TemplateName
7645ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7646 unsigned &Idx) {
7647 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7648 switch (Kind) {
7649 case TemplateName::Template:
7650 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7651
7652 case TemplateName::OverloadedTemplate: {
7653 unsigned size = Record[Idx++];
7654 UnresolvedSet<8> Decls;
7655 while (size--)
7656 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7657
7658 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7659 }
7660
7661 case TemplateName::QualifiedTemplate: {
7662 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7663 bool hasTemplKeyword = Record[Idx++];
7664 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7665 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7666 }
7667
7668 case TemplateName::DependentTemplate: {
7669 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7670 if (Record[Idx++]) // isIdentifier
7671 return Context.getDependentTemplateName(NNS,
7672 GetIdentifierInfo(F, Record,
7673 Idx));
7674 return Context.getDependentTemplateName(NNS,
7675 (OverloadedOperatorKind)Record[Idx++]);
7676 }
7677
7678 case TemplateName::SubstTemplateTemplateParm: {
7679 TemplateTemplateParmDecl *param
7680 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7681 if (!param) return TemplateName();
7682 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7683 return Context.getSubstTemplateTemplateParm(param, replacement);
7684 }
7685
7686 case TemplateName::SubstTemplateTemplateParmPack: {
7687 TemplateTemplateParmDecl *Param
7688 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7689 if (!Param)
7690 return TemplateName();
7691
7692 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7693 if (ArgPack.getKind() != TemplateArgument::Pack)
7694 return TemplateName();
7695
7696 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7697 }
7698 }
7699
7700 llvm_unreachable("Unhandled template name kind!");
7701}
7702
7703TemplateArgument
7704ASTReader::ReadTemplateArgument(ModuleFile &F,
7705 const RecordData &Record, unsigned &Idx) {
7706 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7707 switch (Kind) {
7708 case TemplateArgument::Null:
7709 return TemplateArgument();
7710 case TemplateArgument::Type:
7711 return TemplateArgument(readType(F, Record, Idx));
7712 case TemplateArgument::Declaration: {
7713 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007714 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007715 }
7716 case TemplateArgument::NullPtr:
7717 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7718 case TemplateArgument::Integral: {
7719 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7720 QualType T = readType(F, Record, Idx);
7721 return TemplateArgument(Context, Value, T);
7722 }
7723 case TemplateArgument::Template:
7724 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7725 case TemplateArgument::TemplateExpansion: {
7726 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007727 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007728 if (unsigned NumExpansions = Record[Idx++])
7729 NumTemplateExpansions = NumExpansions - 1;
7730 return TemplateArgument(Name, NumTemplateExpansions);
7731 }
7732 case TemplateArgument::Expression:
7733 return TemplateArgument(ReadExpr(F));
7734 case TemplateArgument::Pack: {
7735 unsigned NumArgs = Record[Idx++];
7736 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7737 for (unsigned I = 0; I != NumArgs; ++I)
7738 Args[I] = ReadTemplateArgument(F, Record, Idx);
7739 return TemplateArgument(Args, NumArgs);
7740 }
7741 }
7742
7743 llvm_unreachable("Unhandled template argument kind!");
7744}
7745
7746TemplateParameterList *
7747ASTReader::ReadTemplateParameterList(ModuleFile &F,
7748 const RecordData &Record, unsigned &Idx) {
7749 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7750 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7751 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7752
7753 unsigned NumParams = Record[Idx++];
7754 SmallVector<NamedDecl *, 16> Params;
7755 Params.reserve(NumParams);
7756 while (NumParams--)
7757 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7758
7759 TemplateParameterList* TemplateParams =
7760 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7761 Params.data(), Params.size(), RAngleLoc);
7762 return TemplateParams;
7763}
7764
7765void
7766ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007767ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007768 ModuleFile &F, const RecordData &Record,
7769 unsigned &Idx) {
7770 unsigned NumTemplateArgs = Record[Idx++];
7771 TemplArgs.reserve(NumTemplateArgs);
7772 while (NumTemplateArgs--)
7773 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7774}
7775
7776/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007777void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007778 const RecordData &Record, unsigned &Idx) {
7779 unsigned NumDecls = Record[Idx++];
7780 Set.reserve(Context, NumDecls);
7781 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007782 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007783 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007784 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007785 }
7786}
7787
7788CXXBaseSpecifier
7789ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7790 const RecordData &Record, unsigned &Idx) {
7791 bool isVirtual = static_cast<bool>(Record[Idx++]);
7792 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7793 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7794 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7795 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7796 SourceRange Range = ReadSourceRange(F, Record, Idx);
7797 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7798 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7799 EllipsisLoc);
7800 Result.setInheritConstructors(inheritConstructors);
7801 return Result;
7802}
7803
7804std::pair<CXXCtorInitializer **, unsigned>
7805ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7806 unsigned &Idx) {
Craig Toppera13603a2014-05-22 05:54:18 +00007807 CXXCtorInitializer **CtorInitializers = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007808 unsigned NumInitializers = Record[Idx++];
7809 if (NumInitializers) {
7810 CtorInitializers
7811 = new (Context) CXXCtorInitializer*[NumInitializers];
7812 for (unsigned i=0; i != NumInitializers; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00007813 TypeSourceInfo *TInfo = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007814 bool IsBaseVirtual = false;
Craig Toppera13603a2014-05-22 05:54:18 +00007815 FieldDecl *Member = nullptr;
7816 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007817
7818 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7819 switch (Type) {
7820 case CTOR_INITIALIZER_BASE:
7821 TInfo = GetTypeSourceInfo(F, Record, Idx);
7822 IsBaseVirtual = Record[Idx++];
7823 break;
7824
7825 case CTOR_INITIALIZER_DELEGATING:
7826 TInfo = GetTypeSourceInfo(F, Record, Idx);
7827 break;
7828
7829 case CTOR_INITIALIZER_MEMBER:
7830 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7831 break;
7832
7833 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7834 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7835 break;
7836 }
7837
7838 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7839 Expr *Init = ReadExpr(F);
7840 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7841 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7842 bool IsWritten = Record[Idx++];
7843 unsigned SourceOrderOrNumArrayIndices;
7844 SmallVector<VarDecl *, 8> Indices;
7845 if (IsWritten) {
7846 SourceOrderOrNumArrayIndices = Record[Idx++];
7847 } else {
7848 SourceOrderOrNumArrayIndices = Record[Idx++];
7849 Indices.reserve(SourceOrderOrNumArrayIndices);
7850 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7851 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7852 }
7853
7854 CXXCtorInitializer *BOMInit;
7855 if (Type == CTOR_INITIALIZER_BASE) {
7856 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7857 LParenLoc, Init, RParenLoc,
7858 MemberOrEllipsisLoc);
7859 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7860 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7861 Init, RParenLoc);
7862 } else if (IsWritten) {
7863 if (Member)
7864 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7865 LParenLoc, Init, RParenLoc);
7866 else
7867 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7868 MemberOrEllipsisLoc, LParenLoc,
7869 Init, RParenLoc);
7870 } else {
Argyrios Kyrtzidis794671d2013-05-30 23:59:46 +00007871 if (IndirectMember) {
7872 assert(Indices.empty() && "Indirect field improperly initialized");
7873 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7874 MemberOrEllipsisLoc, LParenLoc,
7875 Init, RParenLoc);
7876 } else {
7877 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7878 LParenLoc, Init, RParenLoc,
7879 Indices.data(), Indices.size());
7880 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007881 }
7882
7883 if (IsWritten)
7884 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7885 CtorInitializers[i] = BOMInit;
7886 }
7887 }
7888
7889 return std::make_pair(CtorInitializers, NumInitializers);
7890}
7891
7892NestedNameSpecifier *
7893ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7894 const RecordData &Record, unsigned &Idx) {
7895 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00007896 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007897 for (unsigned I = 0; I != N; ++I) {
7898 NestedNameSpecifier::SpecifierKind Kind
7899 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7900 switch (Kind) {
7901 case NestedNameSpecifier::Identifier: {
7902 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7903 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7904 break;
7905 }
7906
7907 case NestedNameSpecifier::Namespace: {
7908 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7909 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7910 break;
7911 }
7912
7913 case NestedNameSpecifier::NamespaceAlias: {
7914 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7915 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7916 break;
7917 }
7918
7919 case NestedNameSpecifier::TypeSpec:
7920 case NestedNameSpecifier::TypeSpecWithTemplate: {
7921 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7922 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00007923 return nullptr;
7924
Guy Benyei11169dd2012-12-18 14:30:41 +00007925 bool Template = Record[Idx++];
7926 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7927 break;
7928 }
7929
7930 case NestedNameSpecifier::Global: {
7931 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7932 // No associated value, and there can't be a prefix.
7933 break;
7934 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00007935
7936 case NestedNameSpecifier::Super: {
7937 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7938 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
7939 break;
7940 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007941 }
7942 Prev = NNS;
7943 }
7944 return NNS;
7945}
7946
7947NestedNameSpecifierLoc
7948ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7949 unsigned &Idx) {
7950 unsigned N = Record[Idx++];
7951 NestedNameSpecifierLocBuilder Builder;
7952 for (unsigned I = 0; I != N; ++I) {
7953 NestedNameSpecifier::SpecifierKind Kind
7954 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7955 switch (Kind) {
7956 case NestedNameSpecifier::Identifier: {
7957 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7958 SourceRange Range = ReadSourceRange(F, Record, Idx);
7959 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7960 break;
7961 }
7962
7963 case NestedNameSpecifier::Namespace: {
7964 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7965 SourceRange Range = ReadSourceRange(F, Record, Idx);
7966 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7967 break;
7968 }
7969
7970 case NestedNameSpecifier::NamespaceAlias: {
7971 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7972 SourceRange Range = ReadSourceRange(F, Record, Idx);
7973 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7974 break;
7975 }
7976
7977 case NestedNameSpecifier::TypeSpec:
7978 case NestedNameSpecifier::TypeSpecWithTemplate: {
7979 bool Template = Record[Idx++];
7980 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7981 if (!T)
7982 return NestedNameSpecifierLoc();
7983 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7984
7985 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7986 Builder.Extend(Context,
7987 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7988 T->getTypeLoc(), ColonColonLoc);
7989 break;
7990 }
7991
7992 case NestedNameSpecifier::Global: {
7993 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7994 Builder.MakeGlobal(Context, ColonColonLoc);
7995 break;
7996 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00007997
7998 case NestedNameSpecifier::Super: {
7999 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8000 SourceRange Range = ReadSourceRange(F, Record, Idx);
8001 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8002 break;
8003 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008004 }
8005 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008006
Guy Benyei11169dd2012-12-18 14:30:41 +00008007 return Builder.getWithLocInContext(Context);
8008}
8009
8010SourceRange
8011ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8012 unsigned &Idx) {
8013 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8014 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8015 return SourceRange(beg, end);
8016}
8017
8018/// \brief Read an integral value
8019llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8020 unsigned BitWidth = Record[Idx++];
8021 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8022 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8023 Idx += NumWords;
8024 return Result;
8025}
8026
8027/// \brief Read a signed integral value
8028llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8029 bool isUnsigned = Record[Idx++];
8030 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8031}
8032
8033/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008034llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8035 const llvm::fltSemantics &Sem,
8036 unsigned &Idx) {
8037 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008038}
8039
8040// \brief Read a string
8041std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8042 unsigned Len = Record[Idx++];
8043 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8044 Idx += Len;
8045 return Result;
8046}
8047
8048VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8049 unsigned &Idx) {
8050 unsigned Major = Record[Idx++];
8051 unsigned Minor = Record[Idx++];
8052 unsigned Subminor = Record[Idx++];
8053 if (Minor == 0)
8054 return VersionTuple(Major);
8055 if (Subminor == 0)
8056 return VersionTuple(Major, Minor - 1);
8057 return VersionTuple(Major, Minor - 1, Subminor - 1);
8058}
8059
8060CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8061 const RecordData &Record,
8062 unsigned &Idx) {
8063 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8064 return CXXTemporary::Create(Context, Decl);
8065}
8066
8067DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008068 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008069}
8070
8071DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8072 return Diags.Report(Loc, DiagID);
8073}
8074
8075/// \brief Retrieve the identifier table associated with the
8076/// preprocessor.
8077IdentifierTable &ASTReader::getIdentifierTable() {
8078 return PP.getIdentifierTable();
8079}
8080
8081/// \brief Record that the given ID maps to the given switch-case
8082/// statement.
8083void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008084 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008085 "Already have a SwitchCase with this ID");
8086 (*CurrSwitchCaseStmts)[ID] = SC;
8087}
8088
8089/// \brief Retrieve the switch-case statement with the given ID.
8090SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008091 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008092 return (*CurrSwitchCaseStmts)[ID];
8093}
8094
8095void ASTReader::ClearSwitchCaseIDs() {
8096 CurrSwitchCaseStmts->clear();
8097}
8098
8099void ASTReader::ReadComments() {
8100 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008101 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008102 serialization::ModuleFile *> >::iterator
8103 I = CommentsCursors.begin(),
8104 E = CommentsCursors.end();
8105 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008106 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008107 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008108 serialization::ModuleFile &F = *I->second;
8109 SavedStreamPosition SavedPosition(Cursor);
8110
8111 RecordData Record;
8112 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008113 llvm::BitstreamEntry Entry =
8114 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008115
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008116 switch (Entry.Kind) {
8117 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8118 case llvm::BitstreamEntry::Error:
8119 Error("malformed block record in AST file");
8120 return;
8121 case llvm::BitstreamEntry::EndBlock:
8122 goto NextCursor;
8123 case llvm::BitstreamEntry::Record:
8124 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008125 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008126 }
8127
8128 // Read a record.
8129 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008130 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008131 case COMMENTS_RAW_COMMENT: {
8132 unsigned Idx = 0;
8133 SourceRange SR = ReadSourceRange(F, Record, Idx);
8134 RawComment::CommentKind Kind =
8135 (RawComment::CommentKind) Record[Idx++];
8136 bool IsTrailingComment = Record[Idx++];
8137 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008138 Comments.push_back(new (Context) RawComment(
8139 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8140 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008141 break;
8142 }
8143 }
8144 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008145 NextCursor:
8146 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008147 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008148}
8149
Argyrios Kyrtzidis1bde1172014-11-18 05:24:18 +00008150void ASTReader::getInputFiles(ModuleFile &F,
8151 SmallVectorImpl<serialization::InputFile> &Files) {
8152 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8153 unsigned ID = I+1;
8154 Files.push_back(getInputFile(F, ID));
8155 }
8156}
8157
Richard Smithcd45dbc2014-04-19 03:48:30 +00008158std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8159 // If we know the owning module, use it.
8160 if (Module *M = D->getOwningModule())
8161 return M->getFullModuleName();
8162
8163 // Otherwise, use the name of the top-level module the decl is within.
8164 if (ModuleFile *M = getOwningModuleFile(D))
8165 return M->ModuleName;
8166
8167 // Not from a module.
8168 return "";
8169}
8170
Guy Benyei11169dd2012-12-18 14:30:41 +00008171void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008172 while (!PendingIdentifierInfos.empty() ||
8173 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008174 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008175 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008176 // If any identifiers with corresponding top-level declarations have
8177 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008178 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8179 TopLevelDeclsMap;
8180 TopLevelDeclsMap TopLevelDecls;
8181
Guy Benyei11169dd2012-12-18 14:30:41 +00008182 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008183 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008184 SmallVector<uint32_t, 4> DeclIDs =
8185 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008186 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008187
8188 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008189 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008190
Richard Smith851072e2014-05-19 20:59:20 +00008191 // For each decl chain that we wanted to complete while deserializing, mark
8192 // it as "still needs to be completed".
8193 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8194 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8195 }
8196 PendingIncompleteDeclChains.clear();
8197
Guy Benyei11169dd2012-12-18 14:30:41 +00008198 // Load pending declaration chains.
8199 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8200 loadPendingDeclChain(PendingDeclChains[I]);
8201 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8202 }
8203 PendingDeclChains.clear();
8204
Douglas Gregor6168bd22013-02-18 15:53:43 +00008205 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008206 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8207 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008208 IdentifierInfo *II = TLD->first;
8209 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008210 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008211 }
8212 }
8213
Guy Benyei11169dd2012-12-18 14:30:41 +00008214 // Load any pending macro definitions.
8215 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008216 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8217 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8218 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8219 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008220 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008221 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008222 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008223 if (Info.M->Kind != MK_ImplicitModule &&
8224 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008225 resolvePendingMacro(II, Info);
8226 }
8227 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008228 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008229 ++IDIdx) {
8230 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008231 if (Info.M->Kind == MK_ImplicitModule ||
8232 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008233 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008234 }
8235 }
8236 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008237
8238 // Wire up the DeclContexts for Decls that we delayed setting until
8239 // recursive loading is completed.
8240 while (!PendingDeclContextInfos.empty()) {
8241 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8242 PendingDeclContextInfos.pop_front();
8243 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8244 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8245 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8246 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008247
Richard Smithd1c46742014-04-30 02:24:17 +00008248 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008249 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008250 auto Update = PendingUpdateRecords.pop_back_val();
8251 ReadingKindTracker ReadingKind(Read_Decl, *this);
8252 loadDeclUpdateRecords(Update.first, Update.second);
8253 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008254 }
8255
8256 // If we deserialized any C++ or Objective-C class definitions, any
8257 // Objective-C protocol definitions, or any redeclarable templates, make sure
8258 // that all redeclarations point to the definitions. Note that this can only
8259 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008260 for (Decl *D : PendingDefinitions) {
8261 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008262 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008263 // Make sure that the TagType points at the definition.
8264 const_cast<TagType*>(TagT)->decl = TD;
8265 }
8266
Craig Topperc6914d02014-08-25 04:15:02 +00008267 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith2c381642014-08-27 23:11:59 +00008268 for (auto R : RD->redecls()) {
8269 assert((R == D) == R->isThisDeclarationADefinition() &&
8270 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008271 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008272 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008273 }
8274
8275 continue;
8276 }
8277
Craig Topperc6914d02014-08-25 04:15:02 +00008278 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008279 // Make sure that the ObjCInterfaceType points at the definition.
8280 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8281 ->Decl = ID;
8282
Aaron Ballman86c93902014-03-06 23:45:36 +00008283 for (auto R : ID->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008284 R->Data = ID->Data;
8285
8286 continue;
8287 }
8288
Craig Topperc6914d02014-08-25 04:15:02 +00008289 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Aaron Ballman86c93902014-03-06 23:45:36 +00008290 for (auto R : PD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008291 R->Data = PD->Data;
8292
8293 continue;
8294 }
8295
Craig Topperc6914d02014-08-25 04:15:02 +00008296 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00008297 for (auto R : RTD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008298 R->Common = RTD->Common;
8299 }
8300 PendingDefinitions.clear();
8301
8302 // Load the bodies of any functions or methods we've encountered. We do
8303 // this now (delayed) so that we can be sure that the declaration chains
8304 // have been fully wired up.
8305 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8306 PBEnd = PendingBodies.end();
8307 PB != PBEnd; ++PB) {
8308 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8309 // FIXME: Check for =delete/=default?
8310 // FIXME: Complain about ODR violations here?
8311 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8312 FD->setLazyBody(PB->second);
8313 continue;
8314 }
8315
8316 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8317 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8318 MD->setLazyBody(PB->second);
8319 }
8320 PendingBodies.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008321}
8322
8323void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008324 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8325 return;
8326
Richard Smitha0ce9c42014-07-29 23:23:27 +00008327 // Trigger the import of the full definition of each class that had any
8328 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008329 // These updates may in turn find and diagnose some ODR failures, so take
8330 // ownership of the set first.
8331 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8332 PendingOdrMergeFailures.clear();
8333 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008334 Merge.first->buildLookup();
8335 Merge.first->decls_begin();
8336 Merge.first->bases_begin();
8337 Merge.first->vbases_begin();
8338 for (auto *RD : Merge.second) {
8339 RD->decls_begin();
8340 RD->bases_begin();
8341 RD->vbases_begin();
8342 }
8343 }
8344
8345 // For each declaration from a merged context, check that the canonical
8346 // definition of that context also contains a declaration of the same
8347 // entity.
8348 //
8349 // Caution: this loop does things that might invalidate iterators into
8350 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8351 while (!PendingOdrMergeChecks.empty()) {
8352 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8353
8354 // FIXME: Skip over implicit declarations for now. This matters for things
8355 // like implicitly-declared special member functions. This isn't entirely
8356 // correct; we can end up with multiple unmerged declarations of the same
8357 // implicit entity.
8358 if (D->isImplicit())
8359 continue;
8360
8361 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008362
8363 bool Found = false;
8364 const Decl *DCanon = D->getCanonicalDecl();
8365
Richard Smith01bdb7a2014-08-28 05:44:07 +00008366 for (auto RI : D->redecls()) {
8367 if (RI->getLexicalDeclContext() == CanonDef) {
8368 Found = true;
8369 break;
8370 }
8371 }
8372 if (Found)
8373 continue;
8374
Richard Smitha0ce9c42014-07-29 23:23:27 +00008375 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith01bdb7a2014-08-28 05:44:07 +00008376 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
Richard Smitha0ce9c42014-07-29 23:23:27 +00008377 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8378 !Found && I != E; ++I) {
8379 for (auto RI : (*I)->redecls()) {
8380 if (RI->getLexicalDeclContext() == CanonDef) {
8381 // This declaration is present in the canonical definition. If it's
8382 // in the same redecl chain, it's the one we're looking for.
8383 if (RI->getCanonicalDecl() == DCanon)
8384 Found = true;
8385 else
8386 Candidates.push_back(cast<NamedDecl>(RI));
8387 break;
8388 }
8389 }
8390 }
8391
8392 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008393 // The AST doesn't like TagDecls becoming invalid after they've been
8394 // completed. We only really need to mark FieldDecls as invalid here.
8395 if (!isa<TagDecl>(D))
8396 D->setInvalidDecl();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008397
8398 std::string CanonDefModule =
8399 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8400 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8401 << D << getOwningModuleNameForDiagnostic(D)
8402 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8403
8404 if (Candidates.empty())
8405 Diag(cast<Decl>(CanonDef)->getLocation(),
8406 diag::note_module_odr_violation_no_possible_decls) << D;
8407 else {
8408 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8409 Diag(Candidates[I]->getLocation(),
8410 diag::note_module_odr_violation_possible_decl)
8411 << Candidates[I];
8412 }
8413
8414 DiagnosedOdrMergeFailures.insert(CanonDef);
8415 }
8416 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008417
8418 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008419 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008420 // If we've already pointed out a specific problem with this class, don't
8421 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008422 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008423 continue;
8424
8425 bool Diagnosed = false;
8426 for (auto *RD : Merge.second) {
8427 // Multiple different declarations got merged together; tell the user
8428 // where they came from.
8429 if (Merge.first != RD) {
8430 // FIXME: Walk the definition, figure out what's different,
8431 // and diagnose that.
8432 if (!Diagnosed) {
8433 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8434 Diag(Merge.first->getLocation(),
8435 diag::err_module_odr_violation_different_definitions)
8436 << Merge.first << Module.empty() << Module;
8437 Diagnosed = true;
8438 }
8439
8440 Diag(RD->getLocation(),
8441 diag::note_module_odr_violation_different_definitions)
8442 << getOwningModuleNameForDiagnostic(RD);
8443 }
8444 }
8445
8446 if (!Diagnosed) {
8447 // All definitions are updates to the same declaration. This happens if a
8448 // module instantiates the declaration of a class template specialization
8449 // and two or more other modules instantiate its definition.
8450 //
8451 // FIXME: Indicate which modules had instantiations of this definition.
8452 // FIXME: How can this even happen?
8453 Diag(Merge.first->getLocation(),
8454 diag::err_module_odr_violation_different_instantiations)
8455 << Merge.first;
8456 }
8457 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008458}
8459
8460void ASTReader::FinishedDeserializing() {
8461 assert(NumCurrentElementsDeserializing &&
8462 "FinishedDeserializing not paired with StartedDeserializing");
8463 if (NumCurrentElementsDeserializing == 1) {
8464 // We decrease NumCurrentElementsDeserializing only after pending actions
8465 // are finished, to avoid recursively re-calling finishPendingActions().
8466 finishPendingActions();
8467 }
8468 --NumCurrentElementsDeserializing;
8469
Richard Smitha0ce9c42014-07-29 23:23:27 +00008470 if (NumCurrentElementsDeserializing == 0) {
8471 diagnoseOdrViolations();
8472
Richard Smith04d05b52014-03-23 00:27:18 +00008473 // We are not in recursive loading, so it's safe to pass the "interesting"
8474 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008475 if (Consumer)
8476 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008477 }
8478}
8479
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008480void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00008481 D = D->getMostRecentDecl();
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008482
8483 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8484 SemaObj->TUScope->AddDecl(D);
8485 } else if (SemaObj->TUScope) {
8486 // Adding the decl to IdResolver may have failed because it was already in
8487 // (even though it was not added in scope). If it is already in, make sure
8488 // it gets in the scope as well.
8489 if (std::find(SemaObj->IdResolver.begin(Name),
8490 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8491 SemaObj->TUScope->AddDecl(D);
8492 }
8493}
8494
Nico Weber824285e2014-05-08 04:26:47 +00008495ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8496 bool DisableValidation, bool AllowASTWithCompilerErrors,
8497 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00008498 bool UseGlobalIndex)
Craig Toppera13603a2014-05-22 05:54:18 +00008499 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008500 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Craig Toppera13603a2014-05-22 05:54:18 +00008501 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8502 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8503 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8504 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008505 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8506 AllowConfigurationMismatch(AllowConfigurationMismatch),
8507 ValidateSystemInputs(ValidateSystemInputs),
8508 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Richard Smith053f6c62014-05-16 23:01:30 +00008509 CurrSwitchCaseStmts(&SwitchCaseStmts),
Nico Weber824285e2014-05-08 04:26:47 +00008510 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8511 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8512 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8513 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8514 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8515 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8516 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8517 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8518 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8519 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8520 ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008521 SourceMgr.setExternalSLocEntrySource(this);
8522}
8523
8524ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008525 if (OwnsDeserializationListener)
8526 delete DeserializationListener;
8527
Guy Benyei11169dd2012-12-18 14:30:41 +00008528 for (DeclContextVisibleUpdatesPending::iterator
8529 I = PendingVisibleUpdates.begin(),
8530 E = PendingVisibleUpdates.end();
8531 I != E; ++I) {
8532 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8533 F = I->second.end();
8534 J != F; ++J)
8535 delete J->first;
8536 }
8537}