Skip to content

Prototype incorrectly generated for struct member function under certain conditions #2161

Open
@jfjlaros

Description

@jfjlaros

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

0b77c0a

Last verified with

c5812ee

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

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions