Skip to content

Commit

Permalink
Merge branch 'simplify-assertions'
Browse files Browse the repository at this point in the history
* simplify-assertions:
  ASE: queuemux.hh: avoid inlining fallback method `empty()`
  ASE: cxxaux: assertion_failed: simplify many call sites by adding noexcept
  ASE: queuemux.hh: simplify impls by adding noexcept
  ASE: platform.cc: fix up assertion_failed() call
  ASE: engine.cc: use ASE_ASSERT_WARN() instead of one-off special macro
  ASE: cxxaux: assertion_failed: simplify many call sites by using char*
  ASE: cxxaux.hh: simplify compiler errors by avoiding macro indirections
  ASE: internal.hh: simplify compiler errors by avoiding macro indirections

Signed-off-by: Tim Janik <[email protected]>
  • Loading branch information
tim-janik committed Nov 17, 2023
2 parents 4756546 + d4da12e commit b9dcc9b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
8 changes: 4 additions & 4 deletions ase/cxxaux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ string_demangle_cxx (const char *mangled_identifier)
}

void
assertion_failed (const std::string &msg, const char *file, int line, const char *func)
assertion_failed (const char *msg, const char *file, int line, const char *func) noexcept
{
if (file && line > 0 && func)
fprintf (stderr, "%s:%u:%s: ", file, line, func);
else if (file && line > 0)
fprintf (stderr, "%s:%u: ", file, line);
else if (file)
fprintf (stderr, "%s: ", file);
if (msg.empty())
if (!msg || !msg[0])
fputs ("state unreachable\n", stderr);
else
{
fputs ("assertion failed: ", stderr);
fputs (msg.c_str(), stderr);
if (msg.size() && msg[msg.size() - 1] != '\n')
fputs (msg, stderr);
if (msg[0] && msg[strlen (msg) - 1] != '\n')
fputc ('\n', stderr);
}
fflush (stderr);
Expand Down
13 changes: 5 additions & 8 deletions ase/cxxaux.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,16 @@ using VoidF = std::function<void()>;
#define ASE_RETURN_UNLESS(cond, ...) do { if (ASE_UNLIKELY (!bool (cond))) return __VA_ARGS__; } while (0)

/// Return from the current function if `expr` evaluates to false and issue an assertion warning.
#define ASE_ASSERT_RETURN(expr, ...) do { if (ASE_ISLIKELY (expr)) break; ::Ase::assertion_failed (#expr); return __VA_ARGS__; } while (0)
#define ASE_ASSERT_RETURN(expr, ...) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); return __VA_ARGS__; } while (0)

/// Return from the current function and issue an assertion warning.
#define ASE_ASSERT_RETURN_UNREACHED(...) do { ::Ase::assertion_failed (""); return __VA_ARGS__; } while (0)

/// Issue an assertion warning if `expr` evaluates to false.
#define ASE_ASSERT_WARN(expr) do { if (ASE_ISLIKELY (expr)) break; ::Ase::assertion_failed (#expr); } while (0)
#define ASE_ASSERT_WARN(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)

/// Like ASE_ASSERT_WARN(), enabled if expensive `expr` are allowed.
#define ASE_ASSERT_PARANOID(expr) do { if (ASE_ISLIKELY (expr)) break; ::Ase::assertion_failed (#expr); } while (0)

/// Return from the current function if `expr` evaluates to false and issue an assertion warning.
#define ASE_ASSERT_ALWAYS(expr, ...) do { if (ASE_ISLIKELY (expr)) break; ::Ase::assertion_failed (#expr); __builtin_trap(); } while (0)
#define ASE_ASSERT_PARANOID(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)

/// Delete copy ctor and assignment operator.
#define ASE_CLASS_NON_COPYABLE(ClassName) \
Expand Down Expand Up @@ -209,8 +206,8 @@ protected:
using VirtualBaseP = std::shared_ptr<VirtualBase>;

/// Issue a warning about an assertion error.
void assertion_failed (const std::string &msg = "", const char *file = __builtin_FILE(),
int line = __builtin_LINE(), const char *func = __builtin_FUNCTION());
void assertion_failed (const char *msg = nullptr, const char *file = __builtin_FILE(),
int line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) noexcept;

/// Test string equality at compile time.
extern inline constexpr bool
Expand Down
4 changes: 2 additions & 2 deletions ase/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ AudioEngineThread::AudioEngineThread (const VoidF &owner_wakeup, uint sample_rat
AudioEngine&
make_audio_engine (const VoidF &owner_wakeup, uint sample_rate, SpeakerArrangement speakerarrangement)
{
ASE_ASSERT_ALWAYS (sample_rate == FIXED_SAMPLE_RATE);
ASE_ASSERT_ALWAYS (speaker_arrangement_count_channels (speakerarrangement) == FIXED_N_CHANNELS);
ASE_ASSERT_WARN (sample_rate == FIXED_SAMPLE_RATE);
ASE_ASSERT_WARN (speaker_arrangement_count_channels (speakerarrangement) == FIXED_N_CHANNELS);
FastMemory::Block transport_block = ServerImpl::instancep()->telemem_allocate (sizeof (AudioTransport));
return *new AudioEngineThread (owner_wakeup, sample_rate, speakerarrangement, transport_block);
}
Expand Down
8 changes: 4 additions & 4 deletions ase/internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ using Ase::String;
#define ARRAY_SIZE(array) ASE_ARRAY_SIZE (array)

/// Return from the current function if `expr` is unmet and issue an assertion warning.
#define assert_return(expr, ...) ASE_ASSERT_RETURN (expr, __VA_ARGS__)
#define assert_return(expr, ...) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); return __VA_ARGS__; } while (0)
/// Return from the current function and issue an assertion warning.
#define assert_return_unreached(...) ASE_ASSERT_RETURN_UNREACHED (__VA_ARGS__)
#define assert_return_unreached(...) do { ::Ase::assertion_failed (""); return __VA_ARGS__; } while (0)
/// Issue an assertion warning if `expr` evaluates to false.
#define assert_warn(expr) ASE_ASSERT_WARN (expr)
#define assert_warn(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)
/// Issue an assertion warning if `expr` evaluates to false, check might be disabled in production.
#define assert_paranoid(expr) ASE_ASSERT_PARANOID (expr)
#define assert_paranoid(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)
/// Explicitely mark unreachable code locations.
#define assert_unreached() __builtin_unreachable()

Expand Down
2 changes: 1 addition & 1 deletion ase/platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ void
this_thread_set_name (const String &name16chars)
{
if (name16chars.find (" ") != name16chars.npos)
::Ase::assertion_failed (string_format ("new thread name contains spaces: \"%s\"", name16chars));
::Ase::assertion_failed (string_format ("new thread name contains spaces: \"%s\"", name16chars).c_str());
pthread_setname_np (pthread_self(), name16chars.c_str());
}

Expand Down
12 changes: 6 additions & 6 deletions ase/queuemux.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ struct QueueMultiplexer {
return more();
}
size_t
count_pending() const
count_pending() const noexcept
{
size_t c = 0;
for (ssize_t i = 0; i < n_queues; i++)
c += ptrs[i].end - ptrs[i].it;
return c;
}
bool
more() const
more() const noexcept
{
return n_queues > 0;
}
const ValueType&
peek ()
peek () noexcept
{
if (!more()) [[unlikely]]
return empty();
return *ptrs[current].it;
}
const ValueType&
pop ()
pop () noexcept
{
ASE_ASSERT_RETURN (more(), empty());
const ValueType &result = *ptrs[current].it++;
Expand Down Expand Up @@ -104,7 +104,7 @@ struct QueueMultiplexer {
iterator end () { return {}; }
private:
void
seek()
seek() noexcept
{
if (n_queues == 0) [[likely]]
return;
Expand All @@ -126,7 +126,7 @@ private:
// dprintf (2, "%s: n_queues=%zd current=%zd first=%ld next=%ld\n", __func__, n_queues, current, long (first), long (next));
}
static const ValueType&
empty()
empty() noexcept ASE_NOINLINE
{
static const ValueType empty_ {};
return empty_;
Expand Down

0 comments on commit b9dcc9b

Please sign in to comment.