c-family, c++: Reject non-string-literal error/warning attribute argument [PR126264]
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gcc_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap |
success
|
Build passed
|
Commit Message
Good day!
Tools gcc-verify and gcc-style are ok. All the new tests are green.
Also, I noticed that the existing test for PR56167 needs updating:
it currently expects only a "attribute ignored" warning for both
attributes with a non-string-literal argument, whereas with this
patch that case is now a hard error. I've adjusted the expected
diagnostics accordingly.
Note that this patch only rejects non-string-literal arguments; it
does not validate the contents of a valid string literal (e.g. the
invalid escape sequence case mentioned in comment 0), which I
will be happy to edit if necessary.
From 1cc1dbf933d31cbe91a534fa1cf1f8cebf0dfae6 Mon Sep 17 00:00:00 2001
From: Vladislav Semykin <vladislav.semykin@gmail.com>
Date: Wed, 5 Aug 2026 21:07:22 +0300
Subject: [PATCH v1] c-family, c++: Reject non-string-literal error/warning
attribute argument [PR126264]
To: gcc-patches@gcc.gnu.org
PR c++/126264
PR c/126264
gcc/c-family/ChangeLog:
* c-attribs.cc (handle_error_attribute): Reject a non-string-literal
argument to the "error"/"warning" attribute with a hard error
instead of silently ignoring it with a warning.
gcc/testsuite/ChangeLog:
* gcc.dg/pr56167.c: Adjust expected diagnostics: a non-string-literal
argument to "error"/"warning" on a function is now a hard error.
* g++.dg/ext/attr-error-2.C: New test.
* g++.dg/ext/attr-error-3.C: New test.
* g++.dg/ext/attr-warning-2.C: New test.
* g++.dg/ext/attr-warning-3.C: New test.
* gcc.dg/attr-error-2.c: New test.
* gcc.dg/attr-error-3.c: New test.
* gcc.dg/attr-warning-2.c: New test.
* gcc.dg/attr-warning-3.c: New test.
Signed-off-by: Vladislav Semykin <vladislav.semykin@gmail.com>
---
gcc/c-family/c-attribs.cc | 14 ++++++++------
gcc/testsuite/g++.dg/ext/attr-error-2.C | 13 +++++++++++++
gcc/testsuite/g++.dg/ext/attr-error-3.C | 6 ++++++
gcc/testsuite/g++.dg/ext/attr-warning-2.C | 13 +++++++++++++
gcc/testsuite/g++.dg/ext/attr-warning-3.C | 6 ++++++
gcc/testsuite/gcc.dg/attr-error-2.c | 13 +++++++++++++
gcc/testsuite/gcc.dg/attr-error-3.c | 7 +++++++
gcc/testsuite/gcc.dg/attr-warning-2.c | 13 +++++++++++++
gcc/testsuite/gcc.dg/attr-warning-3.c | 7 +++++++
gcc/testsuite/gcc.dg/pr56167.c | 7 +++++--
10 files changed, 91 insertions(+), 8 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/attr-error-2.C
create mode 100644 gcc/testsuite/g++.dg/ext/attr-error-3.C
create mode 100644 gcc/testsuite/g++.dg/ext/attr-warning-2.C
create mode 100644 gcc/testsuite/g++.dg/ext/attr-warning-3.C
create mode 100644 gcc/testsuite/gcc.dg/attr-error-2.c
create mode 100644 gcc/testsuite/gcc.dg/attr-error-3.c
create mode 100644 gcc/testsuite/gcc.dg/attr-warning-2.c
create mode 100644 gcc/testsuite/gcc.dg/attr-warning-3.c
@@ -1925,16 +1925,18 @@ static tree
handle_error_attribute (tree *node, tree name, tree args,
int ARG_UNUSED (flags), bool *no_add_attrs)
{
- if (TREE_CODE (*node) == FUNCTION_DECL
- && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
- /* Do nothing else, just set the attribute. We'll get at
- it later with lookup_attribute. */
- ;
- else
+ if (TREE_CODE (*node) != FUNCTION_DECL)
{
warning (OPT_Wattributes, "%qE attribute ignored", name);
*no_add_attrs = true;
}
+ else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
+ {
+ error ("%qE attribute argument not a string constant", name);
+ *no_add_attrs = true;
+ }
+ /* Otherwise, do nothing else, just set the attribute. We'll get at
+ it later with lookup_attribute. */
return NULL_TREE;
}
new file mode 100644
@@ -0,0 +1,13 @@
+// PR c++/126264
+// { dg-do compile }
+
+#include <cstddef>
+
+const char * const message = "hello";
+
+void f1 () __attribute__ ((error (NULL))); // { dg-error "attribute argument not a string constant" }
+void f2 () __attribute__ ((error (0))); // { dg-error "attribute argument not a string constant" }
+void f3 () __attribute__ ((error (message))); // { dg-error "attribute argument not a string constant" }
+
+// Valid usage should still compile without diagnostics.
+void f4 () __attribute__ ((error ("f4 is deprecated"))); // { dg-bogus "attribute" }
new file mode 100644
@@ -0,0 +1,6 @@
+// PR c++/126264
+// nullptr is only available starting with C++11; test it separately so
+// attr-error-2.C isn't gated on a specific standard.
+// { dg-do compile { target c++11 } }
+
+void f () __attribute__ ((error (nullptr))); // { dg-error "attribute argument not a string constant" }
new file mode 100644
@@ -0,0 +1,13 @@
+// PR c++/126264
+// { dg-do compile }
+
+#include <cstddef>
+
+const char * const message = "hello";
+
+void f1 () __attribute__ ((warning (NULL))); // { dg-error "attribute argument not a string constant" }
+void f2 () __attribute__ ((warning (0))); // { dg-error "attribute argument not a string constant" }
+void f3 () __attribute__ ((warning (message))); // { dg-error "attribute argument not a string constant" }
+
+// Valid usage should still compile without diagnostics.
+void f4 () __attribute__ ((warning ("please avoid f4"))); // { dg-bogus "attribute" }
new file mode 100644
@@ -0,0 +1,6 @@
+// PR c++/126264
+// nullptr is only available starting with C++11; test it separately so
+// attr-warning-2.C isn't gated on a specific standard.
+// { dg-do compile { target c++11 } }
+
+void f () __attribute__ ((warning (nullptr))); // { dg-error "attribute argument not a string constant" }
new file mode 100644
@@ -0,0 +1,13 @@
+/* PR c/126264 */
+/* { dg-do compile } */
+
+#include <stddef.h>
+
+const char * const message = "hello";
+
+void f1 (void) __attribute__ ((error (NULL))); /* { dg-error "attribute argument not a string constant" } */
+void f2 (void) __attribute__ ((error (0))); /* { dg-error "attribute argument not a string constant" } */
+void f3 (void) __attribute__ ((error (message))); /* { dg-error "attribute argument not a string constant" } */
+
+/* Valid usage should still compile without diagnostics. */
+void f4 (void) __attribute__ ((error ("f4 is deprecated"))); /* { dg-bogus "attribute" } */
new file mode 100644
@@ -0,0 +1,7 @@
+/* PR c/126264 */
+/* nullptr is only a keyword starting with C23; test it separately so
+ attr-error-2.c isn't gated on a specific standard. */
+/* { dg-do compile } */
+/* { dg-options "-std=c23" } */
+
+void f (void) __attribute__ ((error (nullptr))); /* { dg-error "attribute argument not a string constant" } */
new file mode 100644
@@ -0,0 +1,13 @@
+/* PR c/126264 */
+/* { dg-do compile } */
+
+#include <stddef.h>
+
+const char * const message = "hello";
+
+void f1 (void) __attribute__ ((warning (NULL))); /* { dg-error "attribute argument not a string constant" } */
+void f2 (void) __attribute__ ((warning (0))); /* { dg-error "attribute argument not a string constant" } */
+void f3 (void) __attribute__ ((warning (message))); /* { dg-error "attribute argument not a string constant" } */
+
+/* Valid usage should still compile without diagnostics. */
+void f4 (void) __attribute__ ((warning ("please avoid f4"))); /* { dg-bogus "attribute" } */
new file mode 100644
@@ -0,0 +1,7 @@
+/* PR c/126264 */
+/* nullptr is only a keyword starting with C23; test it separately so
+ attr-warning-2.c isn't gated on a specific standard. */
+/* { dg-do compile } */
+/* { dg-options "-std=c23" } */
+
+void f (void) __attribute__ ((warning (nullptr))); /* { dg-error "attribute argument not a string constant" } */
@@ -1,8 +1,11 @@
/* PR middle-end/56167 */
+/* PR c/126264: a non-string-literal argument to the "error"/"warning"
+ attribute on a function is now a hard error rather than an ignored
+ attribute warning. */
/* { dg-do compile } */
-extern void foo (void) __attribute__ ((error (0))); /* { dg-warning "attribute ignored" } */
-extern void bar (void) __attribute__ ((warning (0))); /* { dg-warning "attribute ignored" } */
+extern void foo (void) __attribute__ ((error (0))); /* { dg-error "attribute argument not a string constant" } */
+extern void bar (void) __attribute__ ((warning (0))); /* { dg-error "attribute argument not a string constant" } */
int var __attribute__ ((error ("foo"))); /* { dg-warning "attribute ignored" } */
int
base-commit: c109d79f5bc4d4e2b65c6ee284a3d853c0173d7d
--
2.43.0