diff mbox series

[v2] C++: add type checking for static local vector variable in template

Message ID 20210918055309.4799-1-pc.wang@linux.alibaba.com
State New
Headers show
Series [v2] C++: add type checking for static local vector variable in template | expand

Commit Message

pc.wang Sept. 18, 2021, 5:53 a.m. UTC
This patch moves verify_type_context from start_decl_1 to cp_finish_decl to
do more type checking such as static local vector variable in C++ template.

2021-08-06  wangpc  <pc.wang@linux.alibaba.com>

gcc/cp/ChangeLog

        * decl.c (start_decl_1): Move verify_type_context to ...
	        (cp_finish_decl): ... to here.

gcc/testsuite/ChangeLog

        * g++.target/aarch64/sve/static-var-in-template.C: New test.

Comments

Jason Merrill Sept. 20, 2021, 7:37 p.m. UTC | #1
On 9/18/21 01:53, wangpc wrote:
> This patch moves verify_type_context from start_decl_1 to cp_finish_decl to
> do more type checking such as static local vector variable in C++ template.

Applied, thanks.  Though a couple of issues with the patch formatting:

> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
> 
> gcc/cp/ChangeLog
> 
>          * decl.c (start_decl_1): Move verify_type_context to ...
> 	        (cp_finish_decl): ... to here.
> 
> gcc/testsuite/ChangeLog
> 
>          * g++.target/aarch64/sve/static-var-in-template.C: New test.

I needed to adjust the spacing on the ChangeLog entries so that the git 
hook would accept them: lines need to start with a single tab.

> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 90111e4c786..deaa6c56a8f 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -5491,13 +5491,6 @@ start_decl_1 (tree decl, bool initialized)
>         cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
>       }
>   
> -  if (is_global_var (decl))
> -    {
> -      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
> -				   ? TCTX_THREAD_STORAGE
> -				   : TCTX_STATIC_STORAGE);
> -      verify_type_context (input_location, context, TREE_TYPE (decl));
> -    }
>     if (initialized)

I needed to apply this hunk by hand because there's a missing blank line 
before if (initialized).

>       /* Is it valid for this decl to have an initializer at all?  */
>       {
> @@ -7520,6 +7513,14 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
>         && DECL_INITIALIZED_IN_CLASS_P (decl))
>       check_static_variable_definition (decl, type);
>   
> +  if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
> +    {
> +      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
> +				   ? TCTX_THREAD_STORAGE
> +				   : TCTX_STATIC_STORAGE);
> +      verify_type_context (input_location, context, TREE_TYPE (decl));
> +    }
> +
>     if (init && TREE_CODE (decl) == FUNCTION_DECL)
>       {
>         tree clone;
> diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> new file mode 100644
> index 00000000000..74237ff7c57
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> @@ -0,0 +1,17 @@
> +/* { dg-do compile } */
> +
> +#include <arm_sve.h>
> +
> +template <int N>
> +void f()
> +{
> +    static svbool_t pg = svwhilelt_b64(0, N);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    f<2>();
> +    return 0;
> +}
> +
> +/* { dg-error "SVE type 'svbool_t' does not have a fixed size" "" { target *-*-* } 0 } */
>
diff mbox series

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 90111e4c786..deaa6c56a8f 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5491,13 +5491,6 @@  start_decl_1 (tree decl, bool initialized)
       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
     }
 
-  if (is_global_var (decl))
-    {
-      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
-				   ? TCTX_THREAD_STORAGE
-				   : TCTX_STATIC_STORAGE);
-      verify_type_context (input_location, context, TREE_TYPE (decl));
-    }
   if (initialized)
     /* Is it valid for this decl to have an initializer at all?  */
     {
@@ -7520,6 +7513,14 @@  cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       && DECL_INITIALIZED_IN_CLASS_P (decl))
     check_static_variable_definition (decl, type);
 
+  if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
+    {
+      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
+				   ? TCTX_THREAD_STORAGE
+				   : TCTX_STATIC_STORAGE);
+      verify_type_context (input_location, context, TREE_TYPE (decl));
+    }
+
   if (init && TREE_CODE (decl) == FUNCTION_DECL)
     {
       tree clone;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644
index 00000000000..74237ff7c57
--- /dev/null
+++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
@@ -0,0 +1,17 @@ 
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error "SVE type 'svbool_t' does not have a fixed size" "" { target *-*-* } 0 } */