nonnull.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. // rdar://9584012
  3. typedef struct {
  4. char *str;
  5. } Class;
  6. typedef union {
  7. Class *object;
  8. } Instance __attribute__((transparent_union));
  9. __attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
  10. this.object->str = str;
  11. }
  12. int main(void) {
  13. Class *obj;
  14. Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
  15. Class_init(obj, "Hello World");
  16. }
  17. void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
  18. 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}}
  19. void baz(__attribute__((nonnull)) const char *str);
  20. void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
  21. void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
  22. void test_baz() {
  23. baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
  24. baz2(0); // no-warning
  25. baz3(0); // no-warning
  26. }
  27. void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
  28. int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
  29. void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
  30. int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
  31. int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}}
  32. void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
  33. void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning
  34. __attribute__((returns_nonnull))
  35. void *test_bad_returns_null(void) {
  36. return 0; // expected-warning {{null returned from function that requires a non-null return value}}
  37. }
  38. void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
  39. g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
  40. }
  41. void PR18795_helper() {
  42. PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
  43. }
  44. void vararg1(int n, ...) __attribute__((nonnull(2)));
  45. void vararg1_test() {
  46. vararg1(0);
  47. vararg1(1, (void*)0); // expected-warning{{null passed}}
  48. vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
  49. vararg1(2, (void*)&vararg1, (void*)0);
  50. }
  51. void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
  52. void vararg2_test() {
  53. vararg2(0);
  54. vararg2(1, (void*)0); // expected-warning{{null passed}}
  55. vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
  56. }
  57. void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
  58. void vararg3_test() {
  59. vararg3(0);
  60. vararg3(1, (void*)0); // expected-warning{{null passed}}
  61. vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
  62. }
  63. void redecl(void *, void *);
  64. void redecl(void *, void *) __attribute__((nonnull(1)));
  65. void redecl(void *, void *) __attribute__((nonnull(2)));
  66. void redecl(void *, void *);
  67. void redecl_test(void *p) {
  68. redecl(p, 0); // expected-warning{{null passed}}
  69. redecl(0, p); // expected-warning{{null passed}}
  70. }
  71. // rdar://18712242
  72. #define NULL (void*)0
  73. __attribute__((__nonnull__))
  74. int evil_nonnull_func(int* pointer, void * pv)
  75. {
  76. if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}}
  77. return 0;
  78. } else {
  79. return *pointer;
  80. }
  81. pointer = pv;
  82. if (!pointer)
  83. return 0;
  84. else
  85. return *pointer;
  86. if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}}
  87. }
  88. void set_param_to_null(int**);
  89. int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3)));
  90. int another_evil_nonnull_func(int* pointer, char ch, void * pv) {
  91. if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}}
  92. return 0;
  93. } else {
  94. return *pointer;
  95. }
  96. set_param_to_null(&pointer);
  97. if (!pointer)
  98. return 0;
  99. else
  100. return *pointer;
  101. if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}}
  102. }
  103. extern void *returns_null(void**);
  104. extern void FOO();
  105. extern void FEE();
  106. extern void *pv;
  107. __attribute__((__nonnull__))
  108. void yet_another_evil_nonnull_func(int* pointer)
  109. {
  110. while (pv) {
  111. // This comparison will not be optimized away.
  112. if (pointer) { // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
  113. FOO();
  114. } else {
  115. FEE();
  116. }
  117. pointer = returns_null(&pv);
  118. }
  119. }
  120. void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) {
  121. if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
  122. ;
  123. if (s) // No warning
  124. ;
  125. }
  126. void pr21668_2(__attribute__((nonnull)) const char *p) {
  127. p = 0;
  128. if (p) // No warning
  129. ;
  130. }