public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: make some cp_trait_kind switch statements exhaustive
@ 2022-09-30 17:20 Patrick Palka
  2022-09-30 17:37 ` Patrick Palka
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2022-09-30 17:20 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

This replaces the unreachable default case in some cp_trait_kind
switches with an exhaustive listing of the _unexpected_ trait codes,
so that when adding a new trait we'll get a -Wswitch diagnostic if we
forget to handle the trait code in one of these switches.

Bootstrappend and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk?

gcc/cp/ChangeLog:

	* semantics.cc (trait_expr_value): Make cp_trait_kind switch
	exhaustive.
	(finish_trait_expr): Likewise.
	(finish_trait_type): Likewise.
---
 gcc/cp/semantics.cc | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 66ee2186a84..91ad8aa340f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12052,10 +12052,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
       return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
 
-    default:
-      gcc_unreachable ();
-      return false;
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
+    case CPTK_##CODE:
+#include "cp-trait.def"
+#undef DEFTRAIT_EXPR
+      /* These are handled by finish_trait_type.  */
+      break;
     }
+
+  gcc_unreachable ();
 }
 
 /* Returns true if TYPE meets the requirements for the specified KIND,
@@ -12204,7 +12209,11 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
 	return error_mark_node;
       break;
 
-    default:
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
+    case CPTK_##CODE:
+#include "cp-trait.def"
+#undef DEFTRAIT_TYPE
+      /* These are handled by finish_trait_type.  */
       gcc_unreachable ();
     }
 
@@ -12250,9 +12259,19 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
       if (TYPE_REF_P (type1))
 	type1 = TREE_TYPE (type1);
       return cv_unqualified (type1);
-    default:
-      gcc_unreachable ();
+
+#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
+    case CPTK_##CODE:
+#include "cp-trait.def"
+#undef DEFTRAIT_EXPR
+      /* These are handled by finish_trait_expr.  */
+    case CPTK_BASES:
+    case CPTK_DIRECT_BASES:
+      /* These are handled by finish_bases.  */
+      break;
     }
+
+  gcc_unreachable ();
 }
 
 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
-- 
2.38.0.rc2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: make some cp_trait_kind switch statements exhaustive
  2022-09-30 17:20 [PATCH] c++: make some cp_trait_kind switch statements exhaustive Patrick Palka
@ 2022-09-30 17:37 ` Patrick Palka
  2022-09-30 19:46   ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2022-09-30 17:37 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches, jason


On Fri, 30 Sep 2022, Patrick Palka wrote:

> This replaces the unreachable default case in some cp_trait_kind
> switches with an exhaustive listing of the _unexpected_ trait codes,
> so that when adding a new trait we'll get a -Wswitch diagnostic if we
> forget to handle the trait code in one of these switches.
> 
> Bootstrappend and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (trait_expr_value): Make cp_trait_kind switch
> 	exhaustive.
> 	(finish_trait_expr): Likewise.
> 	(finish_trait_type): Likewise.
> ---
>  gcc/cp/semantics.cc | 31 +++++++++++++++++++++++++------
>  1 file changed, 25 insertions(+), 6 deletions(-)
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 66ee2186a84..91ad8aa340f 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12052,10 +12052,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
>      case CPTK_REF_CONVERTS_FROM_TEMPORARY:
>        return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
>  
> -    default:
> -      gcc_unreachable ();
> -      return false;
> +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
> +    case CPTK_##CODE:
> +#include "cp-trait.def"
> +#undef DEFTRAIT_EXPR

Whoops, this should be #undef DEFTRAIT_TYPE

