Search in sources :

Example 1 with Declaration

use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration in project cpg by Fraunhofer-AISEC.

the class BotanExampleTest method testExample.

@Test
void testExample() throws Exception {
    File file = new File("src/test/resources/botan/symm_block_cipher.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), false);
    assertNotNull(declaration);
    List<Declaration> declarations = declaration.getDeclarations();
    assertEquals(5, declarations.size());
    Declaration doCrypt = declarations.stream().filter(decl -> decl.getName().equals("do_crypt")).findFirst().get();
    assertTrue(doCrypt instanceof FunctionDeclaration);
    assertEquals("do_crypt", doCrypt.getName());
    Declaration encrypt = declarations.stream().filter(decl -> decl.getName().equals("encrypt")).findFirst().get();
    assertTrue(encrypt instanceof FunctionDeclaration);
    assertEquals("encrypt", encrypt.getName());
    Declaration decrypt = declarations.stream().filter(decl -> decl.getName().equals("decrypt")).findFirst().get();
    assertTrue(decrypt instanceof FunctionDeclaration);
    assertEquals("decrypt", decrypt.getName());
    Declaration main = declarations.stream().filter(decl -> decl.getName().equals("main")).findFirst().get();
    assertTrue(main instanceof FunctionDeclaration);
    assertEquals("main", main.getName());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 2 with Declaration

use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration 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 Declaration

use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration in project cpg by Fraunhofer-AISEC.

the class StatementHandler method handleTryStatement.

private TryStatement handleTryStatement(Statement stmt) {
    TryStmt tryStmt = stmt.asTryStmt();
    TryStatement tryStatement = NodeBuilder.newTryStatement(stmt.toString());
    lang.getScopeManager().enterScope(tryStatement);
    List<de.fraunhofer.aisec.cpg.graph.statements.Statement> resources = tryStmt.getResources().stream().map(lang.getExpressionHandler()::handle).collect(Collectors.toList());
    CompoundStatement tryBlock = handleBlockStatement(tryStmt.getTryBlock());
    List<CatchClause> catchClauses = tryStmt.getCatchClauses().stream().map(this::handleCatchClause).collect(Collectors.toList());
    CompoundStatement finallyBlock = tryStmt.getFinallyBlock().map(this::handleBlockStatement).orElse(null);
    lang.getScopeManager().leaveScope(tryStatement);
    tryStatement.setResources(resources);
    tryStatement.setTryBlock(tryBlock);
    tryStatement.setFinallyBlock(finallyBlock);
    tryStatement.setCatchClauses(catchClauses);
    for (de.fraunhofer.aisec.cpg.graph.statements.Statement r : resources) {
        if (r instanceof DeclarationStatement) {
            for (Declaration d : r.getDeclarations()) {
                if (d instanceof VariableDeclaration) {
                    lang.getScopeManager().addDeclaration(d);
                }
            }
        }
    }
    return tryStatement;
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) de.fraunhofer.aisec.cpg.graph.statements(de.fraunhofer.aisec.cpg.graph.statements) CatchClause(de.fraunhofer.aisec.cpg.graph.statements.CatchClause) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)

Example 4 with Declaration

use of de.fraunhofer.aisec.cpg.graph.declarations.Declaration in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testAssignmentExpression.

@Test
void testAssignmentExpression() throws Exception {
    File file = new File("src/test/resources/assignmentexpression.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    // just take a look at the second function
    FunctionDeclaration functionDeclaration = declaration.getDeclarationAs(1, FunctionDeclaration.class);
    List<Statement> statements = getStatementsOfFunction(functionDeclaration);
    Statement declareA = statements.get(0);
    Declaration a = ((DeclarationStatement) declareA).getSingleDeclaration();
    Statement assignA = statements.get(1);
    assertTrue(assignA instanceof BinaryOperator);
    Expression lhs = ((BinaryOperator) assignA).getLhs();
    Expression rhs = ((BinaryOperator) assignA).getRhs();
    assertEquals("a", ((BinaryOperator) assignA).getLhs().getName());
    assertEquals(2, ((Literal) ((BinaryOperator) assignA).getRhs()).getValue());
    assertRefersTo(((BinaryOperator) assignA).getLhs(), a);
    Statement declareB = statements.get(2);
    assertTrue(declareB instanceof DeclarationStatement);
    Declaration b = ((DeclarationStatement) declareB).getSingleDeclaration();
    // a = b
    Statement assignB = statements.get(3);
    assertTrue(assignB instanceof BinaryOperator);
    lhs = ((BinaryOperator) assignB).getLhs();
    rhs = ((BinaryOperator) assignB).getRhs();
    assertEquals("a", lhs.getName());
    assertTrue(rhs instanceof DeclaredReferenceExpression);
    assertEquals("b", rhs.getName());
    assertRefersTo(rhs, b);
    Statement assignBWithFunction = statements.get(4);
    assertTrue(assignBWithFunction instanceof BinaryOperator);
    assertEquals("a", ((BinaryOperator) assignBWithFunction).getLhs().getName());
    assertTrue(((BinaryOperator) assignBWithFunction).getRhs() instanceof CallExpression);
    CallExpression call = (CallExpression) ((BinaryOperator) assignBWithFunction).getRhs();
    assertEquals("someFunction", call.getName());
    assertRefersTo(call.getArguments().get(0), b);
}
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) 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) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

Declaration (de.fraunhofer.aisec.cpg.graph.declarations.Declaration)4 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)3 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)3 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)3 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)3 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)3 File (java.io.File)3 Test (org.junit.jupiter.api.Test)3 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)2 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)2 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)2 NamespaceDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration)2 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)2 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)2 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)2 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)2 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)2 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)2 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)2 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)2