Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In the <em>upcoming</em> state: Manuel Klimek, from Google, is integrating in the Clang mainline a tool that has been developed at Google for querying and transforming C++ code.</p> <ul> <li><p>The tooling infrastructure has been layed out, it may fill up but it is already functional. The main idea is that it allows you to define actions and will run those actions on the selected files.</p></li> <li><p>Google has created a simple set of C++ classes and methods to allow querying the AST in a friendly way: the AST Matcher framework, it is being developped and will allow very precise matching in the end.</p></li> </ul> <p>It requires creating an executable at the moment, but the code is provided as libraries so it's not necessary to edit it, and one-off transformation tools can be dealt with in a single source file.</p> <hr> <p>Example of the Matcher (<a href="http://clang-developers.42468.n3.nabble.com/Fwd-Patch-AST-Matcher-Framwork-and-an-example-tool-td3016371.html">found in this thread</a>): the goal is to find calls to the constructor overload of <code>std::string</code> formed from the result of <code>std::string::c_str()</code> (with the default allocator), because it can be replaced by a simple copy instead.</p> <pre><code>ConstructorCall( HasDeclaration(Method(HasName(StringConstructor))), ArgumentCountIs(2), // The first argument must have the form x.c_str() or p-&gt;c_str() // where the method is string::c_str(). We can use the copy // constructor of string instead (or the compiler might share // the string object). HasArgument( 0, Id("call", Call( Callee(Id("member", MemberExpression())), Callee(Method(HasName(StringCStrMethod))), On(Id("arg", Expression())) )) ), // The second argument is the alloc object which must not be // present explicitly. HasArgument(1, DefaultArgument()) ) </code></pre> <p>It is very promising compared to ad-hoc tool because it uses the Clang compiler AST library, so not only it is guaranteed that no matter how complicated the macros and template stuff that are used, as long as your code compiles it can be analyzed; but it also means that intricates queries that depend on the result of overload resolution can be expressed.</p> <p>This code returns actual AST nodes from within the Clang library, so the programmer can locate the bits and nits precisely in the source file and edit to tweak it according to her needs.</p> <p>There has been talk about using a textual matching specification, however it was deemed better to start with the C++ API as it would have added much complexity (and bike-shedding). I hope a Python API will emerge.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload