diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-11-14 10:42:46 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-11-14 10:42:46 +0000 |
commit | 8305d01a1de4bfbfdd501b83074a1e80fada752d (patch) | |
tree | 6e0b1b63e500944f773fd7c9e5b90cb8dcfb2fdb | |
parent | 4ee24097a8050b885c1d75ca4ee9f6d534c3f20b (diff) |
Add FrontendActions, which provides a FrontendAction interface to all the existing AST consumer based clang-cc actions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88773 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Frontend/FrontendActions.h | 159 | ||||
-rw-r--r-- | lib/Frontend/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/Frontend/FrontendActions.cpp | 178 |
3 files changed, 338 insertions, 0 deletions
diff --git a/include/clang/Frontend/FrontendActions.h b/include/clang/Frontend/FrontendActions.h new file mode 100644 index 0000000000..3d6aab7fae --- /dev/null +++ b/include/clang/Frontend/FrontendActions.h @@ -0,0 +1,159 @@ +//===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H +#define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H + +#include "clang/Frontend/FrontendAction.h" + +namespace clang { +class FixItRewriter; + +class AnalysisAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class ASTPrintAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class ASTPrintXMLAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class ASTDumpAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class ASTViewAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class DeclContextPrintAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class DumpRecordAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class FixItAction : public ASTFrontendAction { +private: + llvm::OwningPtr<FixItRewriter> Rewriter; + +protected: + + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); + + virtual bool BeginSourceFileAction(CompilerInstance &CI, + llvm::StringRef Filename); + + virtual void EndSourceFileAction(); + + virtual bool hasASTSupport() const { return false; } + +public: + FixItAction(); + ~FixItAction(); +}; + +class GeneratePCHAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); + + virtual bool usesCompleteTranslationUnit() { return false; } + + virtual bool hasASTSupport() const { return false; } +}; + +class HTMLPrintAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class InheritanceViewAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class RewriteObjCAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class RewriteBlocksAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class SyntaxOnlyAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); + +public: + virtual bool hasCodeCompletionSupport() const { return true; } +}; + +// Code Gen Actions + +class CodeGenAction : public ASTFrontendAction { +private: + unsigned Act; + +protected: + CodeGenAction(unsigned _Act); + + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile); +}; + +class EmitAssemblyAction : public CodeGenAction { +public: + EmitAssemblyAction(); +}; + +class EmitBCAction : public CodeGenAction { +public: + EmitBCAction(); +}; + +class EmitLLVMAction : public CodeGenAction { +public: + EmitLLVMAction(); +}; + +class EmitLLVMOnlyAction : public CodeGenAction { +public: + EmitLLVMOnlyAction(); +}; + +} // end namespace clang + +#endif diff --git a/lib/Frontend/CMakeLists.txt b/lib/Frontend/CMakeLists.txt index 1b1cf78b10..9f05919d54 100644 --- a/lib/Frontend/CMakeLists.txt +++ b/lib/Frontend/CMakeLists.txt @@ -13,6 +13,7 @@ add_clang_library(clangFrontend DocumentXML.cpp FixItRewriter.cpp FrontendAction.cpp + FrontendActions.cpp FrontendOptions.cpp GeneratePCH.cpp HTMLDiagnostics.cpp diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp new file mode 100644 index 0000000000..4494f87f34 --- /dev/null +++ b/lib/Frontend/FrontendActions.cpp @@ -0,0 +1,178 @@ +//===--- FrontendActions.cpp ----------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/FrontendActions.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/Basic/FileManager.h" +#include "clang/Frontend/AnalysisConsumer.h" +#include "clang/Frontend/ASTConsumers.h" +#include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FixItRewriter.h" +#include "clang/Frontend/FrontendDiagnostic.h" +#include "clang/Frontend/Utils.h" +#include "llvm/Support/raw_ostream.h" +using namespace clang; + +ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateAnalysisConsumer(CI.getPreprocessor(), + CI.getFrontendOpts().OutputFile, + CI.getAnalyzerOpts()); +} + +ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateASTPrinter(CI.createDefaultOutputFile(false, InFile)); +} + +ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateASTPrinterXML(CI.createDefaultOutputFile(false, InFile, + "xml")); +} + +ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateASTDumper(); +} + +ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateASTViewer(); +} + +ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateDeclContextPrinter(); +} + +ASTConsumer *DumpRecordAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateRecordLayoutDumper(); +} + +ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; + if (CI.getFrontendOpts().RelocatablePCH && + Sysroot.empty()) { + CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot); + return 0; + } + + llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile); + if (CI.getFrontendOpts().RelocatablePCH) + return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str()); + + return CreatePCHGenerator(CI.getPreprocessor(), OS); +} + +ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateHTMLPrinter(CI.createDefaultOutputFile(false, InFile), + CI.getPreprocessor()); +} + +ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance); +} + +FixItAction::FixItAction() {} +FixItAction::~FixItAction() {} + +ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return new ASTConsumer(); +} + +/// AddFixItLocations - Add any individual user specified "fix-it" locations, +/// and return true on success. +static bool AddFixItLocations(CompilerInstance &CI, + FixItRewriter &FixItRewrite) { + const std::vector<ParsedSourceLocation> &Locs = + CI.getFrontendOpts().FixItLocations; + for (unsigned i = 0, e = Locs.size(); i != e; ++i) { + const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName); + if (!File) { + CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file) + << Locs[i].FileName; + return false; + } + + RequestedSourceLocation Requested; + Requested.File = File; + Requested.Line = Locs[i].Line; + Requested.Column = Locs[i].Column; + FixItRewrite.addFixItLocation(Requested); + } + + return true; +} + +bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, + llvm::StringRef Filename) { + Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), + CI.getLangOpts())); + if (!AddFixItLocations(CI, *Rewriter)) + return false; + + return true; +} + +void FixItAction::EndSourceFileAction() { + const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); + Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile); +} + +ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateObjCRewriter(InFile, + CI.createDefaultOutputFile(true, InFile, "cpp"), + CI.getDiagnostics(), CI.getLangOpts(), + CI.getDiagnosticOpts().NoRewriteMacros); +} + +ASTConsumer *RewriteBlocksAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return CreateBlockRewriter(InFile, CI.getDiagnostics(), CI.getLangOpts()); +} + +ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + return new ASTConsumer(); +} + +CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {} + +ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef InFile) { + BackendAction BA = static_cast<BackendAction>(Act); + llvm::OwningPtr<llvm::raw_ostream> OS; + if (BA == Backend_EmitAssembly) + OS.reset(CI.createDefaultOutputFile(false, InFile, "s")); + else if (BA == Backend_EmitLL) + OS.reset(CI.createDefaultOutputFile(false, InFile, "ll")); + else if (BA == Backend_EmitBC) + OS.reset(CI.createDefaultOutputFile(true, InFile, "bc")); + + return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(), + CI.getCodeGenOpts(), InFile, OS.take(), + CI.getLLVMContext()); +} + +EmitAssemblyAction::EmitAssemblyAction() + : CodeGenAction(Backend_EmitAssembly) {} + +EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {} + +EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {} + +EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {} |