Update with warning message for comparison to NULL pointer
authorKrishna Narayanan <krishnanarayanan132002@gmail.com>
Wed, 14 Jun 2023 12:28:35 +0000 (08:28 -0400)
committerAaron Ballman <aaron@aaronballman.com>
Wed, 14 Jun 2023 12:28:35 +0000 (08:28 -0400)
The tautological comparison warning was not properly looking through
parenthesized expressions, which is now fixed.

Fixes https://github.com/llvm/llvm-project/issues/42992
Differential Revision: https://reviews.llvm.org/D149000

clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/conditional-expr.c
clang/test/Sema/warn-tautological-compare.c

index 6bfd77c..2960183 100644 (file)
@@ -340,6 +340,8 @@ Improvements to Clang's diagnostics
   can be controlled using ``-fcaret-diagnostics-max-lines=``.
 - Clang no longer emits ``-Wunused-variable`` warnings for variables declared
   with ``__attribute__((cleanup(...)))`` to match GCC's behavior.
+- Clang now issues expected warnings for situations of comparing with NULL pointers.
+  (`#42992: <https://github.com/llvm/llvm-project/issues/42992>`_)
 
 Bug Fixes in This Version
 -------------------------
index 96f8d49..5522013 100644 (file)
@@ -14844,7 +14844,7 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
 
   bool IsAddressOf = false;
 
-  if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
+  if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
     if (UO->getOpcode() != UO_AddrOf)
       return;
     IsAddressOf = true;
index 8199745..b54b689 100644 (file)
@@ -86,7 +86,8 @@ void foo(void) {
 
 int Postgresql(void) {
   char x;
-  return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); // expected-warning {{C99 forbids conditional expressions with only one void side}}
+  return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); /* expected-warning {{C99 forbids conditional expressions with only one void side}}
+                                                                                                                 expected-warning {{comparison of address of 'x' not equal to a null pointer is always true}} */
 }
 
 #define nil ((void*) 0)
index 88e0f98..dd41f04 100644 (file)
@@ -93,3 +93,8 @@ void test_conditional_operator(void) {
   x = array ? 1 : 0; // expected-warning {{address of array}}
   x = &x ? 1 : 0;    // expected-warning {{address of 'x'}}
 }
+
+void test4(void) {
+  int *a = (void *) 0;
+  int b = (&a) == ((void *) 0); // expected-warning {{comparison of address of 'a' equal to a null pointer is always false}}
+}