缺少桥强制转换会导致预处理源中出现错误,但不会导致实际源中出现错误 [英] Missing bridge cast causes error in preprocessed source but not in real source

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

问题描述

要编译源文件,请先使用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)

clang -x Objective-c -arch armv7s -fmessage-length = 0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit = 0 -std = gnu99 -fobjc-arc -Wno-trigraphs -fpascal-strings -Os -Wno-missing-field-initializers -Wno-missing-prototypes -Werror =返回类型-Wno-隐式原子属性-Werror =不建议使用-objc-isa-usage -Werror = objc根类-Wno-接收器弱-Wno-arc重复使用-弱-Wduplicate方法匹配-Won-missing-括号-Whesheses -Wswitch -Wunused函数-Wno-unused-label -Wno-unused参数-Wunused-变量-Wunused值-Wempty-body -Woninitialized -Wno -unknown-pragmas -Wno-shadow -Wno-四个字符常量-Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno -newline-eof -Wno选择器-Wno严格选择器匹配-Wundeclared选择器-Wno弃用的实现-DNS_BLOCK_ASSERTIONS = 1 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-clarations -g -fvisibility = hidden -Wno-sign-conversion -miphoneos-version-min = 7.0 -c main.m

clang -x objective-c -arch armv7s -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -fobjc-arc -Wno-trigraphs -fpascal-strings -Os -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-receiver-is-weak -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DNS_BLOCK_ASSERTIONS=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -g -fvisibility=hidden -Wno-sign-conversion -miphoneos-version-min=7.0 -c main.m

推荐答案

比较 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 preprocessed file. Since you are transforming the preprocessed source anyway, you can add these pragmas to the start/end of the preprocessed file again. Then there will be no warnings when compiling it.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