ClangPlugins.rst 4.5 KB

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