xref: /minix3/external/bsd/llvm/dist/clang/docs/ClangPlugins.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc=============
2f4a2713aSLionel SambucClang Plugins
3f4a2713aSLionel Sambuc=============
4f4a2713aSLionel Sambuc
5f4a2713aSLionel SambucClang Plugins make it possible to run extra user defined actions during a
6f4a2713aSLionel Sambuccompilation. This document will provide a basic walkthrough of how to write and
7f4a2713aSLionel Sambucrun a Clang Plugin.
8f4a2713aSLionel Sambuc
9f4a2713aSLionel SambucIntroduction
10f4a2713aSLionel Sambuc============
11f4a2713aSLionel Sambuc
12f4a2713aSLionel SambucClang Plugins run FrontendActions over code. See the :doc:`FrontendAction
13f4a2713aSLionel Sambuctutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
14f4a2713aSLionel Sambuc``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
15f4a2713aSLionel Sambucsimple clang plugin.
16f4a2713aSLionel Sambuc
17f4a2713aSLionel SambucWriting a ``PluginASTAction``
18f4a2713aSLionel Sambuc=============================
19f4a2713aSLionel Sambuc
20f4a2713aSLionel SambucThe main difference from writing normal ``FrontendActions`` is that you can
21f4a2713aSLionel Sambuchandle plugin command line options. The ``PluginASTAction`` base class declares
22f4a2713aSLionel Sambuca ``ParseArgs`` method which you have to implement in your plugin.
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc.. code-block:: c++
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc  bool ParseArgs(const CompilerInstance &CI,
27f4a2713aSLionel Sambuc                 const std::vector<std::string>& args) {
28f4a2713aSLionel Sambuc    for (unsigned i = 0, e = args.size(); i != e; ++i) {
29f4a2713aSLionel Sambuc      if (args[i] == "-some-arg") {
30f4a2713aSLionel Sambuc        // Handle the command line argument.
31f4a2713aSLionel Sambuc      }
32f4a2713aSLionel Sambuc    }
33f4a2713aSLionel Sambuc    return true;
34f4a2713aSLionel Sambuc  }
35f4a2713aSLionel Sambuc
36f4a2713aSLionel SambucRegistering a plugin
37f4a2713aSLionel Sambuc====================
38f4a2713aSLionel Sambuc
39f4a2713aSLionel SambucA plugin is loaded from a dynamic library at runtime by the compiler. To
40f4a2713aSLionel Sambucregister a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc.. code-block:: c++
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc  static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
45f4a2713aSLionel Sambuc
46f4a2713aSLionel SambucPutting it all together
47f4a2713aSLionel Sambuc=======================
48f4a2713aSLionel Sambuc
49f4a2713aSLionel SambucLet's look at an example plugin that prints top-level function names.  This
50*0a6a1f1dSLionel Sambucexample is checked into the clang repository; please take a look at
51*0a6a1f1dSLionel Sambucthe `latest version of PrintFunctionNames.cpp
52f4a2713aSLionel Sambuc<http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup>`_.
53f4a2713aSLionel Sambuc
54f4a2713aSLionel SambucRunning the plugin
55f4a2713aSLionel Sambuc==================
56f4a2713aSLionel Sambuc
57f4a2713aSLionel SambucTo run a plugin, the dynamic library containing the plugin registry must be
58f4a2713aSLionel Sambucloaded via the :option:`-load` command line option. This will load all plugins
59f4a2713aSLionel Sambucthat are registered, and you can select the plugins to run by specifying the
60f4a2713aSLionel Sambuc:option:`-plugin` option. Additional parameters for the plugins can be passed with
61f4a2713aSLionel Sambuc:option:`-plugin-arg-<plugin-name>`.
62f4a2713aSLionel Sambuc
63f4a2713aSLionel SambucNote that those options must reach clang's cc1 process. There are two
64f4a2713aSLionel Sambucways to do so:
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc* Directly call the parsing process by using the :option:`-cc1` option; this
67f4a2713aSLionel Sambuc  has the downside of not configuring the default header search paths, so
68f4a2713aSLionel Sambuc  you'll need to specify the full system path configuration on the command
69f4a2713aSLionel Sambuc  line.
70f4a2713aSLionel Sambuc* Use clang as usual, but prefix all arguments to the cc1 process with
71f4a2713aSLionel Sambuc  :option:`-Xclang`.
72f4a2713aSLionel Sambuc
73f4a2713aSLionel SambucFor example, to run the ``print-function-names`` plugin over a source file in
74f4a2713aSLionel Sambucclang, first build the plugin, and then call clang with the plugin from the
75f4a2713aSLionel Sambucsource tree:
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc.. code-block:: console
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc  $ export BD=/path/to/build/directory
80f4a2713aSLionel Sambuc  $ (cd $BD && make PrintFunctionNames )
81f4a2713aSLionel Sambuc  $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
82f4a2713aSLionel Sambuc            -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
83f4a2713aSLionel Sambuc            -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
84f4a2713aSLionel Sambuc            tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
85f4a2713aSLionel Sambuc            -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
86f4a2713aSLionel Sambuc            -plugin -Xclang print-fns
87f4a2713aSLionel Sambuc
88f4a2713aSLionel SambucAlso see the print-function-name plugin example's
89f4a2713aSLionel Sambuc`README <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup>`_
90f4a2713aSLionel Sambuc
91