I can ignore predefined Clang warnings as follows:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:selector];
#pragma clang diagnostic pop
But what if there's a custom warning? I mean
#warning Hello World
How do I ignore it?
There is a -W#warnings diagnostic, which I guess would do it.
Something like:
#pragma clang diagnostic ignored "-W#warnings"
Related
I recently updated an existing framework wrapping a C library to define a module (DEFINES_MODULE = YES in Xcode Build Settings).
The library isn't written by me so I'd like to avoid making changes if at all possible. Some of the library headers contain a few small errors, typically in the documentation. For example:
/**
* A function that does something.
* \param handle An object handle
* \param parameter A parameter value
* \return A status code
*/
int some_function(void *handle, double value);
When compiling the framework the compiler emits a warning:
Parameter 'parameter' not found in the function declaration
When using the framework from Objective-C, before compiling the framework as a module, I could disable diagnostics selectively at the site of the include:
#import <framework/header.h> // Generates a warning similar to the one above
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#import <framework/header.h> // Warning is suppressed
#pragma clang diagnostic pop
When the framework defines a module there seems to be no way to disable diagnostics, whether the framework is imported as a module or not:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#import framework; // Generates a warning
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#import <framework/header.h> // Generates a warning as well
#pragma clang diagnostic pop
Is there a non-global way to suppress warnings when importing frameworks that define modules?
I am trying to compile a simple CLR project, which has no dependency.
When I compile this project I get 973 warnings (C4945) stating that
c:\windows\microsoft.net\framework\v4.0.30319\system.dll : warning C4945: 'xxx' : cannot import symbol from 'c:\windows\microsoft.net\framework\v4.0.30319\system.dll': as 'xxx' has already been imported from another assembly 'System'
As I mentioned that my project has no dependency on other projects, I tried to compile my cpp files one by one, starting with stdafx.cpp.
While doing this I noticed that if I include #include <msclr\marshal.h> I get those warnings, and if I don't there are no warnings.
Now I have following queries.
Is #include <msclr\marshal.h> deprecated and replaced by something
else?
If no, how can I remove those warnings? Is #pragma warning disable the only way?
This is still a problem for VS2019, but you can force the header to skip #using <System.dll> like this:
#pragma push_macro("_CRT_WINDOWS")
#define _CRT_WINDOWS
#include <msclr\marshal.h>
#pragma pop_macro("_CRT_WINDOWS")
This works for the other marshal headers as well (marshal_cppstd.h, etc.)
Even when it is defined in the "...Tests" target Info.plist, the identifier remains nil. I need it to have a valid value for a third party lib.
This is because unit tests don't load your application bundle or even the unit test bundle as the main bundle.
For more info on this see
NSURL to file path in test bundle with XCTest
XCTest fails when calling [NSBundle mainBundle]
Now we can hack around this by including a category on NSBundle in your unit test project. The Objective-C category hack also works for swift unit tests because Bundle is bridged to NSBundle, just make sure you have a bridged header for your unit test project.
You can then have it search for the correct main bundle, but if you just want a quick and dirty way to have a valid bundle identifier then use the following
#interface NSBundle (BundleIdentifier)
-(NSString *)bundleIdentifier;
#end
#implementation NSBundle (BundleIdentifier)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(NSString *)bundleIdentifier
{
return #"com.yourcompany.yourapp.unitTests";
}
#pragma clang diagnostic pop
#end
Now all that is left is to include the header file in your unit test so that the category loads up.
Is there any way to let clang ignore errors when compiling the code? Such as the missing header errors.
I am using -emit-llvm option of clang to compile some programs to llvm bitcode, however, I don't care if some header file is missing.
When using Xcode analysis (product>analyze) is there a way of ignoring any errors in a given file?
Eg a pragma or something?
We just want to ignore any warnings on third party code, so that it's more obvious to us when there's a problem in our code.
Thanks
As suggested by matthew:
#ifndef __clang_analyzer__
...
#endif
#pragma clang diagnostic ignored "-Wall"
See the clang user's manual for other useful related #pragmas for clang.
This also works for GCC.
#pragma GCC diagnostic ignored "-Wall"