Patchwork [C++0x] pedantic warning with 'alignof(expression)'

login
register
mail settings
Submitter Rodrigo Rivas
Date Dec. 22, 2010, 3:38 p.m.
Message ID <AANLkTimwkgKkWYEN__5163JXm+7QiGQe8VafO5jMFB8c@mail.gmail.com>
Download mbox | patch
Permalink /patch/76424/
State New
Headers show

Comments

Rodrigo Rivas - Dec. 22, 2010, 3:38 p.m.
On Tue, Dec 21, 2010 at 3:30 PM, Jason Merrill <jason@redhat.com> wrote:
> Good points, let's handle this the same way.

Ok, what about the attached patch?

I didn't do the call to cp_parser_allow_gnu_extensions_p because the
expression is already parsed when it gets here.
A solution would be to do:
  if (!cp_parser_allow_gnu_extensions_p (parser))
    error(...);
But I don't think it is worth it (and cannot be testsuited since
cp_parser_allow_gnu_extensions_p always returns true).

Also, a simple testcase is added.

Regards.
Rodrigo

--

Changelog:

gcc/cp/

2010-12-22  Rodrigo Rivas Costa  <rodrigorivascosta@gmail.com>

	* parser.c (cp_parser_unary_expression): Call pedwarn por alignof
	with expression.

gcc/testsuite/

2010-12-22  Rodrigo Rivas Costa  <rodrigorivascosta@gmail.com>

	* g++.dg/cpp0x/alignof2.C: New.
Jason Merrill - Dec. 22, 2010, 7:23 p.m.
I added a check for C++0x mode and checked this in.

Thanks,
Jason
Rodrigo Rivas - Dec. 22, 2010, 8:37 p.m.
On Wed, Dec 22, 2010 at 8:23 PM, Jason Merrill <jason@redhat.com> wrote:
> I added a check for C++0x mode and checked this in.
Cool!
Although, technically, if I'm right, this check is not needed because
"alignof" is a C++0x only keyword. From c-family/c-common.c:

  { "__alignof",        RID_ALIGNOF,    0 },
  { "__alignof__",      RID_ALIGNOF,    0 },
  { "alignof",		RID_ALIGNOF,	D_CXXONLY | D_CXX0X | D_CXXWARN },

Thank you!
Rodrigo

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 505f4d8..fb8235a 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -5890,6 +5890,7 @@  cp_parser_pseudo_destructor_name (cp_parser* parser,
      unary-operator cast-expression
      sizeof unary-expression
      sizeof ( type-id )
+     alignof ( type-id )  [C++0x]
      new-expression
      delete-expression
 
@@ -5899,6 +5900,7 @@  cp_parser_pseudo_destructor_name (cp_parser* parser,
      __extension__ cast-expression
      __alignof__ unary-expression
      __alignof__ ( type-id )
+     alignof unary-expression  [C++0x]
      __real__ cast-expression
      __imag__ cast-expression
      && identifier
@@ -5940,7 +5942,17 @@  cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
 	    if (TYPE_P (operand))
 	      return cxx_sizeof_or_alignof_type (operand, op, true);
 	    else
-	      return cxx_sizeof_or_alignof_expr (operand, op, true);
+	      {
+		/* ISO C++ defines alignof only with types, not with 
+		   expressions. So pedwarn if alignof is used with a non 
+		   type expression. However __alignof__ is ok.  */
+		if (!strcmp(IDENTIFIER_POINTER (token->u.value), "alignof"))
+		  pedwarn (token->location, OPT_pedantic,
+			   "ISO C++ does not allow %<alignof%> "
+			   "with a non-type");
+
+		return cxx_sizeof_or_alignof_expr (operand, op, true);
+	      }
 	  }
 
 	case RID_NEW:
diff --git a/gcc/testsuite/g++.dg/cpp0x/alignof2.C b/gcc/testsuite/g++.dg/cpp0x/alignof2.C
new file mode 100644
index 0000000..7c5aad3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alignof2.C
@@ -0,0 +1,7 @@ 
+// { dg-do compile }
+// { dg-options "-std=c++0x -pedantic" }
+int main(void)
+{
+  alignof(int); //ok with a type but not with an expression
+  alignof(3);   // { dg-warning "alignof" }
+}