1 // Copyright (C) 2017-2021 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 // { dg-do run { target c++14 } }
19 // { dg-require-cstdint "" }
20 // { dg-options "-Wno-address" }
21 
22 #include <experimental/source_location>
23 #include <experimental/string_view>
24 #include <testsuite_hooks.h>
25 
26 using std::experimental::source_location;
27 using std::experimental::string_view;
28 
29 void
test01()30 test01()
31 {
32   constexpr source_location loc = source_location::current();
33   static_assert( loc.line() == 32 );
34   // static_assert( loc.column() == 35 );
35   VERIFY( loc.file_name() == __FILE__ );
36   VERIFY( loc.function_name() == string_view(__FUNCTION__) );
37 }
38 
39 struct S {
40   string_view func;
41   source_location loc = source_location::current();
42 
SS43   S(source_location loc = source_location::current())
44   : func(__FUNCTION__), loc(loc) // values of loc will be from call-site
45   {}
46 
SS47   S(int)
48   : func(__FUNCTION__) // values of loc should be hereabouts
49   {}
50 };
51 
test02()52 void test02()
53 {
54   S s0;
55   VERIFY( s0.loc.line() == 54 );
56   // static_assert( s0.loc.column() == 7 );
57   VERIFY( s0.loc.file_name() == __FILE__ );
58   VERIFY( s0.loc.function_name() == string_view(__FUNCTION__) );
59 
60   S s1(1);
61   VERIFY( s1.loc.line() == 48 );
62   VERIFY( s1.loc.file_name() == __FILE__ );
63   VERIFY( s1.loc.function_name() == s1.func );
64 }
65 
f(source_location a=source_location::current ())66 source_location f(source_location a = source_location::current()) {
67   return a;
68 }
69 
g(string_view & func)70 source_location g(string_view& func) {
71   source_location a = source_location::current();
72   func = __FUNCTION__;
73   return a;
74 }
75 
test03()76 void test03()
77 {
78   auto loc = f(); // f's first argument corresponds to this line of code
79   VERIFY( loc.line() == 78 );
80   // static_assert( loc.column() == 16 );
81   VERIFY( loc.file_name() == __FILE__ );
82   VERIFY( loc.function_name() == string_view(__FUNCTION__) );
83 
84   source_location c = source_location::current();
85   loc = f(c); // f's first argument gets the same values as c, above
86   VERIFY( loc.line() == 84 );
87   // static_assert( loc.column() == 23 );
88   VERIFY( loc.file_name() == __FILE__ );
89   VERIFY( loc.function_name() == string_view(__FUNCTION__) );
90 
91   string_view func;
92   loc = g(func);
93   VERIFY( loc.line() == 71 );
94   // static_assert( loc.column() == 23 );
95   VERIFY( loc.file_name() == __FILE__ );
96   VERIFY( loc.function_name() == func );
97 }
98 
99 void
test04()100 test04()
101 {
102   using std::is_same;
103   using std::uint_least32_t;
104   auto loc = source_location::current();
105   static_assert(is_same<decltype(loc), source_location>::value, "");
106   static_assert(is_same<decltype(loc.line()), uint_least32_t>::value, "");
107   static_assert(is_same<decltype(loc.column()), uint_least32_t>::value, "");
108   static_assert(is_same<decltype(loc.file_name()), const char*>::value, "");
109   static_assert(is_same<decltype(loc.function_name()), const char*>::value, "");
110 }
111 
112 int
main()113 main()
114 {
115   test01();
116   test02();
117   test03();
118   test04();
119 }
120