arduino-cli icon indicating copy to clipboard operation
arduino-cli copied to clipboard

Backslash incorrectly inserted into generated prototype for multiline template function

Open jfjlaros opened this issue 2 years ago • 3 comments

Describe the problem

When trying to compile the following sketch,

template <size_t N>
void test(size_t arr[N / 2]) {}

void setup() {}
void loop() {}

this error is raised:

arduino-cli compile --fqbn arduino:avr:pro --warnings all --output-dir build \
    --build-property compiler.cpp.extra_flags="-pedantic"
.../x.ino.cpp:2:7: warning: line number out of range
 #line 0 ".../x.ino"
       ^
.../x.ino:0:43: error: stray '\' in program
.../x.ino:6:7: warning: line number out of range

When the function definition is changed as follows, everything works fine.

void test(size_t arr[N >> 1]) {}

This issue seems to be independent of which core is used (tried AVR and ESP) and of which platform is used (tried Linux and Wokwi simulator).

Arduino CLI version

4a4b784c

Operating system

  • Windows

Operating system version

  • Windows 11

Additional context

For reference, the following program compiles without errors or warnings

#include <cstddef>

template <size_t N>
void test(size_t arr[N / 2]) {}

int main() {
  return 0;
}

with the following command:

g++ -std=c++11 -Wall -Wextra -pedantic x.cc

Issue checklist

  • [X] I searched for previous reports in the issue tracker
  • [X] I verified the problem still occurs when using the nightly build
  • [X] My report contains all necessary details

jfjlaros avatar Jun 24 '22 17:06 jfjlaros

Thanks for your report @jfjlaros. This is a bug in Arduino CLI (which is used by Arduino IDE 1.x, Arduino IDE 2.x, and Arduino Cloud).

You can see the problem in the program produced after Arduino sketch preprocessing:

$ arduino-cli version
arduino-cli.exe  Version: git-snapshot Commit: 4a4b784c Date: 2022-06-26T23:40:19Z

$ SKETCH_PATH="/tmp/SlashBug"
$ mkdir "$SKETCH_PATH"
$ printf "template <size_t N>\nvoid test(size_t arr[N / 2]) {}\nvoid setup() {}\nvoid loop() {}\n" > "$SKETCH_PATH"/SlashBug.ino
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "$SKETCH_PATH"
#include <Arduino.h>
#line 0 "C:\\Users\\per\\AppData\\Local\\Temp\\SlashBug\\SlashBug.ino"
template <size_t N>void test(size_t arr[N \/ 2]);
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\SlashBug\\SlashBug.ino"
void setup();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\SlashBug\\SlashBug.ino"
void loop();
#line 0 "C:\\Users\\per\\AppData\\Local\\Temp\\SlashBug\\SlashBug.ino"
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\SlashBug\\SlashBug.ino"
template <size_t N>
void test(size_t arr[N / 2]) {}
void setup() {}
void loop() {}

Note the backslash that was inserted into the generated prototype:

template <size_t N>void test(size_t arr[N \/ 2]);

can you please point me to the right location?

This issue should be tracked in the Arduino CLI repository. @cmaglie @facchinm, anyone... I don't have any permissions in this repository. Please transfer this issue to arduino/arduino-cli. I already searched for duplicates.

per1234 avatar Jun 27 '22 00:06 per1234

@per1234 done :wink: @jfjlaros it's indeed a nasty bug, mostly due to the fact that ctags is not very template friendly, so we had to create special cases and probably multiline is not well tested. Thanks for the PoC btw

facchinm avatar Jun 27 '22 07:06 facchinm

Here is an other one. The following function definition

template <size_t N>
void func(double const (&)[N]) {}

gives rise to these errors (different than the ones in the previous example)

arduino-cli compile --fqbn arduino:avr:pro --warnings all --output-dir build \
    --build-property compiler.cpp.extra_flags="-pedantic"
.../x.ino:3:45: error: variable or field 'func' declared void
.../x.ino:3:30: error: expected primary-expression before 'double'

while

template <size_t N> void func(double const (&)[N]) {}

compiles fine.

jfjlaros avatar Jul 05 '22 10:07 jfjlaros