[PATCH] c-family, c++: Reject non-string-literal error/warning attribute argument [PR126264]
Vladislav Semykin <[email protected]> Wed, 5 Aug 2026 22:09:38 +0300
| Newsgroups | gmane.comp.gcc.patches |
|---|---|
| Message-ID | <CAOVfs5-okDCPb0w=7B4Ga2p4YjfS47pSkXM9Wu2EDj5RybiJ0A@mail.gmail.com> |
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.
v1-0001-c-family-c-Reject-non-string-literal-error-warnin.patch
(text/x-patch, 9.3 KB)
From 1cc1dbf933d31cbe91a534fa1cf1f8cebf0dfae6 Mon Sep 17 00:00:00 2001 From: Vladislav Semykin <[email protected]> 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: [email protected] 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 <[email protected]> --- 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 diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index d668ab96630..dbda9c67b9d 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -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; } diff --git a/gcc/testsuite/g++.dg/ext/attr-error-2.C b/gcc/testsuite/g++.dg/ext/attr-error-2.C new file mode 100644 index 00000000000..47a1e95262b --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-error-2.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/ext/attr-error-3.C b/gcc/testsuite/g++.dg/ext/attr-error-3.C new file mode 100644 index 00000000000..9688717a7eb --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-error-3.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/ext/attr-warning-2.C b/gcc/testsuite/g++.dg/ext/attr-warning-2.C new file mode 100644 index 00000000000..0d0c98011e9 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-warning-2.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/ext/attr-warning-3.C b/gcc/testsuite/g++.dg/ext/attr-warning-3.C new file mode 100644 index 00000000000..8cdf6e1a5eb --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-warning-3.C @@ -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" } diff --git a/gcc/testsuite/gcc.dg/attr-error-2.c b/gcc/testsuite/gcc.dg/attr-error-2.c new file mode 100644 index 00000000000..caa979d8b05 --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-error-2.c @@ -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" } */ diff --git a/gcc/testsuite/gcc.dg/attr-error-3.c b/gcc/testsuite/gcc.dg/attr-error-3.c new file mode 100644 index 00000000000..302ce29c4f2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-error-3.c @@ -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" } */ diff --git a/gcc/testsuite/gcc.dg/attr-warning-2.c b/gcc/testsuite/gcc.dg/attr-warning-2.c new file mode 100644 index 00000000000..a287f9ea75c --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-warning-2.c @@ -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" } */ diff --git a/gcc/testsuite/gcc.dg/attr-warning-3.c b/gcc/testsuite/gcc.dg/attr-warning-3.c new file mode 100644 index 00000000000..c05ecb2ab21 --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-warning-3.c @@ -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" } */ diff --git a/gcc/testsuite/gcc.dg/pr56167.c b/gcc/testsuite/gcc.dg/pr56167.c index fc377b4756b..f0d1dcadca1 100644 --- a/gcc/testsuite/gcc.dg/pr56167.c +++ b/gcc/testsuite/gcc.dg/pr56167.c @@ -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