[cfe-dev] [RFC] Easier source-to-source transformations with clang tooling

Yitzhak Mandelbaum via cfe-dev cfe-dev at lists.llvm.org
Wed Feb 13 08:18:45 PST 2019


Jonas,

I've updated the revision  (https://reviews.llvm.org/D56933) with two
example clang tidies ported to Transformer.  These are not ideal checks,
because they are very particular in their handling of whitespace, but I
think they're good demonstrations both of Transformers strengths and its
(current) limitations.  Additionally, below are to versions of simple
example that isn't a tidy check yet.  Please let me know what you think.

Also, what are the next steps towards considering these libraries for
acceptance to clang?  Should I put together a diff(s) for review, or is
that still premature?


  // Simplify `e.emplace_back(std::make_pair(e1, e2))` to
`e.emplace_back(e1, e2)`.
  // We elide the extra matching required to check that `e` is in a
container like std::vector and that
  // the element type is the same as that constructed by the call to
make_pair.

  using extra_matchers::isFunctionNamed;
  using extra_matchers::isMethodNamed;

  // Version 1
  // This version has three metavariables -- one for the container and two
for the
  // arguments to `make_pair`.
  ExprId Call, Arg0, Arg1;
  auto MakePairCall =
      callExpr(callee(isFunctionNamed("::std::make_pair")),
               argumentCountIs(2), hasArgument(0, Arg0.bind()),
               hasArgument(1, Arg1.bind()));
  auto Rule1 = RewriteRule()
          .matching(cxxMemberCallExpr(
              callee(isMethodNamed("emplace_back")),
              argumentCountIs(1), hasArgument(0, bind(Call,
make_pair_call))))
          .change(Call)
          .replaceWith(Arg0, ",", Arg1));

  // Version 2
  // In this version, we extract the source code of the arguments using the
`arg()`
  // stencil operator on the node bound to `Call`.
  auto Rule2 = RewriteRule()
          .matching(cxxMemberCallExpr(
              callee(isMethodNamed("emplace_back")), argumentCountIs(1),
              hasArgument(0, bind(Call, callExpr(callee(isFunctionNamed(
                                            "::std::make_tuple")))))))
          .change(Call)
          .replaceWith(tooling::stencil_generators::args(Call)));

Thanks!
Yitzhak

On Fri, Jan 18, 2019 at 4:43 PM Yitzhak Mandelbaum <yitzhakm at google.com>
wrote:

> Thanks! I've added it to a revision (https://reviews.llvm.org/D56933),
> but I wasn't sure what lists to subscribe it to (if any) given that I don't
> intend it yet for proper review.
>
> I haven't ported check, but do have a demo check (using Transformer as a
> whole) that I can add. I'll ping the thread when that's done.
>
> On Fri, Jan 18, 2019 at 3:19 PM Jonas Toth <development at jonas-toth.eu>
> wrote:
>
>> Hi Yitzhak,
>>
>> your code looks very interesting.
>>
>> It might be a good fit for uitlity that clang-refactor can use, too. (not
>> sure about the state of clang-refactor though)
>> I would be interested how your framework integrates into clang-tidy, do
>> you have a reference check that you ported to your approach (i have seen
>> some references to clang-tidy code)?
>>
>> P.S. It might be a good idea to make your diff a full revision, as right
>> now the discussion feature can not be used.
>>
>> Best, Jonas
>> Am 18.01.19 um 19:29 schrieb Yitzhak Mandelbaum via cfe-dev:
>>
>> After some delay, I've created a diff to accompany the doc.  In
>> particular, here's the Stencil library, which provides for generating code
>> in a format-string style:
>>
>> https://reviews.llvm.org/differential/diff/182553/
>>
>> Please see the tests for various examples and the doc for more
>> explanation.
>>
>> On Fri, Nov 16, 2018 at 10:22 AM Yitzhak Mandelbaum <yitzhakm at google.com>
>> wrote:
>>
>>> Hi all,
>>>
>>> I have a proposal for a framework that makes it easier to write source
>>> to source transformations with the clang::Tooling libraries, including
>>> clang-tidy checks.
>>>
>>> The full proposal is in this doc:
>>>
>>>
>>> https://docs.google.com/document/d/1ppw0RhjwsrbBcHYhI85pe6ISDbA6r5d00ot3N8cQWeQ/edit?usp=sharing
>>>
>>> From the doc:
>>> Transformer is a framework that aims to simplify development of
>>> clang-based source-to-source transformations.  It focuses on the particular
>>> class of transformations that act only locally — that is, use local
>>> information about the code and make local changes  (like a syntax-aware
>>> “find-and-replace”); and at scale — that is, will be carried out on many
>>> source files.  The target audience is users that are comfortable with, or
>>> willing to become comfortable with, the clang AST matchers library.
>>>
>>> I have a working prototype of this library which I've used on small
>>> examples inside Google.  I plan to put together a patch for reference
>>> next week, although the doc should stand on its own.
>>>
>>> Thanks!
>>> Yitzhak Mandelbaum
>>>
>>
>> _______________________________________________
>> cfe-dev mailing listcfe-dev at lists.llvm.orghttp://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190213/e949b1d9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4847 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190213/e949b1d9/attachment.bin>


More information about the cfe-dev mailing list