r222327 - Add the exception for strings in logical and expressions to -Wstring-conversion

Richard Trieu rtrieu at google.com
Tue Nov 18 22:08:18 PST 2014


Author: rtrieu
Date: Wed Nov 19 00:08:18 2014
New Revision: 222327

URL: http://llvm.org/viewvc/llvm-project?rev=222327&view=rev
Log:
Add the exception for strings in logical and expressions to -Wstring-conversion
for C code.

Added:
    cfe/trunk/test/Sema/warn-string-conversion.c
Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=222327&r1=222326&r2=222327&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Nov 19 00:08:18 2014
@@ -6613,10 +6613,17 @@ void AnalyzeImplicitConversions(Sema &S,
       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<StringLiteral>(SubExpr))
+      ::CheckBoolLikeConversion(S, SubExpr, SubExpr->getExprLoc());
+
+    SubExpr = BO->getRHS()->IgnoreParenImpCasts();
+    if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
+      ::CheckBoolLikeConversion(S, SubExpr, SubExpr->getExprLoc());
   }
+
   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E))
     if (U->getOpcode() == UO_LNot)
       ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);

Added: cfe/trunk/test/Sema/warn-string-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-string-conversion.c?rev=222327&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-string-conversion.c (added)
+++ cfe/trunk/test/Sema/warn-string-conversion.c Wed Nov 19 00:08:18 2014
@@ -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}}
+}





More information about the cfe-commits mailing list