1 //===--- Tooling.h - Framework for standalone Clang tools -------*- C++ -*-===//
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 // This file implements functions to run clang tools standalone instead
11 // of running them as a plugin.
13 // A ClangTool is initialized with a CompilationDatabase and a set of files
14 // to run over. The tool will then run a user-specified FrontendAction over
15 // all TUs in which the given files are compiled.
17 // It is also possible to run a FrontendAction over a snippet of code by
18 // calling runSyntaxOnlyToolOnCode, which is useful for unit testing.
20 // Applications that need more fine grained control over how to run
21 // multiple FrontendActions over code can use ToolInvocation.
24 // - running clang -fsyntax-only over source code from an editor to get
26 // - running match/replace tools over C++ code
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_CLANG_TOOLING_TOOLING_H
31 #define LLVM_CLANG_TOOLING_TOOLING_H
33 #include "llvm/ADT/StringMap.h"
34 #include "llvm/ADT/Twine.h"
35 #include "clang/Basic/FileManager.h"
36 #include "clang/Basic/LLVM.h"
37 #include "clang/Driver/Util.h"
45 } // end namespace driver
47 class CompilerInvocation;
53 class CompilationDatabase;
55 /// \brief Interface to generate clang::FrontendActions.
56 class FrontendActionFactory {
58 virtual ~FrontendActionFactory();
60 /// \brief Returns a new clang::FrontendAction.
62 /// The caller takes ownership of the returned action.
63 virtual clang::FrontendAction *create() = 0;
66 /// \brief Returns a new FrontendActionFactory for a given type.
68 /// T must extend clang::FrontendAction.
71 /// FrontendActionFactory *Factory =
72 /// newFrontendActionFactory<clang::SyntaxOnlyAction>();
74 FrontendActionFactory *newFrontendActionFactory();
76 /// \brief Returns a new FrontendActionFactory for any type that provides an
77 /// implementation of newFrontendAction().
79 /// FactoryT must implement: FrontendAction *newFrontendAction().
82 /// struct ProvidesFrontendActions {
83 /// FrontendAction *newFrontendAction();
85 /// FrontendActionFactory *FactoryAdapter =
86 /// newFrontendActionFactory(&Factory);
87 template <typename FactoryT>
88 FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory);
90 /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
92 /// \param ToolAction The action to run over the code.
93 /// \param Code C++ code.
94 /// \param FileName The file name which 'Code' will be mapped as.
96 /// \return - True if 'ToolAction' was successfully executed.
97 bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
98 const Twine &FileName = "input.cc");
100 /// \brief Utility to run a FrontendAction in a single clang invocation.
101 class ToolInvocation {
103 /// \brief Create a tool invocation.
105 /// \param CommandLine The command line arguments to clang.
106 /// \param ToolAction The action to be executed. Class takes ownership.
107 /// \param Files The FileManager used for the execution. Class does not take
109 ToolInvocation(ArrayRef<std::string> CommandLine, FrontendAction *ToolAction,
112 /// \brief Map a virtual file to be used while running the tool.
114 /// \param FilePath The path at which the content will be mapped.
115 /// \param Content A null terminated buffer of the file's content.
116 void mapVirtualFile(StringRef FilePath, StringRef Content);
118 /// \brief Run the clang invocation.
120 /// \returns True if there were no errors during execution.
124 void addFileMappingsTo(SourceManager &SourceManager);
126 bool runInvocation(const char *BinaryName,
127 clang::driver::Compilation *Compilation,
128 clang::CompilerInvocation *Invocation,
129 const clang::driver::ArgStringList &CC1Args,
130 clang::FrontendAction *ToolAction);
132 std::vector<std::string> CommandLine;
133 llvm::OwningPtr<FrontendAction> ToolAction;
135 // Maps <file name> -> <file content>.
136 llvm::StringMap<StringRef> MappedFileContents;
139 /// \brief Utility to run a FrontendAction over a set of files.
141 /// This class is written to be usable for command line utilities.
144 /// \brief Constructs a clang tool to run over a list of files.
146 /// \param Compilations The CompilationDatabase which contains the compile
147 /// command lines for the given source paths.
148 /// \param SourcePaths The source files to run over. If a source files is
149 /// not found in Compilations, it is skipped.
150 ClangTool(const CompilationDatabase &Compilations,
151 ArrayRef<std::string> SourcePaths);
153 /// \brief Map a virtual file to be used while running the tool.
155 /// \param FilePath The path at which the content will be mapped.
156 /// \param Content A null terminated buffer of the file's content.
157 void mapVirtualFile(StringRef FilePath, StringRef Content);
159 /// Runs a frontend action over all files specified in the command line.
161 /// \param ActionFactory Factory generating the frontend actions. The function
162 /// takes ownership of this parameter. A new action is generated for every
163 /// processed translation unit.
164 int run(FrontendActionFactory *ActionFactory);
166 /// \brief Returns the file manager used in the tool.
168 /// The file manager is shared between all translation units.
169 FileManager &getFiles() { return Files; }
172 // We store command lines as pair (file name, command line).
173 typedef std::pair< std::string, std::vector<std::string> > CommandLine;
174 std::vector<CommandLine> CommandLines;
177 // Contains a list of pairs (<file name>, <file content>).
178 std::vector< std::pair<StringRef, StringRef> > MappedFileContents;
181 template <typename T>
182 FrontendActionFactory *newFrontendActionFactory() {
183 class SimpleFrontendActionFactory : public FrontendActionFactory {
185 virtual clang::FrontendAction *create() { return new T; }
188 return new SimpleFrontendActionFactory;
191 template <typename FactoryT>
192 FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory) {
193 class FrontendActionFactoryAdapter : public FrontendActionFactory {
195 explicit FrontendActionFactoryAdapter(FactoryT *ActionFactory)
196 : ActionFactory(ActionFactory) {}
198 virtual clang::FrontendAction *create() {
199 return ActionFactory->newFrontendAction();
203 FactoryT *ActionFactory;
206 return new FrontendActionFactoryAdapter(ActionFactory);
209 } // end namespace tooling
210 } // end namespace clang
212 #endif // LLVM_CLANG_TOOLING_TOOLING_H