errors 0.6.3
Loading...
Searching...
No Matches
source_location.hpp
1#pragma once
2
3#if defined(ERRORS_USE_STD_SOURCE_LOCATION)
4#include <source_location>
5#else
6
7#include <cstdint>
8
9#if defined(__clang__) && !defined(__apple_build_version__) && \
10 (__clang_major__ >= 9)
11#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE
12#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION
13#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE
14#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_COLUMN
15#elif defined(__apple_build_version__) && defined(__clang__) && \
16 (__clang_major__ * 10000 + __clang_minor__ * 100 + \
17 __clang_patchlevel__ % 100) >= 110003
18#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE
19#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION
20#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE
21#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_COLUMN
22// GCC
23#elif defined(__GNUC__) && \
24 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
25#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE
26#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION
27#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE
28#define ERRORS_SOURCE_LOCATION_NO_BUILTIN_COLUMN
29// MSVC https://github.com/microsoft/STL/issues/54#issuecomment-616904069 https://learn.microsoft.com/en-us/cpp/overview/compiler-versions?view=msvc-170
30#elif defined(_MSC_VER) && !defined(__clang__) && \
31 !defined(__INTEL_COMPILER) && (_MSC_VER >= 1926)
32#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE
33#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION
34#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE
35#define ERRORS_SOURCE_LOCATION_HAS_BUILTIN_COLUMN
36#endif
37#endif
38
39namespace errors
40{
43#if defined(ERRORS_USE_STD_SOURCE_LOCATION)
44using source_location = std::source_location;
45#else
47 public:
50#if defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE) && \
51 defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION) && \
52 defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE) && \
53 defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_COLUMN)
54 static constexpr source_location
55 current(const char *fileName = __builtin_FILE(),
56 const char *functionName = __builtin_FUNCTION(),
57 const uint_least32_t lineNumber = __builtin_LINE(),
58 const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept
59#elif defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FILE) && \
60 defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_FUNCTION) && \
61 defined(ERRORS_SOURCE_LOCATION_HAS_BUILTIN_LINE) && \
62 defined(ERRORS_SOURCE_LOCATION_NO_BUILTIN_COLUMN)
63 static constexpr source_location
64 current(const char *fileName = __builtin_FILE(),
65 const char *functionName = __builtin_FUNCTION(),
66 const uint_least32_t lineNumber = __builtin_LINE(),
67 const uint_least32_t columnOffset = 0) noexcept
68#else
69 static constexpr source_location
70 current(const char *fileName = "unsupported",
71 const char *functionName = "unsupported",
72 const uint_least32_t lineNumber = 0,
73 const uint_least32_t columnOffset = 0) noexcept
74#endif
75 {
76 return source_location(fileName, functionName, lineNumber,
77 columnOffset);
78 }
79
80 constexpr source_location() noexcept = default;
81
85 constexpr const char *file_name() const noexcept
86 {
87 return file_name_;
88 }
89
92 constexpr const char *function_name() const noexcept
93 {
94 return function_name_;
95 }
96
99 constexpr uint_least32_t line() const noexcept
100 {
101 return line_number;
102 }
103
107 constexpr std::uint_least32_t column() const noexcept
108 {
109 return column_;
110 }
111
112 private:
113 constexpr source_location(const char *file, const char *function,
114 uint_least32_t line,
115 uint_least32_t column) noexcept
116 : file_name_(file)
117 , function_name_(function)
118 , line_number(line)
119 , column_(column)
120 {
121 }
122
123 const char *file_name_ = "";
124 const char *function_name_ = "";
125 std::uint_least32_t line_number{};
126 std::uint_least32_t column_{};
127};
128#endif
129
130}
The source location.
Definition source_location.hpp:46
constexpr const char * function_name() const noexcept
Returns the function name.
Definition source_location.hpp:92
constexpr std::uint_least32_t column() const noexcept
Returns the column number, or 0 if it is not supported by the compiler.
Definition source_location.hpp:107
constexpr const char * file_name() const noexcept
Returns the file name, or "unsupported" if it is not supported by the compiler.
Definition source_location.hpp:85
constexpr uint_least32_t line() const noexcept
Returns the line number.
Definition source_location.hpp:99
static constexpr source_location current(const char *fileName="unsupported", const char *functionName="unsupported", const uint_least32_t lineNumber=0, const uint_least32_t columnOffset=0) noexcept
Returns the current source location.
Definition source_location.hpp:70