What's the -Wsomething flag for 'instance

2020-02-25 22:42发布

问题:

I recently had a case where someone added a parameter to an init method and broke another project that shared the code. Since it's only a warning, nobody realized the app was broken, so I'm trying to turn only this warning into an error:

warning: instance method '-someMethod' not found (return type defaults to 'id')

I've found that you can pass -Werror=foo in Other C Flags to the compiler in Xcode to turn a warning into the error, but I can't seem to find what 'foo' should be. I've tried 'undeclared-selectors' but that only catches @selector cases. I've tried -Werror-implicit-function-declaration but that doesn't seem to catch that case either.

I tried 'inst-method-not-found' and 'instance-method-not-found' after finding 'warn_inst_method_not_found' during a casual search of the huge clang source code.

Help ... ?

Update: Here's an example you can compile (e.g. in CodeRunner) to see the warning: https://gist.github.com/4045701

回答1:

The option you're looking for is -Werror=objc-method-access. Clang explicitly tells you this right in the warning message, if you download and compile that gist you posted:

% clang test.m -c
test.m:13:21: warning: instance method '-initWithNum:' not found (return type
      defaults to 'id') [-Wobjc-method-access]
  ...theObj = [[MyClass alloc] initWithNum: [NSNumber numberWithInt: 15]];
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

% clang test.m -Werror=objc-method-access -c  // ta-da!

But in real-world situations I agree with all the commenters above: You should fix or suppress all compiler warnings. Your build should build cleanly all the time. Otherwise, as you so rightly observed, you won't be able to distinguish real bugs from "the usual spam".

FWIW, here's the version of Clang I'm using:

$ clang --version
clang version 3.2  (http://llvm.org/git/llvm 1503aba4a036f5394c7983417bc1e64613b2fc77)
Target: x86_64-apple-darwin12.2.0
Thread model: posix