
Go-style defer() implementation for C - jagger11
https://github.com/google/honggfuzz/blob/master/honggfuzz.h#L333
======
ill0gicity
I gathered two methods-- one for gcc, another for clang-- and crammed them
into a set of macros that require no special compiler flags almost two years
ago:
[https://github.com/jeffwalter/defer/blob/master/include/defe...](https://github.com/jeffwalter/defer/blob/master/include/defer.h)

~~~
jagger11
The clang code requires -fblocks/-lBlocksRuntime too, and I was a bit quicker
:)

[https://github.com/google/honggfuzz/commit/a9db9ddc2e24d157a...](https://github.com/google/honggfuzz/commit/a9db9ddc2e24d157a754fb964be3908e4d876790)

~~~
ill0gicity
I read your 2016 as 2017; guess that what being sick for a few days does to
the brain. And it doesn't require you pass those options:
[https://imgur.com/a/Hymj9](https://imgur.com/a/Hymj9) (clang-900.0.39.2)

~~~
jagger11
I'm not sure how that'd be possible. It uses blocks (^) so it requires blocks
with clang, no?

I've changed my macro to yours, and compiled with clang (3.9)

    
    
      CC=clang-3.9 make clean indent depend all
    
      clang-3.9 -c -O3 -D_GNU_SOURCE -Wall -Werror -Wframe-larger-than=131072 -Wno-format-truncation -I. -std=c11 -I/usr/local/include -Wextra -Wno-override-init -funroll-loops -D_FILE_OFFSET_BITS=64 -D_HF_LINUX_INTEL_PT_LIB -Wno-initializer-overrides -Wno-unknown-warning-option -Wno-gnu-empty-initializer -Wno-format-pedantic -Wno-gnu-statement-expression -D_HF_ARCH_LINUX  -o cmdline.o cmdline.c
      In file included from cmdline.c:23:
      In file included from ./cmdline.h:28:
      ./honggfuzz.h:357:40: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them
      static inline void defer_cleanup(void (^*b)()) {
                                           ^
      cmdline.c:68:5: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them
        defer {
        ^
      ./honggfuzz.h:364:39: note: expanded from macro 'defer'
            __attribute__((unused)) void (^defer_varname(__COUNTER__))() = ^
                                          ^
      cmdline.c:68:5: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them
      ./honggfuzz.h:364:72: note: expanded from macro 'defer'
            __attribute__((unused)) void (^defer_varname(__COUNTER__))() = ^
                                                                           ^
      3 errors generated.
      Makefile:266: recipe for target 'cmdline.o' failed
      make: *** [cmdline.o] Error 1
    

PS. Hm.. maybe that's the clang version you're using (900)?

~~~
ill0gicity
Maybe?

    
    
        Apple LLVM version 9.0.0 (clang-900.0.39.2)
        Target: x86_64-apple-darwin16.7.0
        Thread model: posix
        InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
        
        ProductName:    Mac OS X
        ProductVersion: 10.12.6
        BuildVersion:   16G1114

------
iKlsR
See also [https://oded.blog/2017/10/05/go-defer-in-
cpp/](https://oded.blog/2017/10/05/go-defer-in-cpp/)

~~~
jagger11
Tho, in C++ I guess almost everybody would use a scoped variable with its
desctructor using lambda.

