Skip to content

Commit

Permalink
fix: source_location
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Jun 26, 2023
1 parent 5e9e1d2 commit 262846a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
22 changes: 11 additions & 11 deletions include/mrdox/Support/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#include <mrdox/Platform.hpp>
#include <mrdox/Support/Format.hpp>
#include <mrdox/Support/source_location.hpp>
#include <exception>
#include <iterator>
#include <source_location>
#include <string>
#include <string_view>
#include <type_traits>
Expand All @@ -33,13 +33,13 @@ class [[nodiscard]] MRDOX_DECL
{
std::string message_;
std::string reason_;
std::source_location loc_;
source_location loc_;

static
std::string
appendSourceLocation(
std::string&&,
std::source_location const&);
source_location const&);

public:
/** Constructor.
Expand Down Expand Up @@ -76,8 +76,8 @@ class [[nodiscard]] MRDOX_DECL
explicit
Error(
std::string reason,
std::source_location loc =
std::source_location::current())
source_location loc =
source_location::current())
: message_(appendSourceLocation(std::string(reason), loc))
, reason_(std::move(reason))
, loc_(loc)
Expand All @@ -92,8 +92,8 @@ class [[nodiscard]] MRDOX_DECL
explicit
Error(
std::error_code const& ec,
std::source_location loc =
std::source_location::current())
source_location loc =
source_location::current())
{
if(! ec)
return;
Expand All @@ -105,8 +105,8 @@ class [[nodiscard]] MRDOX_DECL
explicit
Error(
std::vector<Error> const& errors,
std::source_location loc =
std::source_location::current());
source_location loc =
source_location::current());

/** Return true if this holds an error.
*/
Expand Down Expand Up @@ -142,7 +142,7 @@ class [[nodiscard]] MRDOX_DECL

/** Return the source location.
*/
constexpr std::source_location
constexpr source_location
location() const noexcept
{
return loc_;
Expand Down Expand Up @@ -229,7 +229,7 @@ class MRDOX_DECL

public:
SourceLocation(
std::source_location const& loc) noexcept;
source_location const& loc) noexcept;

std::string_view file_name() const noexcept
{
Expand Down
8 changes: 4 additions & 4 deletions include/mrdox/Support/Format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#define MRDOX_API_SUPPORT_FORMAT_HPP

#include <mrdox/Platform.hpp>
#include <mrdox/Support/source_location.hpp>
#include <fmt/format.h>
#include <source_location>

namespace clang {
namespace mrdox {
Expand All @@ -24,8 +24,8 @@ struct FormatString
template<class T>
FormatString(
T const& fs_,
std::source_location loc_ =
std::source_location::current())
source_location loc_ =
source_location::current())
: fs(fs_)
, loc(loc_)
{
Expand All @@ -34,7 +34,7 @@ struct FormatString
}

std::string_view fs;
std::source_location loc;
source_location loc;
};

} // mrdox
Expand Down
89 changes: 89 additions & 0 deletions include/mrdox/Support/source_location.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Licensed 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
//
// Copyright (c) 2023 Krystian Stasiowski ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#ifndef MRDOX_API_SUPPORT_SOURCE_LOCATION_HPP
#define MRDOX_API_SUPPORT_SOURCE_LOCATION_HPP

#include <version>

#if __cpp_lib_source_location >= 201907L && \
__has_include(<source_location>)
#include <source_location>

namespace clang {
namespace mrdox {

using std::source_location;

} // mrdox
} // clang
#else
#include <cstdint>

namespace clang {
namespace mrdox {

struct source_location
{
static
constexpr
source_location
current(
const char* const file = __builtin_FILE(),
const char* const function = __builtin_FUNCTION(),
const std::uint_least32_t line = __builtin_LINE(),
const std::uint_least32_t column = __builtin_COLUMN()) noexcept

{
source_location result;
result.file_ = file;
result.function_ = function;
result.line_ = line;
result.column_ = column;
return result;
}

constexpr
const char*
file_name() const noexcept
{
return file_;
}
constexpr
const char*
function_name() const noexcept
{
return function_;
}

constexpr
std::uint_least32_t
line() const noexcept
{
return line_;
}
constexpr
std::uint_least32_t
column() const noexcept
{
return column_;
}

private:
const char* file_ = "";
const char* function_ = "";
std::uint_least32_t line_ = 0;
std::uint_least32_t column_ = 0;
};

} // mrdox
} // clang
#endif
#endif
6 changes: 3 additions & 3 deletions source/Support/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string
Error::
appendSourceLocation(
std::string&& text,
std::source_location const& loc)
source_location const& loc)
{
#if 0
fmt::format_to(
Expand All @@ -41,7 +41,7 @@ appendSourceLocation(
Error::
Error(
std::vector<Error> const& errors,
std::source_location loc)
source_location loc)
{
MRDOX_ASSERT(errors.size() > 0);
if(errors.size() == 1)
Expand All @@ -63,7 +63,7 @@ Error(

SourceLocation::
SourceLocation(
std::source_location const& loc) noexcept
source_location const& loc) noexcept
: file_(files::getSourceFilename(loc.file_name()))
, line_(loc.line())
, col_(loc.column())
Expand Down

0 comments on commit 262846a

Please sign in to comment.