blob: d553a62fcfd36ec8302dc2d5c49b615f5c6810ea [file] [log] [blame]
Manuel Klimek882d7942012-05-24 17:07:18 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5<title>Clang Plugins</title>
6<link type="text/css" rel="stylesheet" href="../menu.css">
7<link type="text/css" rel="stylesheet" href="../content.css">
8</head>
9<body>
10<div id="content">
11
12<h1>Clang Plugins</h1>
13<p>Clang Plugins make it possible to run extra user defined actions during
14a compilation. This document will provide a basic walkthrough of how to write
15and run a Clang Plugin.</p>
16
17<!-- ======================================================================= -->
18<h2 id="intro">Introduction</h2>
19<!-- ======================================================================= -->
20
21<p>Clang Plugins run FrontendActions over code. See the
22<a href="RAVFrontendAction.html">FrontendAction tutorial</a> on how to write a
23FrontendAction using the RecursiveASTVisitor. In this tutorial, we'll
24demonstrate how to write a simple clang plugin.
25</p>
26
27<!-- ======================================================================= -->
28<h2 id="pluginactions">Writing a PluginASTAction</h2>
29<!-- ======================================================================= -->
30
31<p>The main difference from writing normal FrontendActions is that you can
32handle plugin command line options. The
33PluginASTAction base class declares a ParseArgs method which you have to
34implement in your plugin.
35</p>
36<pre>
37 bool ParseArgs(const CompilerInstance &amp;CI,
38 const std::vector&lt;std::string>&amp; args) {
39 for (unsigned i = 0, e = args.size(); i != e; ++i) {
40 if (args[i] == "-some-arg") {
41 // Handle the command line argument.
42 }
43 }
44 return true;
45 }
46</pre>
47
48<!-- ======================================================================= -->
49<h2 id="registerplugin">Registering a plugin</h2>
50<!-- ======================================================================= -->
51
52<p>A plugin is loaded from a dynamic library at runtime by the compiler. To register
53a plugin in a library, use FrontendPluginRegistry::Add:</p>
54<pre>
55 static FrontendPluginRegistry::Add&lt;MyPlugin> X("my-plugin-name", "my plugin description");
56</pre>
57
58<!-- ======================================================================= -->
59<h2 id="example">Putting it all together</h2>
60<!-- ======================================================================= -->
61
62<p>Let's look at an example plugin that prints top-level function names.
63This example is also checked into the clang repository; please also take a look
64at the latest <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup">checked in version of PrintFunctionNames.cpp</a>.</p>
65<pre>
66#include "clang/Frontend/FrontendPluginRegistry.h"
67#include "clang/AST/ASTConsumer.h"
68#include "clang/AST/AST.h"
69#include "clang/Frontend/CompilerInstance.h"
70#include "llvm/Support/raw_ostream.h"
71using namespace clang;
72
73namespace {
74
75class PrintFunctionsConsumer : public ASTConsumer {
76public:
77 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
78 for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
79 const Decl *D = *i;
80 if (const NamedDecl *ND = dyn_cast&lt;NamedDecl>(D))
81 llvm::errs() &lt;&lt; "top-level-decl: \"" &lt;&lt; ND->getNameAsString() &lt;&lt; "\"\n";
82 }
83
84 return true;
85 }
86};
87
88class PrintFunctionNamesAction : public PluginASTAction {
89protected:
90 ASTConsumer *CreateASTConsumer(CompilerInstance &amp;CI, llvm::StringRef) {
91 return new PrintFunctionsConsumer();
92 }
93
94 bool ParseArgs(const CompilerInstance &amp;CI,
95 const std::vector&lt;std::string>&amp; args) {
96 for (unsigned i = 0, e = args.size(); i != e; ++i) {
97 llvm::errs() &lt;&lt; "PrintFunctionNames arg = " &lt;&lt; args[i] &lt;&lt; "\n";
98
99 // Example error handling.
100 if (args[i] == "-an-error") {
101 DiagnosticsEngine &amp;D = CI.getDiagnostics();
102 unsigned DiagID = D.getCustomDiagID(
103 DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
104 D.Report(DiagID);
105 return false;
106 }
107 }
108 if (args.size() &amp;&amp; args[0] == "help")
109 PrintHelp(llvm::errs());
110
111 return true;
112 }
113 void PrintHelp(llvm::raw_ostream&amp; ros) {
114 ros &lt;&lt; "Help for PrintFunctionNames plugin goes here\n";
115 }
116
117};
118
119}
120
121static FrontendPluginRegistry::Add&lt;PrintFunctionNamesAction>
122X("print-fns", "print function names");
123</pre>
124
125<!-- ======================================================================= -->
126<h2 id="running">Running the plugin</h2>
127<!-- ======================================================================= -->
128
129<p>To run a plugin, the dynamic library containing the plugin registry must be
130loaded via the -load command line option. This will load all plugins that are
131registered, and you can select the plugins to run by specifying the -plugin
132option. Additional parameters for the plugins can be passed with -plugin-arg-&lt;plugin-name>.</p>
133
134<p>Note that those options must reach clang's cc1 process. There are two
135ways to do so:</p>
136<ul>
137<li>
138Directly call the parsing process by using the -cc1 option; this has the
139downside of not configuring the default header search paths, so you'll need to
140specify the full system path configuration on the command line.
141</li>
142<li>
143Use clang as usual, but prefix all arguments to the cc1 process with -Xclang.
144</li>
145</ul>
146<p>For example, to run the print-function-names plugin over a source file in clang,
147first build the plugin, and then call clang with the plugin from the source tree:</p>
148<pre>
149 $ export BD=/path/to/build/directory
150 $ (cd $BD &amp;&amp; make PrintFunctionNames )
151 $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
152 -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
153 -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
154 tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
155 -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
156 -plugin -Xclang print-fns
157</pre>
158
159<p>Also see the print-function-name plugin example's
160<a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup">README</a></p>
161
162
163
164</div>
165</body>
166</html>
167