diff options
| -rw-r--r-- | defer.h | 20 | ||||
| -rw-r--r-- | tests/Makefile | 9 | ||||
| -rw-r--r-- | tests/defer.c | 12 |
3 files changed, 41 insertions, 0 deletions
@@ -0,0 +1,20 @@ +#ifndef __DEFER_H__ +#define __DEFER_H__ + +#ifndef __unused +#define __unused __attribute__((unused)) +#endif + +#define __cleanup(f) __attribute__((cleanup(f))) + +#define CAT(a, b) CAT_(a, b) +#define CAT_(a, b) a ## b + +#define UNIQ(name) CAT(name, __LINE__) + +#define defer \ + auto void UNIQ(defer_func)(int *); \ + int UNIQ(defer_var) __cleanup(UNIQ(defer_func)); \ + void UNIQ(defer_func)(int *__arg __unused) + +#endif diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..166ee66 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,9 @@ +defer: defer.o + gcc -o $@ $< + +%.o: %.c + gcc -c -I.. -o $@ $< + +clean: + @echo cleaning... + @rm -f *.o diff --git a/tests/defer.c b/tests/defer.c new file mode 100644 index 0000000..d5f75d7 --- /dev/null +++ b/tests/defer.c @@ -0,0 +1,12 @@ +#include "defer.h" + +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + defer { printf("exit\n"); } + + printf("Hello World!\n"); + + return 0; +} |
