1 // Copyright (C) 2020 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" }
22 // { dg-do compile { target c++2a } }
24 #include <source_location>
25 #include <string_view>
28 std::source_location member
= std::source_location::current();
31 constexpr s(std::source_location loc
= std::source_location::current())
32 : member(loc
) // values of member refer to calling function
35 constexpr s(int blather
) : // values of member refer to this location
40 constexpr std::source_location
41 f(std::source_location a
= std::source_location::current())
47 struct srcloc_and_line
49 std::source_location sl
;
53 std::source_location c
= std::source_location::current();
54 return srcloc_and_line
{ f(c
), __LINE__
- 1 };
61 constexpr std::source_location main_sl
= std::source_location::current();
62 constexpr unsigned main_sl_line
= __LINE__
- 1;
63 constexpr std::source_location f_arg_sl
= f(main_sl
);
64 constexpr unsigned f_arg_sl_line
= main_sl_line
;
65 constexpr std::source_location g_sl
= g().sl
;
66 constexpr unsigned g_sl_line
= g().line
;
67 constexpr std::source_location f_sl
= f();
68 constexpr unsigned f_sl_line
= __LINE__
- 1;
69 constexpr std::source_location h_sl
= h(); // defined in ./srcloc.h
70 constexpr s
member_main_sl(main_sl
);
71 constexpr s
member_defaulted_sl(1);
72 constexpr s member_sl
= s
{};
73 constexpr unsigned member_sl_line
= __LINE__
- 1;
75 using namespace std::string_view_literals
;
77 static_assert (std::source_location::current ().line () == __LINE__
);
78 static_assert (std::source_location::current ().column () == 49);
81 constexpr std::string_view
main_sl_fn_name(main_sl
.function_name());
82 constexpr std::string_view
main_sl_fi_name(main_sl
.file_name());
83 static_assert(main_sl
.line() == main_sl_line
);
84 // closing paren of call
85 static_assert(main_sl
.column() == 74);
86 static_assert(main_sl_fn_name
.ends_with("main"sv
));
87 static_assert(main_sl_fi_name
.ends_with("consteval.cc"sv
));
89 constexpr std::string_view
f_arg_sl_fn_name(f_arg_sl
.function_name());
90 constexpr std::string_view
f_arg_sl_fi_name(f_arg_sl
.file_name());
91 static_assert(f_arg_sl
.line() == f_arg_sl_line
);
92 // closing paren of call
93 static_assert(f_arg_sl
.column() == 74);
94 static_assert(f_arg_sl_fn_name
.ends_with("main"sv
));
95 static_assert(f_arg_sl_fi_name
.ends_with("consteval.cc"sv
));
97 constexpr std::string_view
g_sl_fn_name(g_sl
.function_name());
98 constexpr std::string_view
g_sl_fi_name(g_sl
.file_name());
99 static_assert(g_sl
.line() == g_sl_line
);
100 static_assert(g_sl
.column() == 58); // closing paren of call
101 static_assert(g_sl_fn_name
.ends_with("g"sv
));
102 static_assert(g_sl_fi_name
.ends_with("consteval.cc"sv
));
104 constexpr std::string_view
h_sl_fn_name(h_sl
.function_name());
105 constexpr std::string_view
h_sl_fi_name(h_sl
.file_name());
106 static_assert(h_sl
.line() == 23);
107 static_assert(h_sl
.column() == 58); // closing paren of call
108 static_assert(h_sl_fn_name
.ends_with("h"sv
));
109 static_assert(h_sl_fi_name
.ends_with("srcloc.h"sv
));
111 constexpr std::string_view
member_main_sl_fn_name(member_main_sl
.member
.function_name());
112 constexpr std::string_view
member_main_sl_fi_name(member_main_sl
.member
.file_name());
113 static_assert(member_main_sl
.member
.line() == main_sl_line
);
114 static_assert(member_main_sl
.member
.column() == 74);
115 static_assert(member_main_sl_fn_name
.ends_with("main"sv
));
116 static_assert(member_main_sl_fi_name
.ends_with("consteval.cc"sv
));
118 constexpr std::string_view
member_defaulted_sl_fi_name(
119 member_defaulted_sl
.member
.file_name());
120 constexpr std::string_view
member_defaulted_sl_fn_name(
121 member_defaulted_sl
.member
.function_name());
122 static_assert(member_defaulted_sl
.member
.line() == 36);
123 // closing paren of constructor declaration
124 static_assert(member_defaulted_sl
.member
.column() == 25);
126 static_assert(member_defaulted_sl_fn_name
.ends_with("s::s(int)"sv
));
128 static_assert(member_defaulted_sl_fi_name
.ends_with("consteval.cc"sv
));
130 constexpr std::string_view
member_sl_fi_name(
131 member_sl
.member
.file_name());
132 constexpr std::string_view
member_sl_fn_name(
133 member_sl
.member
.function_name());
134 static_assert(member_sl
.member
.line() == member_sl_line
);
135 // closing brace/paren of constructor
136 static_assert(member_sl
.member
.column() == 29);
137 static_assert(member_sl_fn_name
.starts_with("main"sv
));
138 static_assert(member_sl_fi_name
.ends_with("consteval.cc"sv
));
140 constexpr std::string_view
f_sl_fi_name(f_sl
.file_name());
141 constexpr std::string_view
f_sl_fn_name(f_sl
.function_name());
142 static_assert(f_sl
.line() == f_sl_line
);
143 // closing paren of call
144 static_assert(f_sl
.column() == 43);
145 static_assert(f_sl_fn_name
.ends_with("main"sv
));
146 static_assert(f_sl_fi_name
.ends_with("consteval.cc"sv
));