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

Message ID ZdcFafKIf8KkI7RP@tucnak
State Unresolved
Headers
Series c: Handle scoped attributes in __has*attribute and scoped attribute parsing changes in -std=c11 etc. modes [PR114007] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Feb. 22, 2024, 8:27 a.m. UTC
  Hi!

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.

That leads to a question if we should return 1 or 0 from
__has_attribute (gnu::unused) or not, because while
[[gnu::unused]] is parsed fine in -std=gnu*/-std=c23 modes (sure, with
pedwarn for < C23), we do not parse it at all in -std=c* (except for
-std=c23), we only parse [[__extension__ gnu::unused]] there.  While
the __extension__ in there helps to avoid the pedwarn, I think it is
better to be consistent between GNU and strict C < C23 modes and
parse [[gnu::unused]] too; on the other side, I think parsing
[[__extension__ gnu : : unused]] is too weird and undesirable.

So, the following patch adds a flag during preprocessing at the point
where we normally create CPP_SCOPE tokens out of 2 consecutive colons
on the first CPP_COLON to mark the consecutive case (as we are tight
on the bits, I've reused the PURE_ZERO flag, which is used just by the
C++ FE and only ever set (both C and C++) on CPP_NUMBER tokens, this
new flag has the same value and is only ever used on CPP_COLON tokens)
and instead of checking loose_scope_p argument (i.e. whether it is
[[__extension__ ...]] or not), it just parses CPP_SCOPE or CPP_COLON
with CLONE_SCOPE flag followed by another CPP_COLON the same.
The latter will never appear in >= C23 or -std=gnu* modes, though
guarding its use say with flag_iso && !flag_isoc23 && doesn't really
work because the __extension__ case temporarily clears flag_iso flag.

This makes the -std=c11 etc. behavior more similar to -std=gnu11 or
-std=c23, the only difference I'm aware of are the
#define JOIN2(A, B) A##B
[[vendor JOIN2(:,:) attr]]
[[__extension__ vendor JOIN2(:,:) attr]]
cases, which are accepted in the latter modes, but results in error
in -std=c11; but the error is during preprocessing that :: doesn't
form a valid preprocessing token, which is true, so just don't do that if
you try to have __STRICT_ANSI__ && __STDC_VERSION__ <= 201710L
compatibility.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-02-21  Jakub Jelinek  <jakub@redhat.com>

	PR c/114007
gcc/
	* doc/extend.texi: (__extension__): Remove comments about scope
	tokens vs. two colons.
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.
gcc/c/
	* c-parser.cc (c_parser_std_attribute): Remove loose_scope_p argument.
	Instead of checking it, parse 2 CPP_COLONs with the first one with
	COLON_SCOPE flag the same as CPP_SCOPE.
	(c_parser_std_attribute_list): Remove loose_scope_p argument, don't
	pass it to c_parser_std_attribute.
	(c_parser_std_attribute_specifier): Adjust c_parser_std_attribute_list
	caller.
gcc/testsuite/
	* gcc.dg/c23-attr-syntax-6.c: Adjust testcase for :: being valid
	even in -std=c11 even without __extension__ and : : etc. not being
	valid anymore even with __extension__.
	* gcc.dg/c23-attr-syntax-7.c: Likewise.
	* 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.


	Jakub
  

Comments

Michael Matz Feb. 22, 2024, 2:59 p.m. UTC | #1
Hello,

On Thu, 22 Feb 2024, Jakub Jelinek wrote:

> So, the following patch adds a flag during preprocessing at the point
> where we normally create CPP_SCOPE tokens out of 2 consecutive colons
> on the first CPP_COLON to mark the consecutive case (as we are tight
> on the bits, I've reused the PURE_ZERO flag, which is used just by the
> C++ FE and only ever set (both C and C++) on CPP_NUMBER tokens, this
> new flag has the same value and is only ever used on CPP_COLON tokens)

Hmm, shouldn't you be able to use (nonexistence of) the PREV_WHITE flag on 
the second COLON token to see that it's indeed a '::' without intervening 
whitespace?  Instead of setting a new flag on the first COLON token?