> +      /* These are handled by finish_trait_type.  */
> +      break;
>      }
> +
> +  gcc_unreachable ();
>  }
>  
>  /* Returns true if TYPE meets the requirements for the specified KIND,
> @@ -12204,7 +12209,11 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
>  	return error_mark_node;
>        break;
>  
> -    default:
> +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
> +    case CPTK_##CODE:
> +#include "cp-trait.def"
> +#undef DEFTRAIT_TYPE
> +      /* These are handled by finish_trait_type.  */
>        gcc_unreachable ();
>      }
>  
> @@ -12250,9 +12259,19 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
>        if (TYPE_REF_P (type1))
>  	type1 = TREE_TYPE (type1);
>        return cv_unqualified (type1);
> -    default:
> -      gcc_unreachable ();
> +
> +#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
> +    case CPTK_##CODE:
> +#include "cp-trait.def"
> +#undef DEFTRAIT_EXPR
> +      /* These are handled by finish_trait_expr.  */
> +    case CPTK_BASES:
> +    case CPTK_DIRECT_BASES:
> +      /* These are handled by finish_bases.  */
> +      break;
>      }
> +
> +  gcc_unreachable ();
>  }
>  
>  /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
> -- 
> 2.38.0.rc2
> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: make some cp_trait_kind switch statements exhaustive
  2022-09-30 17:37 ` Patrick Palka
@ 2022-09-30 19:46   ` Jason Merrill
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2022-09-30 19:46 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches

On 9/30/22 13:37, Patrick Palka wrote:
> 
> On Fri, 30 Sep 2022, Patrick Palka wrote:
> 
>> This replaces the unreachable default case in some cp_trait_kind
>> switches with an exhaustive listing of the _unexpected_ trait codes,
>> so that when adding a new trait we'll get a -Wswitch diagnostic if we
>> forget to handle the trait code in one of these switches.
>>
>> Bootstrappend and regtested on x86_64-pc-linux-gnu, does this look OK
>> for trunk?
>>
>> gcc/cp/ChangeLog:
>>
>> 	* semantics.cc (trait_expr_value): Make cp_trait_kind switch
>> 	exhaustive.
>> 	(finish_trait_expr): Likewise.
>> 	(finish_trait_type): Likewise.
>> ---
>>   gcc/cp/semantics.cc | 31 +++++++++++++++++++++++++------
>>   1 file changed, 25 insertions(+), 6 deletions(-)
>>
>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
>> index 66ee2186a84..91ad8aa340f 100644
>> --- a/gcc/cp/semantics.cc
>> +++ b/gcc/cp/semantics.cc
>> @@ -12052,10 +12052,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
>>       case CPTK_REF_CONVERTS_FROM_TEMPORARY:
>>         return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
>>   
>> -    default:
>> -      gcc_unreachable ();
>> -      return false;
>> +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
>> +    case CPTK_##CODE:
>> +#include "cp-trait.def"
>> +#undef DEFTRAIT_EXPR
> 
> Whoops, this should be #undef DEFTRAIT_TYPE

OK with that fix.

>> +      /* These are handled by finish_trait_type.  */
>> +      break;
>>       }
>> +
>> +  gcc_unreachable ();
>>   }
>>   
>>   /* Returns true if TYPE meets the requirements for the specified KIND,
>> @@ -12204,7 +12209,11 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
>>   	return error_mark_node;
>>         break;
>>   
>> -    default:
>> +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
>> +    case CPTK_##CODE:
>> +#include "cp-trait.def"
>> +#undef DEFTRAIT_TYPE
>> +      /* These are handled by finish_trait_type.  */
>>         gcc_unreachable ();
>>       }
>>   
>> @@ -12250,9 +12259,19 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
>>         if (TYPE_REF_P (type1))
>>   	type1 = TREE_TYPE (type1);
>>         return cv_unqualified (type1);
>> -    default:
>> -      gcc_unreachable ();
>> +
>> +#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
>> +    case CPTK_##CODE:
>> +#include "cp-trait.def"
>> +#undef DEFTRAIT_EXPR
>> +      /* These are handled by finish_trait_expr.  */
>> +    case CPTK_BASES:
>> +    case CPTK_DIRECT_BASES:
>> +      /* These are handled by finish_bases.  */
>> +      break;
>>       }
>> +
>> +  gcc_unreachable ();
>>   }
>>   
>>   /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
>> -- 
>> 2.38.0.rc2
>>
>>
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-30 19:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-30 17:20 [PATCH] c++: make some cp_trait_kind switch statements exhaustive Patrick Palka
2022-09-30 17:37 ` Patrick Palka
2022-09-30 19:46   ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).