[cfe-dev] -rewrite-objc, C and blocks
jahanian
fjahanian at apple.com
Fri Oct 1 09:00:57 PDT 2010
Concept is right. However, rewriter project was done for a specific purpose and generates c++ code
as the input might be objective-c++. It is best for you to use the rewrite-obj model and have you own
-rewrite-block which generates c code.
- Fariborz
On Oct 1, 2010, at 3:16 AM, Pierre Habouzit wrote:
> I intended to use clang -rewrite-objc to be able to compile code using
> blocks on targets unsupported by clang. I'm totally uninterested in the
> ObjC rewriting, only the blocks one.
>
> Sadly, this rewriting module rewrites to C++ even when the source code
> in question is just plain old C.
>
> I'm under the impression, that when the source code is C, only the fact
> that clang is generating structs with constructors and RAII use of the
> defined blocks. For example:
>
> struct __main_block_impl_0 {
> struct __block_impl impl;
> struct __main_block_desc_0* Desc;
> int a;
> __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
> impl.isa = &_NSConcreteStackBlock;
> impl.Flags = flags;
> impl.FuncPtr = fp;
> Desc = desc;
> }
> };
>
> [...]
>
> int main(void) {
> int a = 1;
> struct __Block_byref_c_0 c = {(void*)0,(struct __Block_byref_c_0 *)&c, 0, sizeof(struct __Block_byref_c_0), 1};
>
> int (*f)(int) = (int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a);
> [...]
> }
>
> I'm under the impression that the following "rewrite" would be correct:
>
> struct __main_block_impl_0 {
> struct __block_impl impl;
> struct __main_block_desc_0* Desc;
> int a;
> #ifdef __cplusplus
> __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
> impl.isa = &_NSConcreteStackBlock;
> impl.Flags = flags;
> impl.FuncPtr = fp;
> Desc = desc;
> }
> #endif
> };
> #ifndef __cplusplus
> #define __main_block_impl_0(fp, desc, _a, flags) \
> ((struct __main_block_impl_0){ \
> .impl = { \
> .isa = &_NSConcreteStackBlock, \
> .Flags = flags, \
> .FuncPtr = fp, \
> }, \
> .Desc = desc, \
> .a = (_a), \
> })
> #endif
>
> [...]
>
> int main(void) {
> int a = 1;
> struct __Block_byref_c_0 c = {(void*)0,(struct __Block_byref_c_0 *)&c, 0, sizeof(struct __Block_byref_c_0), 1};
>
> int (*f)(int) = (int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a);
> [...]
> }
>
>
> Am I correct, or am I missing something obvious ?
>
>
> --
> ·O· Pierre Habouzit
> ··O madcoder at debian.org
> OOO http://www.madism.org
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
More information about the cfe-dev
mailing list