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