[PATCH] Warning on unevaluated expression contexts with side effects

Richard Trieu rtrieu at google.com
Wed Nov 26 14:30:03 PST 2014


On Wed, Nov 26, 2014 at 6:52 AM, Aaron Ballman <aaron at aaronballman.com>
wrote:

> 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.
>

Something like:

int check_small_size(int x);
long check_large_size(long x);

#define bar(x) \
    if (sizeof(x) < 4) \
      check_small_size((x)); \
    else \
      check_large_size((x));

void foo (const char * str) {
  int x = 0;
  bar(str[x++]);
}


> >
> > 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
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141126/fe778731/attachment.html>


More information about the cfe-commits mailing list