2012-12-13 09:10:46 +08:00
|
|
|
=============
|
|
|
|
Clang Plugins
|
|
|
|
=============
|
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
Clang Plugins make it possible to run extra user defined actions during a
|
|
|
|
compilation. This document will provide a basic walkthrough of how to write and
|
|
|
|
run a Clang Plugin.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
|
|
|
Clang Plugins run FrontendActions over code. See the :doc:`FrontendAction
|
2012-12-16 05:10:51 +08:00
|
|
|
tutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
|
|
|
|
``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
|
|
|
|
simple clang plugin.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
Writing a ``PluginASTAction``
|
|
|
|
=============================
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
The main difference from writing normal ``FrontendActions`` is that you can
|
|
|
|
handle plugin command line options. The ``PluginASTAction`` base class declares
|
|
|
|
a ``ParseArgs`` method which you have to implement in your plugin.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
.. code-block:: c++
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
bool ParseArgs(const CompilerInstance &CI,
|
|
|
|
const std::vector<std::string>& args) {
|
|
|
|
for (unsigned i = 0, e = args.size(); i != e; ++i) {
|
|
|
|
if (args[i] == "-some-arg") {
|
|
|
|
// Handle the command line argument.
|
2012-12-13 09:10:46 +08:00
|
|
|
}
|
2012-12-16 05:10:51 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-13 09:10:46 +08:00
|
|
|
|
|
|
|
Registering a plugin
|
|
|
|
====================
|
|
|
|
|
|
|
|
A plugin is loaded from a dynamic library at runtime by the compiler. To
|
2016-02-12 00:33:20 +08:00
|
|
|
register a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
.. code-block:: c++
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2016-04-04 22:22:58 +08:00
|
|
|
Defining pragmas
|
|
|
|
================
|
|
|
|
|
|
|
|
Plugins can also define pragmas by declaring a ``PragmaHandler`` and
|
|
|
|
registering it using ``PragmaHandlerRegistry::Add<>``:
|
|
|
|
|
|
|
|
.. code-block:: c++
|
|
|
|
|
|
|
|
// Define a pragma handler for #pragma example_pragma
|
|
|
|
class ExamplePragmaHandler : public PragmaHandler {
|
|
|
|
public:
|
|
|
|
ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2016-04-04 22:22:58 +08:00
|
|
|
Token &PragmaTok) {
|
|
|
|
// Handle the pragma
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PragmaHandlerRegistry::Add<ExamplePragmaHandler> Y("example_pragma","example pragma description");
|
|
|
|
|
2012-12-13 09:10:46 +08:00
|
|
|
Putting it all together
|
|
|
|
=======================
|
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
Let's look at an example plugin that prints top-level function names. This
|
2014-01-26 13:08:07 +08:00
|
|
|
example is checked into the clang repository; please take a look at
|
|
|
|
the `latest version of PrintFunctionNames.cpp
|
2019-01-30 00:37:27 +08:00
|
|
|
<https://github.com/llvm/llvm-project/blob/master/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp>`_.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
|
|
|
Running the plugin
|
|
|
|
==================
|
|
|
|
|
2016-03-15 20:51:40 +08:00
|
|
|
|
|
|
|
Using the cc1 command line
|
|
|
|
--------------------------
|
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
To run a plugin, the dynamic library containing the plugin registry must be
|
2016-07-14 22:07:37 +08:00
|
|
|
loaded via the `-load` command line option. This will load all plugins
|
2012-12-16 05:10:51 +08:00
|
|
|
that are registered, and you can select the plugins to run by specifying the
|
2016-07-14 22:07:37 +08:00
|
|
|
`-plugin` option. Additional parameters for the plugins can be passed with
|
|
|
|
`-plugin-arg-<plugin-name>`.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
|
|
|
Note that those options must reach clang's cc1 process. There are two
|
|
|
|
ways to do so:
|
|
|
|
|
2016-07-14 22:07:37 +08:00
|
|
|
* Directly call the parsing process by using the `-cc1` option; this
|
2012-12-16 05:10:51 +08:00
|
|
|
has the downside of not configuring the default header search paths, so
|
|
|
|
you'll need to specify the full system path configuration on the command
|
|
|
|
line.
|
|
|
|
* Use clang as usual, but prefix all arguments to the cc1 process with
|
2016-07-14 22:07:37 +08:00
|
|
|
`-Xclang`.
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
For example, to run the ``print-function-names`` plugin over a source file in
|
|
|
|
clang, first build the plugin, and then call clang with the plugin from the
|
|
|
|
source tree:
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
.. code-block:: console
|
2012-12-13 09:10:46 +08:00
|
|
|
|
2012-12-16 05:10:51 +08:00
|
|
|
$ export BD=/path/to/build/directory
|
|
|
|
$ (cd $BD && make PrintFunctionNames )
|
|
|
|
$ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
|
2012-12-13 09:10:46 +08:00
|
|
|
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
|
|
|
|
-I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
|
|
|
|
tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
|
|
|
|
-Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
|
|
|
|
-plugin -Xclang print-fns
|
|
|
|
|
|
|
|
Also see the print-function-name plugin example's
|
2019-01-30 00:37:27 +08:00
|
|
|
`README <https://github.com/llvm/llvm-project/blob/master/clang/examples/PrintFunctionNames/README.txt>`_
|
2012-12-16 05:10:51 +08:00
|
|
|
|
2016-03-15 20:51:40 +08:00
|
|
|
|
|
|
|
Using the clang command line
|
|
|
|
----------------------------
|
|
|
|
|
2016-07-14 22:07:37 +08:00
|
|
|
Using `-fplugin=plugin` on the clang command line passes the plugin
|
|
|
|
through as an argument to `-load` on the cc1 command line. If the plugin
|
2016-03-15 20:51:40 +08:00
|
|
|
class implements the ``getActionType`` method then the plugin is run
|
|
|
|
automatically. For example, to run the plugin automatically after the main AST
|
2016-07-14 22:07:37 +08:00
|
|
|
action (i.e. the same as using `-add-plugin`):
|
2016-03-15 20:51:40 +08:00
|
|
|
|
|
|
|
.. code-block:: c++
|
|
|
|
|
|
|
|
// Automatically run the plugin after the main AST action
|
|
|
|
PluginASTAction::ActionType getActionType() override {
|
|
|
|
return AddAfterMainAction;
|
|
|
|
}
|