Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically deleting unused local variables from C source code

I want to delete unused local variables from C file. Example:

int fun(int a , int b)
{
  int c,sum=0;
  sum=a + b;
    return sum;
}

Here the unused variable is 'c'.

I will externally have a list of all unused local variables. Now using unused local variables which I have, we have to find local variables from source code & delete.
In above Example "c" is unused variable. I will be knowing it (I have code for that). Here I have to find c & delete it .

EDIT

The point is not to find unused local variables with an external tool. The point is to remove them from code given a list of them.

like image 528
user65514 Avatar asked Feb 18 '09 12:02

user65514


2 Answers

Turn up your compiler warning level, and it should tell you.

Putting your source fragment in "f.c":

% gcc -c -Wall f.c
f.c: In function 'fun':
f.c:1: warning: unused variable 'c'
like image 176
Paul Beckingham Avatar answered Nov 15 '22 19:11

Paul Beckingham


Tricky - you will have to parse C code for this. How close does the result have to be? Example of what I mean:

int a, /* foo */
    b, /* << the unused one */
    c; /* bar */

Now, it's obvious to humans that the second comment has to go.

Slight variation:

void test(/* in */ int a, /* unused */ int b, /* out */ int* c);

Again, the second comment has to go, the one before b this time.

In general, you want to parse your input, filter it, and emit everything that's not the declaration of an unused variable. Your parser would have to preserve comments and #include statements, but if you don't #include headers it may be impossible to recognize declarations (even more so if macro's are used to hide the declaration). After all, you need headers to decide if A * B(); is a function declaration (when A is a type) or a multiplication (when A is a variable)


[edit] Furthermore:

Even if you know that a variable is unused, the proper way to remove it depends a lot on remote context. For instance, assume

int foo(int a, int b, int c) { return a + b; }

Clearly, c is unused. Can you change it to ?

int foo(int a, int b) { return a + b; }

Perhaps, but not if &foo is stored int a int(*)(int,int,int). And that may happen somewhere else. If (and only if) that happens, you should change it to

int foo(int a, int b, int /*unused*/ ) { return a + b; }
like image 27
MSalters Avatar answered Nov 15 '22 21:11

MSalters