Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/srcloc16.C

raw
1 // { dg-do run { target c++20 } }
2
3 namespace std {
4 struct source_location {
5 struct __impl {
6 const char *_M_file_name;
7 const char *_M_function_name;
8 unsigned int _M_line, _M_column;
9 };
10 const __impl *__ptr;
11 constexpr source_location () : __ptr (nullptr) {}
12 static consteval source_location
13 current (const void *__p = __builtin_source_location ()) {
14 source_location __ret;
15 __ret.__ptr = static_cast <const __impl *> (__p);
16 return __ret;
17 }
18 constexpr const char *file_name () const {
19 return __ptr ? __ptr->_M_file_name : "";
20 }
21 constexpr const char *function_name () const {
22 return __ptr ? __ptr->_M_function_name : "";
23 }
24 constexpr unsigned line () const {
25 return __ptr ? __ptr->_M_line : 0;
26 }
27 constexpr unsigned column () const {
28 return __ptr ? __ptr->_M_column : 0;
29 }
30 };
31 }
32
33 using namespace std;
34
35 struct S
36 {
37 source_location a = source_location::current ();
38 source_location b = source_location::current ();
39 source_location c = source_location ();
40 constexpr S () { c = source_location::current (); }
41 };
42
43 struct T
44 {
45 int t;
46 source_location u = source_location::current ();
47 int v = __builtin_LINE ();
48 };
49
50 constexpr S s;
51 constexpr T t = { 1 };
52
53 constexpr bool
54 cmp (const char *p, const char *q)
55 {
56 for (; *p && *q; p++, q++)
57 if (*p != *q)
58 return true;
59 return *p || *q;
60 }
61
62 constexpr bool
63 foo ()
64 {
65 T u = { 2 };
66 source_location v = source_location::current ();
67 if (cmp (s.a.file_name (), s.c.file_name ())
68 || cmp (s.b.file_name (), s.c.file_name ())
69 || cmp (t.u.file_name (), s.c.file_name ())
70 || cmp (u.u.file_name (), s.c.file_name ())
71 || cmp (v.file_name (), s.c.file_name ())
72 || cmp (s.a.function_name (), s.c.function_name ())
73 || cmp (s.b.function_name (), s.c.function_name ())
74 || cmp (t.u.function_name (), "")
75 || cmp (u.u.function_name (), v.function_name ())
76 || s.a.line () != s.c.line ()
77 || s.b.line () != s.c.line ()
78 || t.u.line () != t.v
79 || u.u.line () + 1 != v.line ()
80 || s.a.column () != 18
81 || s.b.column () != 18
82 || s.c.column () != 49
83 || t.u.column () != 21
84 || u.u.column () != 13
85 || v.column () != 48)
86 return false;
87 return true;
88 }
89
90 static_assert (foo ());
91
92 int
93 main ()
94 {
95 if (!foo ())
96 __builtin_abort ();
97 }