use of de.fraunhofer.aisec.cpg.graph.statements.Statement in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testAllEogNodeVisitor.
/**
* Visits all nodes along EOG.
*/
@Test
void testAllEogNodeVisitor() {
List<Node> nodeList = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
MethodDeclaration method = recordDeclaration.getMethods().stream().filter(m -> m.getName().equals("method")).collect(Collectors.toList()).get(0);
/* TODO A better way to get the "first" statement in a method body is needed.
This is currently the only (fragile, ugly and unsafe) way to get to the first "real" statement in a method body.
getNextEOG() and getNextCFG() return empty lists.
*/
Statement firstStmt = ((CompoundStatement) method.getBody()).getStatements().get(0);
firstStmt.accept(Strategy::EOG_FORWARD, new IVisitor<Node>() {
public void visit(Node n) {
System.out.println(n);
nodeList.add(n);
}
});
assertEquals(22, nodeList.size());
}
use of de.fraunhofer.aisec.cpg.graph.statements.Statement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testDeclarationStatement.
@Test
void testDeclarationStatement() throws Exception {
File file = new File("src/test/resources/declstmt.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
FunctionDeclaration function = declaration.getDeclarationAs(0, FunctionDeclaration.class);
List<Statement> statements = getStatementsOfFunction(function);
statements.forEach(node -> {
LOGGER.debug("{}", node);
assertTrue(node instanceof DeclarationStatement || statements.indexOf(node) == statements.size() - 1 && node instanceof ReturnStatement);
});
VariableDeclaration declFromMultiplicateExpression = ((DeclarationStatement) statements.get(0)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("SSL_CTX*", true), declFromMultiplicateExpression.getType());
assertEquals("ptr", declFromMultiplicateExpression.getName());
VariableDeclaration withInitializer = ((DeclarationStatement) statements.get(1)).getSingleDeclarationAs(VariableDeclaration.class);
Expression initializer = withInitializer.getInitializer();
assertNotNull(initializer);
assertTrue(initializer instanceof Literal);
assertEquals(1, ((Literal) initializer).getValue());
List<Declaration> twoDeclarations = statements.get(2).getDeclarations();
assertEquals(2, twoDeclarations.size());
VariableDeclaration b = (VariableDeclaration) twoDeclarations.get(0);
assertNotNull(b);
assertEquals("b", b.getName());
assertEquals(TypeParser.createFrom("int*", false), b.getType());
VariableDeclaration c = (VariableDeclaration) twoDeclarations.get(1);
assertNotNull(c);
assertEquals("c", c.getName());
assertEquals(TypeParser.createFrom("int", false), c.getType());
VariableDeclaration withoutInitializer = ((DeclarationStatement) statements.get(3)).getSingleDeclarationAs(VariableDeclaration.class);
initializer = withoutInitializer.getInitializer();
assertEquals(TypeParser.createFrom("int*", true), withoutInitializer.getType());
assertEquals("d", withoutInitializer.getName());
assertNull(initializer);
VariableDeclaration qualifiedType = ((DeclarationStatement) statements.get(4)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("std.string", true), qualifiedType.getType());
assertEquals("text", qualifiedType.getName());
assertTrue(qualifiedType.getInitializer() instanceof Literal);
assertEquals("some text", ((Literal) qualifiedType.getInitializer()).getValue());
VariableDeclaration pointerWithAssign = ((DeclarationStatement) statements.get(5)).getSingleDeclarationAs(VariableDeclaration.class);
assertEquals(TypeParser.createFrom("void*", true), pointerWithAssign.getType());
assertEquals("ptr2", pointerWithAssign.getName());
assertEquals("NULL", pointerWithAssign.getInitializer().getName());
List<Declaration> classWithVariable = statements.get(6).getDeclarations();
assertEquals(2, classWithVariable.size());
RecordDeclaration classA = (RecordDeclaration) classWithVariable.get(0);
assertNotNull(classA);
assertEquals("A", classA.getName());
VariableDeclaration myA = (VariableDeclaration) classWithVariable.get(1);
assertNotNull(myA);
assertEquals("myA", myA.getName());
assertEquals(classA, ((ObjectType) myA.getType()).getRecordDeclaration());
}
use of de.fraunhofer.aisec.cpg.graph.statements.Statement in project cpg by Fraunhofer-AISEC.
the class ExpressionList method addExpression.
public void addExpression(Statement expression) {
if (!this.expressions.isEmpty()) {
Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
if (lastExpression instanceof HasType)
((HasType) lastExpression).unregisterTypeListener(this);
this.removePrevDFG(lastExpression);
}
PropertyEdge<Statement> propertyEdge = new PropertyEdge<>(this, expression);
propertyEdge.addProperty(Properties.INDEX, this.expressions.size());
this.expressions.add(propertyEdge);
this.addPrevDFG(expression);
if (expression instanceof HasType) {
((HasType) expression).registerTypeListener(this);
}
}
use of de.fraunhofer.aisec.cpg.graph.statements.Statement in project cpg by Fraunhofer-AISEC.
the class ExpressionList method setExpressions.
public void setExpressions(List<Statement> expressions) {
if (!this.expressions.isEmpty()) {
Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
if (lastExpression instanceof HasType)
((HasType) lastExpression).unregisterTypeListener(this);
this.removePrevDFG(lastExpression);
}
this.expressions = PropertyEdge.transformIntoOutgoingPropertyEdgeList(expressions, this);
if (!this.expressions.isEmpty()) {
Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
this.addPrevDFG(lastExpression);
if (lastExpression instanceof HasType)
((HasType) lastExpression).registerTypeListener(this);
}
}
use of de.fraunhofer.aisec.cpg.graph.statements.Statement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testShiftExpression.
@Test
void testShiftExpression() throws Exception {
File file = new File("src/test/resources/shiftexpression.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
FunctionDeclaration functionDeclaration = declaration.getDeclarationAs(0, FunctionDeclaration.class);
List<Statement> statements = getStatementsOfFunction(functionDeclaration);
assertTrue(statements.get(1) instanceof BinaryOperator);
}
Aggregations