Skip to content

Commit

Permalink
Move type_name logic back into the header
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed May 16, 2024
1 parent 960b9a5 commit d6c971a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ target_sources(
src/assert.cpp
src/analysis.cpp
src/utils.cpp
src/formatting_and_type_tools.cpp
src/stringification.cpp
src/platform.cpp
src/printing.cpp
Expand Down
26 changes: 24 additions & 2 deletions include/libassert/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define LIBASSERT_UTILITIES_HPP

#include <type_traits>
#include <string>
#include <string_view>

#include <libassert/platform.hpp>

Expand Down Expand Up @@ -62,12 +64,32 @@ namespace libassert::detail {
namespace libassert::detail {
[[nodiscard]] LIBASSERT_EXPORT std::string bstringf(const char* format, ...);

[[nodiscard]] LIBASSERT_EXPORT std::string_view type_name_core(std::string_view signature) noexcept;
LIBASSERT_ATTR_COLD [[nodiscard]]
constexpr inline std::string_view substring_bounded_by(
std::string_view sig,
std::string_view l,
std::string_view r
) noexcept {
auto i = sig.find(l) + l.length();
return sig.substr(i, sig.rfind(r) - i);
}

template<typename T>
LIBASSERT_ATTR_COLD [[nodiscard]]
std::string_view type_name() noexcept {
return type_name_core(LIBASSERT_PFUNC);
// Cases to handle:
// gcc: constexpr std::string_view ns::type_name() [with T = int; std::string_view = std::basic_string_view<char>]
// clang: std::string_view ns::type_name() [T = int]
// msvc: class std::basic_string_view<char,struct std::char_traits<char> > __cdecl ns::type_name<int>(void)
#if LIBASSERT_IS_CLANG
return substring_bounded_by(LIBASSERT_PFUNC, "[T = ", "]");
#elif LIBASSERT_IS_GCC
return substring_bounded_by(LIBASSERT_PFUNC, "[with T = ", "; std::string_view = ");
#elif LIBASSERT_IS_MSVC
return substring_bounded_by(LIBASSERT_PFUNC, "type_name<", ">(void)");
#else
return LIBASSERT_PFUNC;
#endif
}

[[nodiscard]] LIBASSERT_EXPORT std::string prettify_type(std::string type);
Expand Down
50 changes: 0 additions & 50 deletions src/formatting_and_type_tools.cpp

This file was deleted.

19 changes: 19 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <regex>
Expand All @@ -12,7 +13,25 @@
#include "utils.hpp"
#include "microfmt.hpp"

#include <libassert/assert.hpp>

namespace libassert::detail {
// to save template instantiation work in TUs a variadic stringf is used
LIBASSERT_ATTR_COLD
std::string bstringf(const char* format, ...) {
va_list args1;
va_list args2;
va_start(args1, format);
va_start(args2, format);
const int length = vsnprintf(nullptr, 0, format, args1);
if(length < 0) { LIBASSERT_PRIMITIVE_ASSERT(false, "Invalid arguments to stringf"); }
std::string str(length, 0);
(void)vsnprintf(str.data(), length + 1, format, args2);
va_end(args1);
va_end(args2);
return str;
}

LIBASSERT_ATTR_COLD
void primitive_assert_impl(
bool condition,
Expand Down

0 comments on commit d6c971a

Please sign in to comment.