mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-01-29 20:31:58 +00:00
Format CompileErrors using new ErrorFormatter class when compiling through the main KickC class
This commit is contained in:
parent
5a7ffbdb46
commit
2084df4a18
29
src/main/java/dk/camelot64/kickc/ErrorFormatter.java
Normal file
29
src/main/java/dk/camelot64/kickc/ErrorFormatter.java
Normal file
@ -0,0 +1,29 @@
|
||||
package dk.camelot64.kickc;
|
||||
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import dk.camelot64.kickc.model.statements.StatementSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* Formats a CompileError using the GCC format, so that it may be easily parsed by IDEs and automated tools.
|
||||
*/
|
||||
public class ErrorFormatter {
|
||||
public ErrorFormatter() {
|
||||
}
|
||||
|
||||
public String formatError(CompileError e) {
|
||||
StatementSource source = e.getSource();
|
||||
if (source != null) {
|
||||
Path currentPath = new File(".").toPath().toAbsolutePath();
|
||||
Path sourcePath = Paths.get(source.getFileName());
|
||||
Path relativePath = currentPath.relativize(sourcePath);
|
||||
return String.format("%s:%s:%s: error: %s", relativePath.toString(), source.getLineNumber(), source.getStartIndex(), e.getMessage());
|
||||
} else {
|
||||
// Cannot determine the location of the error, just print it directly.
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
@ -384,7 +384,8 @@ public class KickC implements Callable<Integer> {
|
||||
compiler.compile(cFiles, effectiveDefines);
|
||||
} catch(CompileError e) {
|
||||
// Print the error and exit with compile error
|
||||
System.err.println(e.getMessage());
|
||||
ErrorFormatter formatter = new ErrorFormatter();
|
||||
System.err.println(formatter.formatError(e));
|
||||
return COMPILE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -7,15 +7,15 @@ import org.antlr.v4.runtime.Token;
|
||||
/** Signals some error in the code (or compilation) */
|
||||
public class CompileError extends RuntimeException {
|
||||
|
||||
private String source;
|
||||
private StatementSource source;
|
||||
|
||||
public CompileError(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CompileError(String message, StatementSource source) {
|
||||
super(message+"\n"+(source==null?"":source.toString()));
|
||||
this.source = (source==null?"":source.toString());
|
||||
super(message);
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public CompileError(String message, Token token) {
|
||||
@ -30,8 +30,7 @@ public class CompileError extends RuntimeException {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
public StatementSource getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package dk.camelot64.kickc.test;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.*;
|
||||
import dk.camelot64.kickc.Compiler;
|
||||
import dk.camelot64.kickc.SourceLoader;
|
||||
import dk.camelot64.kickc.TmpDirManager;
|
||||
import dk.camelot64.kickc.asm.AsmProgram;
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
@ -28,8 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Compile a number of source files and compare the resulting assembler with expected output
|
||||
@ -186,6 +183,17 @@ public class TestPrograms {
|
||||
compileAndCompare("pragma-unknown.c");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorFormatter() throws IOException, URISyntaxException {
|
||||
try {
|
||||
compileAndCompare("library-constructor-error-2.c");
|
||||
}
|
||||
catch (CompileError e) {
|
||||
ErrorFormatter formatter = new ErrorFormatter();
|
||||
assertTrue(formatter.formatError(e).contains("library-constructor-error-2.c:4:82: error: Error! Procedure not found print"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLibraryConstructorError2() throws IOException, URISyntaxException {
|
||||
assertError("library-constructor-error-2.c", "Error! Procedure not found print");
|
||||
|
Loading…
x
Reference in New Issue
Block a user