Skip to content

Prototype injected inside function definition when there is a line break after type #2931

Open
@per1234

Description

@per1234

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.

Whitespace does not have syntactic significance to the C++ compiler, so users are free to format their code according to their preferences. This includes adding line breaks between any components of a function definition.

🐛 Arduino CLI chooses an incorrect point to inject the generated function prototype under the following conditions:

  • The sketch contains a manual function prototype.
  • The first function definition has a line break after the type.

This causes compilation to fail with a cryptic error.

To reproduce

Setup environment

$ arduino-cli version

arduino-cli  Version: git-snapshot Commit: 2947cfb71 Date: 2025-06-10T23:45:14Z

$ mkdir -p "/tmp/FooSketch"

$ printf '
void setup();
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:5:11: error: two or more data types in declaration of 'loop'
 void loop() {}
           ^
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino: In function 'int setup()':
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:4:1: error: ambiguating new declaration of 'int setup()'
 setup() {}
 ^~~~~
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:6: note: old declaration 'void setup()'
 void setup();

[...]

🐛 The compilation failed even though the sketch is valid C++.

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"

void setup();
void
#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
setup() {}
void loop() {}

🐛 The compilation failure was caused by Arduino CLI injecting the function prototype for void loop() inside the definition of the void setup() function.

Expected behavior

Generated function prototypes are not injected inside function definitions.

Arduino CLI version

2947cfb

Operating system

N/A, Windows

Operating system version

11

Additional context

Originally reported at https://forum.arduino.cc/t/function-prototype-with-newline-compiler-error/1388103

Workaround

Refrain from adding a line break after the type in function definitions.

Related

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

No one assigned

    Labels

    topic: build-processRelated to the sketch build processtopic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions