public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug preprocessor/114007] gcc chokes on __has_cpp_attribute(clang::unsafe_buffer_usage)
Date: Wed, 21 Feb 2024 13:58:47 +0000	[thread overview]
Message-ID: <bug-114007-4-pV4FMiS1RF@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114007-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114007

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsandifo at gcc dot gnu.org

--- Comment #13 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Ah, the thing is that while in -std=gnu* modes or -std=c23 the preprocessor
recognizes CPP_SCOPE as one token, in -std=c{89,99,11,17} modes it doesn't, ::
are 2 CPP_COLONs.
So, we could either:
--- gcc/c-family/c-lex.cc.jj    2024-01-03 12:07:02.171734141 +0100
+++ gcc/c-family/c-lex.cc       2024-02-21 14:30:37.247945782 +0100
@@ -357,7 +357,24 @@ c_common_has_attribute (cpp_reader *pfil
       do
        nxt_token = cpp_peek_token (pfile, idx++);
       while (nxt_token->type == CPP_PADDING);
-      if (nxt_token->type == CPP_SCOPE)
+      if (!c_dialect_cxx ()
+         && flag_iso
+         && !flag_isoc23
+         && nxt_token->type == CPP_COLON)
+       {
+         do
+           nxt_token = cpp_peek_token (pfile, idx++);
+         while (nxt_token->type == CPP_PADDING);
+         if (nxt_token->type == CPP_COLON)
+           {
+             /* __has_attribute (vendor::attr) in -std=c17 etc. modes.
+                :: isn't CPP_SCOPE in there and [[vendor::attr]] will
+                not work, only [[__extension__ vendor::attr]].  */
+             have_scope = true;
+             get_token_no_padding (pfile); // Eat first colon.
+           }
+       }
+      if (nxt_token->type == CPP_SCOPE || have_scope)
        {
          have_scope = true;
          get_token_no_padding (pfile); // Eat scope.
but then on testcase like:
#if __has_c_attribute (gnu::unused)
[[gnu::unused]]
#endif
int i;
#if __has_cpp_attribute (gnu::unused)
[[gnu::unused]]
#endif
int j;
fails to compile with e.g -std=c11:
pr114007.c:2:1: warning: ‘gnu’ attribute ignored [-Wattributes]
    2 | [[gnu::unused]]
      | ^
pr114007.c:2:6: error: expected ‘]’ before ‘:’ token
    2 | [[gnu::unused]]
      |      ^
      |      ]
pr114007.c:6:1: warning: ‘gnu’ attribute ignored [-Wattributes]
    6 | [[gnu::unused]]
      | ^
pr114007.c:6:6: error: expected ‘]’ before ‘:’ token
    6 | [[gnu::unused]]
      |      ^
      |      ]
