[cfe-dev] -Wunused-private-field distracted by side effects

David Blaikie dblaikie at gmail.com
Wed Oct 15 08:17:46 PDT 2014


On Wed, Oct 15, 2014 at 6:41 AM, Stephan Bergmann <sbergman at redhat.com>
wrote:

> BaseAndFieldInfo::addFieldInitializer (lib/Sema/SemaDeclCXX.cpp) contains
>
>      // Check whether this initializer makes the field "used".
>>     if (Init->getInit()->HasSideEffects(S.Context))
>>       S.UnusedPrivateFields.remove(Init->getAnyMember());
>>
>
> which filters out of the list of unused private member variables those
> with initializers that potentially incur side effects.  That is, in
>
>  int f();
>> class C {
>>     int i, j;
>> public:
>>     C(): i(f()), j(1) {}
>> };
>>
>
> clang++ -Wall will emit a -Wunused-private-field for j but not for i.
>
> Is that behavior intended?  (Member variables of class type, where the
> constructor itself may have side effects, are already exempted from the
> UnusedPrivateFields list via the InitializationHasSideEffects() check
> elsewhere in lib/Sema/SemaDeclCXX.cpp.)
>
> Of course, such a warning cannot in general be addressed by naively
> removing the member variable along with any initializers, but that is no
> different to e.g. -Wunused-variable, which does get emitted for
>
>  int f();
>> void g() {
>>     int i(f());
>> }
>>
>
> One arcane legitimate reason to not emit a -Wunused-private-field could be
> a case where f() needs to be called for side effects between construction
> of two other members, as in
>
>  int f();
>> struct S1 { /*...*/ };
>> struct S2 { /*...*/ };
>> class C {
>>     S1 s1;
>>     int dummy;
>>     S2 s2;
>> public:
>>     C(): s1(), dummy(f()), s2() {}
>>
>
This could still be achieved assuming s2 was at least move constructible:

s2([]{f(); return S2(); }());


> };
>>
>
> but that could arguably be required to use a  __attribute__((unused)).
>

But that seems not unreasonable as well.


>
> Stephan
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141015/f0425d18/attachment.html>


More information about the cfe-dev mailing list