Plugin-API.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: Plugin API</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Plugin API">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Plugin API">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Plugins.html#Plugins" rel="up" title="Plugins">
  30. <link href="Plugins-pass.html#Plugins-pass" rel="next" title="Plugins pass">
  31. <link href="Plugins-loading.html#Plugins-loading" rel="prev" title="Plugins loading">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Plugin-API"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Plugins-pass.html#Plugins-pass" accesskey="n" rel="next">Plugins pass</a>, Previous: <a href="Plugins-loading.html#Plugins-loading" accesskey="p" rel="prev">Plugins loading</a>, Up: <a href="Plugins.html#Plugins" accesskey="u" rel="up">Plugins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Plugin-API-1"></a>
  69. <h3 class="section">23.2 Plugin API</h3>
  70. <p>Plugins are activated by the compiler at specific events as defined in
  71. <samp>gcc-plugin.h</samp>. For each event of interest, the plugin should
  72. call <code>register_callback</code> specifying the name of the event and
  73. address of the callback function that will handle that event.
  74. </p>
  75. <p>The header <samp>gcc-plugin.h</samp> must be the first gcc header to be included.
  76. </p>
  77. <a name="Plugin-license-check"></a>
  78. <h4 class="subsection">23.2.1 Plugin license check</h4>
  79. <p>Every plugin should define the global symbol <code>plugin_is_GPL_compatible</code>
  80. to assert that it has been licensed under a GPL-compatible license.
  81. If this symbol does not exist, the compiler will emit a fatal error
  82. and exit with the error message:
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample">fatal error: plugin <var>name</var> is not licensed under a GPL-compatible license
  86. <var>name</var>: undefined symbol: plugin_is_GPL_compatible
  87. compilation terminated
  88. </pre></div>
  89. <p>The declared type of the symbol should be int, to match a forward declaration
  90. in <samp>gcc-plugin.h</samp> that suppresses C++ mangling. It does not need to be in
  91. any allocated section, though. The compiler merely asserts that
  92. the symbol exists in the global scope. Something like this is enough:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">int plugin_is_GPL_compatible;
  96. </pre></div>
  97. <a name="Plugin-initialization"></a>
  98. <h4 class="subsection">23.2.2 Plugin initialization</h4>
  99. <p>Every plugin should export a function called <code>plugin_init</code> that
  100. is called right after the plugin is loaded. This function is
  101. responsible for registering all the callbacks required by the plugin
  102. and do any other required initialization.
  103. </p>
  104. <p>This function is called from <code>compile_file</code> right before invoking
  105. the parser. The arguments to <code>plugin_init</code> are:
  106. </p>
  107. <ul>
  108. <li> <code>plugin_info</code>: Plugin invocation information.
  109. </li><li> <code>version</code>: GCC version.
  110. </li></ul>
  111. <p>The <code>plugin_info</code> struct is defined as follows:
  112. </p>
  113. <div class="smallexample">
  114. <pre class="smallexample">struct plugin_name_args
  115. {
  116. char *base_name; /* Short name of the plugin
  117. (filename without .so suffix). */
  118. const char *full_name; /* Path to the plugin as specified with
  119. -fplugin=. */
  120. int argc; /* Number of arguments specified with
  121. -fplugin-arg-.... */
  122. struct plugin_argument *argv; /* Array of ARGC key-value pairs. */
  123. const char *version; /* Version string provided by plugin. */
  124. const char *help; /* Help string provided by plugin. */
  125. }
  126. </pre></div>
  127. <p>If initialization fails, <code>plugin_init</code> must return a non-zero
  128. value. Otherwise, it should return 0.
  129. </p>
  130. <p>The version of the GCC compiler loading the plugin is described by the
  131. following structure:
  132. </p>
  133. <div class="smallexample">
  134. <pre class="smallexample">struct plugin_gcc_version
  135. {
  136. const char *basever;
  137. const char *datestamp;
  138. const char *devphase;
  139. const char *revision;
  140. const char *configuration_arguments;
  141. };
  142. </pre></div>
  143. <p>The function <code>plugin_default_version_check</code> takes two pointers to
  144. such structure and compare them field by field. It can be used by the
  145. plugin&rsquo;s <code>plugin_init</code> function.
  146. </p>
  147. <p>The version of GCC used to compile the plugin can be found in the symbol
  148. <code>gcc_version</code> defined in the header <samp>plugin-version.h</samp>. The
  149. recommended version check to perform looks like
  150. </p>
  151. <div class="smallexample">
  152. <pre class="smallexample">#include &quot;plugin-version.h&quot;
  153. ...
  154. int
  155. plugin_init (struct plugin_name_args *plugin_info,
  156. struct plugin_gcc_version *version)
  157. {
  158. if (!plugin_default_version_check (version, &amp;gcc_version))
  159. return 1;
  160. }
  161. </pre></div>
  162. <p>but you can also check the individual fields if you want a less strict check.
  163. </p>
  164. <a name="Plugin-callbacks"></a>
  165. <h4 class="subsection">23.2.3 Plugin callbacks</h4>
  166. <p>Callback functions have the following prototype:
  167. </p>
  168. <div class="smallexample">
  169. <pre class="smallexample">/* The prototype for a plugin callback function.
  170. gcc_data - event-specific data provided by GCC
  171. user_data - plugin-specific data provided by the plug-in. */
  172. typedef void (*plugin_callback_func)(void *gcc_data, void *user_data);
  173. </pre></div>
  174. <p>Callbacks can be invoked at the following pre-determined events:
  175. </p>
  176. <div class="smallexample">
  177. <pre class="smallexample">enum plugin_event
  178. {
  179. PLUGIN_START_PARSE_FUNCTION, /* Called before parsing the body of a function. */
  180. PLUGIN_FINISH_PARSE_FUNCTION, /* After finishing parsing a function. */
  181. PLUGIN_PASS_MANAGER_SETUP, /* To hook into pass manager. */
  182. PLUGIN_FINISH_TYPE, /* After finishing parsing a type. */
  183. PLUGIN_FINISH_DECL, /* After finishing parsing a declaration. */
  184. PLUGIN_FINISH_UNIT, /* Useful for summary processing. */
  185. PLUGIN_PRE_GENERICIZE, /* Allows to see low level AST in C and C++ frontends. */
  186. PLUGIN_FINISH, /* Called before GCC exits. */
  187. PLUGIN_INFO, /* Information about the plugin. */
  188. PLUGIN_GGC_START, /* Called at start of GCC Garbage Collection. */
  189. PLUGIN_GGC_MARKING, /* Extend the GGC marking. */
  190. PLUGIN_GGC_END, /* Called at end of GGC. */
  191. PLUGIN_REGISTER_GGC_ROOTS, /* Register an extra GGC root table. */
  192. PLUGIN_ATTRIBUTES, /* Called during attribute registration */
  193. PLUGIN_START_UNIT, /* Called before processing a translation unit. */
  194. PLUGIN_PRAGMAS, /* Called during pragma registration. */
  195. /* Called before first pass from all_passes. */
  196. PLUGIN_ALL_PASSES_START,
  197. /* Called after last pass from all_passes. */
  198. PLUGIN_ALL_PASSES_END,
  199. /* Called before first ipa pass. */
  200. PLUGIN_ALL_IPA_PASSES_START,
  201. /* Called after last ipa pass. */
  202. PLUGIN_ALL_IPA_PASSES_END,
  203. /* Allows to override pass gate decision for current_pass. */
  204. PLUGIN_OVERRIDE_GATE,
  205. /* Called before executing a pass. */
  206. PLUGIN_PASS_EXECUTION,
  207. /* Called before executing subpasses of a GIMPLE_PASS in
  208. execute_ipa_pass_list. */
  209. PLUGIN_EARLY_GIMPLE_PASSES_START,
  210. /* Called after executing subpasses of a GIMPLE_PASS in
  211. execute_ipa_pass_list. */
  212. PLUGIN_EARLY_GIMPLE_PASSES_END,
  213. /* Called when a pass is first instantiated. */
  214. PLUGIN_NEW_PASS,
  215. /* Called when a file is #include-d or given via the #line directive.
  216. This could happen many times. The event data is the included file path,
  217. as a const char* pointer. */
  218. PLUGIN_INCLUDE_FILE,
  219. PLUGIN_EVENT_FIRST_DYNAMIC /* Dummy event used for indexing callback
  220. array. */
  221. };
  222. </pre></div>
  223. <p>In addition, plugins can also look up the enumerator of a named event,
  224. and / or generate new events dynamically, by calling the function
  225. <code>get_named_event_id</code>.
  226. </p>
  227. <p>To register a callback, the plugin calls <code>register_callback</code> with
  228. the arguments:
  229. </p>
  230. <ul>
  231. <li> <code>char *name</code>: Plugin name.
  232. </li><li> <code>int event</code>: The event code.
  233. </li><li> <code>plugin_callback_func callback</code>: The function that handles <code>event</code>.
  234. </li><li> <code>void *user_data</code>: Pointer to plugin-specific data.
  235. </li></ul>
  236. <p>For the <i>PLUGIN_PASS_MANAGER_SETUP</i>, <i>PLUGIN_INFO</i>, and
  237. <i>PLUGIN_REGISTER_GGC_ROOTS</i> pseudo-events the <code>callback</code> should be null,
  238. and the <code>user_data</code> is specific.
  239. </p>
  240. <p>When the <i>PLUGIN_PRAGMAS</i> event is triggered (with a null pointer as
  241. data from GCC), plugins may register their own pragmas. Notice that
  242. pragmas are not available from <samp>lto1</samp>, so plugins used with
  243. <code>-flto</code> option to GCC during link-time optimization cannot use
  244. pragmas and do not even see functions like <code>c_register_pragma</code> or
  245. <code>pragma_lex</code>.
  246. </p>
  247. <p>The <i>PLUGIN_INCLUDE_FILE</i> event, with a <code>const char*</code> file path as
  248. GCC data, is triggered for processing of <code>#include</code> or
  249. <code>#line</code> directives.
  250. </p>
  251. <p>The <i>PLUGIN_FINISH</i> event is the last time that plugins can call GCC
  252. functions, notably emit diagnostics with <code>warning</code>, <code>error</code>
  253. etc.
  254. </p>
  255. <hr>
  256. <div class="header">
  257. <p>
  258. Next: <a href="Plugins-pass.html#Plugins-pass" accesskey="n" rel="next">Plugins pass</a>, Previous: <a href="Plugins-loading.html#Plugins-loading" accesskey="p" rel="prev">Plugins loading</a>, Up: <a href="Plugins.html#Plugins" accesskey="u" rel="up">Plugins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  259. </div>
  260. </body>
  261. </html>