diff options
author | Jason Merrill <jason@redhat.com> | 2014-10-03 12:48:24 -0400 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2014-10-03 12:48:24 -0400 |
commit | b2cb98ed66787bd0050286d705d2ca9c9d86c41c (patch) | |
tree | 15dd87ec43d36e873c009b43af78a0aa2e569fb3 /gcc | |
parent | e01a49c11b529db0a092c9af935141730a9269ed (diff) | |
download | gcc-b2cb98ed66787bd0050286d705d2ca9c9d86c41c.zip gcc-b2cb98ed66787bd0050286d705d2ca9c9d86c41c.tar.gz gcc-b2cb98ed66787bd0050286d705d2ca9c9d86c41c.tar.bz2 |
decl.c (start_decl): Complain about static/thread_local vars in constexpr function.
* decl.c (start_decl): Complain about static/thread_local vars
in constexpr function.
(check_for_uninitialized_const_var): Also uninitialized vars.
* parser.c (cp_parser_jump_statement): And gotos.
(cp_parser_asm_operand_list): And asm.
(cp_parser_try_block): And try.
* semantics.c (ensure_literal_type_for_constexpr_object): And
non-literal.
From-SVN: r215863
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 9 | ||||
-rw-r--r-- | gcc/cp/decl.c | 21 | ||||
-rw-r--r-- | gcc/cp/parser.c | 13 | ||||
-rw-r--r-- | gcc/cp/semantics.c | 12 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C | 17 |
5 files changed, 66 insertions, 6 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 56c3bdb..7a703aa 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,14 @@ 2014-10-03 Jason Merrill <jason@redhat.com> + * decl.c (start_decl): Complain about static/thread_local vars + in constexpr function. + (check_for_uninitialized_const_var): Also uninitialized vars. + * parser.c (cp_parser_jump_statement): And gotos. + (cp_parser_asm_operand_list): And asm. + (cp_parser_try_block): And try. + * semantics.c (ensure_literal_type_for_constexpr_object): And + non-literal. + * semantics.c (constexpr_fn_retval): Ignore declarations in C++14. (var_in_constexpr_fn): New. (cxx_eval_constant_expression): Look into DECL_INITIAL. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 7856dd8..9c8ecc0 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -4767,6 +4767,16 @@ start_decl (const cp_declarator *declarator, DECL_THIS_STATIC (decl) = 1; } + if (current_function_decl && VAR_P (decl) + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + { + if (DECL_THREAD_LOCAL_P (decl)) + error ("%qD declared %<thread_local%> in %<constexpr%> function", + decl); + else if (TREE_STATIC (decl)) + error ("%qD declared %<static%> in %<constexpr%> function", decl); + } + if (!processing_template_decl && VAR_P (decl)) start_decl_1 (decl, initialized); @@ -5135,15 +5145,20 @@ check_for_uninitialized_const_var (tree decl) 7.1.6 */ if (VAR_P (decl) && TREE_CODE (type) != REFERENCE_TYPE - && CP_TYPE_CONST_P (type) + && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl)) && !DECL_INITIAL (decl)) { tree field = default_init_uninitialized_part (type); if (!field) return; - permerror (DECL_SOURCE_LOCATION (decl), - "uninitialized const %qD", decl); + if (CP_TYPE_CONST_P (type)) + permerror (DECL_SOURCE_LOCATION (decl), + "uninitialized const %qD", decl); + else + error_at (DECL_SOURCE_LOCATION (decl), + "uninitialized variable %qD in %<constexpr%> function", + decl); if (CLASS_TYPE_P (type)) { diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 0050b8d..18cae5b 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -10915,6 +10915,10 @@ cp_parser_jump_statement (cp_parser* parser) break; case RID_GOTO: + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<goto%> in %<constexpr%> function"); + /* Create the goto-statement. */ if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) { @@ -16484,6 +16488,11 @@ cp_parser_asm_definition (cp_parser* parser) /* Look for the `asm' keyword. */ cp_parser_require_keyword (parser, RID_ASM, RT_ASM); + + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<asm%> in %<constexpr%> function"); + /* See if the next token is `volatile'. */ if (cp_parser_allow_gnu_extensions_p (parser) && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) @@ -21441,6 +21450,10 @@ cp_parser_try_block (cp_parser* parser) tree try_block; cp_parser_require_keyword (parser, RID_TRY, RT_TRY); + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<try%> in %<constexpr%> function"); + try_block = begin_try_block (); cp_parser_compound_statement (parser, NULL, true, false); finish_try_block (try_block); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 6c6a5c8..5d1aafc 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7537,7 +7537,9 @@ tree ensure_literal_type_for_constexpr_object (tree decl) { tree type = TREE_TYPE (decl); - if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl) + if (VAR_P (decl) + && (DECL_DECLARED_CONSTEXPR_P (decl) + || var_in_constexpr_fn (decl)) && !processing_template_decl) { tree stype = strip_array_types (type); @@ -7546,8 +7548,12 @@ ensure_literal_type_for_constexpr_object (tree decl) when we try to initialize the variable. */; else if (!literal_type_p (type)) { - error ("the type %qT of constexpr variable %qD is not literal", - type, decl); + if (DECL_DECLARED_CONSTEXPR_P (decl)) + error ("the type %qT of constexpr variable %qD is not literal", + type, decl); + else + error ("variable %qD of non-literal type %qT in %<constexpr%> " + "function", decl, type); explain_non_literal_class (type); return NULL; } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C new file mode 100644 index 0000000..ae3dcc6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++14 } } + +struct A { A(); }; + +constexpr int f(int i) { + static int j = i; // { dg-error "static" } + thread_local int l = i; // { dg-error "thread_local" } + goto foo; // { dg-error "goto" } + foo: + asm("foo"); // { dg-error "asm" } + int k; // { dg-error "uninitialized" } + A a; // { dg-error "non-literal" } + return i; +} + +// FIXME remove +// { dg-prune-output "return" } |