Is it possible to avoid FP when there is #ifdef in the code

Hello!

Sometimes there are false positives when #ifdef is used and for instance usage of some macro/parameter is hidden.

I am running Clang in a project where we get several such FPs.

Example code:

void f(int x) {
    // ..code..
#ifdef XYZ
    dostuff(x);
#endif
    // ..code..
}

Warning:
1.c:1:12: warning: unused parameter 'x' [-Wunused-parameter]
void f(int x) {
           ^

Is there anything that can be done in the unused-parameter checker to prevent such FP?

For instance.. is it possible to see if there was a #ifdef in a function and disable the warning in that function.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB Warfvinges väg 34 SE-112 51 Stockholm Sweden

Mobile: +46 (0)709 12 42 62
E-mail: Daniel.Marjamaki@evidente.se

www.evidente.se

I don't think it's fair to call this a false positive, unless the "XYZ" macro is defined.

The analyzer has to run the preprocessor to handle basic things like #include. Modifying the checker to do what you propose would require going from the AST -> source code, scanning for a preprocessor command, altering it, re-parsing it, re-running the checker etc.

It would be easier to add a line like "(void) x;" to suppress the warning.

best
vedant

I don't think it's fair to call this a false positive, unless the "XYZ"
macro is defined.

The analyzer has to run the preprocessor to handle basic things like
#include. Modifying the checker to do what you propose would require going
from the AST -> source code, scanning for a preprocessor command, altering
it, re-parsing it, re-running the checker etc.

It would be easier to add a line like "(void) x;" to suppress the warning.

Other possible workarounds (in case you have several unused variables):
- You can use pragmas to disable some diagnostics for certain regions of
code.
- You can use compiler flags to disable some diagnostics for certain
translation units.
- You can always factor out platform dependent code to multiple files and
have the #ifdef on includes.

In general I do like that the compiler finds unused variables. Sometimes
this uncovers real bugs. Sometimes it just helps to maintain readable code.
IMHO it is better to have false positives than false negatives for this
warning.

Hello!

I don't think it's fair to call this a false positive, unless the "XYZ" macro is defined.

I also agree it is not fair to call this a false positive. But technically it is a false positive. You and I can see that the variable is used.

Well.. I had not much hope it would be possible. But asked anyway.

Adding a redundant (void)x should be ok.

Thanks!
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB Warfvinges väg 34 SE-112 51 Stockholm Sweden

Mobile: +46 (0)709 12 42 62
E-mail: Daniel.Marjamaki@evidente.se

www.evidente.se

Windows.h defines UNREFERENCED_PARAMETER() for this purpose. When I’m not working with code that includes that header I define it as:
UNREFERENCED_PARAMETER(P) (void)(P)

Unfortunately I couldn’t use the same definition as was used in windows.h with clang without triggering another warning.

Technically it *is* a false positive. The compiler cannot see the
variable being used.

Csaba