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

Aaron Ballman via cfe-dev cfe-dev at lists.llvm.org
Mon Mar 12 13:56:29 PDT 2018


On Mon, Mar 12, 2018 at 4:49 PM, Matthieu Brucher via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
>
> 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?

There aren't any books that I'm aware of, and I'm not certain about
other tutorials (perhaps they exist and I've simply not found them). I
mostly go off the AST dumps (clang -cc1 -ast-dump foo.cpp), the AST
matchers reference, clang-query, and trial & error.

~Aaron

>
> Regards,
>
> Matthieu
> --
> Quantitative analyst, Ph.D.
> Blog: http://blog.audio-tk.com/
> LinkedIn: http://www.linkedin.com/in/matthieubrucher
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>



More information about the cfe-dev mailing list