Find Unused Functions in Project C by Static Analysis

I am trying to run static analysis in C project to identify executable functions or code codes that are never called. I can create this project using Visual Studio.Net for Windows or using gcc for Linux. I tried to find a reasonable tool that can do this for me, but so far I have not succeeded. I read related questions about stack overflow, i.e. this and this , and I tried to use -Wunreachable-codewith gcc, but the output in gcc is not very useful. It has the following format

/home/adnan/my_socket.c: In function ‘my_sockNtoH32’: 
/home/adnan/my_socket.c:666: warning: will never be executed

but when I look at line 666 in my_socket.c, it is actually inside another function that is called from the my_sockNtoH32 () function and will not be executed for this particular instance, but will be executed when called from some other functions.

I need to find code that will never be executed. Can anyone help with this?

PS: I can’t convince management to buy a tool for this task, so please stick with free / open source tools.

+3
source share
3 answers

When the GCC says it will never be executed, it means that. You may have a bug that actually makes this dead code. For example, something like:

if (a = 42) {
    // some code
} else {
    // warning: unreachable code
}

Without looking at the code, of course, it is not necessary to be specific.

, 666 , , GCC .

+1

GCC , clang (, , ). ( , , ) , GCC ( ). Apple Xcode, .

+3

GCC . , . , - . , , , GCC , , , .

. , A a, B b, a. ? , . ; b , a b, a . , b & a X. , , ​​, , , a .

​​ "" , , - . a , ( , ) -. ( : , , , . C C, , a ).

Threading ; root, , , DAG . C, , , C , - , , .

, . , DMS Software Reengineering Toolkit C Front End , C, , . DMS , / , , ( ) . DMS 26 ( 18 000 ) .

[ : DMS , . , 95% , , . , C . , .]

Tools like GCC remove dead code at compile time. This is useful, but the dead code is still in your source code of the compilation unit, using the attention of the developers (they have to find out if it is dead too!). DMS in its program conversion mode can be configured modulo some problems with the preprocessor to actually remove this dead code from the source. On very large software systems, you really don't want to do this manually.

0
source

All Articles