Debian Codegraph @

gcc/testsuite/g++.dg/ext/attr-no-dangling1.C

raw
1 // { dg-do compile { target c++11 } }
2 // { dg-options "-Wdangling-reference" }
3
4 int g = 42;
5
6 struct [[gnu::no_dangling]] A {
7 ~A();
8 int *i;
9 int &foo() { return *i; }
10 };
11
12 struct A2 {
13 ~A2();
14 int *i;
15 [[gnu::no_dangling]] int &foo() { return *i; }
16 [[gnu::no_dangling]] static int &bar (const int &) { return *&g; }
17 };
18
19 union [[gnu::no_dangling]] U { };
20
21 A a() { return A{&g}; }
22 A2 a2() { return A2{&g}; }
23
24 class X { };
25 const X x1;
26 const X x2;
27
28 [[gnu::no_dangling]] const X& get(const int& i)
29 {
30 return i == 0 ? x1 : x2;
31 }
32
33 void
34 test ()
35 {
36 [[maybe_unused]] const X& x = get (10); // { dg-bogus "dangling" }
37 [[maybe_unused]] const int &i = a().foo(); // { dg-bogus "dangling" }
38 [[maybe_unused]] const int &j = a2().foo(); // { dg-bogus "dangling" }
39 [[maybe_unused]] const int &k = a2().bar(10); // { dg-bogus "dangling" }
40 }