diff mbox series

[pushed] c++: support C++11 attributes in C++98

Message ID 20240903220018.2785461-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: support C++11 attributes in C++98 | expand

Commit Message

Jason Merrill Sept. 3, 2024, 10 p.m. UTC
Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

I don't see any reason why we can't allow the [[]] attribute syntax in C++98
mode with a pedwarn just like many other C++11 features.  In fact, we
already do support it in some places in the grammar, but not in places that
check cp_nth_tokens_can_be_std_attribute_p.

Let's also follow the C front-end's lead in only warning about them when
 -pedantic.

It still isn't necessary for this function to guard against Objective-C
message passing syntax; we handle that with tentative parsing in
cp_parser_statement, and we don't call this function in that context anyway.

gcc/cp/ChangeLog:

	* parser.cc (cp_nth_tokens_can_be_std_attribute_p): Don't check
	cxx_dialect.
	* error.cc (maybe_warn_cpp0x): Only complain about C++11 attributes
	if pedantic.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/gen-attrs-1.C: Also run in C++98 mode.
	* g++.dg/cpp0x/gen-attrs-11.C: Likewise.
	* g++.dg/cpp0x/gen-attrs-13.C: Likewise.
	* g++.dg/cpp0x/gen-attrs-15.C: Likewise.
	* g++.dg/cpp0x/gen-attrs-75.C: Don't expect C++98 warning after
	__extension__.
---
 gcc/cp/error.cc                           |  7 ++++---
 gcc/cp/parser.cc                          |  9 ++++-----
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C  |  2 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C |  2 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C |  2 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C |  2 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C | 10 +++++-----
 7 files changed, 17 insertions(+), 17 deletions(-)


base-commit: 1fad396dd467326251572811b703e788e62a2588

Comments

Andrew Pinski Sept. 3, 2024, 11 p.m. UTC | #1
On Tue, Sep 3, 2024 at 3:01 PM Jason Merrill <jason@redhat.com> wrote:
>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
> -- 8< --
>
> I don't see any reason why we can't allow the [[]] attribute syntax in C++98
> mode with a pedwarn just like many other C++11 features.  In fact, we
> already do support it in some places in the grammar, but not in places that
> check cp_nth_tokens_can_be_std_attribute_p.
>
> Let's also follow the C front-end's lead in only warning about them when
>  -pedantic.
>
> It still isn't necessary for this function to guard against Objective-C
> message passing syntax; we handle that with tentative parsing in
> cp_parser_statement, and we don't call this function in that context anyway.
>
> gcc/cp/ChangeLog:
>
>         * parser.cc (cp_nth_tokens_can_be_std_attribute_p): Don't check
>         cxx_dialect.
>         * error.cc (maybe_warn_cpp0x): Only complain about C++11 attributes
>         if pedantic.
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/cpp0x/gen-attrs-1.C: Also run in C++98 mode.
>         * g++.dg/cpp0x/gen-attrs-11.C: Likewise.
>         * g++.dg/cpp0x/gen-attrs-13.C: Likewise.
>         * g++.dg/cpp0x/gen-attrs-15.C: Likewise.
>         * g++.dg/cpp0x/gen-attrs-75.C: Don't expect C++98 warning after
>         __extension__.
> ---
>  gcc/cp/error.cc                           |  7 ++++---
>  gcc/cp/parser.cc                          |  9 ++++-----
>  gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C  |  2 +-
>  gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C |  2 +-
>  gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C |  2 +-
>  gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C |  2 +-
>  gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C | 10 +++++-----
>  7 files changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
> index 57cd76caf49..4a9e9aa3cdc 100644
> --- a/gcc/cp/error.cc
> +++ b/gcc/cp/error.cc
> @@ -4735,9 +4735,10 @@ maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
>                  "only available with %<-std=c++11%> or %<-std=gnu++11%>");
>          break;
>        case CPP0X_ATTRIBUTES:
> -       pedwarn (loc, OPT_Wc__11_extensions,
> -                "C++11 attributes "
> -                "only available with %<-std=c++11%> or %<-std=gnu++11%>");
> +       if (pedantic)
> +         pedwarn (loc, OPT_Wc__11_extensions,
> +                  "C++11 attributes "
> +                  "only available with %<-std=c++11%> or %<-std=gnu++11%>");

Shouldn't the warning also change to mention -std=gnu++98 now? Or
maybe reworded a little more?

Thanks,
Andrew Pinski


