ClangPlugins.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. =============
  2. Clang Plugins
  3. =============
  4. NOTE: this document applies to the original Clang project, not the DirectX
  5. Compiler. It's made available for informational purposes only.
  6. Clang Plugins make it possible to run extra user defined actions during a
  7. compilation. This document will provide a basic walkthrough of how to write and
  8. run a Clang Plugin.
  9. Introduction
  10. ============
  11. Clang Plugins run FrontendActions over code. See the :doc:`FrontendAction
  12. tutorial <RAVFrontendAction>` on how to write a ``FrontendAction`` using the
  13. ``RecursiveASTVisitor``. In this tutorial, we'll demonstrate how to write a
  14. simple clang plugin.
  15. Writing a ``PluginASTAction``
  16. =============================
  17. The main difference from writing normal ``FrontendActions`` is that you can
  18. handle plugin command line options. The ``PluginASTAction`` base class declares
  19. a ``ParseArgs`` method which you have to implement in your plugin.
  20. .. code-block:: c++
  21. bool ParseArgs(const CompilerInstance &CI,
  22. const std::vector<std::string>& args) {
  23. for (unsigned i = 0, e = args.size(); i != e; ++i) {
  24. if (args[i] == "-some-arg") {
  25. // Handle the command line argument.
  26. }
  27. }
  28. return true;
  29. }
  30. Registering a plugin
  31. ====================
  32. A plugin is loaded from a dynamic library at runtime by the compiler. To
  33. register a plugin in a library, use ``FrontendPluginRegistry::Add<>``:
  34. .. code-block:: c++
  35. static FrontendPluginRegistry::Add<MyPlugin> X("my-plugin-name", "my plugin description");
  36. Putting it all together
  37. =======================
  38. Let's look at an example plugin that prints top-level function names. This
  39. example is checked into the clang repository; please take a look at
  40. the `latest version of PrintFunctionNames.cpp
  41. <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?view=markup>`_.
  42. Running the plugin
  43. ==================
  44. To run a plugin, the dynamic library containing the plugin registry must be
  45. loaded via the :option:`-load` command line option. This will load all plugins
  46. that are registered, and you can select the plugins to run by specifying the
  47. :option:`-plugin` option. Additional parameters for the plugins can be passed with
  48. :option:`-plugin-arg-<plugin-name>`.
  49. Note that those options must reach clang's cc1 process. There are two
  50. ways to do so:
  51. * Directly call the parsing process by using the :option:`-cc1` option; this
  52. has the downside of not configuring the default header search paths, so
  53. you'll need to specify the full system path configuration on the command
  54. line.
  55. * Use clang as usual, but prefix all arguments to the cc1 process with
  56. :option:`-Xclang`.
  57. For example, to run the ``print-function-names`` plugin over a source file in
  58. clang, first build the plugin, and then call clang with the plugin from the
  59. source tree:
  60. .. code-block:: console
  61. $ export BD=/path/to/build/directory
  62. $ (cd $BD && make PrintFunctionNames )
  63. $ clang++ -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS \
  64. -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE \
  65. -I$BD/tools/clang/include -Itools/clang/include -I$BD/include -Iinclude \
  66. tools/clang/tools/clang-check/ClangCheck.cpp -fsyntax-only \
  67. -Xclang -load -Xclang $BD/lib/PrintFunctionNames.so -Xclang \
  68. -plugin -Xclang print-fns
  69. Also see the print-function-name plugin example's
  70. `README <http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/README.txt?view=markup>`_