]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/source_location/consteval.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / source_location / consteval.cc
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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/>.
17
18 // Example from C++ Standard Working Draft N4842, November 2019 Mailing
19 // Adapted for testing.
20
21 // { dg-do compile { target c++20 } }
22
23 #include <source_location>
24 #include <string_view>
25
26 struct s {
27 std::source_location member = std::source_location::current();
28 int other_member = 1;
29
30 constexpr s(std::source_location loc = std::source_location::current())
31 : member(loc) // values of member refer to calling function
32 { }
33
34 constexpr s(int blather) : // values of member refer to this location
35 other_member(blather)
36 { }
37 };
38
39 constexpr std::source_location
40 f(std::source_location a = std::source_location::current())
41 { return a; }
42
43 constexpr auto
44 g()
45 {
46 struct srcloc_and_line
47 {
48 std::source_location sl;
49 unsigned line;
50 };
51
52 std::source_location c = std::source_location::current();
53 return srcloc_and_line{ f(c), __LINE__ - 1 };
54 }
55
56 #include "srcloc.h"
57
58 int main ()
59 {
60 constexpr std::source_location main_sl = std::source_location::current();
61 constexpr unsigned main_sl_line = __LINE__ - 1;
62 constexpr std::source_location f_arg_sl = f(main_sl);
63 constexpr unsigned f_arg_sl_line = main_sl_line;
64 constexpr std::source_location g_sl = g().sl;
65 constexpr unsigned g_sl_line = g().line;
66 constexpr std::source_location f_sl = f();
67 constexpr unsigned f_sl_line = __LINE__ - 1;
68 constexpr std::source_location h_sl = h(); // defined in ./srcloc.h
69 constexpr s member_main_sl(main_sl);
70 constexpr s member_defaulted_sl(1);
71 constexpr s member_sl = s{};
72 constexpr unsigned member_sl_line = __LINE__ - 1;
73
74 using namespace std::string_view_literals;
75
76 static_assert (std::source_location::current ().line () == __LINE__);
77 static_assert (std::source_location::current ().column () == 48);
78
79
80 constexpr std::string_view main_sl_fn_name(main_sl.function_name());
81 constexpr std::string_view main_sl_fi_name(main_sl.file_name());
82 static_assert(main_sl.line() == main_sl_line);
83 // opening paren of call
84 static_assert(main_sl.column() == 73);
85 static_assert(main_sl_fn_name.ends_with("main()"sv));
86 static_assert(main_sl_fi_name.ends_with("consteval.cc"sv));
87
88 constexpr std::string_view f_arg_sl_fn_name(f_arg_sl.function_name());
89 constexpr std::string_view f_arg_sl_fi_name(f_arg_sl.file_name());
90 static_assert(f_arg_sl.line() == f_arg_sl_line);
91 // opening paren of call
92 static_assert(f_arg_sl.column() == 73);
93 static_assert(f_arg_sl_fn_name.ends_with("main()"sv));
94 static_assert(f_arg_sl_fi_name.ends_with("consteval.cc"sv));
95
96 constexpr std::string_view g_sl_fn_name(g_sl.function_name());
97 constexpr std::string_view g_sl_fi_name(g_sl.file_name());
98 static_assert(g_sl.line() == g_sl_line);
99 static_assert(g_sl.column() == 57); // opening paren of call
100 static_assert(g_sl_fn_name.ends_with("g()"sv));
101 static_assert(g_sl_fi_name.ends_with("consteval.cc"sv));
102
103 constexpr std::string_view h_sl_fn_name(h_sl.function_name());
104 constexpr std::string_view h_sl_fi_name(h_sl.file_name());
105 static_assert(h_sl.line() == 23);
106 static_assert(h_sl.column() == 57); // opening paren of call
107 static_assert(h_sl_fn_name.ends_with("h()"sv));
108 static_assert(h_sl_fi_name.ends_with("srcloc.h"sv));
109
110 constexpr std::string_view member_main_sl_fn_name(member_main_sl.member.function_name());
111 constexpr std::string_view member_main_sl_fi_name(member_main_sl.member.file_name());
112 static_assert(member_main_sl.member.line() == main_sl_line);
113 static_assert(member_main_sl.member.column() == 73);
114 static_assert(member_main_sl_fn_name.ends_with("main()"sv));
115 static_assert(member_main_sl_fi_name.ends_with("consteval.cc"sv));
116
117 constexpr std::string_view member_defaulted_sl_fi_name(
118 member_defaulted_sl.member.file_name());
119 constexpr std::string_view member_defaulted_sl_fn_name(
120 member_defaulted_sl.member.function_name());
121 static_assert(member_defaulted_sl.member.line() == 35);
122 // closing paren of constructor declaration
123 static_assert(member_defaulted_sl.member.column() == 25);
124 static_assert(member_defaulted_sl_fn_name.ends_with("s::s(int)"sv));
125 static_assert(member_defaulted_sl_fi_name.ends_with("consteval.cc"sv));
126
127 constexpr std::string_view member_sl_fi_name(
128 member_sl.member.file_name());
129 constexpr std::string_view member_sl_fn_name(
130 member_sl.member.function_name());
131 static_assert(member_sl.member.line() == member_sl_line);
132 // closing brace/paren of constructor
133 static_assert(member_sl.member.column() == 29);
134 static_assert(member_sl_fn_name.starts_with("int main()"sv));
135 static_assert(member_sl_fi_name.ends_with("consteval.cc"sv));
136
137 constexpr std::string_view f_sl_fi_name(f_sl.file_name());
138 constexpr std::string_view f_sl_fn_name(f_sl.function_name());
139 static_assert(f_sl.line() == f_sl_line);
140 // opening paren of call
141 static_assert(f_sl.column() == 42);
142 static_assert(f_sl_fn_name.ends_with("main()"sv));
143 static_assert(f_sl_fi_name.ends_with("consteval.cc"sv));
144
145 return 0;
146 }