Skip to content

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

@jfjlaros

Description

@jfjlaros

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
    I verified the problem still occurs when using the
    My report contains all necessary details

Activity

per1234

per1234 commented on Jun 27, 2022

@per1234
Contributor

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]);
added
topic: codeRelated to content of the project itself
type: imperfectionPerceived defect in any part of project
on Jun 27, 2022
facchinm

facchinm commented on Jun 27, 2022

@facchinm
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

jfjlaros commented on Jul 5, 2022

@jfjlaros
Author

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.

changed the title [-]Preprocessing bug (probably).[/-] [+]Preprocessing bug[/+] on May 29, 2023
changed the title [-]Preprocessing bug[/-] [+]Backslash incorrectly inserted into generated prototype for multiline template function [/+] on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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

      Participants

      @cmaglie@jfjlaros@per1234@facchinm

      Issue actions

        Backslash incorrectly inserted into generated prototype for multiline template function · Issue #1785 · arduino/arduino-cli