Search in sources :

Example 1 with IfStatement

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

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 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)1 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)1 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)1 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)1 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)1 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)1 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)1 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)1 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)1 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1