This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: {PATCH] New C++ warning -Wcatch-value
- From: Volker Reichelt <v dot reichelt at netcologne dot de>
- To: Martin Sebor <msebor at gmail dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Mon, 5 Jun 2017 12:55:01 +0200 (CEST)
- Subject: Re: {PATCH] New C++ warning -Wcatch-value
- Authentication-results: sourceware.org; auth=none
- References: <tkrat.e779dc1a05db1c81@netcologne.de> <297400e4-5e33-08aa-8048-cffd3626740a@gmail.com> <tkrat.3adbc674905c42cf@netcologne.de> <c5a2c640-1ac3-ec0b-0215-e2c2d270b25d@gmail.com> <tkrat.8c7b4260a533be2f@netcologne.de> <a04c422d-4ae4-31e9-105e-872cf79e2fa3@gmail.com>
On 15 May, Martin Sebor wrote:
>> So how about the following then? I stayed with the catch part and added
>> a parameter to the warning to let the user decide on the warnings she/he
>> wants to get: -Wcatch-value=n.
>> -Wcatch-value=1 only warns for polymorphic classes that are caught by
>> value (to avoid slicing), -Wcatch-value=2 warns for all classes that
>> are caught by value (to avoid copies). And finally -Wcatch-value=3
>> warns for everything not caught by reference to find typos (like pointer
>> instead of reference) and bad coding practices.
>
> It seems reasonable to me. I'm not too fond of multi-level
> warnings since few users take advantage of anything but the
> default, but this case is simple and innocuous enough that
> I don't think it can do harm.
>
>>
>> Bootstrapped and regtested on x86_64-pc-linux-gnu.
>> OK for trunk?
>>
>> If so, would it make sense to add -Wcatch-value=1 to -Wextra or even -Wall?
>> I would do this in a seperate patch, becuase I haven't checked what that
>> would mean for the testsuite.
>
> I can't think of a use case for polymorphic slicing that's not
> harmful so unless there is a common one that escapes me, I'd say
> -Wall.
So that's what I committed after Jason's OK.
> What are your thoughts on enhancing the warning to also handle
> the rethrow case?
>
> Also, it seems that a similar warning would be useful even beyond
> catch handlers, to help detect slicing when passing arguments to
> functions by value. Especially in code that mixes OOP with the
> STL (or other template libraries). Have you thought about tackling
> that at some point as well?
I don't have any plans to handle handle the rethrow case.
A general slicing warning would be very nice to have. Actually
clang-tidy has one (which is a little buggy, though).
Implementing this is over my head, though.
I'd rather stick with something less ambitious.
> Martin
>
>>
>> Regards,
>> Volker