Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COI hoped that someone would come up with something based on llvm! I found only the [Clang Static Analyzer](http://clang-analyzer.llvm.org/) when searching the web for something like that and unfortunately this project didn't seem very flexible with respect to custom rules. This AST Matcher Framwork sounds very promising and as long as the C++ API is as nice as the given example, there is no need for a special config file syntax :-)
      singulars
    2. CO@g_daniel: you are right that the static analyzer is not customizable. In the past I think a few style rules made it into it (for lack of a better tool), but in the end it is more a tool to reason about safety issues (memory overwrite, etc...). I am not sure whether the AST Matcher would be suitable for style analysis, I see it more as a C++ aware version of `grep` and `sed` :)
      singulars
    3. COI'm not looking for style analyzers, but for analyzers of coding conventions beyond style ;-) i.e. for coding conventions like "don't use std::auto_ptr, use std::shared_ptr" or "don't throw ints to indicate error codes, don't throw (C-)strings with error messages, throw proper exception classes derived from std::exception". I think the AST Matcher Framework would be great for implementing something like that. I'll wait a little longer to see if there are some already released alternatives. If there are none, I'll accept your answer ;-)
      singulars
 

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