This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Reject __FUNCTION__ as template parameter (PR c++/38647)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>, Mark Mitchell <mark at codesourcery dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Wed, 31 Dec 2008 00:32:01 +0100
- Subject: [C++ PATCH] Reject __FUNCTION__ as template parameter (PR c++/38647)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
As shown on the testcase below, while const char func[] is
rejected as template parameter, __FUNCTION__ is not (instead ICEs later on),
although both should IMHO behave the same, as __FUNCTION__ is supposed
to be locally defined static const char __FUNCTION__[] = "something";
variable. The problem is that for RID_FUNCTION_NAME etc.
cp_parser_primary_expression finish_id_expression isn't called and thus
it is accepted even where vars with the same type aren't accepted.
Fixed thusly, bootstrapped/regtested on x86_64-linux.
Ok for trunk?
2008-12-31 Jakub Jelinek <jakub@redhat.com>
PR c++/38647
* parser.c (cp_parser_primary_expression) <case RID_FUNCTION_NAME>:
Error if integral_constant_expression_p and
!allow_non_integral_constant_expression_p.
* g++.dg/template/function1.C: New test.
--- gcc/cp/parser.c.jj 2008-12-30 11:06:58.000000000 +0100
+++ gcc/cp/parser.c 2008-12-30 13:05:27.000000000 +0100
@@ -3316,6 +3316,15 @@ cp_parser_primary_expression (cp_parser
Consume the token. */
token = cp_lexer_consume_token (parser->lexer);
+
+ if (parser->integral_constant_expression_p
+ && !parser->allow_non_integral_constant_expression_p)
+ {
+ error ("%qD cannot appear in a constant-expression",
+ ridpointers[token->keyword]);
+ return error_mark_node;
+ }
+
/* Look up the name. */
return finish_fname (token->u.value);
--- gcc/testsuite/g++.dg/template/function1.C.jj 2008-12-30 22:30:12.000000000 +0100
+++ gcc/testsuite/g++.dg/template/function1.C 2008-12-30 22:29:38.000000000 +0100
@@ -0,0 +1,15 @@
+// PR c++/38647
+// { dg-do compile }
+
+template<const char *, int> struct A {};
+const char func[] = "abc";
+template<int N> struct A<func, N> {}; // { dg-error "cannot appear|is invalid" }
+
+char a1[1];
+A<a1, 0> a;
+
+template<const char *, int> struct B {};
+template<int N> struct B<__FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid" }
+
+char b1[1];
+B<b1, 0> b;
Jakub