I.e. something like this:

   if (c_parser_next_token_is (parser, CPP_SCOPE)
-      || (loose_scope_p
-         && c_parser_next_token_is (parser, CPP_COLON)
          && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
+         && !(c_parser_peek_2nd_token (parser)->flags & PREV_WHITE)))

?


Ciao,
Michael.
  
Jakub Jelinek Feb. 22, 2024, 3:28 p.m. UTC | #2
On Thu, Feb 22, 2024 at 03:59:31PM +0100, Michael Matz wrote:
> Hello,
> 
> On Thu, 22 Feb 2024, Jakub Jelinek wrote:
> 
> > So, the following patch adds a flag during preprocessing at the point
> > where we normally create CPP_SCOPE tokens out of 2 consecutive colons
> > on the first CPP_COLON to mark the consecutive case (as we are tight
> > on the bits, I've reused the PURE_ZERO flag, which is used just by the
> > C++ FE and only ever set (both C and C++) on CPP_NUMBER tokens, this
> > new flag has the same value and is only ever used on CPP_COLON tokens)
> 
> Hmm, shouldn't you be able to use (nonexistence of) the PREV_WHITE flag on 
> the second COLON token to see that it's indeed a '::' without intervening 
> whitespace?  Instead of setting a new flag on the first COLON token?
> 
> I.e. something like this:
> 
>    if (c_parser_next_token_is (parser, CPP_SCOPE)
> -      || (loose_scope_p
> -         && c_parser_next_token_is (parser, CPP_COLON)
>           && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
> +         && !(c_parser_peek_2nd_token (parser)->flags & PREV_WHITE)))
> 
> ?

That doesn't seem to work.
Compared to the posted patch it doesn't raise the 2 extra errors on
gcc.dg/c23-attr-syntax-6.c
#define JOIN2(A, B) A##B
typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5;
and that is just fine, that is error recovery after another error,
but doesn't diagnose:
#define BAR :
typedef int [[__extension__ gnu BAR BAR vector_size (4)]] b8;
nor
#define JOIN(A, B) A/**/B
typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] b10;
(nor similar cases without __extension__).

Maybe it is about whether there are CPP_PADDING tokens in between if
PREV_WHITE is missing, but on c_parser_peek*_token we don't know if
there were any.  Sure, on the c_common_has_attribute side that could
be done just by dropping the second loop.

--- gcc/doc/extend.texi.jj	2024-02-22 10:10:18.907029080 +0100
+++ gcc/doc/extend.texi	2024-02-22 16:06:33.197555930 +0100
@@ -12626,10 +12626,7 @@ In C, writing:
 @end smallexample
 
 suppresses warnings about using @samp{[[]]} attributes in C versions
-that predate C23@.  Since the scope token @samp{::} is not a single
-lexing token in earlier versions of C, this construct also allows two colons
-to be used in place of @code{::}.  GCC does not check whether the two
-colons are immediately adjacent.
+that predate C23@.
 @end itemize
 
 @code{__extension__} has no effect aside from this.
