This patch implements an AST matching framework that allows to write tools that match on the C++ ASTs. The main interface is in ASTMatchers.h, an example implementation of a tool that removes redundant .c_str() calls is in the example RemoveCStrCalls.cpp (patch to llvm/clang produced by running this tool will be sent out shortly in an extra email).<br>
<br>Currently we have an in-language DSL that allows to write expressions such as (taken from the .c_str() example):<br><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 13px; "><table cellpadding="0" cellspacing="0" id="thecode" style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; ">
<tbody><tr><td class="udiff udiffadd" id="newcode187" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">ConstructorCall(
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-187"></td></tr><tr><td class="udiff udiffadd" id="newcode188" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     HasDeclaration(Method(HasName(StringConstructor))),
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-188"></td></tr><tr><td class="udiff udiffadd" id="newcode189" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     ArgumentCountIs(2),
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-189"></td></tr><tr><td class="udiff udiffadd" id="newcode190" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // The first argument must have the form x.c_str() or p->c_str()
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-190"></td></tr><tr><td class="udiff udiffadd" id="newcode191" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // where the method is string::c_str().  We can use the copy
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-191"></td></tr><tr><td class="udiff udiffadd" id="newcode192" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // constructor of string instead (or the compiler might share
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-192"></td></tr><tr><td class="udiff udiffadd" id="newcode193" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // the string object).
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-193"></td></tr><tr><td class="udiff udiffadd" id="newcode194" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     HasArgument(
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-194"></td></tr><tr><td class="udiff udiffadd" id="newcode195" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
         0,
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-195"></td></tr><tr><td class="udiff udiffadd" id="newcode196" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
         Id("call", Call(
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-196"></td></tr><tr><td class="udiff udiffadd" id="newcode197" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
             Callee(Id("member", MemberExpression())),
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-197"></td></tr><tr><td class="udiff udiffadd" id="newcode198" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
             Callee(Method(HasName(StringCStrMethod))),
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-198"></td></tr><tr><td class="udiff udiffadd" id="newcode199" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
             On(Id("arg", Expression()))))),
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-199"></td></tr><tr><td class="udiff udiffadd" id="newcode200" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // The second argument is the alloc object which must not be
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-200"></td></tr><tr><td class="udiff udiffadd" id="newcode201" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     // present explicitly.
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-201"></td></tr><tr><td class="udiff udiffadd" id="newcode202" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
     HasArgument(
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-202"></td></tr><tr><td class="udiff udiffadd" id="newcode203" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
         1,
</td></tr><tr class="inline-comments" style="background-color: rgb(229, 236, 249); "><td id="new-line-203"></td></tr><tr><td class="udiff udiffadd" id="newcode204" style="white-space: pre; font-family: monospace; font-size: 12px; vertical-align: top; ">
         DefaultArgument()))</td></tr></tbody></table></span><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">The next steps will be to build up better support for in-process refactorings based on the Rewriter, to build up higher-level matchers for common patterns, and to extend the low-level matcher library.</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">(rietveld link: </font><a href="http://codereview.appspot.com/4552059/">http://codereview.appspot.com/4552059/</a>)</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><meta http-equiv="content-type" content="text/html; charset=utf-8">