Search in sources :

Example 1 with CatchClause

use of de.fraunhofer.aisec.cpg.graph.statements.CatchClause in project cpg by Fraunhofer-AISEC.

the class StatementHandler method handleCatchClause.

private CatchClause handleCatchClause(com.github.javaparser.ast.stmt.CatchClause catchCls) {
    CatchClause cClause = NodeBuilder.newCatchClause(catchCls.toString());
    lang.getScopeManager().enterScope(cClause);
    HashSet<Type> possibleTypes = new HashSet<>();
    Type concreteType;
    if (catchCls.getParameter().getType() instanceof UnionType) {
        for (ReferenceType t : ((UnionType) catchCls.getParameter().getType()).getElements()) {
            possibleTypes.add(lang.getTypeAsGoodAsPossible(t));
        }
        // we do not know which of the exceptions was actually thrown, so we assume this might be any
        concreteType = TypeParser.createFrom("java.lang.Throwable", true);
        concreteType.setTypeOrigin(Type.Origin.GUESSED);
    } else {
        concreteType = lang.getTypeAsGoodAsPossible(catchCls.getParameter().getType());
        possibleTypes.add(concreteType);
    }
    VariableDeclaration parameter = NodeBuilder.newVariableDeclaration(catchCls.getParameter().getName().toString(), concreteType, catchCls.getParameter().toString(), false);
    parameter.setPossibleSubTypes(possibleTypes);
    CompoundStatement body = handleBlockStatement(catchCls.getBody());
    cClause.setBody(body);
    cClause.setParameter(parameter);
    lang.getScopeManager().addDeclaration(parameter);
    lang.getScopeManager().leaveScope(cClause);
    return cClause;
}
Also used : UnionType(com.github.javaparser.ast.type.UnionType) UnionType(com.github.javaparser.ast.type.UnionType) ReferenceType(com.github.javaparser.ast.type.ReferenceType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) CatchClause(de.fraunhofer.aisec.cpg.graph.statements.CatchClause) ReferenceType(com.github.javaparser.ast.type.ReferenceType) HashSet(java.util.HashSet)

Example 2 with CatchClause

use of de.fraunhofer.aisec.cpg.graph.statements.CatchClause 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 3 with CatchClause

use of de.fraunhofer.aisec.cpg.graph.statements.CatchClause in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testTryCatch.

@Test
void testTryCatch() throws Exception {
    File file = new File("src/test/resources/components/trystmt.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    TryStatement tryStatement = main.iterator().next().getBodyStatementAs(0, TryStatement.class);
    assertNotNull(tryStatement);
    List<CatchClause> catchClauses = tryStatement.getCatchClauses();
    // should have 3 catch clauses
    assertEquals(3, catchClauses.size());
    // declared exception variable
    VariableDeclaration parameter = catchClauses.get(0).getParameter();
    assertNotNull(parameter);
    assertEquals("e", parameter.getName());
    assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
    // anonymous variable (this is not 100% handled correctly but will do for now)
    parameter = catchClauses.get(1).getParameter();
    assertNotNull(parameter);
    // this is currently our 'unnamed' parameter
    assertEquals("", parameter.getName());
    assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
    // catch all
    parameter = catchClauses.get(2).getParameter();
    assertNull(parameter);
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TryStatement(de.fraunhofer.aisec.cpg.graph.statements.TryStatement) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) CatchClause(de.fraunhofer.aisec.cpg.graph.statements.CatchClause) 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

VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)3 CatchClause (de.fraunhofer.aisec.cpg.graph.statements.CatchClause)3 Statement (com.github.javaparser.ast.stmt.Statement)1 ReferenceType (com.github.javaparser.ast.type.ReferenceType)1 UnionType (com.github.javaparser.ast.type.UnionType)1 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 Declaration (de.fraunhofer.aisec.cpg.graph.declarations.Declaration)1 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)1 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1 de.fraunhofer.aisec.cpg.graph.statements (de.fraunhofer.aisec.cpg.graph.statements)1 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)1 Type (de.fraunhofer.aisec.cpg.graph.types.Type)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Test (org.junit.jupiter.api.Test)1