Skip to content

Generated function prototype injected before declaration of custom parameter type #2696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
6v6gt-duino opened this issue Nov 1, 2020 · 5 comments
Open
3 tasks done
Labels
topic: build-process Related to the sketch build process topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@6v6gt-duino
Copy link

6v6gt-duino commented Nov 1, 2020

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, the function prototype is inserted into the code at a location before the declaration of a custom type referenced in the function signature.

🐛 The sketch compilation fails spuriously.

To reproduce

Setup environment

$ arduino-cli version

arduino-cli  Version: git-snapshot Commit: c5812eea6 Date: 2024-09-02T17:16:38Z

$ mkdir -P "/tmp/FooSketch"

$ printf '
void foo() {}  // This causes the prototype to be inserted before the declaration of `bar_t`
enum bar_t {};
void baz(bar_t qux) {
  (void)qux;  // Fix "unused parameter" warning
}
void setup() {}
void loop() {}
' > "/tmp/FooSketch/FooSketch.ino"

Demo

$ arduino-cli compile --fqbn arduino:avr:uno "/tmp/FooSketch"

C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:4:10: error: variable or field 'baz' declared void
 void baz(bar_t qux) {
          ^~~~~
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:4:10: error: 'bar_t' was not declared in this scope

🐛 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"

#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void foo();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void baz(bar_t qux);
#line 7 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void setup();
#line 8 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void foo() {}  // This causes the prototype to be inserted before the declaration of `bar_t`
enum bar_t {};
void baz(bar_t qux) {
  (void)qux;  // Fix "unused parameter" warning
}
void setup() {}
void loop() {}

🐛 The spurious compilation failure was caused by Arduino CLI placing the prototype for the baz function before the declaration of the bar_t type.

Expected behavior

Generated function prototypes should be inserted after the declarations of types used in the function signature.

Arduino CLI version

Original report

Arduino IDE 1.8.13

Last verified with

c5812ee

Operating system

  • Windows

Operating system version

  • Windows 11

Additional context

Originally reported at https://forum.arduino.cc/t/weird-g-error-compiling-simple-type-definition/680759

Additional reports

Related

Workaround

Manually add a function prototype at the appropriate location:

void foo() {}
enum bar_t {};
void baz(bar_t qux);  // Manually added function prototype to workaround prototype generator bug.
void baz(bar_t qux) {
  (void)qux;  // Fix "unused parameter" warning
}
void setup() {}
void loop() {}

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
@facchinm
Copy link
Member

facchinm commented Nov 2, 2020

Hi @6v6gt-duino ,
thank for reporting! The issue lies in the prototype generator as you noticed. What bothers me is that both functions are already declared before their usage so the prototype generator shouldn't create anything.
We'll investigate soon and report back

@6v6gt-duino

This comment was marked as duplicate.

@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Sep 30, 2021
@sterretjeToo

This comment was marked as duplicate.

@6v6gt-duino

This comment was marked as off-topic.

@6v6gt-duino

This comment was marked as off-topic.

@per1234 per1234 changed the title Incorrect placement of auto-generated prototypes gives misleading error messages Generated function prototype injected before declaration of custom parameter type Sep 2, 2024
@per1234 per1234 transferred this issue from arduino/arduino-builder Sep 2, 2024
@per1234 per1234 added the topic: build-process Related to the sketch build process label Sep 2, 2024
@arduino arduino locked as too heated and limited conversation to collaborators Sep 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: build-process Related to the sketch build process topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

4 participants