blob: 833f0dd39f77e4338e3cbbd8598db9d69e3e188e [file] [log] [blame]
Sean Silvabf9b4cd2012-12-13 01:10:461=============
2Clang Plugins
3=============
4
Dmitri Gribenko7ac0cc32012-12-15 21:10:515Clang Plugins make it possible to run extra user defined actions during a
6compilation. This document will provide a basic walkthrough of how to write and
7run a Clang Plugin.
Sean Silvabf9b4cd2012-12-13 01:10:468
9Introduction
10============
11
12Clang Plugins run FrontendActions over code. See the :doc:`FrontendAction
Dmitri Gribenko7ac0cc32012-12-15 21:10:5113tutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
14``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
15simple clang plugin.
Sean Silvabf9b4cd2012-12-13 01:10:4616
Dmitri Gribenko7ac0cc32012-12-15 21:10:5117Writing a ``PluginASTAction``
18=============================
Sean Silvabf9b4cd2012-12-13 01:10:4619
Dmitri Gribenko7ac0cc32012-12-15 21:10:5120The main difference from writing normal ``FrontendActions`` is that you can
21handle plugin command line options. The ``PluginASTAction`` base class declares
22a ``ParseArgs`` method which you have to implement in your plugin.
Sean Silvabf9b4cd2012-12-13 01:10:4623
Dmitri Gribenko7ac0cc32012-12-15 21:10:5124.. code-block:: c++
Sean Silvabf9b4cd2012-12-13 01:10:4625
Dmitri Gribenko7ac0cc32012-12-15 21:10:5126 bool ParseArgs(const CompilerInstance &CI,
27 const std::vector<std::string>& args) {
28 for (unsigned i = 0, e = args.size(); i != e; ++i) {
29 if (args[i] == "-some-arg") {
30 // Handle the command line argument.
Sean Silvabf9b4cd2012-12-13 01:10:4631 }
Dmitri Gribenko7ac0cc32012-12-15 21:10:5132 }
33 return true;
34 }
Sean Silvabf9b4cd2012-12-13 01:10:4635
36Registering a plugin
37====================
38
39A plugin is loaded from a dynamic library at runtime by the compiler. To
NAKAMURA Takumid8cd0602016-02-11 16:33:2040register a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
Sean Silvabf9b4cd2012-12-13 01:10:4641
Dmitri Gribenko7ac0cc32012-12-15 21:10:5142.. code-block:: c++
Sean Silvabf9b4cd2012-12-13 01:10:4643
Dmitri Gribenko7ac0cc32012-12-15 21:10:5144 static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
Sean Silvabf9b4cd2012-12-13 01:10:4645
John Brawn8e62db32016-04-04 14:22:5846Defining pragmas
47================
48
49Plugins can also define pragmas by declaring a ``PragmaHandler`` and
50registering it using ``PragmaHandlerRegistry::Add<>``:
51
52.. code-block:: c++
53
54 // Define a pragma handler for #pragma example_pragma
55 class ExamplePragmaHandler : public PragmaHandler {
56 public:
57 ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
58 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
59 Token &PragmaTok) {
60 // Handle the pragma
61 }
62 };
63
64 static PragmaHandlerRegistry::Add<ExamplePragmaHandler> Y("example_pragma","example pragma description");
65
Sean Silvabf9b4cd2012-12-13 01:10:4666Putting it all together
67=======================
68
Dmitri Gribenko7ac0cc32012-12-15 21:10:5169Let's look at an example plugin that prints top-level function names. This
Alp Toker4af8fa92014-01-26 05:08:0770example is checked into the clang repository; please take a look at
71the `latest version of PrintFunctionNames.cpp
Dmitri Gribenko7ac0cc32012-12-15 21:10:5172<http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup>`_.
Sean Silvabf9b4cd2012-12-13 01:10:4673
Sean Silvabf9b4cd2012-12-13 01:10:4674Running the plugin
75==================
76
John Brawn6c789742016-03-15 12:51:4077
78Using the cc1 command line
79--------------------------
80
Dmitri Gribenko7ac0cc32012-12-15 21:10:5181To run a plugin, the dynamic library containing the plugin registry must be
Aaron Ballman34be2a02016-07-14 14:07:3782loaded via the `-load` command line option. This will load all plugins
Dmitri Gribenko7ac0cc32012-12-15 21:10:5183that are registered, and you can select the plugins to run by specifying the
Aaron Ballman34be2a02016-07-14 14:07:3784`-plugin` option. Additional parameters for the plugins can be passed with
85`-plugin-arg-<plugin-name>`.
Sean Silvabf9b4cd2012-12-13 01:10:4686
87Note that those options must reach clang's cc1 process. There are two
88ways to do so:
89
Aaron Ballman34be2a02016-07-14 14:07:3790* Directly call the parsing process by using the `-cc1` option; this
Dmitri Gribenko7ac0cc32012-12-15 21:10:5191 has the downside of not configuring the default header search paths, so
92 you'll need to specify the full system path configuration on the command
93 line.
94* Use clang as usual, but prefix all arguments to the cc1 process with
Aaron Ballman34be2a02016-07-14 14:07:3795 `-Xclang`.
Sean Silvabf9b4cd2012-12-13 01:10:4696
Dmitri Gribenko7ac0cc32012-12-15 21:10:5197For example, to run the ``print-function-names`` plugin over a source file in
98clang, first build the plugin, and then call clang with the plugin from the
99source tree:
Sean Silvabf9b4cd2012-12-13 01:10:46100
Dmitri Gribenko7ac0cc32012-12-15 21:10:51101.. code-block:: console
Sean Silvabf9b4cd2012-12-13 01:10:46102
Dmitri Gribenko7ac0cc32012-12-15 21:10:51103 $ export BD=/path/to/build/directory
104 $ (cd $BD && make PrintFunctionNames )
105 $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
Sean Silvabf9b4cd2012-12-13 01:10:46106 -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
107 -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
108 tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
109 -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
110 -plugin -Xclang print-fns
111
112Also see the print-function-name plugin example's
113`README <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup>`_
Dmitri Gribenko7ac0cc32012-12-15 21:10:51114
John Brawn6c789742016-03-15 12:51:40115
116Using the clang command line
117----------------------------
118
Aaron Ballman34be2a02016-07-14 14:07:37119Using `-fplugin=plugin` on the clang command line passes the plugin
120through as an argument to `-load` on the cc1 command line. If the plugin
John Brawn6c789742016-03-15 12:51:40121class implements the ``getActionType`` method then the plugin is run
122automatically. For example, to run the plugin automatically after the main AST
Aaron Ballman34be2a02016-07-14 14:07:37123action (i.e. the same as using `-add-plugin`):
John Brawn6c789742016-03-15 12:51:40124
125.. code-block:: c++
126
127 // Automatically run the plugin after the main AST action
128 PluginASTAction::ActionType getActionType() override {
129 return AddAfterMainAction;
130 }