Skip to content
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

Prototype not generated for function with inline attribute #2697

Open
3 tasks done
per1234 opened this issue Sep 2, 2024 · 0 comments
Open
3 tasks done

Prototype not generated for function with inline attribute #2697

per1234 opened this issue Sep 2, 2024 · 0 comments
Labels
topic: build-process Related to the sketch build process type: imperfection Perceived defect in any part of project

Comments

@per1234
Copy link
Contributor

per1234 commented Sep 2, 2024

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.

Attributes can be used to specify properties of a function. The attribute declaration may be placed at a variety of locations relative to a function definition.

🐛 A prototype is not generated for the function if the attribute declaration is placed between the type and name in the function definition.

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 loop() {}
void setup() {
  foo();
}
void __attribute__(()) foo() {}
' > "/tmp/FooSketch/FooSketch.ino"

Demo

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

C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino: In function 'void setup()':
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:3:3: error: 'foo' was not declared in this scope
   foo();
   ^~~

🐛 The compilation failed unexpectedly.

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 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void setup();
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop() {}
void setup() {
  foo();
}
void __attribute__(()) foo() {}

🐛 The compilation failure was caused by Arduino CLI not creating a prototype for the foo function.

Expected behavior

Prototypes are generated for functions with attribute declarations between the type and name.

Arduino CLI version

c5812ee

Operating system

  • Windows

Operating system version

  • Windows 11

Additional context

Originally reported by @JLBCS at https://forum.arduino.cc/t/iram-attr-questions/1297715

In the real world sketch shared there, IRAM_ATTR was used. This is a macro for an __attribute__ declaration, provided by Espressif's esp-idf framework.

Additional reports

Related

Workaround

Manually add a function prototype to the sketch:

void __attribute__(()) foo();
void loop() {}
void setup() {
  foo();
}
void foo() {}

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
@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: build-process Related to the sketch build process labels Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: build-process Related to the sketch build process type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

1 participant