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

Generated prototype incorrectly prefixed with extern "C" when comment contains // #1591

Open
3 tasks done
Rob58329 opened this issue Dec 15, 2021 · 1 comment
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

@Rob58329
Copy link

Rob58329 commented Dec 15, 2021

Describe the problem

A stray curly-brace in // comments or #if (false) sections causes the function prototypes generated during the "sketch pre-processing" stage of the compilation operation to be incorrectly prefixed with extern "C"

For example:

extern "C" void setup();

To reproduce

Compile the following sketch:

#if (false) // The below lines should not be enabled, and so should not have any effect 
#error "Should not get to here"
// {               // either delete this line,
// extern "C" {}   // or delete this line, and the below code is compiled fine!
#endif

void setup() {
  Serial.begin(74880);
}

void loop() {
  delay(5000);
  Serial.println("Hello");
}

Examine the .cpp file generated by the compilation.

🐛 Generated function prototypes are prefixed with extern "C"


Compile the following sketch:

#if (false) // The below lines should not be enabled, and so should not have any effect 
#error "Should not get to here"
// {               // either delete this line,
// extern "C" {}   // or delete this line, and the below code works fine!
#endif

ADC_MODE(ADC_VCC);

void setup() {
  Serial.begin(74880);
}

void loop() {
  delay(5000);
  Serial.print("raw_battery (should be between 3000 and 3300)="); Serial.println(ESP.getVcc());
  // unless you DELETE one of the above two COMMENT lines, you will get "=65535" and not "=3017"
}

Examine the .cpp file generated by the compilation.

🐛 Generated function prototypes are prefixed with extern "C"

Expected behavior

Generated function prototypes should be correct regardless of the contents of comments:

void setup();

Arduino CLI version

Original report

Arduino IDE v1.8.16

Last verified with

d563755

Operating system

Windows

Operating system version

11

Additional context

I note that there are several discussions regarding curly-braces for Arduino IDE but none of these seem to cover this specific issue:

Additional reports

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
@per1234
Copy link
Contributor

per1234 commented Dec 16, 2021

Thanks @Rob58329!

In case it will be useful to the developers, I'll provide a minimal demo of the bug:

$ arduino-cli version
arduino-cli.exe  Version: nightly-20211216 Commit: d458040 Date: 2021-12-16T01:27:31Z

$ SKETCH_PATH="/tmp/ExternCBug"
$ mkdir "$SKETCH_PATH"
$ printf "// {\n// extern \"C\" {}\nvoid setup() {}\nvoid loop() {}\n" > "$SKETCH_PATH"/ExternCBug.ino
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "$SKETCH_PATH"
#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
// {
// extern "C" {}
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
 extern "C" void setup();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
 extern "C" void loop();
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
void setup() {}
void loop() {}

Note that the generated setup and loop function prototypes unexpectedly are prefixed by extern "C"

Preprocessed sketch is as expected after removing the first comment from the sketch:

$ printf "// extern \"C\" {}\nvoid setup() {}\nvoid loop() {}\n" > "$SKETCH_PATH"/ExternCBug.ino
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "$SKETCH_PATH"
#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
// extern "C" {}
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
void setup();
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
void loop();
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\ExternCBug\\ExternCBug.ino"
void setup() {}
void loop() {}

@umbynos umbynos added the topic: build-process Related to the sketch build process label Feb 2, 2023
@per1234 per1234 changed the title Arduino-IDE not ignoring stray { curly-braces in “//” comments and “#if (false)” sections when doing the “.ino to .cpp conversion” Generated prototype incorrectly prefixed with extern "C" when comment contains // 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