Skip to content

Commit

Permalink
chore: better assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian authored and alandefreitas committed Jul 28, 2023
1 parent 0df0e02 commit d921878
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
14 changes: 1 addition & 13 deletions include/mrdox/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef MRDOX_API_PLATFORM_HPP
#define MRDOX_API_PLATFORM_HPP

#include <cassert>
#include <mrdox/Support/Assert.hpp>
#include <type_traits>

#if __cplusplus < 202002L
Expand Down Expand Up @@ -66,18 +66,6 @@ namespace mrdox {

//------------------------------------------------

#ifndef MRDOX_ASSERT
# define MRDOX_ASSERT(x) assert(x)
#endif

#ifndef MRDOX_UNREACHABLE
# ifdef __GNUC__
# define MRDOX_UNREACHABLE() do { MRDOX_ASSERT(false); __builtin_unreachable(); } while(false)
# elif defined(_MSC_VER)
# define MRDOX_UNREACHABLE() do { MRDOX_ASSERT(false); __assume(false); } while(false)
# endif
#endif

#ifndef FMT_CONSTEVAL
# if !defined(__GNUC__) && defined(_MSC_VER)
# define FMT_CONSTEVAL
Expand Down
47 changes: 47 additions & 0 deletions include/mrdox/Support/Assert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 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_ASSERT_HPP
#define MRDOX_API_SUPPORT_ASSERT_HPP

#include <cstdint>

namespace clang {
namespace mrdox {

#ifdef NDEBUG
#ifdef __GNUC__
#define MRDOX_UNREACHABLE() static_cast<void>(__builtin_unreachable())
#elif defined(_MSC_VER)
#define MRDOX_UNREACHABLE() static_cast<void>(__assume(false))
#endif
#define MRDOX_ASSERT(x) static_cast<void>(false)
#else
#ifdef __GNUC__
#define MRDOX_UNREACHABLE() static_cast<void>(__builtin_trap(), __builtin_unreachable())
#elif defined(_MSC_VER)
#define MRDOX_UNREACHABLE() static_cast<void>(__debugbreak(), __assume(false))
#endif

void
assert_failed(
const char* msg,
const char* file,
std::uint_least32_t line);

#define MRDOX_ASSERT(x) static_cast<void>(!! (x) || \
(assert_failed(#x, __builtin_FILE(), __builtin_LINE()), \
MRDOX_UNREACHABLE(), true))
#endif

} // mrdox
} // clang

#endif
35 changes: 35 additions & 0 deletions src/lib/Support/Assert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// 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
//

#include <mrdox/Support/Assert.hpp>
#include <mrdox/Support/Path.hpp>
#include <fmt/format.h>
#include <llvm/Support/raw_ostream.h>

namespace SourceFileNames {
extern char const* getFileName(char const*) noexcept;
} // SourceFileNames

namespace clang {
namespace mrdox {

void
assert_failed(
const char* msg,
const char* file,
std::uint_least32_t line)
{
llvm::errs() << fmt::format(
"assertion failed: {} on line {} in {}\n",
msg, line, ::SourceFileNames::getFileName(file));
}

} // mrdox
} // clang

0 comments on commit d921878

Please sign in to comment.