Search in sources :

Example 1 with ReturnStatement

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with ReturnStatement

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());
}
Also used : DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) File(java.io.File) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with ReturnStatement

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());
}
Also used : TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) NamespaceDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration) File(java.io.File) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with ReturnStatement

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);
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 5 with 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());
}
Also used : CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) IfStatement(de.fraunhofer.aisec.cpg.graph.statements.IfStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)9 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)9 Test (org.junit.jupiter.api.Test)9 File (java.io.File)6 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)5 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)5 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)5 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)5 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)5 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)5 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)5 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)5 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)5 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)5 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)5 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)2 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)2 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)2 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)1 Declaration (de.fraunhofer.aisec.cpg.graph.declarations.Declaration)1