re PR c++/86993 (assignment of read-only variable error reported at wrong location)
authorJakub Jelinek <jakub@gcc.gnu.org>
Mon, 27 Aug 2018 18:36:23 +0000 (20:36 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 27 Aug 2018 18:36:23 +0000 (20:36 +0200)
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
gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/semantics.c
gcc/cp/typeck.c
gcc/cp/typeck2.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/diagnostic/pr86993.C [new file with mode: 0644]

index 1f9e77b..ac46b74 100644 (file)
@@ -16,7 +16,7 @@
 
 2018-08-27  David Malcolm  <dmalcolm@redhat.com>
 
-       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  <dmalcolm@redhat.com>
 
-       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
 
 2018-08-24  David Malcolm  <dmalcolm@redhat.com>
 
-       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.
index f1e1026..c459477 100644 (file)
@@ -1,6 +1,19 @@
+2018-08-27  Jakub Jelinek  <jakub@redhat.com>
+
+       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  <dmalcolm@redhat.com>
 
-       PR 87091
+       PR c++/87091
        * decl.c (grokdeclarator): Update for conversion of show_caret_p
        to a tri-state.
        * error.c (cp_printer): Likewise.
index 055f2bc..43e452c 100644 (file)
@@ -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);
index b24d092..676de01 100644 (file)
@@ -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)
index fa3ba71..ab088a9 100644 (file)
@@ -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;
     }
 
index 1e899ab..71fbff1 100644 (file)
@@ -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 %<asm%> 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 %<asm%> 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 %<asm%> 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 %<asm%> 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);
 }
-
 \f
 /* Structure that holds information about declarations whose type was
    incomplete and we could not check whether it was abstract or not.  */
index 8ebadb0..824372c 100644 (file)
@@ -1,3 +1,8 @@
+2018-08-27  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/86993
+       * g++.dg/diagnostic/pr86993.C: New test.
+
 2018-08-27  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/86927
@@ -5,7 +10,7 @@
 
 2018-08-27  David Malcolm  <dmalcolm@redhat.com>
 
-       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  <dmalcolm@redhat.com>
 
-       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  <dmalcolm@redhat.com>
 
-       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 (file)
index 0000000..313a489
--- /dev/null
@@ -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 "" } */
+}