aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite
diff options
context:
space:
mode:
authorJason Thorpe <thorpej@wasabisystems.com>2002-05-23 15:48:05 +0000
committerJason Thorpe <thorpej@gcc.gnu.org>2002-05-23 15:48:05 +0000
commitb34c78814983ef7dcb1b6baacb335836c960ac15 (patch)
treeec1ff9d3e5c1ac88998d2453691cca3c42bc0416 /gcc/testsuite
parent9547983155087b81454e9149ad37c2945f8137b9 (diff)
downloadgcc-b34c78814983ef7dcb1b6baacb335836c960ac15.zip
gcc-b34c78814983ef7dcb1b6baacb335836c960ac15.tar.gz
gcc-b34c78814983ef7dcb1b6baacb335836c960ac15.tar.bz2
c-common.c (warn_nonnull): Declare.
* c-common.c (warn_nonnull): Declare. (c_common_attribute_table): Add "nonnull" attribute. (handle_nonnull_attribute, check_function_nonnull, nonnull_check_p, check_nonnull_arg, get_nonnull_operand, check_function_arguments, check_function_arguments_recurse): New functions. * c-common.h (warn_nonnull): Declare extern. (check_function_arguments, check_function_arguments_recurse): New prototypes. * c-decl.c (c_decode_option): Add -Wnonnull option. * c-format.c (set_Wformat): Set warn_nonnull if enabling format checking. (format_check_context): New structure. (check_format_info_recurse): Remove recursion and rename to... (check_format_arg): ...this. Update comment. (check_format_info): Use check_function_arguments_recurse. * c-typeck.c (build_function_call): Call check_function_arguments instead of check_function_format. * doc/extend.texi: Document "nonnull" attribute. * doc/invoke.texi: Docuemnt -Wnonnull option. * testsuite/gcc.dg/nonnull-1.c: New test. * testsuite/gcc.dg/nonnull-2.c: New test. From-SVN: r53790
Diffstat (limited to 'gcc/testsuite')
-rw-r--r--gcc/testsuite/gcc.dg/nonnull-1.c39
-rw-r--r--gcc/testsuite/gcc.dg/nonnull-2.c16
2 files changed, 55 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/nonnull-1.c b/gcc/testsuite/gcc.dg/nonnull-1.c
new file mode 100644
index 0000000..51a8a33
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/nonnull-1.c
@@ -0,0 +1,39 @@
+/* Test for the "nonnull" function attribute. */
+/* Origin: Jason Thorpe <thorpej@wasabisystems.com> */
+/* { dg-do compile } */
+/* { dg-options "-Wnonnull" } */
+
+#include <stddef.h>
+
+extern void func1 (char *, char *, int) __attribute__((nonnull));
+
+extern void func2 (char *, char *) __attribute__((nonnull(1)));
+
+extern void func3 (char *, int, char *, int)
+ __attribute__((nonnull(1,3)));
+
+extern void func4 (char *, char *) __attribute__((nonnull(1)))
+ __attribute__((nonnull(2)));
+
+void
+foo (int i1, int i2, int i3, char *cp1, char *cp2, char *cp3)
+{
+ func1(cp1, cp2, i1);
+
+ func1(NULL, cp2, i1); /* { dg-warning "null" "null with argless nonnull 1" } */
+ func1(cp1, NULL, i1); /* { dg-warning "null" "null with argless nonnull 2" } */
+ func1(cp1, cp2, 0);
+
+ func2(cp1, NULL);
+ func2(NULL, cp1); /* { dg-warning "null" "null with single explicit nonnull" } */
+
+ func3(NULL, i2, cp3, i3); /* { dg-warning "null" "null with explicit nonnull 1" } */
+ func3(cp3, i2, NULL, i3); /* { dg-warning "null" "null with explicit nonnull 3" } */
+
+ func1(i1 ? cp1 : NULL, cp2, i3); /* { dg-warning "null" "null with cond expr rhs" } */
+ func1(i1 ? NULL : cp1, cp2, i3); /* { dg-warning "null" "null with cond expr lhs" } */
+ func1(i1 ? (i2 ? cp1 : NULL) : cp2, cp3, i3); /* { dg-warning "null" "null with nested cond expr" } */
+
+ func4(NULL, cp1); /* { dg-warning "null" "null with multiple attributes 1" } */
+ func4(cp1, NULL); /* { dg-warning "null" "null with multiple attributes 2" } */
+}
diff --git a/gcc/testsuite/gcc.dg/nonnull-2.c b/gcc/testsuite/gcc.dg/nonnull-2.c
new file mode 100644
index 0000000..bd36d232
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/nonnull-2.c
@@ -0,0 +1,16 @@
+/* Test for the invalid use of the "nonnull" function attribute. */
+/* Origin: Jason Thorpe <thorpej@wasabisystems.com> */
+/* { dg-do compile } */
+
+extern void func1 () __attribute__((nonnull)); /* { dg-error "without arguments" } */
+
+extern void func2 (char *) __attribute__((nonnull(2))); /* { dg-error "out-of-range operand" } */
+
+extern void func3 (char *) __attribute__((nonnull(foo))); /* { dg-error "invalid operand number" } */
+
+extern void func4 (int) __attribute__((nonnull(1))); /* { dg-error "references non-pointer" } */
+
+void
+foo (void)
+{
+}