Description
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
Last verified with
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
- #line directives inserted into raw string literal by sketch preprocessor #1191
- ) in comment breaks function prototype generation #1253
- Generated function prototype injected before declaration of custom parameter type #1269
- Generated prototype incorrectly prefixed with
extern "C"
when comment contains//
#1591 - Generated prototype incorrectly prefixed with
extern "C"
when usingextern "C" { ... }
to mix C functions in an.ino
file. #1618 - Backslash incorrectly inserted into generated prototype for multiline template function #1785
- Generated function prototype injected before declaration of custom parameter type #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
- I searched for previous reports in the issue tracker
- I verified the problem still occurs when using the nightly build
- My report contains all necessary details