Skip to content

Instantly share code, notes, and snippets.

@codehz
Last active December 22, 2019 02:27
Show Gist options
  • Save codehz/c295c419fb0148aa0191f4e28b79d020 to your computer and use it in GitHub Desktop.
Save codehz/c295c419fb0148aa0191f4e28b79d020 to your computer and use it in GitHub Desktop.
#pragma once
#ifndef __GNUC__
# error This header only works in GCC
#endif
#ifdef __clang__
# ifndef __ccls__
# warning This header may not working in clang
# endif
#endif
// https://gcc.gnu.org/onlinedocs/gcc/Local-Labels.html
#define local_label __label__
// https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
#define scoped(f) __attribute__((cleanup(f)))
#ifndef __cplusplus
# ifdef __ccls__
# define defer(...)
# else
# define defer(name, ...) \
__attribute__((always_inline)) inline void defer_##name(void *_) { __VA_ARGS__; }; \
struct { \
} __defer_##name scoped(defer_##name)
# endif
#else
template <typename T> struct __defer__ {
T func;
~__defer__() { func(); }
};
template <typename T> __defer__(T t)->__defer__<T>;
# define defer(name, ...) \
auto __defer_##name = __defer__ { \
[&]() { __VA_ARGS__; } \
}
#endif
#define defer_(thing, fn) defer(thing, fn(thing))
#define defer_ref(thing, fn) defer(thing, fn(&thing))
#ifndef __cplusplus
# ifndef __ccls__
# define lam(ret, ...) \
({ \
ret __fn__ __VA_ARGS__; \
__fn__; \
})
# else
# define lam(...) (void *) 0
# endif
#else
# define lam(ret, ...) +[] __VA_ARGS__
#endif
#define lam0(...) lam(void, __VA_ARGS__)
// https://gcc.gnu.org/onlinedocs/gcc/Alignment.html
#define alignof __alignof__
#ifndef __cplusplus
# ifndef static_assert
# define static_assert _Static_assert
# endif
# ifndef thread_local
# define thread_local __thread
# endif
# ifndef auto
# define auto __auto_type
# endif
#endif
#define DO_PRAGMA(x) _Pragma(#x)
#define TODO(x) DO_PRAGMA(message("TODO - " #x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment