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

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

Open
3 tasks done
jfjlaros opened this issue Jun 24, 2022 · 3 comments
Open
3 tasks done
Assignees
Labels
topic: build-process Related to the sketch build process topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@jfjlaros
Copy link

jfjlaros commented Jun 24, 2022

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

4a4b784

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

  • 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
Copy link
Contributor

per1234 commented Jun 27, 2022

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 facchinm transferred this issue from arduino/toolchain-avr Jun 27, 2022
@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Jun 27, 2022
@facchinm
Copy link
Member

@per1234 done 😉
@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
Copy link
Author

jfjlaros commented Jul 5, 2022

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.

@cmaglie cmaglie added the topic: build-process Related to the sketch build process label Apr 27, 2023
@umbynos umbynos changed the title Preprocessing bug (probably). Preprocessing bug May 29, 2023
@per1234 per1234 changed the title Preprocessing bug Backslash incorrectly inserted into generated prototype for multiline template function 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 topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

4 participants