Patchwork Emit error for negative _Alignas alignment values

login
register
mail settings
Submitter Senthil Kumar Selvaraj
Date April 3, 2013, 1:48 p.m.
Message ID <20130403134820.GA796@atmel.com>
Download mbox | patch
Permalink /patch/233465/
State New
Headers show

Comments

Senthil Kumar Selvaraj - April 3, 2013, 1:48 p.m.
This patch detects and emits an error if the value provided in _Alignas
is negative. The fix was approved pending full regression testing in a
previous discussion (http://gcc.gnu.org/ml/gcc/2013-03/msg00282.html). 
To add to that patch, I have added a testcase that explicitly checks for the
error for a negative power of 2.

Bootstrapped and regression tested with x86_64-unknown-linux-gnu with no new
failures.

If ok, could someone commit please? I don't have commit access.

Regards
Senthil

ChangeLog

2013-04-03	Senthil Kumar Selvaraj	<senthil_kumar.selvaraj@atmel.com>

	* c-common.c (check_user_alignment): Emit error for negative values
    
	* gcc.dg/c1x-align-3.c: Add test for negative power of 2
Senthil Kumar Selvaraj - April 8, 2013, 10:24 a.m.
Ping.

http://gcc.gnu.org/ml/gcc-patches/2013-04/msg00163.html

Regards
Senthil

On Wed, Apr 03, 2013 at 07:18:20PM +0530, Senthil Kumar Selvaraj wrote:
> This patch detects and emits an error if the value provided in _Alignas
> is negative. The fix was approved pending full regression testing in a
> previous discussion (http://gcc.gnu.org/ml/gcc/2013-03/msg00282.html). 
> To add to that patch, I have added a testcase that explicitly checks for the
> error for a negative power of 2.
> 
> Bootstrapped and regression tested with x86_64-unknown-linux-gnu with no new
> failures.
> 
> If ok, could someone commit please? I don't have commit access.
> 
> Regards
> Senthil
> 
> ChangeLog
> 
> 2013-04-03	Senthil Kumar Selvaraj	<senthil_kumar.selvaraj@atmel.com>
> 
> 	* c-common.c (check_user_alignment): Emit error for negative values
>     
> 	* gcc.dg/c1x-align-3.c: Add test for negative power of 2
> 
> diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
> index c7cdd0f..dfdfbb6 100644
> --- gcc/c-family/c-common.c
> +++ gcc/c-family/c-common.c
> @@ -7308,9 +7308,10 @@ check_user_alignment (const_tree align, bool allow_zero)
>      }
>    else if (allow_zero && integer_zerop (align))
>      return -1;
> -  else if ((i = tree_log2 (align)) == -1)
> +  else if (tree_int_cst_sgn (align) == -1
> +           || (i = tree_log2 (align)) == -1)
>      {
> -      error ("requested alignment is not a power of 2");
> +      error ("requested alignment is not a positive power of 2");
>        return -1;
>      }
>    else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
> diff --git gcc/testsuite/gcc.dg/c1x-align-3.c gcc/testsuite/gcc.dg/c1x-align-3.c
> index 0b2a77f..b97351c 100644
> --- gcc/testsuite/gcc.dg/c1x-align-3.c
> +++ gcc/testsuite/gcc.dg/c1x-align-3.c
> @@ -23,6 +23,7 @@ _Alignas (-(__LONG_LONG_MAX__-1)/4) char i3; /* { dg-error "too large|power of 2
>  _Alignas (-(__LONG_LONG_MAX__-1)/8) char i4; /* { dg-error "too large|power of 2" } */
>  _Alignas (-(__LONG_LONG_MAX__-1)/16) char i5; /* { dg-error "too large|power of 2" } */
>  _Alignas (-1) char j; /* { dg-error "power of 2" } */
> +_Alignas (-2) char j; /* { dg-error "positive power of 2" } */
>  _Alignas (3) char k; /* { dg-error "power of 2" } */
>  
>  _Alignas ((void *) 1) char k; /* { dg-error "integer constant" } */
Senthil Kumar Selvaraj - April 18, 2013, 2:03 p.m.
Ping again!

Regards
Senthil

On Wed, Apr 03, 2013 at 07:18:20PM +0530, Senthil Kumar Selvaraj wrote:
> This patch detects and emits an error if the value provided in _Alignas
> is negative. The fix was approved pending full regression testing in a
> previous discussion (http://gcc.gnu.org/ml/gcc/2013-03/msg00282.html). 
> To add to that patch, I have added a testcase that explicitly checks for the
> error for a negative power of 2.
> 
> Bootstrapped and regression tested with x86_64-unknown-linux-gnu with no new
> failures.
> 
> If ok, could someone commit please? I don't have commit access.
> 
> Regards
> Senthil
> 
> gcc/c-family/ChangeLog
> 
> 2013-04-03	Senthil Kumar Selvaraj	<senthil_kumar.selvaraj@atmel.com>
> 
> 	* c-common.c (check_user_alignment): Emit error for negative values
>     
> gcc/testsuite/ChangeLog
>
> 2013-04-03	Senthil Kumar Selvaraj	<senthil_kumar.selvaraj@atmel.com>
>
> 	* gcc.dg/c1x-align-3.c: Add test for negative power of 2
> 
> diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
> index c7cdd0f..dfdfbb6 100644
> --- gcc/c-family/c-common.c
> +++ gcc/c-family/c-common.c
> @@ -7308,9 +7308,10 @@ check_user_alignment (const_tree align, bool allow_zero)
>      }
>    else if (allow_zero && integer_zerop (align))
>      return -1;
> -  else if ((i = tree_log2 (align)) == -1)
> +  else if (tree_int_cst_sgn (align) == -1
> +           || (i = tree_log2 (align)) == -1)
>      {
> -      error ("requested alignment is not a power of 2");
> +      error ("requested alignment is not a positive power of 2");
>        return -1;
>      }
>    else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
> diff --git gcc/testsuite/gcc.dg/c1x-align-3.c gcc/testsuite/gcc.dg/c1x-align-3.c
> index 0b2a77f..b97351c 100644
> --- gcc/testsuite/gcc.dg/c1x-align-3.c
> +++ gcc/testsuite/gcc.dg/c1x-align-3.c
> @@ -23,6 +23,7 @@ _Alignas (-(__LONG_LONG_MAX__-1)/4) char i3; /* { dg-error "too large|power of 2
>  _Alignas (-(__LONG_LONG_MAX__-1)/8) char i4; /* { dg-error "too large|power of 2" } */
>  _Alignas (-(__LONG_LONG_MAX__-1)/16) char i5; /* { dg-error "too large|power of 2" } */
>  _Alignas (-1) char j; /* { dg-error "power of 2" } */
> +_Alignas (-2) char j; /* { dg-error "positive power of 2" } */
>  _Alignas (3) char k; /* { dg-error "power of 2" } */
>  
>  _Alignas ((void *) 1) char k; /* { dg-error "integer constant" } */
Joseph S. Myers - April 24, 2013, 3:18 p.m.
On Wed, 3 Apr 2013, Senthil Kumar Selvaraj wrote:

> 2013-04-03	Senthil Kumar Selvaraj	<senthil_kumar.selvaraj@atmel.com>
> 
> 	* c-common.c (check_user_alignment): Emit error for negative values
>     
> 	* gcc.dg/c1x-align-3.c: Add test for negative power of 2

OK (but note there should be a "." at the end of each ChangeLog entry).

Patch

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index c7cdd0f..dfdfbb6 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -7308,9 +7308,10 @@  check_user_alignment (const_tree align, bool allow_zero)
     }
   else if (allow_zero && integer_zerop (align))
     return -1;
