问题
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;
}
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?
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=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
回答1:
Compare ARC - implicit bridging: "CFString.h" and other Core Foundation headers contain the macros
CF_IMPLICIT_BRIDGING_ENABLED
...
CF_IMPLICIT_BRIDGING_DISABLED
which are expanded to
_Pragma("clang arc_cf_code_audited begin")
...
_Pragma("clang arc_cf_code_audited end")
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.
来源:https://stackoverflow.com/questions/21142731/missing-bridge-cast-causes-error-in-preprocessed-source-but-not-in-real-source