From: Richard Trieu Date: Wed, 19 Nov 2014 06:08:18 +0000 (+0000) Subject: Add the exception for strings in logical and expressions to -Wstring-conversion X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b4adcf5e119cfd1e20040ee974c598a435a7c17f;p=clang Add the exception for strings in logical and expressions to -Wstring-conversion for C code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222327 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index aa6bf1760d..91ba91e4f5 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -6613,10 +6613,17 @@ void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { continue; AnalyzeImplicitConversions(S, ChildExpr, CC); } + if (BO && BO->isLogicalOp()) { - ::CheckBoolLikeConversion(S, BO->getLHS(), BO->getLHS()->getExprLoc()); - ::CheckBoolLikeConversion(S, BO->getRHS(), BO->getRHS()->getExprLoc()); + Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); + if (!IsLogicalAndOperator || !isa(SubExpr)) + ::CheckBoolLikeConversion(S, SubExpr, SubExpr->getExprLoc()); + + SubExpr = BO->getRHS()->IgnoreParenImpCasts(); + if (!IsLogicalAndOperator || !isa(SubExpr)) + ::CheckBoolLikeConversion(S, SubExpr, SubExpr->getExprLoc()); } + if (const UnaryOperator *U = dyn_cast(E)) if (U->getOpcode() == UO_LNot) ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); diff --git a/test/Sema/warn-string-conversion.c b/test/Sema/warn-string-conversion.c new file mode 100644 index 0000000000..708dd543e4 --- /dev/null +++ b/test/Sema/warn-string-conversion.c @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -verify -fsyntax-only -Wstring-conversion %s + +#define assert(EXPR) (void)(EXPR); + +// Expection for common assert form. +void test1() { + assert(0 && "foo"); + assert("foo" && 0); + assert(0 || "foo"); // expected-warning {{string literal}} +} + +void test2() { + if ("hi") {} // expected-warning {{string literal}} + while ("hello") {} // expected-warning {{string literal}} + for (;"howdy";) {} // expected-warning {{string literal}} + do { } while ("hey"); // expected-warning {{string literal}} +}