blob: 777ea83198192946e9712ca469ed52af25183160 [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}
Ben Langmuircb69b572014-03-07 06:40:32 +000083bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
84 bool Complain) {
85 return First->ReadLanguageOptions(LangOpts, Complain) ||
86 Second->ReadLanguageOptions(LangOpts, Complain);
87}
88bool
89ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
90 bool Complain) {
91 return First->ReadTargetOptions(TargetOpts, Complain) ||
92 Second->ReadTargetOptions(TargetOpts, Complain);
93}
94bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +000095 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +000096 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
97 Second->ReadDiagnosticOptions(DiagOpts, Complain);
98}
99bool
100ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
101 bool Complain) {
102 return First->ReadFileSystemOptions(FSOpts, Complain) ||
103 Second->ReadFileSystemOptions(FSOpts, Complain);
104}
105
106bool ChainedASTReaderListener::ReadHeaderSearchOptions(
107 const HeaderSearchOptions &HSOpts, bool Complain) {
108 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
109 Second->ReadHeaderSearchOptions(HSOpts, Complain);
110}
111bool ChainedASTReaderListener::ReadPreprocessorOptions(
112 const PreprocessorOptions &PPOpts, bool Complain,
113 std::string &SuggestedPredefines) {
114 return First->ReadPreprocessorOptions(PPOpts, Complain,
115 SuggestedPredefines) ||
116 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
117}
118void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
119 unsigned Value) {
120 First->ReadCounter(M, Value);
121 Second->ReadCounter(M, Value);
122}
123bool ChainedASTReaderListener::needsInputFileVisitation() {
124 return First->needsInputFileVisitation() ||
125 Second->needsInputFileVisitation();
126}
127bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
128 return First->needsSystemInputFileVisitation() ||
129 Second->needsSystemInputFileVisitation();
130}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000131void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
132 First->visitModuleFile(Filename);
133 Second->visitModuleFile(Filename);
134}
Ben Langmuircb69b572014-03-07 06:40:32 +0000135bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000136 bool isSystem,
137 bool isOverridden) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000138 bool Continue = false;
139 if (First->needsInputFileVisitation() &&
140 (!isSystem || First->needsSystemInputFileVisitation()))
141 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
142 if (Second->needsInputFileVisitation() &&
143 (!isSystem || Second->needsSystemInputFileVisitation()))
144 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
145 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000146}
147
Guy Benyei11169dd2012-12-18 14:30:41 +0000148//===----------------------------------------------------------------------===//
149// PCH validator implementation
150//===----------------------------------------------------------------------===//
151
152ASTReaderListener::~ASTReaderListener() {}
153
154/// \brief Compare the given set of language options against an existing set of
155/// language options.
156///
157/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
158///
159/// \returns true if the languagae options mis-match, false otherwise.
160static bool checkLanguageOptions(const LangOptions &LangOpts,
161 const LangOptions &ExistingLangOpts,
162 DiagnosticsEngine *Diags) {
163#define LANGOPT(Name, Bits, Default, Description) \
164 if (ExistingLangOpts.Name != LangOpts.Name) { \
165 if (Diags) \
166 Diags->Report(diag::err_pch_langopt_mismatch) \
167 << Description << LangOpts.Name << ExistingLangOpts.Name; \
168 return true; \
169 }
170
171#define VALUE_LANGOPT(Name, Bits, Default, Description) \
172 if (ExistingLangOpts.Name != LangOpts.Name) { \
173 if (Diags) \
174 Diags->Report(diag::err_pch_langopt_value_mismatch) \
175 << Description; \
176 return true; \
177 }
178
179#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
180 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
181 if (Diags) \
182 Diags->Report(diag::err_pch_langopt_value_mismatch) \
183 << Description; \
184 return true; \
185 }
186
187#define BENIGN_LANGOPT(Name, Bits, Default, Description)
188#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
189#include "clang/Basic/LangOptions.def"
190
191 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
192 if (Diags)
193 Diags->Report(diag::err_pch_langopt_value_mismatch)
194 << "target Objective-C runtime";
195 return true;
196 }
197
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000198 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
199 LangOpts.CommentOpts.BlockCommandNames) {
200 if (Diags)
201 Diags->Report(diag::err_pch_langopt_value_mismatch)
202 << "block command names";
203 return true;
204 }
205
Guy Benyei11169dd2012-12-18 14:30:41 +0000206 return false;
207}
208
209/// \brief Compare the given set of target options against an existing set of
210/// target options.
211///
212/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
213///
214/// \returns true if the target options mis-match, false otherwise.
215static bool checkTargetOptions(const TargetOptions &TargetOpts,
216 const TargetOptions &ExistingTargetOpts,
217 DiagnosticsEngine *Diags) {
218#define CHECK_TARGET_OPT(Field, Name) \
219 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
220 if (Diags) \
221 Diags->Report(diag::err_pch_targetopt_mismatch) \
222 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
223 return true; \
224 }
225
226 CHECK_TARGET_OPT(Triple, "target");
227 CHECK_TARGET_OPT(CPU, "target CPU");
228 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei11169dd2012-12-18 14:30:41 +0000229#undef CHECK_TARGET_OPT
230
231 // Compare feature sets.
232 SmallVector<StringRef, 4> ExistingFeatures(
233 ExistingTargetOpts.FeaturesAsWritten.begin(),
234 ExistingTargetOpts.FeaturesAsWritten.end());
235 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
236 TargetOpts.FeaturesAsWritten.end());
237 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
238 std::sort(ReadFeatures.begin(), ReadFeatures.end());
239
240 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
241 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
242 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
243 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
244 ++ExistingIdx;
245 ++ReadIdx;
246 continue;
247 }
248
249 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
250 if (Diags)
251 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
252 << false << ReadFeatures[ReadIdx];
253 return true;
254 }
255
256 if (Diags)
257 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
258 << true << ExistingFeatures[ExistingIdx];
259 return true;
260 }
261
262 if (ExistingIdx < ExistingN) {
263 if (Diags)
264 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
265 << true << ExistingFeatures[ExistingIdx];
266 return true;
267 }
268
269 if (ReadIdx < ReadN) {
270 if (Diags)
271 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
272 << false << ReadFeatures[ReadIdx];
273 return true;
274 }
275
276 return false;
277}
278
279bool
280PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
281 bool Complain) {
282 const LangOptions &ExistingLangOpts = PP.getLangOpts();
283 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000284 Complain? &Reader.Diags : nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +0000285}
286
287bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
288 bool Complain) {
289 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
290 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000291 Complain? &Reader.Diags : nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +0000292}
293
294namespace {
295 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
296 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000297 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
298 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000299}
300
Ben Langmuirb92de022014-04-29 16:25:26 +0000301static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
302 DiagnosticsEngine &Diags,
303 bool Complain) {
304 typedef DiagnosticsEngine::Level Level;
305
306 // Check current mappings for new -Werror mappings, and the stored mappings
307 // for cases that were explicitly mapped to *not* be errors that are now
308 // errors because of options like -Werror.
309 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
310
311 for (DiagnosticsEngine *MappingSource : MappingSources) {
312 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
313 diag::kind DiagID = DiagIDMappingPair.first;
314 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
315 if (CurLevel < DiagnosticsEngine::Error)
316 continue; // not significant
317 Level StoredLevel =
318 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
319 if (StoredLevel < DiagnosticsEngine::Error) {
320 if (Complain)
321 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
322 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
323 return true;
324 }
325 }
326 }
327
328 return false;
329}
330
331static DiagnosticsEngine::ExtensionHandling
332isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
333 DiagnosticsEngine::ExtensionHandling Ext =
334 Diags.getExtensionHandlingBehavior();
335 if (Ext == DiagnosticsEngine::Ext_Warn && Diags.getWarningsAsErrors())
336 Ext = DiagnosticsEngine::Ext_Error;
337 return Ext;
338}
339
340static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
341 DiagnosticsEngine &Diags,
342 bool IsSystem, bool Complain) {
343 // Top-level options
344 if (IsSystem) {
345 if (Diags.getSuppressSystemWarnings())
346 return false;
347 // If -Wsystem-headers was not enabled before, be conservative
348 if (StoredDiags.getSuppressSystemWarnings()) {
349 if (Complain)
350 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
351 return true;
352 }
353 }
354
355 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
356 if (Complain)
357 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
358 return true;
359 }
360
361 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
362 !StoredDiags.getEnableAllWarnings()) {
363 if (Complain)
364 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
365 return true;
366 }
367
368 if (isExtHandlingFromDiagsError(Diags) &&
369 !isExtHandlingFromDiagsError(StoredDiags)) {
370 if (Complain)
371 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
372 return true;
373 }
374
375 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
376}
377
378bool PCHValidator::ReadDiagnosticOptions(
379 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
380 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
381 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
382 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
383 new DiagnosticsEngine(DiagIDs, DiagOpts.getPtr()));
384 // This should never fail, because we would have processed these options
385 // before writing them to an ASTFile.
386 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
387
388 ModuleManager &ModuleMgr = Reader.getModuleManager();
389 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
390
391 // If the original import came from a file explicitly generated by the user,
392 // don't check the diagnostic mappings.
393 // FIXME: currently this is approximated by checking whether this is not a
394 // module import.
395 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
396 // the transitive closure of its imports, since unrelated modules cannot be
397 // imported until after this module finishes validation.
398 ModuleFile *TopImport = *ModuleMgr.rbegin();
399 while (!TopImport->ImportedBy.empty())
400 TopImport = TopImport->ImportedBy[0];
401 if (TopImport->Kind != MK_Module)
402 return false;
403
404 StringRef ModuleName = TopImport->ModuleName;
405 assert(!ModuleName.empty() && "diagnostic options read before module name");
406
407 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
408 assert(M && "missing module");
409
410 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
411 // contains the union of their flags.
412 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
413}
414
Guy Benyei11169dd2012-12-18 14:30:41 +0000415/// \brief Collect the macro definitions provided by the given preprocessor
416/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000417static void
418collectMacroDefinitions(const PreprocessorOptions &PPOpts,
419 MacroDefinitionsMap &Macros,
420 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
422 StringRef Macro = PPOpts.Macros[I].first;
423 bool IsUndef = PPOpts.Macros[I].second;
424
425 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
426 StringRef MacroName = MacroPair.first;
427 StringRef MacroBody = MacroPair.second;
428
429 // For an #undef'd macro, we only care about the name.
430 if (IsUndef) {
431 if (MacroNames && !Macros.count(MacroName))
432 MacroNames->push_back(MacroName);
433
434 Macros[MacroName] = std::make_pair("", true);
435 continue;
436 }
437
438 // For a #define'd macro, figure out the actual definition.
439 if (MacroName.size() == Macro.size())
440 MacroBody = "1";
441 else {
442 // Note: GCC drops anything following an end-of-line character.
443 StringRef::size_type End = MacroBody.find_first_of("\n\r");
444 MacroBody = MacroBody.substr(0, End);
445 }
446
447 if (MacroNames && !Macros.count(MacroName))
448 MacroNames->push_back(MacroName);
449 Macros[MacroName] = std::make_pair(MacroBody, false);
450 }
451}
452
453/// \brief Check the preprocessor options deserialized from the control block
454/// against the preprocessor options in an existing preprocessor.
455///
456/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
457static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
458 const PreprocessorOptions &ExistingPPOpts,
459 DiagnosticsEngine *Diags,
460 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000461 std::string &SuggestedPredefines,
462 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 // Check macro definitions.
464 MacroDefinitionsMap ASTFileMacros;
465 collectMacroDefinitions(PPOpts, ASTFileMacros);
466 MacroDefinitionsMap ExistingMacros;
467 SmallVector<StringRef, 4> ExistingMacroNames;
468 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
469
470 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
471 // Dig out the macro definition in the existing preprocessor options.
472 StringRef MacroName = ExistingMacroNames[I];
473 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
474
475 // Check whether we know anything about this macro name or not.
476 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
477 = ASTFileMacros.find(MacroName);
478 if (Known == ASTFileMacros.end()) {
479 // FIXME: Check whether this identifier was referenced anywhere in the
480 // AST file. If so, we should reject the AST file. Unfortunately, this
481 // information isn't in the control block. What shall we do about it?
482
483 if (Existing.second) {
484 SuggestedPredefines += "#undef ";
485 SuggestedPredefines += MacroName.str();
486 SuggestedPredefines += '\n';
487 } else {
488 SuggestedPredefines += "#define ";
489 SuggestedPredefines += MacroName.str();
490 SuggestedPredefines += ' ';
491 SuggestedPredefines += Existing.first.str();
492 SuggestedPredefines += '\n';
493 }
494 continue;
495 }
496
497 // If the macro was defined in one but undef'd in the other, we have a
498 // conflict.
499 if (Existing.second != Known->second.second) {
500 if (Diags) {
501 Diags->Report(diag::err_pch_macro_def_undef)
502 << MacroName << Known->second.second;
503 }
504 return true;
505 }
506
507 // If the macro was #undef'd in both, or if the macro bodies are identical,
508 // it's fine.
509 if (Existing.second || Existing.first == Known->second.first)
510 continue;
511
512 // The macro bodies differ; complain.
513 if (Diags) {
514 Diags->Report(diag::err_pch_macro_def_conflict)
515 << MacroName << Known->second.first << Existing.first;
516 }
517 return true;
518 }
519
520 // Check whether we're using predefines.
521 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
522 if (Diags) {
523 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
524 }
525 return true;
526 }
527
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000528 // Detailed record is important since it is used for the module cache hash.
529 if (LangOpts.Modules &&
530 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
531 if (Diags) {
532 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
533 }
534 return true;
535 }
536
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 // Compute the #include and #include_macros lines we need.
538 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
539 StringRef File = ExistingPPOpts.Includes[I];
540 if (File == ExistingPPOpts.ImplicitPCHInclude)
541 continue;
542
543 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
544 != PPOpts.Includes.end())
545 continue;
546
547 SuggestedPredefines += "#include \"";
548 SuggestedPredefines +=
549 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
550 SuggestedPredefines += "\"\n";
551 }
552
553 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
554 StringRef File = ExistingPPOpts.MacroIncludes[I];
555 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
556 File)
557 != PPOpts.MacroIncludes.end())
558 continue;
559
560 SuggestedPredefines += "#__include_macros \"";
561 SuggestedPredefines +=
562 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
563 SuggestedPredefines += "\"\n##\n";
564 }
565
566 return false;
567}
568
569bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
570 bool Complain,
571 std::string &SuggestedPredefines) {
572 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
573
574 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000575 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000577 SuggestedPredefines,
578 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000579}
580
Guy Benyei11169dd2012-12-18 14:30:41 +0000581void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
582 PP.setCounterValue(Value);
583}
584
585//===----------------------------------------------------------------------===//
586// AST reader implementation
587//===----------------------------------------------------------------------===//
588
Nico Weber824285e2014-05-08 04:26:47 +0000589void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
590 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000591 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000592 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000593}
594
595
596
597unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
598 return serialization::ComputeHash(Sel);
599}
600
601
602std::pair<unsigned, unsigned>
603ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000604 using namespace llvm::support;
605 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
606 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000607 return std::make_pair(KeyLen, DataLen);
608}
609
610ASTSelectorLookupTrait::internal_key_type
611ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000612 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000613 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000614 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
615 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
616 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 if (N == 0)
618 return SelTable.getNullarySelector(FirstII);
619 else if (N == 1)
620 return SelTable.getUnarySelector(FirstII);
621
622 SmallVector<IdentifierInfo *, 16> Args;
623 Args.push_back(FirstII);
624 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000625 Args.push_back(Reader.getLocalIdentifier(
626 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000627
628 return SelTable.getSelector(N, Args.data());
629}
630
631ASTSelectorLookupTrait::data_type
632ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
633 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000634 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000635
636 data_type Result;
637
Justin Bogner57ba0b22014-03-28 22:03:24 +0000638 Result.ID = Reader.getGlobalSelectorID(
639 F, endian::readNext<uint32_t, little, unaligned>(d));
640 unsigned NumInstanceMethodsAndBits =
641 endian::readNext<uint16_t, little, unaligned>(d);
642 unsigned NumFactoryMethodsAndBits =
643 endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000644 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
645 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
646 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
647 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000648
649 // Load instance methods
650 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000651 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
652 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000653 Result.Instance.push_back(Method);
654 }
655
656 // Load factory methods
657 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000658 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
659 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000660 Result.Factory.push_back(Method);
661 }
662
663 return Result;
664}
665
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000666unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
667 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000668}
669
670std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000671ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000672 using namespace llvm::support;
673 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
674 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 return std::make_pair(KeyLen, DataLen);
676}
677
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000678ASTIdentifierLookupTraitBase::internal_key_type
679ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000680 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000681 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000682}
683
Douglas Gregordcf25082013-02-11 18:16:18 +0000684/// \brief Whether the given identifier is "interesting".
685static bool isInterestingIdentifier(IdentifierInfo &II) {
686 return II.isPoisoned() ||
687 II.isExtensionToken() ||
688 II.getObjCOrBuiltinID() ||
689 II.hasRevertedTokenIDToIdentifier() ||
690 II.hadMacroDefinition() ||
691 II.getFETokenInfo<void>();
692}
693
Guy Benyei11169dd2012-12-18 14:30:41 +0000694IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
695 const unsigned char* d,
696 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000697 using namespace llvm::support;
698 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 bool IsInteresting = RawID & 0x01;
700
701 // Wipe out the "is interesting" bit.
702 RawID = RawID >> 1;
703
704 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
705 if (!IsInteresting) {
706 // For uninteresting identifiers, just build the IdentifierInfo
707 // and associate it with the persistent ID.
708 IdentifierInfo *II = KnownII;
709 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000710 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000711 KnownII = II;
712 }
713 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000714 if (!II->isFromAST()) {
715 bool WasInteresting = isInterestingIdentifier(*II);
716 II->setIsFromAST();
717 if (WasInteresting)
718 II->setChangedSinceDeserialization();
719 }
720 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 return II;
722 }
723
Justin Bogner57ba0b22014-03-28 22:03:24 +0000724 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
725 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000726 bool CPlusPlusOperatorKeyword = Bits & 0x01;
727 Bits >>= 1;
728 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
729 Bits >>= 1;
730 bool Poisoned = Bits & 0x01;
731 Bits >>= 1;
732 bool ExtensionToken = Bits & 0x01;
733 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000734 bool hasSubmoduleMacros = Bits & 0x01;
735 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 bool hadMacroDefinition = Bits & 0x01;
737 Bits >>= 1;
738
739 assert(Bits == 0 && "Extra bits in the identifier?");
740 DataLen -= 8;
741
742 // Build the IdentifierInfo itself and link the identifier ID with
743 // the new IdentifierInfo.
744 IdentifierInfo *II = KnownII;
745 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000746 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000747 KnownII = II;
748 }
749 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000750 if (!II->isFromAST()) {
751 bool WasInteresting = isInterestingIdentifier(*II);
752 II->setIsFromAST();
753 if (WasInteresting)
754 II->setChangedSinceDeserialization();
755 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000756
757 // Set or check the various bits in the IdentifierInfo structure.
758 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000759 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 II->RevertTokenIDToIdentifier();
761 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
762 assert(II->isExtensionToken() == ExtensionToken &&
763 "Incorrect extension token flag");
764 (void)ExtensionToken;
765 if (Poisoned)
766 II->setIsPoisoned(true);
767 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
768 "Incorrect C++ operator keyword flag");
769 (void)CPlusPlusOperatorKeyword;
770
771 // If this identifier is a macro, deserialize the macro
772 // definition.
773 if (hadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000774 uint32_t MacroDirectivesOffset =
775 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000776 DataLen -= 4;
777 SmallVector<uint32_t, 8> LocalMacroIDs;
778 if (hasSubmoduleMacros) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000779 while (uint32_t LocalMacroID =
780 endian::readNext<uint32_t, little, unaligned>(d)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000781 DataLen -= 4;
782 LocalMacroIDs.push_back(LocalMacroID);
783 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000784 DataLen -= 4;
785 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000786
787 if (F.Kind == MK_Module) {
Richard Smith49f906a2014-03-01 00:08:04 +0000788 // Macro definitions are stored from newest to oldest, so reverse them
789 // before registering them.
790 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000791 for (SmallVectorImpl<uint32_t>::iterator
Richard Smith49f906a2014-03-01 00:08:04 +0000792 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
793 unsigned Size = 1;
794
795 static const uint32_t HasOverridesFlag = 0x80000000U;
796 if (I + 1 != E && (I[1] & HasOverridesFlag))
797 Size += 1 + (I[1] & ~HasOverridesFlag);
798
799 MacroSizes.push_back(Size);
800 I += Size;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000801 }
Richard Smith49f906a2014-03-01 00:08:04 +0000802
803 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
804 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
805 SE = MacroSizes.rend();
806 SI != SE; ++SI) {
807 I -= *SI;
808
809 uint32_t LocalMacroID = *I;
810 llvm::ArrayRef<uint32_t> Overrides;
811 if (*SI != 1)
812 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
813 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
814 }
815 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000816 } else {
817 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
818 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 }
820
821 Reader.SetIdentifierInfo(ID, II);
822
823 // Read all of the declarations visible at global scope with this
824 // name.
825 if (DataLen > 0) {
826 SmallVector<uint32_t, 4> DeclIDs;
827 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000828 DeclIDs.push_back(Reader.getGlobalDeclID(
829 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 Reader.SetGloballyVisibleDecls(II, DeclIDs);
831 }
832
833 return II;
834}
835
836unsigned
837ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
838 llvm::FoldingSetNodeID ID;
839 ID.AddInteger(Key.Kind);
840
841 switch (Key.Kind) {
842 case DeclarationName::Identifier:
843 case DeclarationName::CXXLiteralOperatorName:
844 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
845 break;
846 case DeclarationName::ObjCZeroArgSelector:
847 case DeclarationName::ObjCOneArgSelector:
848 case DeclarationName::ObjCMultiArgSelector:
849 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
850 break;
851 case DeclarationName::CXXOperatorName:
852 ID.AddInteger((OverloadedOperatorKind)Key.Data);
853 break;
854 case DeclarationName::CXXConstructorName:
855 case DeclarationName::CXXDestructorName:
856 case DeclarationName::CXXConversionFunctionName:
857 case DeclarationName::CXXUsingDirective:
858 break;
859 }
860
861 return ID.ComputeHash();
862}
863
864ASTDeclContextNameLookupTrait::internal_key_type
865ASTDeclContextNameLookupTrait::GetInternalKey(
866 const external_key_type& Name) const {
867 DeclNameKey Key;
868 Key.Kind = Name.getNameKind();
869 switch (Name.getNameKind()) {
870 case DeclarationName::Identifier:
871 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
872 break;
873 case DeclarationName::ObjCZeroArgSelector:
874 case DeclarationName::ObjCOneArgSelector:
875 case DeclarationName::ObjCMultiArgSelector:
876 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
877 break;
878 case DeclarationName::CXXOperatorName:
879 Key.Data = Name.getCXXOverloadedOperator();
880 break;
881 case DeclarationName::CXXLiteralOperatorName:
882 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
883 break;
884 case DeclarationName::CXXConstructorName:
885 case DeclarationName::CXXDestructorName:
886 case DeclarationName::CXXConversionFunctionName:
887 case DeclarationName::CXXUsingDirective:
888 Key.Data = 0;
889 break;
890 }
891
892 return Key;
893}
894
895std::pair<unsigned, unsigned>
896ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000897 using namespace llvm::support;
898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000900 return std::make_pair(KeyLen, DataLen);
901}
902
903ASTDeclContextNameLookupTrait::internal_key_type
904ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000905 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000906
907 DeclNameKey Key;
908 Key.Kind = (DeclarationName::NameKind)*d++;
909 switch (Key.Kind) {
910 case DeclarationName::Identifier:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000911 Key.Data = (uint64_t)Reader.getLocalIdentifier(
912 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000913 break;
914 case DeclarationName::ObjCZeroArgSelector:
915 case DeclarationName::ObjCOneArgSelector:
916 case DeclarationName::ObjCMultiArgSelector:
917 Key.Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000918 (uint64_t)Reader.getLocalSelector(
919 F, endian::readNext<uint32_t, little, unaligned>(
920 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000921 break;
922 case DeclarationName::CXXOperatorName:
923 Key.Data = *d++; // OverloadedOperatorKind
924 break;
925 case DeclarationName::CXXLiteralOperatorName:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000926 Key.Data = (uint64_t)Reader.getLocalIdentifier(
927 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000928 break;
929 case DeclarationName::CXXConstructorName:
930 case DeclarationName::CXXDestructorName:
931 case DeclarationName::CXXConversionFunctionName:
932 case DeclarationName::CXXUsingDirective:
933 Key.Data = 0;
934 break;
935 }
936
937 return Key;
938}
939
940ASTDeclContextNameLookupTrait::data_type
941ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
942 const unsigned char* d,
943 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000944 using namespace llvm::support;
945 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000946 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
947 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000948 return std::make_pair(Start, Start + NumDecls);
949}
950
951bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000952 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000953 const std::pair<uint64_t, uint64_t> &Offsets,
954 DeclContextInfo &Info) {
955 SavedStreamPosition SavedPosition(Cursor);
956 // First the lexical decls.
957 if (Offsets.first != 0) {
958 Cursor.JumpToBit(Offsets.first);
959
960 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000961 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000963 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000964 if (RecCode != DECL_CONTEXT_LEXICAL) {
965 Error("Expected lexical block");
966 return true;
967 }
968
Chris Lattner0e6c9402013-01-20 02:38:54 +0000969 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
970 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000971 }
972
973 // Now the lookup table.
974 if (Offsets.second != 0) {
975 Cursor.JumpToBit(Offsets.second);
976
977 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000978 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000979 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000980 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000981 if (RecCode != DECL_CONTEXT_VISIBLE) {
982 Error("Expected visible lookup table block");
983 return true;
984 }
Justin Bognerda4e6502014-04-14 16:34:29 +0000985 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
986 (const unsigned char *)Blob.data() + Record[0],
987 (const unsigned char *)Blob.data() + sizeof(uint32_t),
988 (const unsigned char *)Blob.data(),
989 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000990 }
991
992 return false;
993}
994
995void ASTReader::Error(StringRef Msg) {
996 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +0000997 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
998 Diag(diag::note_module_cache_path)
999 << PP.getHeaderSearchInfo().getModuleCachePath();
1000 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001001}
1002
1003void ASTReader::Error(unsigned DiagID,
1004 StringRef Arg1, StringRef Arg2) {
1005 if (Diags.isDiagnosticInFlight())
1006 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1007 else
1008 Diag(DiagID) << Arg1 << Arg2;
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// Source Manager Deserialization
1013//===----------------------------------------------------------------------===//
1014
1015/// \brief Read the line table in the source manager block.
1016/// \returns true if there was an error.
1017bool ASTReader::ParseLineTable(ModuleFile &F,
1018 SmallVectorImpl<uint64_t> &Record) {
1019 unsigned Idx = 0;
1020 LineTableInfo &LineTable = SourceMgr.getLineTable();
1021
1022 // Parse the file names
1023 std::map<int, int> FileIDs;
1024 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1025 // Extract the file name
1026 unsigned FilenameLen = Record[Idx++];
1027 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1028 Idx += FilenameLen;
1029 MaybeAddSystemRootToFilename(F, Filename);
1030 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1031 }
1032
1033 // Parse the line entries
1034 std::vector<LineEntry> Entries;
1035 while (Idx < Record.size()) {
1036 int FID = Record[Idx++];
1037 assert(FID >= 0 && "Serialized line entries for non-local file.");
1038 // Remap FileID from 1-based old view.
1039 FID += F.SLocEntryBaseID - 1;
1040
1041 // Extract the line entries
1042 unsigned NumEntries = Record[Idx++];
1043 assert(NumEntries && "Numentries is 00000");
1044 Entries.clear();
1045 Entries.reserve(NumEntries);
1046 for (unsigned I = 0; I != NumEntries; ++I) {
1047 unsigned FileOffset = Record[Idx++];
1048 unsigned LineNo = Record[Idx++];
1049 int FilenameID = FileIDs[Record[Idx++]];
1050 SrcMgr::CharacteristicKind FileKind
1051 = (SrcMgr::CharacteristicKind)Record[Idx++];
1052 unsigned IncludeOffset = Record[Idx++];
1053 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1054 FileKind, IncludeOffset));
1055 }
1056 LineTable.AddEntry(FileID::get(FID), Entries);
1057 }
1058
1059 return false;
1060}
1061
1062/// \brief Read a source manager block
1063bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1064 using namespace SrcMgr;
1065
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001066 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001067
1068 // Set the source-location entry cursor to the current position in
1069 // the stream. This cursor will be used to read the contents of the
1070 // source manager block initially, and then lazily read
1071 // source-location entries as needed.
1072 SLocEntryCursor = F.Stream;
1073
1074 // The stream itself is going to skip over the source manager block.
1075 if (F.Stream.SkipBlock()) {
1076 Error("malformed block record in AST file");
1077 return true;
1078 }
1079
1080 // Enter the source manager block.
1081 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1082 Error("malformed source manager block record in AST file");
1083 return true;
1084 }
1085
1086 RecordData Record;
1087 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001088 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1089
1090 switch (E.Kind) {
1091 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1092 case llvm::BitstreamEntry::Error:
1093 Error("malformed block record in AST file");
1094 return true;
1095 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001097 case llvm::BitstreamEntry::Record:
1098 // The interesting case.
1099 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001100 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001101
Guy Benyei11169dd2012-12-18 14:30:41 +00001102 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001103 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001104 StringRef Blob;
1105 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 default: // Default behavior: ignore.
1107 break;
1108
1109 case SM_SLOC_FILE_ENTRY:
1110 case SM_SLOC_BUFFER_ENTRY:
1111 case SM_SLOC_EXPANSION_ENTRY:
1112 // Once we hit one of the source location entries, we're done.
1113 return false;
1114 }
1115 }
1116}
1117
1118/// \brief If a header file is not found at the path that we expect it to be
1119/// and the PCH file was moved from its original location, try to resolve the
1120/// file by assuming that header+PCH were moved together and the header is in
1121/// the same place relative to the PCH.
1122static std::string
1123resolveFileRelativeToOriginalDir(const std::string &Filename,
1124 const std::string &OriginalDir,
1125 const std::string &CurrDir) {
1126 assert(OriginalDir != CurrDir &&
1127 "No point trying to resolve the file if the PCH dir didn't change");
1128 using namespace llvm::sys;
1129 SmallString<128> filePath(Filename);
1130 fs::make_absolute(filePath);
1131 assert(path::is_absolute(OriginalDir));
1132 SmallString<128> currPCHPath(CurrDir);
1133
1134 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1135 fileDirE = path::end(path::parent_path(filePath));
1136 path::const_iterator origDirI = path::begin(OriginalDir),
1137 origDirE = path::end(OriginalDir);
1138 // Skip the common path components from filePath and OriginalDir.
1139 while (fileDirI != fileDirE && origDirI != origDirE &&
1140 *fileDirI == *origDirI) {
1141 ++fileDirI;
1142 ++origDirI;
1143 }
1144 for (; origDirI != origDirE; ++origDirI)
1145 path::append(currPCHPath, "..");
1146 path::append(currPCHPath, fileDirI, fileDirE);
1147 path::append(currPCHPath, path::filename(Filename));
1148 return currPCHPath.str();
1149}
1150
1151bool ASTReader::ReadSLocEntry(int ID) {
1152 if (ID == 0)
1153 return false;
1154
1155 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1156 Error("source location entry ID out-of-range for AST file");
1157 return true;
1158 }
1159
1160 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1161 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001162 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001163 unsigned BaseOffset = F->SLocEntryBaseOffset;
1164
1165 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001166 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1167 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001168 Error("incorrectly-formatted source location entry in AST file");
1169 return true;
1170 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001171
Guy Benyei11169dd2012-12-18 14:30:41 +00001172 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001173 StringRef Blob;
1174 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001175 default:
1176 Error("incorrectly-formatted source location entry in AST file");
1177 return true;
1178
1179 case SM_SLOC_FILE_ENTRY: {
1180 // We will detect whether a file changed and return 'Failure' for it, but
1181 // we will also try to fail gracefully by setting up the SLocEntry.
1182 unsigned InputID = Record[4];
1183 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001184 const FileEntry *File = IF.getFile();
1185 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001186
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001187 // Note that we only check if a File was returned. If it was out-of-date
1188 // we have complained but we will continue creating a FileID to recover
1189 // gracefully.
1190 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 return true;
1192
1193 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1194 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1195 // This is the module's main file.
1196 IncludeLoc = getImportLocation(F);
1197 }
1198 SrcMgr::CharacteristicKind
1199 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1200 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1201 ID, BaseOffset + Record[0]);
1202 SrcMgr::FileInfo &FileInfo =
1203 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1204 FileInfo.NumCreatedFIDs = Record[5];
1205 if (Record[3])
1206 FileInfo.setHasLineDirectives();
1207
1208 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1209 unsigned NumFileDecls = Record[7];
1210 if (NumFileDecls) {
1211 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1212 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1213 NumFileDecls));
1214 }
1215
1216 const SrcMgr::ContentCache *ContentCache
1217 = SourceMgr.getOrCreateContentCache(File,
1218 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1219 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1220 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1221 unsigned Code = SLocEntryCursor.ReadCode();
1222 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001223 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001224
1225 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1226 Error("AST record has invalid code");
1227 return true;
1228 }
1229
1230 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001231 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 SourceMgr.overrideFileContents(File, Buffer);
1233 }
1234
1235 break;
1236 }
1237
1238 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001239 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 unsigned Offset = Record[0];
1241 SrcMgr::CharacteristicKind
1242 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1243 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1244 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1245 IncludeLoc = getImportLocation(F);
1246 }
1247 unsigned Code = SLocEntryCursor.ReadCode();
1248 Record.clear();
1249 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001250 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001251
1252 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1253 Error("AST record has invalid code");
1254 return true;
1255 }
1256
1257 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001258 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Alp Toker6ac2cd02014-05-16 17:23:01 +00001259 SourceMgr.createFileID(Buffer, FileCharacter, ID, BaseOffset + Offset,
1260 IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001261 break;
1262 }
1263
1264 case SM_SLOC_EXPANSION_ENTRY: {
1265 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1266 SourceMgr.createExpansionLoc(SpellingLoc,
1267 ReadSourceLocation(*F, Record[2]),
1268 ReadSourceLocation(*F, Record[3]),
1269 Record[4],
1270 ID,
1271 BaseOffset + Record[0]);
1272 break;
1273 }
1274 }
1275
1276 return false;
1277}
1278
1279std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1280 if (ID == 0)
1281 return std::make_pair(SourceLocation(), "");
1282
1283 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1284 Error("source location entry ID out-of-range for AST file");
1285 return std::make_pair(SourceLocation(), "");
1286 }
1287
1288 // Find which module file this entry lands in.
1289 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1290 if (M->Kind != MK_Module)
1291 return std::make_pair(SourceLocation(), "");
1292
1293 // FIXME: Can we map this down to a particular submodule? That would be
1294 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001295 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001296}
1297
1298/// \brief Find the location where the module F is imported.
1299SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1300 if (F->ImportLoc.isValid())
1301 return F->ImportLoc;
1302
1303 // Otherwise we have a PCH. It's considered to be "imported" at the first
1304 // location of its includer.
1305 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001306 // Main file is the importer.
1307 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1308 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001309 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 return F->ImportedBy[0]->FirstLoc;
1311}
1312
1313/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1314/// specified cursor. Read the abbreviations that are at the top of the block
1315/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001316bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001317 if (Cursor.EnterSubBlock(BlockID)) {
1318 Error("malformed block record in AST file");
1319 return Failure;
1320 }
1321
1322 while (true) {
1323 uint64_t Offset = Cursor.GetCurrentBitNo();
1324 unsigned Code = Cursor.ReadCode();
1325
1326 // We expect all abbrevs to be at the start of the block.
1327 if (Code != llvm::bitc::DEFINE_ABBREV) {
1328 Cursor.JumpToBit(Offset);
1329 return false;
1330 }
1331 Cursor.ReadAbbrevRecord();
1332 }
1333}
1334
Richard Smithe40f2ba2013-08-07 21:41:30 +00001335Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001336 unsigned &Idx) {
1337 Token Tok;
1338 Tok.startToken();
1339 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1340 Tok.setLength(Record[Idx++]);
1341 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1342 Tok.setIdentifierInfo(II);
1343 Tok.setKind((tok::TokenKind)Record[Idx++]);
1344 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1345 return Tok;
1346}
1347
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001348MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001349 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001350
1351 // Keep track of where we are in the stream, then jump back there
1352 // after reading this macro.
1353 SavedStreamPosition SavedPosition(Stream);
1354
1355 Stream.JumpToBit(Offset);
1356 RecordData Record;
1357 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001358 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001359
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001361 // Advance to the next record, but if we get to the end of the block, don't
1362 // pop it (removing all the abbreviations from the cursor) since we want to
1363 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001364 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001365 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1366
1367 switch (Entry.Kind) {
1368 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1369 case llvm::BitstreamEntry::Error:
1370 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001371 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001372 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001373 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001374 case llvm::BitstreamEntry::Record:
1375 // The interesting case.
1376 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001377 }
1378
1379 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 Record.clear();
1381 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001382 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001384 case PP_MACRO_DIRECTIVE_HISTORY:
1385 return Macro;
1386
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 case PP_MACRO_OBJECT_LIKE:
1388 case PP_MACRO_FUNCTION_LIKE: {
1389 // If we already have a macro, that means that we've hit the end
1390 // of the definition of the macro we were looking for. We're
1391 // done.
1392 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001393 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001394
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001395 unsigned NextIndex = 1; // Skip identifier ID.
1396 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001398 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001399 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001401 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001402
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1404 // Decode function-like macro info.
1405 bool isC99VarArgs = Record[NextIndex++];
1406 bool isGNUVarArgs = Record[NextIndex++];
1407 bool hasCommaPasting = Record[NextIndex++];
1408 MacroArgs.clear();
1409 unsigned NumArgs = Record[NextIndex++];
1410 for (unsigned i = 0; i != NumArgs; ++i)
1411 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1412
1413 // Install function-like macro info.
1414 MI->setIsFunctionLike();
1415 if (isC99VarArgs) MI->setIsC99Varargs();
1416 if (isGNUVarArgs) MI->setIsGNUVarargs();
1417 if (hasCommaPasting) MI->setHasCommaPasting();
1418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1419 PP.getPreprocessorAllocator());
1420 }
1421
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 // Remember that we saw this macro last so that we add the tokens that
1423 // form its body to it.
1424 Macro = MI;
1425
1426 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1427 Record[NextIndex]) {
1428 // We have a macro definition. Register the association
1429 PreprocessedEntityID
1430 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1431 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001432 PreprocessingRecord::PPEntityID
1433 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1434 MacroDefinition *PPDef =
1435 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1436 if (PPDef)
1437 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 }
1439
1440 ++NumMacrosRead;
1441 break;
1442 }
1443
1444 case PP_TOKEN: {
1445 // If we see a TOKEN before a PP_MACRO_*, then the file is
1446 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001447 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001448
John McCallf413f5e2013-05-03 00:10:13 +00001449 unsigned Idx = 0;
1450 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001451 Macro->AddTokenToBody(Tok);
1452 break;
1453 }
1454 }
1455 }
1456}
1457
1458PreprocessedEntityID
1459ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1460 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1461 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1462 assert(I != M.PreprocessedEntityRemap.end()
1463 && "Invalid index into preprocessed entity index remap");
1464
1465 return LocalID + I->second;
1466}
1467
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001468unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1469 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001470}
1471
1472HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001473HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1474 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1475 FE->getName() };
1476 return ikey;
1477}
Guy Benyei11169dd2012-12-18 14:30:41 +00001478
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001479bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1480 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 return false;
1482
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001483 if (strcmp(a.Filename, b.Filename) == 0)
1484 return true;
1485
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001487 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001488 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1489 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001490 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001491}
1492
1493std::pair<unsigned, unsigned>
1494HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001495 using namespace llvm::support;
1496 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001497 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001498 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001499}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001500
1501HeaderFileInfoTrait::internal_key_type
1502HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001503 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001504 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001505 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1506 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001507 ikey.Filename = (const char *)d;
1508 return ikey;
1509}
1510
Guy Benyei11169dd2012-12-18 14:30:41 +00001511HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001512HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001513 unsigned DataLen) {
1514 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001515 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001516 HeaderFileInfo HFI;
1517 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001518 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1519 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 HFI.isImport = (Flags >> 5) & 0x01;
1521 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1522 HFI.DirInfo = (Flags >> 2) & 0x03;
1523 HFI.Resolved = (Flags >> 1) & 0x01;
1524 HFI.IndexHeaderMapHeader = Flags & 0x01;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001525 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1526 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1527 M, endian::readNext<uint32_t, little, unaligned>(d));
1528 if (unsigned FrameworkOffset =
1529 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 // The framework offset is 1 greater than the actual offset,
1531 // since 0 is used as an indicator for "no framework name".
1532 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1533 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1534 }
1535
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001536 if (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001537 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001538 if (LocalSMID) {
1539 // This header is part of a module. Associate it with the module to enable
1540 // implicit module import.
1541 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1542 Module *Mod = Reader.getSubmodule(GlobalSMID);
1543 HFI.isModuleHeader = true;
1544 FileManager &FileMgr = Reader.getFileManager();
1545 ModuleMap &ModMap =
1546 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001547 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001548 }
1549 }
1550
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1552 (void)End;
1553
1554 // This HeaderFileInfo was externally loaded.
1555 HFI.External = true;
1556 return HFI;
1557}
1558
Richard Smith49f906a2014-03-01 00:08:04 +00001559void
1560ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1561 GlobalMacroID GMacID,
1562 llvm::ArrayRef<SubmoduleID> Overrides) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Craig Toppera13603a2014-05-22 05:54:18 +00001564 SubmoduleID *OverrideData = nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001565 if (!Overrides.empty()) {
1566 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1567 OverrideData[0] = Overrides.size();
1568 for (unsigned I = 0; I != Overrides.size(); ++I)
1569 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1570 }
1571 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001572}
1573
1574void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1575 ModuleFile *M,
1576 uint64_t MacroDirectivesOffset) {
1577 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1578 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001579}
1580
1581void ASTReader::ReadDefinedMacros() {
1582 // Note that we are loading defined macros.
1583 Deserializing Macros(this);
1584
1585 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1586 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001587 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001588
1589 // If there was no preprocessor block, skip this file.
1590 if (!MacroCursor.getBitStreamReader())
1591 continue;
1592
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001593 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 Cursor.JumpToBit((*I)->MacroStartOffset);
1595
1596 RecordData Record;
1597 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001598 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1599
1600 switch (E.Kind) {
1601 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1602 case llvm::BitstreamEntry::Error:
1603 Error("malformed block record in AST file");
1604 return;
1605 case llvm::BitstreamEntry::EndBlock:
1606 goto NextCursor;
1607
1608 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001609 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001610 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001611 default: // Default behavior: ignore.
1612 break;
1613
1614 case PP_MACRO_OBJECT_LIKE:
1615 case PP_MACRO_FUNCTION_LIKE:
1616 getLocalIdentifier(**I, Record[0]);
1617 break;
1618
1619 case PP_TOKEN:
1620 // Ignore tokens.
1621 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001622 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001623 break;
1624 }
1625 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001626 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001627 }
1628}
1629
1630namespace {
1631 /// \brief Visitor class used to look up identifirs in an AST file.
1632 class IdentifierLookupVisitor {
1633 StringRef Name;
1634 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001635 unsigned &NumIdentifierLookups;
1636 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001637 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001638
Guy Benyei11169dd2012-12-18 14:30:41 +00001639 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001640 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1641 unsigned &NumIdentifierLookups,
1642 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001643 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001644 NumIdentifierLookups(NumIdentifierLookups),
1645 NumIdentifierLookupHits(NumIdentifierLookupHits),
1646 Found()
1647 {
1648 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001649
1650 static bool visit(ModuleFile &M, void *UserData) {
1651 IdentifierLookupVisitor *This
1652 = static_cast<IdentifierLookupVisitor *>(UserData);
1653
1654 // If we've already searched this module file, skip it now.
1655 if (M.Generation <= This->PriorGeneration)
1656 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001657
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 ASTIdentifierLookupTable *IdTable
1659 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1660 if (!IdTable)
1661 return false;
1662
1663 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1664 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001665 ++This->NumIdentifierLookups;
1666 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 if (Pos == IdTable->end())
1668 return false;
1669
1670 // Dereferencing the iterator has the effect of building the
1671 // IdentifierInfo node and populating it with the various
1672 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001673 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 This->Found = *Pos;
1675 return true;
1676 }
1677
1678 // \brief Retrieve the identifier info found within the module
1679 // files.
1680 IdentifierInfo *getIdentifierInfo() const { return Found; }
1681 };
1682}
1683
1684void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1685 // Note that we are loading an identifier.
1686 Deserializing AnIdentifier(this);
1687
1688 unsigned PriorGeneration = 0;
1689 if (getContext().getLangOpts().Modules)
1690 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001691
1692 // If there is a global index, look there first to determine which modules
1693 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001694 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001695 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001696 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001697 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1698 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001699 }
1700 }
1701
Douglas Gregor7211ac12013-01-25 23:32:03 +00001702 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001703 NumIdentifierLookups,
1704 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001705 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 markIdentifierUpToDate(&II);
1707}
1708
1709void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1710 if (!II)
1711 return;
1712
1713 II->setOutOfDate(false);
1714
1715 // Update the generation for this identifier.
1716 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001717 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001718}
1719
Richard Smith49f906a2014-03-01 00:08:04 +00001720struct ASTReader::ModuleMacroInfo {
1721 SubmoduleID SubModID;
1722 MacroInfo *MI;
1723 SubmoduleID *Overrides;
1724 // FIXME: Remove this.
1725 ModuleFile *F;
1726
1727 bool isDefine() const { return MI; }
1728
1729 SubmoduleID getSubmoduleID() const { return SubModID; }
1730
1731 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1732 if (!Overrides)
1733 return llvm::ArrayRef<SubmoduleID>();
1734 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1735 }
1736
1737 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1738 if (!MI)
Craig Toppera13603a2014-05-22 05:54:18 +00001739 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001740 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1741 }
1742};
1743
1744ASTReader::ModuleMacroInfo *
1745ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1746 ModuleMacroInfo Info;
1747
1748 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1749 if (ID & 1) {
1750 // Macro undefinition.
1751 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
Craig Toppera13603a2014-05-22 05:54:18 +00001752 Info.MI = nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001753 } else {
1754 // Macro definition.
1755 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1756 assert(GMacID);
1757
1758 // If this macro has already been loaded, don't do so again.
1759 // FIXME: This is highly dubious. Multiple macro definitions can have the
1760 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1761 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
Craig Toppera13603a2014-05-22 05:54:18 +00001762 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001763
1764 Info.MI = getMacro(GMacID);
1765 Info.SubModID = Info.MI->getOwningModuleID();
1766 }
1767 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1768 Info.F = PMInfo.M;
1769
1770 return new (Context) ModuleMacroInfo(Info);
1771}
1772
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001773void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1774 const PendingMacroInfo &PMInfo) {
1775 assert(II);
1776
1777 if (PMInfo.M->Kind != MK_Module) {
1778 installPCHMacroDirectives(II, *PMInfo.M,
1779 PMInfo.PCHMacroData.MacroDirectivesOffset);
1780 return;
1781 }
Richard Smith49f906a2014-03-01 00:08:04 +00001782
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001783 // Module Macro.
1784
Richard Smith49f906a2014-03-01 00:08:04 +00001785 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1786 if (!MMI)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001787 return;
1788
Richard Smith49f906a2014-03-01 00:08:04 +00001789 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1790 if (Owner && Owner->NameVisibility == Module::Hidden) {
1791 // Macros in the owning module are hidden. Just remember this macro to
1792 // install if we make this module visible.
1793 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1794 } else {
1795 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001796 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001797}
1798
1799void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1800 ModuleFile &M, uint64_t Offset) {
1801 assert(M.Kind != MK_Module);
1802
1803 BitstreamCursor &Cursor = M.MacroCursor;
1804 SavedStreamPosition SavedPosition(Cursor);
1805 Cursor.JumpToBit(Offset);
1806
1807 llvm::BitstreamEntry Entry =
1808 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1809 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1810 Error("malformed block record in AST file");
1811 return;
1812 }
1813
1814 RecordData Record;
1815 PreprocessorRecordTypes RecType =
1816 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1817 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1818 Error("malformed block record in AST file");
1819 return;
1820 }
1821
1822 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001823 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001824 unsigned Idx = 0, N = Record.size();
1825 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001826 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001827 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001828 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1829 switch (K) {
1830 case MacroDirective::MD_Define: {
1831 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1832 MacroInfo *MI = getMacro(GMacID);
1833 bool isImported = Record[Idx++];
1834 bool isAmbiguous = Record[Idx++];
1835 DefMacroDirective *DefMD =
1836 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1837 DefMD->setAmbiguous(isAmbiguous);
1838 MD = DefMD;
1839 break;
1840 }
1841 case MacroDirective::MD_Undefine:
1842 MD = PP.AllocateUndefMacroDirective(Loc);
1843 break;
1844 case MacroDirective::MD_Visibility: {
1845 bool isPublic = Record[Idx++];
1846 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1847 break;
1848 }
1849 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001850
1851 if (!Latest)
1852 Latest = MD;
1853 if (Earliest)
1854 Earliest->setPrevious(MD);
1855 Earliest = MD;
1856 }
1857
1858 PP.setLoadedMacroDirective(II, Latest);
1859}
1860
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001861/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001862/// modules.
1863static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001864 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001865 assert(PrevMI && NewMI);
Craig Toppera13603a2014-05-22 05:54:18 +00001866 Module *PrevOwner = nullptr;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001867 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1868 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001869 SourceManager &SrcMgr = Reader.getSourceManager();
1870 bool PrevInSystem
1871 = PrevOwner? PrevOwner->IsSystem
1872 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1873 bool NewInSystem
1874 = NewOwner? NewOwner->IsSystem
1875 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1876 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001877 return false;
Douglas Gregor5e461192013-06-07 22:56:11 +00001878 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001879}
1880
Richard Smith49f906a2014-03-01 00:08:04 +00001881void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1882 AmbiguousMacros &Ambig,
1883 llvm::ArrayRef<SubmoduleID> Overrides) {
1884 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1885 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001886
Richard Smith49f906a2014-03-01 00:08:04 +00001887 // If this macro is not yet visible, remove it from the hidden names list.
1888 Module *Owner = getSubmodule(OwnerID);
1889 HiddenNames &Hidden = HiddenNamesMap[Owner];
1890 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1891 if (HI != Hidden.HiddenMacros.end()) {
Richard Smith9d100862014-03-06 03:16:27 +00001892 auto SubOverrides = HI->second->getOverriddenSubmodules();
Richard Smith49f906a2014-03-01 00:08:04 +00001893 Hidden.HiddenMacros.erase(HI);
Richard Smith9d100862014-03-06 03:16:27 +00001894 removeOverriddenMacros(II, Ambig, SubOverrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001895 }
1896
1897 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001898 Ambig.erase(
1899 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1900 return MD->getInfo()->getOwningModuleID() == OwnerID;
1901 }),
1902 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001903 }
1904}
1905
1906ASTReader::AmbiguousMacros *
1907ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1908 llvm::ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001909 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001910 if (!Prev && Overrides.empty())
Craig Toppera13603a2014-05-22 05:54:18 +00001911 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001912
Craig Toppera13603a2014-05-22 05:54:18 +00001913 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1914 : nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001915 if (PrevDef && PrevDef->isAmbiguous()) {
1916 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1917 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1918 Ambig.push_back(PrevDef);
1919
1920 removeOverriddenMacros(II, Ambig, Overrides);
1921
1922 if (!Ambig.empty())
1923 return &Ambig;
1924
1925 AmbiguousMacroDefs.erase(II);
1926 } else {
1927 // There's no ambiguity yet. Maybe we're introducing one.
Benjamin Kramer834652a2014-05-03 18:44:26 +00001928 AmbiguousMacros Ambig;
Richard Smith49f906a2014-03-01 00:08:04 +00001929 if (PrevDef)
1930 Ambig.push_back(PrevDef);
1931
1932 removeOverriddenMacros(II, Ambig, Overrides);
1933
1934 if (!Ambig.empty()) {
1935 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
Benjamin Kramer834652a2014-05-03 18:44:26 +00001936 std::swap(Result, Ambig);
Richard Smith49f906a2014-03-01 00:08:04 +00001937 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001938 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001939 }
Richard Smith49f906a2014-03-01 00:08:04 +00001940
1941 // We ended up with no ambiguity.
Craig Toppera13603a2014-05-22 05:54:18 +00001942 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001943}
1944
1945void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1946 Module *Owner) {
1947 assert(II && Owner);
1948
1949 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1950 if (ImportLoc.isInvalid()) {
1951 // FIXME: If we made macros from this module visible but didn't provide a
1952 // source location for the import, we don't have a location for the macro.
1953 // Use the location at which the containing module file was first imported
1954 // for now.
1955 ImportLoc = MMI->F->DirectImportLoc;
Richard Smith56be7542014-03-21 00:33:59 +00001956 assert(ImportLoc.isValid() && "no import location for a visible macro?");
Richard Smith49f906a2014-03-01 00:08:04 +00001957 }
1958
Benjamin Kramer834652a2014-05-03 18:44:26 +00001959 AmbiguousMacros *Prev =
Richard Smith49f906a2014-03-01 00:08:04 +00001960 removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1961
Richard Smith49f906a2014-03-01 00:08:04 +00001962 // Create a synthetic macro definition corresponding to the import (or null
1963 // if this was an undefinition of the macro).
1964 DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1965
1966 // If there's no ambiguity, just install the macro.
1967 if (!Prev) {
1968 if (MD)
1969 PP.appendMacroDirective(II, MD);
1970 else
1971 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1972 return;
1973 }
1974 assert(!Prev->empty());
1975
1976 if (!MD) {
1977 // We imported a #undef that didn't remove all prior definitions. The most
1978 // recent prior definition remains, and we install it in the place of the
1979 // imported directive.
1980 MacroInfo *NewMI = Prev->back()->getInfo();
1981 Prev->pop_back();
1982 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1983 }
1984
1985 // We're introducing a macro definition that creates or adds to an ambiguity.
1986 // We can resolve that ambiguity if this macro is token-for-token identical to
1987 // all of the existing definitions.
1988 MacroInfo *NewMI = MD->getInfo();
1989 assert(NewMI && "macro definition with no MacroInfo?");
1990 while (!Prev->empty()) {
1991 MacroInfo *PrevMI = Prev->back()->getInfo();
1992 assert(PrevMI && "macro definition with no MacroInfo?");
1993
1994 // Before marking the macros as ambiguous, check if this is a case where
1995 // both macros are in system headers. If so, we trust that the system
1996 // did not get it wrong. This also handles cases where Clang's own
1997 // headers have a different spelling of certain system macros:
1998 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1999 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2000 //
2001 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2002 // overrides the system limits.h's macros, so there's no conflict here.
2003 if (NewMI != PrevMI &&
2004 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2005 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2006 break;
2007
2008 // The previous definition is the same as this one (or both are defined in
2009 // system modules so we can assume they're equivalent); we don't need to
2010 // track it any more.
2011 Prev->pop_back();
2012 }
2013
2014 if (!Prev->empty())
2015 MD->setAmbiguous(true);
2016
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002017 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002018}
2019
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002020ASTReader::InputFileInfo
2021ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002022 // Go find this input file.
2023 BitstreamCursor &Cursor = F.InputFilesCursor;
2024 SavedStreamPosition SavedPosition(Cursor);
2025 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2026
2027 unsigned Code = Cursor.ReadCode();
2028 RecordData Record;
2029 StringRef Blob;
2030
2031 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2032 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2033 "invalid record type for input file");
2034 (void)Result;
2035
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002036 std::string Filename;
2037 off_t StoredSize;
2038 time_t StoredTime;
2039 bool Overridden;
2040
Ben Langmuir198c1682014-03-07 07:27:49 +00002041 assert(Record[0] == ID && "Bogus stored ID or offset");
2042 StoredSize = static_cast<off_t>(Record[1]);
2043 StoredTime = static_cast<time_t>(Record[2]);
2044 Overridden = static_cast<bool>(Record[3]);
2045 Filename = Blob;
2046 MaybeAddSystemRootToFilename(F, Filename);
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002047
Hans Wennborg73945142014-03-14 17:45:06 +00002048 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2049 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002050}
2051
2052std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002053 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00002054}
2055
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002056InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002057 // If this ID is bogus, just return an empty input file.
2058 if (ID == 0 || ID > F.InputFilesLoaded.size())
2059 return InputFile();
2060
2061 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002062 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002063 return F.InputFilesLoaded[ID-1];
2064
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002065 if (F.InputFilesLoaded[ID-1].isNotFound())
2066 return InputFile();
2067
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002069 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 SavedStreamPosition SavedPosition(Cursor);
2071 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2072
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002073 InputFileInfo FI = readInputFileInfo(F, ID);
2074 off_t StoredSize = FI.StoredSize;
2075 time_t StoredTime = FI.StoredTime;
2076 bool Overridden = FI.Overridden;
2077 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002078
Ben Langmuir198c1682014-03-07 07:27:49 +00002079 const FileEntry *File
2080 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2081 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2082
2083 // If we didn't find the file, resolve it relative to the
2084 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00002085 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00002086 F.OriginalDir != CurrentDir) {
2087 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2088 F.OriginalDir,
2089 CurrentDir);
2090 if (!Resolved.empty())
2091 File = FileMgr.getFile(Resolved);
2092 }
2093
2094 // For an overridden file, create a virtual file with the stored
2095 // size/timestamp.
Craig Toppera13603a2014-05-22 05:54:18 +00002096 if (Overridden && File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002097 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2098 }
2099
Craig Toppera13603a2014-05-22 05:54:18 +00002100 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002101 if (Complain) {
2102 std::string ErrorStr = "could not find file '";
2103 ErrorStr += Filename;
2104 ErrorStr += "' referenced by AST file";
2105 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00002106 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002107 // Record that we didn't find the file.
2108 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2109 return InputFile();
2110 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002111
Ben Langmuir198c1682014-03-07 07:27:49 +00002112 // Check if there was a request to override the contents of the file
2113 // that was part of the precompiled header. Overridding such a file
2114 // can lead to problems when lexing using the source locations from the
2115 // PCH.
2116 SourceManager &SM = getSourceManager();
2117 if (!Overridden && SM.isFileOverridden(File)) {
2118 if (Complain)
2119 Error(diag::err_fe_pch_file_overridden, Filename);
2120 // After emitting the diagnostic, recover by disabling the override so
2121 // that the original file will be used.
2122 SM.disableFileContentsOverride(File);
2123 // The FileEntry is a virtual file entry with the size of the contents
2124 // that would override the original contents. Set it to the original's
2125 // size/time.
2126 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2127 StoredSize, StoredTime);
2128 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002129
Ben Langmuir198c1682014-03-07 07:27:49 +00002130 bool IsOutOfDate = false;
2131
2132 // For an overridden file, there is nothing to validate.
2133 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00002134#if !defined(LLVM_ON_WIN32)
Ben Langmuir198c1682014-03-07 07:27:49 +00002135 // In our regression testing, the Windows file system seems to
2136 // have inconsistent modification times that sometimes
2137 // erroneously trigger this error-handling path.
2138 || StoredTime != File->getModificationTime()
Guy Benyei11169dd2012-12-18 14:30:41 +00002139#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00002140 )) {
2141 if (Complain) {
2142 // Build a list of the PCH imports that got us here (in reverse).
2143 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2144 while (ImportStack.back()->ImportedBy.size() > 0)
2145 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002146
Ben Langmuir198c1682014-03-07 07:27:49 +00002147 // The top-level PCH is stale.
2148 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2149 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002150
Ben Langmuir198c1682014-03-07 07:27:49 +00002151 // Print the import stack.
2152 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2153 Diag(diag::note_pch_required_by)
2154 << Filename << ImportStack[0]->FileName;
2155 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002156 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002157 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002158 }
2159
Ben Langmuir198c1682014-03-07 07:27:49 +00002160 if (!Diags.isDiagnosticInFlight())
2161 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 }
2163
Ben Langmuir198c1682014-03-07 07:27:49 +00002164 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 }
2166
Ben Langmuir198c1682014-03-07 07:27:49 +00002167 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2168
2169 // Note that we've loaded this input file.
2170 F.InputFilesLoaded[ID-1] = IF;
2171 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002172}
2173
2174const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2175 ModuleFile &M = ModuleMgr.getPrimaryModule();
2176 std::string Filename = filenameStrRef;
2177 MaybeAddSystemRootToFilename(M, Filename);
2178 const FileEntry *File = FileMgr.getFile(Filename);
Craig Toppera13603a2014-05-22 05:54:18 +00002179 if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 M.OriginalDir != CurrentDir) {
2181 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2182 M.OriginalDir,
2183 CurrentDir);
2184 if (!resolved.empty())
2185 File = FileMgr.getFile(resolved);
2186 }
2187
2188 return File;
2189}
2190
2191/// \brief If we are loading a relocatable PCH file, and the filename is
2192/// not an absolute path, add the system root to the beginning of the file
2193/// name.
2194void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2195 std::string &Filename) {
2196 // If this is not a relocatable PCH file, there's nothing to do.
2197 if (!M.RelocatablePCH)
2198 return;
2199
2200 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2201 return;
2202
2203 if (isysroot.empty()) {
2204 // If no system root was given, default to '/'
2205 Filename.insert(Filename.begin(), '/');
2206 return;
2207 }
2208
2209 unsigned Length = isysroot.size();
2210 if (isysroot[Length - 1] != '/')
2211 Filename.insert(Filename.begin(), '/');
2212
2213 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2214}
2215
2216ASTReader::ASTReadResult
2217ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002218 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002219 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002221 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002222
2223 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2224 Error("malformed block record in AST file");
2225 return Failure;
2226 }
2227
2228 // Read all of the records and blocks in the control block.
2229 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002230 while (1) {
2231 llvm::BitstreamEntry Entry = Stream.advance();
2232
2233 switch (Entry.Kind) {
2234 case llvm::BitstreamEntry::Error:
2235 Error("malformed block record in AST file");
2236 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002237 case llvm::BitstreamEntry::EndBlock: {
2238 // Validate input files.
2239 const HeaderSearchOptions &HSOpts =
2240 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002241
2242 // All user input files reside at the index range [0, Record[1]), and
2243 // system input files reside at [Record[1], Record[0]).
2244 // Record is the one from INPUT_FILE_OFFSETS.
2245 unsigned NumInputs = Record[0];
2246 unsigned NumUserInputs = Record[1];
2247
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002248 if (!DisableValidation &&
Ben Langmuir1e258222014-04-08 15:36:28 +00002249 (ValidateSystemInputs || !HSOpts.ModulesValidateOncePerBuildSession ||
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002250 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002251 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002252
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002253 // If we are reading a module, we will create a verification timestamp,
2254 // so we verify all input files. Otherwise, verify only user input
2255 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002256
2257 unsigned N = NumUserInputs;
2258 if (ValidateSystemInputs ||
Ben Langmuircb69b572014-03-07 06:40:32 +00002259 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2260 N = NumInputs;
2261
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002262 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002263 InputFile IF = getInputFile(F, I+1, Complain);
2264 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002265 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002266 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002268
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002269 if (Listener)
2270 Listener->visitModuleFile(F.FileName);
2271
Ben Langmuircb69b572014-03-07 06:40:32 +00002272 if (Listener && Listener->needsInputFileVisitation()) {
2273 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2274 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002275 for (unsigned I = 0; I < N; ++I) {
2276 bool IsSystem = I >= NumUserInputs;
2277 InputFileInfo FI = readInputFileInfo(F, I+1);
2278 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2279 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002280 }
2281
Guy Benyei11169dd2012-12-18 14:30:41 +00002282 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002283 }
2284
Chris Lattnere7b154b2013-01-19 21:39:22 +00002285 case llvm::BitstreamEntry::SubBlock:
2286 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 case INPUT_FILES_BLOCK_ID:
2288 F.InputFilesCursor = Stream;
2289 if (Stream.SkipBlock() || // Skip with the main cursor
2290 // Read the abbreviations
2291 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2292 Error("malformed block record in AST file");
2293 return Failure;
2294 }
2295 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002296
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002298 if (Stream.SkipBlock()) {
2299 Error("malformed block record in AST file");
2300 return Failure;
2301 }
2302 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002304
2305 case llvm::BitstreamEntry::Record:
2306 // The interesting case.
2307 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 }
2309
2310 // Read and process a record.
2311 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002312 StringRef Blob;
2313 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002314 case METADATA: {
2315 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2316 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002317 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2318 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002319 return VersionMismatch;
2320 }
2321
2322 bool hasErrors = Record[5];
2323 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2324 Diag(diag::err_pch_with_compiler_errors);
2325 return HadErrors;
2326 }
2327
2328 F.RelocatablePCH = Record[4];
2329
2330 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002331 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002332 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2333 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002334 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 return VersionMismatch;
2336 }
2337 break;
2338 }
2339
2340 case IMPORTS: {
2341 // Load each of the imported PCH files.
2342 unsigned Idx = 0, N = Record.size();
2343 while (Idx < N) {
2344 // Read information about the AST file.
2345 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2346 // The import location will be the local one for now; we will adjust
2347 // all import locations of module imports after the global source
2348 // location info are setup.
2349 SourceLocation ImportLoc =
2350 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002351 off_t StoredSize = (off_t)Record[Idx++];
2352 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002353 unsigned Length = Record[Idx++];
2354 SmallString<128> ImportedFile(Record.begin() + Idx,
2355 Record.begin() + Idx + Length);
2356 Idx += Length;
2357
2358 // Load the AST file.
2359 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002360 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 ClientLoadCapabilities)) {
2362 case Failure: return Failure;
2363 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002364 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002365 case OutOfDate: return OutOfDate;
2366 case VersionMismatch: return VersionMismatch;
2367 case ConfigurationMismatch: return ConfigurationMismatch;
2368 case HadErrors: return HadErrors;
2369 case Success: break;
2370 }
2371 }
2372 break;
2373 }
2374
2375 case LANGUAGE_OPTIONS: {
2376 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2377 if (Listener && &F == *ModuleMgr.begin() &&
2378 ParseLanguageOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002379 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 return ConfigurationMismatch;
2381 break;
2382 }
2383
2384 case TARGET_OPTIONS: {
2385 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2386 if (Listener && &F == *ModuleMgr.begin() &&
2387 ParseTargetOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002388 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002389 return ConfigurationMismatch;
2390 break;
2391 }
2392
2393 case DIAGNOSTIC_OPTIONS: {
Ben Langmuirb92de022014-04-29 16:25:26 +00002394 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002395 if (Listener && &F == *ModuleMgr.begin() &&
2396 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuirb92de022014-04-29 16:25:26 +00002397 !DisableValidation)
2398 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00002399 break;
2400 }
2401
2402 case FILE_SYSTEM_OPTIONS: {
2403 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2404 if (Listener && &F == *ModuleMgr.begin() &&
2405 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002406 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 return ConfigurationMismatch;
2408 break;
2409 }
2410
2411 case HEADER_SEARCH_OPTIONS: {
2412 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2413 if (Listener && &F == *ModuleMgr.begin() &&
2414 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002415 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002416 return ConfigurationMismatch;
2417 break;
2418 }
2419
2420 case PREPROCESSOR_OPTIONS: {
2421 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2422 if (Listener && &F == *ModuleMgr.begin() &&
2423 ParsePreprocessorOptions(Record, Complain, *Listener,
2424 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002425 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 return ConfigurationMismatch;
2427 break;
2428 }
2429
2430 case ORIGINAL_FILE:
2431 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002432 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002433 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2434 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2435 break;
2436
2437 case ORIGINAL_FILE_ID:
2438 F.OriginalSourceFileID = FileID::get(Record[0]);
2439 break;
2440
2441 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002442 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002443 break;
2444
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002445 case MODULE_NAME:
2446 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002447 if (Listener)
2448 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002449 break;
2450
2451 case MODULE_MAP_FILE:
2452 F.ModuleMapPath = Blob;
2453
2454 // Try to resolve ModuleName in the current header search context and
2455 // verify that it is found in the same module map file as we saved. If the
2456 // top-level AST file is a main file, skip this check because there is no
2457 // usable header search context.
2458 assert(!F.ModuleName.empty() &&
2459 "MODULE_NAME should come before MOUDLE_MAP_FILE");
2460 if (F.Kind == MK_Module &&
2461 (*ModuleMgr.begin())->Kind != MK_MainFile) {
2462 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2463 if (!M) {
2464 assert(ImportedBy && "top-level import should be verified");
2465 if ((ClientLoadCapabilities & ARR_Missing) == 0)
2466 Diag(diag::err_imported_module_not_found)
2467 << F.ModuleName << ImportedBy->FileName;
2468 return Missing;
2469 }
2470
2471 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
2472 if (StoredModMap == nullptr || StoredModMap != M->ModuleMap) {
2473 assert(M->ModuleMap && "found module is missing module map file");
2474 assert(M->Name == F.ModuleName && "found module with different name");
2475 assert(ImportedBy && "top-level import should be verified");
2476 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2477 Diag(diag::err_imported_module_modmap_changed)
2478 << F.ModuleName << ImportedBy->FileName
2479 << M->ModuleMap->getName() << F.ModuleMapPath;
2480 return OutOfDate;
2481 }
2482 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002483
2484 if (Listener)
2485 Listener->ReadModuleMapFile(F.ModuleMapPath);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002486 break;
2487
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002489 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002490 F.InputFilesLoaded.resize(Record[0]);
2491 break;
2492 }
2493 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002494}
2495
Ben Langmuir2c9af442014-04-10 17:57:43 +00002496ASTReader::ASTReadResult
2497ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002498 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002499
2500 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2501 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002502 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 }
2504
2505 // Read all of the records and blocks for the AST file.
2506 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002507 while (1) {
2508 llvm::BitstreamEntry Entry = Stream.advance();
2509
2510 switch (Entry.Kind) {
2511 case llvm::BitstreamEntry::Error:
2512 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002513 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002514 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002515 // Outside of C++, we do not store a lookup map for the translation unit.
2516 // Instead, mark it as needing a lookup map to be built if this module
2517 // contains any declarations lexically within it (which it always does!).
2518 // This usually has no cost, since we very rarely need the lookup map for
2519 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002520 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002521 if (DC->hasExternalLexicalStorage() &&
2522 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002524
Ben Langmuir2c9af442014-04-10 17:57:43 +00002525 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002527 case llvm::BitstreamEntry::SubBlock:
2528 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 case DECLTYPES_BLOCK_ID:
2530 // We lazily load the decls block, but we want to set up the
2531 // DeclsCursor cursor to point into it. Clone our current bitcode
2532 // cursor to it, enter the block and read the abbrevs in that block.
2533 // With the main cursor, we just skip over it.
2534 F.DeclsCursor = Stream;
2535 if (Stream.SkipBlock() || // Skip with the main cursor.
2536 // Read the abbrevs.
2537 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2538 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002539 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002540 }
2541 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002542
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 case PREPROCESSOR_BLOCK_ID:
2544 F.MacroCursor = Stream;
2545 if (!PP.getExternalSource())
2546 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002547
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 if (Stream.SkipBlock() ||
2549 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2550 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002551 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 }
2553 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2554 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002555
Guy Benyei11169dd2012-12-18 14:30:41 +00002556 case PREPROCESSOR_DETAIL_BLOCK_ID:
2557 F.PreprocessorDetailCursor = Stream;
2558 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002559 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002561 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002562 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002563 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002565 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2566
Guy Benyei11169dd2012-12-18 14:30:41 +00002567 if (!PP.getPreprocessingRecord())
2568 PP.createPreprocessingRecord();
2569 if (!PP.getPreprocessingRecord()->getExternalSource())
2570 PP.getPreprocessingRecord()->SetExternalSource(*this);
2571 break;
2572
2573 case SOURCE_MANAGER_BLOCK_ID:
2574 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002575 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002577
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002579 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2580 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002582
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002584 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 if (Stream.SkipBlock() ||
2586 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2587 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002588 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 }
2590 CommentsCursors.push_back(std::make_pair(C, &F));
2591 break;
2592 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002593
Guy Benyei11169dd2012-12-18 14:30:41 +00002594 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002595 if (Stream.SkipBlock()) {
2596 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002597 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002598 }
2599 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 }
2601 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002602
2603 case llvm::BitstreamEntry::Record:
2604 // The interesting case.
2605 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002606 }
2607
2608 // Read and process a record.
2609 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002610 StringRef Blob;
2611 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 default: // Default behavior: ignore.
2613 break;
2614
2615 case TYPE_OFFSET: {
2616 if (F.LocalNumTypes != 0) {
2617 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002618 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002619 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002620 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002621 F.LocalNumTypes = Record[0];
2622 unsigned LocalBaseTypeIndex = Record[1];
2623 F.BaseTypeIndex = getTotalNumTypes();
2624
2625 if (F.LocalNumTypes > 0) {
2626 // Introduce the global -> local mapping for types within this module.
2627 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2628
2629 // Introduce the local -> global mapping for types within this module.
2630 F.TypeRemap.insertOrReplace(
2631 std::make_pair(LocalBaseTypeIndex,
2632 F.BaseTypeIndex - LocalBaseTypeIndex));
2633
2634 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2635 }
2636 break;
2637 }
2638
2639 case DECL_OFFSET: {
2640 if (F.LocalNumDecls != 0) {
2641 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002642 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002644 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002645 F.LocalNumDecls = Record[0];
2646 unsigned LocalBaseDeclID = Record[1];
2647 F.BaseDeclID = getTotalNumDecls();
2648
2649 if (F.LocalNumDecls > 0) {
2650 // Introduce the global -> local mapping for declarations within this
2651 // module.
2652 GlobalDeclMap.insert(
2653 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2654
2655 // Introduce the local -> global mapping for declarations within this
2656 // module.
2657 F.DeclRemap.insertOrReplace(
2658 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2659
2660 // Introduce the global -> local mapping for declarations within this
2661 // module.
2662 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2663
2664 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2665 }
2666 break;
2667 }
2668
2669 case TU_UPDATE_LEXICAL: {
2670 DeclContext *TU = Context.getTranslationUnitDecl();
2671 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002672 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002673 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002674 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 TU->setHasExternalLexicalStorage(true);
2676 break;
2677 }
2678
2679 case UPDATE_VISIBLE: {
2680 unsigned Idx = 0;
2681 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2682 ASTDeclContextNameLookupTable *Table =
Justin Bognerda4e6502014-04-14 16:34:29 +00002683 ASTDeclContextNameLookupTable::Create(
2684 (const unsigned char *)Blob.data() + Record[Idx++],
2685 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2686 (const unsigned char *)Blob.data(),
2687 ASTDeclContextNameLookupTrait(*this, F));
Richard Smithcd45dbc2014-04-19 03:48:30 +00002688 if (Decl *D = GetExistingDecl(ID)) {
Richard Smithd9174792014-03-11 03:10:46 +00002689 auto *DC = cast<DeclContext>(D);
2690 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002691 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00002692 // FIXME: There should never be an existing lookup table.
Richard Smith52e3fba2014-03-11 07:17:35 +00002693 delete LookupTable;
2694 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002695 } else
2696 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2697 break;
2698 }
2699
2700 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002701 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002702 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002703 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2704 (const unsigned char *)F.IdentifierTableData + Record[0],
2705 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2706 (const unsigned char *)F.IdentifierTableData,
2707 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002708
2709 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2710 }
2711 break;
2712
2713 case IDENTIFIER_OFFSET: {
2714 if (F.LocalNumIdentifiers != 0) {
2715 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002716 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002718 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002719 F.LocalNumIdentifiers = Record[0];
2720 unsigned LocalBaseIdentifierID = Record[1];
2721 F.BaseIdentifierID = getTotalNumIdentifiers();
2722
2723 if (F.LocalNumIdentifiers > 0) {
2724 // Introduce the global -> local mapping for identifiers within this
2725 // module.
2726 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2727 &F));
2728
2729 // Introduce the local -> global mapping for identifiers within this
2730 // module.
2731 F.IdentifierRemap.insertOrReplace(
2732 std::make_pair(LocalBaseIdentifierID,
2733 F.BaseIdentifierID - LocalBaseIdentifierID));
2734
2735 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2736 + F.LocalNumIdentifiers);
2737 }
2738 break;
2739 }
2740
Ben Langmuir332aafe2014-01-31 01:06:56 +00002741 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002743 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002744 break;
2745
2746 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002747 if (SpecialTypes.empty()) {
2748 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2749 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2750 break;
2751 }
2752
2753 if (SpecialTypes.size() != Record.size()) {
2754 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002755 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002756 }
2757
2758 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2759 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2760 if (!SpecialTypes[I])
2761 SpecialTypes[I] = ID;
2762 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2763 // merge step?
2764 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 break;
2766
2767 case STATISTICS:
2768 TotalNumStatements += Record[0];
2769 TotalNumMacros += Record[1];
2770 TotalLexicalDeclContexts += Record[2];
2771 TotalVisibleDeclContexts += Record[3];
2772 break;
2773
2774 case UNUSED_FILESCOPED_DECLS:
2775 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2776 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2777 break;
2778
2779 case DELEGATING_CTORS:
2780 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2781 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2782 break;
2783
2784 case WEAK_UNDECLARED_IDENTIFIERS:
2785 if (Record.size() % 4 != 0) {
2786 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002787 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002788 }
2789
2790 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2791 // files. This isn't the way to do it :)
2792 WeakUndeclaredIdentifiers.clear();
2793
2794 // Translate the weak, undeclared identifiers into global IDs.
2795 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2796 WeakUndeclaredIdentifiers.push_back(
2797 getGlobalIdentifierID(F, Record[I++]));
2798 WeakUndeclaredIdentifiers.push_back(
2799 getGlobalIdentifierID(F, Record[I++]));
2800 WeakUndeclaredIdentifiers.push_back(
2801 ReadSourceLocation(F, Record, I).getRawEncoding());
2802 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2803 }
2804 break;
2805
Richard Smith78165b52013-01-10 23:43:47 +00002806 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002808 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 break;
2810
2811 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002812 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 F.LocalNumSelectors = Record[0];
2814 unsigned LocalBaseSelectorID = Record[1];
2815 F.BaseSelectorID = getTotalNumSelectors();
2816
2817 if (F.LocalNumSelectors > 0) {
2818 // Introduce the global -> local mapping for selectors within this
2819 // module.
2820 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2821
2822 // Introduce the local -> global mapping for selectors within this
2823 // module.
2824 F.SelectorRemap.insertOrReplace(
2825 std::make_pair(LocalBaseSelectorID,
2826 F.BaseSelectorID - LocalBaseSelectorID));
2827
2828 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2829 }
2830 break;
2831 }
2832
2833 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002834 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002835 if (Record[0])
2836 F.SelectorLookupTable
2837 = ASTSelectorLookupTable::Create(
2838 F.SelectorLookupTableData + Record[0],
2839 F.SelectorLookupTableData,
2840 ASTSelectorLookupTrait(*this, F));
2841 TotalNumMethodPoolEntries += Record[1];
2842 break;
2843
2844 case REFERENCED_SELECTOR_POOL:
2845 if (!Record.empty()) {
2846 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2847 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2848 Record[Idx++]));
2849 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2850 getRawEncoding());
2851 }
2852 }
2853 break;
2854
2855 case PP_COUNTER_VALUE:
2856 if (!Record.empty() && Listener)
2857 Listener->ReadCounter(F, Record[0]);
2858 break;
2859
2860 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002861 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002862 F.NumFileSortedDecls = Record[0];
2863 break;
2864
2865 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002866 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002867 F.LocalNumSLocEntries = Record[0];
2868 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002869 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2871 SLocSpaceSize);
2872 // Make our entry in the range map. BaseID is negative and growing, so
2873 // we invert it. Because we invert it, though, we need the other end of
2874 // the range.
2875 unsigned RangeStart =
2876 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2877 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2878 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2879
2880 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2881 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2882 GlobalSLocOffsetMap.insert(
2883 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2884 - SLocSpaceSize,&F));
2885
2886 // Initialize the remapping table.
2887 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002888 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002890 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002891 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2892
2893 TotalNumSLocEntries += F.LocalNumSLocEntries;
2894 break;
2895 }
2896
2897 case MODULE_OFFSET_MAP: {
2898 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002899 const unsigned char *Data = (const unsigned char*)Blob.data();
2900 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002901
2902 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2903 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2904 F.SLocRemap.insert(std::make_pair(0U, 0));
2905 F.SLocRemap.insert(std::make_pair(2U, 1));
2906 }
2907
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 // Continuous range maps we may be updating in our module.
2909 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2910 ContinuousRangeMap<uint32_t, int, 2>::Builder
2911 IdentifierRemap(F.IdentifierRemap);
2912 ContinuousRangeMap<uint32_t, int, 2>::Builder
2913 MacroRemap(F.MacroRemap);
2914 ContinuousRangeMap<uint32_t, int, 2>::Builder
2915 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2916 ContinuousRangeMap<uint32_t, int, 2>::Builder
2917 SubmoduleRemap(F.SubmoduleRemap);
2918 ContinuousRangeMap<uint32_t, int, 2>::Builder
2919 SelectorRemap(F.SelectorRemap);
2920 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2921 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2922
2923 while(Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002924 using namespace llvm::support;
2925 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 StringRef Name = StringRef((const char*)Data, Len);
2927 Data += Len;
2928 ModuleFile *OM = ModuleMgr.lookup(Name);
2929 if (!OM) {
2930 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002931 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 }
2933
Justin Bogner57ba0b22014-03-28 22:03:24 +00002934 uint32_t SLocOffset =
2935 endian::readNext<uint32_t, little, unaligned>(Data);
2936 uint32_t IdentifierIDOffset =
2937 endian::readNext<uint32_t, little, unaligned>(Data);
2938 uint32_t MacroIDOffset =
2939 endian::readNext<uint32_t, little, unaligned>(Data);
2940 uint32_t PreprocessedEntityIDOffset =
2941 endian::readNext<uint32_t, little, unaligned>(Data);
2942 uint32_t SubmoduleIDOffset =
2943 endian::readNext<uint32_t, little, unaligned>(Data);
2944 uint32_t SelectorIDOffset =
2945 endian::readNext<uint32_t, little, unaligned>(Data);
2946 uint32_t DeclIDOffset =
2947 endian::readNext<uint32_t, little, unaligned>(Data);
2948 uint32_t TypeIndexOffset =
2949 endian::readNext<uint32_t, little, unaligned>(Data);
2950
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2952 SLocRemap.insert(std::make_pair(SLocOffset,
2953 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2954 IdentifierRemap.insert(
2955 std::make_pair(IdentifierIDOffset,
2956 OM->BaseIdentifierID - IdentifierIDOffset));
2957 MacroRemap.insert(std::make_pair(MacroIDOffset,
2958 OM->BaseMacroID - MacroIDOffset));
2959 PreprocessedEntityRemap.insert(
2960 std::make_pair(PreprocessedEntityIDOffset,
2961 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2962 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2963 OM->BaseSubmoduleID - SubmoduleIDOffset));
2964 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2965 OM->BaseSelectorID - SelectorIDOffset));
2966 DeclRemap.insert(std::make_pair(DeclIDOffset,
2967 OM->BaseDeclID - DeclIDOffset));
2968
2969 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2970 OM->BaseTypeIndex - TypeIndexOffset));
2971
2972 // Global -> local mappings.
2973 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2974 }
2975 break;
2976 }
2977
2978 case SOURCE_MANAGER_LINE_TABLE:
2979 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002980 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 break;
2982
2983 case SOURCE_LOCATION_PRELOADS: {
2984 // Need to transform from the local view (1-based IDs) to the global view,
2985 // which is based off F.SLocEntryBaseID.
2986 if (!F.PreloadSLocEntries.empty()) {
2987 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002988 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 }
2990
2991 F.PreloadSLocEntries.swap(Record);
2992 break;
2993 }
2994
2995 case EXT_VECTOR_DECLS:
2996 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2997 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2998 break;
2999
3000 case VTABLE_USES:
3001 if (Record.size() % 3 != 0) {
3002 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003003 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003004 }
3005
3006 // Later tables overwrite earlier ones.
3007 // FIXME: Modules will have some trouble with this. This is clearly not
3008 // the right way to do this.
3009 VTableUses.clear();
3010
3011 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3012 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3013 VTableUses.push_back(
3014 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3015 VTableUses.push_back(Record[Idx++]);
3016 }
3017 break;
3018
3019 case DYNAMIC_CLASSES:
3020 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3021 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3022 break;
3023
3024 case PENDING_IMPLICIT_INSTANTIATIONS:
3025 if (PendingInstantiations.size() % 2 != 0) {
3026 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003027 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 }
3029
3030 if (Record.size() % 2 != 0) {
3031 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003032 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 }
3034
3035 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3036 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3037 PendingInstantiations.push_back(
3038 ReadSourceLocation(F, Record, I).getRawEncoding());
3039 }
3040 break;
3041
3042 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00003043 if (Record.size() != 2) {
3044 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003045 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003046 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003047 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3048 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3049 break;
3050
3051 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003052 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3053 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3054 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003055
3056 unsigned LocalBasePreprocessedEntityID = Record[0];
3057
3058 unsigned StartingID;
3059 if (!PP.getPreprocessingRecord())
3060 PP.createPreprocessingRecord();
3061 if (!PP.getPreprocessingRecord()->getExternalSource())
3062 PP.getPreprocessingRecord()->SetExternalSource(*this);
3063 StartingID
3064 = PP.getPreprocessingRecord()
3065 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3066 F.BasePreprocessedEntityID = StartingID;
3067
3068 if (F.NumPreprocessedEntities > 0) {
3069 // Introduce the global -> local mapping for preprocessed entities in
3070 // this module.
3071 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3072
3073 // Introduce the local -> global mapping for preprocessed entities in
3074 // this module.
3075 F.PreprocessedEntityRemap.insertOrReplace(
3076 std::make_pair(LocalBasePreprocessedEntityID,
3077 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3078 }
3079
3080 break;
3081 }
3082
3083 case DECL_UPDATE_OFFSETS: {
3084 if (Record.size() % 2 != 0) {
3085 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003086 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003088 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3089 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3090 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3091
3092 // If we've already loaded the decl, perform the updates when we finish
3093 // loading this block.
3094 if (Decl *D = GetExistingDecl(ID))
3095 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3096 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 break;
3098 }
3099
3100 case DECL_REPLACEMENTS: {
3101 if (Record.size() % 3 != 0) {
3102 Error("invalid DECL_REPLACEMENTS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003103 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 }
3105 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3106 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3107 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3108 break;
3109 }
3110
3111 case OBJC_CATEGORIES_MAP: {
3112 if (F.LocalNumObjCCategoriesInMap != 0) {
3113 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003114 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 }
3116
3117 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003118 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003119 break;
3120 }
3121
3122 case OBJC_CATEGORIES:
3123 F.ObjCCategories.swap(Record);
3124 break;
3125
3126 case CXX_BASE_SPECIFIER_OFFSETS: {
3127 if (F.LocalNumCXXBaseSpecifiers != 0) {
3128 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003129 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 }
3131
3132 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003133 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003134 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3135 break;
3136 }
3137
3138 case DIAG_PRAGMA_MAPPINGS:
3139 if (F.PragmaDiagMappings.empty())
3140 F.PragmaDiagMappings.swap(Record);
3141 else
3142 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3143 Record.begin(), Record.end());
3144 break;
3145
3146 case CUDA_SPECIAL_DECL_REFS:
3147 // Later tables overwrite earlier ones.
3148 // FIXME: Modules will have trouble with this.
3149 CUDASpecialDeclRefs.clear();
3150 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3151 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3152 break;
3153
3154 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003155 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003156 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 if (Record[0]) {
3158 F.HeaderFileInfoTable
3159 = HeaderFileInfoLookupTable::Create(
3160 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3161 (const unsigned char *)F.HeaderFileInfoTableData,
3162 HeaderFileInfoTrait(*this, F,
3163 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003164 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003165
3166 PP.getHeaderSearchInfo().SetExternalSource(this);
3167 if (!PP.getHeaderSearchInfo().getExternalLookup())
3168 PP.getHeaderSearchInfo().SetExternalLookup(this);
3169 }
3170 break;
3171 }
3172
3173 case FP_PRAGMA_OPTIONS:
3174 // Later tables overwrite earlier ones.
3175 FPPragmaOptions.swap(Record);
3176 break;
3177
3178 case OPENCL_EXTENSIONS:
3179 // Later tables overwrite earlier ones.
3180 OpenCLExtensions.swap(Record);
3181 break;
3182
3183 case TENTATIVE_DEFINITIONS:
3184 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3185 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3186 break;
3187
3188 case KNOWN_NAMESPACES:
3189 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3190 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3191 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003192
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003193 case UNDEFINED_BUT_USED:
3194 if (UndefinedButUsed.size() % 2 != 0) {
3195 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003196 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003197 }
3198
3199 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003200 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003201 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003202 }
3203 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003204 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3205 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003206 ReadSourceLocation(F, Record, I).getRawEncoding());
3207 }
3208 break;
3209
Guy Benyei11169dd2012-12-18 14:30:41 +00003210 case IMPORTED_MODULES: {
3211 if (F.Kind != MK_Module) {
3212 // If we aren't loading a module (which has its own exports), make
3213 // all of the imported modules visible.
3214 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003215 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3216 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3217 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3218 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003219 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 }
3221 }
3222 break;
3223 }
3224
3225 case LOCAL_REDECLARATIONS: {
3226 F.RedeclarationChains.swap(Record);
3227 break;
3228 }
3229
3230 case LOCAL_REDECLARATIONS_MAP: {
3231 if (F.LocalNumRedeclarationsInMap != 0) {
3232 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003233 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 }
3235
3236 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003237 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003238 break;
3239 }
3240
3241 case MERGED_DECLARATIONS: {
3242 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3243 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3244 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3245 for (unsigned N = Record[Idx++]; N > 0; --N)
3246 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3247 }
3248 break;
3249 }
3250
3251 case MACRO_OFFSET: {
3252 if (F.LocalNumMacros != 0) {
3253 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003254 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003255 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003256 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003257 F.LocalNumMacros = Record[0];
3258 unsigned LocalBaseMacroID = Record[1];
3259 F.BaseMacroID = getTotalNumMacros();
3260
3261 if (F.LocalNumMacros > 0) {
3262 // Introduce the global -> local mapping for macros within this module.
3263 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3264
3265 // Introduce the local -> global mapping for macros within this module.
3266 F.MacroRemap.insertOrReplace(
3267 std::make_pair(LocalBaseMacroID,
3268 F.BaseMacroID - LocalBaseMacroID));
3269
3270 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3271 }
3272 break;
3273 }
3274
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003275 case MACRO_TABLE: {
3276 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 break;
3278 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00003279
3280 case LATE_PARSED_TEMPLATE: {
3281 LateParsedTemplates.append(Record.begin(), Record.end());
3282 break;
3283 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003284
3285 case OPTIMIZE_PRAGMA_OPTIONS:
3286 if (Record.size() != 1) {
3287 Error("invalid pragma optimize record");
3288 return Failure;
3289 }
3290 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3291 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003292 }
3293 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003294}
3295
Douglas Gregorc1489562013-02-12 23:36:21 +00003296/// \brief Move the given method to the back of the global list of methods.
3297static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3298 // Find the entry for this selector in the method pool.
3299 Sema::GlobalMethodPool::iterator Known
3300 = S.MethodPool.find(Method->getSelector());
3301 if (Known == S.MethodPool.end())
3302 return;
3303
3304 // Retrieve the appropriate method list.
3305 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3306 : Known->second.second;
3307 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003308 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003309 if (!Found) {
3310 if (List->Method == Method) {
3311 Found = true;
3312 } else {
3313 // Keep searching.
3314 continue;
3315 }
3316 }
3317
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003318 if (List->getNext())
3319 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00003320 else
3321 List->Method = Method;
3322 }
3323}
3324
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003325void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00003326 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3327 Decl *D = Names.HiddenDecls[I];
3328 bool wasHidden = D->Hidden;
3329 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003330
Richard Smith49f906a2014-03-01 00:08:04 +00003331 if (wasHidden && SemaObj) {
3332 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3333 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003334 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003335 }
3336 }
Richard Smith49f906a2014-03-01 00:08:04 +00003337
3338 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3339 E = Names.HiddenMacros.end();
3340 I != E; ++I)
3341 installImportedMacro(I->first, I->second, Owner);
Guy Benyei11169dd2012-12-18 14:30:41 +00003342}
3343
Richard Smith49f906a2014-03-01 00:08:04 +00003344void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003345 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003346 SourceLocation ImportLoc,
3347 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003348 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003349 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003350 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003351 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003352 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003353
3354 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003355 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 // there is nothing more to do.
3357 continue;
3358 }
Richard Smith49f906a2014-03-01 00:08:04 +00003359
Guy Benyei11169dd2012-12-18 14:30:41 +00003360 if (!Mod->isAvailable()) {
3361 // Modules that aren't available cannot be made visible.
3362 continue;
3363 }
3364
3365 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003366 if (NameVisibility >= Module::MacrosVisible &&
3367 Mod->NameVisibility < Module::MacrosVisible)
3368 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003369 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003370
Guy Benyei11169dd2012-12-18 14:30:41 +00003371 // If we've already deserialized any names from this module,
3372 // mark them as visible.
3373 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3374 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003375 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 HiddenNamesMap.erase(Hidden);
3377 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003378
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003380 SmallVector<Module *, 16> Exports;
3381 Mod->getExportedModules(Exports);
3382 for (SmallVectorImpl<Module *>::iterator
3383 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3384 Module *Exported = *I;
3385 if (Visited.insert(Exported))
3386 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003387 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003388
3389 // Detect any conflicts.
3390 if (Complain) {
3391 assert(ImportLoc.isValid() && "Missing import location");
3392 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3393 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3394 Diag(ImportLoc, diag::warn_module_conflict)
3395 << Mod->getFullModuleName()
3396 << Mod->Conflicts[I].Other->getFullModuleName()
3397 << Mod->Conflicts[I].Message;
3398 // FIXME: Need note where the other module was imported.
3399 }
3400 }
3401 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003402 }
3403}
3404
Douglas Gregore060e572013-01-25 01:03:03 +00003405bool ASTReader::loadGlobalIndex() {
3406 if (GlobalIndex)
3407 return false;
3408
3409 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3410 !Context.getLangOpts().Modules)
3411 return true;
3412
3413 // Try to load the global index.
3414 TriedLoadingGlobalIndex = true;
3415 StringRef ModuleCachePath
3416 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3417 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003418 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003419 if (!Result.first)
3420 return true;
3421
3422 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003423 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003424 return false;
3425}
3426
3427bool ASTReader::isGlobalIndexUnavailable() const {
3428 return Context.getLangOpts().Modules && UseGlobalIndex &&
3429 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3430}
3431
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003432static void updateModuleTimestamp(ModuleFile &MF) {
3433 // Overwrite the timestamp file contents so that file's mtime changes.
3434 std::string TimestampFilename = MF.getTimestampFilename();
3435 std::string ErrorInfo;
Rafael Espindola04a13be2014-02-24 15:06:52 +00003436 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
Rafael Espindola4fbd3732014-02-24 18:20:21 +00003437 llvm::sys::fs::F_Text);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003438 if (!ErrorInfo.empty())
3439 return;
3440 OS << "Timestamp file\n";
3441}
3442
Guy Benyei11169dd2012-12-18 14:30:41 +00003443ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3444 ModuleKind Type,
3445 SourceLocation ImportLoc,
3446 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003447 llvm::SaveAndRestore<SourceLocation>
3448 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3449
Richard Smithd1c46742014-04-30 02:24:17 +00003450 // Defer any pending actions until we get to the end of reading the AST file.
3451 Deserializing AnASTFile(this);
3452
Guy Benyei11169dd2012-12-18 14:30:41 +00003453 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003454 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003455
3456 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003457 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003459 /*ImportedBy=*/nullptr, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003460 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 ClientLoadCapabilities)) {
3462 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003463 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003464 case OutOfDate:
3465 case VersionMismatch:
3466 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003467 case HadErrors: {
3468 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3469 for (const ImportedModule &IM : Loaded)
3470 LoadedSet.insert(IM.Mod);
3471
Douglas Gregor7029ce12013-03-19 00:28:20 +00003472 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003473 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003474 Context.getLangOpts().Modules
3475 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003476 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003477
3478 // If we find that any modules are unusable, the global index is going
3479 // to be out-of-date. Just remove it.
3480 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003481 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003482 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003483 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003484 case Success:
3485 break;
3486 }
3487
3488 // Here comes stuff that we only do once the entire chain is loaded.
3489
3490 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003491 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3492 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003493 M != MEnd; ++M) {
3494 ModuleFile &F = *M->Mod;
3495
3496 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003497 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3498 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003499
3500 // Once read, set the ModuleFile bit base offset and update the size in
3501 // bits of all files we've seen.
3502 F.GlobalBitOffset = TotalModulesSizeInBits;
3503 TotalModulesSizeInBits += F.SizeInBits;
3504 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3505
3506 // Preload SLocEntries.
3507 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3508 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3509 // Load it through the SourceManager and don't call ReadSLocEntry()
3510 // directly because the entry may have already been loaded in which case
3511 // calling ReadSLocEntry() directly would trigger an assertion in
3512 // SourceManager.
3513 SourceMgr.getLoadedSLocEntryByID(Index);
3514 }
3515 }
3516
Douglas Gregor603cd862013-03-22 18:50:14 +00003517 // Setup the import locations and notify the module manager that we've
3518 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003519 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3520 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003521 M != MEnd; ++M) {
3522 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003523
3524 ModuleMgr.moduleFileAccepted(&F);
3525
3526 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003527 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003528 if (!M->ImportedBy)
3529 F.ImportLoc = M->ImportLoc;
3530 else
3531 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3532 M->ImportLoc.getRawEncoding());
3533 }
3534
3535 // Mark all of the identifiers in the identifier table as being out of date,
3536 // so that various accessors know to check the loaded modules when the
3537 // identifier is used.
3538 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3539 IdEnd = PP.getIdentifierTable().end();
3540 Id != IdEnd; ++Id)
3541 Id->second->setOutOfDate(true);
3542
3543 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003544 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3545 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003546 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3547 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003548
3549 switch (Unresolved.Kind) {
3550 case UnresolvedModuleRef::Conflict:
3551 if (ResolvedMod) {
3552 Module::Conflict Conflict;
3553 Conflict.Other = ResolvedMod;
3554 Conflict.Message = Unresolved.String.str();
3555 Unresolved.Mod->Conflicts.push_back(Conflict);
3556 }
3557 continue;
3558
3559 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 if (ResolvedMod)
3561 Unresolved.Mod->Imports.push_back(ResolvedMod);
3562 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003563
Douglas Gregorfb912652013-03-20 21:10:35 +00003564 case UnresolvedModuleRef::Export:
3565 if (ResolvedMod || Unresolved.IsWildcard)
3566 Unresolved.Mod->Exports.push_back(
3567 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3568 continue;
3569 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003570 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003571 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003572
3573 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3574 // Might be unnecessary as use declarations are only used to build the
3575 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003576
3577 InitializeContext();
3578
Richard Smith3d8e97e2013-10-18 06:54:39 +00003579 if (SemaObj)
3580 UpdateSema();
3581
Guy Benyei11169dd2012-12-18 14:30:41 +00003582 if (DeserializationListener)
3583 DeserializationListener->ReaderInitialized(this);
3584
3585 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3586 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3587 PrimaryModule.OriginalSourceFileID
3588 = FileID::get(PrimaryModule.SLocEntryBaseID
3589 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3590
3591 // If this AST file is a precompiled preamble, then set the
3592 // preamble file ID of the source manager to the file source file
3593 // from which the preamble was built.
3594 if (Type == MK_Preamble) {
3595 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3596 } else if (Type == MK_MainFile) {
3597 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3598 }
3599 }
3600
3601 // For any Objective-C class definitions we have already loaded, make sure
3602 // that we load any additional categories.
3603 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3604 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3605 ObjCClassesLoaded[I],
3606 PreviousGeneration);
3607 }
Douglas Gregore060e572013-01-25 01:03:03 +00003608
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003609 if (PP.getHeaderSearchInfo()
3610 .getHeaderSearchOpts()
3611 .ModulesValidateOncePerBuildSession) {
3612 // Now we are certain that the module and all modules it depends on are
3613 // up to date. Create or update timestamp files for modules that are
3614 // located in the module cache (not for PCH files that could be anywhere
3615 // in the filesystem).
3616 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3617 ImportedModule &M = Loaded[I];
3618 if (M.Mod->Kind == MK_Module) {
3619 updateModuleTimestamp(*M.Mod);
3620 }
3621 }
3622 }
3623
Guy Benyei11169dd2012-12-18 14:30:41 +00003624 return Success;
3625}
3626
3627ASTReader::ASTReadResult
3628ASTReader::ReadASTCore(StringRef FileName,
3629 ModuleKind Type,
3630 SourceLocation ImportLoc,
3631 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003632 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003633 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00003634 unsigned ClientLoadCapabilities) {
3635 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003636 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003637 ModuleManager::AddModuleResult AddResult
3638 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003639 getGeneration(), ExpectedSize, ExpectedModTime,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003640 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003641
Douglas Gregor7029ce12013-03-19 00:28:20 +00003642 switch (AddResult) {
3643 case ModuleManager::AlreadyLoaded:
3644 return Success;
3645
3646 case ModuleManager::NewlyLoaded:
3647 // Load module file below.
3648 break;
3649
3650 case ModuleManager::Missing:
3651 // The module file was missing; if the client handle handle, that, return
3652 // it.
3653 if (ClientLoadCapabilities & ARR_Missing)
3654 return Missing;
3655
3656 // Otherwise, return an error.
3657 {
3658 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3659 + ErrorStr;
3660 Error(Msg);
3661 }
3662 return Failure;
3663
3664 case ModuleManager::OutOfDate:
3665 // We couldn't load the module file because it is out-of-date. If the
3666 // client can handle out-of-date, return it.
3667 if (ClientLoadCapabilities & ARR_OutOfDate)
3668 return OutOfDate;
3669
3670 // Otherwise, return an error.
3671 {
3672 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3673 + ErrorStr;
3674 Error(Msg);
3675 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003676 return Failure;
3677 }
3678
Douglas Gregor7029ce12013-03-19 00:28:20 +00003679 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003680
3681 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3682 // module?
3683 if (FileName != "-") {
3684 CurrentDir = llvm::sys::path::parent_path(FileName);
3685 if (CurrentDir.empty()) CurrentDir = ".";
3686 }
3687
3688 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003689 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003690 Stream.init(F.StreamFile);
3691 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3692
3693 // Sniff for the signature.
3694 if (Stream.Read(8) != 'C' ||
3695 Stream.Read(8) != 'P' ||
3696 Stream.Read(8) != 'C' ||
3697 Stream.Read(8) != 'H') {
3698 Diag(diag::err_not_a_pch_file) << FileName;
3699 return Failure;
3700 }
3701
3702 // This is used for compatibility with older PCH formats.
3703 bool HaveReadControlBlock = false;
3704
Chris Lattnerefa77172013-01-20 00:00:22 +00003705 while (1) {
3706 llvm::BitstreamEntry Entry = Stream.advance();
3707
3708 switch (Entry.Kind) {
3709 case llvm::BitstreamEntry::Error:
3710 case llvm::BitstreamEntry::EndBlock:
3711 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003712 Error("invalid record at top-level of AST file");
3713 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003714
3715 case llvm::BitstreamEntry::SubBlock:
3716 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003717 }
3718
Guy Benyei11169dd2012-12-18 14:30:41 +00003719 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003720 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003721 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3722 if (Stream.ReadBlockInfoBlock()) {
3723 Error("malformed BlockInfoBlock in AST file");
3724 return Failure;
3725 }
3726 break;
3727 case CONTROL_BLOCK_ID:
3728 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003729 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003730 case Success:
3731 break;
3732
3733 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003734 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003735 case OutOfDate: return OutOfDate;
3736 case VersionMismatch: return VersionMismatch;
3737 case ConfigurationMismatch: return ConfigurationMismatch;
3738 case HadErrors: return HadErrors;
3739 }
3740 break;
3741 case AST_BLOCK_ID:
3742 if (!HaveReadControlBlock) {
3743 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003744 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003745 return VersionMismatch;
3746 }
3747
3748 // Record that we've loaded this module.
3749 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3750 return Success;
3751
3752 default:
3753 if (Stream.SkipBlock()) {
3754 Error("malformed block record in AST file");
3755 return Failure;
3756 }
3757 break;
3758 }
3759 }
3760
3761 return Success;
3762}
3763
3764void ASTReader::InitializeContext() {
3765 // If there's a listener, notify them that we "read" the translation unit.
3766 if (DeserializationListener)
3767 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3768 Context.getTranslationUnitDecl());
3769
Guy Benyei11169dd2012-12-18 14:30:41 +00003770 // FIXME: Find a better way to deal with collisions between these
3771 // built-in types. Right now, we just ignore the problem.
3772
3773 // Load the special types.
3774 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3775 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3776 if (!Context.CFConstantStringTypeDecl)
3777 Context.setCFConstantStringType(GetType(String));
3778 }
3779
3780 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3781 QualType FileType = GetType(File);
3782 if (FileType.isNull()) {
3783 Error("FILE type is NULL");
3784 return;
3785 }
3786
3787 if (!Context.FILEDecl) {
3788 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3789 Context.setFILEDecl(Typedef->getDecl());
3790 else {
3791 const TagType *Tag = FileType->getAs<TagType>();
3792 if (!Tag) {
3793 Error("Invalid FILE type in AST file");
3794 return;
3795 }
3796 Context.setFILEDecl(Tag->getDecl());
3797 }
3798 }
3799 }
3800
3801 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3802 QualType Jmp_bufType = GetType(Jmp_buf);
3803 if (Jmp_bufType.isNull()) {
3804 Error("jmp_buf type is NULL");
3805 return;
3806 }
3807
3808 if (!Context.jmp_bufDecl) {
3809 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3810 Context.setjmp_bufDecl(Typedef->getDecl());
3811 else {
3812 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3813 if (!Tag) {
3814 Error("Invalid jmp_buf type in AST file");
3815 return;
3816 }
3817 Context.setjmp_bufDecl(Tag->getDecl());
3818 }
3819 }
3820 }
3821
3822 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3823 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3824 if (Sigjmp_bufType.isNull()) {
3825 Error("sigjmp_buf type is NULL");
3826 return;
3827 }
3828
3829 if (!Context.sigjmp_bufDecl) {
3830 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3831 Context.setsigjmp_bufDecl(Typedef->getDecl());
3832 else {
3833 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3834 assert(Tag && "Invalid sigjmp_buf type in AST file");
3835 Context.setsigjmp_bufDecl(Tag->getDecl());
3836 }
3837 }
3838 }
3839
3840 if (unsigned ObjCIdRedef
3841 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3842 if (Context.ObjCIdRedefinitionType.isNull())
3843 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3844 }
3845
3846 if (unsigned ObjCClassRedef
3847 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3848 if (Context.ObjCClassRedefinitionType.isNull())
3849 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3850 }
3851
3852 if (unsigned ObjCSelRedef
3853 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3854 if (Context.ObjCSelRedefinitionType.isNull())
3855 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3856 }
3857
3858 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3859 QualType Ucontext_tType = GetType(Ucontext_t);
3860 if (Ucontext_tType.isNull()) {
3861 Error("ucontext_t type is NULL");
3862 return;
3863 }
3864
3865 if (!Context.ucontext_tDecl) {
3866 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3867 Context.setucontext_tDecl(Typedef->getDecl());
3868 else {
3869 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3870 assert(Tag && "Invalid ucontext_t type in AST file");
3871 Context.setucontext_tDecl(Tag->getDecl());
3872 }
3873 }
3874 }
3875 }
3876
3877 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3878
3879 // If there were any CUDA special declarations, deserialize them.
3880 if (!CUDASpecialDeclRefs.empty()) {
3881 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3882 Context.setcudaConfigureCallDecl(
3883 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3884 }
Richard Smith56be7542014-03-21 00:33:59 +00003885
Guy Benyei11169dd2012-12-18 14:30:41 +00003886 // Re-export any modules that were imported by a non-module AST file.
Richard Smith56be7542014-03-21 00:33:59 +00003887 // FIXME: This does not make macro-only imports visible again. It also doesn't
3888 // make #includes mapped to module imports visible.
3889 for (auto &Import : ImportedModules) {
3890 if (Module *Imported = getSubmodule(Import.ID))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003891 makeModuleVisible(Imported, Module::AllVisible,
Richard Smith56be7542014-03-21 00:33:59 +00003892 /*ImportLoc=*/Import.ImportLoc,
Douglas Gregorfb912652013-03-20 21:10:35 +00003893 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003894 }
3895 ImportedModules.clear();
3896}
3897
3898void ASTReader::finalizeForWriting() {
3899 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3900 HiddenEnd = HiddenNamesMap.end();
3901 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003902 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003903 }
3904 HiddenNamesMap.clear();
3905}
3906
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003907/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3908/// cursor into the start of the given block ID, returning false on success and
3909/// true on failure.
3910static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003911 while (1) {
3912 llvm::BitstreamEntry Entry = Cursor.advance();
3913 switch (Entry.Kind) {
3914 case llvm::BitstreamEntry::Error:
3915 case llvm::BitstreamEntry::EndBlock:
3916 return true;
3917
3918 case llvm::BitstreamEntry::Record:
3919 // Ignore top-level records.
3920 Cursor.skipRecord(Entry.ID);
3921 break;
3922
3923 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003924 if (Entry.ID == BlockID) {
3925 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003926 return true;
3927 // Found it!
3928 return false;
3929 }
3930
3931 if (Cursor.SkipBlock())
3932 return true;
3933 }
3934 }
3935}
3936
Guy Benyei11169dd2012-12-18 14:30:41 +00003937/// \brief Retrieve the name of the original source file name
3938/// directly from the AST file, without actually loading the AST
3939/// file.
3940std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3941 FileManager &FileMgr,
3942 DiagnosticsEngine &Diags) {
3943 // Open the AST file.
3944 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003945 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003946 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3947 if (!Buffer) {
3948 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3949 return std::string();
3950 }
3951
3952 // Initialize the stream
3953 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003954 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003955 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3956 (const unsigned char *)Buffer->getBufferEnd());
3957 Stream.init(StreamFile);
3958
3959 // Sniff for the signature.
3960 if (Stream.Read(8) != 'C' ||
3961 Stream.Read(8) != 'P' ||
3962 Stream.Read(8) != 'C' ||
3963 Stream.Read(8) != 'H') {
3964 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3965 return std::string();
3966 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003967
Chris Lattnere7b154b2013-01-19 21:39:22 +00003968 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003969 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003970 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3971 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003972 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003973
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003974 // Scan for ORIGINAL_FILE inside the control block.
3975 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003976 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003977 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003978 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3979 return std::string();
3980
3981 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3982 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3983 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003984 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003985
Guy Benyei11169dd2012-12-18 14:30:41 +00003986 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003987 StringRef Blob;
3988 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3989 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003990 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003991}
3992
3993namespace {
3994 class SimplePCHValidator : public ASTReaderListener {
3995 const LangOptions &ExistingLangOpts;
3996 const TargetOptions &ExistingTargetOpts;
3997 const PreprocessorOptions &ExistingPPOpts;
3998 FileManager &FileMgr;
3999
4000 public:
4001 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4002 const TargetOptions &ExistingTargetOpts,
4003 const PreprocessorOptions &ExistingPPOpts,
4004 FileManager &FileMgr)
4005 : ExistingLangOpts(ExistingLangOpts),
4006 ExistingTargetOpts(ExistingTargetOpts),
4007 ExistingPPOpts(ExistingPPOpts),
4008 FileMgr(FileMgr)
4009 {
4010 }
4011
Craig Topper3e89dfe2014-03-13 02:13:41 +00004012 bool ReadLanguageOptions(const LangOptions &LangOpts,
4013 bool Complain) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004014 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004015 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004016 bool ReadTargetOptions(const TargetOptions &TargetOpts,
4017 bool Complain) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004018 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004019 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004020 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4021 bool Complain,
4022 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004023 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004024 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004025 }
4026 };
4027}
4028
4029bool ASTReader::readASTFileControlBlock(StringRef Filename,
4030 FileManager &FileMgr,
4031 ASTReaderListener &Listener) {
4032 // Open the AST file.
4033 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00004034 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00004035 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
4036 if (!Buffer) {
4037 return true;
4038 }
4039
4040 // Initialize the stream
4041 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004042 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00004043 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
4044 (const unsigned char *)Buffer->getBufferEnd());
4045 Stream.init(StreamFile);
4046
4047 // Sniff for the signature.
4048 if (Stream.Read(8) != 'C' ||
4049 Stream.Read(8) != 'P' ||
4050 Stream.Read(8) != 'C' ||
4051 Stream.Read(8) != 'H') {
4052 return true;
4053 }
4054
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004055 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004056 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004057 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004058
4059 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004060 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004061 BitstreamCursor InputFilesCursor;
4062 if (NeedsInputFiles) {
4063 InputFilesCursor = Stream;
4064 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4065 return true;
4066
4067 // Read the abbreviations
4068 while (true) {
4069 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4070 unsigned Code = InputFilesCursor.ReadCode();
4071
4072 // We expect all abbrevs to be at the start of the block.
4073 if (Code != llvm::bitc::DEFINE_ABBREV) {
4074 InputFilesCursor.JumpToBit(Offset);
4075 break;
4076 }
4077 InputFilesCursor.ReadAbbrevRecord();
4078 }
4079 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004080
4081 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00004082 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004083 while (1) {
4084 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4085 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4086 return false;
4087
4088 if (Entry.Kind != llvm::BitstreamEntry::Record)
4089 return true;
4090
Guy Benyei11169dd2012-12-18 14:30:41 +00004091 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004092 StringRef Blob;
4093 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004094 switch ((ControlRecordTypes)RecCode) {
4095 case METADATA: {
4096 if (Record[0] != VERSION_MAJOR)
4097 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004098
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004099 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004100 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004101
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004102 break;
4103 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004104 case MODULE_NAME:
4105 Listener.ReadModuleName(Blob);
4106 break;
4107 case MODULE_MAP_FILE:
4108 Listener.ReadModuleMapFile(Blob);
4109 break;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004110 case LANGUAGE_OPTIONS:
4111 if (ParseLanguageOptions(Record, false, Listener))
4112 return true;
4113 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004114
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004115 case TARGET_OPTIONS:
4116 if (ParseTargetOptions(Record, false, Listener))
4117 return true;
4118 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004119
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004120 case DIAGNOSTIC_OPTIONS:
4121 if (ParseDiagnosticOptions(Record, false, Listener))
4122 return true;
4123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004124
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004125 case FILE_SYSTEM_OPTIONS:
4126 if (ParseFileSystemOptions(Record, false, Listener))
4127 return true;
4128 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004129
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004130 case HEADER_SEARCH_OPTIONS:
4131 if (ParseHeaderSearchOptions(Record, false, Listener))
4132 return true;
4133 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004134
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004135 case PREPROCESSOR_OPTIONS: {
4136 std::string IgnoredSuggestedPredefines;
4137 if (ParsePreprocessorOptions(Record, false, Listener,
4138 IgnoredSuggestedPredefines))
4139 return true;
4140 break;
4141 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004142
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004143 case INPUT_FILE_OFFSETS: {
4144 if (!NeedsInputFiles)
4145 break;
4146
4147 unsigned NumInputFiles = Record[0];
4148 unsigned NumUserFiles = Record[1];
4149 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4150 for (unsigned I = 0; I != NumInputFiles; ++I) {
4151 // Go find this input file.
4152 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004153
4154 if (isSystemFile && !NeedsSystemInputFiles)
4155 break; // the rest are system input files
4156
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004157 BitstreamCursor &Cursor = InputFilesCursor;
4158 SavedStreamPosition SavedPosition(Cursor);
4159 Cursor.JumpToBit(InputFileOffs[I]);
4160
4161 unsigned Code = Cursor.ReadCode();
4162 RecordData Record;
4163 StringRef Blob;
4164 bool shouldContinue = false;
4165 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4166 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004167 bool Overridden = static_cast<bool>(Record[3]);
4168 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004169 break;
4170 }
4171 if (!shouldContinue)
4172 break;
4173 }
4174 break;
4175 }
4176
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004177 default:
4178 // No other validation to perform.
4179 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004180 }
4181 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004182}
4183
4184
4185bool ASTReader::isAcceptableASTFile(StringRef Filename,
4186 FileManager &FileMgr,
4187 const LangOptions &LangOpts,
4188 const TargetOptions &TargetOpts,
4189 const PreprocessorOptions &PPOpts) {
4190 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4191 return !readASTFileControlBlock(Filename, FileMgr, validator);
4192}
4193
Ben Langmuir2c9af442014-04-10 17:57:43 +00004194ASTReader::ASTReadResult
4195ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004196 // Enter the submodule block.
4197 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4198 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004199 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004200 }
4201
4202 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4203 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004204 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004205 RecordData Record;
4206 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004207 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4208
4209 switch (Entry.Kind) {
4210 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4211 case llvm::BitstreamEntry::Error:
4212 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004213 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004214 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004215 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004216 case llvm::BitstreamEntry::Record:
4217 // The interesting case.
4218 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004219 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004220
Guy Benyei11169dd2012-12-18 14:30:41 +00004221 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004222 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004223 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004224 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004225 default: // Default behavior: ignore.
4226 break;
4227
4228 case SUBMODULE_DEFINITION: {
4229 if (First) {
4230 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004231 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004232 }
4233
Douglas Gregor8d932422013-03-20 03:59:18 +00004234 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004235 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004236 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004237 }
4238
Chris Lattner0e6c9402013-01-20 02:38:54 +00004239 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004240 unsigned Idx = 0;
4241 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4242 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4243 bool IsFramework = Record[Idx++];
4244 bool IsExplicit = Record[Idx++];
4245 bool IsSystem = Record[Idx++];
4246 bool IsExternC = Record[Idx++];
4247 bool InferSubmodules = Record[Idx++];
4248 bool InferExplicitSubmodules = Record[Idx++];
4249 bool InferExportWildcard = Record[Idx++];
4250 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004251
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004252 Module *ParentModule = nullptr;
4253 const FileEntry *ModuleMap = nullptr;
4254 if (Parent) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004255 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004256 ModuleMap = ParentModule->ModuleMap;
4257 }
4258
4259 if (!F.ModuleMapPath.empty())
4260 ModuleMap = FileMgr.getFile(F.ModuleMapPath);
4261
Guy Benyei11169dd2012-12-18 14:30:41 +00004262 // Retrieve this (sub)module from the module map, creating it if
4263 // necessary.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004264 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, ModuleMap,
Guy Benyei11169dd2012-12-18 14:30:41 +00004265 IsFramework,
4266 IsExplicit).first;
4267 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4268 if (GlobalIndex >= SubmodulesLoaded.size() ||
4269 SubmodulesLoaded[GlobalIndex]) {
4270 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004271 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004272 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004273
Douglas Gregor7029ce12013-03-19 00:28:20 +00004274 if (!ParentModule) {
4275 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4276 if (CurFile != F.File) {
4277 if (!Diags.isDiagnosticInFlight()) {
4278 Diag(diag::err_module_file_conflict)
4279 << CurrentModule->getTopLevelModuleName()
4280 << CurFile->getName()
4281 << F.File->getName();
4282 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004283 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004284 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004285 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004286
4287 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004288 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004289
Guy Benyei11169dd2012-12-18 14:30:41 +00004290 CurrentModule->IsFromModuleFile = true;
4291 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004292 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004293 CurrentModule->InferSubmodules = InferSubmodules;
4294 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4295 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004296 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004297 if (DeserializationListener)
4298 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4299
4300 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004301
Douglas Gregorfb912652013-03-20 21:10:35 +00004302 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004303 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004304 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004305 CurrentModule->UnresolvedConflicts.clear();
4306 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004307 break;
4308 }
4309
4310 case SUBMODULE_UMBRELLA_HEADER: {
4311 if (First) {
4312 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004313 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004314 }
4315
4316 if (!CurrentModule)
4317 break;
4318
Chris Lattner0e6c9402013-01-20 02:38:54 +00004319 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004320 if (!CurrentModule->getUmbrellaHeader())
4321 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4322 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004323 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4324 Error("mismatched umbrella headers in submodule");
4325 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004326 }
4327 }
4328 break;
4329 }
4330
4331 case SUBMODULE_HEADER: {
4332 if (First) {
4333 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004334 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004335 }
4336
4337 if (!CurrentModule)
4338 break;
4339
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004340 // We lazily associate headers with their modules via the HeaderInfoTable.
4341 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4342 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004343 break;
4344 }
4345
4346 case SUBMODULE_EXCLUDED_HEADER: {
4347 if (First) {
4348 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004349 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004350 }
4351
4352 if (!CurrentModule)
4353 break;
4354
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004355 // We lazily associate headers with their modules via the HeaderInfoTable.
4356 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4357 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004358 break;
4359 }
4360
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004361 case SUBMODULE_PRIVATE_HEADER: {
4362 if (First) {
4363 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004364 return Failure;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004365 }
4366
4367 if (!CurrentModule)
4368 break;
4369
4370 // We lazily associate headers with their modules via the HeaderInfoTable.
4371 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4372 // of complete filenames or remove it entirely.
4373 break;
4374 }
4375
Guy Benyei11169dd2012-12-18 14:30:41 +00004376 case SUBMODULE_TOPHEADER: {
4377 if (First) {
4378 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004379 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004380 }
4381
4382 if (!CurrentModule)
4383 break;
4384
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004385 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004386 break;
4387 }
4388
4389 case SUBMODULE_UMBRELLA_DIR: {
4390 if (First) {
4391 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004392 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004393 }
4394
4395 if (!CurrentModule)
4396 break;
4397
Guy Benyei11169dd2012-12-18 14:30:41 +00004398 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004399 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004400 if (!CurrentModule->getUmbrellaDir())
4401 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4402 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004403 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4404 Error("mismatched umbrella directories in submodule");
4405 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004406 }
4407 }
4408 break;
4409 }
4410
4411 case SUBMODULE_METADATA: {
4412 if (!First) {
4413 Error("submodule metadata record not at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004414 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004415 }
4416 First = false;
4417
4418 F.BaseSubmoduleID = getTotalNumSubmodules();
4419 F.LocalNumSubmodules = Record[0];
4420 unsigned LocalBaseSubmoduleID = Record[1];
4421 if (F.LocalNumSubmodules > 0) {
4422 // Introduce the global -> local mapping for submodules within this
4423 // module.
4424 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4425
4426 // Introduce the local -> global mapping for submodules within this
4427 // module.
4428 F.SubmoduleRemap.insertOrReplace(
4429 std::make_pair(LocalBaseSubmoduleID,
4430 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4431
4432 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4433 }
4434 break;
4435 }
4436
4437 case SUBMODULE_IMPORTS: {
4438 if (First) {
4439 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004440 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004441 }
4442
4443 if (!CurrentModule)
4444 break;
4445
4446 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004447 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004448 Unresolved.File = &F;
4449 Unresolved.Mod = CurrentModule;
4450 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004451 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004452 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004453 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004454 }
4455 break;
4456 }
4457
4458 case SUBMODULE_EXPORTS: {
4459 if (First) {
4460 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004461 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004462 }
4463
4464 if (!CurrentModule)
4465 break;
4466
4467 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004468 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004469 Unresolved.File = &F;
4470 Unresolved.Mod = CurrentModule;
4471 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004472 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004473 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004474 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004475 }
4476
4477 // Once we've loaded the set of exports, there's no reason to keep
4478 // the parsed, unresolved exports around.
4479 CurrentModule->UnresolvedExports.clear();
4480 break;
4481 }
4482 case SUBMODULE_REQUIRES: {
4483 if (First) {
4484 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004485 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004486 }
4487
4488 if (!CurrentModule)
4489 break;
4490
Richard Smitha3feee22013-10-28 22:18:19 +00004491 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004492 Context.getTargetInfo());
4493 break;
4494 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004495
4496 case SUBMODULE_LINK_LIBRARY:
4497 if (First) {
4498 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004499 return Failure;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004500 }
4501
4502 if (!CurrentModule)
4503 break;
4504
4505 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004506 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004507 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004508
4509 case SUBMODULE_CONFIG_MACRO:
4510 if (First) {
4511 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004512 return Failure;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004513 }
4514
4515 if (!CurrentModule)
4516 break;
4517
4518 CurrentModule->ConfigMacros.push_back(Blob.str());
4519 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004520
4521 case SUBMODULE_CONFLICT: {
4522 if (First) {
4523 Error("missing submodule metadata record at beginning of block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004524 return Failure;
Douglas Gregorfb912652013-03-20 21:10:35 +00004525 }
4526
4527 if (!CurrentModule)
4528 break;
4529
4530 UnresolvedModuleRef Unresolved;
4531 Unresolved.File = &F;
4532 Unresolved.Mod = CurrentModule;
4533 Unresolved.ID = Record[0];
4534 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4535 Unresolved.IsWildcard = false;
4536 Unresolved.String = Blob;
4537 UnresolvedModuleRefs.push_back(Unresolved);
4538 break;
4539 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004540 }
4541 }
4542}
4543
4544/// \brief Parse the record that corresponds to a LangOptions data
4545/// structure.
4546///
4547/// This routine parses the language options from the AST file and then gives
4548/// them to the AST listener if one is set.
4549///
4550/// \returns true if the listener deems the file unacceptable, false otherwise.
4551bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4552 bool Complain,
4553 ASTReaderListener &Listener) {
4554 LangOptions LangOpts;
4555 unsigned Idx = 0;
4556#define LANGOPT(Name, Bits, Default, Description) \
4557 LangOpts.Name = Record[Idx++];
4558#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4559 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4560#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00004561#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4562#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004563
4564 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4565 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4566 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4567
4568 unsigned Length = Record[Idx++];
4569 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4570 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004571
4572 Idx += Length;
4573
4574 // Comment options.
4575 for (unsigned N = Record[Idx++]; N; --N) {
4576 LangOpts.CommentOpts.BlockCommandNames.push_back(
4577 ReadString(Record, Idx));
4578 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004579 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004580
Guy Benyei11169dd2012-12-18 14:30:41 +00004581 return Listener.ReadLanguageOptions(LangOpts, Complain);
4582}
4583
4584bool ASTReader::ParseTargetOptions(const RecordData &Record,
4585 bool Complain,
4586 ASTReaderListener &Listener) {
4587 unsigned Idx = 0;
4588 TargetOptions TargetOpts;
4589 TargetOpts.Triple = ReadString(Record, Idx);
4590 TargetOpts.CPU = ReadString(Record, Idx);
4591 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004592 for (unsigned N = Record[Idx++]; N; --N) {
4593 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4594 }
4595 for (unsigned N = Record[Idx++]; N; --N) {
4596 TargetOpts.Features.push_back(ReadString(Record, Idx));
4597 }
4598
4599 return Listener.ReadTargetOptions(TargetOpts, Complain);
4600}
4601
4602bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4603 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004604 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004605 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004606#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004607#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004608 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004609#include "clang/Basic/DiagnosticOptions.def"
4610
4611 for (unsigned N = Record[Idx++]; N; --N) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004612 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004613 }
4614
4615 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4616}
4617
4618bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4619 ASTReaderListener &Listener) {
4620 FileSystemOptions FSOpts;
4621 unsigned Idx = 0;
4622 FSOpts.WorkingDir = ReadString(Record, Idx);
4623 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4624}
4625
4626bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4627 bool Complain,
4628 ASTReaderListener &Listener) {
4629 HeaderSearchOptions HSOpts;
4630 unsigned Idx = 0;
4631 HSOpts.Sysroot = ReadString(Record, Idx);
4632
4633 // Include entries.
4634 for (unsigned N = Record[Idx++]; N; --N) {
4635 std::string Path = ReadString(Record, Idx);
4636 frontend::IncludeDirGroup Group
4637 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004638 bool IsFramework = Record[Idx++];
4639 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004640 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004641 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004642 }
4643
4644 // System header prefixes.
4645 for (unsigned N = Record[Idx++]; N; --N) {
4646 std::string Prefix = ReadString(Record, Idx);
4647 bool IsSystemHeader = Record[Idx++];
4648 HSOpts.SystemHeaderPrefixes.push_back(
4649 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4650 }
4651
4652 HSOpts.ResourceDir = ReadString(Record, Idx);
4653 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004654 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004655 HSOpts.DisableModuleHash = Record[Idx++];
4656 HSOpts.UseBuiltinIncludes = Record[Idx++];
4657 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4658 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4659 HSOpts.UseLibcxx = Record[Idx++];
4660
4661 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4662}
4663
4664bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4665 bool Complain,
4666 ASTReaderListener &Listener,
4667 std::string &SuggestedPredefines) {
4668 PreprocessorOptions PPOpts;
4669 unsigned Idx = 0;
4670
4671 // Macro definitions/undefs
4672 for (unsigned N = Record[Idx++]; N; --N) {
4673 std::string Macro = ReadString(Record, Idx);
4674 bool IsUndef = Record[Idx++];
4675 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4676 }
4677
4678 // Includes
4679 for (unsigned N = Record[Idx++]; N; --N) {
4680 PPOpts.Includes.push_back(ReadString(Record, Idx));
4681 }
4682
4683 // Macro Includes
4684 for (unsigned N = Record[Idx++]; N; --N) {
4685 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4686 }
4687
4688 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004689 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004690 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4691 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4692 PPOpts.ObjCXXARCStandardLibrary =
4693 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4694 SuggestedPredefines.clear();
4695 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4696 SuggestedPredefines);
4697}
4698
4699std::pair<ModuleFile *, unsigned>
4700ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4701 GlobalPreprocessedEntityMapType::iterator
4702 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4703 assert(I != GlobalPreprocessedEntityMap.end() &&
4704 "Corrupted global preprocessed entity map");
4705 ModuleFile *M = I->second;
4706 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4707 return std::make_pair(M, LocalIndex);
4708}
4709
4710std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4711ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4712 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4713 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4714 Mod.NumPreprocessedEntities);
4715
4716 return std::make_pair(PreprocessingRecord::iterator(),
4717 PreprocessingRecord::iterator());
4718}
4719
4720std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4721ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4722 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4723 ModuleDeclIterator(this, &Mod,
4724 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4725}
4726
4727PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4728 PreprocessedEntityID PPID = Index+1;
4729 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4730 ModuleFile &M = *PPInfo.first;
4731 unsigned LocalIndex = PPInfo.second;
4732 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4733
Guy Benyei11169dd2012-12-18 14:30:41 +00004734 if (!PP.getPreprocessingRecord()) {
4735 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004736 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004737 }
4738
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004739 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4740 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4741
4742 llvm::BitstreamEntry Entry =
4743 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4744 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004745 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004746
Guy Benyei11169dd2012-12-18 14:30:41 +00004747 // Read the record.
4748 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4749 ReadSourceLocation(M, PPOffs.End));
4750 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004751 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004752 RecordData Record;
4753 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004754 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4755 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004756 switch (RecType) {
4757 case PPD_MACRO_EXPANSION: {
4758 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004759 IdentifierInfo *Name = nullptr;
4760 MacroDefinition *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004761 if (isBuiltin)
4762 Name = getLocalIdentifier(M, Record[1]);
4763 else {
4764 PreprocessedEntityID
4765 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4766 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4767 }
4768
4769 MacroExpansion *ME;
4770 if (isBuiltin)
4771 ME = new (PPRec) MacroExpansion(Name, Range);
4772 else
4773 ME = new (PPRec) MacroExpansion(Def, Range);
4774
4775 return ME;
4776 }
4777
4778 case PPD_MACRO_DEFINITION: {
4779 // Decode the identifier info and then check again; if the macro is
4780 // still defined and associated with the identifier,
4781 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4782 MacroDefinition *MD
4783 = new (PPRec) MacroDefinition(II, Range);
4784
4785 if (DeserializationListener)
4786 DeserializationListener->MacroDefinitionRead(PPID, MD);
4787
4788 return MD;
4789 }
4790
4791 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004792 const char *FullFileNameStart = Blob.data() + Record[0];
4793 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004794 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004795 if (!FullFileName.empty())
4796 File = PP.getFileManager().getFile(FullFileName);
4797
4798 // FIXME: Stable encoding
4799 InclusionDirective::InclusionKind Kind
4800 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4801 InclusionDirective *ID
4802 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004803 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004804 Record[1], Record[3],
4805 File,
4806 Range);
4807 return ID;
4808 }
4809 }
4810
4811 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4812}
4813
4814/// \brief \arg SLocMapI points at a chunk of a module that contains no
4815/// preprocessed entities or the entities it contains are not the ones we are
4816/// looking for. Find the next module that contains entities and return the ID
4817/// of the first entry.
4818PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4819 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4820 ++SLocMapI;
4821 for (GlobalSLocOffsetMapType::const_iterator
4822 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4823 ModuleFile &M = *SLocMapI->second;
4824 if (M.NumPreprocessedEntities)
4825 return M.BasePreprocessedEntityID;
4826 }
4827
4828 return getTotalNumPreprocessedEntities();
4829}
4830
4831namespace {
4832
4833template <unsigned PPEntityOffset::*PPLoc>
4834struct PPEntityComp {
4835 const ASTReader &Reader;
4836 ModuleFile &M;
4837
4838 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4839
4840 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4841 SourceLocation LHS = getLoc(L);
4842 SourceLocation RHS = getLoc(R);
4843 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4844 }
4845
4846 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4847 SourceLocation LHS = getLoc(L);
4848 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4849 }
4850
4851 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4852 SourceLocation RHS = getLoc(R);
4853 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4854 }
4855
4856 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4857 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4858 }
4859};
4860
4861}
4862
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004863PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4864 bool EndsAfter) const {
4865 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00004866 return getTotalNumPreprocessedEntities();
4867
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004868 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4869 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004870 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4871 "Corrupted global sloc offset map");
4872
4873 if (SLocMapI->second->NumPreprocessedEntities == 0)
4874 return findNextPreprocessedEntity(SLocMapI);
4875
4876 ModuleFile &M = *SLocMapI->second;
4877 typedef const PPEntityOffset *pp_iterator;
4878 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4879 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4880
4881 size_t Count = M.NumPreprocessedEntities;
4882 size_t Half;
4883 pp_iterator First = pp_begin;
4884 pp_iterator PPI;
4885
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004886 if (EndsAfter) {
4887 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4888 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4889 } else {
4890 // Do a binary search manually instead of using std::lower_bound because
4891 // The end locations of entities may be unordered (when a macro expansion
4892 // is inside another macro argument), but for this case it is not important
4893 // whether we get the first macro expansion or its containing macro.
4894 while (Count > 0) {
4895 Half = Count / 2;
4896 PPI = First;
4897 std::advance(PPI, Half);
4898 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4899 Loc)) {
4900 First = PPI;
4901 ++First;
4902 Count = Count - Half - 1;
4903 } else
4904 Count = Half;
4905 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004906 }
4907
4908 if (PPI == pp_end)
4909 return findNextPreprocessedEntity(SLocMapI);
4910
4911 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4912}
4913
Guy Benyei11169dd2012-12-18 14:30:41 +00004914/// \brief Returns a pair of [Begin, End) indices of preallocated
4915/// preprocessed entities that \arg Range encompasses.
4916std::pair<unsigned, unsigned>
4917 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4918 if (Range.isInvalid())
4919 return std::make_pair(0,0);
4920 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4921
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004922 PreprocessedEntityID BeginID =
4923 findPreprocessedEntity(Range.getBegin(), false);
4924 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004925 return std::make_pair(BeginID, EndID);
4926}
4927
4928/// \brief Optionally returns true or false if the preallocated preprocessed
4929/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004930Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004931 FileID FID) {
4932 if (FID.isInvalid())
4933 return false;
4934
4935 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4936 ModuleFile &M = *PPInfo.first;
4937 unsigned LocalIndex = PPInfo.second;
4938 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4939
4940 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4941 if (Loc.isInvalid())
4942 return false;
4943
4944 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4945 return true;
4946 else
4947 return false;
4948}
4949
4950namespace {
4951 /// \brief Visitor used to search for information about a header file.
4952 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004953 const FileEntry *FE;
4954
David Blaikie05785d12013-02-20 22:23:23 +00004955 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004956
4957 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004958 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4959 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004960
4961 static bool visit(ModuleFile &M, void *UserData) {
4962 HeaderFileInfoVisitor *This
4963 = static_cast<HeaderFileInfoVisitor *>(UserData);
4964
Guy Benyei11169dd2012-12-18 14:30:41 +00004965 HeaderFileInfoLookupTable *Table
4966 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4967 if (!Table)
4968 return false;
4969
4970 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004971 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004972 if (Pos == Table->end())
4973 return false;
4974
4975 This->HFI = *Pos;
4976 return true;
4977 }
4978
David Blaikie05785d12013-02-20 22:23:23 +00004979 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004980 };
4981}
4982
4983HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004984 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004985 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00004986 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00004987 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004988
4989 return HeaderFileInfo();
4990}
4991
4992void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4993 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004994 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004995 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4996 ModuleFile &F = *(*I);
4997 unsigned Idx = 0;
4998 DiagStates.clear();
4999 assert(!Diag.DiagStates.empty());
5000 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5001 while (Idx < F.PragmaDiagMappings.size()) {
5002 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5003 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5004 if (DiagStateID != 0) {
5005 Diag.DiagStatePoints.push_back(
5006 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5007 FullSourceLoc(Loc, SourceMgr)));
5008 continue;
5009 }
5010
5011 assert(DiagStateID == 0);
5012 // A new DiagState was created here.
5013 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5014 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5015 DiagStates.push_back(NewState);
5016 Diag.DiagStatePoints.push_back(
5017 DiagnosticsEngine::DiagStatePoint(NewState,
5018 FullSourceLoc(Loc, SourceMgr)));
5019 while (1) {
5020 assert(Idx < F.PragmaDiagMappings.size() &&
5021 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5022 if (Idx >= F.PragmaDiagMappings.size()) {
5023 break; // Something is messed up but at least avoid infinite loop in
5024 // release build.
5025 }
5026 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5027 if (DiagID == (unsigned)-1) {
5028 break; // no more diag/map pairs for this location.
5029 }
Alp Tokerc726c362014-06-10 09:31:37 +00005030 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5031 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5032 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005033 }
5034 }
5035 }
5036}
5037
5038/// \brief Get the correct cursor and offset for loading a type.
5039ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5040 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5041 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5042 ModuleFile *M = I->second;
5043 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5044}
5045
5046/// \brief Read and return the type with the given index..
5047///
5048/// The index is the type ID, shifted and minus the number of predefs. This
5049/// routine actually reads the record corresponding to the type at the given
5050/// location. It is a helper routine for GetType, which deals with reading type
5051/// IDs.
5052QualType ASTReader::readTypeRecord(unsigned Index) {
5053 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005054 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005055
5056 // Keep track of where we are in the stream, then jump back there
5057 // after reading this type.
5058 SavedStreamPosition SavedPosition(DeclsCursor);
5059
5060 ReadingKindTracker ReadingKind(Read_Type, *this);
5061
5062 // Note that we are loading a type record.
5063 Deserializing AType(this);
5064
5065 unsigned Idx = 0;
5066 DeclsCursor.JumpToBit(Loc.Offset);
5067 RecordData Record;
5068 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005069 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005070 case TYPE_EXT_QUAL: {
5071 if (Record.size() != 2) {
5072 Error("Incorrect encoding of extended qualifier type");
5073 return QualType();
5074 }
5075 QualType Base = readType(*Loc.F, Record, Idx);
5076 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5077 return Context.getQualifiedType(Base, Quals);
5078 }
5079
5080 case TYPE_COMPLEX: {
5081 if (Record.size() != 1) {
5082 Error("Incorrect encoding of complex type");
5083 return QualType();
5084 }
5085 QualType ElemType = readType(*Loc.F, Record, Idx);
5086 return Context.getComplexType(ElemType);
5087 }
5088
5089 case TYPE_POINTER: {
5090 if (Record.size() != 1) {
5091 Error("Incorrect encoding of pointer type");
5092 return QualType();
5093 }
5094 QualType PointeeType = readType(*Loc.F, Record, Idx);
5095 return Context.getPointerType(PointeeType);
5096 }
5097
Reid Kleckner8a365022013-06-24 17:51:48 +00005098 case TYPE_DECAYED: {
5099 if (Record.size() != 1) {
5100 Error("Incorrect encoding of decayed type");
5101 return QualType();
5102 }
5103 QualType OriginalType = readType(*Loc.F, Record, Idx);
5104 QualType DT = Context.getAdjustedParameterType(OriginalType);
5105 if (!isa<DecayedType>(DT))
5106 Error("Decayed type does not decay");
5107 return DT;
5108 }
5109
Reid Kleckner0503a872013-12-05 01:23:43 +00005110 case TYPE_ADJUSTED: {
5111 if (Record.size() != 2) {
5112 Error("Incorrect encoding of adjusted type");
5113 return QualType();
5114 }
5115 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5116 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5117 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5118 }
5119
Guy Benyei11169dd2012-12-18 14:30:41 +00005120 case TYPE_BLOCK_POINTER: {
5121 if (Record.size() != 1) {
5122 Error("Incorrect encoding of block pointer type");
5123 return QualType();
5124 }
5125 QualType PointeeType = readType(*Loc.F, Record, Idx);
5126 return Context.getBlockPointerType(PointeeType);
5127 }
5128
5129 case TYPE_LVALUE_REFERENCE: {
5130 if (Record.size() != 2) {
5131 Error("Incorrect encoding of lvalue reference type");
5132 return QualType();
5133 }
5134 QualType PointeeType = readType(*Loc.F, Record, Idx);
5135 return Context.getLValueReferenceType(PointeeType, Record[1]);
5136 }
5137
5138 case TYPE_RVALUE_REFERENCE: {
5139 if (Record.size() != 1) {
5140 Error("Incorrect encoding of rvalue reference type");
5141 return QualType();
5142 }
5143 QualType PointeeType = readType(*Loc.F, Record, Idx);
5144 return Context.getRValueReferenceType(PointeeType);
5145 }
5146
5147 case TYPE_MEMBER_POINTER: {
5148 if (Record.size() != 2) {
5149 Error("Incorrect encoding of member pointer type");
5150 return QualType();
5151 }
5152 QualType PointeeType = readType(*Loc.F, Record, Idx);
5153 QualType ClassType = readType(*Loc.F, Record, Idx);
5154 if (PointeeType.isNull() || ClassType.isNull())
5155 return QualType();
5156
5157 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5158 }
5159
5160 case TYPE_CONSTANT_ARRAY: {
5161 QualType ElementType = readType(*Loc.F, Record, Idx);
5162 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5163 unsigned IndexTypeQuals = Record[2];
5164 unsigned Idx = 3;
5165 llvm::APInt Size = ReadAPInt(Record, Idx);
5166 return Context.getConstantArrayType(ElementType, Size,
5167 ASM, IndexTypeQuals);
5168 }
5169
5170 case TYPE_INCOMPLETE_ARRAY: {
5171 QualType ElementType = readType(*Loc.F, Record, Idx);
5172 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5173 unsigned IndexTypeQuals = Record[2];
5174 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5175 }
5176
5177 case TYPE_VARIABLE_ARRAY: {
5178 QualType ElementType = readType(*Loc.F, Record, Idx);
5179 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5180 unsigned IndexTypeQuals = Record[2];
5181 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5182 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5183 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5184 ASM, IndexTypeQuals,
5185 SourceRange(LBLoc, RBLoc));
5186 }
5187
5188 case TYPE_VECTOR: {
5189 if (Record.size() != 3) {
5190 Error("incorrect encoding of vector type in AST file");
5191 return QualType();
5192 }
5193
5194 QualType ElementType = readType(*Loc.F, Record, Idx);
5195 unsigned NumElements = Record[1];
5196 unsigned VecKind = Record[2];
5197 return Context.getVectorType(ElementType, NumElements,
5198 (VectorType::VectorKind)VecKind);
5199 }
5200
5201 case TYPE_EXT_VECTOR: {
5202 if (Record.size() != 3) {
5203 Error("incorrect encoding of extended vector type in AST file");
5204 return QualType();
5205 }
5206
5207 QualType ElementType = readType(*Loc.F, Record, Idx);
5208 unsigned NumElements = Record[1];
5209 return Context.getExtVectorType(ElementType, NumElements);
5210 }
5211
5212 case TYPE_FUNCTION_NO_PROTO: {
5213 if (Record.size() != 6) {
5214 Error("incorrect encoding of no-proto function type");
5215 return QualType();
5216 }
5217 QualType ResultType = readType(*Loc.F, Record, Idx);
5218 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5219 (CallingConv)Record[4], Record[5]);
5220 return Context.getFunctionNoProtoType(ResultType, Info);
5221 }
5222
5223 case TYPE_FUNCTION_PROTO: {
5224 QualType ResultType = readType(*Loc.F, Record, Idx);
5225
5226 FunctionProtoType::ExtProtoInfo EPI;
5227 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5228 /*hasregparm*/ Record[2],
5229 /*regparm*/ Record[3],
5230 static_cast<CallingConv>(Record[4]),
5231 /*produces*/ Record[5]);
5232
5233 unsigned Idx = 6;
5234 unsigned NumParams = Record[Idx++];
5235 SmallVector<QualType, 16> ParamTypes;
5236 for (unsigned I = 0; I != NumParams; ++I)
5237 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5238
5239 EPI.Variadic = Record[Idx++];
5240 EPI.HasTrailingReturn = Record[Idx++];
5241 EPI.TypeQuals = Record[Idx++];
5242 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005243 SmallVector<QualType, 8> ExceptionStorage;
5244 readExceptionSpec(*Loc.F, ExceptionStorage, EPI, Record, Idx);
Jordan Rose5c382722013-03-08 21:51:21 +00005245 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005246 }
5247
5248 case TYPE_UNRESOLVED_USING: {
5249 unsigned Idx = 0;
5250 return Context.getTypeDeclType(
5251 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5252 }
5253
5254 case TYPE_TYPEDEF: {
5255 if (Record.size() != 2) {
5256 Error("incorrect encoding of typedef type");
5257 return QualType();
5258 }
5259 unsigned Idx = 0;
5260 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5261 QualType Canonical = readType(*Loc.F, Record, Idx);
5262 if (!Canonical.isNull())
5263 Canonical = Context.getCanonicalType(Canonical);
5264 return Context.getTypedefType(Decl, Canonical);
5265 }
5266
5267 case TYPE_TYPEOF_EXPR:
5268 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5269
5270 case TYPE_TYPEOF: {
5271 if (Record.size() != 1) {
5272 Error("incorrect encoding of typeof(type) in AST file");
5273 return QualType();
5274 }
5275 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5276 return Context.getTypeOfType(UnderlyingType);
5277 }
5278
5279 case TYPE_DECLTYPE: {
5280 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5281 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5282 }
5283
5284 case TYPE_UNARY_TRANSFORM: {
5285 QualType BaseType = readType(*Loc.F, Record, Idx);
5286 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5287 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5288 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5289 }
5290
Richard Smith74aeef52013-04-26 16:15:35 +00005291 case TYPE_AUTO: {
5292 QualType Deduced = readType(*Loc.F, Record, Idx);
5293 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005294 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005295 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005296 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005297
5298 case TYPE_RECORD: {
5299 if (Record.size() != 2) {
5300 Error("incorrect encoding of record type");
5301 return QualType();
5302 }
5303 unsigned Idx = 0;
5304 bool IsDependent = Record[Idx++];
5305 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5306 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5307 QualType T = Context.getRecordType(RD);
5308 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5309 return T;
5310 }
5311
5312 case TYPE_ENUM: {
5313 if (Record.size() != 2) {
5314 Error("incorrect encoding of enum type");
5315 return QualType();
5316 }
5317 unsigned Idx = 0;
5318 bool IsDependent = Record[Idx++];
5319 QualType T
5320 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5321 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5322 return T;
5323 }
5324
5325 case TYPE_ATTRIBUTED: {
5326 if (Record.size() != 3) {
5327 Error("incorrect encoding of attributed type");
5328 return QualType();
5329 }
5330 QualType modifiedType = readType(*Loc.F, Record, Idx);
5331 QualType equivalentType = readType(*Loc.F, Record, Idx);
5332 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5333 return Context.getAttributedType(kind, modifiedType, equivalentType);
5334 }
5335
5336 case TYPE_PAREN: {
5337 if (Record.size() != 1) {
5338 Error("incorrect encoding of paren type");
5339 return QualType();
5340 }
5341 QualType InnerType = readType(*Loc.F, Record, Idx);
5342 return Context.getParenType(InnerType);
5343 }
5344
5345 case TYPE_PACK_EXPANSION: {
5346 if (Record.size() != 2) {
5347 Error("incorrect encoding of pack expansion type");
5348 return QualType();
5349 }
5350 QualType Pattern = readType(*Loc.F, Record, Idx);
5351 if (Pattern.isNull())
5352 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005353 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005354 if (Record[1])
5355 NumExpansions = Record[1] - 1;
5356 return Context.getPackExpansionType(Pattern, NumExpansions);
5357 }
5358
5359 case TYPE_ELABORATED: {
5360 unsigned Idx = 0;
5361 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5362 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5363 QualType NamedType = readType(*Loc.F, Record, Idx);
5364 return Context.getElaboratedType(Keyword, NNS, NamedType);
5365 }
5366
5367 case TYPE_OBJC_INTERFACE: {
5368 unsigned Idx = 0;
5369 ObjCInterfaceDecl *ItfD
5370 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5371 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5372 }
5373
5374 case TYPE_OBJC_OBJECT: {
5375 unsigned Idx = 0;
5376 QualType Base = readType(*Loc.F, Record, Idx);
5377 unsigned NumProtos = Record[Idx++];
5378 SmallVector<ObjCProtocolDecl*, 4> Protos;
5379 for (unsigned I = 0; I != NumProtos; ++I)
5380 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5381 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5382 }
5383
5384 case TYPE_OBJC_OBJECT_POINTER: {
5385 unsigned Idx = 0;
5386 QualType Pointee = readType(*Loc.F, Record, Idx);
5387 return Context.getObjCObjectPointerType(Pointee);
5388 }
5389
5390 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5391 unsigned Idx = 0;
5392 QualType Parm = readType(*Loc.F, Record, Idx);
5393 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005394 return Context.getSubstTemplateTypeParmType(
5395 cast<TemplateTypeParmType>(Parm),
5396 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005397 }
5398
5399 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5400 unsigned Idx = 0;
5401 QualType Parm = readType(*Loc.F, Record, Idx);
5402 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5403 return Context.getSubstTemplateTypeParmPackType(
5404 cast<TemplateTypeParmType>(Parm),
5405 ArgPack);
5406 }
5407
5408 case TYPE_INJECTED_CLASS_NAME: {
5409 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5410 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5411 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5412 // for AST reading, too much interdependencies.
Richard Smithf17fdbd2014-04-24 02:25:27 +00005413 const Type *T;
5414 if (const Type *Existing = D->getTypeForDecl())
5415 T = Existing;
5416 else if (auto *Prev = D->getPreviousDecl())
5417 T = Prev->getTypeForDecl();
5418 else
5419 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5420 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005421 }
5422
5423 case TYPE_TEMPLATE_TYPE_PARM: {
5424 unsigned Idx = 0;
5425 unsigned Depth = Record[Idx++];
5426 unsigned Index = Record[Idx++];
5427 bool Pack = Record[Idx++];
5428 TemplateTypeParmDecl *D
5429 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5430 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5431 }
5432
5433 case TYPE_DEPENDENT_NAME: {
5434 unsigned Idx = 0;
5435 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5436 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5437 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5438 QualType Canon = readType(*Loc.F, Record, Idx);
5439 if (!Canon.isNull())
5440 Canon = Context.getCanonicalType(Canon);
5441 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5442 }
5443
5444 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5445 unsigned Idx = 0;
5446 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5447 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5448 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5449 unsigned NumArgs = Record[Idx++];
5450 SmallVector<TemplateArgument, 8> Args;
5451 Args.reserve(NumArgs);
5452 while (NumArgs--)
5453 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5454 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5455 Args.size(), Args.data());
5456 }
5457
5458 case TYPE_DEPENDENT_SIZED_ARRAY: {
5459 unsigned Idx = 0;
5460
5461 // ArrayType
5462 QualType ElementType = readType(*Loc.F, Record, Idx);
5463 ArrayType::ArraySizeModifier ASM
5464 = (ArrayType::ArraySizeModifier)Record[Idx++];
5465 unsigned IndexTypeQuals = Record[Idx++];
5466
5467 // DependentSizedArrayType
5468 Expr *NumElts = ReadExpr(*Loc.F);
5469 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5470
5471 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5472 IndexTypeQuals, Brackets);
5473 }
5474
5475 case TYPE_TEMPLATE_SPECIALIZATION: {
5476 unsigned Idx = 0;
5477 bool IsDependent = Record[Idx++];
5478 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5479 SmallVector<TemplateArgument, 8> Args;
5480 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5481 QualType Underlying = readType(*Loc.F, Record, Idx);
5482 QualType T;
5483 if (Underlying.isNull())
5484 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5485 Args.size());
5486 else
5487 T = Context.getTemplateSpecializationType(Name, Args.data(),
5488 Args.size(), Underlying);
5489 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5490 return T;
5491 }
5492
5493 case TYPE_ATOMIC: {
5494 if (Record.size() != 1) {
5495 Error("Incorrect encoding of atomic type");
5496 return QualType();
5497 }
5498 QualType ValueType = readType(*Loc.F, Record, Idx);
5499 return Context.getAtomicType(ValueType);
5500 }
5501 }
5502 llvm_unreachable("Invalid TypeCode!");
5503}
5504
Richard Smith564417a2014-03-20 21:47:22 +00005505void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5506 SmallVectorImpl<QualType> &Exceptions,
5507 FunctionProtoType::ExtProtoInfo &EPI,
5508 const RecordData &Record, unsigned &Idx) {
5509 ExceptionSpecificationType EST =
5510 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5511 EPI.ExceptionSpecType = EST;
5512 if (EST == EST_Dynamic) {
5513 EPI.NumExceptions = Record[Idx++];
5514 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
5515 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5516 EPI.Exceptions = Exceptions.data();
5517 } else if (EST == EST_ComputedNoexcept) {
5518 EPI.NoexceptExpr = ReadExpr(ModuleFile);
5519 } else if (EST == EST_Uninstantiated) {
5520 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5521 EPI.ExceptionSpecTemplate =
5522 ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5523 } else if (EST == EST_Unevaluated) {
5524 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5525 }
5526}
5527
Guy Benyei11169dd2012-12-18 14:30:41 +00005528class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5529 ASTReader &Reader;
5530 ModuleFile &F;
5531 const ASTReader::RecordData &Record;
5532 unsigned &Idx;
5533
5534 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5535 unsigned &I) {
5536 return Reader.ReadSourceLocation(F, R, I);
5537 }
5538
5539 template<typename T>
5540 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5541 return Reader.ReadDeclAs<T>(F, Record, Idx);
5542 }
5543
5544public:
5545 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5546 const ASTReader::RecordData &Record, unsigned &Idx)
5547 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5548 { }
5549
5550 // We want compile-time assurance that we've enumerated all of
5551 // these, so unfortunately we have to declare them first, then
5552 // define them out-of-line.
5553#define ABSTRACT_TYPELOC(CLASS, PARENT)
5554#define TYPELOC(CLASS, PARENT) \
5555 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5556#include "clang/AST/TypeLocNodes.def"
5557
5558 void VisitFunctionTypeLoc(FunctionTypeLoc);
5559 void VisitArrayTypeLoc(ArrayTypeLoc);
5560};
5561
5562void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5563 // nothing to do
5564}
5565void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5566 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5567 if (TL.needsExtraLocalData()) {
5568 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5569 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5570 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5571 TL.setModeAttr(Record[Idx++]);
5572 }
5573}
5574void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5575 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5576}
5577void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5578 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5579}
Reid Kleckner8a365022013-06-24 17:51:48 +00005580void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5581 // nothing to do
5582}
Reid Kleckner0503a872013-12-05 01:23:43 +00005583void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5584 // nothing to do
5585}
Guy Benyei11169dd2012-12-18 14:30:41 +00005586void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5587 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5588}
5589void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5590 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5591}
5592void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5593 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5594}
5595void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5596 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5597 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5598}
5599void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5600 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5601 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5602 if (Record[Idx++])
5603 TL.setSizeExpr(Reader.ReadExpr(F));
5604 else
Craig Toppera13603a2014-05-22 05:54:18 +00005605 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005606}
5607void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5608 VisitArrayTypeLoc(TL);
5609}
5610void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5611 VisitArrayTypeLoc(TL);
5612}
5613void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5614 VisitArrayTypeLoc(TL);
5615}
5616void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5617 DependentSizedArrayTypeLoc TL) {
5618 VisitArrayTypeLoc(TL);
5619}
5620void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5621 DependentSizedExtVectorTypeLoc TL) {
5622 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5623}
5624void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5625 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5626}
5627void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5628 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5629}
5630void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5631 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5632 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5633 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5634 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005635 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5636 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005637 }
5638}
5639void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5640 VisitFunctionTypeLoc(TL);
5641}
5642void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5643 VisitFunctionTypeLoc(TL);
5644}
5645void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5646 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5647}
5648void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5649 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5650}
5651void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5652 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5653 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5654 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5655}
5656void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5657 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5658 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5659 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5660 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5661}
5662void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5663 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5664}
5665void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5666 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5667 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5668 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5669 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5670}
5671void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5672 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5673}
5674void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5675 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5676}
5677void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5678 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5679}
5680void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5681 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5682 if (TL.hasAttrOperand()) {
5683 SourceRange range;
5684 range.setBegin(ReadSourceLocation(Record, Idx));
5685 range.setEnd(ReadSourceLocation(Record, Idx));
5686 TL.setAttrOperandParensRange(range);
5687 }
5688 if (TL.hasAttrExprOperand()) {
5689 if (Record[Idx++])
5690 TL.setAttrExprOperand(Reader.ReadExpr(F));
5691 else
Craig Toppera13603a2014-05-22 05:54:18 +00005692 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005693 } else if (TL.hasAttrEnumOperand())
5694 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5695}
5696void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5697 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5698}
5699void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5700 SubstTemplateTypeParmTypeLoc TL) {
5701 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5702}
5703void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5704 SubstTemplateTypeParmPackTypeLoc TL) {
5705 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5706}
5707void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5708 TemplateSpecializationTypeLoc TL) {
5709 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5710 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5711 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5712 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5713 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5714 TL.setArgLocInfo(i,
5715 Reader.GetTemplateArgumentLocInfo(F,
5716 TL.getTypePtr()->getArg(i).getKind(),
5717 Record, Idx));
5718}
5719void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5720 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5721 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5722}
5723void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5724 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5725 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5726}
5727void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5728 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5729}
5730void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5731 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5732 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5733 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5734}
5735void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5736 DependentTemplateSpecializationTypeLoc TL) {
5737 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5738 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5739 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5740 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5741 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5742 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5743 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5744 TL.setArgLocInfo(I,
5745 Reader.GetTemplateArgumentLocInfo(F,
5746 TL.getTypePtr()->getArg(I).getKind(),
5747 Record, Idx));
5748}
5749void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5750 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5751}
5752void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5753 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5754}
5755void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5756 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5757 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5758 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5759 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5760 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5761}
5762void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5763 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5764}
5765void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5766 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5767 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5768 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5769}
5770
5771TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5772 const RecordData &Record,
5773 unsigned &Idx) {
5774 QualType InfoTy = readType(F, Record, Idx);
5775 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005776 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005777
5778 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5779 TypeLocReader TLR(*this, F, Record, Idx);
5780 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5781 TLR.Visit(TL);
5782 return TInfo;
5783}
5784
5785QualType ASTReader::GetType(TypeID ID) {
5786 unsigned FastQuals = ID & Qualifiers::FastMask;
5787 unsigned Index = ID >> Qualifiers::FastWidth;
5788
5789 if (Index < NUM_PREDEF_TYPE_IDS) {
5790 QualType T;
5791 switch ((PredefinedTypeIDs)Index) {
5792 case PREDEF_TYPE_NULL_ID: return QualType();
5793 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5794 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5795
5796 case PREDEF_TYPE_CHAR_U_ID:
5797 case PREDEF_TYPE_CHAR_S_ID:
5798 // FIXME: Check that the signedness of CharTy is correct!
5799 T = Context.CharTy;
5800 break;
5801
5802 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5803 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5804 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5805 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5806 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5807 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5808 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5809 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5810 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5811 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5812 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5813 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5814 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5815 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5816 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5817 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5818 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5819 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5820 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5821 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5822 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5823 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5824 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5825 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5826 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5827 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5828 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5829 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005830 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5831 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5832 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5833 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5834 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5835 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005836 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005837 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005838 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5839
5840 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5841 T = Context.getAutoRRefDeductType();
5842 break;
5843
5844 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5845 T = Context.ARCUnbridgedCastTy;
5846 break;
5847
5848 case PREDEF_TYPE_VA_LIST_TAG:
5849 T = Context.getVaListTagType();
5850 break;
5851
5852 case PREDEF_TYPE_BUILTIN_FN:
5853 T = Context.BuiltinFnTy;
5854 break;
5855 }
5856
5857 assert(!T.isNull() && "Unknown predefined type");
5858 return T.withFastQualifiers(FastQuals);
5859 }
5860
5861 Index -= NUM_PREDEF_TYPE_IDS;
5862 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5863 if (TypesLoaded[Index].isNull()) {
5864 TypesLoaded[Index] = readTypeRecord(Index);
5865 if (TypesLoaded[Index].isNull())
5866 return QualType();
5867
5868 TypesLoaded[Index]->setFromAST();
5869 if (DeserializationListener)
5870 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5871 TypesLoaded[Index]);
5872 }
5873
5874 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5875}
5876
5877QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5878 return GetType(getGlobalTypeID(F, LocalID));
5879}
5880
5881serialization::TypeID
5882ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5883 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5884 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5885
5886 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5887 return LocalID;
5888
5889 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5890 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5891 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5892
5893 unsigned GlobalIndex = LocalIndex + I->second;
5894 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5895}
5896
5897TemplateArgumentLocInfo
5898ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5899 TemplateArgument::ArgKind Kind,
5900 const RecordData &Record,
5901 unsigned &Index) {
5902 switch (Kind) {
5903 case TemplateArgument::Expression:
5904 return ReadExpr(F);
5905 case TemplateArgument::Type:
5906 return GetTypeSourceInfo(F, Record, Index);
5907 case TemplateArgument::Template: {
5908 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5909 Index);
5910 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5911 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5912 SourceLocation());
5913 }
5914 case TemplateArgument::TemplateExpansion: {
5915 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5916 Index);
5917 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5918 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5919 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5920 EllipsisLoc);
5921 }
5922 case TemplateArgument::Null:
5923 case TemplateArgument::Integral:
5924 case TemplateArgument::Declaration:
5925 case TemplateArgument::NullPtr:
5926 case TemplateArgument::Pack:
5927 // FIXME: Is this right?
5928 return TemplateArgumentLocInfo();
5929 }
5930 llvm_unreachable("unexpected template argument loc");
5931}
5932
5933TemplateArgumentLoc
5934ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5935 const RecordData &Record, unsigned &Index) {
5936 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5937
5938 if (Arg.getKind() == TemplateArgument::Expression) {
5939 if (Record[Index++]) // bool InfoHasSameExpr.
5940 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5941 }
5942 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5943 Record, Index));
5944}
5945
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005946const ASTTemplateArgumentListInfo*
5947ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5948 const RecordData &Record,
5949 unsigned &Index) {
5950 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5951 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5952 unsigned NumArgsAsWritten = Record[Index++];
5953 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5954 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5955 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5956 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5957}
5958
Guy Benyei11169dd2012-12-18 14:30:41 +00005959Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5960 return GetDecl(ID);
5961}
5962
Richard Smith053f6c62014-05-16 23:01:30 +00005963void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00005964 if (NumCurrentElementsDeserializing) {
5965 // We arrange to not care about the complete redeclaration chain while we're
5966 // deserializing. Just remember that the AST has marked this one as complete
5967 // but that it's not actually complete yet, so we know we still need to
5968 // complete it later.
5969 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
5970 return;
5971 }
5972
Richard Smith053f6c62014-05-16 23:01:30 +00005973 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
5974
5975 // Recursively ensure that the decl context itself is complete
5976 // (in particular, this matters if the decl context is a namespace).
5977 //
5978 // FIXME: This should be performed by lookup instead of here.
5979 cast<Decl>(DC)->getMostRecentDecl();
5980
5981 // If this is a named declaration, complete it by looking it up
5982 // within its context.
5983 //
5984 // FIXME: We don't currently handle the cases where we can't do this;
5985 // merging a class definition that contains unnamed entities should merge
5986 // those entities. Likewise, merging a function definition should merge
5987 // all mergeable entities within it.
5988 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
5989 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
5990 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
5991 auto *II = Name.getAsIdentifierInfo();
5992 if (isa<TranslationUnitDecl>(DC) && II) {
5993 // Outside of C++, we don't have a lookup table for the TU, so update
5994 // the identifier instead. In C++, either way should work fine.
5995 if (II->isOutOfDate())
5996 updateOutOfDateIdentifier(*II);
5997 } else
5998 DC->lookup(Name);
5999 }
6000 }
6001}
6002
Richard Smithcd45dbc2014-04-19 03:48:30 +00006003uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6004 const RecordData &Record,
6005 unsigned &Idx) {
6006 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6007 Error("malformed AST file: missing C++ base specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00006008 return 0;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006009 }
6010
Guy Benyei11169dd2012-12-18 14:30:41 +00006011 unsigned LocalID = Record[Idx++];
6012 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6013}
6014
6015CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6016 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006017 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006018 SavedStreamPosition SavedPosition(Cursor);
6019 Cursor.JumpToBit(Loc.Offset);
6020 ReadingKindTracker ReadingKind(Read_Decl, *this);
6021 RecordData Record;
6022 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006023 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006024 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006025 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006026 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006027 }
6028
6029 unsigned Idx = 0;
6030 unsigned NumBases = Record[Idx++];
6031 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6032 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6033 for (unsigned I = 0; I != NumBases; ++I)
6034 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6035 return Bases;
6036}
6037
6038serialization::DeclID
6039ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6040 if (LocalID < NUM_PREDEF_DECL_IDS)
6041 return LocalID;
6042
6043 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6044 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6045 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6046
6047 return LocalID + I->second;
6048}
6049
6050bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6051 ModuleFile &M) const {
6052 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6053 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6054 return &M == I->second;
6055}
6056
Douglas Gregor9f782892013-01-21 15:25:38 +00006057ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006058 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006059 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006060 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6061 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6062 return I->second;
6063}
6064
6065SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6066 if (ID < NUM_PREDEF_DECL_IDS)
6067 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006068
Guy Benyei11169dd2012-12-18 14:30:41 +00006069 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6070
6071 if (Index > DeclsLoaded.size()) {
6072 Error("declaration ID out-of-range for AST file");
6073 return SourceLocation();
6074 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006075
Guy Benyei11169dd2012-12-18 14:30:41 +00006076 if (Decl *D = DeclsLoaded[Index])
6077 return D->getLocation();
6078
6079 unsigned RawLocation = 0;
6080 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6081 return ReadSourceLocation(*Rec.F, RawLocation);
6082}
6083
Richard Smithcd45dbc2014-04-19 03:48:30 +00006084Decl *ASTReader::GetExistingDecl(DeclID ID) {
6085 if (ID < NUM_PREDEF_DECL_IDS) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006086 switch ((PredefinedDeclIDs)ID) {
6087 case PREDEF_DECL_NULL_ID:
Craig Toppera13603a2014-05-22 05:54:18 +00006088 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006089
Guy Benyei11169dd2012-12-18 14:30:41 +00006090 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6091 return Context.getTranslationUnitDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006092
Guy Benyei11169dd2012-12-18 14:30:41 +00006093 case PREDEF_DECL_OBJC_ID_ID:
6094 return Context.getObjCIdDecl();
6095
6096 case PREDEF_DECL_OBJC_SEL_ID:
6097 return Context.getObjCSelDecl();
6098
6099 case PREDEF_DECL_OBJC_CLASS_ID:
6100 return Context.getObjCClassDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006101
Guy Benyei11169dd2012-12-18 14:30:41 +00006102 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6103 return Context.getObjCProtocolDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006104
Guy Benyei11169dd2012-12-18 14:30:41 +00006105 case PREDEF_DECL_INT_128_ID:
6106 return Context.getInt128Decl();
6107
6108 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6109 return Context.getUInt128Decl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006110
Guy Benyei11169dd2012-12-18 14:30:41 +00006111 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6112 return Context.getObjCInstanceTypeDecl();
6113
6114 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6115 return Context.getBuiltinVaListDecl();
6116 }
6117 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006118
Guy Benyei11169dd2012-12-18 14:30:41 +00006119 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6120
6121 if (Index >= DeclsLoaded.size()) {
6122 assert(0 && "declaration ID out-of-range for AST file");
6123 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006124 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006125 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006126
6127 return DeclsLoaded[Index];
6128}
6129
6130Decl *ASTReader::GetDecl(DeclID ID) {
6131 if (ID < NUM_PREDEF_DECL_IDS)
6132 return GetExistingDecl(ID);
6133
6134 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6135
6136 if (Index >= DeclsLoaded.size()) {
6137 assert(0 && "declaration ID out-of-range for AST file");
6138 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006139 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006140 }
6141
Guy Benyei11169dd2012-12-18 14:30:41 +00006142 if (!DeclsLoaded[Index]) {
6143 ReadDeclRecord(ID);
6144 if (DeserializationListener)
6145 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6146 }
6147
6148 return DeclsLoaded[Index];
6149}
6150
6151DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6152 DeclID GlobalID) {
6153 if (GlobalID < NUM_PREDEF_DECL_IDS)
6154 return GlobalID;
6155
6156 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6157 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6158 ModuleFile *Owner = I->second;
6159
6160 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6161 = M.GlobalToLocalDeclIDs.find(Owner);
6162 if (Pos == M.GlobalToLocalDeclIDs.end())
6163 return 0;
6164
6165 return GlobalID - Owner->BaseDeclID + Pos->second;
6166}
6167
6168serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6169 const RecordData &Record,
6170 unsigned &Idx) {
6171 if (Idx >= Record.size()) {
6172 Error("Corrupted AST file");
6173 return 0;
6174 }
6175
6176 return getGlobalDeclID(F, Record[Idx++]);
6177}
6178
6179/// \brief Resolve the offset of a statement into a statement.
6180///
6181/// This operation will read a new statement from the external
6182/// source each time it is called, and is meant to be used via a
6183/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6184Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6185 // Switch case IDs are per Decl.
6186 ClearSwitchCaseIDs();
6187
6188 // Offset here is a global offset across the entire chain.
6189 RecordLocation Loc = getLocalBitOffset(Offset);
6190 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6191 return ReadStmtFromStream(*Loc.F);
6192}
6193
6194namespace {
6195 class FindExternalLexicalDeclsVisitor {
6196 ASTReader &Reader;
6197 const DeclContext *DC;
6198 bool (*isKindWeWant)(Decl::Kind);
6199
6200 SmallVectorImpl<Decl*> &Decls;
6201 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6202
6203 public:
6204 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6205 bool (*isKindWeWant)(Decl::Kind),
6206 SmallVectorImpl<Decl*> &Decls)
6207 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6208 {
6209 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6210 PredefsVisited[I] = false;
6211 }
6212
6213 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6214 if (Preorder)
6215 return false;
6216
6217 FindExternalLexicalDeclsVisitor *This
6218 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6219
6220 ModuleFile::DeclContextInfosMap::iterator Info
6221 = M.DeclContextInfos.find(This->DC);
6222 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6223 return false;
6224
6225 // Load all of the declaration IDs
6226 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6227 *IDE = ID + Info->second.NumLexicalDecls;
6228 ID != IDE; ++ID) {
6229 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6230 continue;
6231
6232 // Don't add predefined declarations to the lexical context more
6233 // than once.
6234 if (ID->second < NUM_PREDEF_DECL_IDS) {
6235 if (This->PredefsVisited[ID->second])
6236 continue;
6237
6238 This->PredefsVisited[ID->second] = true;
6239 }
6240
6241 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6242 if (!This->DC->isDeclInLexicalTraversal(D))
6243 This->Decls.push_back(D);
6244 }
6245 }
6246
6247 return false;
6248 }
6249 };
6250}
6251
6252ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6253 bool (*isKindWeWant)(Decl::Kind),
6254 SmallVectorImpl<Decl*> &Decls) {
6255 // There might be lexical decls in multiple modules, for the TU at
6256 // least. Walk all of the modules in the order they were loaded.
6257 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6258 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6259 ++NumLexicalDeclContextsRead;
6260 return ELR_Success;
6261}
6262
6263namespace {
6264
6265class DeclIDComp {
6266 ASTReader &Reader;
6267 ModuleFile &Mod;
6268
6269public:
6270 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6271
6272 bool operator()(LocalDeclID L, LocalDeclID R) const {
6273 SourceLocation LHS = getLocation(L);
6274 SourceLocation RHS = getLocation(R);
6275 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6276 }
6277
6278 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6279 SourceLocation RHS = getLocation(R);
6280 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6281 }
6282
6283 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6284 SourceLocation LHS = getLocation(L);
6285 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6286 }
6287
6288 SourceLocation getLocation(LocalDeclID ID) const {
6289 return Reader.getSourceManager().getFileLoc(
6290 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6291 }
6292};
6293
6294}
6295
6296void ASTReader::FindFileRegionDecls(FileID File,
6297 unsigned Offset, unsigned Length,
6298 SmallVectorImpl<Decl *> &Decls) {
6299 SourceManager &SM = getSourceManager();
6300
6301 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6302 if (I == FileDeclIDs.end())
6303 return;
6304
6305 FileDeclsInfo &DInfo = I->second;
6306 if (DInfo.Decls.empty())
6307 return;
6308
6309 SourceLocation
6310 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6311 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6312
6313 DeclIDComp DIDComp(*this, *DInfo.Mod);
6314 ArrayRef<serialization::LocalDeclID>::iterator
6315 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6316 BeginLoc, DIDComp);
6317 if (BeginIt != DInfo.Decls.begin())
6318 --BeginIt;
6319
6320 // If we are pointing at a top-level decl inside an objc container, we need
6321 // to backtrack until we find it otherwise we will fail to report that the
6322 // region overlaps with an objc container.
6323 while (BeginIt != DInfo.Decls.begin() &&
6324 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6325 ->isTopLevelDeclInObjCContainer())
6326 --BeginIt;
6327
6328 ArrayRef<serialization::LocalDeclID>::iterator
6329 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6330 EndLoc, DIDComp);
6331 if (EndIt != DInfo.Decls.end())
6332 ++EndIt;
6333
6334 for (ArrayRef<serialization::LocalDeclID>::iterator
6335 DIt = BeginIt; DIt != EndIt; ++DIt)
6336 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6337}
6338
6339namespace {
6340 /// \brief ModuleFile visitor used to perform name lookup into a
6341 /// declaration context.
6342 class DeclContextNameLookupVisitor {
6343 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006344 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006345 DeclarationName Name;
6346 SmallVectorImpl<NamedDecl *> &Decls;
6347
6348 public:
6349 DeclContextNameLookupVisitor(ASTReader &Reader,
6350 SmallVectorImpl<const DeclContext *> &Contexts,
6351 DeclarationName Name,
6352 SmallVectorImpl<NamedDecl *> &Decls)
6353 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6354
6355 static bool visit(ModuleFile &M, void *UserData) {
6356 DeclContextNameLookupVisitor *This
6357 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6358
6359 // Check whether we have any visible declaration information for
6360 // this context in this module.
6361 ModuleFile::DeclContextInfosMap::iterator Info;
6362 bool FoundInfo = false;
6363 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6364 Info = M.DeclContextInfos.find(This->Contexts[I]);
6365 if (Info != M.DeclContextInfos.end() &&
6366 Info->second.NameLookupTableData) {
6367 FoundInfo = true;
6368 break;
6369 }
6370 }
6371
6372 if (!FoundInfo)
6373 return false;
6374
6375 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006376 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006377 Info->second.NameLookupTableData;
6378 ASTDeclContextNameLookupTable::iterator Pos
6379 = LookupTable->find(This->Name);
6380 if (Pos == LookupTable->end())
6381 return false;
6382
6383 bool FoundAnything = false;
6384 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6385 for (; Data.first != Data.second; ++Data.first) {
6386 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6387 if (!ND)
6388 continue;
6389
6390 if (ND->getDeclName() != This->Name) {
6391 // A name might be null because the decl's redeclarable part is
6392 // currently read before reading its name. The lookup is triggered by
6393 // building that decl (likely indirectly), and so it is later in the
6394 // sense of "already existing" and can be ignored here.
6395 continue;
6396 }
6397
6398 // Record this declaration.
6399 FoundAnything = true;
6400 This->Decls.push_back(ND);
6401 }
6402
6403 return FoundAnything;
6404 }
6405 };
6406}
6407
Douglas Gregor9f782892013-01-21 15:25:38 +00006408/// \brief Retrieve the "definitive" module file for the definition of the
6409/// given declaration context, if there is one.
6410///
6411/// The "definitive" module file is the only place where we need to look to
6412/// find information about the declarations within the given declaration
6413/// context. For example, C++ and Objective-C classes, C structs/unions, and
6414/// Objective-C protocols, categories, and extensions are all defined in a
6415/// single place in the source code, so they have definitive module files
6416/// associated with them. C++ namespaces, on the other hand, can have
6417/// definitions in multiple different module files.
6418///
6419/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6420/// NDEBUG checking.
6421static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6422 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006423 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6424 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006425
Craig Toppera13603a2014-05-22 05:54:18 +00006426 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +00006427}
6428
Richard Smith9ce12e32013-02-07 03:30:24 +00006429bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006430ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6431 DeclarationName Name) {
6432 assert(DC->hasExternalVisibleStorage() &&
6433 "DeclContext has no visible decls in storage");
6434 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006435 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006436
6437 SmallVector<NamedDecl *, 64> Decls;
6438
6439 // Compute the declaration contexts we need to look into. Multiple such
6440 // declaration contexts occur when two declaration contexts from disjoint
6441 // modules get merged, e.g., when two namespaces with the same name are
6442 // independently defined in separate modules.
6443 SmallVector<const DeclContext *, 2> Contexts;
6444 Contexts.push_back(DC);
6445
6446 if (DC->isNamespace()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006447 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei11169dd2012-12-18 14:30:41 +00006448 if (Merged != MergedDecls.end()) {
6449 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6450 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6451 }
6452 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006453 if (isa<CXXRecordDecl>(DC)) {
6454 auto Merged = MergedLookups.find(DC);
6455 if (Merged != MergedLookups.end())
6456 Contexts.insert(Contexts.end(), Merged->second.begin(),
6457 Merged->second.end());
6458 }
6459
Guy Benyei11169dd2012-12-18 14:30:41 +00006460 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00006461
6462 // If we can definitively determine which module file to look into,
6463 // only look there. Otherwise, look in all module files.
6464 ModuleFile *Definitive;
6465 if (Contexts.size() == 1 &&
6466 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
6467 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6468 } else {
6469 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6470 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006471 ++NumVisibleDeclContextsRead;
6472 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006473 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006474}
6475
6476namespace {
6477 /// \brief ModuleFile visitor used to retrieve all visible names in a
6478 /// declaration context.
6479 class DeclContextAllNamesVisitor {
6480 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006481 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006482 DeclsMap &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006483 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006484
6485 public:
6486 DeclContextAllNamesVisitor(ASTReader &Reader,
6487 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006488 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006489 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006490
6491 static bool visit(ModuleFile &M, void *UserData) {
6492 DeclContextAllNamesVisitor *This
6493 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6494
6495 // Check whether we have any visible declaration information for
6496 // this context in this module.
6497 ModuleFile::DeclContextInfosMap::iterator Info;
6498 bool FoundInfo = false;
6499 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6500 Info = M.DeclContextInfos.find(This->Contexts[I]);
6501 if (Info != M.DeclContextInfos.end() &&
6502 Info->second.NameLookupTableData) {
6503 FoundInfo = true;
6504 break;
6505 }
6506 }
6507
6508 if (!FoundInfo)
6509 return false;
6510
Richard Smith52e3fba2014-03-11 07:17:35 +00006511 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006512 Info->second.NameLookupTableData;
6513 bool FoundAnything = false;
6514 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006515 I = LookupTable->data_begin(), E = LookupTable->data_end();
6516 I != E;
6517 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006518 ASTDeclContextNameLookupTrait::data_type Data = *I;
6519 for (; Data.first != Data.second; ++Data.first) {
6520 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6521 *Data.first);
6522 if (!ND)
6523 continue;
6524
6525 // Record this declaration.
6526 FoundAnything = true;
6527 This->Decls[ND->getDeclName()].push_back(ND);
6528 }
6529 }
6530
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006531 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006532 }
6533 };
6534}
6535
6536void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6537 if (!DC->hasExternalVisibleStorage())
6538 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006539 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006540
6541 // Compute the declaration contexts we need to look into. Multiple such
6542 // declaration contexts occur when two declaration contexts from disjoint
6543 // modules get merged, e.g., when two namespaces with the same name are
6544 // independently defined in separate modules.
6545 SmallVector<const DeclContext *, 2> Contexts;
6546 Contexts.push_back(DC);
6547
6548 if (DC->isNamespace()) {
6549 MergedDeclsMap::iterator Merged
6550 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6551 if (Merged != MergedDecls.end()) {
6552 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6553 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6554 }
6555 }
6556
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006557 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6558 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006559 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6560 ++NumVisibleDeclContextsRead;
6561
Craig Topper79be4cd2013-07-05 04:33:53 +00006562 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006563 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6564 }
6565 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6566}
6567
6568/// \brief Under non-PCH compilation the consumer receives the objc methods
6569/// before receiving the implementation, and codegen depends on this.
6570/// We simulate this by deserializing and passing to consumer the methods of the
6571/// implementation before passing the deserialized implementation decl.
6572static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6573 ASTConsumer *Consumer) {
6574 assert(ImplD && Consumer);
6575
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006576 for (auto *I : ImplD->methods())
6577 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006578
6579 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6580}
6581
6582void ASTReader::PassInterestingDeclsToConsumer() {
6583 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006584
6585 if (PassingDeclsToConsumer)
6586 return;
6587
6588 // Guard variable to avoid recursively redoing the process of passing
6589 // decls to consumer.
6590 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6591 true);
6592
Guy Benyei11169dd2012-12-18 14:30:41 +00006593 while (!InterestingDecls.empty()) {
6594 Decl *D = InterestingDecls.front();
6595 InterestingDecls.pop_front();
6596
6597 PassInterestingDeclToConsumer(D);
6598 }
6599}
6600
6601void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6602 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6603 PassObjCImplDeclToConsumer(ImplD, Consumer);
6604 else
6605 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6606}
6607
6608void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6609 this->Consumer = Consumer;
6610
6611 if (!Consumer)
6612 return;
6613
Ben Langmuir332aafe2014-01-31 01:06:56 +00006614 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006615 // Force deserialization of this decl, which will cause it to be queued for
6616 // passing to the consumer.
Ben Langmuir332aafe2014-01-31 01:06:56 +00006617 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006618 }
Ben Langmuir332aafe2014-01-31 01:06:56 +00006619 EagerlyDeserializedDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006620
6621 PassInterestingDeclsToConsumer();
6622}
6623
6624void ASTReader::PrintStats() {
6625 std::fprintf(stderr, "*** AST File Statistics:\n");
6626
6627 unsigned NumTypesLoaded
6628 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6629 QualType());
6630 unsigned NumDeclsLoaded
6631 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006632 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006633 unsigned NumIdentifiersLoaded
6634 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6635 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006636 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006637 unsigned NumMacrosLoaded
6638 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6639 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006640 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006641 unsigned NumSelectorsLoaded
6642 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6643 SelectorsLoaded.end(),
6644 Selector());
6645
6646 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6647 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6648 NumSLocEntriesRead, TotalNumSLocEntries,
6649 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6650 if (!TypesLoaded.empty())
6651 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6652 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6653 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6654 if (!DeclsLoaded.empty())
6655 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6656 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6657 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6658 if (!IdentifiersLoaded.empty())
6659 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6660 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6661 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6662 if (!MacrosLoaded.empty())
6663 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6664 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6665 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6666 if (!SelectorsLoaded.empty())
6667 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6668 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6669 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6670 if (TotalNumStatements)
6671 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6672 NumStatementsRead, TotalNumStatements,
6673 ((float)NumStatementsRead/TotalNumStatements * 100));
6674 if (TotalNumMacros)
6675 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6676 NumMacrosRead, TotalNumMacros,
6677 ((float)NumMacrosRead/TotalNumMacros * 100));
6678 if (TotalLexicalDeclContexts)
6679 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6680 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6681 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6682 * 100));
6683 if (TotalVisibleDeclContexts)
6684 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6685 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6686 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6687 * 100));
6688 if (TotalNumMethodPoolEntries) {
6689 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6690 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6691 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6692 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006693 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006694 if (NumMethodPoolLookups) {
6695 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6696 NumMethodPoolHits, NumMethodPoolLookups,
6697 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6698 }
6699 if (NumMethodPoolTableLookups) {
6700 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6701 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6702 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6703 * 100.0));
6704 }
6705
Douglas Gregor00a50f72013-01-25 00:38:33 +00006706 if (NumIdentifierLookupHits) {
6707 std::fprintf(stderr,
6708 " %u / %u identifier table lookups succeeded (%f%%)\n",
6709 NumIdentifierLookupHits, NumIdentifierLookups,
6710 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6711 }
6712
Douglas Gregore060e572013-01-25 01:03:03 +00006713 if (GlobalIndex) {
6714 std::fprintf(stderr, "\n");
6715 GlobalIndex->printStats();
6716 }
6717
Guy Benyei11169dd2012-12-18 14:30:41 +00006718 std::fprintf(stderr, "\n");
6719 dump();
6720 std::fprintf(stderr, "\n");
6721}
6722
6723template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6724static void
6725dumpModuleIDMap(StringRef Name,
6726 const ContinuousRangeMap<Key, ModuleFile *,
6727 InitialCapacity> &Map) {
6728 if (Map.begin() == Map.end())
6729 return;
6730
6731 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6732 llvm::errs() << Name << ":\n";
6733 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6734 I != IEnd; ++I) {
6735 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6736 << "\n";
6737 }
6738}
6739
6740void ASTReader::dump() {
6741 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6742 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6743 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6744 dumpModuleIDMap("Global type map", GlobalTypeMap);
6745 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6746 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6747 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6748 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6749 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6750 dumpModuleIDMap("Global preprocessed entity map",
6751 GlobalPreprocessedEntityMap);
6752
6753 llvm::errs() << "\n*** PCH/Modules Loaded:";
6754 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6755 MEnd = ModuleMgr.end();
6756 M != MEnd; ++M)
6757 (*M)->dump();
6758}
6759
6760/// Return the amount of memory used by memory buffers, breaking down
6761/// by heap-backed versus mmap'ed memory.
6762void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6763 for (ModuleConstIterator I = ModuleMgr.begin(),
6764 E = ModuleMgr.end(); I != E; ++I) {
6765 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6766 size_t bytes = buf->getBufferSize();
6767 switch (buf->getBufferKind()) {
6768 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6769 sizes.malloc_bytes += bytes;
6770 break;
6771 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6772 sizes.mmap_bytes += bytes;
6773 break;
6774 }
6775 }
6776 }
6777}
6778
6779void ASTReader::InitializeSema(Sema &S) {
6780 SemaObj = &S;
6781 S.addExternalSource(this);
6782
6783 // Makes sure any declarations that were deserialized "too early"
6784 // still get added to the identifier's declaration chains.
6785 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006786 pushExternalDeclIntoScope(PreloadedDecls[I],
6787 PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006788 }
6789 PreloadedDecls.clear();
6790
Richard Smith3d8e97e2013-10-18 06:54:39 +00006791 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006792 if (!FPPragmaOptions.empty()) {
6793 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6794 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6795 }
6796
Richard Smith3d8e97e2013-10-18 06:54:39 +00006797 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006798 if (!OpenCLExtensions.empty()) {
6799 unsigned I = 0;
6800#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6801#include "clang/Basic/OpenCLExtensions.def"
6802
6803 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6804 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006805
6806 UpdateSema();
6807}
6808
6809void ASTReader::UpdateSema() {
6810 assert(SemaObj && "no Sema to update");
6811
6812 // Load the offsets of the declarations that Sema references.
6813 // They will be lazily deserialized when needed.
6814 if (!SemaDeclRefs.empty()) {
6815 assert(SemaDeclRefs.size() % 2 == 0);
6816 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6817 if (!SemaObj->StdNamespace)
6818 SemaObj->StdNamespace = SemaDeclRefs[I];
6819 if (!SemaObj->StdBadAlloc)
6820 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6821 }
6822 SemaDeclRefs.clear();
6823 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006824
6825 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6826 // encountered the pragma in the source.
6827 if(OptimizeOffPragmaLocation.isValid())
6828 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei11169dd2012-12-18 14:30:41 +00006829}
6830
6831IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6832 // Note that we are loading an identifier.
6833 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006834 StringRef Name(NameStart, NameEnd - NameStart);
6835
6836 // If there is a global index, look there first to determine which modules
6837 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006838 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00006839 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00006840 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006841 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6842 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006843 }
6844 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006845 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006846 NumIdentifierLookups,
6847 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006848 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006849 IdentifierInfo *II = Visitor.getIdentifierInfo();
6850 markIdentifierUpToDate(II);
6851 return II;
6852}
6853
6854namespace clang {
6855 /// \brief An identifier-lookup iterator that enumerates all of the
6856 /// identifiers stored within a set of AST files.
6857 class ASTIdentifierIterator : public IdentifierIterator {
6858 /// \brief The AST reader whose identifiers are being enumerated.
6859 const ASTReader &Reader;
6860
6861 /// \brief The current index into the chain of AST files stored in
6862 /// the AST reader.
6863 unsigned Index;
6864
6865 /// \brief The current position within the identifier lookup table
6866 /// of the current AST file.
6867 ASTIdentifierLookupTable::key_iterator Current;
6868
6869 /// \brief The end position within the identifier lookup table of
6870 /// the current AST file.
6871 ASTIdentifierLookupTable::key_iterator End;
6872
6873 public:
6874 explicit ASTIdentifierIterator(const ASTReader &Reader);
6875
Craig Topper3e89dfe2014-03-13 02:13:41 +00006876 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006877 };
6878}
6879
6880ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6881 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6882 ASTIdentifierLookupTable *IdTable
6883 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6884 Current = IdTable->key_begin();
6885 End = IdTable->key_end();
6886}
6887
6888StringRef ASTIdentifierIterator::Next() {
6889 while (Current == End) {
6890 // If we have exhausted all of our AST files, we're done.
6891 if (Index == 0)
6892 return StringRef();
6893
6894 --Index;
6895 ASTIdentifierLookupTable *IdTable
6896 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6897 IdentifierLookupTable;
6898 Current = IdTable->key_begin();
6899 End = IdTable->key_end();
6900 }
6901
6902 // We have any identifiers remaining in the current AST file; return
6903 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006904 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006905 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006906 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006907}
6908
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006909IdentifierIterator *ASTReader::getIdentifiers() {
6910 if (!loadGlobalIndex())
6911 return GlobalIndex->createIdentifierIterator();
6912
Guy Benyei11169dd2012-12-18 14:30:41 +00006913 return new ASTIdentifierIterator(*this);
6914}
6915
6916namespace clang { namespace serialization {
6917 class ReadMethodPoolVisitor {
6918 ASTReader &Reader;
6919 Selector Sel;
6920 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006921 unsigned InstanceBits;
6922 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006923 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6924 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006925
6926 public:
6927 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6928 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006929 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6930 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006931
6932 static bool visit(ModuleFile &M, void *UserData) {
6933 ReadMethodPoolVisitor *This
6934 = static_cast<ReadMethodPoolVisitor *>(UserData);
6935
6936 if (!M.SelectorLookupTable)
6937 return false;
6938
6939 // If we've already searched this module file, skip it now.
6940 if (M.Generation <= This->PriorGeneration)
6941 return true;
6942
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006943 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006944 ASTSelectorLookupTable *PoolTable
6945 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6946 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6947 if (Pos == PoolTable->end())
6948 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006949
6950 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006951 ++This->Reader.NumSelectorsRead;
6952 // FIXME: Not quite happy with the statistics here. We probably should
6953 // disable this tracking when called via LoadSelector.
6954 // Also, should entries without methods count as misses?
6955 ++This->Reader.NumMethodPoolEntriesRead;
6956 ASTSelectorLookupTrait::data_type Data = *Pos;
6957 if (This->Reader.DeserializationListener)
6958 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6959 This->Sel);
6960
6961 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6962 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006963 This->InstanceBits = Data.InstanceBits;
6964 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006965 return true;
6966 }
6967
6968 /// \brief Retrieve the instance methods found by this visitor.
6969 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6970 return InstanceMethods;
6971 }
6972
6973 /// \brief Retrieve the instance methods found by this visitor.
6974 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6975 return FactoryMethods;
6976 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006977
6978 unsigned getInstanceBits() const { return InstanceBits; }
6979 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006980 };
6981} } // end namespace clang::serialization
6982
6983/// \brief Add the given set of methods to the method list.
6984static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6985 ObjCMethodList &List) {
6986 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6987 S.addMethodToGlobalList(&List, Methods[I]);
6988 }
6989}
6990
6991void ASTReader::ReadMethodPool(Selector Sel) {
6992 // Get the selector generation and update it to the current generation.
6993 unsigned &Generation = SelectorGeneration[Sel];
6994 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00006995 Generation = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00006996
6997 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006998 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006999 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7000 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7001
7002 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007003 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007004 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007005
7006 ++NumMethodPoolHits;
7007
Guy Benyei11169dd2012-12-18 14:30:41 +00007008 if (!getSema())
7009 return;
7010
7011 Sema &S = *getSema();
7012 Sema::GlobalMethodPool::iterator Pos
7013 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7014
7015 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7016 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007017 Pos->second.first.setBits(Visitor.getInstanceBits());
7018 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00007019}
7020
7021void ASTReader::ReadKnownNamespaces(
7022 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7023 Namespaces.clear();
7024
7025 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7026 if (NamespaceDecl *Namespace
7027 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7028 Namespaces.push_back(Namespace);
7029 }
7030}
7031
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007032void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00007033 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007034 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7035 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007036 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007037 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007038 Undefined.insert(std::make_pair(D, Loc));
7039 }
7040}
Nick Lewycky8334af82013-01-26 00:35:08 +00007041
Guy Benyei11169dd2012-12-18 14:30:41 +00007042void ASTReader::ReadTentativeDefinitions(
7043 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7044 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7045 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7046 if (Var)
7047 TentativeDefs.push_back(Var);
7048 }
7049 TentativeDefinitions.clear();
7050}
7051
7052void ASTReader::ReadUnusedFileScopedDecls(
7053 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7054 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7055 DeclaratorDecl *D
7056 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7057 if (D)
7058 Decls.push_back(D);
7059 }
7060 UnusedFileScopedDecls.clear();
7061}
7062
7063void ASTReader::ReadDelegatingConstructors(
7064 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7065 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7066 CXXConstructorDecl *D
7067 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7068 if (D)
7069 Decls.push_back(D);
7070 }
7071 DelegatingCtorDecls.clear();
7072}
7073
7074void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7075 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7076 TypedefNameDecl *D
7077 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7078 if (D)
7079 Decls.push_back(D);
7080 }
7081 ExtVectorDecls.clear();
7082}
7083
7084void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7085 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7086 CXXRecordDecl *D
7087 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7088 if (D)
7089 Decls.push_back(D);
7090 }
7091 DynamicClasses.clear();
7092}
7093
7094void
Richard Smith78165b52013-01-10 23:43:47 +00007095ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7096 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7097 NamedDecl *D
7098 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00007099 if (D)
7100 Decls.push_back(D);
7101 }
Richard Smith78165b52013-01-10 23:43:47 +00007102 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007103}
7104
7105void ASTReader::ReadReferencedSelectors(
7106 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7107 if (ReferencedSelectorsData.empty())
7108 return;
7109
7110 // If there are @selector references added them to its pool. This is for
7111 // implementation of -Wselector.
7112 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7113 unsigned I = 0;
7114 while (I < DataSize) {
7115 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7116 SourceLocation SelLoc
7117 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7118 Sels.push_back(std::make_pair(Sel, SelLoc));
7119 }
7120 ReferencedSelectorsData.clear();
7121}
7122
7123void ASTReader::ReadWeakUndeclaredIdentifiers(
7124 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7125 if (WeakUndeclaredIdentifiers.empty())
7126 return;
7127
7128 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7129 IdentifierInfo *WeakId
7130 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7131 IdentifierInfo *AliasId
7132 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7133 SourceLocation Loc
7134 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7135 bool Used = WeakUndeclaredIdentifiers[I++];
7136 WeakInfo WI(AliasId, Loc);
7137 WI.setUsed(Used);
7138 WeakIDs.push_back(std::make_pair(WeakId, WI));
7139 }
7140 WeakUndeclaredIdentifiers.clear();
7141}
7142
7143void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7144 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7145 ExternalVTableUse VT;
7146 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7147 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7148 VT.DefinitionRequired = VTableUses[Idx++];
7149 VTables.push_back(VT);
7150 }
7151
7152 VTableUses.clear();
7153}
7154
7155void ASTReader::ReadPendingInstantiations(
7156 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7157 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7158 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7159 SourceLocation Loc
7160 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7161
7162 Pending.push_back(std::make_pair(D, Loc));
7163 }
7164 PendingInstantiations.clear();
7165}
7166
Richard Smithe40f2ba2013-08-07 21:41:30 +00007167void ASTReader::ReadLateParsedTemplates(
7168 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7169 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7170 /* In loop */) {
7171 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7172
7173 LateParsedTemplate *LT = new LateParsedTemplate;
7174 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7175
7176 ModuleFile *F = getOwningModuleFile(LT->D);
7177 assert(F && "No module");
7178
7179 unsigned TokN = LateParsedTemplates[Idx++];
7180 LT->Toks.reserve(TokN);
7181 for (unsigned T = 0; T < TokN; ++T)
7182 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7183
7184 LPTMap[FD] = LT;
7185 }
7186
7187 LateParsedTemplates.clear();
7188}
7189
Guy Benyei11169dd2012-12-18 14:30:41 +00007190void ASTReader::LoadSelector(Selector Sel) {
7191 // It would be complicated to avoid reading the methods anyway. So don't.
7192 ReadMethodPool(Sel);
7193}
7194
7195void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7196 assert(ID && "Non-zero identifier ID required");
7197 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7198 IdentifiersLoaded[ID - 1] = II;
7199 if (DeserializationListener)
7200 DeserializationListener->IdentifierRead(ID, II);
7201}
7202
7203/// \brief Set the globally-visible declarations associated with the given
7204/// identifier.
7205///
7206/// If the AST reader is currently in a state where the given declaration IDs
7207/// cannot safely be resolved, they are queued until it is safe to resolve
7208/// them.
7209///
7210/// \param II an IdentifierInfo that refers to one or more globally-visible
7211/// declarations.
7212///
7213/// \param DeclIDs the set of declaration IDs with the name @p II that are
7214/// visible at global scope.
7215///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007216/// \param Decls if non-null, this vector will be populated with the set of
7217/// deserialized declarations. These declarations will not be pushed into
7218/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007219void
7220ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7221 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007222 SmallVectorImpl<Decl *> *Decls) {
7223 if (NumCurrentElementsDeserializing && !Decls) {
7224 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007225 return;
7226 }
7227
7228 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7229 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7230 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007231 // If we're simply supposed to record the declarations, do so now.
7232 if (Decls) {
7233 Decls->push_back(D);
7234 continue;
7235 }
7236
Guy Benyei11169dd2012-12-18 14:30:41 +00007237 // Introduce this declaration into the translation-unit scope
7238 // and add it to the declaration chain for this identifier, so
7239 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007240 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007241 } else {
7242 // Queue this declaration so that it will be added to the
7243 // translation unit scope and identifier's declaration chain
7244 // once a Sema object is known.
7245 PreloadedDecls.push_back(D);
7246 }
7247 }
7248}
7249
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007250IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007251 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007252 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007253
7254 if (IdentifiersLoaded.empty()) {
7255 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007256 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007257 }
7258
7259 ID -= 1;
7260 if (!IdentifiersLoaded[ID]) {
7261 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7262 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7263 ModuleFile *M = I->second;
7264 unsigned Index = ID - M->BaseIdentifierID;
7265 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7266
7267 // All of the strings in the AST file are preceded by a 16-bit length.
7268 // Extract that 16-bit length to avoid having to execute strlen().
7269 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7270 // unsigned integers. This is important to avoid integer overflow when
7271 // we cast them to 'unsigned'.
7272 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7273 unsigned StrLen = (((unsigned) StrLenPtr[0])
7274 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007275 IdentifiersLoaded[ID]
7276 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00007277 if (DeserializationListener)
7278 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7279 }
7280
7281 return IdentifiersLoaded[ID];
7282}
7283
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007284IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7285 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007286}
7287
7288IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7289 if (LocalID < NUM_PREDEF_IDENT_IDS)
7290 return LocalID;
7291
7292 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7293 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7294 assert(I != M.IdentifierRemap.end()
7295 && "Invalid index into identifier index remap");
7296
7297 return LocalID + I->second;
7298}
7299
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007300MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007301 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007302 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007303
7304 if (MacrosLoaded.empty()) {
7305 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007306 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007307 }
7308
7309 ID -= NUM_PREDEF_MACRO_IDS;
7310 if (!MacrosLoaded[ID]) {
7311 GlobalMacroMapType::iterator I
7312 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7313 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7314 ModuleFile *M = I->second;
7315 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007316 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7317
7318 if (DeserializationListener)
7319 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7320 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007321 }
7322
7323 return MacrosLoaded[ID];
7324}
7325
7326MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7327 if (LocalID < NUM_PREDEF_MACRO_IDS)
7328 return LocalID;
7329
7330 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7331 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7332 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7333
7334 return LocalID + I->second;
7335}
7336
7337serialization::SubmoduleID
7338ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7339 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7340 return LocalID;
7341
7342 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7343 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7344 assert(I != M.SubmoduleRemap.end()
7345 && "Invalid index into submodule index remap");
7346
7347 return LocalID + I->second;
7348}
7349
7350Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7351 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7352 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007353 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007354 }
7355
7356 if (GlobalID > SubmodulesLoaded.size()) {
7357 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007358 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007359 }
7360
7361 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7362}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007363
7364Module *ASTReader::getModule(unsigned ID) {
7365 return getSubmodule(ID);
7366}
7367
Guy Benyei11169dd2012-12-18 14:30:41 +00007368Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7369 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7370}
7371
7372Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7373 if (ID == 0)
7374 return Selector();
7375
7376 if (ID > SelectorsLoaded.size()) {
7377 Error("selector ID out of range in AST file");
7378 return Selector();
7379 }
7380
Craig Toppera13603a2014-05-22 05:54:18 +00007381 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007382 // Load this selector from the selector table.
7383 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7384 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7385 ModuleFile &M = *I->second;
7386 ASTSelectorLookupTrait Trait(*this, M);
7387 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7388 SelectorsLoaded[ID - 1] =
7389 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7390 if (DeserializationListener)
7391 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7392 }
7393
7394 return SelectorsLoaded[ID - 1];
7395}
7396
7397Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7398 return DecodeSelector(ID);
7399}
7400
7401uint32_t ASTReader::GetNumExternalSelectors() {
7402 // ID 0 (the null selector) is considered an external selector.
7403 return getTotalNumSelectors() + 1;
7404}
7405
7406serialization::SelectorID
7407ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7408 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7409 return LocalID;
7410
7411 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7412 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7413 assert(I != M.SelectorRemap.end()
7414 && "Invalid index into selector index remap");
7415
7416 return LocalID + I->second;
7417}
7418
7419DeclarationName
7420ASTReader::ReadDeclarationName(ModuleFile &F,
7421 const RecordData &Record, unsigned &Idx) {
7422 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7423 switch (Kind) {
7424 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007425 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007426
7427 case DeclarationName::ObjCZeroArgSelector:
7428 case DeclarationName::ObjCOneArgSelector:
7429 case DeclarationName::ObjCMultiArgSelector:
7430 return DeclarationName(ReadSelector(F, Record, Idx));
7431
7432 case DeclarationName::CXXConstructorName:
7433 return Context.DeclarationNames.getCXXConstructorName(
7434 Context.getCanonicalType(readType(F, Record, Idx)));
7435
7436 case DeclarationName::CXXDestructorName:
7437 return Context.DeclarationNames.getCXXDestructorName(
7438 Context.getCanonicalType(readType(F, Record, Idx)));
7439
7440 case DeclarationName::CXXConversionFunctionName:
7441 return Context.DeclarationNames.getCXXConversionFunctionName(
7442 Context.getCanonicalType(readType(F, Record, Idx)));
7443
7444 case DeclarationName::CXXOperatorName:
7445 return Context.DeclarationNames.getCXXOperatorName(
7446 (OverloadedOperatorKind)Record[Idx++]);
7447
7448 case DeclarationName::CXXLiteralOperatorName:
7449 return Context.DeclarationNames.getCXXLiteralOperatorName(
7450 GetIdentifierInfo(F, Record, Idx));
7451
7452 case DeclarationName::CXXUsingDirective:
7453 return DeclarationName::getUsingDirectiveName();
7454 }
7455
7456 llvm_unreachable("Invalid NameKind!");
7457}
7458
7459void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7460 DeclarationNameLoc &DNLoc,
7461 DeclarationName Name,
7462 const RecordData &Record, unsigned &Idx) {
7463 switch (Name.getNameKind()) {
7464 case DeclarationName::CXXConstructorName:
7465 case DeclarationName::CXXDestructorName:
7466 case DeclarationName::CXXConversionFunctionName:
7467 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7468 break;
7469
7470 case DeclarationName::CXXOperatorName:
7471 DNLoc.CXXOperatorName.BeginOpNameLoc
7472 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7473 DNLoc.CXXOperatorName.EndOpNameLoc
7474 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7475 break;
7476
7477 case DeclarationName::CXXLiteralOperatorName:
7478 DNLoc.CXXLiteralOperatorName.OpNameLoc
7479 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7480 break;
7481
7482 case DeclarationName::Identifier:
7483 case DeclarationName::ObjCZeroArgSelector:
7484 case DeclarationName::ObjCOneArgSelector:
7485 case DeclarationName::ObjCMultiArgSelector:
7486 case DeclarationName::CXXUsingDirective:
7487 break;
7488 }
7489}
7490
7491void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7492 DeclarationNameInfo &NameInfo,
7493 const RecordData &Record, unsigned &Idx) {
7494 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7495 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7496 DeclarationNameLoc DNLoc;
7497 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7498 NameInfo.setInfo(DNLoc);
7499}
7500
7501void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7502 const RecordData &Record, unsigned &Idx) {
7503 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7504 unsigned NumTPLists = Record[Idx++];
7505 Info.NumTemplParamLists = NumTPLists;
7506 if (NumTPLists) {
7507 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7508 for (unsigned i=0; i != NumTPLists; ++i)
7509 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7510 }
7511}
7512
7513TemplateName
7514ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7515 unsigned &Idx) {
7516 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7517 switch (Kind) {
7518 case TemplateName::Template:
7519 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7520
7521 case TemplateName::OverloadedTemplate: {
7522 unsigned size = Record[Idx++];
7523 UnresolvedSet<8> Decls;
7524 while (size--)
7525 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7526
7527 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7528 }
7529
7530 case TemplateName::QualifiedTemplate: {
7531 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7532 bool hasTemplKeyword = Record[Idx++];
7533 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7534 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7535 }
7536
7537 case TemplateName::DependentTemplate: {
7538 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7539 if (Record[Idx++]) // isIdentifier
7540 return Context.getDependentTemplateName(NNS,
7541 GetIdentifierInfo(F, Record,
7542 Idx));
7543 return Context.getDependentTemplateName(NNS,
7544 (OverloadedOperatorKind)Record[Idx++]);
7545 }
7546
7547 case TemplateName::SubstTemplateTemplateParm: {
7548 TemplateTemplateParmDecl *param
7549 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7550 if (!param) return TemplateName();
7551 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7552 return Context.getSubstTemplateTemplateParm(param, replacement);
7553 }
7554
7555 case TemplateName::SubstTemplateTemplateParmPack: {
7556 TemplateTemplateParmDecl *Param
7557 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7558 if (!Param)
7559 return TemplateName();
7560
7561 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7562 if (ArgPack.getKind() != TemplateArgument::Pack)
7563 return TemplateName();
7564
7565 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7566 }
7567 }
7568
7569 llvm_unreachable("Unhandled template name kind!");
7570}
7571
7572TemplateArgument
7573ASTReader::ReadTemplateArgument(ModuleFile &F,
7574 const RecordData &Record, unsigned &Idx) {
7575 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7576 switch (Kind) {
7577 case TemplateArgument::Null:
7578 return TemplateArgument();
7579 case TemplateArgument::Type:
7580 return TemplateArgument(readType(F, Record, Idx));
7581 case TemplateArgument::Declaration: {
7582 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7583 bool ForReferenceParam = Record[Idx++];
7584 return TemplateArgument(D, ForReferenceParam);
7585 }
7586 case TemplateArgument::NullPtr:
7587 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7588 case TemplateArgument::Integral: {
7589 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7590 QualType T = readType(F, Record, Idx);
7591 return TemplateArgument(Context, Value, T);
7592 }
7593 case TemplateArgument::Template:
7594 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7595 case TemplateArgument::TemplateExpansion: {
7596 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007597 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007598 if (unsigned NumExpansions = Record[Idx++])
7599 NumTemplateExpansions = NumExpansions - 1;
7600 return TemplateArgument(Name, NumTemplateExpansions);
7601 }
7602 case TemplateArgument::Expression:
7603 return TemplateArgument(ReadExpr(F));
7604 case TemplateArgument::Pack: {
7605 unsigned NumArgs = Record[Idx++];
7606 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7607 for (unsigned I = 0; I != NumArgs; ++I)
7608 Args[I] = ReadTemplateArgument(F, Record, Idx);
7609 return TemplateArgument(Args, NumArgs);
7610 }
7611 }
7612
7613 llvm_unreachable("Unhandled template argument kind!");
7614}
7615
7616TemplateParameterList *
7617ASTReader::ReadTemplateParameterList(ModuleFile &F,
7618 const RecordData &Record, unsigned &Idx) {
7619 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7620 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7621 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7622
7623 unsigned NumParams = Record[Idx++];
7624 SmallVector<NamedDecl *, 16> Params;
7625 Params.reserve(NumParams);
7626 while (NumParams--)
7627 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7628
7629 TemplateParameterList* TemplateParams =
7630 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7631 Params.data(), Params.size(), RAngleLoc);
7632 return TemplateParams;
7633}
7634
7635void
7636ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007637ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007638 ModuleFile &F, const RecordData &Record,
7639 unsigned &Idx) {
7640 unsigned NumTemplateArgs = Record[Idx++];
7641 TemplArgs.reserve(NumTemplateArgs);
7642 while (NumTemplateArgs--)
7643 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7644}
7645
7646/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007647void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007648 const RecordData &Record, unsigned &Idx) {
7649 unsigned NumDecls = Record[Idx++];
7650 Set.reserve(Context, NumDecls);
7651 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007652 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007653 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007654 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007655 }
7656}
7657
7658CXXBaseSpecifier
7659ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7660 const RecordData &Record, unsigned &Idx) {
7661 bool isVirtual = static_cast<bool>(Record[Idx++]);
7662 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7663 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7664 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7665 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7666 SourceRange Range = ReadSourceRange(F, Record, Idx);
7667 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7668 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7669 EllipsisLoc);
7670 Result.setInheritConstructors(inheritConstructors);
7671 return Result;
7672}
7673
7674std::pair<CXXCtorInitializer **, unsigned>
7675ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7676 unsigned &Idx) {
Craig Toppera13603a2014-05-22 05:54:18 +00007677 CXXCtorInitializer **CtorInitializers = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007678 unsigned NumInitializers = Record[Idx++];
7679 if (NumInitializers) {
7680 CtorInitializers
7681 = new (Context) CXXCtorInitializer*[NumInitializers];
7682 for (unsigned i=0; i != NumInitializers; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00007683 TypeSourceInfo *TInfo = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007684 bool IsBaseVirtual = false;
Craig Toppera13603a2014-05-22 05:54:18 +00007685 FieldDecl *Member = nullptr;
7686 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007687
7688 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7689 switch (Type) {
7690 case CTOR_INITIALIZER_BASE:
7691 TInfo = GetTypeSourceInfo(F, Record, Idx);
7692 IsBaseVirtual = Record[Idx++];
7693 break;
7694
7695 case CTOR_INITIALIZER_DELEGATING:
7696 TInfo = GetTypeSourceInfo(F, Record, Idx);
7697 break;
7698
7699 case CTOR_INITIALIZER_MEMBER:
7700 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7701 break;
7702
7703 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7704 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7705 break;
7706 }
7707
7708 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7709 Expr *Init = ReadExpr(F);
7710 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7711 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7712 bool IsWritten = Record[Idx++];
7713 unsigned SourceOrderOrNumArrayIndices;
7714 SmallVector<VarDecl *, 8> Indices;
7715 if (IsWritten) {
7716 SourceOrderOrNumArrayIndices = Record[Idx++];
7717 } else {
7718 SourceOrderOrNumArrayIndices = Record[Idx++];
7719 Indices.reserve(SourceOrderOrNumArrayIndices);
7720 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7721 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7722 }
7723
7724 CXXCtorInitializer *BOMInit;
7725 if (Type == CTOR_INITIALIZER_BASE) {
7726 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7727 LParenLoc, Init, RParenLoc,
7728 MemberOrEllipsisLoc);
7729 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7730 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7731 Init, RParenLoc);
7732 } else if (IsWritten) {
7733 if (Member)
7734 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7735 LParenLoc, Init, RParenLoc);
7736 else
7737 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7738 MemberOrEllipsisLoc, LParenLoc,
7739 Init, RParenLoc);
7740 } else {
Argyrios Kyrtzidis794671d2013-05-30 23:59:46 +00007741 if (IndirectMember) {
7742 assert(Indices.empty() && "Indirect field improperly initialized");
7743 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7744 MemberOrEllipsisLoc, LParenLoc,
7745 Init, RParenLoc);
7746 } else {
7747 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7748 LParenLoc, Init, RParenLoc,
7749 Indices.data(), Indices.size());
7750 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007751 }
7752
7753 if (IsWritten)
7754 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7755 CtorInitializers[i] = BOMInit;
7756 }
7757 }
7758
7759 return std::make_pair(CtorInitializers, NumInitializers);
7760}
7761
7762NestedNameSpecifier *
7763ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7764 const RecordData &Record, unsigned &Idx) {
7765 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00007766 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007767 for (unsigned I = 0; I != N; ++I) {
7768 NestedNameSpecifier::SpecifierKind Kind
7769 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7770 switch (Kind) {
7771 case NestedNameSpecifier::Identifier: {
7772 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7773 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7774 break;
7775 }
7776
7777 case NestedNameSpecifier::Namespace: {
7778 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7779 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7780 break;
7781 }
7782
7783 case NestedNameSpecifier::NamespaceAlias: {
7784 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7785 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7786 break;
7787 }
7788
7789 case NestedNameSpecifier::TypeSpec:
7790 case NestedNameSpecifier::TypeSpecWithTemplate: {
7791 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7792 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00007793 return nullptr;
7794
Guy Benyei11169dd2012-12-18 14:30:41 +00007795 bool Template = Record[Idx++];
7796 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7797 break;
7798 }
7799
7800 case NestedNameSpecifier::Global: {
7801 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7802 // No associated value, and there can't be a prefix.
7803 break;
7804 }
7805 }
7806 Prev = NNS;
7807 }
7808 return NNS;
7809}
7810
7811NestedNameSpecifierLoc
7812ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7813 unsigned &Idx) {
7814 unsigned N = Record[Idx++];
7815 NestedNameSpecifierLocBuilder Builder;
7816 for (unsigned I = 0; I != N; ++I) {
7817 NestedNameSpecifier::SpecifierKind Kind
7818 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7819 switch (Kind) {
7820 case NestedNameSpecifier::Identifier: {
7821 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7822 SourceRange Range = ReadSourceRange(F, Record, Idx);
7823 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7824 break;
7825 }
7826
7827 case NestedNameSpecifier::Namespace: {
7828 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7829 SourceRange Range = ReadSourceRange(F, Record, Idx);
7830 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7831 break;
7832 }
7833
7834 case NestedNameSpecifier::NamespaceAlias: {
7835 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7836 SourceRange Range = ReadSourceRange(F, Record, Idx);
7837 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7838 break;
7839 }
7840
7841 case NestedNameSpecifier::TypeSpec:
7842 case NestedNameSpecifier::TypeSpecWithTemplate: {
7843 bool Template = Record[Idx++];
7844 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7845 if (!T)
7846 return NestedNameSpecifierLoc();
7847 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7848
7849 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7850 Builder.Extend(Context,
7851 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7852 T->getTypeLoc(), ColonColonLoc);
7853 break;
7854 }
7855
7856 case NestedNameSpecifier::Global: {
7857 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7858 Builder.MakeGlobal(Context, ColonColonLoc);
7859 break;
7860 }
7861 }
7862 }
7863
7864 return Builder.getWithLocInContext(Context);
7865}
7866
7867SourceRange
7868ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7869 unsigned &Idx) {
7870 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7871 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7872 return SourceRange(beg, end);
7873}
7874
7875/// \brief Read an integral value
7876llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7877 unsigned BitWidth = Record[Idx++];
7878 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7879 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7880 Idx += NumWords;
7881 return Result;
7882}
7883
7884/// \brief Read a signed integral value
7885llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7886 bool isUnsigned = Record[Idx++];
7887 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7888}
7889
7890/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007891llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7892 const llvm::fltSemantics &Sem,
7893 unsigned &Idx) {
7894 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007895}
7896
7897// \brief Read a string
7898std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7899 unsigned Len = Record[Idx++];
7900 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7901 Idx += Len;
7902 return Result;
7903}
7904
7905VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7906 unsigned &Idx) {
7907 unsigned Major = Record[Idx++];
7908 unsigned Minor = Record[Idx++];
7909 unsigned Subminor = Record[Idx++];
7910 if (Minor == 0)
7911 return VersionTuple(Major);
7912 if (Subminor == 0)
7913 return VersionTuple(Major, Minor - 1);
7914 return VersionTuple(Major, Minor - 1, Subminor - 1);
7915}
7916
7917CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7918 const RecordData &Record,
7919 unsigned &Idx) {
7920 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7921 return CXXTemporary::Create(Context, Decl);
7922}
7923
7924DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00007925 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007926}
7927
7928DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7929 return Diags.Report(Loc, DiagID);
7930}
7931
7932/// \brief Retrieve the identifier table associated with the
7933/// preprocessor.
7934IdentifierTable &ASTReader::getIdentifierTable() {
7935 return PP.getIdentifierTable();
7936}
7937
7938/// \brief Record that the given ID maps to the given switch-case
7939/// statement.
7940void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00007941 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007942 "Already have a SwitchCase with this ID");
7943 (*CurrSwitchCaseStmts)[ID] = SC;
7944}
7945
7946/// \brief Retrieve the switch-case statement with the given ID.
7947SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00007948 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00007949 return (*CurrSwitchCaseStmts)[ID];
7950}
7951
7952void ASTReader::ClearSwitchCaseIDs() {
7953 CurrSwitchCaseStmts->clear();
7954}
7955
7956void ASTReader::ReadComments() {
7957 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007958 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007959 serialization::ModuleFile *> >::iterator
7960 I = CommentsCursors.begin(),
7961 E = CommentsCursors.end();
7962 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00007963 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007964 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007965 serialization::ModuleFile &F = *I->second;
7966 SavedStreamPosition SavedPosition(Cursor);
7967
7968 RecordData Record;
7969 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007970 llvm::BitstreamEntry Entry =
7971 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00007972
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007973 switch (Entry.Kind) {
7974 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7975 case llvm::BitstreamEntry::Error:
7976 Error("malformed block record in AST file");
7977 return;
7978 case llvm::BitstreamEntry::EndBlock:
7979 goto NextCursor;
7980 case llvm::BitstreamEntry::Record:
7981 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007982 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007983 }
7984
7985 // Read a record.
7986 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007987 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007988 case COMMENTS_RAW_COMMENT: {
7989 unsigned Idx = 0;
7990 SourceRange SR = ReadSourceRange(F, Record, Idx);
7991 RawComment::CommentKind Kind =
7992 (RawComment::CommentKind) Record[Idx++];
7993 bool IsTrailingComment = Record[Idx++];
7994 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00007995 Comments.push_back(new (Context) RawComment(
7996 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7997 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00007998 break;
7999 }
8000 }
8001 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008002 NextCursor:
8003 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008004 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008005}
8006
Richard Smithcd45dbc2014-04-19 03:48:30 +00008007std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8008 // If we know the owning module, use it.
8009 if (Module *M = D->getOwningModule())
8010 return M->getFullModuleName();
8011
8012 // Otherwise, use the name of the top-level module the decl is within.
8013 if (ModuleFile *M = getOwningModuleFile(D))
8014 return M->ModuleName;
8015
8016 // Not from a module.
8017 return "";
8018}
8019
Guy Benyei11169dd2012-12-18 14:30:41 +00008020void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008021 while (!PendingIdentifierInfos.empty() ||
8022 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008023 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smith93914a92014-05-08 00:25:01 +00008024 !PendingUpdateRecords.empty() || !PendingOdrMergeChecks.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008025 // If any identifiers with corresponding top-level declarations have
8026 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008027 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8028 TopLevelDeclsMap;
8029 TopLevelDeclsMap TopLevelDecls;
8030
Guy Benyei11169dd2012-12-18 14:30:41 +00008031 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008032 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008033 SmallVector<uint32_t, 4> DeclIDs =
8034 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008035 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008036
8037 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008038 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008039
Richard Smith851072e2014-05-19 20:59:20 +00008040 // For each decl chain that we wanted to complete while deserializing, mark
8041 // it as "still needs to be completed".
8042 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8043 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8044 }
8045 PendingIncompleteDeclChains.clear();
8046
Guy Benyei11169dd2012-12-18 14:30:41 +00008047 // Load pending declaration chains.
8048 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8049 loadPendingDeclChain(PendingDeclChains[I]);
8050 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8051 }
8052 PendingDeclChains.clear();
8053
Douglas Gregor6168bd22013-02-18 15:53:43 +00008054 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008055 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8056 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008057 IdentifierInfo *II = TLD->first;
8058 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008059 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008060 }
8061 }
8062
Guy Benyei11169dd2012-12-18 14:30:41 +00008063 // Load any pending macro definitions.
8064 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008065 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8066 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8067 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8068 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008069 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008070 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008071 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8072 if (Info.M->Kind != MK_Module)
8073 resolvePendingMacro(II, Info);
8074 }
8075 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008076 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008077 ++IDIdx) {
8078 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8079 if (Info.M->Kind == MK_Module)
8080 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008081 }
8082 }
8083 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008084
8085 // Wire up the DeclContexts for Decls that we delayed setting until
8086 // recursive loading is completed.
8087 while (!PendingDeclContextInfos.empty()) {
8088 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8089 PendingDeclContextInfos.pop_front();
8090 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8091 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8092 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8093 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008094
Richard Smithd1c46742014-04-30 02:24:17 +00008095 // Perform any pending declaration updates.
Richard Smith675d2792014-06-16 20:26:19 +00008096 //
8097 // Don't do this if we have known-incomplete redecl chains: it relies on
8098 // being able to walk redeclaration chains.
8099 while (PendingDeclChains.empty() && !PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008100 auto Update = PendingUpdateRecords.pop_back_val();
8101 ReadingKindTracker ReadingKind(Read_Decl, *this);
8102 loadDeclUpdateRecords(Update.first, Update.second);
8103 }
8104
Richard Smithcd45dbc2014-04-19 03:48:30 +00008105 // Trigger the import of the full definition of each class that had any
8106 // odr-merging problems, so we can produce better diagnostics for them.
8107 for (auto &Merge : PendingOdrMergeFailures) {
8108 Merge.first->buildLookup();
8109 Merge.first->decls_begin();
8110 Merge.first->bases_begin();
8111 Merge.first->vbases_begin();
8112 for (auto *RD : Merge.second) {
8113 RD->decls_begin();
8114 RD->bases_begin();
8115 RD->vbases_begin();
8116 }
8117 }
8118
Richard Smith2b9e3e32013-10-18 06:05:18 +00008119 // For each declaration from a merged context, check that the canonical
8120 // definition of that context also contains a declaration of the same
8121 // entity.
8122 while (!PendingOdrMergeChecks.empty()) {
8123 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8124
8125 // FIXME: Skip over implicit declarations for now. This matters for things
8126 // like implicitly-declared special member functions. This isn't entirely
8127 // correct; we can end up with multiple unmerged declarations of the same
8128 // implicit entity.
8129 if (D->isImplicit())
8130 continue;
8131
8132 DeclContext *CanonDef = D->getDeclContext();
8133 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8134
8135 bool Found = false;
8136 const Decl *DCanon = D->getCanonicalDecl();
8137
8138 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8139 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8140 !Found && I != E; ++I) {
Aaron Ballman86c93902014-03-06 23:45:36 +00008141 for (auto RI : (*I)->redecls()) {
8142 if (RI->getLexicalDeclContext() == CanonDef) {
Richard Smith2b9e3e32013-10-18 06:05:18 +00008143 // This declaration is present in the canonical definition. If it's
8144 // in the same redecl chain, it's the one we're looking for.
Aaron Ballman86c93902014-03-06 23:45:36 +00008145 if (RI->getCanonicalDecl() == DCanon)
Richard Smith2b9e3e32013-10-18 06:05:18 +00008146 Found = true;
8147 else
Aaron Ballman86c93902014-03-06 23:45:36 +00008148 Candidates.push_back(cast<NamedDecl>(RI));
Richard Smith2b9e3e32013-10-18 06:05:18 +00008149 break;
8150 }
8151 }
8152 }
8153
8154 if (!Found) {
8155 D->setInvalidDecl();
8156
Richard Smithcd45dbc2014-04-19 03:48:30 +00008157 std::string CanonDefModule =
8158 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
Richard Smith2b9e3e32013-10-18 06:05:18 +00008159 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008160 << D << getOwningModuleNameForDiagnostic(D)
8161 << CanonDef << CanonDefModule.empty() << CanonDefModule;
Richard Smith2b9e3e32013-10-18 06:05:18 +00008162
8163 if (Candidates.empty())
8164 Diag(cast<Decl>(CanonDef)->getLocation(),
8165 diag::note_module_odr_violation_no_possible_decls) << D;
8166 else {
8167 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8168 Diag(Candidates[I]->getLocation(),
8169 diag::note_module_odr_violation_possible_decl)
8170 << Candidates[I];
8171 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008172
8173 DiagnosedOdrMergeFailures.insert(CanonDef);
Richard Smith2b9e3e32013-10-18 06:05:18 +00008174 }
8175 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008176 }
8177
8178 // If we deserialized any C++ or Objective-C class definitions, any
8179 // Objective-C protocol definitions, or any redeclarable templates, make sure
8180 // that all redeclarations point to the definitions. Note that this can only
8181 // happen now, after the redeclaration chains have been fully wired.
8182 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
8183 DEnd = PendingDefinitions.end();
8184 D != DEnd; ++D) {
8185 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008186 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008187 // Make sure that the TagType points at the definition.
8188 const_cast<TagType*>(TagT)->decl = TD;
8189 }
8190
Aaron Ballman86c93902014-03-06 23:45:36 +00008191 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
8192 for (auto R : RD->redecls())
8193 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Guy Benyei11169dd2012-12-18 14:30:41 +00008194 }
8195
8196 continue;
8197 }
8198
Aaron Ballman86c93902014-03-06 23:45:36 +00008199 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008200 // Make sure that the ObjCInterfaceType points at the definition.
8201 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8202 ->Decl = ID;
8203
Aaron Ballman86c93902014-03-06 23:45:36 +00008204 for (auto R : ID->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008205 R->Data = ID->Data;
8206
8207 continue;
8208 }
8209
Aaron Ballman86c93902014-03-06 23:45:36 +00008210 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
8211 for (auto R : PD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008212 R->Data = PD->Data;
8213
8214 continue;
8215 }
8216
Aaron Ballman86c93902014-03-06 23:45:36 +00008217 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
8218 for (auto R : RTD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00008219 R->Common = RTD->Common;
8220 }
8221 PendingDefinitions.clear();
8222
8223 // Load the bodies of any functions or methods we've encountered. We do
8224 // this now (delayed) so that we can be sure that the declaration chains
8225 // have been fully wired up.
8226 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8227 PBEnd = PendingBodies.end();
8228 PB != PBEnd; ++PB) {
8229 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8230 // FIXME: Check for =delete/=default?
8231 // FIXME: Complain about ODR violations here?
8232 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8233 FD->setLazyBody(PB->second);
8234 continue;
8235 }
8236
8237 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8238 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8239 MD->setLazyBody(PB->second);
8240 }
8241 PendingBodies.clear();
Richard Smithcd45dbc2014-04-19 03:48:30 +00008242
8243 // Issue any pending ODR-failure diagnostics.
8244 for (auto &Merge : PendingOdrMergeFailures) {
8245 if (!DiagnosedOdrMergeFailures.insert(Merge.first))
8246 continue;
8247
8248 bool Diagnosed = false;
8249 for (auto *RD : Merge.second) {
8250 // Multiple different declarations got merged together; tell the user
8251 // where they came from.
8252 if (Merge.first != RD) {
8253 // FIXME: Walk the definition, figure out what's different,
8254 // and diagnose that.
8255 if (!Diagnosed) {
8256 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8257 Diag(Merge.first->getLocation(),
8258 diag::err_module_odr_violation_different_definitions)
8259 << Merge.first << Module.empty() << Module;
8260 Diagnosed = true;
8261 }
8262
8263 Diag(RD->getLocation(),
8264 diag::note_module_odr_violation_different_definitions)
8265 << getOwningModuleNameForDiagnostic(RD);
8266 }
8267 }
8268
8269 if (!Diagnosed) {
8270 // All definitions are updates to the same declaration. This happens if a
8271 // module instantiates the declaration of a class template specialization
8272 // and two or more other modules instantiate its definition.
8273 //
8274 // FIXME: Indicate which modules had instantiations of this definition.
8275 // FIXME: How can this even happen?
8276 Diag(Merge.first->getLocation(),
8277 diag::err_module_odr_violation_different_instantiations)
8278 << Merge.first;
8279 }
8280 }
8281 PendingOdrMergeFailures.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00008282}
8283
8284void ASTReader::FinishedDeserializing() {
8285 assert(NumCurrentElementsDeserializing &&
8286 "FinishedDeserializing not paired with StartedDeserializing");
8287 if (NumCurrentElementsDeserializing == 1) {
8288 // We decrease NumCurrentElementsDeserializing only after pending actions
8289 // are finished, to avoid recursively re-calling finishPendingActions().
8290 finishPendingActions();
8291 }
8292 --NumCurrentElementsDeserializing;
8293
Richard Smith04d05b52014-03-23 00:27:18 +00008294 if (NumCurrentElementsDeserializing == 0 && Consumer) {
8295 // We are not in recursive loading, so it's safe to pass the "interesting"
8296 // decls to the consumer.
8297 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008298 }
8299}
8300
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008301void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00008302 D = D->getMostRecentDecl();
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008303
8304 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8305 SemaObj->TUScope->AddDecl(D);
8306 } else if (SemaObj->TUScope) {
8307 // Adding the decl to IdResolver may have failed because it was already in
8308 // (even though it was not added in scope). If it is already in, make sure
8309 // it gets in the scope as well.
8310 if (std::find(SemaObj->IdResolver.begin(Name),
8311 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8312 SemaObj->TUScope->AddDecl(D);
8313 }
8314}
8315
Nico Weber824285e2014-05-08 04:26:47 +00008316ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8317 bool DisableValidation, bool AllowASTWithCompilerErrors,
8318 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00008319 bool UseGlobalIndex)
Craig Toppera13603a2014-05-22 05:54:18 +00008320 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008321 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Craig Toppera13603a2014-05-22 05:54:18 +00008322 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8323 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8324 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8325 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008326 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8327 AllowConfigurationMismatch(AllowConfigurationMismatch),
8328 ValidateSystemInputs(ValidateSystemInputs),
8329 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Richard Smith053f6c62014-05-16 23:01:30 +00008330 CurrSwitchCaseStmts(&SwitchCaseStmts),
Nico Weber824285e2014-05-08 04:26:47 +00008331 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8332 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8333 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8334 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8335 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8336 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8337 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8338 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8339 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8340 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8341 ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008342 SourceMgr.setExternalSLocEntrySource(this);
8343}
8344
8345ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008346 if (OwnsDeserializationListener)
8347 delete DeserializationListener;
8348
Guy Benyei11169dd2012-12-18 14:30:41 +00008349 for (DeclContextVisibleUpdatesPending::iterator
8350 I = PendingVisibleUpdates.begin(),
8351 E = PendingVisibleUpdates.end();
8352 I != E; ++I) {
8353 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8354 F = I->second.end();
8355 J != F; ++J)
8356 delete J->first;
8357 }
8358}