>         break;
>        case CPP0X_REF_QUALIFIER:
>         pedwarn (loc, OPT_Wc__11_extensions,
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index edfa5a49440..64122d937fa 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -29924,11 +29924,10 @@ cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
>  {
>    cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
>
> -  return (cxx_dialect >= cxx11
> -         && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
> -             || (token->type == CPP_OPEN_SQUARE
> -                 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
> -                 && token->type == CPP_OPEN_SQUARE)));
> +  return ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
> +         || (token->type == CPP_OPEN_SQUARE
> +             && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
> +             && token->type == CPP_OPEN_SQUARE));
>  }
>
>  /* Return TRUE iff the next Nth tokens in the stream are possibly the
> diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
> index c2cf912047e..b1625d96916 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
> @@ -1,3 +1,3 @@
> -// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-Wno-c++11-extensions" }
>
>  int **** [[gnu::format(printf, 1, 2)]] foo(const char *, ...); // { dg-warning "only applies to function types" }
> diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
> index 504b4565679..040f15c9dbb 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
> @@ -1,4 +1,4 @@
> -// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-Wno-c++11-extensions" }
>  // PR c++/13791
>
>  template <typename T> struct O {
> diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
> index a1b4a84b7e5..8997b845dfd 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
> @@ -1,4 +1,4 @@
> -// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-Wno-c++11-extensions" }
>  // PR c++/13854
>
>  extern char *rindex [[gnu::__pure__]] (__const char *__s, int __c) throw ();
> diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
> index bf05dbeb31b..8b552ca1fbe 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
> @@ -1,4 +1,4 @@
> -// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-Wno-c++11-extensions" }
>  // PR c++/15317
>
>  struct A
> diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
> index bac80aa02ff..c2a328c7c7f 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
> @@ -1,6 +1,6 @@
>  // PR c++/101582
>  // { dg-do compile }
> -// { dg-options "" }
> +// { dg-options "-pedantic -Wno-extra-semi" }
>
>  ;
>  [[]] [[]] [[]];        // { dg-warning "attributes only available with" "" { target c++98_only } }
> @@ -12,9 +12,9 @@ extern "C" [[]];      // { dg-warning "attributes only available with" "" { target c+
>  extern "C" extern "C" ;
>  extern "C" extern "C" [[]][[]][[]];    // { dg-warning "attributes only available with" "" { target c++98_only } }
>  __extension__ ;
> -__extension__ [[]];                    // { dg-warning "attributes only available with" "" { target c++98_only } }
> +__extension__ [[]];
>  __extension__ __extension__ ;
> -__extension__ __extension__ [[]][[]];  // { dg-warning "attributes only available with" "" { target c++98_only } }
> +__extension__ __extension__ [[]][[]];
>
>  namespace N {
>
> @@ -28,8 +28,8 @@ extern "C" [[]];      // { dg-warning "attributes only available with" "" { target c+
>  extern "C" extern "C" ;
>  extern "C" extern "C" [[]][[]][[]];    // { dg-warning "attributes only available with" "" { target c++98_only } }
>  __extension__ ;
> -__extension__ [[]];                    // { dg-warning "attributes only available with" "" { target c++98_only } }
> +__extension__ [[]];
>  __extension__ __extension__ ;
> -__extension__ __extension__ [[]][[]];  // { dg-warning "attributes only available with" "" { target c++98_only } }
> +__extension__ __extension__ [[]][[]];
>
>  }
>
> base-commit: 1fad396dd467326251572811b703e788e62a2588
> --
> 2.46.0
>
Jason Merrill Sept. 4, 2024, 1:08 a.m. UTC | #2
On 9/3/24 7:00 PM, Andrew Pinski wrote:
> On Tue, Sep 3, 2024 at 3:01 PM Jason Merrill <jason@redhat.com> wrote:
>>
>> Tested x86_64-pc-linux-gnu, applying to trunk.
>>
>> -- 8< --
>>
>> I don't see any reason why we can't allow the [[]] attribute syntax in C++98
>> mode with a pedwarn just like many other C++11 features.  In fact, we
>> already do support it in some places in the grammar, but not in places that
>> check cp_nth_tokens_can_be_std_attribute_p.
>>
>> Let's also follow the C front-end's lead in only warning about them when
>>   -pedantic.
>>
>> It still isn't necessary for this function to guard against Objective-C
>> message passing syntax; we handle that with tentative parsing in
>> cp_parser_statement, and we don't call this function in that context anyway.
>>
>> gcc/cp/ChangeLog:
>>
>>          * parser.cc (cp_nth_tokens_can_be_std_attribute_p): Don't check
>>          cxx_dialect.
>>          * error.cc (maybe_warn_cpp0x): Only complain about C++11 attributes
>>          if pedantic.
>>
>> gcc/testsuite/ChangeLog:
>>
>>          * g++.dg/cpp0x/gen-attrs-1.C: Also run in C++98 mode.
>>          * g++.dg/cpp0x/gen-attrs-11.C: Likewise.
>>          * g++.dg/cpp0x/gen-attrs-13.C: Likewise.
>>          * g++.dg/cpp0x/gen-attrs-15.C: Likewise.
>>          * g++.dg/cpp0x/gen-attrs-75.C: Don't expect C++98 warning after
>>          __extension__.
>> ---
>>   gcc/cp/error.cc                           |  7 ++++---
>>   gcc/cp/parser.cc                          |  9 ++++-----
>>   gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C  |  2 +-
>>   gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C |  2 +-
>>   gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C |  2 +-
>>   gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C |  2 +-
>>   gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C | 10 +++++-----
>>   7 files changed, 17 insertions(+), 17 deletions(-)
>>
>> diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
>> index 57cd76caf49..4a9e9aa3cdc 100644
>> --- a/gcc/cp/error.cc
>> +++ b/gcc/cp/error.cc
>> @@ -4735,9 +4735,10 @@ maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
>>                   "only available with %<-std=c++11%> or %<-std=gnu++11%>");
>>           break;
>>         case CPP0X_ATTRIBUTES:
>> -       pedwarn (loc, OPT_Wc__11_extensions,
>> -                "C++11 attributes "
>> -                "only available with %<-std=c++11%> or %<-std=gnu++11%>");
>> +       if (pedantic)
>> +         pedwarn (loc, OPT_Wc__11_extensions,
>> +                  "C++11 attributes "
>> +                  "only available with %<-std=c++11%> or %<-std=gnu++11%>");
> 
> Shouldn't the warning also change to mention -std=gnu++98 now? Or
> maybe reworded a little more?

That's the conventional wording for pedwarns about extensions from later 
standards; I wouldn't change this one alone, though I agree a general 
rewording might make sense.

Jason
diff mbox series

Patch

diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 57cd76caf49..4a9e9aa3cdc 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -4735,9 +4735,10 @@  maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
 		 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
         break;
       case CPP0X_ATTRIBUTES:
-	pedwarn (loc, OPT_Wc__11_extensions,
-		 "C++11 attributes "
-		 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
+	if (pedantic)
+	  pedwarn (loc, OPT_Wc__11_extensions,
+		   "C++11 attributes "
+		   "only available with %<-std=c++11%> or %<-std=gnu++11%>");
 	break;
       case CPP0X_REF_QUALIFIER:
 	pedwarn (loc, OPT_Wc__11_extensions,
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index edfa5a49440..64122d937fa 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -29924,11 +29924,10 @@  cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
 {
   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
 
-  return (cxx_dialect >= cxx11
-	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
-	      || (token->type == CPP_OPEN_SQUARE
-		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
-		  && token->type == CPP_OPEN_SQUARE)));
+  return ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
+	  || (token->type == CPP_OPEN_SQUARE
+	      && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
+	      && token->type == CPP_OPEN_SQUARE));
 }
 
 /* Return TRUE iff the next Nth tokens in the stream are possibly the
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
index c2cf912047e..b1625d96916 100644
--- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-1.C
@@ -1,3 +1,3 @@ 
-// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-c++11-extensions" }
 
 int **** [[gnu::format(printf, 1, 2)]] foo(const char *, ...); // { dg-warning "only applies to function types" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
index 504b4565679..040f15c9dbb 100644
--- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-11.C
@@ -1,4 +1,4 @@ 
-// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-c++11-extensions" }
 // PR c++/13791
 
 template <typename T> struct O {
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
index a1b4a84b7e5..8997b845dfd 100644
--- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-13.C
@@ -1,4 +1,4 @@ 
-// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-c++11-extensions" }
 // PR c++/13854
 
 extern char *rindex [[gnu::__pure__]] (__const char *__s, int __c) throw ();
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
index bf05dbeb31b..8b552ca1fbe 100644
--- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-15.C
@@ -1,4 +1,4 @@ 
-// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-c++11-extensions" }
 // PR c++/15317
 
 struct A
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
index bac80aa02ff..c2a328c7c7f 100644
--- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C
@@ -1,6 +1,6 @@ 
 // PR c++/101582
 // { dg-do compile }
-// { dg-options "" }
+// { dg-options "-pedantic -Wno-extra-semi" }
 
 ;
 [[]] [[]] [[]];	// { dg-warning "attributes only available with" "" { target c++98_only } }
@@ -12,9 +12,9 @@  extern "C" [[]];	// { dg-warning "attributes only available with" "" { target c+
 extern "C" extern "C" ;
 extern "C" extern "C" [[]][[]][[]];	// { dg-warning "attributes only available with" "" { target c++98_only } }
 __extension__ ;
-__extension__ [[]];			// { dg-warning "attributes only available with" "" { target c++98_only } }
+__extension__ [[]];
 __extension__ __extension__ ;
-__extension__ __extension__ [[]][[]];	// { dg-warning "attributes only available with" "" { target c++98_only } }
+__extension__ __extension__ [[]][[]];
 
 namespace N {
 
@@ -28,8 +28,8 @@  extern "C" [[]];	// { dg-warning "attributes only available with" "" { target c+
 extern "C" extern "C" ;
 extern "C" extern "C" [[]][[]][[]];	// { dg-warning "attributes only available with" "" { target c++98_only } }
 __extension__ ;
-__extension__ [[]];			// { dg-warning "attributes only available with" "" { target c++98_only } }
+__extension__ [[]];
 __extension__ __extension__ ;
-__extension__ __extension__ [[]][[]];	// { dg-warning "attributes only available with" "" { target c++98_only } }
+__extension__ __extension__ [[]][[]];
 
 }