[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 12:40:05 PDT 2018


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?

Matthieu


> --
> "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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180316/98d9d5bc/attachment.html>


More information about the cfe-dev mailing list