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 () { }
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.
I got the correct result on amd64 with g++-3.3 (GCC) 3.3.6 (Debian 1:3.3.6-15).
(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; }
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.
dwarf2out.c needs to handle DECL_BY_REFERENCE PARM_DECLs (and perhaps also RESULT_DECLs).
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
Fixed on the trunk so far.
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.)
Closing 4.1 branch.
Closing 4.2 branch, fixed in 4.3.