Bug 33537 - [4.2 regression] C++ arguments passed by invisible reference have wrong type
Summary: [4.2 regression] C++ arguments passed by invisible reference have wrong type
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: debug (show other bugs)
Version: 4.2.1
: P2 normal
Target Milestone: 4.3.0
Assignee: Jakub Jelinek
URL:
Keywords: wrong-debug
Depends on:
Blocks:
 
Reported: 2007-09-23 15:18 UTC by Daniel Jacobowitz
Modified: 2009-03-30 22:26 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0
Known to fail: 4.2.5
Last reconfirmed: 2007-10-31 15:58:54


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Jacobowitz 2007-09-23 15:18:36 UTC
Compile the code below using g++ -g, load the object into gdb, and 'ptype foo'.  You will see that the type of foo is 'int (Obj &)'.  If this function is called from GDB, and the called function modifies its argument, it will end up modifying the original copy.  It's supposed to get a temporary copy.

This changed sometime between g++ 3.3 and g++ 4.0.

class Obj {
public:
  Obj ();
  Obj (const Obj &);
  ~Obj ();
  int var[2];
};

int foo (Obj arg)
{
  return arg.var[0] + arg.var[1];
}

int main() { return 0; }

Obj::Obj ()
{
  var[0] = 1;
  var[1] = 2;
}

Obj::Obj (const Obj &obj)
{
  var[0] = obj.var[0];
  var[1] = obj.var[1];
}

Obj::~Obj ()
{

}
Comment 1 Wolfgang Bangerth 2007-09-23 17:07:01 UTC
I get the same output
  (gdb) ptype foo
  type = int (Obj &)
for all compilers I tried here, i.e. gcc versions 2.95, 3.2.3, 3.3.6, 3.4.6
and 4.1.2. This is with gdb 6.5.

On the other hand, when using icc10 and Sun Studio, I get this:
  (gdb) ptype foo
  type = int (Obj)
which appears correct. So I can confirm that this appears to be a bug.

I'm not sure yet about the regression status: Can you re-check whether you
indeed got the correct results with pre-4.0 versions of gcc?

W.
Comment 2 Daniel Jacobowitz 2007-09-23 17:49:40 UTC
I got the correct result on amd64 with g++-3.3 (GCC) 3.3.6 (Debian 1:3.3.6-15).

Comment 3 Pawel Sikora 2007-09-23 18:31:42 UTC
(In reply to comment #0)

> If this function is called from GDB, and the called function modifies
> its argument, it will end up modifying the original copy.
> It's supposed to get a temporary copy.

compiler does the temporary copy. please look at this example:

$ cat 33537.cpp
struct X
{
    X();
    X( X const& );
    int var;
};
int f( X arg )
{
    arg.var = 0;
    return arg.var;
}
int main()
{
    X x;
    f( x );
    return 0;
}

at tree dump we see the temporary object passed to f(X) by reference:

int f(X) (arg)
{
  arg->var = 0;
  return 0;
}
int main() ()
{
  struct X x;
  struct X D.2360;
  __comp_ctor  (&x);
  __comp_ctor  (&D.2360, &x);
  f (&D.2360);
  return 0;
}
Comment 4 Daniel Jacobowitz 2007-09-23 18:40:44 UTC
Subject: Re:  [4.0/4.1/4.2/4.3 regression] C++ arguments
	passed by invisible reference have wrong type

On Sun, Sep 23, 2007 at 06:31:42PM -0000, pluto at agmk dot net wrote:
> > If this function is called from GDB, and the called function modifies
> > its argument, it will end up modifying the original copy.
> > It's supposed to get a temporary copy.
> 
> compiler does the temporary copy. please look at this example:

That's why the bug report said "from GDB".  It's a debug info bug, not
a C++ wrong-code bug.

Comment 5 Jakub Jelinek 2007-10-31 15:58:54 UTC
dwarf2out.c needs to handle DECL_BY_REFERENCE PARM_DECLs (and perhaps also
RESULT_DECLs).
Comment 6 Jakub Jelinek 2007-11-01 10:17:54 UTC
Subject: Bug 33537

Author: jakub
Date: Thu Nov  1 10:17:42 2007
New Revision: 129820

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129820
Log:
	PR debug/33537
	* dwarf2out.c (gen_formal_parameter_die, gen_variable_die,
	gen_decl_die): Use TREE_TYPE (TREE_TYPE (decl)) as type
	rather than TREE_TYPE (decl) if DECL_BY_REFERENCE (decl).

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/dwarf2out.c

Comment 7 Jakub Jelinek 2007-11-01 10:19:17 UTC
Fixed on the trunk so far.
Comment 8 Jan Kratochvil 2007-11-01 17:37:07 UTC
ptype testcase is now in GDB as: gdb/testsuite/gdb.cp/arg-reference.exp 
(GDB still does not create the temporary copy during a call from GDB itself.)
Comment 9 Joseph S. Myers 2008-07-04 22:18:03 UTC
Closing 4.1 branch.
Comment 10 Joseph S. Myers 2009-03-30 22:26:55 UTC
Closing 4.2 branch, fixed in 4.3.