--- gcc/c-family/c-lex.cc.jj	2024-02-22 10:09:48.408450163 +0100
+++ gcc/c-family/c-lex.cc	2024-02-22 16:09:50.822825035 +0100
@@ -357,7 +357,29 @@ 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)
+	{
+	  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
+	      && (nxt_token->flags & PREV_WHITE) == 0)
+	    {
+	      /* __has_attribute (vendor::attr) in -std=c17 etc. modes.
+		 :: isn't CPP_SCOPE but 2 CPP_COLON tokens, where the
+		 second one shouldn't have PREV_WHITE 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.
--- gcc/c/c-parser.cc.jj	2024-02-22 10:09:48.467449349 +0100
+++ gcc/c/c-parser.cc	2024-02-22 16:11:05.320795586 +0100
@@ -5705,8 +5705,7 @@ c_parser_omp_sequence_args (c_parser *pa
    indicates whether this relaxation is in effect.  */
 
 static tree
-c_parser_std_attribute (c_parser *parser, bool for_tm,
-			bool loose_scope_p = false)
+c_parser_std_attribute (c_parser *parser, bool for_tm)
 {
   c_token *token = c_parser_peek_token (parser);
   tree ns, name, attribute;
@@ -5720,9 +5719,10 @@ c_parser_std_attribute (c_parser *parser
   name = canonicalize_attr_name (token->value);
   c_parser_consume_token (parser);
   if (c_parser_next_token_is (parser, CPP_SCOPE)
-      || (loose_scope_p
+      || (!flag_isoc23
 	  && c_parser_next_token_is (parser, CPP_COLON)
-	  && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
+	  && c_parser_peek_2nd_token (parser)->type == CPP_COLON
+	  && (c_parser_peek_2nd_token (parser)->flags & PREV_WHITE) == 0))
     {
       ns = name;
       if (c_parser_next_token_is (parser, CPP_COLON))
@@ -5841,8 +5841,7 @@ c_parser_std_attribute (c_parser *parser
 }
 
 static tree
-c_parser_std_attribute_list (c_parser *parser, bool for_tm,
-			     bool loose_scope_p = false)
+c_parser_std_attribute_list (c_parser *parser, bool for_tm)
 {
   tree attributes = NULL_TREE;
   while (true)
@@ -5855,7 +5854,7 @@ c_parser_std_attribute_list (c_parser *p
 	  c_parser_consume_token (parser);
 	  continue;
 	}
-      tree attribute = c_parser_std_attribute (parser, for_tm, loose_scope_p);
+      tree attribute = c_parser_std_attribute (parser, for_tm);
       if (attribute != error_mark_node)
 	{
 	  TREE_CHAIN (attribute) = attributes;
@@ -5883,7 +5882,7 @@ c_parser_std_attribute_specifier (c_pars
     {
       auto ext = disable_extension_diagnostics ();
       c_parser_consume_token (parser);
-      attributes = c_parser_std_attribute_list (parser, for_tm, true);
+      attributes = c_parser_std_attribute_list (parser, for_tm);
       restore_extension_diagnostics (ext);
     }
   else
--- gcc/testsuite/gcc.dg/c23-attr-syntax-6.c.jj	2024-02-22 10:09:48.912443204 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-6.c	2024-02-22 16:06:33.227555515 +0100
@@ -9,19 +9,14 @@
 
 typedef int [[__extension__ gnu::vector_size (4)]] g1;
 typedef int [[__extension__ gnu :: vector_size (4)]] g2;
-typedef int [[__extension__ gnu : : vector_size (4)]] g3;
-typedef int [[__extension__ gnu: :vector_size (4)]] g4;
-typedef int [[__extension__ gnu FOO vector_size (4)]] g5;
-typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g6;
-typedef int [[__extension__ gnu :/**/: vector_size (4)]] g7;
-typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] g8;
-typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g10;
-typedef int [[__extension__]] g11;
-typedef int [[__extension__,]] g12;
-typedef int [[__extension__, ,,,, ,, ,]] g13;
-[[__extension__ deprecated]] int g14 ();
-[[__extension__ nodiscard]] int g15 ();
-[[__extension__ noreturn]] void g16 ();
+typedef int [[__extension__ gnu FOO vector_size (4)]] g3;
+typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g4;
+typedef int [[__extension__]] g5;
+typedef int [[__extension__,]] g6;
+typedef int [[__extension__, ,,,, ,, ,]] g7;
+[[__extension__ deprecated]] int g8 ();
+[[__extension__ nodiscard]] int g9 ();
+[[__extension__ noreturn]] void g10 ();
 
 int
 cases (int x)
@@ -51,12 +46,42 @@ typedef int [[__extension__ unknown_attr
 typedef int [[__extension__ gnu:vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */
-typedef int [[gnu::vector_size(4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */
+typedef int [[__extension__ gnu : : vector_size (4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu: :vector_size (4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu BAR BAR vector_size (4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu :/**/: vector_size (4)]] b9; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] b10; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[gnu::vector_size(4)]] b11; /* { dg-error {attributes before C23} } */
+typedef int [[gnu : : vector_size(4)]] b12; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu : vector_size(4)]] b13; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu: :vector_size (4)]] b14; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu BAR BAR vector_size (4)]] b15; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : : vector_size(4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu :/**/: vector_size (4)]] b16; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : vector_size(4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu JOIN(:,:) vector_size (4)]] b17; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :: vector_size (4)]] b18; /* { dg-error {attributes before C23} } */
+typedef int [[gnu FOO vector_size (4)]] b19; /* { dg-error {attributes before C23} } */
+typedef int [[gnu :: vector_size (sizeof (void (*)(...)))]] b20; /* { dg-error {attributes before C23} } */
+/* { dg-error {requires a named argument before} "" { target *-*-* } .-1 } */
+typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */
+/* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-3 } */
--- gcc/testsuite/gcc.dg/c23-attr-syntax-7.c.jj	2024-02-22 10:09:48.957442583 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-7.c	2024-02-22 16:06:33.227555515 +0100
@@ -9,19 +9,14 @@
 
 typedef int [[__extension__ gnu::vector_size (4)]] g1;
 typedef int [[__extension__ gnu :: vector_size (4)]] g2;
-typedef int [[__extension__ gnu : : vector_size (4)]] g3;
-typedef int [[__extension__ gnu: :vector_size (4)]] g4;
-typedef int [[__extension__ gnu FOO vector_size (4)]] g5;
-typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g6;
-typedef int [[__extension__ gnu :/**/: vector_size (4)]] g7;
-typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] g8;
-typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g10;
-typedef int [[__extension__]] g11;
-typedef int [[__extension__,]] g12;
-typedef int [[__extension__, ,,,, ,, ,]] g13;
-[[__extension__ deprecated]] int g14 ();
-[[__extension__ nodiscard]] int g15 ();
-[[__extension__ noreturn]] void g16 ();
+typedef int [[__extension__ gnu FOO vector_size (4)]] g3;
+typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g4;
+typedef int [[__extension__]] g5;
+typedef int [[__extension__,]] g6;
+typedef int [[__extension__, ,,,, ,, ,]] g7;
+[[__extension__ deprecated]] int g8 ();
+[[__extension__ nodiscard]] int g9 ();
+[[__extension__ noreturn]] void g10 ();
 
 int
 cases (int x)
@@ -51,10 +46,37 @@ typedef int [[__extension__ unknown_attr
 typedef int [[__extension__ gnu:vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5;
-typedef int [[gnu::vector_size(4)]] b6; /* { dg-warning {attributes before C23} } */
-typedef int [[gnu : : vector_size(4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[__extension__ gnu : : vector_size (4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu: :vector_size (4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu BAR BAR vector_size (4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu :/**/: vector_size (4)]] b9; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] b10; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[gnu::vector_size(4)]] b11; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu : : vector_size(4)]] b12; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu : vector_size(4)]] b13; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu: :vector_size (4)]] b14; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu BAR BAR vector_size (4)]] b15; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :/**/: vector_size (4)]] b16; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : vector_size(4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu JOIN(:,:) vector_size (4)]] b17; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :: vector_size (4)]] b18; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu FOO vector_size (4)]] b19; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu :: vector_size (sizeof (void (*)(...)))]] b20; /* { dg-warning {attributes before C23} } */
+/* { dg-warning {requires a named argument before} "" { target *-*-* } .-1 } */
+typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-warning {attributes before C23} } */
--- gcc/testsuite/gcc.dg/c23-attr-syntax-8.c.jj	2024-02-22 16:06:33.227555515 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-8.c	2024-02-22 16:06:33.227555515 +0100
@@ -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;


	Jakub
  
