diff mbox series

[RFC] libcpp: adjust pedwarn handling

Message ID 20240828151712.1744654-1-jason@redhat.com
State New
Headers show
Series [RFC] libcpp: adjust pedwarn handling | expand

Commit Message

Jason Merrill Aug. 28, 2024, 3:11 p.m. UTC
Tested x86_64-pc-linux-gnu.  Any objections?  Should I change all the other
instances of

if (CPP_PEDANTIC...
  cpp_error (...CPP_DL_PEDWARN

the same way?

-- 8< --

Using CPP_W_PEDANTIC lets users suppress these diagnostics with
 #pragma GCC diagnostic ignored "-Wpedantic".

libcpp/ChangeLog:

	* expr.cc (cpp_classify_number): Use cpp_pedwarning_with_line
	instead of cpp_error_with_line for pedwarns.

gcc/testsuite/ChangeLog:

	* c-c++-common/pragma-diag-17.c: New test.
---
 gcc/testsuite/c-c++-common/pragma-diag-17.c | 14 ++++++
 libcpp/expr.cc                              | 53 +++++++++++----------
 2 files changed, 41 insertions(+), 26 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/pragma-diag-17.c


base-commit: 4246cf4f18053eeb47cb2a241fffa9a41573916e

Comments

Joseph Myers Aug. 28, 2024, 4:44 p.m. UTC | #1
On Wed, 28 Aug 2024, Jason Merrill wrote:

> Tested x86_64-pc-linux-gnu.  Any objections?  Should I change all the other
> instances of
> 
> if (CPP_PEDANTIC...
>   cpp_error (...CPP_DL_PEDWARN
> 
> the same way?

Yes, I think that's a good change.

A followup question might be whether cases such as the unchanged code in

> -      if (!CPP_OPTION (pfile, binary_constants)
> -	  && CPP_PEDANTIC (pfile))
> -	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
> -			     CPP_OPTION (pfile, cplusplus)
> -			     ? N_("binary constants are a C++14 feature "
> -				  "or GCC extension")
> -			     : N_("binary constants are a C23 feature "
> -				  "or GCC extension"));
> +      if (!CPP_OPTION (pfile, binary_constants))
> +	cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
> +				  CPP_OPTION (pfile, cplusplus)
> +				  ? N_("binary constants are a C++14 feature "
> +				       "or GCC extension")
> +				  : N_("binary constants are a C23 feature "
> +				       "or GCC extension"));
>        else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
>  	cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
>  			       virtual_location, 0,

could be refactored to reduce duplication (the C front end has pedwarn_c11 
for such cases of features added in C23, that can be a pedwarn-if-pedantic 
up to C11, or a warning with -Wc11-c23-compat).  Though libcpp handling 
both C and C++, and the use of conditionals such as CPP_OPTION (pfile, 
binary_constants) for individual features, might make that hard.
diff mbox series

Patch

diff --git a/gcc/testsuite/c-c++-common/pragma-diag-17.c b/gcc/testsuite/c-c++-common/pragma-diag-17.c
new file mode 100644
index 00000000000..3a7e09ca982
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pragma-diag-17.c
@@ -0,0 +1,14 @@ 
+/* Test silencing the numeric constant extension pedwarns.  */
+/* { dg-additional-options -fext-numeric-literals { target c++ } } */
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpedantic"
+
+void f()
+{
+  2.0j;
+  1.0dd;
+  1.0d;
+  0b0100;
+  1.0K; /* { dg-bogus {fixed-point types not supported in C\+\+} "" { xfail c++ } } */
+}	/* { dg-error {fixed-point types not supported for this target} "" { target { c && { ! fixed_point } } } .-1 } */
diff --git a/libcpp/expr.cc b/libcpp/expr.cc
index 815eb137a99..a7780ab50b7 100644
--- a/libcpp/expr.cc
+++ b/libcpp/expr.cc
@@ -662,9 +662,9 @@  cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
 	  if (radix == 8)
 	    radix = 10;
 
-	  if (CPP_PEDANTIC (pfile))
-	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-				 "fixed-point constants are a GCC extension");
+	  cpp_pedwarning_with_line
+	    (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+	     "fixed-point constants are a GCC extension");
 	  goto syntax_ok;
 	}
       else
@@ -766,9 +766,10 @@  cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
       /* A suffix for double is a GCC extension via decimal float support.
 	 If the suffix also specifies an imaginary value we'll catch that
 	 later.  */
-      if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
-	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-			     "suffix for double constant is a GCC extension");
+      if (result == CPP_N_MEDIUM)
+	cpp_pedwarning_with_line
+	  (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+	   "suffix for double constant is a GCC extension");
 
       /* Radix must be 10 for decimal floats.  */
       if ((result & CPP_N_DFLOAT) && radix != 10)
@@ -779,15 +780,16 @@  cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
           return CPP_N_INVALID;
         }
 
-      if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
-	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-			     "fixed-point constants are a GCC extension");
+      if (result & (CPP_N_FRACT | CPP_N_ACCUM))
+	cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+				  "fixed-point constants are a GCC extension");
 
       if (result & CPP_N_DFLOAT)
 	{
-	  if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, dfp_constants))
-	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-				 "decimal float constants are a C23 feature");
+	  if (!CPP_OPTION (pfile, dfp_constants))
+	    cpp_pedwarning_with_line
+	      (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+	       "decimal float constants are a C23 feature");
 	  else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
 	    cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
 				   virtual_location, 0,
@@ -870,12 +872,12 @@  cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
 		cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
 				       virtual_location, 0, message);
 	    }
-	  else if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, true_false))
+	  else if (!CPP_OPTION (pfile, true_false))
 	    {
 	      const char *message = N_("ISO C does not support literal "
 				       "%<wb%> suffixes before C23");
-	      cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-				   message);
+	      cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC,
+					virtual_location, 0, message);
 	    }
 	}
 
@@ -883,19 +885,18 @@  cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
     }
 
  syntax_ok:
-  if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
-    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-			 "imaginary constants are a GCC extension");
+  if (result & CPP_N_IMAGINARY)
+    cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+			      "imaginary constants are a GCC extension");
   if (radix == 2)
     {
-      if (!CPP_OPTION (pfile, binary_constants)
-	  && CPP_PEDANTIC (pfile))
-	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
-			     CPP_OPTION (pfile, cplusplus)
-			     ? N_("binary constants are a C++14 feature "
-				  "or GCC extension")
-			     : N_("binary constants are a C23 feature "
-				  "or GCC extension"));
+      if (!CPP_OPTION (pfile, binary_constants))
+	cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
+				  CPP_OPTION (pfile, cplusplus)
+				  ? N_("binary constants are a C++14 feature "
+				       "or GCC extension")
+				  : N_("binary constants are a C23 feature "
+				       "or GCC extension"));
       else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
 	cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
 			       virtual_location, 0,