Is there a way to identify all the unused global variable in a C project?

后端 未结 6 1076
清歌不尽
清歌不尽 2021-01-17 22:28

I\'m cleaning up some C code. There are global variables everywhere, but not all of them are used. I want to clean up those. But it\'s too much work to test them one by one.

6条回答
  •  一向
    一向 (楼主)
    2021-01-17 22:44

    Easy? No. For a global variable X, you need to scan every compilation unit in your code for a potential access to X (a read, a write, or the generation of a reference). If there are no such accesses, then you can be reasonably sure (you don't have any assembly code, right?) that X isn't used.

    It may even be the case that X is referenced in one of the above ways, but in fact has no actual effect on the program (e.g., X is read but ignore, written but not read, address taken but never dereferenced). That makes X effectively dead. Determining this requires global data flow analysis.

提交回复
热议问题