Michael Matz Feb. 22, 2024, 3:35 p.m. UTC | #3
Hi,

On Thu, 22 Feb 2024, Jakub Jelinek wrote:

> > Hmm, shouldn't you be able to use (nonexistence of) the PREV_WHITE flag on 
> > the second COLON token to see that it's indeed a '::' without intervening 
> > whitespace?  Instead of setting a new flag on the first COLON token?
> > 
> > I.e. something like this:
> > 
> >    if (c_parser_next_token_is (parser, CPP_SCOPE)
> > -      || (loose_scope_p
> > -         && c_parser_next_token_is (parser, CPP_COLON)
> >           && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
> > +         && !(c_parser_peek_2nd_token (parser)->flags & PREV_WHITE)))
> > 
> > ?
> 
> That doesn't seem to work.

Too bad then.  I had hoped it would make the code easier without changes 
to c-lex.  Well, then ... was worth a try, I'll crouch back under my stone
:)


Ciao,
Michael.
  
Joseph Myers Feb. 22, 2024, 5:49 p.m. UTC | #4
On Thu, 22 Feb 2024, Jakub Jelinek wrote:

> Hi!
> 
> 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.
> 
> That leads to a question if we should return 1 or 0 from
> __has_attribute (gnu::unused) or not, because while
> [[gnu::unused]] is parsed fine in -std=gnu*/-std=c23 modes (sure, with
> pedwarn for < C23), we do not parse it at all in -std=c* (except for
> -std=c23), we only parse [[__extension__ gnu::unused]] there.  While
> the __extension__ in there helps to avoid the pedwarn, I think it is
> better to be consistent between GNU and strict C < C23 modes and
> parse [[gnu::unused]] too; on the other side, I think parsing
> [[__extension__ gnu : : unused]] is too weird and undesirable.
> 
> So, the following patch adds a flag during preprocessing at the point
> where we normally create CPP_SCOPE tokens out of 2 consecutive colons
> on the first CPP_COLON to mark the consecutive case (as we are tight
> on the bits, I've reused the PURE_ZERO flag, which is used just by the
> C++ FE and only ever set (both C and C++) on CPP_NUMBER tokens, this
> new flag has the same value and is only ever used on CPP_COLON tokens)
> and instead of checking loose_scope_p argument (i.e. whether it is
> [[__extension__ ...]] or not), it just parses CPP_SCOPE or CPP_COLON
> with CLONE_SCOPE flag followed by another CPP_COLON the same.
> The latter will never appear in >= C23 or -std=gnu* modes, though
> guarding its use say with flag_iso && !flag_isoc23 && doesn't really
> work because the __extension__ case temporarily clears flag_iso flag.

This patch (the one using COLON_SCOPE, *not* the one using PREV_WHITE) is 
OK.

PREV_WHITE is about whether there is whitespace between the tokens in the 
macro expansion, for the purposes of stringization - I don't think it's 
appropriate to use here.  For example, given

  #define COLON() :

then

  [[gnu COLON()COLON() unused]] int x;

should preferably not be valid; stringizing the results of expanding 
COLON()COLON() will produce "::" (PREV_WHITE not set), but that wouldn't 
be a valid attribute in C23 and I don't think anyone could reasonably 
expect it to be valid with previous standard versions.
  
Jakub Jelinek Feb. 22, 2024, 6:07 p.m. UTC | #5
On Thu, Feb 22, 2024 at 05:49:12PM +0000, Joseph Myers wrote:
> This patch (the one using COLON_SCOPE, *not* the one using PREV_WHITE) is 
> OK.
> 
> PREV_WHITE is about whether there is whitespace between the tokens in the 
> macro expansion, for the purposes of stringization - I don't think it's 
> appropriate to use here.  For example, given
> 
>   #define COLON() :
> 
> then
> 
>   [[gnu COLON()COLON() unused]] int x;
> 
> should preferably not be valid; stringizing the results of expanding 
> COLON()COLON() will produce "::" (PREV_WHITE not set), but that wouldn't 
> be a valid attribute in C23 and I don't think anyone could reasonably 
> expect it to be valid with previous standard versions.

