Skip to content

Commit 262846a

Browse files
committed
fix: source_location
1 parent 5e9e1d2 commit 262846a

File tree

4 files changed

+107
-18
lines changed

4 files changed

+107
-18
lines changed

include/mrdox/Support/Error.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
#include <mrdox/Platform.hpp>
1616
#include <mrdox/Support/Format.hpp>
17+
#include <mrdox/Support/source_location.hpp>
1718
#include <exception>
1819
#include <iterator>
19-
#include <source_location>
2020
#include <string>
2121
#include <string_view>
2222
#include <type_traits>
@@ -33,13 +33,13 @@ class [[nodiscard]] MRDOX_DECL
3333
{
3434
std::string message_;
3535
std::string reason_;
36-
std::source_location loc_;
36+
source_location loc_;
3737

3838
static
3939
std::string
4040
appendSourceLocation(
4141
std::string&&,
42-
std::source_location const&);
42+
source_location const&);
4343

4444
public:
4545
/** Constructor.
@@ -76,8 +76,8 @@ class [[nodiscard]] MRDOX_DECL
7676
explicit
7777
Error(
7878
std::string reason,
79-
std::source_location loc =
80-
std::source_location::current())
79+
source_location loc =
80+
source_location::current())
8181
: message_(appendSourceLocation(std::string(reason), loc))
8282
, reason_(std::move(reason))
8383
, loc_(loc)
@@ -92,8 +92,8 @@ class [[nodiscard]] MRDOX_DECL
9292
explicit
9393
Error(
9494
std::error_code const& ec,
95-
std::source_location loc =
96-
std::source_location::current())
95+
source_location loc =
96+
source_location::current())
9797
{
9898
if(! ec)
9999
return;
@@ -105,8 +105,8 @@ class [[nodiscard]] MRDOX_DECL
105105
explicit
106106
Error(
107107
std::vector<Error> const& errors,
108-
std::source_location loc =
109-
std::source_location::current());
108+
source_location loc =
109+
source_location::current());
110110

111111
/** Return true if this holds an error.
112112
*/
@@ -142,7 +142,7 @@ class [[nodiscard]] MRDOX_DECL
142142

143143
/** Return the source location.
144144
*/
145-
constexpr std::source_location
145+
constexpr source_location
146146
location() const noexcept
147147
{
148148
return loc_;
@@ -229,7 +229,7 @@ class MRDOX_DECL
229229

230230
public:
231231
SourceLocation(
232-
std::source_location const& loc) noexcept;
232+
source_location const& loc) noexcept;
233233

234234
std::string_view file_name() const noexcept
235235
{

include/mrdox/Support/Format.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#define MRDOX_API_SUPPORT_FORMAT_HPP
1313

1414
#include <mrdox/Platform.hpp>
15+
#include <mrdox/Support/source_location.hpp>
1516
#include <fmt/format.h>
16-
#include <source_location>
1717

1818
namespace clang {
1919
namespace mrdox {
@@ -24,8 +24,8 @@ struct FormatString
2424
template<class T>
2525
FormatString(
2626
T const& fs_,
27-
std::source_location loc_ =
28-
std::source_location::current())
27+
source_location loc_ =
28+
source_location::current())
2929
: fs(fs_)
3030
, loc(loc_)
3131
{
@@ -34,7 +34,7 @@ struct FormatString
3434
}
3535

3636
std::string_view fs;
37-
std::source_location loc;
37+
source_location loc;
3838
};
3939

4040
} // mrdox
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Krystian Stasiowski (sdkrystian@gmail.com)
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_API_SUPPORT_SOURCE_LOCATION_HPP
12+
#define MRDOX_API_SUPPORT_SOURCE_LOCATION_HPP
13+
14+
#include <version>
15+
16+
#if __cpp_lib_source_location >= 201907L && \
17+
__has_include(<source_location>)
18+
#include <source_location>
19+
20+
namespace clang {
21+
namespace mrdox {
22+
23+
using std::source_location;
24+
25+
} // mrdox
26+
} // clang
27+
#else
28+
#include <cstdint>
29+
30+
namespace clang {
31+
namespace mrdox {
32+
33+
struct source_location
34+
{
35+
static
36+
constexpr
37+
source_location
38+
current(
39+
const char* const file = __builtin_FILE(),
40+
const char* const function = __builtin_FUNCTION(),
41+
const std::uint_least32_t line = __builtin_LINE(),
42+
const std::uint_least32_t column = __builtin_COLUMN()) noexcept
43+
44+
{
45+
source_location result;
46+
result.file_ = file;
47+
result.function_ = function;
48+
result.line_ = line;
49+
result.column_ = column;
50+
return result;
51+
}
52+
53+
constexpr
54+
const char*
55+
file_name() const noexcept
56+
{
57+
return file_;
58+
}
59+
constexpr
60+
const char*
61+
function_name() const noexcept
62+
{
63+
return function_;
64+
}
65+
66+
constexpr
67+
std::uint_least32_t
68+
line() const noexcept
69+
{
70+
return line_;
71+
}
72+
constexpr
73+
std::uint_least32_t
74+
column() const noexcept
75+
{
76+
return column_;
77+
}
78+
79+
private:
80+
const char* file_ = "";
81+
const char* function_ = "";
82+
std::uint_least32_t line_ = 0;
83+
std::uint_least32_t column_ = 0;
84+
};
85+
86+
} // mrdox
87+
} // clang
88+
#endif
89+
#endif

source/Support/Error.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ std::string
1818
Error::
1919
appendSourceLocation(
2020
std::string&& text,
21-
std::source_location const& loc)
21+
source_location const& loc)
2222
{
2323
#if 0
2424
fmt::format_to(
@@ -41,7 +41,7 @@ appendSourceLocation(
4141
Error::
4242
Error(
4343
std::vector<Error> const& errors,
44-
std::source_location loc)
44+
source_location loc)
4545
{
4646
MRDOX_ASSERT(errors.size() > 0);
4747
if(errors.size() == 1)
@@ -63,7 +63,7 @@ Error(
6363

6464
SourceLocation::
6565
SourceLocation(
66-
std::source_location const& loc) noexcept
66+
source_location const& loc) noexcept
6767
: file_(files::getSourceFilename(loc.file_name()))
6868
, line_(loc.line())
6969
, col_(loc.column())

0 commit comments

Comments
 (0)