From 079a66801bfae16d282c9814266325569f91180d Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 15 Jan 2019 09:36:43 +0000 Subject: [PATCH] decl.c (start_decl): Improve error location. /cp 2019-01-15 Paolo Carlini * decl.c (start_decl): Improve error location. * decl2.c (grokfield): Likewise. /testsuite 2019-01-15 Paolo Carlini * g++.dg/diagnostic/typedef-initialized.C: New. /cp 2019-01-15 Paolo Carlini * decl.c (grokdeclarator): Move further up the location_t loc declaration and use the location when building a TYPE_DECL for a typedef name. * decl2.c (grokbitfield): Use DECL_SOURCE_LOCATION in the error about an ill-formed bit-field as typedef. /testsuite 2019-01-15 Paolo Carlini * g++.dg/diagnostic/bitfld3.C: New. From-SVN: r267932 --- gcc/cp/ChangeLog | 13 +++++++++++++ gcc/cp/decl.c | 10 ++++++---- gcc/cp/decl2.c | 6 ++++-- gcc/testsuite/ChangeLog | 8 ++++++++ gcc/testsuite/g++.dg/diagnostic/bitfld3.C | 5 +++++ .../g++.dg/diagnostic/typedef-initialized.C | 6 ++++++ 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/bitfld3.C create mode 100644 gcc/testsuite/g++.dg/diagnostic/typedef-initialized.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index a99ddb5313d..35eec34fca5 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,16 @@ +2019-01-15 Paolo Carlini + + * decl.c (start_decl): Improve error location. + * decl2.c (grokfield): Likewise. + +2019-01-15 Paolo Carlini + + * decl.c (grokdeclarator): Move further up the location_t loc + declaration and use the location when building a TYPE_DECL for + a typedef name. + * decl2.c (grokbitfield): Use DECL_SOURCE_LOCATION in the error + about an ill-formed bit-field as typedef. + 2019-01-14 Marek Polacek PR c++/88830 - ICE with abstract class. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 41972aa0955..8e1d12d8feb 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -5059,7 +5059,8 @@ start_decl (const cp_declarator *declarator, if (initialized && TREE_CODE (decl) == TYPE_DECL) { - error ("typedef %qD is initialized (use decltype instead)", decl); + error_at (DECL_SOURCE_LOCATION (decl), + "typedef %qD is initialized (use decltype instead)", decl); return error_mark_node; } @@ -11935,6 +11936,8 @@ grokdeclarator (const cp_declarator *declarator, } } + location_t loc = declarator ? declarator->id_loc : input_location; + /* If this is declaring a typedef name, return a TYPE_DECL. */ if (typedef_p && decl_context != TYPENAME) { @@ -11980,9 +11983,9 @@ grokdeclarator (const cp_declarator *declarator, } if (decl_context == FIELD) - decl = build_lang_decl (TYPE_DECL, unqualified_id, type); + decl = build_lang_decl_loc (loc, TYPE_DECL, unqualified_id, type); else - decl = build_decl (input_location, TYPE_DECL, unqualified_id, type); + decl = build_decl (loc, TYPE_DECL, unqualified_id, type); if (decl_context != FIELD) { @@ -12223,7 +12226,6 @@ grokdeclarator (const cp_declarator *declarator, { tree decl = NULL_TREE; - location_t loc = declarator ? declarator->id_loc : input_location; if (decl_context == PARM) { diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 0869fd30824..5da5beb7784 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -820,7 +820,8 @@ grokfield (const cp_declarator *declarator, if (TREE_CODE (value) == TYPE_DECL && init) { - error ("typedef %qD is initialized (use decltype instead)", value); + error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)), + "typedef %qD is initialized (use decltype instead)", value); init = NULL_TREE; } @@ -1038,7 +1039,8 @@ grokbitfield (const cp_declarator *declarator, if (TREE_CODE (value) == TYPE_DECL) { - error ("cannot declare %qD to be a bit-field type", value); + error_at (DECL_SOURCE_LOCATION (value), + "cannot declare %qD to be a bit-field type", value); return NULL_TREE; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 810c45780a7..999810a1c89 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2019-01-15 Paolo Carlini + + * g++.dg/diagnostic/typedef-initialized.C: New. + +2019-01-15 Paolo Carlini + + * g++.dg/diagnostic/bitfld3.C: New. + 2019-01-15 Jakub Jelinek PR tree-optimization/88775 diff --git a/gcc/testsuite/g++.dg/diagnostic/bitfld3.C b/gcc/testsuite/g++.dg/diagnostic/bitfld3.C new file mode 100644 index 00000000000..516e776d736 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/bitfld3.C @@ -0,0 +1,5 @@ +struct S +{ + typedef int i : 3; // { dg-error "15:cannot declare .i." } + typedef int : 3; // { dg-error "cannot declare" } +}; diff --git a/gcc/testsuite/g++.dg/diagnostic/typedef-initialized.C b/gcc/testsuite/g++.dg/diagnostic/typedef-initialized.C new file mode 100644 index 00000000000..143134bee7e --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/typedef-initialized.C @@ -0,0 +1,6 @@ +struct S +{ + typedef int i __attribute__((unused)) = 1; // { dg-error "15:typedef .i. is initialized" } +}; + +typedef int i __attribute__((unused)) = 1; // { dg-error "13:typedef .i. is initialized" } -- 2.30.2