summaryrefslogtreecommitdiff
path: root/libcxx/test/std/language.support/support.srcloc/general.pass.cpp
blob: 761cd66dc87a465ddd43fb1da37f2788582e77b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: clang-15
// UNSUPPORTED: apple-clang-14

#include <source_location>

#include <cassert>
#include <concepts>
#include <cstdint>
#include <cstring>
#include <type_traits>

#include "test_macros.h"

static_assert(std::is_nothrow_move_constructible_v<std::source_location>, "support.srcloc.cons (1.1)");
static_assert(std::is_nothrow_move_assignable_v<std::source_location>, "support.srcloc.cons (1.2)");
static_assert(std::is_nothrow_swappable_v<std::source_location>, "support.srcloc.cons (1.3)");

ASSERT_NOEXCEPT(std::source_location());
ASSERT_NOEXCEPT(std::source_location::current());

// Note: the standard doesn't strictly require the particular values asserted
// here, but does "suggest" them.  Additional tests for details of how the
// implementation of current() chooses which location to report for more complex
// scenarios are in the Clang test-suite, and not replicated here.

// A default-constructed value.
constexpr std::source_location empty;
static_assert(empty.line() == 0);
static_assert(empty.column() == 0);
static_assert(empty.file_name()[0] == '\0');
static_assert(empty.function_name()[0] == '\0');

ASSERT_NOEXCEPT(empty.line());
ASSERT_NOEXCEPT(empty.column());
ASSERT_NOEXCEPT(empty.file_name());
ASSERT_NOEXCEPT(empty.function_name());
std::same_as<std::uint_least32_t> auto line   = empty.line();
std::same_as<std::uint_least32_t> auto column = empty.column();
std::same_as<const char*> auto file      = empty.file_name();
std::same_as<const char*> auto function  = empty.function_name();

// A simple use of current() outside a function.
constexpr std::source_location cur =
#line 1000 "ss"
    std::source_location::current();
static_assert(cur.line() == 1000);
static_assert(cur.column() > 0);
static_assert(cur.file_name()[0] == 's' && cur.file_name()[1] == 's' && cur.file_name()[2] == '\0');
static_assert(cur.function_name()[0] == '\0');

// and inside a function.
int main(int, char**) {
  auto local =
#line 2000
      std::source_location::current();
  assert(strcmp(local.file_name(), "ss") == 0);
  assert(strstr(local.function_name(), "main") != nullptr);
  assert(local.line() == 2000);
  assert(local.column() > 0);

  // Finally, the type should be copy-constructible
  auto local2 = cur;
  assert(strcmp(local2.file_name(), cur.file_name()) == 0);
  assert(strcmp(local2.function_name(), cur.function_name()) == 0);
  assert(local2.line() == cur.line());
  assert(local2.column() == cur.column());

  // and copy-assignable.
  local = cur;
  assert(strcmp(local.file_name(), cur.file_name()) == 0);
  assert(strcmp(local.function_name(), cur.function_name()) == 0);
  assert(local.line() == cur.line());
  assert(local.column() == cur.column());

  return 0;
}