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

Message ID 20210916091153.3035-1-pc.wang@linux.alibaba.com
State Superseded
Headers
Series C++: add type checking for static local vector variable in template |

Commit Message

pc.wang Sept. 16, 2021, 9:11 a.m. UTC
  This patch adds type checking for static local vector variable in
C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
and they all have this issue.

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

gcc/cp/ChangeLog

        * decl.c (cp_finish_decl): Add type checking.

gcc/testsuite/ChangeLog

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

Comments

Jason Merrill Sept. 16, 2021, 3:19 p.m. UTC | #1
On 9/16/21 05:11, wangpc via Gcc-patches wrote:
> This patch adds type checking for static local vector variable in
> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
> and they all have this issue.
> 
> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
> 
> gcc/cp/ChangeLog
> 
>          * decl.c (cp_finish_decl): Add type checking.
> 
> gcc/testsuite/ChangeLog
> 
>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
> 
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 90111e4c786..e3a06ea0858 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -7520,6 +7520,12 @@ 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 (VAR_P (decl)
> +      && DECL_FUNCTION_SCOPE_P (decl)
> +      && TREE_STATIC (decl))
> +    verify_type_context (DECL_SOURCE_LOCATION (decl),
> +			      TCTX_STATIC_STORAGE, type);

I was thinking to move the verify_type_context code from start_decl, 
which handles more cases:

>   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));
>     }

Jason
  
pc.wang Sept. 17, 2021, 8:05 a.m. UTC | #2
Thanks for your advice, I have misunderstood what you meant.

I have sent a second version patch, please review whether it is OK.

On 2021/9/16 23:19, Jason Merrill wrote:
> On 9/16/21 05:11, wangpc via Gcc-patches wrote:
>> This patch adds type checking for static local vector variable in
>> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
>> and they all have this issue.
>>
>> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
>>
>> gcc/cp/ChangeLog
>>
>>          * decl.c (cp_finish_decl): Add type checking.
>>
>> gcc/testsuite/ChangeLog
>>
>>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
>>
>> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
>> index 90111e4c786..e3a06ea0858 100644
>> --- a/gcc/cp/decl.c
>> +++ b/gcc/cp/decl.c
>> @@ -7520,6 +7520,12 @@ 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 (VAR_P (decl)
>> +      && DECL_FUNCTION_SCOPE_P (decl)
>> +      && TREE_STATIC (decl))
>> +    verify_type_context (DECL_SOURCE_LOCATION (decl),
>> +                  TCTX_STATIC_STORAGE, type);
>
> I was thinking to move the verify_type_context code from start_decl, 
> which handles more cases:
>
>>   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));
>>     }
>
> Jason
  

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 90111e4c786..e3a06ea0858 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7520,6 +7520,12 @@  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 (VAR_P (decl)
+      && DECL_FUNCTION_SCOPE_P (decl)
+      && TREE_STATIC (decl))
+    verify_type_context (DECL_SOURCE_LOCATION (decl),
+			      TCTX_STATIC_STORAGE, type);
+
   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..c2395d18d50
--- /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} } */