public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-8392] c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes in -std=c11 etc.
@ 2024-03-02  0:39 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2024-03-02  0:39 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:051cd2a3ee9ba9f47d640a10e96c79b6c5124736

commit r13-8392-g051cd2a3ee9ba9f47d640a10e96c79b6c5124736
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Feb 22 19:32:02 2024 +0100

    c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes in -std=c11 etc. modes [PR114007]
    
    We aren't able to parse __has_attribute (vendor::attr) (and __has_c_attribute
    and __has_cpp_attribute) in strict C < C23 modes.  While in -std=gnu* modes
    or in -std=c23 there is CPP_SCOPE token, in -std=c* (except for -std=c23)
    there are is just a pair of CPP_COLON tokens.
    The c-lex.cc hunk adds support for that, but always returns 0 in that case
    unlike the GCC 14+ version.
    
    2024-02-22  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/114007
    gcc/c-family/
            * c-lex.cc (c_common_has_attribute): Parse 2 CPP_COLONs with
            the first one with COLON_SCOPE flag the same as CPP_SCOPE but
            ensure 0 is returned then.
    gcc/testsuite/
            * gcc.dg/c23-attr-syntax-8.c: New test.
    libcpp/
            * include/cpplib.h (COLON_SCOPE): Define to PURE_ZERO.
            * lex.cc (_cpp_lex_direct): When lexing CPP_COLON with another
            colon after it, if !CPP_OPTION (pfile, scope) set COLON_SCOPE
            flag on the first CPP_COLON token.
    
    (cherry picked from commit 37127ed975e09813eaa2d1cf1062055fce45dd16)

Diff:
---
 gcc/c-family/c-lex.cc                    | 32 ++++++++++++++++++++++++++++++--
 gcc/testsuite/gcc.dg/c23-attr-syntax-8.c | 12 ++++++++++++
 libcpp/include/cpplib.h                  |  1 +
 libcpp/lex.cc                            |  9 +++++++--
 4 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/gcc/c-family/c-lex.cc b/gcc/c-family/c-lex.cc
index 6eb0fae2f53..0acfdaa95c9 100644
--- a/gcc/c-family/c-lex.cc
+++ b/gcc/c-family/c-lex.cc
@@ -327,9 +327,28 @@ c_common_has_attribute (cpp_reader *pfile, bool std_syntax)
       do
 	nxt_token = cpp_peek_token (pfile, idx++);
       while (nxt_token->type == CPP_PADDING);
-      if (nxt_token->type == CPP_SCOPE)
+      if (!c_dialect_cxx ()
+	  && nxt_token->type == CPP_COLON
+	  && (nxt_token->flags & COLON_SCOPE) != 0)
+	{
+	  const cpp_token *prev_token = nxt_token;
+	  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 but 2 CPP_COLON tokens, where the
+		 first one should have COLON_SCOPE flag to distinguish
+		 it from : :.  */
+	      have_scope = true;
+	      get_token_no_padding (pfile); // Eat first colon.
+	    }
+	  else
+	    nxt_token = prev_token;
+	}
+      if (nxt_token->type == CPP_SCOPE || have_scope)
 	{
-	  have_scope = true;
 	  get_token_no_padding (pfile); // Eat scope.
 	  nxt_token = get_token_no_padding (pfile);
 	  if (nxt_token->type == CPP_NAME)
@@ -359,6 +378,15 @@ c_common_has_attribute (cpp_reader *pfile, bool std_syntax)
 			 "attribute identifier required after scope");
 	      attr_name = NULL_TREE;
 	    }
+	  if (have_scope)
+	    {
+	      /* The parser in this case won't be able to parse
+		 [[vendor::attr]], so ensure 0 is returned.  */
+	      result = 0;
+	      attr_name = NULL_TREE;
+	    }
+	  else
+	    have_scope = true;
 	}
       else
 	{
diff --git a/gcc/testsuite/gcc.dg/c23-attr-syntax-8.c b/gcc/testsuite/gcc.dg/c23-attr-syntax-8.c
new file mode 100644
index 00000000000..6fff160dff0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-attr-syntax-8.c
@@ -0,0 +1,12 @@
+/* PR c/114007 */
+/* { dg-do compile } */
+/* { dg-options "-std=c11" } */
+
+#if __has_c_attribute (gnu::unused)
+[[gnu::unused]]
+#endif
+int i;
+#if __has_cpp_attribute (gnu::unused)
+[[gnu::unused]]
+#endif
+int j;
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index a6f0abd894c..b8e50ae15bb 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -197,6 +197,7 @@ struct GTY(()) cpp_string {
 #define DECIMAL_INT     (1 << 6) /* Decimal integer, set in c-lex.cc.  */
 #define PURE_ZERO	(1 << 7) /* Single 0 digit, used by the C++ frontend,
 				    set in c-lex.cc.  */
+#define COLON_SCOPE	PURE_ZERO /* Adjacent colons in C < 23.  */
 #define SP_DIGRAPH	(1 << 8) /* # or ## token was a digraph.  */
 #define SP_PREV_WHITE	(1 << 9) /* If whitespace before a ##
 				    operator, or before this token
diff --git a/libcpp/lex.cc b/libcpp/lex.cc
index 45ea16a91bc..8951ac56ee2 100644
--- a/libcpp/lex.cc
+++ b/libcpp/lex.cc
@@ -4180,8 +4180,13 @@ _cpp_lex_direct (cpp_reader *pfile)
 
     case ':':
       result->type = CPP_COLON;
-      if (*buffer->cur == ':' && CPP_OPTION (pfile, scope))
-	buffer->cur++, result->type = CPP_SCOPE;
+      if (*buffer->cur == ':')
+	{
+	  if (CPP_OPTION (pfile, scope))
+	    buffer->cur++, result->type = CPP_SCOPE;
+	  else
+	    result->flags |= COLON_SCOPE;
+	}
       else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
 	{
 	  buffer->cur++;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-02  0:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-02  0:39 [gcc r13-8392] c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes in -std=c11 etc Jakub Jelinek

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).