Coverage Report

Created: 2025-06-13 06:26

/src/boost/boost/assert/source_location.hpp
Line
Count
Source (jump to first uncovered line)
1
#ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
2
#define BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
3
4
// http://www.boost.org/libs/assert
5
//
6
// Copyright 2019, 2021 Peter Dimov
7
// Distributed under the Boost Software License, Version 1.0.
8
// http://www.boost.org/LICENSE_1_0.txt
9
10
#include <boost/config.hpp>
11
#include <boost/cstdint.hpp>
12
#include <string>
13
#include <cstdio>
14
#include <cstring>
15
16
#if !defined(BOOST_NO_IOSTREAM)
17
#include <iosfwd>
18
#endif
19
20
#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
21
# include <source_location>
22
#endif
23
24
namespace boost
25
{
26
27
struct source_location
28
{
29
private:
30
31
    char const * file_;
32
    char const * function_;
33
    boost::uint_least32_t line_;
34
    boost::uint_least32_t column_;
35
36
public:
37
38
    BOOST_CONSTEXPR source_location() BOOST_NOEXCEPT: file_( "" ), function_( "" ), line_( 0 ), column_( 0 )
39
    {
40
    }
41
42
163
    BOOST_CONSTEXPR source_location( char const * file, boost::uint_least32_t ln, char const * function, boost::uint_least32_t col = 0 ) BOOST_NOEXCEPT: file_( file ), function_( function ), line_( ln ), column_( col )
43
163
    {
44
163
    }
45
46
#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
47
48
    BOOST_CONSTEXPR source_location( std::source_location const& loc ) BOOST_NOEXCEPT: file_( loc.file_name() ), function_( loc.function_name() ), line_( loc.line() ), column_( loc.column() )
49
    {
50
    }
51
52
#endif
53
54
    BOOST_CONSTEXPR char const * file_name() const BOOST_NOEXCEPT
55
163
    {
56
163
        return file_;
57
163
    }
58
59
    BOOST_CONSTEXPR char const * function_name() const BOOST_NOEXCEPT
60
163
    {
61
163
        return function_;
62
163
    }
63
64
    BOOST_CONSTEXPR boost::uint_least32_t line() const BOOST_NOEXCEPT
65
163
    {
66
163
        return line_;
67
163
    }
68
69
    BOOST_CONSTEXPR boost::uint_least32_t column() const BOOST_NOEXCEPT
70
163
    {
71
163
        return column_;
72
163
    }
73
74
#if defined(BOOST_MSVC)
75
# pragma warning( push )
76
# pragma warning( disable: 4996 )
77
#endif
78
79
#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
80
# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::sprintf(buffer, format, arg)
81
#else
82
0
# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::snprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), format, arg)
83
#endif
84
85
    std::string to_string() const
86
0
    {
87
0
        unsigned long ln = line();
88
89
0
        if( ln == 0 )
90
0
        {
91
0
            return "(unknown source location)";
92
0
        }
93
94
0
        std::string r = file_name();
95
96
0
        char buffer[ 16 ];
97
98
0
        BOOST_ASSERT_SNPRINTF( buffer, ":%lu", ln );
99
0
        r += buffer;
100
101
0
        unsigned long co = column();
102
103
0
        if( co )
104
0
        {
105
0
            BOOST_ASSERT_SNPRINTF( buffer, ":%lu", co );
106
0
            r += buffer;
107
0
        }
108
109
0
        char const* fn = function_name();
110
111
0
        if( *fn != 0 )
112
0
        {
113
0
            r += " in function '";
114
0
            r += fn;
115
0
            r += '\'';
116
0
        }
117
118
0
        return r;
119
0
    }
120
121
#undef BOOST_ASSERT_SNPRINTF
122
123
#if defined(BOOST_MSVC)
124
# pragma warning( pop )
125
#endif
126
127
    inline friend bool operator==( source_location const& s1, source_location const& s2 ) BOOST_NOEXCEPT
128
    {
129
        return std::strcmp( s1.file_, s2.file_ ) == 0 && std::strcmp( s1.function_, s2.function_ ) == 0 && s1.line_ == s2.line_ && s1.column_ == s2.column_;
130
    }
131
132
    inline friend bool operator!=( source_location const& s1, source_location const& s2 ) BOOST_NOEXCEPT
133
    {
134
        return !( s1 == s2 );
135
    }
136
};
137
138
#if !defined(BOOST_NO_IOSTREAM)
139
140
template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & os, source_location const & loc )
141
{
142
    os << loc.to_string();
143
    return os;
144
}
145
146
#endif
147
148
} // namespace boost
149
150
#if defined(BOOST_DISABLE_CURRENT_LOCATION)
151
152
# define BOOST_CURRENT_LOCATION ::boost::source_location()
153
154
#elif defined(BOOST_MSVC) && BOOST_MSVC >= 1935
155
156
# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCSIG(), __builtin_COLUMN())
157
158
#elif defined(BOOST_MSVC) && BOOST_MSVC >= 1926
159
160
// std::source_location::current() is available in -std:c++20, but fails with consteval errors before 19.31, and doesn't produce
161
// the correct result under 19.31, so prefer the built-ins
162
# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
163
164
#elif defined(BOOST_MSVC)
165
166
// __LINE__ is not a constant expression under /ZI (edit and continue) for 1925 and before
167
168
# define BOOST_CURRENT_LOCATION_IMPL_1(x) BOOST_CURRENT_LOCATION_IMPL_2(x)
169
# define BOOST_CURRENT_LOCATION_IMPL_2(x) (x##0 / 10)
170
171
# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, BOOST_CURRENT_LOCATION_IMPL_1(__LINE__), "")
172
173
#elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L && !defined(__NVCC__)
174
175
// Under nvcc, __builtin_source_location is not constexpr
176
// https://github.com/boostorg/assert/issues/32
177
178
# define BOOST_CURRENT_LOCATION ::boost::source_location(::std::source_location::current())
179
180
#elif defined(BOOST_CLANG) && BOOST_CLANG_VERSION >= 90000
181
182
13.4k
# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
183
184
#elif defined(BOOST_GCC) && BOOST_GCC >= 80000
185
186
// The built-ins are available in 4.8+, but are not constant expressions until 7
187
// In addition, reproducible builds require -ffile-prefix-map, which is GCC 8
188
// https://github.com/boostorg/assert/issues/38
189
190
# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION())
191
192
#elif defined(BOOST_GCC) && BOOST_GCC >= 50000
193
194
// __PRETTY_FUNCTION__ is allowed outside functions under GCC, but 4.x suffers from codegen bugs
195
# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, __PRETTY_FUNCTION__)
196
197
#else
198
199
// __func__ macros aren't allowed outside functions, but BOOST_CURRENT_LOCATION is
200
# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, "")
201
202
#endif
203
204
#endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED