mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-08-14 22:27:37 +00:00
Added test for missing function definition. #196
This commit is contained in:
@@ -164,7 +164,6 @@ public class Compiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Pass0GenerateStatementSequence pass0GenerateStatementSequence = new Pass0GenerateStatementSequence(cParser, cFileContext, program, variableBuilderConfig, callingConvention);
|
Pass0GenerateStatementSequence pass0GenerateStatementSequence = new Pass0GenerateStatementSequence(cParser, cFileContext, program, variableBuilderConfig, callingConvention);
|
||||||
|
|
||||||
pass0GenerateStatementSequence.generate();
|
pass0GenerateStatementSequence.generate();
|
||||||
|
|
||||||
StatementSequence sequence = program.getStatementSequence();
|
StatementSequence sequence = program.getStatementSequence();
|
||||||
@@ -199,6 +198,7 @@ public class Compiler {
|
|||||||
|
|
||||||
new Pass1GenerateControlFlowGraph(program).execute();
|
new Pass1GenerateControlFlowGraph(program).execute();
|
||||||
new Pass1ResolveForwardReferences(program).execute();
|
new Pass1ResolveForwardReferences(program).execute();
|
||||||
|
new Pass1AssertProcedureDefined(program).execute();
|
||||||
new PassNAssertStructMembers(program).execute();
|
new PassNAssertStructMembers(program).execute();
|
||||||
new Pass1UnwindBlockScopes(program).execute();
|
new Pass1UnwindBlockScopes(program).execute();
|
||||||
new Pass1Procedures(program).execute();
|
new Pass1Procedures(program).execute();
|
||||||
|
@@ -245,7 +245,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
|||||||
if(existingSymbol!=null) {
|
if(existingSymbol!=null) {
|
||||||
// Already declared - check equality
|
// Already declared - check equality
|
||||||
if(!SymbolTypeConversion.procedureDeclarationMatch((Procedure) existingSymbol, procedure))
|
if(!SymbolTypeConversion.procedureDeclarationMatch((Procedure) existingSymbol, procedure))
|
||||||
throw new CompileError("Error! Conflicting declarations for "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
|
throw new CompileError("Error! Conflicting declarations for: "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
|
||||||
} else {
|
} else {
|
||||||
// Not declared before - add it
|
// Not declared before - add it
|
||||||
program.getScope().add(procedure);
|
program.getScope().add(procedure);
|
||||||
@@ -261,7 +261,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
|||||||
// Check that the body has not already been added
|
// Check that the body has not already been added
|
||||||
for(Statement statement : sequence.getStatements())
|
for(Statement statement : sequence.getStatements())
|
||||||
if(statement instanceof StatementProcedureBegin && ((StatementProcedureBegin) statement).getProcedure().equals(procedure.getRef()))
|
if(statement instanceof StatementProcedureBegin && ((StatementProcedureBegin) statement).getProcedure().equals(procedure.getRef()))
|
||||||
throw new CompileError("Error! Redefinition of function "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
|
throw new CompileError("Error! Redefinition of function: "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
|
||||||
// Add the body
|
// Add the body
|
||||||
scopeStack.push(procedure);
|
scopeStack.push(procedure);
|
||||||
sequence.addStatement(new StatementProcedureBegin(procedure.getRef(), StatementSource.procedureBegin(ctx), Comment.NO_COMMENTS));
|
sequence.addStatement(new StatementProcedureBegin(procedure.getRef(), StatementSource.procedureBegin(ctx), Comment.NO_COMMENTS));
|
||||||
|
@@ -0,0 +1,30 @@
|
|||||||
|
package dk.camelot64.kickc.passes;
|
||||||
|
|
||||||
|
import dk.camelot64.kickc.model.CompileError;
|
||||||
|
import dk.camelot64.kickc.model.ControlFlowBlock;
|
||||||
|
import dk.camelot64.kickc.model.Program;
|
||||||
|
import dk.camelot64.kickc.model.symbols.Label;
|
||||||
|
import dk.camelot64.kickc.model.symbols.Procedure;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/** Pass that checks that all functions declared have a definition with a body*/
|
||||||
|
public class Pass1AssertProcedureDefined extends Pass1Base {
|
||||||
|
|
||||||
|
public Pass1AssertProcedureDefined(Program program) {
|
||||||
|
super(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean step() {
|
||||||
|
final Collection<Procedure> allProcedures = getScope().getAllProcedures(true);
|
||||||
|
for(Procedure procedure : allProcedures) {
|
||||||
|
final Label procedureLabel = procedure.getLabel();
|
||||||
|
final ControlFlowBlock procedureBlock = getGraph().getBlock(procedureLabel.getRef());
|
||||||
|
if(procedureBlock == null)
|
||||||
|
throw new CompileError("Error! Function is never declared: " + procedure.getFullName());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -37,14 +37,19 @@ public class TestPrograms {
|
|||||||
public TestPrograms() {
|
public TestPrograms() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCStyleDeclMissing() throws IOException, URISyntaxException {
|
||||||
|
assertError("cstyle-decl-missing", "Error! Function is never declared: sum");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCStyleDeclRedefinition() throws IOException, URISyntaxException {
|
public void testCStyleDeclRedefinition() throws IOException, URISyntaxException {
|
||||||
assertError("cstyle-decl-redefinition", "Error! Redefinition of function sum");
|
assertError("cstyle-decl-redefinition", "Error! Redefinition of function: sum");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCStyleDeclMismatch() throws IOException, URISyntaxException {
|
public void testCStyleDeclMismatch() throws IOException, URISyntaxException {
|
||||||
assertError("cstyle-decl-mismatch", "Error! Conflicting declarations for sum");
|
assertError("cstyle-decl-mismatch", "Error! Conflicting declarations for: sum");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
18
src/test/kc/cstyle-decl-missing.kc
Normal file
18
src/test/kc/cstyle-decl-missing.kc
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Test function declarations
|
||||||
|
// Definition of function missing
|
||||||
|
|
||||||
|
// Declaration of a sum-function
|
||||||
|
char sum(char a, int b);
|
||||||
|
|
||||||
|
char * const SCREEN = 0x0400;
|
||||||
|
|
||||||
|
// Definition of main()
|
||||||
|
void main() {
|
||||||
|
SCREEN[0] = sum('a', 2);
|
||||||
|
SCREEN[1] = sum('a', 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user