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-14, clang-15 11 // UNSUPPORTED: apple-clang-14 12 13 #include <source_location> 14 15 #include <cassert> 16 #include <concepts> 17 #include <cstdint> 18 #include <cstring> 19 #include <type_traits> 20 21 #include "test_macros.h" 22 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)"); 26 27 ASSERT_NOEXCEPT(std::source_location()); 28 ASSERT_NOEXCEPT(std::source_location::current()); 29 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. 34 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'); 41 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(); 50 51 // A simple use of current() outside a function. 52 constexpr std::source_location cur = 53 #line 1000 "ss" 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'); 59 60 // and inside a function. 61 int main(int, char**) { 62 auto local = 63 #line 2000 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); 69 70 // Finally, the type should be copy-constructible 71 auto local2 = cur; 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()); 76 77 // and copy-assignable. 78 local = cur; 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()); 83 84 return 0; 85 } 86