[gcc r11-11495] c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes in -std=c11 etc.

Jakub Jelinek jakub@gcc.gnu.org
Thu Jun 20 13:22:19 GMT 2024


https://gcc.gnu.org/g:d241835c8f47031259280930449a3ba4d8eb8eea

commit r11-11495-gd241835c8f47031259280930449a3ba4d8eb8eea
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.c (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.c (_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.c                     | 32 ++++++++++++++++++++++++++++++--
 gcc/testsuite/gcc.dg/c23-attr-syntax-8.c | 12 ++++++++++++
 libcpp/include/cpplib.h                  |  1 +
 libcpp/lex.c                             |  9 +++++++--
 4 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 88ec7a48fcf1..97ddc797a4a1 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -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)
@@ -347,6 +366,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 000000000000..6fff160dff03
--- /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 41d75d9e731b..93cc7c07b021 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -193,6 +193,7 @@ struct GTY(()) cpp_string {
 #define BOL		(1 << 6) /* Token at beginning of line.  */
 #define PURE_ZERO	(1 << 7) /* Single 0 digit, used by the C++ frontend,
 				    set in c-lex.c.  */
+#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.c b/libcpp/lex.c
index 0cdd846bbc33..a450058ad6e5 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -3344,8 +3344,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++;


More information about the Gcc-cvs mailing list