]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes...
authorJakub Jelinek <jakub@redhat.com>
Thu, 22 Feb 2024 18:32:02 +0000 (19:32 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 20 Jun 2024 13:07:36 +0000 (15:07 +0200)
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)

gcc/c-family/c-lex.c
gcc/testsuite/gcc.dg/c23-attr-syntax-8.c [new file with mode: 0644]
libcpp/include/cpplib.h
libcpp/lex.c

index 88ec7a48fcf1989ad091b2a2f24c86c3ed6871fd..97ddc797a4a1fb279b06ae5d8f280a0c97a6d10d 100644 (file)
@@ -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 (file)
index 0000000..6fff160
--- /dev/null
@@ -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;
index 41d75d9e731b6225a5c1bdc651a95e82914bea5f..93cc7c07b021cc7da2b9af6071b406f6393f4168 100644 (file)
@@ -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
index 0cdd846bbc334e95bb05f210ed7ea19f76b0d555..a450058ad6e547bf15eadfd40e13de8f1f2321a0 100644 (file)
@@ -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++;