]> granicus.if.org Git - clang/commitdiff
Add the exception for strings in logical and expressions to -Wstring-conversion
authorRichard Trieu <rtrieu@google.com>
Wed, 19 Nov 2014 06:08:18 +0000 (06:08 +0000)
committerRichard Trieu <rtrieu@google.com>
Wed, 19 Nov 2014 06:08:18 +0000 (06:08 +0000)
for C code.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222327 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/Sema/warn-string-conversion.c [new file with mode: 0644]

index aa6bf1760d3da0210416cab60850ecd0855ae4d7..91ba91e4f5317a1c9e05b7383fb88da4ddb4b143 100644 (file)
@@ -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<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);
diff --git a/test/Sema/warn-string-conversion.c b/test/Sema/warn-string-conversion.c
new file mode 100644 (file)
index 0000000..708dd54
--- /dev/null
@@ -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}}
+}