Skip to content

Instantly share code, notes, and snippets.

@pskrgag
Last active April 14, 2025 19:51
Show Gist options
  • Save pskrgag/39c8640c0b383ed1f1c0dd6c8f5a832e to your computer and use it in GitHub Desktop.
Save pskrgag/39c8640c0b383ed1f1c0dd6c8f5a832e to your computer and use it in GitHub Desktop.
assert warn side effects (recent clangs)
// See in action https://godbolt.org/z/eYon5nWrP
extern void __assert_fn(void);
#define assert(e) do { if (!(e)) { __builtin_assume(!(e)); __assert_fn(); } } while (0);
static inline int nonpure_fn(void)
{
return 0;
}
__attribute__((pure))
static inline int pure_fn(void)
{
return 0;
}
__attribute__((const))
static inline int const_fn(void)
{
return 0;
}
void foo(void)
{
int a = 0;
int b = 0;
assert(1);
assert(2 + 1);
assert(a + 1);
assert(a = b); // warn here
assert(a++); // warn here
assert(nonpure_fn()); // warn here
assert(pure_fn());
assert(const_fn());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment