Skip to content

Commit

Permalink
Fixing placement of error messages. (Paul Stoffregen)
Browse files Browse the repository at this point in the history
This patch places #line preprocessor directives into the generated code
file so that the compiler reports the correct location for error
messages.

http://code.google.com/p/arduino/issues/detail?id=907
  • Loading branch information
damellis committed May 27, 2012
1 parent a24999d commit 77ed2f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 52 deletions.
61 changes: 13 additions & 48 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,9 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
for (SketchCode sc : code) {
if (sc.isExtension("ino") || sc.isExtension("pde")) {
sc.setPreprocOffset(bigCount);
// These #line directives help the compiler report errors with
// correct the filename and line number (issue 281 & 907)
bigCode.append("#line 1 \"" + sc.getFileName() + "\"\n");
bigCode.append(sc.getProgram());
bigCode.append('\n');
bigCount += sc.getLineCount();
Expand Down Expand Up @@ -1542,54 +1545,16 @@ public ArrayList<File> getImportedLibraries() {
public RunnerException placeException(String message,
String dotJavaFilename,
int dotJavaLine) {
int codeIndex = 0; //-1;
int codeLine = -1;

// System.out.println("placing " + dotJavaFilename + " " + dotJavaLine);
// System.out.println("code count is " + getCodeCount());

// first check to see if it's a .java file
for (int i = 0; i < getCodeCount(); i++) {
SketchCode code = getCode(i);
if (!code.isExtension(getDefaultExtension())) {
if (dotJavaFilename.equals(code.getFileName())) {
codeIndex = i;
codeLine = dotJavaLine;
return new RunnerException(message, codeIndex, codeLine);
}
}
}

// If not the preprocessed file at this point, then need to get out
if (!dotJavaFilename.equals(name + ".cpp")) {
return null;
}

// if it's not a .java file, codeIndex will still be 0
// this section searches through the list of .pde files
codeIndex = 0;
for (int i = 0; i < getCodeCount(); i++) {
SketchCode code = getCode(i);

if (code.isExtension(getDefaultExtension())) {
// System.out.println("preproc offset is " + code.getPreprocOffset());
// System.out.println("looking for line " + dotJavaLine);
if (code.getPreprocOffset() <= dotJavaLine) {
codeIndex = i;
// System.out.println("i'm thinkin file " + i);
codeLine = dotJavaLine - code.getPreprocOffset();
}
}
}
// could not find a proper line number, so deal with this differently.
// but if it was in fact the .java file we're looking for, though,
// send the error message through.
// this is necessary because 'import' statements will be at a line
// that has a lower number than the preproc offset, for instance.
// if (codeLine == -1 && !dotJavaFilename.equals(name + ".java")) {
// return null;
// }
return new RunnerException(message, codeIndex, codeLine);
// Placing errors is simple, because we inserted #line directives
// into the preprocessed source. The compiler gives us correct
// the file name and line number. :-)
for (int codeIndex = 0; codeIndex < getCodeCount(); codeIndex++) {
SketchCode code = getCode(codeIndex);
if (dotJavaFilename.equals(code.getFileName())) {
return new RunnerException(message, codeIndex, dotJavaLine);
}
}
return null;
}


Expand Down
15 changes: 12 additions & 3 deletions app/src/processing/app/debug/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Compiler implements MessageConsumer {
String buildPath;
String primaryClassName;
boolean verbose;
boolean sketchIsCompiled;

RunnerException exception;

Expand All @@ -68,6 +69,7 @@ public boolean compile(Sketch sketch,
this.buildPath = buildPath;
this.primaryClassName = primaryClassName;
this.verbose = verbose;
this.sketchIsCompiled = false;

// the pms object isn't used for anything but storage
MessageStream pms = new MessageStream(this);
Expand Down Expand Up @@ -130,6 +132,7 @@ public boolean compile(Sketch sketch,
findFilesInPath(buildPath, "c", false),
findFilesInPath(buildPath, "cpp", false),
boardPreferences));
sketchIsCompiled = true;

// 2. compile the libraries, outputting .o files to: <buildPath>/<library>/

Expand Down Expand Up @@ -513,14 +516,20 @@ public void message(String s) {
//msg = _("\nThe 'Keyboard' class is only supported on the Arduino Leonardo.\n\n");
}

RunnerException e = sketch.placeException(error, pieces[1], PApplet.parseInt(pieces[2]) - 1);
RunnerException e = null;
if (!sketchIsCompiled) {
// Place errors when compiling the sketch, but never while compiling libraries
// or the core. The user's sketch might contain the same filename!
e = sketch.placeException(error, pieces[1], PApplet.parseInt(pieces[2]) - 1);
}

// replace full file path with the name of the sketch tab (unless we're
// in verbose mode, in which case don't modify the compiler output)
if (e != null && !verbose) {
SketchCode code = sketch.getCode(e.getCodeIndex());
String fileName = code.isExtension(sketch.getDefaultExtension()) ? code.getPrettyName() : code.getFileName();
s = fileName + ":" + e.getCodeLine() + ": error: " + pieces[3] + msg;
String fileName = (code.isExtension("ino") || code.isExtension("pde")) ? code.getPrettyName() : code.getFileName();
int lineNum = e.getCodeLine() + 1;
s = fileName + ":" + lineNum + ": error: " + pieces[3] + msg;
}

if (exception == null && e != null) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/processing/app/preproc/PdePreprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected void writeProgram(PrintStream out, String program, List<String> protot
for (int i = 0; i < prototypes.size(); i++) {
out.print(prototypes.get(i) + "\n");
}

out.println("#line 1");
out.print(program.substring(prototypeInsertionPoint));
}

Expand Down

1 comment on commit 77ed2f4

@WestfW
Copy link
Contributor

@WestfW WestfW commented on 77ed2f4 Mar 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

causes disassembly using avr-objdump to fail to find the source file:
#1337

Please sign in to comment.