Search code examples
iosxcodegccclanggcc-warning

How to suppress -Wno-protocol per file


I have implementation of Objective-C Protocol which forward all protocol's methods to another target. Everething is fine except that compiler warns that this Class doesn implement protocol's method. I am trying suppress this warning using #pragma diagnostic:

//Header file
@protocol A
-(void)test;
@end

@interface AImpl : NSObject<A> {
    id<A> myItems;
}
@end

//Implementation file:
#pragma GCC diagnostic push
#pragma clang diagnostic push

#pragma GCC diagnostic ignored "-Wno-protocol"
#pragma clang diagnostic ignored "-Wno-protocol"

@implementation AImpl
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];

    if ([myItems respondsToSelector:selector]) {
    [invocation invokeWithTarget:myItems];
} else {
    [super forwardInvocation:invocation];
}
}
@end

#pragma clang diagnostic pop
#pragma GCC diagnostic pop

But compiler warns that "Unknown warning group '-Wno-protocol'"


Solution

  • You've 3 immediate approaches:

    1) you can do this instead:

    #pragma GCC diagnostic ignored "-Wprotocol"
    #pragma clang diagnostic ignored "-Wprotocol"
    

    you specify the group to disable using the pragma, rather than the compiler flag to set/alter.

    2) or you can correct the issue like so (assuming you are not declaring a root class):

    @interface AImpl : NSObject<A>
    {
      id<NSObject,A> myItems;
    }
    @end
    

    3) or you can specify the setting on a per-file basis in Xcode's Project > Target > Build Phases > Compile Sources > Compiler Flags = "-Wno-protocol"

    I'd go with #2.