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
21static_assert(std::is_nothrow_move_constructible_v<std::source_location>, "support.srcloc.cons (1.1)");
22static_assert(std::is_nothrow_move_assignable_v<std::source_location>, "support.srcloc.cons (1.2)");
23static_assert(std::is_nothrow_swappable_v<std::source_location>, "support.srcloc.cons (1.3)");
24
25ASSERT_NOEXCEPT(std::source_location());
26ASSERT_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.
34constexpr std::source_location empty;
35static_assert(empty.line() == 0);
36static_assert(empty.column() == 0);
37static_assert(empty.file_name()[0] == '\0');
38static_assert(empty.function_name()[0] == '\0');
39
40ASSERT_NOEXCEPT(empty.line());
41ASSERT_NOEXCEPT(empty.column());
42ASSERT_NOEXCEPT(empty.file_name());
43ASSERT_NOEXCEPT(empty.function_name());
44std::same_as<std::uint_least32_t> auto line = empty.line();
45std::same_as<std::uint_least32_t> auto column = empty.column();
46std::same_as<const char*> auto file = empty.file_name();
47std::same_as<const char*> auto function = empty.function_name();
48
49// A simple use of current() outside a function.
50constexpr std::source_location cur =
51#line 1000 "ss"
52 std::source_location::current();
53static_assert(cur.line() == 1000);
54static_assert(cur.column() > 0);
55static_assert(cur.file_name()[0] == 's' && cur.file_name()[1] == 's' && cur.file_name()[2] == '\0');
56static_assert(cur.function_name()[0] == '\0');
57
58// and inside a function.
59int 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

source code of libcxx/test/std/language.support/support.srcloc/general.pass.cpp