arduino / arduino-cli

Arduino command line tool
https://arduino.github.io/arduino-cli/latest/
GNU General Public License v3.0
4.4k stars 384 forks source link

Backslash incorrectly inserted into generated prototype for multiline template function #1785

Open jfjlaros opened 2 years ago

jfjlaros commented 2 years ago

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

Operating system version

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

per1234 commented 2 years ago

Thanks for your report @jfjlaros.

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]);
facchinm commented 2 years ago

@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

jfjlaros commented 2 years ago

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.