[clang] 20e81a8 - Update with warning message for comparison to NULL pointer
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 14 05:33:59 PDT 2023
Author: Krishna Narayanan
Date: 2023-06-14T08:28:35-04:00
New Revision: 20e81a80e9cb6403f850ab981438f3d9d88b5a4b
URL: https://github.com/llvm/llvm-project/commit/20e81a80e9cb6403f850ab981438f3d9d88b5a4b
DIFF: https://github.com/llvm/llvm-project/commit/20e81a80e9cb6403f850ab981438f3d9d88b5a4b.diff
LOG: Update with warning message for comparison to NULL pointer
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
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/conditional-expr.c
clang/test/Sema/warn-tautological-compare.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6bfd77cc91028..2960183eb8c5f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
-------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 96f8d49b80f2c..55220138f94f5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -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;
diff --git a/clang/test/Sema/conditional-expr.c b/clang/test/Sema/conditional-expr.c
index 81997459c1f20..b54b689ec4f05 100644
--- a/clang/test/Sema/conditional-expr.c
+++ b/clang/test/Sema/conditional-expr.c
@@ -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)
diff --git a/clang/test/Sema/warn-tautological-compare.c b/clang/test/Sema/warn-tautological-compare.c
index 88e0f98b48ea4..dd41f04036391 100644
--- a/clang/test/Sema/warn-tautological-compare.c
+++ b/clang/test/Sema/warn-tautological-compare.c
@@ -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}}
+}
More information about the cfe-commits
mailing list