or we could force always returning 0 from __has_attribute/__has_cpp_attribute
in that case, like:
--- gcc/c-family/c-lex.cc.jj    2024-01-03 12:07:02.171734141 +0100
+++ gcc/c-family/c-lex.cc       2024-02-21 14:41:33.768992572 +0100
@@ -357,7 +357,33 @@ c_common_has_attribute (cpp_reader *pfil
       do
        nxt_token = cpp_peek_token (pfile, idx++);
       while (nxt_token->type == CPP_PADDING);
-      if (nxt_token->type == CPP_SCOPE)
+      if (!c_dialect_cxx ()
+         && flag_iso
+         && !flag_isoc23
+         && nxt_token->type == CPP_COLON)
+       {
+         do
+           nxt_token = cpp_peek_token (pfile, idx++);
+         while (nxt_token->type == CPP_PADDING);
+         if (nxt_token->type == CPP_COLON)
+           /* __has_attribute (vendor::attr) in -std=c17 etc. modes.
+              :: isn't CPP_SCOPE in there but 2 CPP_COLON tokens.  */
+           have_scope = true;
+       }
+      if (have_scope)
+       {
+         /* [[vendor::attr]] will not work, only
+            [[__extension__ vendor::attr]] will.  Better always return 0
+            for scoped attributes.  */
+         get_token_no_padding (pfile); // Eat first colon.
+         get_token_no_padding (pfile); // Eat second colon.
+         nxt_token = get_token_no_padding (pfile);
+         if (nxt_token->type != CPP_NAME)
+           cpp_error (pfile, CPP_DL_ERROR,
+                      "attribute identifier required after scope");
+         attr_name = NULL_TREE;
+       }
+      else if (nxt_token->type == CPP_SCOPE)
        {
          have_scope = true;
          get_token_no_padding (pfile); // Eat scope.
The drawback of the second patch is that then users in -std=c{89,99,11,17}
modes don't have a way to query whether a certain scoped attribute is supported
in the preprocessor if they are aware that they need to use [[__extension__
vendor::attr]] rather then
[[vendor::attr]].  On the other side, e.g. in -std=gnu11 -pedantic-errors
compilation
we give 1 for __has_c_attribute (gnu::unused), but it is still rejected, just
with -std=c11 it is rejected even without -Wpedantic.

Maybe instead of loose_scope_p we should be using flag_iso && !flag_isoc23 and
accept [[vendor: :attr]] in the -std=c{89,99,11,17} modes too (with pedwarn for
the [[]] use), and on the other side reject [[__extension__ vendor: :attr]] in
-std=c23
or -std=gnu{89,99,11,17} modes, so that people don't feel using 2 colons rather
than a scope is correct.  And then perhaps go with the first patch rather than
second.

Joseph/Richard, your thoughts on this?

  parent reply	other threads:[~2024-02-21 13:58 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20  9:01 [Bug preprocessor/114007] New: " ro at gcc dot gnu.org
2024-02-20  9:02 ` [Bug preprocessor/114007] " ro at gcc dot gnu.org
2024-02-20  9:09 ` iains at gcc dot gnu.org
2024-02-20  9:11 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-02-20  9:17 ` jakub at gcc dot gnu.org
2024-02-20  9:22 ` jakub at gcc dot gnu.org
2024-02-20  9:29 ` iains at gcc dot gnu.org
2024-02-20  9:31 ` iains at gcc dot gnu.org
2024-02-20  9:35 ` jakub at gcc dot gnu.org
2024-02-20  9:43 ` jakub at gcc dot gnu.org
2024-02-20  9:44 ` jakub at gcc dot gnu.org
2024-02-20 10:01 ` iains at gcc dot gnu.org
2024-02-20 10:12 ` iains at gcc dot gnu.org
2024-02-20 12:04 ` iains at gcc dot gnu.org
2024-02-21 13:58 ` jakub at gcc dot gnu.org [this message]
2024-02-21 14:15 ` rsandifo at gcc dot gnu.org
2024-02-21 14:26 ` jakub at gcc dot gnu.org
2024-02-21 17:04 ` jsm28 at gcc dot gnu.org
2024-02-21 18:29 ` jakub at gcc dot gnu.org
2024-02-22  8:48 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-02-22  9:45 ` fxcoudert at gmail dot com
2024-02-22  9:45 ` fxcoudert at gmail dot com
2024-02-22  9:50 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-02-22  9:52 ` iains at gcc dot gnu.org
2024-02-22  9:58 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-02-22 10:22 ` jakub at gcc dot gnu.org
2024-02-22 10:31 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-02-22 10:34 ` iains at gcc dot gnu.org
2024-02-22 18:33 ` cvs-commit at gcc dot gnu.org
2024-02-22 18:34 ` jakub at gcc dot gnu.org
2024-03-02  0:39 ` cvs-commit at gcc dot gnu.org
2024-03-04 12:10 ` jakub at gcc dot gnu.org
2024-06-11 10:37 ` cvs-commit at gcc dot gnu.org
2024-06-11 10:50 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-114007-4-pV4FMiS1RF@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).