xref: /aosp_15_r20/external/clang/docs/ClangPlugins.rst (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li=============
2*67e74705SXin LiClang Plugins
3*67e74705SXin Li=============
4*67e74705SXin Li
5*67e74705SXin LiClang Plugins make it possible to run extra user defined actions during a
6*67e74705SXin Licompilation. This document will provide a basic walkthrough of how to write and
7*67e74705SXin Lirun a Clang Plugin.
8*67e74705SXin Li
9*67e74705SXin LiIntroduction
10*67e74705SXin Li============
11*67e74705SXin Li
12*67e74705SXin LiClang Plugins run FrontendActions over code. See the :doc:`FrontendAction
13*67e74705SXin Litutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
14*67e74705SXin Li``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
15*67e74705SXin Lisimple clang plugin.
16*67e74705SXin Li
17*67e74705SXin LiWriting a ``PluginASTAction``
18*67e74705SXin Li=============================
19*67e74705SXin Li
20*67e74705SXin LiThe main difference from writing normal ``FrontendActions`` is that you can
21*67e74705SXin Lihandle plugin command line options. The ``PluginASTAction`` base class declares
22*67e74705SXin Lia ``ParseArgs`` method which you have to implement in your plugin.
23*67e74705SXin Li
24*67e74705SXin Li.. code-block:: c++
25*67e74705SXin Li
26*67e74705SXin Li  bool ParseArgs(const CompilerInstance &CI,
27*67e74705SXin Li                 const std::vector<std::string>& args) {
28*67e74705SXin Li    for (unsigned i = 0, e = args.size(); i != e; ++i) {
29*67e74705SXin Li      if (args[i] == "-some-arg") {
30*67e74705SXin Li        // Handle the command line argument.
31*67e74705SXin Li      }
32*67e74705SXin Li    }
33*67e74705SXin Li    return true;
34*67e74705SXin Li  }
35*67e74705SXin Li
36*67e74705SXin LiRegistering a plugin
37*67e74705SXin Li====================
38*67e74705SXin Li
39*67e74705SXin LiA plugin is loaded from a dynamic library at runtime by the compiler. To
40*67e74705SXin Liregister a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
41*67e74705SXin Li
42*67e74705SXin Li.. code-block:: c++
43*67e74705SXin Li
44*67e74705SXin Li  static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
45*67e74705SXin Li
46*67e74705SXin LiDefining pragmas
47*67e74705SXin Li================
48*67e74705SXin Li
49*67e74705SXin LiPlugins can also define pragmas by declaring a ``PragmaHandler`` and
50*67e74705SXin Liregistering it using ``PragmaHandlerRegistry::Add<>``:
51*67e74705SXin Li
52*67e74705SXin Li.. code-block:: c++
53*67e74705SXin Li
54*67e74705SXin Li  // Define a pragma handler for #pragma example_pragma
55*67e74705SXin Li  class ExamplePragmaHandler : public PragmaHandler {
56*67e74705SXin Li  public:
57*67e74705SXin Li    ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
58*67e74705SXin Li    void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
59*67e74705SXin Li                      Token &PragmaTok) {
60*67e74705SXin Li      // Handle the pragma
61*67e74705SXin Li    }
62*67e74705SXin Li  };
63*67e74705SXin Li
64*67e74705SXin Li  static PragmaHandlerRegistry::Add<ExamplePragmaHandler> Y("example_pragma","example pragma description");
65*67e74705SXin Li
66*67e74705SXin LiPutting it all together
67*67e74705SXin Li=======================
68*67e74705SXin Li
69*67e74705SXin LiLet's look at an example plugin that prints top-level function names.  This
70*67e74705SXin Liexample is checked into the clang repository; please take a look at
71*67e74705SXin Lithe `latest version of PrintFunctionNames.cpp
72*67e74705SXin Li<http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup>`_.
73*67e74705SXin Li
74*67e74705SXin LiRunning the plugin
75*67e74705SXin Li==================
76*67e74705SXin Li
77*67e74705SXin Li
78*67e74705SXin LiUsing the cc1 command line
79*67e74705SXin Li--------------------------
80*67e74705SXin Li
81*67e74705SXin LiTo run a plugin, the dynamic library containing the plugin registry must be
82*67e74705SXin Liloaded via the `-load` command line option. This will load all plugins
83*67e74705SXin Lithat are registered, and you can select the plugins to run by specifying the
84*67e74705SXin Li`-plugin` option. Additional parameters for the plugins can be passed with
85*67e74705SXin Li`-plugin-arg-<plugin-name>`.
86*67e74705SXin Li
87*67e74705SXin LiNote that those options must reach clang's cc1 process. There are two
88*67e74705SXin Liways to do so:
89*67e74705SXin Li
90*67e74705SXin Li* Directly call the parsing process by using the `-cc1` option; this
91*67e74705SXin Li  has the downside of not configuring the default header search paths, so
92*67e74705SXin Li  you'll need to specify the full system path configuration on the command
93*67e74705SXin Li  line.
94*67e74705SXin Li* Use clang as usual, but prefix all arguments to the cc1 process with
95*67e74705SXin Li  `-Xclang`.
96*67e74705SXin Li
97*67e74705SXin LiFor example, to run the ``print-function-names`` plugin over a source file in
98*67e74705SXin Liclang, first build the plugin, and then call clang with the plugin from the
99*67e74705SXin Lisource tree:
100*67e74705SXin Li
101*67e74705SXin Li.. code-block:: console
102*67e74705SXin Li
103*67e74705SXin Li  $ export BD=/path/to/build/directory
104*67e74705SXin Li  $ (cd $BD && make PrintFunctionNames )
105*67e74705SXin Li  $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
106*67e74705SXin Li            -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
107*67e74705SXin Li            -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
108*67e74705SXin Li            tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
109*67e74705SXin Li            -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
110*67e74705SXin Li            -plugin -Xclang print-fns
111*67e74705SXin Li
112*67e74705SXin LiAlso see the print-function-name plugin example's
113*67e74705SXin Li`README <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup>`_
114*67e74705SXin Li
115*67e74705SXin Li
116*67e74705SXin LiUsing the clang command line
117*67e74705SXin Li----------------------------
118*67e74705SXin Li
119*67e74705SXin LiUsing `-fplugin=plugin` on the clang command line passes the plugin
120*67e74705SXin Lithrough as an argument to `-load` on the cc1 command line. If the plugin
121*67e74705SXin Liclass implements the ``getActionType`` method then the plugin is run
122*67e74705SXin Liautomatically. For example, to run the plugin automatically after the main AST
123*67e74705SXin Liaction (i.e. the same as using `-add-plugin`):
124*67e74705SXin Li
125*67e74705SXin Li.. code-block:: c++
126*67e74705SXin Li
127*67e74705SXin Li  // Automatically run the plugin after the main AST action
128*67e74705SXin Li  PluginASTAction::ActionType getActionType() override {
129*67e74705SXin Li    return AddAfterMainAction;
130*67e74705SXin Li  }
131