From d4714a1b5eb458fb233024ef16b0dda6e712d632 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 27 Aug 2018 20:36:23 +0200 Subject: [PATCH] re PR c++/86993 (assignment of read-only variable error reported at wrong location) PR c++/86993 * cp-tree.h (cxx_readonly_error): Add location_t argument. * typeck2.c (cxx_readonly_error): Add LOC argument, pass it to ERROR_FOR_ASSIGNMENT macro and readonly_error. Add LOC argument to ERROR_FOR_ASSIGNMENT macro, use error_at instead of error and pass LOC to it. Formatting fixes. * typeck.c (cp_build_unary_op): Pass location to cxx_readonly_error. (cp_build_modify_expr): Pass loc to cxx_readonly_error. * semantics.c (finish_asm_stmt): Pass input_location to cxx_readonly_error. * g++.dg/diagnostic/pr86993.C: New test. From-SVN: r263891 --- gcc/ChangeLog | 6 ++--- gcc/cp/ChangeLog | 15 ++++++++++- gcc/cp/cp-tree.h | 3 ++- gcc/cp/semantics.c | 2 +- gcc/cp/typeck.c | 9 ++++--- gcc/cp/typeck2.c | 45 +++++++++++++------------------ gcc/testsuite/ChangeLog | 11 +++++--- gcc/testsuite/g++.dg/diagnostic/pr86993.C | 13 +++++++++ 8 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/pr86993.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1f9e77b..ac46b74 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -16,7 +16,7 @@ 2018-08-27 David Malcolm - PR 87091 + PR c++/87091 * diagnostic-show-locus.c (class layout_range): Update for conversion of show_caret_p to a tri-state. (layout_range::layout_range): Likewise. @@ -44,7 +44,7 @@ 2018-08-27 David Malcolm - PR 87091 + PR c++/87091 * diagnostic-show-locus.c (get_line_span_for_fixit_hint): Show the line above for line-insertion fix-it hints. (selftest::test_fixit_insert_containing_newline): Update the @@ -369,7 +369,7 @@ 2018-08-24 David Malcolm - PR 87091 + PR c++/87091 * diagnostic-show-locus.c (layout::layout): Ensure the margin is wide enough for jumps in the line-numbering to be visible. (layout::print_gap_in_line_numbering): New member function. diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f1e1026..c459477 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,6 +1,19 @@ +2018-08-27 Jakub Jelinek + + PR c++/86993 + * cp-tree.h (cxx_readonly_error): Add location_t argument. + * typeck2.c (cxx_readonly_error): Add LOC argument, pass it to + ERROR_FOR_ASSIGNMENT macro and readonly_error. Add LOC argument + to ERROR_FOR_ASSIGNMENT macro, use error_at instead of error and + pass LOC to it. Formatting fixes. + * typeck.c (cp_build_unary_op): Pass location to cxx_readonly_error. + (cp_build_modify_expr): Pass loc to cxx_readonly_error. + * semantics.c (finish_asm_stmt): Pass input_location to + cxx_readonly_error. + 2018-08-27 David Malcolm - PR 87091 + PR c++/87091 * decl.c (grokdeclarator): Update for conversion of show_caret_p to a tri-state. * error.c (cp_printer): Likewise. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 055f2bc..43e452c 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7388,7 +7388,8 @@ cxx_incomplete_type_error (const_tree value, const_tree type) extern void cxx_incomplete_type_inform (const_tree); extern tree error_not_base_type (tree, tree); extern tree binfo_or_else (tree, tree); -extern void cxx_readonly_error (tree, enum lvalue_use); +extern void cxx_readonly_error (location_t, tree, + enum lvalue_use); extern void complete_type_check_abstract (tree); extern int abstract_virtuals_error (tree, tree); extern int abstract_virtuals_error (abstract_class_use, tree); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index b24d0922..676de01 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -1532,7 +1532,7 @@ finish_asm_stmt (int volatile_p, tree string, tree output_operands, effectively const. */ || (CLASS_TYPE_P (TREE_TYPE (operand)) && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand))))) - cxx_readonly_error (operand, lv_asm); + cxx_readonly_error (input_location, operand, lv_asm); tree *op = &operand; while (TREE_CODE (*op) == COMPOUND_EXPR) diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index fa3ba71..ab088a9 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6228,9 +6228,10 @@ cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert, || TREE_READONLY (arg)) { if (complain & tf_error) - cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR - || code == POSTINCREMENT_EXPR) - ? lv_increment : lv_decrement)); + cxx_readonly_error (location, arg, + ((code == PREINCREMENT_EXPR + || code == POSTINCREMENT_EXPR) + ? lv_increment : lv_decrement)); else return error_mark_node; } @@ -8159,7 +8160,7 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, && C_TYPE_FIELDS_READONLY (lhstype)))) { if (complain & tf_error) - cxx_readonly_error (lhs, lv_assign); + cxx_readonly_error (loc, lhs, lv_assign); return error_mark_node; } diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 1e899ab..71fbff1 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -67,28 +67,28 @@ binfo_or_else (tree base, tree type) value may not be changed thereafter. */ void -cxx_readonly_error (tree arg, enum lvalue_use errstring) +cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring) { /* This macro is used to emit diagnostics to ensure that all format strings are complete sentences, visible to gettext and checked at compile time. */ -#define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \ +#define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \ do { \ switch (errstring) \ { \ case lv_assign: \ - error(AS, ARG); \ + error_at (LOC, AS, ARG); \ break; \ case lv_asm: \ - error(ASM, ARG); \ + error_at (LOC, ASM, ARG); \ break; \ case lv_increment: \ - error (IN, ARG); \ + error_at (LOC, IN, ARG); \ break; \ - case lv_decrement: \ - error (DE, ARG); \ + case lv_decrement: \ + error_at (LOC, DE, ARG); \ break; \ default: \ gcc_unreachable (); \ @@ -101,32 +101,25 @@ cxx_readonly_error (tree arg, enum lvalue_use errstring) && DECL_LANG_SPECIFIC (arg) && DECL_IN_AGGR_P (arg) && !TREE_STATIC (arg)) - ERROR_FOR_ASSIGNMENT (G_("assignment of " - "constant field %qD"), - G_("constant field %qD " - "used as % output"), - G_("increment of " - "constant field %qD"), - G_("decrement of " - "constant field %qD"), + ERROR_FOR_ASSIGNMENT (loc, + G_("assignment of constant field %qD"), + G_("constant field %qD used as % output"), + G_("increment of constant field %qD"), + G_("decrement of constant field %qD"), arg); else if (INDIRECT_REF_P (arg) && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0))) && (VAR_P (TREE_OPERAND (arg, 0)) || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) - ERROR_FOR_ASSIGNMENT (G_("assignment of " - "read-only reference %qD"), - G_("read-only reference %qD " - "used as % output"), - G_("increment of " - "read-only reference %qD"), - G_("decrement of " - "read-only reference %qD"), - TREE_OPERAND (arg, 0)); + ERROR_FOR_ASSIGNMENT (loc, + G_("assignment of read-only reference %qD"), + G_("read-only reference %qD used as % output"), + G_("increment of read-only reference %qD"), + G_("decrement of read-only reference %qD"), + TREE_OPERAND (arg, 0)); else - readonly_error (input_location, arg, errstring); + readonly_error (loc, arg, errstring); } - /* Structure that holds information about declarations whose type was incomplete and we could not check whether it was abstract or not. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8ebadb0..824372c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-08-27 Jakub Jelinek + + PR c++/86993 + * g++.dg/diagnostic/pr86993.C: New test. + 2018-08-27 Richard Biener PR tree-optimization/86927 @@ -5,7 +10,7 @@ 2018-08-27 David Malcolm - PR 87091 + PR c++/87091 * gcc.dg/empty.h: New file. * gcc.dg/fixits-pr84852-1.c: Update for move of fix-it hint to top of file and removal of redundant second printing of warning @@ -18,7 +23,7 @@ 2018-08-27 David Malcolm - PR 87091 + PR c++/87091 * g++.dg/pr85523.C: Extend expected output to show line before line-insertion fix-it hint. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c @@ -88,7 +93,7 @@ 2018-08-24 David Malcolm - PR 87091 + PR c++/87091 * gcc.dg/missing-header-fixit-3.c: Update for changes to how line spans are printed with -fdiagnostics-show-line-numbers. diff --git a/gcc/testsuite/g++.dg/diagnostic/pr86993.C b/gcc/testsuite/g++.dg/diagnostic/pr86993.C new file mode 100644 index 0000000..313a489 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/pr86993.C @@ -0,0 +1,13 @@ +// PR c++/86993 +// { dg-options "-fdiagnostics-show-caret" } + +int +main () +{ + const int i = 5; // { dg-error "assignment of read-only variable 'i'" "" { target *-*-* } .+1 } + i = 5 + 6; +/* { dg-begin-multiline-output "" } + i = 5 + 6; + ~~^~~~~~~ + { dg-end-multiline-output "" } */ +} -- 2.7.4