It is not valid in either version of the patch, with any of -std=c11,
-std=gnu11 or -std=c23 with both compilers I get the same
/tmp/test.c:2:1: warning: ‘gnu’ attribute ignored [-Wattributes]
    2 | [[gnu COLON()COLON() unused]] int x;
      | ^
/tmp/test.c:2:6: error: expected ‘]’ before ‘:’ token
    2 | [[gnu COLON()COLON() unused]] int x;
      |      ^
      |      ]
message all the time.
But sure, if you prefer the COLON_SCOPE version of the patch, I can commit
that.  There is no PREV_WHITE in the preprocessor, there is
CPP_PADDING token in between the CPP_COLONs though and that translates
into PREV_WHITE on the second CPP_COLON in the FE token.

When trying to verify it, I've noticed one typo in the PREV_WHITE patch:
@@ -73,7 +73,7 @@ gcc/testsuite/
    switch (type)
      {
      case CPP_PADDING:
-+      add_flags |= CPP_PADDING;
++      add_flags |= PREV_WHITE;
        goto retry;
  
      case CPP_NAME:
but it doesn't change anything on the outcome of those tests nor your
testcase above nor the C++ -std=c++98 -fpermissive test.  CPP_PADDING
is 85 and PREV_WHITE 1, so it was setting PREV_WHITE too (just 3 other flags
too).

	Jakub
  
Joseph Myers Feb. 22, 2024, 6:25 p.m. UTC | #6
On Thu, 22 Feb 2024, Jakub Jelinek wrote:

> But sure, if you prefer the COLON_SCOPE version of the patch, I can commit
> that.  There is no PREV_WHITE in the preprocessor, there is

Yes, I prefer the COLON_SCOPE version.
  

Patch

--- gcc/doc/extend.texi.jj	2024-02-21 10:46:55.746515061 +0100
+++ gcc/doc/extend.texi	2024-02-21 17:03:22.137717754 +0100
@@ -12626,10 +12626,7 @@  In C, writing:
 @end smallexample
 
 suppresses warnings about using @samp{[[]]} attributes in C versions
-that predate C23@.  Since the scope token @samp{::} is not a single
-lexing token in earlier versions of C, this construct also allows two colons
-to be used in place of @code{::}.  GCC does not check whether the two
-colons are immediately adjacent.
+that predate C23@.
 @end itemize
 
 @code{__extension__} has no effect aside from this.
--- gcc/c-family/c-lex.cc.jj	2024-01-03 12:07:02.171734141 +0100
+++ gcc/c-family/c-lex.cc	2024-02-21 18:07:37.615640395 +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 ()
+	  && nxt_token->type == CPP_COLON
+	  && (nxt_token->flags & COLON_SCOPE) != 0)
+	{
+	  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.
+	    }
+	}
+      if (nxt_token->type == CPP_SCOPE || have_scope)
 	{
 	  have_scope = true;
 	  get_token_no_padding (pfile); // Eat scope.
--- gcc/c/c-parser.cc.jj	2024-02-14 14:26:19.599812963 +0100
+++ gcc/c/c-parser.cc	2024-02-21 18:07:20.583874512 +0100
@@ -5705,8 +5705,7 @@  c_parser_omp_sequence_args (c_parser *pa
    indicates whether this relaxation is in effect.  */
 
 static tree
-c_parser_std_attribute (c_parser *parser, bool for_tm,
-			bool loose_scope_p = false)
+c_parser_std_attribute (c_parser *parser, bool for_tm)
 {
   c_token *token = c_parser_peek_token (parser);
   tree ns, name, attribute;
@@ -5720,8 +5719,8 @@  c_parser_std_attribute (c_parser *parser
   name = canonicalize_attr_name (token->value);
   c_parser_consume_token (parser);
   if (c_parser_next_token_is (parser, CPP_SCOPE)
-      || (loose_scope_p
-	  && c_parser_next_token_is (parser, CPP_COLON)
+      || (c_parser_next_token_is (parser, CPP_COLON)
+	  && (c_parser_peek_token (parser)->flags & COLON_SCOPE) != 0
 	  && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
     {
       ns = name;
@@ -5841,8 +5840,7 @@  c_parser_std_attribute (c_parser *parser
 }
 
 static tree
-c_parser_std_attribute_list (c_parser *parser, bool for_tm,
-			     bool loose_scope_p = false)
+c_parser_std_attribute_list (c_parser *parser, bool for_tm)
 {
   tree attributes = NULL_TREE;
   while (true)
@@ -5855,7 +5853,7 @@  c_parser_std_attribute_list (c_parser *p
 	  c_parser_consume_token (parser);
 	  continue;
 	}
-      tree attribute = c_parser_std_attribute (parser, for_tm, loose_scope_p);
+      tree attribute = c_parser_std_attribute (parser, for_tm);
       if (attribute != error_mark_node)
 	{
 	  TREE_CHAIN (attribute) = attributes;
@@ -5883,7 +5881,7 @@  c_parser_std_attribute_specifier (c_pars
     {
       auto ext = disable_extension_diagnostics ();
       c_parser_consume_token (parser);
-      attributes = c_parser_std_attribute_list (parser, for_tm, true);
+      attributes = c_parser_std_attribute_list (parser, for_tm);
       restore_extension_diagnostics (ext);
     }
   else
--- gcc/testsuite/gcc.dg/c23-attr-syntax-6.c.jj	2023-11-09 09:04:19.499530894 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-6.c	2024-02-21 19:16:48.904662570 +0100
@@ -9,19 +9,14 @@ 
 
 typedef int [[__extension__ gnu::vector_size (4)]] g1;
 typedef int [[__extension__ gnu :: vector_size (4)]] g2;
-typedef int [[__extension__ gnu : : vector_size (4)]] g3;
-typedef int [[__extension__ gnu: :vector_size (4)]] g4;
-typedef int [[__extension__ gnu FOO vector_size (4)]] g5;
-typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g6;
-typedef int [[__extension__ gnu :/**/: vector_size (4)]] g7;
-typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] g8;
-typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g10;
-typedef int [[__extension__]] g11;
-typedef int [[__extension__,]] g12;
-typedef int [[__extension__, ,,,, ,, ,]] g13;
-[[__extension__ deprecated]] int g14 ();
-[[__extension__ nodiscard]] int g15 ();
-[[__extension__ noreturn]] void g16 ();
+typedef int [[__extension__ gnu FOO vector_size (4)]] g3;
+typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g4;
+typedef int [[__extension__]] g5;
+typedef int [[__extension__,]] g6;
+typedef int [[__extension__, ,,,, ,, ,]] g7;
+[[__extension__ deprecated]] int g8 ();
+[[__extension__ nodiscard]] int g9 ();
+[[__extension__ noreturn]] void g10 ();
 
 int
 cases (int x)
@@ -51,12 +46,42 @@  typedef int [[__extension__ unknown_attr
 typedef int [[__extension__ gnu:vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */
-typedef int [[gnu::vector_size(4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */
+typedef int [[__extension__ gnu : : vector_size (4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu: :vector_size (4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu BAR BAR vector_size (4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu :/**/: vector_size (4)]] b9; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] b10; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[gnu::vector_size(4)]] b11; /* { dg-error {attributes before C23} } */
+typedef int [[gnu : : vector_size(4)]] b12; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu : vector_size(4)]] b13; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu: :vector_size (4)]] b14; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu BAR BAR vector_size (4)]] b15; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : : vector_size(4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu :/**/: vector_size (4)]] b16; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : vector_size(4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu JOIN(:,:) vector_size (4)]] b17; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-error {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :: vector_size (4)]] b18; /* { dg-error {attributes before C23} } */
+typedef int [[gnu FOO vector_size (4)]] b19; /* { dg-error {attributes before C23} } */
+typedef int [[gnu :: vector_size (sizeof (void (*)(...)))]] b20; /* { dg-error {attributes before C23} } */
+/* { dg-error {requires a named argument before} "" { target *-*-* } .-1 } */
+typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */
+/* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */
+/* { dg-error {attributes before C23} "" { target *-*-* } .-3 } */
--- gcc/testsuite/gcc.dg/c23-attr-syntax-7.c.jj	2023-11-09 09:04:19.499530894 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-7.c	2024-02-21 19:17:19.193247405 +0100
@@ -9,19 +9,14 @@ 
 
 typedef int [[__extension__ gnu::vector_size (4)]] g1;
 typedef int [[__extension__ gnu :: vector_size (4)]] g2;
-typedef int [[__extension__ gnu : : vector_size (4)]] g3;
-typedef int [[__extension__ gnu: :vector_size (4)]] g4;
-typedef int [[__extension__ gnu FOO vector_size (4)]] g5;
-typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g6;
-typedef int [[__extension__ gnu :/**/: vector_size (4)]] g7;
-typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] g8;
-typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g10;
-typedef int [[__extension__]] g11;
-typedef int [[__extension__,]] g12;
-typedef int [[__extension__, ,,,, ,, ,]] g13;
-[[__extension__ deprecated]] int g14 ();
-[[__extension__ nodiscard]] int g15 ();
-[[__extension__ noreturn]] void g16 ();
+typedef int [[__extension__ gnu FOO vector_size (4)]] g3;
+typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g4;
+typedef int [[__extension__]] g5;
+typedef int [[__extension__,]] g6;
+typedef int [[__extension__, ,,,, ,, ,]] g7;
+[[__extension__ deprecated]] int g8 ();
+[[__extension__ nodiscard]] int g9 ();
+[[__extension__ noreturn]] void g10 ();
 
 int
 cases (int x)
@@ -51,10 +46,37 @@  typedef int [[__extension__ unknown_attr
 typedef int [[__extension__ gnu:vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5;
-typedef int [[gnu::vector_size(4)]] b6; /* { dg-warning {attributes before C23} } */
-typedef int [[gnu : : vector_size(4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[__extension__ gnu : : vector_size (4)]] b6; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu: :vector_size (4)]] b7; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu BAR BAR vector_size (4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu :/**/: vector_size (4)]] b9; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[__extension__ gnu JOIN(:,:) vector_size (4)]] b10; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+typedef int [[gnu::vector_size(4)]] b11; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu : : vector_size(4)]] b12; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu : vector_size(4)]] b13; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu: :vector_size (4)]] b14; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu BAR BAR vector_size (4)]] b15; /* { dg-error {expected '\]' before ':'} } */
+/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
+/* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :/**/: vector_size (4)]] b16; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
-typedef int [[gnu : vector_size(4)]] b8; /* { dg-error {expected '\]' before ':'} } */
+typedef int [[gnu JOIN(:,:) vector_size (4)]] b17; /* { dg-error {expected '\]' before ':'} } */
 /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */
 /* { dg-warning {attributes before C23} "" { target *-*-* } .-2 } */
+typedef int [[gnu :: vector_size (4)]] b18; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu FOO vector_size (4)]] b19; /* { dg-warning {attributes before C23} } */
+typedef int [[gnu :: vector_size (sizeof (void (*)(...)))]] b20; /* { dg-warning {attributes before C23} } */
+/* { dg-warning {requires a named argument before} "" { target *-*-* } .-1 } */
+typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-warning {attributes before C23} } */
--- gcc/testsuite/gcc.dg/c23-attr-syntax-8.c.jj	2024-02-21 17:12:06.949467337 +0100
+++ gcc/testsuite/gcc.dg/c23-attr-syntax-8.c	2024-02-21 17:07:37.412189117 +0100
@@ -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;
--- libcpp/include/cpplib.h.jj	2024-01-05 08:35:13.720827001 +0100
+++ libcpp/include/cpplib.h	2024-02-21 16:45:17.134709568 +0100
@@ -200,6 +200,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
--- libcpp/lex.cc.jj	2024-01-03 12:07:27.589378046 +0100
+++ libcpp/lex.cc	2024-02-21 16:38:38.719164770 +0100
@@ -4235,8 +4235,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++;