Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

info : cc Compiler option

Kernel Tunable parmeter
Occasional Contributor

info : cc Compiler option

Hi, I have simple c program in which Function prototype and Function definition is present, but I am not calling that function, is there any way to check this during compiler time and linker time or any "cc" complier that through error or warning.
Simple program.
main ()
{
void add();
}

void add()
{}
in the above program "add" function prototype and definition is present but I am not calling it,so it there any option with "cc", that through error while compiling the program.
6 REPLIES
Joseph Huber_1
Honored Contributor

Re: info : cc Compiler option

You are asking the compiler to flag something as an error which IS NOT an error!

And if You just mean to flag an uncalled function in the current program unit as informational, how should the compiler know if the source file just includes a function called by some other part/library linked to this main program ?

If at all, then this could be an optional task for the linker/crossrefencer. In fact the VMS linker does this already by listing the routine(s) as defined, but not as referenced:

Symbol Value Defined By Referenced By ...
------ ----- ---------- -----------------
ADD 00010060-R TMP
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: info : cc Compiler option

And to answer the untold question as to why the source of a program includes unused function definitions:
often a library package needs user defined call back functions, e.g. sorting libraries use user defined comparison functions, or cost functions in operations research/optimization.
It is good practice to keep the user routines together with the user main programs.

Of course one can omit the prototype for those uncalled routines, but then again it is better to keep the prototypes in an #include file supplied by the associated library, to detect mismatches at compile time.
http://www.mpp.mpg.de/~huber
Dennis Handly
Acclaimed Contributor

Re: info : cc Compiler option

Are you asking about mismatches or unreferenced functions? If the function declaration and definition are in the same comp unit, the header is included with the source file, then the compiler can compare the two. Otherwise you need something like lint that looks at lots of source files. If you aren't calling it, no mismatches can be checked at link time. Though a linker could remove unreferenced functions.

>main()

For C99 this should be: int main()

>void add();

This is NOT a valid prototype for C but perfectly valid for C++. You need to have: void add(void);

Since your declaration is inside main, it probably won't be compared with the later definition. I.e. all file scope declarations should be moved outside of functions.
Hoff
Honored Contributor

Re: info : cc Compiler option

That code may or may not be unused. In your contrived single-module example, yes, it is unused. In a larger and particularly in a non-trivial and multiple-source-module example, that status is not quite as clear to a compiler.

As for an interpretation of your question and depending on your goal and your C compiler and your operating system, there can be options here.

VMS with the HP/Compaq/DEC C compilers:

$ CC /WARN=WARNINGS=UNUSED

The following are found in various clang and gcc compilers:

-Wunreachable-code

-Wunused

-Wunused-function

If you're using gcc, have a look at gcov. OpenVMS has no analog of gcov, nor tools such as valgrind. The closest analog is probably the DECset Performance and Coverage Analyzer (PCA) tool.

As for questions such as these, Google can be your friend here. With the exception of the VMS qualifier syntax, all of this code coverage information gets asked regularly around the Internet, and discussions are readily available via tools including Google.

For a discussion and an example of both what is available using Google and of what Mr Huber is referring to with the inherent local- and global-reference aspects of this particular question, please see this forum posting:

http://stackoverflow.com/questions/4813947/how-can-i-know-which-parts-in-the-code-are-never-used

Details and options can also available via the man pages and HELP text with the platforms, as well; with the software documentation. Reading the manuals does not signify an admission of defeat, after all.

I would also encourage use of the "static" keyword with any of the truly-local functions, as this helps the compiler with determining the status of functions and particularly around whether the functions can be fully inlined and/or the entry-point and even the code can be eliminated. Put another way, this keyword is how you flag local code within the compilation unit.

C in particular has been a moving target on all platforms over the past twenty years or so, and particularly on VMS. If you've been at this a while and haven't re-read the full C doc in the past five or ten years, go do that now. gcc and particularly llvm/clang are very interesting C/C++ compilers, with piles of useful features. (VMS C changed pretty quickly for a number of years there, too, though it now includes only partial support for C99 and AFAIK none of C1X; partial C99 is the apparent end of the line for the VMS C compiler.)

Also, please go read this 3-part series of postings from one of the folks working on the llvm/clang C compiler:

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html

For posting questions (a skill which is harder than it looks), please also read and heed this famous article:

http://www.catb.org/~esr/faqs/smart-questions.html
Hoff
Honored Contributor

Re: info : cc Compiler option

ps: see:

http://en.wikipedia.org/wiki/Unreachable_code

and links from there.
Joseph Huber_1
Honored Contributor

Re: info : cc Compiler option

Well maybe because on Sunday my system is not professional :-), but neither The VMS /warning nor one of the 'gcc -W' is warning on the OPs code.

A different case would be an unused static function, these are warned through -Wunused-function, because a static function cannot be called from outside file-scope.
http://www.mpp.mpg.de/~huber