[cfe-dev] [Clang-tidy] Detecting if a catch was done by const ref

Matthieu Brucher via cfe-dev cfe-dev at lists.llvm.org
Mon Mar 12 13:49:50 PDT 2018


2018-03-12 19:39 GMT+00:00 Aaron Ballman via cfe-dev <cfe-dev at lists.llvm.org
>:

> On Mon, Mar 12, 2018 at 3:34 PM, Richard via cfe-dev
> <cfe-dev at lists.llvm.org> wrote:
> > [Please reply *only* to the list and do not include my email directly
> > in the To: or Cc: of your reply; otherwise I will not see your reply.
> > Thanks.]
> >
> > In article <CAAt6xTvjSPEg7OVPWVbtqWdsDp5zja9_TfLTkdzoNoksmEr=KQ at mail.
> gmail.com>,
> >     Aaron Ballman via cfe-dev <cfe-dev at lists.llvm.org> writes:
> >
> >> I believe the matcher you're looking for is:
> >> varDecl(hasType(references(qualType(isConstQualified()))))
> >>
> >> For example, given:
> >>
> >> void f1() {
> >>   try {
> >>   } catch (int &e) {
> >>   }
> >> }
> >>
> >> void f2() {
> >>   try {
> >>   } catch (const int &e) {
> >>   }
> >> }
> >>
> >> produces:
> >>
> >> clang-query> match
> >> varDecl(hasType(references(qualType(isConstQualified()))))
> >>
> >> Match #1:
> >>
> >> C:\Users\Aaron\Desktop\test.cpp:9:12: note: "root" binds here
> >>   } catch (const int &e) {
> >>            ^~~~~~~~~~~~
> >> 1 match.
> >
> > I think they want to match the catches that aren't const qualified, so
> > it might be (untested):
> >
> > varDecl(hasType(references(qualType(not(isConstQualified())))))
>
> Ah, oops, I didn't see that I had the predicate reversed. Your matcher
> is close, but instead of not(), you use unless().
>
> ~Aaron
>
> > --
> > "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-
> pipeline>
> >             The Terminals Wiki <http://terminals-wiki.org>
> >      The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
> >   Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at lists.llvm.org
> > http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>

Excellent! I thought I had to use the catch statement, but this is indeed
far better.
I guess you have to know the AST matcher expressions... Are there tutorials
on top of the doxygen doc? Or perhaps even books?

Regards,

Matthieu
-- 
Quantitative analyst, Ph.D.
Blog: http://blog.audio-tk.com/
LinkedIn: http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180312/d1ce70d0/attachment.html>


More information about the cfe-dev mailing list