Search code examples
csplint

How does splint know my function isn't used in another file?


Splint gives me the following warning:

encrypt.c:4:8: Function exported but not used outside encrypt: flip
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   encrypt.c:10:1: Definition of flip

Since I called splint only on this file how does it know that?

#include        <stdio.h>
#include        <stdlib.h>

int    flip( int a)
{
        int b;
        b = a;
        b ^= 0x000C;
        return b;
}

int     blah(int argc, char    *argv[]) {

        FILE    *fp = NULL, *fpOut=NULL;
        int             ch;
        ch = 20; flip(20); return (ERROR_SUCCESS);
}

I even got rid of main so that it could not figure out that the file is complete in any way. I am totally stumped!


Solution

  • You might find that if you included a header that declared flip() - as you should, of course - then splint would not complain. You should also declare blah() in the header as well.

    I'm not wholly convinced that this is the explanation because blah() is not used at all (though it uses flip()) and you don't mention splint complaining about that.

    However, it is a good practice to make every function (in C) static until you can demonstrate that it is needed outside its source file, and then you ensure that there is a header that declares the function, and that header is used in the file that defines the function and in every file that uses the function.

    In C++, the 'every function should be static' advice becomes 'every function should be defined in the anonymous namespace'.