Find and delete unused variable declarations

While developing a fairly extensive project I've created a lot of global variables and many of them may now be redundant.
Its making the program difficult to follow.

Is there any easy way to find them so I can delete them?

There are a few old threads about this but pre-date IDE2

Comment them all out and compile the code. Uncomment the ones that cause errors. Once compiled successfully, delete any remaining commented out declarations.

More sophisticated IDEs have the feature you want, like IntelliJ-IDE and PyCharm, but you can't use those with Arduino code, and Arduino IDE is free.

3 Likes

static analyzer command line tools such as cppcheck or clang --analyze could probably report unused variables .

(if you have multiple .ino it's more tricky as they are combined before compilation and so the IDE breaks the cpp variable scope rules and you would need to combine the files yourself for exploration )

That would be a useful feature, back in the old days compilers produced cross references but now not so much.

Possibly cppcheck can find unused variables.

If you turn up the level of compiler warnings, maybe there will be warnings about unused declarations. I guess you will get an ocean of other warnings too, so I would deliberately declare something you know isn't used like int iAmNotNeeded;. Find that in the compiler warnings output and you will know the exact format of the warning, enabling you to find others.

1 Like

Unused variables may be removed from binary code by the optimizing linker.

If you declare a global variable static, you will get unused variable warnings for these variables, just as you do with the local variables:

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wunused-variable

Warn whenever a local or static variable is unused aside from its declaration.

For example, if you compile this code with warnings enabled:

static int foo;
int bar;
void setup() {}
void loop() {}

You will get a warning like this:

C:\Users\per\Documents\Arduino\sketch_apr27a\sketch_apr27a.ino:1:12: warning: 'foo' defined but not used [-Wunused-variable]
 static int foo;
            ^~~

(a warning for the unused foo, but not for the unused bar)

That's correct, but how will it help?

If the variable is used in another module then the linker warns about a missing declaration and you know that the variable can not be removed.

Any downside to declaring static? If not, then why not always use?

Do you think that is likely to occur in the average Arduino sketch project?

Global variables declared as static in one compilation unit (eg .cpp file) are unavailable in other compilation units. Whether or not that's a down side depends on your use case.

I seldom have more than one compile unit in my little projects these days, so no big deal.

I may not live that long!

@J-M-L yes, multiple .ino's and .h's

@ptillisch editing to rename them all "static" wouldnt be easy as I have rather a lot of different variable types and multiple iso's and .h's.

The lesson I think is to be more systematic in declaring and naming variables, and also to rely less on global declarations.

I need to move some bits of code to different .isos and when I've done that I'll print them out and see where locals could be used instead of globals.
Thanks all for your advice

That does not make it easy to use the standard tools unless you merge them yourself

Working out of the temporary directory used during compile time a would make things easier as all the code would be there.

Using .h and .cpp files in the standard way would go a long way toward solving issues like this. It would also provide your code the modularity that using the "Arduino Way" of multiple .ino files doesn't. See My Post #5 in this Thread for a basic guide to break up large programs into .cpp / .h files.

CTRL-F (find) the variable (without the # sign) will give you a number of how many times a search term is used. If the result is 1, then the variable is not used anywhere else in the sketch.

Put all the actual global definitions in a single file. "globals.c", "globals.ino", whatever.

The linker, as used by the Arduino IDE, does automatically omit unused globals from the final binary, and some boards (ARM boards in particular) do produce a linker map in the build directory. If you're using such a board, or if your code can be compiled for such a board (even if it's not the a actual target), the end of the .map file has a list of "Discarded input sections" that will include unused global variables from your sketch (plus a lot from the Arduino core, alas.)

If I add a line int z, zz, zzz, zzzz; to the Blink example and compile for Arduino Zero, the Blink.ino.map produce will contain:

Discarded input sections

 .text          0x0000000000000000        0x0 /Applications/Arduino-1.8.13.app/Contents/Java/portable/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v6-m/crti.o
 .data          0x0000000000000000        0x0 /Applications/Arduino-1.8.13.app/Contents/Java/portable/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v6-m/crti.o
   :
   : bunch more discarded segments from the core.
   :
var/folders/gv/zn3wcml52jq0vvjnd95j8g6h0000gp/T/arduino_cache_970164/core/core_arduino_samd_arduino_zero_native_2df40d67209c4d62f9ef8cfcc9f8a451.a(hooks.c.o)
                                                  /tmp/Arduino1.8.13Build/../../var/folders/gv/zn3wcml52jq0vvjnd95j8g6h0000gp/T/arduino_cache_970164/core/core_arduino_samd_arduino_zero_native_2df40d67209c4d62f9ef8cfcc9f8a451.a(delay.c.o)
z                                                 /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zz                                                /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zzz                                               /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zzzz                                              /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o

(the lines thaht contain "/sketch/sketchname.ino.cpp.o" are the ones you're looking for.)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.