65821 complains that in this testcase, setting a breakpoint on main()
ends up stopping at the line with the default argument of foo().  I
think I agree that the confusion from this debugging experience
outweighs the usefulness of being able to step through evaluation of a
default argument, so this patch changes all expressions from a default
argument to have the location of the call, rather than only calls to
the source-location built-ins.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit c45f7db420e310b97fca4a83f661efcb6b0fe943
Author: Jason Merrill <ja...@redhat.com>
Date:   Mon Apr 9 17:51:15 2018 -0400

            PR debug/65821 - wrong location for main().
    
            * call.c (clear_location_r): New.
            (convert_default_arg): Use it.
            * tree.c (bot_manip): Remove builtin_LINE/FILE handling.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index f978ea73f3d..94226d6ea71 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -7296,6 +7296,21 @@ cxx_type_promotes_to (tree type)
   return promote;
 }
 
+/* walk_tree callback to override EXPR_LOCATION in an expression tree.  */
+
+tree
+clear_location_r (tree *tp, int *walk_subtrees, void */*data*/)
+{
+  if (!EXPR_P (*tp))
+    {
+      *walk_subtrees = 0;
+      return NULL_TREE;
+    }
+  if (EXPR_HAS_LOCATION (*tp))
+    SET_EXPR_LOCATION (*tp, input_location);
+  return NULL_TREE;
+}
+
 /* ARG is a default argument expression being passed to a parameter of
    the indicated TYPE, which is a parameter to FN.  PARMNUM is the
    zero-based argument number.  Do any required conversions.  Return
@@ -7360,6 +7375,11 @@ convert_default_arg (tree type, tree arg, tree fn, int parmnum,
   /* We must make a copy of ARG, in case subsequent processing
      alters any part of it.  */
   arg = break_out_target_exprs (arg);
+
+  /* The use of a default argument has the location of the call, not where it
+     was originally written.  */
+  cp_walk_tree_without_duplicates (&arg, clear_location_r, NULL);
+
   arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
 				    ICR_DEFAULT_ARGUMENT, fn, parmnum,
 				    complain);
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index d0835cfaa29..18e7793b869 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -2992,22 +2992,7 @@ bot_manip (tree* tp, int* walk_subtrees, void* data)
   /* Make a copy of this node.  */
   t = copy_tree_r (tp, walk_subtrees, NULL);
   if (TREE_CODE (*tp) == CALL_EXPR)
-    {
-      set_flags_from_callee (*tp);
-
-      /* builtin_LINE and builtin_FILE get the location where the default
-	 argument is expanded, not where the call was written.  */
-      tree callee = get_callee_fndecl (*tp);
-      if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
-	switch (DECL_FUNCTION_CODE (callee))
-	  {
-	  case BUILT_IN_FILE:
-	  case BUILT_IN_LINE:
-	    SET_EXPR_LOCATION (*tp, input_location);
-	  default:
-	    break;
-	  }
-    }
+    set_flags_from_callee (*tp);
   return t;
 }
 
diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/default-arg1.C b/gcc/testsuite/g++.dg/debug/dwarf2/default-arg1.C
new file mode 100644
index 00000000000..d8edffe1a8c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/dwarf2/default-arg1.C
@@ -0,0 +1,14 @@
+// PR c++/65821
+// { dg-options "-gdwarf-2 -dA" }
+
+int b = 12;
+
+inline void foo(const int &x = (b+3))
+{
+  b = x;
+}
+
+int main()
+{
+  foo();	      // { dg-final { scan-assembler-not "default-arg1.C:6" } }
+}

Reply via email to