Debian Codegraph @

libstdc++-v3/testsuite/18_support/source_location/1.cc

raw
1 // Copyright (C) 2020-2025 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-options "-include string -include stdexcept" }
22 // { dg-do run { target c++20 } }
23 // { dg-add-options no_pch }
24
25 #include <source_location>
26
27 #ifndef __cpp_lib_source_location
28 # error "Feature-test macro for source_location missing in <source_location>"
29 #elif __cpp_lib_source_location != 201907L
30 # error "Feature-test macro for source_location has wrong value in <source_location>"
31 #endif
32
33 #include <string_view>
34 #include <testsuite_hooks.h>
35 #include "srcloc.h"
36
37
38 struct s {
39 std::source_location member = std::source_location::current();
40 int other_member = 1;
41
42 s(std::source_location loc = std::source_location::current())
43 : member(loc) // values of member refer to calling function
44 { }
45
46 s(int blather) : // values of member refer to this location
47 other_member(blather)
48 { }
49 };
50
51 std::source_location
52 f(std::source_location a = std::source_location::current());
53
54 std::source_location
55 f(std::source_location a)
56 { return a; }
57
58 auto
59 g()
60 {
61 struct srcloc_and_line
62 {
63 std::source_location sl;
64 unsigned line;
65 };
66
67 std::source_location c = std::source_location::current();
68 return srcloc_and_line{ f(c), __LINE__ - 1 };
69 }
70
71 int main ()
72 {
73 std::source_location main_sl = std::source_location::current();
74 unsigned main_sl_line = __LINE__ - 1;
75 std::source_location f_arg_sl = f(main_sl);
76 unsigned f_arg_sl_line = main_sl_line;
77 auto [g_sl, g_sl_line] = g();
78 std::source_location f_sl = f();
79 unsigned f_sl_line = __LINE__ - 1;
80 std::source_location h_sl = h(); // defined in ./srcloc.h
81 s member_main_sl(main_sl);
82 s member_defaulted_sl(1);
83 s member_sl = s{};
84 const unsigned member_sl_line = __LINE__ - 1;
85
86 using namespace std::string_view_literals;
87
88 std::string_view main_sl_fn_name(main_sl.function_name());
89 std::string_view main_sl_fi_name(main_sl.file_name());
90 VERIFY(main_sl.line() == main_sl_line);
91 // opening paren of call
92 VERIFY(main_sl.column() == 63);
93 VERIFY(main_sl_fn_name.ends_with("main()"sv));
94 VERIFY(main_sl_fi_name.ends_with("1.cc"sv));
95
96 std::string_view f_arg_sl_fn_name(f_arg_sl.function_name());
97 std::string_view f_arg_sl_fi_name(f_arg_sl.file_name());
98 VERIFY(f_arg_sl.line() == f_arg_sl_line);
99 // opening paren of call
100 VERIFY(f_arg_sl.column() == 63);
101 VERIFY(f_arg_sl_fn_name.ends_with("main()"sv));
102 VERIFY(f_arg_sl_fi_name.ends_with("1.cc"sv));
103
104 std::string_view g_sl_fn_name(g_sl.function_name());
105 std::string_view g_sl_fi_name(g_sl.file_name());
106 VERIFY(g_sl.line() == g_sl_line);
107 VERIFY(g_sl.column() == 57); // opening paren of call
108 VERIFY(g_sl_fn_name.ends_with("g()"sv));
109 VERIFY(g_sl_fi_name.ends_with("1.cc"sv));
110
111 std::string_view h_sl_fn_name(h_sl.function_name());
112 std::string_view h_sl_fi_name(h_sl.file_name());
113 VERIFY(h_sl.line() == 23);
114 VERIFY(h_sl.column() == 57); // opening paren of call
115 VERIFY(h_sl_fn_name.ends_with("h()"sv));
116 VERIFY(h_sl_fi_name.ends_with("srcloc.h"sv));
117
118 std::string_view member_main_sl_fn_name(member_main_sl.member.function_name());
119 std::string_view member_main_sl_fi_name(member_main_sl.member.file_name());
120 VERIFY(member_main_sl.member.line() == main_sl_line);
121 VERIFY(member_main_sl.member.column() == 63);
122 VERIFY(member_main_sl_fn_name.ends_with("main()"sv));
123 VERIFY(member_main_sl_fi_name.ends_with("1.cc"sv));
124
125 std::string_view member_defaulted_sl_fi_name(
126 member_defaulted_sl.member.file_name());
127 std::string_view member_defaulted_sl_fn_name(
128 member_defaulted_sl.member.function_name());
129 VERIFY(member_defaulted_sl.member.line() == 47);
130 // closing paren of constructor declaration
131 VERIFY(member_defaulted_sl.member.column() == 25);
132 VERIFY(member_defaulted_sl_fn_name.starts_with("s::s(int)"sv));
133 VERIFY(member_defaulted_sl_fi_name.ends_with("1.cc"sv));
134
135 std::string_view member_sl_fi_name(
136 member_sl.member.file_name());
137 std::string_view member_sl_fn_name(
138 member_sl.member.function_name());
139 VERIFY(member_sl.member.line() == member_sl_line);
140 // closing brace/paren of constructor
141 VERIFY(member_sl.member.column() == 19);
142 VERIFY(member_sl_fn_name.starts_with("int main()"sv));
143 VERIFY(member_sl_fi_name.ends_with("1.cc"sv));
144
145 std::string_view f_sl_fi_name(f_sl.file_name());
146 std::string_view f_sl_fn_name(f_sl.function_name());
147 VERIFY(f_sl.line() == f_sl_line);
148 // opening paren of call
149 VERIFY(f_sl.column() == 32);
150 VERIFY(f_sl_fn_name.ends_with("main()"sv));
151 VERIFY(f_sl_fi_name.ends_with("1.cc"sv));
152
153 return 0;
154 }