1 //===--- FrontendAction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/FrontendAction.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/DeclGroup.h"
14 #include "clang/Lex/HeaderSearch.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Frontend/ASTUnit.h"
17 #include "clang/Frontend/ChainedIncludesSource.h"
18 #include "clang/Frontend/CompilerInstance.h"
19 #include "clang/Frontend/FrontendDiagnostic.h"
20 #include "clang/Frontend/FrontendPluginRegistry.h"
21 #include "clang/Frontend/LayoutOverrideSource.h"
22 #include "clang/Frontend/MultiplexConsumer.h"
23 #include "clang/Parse/ParseAST.h"
24 #include "clang/Serialization/ASTDeserializationListener.h"
25 #include "clang/Serialization/ASTReader.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/system_error.h"
31 #include "llvm/Support/Timer.h"
32 using namespace clang;
36 class DelegatingDeserializationListener : public ASTDeserializationListener {
37 ASTDeserializationListener *Previous;
40 explicit DelegatingDeserializationListener(
41 ASTDeserializationListener *Previous)
42 : Previous(Previous) { }
44 virtual void ReaderInitialized(ASTReader *Reader) {
46 Previous->ReaderInitialized(Reader);
48 virtual void IdentifierRead(serialization::IdentID ID,
51 Previous->IdentifierRead(ID, II);
53 virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
55 Previous->TypeRead(Idx, T);
57 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
59 Previous->DeclRead(ID, D);
61 virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
63 Previous->SelectorRead(ID, Sel);
65 virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
66 MacroDefinition *MD) {
68 Previous->MacroDefinitionRead(PPID, MD);
72 /// \brief Dumps deserialized declarations.
73 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
75 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
76 : DelegatingDeserializationListener(Previous) { }
78 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
79 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
80 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
81 llvm::outs() << " - " << *ND;
84 DelegatingDeserializationListener::DeclRead(ID, D);
88 /// \brief Checks deserialized declarations and emits error if a name
89 /// matches one given in command-line using -error-on-deserialized-decl.
90 class DeserializedDeclsChecker : public DelegatingDeserializationListener {
92 std::set<std::string> NamesToCheck;
95 DeserializedDeclsChecker(ASTContext &Ctx,
96 const std::set<std::string> &NamesToCheck,
97 ASTDeserializationListener *Previous)
98 : DelegatingDeserializationListener(Previous),
99 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
101 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
102 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
103 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
105 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
106 "%0 was deserialized");
107 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
108 << ND->getNameAsString();
111 DelegatingDeserializationListener::DeclRead(ID, D);
115 } // end anonymous namespace
117 FrontendAction::FrontendAction() : Instance(0) {}
119 FrontendAction::~FrontendAction() {}
121 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
123 this->CurrentInput = CurrentInput;
124 CurrentASTUnit.reset(AST);
127 ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
129 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
133 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
136 // Make sure the non-plugin consumer is first, so that plugins can't
138 std::vector<ASTConsumer*> Consumers(1, Consumer);
140 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
142 // This is O(|plugins| * |add_plugins|), but since both numbers are
143 // way below 50 in practice, that's ok.
144 for (FrontendPluginRegistry::iterator
145 it = FrontendPluginRegistry::begin(),
146 ie = FrontendPluginRegistry::end();
148 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
149 OwningPtr<PluginASTAction> P(it->instantiate());
150 FrontendAction* c = P.get();
151 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
152 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
157 return new MultiplexConsumer(Consumers);
161 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
162 const FrontendInputFile &Input) {
163 assert(!Instance && "Already processing a source file!");
164 assert(!Input.isEmpty() && "Unexpected empty filename!");
165 setCurrentInput(Input);
166 setCompilerInstance(&CI);
168 StringRef InputFile = Input.getFile();
169 bool HasBegunSourceFile = false;
170 if (!BeginInvocation(CI))
173 // AST files follow a very different path, since they share objects via the
175 if (Input.getKind() == IK_AST) {
176 assert(!usesPreprocessorOnly() &&
177 "Attempt to pass AST file to preprocessor only action!");
178 assert(hasASTFileSupport() &&
179 "This action does not have AST file support!");
181 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
183 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
184 CI.getFileSystemOpts());
188 setCurrentInput(Input, AST);
190 // Set the shared objects, these are reset when we finish processing the
191 // file, otherwise the CompilerInstance will happily destroy them.
192 CI.setFileManager(&AST->getFileManager());
193 CI.setSourceManager(&AST->getSourceManager());
194 CI.setPreprocessor(&AST->getPreprocessor());
195 CI.setASTContext(&AST->getASTContext());
197 // Initialize the action.
198 if (!BeginSourceFileAction(CI, InputFile))
201 /// Create the AST consumer.
202 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
203 if (!CI.hasASTConsumer())
209 // Set up the file and source managers, if needed.
210 if (!CI.hasFileManager())
211 CI.createFileManager();
212 if (!CI.hasSourceManager())
213 CI.createSourceManager(CI.getFileManager());
215 // IR files bypass the rest of initialization.
216 if (Input.getKind() == IK_LLVM_IR) {
217 assert(hasIRSupport() &&
218 "This action does not have IR file support!");
220 // Inform the diagnostic client we are processing a source file.
221 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
222 HasBegunSourceFile = true;
224 // Initialize the action.
225 if (!BeginSourceFileAction(CI, InputFile))
231 // If the implicit PCH include is actually a directory, rather than
232 // a single file, search for a suitable PCH file in that directory.
233 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
234 FileManager &FileMgr = CI.getFileManager();
235 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
236 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
237 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
239 SmallString<128> DirNative;
240 llvm::sys::path::native(PCHDir->getName(), DirNative);
242 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
243 Dir != DirEnd && !EC; Dir.increment(EC)) {
244 // Check whether this is an acceptable AST file.
245 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
248 CI.getPreprocessorOpts())) {
249 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
250 if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
251 PPOpts.Includes[I] = Dir->path();
252 PPOpts.ImplicitPCHInclude = Dir->path();
258 assert(Found && "Implicit PCH include not in includes list?");
264 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
270 // Set up the preprocessor.
271 CI.createPreprocessor();
273 // Inform the diagnostic client we are processing a source file.
274 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
275 &CI.getPreprocessor());
276 HasBegunSourceFile = true;
278 // Initialize the action.
279 if (!BeginSourceFileAction(CI, InputFile))
282 /// Create the AST context and consumer unless this is a preprocessor only
284 if (!usesPreprocessorOnly()) {
285 CI.createASTContext();
287 OwningPtr<ASTConsumer> Consumer(
288 CreateWrappedASTConsumer(CI, InputFile));
292 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
293 CI.getPreprocessor().setPPMutationListener(
294 Consumer->GetPPMutationListener());
296 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
297 // Convert headers to PCH and chain them.
298 OwningPtr<ExternalASTSource> source;
299 source.reset(ChainedIncludesSource::create(CI));
302 CI.getASTContext().setExternalSource(source);
304 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
306 assert(hasPCHSupport() && "This action does not have PCH support!");
307 ASTDeserializationListener *DeserialListener =
308 Consumer->GetASTDeserializationListener();
309 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
310 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
311 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
312 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
313 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
315 CI.createPCHExternalASTSource(
316 CI.getPreprocessorOpts().ImplicitPCHInclude,
317 CI.getPreprocessorOpts().DisablePCHValidation,
318 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
320 if (!CI.getASTContext().getExternalSource())
324 CI.setASTConsumer(Consumer.take());
325 if (!CI.hasASTConsumer())
329 // Initialize built-in info as long as we aren't using an external AST
331 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
332 Preprocessor &PP = CI.getPreprocessor();
333 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
337 // If there is a layout overrides file, attach an external AST source that
338 // provides the layouts from that file.
339 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
340 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
341 OwningPtr<ExternalASTSource>
342 Override(new LayoutOverrideSource(
343 CI.getFrontendOpts().OverrideRecordLayoutsFile));
344 CI.getASTContext().setExternalSource(Override);
349 // If we failed, reset state since the client will not end up calling the
350 // matching EndSourceFile().
352 if (isCurrentFileAST()) {
354 CI.setPreprocessor(0);
355 CI.setSourceManager(0);
356 CI.setFileManager(0);
359 if (HasBegunSourceFile)
360 CI.getDiagnosticClient().EndSourceFile();
361 CI.clearOutputFiles(/*EraseFiles=*/true);
362 setCurrentInput(FrontendInputFile());
363 setCompilerInstance(0);
367 bool FrontendAction::Execute() {
368 CompilerInstance &CI = getCompilerInstance();
370 // Initialize the main file entry. This needs to be delayed until after PCH
372 if (!isCurrentFileAST()) {
373 if (!CI.InitializeSourceManager(getCurrentInput()))
377 if (CI.hasFrontendTimer()) {
378 llvm::TimeRegion Timer(CI.getFrontendTimer());
381 else ExecuteAction();
386 void FrontendAction::EndSourceFile() {
387 CompilerInstance &CI = getCompilerInstance();
389 // Inform the diagnostic client we are done with this source file.
390 CI.getDiagnosticClient().EndSourceFile();
392 // Finalize the action.
393 EndSourceFileAction();
395 // Release the consumer and the AST, in that order since the consumer may
396 // perform actions in its destructor which require the context.
398 // FIXME: There is more per-file stuff we could just drop here?
399 if (CI.getFrontendOpts().DisableFree) {
400 CI.takeASTConsumer();
401 if (!isCurrentFileAST()) {
403 CI.resetAndLeakASTContext();
406 if (!isCurrentFileAST()) {
410 CI.setASTConsumer(0);
413 // Inform the preprocessor we are done.
414 if (CI.hasPreprocessor())
415 CI.getPreprocessor().EndSourceFile();
417 if (CI.getFrontendOpts().ShowStats) {
418 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
419 CI.getPreprocessor().PrintStats();
420 CI.getPreprocessor().getIdentifierTable().PrintStats();
421 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
422 CI.getSourceManager().PrintStats();
423 llvm::errs() << "\n";
426 // Cleanup the output streams, and erase the output files if we encountered
428 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
430 if (isCurrentFileAST()) {
432 CI.resetAndLeakASTContext();
433 CI.resetAndLeakPreprocessor();
434 CI.resetAndLeakSourceManager();
435 CI.resetAndLeakFileManager();
438 setCompilerInstance(0);
439 setCurrentInput(FrontendInputFile());
442 //===----------------------------------------------------------------------===//
444 //===----------------------------------------------------------------------===//
446 void ASTFrontendAction::ExecuteAction() {
447 CompilerInstance &CI = getCompilerInstance();
449 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
450 // here so the source manager would be initialized.
451 if (hasCodeCompletionSupport() &&
452 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
453 CI.createCodeCompletionConsumer();
455 // Use a code completion consumer?
456 CodeCompleteConsumer *CompletionConsumer = 0;
457 if (CI.hasCodeCompletionConsumer())
458 CompletionConsumer = &CI.getCodeCompletionConsumer();
461 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
463 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
464 CI.getFrontendOpts().SkipFunctionBodies);
467 void PluginASTAction::anchor() { }
470 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
472 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
475 ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
477 return WrappedAction->CreateASTConsumer(CI, InFile);
479 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
480 return WrappedAction->BeginInvocation(CI);
482 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
483 StringRef Filename) {
484 WrappedAction->setCurrentInput(getCurrentInput());
485 WrappedAction->setCompilerInstance(&CI);
486 return WrappedAction->BeginSourceFileAction(CI, Filename);
488 void WrapperFrontendAction::ExecuteAction() {
489 WrappedAction->ExecuteAction();
491 void WrapperFrontendAction::EndSourceFileAction() {
492 WrappedAction->EndSourceFileAction();
495 bool WrapperFrontendAction::usesPreprocessorOnly() const {
496 return WrappedAction->usesPreprocessorOnly();
498 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
499 return WrappedAction->getTranslationUnitKind();
501 bool WrapperFrontendAction::hasPCHSupport() const {
502 return WrappedAction->hasPCHSupport();
504 bool WrapperFrontendAction::hasASTFileSupport() const {
505 return WrappedAction->hasASTFileSupport();
507 bool WrapperFrontendAction::hasIRSupport() const {
508 return WrappedAction->hasIRSupport();
510 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
511 return WrappedAction->hasCodeCompletionSupport();
514 WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
515 : WrappedAction(WrappedAction) {}