Search in sources :

Example 1 with TryStatement

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

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)1 CatchClause (de.fraunhofer.aisec.cpg.graph.statements.CatchClause)1 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1