[PATCH] Warning on unevaluated expression contexts with side effects

Aaron Ballman aaron at aaronballman.com
Wed Nov 26 06:52:35 PST 2014


On Tue, Nov 25, 2014 at 9:59 PM, Richard Trieu <rtrieu at google.com> wrote:
> After running some code through a Clang with this patch, a few issues came
> up:

Thank you!

>
> 1) This warning should likely be disabled in macros.  Some macros will
> perform a sizeof on the macro argument which will trigger this warning even
> if argument is used elsewhere in the macro expansion.

Can you point me to an example of this? It seems like a reasonable
idea, but I'd like to better understand the code patterns before I
make the changes.

>
> 2) This warning is counting a dereference of a volatile pointer as a side
> effect, for instance:
>   int* volatile x;
>   return sizeof(*x);

Good catch!

>
> 3) This hits an llvm_unreachable:
>
> template <typename T> void F(T t) {}
> template <typename T> void G(T t) { decltype(&F<T>) x; }
> template <typename T> class V {};
> void foo() { G(V<int>()); }
>
> shouldn't see dependent / unresolved nodes here
> UNREACHABLE executed at ../tools/clang/lib/AST/Expr.cpp:2891!

Good catch, thanks!

~Aaron
>
>
> On Tue, Nov 25, 2014 at 6:51 AM, Aaron Ballman <aaron at aaronballman.com>
> wrote:
>>
>> The attached patch adds a -Wunused-value warning when an expression
>> with side effects is used in an unevaluated expression context, such
>> as sizeof(), or decltype(). It mostly reuses the logic from
>> Expr::HasSideEffects, except with a flag that treats certain
>> expressions as not having side effects -- mostly
>> instantiation-dependent contexts, and function call-like operations.
>>
>> int f();
>> int j = 0;
>>
>> (void)sizeof(f()); // Ok
>> (void)sizeof(++j); // Warn
>> (void)sizeof(int[++j]); // Ok
>>
>> I've added support for: sizeof, typeid, _Generic, _Alignof, noexcept,
>> and decltype.
>>
>> ~Aaron
>
>



More information about the cfe-commits mailing list