I have a legacy C code on my hands, and I am given the task to filter dead/unused symbols and paths from it. Over the time there were many insertions and deletions, causing lots of unused symbols. I have identified many dead variables which were only being written to once or twice, but were never being read from.

Both blackbox/whitebox/regression testing proved that dead code removal did not affected any procedures. (We have a comprehensive test-suite).

But this removal was done only on a small part of code. Now I am looking for some way to automate this work.

We rely on GCC to do the work.

P.S. I'm interested in removing stuff like:

  1. variables which are being read just for the sake of reading from them.
  2. variables which are spread across multiple source files and only being written to.

For example:

int i;

extern int i;
  • Try profiling coverage, it might give you a good starting point for such an analysis.
    – SK-logic
    Jul 6, 2012 at 7:29

2 Answers 2


You should check what clang's scan-build can do for your build. It can sometimes determine if a variable is only written to, but the results is never used (Dead store in scan-build's language).

The problem all these tools have is of course that they cannot determine if a call like

int status = do_something(input_var);

does more than assigns some value to status, e.g. do_something might modify not only input_var, but also global state. So when cleaning out stuff make sure you check stuff that relies on side effects.

scan-build integrates nicely into make-based build process, e.g.

$ scan-build ./configure
$ scan-build make

would run your code through scan-build when using autotools.

You could also try cppcheck, but in my experience it doesn't find more than what scan-build finds (and scan-build can find other issues which cppcheck cannot see).


If I'm not mistaken, Gimpel's PC-Lint/FlexeLint is able to tell you if things are unused. I believe from when last I used it (many moons ago), it will pick out never-used variables, never-used functions, never-used headers, etc, etc.

It also has some kind of system for embedding tags to turn off it's warnings on particular lines of code, should you desire.

Note that when last I used this tool to find unused code paths, it was a multi-pass process - run tool, remove all unused stuff, run tool, find more unused stuff (because I'd just removed functions that were the only users of some other functions), etc, etc.

Kinda pricey, I'm afraid, but pretty good tools, and if you only need one license to get through this problem, rather than one per dev to integrate with your build, then it's not too bad. It looks like the PC licenses are much cheaper, so if only buying one for this job, I'd go for that.

  • Splint is unmaintained, but free. It behaves more or less like PC-Lint. Neither will show you logically dead code that still gets set or read.
    – Philip
    Jul 6, 2012 at 14:09
  • Nothing's perfect, but I know pc-lint will find unused variables, and functions that aren't called, which generally leads to finding further functions that aren't called, etc. Not perfect (we haven't solved the halting problem, so nothing can be perfect), but it's darn good start. Jul 6, 2012 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.