Skip to content

Generated prototype for function in tab causes #line directive to have the wrong filename #99

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
per1234 opened this issue Jan 20, 2016 · 1 comment

Comments

@per1234
Copy link
Contributor

per1234 commented Jan 20, 2016

Using Arduino IDE 1.6.8 2016/01/19 08:42 with Windows 7 64bit

tab1.ino:

void setup() {
  bug
}
void loop() {}

tab2.ino:

void dummy() {
}

Compiler error:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_968481\tab2.ino: In function 'void setup()':
tab2:2: error: 'bug' was not declared in this scope

The error highlight is also on the wrong tab.

C:\Users\per\AppData\Local\Temp\buildc2e11d1cafc78478b2a43bb59a48970a.tmp\sketch\tab1.ino.cpp:

#include <Arduino.h>
#line 1
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_968481\\tab1.ino"
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_968481\\tab1.ino"
void setup();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_968481\\tab1.ino"
void loop();
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_968481\\tab2.ino"
void dummy();
#line 1
void setup() {
  bug
}
void loop() {}
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_968481\\tab2.ino"
void dummy() {
}

The last #line directive should specify the tab1.ino filename.

This also can cause the __FILE__ macro to have the wrong value.

Originally reported at: http://forum.arduino.cc/index.php?topic=372882.0

@matthijskooijman
Copy link
Collaborator

I believe the following happens:

  • Multiple .ino files get merged into 1 source file. #line directives are added above each file's contents to indicate the original source of all code.
  • Then, prototypes are generated. #line directives are added above each prototype to indicate the original source of the prototype.

In this case, the last prototype generated is from tab2, but no additional #line directive is generated to "switch back" to the original tab1 file whose contents follow next.

A fix for this might be tricky, since by the time prototypes are inserted, arduino-builder does not really know into what file it is inserting (it would need to parse #line directives, which seems somewhat fragile, or carry extra information about line numbers and filenames from the merging step).

A related problem (or rather, another problem whose fix might also make this problem easier to fix) is that prototypes are inserted before the first line containing a function (or a function pointer, though that check is rough). However, because we've told ctags to follow #line directives, this line number refers to the line number in the original .ino file, not in the merged source. There is some correction for the #include <Arduino.h> and #line directives added in the merged source (through CTX_LINE_OFFSET), but if the main .ino file would contain no functions, this line number would refer to a secondary .ino file, but will be interpreted as a line number in the merged source.

A fix for this would be to merge sources, generate prototypes and then instead of inserting them directly, just note the file name and line number of the insertion point. Then merge sources again, inserting the prototypes into the right file and line number in the process. This also makes it trivial to insert a #line directive with the correct filename after the inserted prototypes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants