Description
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.
An Arduino IDE editor tab can be in one of two states:
- Saved: the contents of the file on disk matches the content in the editor buffer
- The editor will always be in this state if the "Auto save" feature is enabled.
- Dirty: the contents of the file on disk differs from the content staged in the editor buffer.
- This is indicated by the presence of a ⬤ at the right side of the editor tab.
Function prototypes are not generated under the following conditions:
- The function's parameter list spans multiple lines
- The sketch is in an unsaved (AKA "dirty") state
🐛 Compilation of the sketch fails spuriously.
To reproduce
- Select File > Preferences from the Arduino IDE menus.
- Uncheck the box next to "☐ Auto save".
- Click the "OK" button.
- Create the following sketch:
void setup() { foo(1, 2); } void loop() {} void foo(int bar, int baz) {}
⚠️ Do not save. - Select Sketch > Verify/Compile from the Arduino IDE menus.
🐛 Compilation fails unexpectedly:
C:\Users\per\Documents\Arduino\sketch_dec11b\sketch_dec11b.ino: In function 'void setup()':
C:\Users\per\Documents\Arduino\sketch_dec11b\sketch_dec11b.ino:2:3: error: 'foo' was not declared in this scope
// put your setup code here, to run once:
^~~
C:\Users\per\Documents\Arduino\sketch_dec11b\sketch_dec11b.ino:2:3: note: suggested alternative: 'feof'
// put your setup code here, to run once:
^~~
feof
Compilation error: exit status 1}
(Outdated sketch source in compilation output tracked here: https://github.com/arduino/arduino-cli/issues/1185
)
🐛 Note that the preprocessed sketch program does not contain a function prototype for void foo(int, int)
:
#include <Arduino.h>
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void setup();
#line 4 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void loop();
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void setup() {
foo(1, 2);
}
void loop() {}
void foo(int bar,
int baz) {}
If you compile the identical sketch while it is in a saved state, the function prototype is generated as expected 🙂, so this is not about a fundamental limitation of the prototype generation system of Arduino CLI:
#include <Arduino.h>
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void setup();
#line 4 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void loop();
#line 5 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void foo(int bar, int baz);
#line 1 "C:\\Users\\per\\Documents\\Arduino\\sketch_dec11b\\sketch_dec11b.ino"
void setup() {
foo(1, 2);
}
void loop() {}
void foo(int bar,
int baz) {}
Expected behavior
Prototypes for functions in .ino
files are always generated.
Arduino CLI version
Original report
0.20.2, bundled with Arduino IDE a8ae0bb
Last verified with
0.36.0-rc.1, bundled with Arduino IDE aa9b10d
Operating system
Windows
Operating system version
- Windows 10
- Windows 11
Additional context
This issue does not occur in Arduino IDE 1.8.16
Originally reported by @UKHeliBob at https://forum.arduino.cc/t/auto-format-using-clang-format-file-produces-code-that-cannot-be-compiled/934610
Related
Workaround
Add a prototype for the function in the sketch before the first reference:
void foo(int bar, int baz);
void setup() {
foo(1, 2);
}
void loop() {}
void foo(int bar,
int baz) {}
- OR -
Save the sketch before compiling.
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