From 543c2ee4e169a0192650da497b414e63c48981ed Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Tue, 8 Dec 2015 22:02:00 +0000 Subject: [Sema] Add warning when comparing nonnull and null Currently, we emit warnings in some cases where nonnull function parameters are compared against null. This patch extends this support to warn when comparing the result of `returns_nonnull` functions against null. More specifically, we will now warn cases like: int *foo() __attribute__((returns_nonnull)); int main() { if (foo() == NULL) {} // warning: will always evaluate to false } Differential Revision: http://reviews.llvm.org/D15324 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@255058 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Sema/nonnull.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'test/Sema') diff --git a/test/Sema/nonnull.c b/test/Sema/nonnull.c index 4b3df8518c..9503e7c32a 100644 --- a/test/Sema/nonnull.c +++ b/test/Sema/nonnull.c @@ -89,7 +89,7 @@ void redecl_test(void *p) { __attribute__((__nonnull__)) int evil_nonnull_func(int* pointer, void * pv) { - if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}} return 0; } else { return *pointer; @@ -101,13 +101,13 @@ int evil_nonnull_func(int* pointer, void * pv) else return *pointer; - if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} } void set_param_to_null(int**); int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3))); int another_evil_nonnull_func(int* pointer, char ch, void * pv) { - if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}} return 0; } else { return *pointer; @@ -119,7 +119,7 @@ int another_evil_nonnull_func(int* pointer, char ch, void * pv) { else return *pointer; - if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} } extern void *returns_null(void**); @@ -153,3 +153,17 @@ void pr21668_2(__attribute__((nonnull)) const char *p) { if (p) // No warning ; } + +__attribute__((returns_nonnull)) void *returns_nonnull_whee(); + +void returns_nonnull_warning_tests() { + if (returns_nonnull_whee() == NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' equal to a null pointer is 'false' on first encounter}} + + if (returns_nonnull_whee() != NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' not equal to a null pointer is 'true' on first encounter}} + + if (returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + if (!returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + + int and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} +} -- cgit v1.2.1