]> granicus.if.org Git - clang/commitdiff
Patch to warn when logical evaluation of operand evalutes to a true value;
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 11 Nov 2014 19:59:16 +0000 (19:59 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 11 Nov 2014 19:59:16 +0000 (19:59 +0000)
That this is a c-only patch. c++ already has this warning.
This addresses rdar://18716393

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

include/clang/Sema/Sema.h
lib/Sema/SemaChecking.cpp
lib/Sema/SemaExpr.cpp
test/Analysis/logical-ops.c
test/Sema/exprs.c
test/Sema/warn-tautological-compare.c [new file with mode: 0644]

index 4ad7463767d6bd2f7eb89f77df810011130f4794..aa039e3e47c0f00b70b7c191d3c3c569e627fe13 100644 (file)
@@ -8033,6 +8033,7 @@ public:
   void DiagnoseAlwaysNonNullPointer(Expr *E,
                                     Expr::NullPointerConstantKind NullType,
                                     bool IsEqual, SourceRange Range);
+  void CheckAlwaysNonNullPointer(Expr *OrigExp);
 
   /// type checking for vector binary operators.
   QualType CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
index ea4f3476b6ce85e73698ef0ac50404850d3d6cc1..fe47bfb12e9b2c44b1c117dd75cf46b8e6b297de 100644 (file)
@@ -6748,6 +6748,17 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
       << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()");
 }
 
+void Sema::CheckAlwaysNonNullPointer(Expr *OrigExpr) {
+  if (const UnaryOperator *U = dyn_cast<UnaryOperator>(OrigExpr))
+    if (U->getOpcode() == UO_LNot)
+      return CheckAlwaysNonNullPointer(U->getSubExpr());
+    
+  Expr *E = OrigExpr->IgnoreParenImpCasts();
+  QualType Source = E->getType();
+  if (Source->isPointerType() || Source->canDecayToPointerType())
+    DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
+                                 SourceRange());
+}
 
 /// Diagnoses "dangerous" implicit conversions within the given
 /// expression (which is a full expression).  Implements -Wconversion
index 37a08cfb0818a5f82b88f74ed9d0f768f0b38344..a8429b92ed6062a7149c91db122dea4dde468b2d 100644 (file)
@@ -8413,7 +8413,10 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
     if (!LHS.get()->getType()->isScalarType() ||
         !RHS.get()->getType()->isScalarType())
       return InvalidOperands(Loc, LHS, RHS);
-
+    
+    CheckAlwaysNonNullPointer(LHS.get());
+    CheckAlwaysNonNullPointer(RHS.get());
+    
     return Context.IntTy;
   }
 
@@ -12971,6 +12974,7 @@ ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) {
         << T << E->getSourceRange();
       return ExprError();
     }
+    CheckAlwaysNonNullPointer(E);
   }
 
   return E;
index afaa2f1077807767b7a115639e68cac285c24151..2a03d96c0cb75eb48b9db08a2bc536a79cb3f3ac 100644 (file)
@@ -36,4 +36,5 @@ int between(char *x) {
 int undef(void) {} // expected-warning{{control reaches end of non-void function}}
 void useUndef(void) { 0 || undef(); }
 
-void testPointer(void) { (void) (1 && testPointer && 0); }
+void testPointer(void) { (void) (1 && testPointer && 0); } // expected-warning {{address of function 'testPointer' will always evaluate to 'true'}} \
+                                                          // expected-note {{prefix with the address-of operator to silence this warning}}
index 2fb17e4880c645edbc9c6243eacc67ea062ba7f1..17b1aa2851b067cd47c7fbae7a68ff7dda4cf216 100644 (file)
@@ -244,6 +244,10 @@ void test22() {
   if ("help")
     (void) 0;
 
-  if (test22)
+  if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \
+             // expected-note {{prefix with the address-of operator to silence this warning}}
+    (void) 0;
+
+  if (&test22)
     (void) 0;
 }
diff --git a/test/Sema/warn-tautological-compare.c b/test/Sema/warn-tautological-compare.c
new file mode 100644 (file)
index 0000000..2856edd
--- /dev/null
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify  %s
+// rdar://18716393
+
+extern int a[] __attribute__((weak));
+int b[] = {8,13,21};
+struct {
+  int x[10];
+} c;
+const char str[] = "text";
+
+void ignore() {
+  if (!a) {}
+}
+void test() {
+  if (!b) {} // expected-warning {{address of array 'b' will always evaluate to 'true'}}
+  if (b == 0) {} // expected-warning {{comparison of array 'b' equal to a null pointer is always false}}
+  if (!c.x) {} // expected-warning {{address of array 'c.x' will always evaluate to 'true'}}
+  if (c.x == 0) {} // expected-warning {{comparison of array 'c.x' equal to a null pointer is always false}}
+  if (!str) {} // expected-warning {{address of array 'str' will always evaluate to 'true'}}
+  if (0 == str) {} // expected-warning {{comparison of array 'str' equal to a null pointer is always false}}
+}
+
+int array[2];
+int test1()
+{
+  if (!array) { // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+    return array[0];
+  } else if (array != 0) { // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
+    return array[1];
+  }
+  if (array == 0) // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
+    return 1;
+  return 0;
+}
+
+#define NULL (void*)0
+
+int test2(int* pointer, char ch, void * pv) {
+   if (!&pointer) {  // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
+     return 0;
+   }
+
+   if (&pointer) {  // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
+     return 0;
+   }
+
+   if (&pointer == NULL) {} // expected-warning {{comparison of address of 'pointer' equal to a null pointer is always false}}
+
+   if (&pointer != NULL) {} // expected-warning {{comparison of address of 'pointer' not equal to a null pointer is always true}}
+
+   return 1;
+}
+
+void test3() {
+   if (array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+   if (array != 0) {} // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
+   if (!array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+   if (array == 0) {} // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
+
+   if (array[0] &&
+       array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+   if (array[0] ||
+       array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+   if (array[0] &&
+       !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+   if (array[0] ||
+       !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+   if (array && // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+       array[0]) {}
+   if (!array || // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+       array[0]) {}
+
+   if (array ||  // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+       (!array && array[0])) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ }
+
+