]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/source_location/consteval.cc
libtdc++: Define std::source_location for C++20
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / source_location / consteval.cc
1 // Copyright (C) 2020 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 "-std=gnu++2a" }
22 // { dg-do compile { target c++2a } }
23
24 #include <source_location>
25 #include <string_view>
26
27 struct s {
28 std::source_location member = std::source_location::current();
29 int other_member = 1;
30
31 constexpr s(std::source_location loc = std::source_location::current())
32 : member(loc) // values of member refer to calling function
33 { }
34
35 constexpr s(int blather) : // values of member refer to this location
36 other_member(blather)
37 { }
38 };
39
40 constexpr std::source_location
41 f(std::source_location a = std::source_location::current())
42 { return a; }
43
44 constexpr auto
45 g()
46 {
47 struct srcloc_and_line
48 {
49 std::source_location sl;
50 unsigned line;
51 };
52
53 std::source_location c = std::source_location::current();
54 return srcloc_and_line{ f(c), __LINE__ - 1 };
55 }
56
57 #include "srcloc.h"
58
59 int main ()
60 {
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;
74
75 using namespace std::string_view_literals;
76
77 static_assert (std::source_location::current ().line () == __LINE__);
78 static_assert (std::source_location::current ().column () == 49);
79
80
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));
88
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));
96
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));
103
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));
110
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));
117
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);
125 #if 0
126 static_assert(member_defaulted_sl_fn_name.ends_with("s::s(int)"sv));
127 #endif
128 static_assert(member_defaulted_sl_fi_name.ends_with("consteval.cc"sv));
129
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));
139
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));
147
148 return 0;
149 }