blob: 9a5bc142130b7f3000b3bd834b4bb7122fe247dc [file] [log] [blame]
Sean Silva93ca0212012-12-13 01:10:46 +00001=============
2Clang Plugins
3=============
4
Dmitri Gribenko97555a12012-12-15 21:10:51 +00005Clang 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 Silva93ca0212012-12-13 01:10:46 +00008
9Introduction
10============
11
12Clang Plugins run FrontendActions over code. See the :doc:`FrontendAction
Dmitri Gribenko97555a12012-12-15 21:10:51 +000013tutorial <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 Silva93ca0212012-12-13 01:10:46 +000016
Dmitri Gribenko97555a12012-12-15 21:10:51 +000017Writing a ``PluginASTAction``
18=============================
Sean Silva93ca0212012-12-13 01:10:46 +000019
Dmitri Gribenko97555a12012-12-15 21:10:51 +000020The 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 Silva93ca0212012-12-13 01:10:46 +000023
Dmitri Gribenko97555a12012-12-15 21:10:51 +000024.. code-block:: c++
Sean Silva93ca0212012-12-13 01:10:46 +000025
Dmitri Gribenko97555a12012-12-15 21:10:51 +000026 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 Silva93ca0212012-12-13 01:10:46 +000031 }
Dmitri Gribenko97555a12012-12-15 21:10:51 +000032 }
33 return true;
34 }
Sean Silva93ca0212012-12-13 01:10:46 +000035
36Registering a plugin
37====================
38
39A plugin is loaded from a dynamic library at runtime by the compiler. To
Dmitri Gribenko97555a12012-12-15 21:10:51 +000040register a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
Sean Silva93ca0212012-12-13 01:10:46 +000041
Dmitri Gribenko97555a12012-12-15 21:10:51 +000042.. code-block:: c++
Sean Silva93ca0212012-12-13 01:10:46 +000043
Dmitri Gribenko97555a12012-12-15 21:10:51 +000044 static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
Sean Silva93ca0212012-12-13 01:10:46 +000045
46Putting it all together
47=======================
48
Dmitri Gribenko97555a12012-12-15 21:10:51 +000049Let's look at an example plugin that prints top-level function names. This
Stephen Hines651f13c2014-04-23 16:59:28 -070050example is checked into the clang repository; please take a look at
51the `latest version of PrintFunctionNames.cpp
Dmitri Gribenko97555a12012-12-15 21:10:51 +000052<http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup>`_.
Sean Silva93ca0212012-12-13 01:10:46 +000053
Sean Silva93ca0212012-12-13 01:10:46 +000054Running the plugin
55==================
56
Dmitri Gribenko97555a12012-12-15 21:10:51 +000057To run a plugin, the dynamic library containing the plugin registry must be
58loaded via the :option:`-load` command line option. This will load all plugins
59that are registered, and you can select the plugins to run by specifying the
60:option:`-plugin` option. Additional parameters for the plugins can be passed with
61:option:`-plugin-arg-<plugin-name>`.
Sean Silva93ca0212012-12-13 01:10:46 +000062
63Note that those options must reach clang's cc1 process. There are two
64ways to do so:
65
Dmitri Gribenko97555a12012-12-15 21:10:51 +000066* Directly call the parsing process by using the :option:`-cc1` option; this
67 has the downside of not configuring the default header search paths, so
68 you'll need to specify the full system path configuration on the command
69 line.
70* Use clang as usual, but prefix all arguments to the cc1 process with
71 :option:`-Xclang`.
Sean Silva93ca0212012-12-13 01:10:46 +000072
Dmitri Gribenko97555a12012-12-15 21:10:51 +000073For example, to run the ``print-function-names`` plugin over a source file in
74clang, first build the plugin, and then call clang with the plugin from the
75source tree:
Sean Silva93ca0212012-12-13 01:10:46 +000076
Dmitri Gribenko97555a12012-12-15 21:10:51 +000077.. code-block:: console
Sean Silva93ca0212012-12-13 01:10:46 +000078
Dmitri Gribenko97555a12012-12-15 21:10:51 +000079 $ export BD=/path/to/build/directory
80 $ (cd $BD && make PrintFunctionNames )
81 $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
Sean Silva93ca0212012-12-13 01:10:46 +000082 -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
83 -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
84 tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
85 -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
86 -plugin -Xclang print-fns
87
88Also see the print-function-name plugin example's
89`README <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup>`_
Dmitri Gribenko97555a12012-12-15 21:10:51 +000090