Skip to content

Commit 77ed2f4

Browse files
committed
Fixing placement of error messages. (Paul Stoffregen)
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
1 parent a24999d commit 77ed2f4

File tree

3 files changed

+26
-52
lines changed

3 files changed

+26
-52
lines changed

Diff for: app/src/processing/app/Sketch.java

+13-48
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,9 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
13731373
for (SketchCode sc : code) {
13741374
if (sc.isExtension("ino") || sc.isExtension("pde")) {
13751375
sc.setPreprocOffset(bigCount);
1376+
// These #line directives help the compiler report errors with
1377+
// correct the filename and line number (issue 281 & 907)
1378+
bigCode.append("#line 1 \"" + sc.getFileName() + "\"\n");
13761379
bigCode.append(sc.getProgram());
13771380
bigCode.append('\n');
13781381
bigCount += sc.getLineCount();
@@ -1542,54 +1545,16 @@ public ArrayList<File> getImportedLibraries() {
15421545
public RunnerException placeException(String message,
15431546
String dotJavaFilename,
15441547
int dotJavaLine) {
1545-
int codeIndex = 0; //-1;
1546-
int codeLine = -1;
1547-
1548-
// System.out.println("placing " + dotJavaFilename + " " + dotJavaLine);
1549-
// System.out.println("code count is " + getCodeCount());
1550-
1551-
// first check to see if it's a .java file
1552-
for (int i = 0; i < getCodeCount(); i++) {
1553-
SketchCode code = getCode(i);
1554-
if (!code.isExtension(getDefaultExtension())) {
1555-
if (dotJavaFilename.equals(code.getFileName())) {
1556-
codeIndex = i;
1557-
codeLine = dotJavaLine;
1558-
return new RunnerException(message, codeIndex, codeLine);
1559-
}
1560-
}
1561-
}
1562-
1563-
// If not the preprocessed file at this point, then need to get out
1564-
if (!dotJavaFilename.equals(name + ".cpp")) {
1565-
return null;
1566-
}
1567-
1568-
// if it's not a .java file, codeIndex will still be 0
1569-
// this section searches through the list of .pde files
1570-
codeIndex = 0;
1571-
for (int i = 0; i < getCodeCount(); i++) {
1572-
SketchCode code = getCode(i);
1573-
1574-
if (code.isExtension(getDefaultExtension())) {
1575-
// System.out.println("preproc offset is " + code.getPreprocOffset());
1576-
// System.out.println("looking for line " + dotJavaLine);
1577-
if (code.getPreprocOffset() <= dotJavaLine) {
1578-
codeIndex = i;
1579-
// System.out.println("i'm thinkin file " + i);
1580-
codeLine = dotJavaLine - code.getPreprocOffset();
1581-
}
1582-
}
1583-
}
1584-
// could not find a proper line number, so deal with this differently.
1585-
// but if it was in fact the .java file we're looking for, though,
1586-
// send the error message through.
1587-
// this is necessary because 'import' statements will be at a line
1588-
// that has a lower number than the preproc offset, for instance.
1589-
// if (codeLine == -1 && !dotJavaFilename.equals(name + ".java")) {
1590-
// return null;
1591-
// }
1592-
return new RunnerException(message, codeIndex, codeLine);
1548+
// Placing errors is simple, because we inserted #line directives
1549+
// into the preprocessed source. The compiler gives us correct
1550+
// the file name and line number. :-)
1551+
for (int codeIndex = 0; codeIndex < getCodeCount(); codeIndex++) {
1552+
SketchCode code = getCode(codeIndex);
1553+
if (dotJavaFilename.equals(code.getFileName())) {
1554+
return new RunnerException(message, codeIndex, dotJavaLine);
1555+
}
1556+
}
1557+
return null;
15931558
}
15941559

15951560

Diff for: app/src/processing/app/debug/Compiler.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class Compiler implements MessageConsumer {
4646
String buildPath;
4747
String primaryClassName;
4848
boolean verbose;
49+
boolean sketchIsCompiled;
4950

5051
RunnerException exception;
5152

@@ -68,6 +69,7 @@ public boolean compile(Sketch sketch,
6869
this.buildPath = buildPath;
6970
this.primaryClassName = primaryClassName;
7071
this.verbose = verbose;
72+
this.sketchIsCompiled = false;
7173

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

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

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

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

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

526535
if (exception == null && e != null) {

Diff for: app/src/processing/app/preproc/PdePreprocessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected void writeProgram(PrintStream out, String program, List<String> protot
205205
for (int i = 0; i < prototypes.size(); i++) {
206206
out.print(prototypes.get(i) + "\n");
207207
}
208-
208+
out.println("#line 1");
209209
out.print(program.substring(prototypeInsertionPoint));
210210
}
211211

0 commit comments

Comments
 (0)