annotate clang/docs/ClangPlugins.rst @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 =============
anatofuz
parents:
diff changeset
2 Clang Plugins
anatofuz
parents:
diff changeset
3 =============
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 Clang Plugins make it possible to run extra user defined actions during a
anatofuz
parents:
diff changeset
6 compilation. This document will provide a basic walkthrough of how to write and
anatofuz
parents:
diff changeset
7 run a Clang Plugin.
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 Introduction
anatofuz
parents:
diff changeset
10 ============
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 Clang Plugins run FrontendActions over code. See the :doc:`FrontendAction
anatofuz
parents:
diff changeset
13 tutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
anatofuz
parents:
diff changeset
14 ``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
anatofuz
parents:
diff changeset
15 simple clang plugin.
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 Writing a ``PluginASTAction``
anatofuz
parents:
diff changeset
18 =============================
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 The main difference from writing normal ``FrontendActions`` is that you can
anatofuz
parents:
diff changeset
21 handle plugin command line options. The ``PluginASTAction`` base class declares
anatofuz
parents:
diff changeset
22 a ``ParseArgs`` method which you have to implement in your plugin.
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 .. code-block:: c++
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 bool ParseArgs(const CompilerInstance &CI,
anatofuz
parents:
diff changeset
27 const std::vector<std::string>& args) {
anatofuz
parents:
diff changeset
28 for (unsigned i = 0, e = args.size(); i != e; ++i) {
anatofuz
parents:
diff changeset
29 if (args[i] == "-some-arg") {
anatofuz
parents:
diff changeset
30 // Handle the command line argument.
anatofuz
parents:
diff changeset
31 }
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33 return true;
anatofuz
parents:
diff changeset
34 }
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 Registering a plugin
anatofuz
parents:
diff changeset
37 ====================
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 A plugin is loaded from a dynamic library at runtime by the compiler. To
anatofuz
parents:
diff changeset
40 register a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 .. code-block:: c++
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 Defining pragmas
anatofuz
parents:
diff changeset
47 ================
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 Plugins can also define pragmas by declaring a ``PragmaHandler`` and
anatofuz
parents:
diff changeset
50 registering it using ``PragmaHandlerRegistry::Add<>``:
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 .. code-block:: c++
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 // Define a pragma handler for #pragma example_pragma
anatofuz
parents:
diff changeset
55 class ExamplePragmaHandler : public PragmaHandler {
anatofuz
parents:
diff changeset
56 public:
anatofuz
parents:
diff changeset
57 ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
anatofuz
parents:
diff changeset
58 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
anatofuz
parents:
diff changeset
59 Token &PragmaTok) {
anatofuz
parents:
diff changeset
60 // Handle the pragma
anatofuz
parents:
diff changeset
61 }
anatofuz
parents:
diff changeset
62 };
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 static PragmaHandlerRegistry::Add<ExamplePragmaHandler> Y("example_pragma","example pragma description");
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 Putting it all together
anatofuz
parents:
diff changeset
67 =======================
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 Let's look at an example plugin that prints top-level function names. This
anatofuz
parents:
diff changeset
70 example is checked into the clang repository; please take a look at
anatofuz
parents:
diff changeset
71 the `latest version of PrintFunctionNames.cpp
anatofuz
parents:
diff changeset
72 <https://github.com/llvm/llvm-project/blob/master/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp>`_.
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 Running the plugin
anatofuz
parents:
diff changeset
75 ==================
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 Using the cc1 command line
anatofuz
parents:
diff changeset
79 --------------------------
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 To run a plugin, the dynamic library containing the plugin registry must be
anatofuz
parents:
diff changeset
82 loaded via the `-load` command line option. This will load all plugins
anatofuz
parents:
diff changeset
83 that are registered, and you can select the plugins to run by specifying the
anatofuz
parents:
diff changeset
84 `-plugin` option. Additional parameters for the plugins can be passed with
anatofuz
parents:
diff changeset
85 `-plugin-arg-<plugin-name>`.
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 Note that those options must reach clang's cc1 process. There are two
anatofuz
parents:
diff changeset
88 ways to do so:
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 * Directly call the parsing process by using the `-cc1` option; this
anatofuz
parents:
diff changeset
91 has the downside of not configuring the default header search paths, so
anatofuz
parents:
diff changeset
92 you'll need to specify the full system path configuration on the command
anatofuz
parents:
diff changeset
93 line.
anatofuz
parents:
diff changeset
94 * Use clang as usual, but prefix all arguments to the cc1 process with
anatofuz
parents:
diff changeset
95 `-Xclang`.
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 For example, to run the ``print-function-names`` plugin over a source file in
anatofuz
parents:
diff changeset
98 clang, first build the plugin, and then call clang with the plugin from the
anatofuz
parents:
diff changeset
99 source tree:
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 .. code-block:: console
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 $ export BD=/path/to/build/directory
anatofuz
parents:
diff changeset
104 $ (cd $BD && make PrintFunctionNames )
anatofuz
parents:
diff changeset
105 $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
anatofuz
parents:
diff changeset
106 -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
anatofuz
parents:
diff changeset
107 -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
anatofuz
parents:
diff changeset
108 tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
anatofuz
parents:
diff changeset
109 -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
anatofuz
parents:
diff changeset
110 -plugin -Xclang print-fns
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 Also see the print-function-name plugin example's
anatofuz
parents:
diff changeset
113 `README <https://github.com/llvm/llvm-project/blob/master/clang/examples/PrintFunctionNames/README.txt>`_
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 Using the clang command line
anatofuz
parents:
diff changeset
117 ----------------------------
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 Using `-fplugin=plugin` on the clang command line passes the plugin
anatofuz
parents:
diff changeset
120 through as an argument to `-load` on the cc1 command line. If the plugin
anatofuz
parents:
diff changeset
121 class implements the ``getActionType`` method then the plugin is run
anatofuz
parents:
diff changeset
122 automatically. For example, to run the plugin automatically after the main AST
anatofuz
parents:
diff changeset
123 action (i.e. the same as using `-add-plugin`):
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 .. code-block:: c++
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 // Automatically run the plugin after the main AST action
anatofuz
parents:
diff changeset
128 PluginASTAction::ActionType getActionType() override {
anatofuz
parents:
diff changeset
129 return AddAfterMainAction;
anatofuz
parents:
diff changeset
130 }