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

Matthieu Brucher via cfe-dev cfe-dev at lists.llvm.org
Fri Mar 16 14:44:50 PDT 2018


2018-03-16 19:59 GMT+00:00 Aaron Ballman <aaron at aaronballman.com>:

> On Fri, Mar 16, 2018 at 3:40 PM, Matthieu Brucher via cfe-dev
> <cfe-dev at lists.llvm.org> wrote:
> >
> >
> > Le 12 mars 2018 17:59, "Aaron Ballman via cfe-dev" <
> cfe-dev at lists.llvm.org>
> > a écrit :
> >
> > 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.
> >
> > ~Aaron
> >
> > On Mon, Mar 12, 2018 at 2:12 AM, 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
> >> <CAHCaCkKdVLA-Mvg6i8BLAURRAeFfq5_GSGc6NmY3bEjRPpXq3Q at mail.gmail.com>,
> >>     Matthieu Brucher via cfe-dev <cfe-dev at lists.llvm.org> writes:
> >>
> >>> 2018-03-11 21:26 GMT+00:00 Richard via cfe-dev <cfe-dev at lists.llvm.org
> >:
> >>>
> >>> > Have you tried prototyping with clang-query?
> >>> >
> >>>
> >>> I know how to use it to create the query, but that's it. And even
> then, I
> >>> can only do basic query, as I don't know where to find documentation on
> >>> the
> >>> specifics.
> >>> It's possible to use for "dematching"? The ideal would be to select all
> >>> catch statements that are not const&, but there are no example anywhere
> >>> on
> >>> that kind of selection.
> >>
> >> I confess that I find the type related matchers confusing and I'm
> >> never quite sure how to use them.  If you look at the matcher
> >> reference <http://clang.llvm.org/docs/LibASTMatchersReference.html>
> >> you'll see a matcher for isExceptionVariable which is how you match
> >> variable declarations in a catch statement.  (Maybe the matcher name
> >> here isn't the best.)
> >>
> >>         c:\tmp
> >>         > copy con: tmp.cpp
> >>         #include <stdexcept>
> >>
> >>         void f()
> >>         {
> >>           try {
> >>                 g();
> >>           } catch (std::exception &e) {
> >>                 throw;
> >>           }
> >>         }
> >>         ^Z
> >>                         1 file(s) copied.
> >>
> >>         c:\tmp
> >>         > clang-query tmp.cpp --
> >>         clang-query> match varDecl(isExceptionVariable())
> >>
> >>         Match #1:
> >>
> >>         c:\tmp\tmp.cpp:9:12: note: "root" binds here
> >>           } catch (std::exception &e) {
> >>                            ^~~~~~~~~~~~~~~~~
> >>         1 match.
> >>         clang-query>
> >>
> >> The part that always confuses me about the matchers is how to modify
> >> matchers that find statements or declarations to qualify the matches
> >> by aspects of the associated type.
> >>
> >> Here we want to narrow the varDecl() so that the type of the variable
> >> is reference but not reference to const.  Frankly, I have never
> >> figured out how to do that in clang-query and I'd love for someone to
> >> enlighten me :)
> >
> >
> > Seems like this query also matches function arguments. I suppose I have
> to
> > use a condition saying that the previous or parent statement is catch?
>
> It will match all variable declarations. For just catch arguments, I
> think you will want:
>
> varDecl(isExceptionVariable(),
> hasType(references(qualType(isConstQualified()))))
>

I really have to look at the ast_matchers functions (because I just noticed
that this is where these are!).

Thanks a lot!

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/20180316/cb617a46/attachment.html>


More information about the cfe-dev mailing list