Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/srcloc17.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 template <int N>
36 constexpr source_location
37 foo (const source_location x = source_location::current ())
38 {
39 return x;
40 }
41
42 template <int N>
43 struct S {
44 const char *func;
45 unsigned line = 0;
46 source_location loc = source_location::current ();
47
48 constexpr S (int l, source_location loc = source_location::current ())
49 : func(__PRETTY_FUNCTION__), line(l), loc(loc)
50 {}
51
52 constexpr S (double)
53 : func(__PRETTY_FUNCTION__), line(__LINE__)
54 // ^ column 45
55 {}
56 };
57
58 constexpr bool
59 cmp (const char *p, const char *q)
60 {
61 for (; *p && *q; p++, q++)
62 if (*p != *q)
63 return true;
64 return *p || *q;
65 }
66
67 template <int N>
68 constexpr bool
69 bar ()
70 {
71 int line = __LINE__;
72 source_location a = foo<N> ();
73 source_location b = source_location::current ();
74 source_location c = foo<N> ();
75 // ^ column 30
76 // ^ column 48
77 const source_location *d[3] = { &a, &b, &c };
78 const char *file1 = __FILE__;
79 const char *function1 = __PRETTY_FUNCTION__;
80 for (int j = 0; j < 3; j++)
81 {
82 int i= 0;
83 if (cmp (d[j]->file_name (), file1))
84 return false;
85 if (cmp (d[j]->function_name (), function1))
86 return false;
87 if (d[j]->line () != line + j + 1)
88 return false;
89 if (d[j]->column () != (j == 1 ? 48 : 30))
90 return false;
91 }
92
93 S<N> e (__LINE__);
94 // ^ column 8
95 S<N> f = 1.0;
96 if (cmp (e.loc.file_name (), file1))
97 return false;
98 if (cmp (f.loc.file_name (), file1))
99 return false;
100 if (cmp (e.loc.function_name (), function1))
101 return false;
102 if (cmp (f.loc.function_name (), f.func))
103 return false;
104 if (e.loc.line () != e.line)
105 return false;
106 if (f.loc.line () != f.line)
107 return false;
108 if (e.loc.column () != 8)
109 return false;
110 if (f.loc.column () != 45)
111 return false;
112 return true;
113 }
114
115 static_assert (bar<0> ());
116
117 int
118 main ()
119 {
120 if (!bar<0> ())
121 __builtin_abort ();
122 }