[PATCH] D149000: Update with warning message for comparison to NULL pointer

Krishna Narayanan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 25 11:32:47 PDT 2023


Krishna-13-cyber updated this revision to Diff 516857.
Krishna-13-cyber added a comment.

- Updated with reviewed changes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149000/new/

https://reviews.llvm.org/D149000

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/conditional-expr.c
  clang/test/Sema/warn-tautological-compare.c


Index: clang/test/Sema/warn-tautological-compare.c
===================================================================
--- clang/test/Sema/warn-tautological-compare.c
+++ clang/test/Sema/warn-tautological-compare.c
@@ -93,3 +93,9 @@
   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}}
+}
Index: clang/test/Sema/conditional-expr.c
===================================================================
--- clang/test/Sema/conditional-expr.c
+++ clang/test/Sema/conditional-expr.c
@@ -86,7 +86,7 @@
 
 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 {{comparison of address of 'x' not equal to a null pointer is always true}}
 }
 
 #define nil ((void*) 0)
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -14723,7 +14723,7 @@
 
   bool IsAddressOf = false;
 
-  if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
+  if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) {
     if (UO->getOpcode() != UO_AddrOf)
       return;
     IsAddressOf = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149000.516857.patch
Type: text/x-patch
Size: 1660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230425/8ad63b62/attachment-0001.bin>


More information about the cfe-commits mailing list