r226870 - Make the ?: precedence warning handle pointers to the left of ?

David Blaikie dblaikie at gmail.com
Thu Jan 22 14:43:24 PST 2015


On Thu, Jan 22, 2015 at 2:11 PM, Hans Wennborg <hans at hanshq.net> wrote:

> Author: hans
> Date: Thu Jan 22 16:11:56 2015
> New Revision: 226870
>
> URL: http://llvm.org/viewvc/llvm-project?rev=226870&view=rev
> Log:
> Make the ?: precedence warning handle pointers to the left of ?
>
> Previously, Clang would fail to warn on:
>
>   int n = x + foo ? 1 : 2;
>
> when foo is a pointer.
>
> Modified:
>     cfe/trunk/lib/Sema/SemaExpr.cpp
>     cfe/trunk/test/Sema/parentheses.c
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=226870&r1=226869&r2=226870&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Jan 22 16:11:56 2015
> @@ -6129,6 +6129,8 @@ static bool ExprLooksBoolean(Expr *E) {
>      return IsLogicOp(OP->getOpcode());
>    if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E))
>      return OP->getOpcode() == UO_LNot;
> +  if (E->getType()->isPointerType())
>

Could we generalize this a bit further, somehow? (I haven't looked at the
code in question, but it sounds like this should use some more general tool
of "try to apply contextual conversion to bool" so that it matches the
actual semantic situation we're interested in here)


> +    return true;
>
>    return false;
>  }
>
> Modified: cfe/trunk/test/Sema/parentheses.c
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/parentheses.c?rev=226870&r1=226869&r2=226870&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Sema/parentheses.c (original)
> +++ cfe/trunk/test/Sema/parentheses.c Thu Jan 22 16:11:56 2015
> @@ -80,7 +80,7 @@ void bitwise_rel(unsigned i) {
>
>  _Bool someConditionFunc();
>
> -void conditional_op(int x, int y, _Bool b) {
> +void conditional_op(int x, int y, _Bool b, void* p) {
>    (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator
> '?:' has lower precedence than '+'}} \
>                                             // expected-note {{place
> parentheses around the '+' expression to silence this warning}} \
>                                             // expected-note {{place
> parentheses around the '?:' expression to evaluate it first}}
> @@ -116,6 +116,14 @@ void conditional_op(int x, int y, _Bool
>    // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")"
>
>    (void)(x % 2 ? 1 : 2); // no warning
> +
> +  (void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower
> precedence than '+'}} expected-note 2{{place parentheses}}
> +  (void)(p + x ? 1 : 2); // no warning
> +
> +  (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower
> precedence than '+'}} expected-note 2{{place parentheses}}
> +
> +  (void)(x + y > 0 ? 1 : 2); // no warning
> +  (void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has
> lower precedence than '+'}} expected-note 2{{place parentheses}}
>  }
>
>  // RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror
> -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150122/0b8965cd/attachment.html>


More information about the cfe-commits mailing list