缺少桥强制转换会导致预处理源中出现错误

缺少桥强制转换会导致预处理源中出现错误

本文介绍了缺少桥强制转换会导致预处理源中出现错误,但不会导致实际源中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要编译源文件,请先使用clang对其进行预处理,然后再对其进行编译.因此,如果运行 clang -E ,我应该得到一个预处理文件,可以使用 clang -c 进行编译.但是以下代码在经过预处理后无法编译.

To compile a source file, clang preprocesses it at first and then compiles it. So if I run clang -E, I should get a preprocessed file, that can be compiled with clang -c. But the following code doesn't compile after preprocessing it.

int main(int argc, char * argv[])
{
    NSString* foo = @"bar";

    CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
        NULL,
        (CFStringRef)foo,
        NULL,
        (CFStringRef)@"",
        kCFStringEncodingUTF8 );

    CFRelease(urlString);

    return 0;
}

它使用 clang -c 进行编译,而忽略foo被强制转换为不带__bridge的CFStringRef.在对代码进行预处理后,它将不再编译,并且clang抱怨缺少__bridge强制转换.是否有禁用此行为的标志或解决此问题的方法?

It compiles with clang -c ignoring that foo is cast to CFStringRef without __bridge. When the code is preprocessed, it doesn't compile anymore and clang complains about missing __bridge cast. Is there a flag to disable this behavior or a method to work around this?

完整的clang命令(用于与-E一起编译和预处理)

Full clang command (used it for compiling and preprocessing with -E)

推荐答案

比较 ARC-隐式桥接 :"CFString.h"和其他Core Foundation标头包含宏

Compare ARC - implicit bridging:"CFString.h" and other Core Foundation headers contain the macros

CF_IMPLICIT_BRIDGING_ENABLED
...
CF_IMPLICIT_BRIDGING_DISABLED

展开为

_Pragma("clang arc_cf_code_audited begin")
...
_Pragma("clang arc_cf_code_audited end")

,这会使Clang不再抱怨缺少__bridge强制类型转换.

and that causes Clang to not complain about missing __bridge casts.

杂物由预处理器消耗",因此不在预处理中文件.由于无论如何都要转换预处理的源,因此您可以添加这些实用程序再次到达预处理文件的开头/结尾.这样编译时就不会发出警告.

The pragmas are "consumed" by the preprocessor and therefore not in the preprocessedfile.Since you are transforming the preprocessed source anyway, you can addthese pragmas to the start/end of the preprocessed file again.Then there will be no warnings when compiling it.

这篇关于缺少桥强制转换会导致预处理源中出现错误,但不会导致实际源中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:56