GCC will warn on unused functions, labels, etc. But it seems it doesn't warn on unused definitions? Is there a way to detect unused struct definitions automatically?
Asked
Active
Viewed 306 times
3
-
1I don't think you want to be warned about any unused struct definition in various library headers. And in order to perform such check only in your files you can write a simple python script... – Alex Lop. Sep 02 '20 at 04:28
-
1GCC will warn on unused **static** functions. It will not warn on unused global functions because that would (1) require global program analysis and (2) make using libraries impossible. You almost never use *all* functions from any given library. Types are global so the same reasoning applies to them. – n. m. could be an AI Sep 02 '20 at 04:54
-
1Looking for unused structs is a minefield. First you have to define what used means. If the struct appears in a typedef or a union or another struct, is that a use if that typedef, union or struct is never used. How about nameless structs? – cup Sep 02 '20 at 05:32
-
Really? GCC warns on unused labels? I never tried but I'd assume it only does so if the statements after the label are not reached by another way, and the diagnostic will be "dead code". – the busybee Sep 02 '20 at 05:52
-
@AlexLop. I doubt that script to be "simple". It will rapidly approach a C parser. Are you thinking of a specific library or module which would make implementing such a script easy? – Yunnosch Sep 02 '20 at 06:08
-
@Yunnosch I agree that it may be not that simple when implementing it for any possible C syntax writing. But assuming specific coding convention for personal usage shouldn’t be too complicated – Alex Lop. Sep 02 '20 at 06:23
-
@AlexLop. "Should not be" but is. I tried (admittedly in a different language than python, that is why I ask about a lib you have in mind). And with some pretty tight coding rules in place which should have made the input code predictable via some assumptions.... – Yunnosch Sep 02 '20 at 06:26
1 Answers
3
You won't necessarily be able to identify individual structs but there is a tool called include-what-you-use
which allows you to eliminate unused includes. See here:
https://include-what-you-use.org/
and here for using with CMake: How to use the tool include-what-you-use together with CMake to detect unused headers?

Den-Jason
- 2,395
- 1
- 22
- 17