binutils-gdb/gdb/testsuite/gdb.cp/nsusing.cc
Bruno Larsen 68ce1575fc gdb/c++: validate 'using' directives based on the current line
When asking GDB to print a variable from an imported namespace, we only
want to see variables imported in lines that the inferior has already
gone through, as is being tested last in gdb.cp/nsusing.exp. However
with the proposed change to gdb.cp/nsusing.exp, we get the following
failures:

(gdb) PASS: gdb.cp/nsusing.exp: continue to breakpoint: marker10 stop
print x
$9 = 911
(gdb) FAIL: gdb.cp/nsusing.exp: print x, before using statement
next
15        y += x;
(gdb) PASS: gdb.cp/nsusing.exp: using namespace M
print x
$10 = 911
(gdb) PASS: gdb.cp/nsusing.exp: print x, only using M

Showing that the feature wasn't functioning properly, it just so
happened that gcc ordered the namespaces in a convenient way.
This happens because GDB doesn't take into account the line where the
"using namespace" directive is written. So long as it shows up in the
current scope, we assume it is valid.

To fix this, add a new member to struct using_direct, that stores the
line where the directive was written, and a new function that informs if
the using directive is valid already.

Unfortunately, due to a GCC bug, the failure still shows up. Compilers
that set the declaration line of the using directive correctly (such as
Clang) do not show such a bug, so the test includes an XFAIL for gcc
code.

Finally, because the final test of gdb.cp/nsusing.exp has turned into
multiple that all would need XFAILs for older GCCs (<= 4.3), and that
GCC is very old, if it is detected, the test just exits early.

Approved-by: Tom Tromey <tom@tromey.com>
2022-12-21 16:26:44 +01:00

141 lines
1.4 KiB
C++

namespace M
{
int x = 911;
}
namespace N
{
int x = 912;
}
int marker10 ()
{
int y = 1; // marker10 stop
using namespace M;
y += x;
using namespace N;
return y;
}
namespace J
{
int jx = 44;
}
namespace K
{
int marker9 ()
{
//x;
return marker10 ();
}
}
namespace L
{
using namespace J;
int marker8 ()
{
(void) jx;
return K::marker9 ();
}
}
namespace G
{
namespace H
{
int ghx = 6;
}
}
namespace I
{
int marker7 ()
{
using namespace G::H;
(void) ghx;
return L::marker8 ();
}
}
namespace E
{
namespace F
{
int efx = 5;
}
}
using namespace E::F;
int marker6 ()
{
(void) efx;
return I::marker7 ();
}
namespace A
{
int _a = 1;
int x = 2;
}
namespace C
{
int cc = 3;
}
namespace D
{
int dx = 4;
}
using namespace C;
int marker5 ()
{
(void) cc;
return marker6 ();
}
int marker4 ()
{
using D::dx;
return marker5 ();
}
int marker3 ()
{
return marker4 ();
}
int marker2 ()
{
namespace B = A;
(void) B::_a;
return marker3 ();
}
int marker1 ()
{
int total = 0;
{
int b = 1;
{
using namespace A;
int c = 2;
{
int d = 3;
total = _a + b + c + d + marker2 (); // marker1 stop
}
}
}
return marker2 () + total;
}
int main ()
{
using namespace A;
(void) _a;
return marker1 ();
}