use of de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testReturnStmtVisitor.
/**
* Visits only ReturnStatement nodes.
*/
@Test
void testReturnStmtVisitor() {
List<ReturnStatement> returnStmts = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
recordDeclaration.accept(Strategy::AST_FORWARD, new IVisitor<Node>() {
public void visit(ReturnStatement r) {
returnStmts.add(r);
}
});
assertEquals(2, returnStmts.size());
}
use of de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement in project cpg by Fraunhofer-AISEC.
the class CXXIncludeTest method testDefinitionsAndDeclaration.
@Test
void testDefinitionsAndDeclaration() throws Exception {
File file = new File("src/test/resources/include.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
for (Declaration d : tu.getDeclarations()) {
System.out.println(d.getName() + " " + d.getLocation());
}
assertEquals(6, tu.getDeclarations().size());
RecordDeclaration someClass = tu.getDeclarationsByName("SomeClass", RecordDeclaration.class).iterator().next();
assertNotNull(someClass);
Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
ConstructorDeclaration someClassConstructor = tu.getDeclarationsByName("SomeClass", ConstructorDeclaration.class).iterator().next();
assertNotNull(someClassConstructor);
assertEquals(someClass, someClassConstructor.getRecordDeclaration());
MethodDeclaration doSomething = tu.getDeclarationsByName("DoSomething", MethodDeclaration.class).iterator().next();
assertNotNull(doSomething);
assertEquals(someClass, doSomething.getRecordDeclaration());
ReturnStatement returnStatement = doSomething.getBodyStatementAs(0, ReturnStatement.class);
assertNotNull(returnStatement);
DeclaredReferenceExpression ref = (DeclaredReferenceExpression) returnStatement.getReturnValue();
assertNotNull(ref);
FieldDeclaration someField = someClass.getField("someField");
assertNotNull(someField);
assertEquals(someField, ref.getRefersTo());
}
use of de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement 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.ReturnStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testIf.
@Test
void testIf() throws Exception {
File file = new File("src/test/resources/if.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
List<Statement> statements = getStatementsOfFunction(declaration.getDeclarationAs(0, FunctionDeclaration.class));
IfStatement ifStatement = (IfStatement) statements.get(0);
assertNotNull(ifStatement);
assertNotNull(ifStatement.getCondition());
assertEquals("bool", ifStatement.getCondition().getType().getTypeName());
assertEquals(true, ((Literal) ifStatement.getCondition()).getValue());
assertTrue(((CompoundStatement) ifStatement.getThenStatement()).getStatements().get(0) instanceof ReturnStatement);
assertTrue(((CompoundStatement) ifStatement.getElseStatement()).getStatements().get(0) instanceof ReturnStatement);
}
use of de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testDesignatedInitializer.
@Test
void testDesignatedInitializer() throws Exception {
File file = new File("src/test/resources/components/designatedInitializer.c");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
// should be four method nodes
assertEquals(2, declaration.getDeclarations().size());
FunctionDeclaration method = declaration.getDeclarationAs(1, FunctionDeclaration.class);
assertEquals("main()int", method.getSignature());
assertTrue(method.getBody() instanceof CompoundStatement);
List<Statement> statements = ((CompoundStatement) method.getBody()).getStatements();
assertEquals(4, statements.size());
assertTrue(statements.get(0) instanceof DeclarationStatement);
assertTrue(statements.get(1) instanceof DeclarationStatement);
assertTrue(statements.get(2) instanceof DeclarationStatement);
assertTrue(statements.get(3) instanceof ReturnStatement);
Expression initializer = ((VariableDeclaration) ((DeclarationStatement) statements.get(0)).getSingleDeclaration()).getInitializer();
assertTrue(initializer instanceof InitializerListExpression);
assertEquals(3, ((InitializerListExpression) initializer).getInitializers().size());
assertTrue(((InitializerListExpression) initializer).getInitializers().get(0) instanceof DesignatedInitializerExpression);
assertTrue(((InitializerListExpression) initializer).getInitializers().get(1) instanceof DesignatedInitializerExpression);
assertTrue(((InitializerListExpression) initializer).getInitializers().get(2) instanceof DesignatedInitializerExpression);
DesignatedInitializerExpression die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(0);
assertTrue(die.getLhs().get(0) instanceof DeclaredReferenceExpression);
assertTrue(die.getRhs() instanceof Literal);
assertEquals("y", die.getLhs().get(0).getName());
assertEquals(0, ((Literal) die.getRhs()).getValue());
die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(1);
assertTrue(die.getLhs().get(0) instanceof DeclaredReferenceExpression);
assertTrue(die.getRhs() instanceof Literal);
assertEquals("z", die.getLhs().get(0).getName());
assertEquals(1, ((Literal) die.getRhs()).getValue());
die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(2);
assertTrue(die.getLhs().get(0) instanceof DeclaredReferenceExpression);
assertTrue(die.getRhs() instanceof Literal);
assertEquals("x", die.getLhs().get(0).getName());
assertEquals(2, ((Literal) die.getRhs()).getValue());
initializer = ((VariableDeclaration) ((DeclarationStatement) statements.get(1)).getSingleDeclaration()).getInitializer();
assertTrue(initializer instanceof InitializerListExpression);
assertEquals(1, ((InitializerListExpression) initializer).getInitializers().size());
assertTrue(((InitializerListExpression) initializer).getInitializers().get(0) instanceof DesignatedInitializerExpression);
die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(0);
assertTrue(die.getLhs().get(0) instanceof DeclaredReferenceExpression);
assertTrue(die.getRhs() instanceof Literal);
assertEquals("x", die.getLhs().get(0).getName());
assertEquals(20, ((Literal) die.getRhs()).getValue());
initializer = ((VariableDeclaration) ((DeclarationStatement) statements.get(2)).getSingleDeclaration()).getInitializer();
assertTrue(initializer instanceof InitializerListExpression);
assertEquals(2, ((InitializerListExpression) initializer).getInitializers().size());
assertTrue(((InitializerListExpression) initializer).getInitializers().get(0) instanceof DesignatedInitializerExpression);
assertTrue(((InitializerListExpression) initializer).getInitializers().get(1) instanceof DesignatedInitializerExpression);
die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(0);
assertTrue(die.getLhs().get(0) instanceof Literal);
assertTrue(die.getRhs() instanceof Literal);
assertEquals(3, ((Literal) die.getLhs().get(0)).getValue());
assertEquals(1, ((Literal) die.getRhs()).getValue());
die = (DesignatedInitializerExpression) ((InitializerListExpression) initializer).getInitializers().get(1);
assertTrue(die.getLhs().get(0) instanceof Literal);
assertTrue(die.getRhs() instanceof Literal);
assertEquals(5, ((Literal) die.getLhs().get(0)).getValue());
assertEquals(2, ((Literal) die.getRhs()).getValue());
}
Aggregations