I delivered a talk about writing a refactoring tool with Clang Tooling at code::dive in November. It was uploaded to YouTube today:
The slides are available here and the code samples are here.
This was a fun talk to deliver as I got to demo some features which had never been seen by anyone before. For people who are already familiar with clang-tidy and clang-query, the interesting content starts about 15 minutes in. There I start to show new features in the clang-query interpreter command line.
The existing clang-query interpreter lacks many features which the replxx library provides, such as syntax highlighting and portable code completion:
It also allows scrolling through results to make a selection:
A really nice feature is value-based code-completion instead of type-based code completion. Existing code completion only completes candidates based on type-compatibility. It recognizes that a parameterCountIs() matcher can be used with a functionDecl() matcher for example. If the code completion already on the command line is sufficiently constrained so that there is only one result already, the code completion is able to complete candidates based on that one result node:
Another major problem with clang-query currently is that it is hard to know which parenthesis represents the closing of which matcher. The syntax highlighting of replxx help with this, along with a brace matching feature I wrote for it:
I’m working on upstreaming those features to replxx and Clang to make them available for everyone, but for now it is possible to experiment with some of the features on my Compiler Explorer instance on ce.steveire.com.
I wrote about the AST-Matcher and Source Location/Source Range discovery features on my blog here since delivering the talk. I also wrote about Composing AST Matchers, which was part of the tips and tricks section of the talk. Over on the Visual C++ blog, I wrote about distributing the refactoring task among computers on the network using Icecream. My blogs on that platform can be seen in the Clang category.
All of that blog content is repeated in the code::dive presentation, but some people prefer to learn from conference videos instead of blogs, so this might help the content reach a larger audience. Let me know if there is more you would like to see about clang-query!
January 4, 2019 at 2:27 am |
Your presentation material link is broken. You linked “steveire.com/CodeDive2018Presentation.pdf” but it opens “https://steveire.wordpress.com/2019/01/02/refactor-with-clang-tooling-at-codedive-2018/steveire.com/CodeDive2018Presentation.pdf” in chrome.
I already got pdf from “https://steveire.com/CodeDive2018Presentation.pdf”. I just want to let you know because this presentation is interesting.
January 4, 2019 at 10:28 pm |
Thanks, I fixed the link!
January 7, 2019 at 8:41 pm |
Steve, you mentioned in the talk (IIRC) a way to avoid matching against code that is generated by the compiler (like generated constructors/methods and range-fors). Can you expand on that? Have you augmented the AST to expose this property and/or added controls for how matchers work (which I remember us discussing on the bug for ignoreImplicit())?
January 7, 2019 at 9:44 pm |
@Yitzhak, yes, See the difference between my clang-query and the trunk clang-query here: http://ec2-18-191-7-3.us-east-2.compute.amazonaws.com:10240/z/sNny36
See here: https://youtu.be/VqCkCDFLSsc?t=1748
The implicit CXXRecordDecl representing the injected class name is only a cause of confusion, and is really not needed when using clang-query or clang-tidy because it has no source code representation. I will make sure to add you as a reviewer on that change.
I have a separate change to both clang-query and clang-tidy which optionally ignores all invisible nodes: http://ec2-18-191-7-3.us-east-2.compute.amazonaws.com:10240/z/uBshw1
Even adding ignoringImplicit() matchers in current clang trunk would not help you there because you would have to explicitly match the invisible conversion method call. There is really no need for that and it only causes confusion.
Hopefully these will get into clang!
January 11, 2019 at 4:13 pm
These are fantastic! Yes, please include me on that review.
January 7, 2019 at 9:58 pm |
[…] https://steveire.wordpress.com/2019/…codedive-2018/ […]
January 7, 2019 at 10:21 pm |
Impressive!
April 16, 2019 at 8:29 am |
[…] Dreamer, but not the only one. « Refactor with Clang Tooling at code::dive 2018 […]