Search in sources :

Example 1 with Statement

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) 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 Statement

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());
}
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 3 with Statement

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);
    }
}
Also used : Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Example 4 with Statement

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);
    }
}
Also used : Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 5 with Statement

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

Aggregations

Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)14 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)12 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)12 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)12 Test (org.junit.jupiter.api.Test)12 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)11 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)11 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)11 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)11 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)11 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)11 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)11 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)11 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)11 File (java.io.File)11 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)6 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)3 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)3 HasType (de.fraunhofer.aisec.cpg.graph.HasType)2 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)2