From patchwork Wed Aug 5 19:09:38 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladislav Semykin X-Patchwork-Id: 140674 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A1554BAE7E8 for ; Wed, 5 Aug 2026 19:10:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A1554BAE7E8 Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=UU1euIfZ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by sourceware.org (Postfix) with ESMTPS id 0F4124BA903E for ; Wed, 5 Aug 2026 19:09:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0F4124BA903E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0F4124BA903E Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2607:f8b0:4864:20::1134 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785956991; cv=pass; b=BjWm6K6ff+u4bofRsX7RNmu1Kyl2WMpQztfZ6Or7/5xyUuf+QEvukN5bYyrIZd3VNTpIZt+quO4QlQwe8358FZYORWA90ffZwkrXua7A0Sgl4lpETlbSeS6sApGkAQD3wbFVXszwy8NUFTV4eU0fPbMdtUYLabzUNKDNq+vKE1o= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785956991; c=relaxed/simple; bh=JVNKPw82kKBrJFVo5wy/hLOAq+UQ3bpItjvDWCoeEuU=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=EgXGL8rie2eyW6PvZjJKlFMnEI7XJ3dGCCFAG/A7/p7K756hF07Ob8rTvL29RSJpNkK/NFUURUCU60QqG9bTlj6H4rhtiCYxF6DtyRGwEwlF3bilrO61g1Wr3lVss3Kgd9dPlNuB2f3N8Ah+LRmo6VUJOSHJ3OkgXNfSxCMLiV0= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=UU1euIfZ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F4124BA903E Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-81f64e8dfbcso20538997b3.2 for ; Wed, 05 Aug 2026 12:09:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1785956990; cv=none; d=google.com; s=arc-20260327; b=HD5cW2Yn+j9h4I4jm9dgGIORImVOCq4FwRl1fa/jt8b2kcvG/Ga4w5+X1vS/vqqD6w E0ffIr4sR1VOSBenKMHQFWLivGuf/aUqhFMEU1by4avrXkva+o6m+SaDlsGhFQtAxv4o BRYliYsHj+Ib4KxECrSBRQzBcK5KZgNhukFB+X+jAOZmvfFBfjLStVKqmJJ+F3JG1UBt RRulC95lS8crsirr5hSqzehL+uU5mDPm8bc3V2IWxjqct5gmqYgXdQjCFUdrN5lNdHky fc8YFbfEPse1tpng0xXXN4d61cHQoA2L+ftFW2OIw/YBXG7Z/KTNXMw8Zv6SEKwBsmQS Nczw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=to:subject:message-id:date:from:mime-version:dkim-signature; bh=jLSkoweZ+viZpvWAKYCpP9YPXGE7+C7aA03zgHMvpBc=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=VC5VQR5f/8Y48TOCEWKnUxeBRJOoSwdfY3387MRzkIDBZRleZJzC+uECUUF/fKNC9k fxDhq40iKlh87WPUE+5lKO8mQQ7J677bAd9Kry6kOIRuEQTBmjv+SrVg7JMgsv9GYkYv re6jNgcSj6dZyNqM2PjV8YRv+zsKcJWuCyEOVMAHKo8YhWfH8Gry16Dtis6IN2Me5ScC i4/Bqds67Bl8KrmFLkGWwAbs/1IS4eHD+X7yVwRqfb0QZZilBtdNPLrLI7qfeTeqj6ZJ vnFJkUf+5vh6iC9EDQEsj8171TCVo70WyY/vTxRUmVMLjD5nJiF/s/yDpFlNlsz3hx0E aotA==; darn=gcc.gnu.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785956990; x=1786561790; darn=gcc.gnu.org; h=content-type:to:subject:message-id:date:from:mime-version:from:to :cc:subject:date:message-id:reply-to:content-type; bh=jLSkoweZ+viZpvWAKYCpP9YPXGE7+C7aA03zgHMvpBc=; b=UU1euIfZmqmszb0PbJQ6J+sCc7l1Rt535sncZh/Gbw/TP239JJcxo4GXwPZrj5tZJR kY/2n+Zu/r9AHO43cJBP6WAt8ufMcy1SR+y5nCGD0FHuhtuXEcfEE6u+FzJE9w5FZ1fI mFY0+XccsVntw/xhIlAT396It3rFkYhtx94JF6/lEZpPNjp3nppqrxuyrl3jl1mHJC3+ YmqLniTjkSFU858tiVFvj195KIjsKTTMhF6pRdsbGjhyDSgbx+YXWtCt5HM2W8qFOeRE xrb1+C1um06fhTFgLMpqbxlMaaf65WS2IUzyRNl3W+tqgxVIokbuQ9W+J1UbnVPhV3Bc dbqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785956990; x=1786561790; h=content-type:to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jLSkoweZ+viZpvWAKYCpP9YPXGE7+C7aA03zgHMvpBc=; b=jEYyEZjEfnNkvJN5QMWjoWx2Bm+OBa0imIyyVSNVCjfeGjBnlOQU4M77kub4EpzO3o zaVkef1fu0eZkLv4BDzU+2ExeYy0hM924k253GHOcZvUDpSlxZV5Z5p/KvVFENZ7eBQk arvbhB/NJVeBctEB5HL9gCA4RyQ2rpL0uegCCLmRILI7wtlruyLcGffEaGPHvXy343nn sAavax8MjJuS8c9GfwDbPG/TWxyv6Qx4XGC3dUD0v2p9wbrIy93EcMzieNj04tMeTMoA Q0qVOgBcLwi14vo/OsMizlrNIFdrN+xI+10dLFjl/vI+BHvqeMAHe9PoteGrhqTBE9KH Rlcg== X-Gm-Message-State: AOJu0Yz4V2bpoCBVnU9TCSqYYo2YMZjaXRyhy7LlExdeefqTzehrr/iO 5XUzVOvP4/PC9HlmqMA/Y6VvS2dbx1HKpZ0cpWm6R4LJsMwGts334j1HoXDCvonSYg1drRd5tWU Zd7aCJQM14kP64M+b83AEmIyZJM1utePYpIbmMk8PhD7m X-Gm-Gg: AR+sD12j58yhw3/9WnYFGqwUwM/HNxIuFK4R/nl/Lw9i8rucwV6gh4DZCGLGnncO7Ul to7I0VmkXV08Dv4qVs1bopVEfkE4G4pwqyTESi08kvvRyZydAVOLGSBz/RK3Y/dRvJhK/hfzIUZ H1wn4k+AX9BG+o4cq7vTnY00R2ndZ2vMdqeIUdqOLgd762BPyfltOhepRCS3joeXYlSQDouLVOx L0SXIjLc/9ME5x56OPPRh4DyKIEcz7oKeXFazkXwHWGAMZNXLAzwzr4n7YD+wgBlnEbWs/aqyG9 1OSpCGqbHzGEp3leovVBBFa2ZYWJeJSvLPiq777TSkd/ X-Received: by 2002:a05:690c:4d87:b0:820:de0:6264 with SMTP id 00721157ae682-8201bb5ebeemr56438567b3.0.1785956990248; Wed, 05 Aug 2026 12:09:50 -0700 (PDT) MIME-Version: 1.0 From: Vladislav Semykin Date: Wed, 5 Aug 2026 22:09:38 +0300 X-Gm-Features: AUfX_myDfBMIDh4ChO28X5gkPGNgYtbvbidv9voiuB6nXwGsAwdJ5NK0rcEw8q8 Message-ID: Subject: [PATCH] c-family, c++: Reject non-string-literal error/warning attribute argument [PR126264] To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org 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 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 --- 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 + +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 + +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 + +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 + +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