aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2019-11-15 18:39:35 +0000
committerJoseph Myers <jsm28@gcc.gnu.org>2019-11-15 18:39:35 +0000
commit97cc11871e39888bfca4ed4bf09fa09fe0916266 (patch)
treec0b8023fcc02ba1d811307c9478303e4dd99badc
parenta91eb2341f6a0ae5700e0f09ccb8b561f35cb0f8 (diff)
downloadgcc-97cc11871e39888bfca4ed4bf09fa09fe0916266.zip
gcc-97cc11871e39888bfca4ed4bf09fa09fe0916266.tar.gz
gcc-97cc11871e39888bfca4ed4bf09fa09fe0916266.tar.bz2
Support C2x [[maybe_unused]] attribute.
This patch adds support for the C2x [[maybe_unused]] attribute, using the same handler as for GNU __attribute__ ((unused)). As with other such attribute support, I think turning certain warnings into pedwarns for usage in cases where that is a constraint violation can be addressed later as a bug fix, as can the C2x constraint for various standard attributes that they do not appear more than once inside a single [[]]. However, the warnings that appear in c2x-attr-maybe_unused-1.c (that the attribute is ignored on member declarations) need to remain as warnings not pedwarns, since C2x does permit the attribute there. (Or they could be silenced, on the basis that GCC doesn't have warnings for unused struct and union members so it's completely harmless that it's ignoring an attribute that might do something useful with another compiler that does have such warnings.) Bootstrapped with no regressions on x86_64-pc-linux-gnu. gcc/c: * c-decl.c (std_attribute_table): Add maybe_unused. gcc/testsuite: * gcc.dg/c2x-attr-maybe_unused-1.c, gcc.dg/c2x-attr-maybe_unused-2.c, gcc.dg/c2x-attr-maybe_unused-3.c: New tests. From-SVN: r278310
-rw-r--r--gcc/c/ChangeLog4
-rw-r--r--gcc/c/c-decl.c2
-rw-r--r--gcc/testsuite/ChangeLog6
-rw-r--r--gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-1.c31
-rw-r--r--gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c25
-rw-r--r--gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-3.c11
6 files changed, 79 insertions, 0 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index c2cab57..75a44e2 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,5 +1,9 @@
2019-11-15 Joseph Myers <joseph@codesourcery.com>
+ * c-decl.c (std_attribute_table): Add maybe_unused.
+
+2019-11-15 Joseph Myers <joseph@codesourcery.com>
+
* c-decl.c (std_attribute_table): Add fallthrough.
* c-parser.c (c_parser_declaration_or_fndef): Diagnose fallthrough
attribute at top level.
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 98b71ea..cb6169c 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -4345,6 +4345,8 @@ const struct attribute_spec std_attribute_table[] =
handle_deprecated_attribute, NULL },
{ "fallthrough", 0, 0, false, false, false, false,
handle_fallthrough_attribute, NULL },
+ { "maybe_unused", 0, 0, false, false, false, false,
+ handle_unused_attribute, NULL },
{ NULL, 0, 0, false, false, false, false, NULL, NULL }
};
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index b835d67..8473e7a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2019-11-15 Joseph Myers <joseph@codesourcery.com>
+
+ * gcc.dg/c2x-attr-maybe_unused-1.c,
+ gcc.dg/c2x-attr-maybe_unused-2.c,
+ gcc.dg/c2x-attr-maybe_unused-3.c: New tests.
+
2019-11-15 Matthew Malcomson <matthew.malcomson@arm.com>
* gcc.dg/rtl/aarch64/rtl-handle-column-numbers.c: New test.
diff --git a/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-1.c b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-1.c
new file mode 100644
index 0000000..221ebdd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-1.c
@@ -0,0 +1,31 @@
+/* Test C2x maybe_unused attribute: valid uses. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors -Wall -Wextra -Wunused" } */
+
+[[maybe_unused]] static void f1 (void) {}
+
+[[__maybe_unused__]] const int c1 = 1;
+static int c2 [[maybe_unused]] = 2;
+
+int
+g ([[maybe_unused]] int x, int y)
+{
+ [[maybe_unused]] typedef float F;
+ [[maybe_unused]] int a;
+ int b [[__maybe_unused__]];
+ int c [[maybe_unused]];
+ c = y;
+ return y;
+}
+
+struct [[maybe_unused]] s { double d; };
+
+struct s2 { [[__maybe_unused__]] int a; int b [[maybe_unused]]; } x; /* { dg-warning "attribute ignored" } */
+
+enum e { E1 [[maybe_unused]] };
+
+union [[maybe_unused]] u { int x; };
+
+enum [[maybe_unused]] eu { E2 };
+
+union u2 { [[maybe_unused]] int a; int b [[maybe_unused]]; } y; /* { dg-warning "attribute ignored" } */
diff --git a/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c
new file mode 100644
index 0000000..9b5055b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c
@@ -0,0 +1,25 @@
+/* Test C2x maybe_unused attribute: invalid contexts. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+/* This attribute is not valid in most cases on types other than their
+ definitions, or on statements, or as an attribute-declaration. */
+
+[[maybe_unused]]; /* { dg-warning "ignored" } */
+
+int [[maybe_unused]] var; /* { dg-warning "ignored" } */
+/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */
+
+int array_with_dep_type[2] [[maybe_unused]]; /* { dg-warning "ignored" } */
+/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */
+
+void fn_with_dep_type () [[maybe_unused]]; /* { dg-warning "ignored" } */
+/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */
+
+void
+f (void)
+{
+ int a;
+ [[maybe_unused]]; /* { dg-warning "ignored" } */
+ [[maybe_unused]] a = 1; /* { dg-warning "ignored" } */
+}
diff --git a/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-3.c b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-3.c
new file mode 100644
index 0000000..4d4da08
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-3.c
@@ -0,0 +1,11 @@
+/* Test C2x maybe_unused attribute: invalid syntax. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+[[maybe_unused()]] int a; /* { dg-error "does not take any arguments" } */
+
+[[maybe_unused(0)]] int b; /* { dg-error "does not take any arguments|expected" } */
+
+[[maybe_unused("", 123)]] int c; /* { dg-error "does not take any arguments|expected" } */
+
+[[maybe_unused("")]] int d; /* { dg-error "does not take any arguments|expected" } */