<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Working with the preprocessor — gcc-python-plugin 0.9 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '0.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="top" title="gcc-python-plugin 0.9 documentation" href="index.html" /> <link rel="up" title="Miscellanea" href="misc.html" /> <link rel="next" title="Version handling" href="versions.html" /> <link rel="prev" title="Working with GCC’s tunable parameters" href="parameters.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="versions.html" title="Version handling" accesskey="N">next</a> |</li> <li class="right" > <a href="parameters.html" title="Working with GCC’s tunable parameters" accesskey="P">previous</a> |</li> <li><a href="index.html">gcc-python-plugin 0.9 documentation</a> »</li> <li><a href="misc.html" accesskey="U">Miscellanea</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="working-with-the-preprocessor"> <h1>Working with the preprocessor<a class="headerlink" href="#working-with-the-preprocessor" title="Permalink to this headline">¶</a></h1> <p>For languages that support a preprocessor, it’s possible to inject new “built-in” macros into the compilation from a Python script.</p> <p>The motivation for this is to better support the creation of custom attributes, by creating preprocessor names that can be tested against.</p> <dl class="function"> <dt id="gcc.define_macro"> <tt class="descclassname">gcc.</tt><tt class="descname">define_macro</tt><big>(</big><em>argument</em><big>)</big><a class="headerlink" href="#gcc.define_macro" title="Permalink to this definition">¶</a></dt> <dd><p>Defines a preprocessor macro with the given argument, which may be of use for code that needs to test for the presence of your script. The argument can either be a simple name, or a name with a definition:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">gcc</span><span class="o">.</span><span class="n">define_macro</span><span class="p">(</span><span class="s">"SOMETHING"</span><span class="p">)</span> <span class="c"># define as the empty string</span> <span class="n">gcc</span><span class="o">.</span><span class="n">define_macro</span><span class="p">(</span><span class="s">"SOMETHING=72"</span><span class="p">)</span> </pre></div> </div> <p>This function can only be called from within specific event callbacks, since it manipulates the state of the preprocessor for a given source file.</p> <p>For now, only call it in a handler for the event <cite>gcc.PLUGIN_ATTRIBUTES</cite>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">gcc</span> <span class="k">def</span> <span class="nf">attribute_callback_for_claims_mutex</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">'attribute_callback_for_claims_mutex called: args: </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="p">))</span> <span class="k">def</span> <span class="nf">attribute_callback_for_releases_mutex</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">'attribute_callback_for_releases_mutex called: args: </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="p">))</span> <span class="k">def</span> <span class="nf">register_our_attributes</span><span class="p">():</span> <span class="n">gcc</span><span class="o">.</span><span class="n">register_attribute</span><span class="p">(</span><span class="s">'claims_mutex'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="n">attribute_callback_for_claims_mutex</span><span class="p">)</span> <span class="n">gcc</span><span class="o">.</span><span class="n">define_macro</span><span class="p">(</span><span class="s">'WITH_ATTRIBUTE_CLAIMS_MUTEX'</span><span class="p">)</span> <span class="n">gcc</span><span class="o">.</span><span class="n">register_attribute</span><span class="p">(</span><span class="s">'releases_mutex'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="n">attribute_callback_for_releases_mutex</span><span class="p">)</span> <span class="n">gcc</span><span class="o">.</span><span class="n">define_macro</span><span class="p">(</span><span class="s">'WITH_ATTRIBUTE_RELEASES_MUTEX'</span><span class="p">)</span> <span class="c"># Wire up our callback:</span> <span class="n">gcc</span><span class="o">.</span><span class="n">register_callback</span><span class="p">(</span><span class="n">gcc</span><span class="o">.</span><span class="n">PLUGIN_ATTRIBUTES</span><span class="p">,</span> <span class="n">register_our_attributes</span><span class="p">)</span> </pre></div> </div> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="parameters.html" title="previous chapter">Working with GCC’s tunable parameters</a></p> <h4>Next topic</h4> <p class="topless"><a href="versions.html" title="next chapter">Version handling</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/preprocessor.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="versions.html" title="Version handling" >next</a> |</li> <li class="right" > <a href="parameters.html" title="Working with GCC’s tunable parameters" >previous</a> |</li> <li><a href="index.html">gcc-python-plugin 0.9 documentation</a> »</li> <li><a href="misc.html" >Miscellanea</a> »</li> </ul> </div> <div class="footer"> © Copyright 2011, David Malcolm. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>