-  else if ((i = tree_log2 (align)) == -1)
+  else if (tree_int_cst_sgn (align) == -1
+           || (i = tree_log2 (align)) == -1)
     {
-      error ("requested alignment is not a power of 2");
+      error ("requested alignment is not a positive power of 2");
       return -1;
     }
   else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
diff --git gcc/testsuite/gcc.dg/c1x-align-3.c gcc/testsuite/gcc.dg/c1x-align-3.c
index 0b2a77f..b97351c 100644
--- gcc/testsuite/gcc.dg/c1x-align-3.c
+++ gcc/testsuite/gcc.dg/c1x-align-3.c
@@ -23,6 +23,7 @@  _Alignas (-(__LONG_LONG_MAX__-1)/4) char i3; /* { dg-error "too large|power of 2
 _Alignas (-(__LONG_LONG_MAX__-1)/8) char i4; /* { dg-error "too large|power of 2" } */
 _Alignas (-(__LONG_LONG_MAX__-1)/16) char i5; /* { dg-error "too large|power of 2" } */
 _Alignas (-1) char j; /* { dg-error "power of 2" } */
+_Alignas (-2) char j; /* { dg-error "positive power of 2" } */
 _Alignas (3) char k; /* { dg-error "power of 2" } */
 
 _Alignas ((void *) 1) char k; /* { dg-error "integer constant" } */