1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // Example from C++ Standard Working Draft N4842, November 2019 Mailing
19 // Adapted for testing.
21 // { dg-options "-std=gnu++2a -include string -include stdexcept" }
22 // { dg-do run { target c++2a } }
24 #include <source_location>
26 #ifndef __cpp_lib_source_location
27 # error "Feature-test macro for source_location missing in <source_location>"
28 #elif __cpp_lib_source_location != 201907L
29 # error "Feature-test macro for source_location has wrong value in <source_location>"
32 #include <string_view>
33 #include <testsuite_hooks.h>
38 std::source_location member
= std::source_location::current();
41 s(std::source_location loc
= std::source_location::current())
42 : member(loc
) // values of member refer to calling function
45 s(int blather
) : // values of member refer to this location
51 f(std::source_location a
= std::source_location::current());
54 f(std::source_location a
)
60 struct srcloc_and_line
62 std::source_location sl
;
66 std::source_location c
= std::source_location::current();
67 return srcloc_and_line
{ f(c
), __LINE__
- 1 };
72 std::source_location main_sl
= std::source_location::current();
73 unsigned main_sl_line
= __LINE__
- 1;
74 std::source_location f_arg_sl
= f(main_sl
);
75 unsigned f_arg_sl_line
= main_sl_line
;
76 auto [g_sl
, g_sl_line
] = g();
77 std::source_location f_sl
= f();
78 unsigned f_sl_line
= __LINE__
- 1;
79 std::source_location h_sl
= h(); // defined in ./srcloc.h
80 s
member_main_sl(main_sl
);
81 s
member_defaulted_sl(1);
83 const unsigned member_sl_line
= __LINE__
- 1;
85 using namespace std::string_view_literals
;
87 std::string_view
main_sl_fn_name(main_sl
.function_name());
88 std::string_view
main_sl_fi_name(main_sl
.file_name());
89 VERIFY(main_sl
.line() == main_sl_line
);
90 // opening paren of call
91 VERIFY(main_sl
.column() == 63);
92 VERIFY(main_sl_fn_name
.ends_with("main()"sv
));
93 VERIFY(main_sl_fi_name
.ends_with("1.cc"sv
));
95 std::string_view
f_arg_sl_fn_name(f_arg_sl
.function_name());
96 std::string_view
f_arg_sl_fi_name(f_arg_sl
.file_name());
97 VERIFY(f_arg_sl
.line() == f_arg_sl_line
);
98 // opening paren of call
99 VERIFY(f_arg_sl
.column() == 63);
100 VERIFY(f_arg_sl_fn_name
.ends_with("main()"sv
));
101 VERIFY(f_arg_sl_fi_name
.ends_with("1.cc"sv
));
103 std::string_view
g_sl_fn_name(g_sl
.function_name());
104 std::string_view
g_sl_fi_name(g_sl
.file_name());
105 VERIFY(g_sl
.line() == g_sl_line
);
106 VERIFY(g_sl
.column() == 57); // opening paren of call
107 VERIFY(g_sl_fn_name
.ends_with("g()"sv
));
108 VERIFY(g_sl_fi_name
.ends_with("1.cc"sv
));
110 std::string_view
h_sl_fn_name(h_sl
.function_name());
111 std::string_view
h_sl_fi_name(h_sl
.file_name());
112 VERIFY(h_sl
.line() == 23);
113 VERIFY(h_sl
.column() == 57); // opening paren of call
114 VERIFY(h_sl_fn_name
.ends_with("h()"sv
));
115 VERIFY(h_sl_fi_name
.ends_with("srcloc.h"sv
));
117 std::string_view
member_main_sl_fn_name(member_main_sl
.member
.function_name());
118 std::string_view
member_main_sl_fi_name(member_main_sl
.member
.file_name());
119 VERIFY(member_main_sl
.member
.line() == main_sl_line
);
120 VERIFY(member_main_sl
.member
.column() == 63);
121 VERIFY(member_main_sl_fn_name
.ends_with("main()"sv
));
122 VERIFY(member_main_sl_fi_name
.ends_with("1.cc"sv
));
124 std::string_view
member_defaulted_sl_fi_name(
125 member_defaulted_sl
.member
.file_name());
126 std::string_view
member_defaulted_sl_fn_name(
127 member_defaulted_sl
.member
.function_name());
128 VERIFY(member_defaulted_sl
.member
.line() == 46);
129 // closing paren of constructor declaration
130 VERIFY(member_defaulted_sl
.member
.column() == 25);
131 VERIFY(member_defaulted_sl_fn_name
.starts_with("s::s(int)"sv
));
132 VERIFY(member_defaulted_sl_fi_name
.ends_with("1.cc"sv
));
134 std::string_view
member_sl_fi_name(
135 member_sl
.member
.file_name());
136 std::string_view
member_sl_fn_name(
137 member_sl
.member
.function_name());
138 VERIFY(member_sl
.member
.line() == member_sl_line
);
139 // closing brace/paren of constructor
140 VERIFY(member_sl
.member
.column() == 19);
141 VERIFY(member_sl_fn_name
.starts_with("int main()"sv
));
142 VERIFY(member_sl_fi_name
.ends_with("1.cc"sv
));
144 std::string_view
f_sl_fi_name(f_sl
.file_name());
145 std::string_view
f_sl_fn_name(f_sl
.function_name());
146 VERIFY(f_sl
.line() == f_sl_line
);
147 // opening paren of call
148 VERIFY(f_sl
.column() == 32);
149 VERIFY(f_sl_fn_name
.ends_with("main()"sv
));
150 VERIFY(f_sl_fi_name
.ends_with("1.cc"sv
));