You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 satır
12KB

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