tags:

views:

308

answers:

4

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!

A: 

It can only report on what it sees. Ignore the warning or follow the instructions to inhibit it if you know better than what it says. Don't assume that a tool like this necessarily knows your program better than you do.

If it really is not intended to be used outside of the file, you can declare it static and it should correct the problem, but it will be inaccessible from other files.

Volte
But thats the point... if I put it into another file the warning goes away!?!?!?!??!
ojblass
splint's diagnosis is correct, within the context of its input (i.e. the only possible kind of correct for a computer program). The problem is that you are interpreting it as a dire warning rather than an observation that has been generated given the parameters given to it. You can ignore it safely.
Volte
+1  A: 

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

You have answered your question. You've fed in one file to lint, so lint knows there is only file to be taken care of (apart from the standard header includes, of course).

dirkgently
+1  A: 

int flip() is not declared as static, so it can be potentially used externally. Since you invoked splint with only one source file, it correctly says that your function, if not used externally, must be declared static

dfa
+2  A: 

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'.

Jonathan Leffler
+1, if a function is public it should be declared in a header, otherwise it should be static.
Bastien Léonard
And you are right... forward declaring it in a header file shuts it up and it was complaining about blah as well.
ojblass