r200056 - Broaden -Wstring-conversion to catch string literals in logical or expressions.

Richard Trieu rtrieu at google.com
Fri Jan 24 17:10:35 PST 2014


Author: rtrieu
Date: Fri Jan 24 19:10:35 2014
New Revision: 200056

URL: http://llvm.org/viewvc/llvm-project?rev=200056&view=rev
Log:
Broaden -Wstring-conversion to catch string literals in logical or expressions.
Previously, string literals were ignored in all logical expressions.  This
reduces it to only ignore in logical and expressions.

assert(0 && "error"); // No warning
assert(0 || "error"); // Warn

Fixes PR17565

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

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=200056&r1=200055&r2=200056&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Jan 24 19:10:35 2014
@@ -5346,8 +5346,8 @@ void CheckImplicitConversion(Sema &S, Ex
   if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
     if (isa<StringLiteral>(E))
       // Warn on string literal to bool.  Checks for string literals in logical
-      // expressions, for instances, assert(0 && "error here"), are prevented
-      // by a check in AnalyzeImplicitConversions().
+      // and expressions, for instance, assert(0 && "error here"), are
+      // prevented by a check in AnalyzeImplicitConversions().
       return DiagnoseImpCast(S, E, T, CC,
                              diag::warn_impcast_string_literal_to_bool);
     if (Source->isFunctionType()) {
@@ -5698,15 +5698,16 @@ void AnalyzeImplicitConversions(Sema &S,
   // Now just recurse over the expression's children.
   CC = E->getExprLoc();
   BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
-  bool IsLogicalOperator = BO && BO->isLogicalOp();
+  bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
   for (Stmt::child_range I = E->children(); I; ++I) {
     Expr *ChildExpr = dyn_cast_or_null<Expr>(*I);
     if (!ChildExpr)
       continue;
 
-    if (IsLogicalOperator &&
+    if (IsLogicalAndOperator &&
         isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
-      // Ignore checking string literals that are in logical operators.
+      // Ignore checking string literals that are in logical and operators.
+      // This is a common pattern for asserts.
       continue;
     AnalyzeImplicitConversions(S, ChildExpr, CC);
   }

Modified: cfe/trunk/test/SemaCXX/warn-string-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-string-conversion.cpp?rev=200056&r1=200055&r2=200056&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-string-conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-string-conversion.cpp Fri Jan 24 19:10:35 2014
@@ -1,14 +1,20 @@
 // RUN: %clang_cc1 -fsyntax-only -Wstring-conversion -verify %s
 
 // Warn on cases where a string literal is converted into a bool.
-// An exception is made for this in logical operators.
+// An exception is made for this in logical and operators.
 void assert(bool condition);
 void test0() {
   bool b0 = "hi"; // expected-warning{{implicit conversion turns string literal into bool: 'const char [3]' to 'bool'}}
   b0 = ""; // expected-warning{{implicit conversion turns string literal into bool: 'const char [1]' to 'bool'}}
+  b0 = 0 || ""; // expected-warning{{implicit conversion turns string literal into bool: 'const char [1]' to 'bool'}}
+  b0 = "" || 0; // expected-warning{{implicit conversion turns string literal into bool: 'const char [1]' to 'bool'}}
   b0 = 0 && "";
+  b0 = "" && 0;
   assert("error"); // expected-warning{{implicit conversion turns string literal into bool: 'const char [6]' to 'bool'}}
+  assert(0 || "error"); // expected-warning{{implicit conversion turns string literal into bool: 'const char [6]' to 'bool'}}
+  assert("error" || 0); // expected-warning{{implicit conversion turns string literal into bool: 'const char [6]' to 'bool'}}
   assert(0 && "error");
+  assert("error" && 0);
 
   while("hi") {} // expected-warning{{implicit conversion turns string literal into bool: 'const char [3]' to 'bool'}}
   do {} while("hi"); // expected-warning{{implicit conversion turns string literal into bool: 'const char [3]' to 'bool'}}





More information about the cfe-commits mailing list