1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 #include <source_location>
12 
13 #include <cassert>
14 #include <concepts>
15 #include <cstdint>
16 #include <cstring>
17 #include <type_traits>
18 
19 #include "test_macros.h"
20 
21 static_assert(std::is_nothrow_move_constructible_v<std::source_location>, "support.srcloc.cons (1.1)");
22 static_assert(std::is_nothrow_move_assignable_v<std::source_location>, "support.srcloc.cons (1.2)");
23 static_assert(std::is_nothrow_swappable_v<std::source_location>, "support.srcloc.cons (1.3)");
24 
25 ASSERT_NOEXCEPT(std::source_location());
26 ASSERT_NOEXCEPT(std::source_location::current());
27 
28 // Note: the standard doesn't strictly require the particular values asserted
29 // here, but does "suggest" them.  Additional tests for details of how the
30 // implementation of current() chooses which location to report for more complex
31 // scenarios are in the Clang test-suite, and not replicated here.
32 
33 // A default-constructed value.
34 constexpr std::source_location empty;
35 static_assert(empty.line() == 0);
36 static_assert(empty.column() == 0);
37 static_assert(empty.file_name()[0] == '\0');
38 static_assert(empty.function_name()[0] == '\0');
39 
40 ASSERT_NOEXCEPT(empty.line());
41 ASSERT_NOEXCEPT(empty.column());
42 ASSERT_NOEXCEPT(empty.file_name());
43 ASSERT_NOEXCEPT(empty.function_name());
44 std::same_as<std::uint_least32_t> auto line   = empty.line();
45 std::same_as<std::uint_least32_t> auto column = empty.column();
46 std::same_as<const char*> auto file      = empty.file_name();
47 std::same_as<const char*> auto function  = empty.function_name();
48 
49 // A simple use of current() outside a function.
50 constexpr std::source_location cur =
51 #line 1000 "ss"
52     std::source_location::current();
53 static_assert(cur.line() == 1000);
54 static_assert(cur.column() > 0);
55 static_assert(cur.file_name()[0] == 's' && cur.file_name()[1] == 's' && cur.file_name()[2] == '\0');
56 static_assert(cur.function_name()[0] == '\0');
57 
58 // and inside a function.
main(int,char **)59 int main(int, char**) {
60   auto local =
61 #line 2000
62       std::source_location::current();
63   assert(strcmp(local.file_name(), "ss") == 0);
64   assert(strstr(local.function_name(), "main") != nullptr);
65   assert(local.line() == 2000);
66   assert(local.column() > 0);
67 
68   // Finally, the type should be copy-constructible
69   auto local2 = cur;
70   assert(strcmp(local2.file_name(), cur.file_name()) == 0);
71   assert(strcmp(local2.function_name(), cur.function_name()) == 0);
72   assert(local2.line() == cur.line());
73   assert(local2.column() == cur.column());
74 
75   // and copy-assignable.
76   local = cur;
77   assert(strcmp(local.file_name(), cur.file_name()) == 0);
78   assert(strcmp(local.function_name(), cur.function_name()) == 0);
79   assert(local.line() == cur.line());
80   assert(local.column() == cur.column());
81 
82   return 0;
83 }
84