arduino-cli
arduino-cli copied to clipboard
Prototype incorrectly generated for struct member function under certain conditions
Prototype incorrectly generated for struct member function under certain conditions
Describe the problem
In order to make it easier for beginners to get started with writing Arduino sketches, and for the convenience of all users, Arduino CLI automatically generates and adds prototypes for functions defined in a .ino
file of a sketch.
Under certain specific conditions, a prototype is incorrectly generated for struct
member functions:
- An
enum
is declared above the function. - The
enum
has an "enum-base" (e.g.,enum bar_t : int
). - The
enum
contains a comma (either a trailing comma on a single identifier (e.g.,{pippo,}
) or separating multiple identifiers (e.g.,{pippo, pluto}
).
🐛 The prototype causes a spurious compilation failure of what would otherwise be valid code.
To reproduce
Setup environment
$ arduino-cli version
arduino-cli Version: git-snapshot Commit: c5812eea6 Date: 2024-09-02T17:16:38Z
$ mkdir "/tmp/FooSketch"
$ printf 'struct Foo {\n enum bar_t : int {pippo, pluto};\n void baz() {}\n};\nvoid setup() {}\nvoid loop() {}\n' > "/tmp/FooSketch/FooSketch.ino"
Demo
$ arduino-cli compile --fqbn arduino:avr:uno "/tmp/FooSketch"
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:3:8: error: 'void Foo::baz()' cannot be overloaded
void baz() {}
^~~
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:3:6: error: with 'void Foo::baz()'
void baz() {}
🐛 There was a spurious compilation failure of valid code.
By looking at the C++ code generated by the Arduino sketch preprocessor, we can see the cause of the error:
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "/tmp/FooSketch"
#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
struct Foo {
enum bar_t : int {pippo, pluto};
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void baz();
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void baz() {}
};
void setup() {}
void loop() {}
🐛 The spurious compilation failure was caused by Arduino CLI creating a prototype for the baz
function.
🐛 Prototypes for the setup
and loop
functions were not generated as expected.
Expected behavior
Prototype generation should always be skipped for struct member functions.
Arduino CLI version
Original report
0b77c0a07fd24d9c166754b2a066665d7a75d941
Last verified with
c5812eea68afdb0e4e774f4f15ec4a34f0c3100c
Operating system
- Linux
- Windows
Operating system version
- Debian Bookworm/sid
- Windows 11
Additional context
The behavior is as expected when the sketch code doesn't have the specific conditions described above. For example, this sketch compiles without any error because a function prototype is not generated for baz
:
struct Foo {
void baz() {}
};
void setup() {}
void loop() {}
Preprocessed sketch:
#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
struct Foo {
void baz() {}
};
#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void setup();
#line 6 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void setup() {}
void loop() {}
Related
- https://github.com/arduino/arduino-cli/issues/1191
- https://github.com/arduino/arduino-cli/issues/1253
- https://github.com/arduino/arduino-cli/issues/1269
- https://github.com/arduino/arduino-cli/issues/1591
- https://github.com/arduino/arduino-cli/issues/1618
- https://github.com/arduino/arduino-cli/issues/1785
- https://github.com/arduino/arduino-cli/issues/2696
- https://forum.arduino.cc/t/is-the-location-of-a-global-function-important/996378/18
- https://forum.arduino.cc/t/ide-mistakes-javascript-function-for-type/953022
- https://forum.arduino.cc/t/c-super-quotes-compiler-bug/990071
- https://forum.arduino.cc/t/over-length-line-in-editor-forces-forward-declaration/955571
- https://forum.arduino.cc/t/solved-multiple-ide-tabs-issue/988086
Workaround
Put the struct
in a .h
file. Only .ino
files undergo Arduino sketch preprocessing so doing this will avoid the incorrect function prototype generation.
Issue checklist
- [X] I searched for previous reports in the issue tracker
- [X] I verified the problem still occurs when using the nightly build
- [X] My report contains all necessary details