1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: clang-15
11 // UNSUPPORTED: apple-clang-14
13 #include <source_location>
19 #include <type_traits>
21 #include "test_macros.h"
23 static_assert(std::is_nothrow_move_constructible_v
<std::source_location
>, "support.srcloc.cons (1.1)");
24 static_assert(std::is_nothrow_move_assignable_v
<std::source_location
>, "support.srcloc.cons (1.2)");
25 static_assert(std::is_nothrow_swappable_v
<std::source_location
>, "support.srcloc.cons (1.3)");
27 ASSERT_NOEXCEPT(std::source_location());
28 ASSERT_NOEXCEPT(std::source_location::current());
30 // Note: the standard doesn't strictly require the particular values asserted
31 // here, but does "suggest" them. Additional tests for details of how the
32 // implementation of current() chooses which location to report for more complex
33 // scenarios are in the Clang test-suite, and not replicated here.
35 // A default-constructed value.
36 constexpr std::source_location empty
;
37 static_assert(empty
.line() == 0);
38 static_assert(empty
.column() == 0);
39 static_assert(empty
.file_name()[0] == '\0');
40 static_assert(empty
.function_name()[0] == '\0');
42 ASSERT_NOEXCEPT(empty
.line());
43 ASSERT_NOEXCEPT(empty
.column());
44 ASSERT_NOEXCEPT(empty
.file_name());
45 ASSERT_NOEXCEPT(empty
.function_name());
46 std::same_as
<std::uint_least32_t> auto line
= empty
.line();
47 std::same_as
<std::uint_least32_t> auto column
= empty
.column();
48 std::same_as
<const char*> auto file
= empty
.file_name();
49 std::same_as
<const char*> auto function
= empty
.function_name();
51 // A simple use of current() outside a function.
52 constexpr std::source_location cur
=
54 std::source_location::current();
55 static_assert(cur
.line() == 1000);
56 static_assert(cur
.column() > 0);
57 static_assert(cur
.file_name()[0] == 's' && cur
.file_name()[1] == 's' && cur
.file_name()[2] == '\0');
58 static_assert(cur
.function_name()[0] == '\0');
60 // and inside a function.
61 int main(int, char**) {
64 std::source_location::current();
65 assert(strcmp(local
.file_name(), "ss") == 0);
66 assert(strstr(local
.function_name(), "main") != nullptr);
67 assert(local
.line() == 2000);
68 assert(local
.column() > 0);
70 // Finally, the type should be copy-constructible
72 assert(strcmp(local2
.file_name(), cur
.file_name()) == 0);
73 assert(strcmp(local2
.function_name(), cur
.function_name()) == 0);
74 assert(local2
.line() == cur
.line());
75 assert(local2
.column() == cur
.column());
77 // and copy-assignable.
79 assert(strcmp(local
.file_name(), cur
.file_name()) == 0);
80 assert(strcmp(local
.function_name(), cur
.function_name()) == 0);
81 assert(local
.line() == cur
.line());
82 assert(local
.column() == cur
.column());