Skip to content

Generated function prototype injected before declaration of custom parameter type #1269

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

Closed
allnick opened this issue Apr 18, 2021 · 3 comments
Assignees
Labels
conclusion: duplicate Has already been submitted 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

@allnick
Copy link

allnick commented Apr 18, 2021

The type declared after any function is not visible as the parameter type for any other function below.
Although it is suitable for other purposes.
For example:

struct tword{
  byte low;
  byte high;
};

void none(){}

bool test(tword par){return true;}

void setup() {
  word w;
  tword* var = (tword*)(&w);
}

void loop(){}

It's ok.

void none(){}

struct tword{
  byte low;
  byte high;
};

//bool test(tword par){return true;}

void setup() {
  word w;
  tword* var = (tword*)(&w);
}

void loop(){}

Too it's ok

void none(){}

struct tword{
  byte low;
  byte high;
};

bool test(tword par){return true;}

void setup() {
  word w;
  tword* var = (tword*)(&w);
}

void loop(){}

Compilation fails with the error:

'tword' was not declared in this scope

Additional context

Additional reports

@per1234 per1234 transferred this issue from arduino/Arduino Apr 18, 2021
@per1234
Copy link
Contributor

per1234 commented Apr 18, 2021

Hi @allnick. Thanks for your report.

During sketch preprocessing, the Arduino build system (which is implemented here in the Arduino CLI code base) automatically generates function prototypes in .ino files for any functions that don't already have a prototype. This turns out to be surprisingly difficult. Although the prototype generation system works fine for most sketches, under certain circumstances, it inserts the prototype at the wrong position in the code. That is the cause of the error you encountered.

You can see how the code looks after preprocessing here:

$ arduino-cli compile --fqbn arduino:avr:uno --preprocess Foo
#include <Arduino.h>
#line 1 "C:\\Foo\\Foo.ino"
#line 1 "C:\\Foo\\Foo.ino"
void none();
#line 8 "C:\\Foo\\Foo.ino"
bool test(tword par);
#line 10 "C:\\Foo\\Foo.ino"
void setup();
#line 15 "C:\\Foo\\Foo.ino"
void loop();
#line 1 "C:\\Foo\\Foo.ino"
void none(){}

struct tword{
  byte low;
  byte high;
};

bool test(tword par){return true;}

void setup() {
  word w;
  tword* var = (tword*)(&w);
}

void loop(){}

Note that the bool test(tword par); prototype was inserted above the declaration of the tword type.

The workaround is to simply manually add your own prototype in the correct location:

void none(){}

struct tword{
  byte low;
  byte high;
};
bool test(tword par); // function prototype
bool test(tword par){return true;}

void setup() {
  word w;
  tword* var = (tword*)(&w);
}

void loop(){}

@allnick
Copy link
Author

allnick commented Apr 18, 2021

Respected per1234. Thanks for your explanation.
But I just moved the type declaration above all the functions.
But your remark, I necessarily take into account.

@fstasi fstasi removed the type: bug label Sep 16, 2021
@rsora rsora added type: imperfection Perceived defect in any part of project topic: core labels Sep 22, 2021
@per1234 per1234 added the topic: code Related to content of the project itself label Aug 5, 2022
@umbynos umbynos added the topic: build-process Related to the sketch build process label Nov 30, 2022
@per1234 per1234 changed the title The type declared after any function is not visible as the parameter type for any other function below. Generated function prototype injected before declaration of custom parameter type Sep 2, 2024
@per1234 per1234 self-assigned this Sep 2, 2024
@per1234
Copy link
Contributor

per1234 commented Sep 2, 2024

Thanks for your report @allnick. I see we have another report of the bug at #2696. Since the other one was submitted earlier, and it a bit more detailed, I'll close this in favor of the other.

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 2, 2024
@per1234 per1234 added the conclusion: duplicate Has already been submitted label Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: duplicate Has already been submitted 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

6 participants