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 1f9e77bc3a3e678da6a1b4f3a7363186cd24b1ee..ac46b7422bcac01f799ad9ece01e699755fb33e8 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 f1e1026314833386516a9f624a630277031a41df..c45947794ead9cc1e05fffccca1dfaef397d58b4 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 055f2bc5f2b2ab62c3a50ca5aded87eb7ba48387..43e452cc1a3e3ddad89caa8a493b6c4ab75e0368 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 b24d09224a963c72fb160357cde0f0278622f51c..676de0118687ac29506eef45df0c89cd9d7ebfa3 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 fa3ba7156984bc333d315aa1d1925dc04db7bbaa..ab088a946b344c5df31139dabb3b7e96cfac3b2d 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 1e899ab17a1d22bbeaba668567dc04cd765ca293..71fbff167a5b9cac747af3dca01d3b66660669d0 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 8ebadb04e875ab89cc9cf0e348025bf389d44a32..824372c346a6ecc04897807f9ee800851cf9498e 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 "" } */
+}