reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
// RUN: %clang_cc1 -fsyntax-only -verify %s
// rdar://9584012
//
// Verify All warnings are still issued with the option -fno-delete-null-pointer-checks
// if nullptr is passed to function with nonnull attribute.
// RUN: %clang_cc1 -fsyntax-only -fno-delete-null-pointer-checks -verify %s

typedef struct {
	char *str;
} Class;

typedef union {
	Class *object;
} Instance __attribute__((transparent_union));

__attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
	this.object->str = str;
}

int main(void) {
	Class *obj;
	Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
	Class_init(obj, "Hello World");
}

void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
void bar(int i) __attribute__((nonnull(1))); // expected-warning {{'nonnull' attribute only applies to pointer arguments}} expected-warning {{'nonnull' attribute applied to function with no pointer arguments}}

void baz(__attribute__((nonnull)) const char *str);
void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}

void test_baz() {
  baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
  baz2(0); // no-warning
  baz3(0); // no-warning
}

void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning

int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to Objective-C methods and functions}}
void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning

__attribute__((returns_nonnull))
void *test_bad_returns_null(void) {
  return 0; // expected-warning {{null returned from function that requires a non-null return value}}
}

void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
  g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
}
void PR18795_helper() {
  PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
}

void vararg1(int n, ...) __attribute__((nonnull(2)));
void vararg1_test() {
  vararg1(0);
  vararg1(1, (void*)0); // expected-warning{{null passed}}
  vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
  vararg1(2, (void*)&vararg1, (void*)0);
}

void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
void vararg2_test() {
  vararg2(0);
  vararg2(1, (void*)0); // expected-warning{{null passed}}
  vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
}

void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
void vararg3_test() {
  vararg3(0);
  vararg3(1, (void*)0); // expected-warning{{null passed}}
  vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
}

void redecl(void *, void *);
void redecl(void *, void *) __attribute__((nonnull(1)));
void redecl(void *, void *) __attribute__((nonnull(2)));
void redecl(void *, void *);
void redecl_test(void *p) {
  redecl(p, 0); // expected-warning{{null passed}}
  redecl(0, p); // expected-warning{{null passed}}
}

// rdar://18712242
#define NULL (void*)0
__attribute__((__nonnull__))  // expected-note 2{{declared 'nonnull' here}}
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}}
     return 0;
   } else {
     return *pointer;
   } 

   pointer = pv;
   if (!pointer)
     return 0;
   else
     return *pointer;

   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)));  // expected-note 2{{declared 'nonnull' here}}
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}}
     return 0;
   } else {
     return *pointer;
   } 

   set_param_to_null(&pointer);
   if (!pointer)
     return 0;
   else
     return *pointer;

   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**);
extern void FOO();
extern void FEE();

extern void *pv;
__attribute__((__nonnull__))  // expected-note {{declared 'nonnull' here}}
void yet_another_evil_nonnull_func(int* pointer)
{
 while (pv) {
   // This comparison will not be optimized away.
   if (pointer) {  // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
     FOO();
   } else {
     FEE();
   } 
   pointer = returns_null(&pv);
 }
}

void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) { // expected-note {{declared 'nonnull' here}}
  if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
    ;
  if (s) // No warning
    ;
}

void pr21668_2(__attribute__((nonnull)) const char *p) {
  p = 0;
  if (p) // No warning
    ;
}

__attribute__((returns_nonnull)) void *returns_nonnull_whee();  // expected-note 6{{declared 'returns_nonnull' here}}

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}}
}

void pr30828(char *p __attribute__((nonnull)));
void pr30828(char *p) {}

void call_pr30828() {
  pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
}