How to disable the warning for pragma message in Clang? - clang

#pragma message is implemented in Clang as warning.
Is there any way to disable the warning only for this pragma, so it will be have like MS Visual Studio, or some other way to give informational messages from code ?
(I need an answer as an end user with a given Clang, I cannot re-compile the Clang itself on this project)

I realize this post is a year old and the OP probably already found his answer but since I was looking for the answer myself I thought I may as well include the info here.
From the Clang user's manual:
https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas
The pragma may control any warning that can be used from the command line. Warnings may be set to ignored, warning, error, or fatal.
Attempting to set the class to something other than the above:
#pragma clang diagnostic push
#pragma clang diagnostic note "-W#pragma-messages"
#pragma message "HELLO!"
#pragma clang diagnostic pop
int main(int argc, char **argv)
{
}
Results in a warning showing the same options:
/tmp/test.cc:3:26: warning: pragma diagnostic expected 'error', 'warning', 'ignored', 'fatal', 'push', or 'pop' [-Wunknown-pragmas]
#pragma clang diagnostic note "-W#pragma-messages"
^
/tmp/test.cc:4:9: warning: HELLO! [-W#pragma-messages]
#pragma message "HELLO!"
^
2 warnings generated.
So it appears that as of Clang 5.0 there is no way to treat the message as informational instead of as a warning.

Related

Ignore CGFloat type warning

I've got a function with such part of code:
if (CGFLOAT_IS_DOUBLE) {
return fabs(x / y);
} else {
return fabsf(x / y);
}
However with new Xcode I'm obtaining a warning here:
Absolute value function 'fabsf' given an argument of type 'double' but
has parameter of type 'float' which may cause truncation of value
The warning wasn't visible in Xcode 6, but in Xcode 7 Beta it is.
With such wrapping, I'm moreover sure that the values are float type. Now the question is how to suppress the warning?
I've tried with many Clang flags, ended on most general:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
// above code
#pragma clang diagnostic pop
But it doesn't help (odd?). "-W" also doesn't help. Any tip..?
I don't want to cast, create extra float variables nor anything similar. I just want to silence the warning.

Return statement will never be executed warning

I've recently come across with double return statement (one of them was added by mistake) in our project and wondering why compiler doesn't show a warning for that?!
Ok, I added -Wunreachable-code to other warning flags, but still no luck.
Got warning - with a code to execute after return statement:
Didn't get warning but still second return statement will never be executed.
Even if I add something like this, still no warning
Is there extra warning flag for that, or compiler isn't smart enough?
Good catch!
-Wunreachable-code does not report a warning and there is no other warning flag which would do.
Not even the Static Analyzer catches this mistake!
(Tested with XCode 6.1 GM 2)
Wrap your code in pragma flags that will suppress that warning between the push and pop
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunreachable-code"
//Your code goes here
#pragma GCC diagnostic pop
Use -Wno-unreachable-code for your file in Build Phases.
Try this...
-(NSString*) makeSomeStringForMe {
NSString *someString = #"some string";
return someString;
}

How to suppress "Availability does not match previous declaration" warning

I have to use some private functions, like:
SCDynamicStoreRef
SCDynamicStoreCreate (
CFAllocatorRef allocator,
CFStringRef name,
SCDynamicStoreCallBack callout,
SCDynamicStoreContext *context
) __OSX_AVAILABLE_STARTING(__MAC_10_1,__IPHONE_NA);
By default they are not allowed for iphone, so I've changed declaration of them in my .m file. But now it shows "Availability does not match previous declaration" warning. How to suppress this warning?
With the usual disclaimer that using private APIs might cause your app to be rejected:
You can suppress the warning with
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wavailability"
…
#pragma clang diagnostic pop
It might also cause crashes or other failures if that function is present in the iOS
frameworks, but with different parameters.

#pragma objective-c: can you have more than just 'mark'?

I am familiar with #pragma mark objective-c / xcode / ios development and that it is useful for finding sections of code.
However, I am wondering if there are other keywords other than 'mark'. Like, can you do #pragma somethingelse? Thanks in advance!
First, some examples:
You can control diagnostics:
http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas
And from the same link:
clang supports the Microsoft "#pragma pack" feature for controlling record layout. GCC also contains support for this feature, however where MSVC and GCC are incompatible clang follows the MSVC definition.
clang supports the Microsoft #pragma comment(lib, "foo.lib") feature for automatically linking against the specified library. Currently this feature only works with the Visual C++ linker.
clang supports the Microsoft #pragma comment(linker, "/flag:foo") feature for adding linker flags to COFF object files. The user is responsible for ensuring that the linker understands the flags.
The second and third from that list won't apply to your iOS code, though.
Wikipedia [link] says that clang supports #pragma once, too.
And finally, here's a link to the clang API documentation for the pragma handling code. You can browse from there to see everything else. In particular, TokenKinds.def describes all the accepted tokens, so presumably it's complete:
#pragma unused
#pragma GCC visibility [push/pop]
#pragma pack [value/show/push/pop/etc/etc/etc]
#pragma clang __debug parser_crash
#pragma clang __debug captured
#pragma ms_struct [on/off]
#pragma align [native/natural/mac68k/power/reset]
#pragma weak [identifier]
#pragma weak [identifier = identifier] // alias
#pragma redefine_extname [identifier identifier]
#pragma STDC FP_CONTRACT
#pragma OPENCL EXTENSION
#pragma omp [...]
#pragma detect_mismatch
#pragma comment
The parsing code, found in ParsePragma.cpp, seems to indicate that not all of them are implemented, even if the front-end accepts them, though.
Yes. It's often used for implementation defined directives, but there are a few which are defined in C.
6.10.6 Pragma directive Semantics 1
A preprocessing directive of the form # pragma pp-tokensopt new-line where the preprocessing token STDC does not immediately
follow pragma in the directive (prior to any macro replacement)
causes the implementation to behave in an implementation-defined
manner. The behavior might cause translation to fail or cause the
translator or the resulting program to behave in a non-conforming
manner. Any such pragma that is not recognized by the implementation
is ignored.
If the preprocessing token STDC does immediately follow pragma in the directive (prior to any macro replacement), then no macro
replacement is performed on the directive, and the directive shall
have one of the following forms whose meanings are described
elsewhere:
#pragma STDC FP_CONTRACT on-off-switch
#pragma STDC FENV_ACCESS on-off-switch
#pragma STDC CX_LIMITED_RANGE on-off-switch
`on-off-switch`: one of `ON OFF DEFAULT`
Carl Norum's answer covered examples of implementation defined directives well. For a complete list, you should refer to your compiler's documentation.

iOS: check system version in #define and do different stuff, compiler error

I am using MKReverseGeocoder for iOS < 5 and CLGeocoder for iOS >= 5, but got a warning because MKReverseGeocoder is deprecated, then I tried to do something like:
#define SYSTEM_LOWER_THAN_5 ([[[UIDevice currentDevice] systemVersion] compare:#"5.0" options:NSNumericSearch] == NSOrderedAscending)
#if SYSTEM_LOWER_THAN_5
#interface TelstraLocationService () <MKReverseGeocoderDelegate>
#else
#interface TelstraLocationService ()
#endif
but I got an error:
Invalid token at start of a preprocessor expression
This is caused by the macro that can only be evaluated at runtime. Is there a way to get rid of the warning (without changing deployment target)?
If your goal is to simply get rid of the compiler warning (since you have accounted for any potential problems and made the explicit decision to ignore it), surround the triggering code with these compiler directives:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// some code that uses something deprecated
#pragma clang diagnostic pop
You want to be as specific as you can to avoid inadvertently ignoring other valid deprecation warnings. Surround just the MKReverseGeocoderDelegate if you have another protocol before and after it, otherwise just surround the entire #interface block:
// option 1
#interface TelstraLocationService () <
SomeProtocol
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
MKReverseGeocoderDelegate
#pragma clang diagnostic pop
AnotherProtocol>
// option 2
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#interface TelstraLocationService () <MKReverseGeocoderDelegate>
#pragma clang diagnostic pop
Surround any calls to reverseGeocoder:didFindPlacemark: or reverseGeocoder:didFailWithError: with the same compiler directives to silence deprecation warnings for them as well.

Resources