Search in sources :

Example 1 with CastExpression

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

the class CallResolverTest method testImplicitCastMethodCallResolution.

@Test
void testImplicitCastMethodCallResolution() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcast", "implicitCastInMethod.cpp").toFile()), topLevel, true);
    List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    List<CallExpression> callExpressions = TestUtils.subnodesOfType(result, CallExpression.class);
    // Check resolution of calc
    CallExpression calc = TestUtils.findByUniqueName(callExpressions, "calc");
    FunctionDeclaration calcFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("calc") && !f.isInferred());
    assertEquals(1, calc.getInvokes().size());
    assertEquals(calcFunctionDeclaration, calc.getInvokes().get(0));
    assertTrue(calc.getArguments().get(0) instanceof CastExpression);
    assertEquals(2.0, ((Literal) ((CastExpression) calc.getArguments().get(0)).getExpression()).getValue());
    assertEquals("int", ((CastExpression) calc.getArguments().get(0)).getCastType().getName());
    // Check resolution of doSmth
    CallExpression doSmth = TestUtils.findByUniqueName(callExpressions, "doSmth");
    FunctionDeclaration doSmthFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("doSmth") && !f.isInferred());
    assertEquals(1, doSmth.getInvokes().size());
    assertEquals(doSmthFunctionDeclaration, doSmth.getInvokes().get(0));
    assertTrue(doSmth.getArguments().get(0) instanceof CastExpression);
    assertEquals(10.0, ((Literal) ((CastExpression) doSmth.getArguments().get(0)).getExpression()).getValue());
    assertEquals("int", ((CastExpression) doSmth.getArguments().get(0)).getCastType().getName());
}
Also used : CastExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with CastExpression

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

the class CallResolverTest method testCxxPrioResolutionWithMethods.

@Test
void testCxxPrioResolutionWithMethods() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "cxxprioresolution", "methodresolution", "overloadedresolution.cpp").toFile()), topLevel, true);
    List<CallExpression> calls = TestUtils.subnodesOfType(result, CallExpression.class);
    List<MethodDeclaration> methodDeclarations = TestUtils.subnodesOfType(result, MethodDeclaration.class);
    FunctionDeclaration calcOverload = TestUtils.findByUniquePredicate(methodDeclarations, c -> c.getRecordDeclaration().getName().equals("Overload") && !(c instanceof ConstructorDeclaration));
    // This call must resolve to implicit cast of the overloaded class and not to the base class
    CallExpression calcInt = TestUtils.findByUniquePredicate(calls, c -> {
        if (c.getLocation() != null) {
            return c.getLocation().getRegion().getStartLine() == 24;
        }
        return false;
    });
    assertEquals(1, calcInt.getInvokes().size());
    assertEquals(calcOverload, calcInt.getInvokes().get(0));
    assertTrue(calcInt.getArguments().get(0) instanceof CastExpression);
    assertEquals("double", ((CastExpression) calcInt.getArguments().get(0)).getCastType().getName());
    CallExpression calcDouble = TestUtils.findByUniquePredicate(calls, c -> {
        if (c.getLocation() != null) {
            return c.getLocation().getRegion().getStartLine() == 25;
        }
        return false;
    });
    assertEquals(1, calcDouble.getInvokes().size());
    assertEquals(calcOverload, calcDouble.getInvokes().get(0));
    assertEquals(1.1, ((Literal) calcDouble.getArguments().get(0)).getValue());
}
Also used : CastExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with CastExpression

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

the class CallResolverTest method testImplicitCastCallResolution.

@Test
void testImplicitCastCallResolution() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcast", "ambiguouscall.cpp").toFile(), Path.of(topLevel.toString(), "implicitcast", "implicitcast.cpp").toFile()), topLevel, true);
    List<CallExpression> callExpressions = TestUtils.subnodesOfType(result, CallExpression.class);
    // Check resolution of implicit cast
    CallExpression multiply = TestUtils.findByUniqueName(callExpressions, "multiply");
    assertEquals(1, multiply.getInvokes().size());
    FunctionDeclaration functionDeclaration = multiply.getInvokes().get(0);
    assertFalse(functionDeclaration.isInferred());
    assertEquals("int", functionDeclaration.getSignatureTypes().get(0).getTypeName());
    assertTrue(multiply.getArguments().get(0) instanceof CastExpression);
    CastExpression implicitCast = (CastExpression) multiply.getArguments().get(0);
    assertEquals("int", implicitCast.getCastType().getTypeName());
    assertEquals("10.0", implicitCast.getExpression().getCode());
    // Check implicit cast in case of ambiguous call
    CallExpression ambiguousCall = TestUtils.findByUniqueName(callExpressions, "ambiguous_multiply");
    // Check invokes
    List<FunctionDeclaration> functionDeclarations = ambiguousCall.getInvokes();
    assertEquals(2, functionDeclarations.size());
    for (FunctionDeclaration func : functionDeclarations) {
        assertFalse(func.isImplicit());
        assertTrue((func.getParameters().get(0).getType().getName().equals("int")) || (func.getParameters().get(0).getType().getName().equals("float")));
    }
    // Check Cast
    assertTrue(ambiguousCall.getArguments().get(0) instanceof CastExpression);
    CastExpression castExpression = (CastExpression) ambiguousCall.getArguments().get(0);
    assertEquals(UnknownType.getUnknownType(), castExpression.getType());
    assertEquals("10.0", castExpression.getExpression().getCode());
}
Also used : CastExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with CastExpression

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

the class CallResolverTest method testDefaultArgumentsInDeclaration.

@Test
void testDefaultArgumentsInDeclaration() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultargs", "defaultInDeclaration.cpp").toFile()), topLevel, true);
    List<CallExpression> calls = TestUtils.subnodesOfType(result, CallExpression.class);
    List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    FunctionDeclaration displayDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("display") && !f.isDefinition() && !f.isImplicit());
    FunctionDeclaration displayDefinition = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("display") && f.isDefinition() && !f.isImplicit());
    // Check defines edge
    assertEquals(displayDefinition, displayDeclaration.getDefinition());
    // Check defaults edge of ParamVariableDeclaration
    assertEquals(displayDeclaration.getDefaultParameters(), displayDefinition.getDefaultParameters());
    // Check call display(1);
    CallExpression display1 = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display(1);");
    });
    // it will contain two nodes: the definition and the declaration. this is a general
    // problem, that we need to tackle in the future, how to combine those two. See
    // https://github.com/Fraunhofer-AISEC/cpg/issues/194
    assertEquals(2, display1.getInvokes().size());
    assertTrue(display1.getInvokes().contains(displayDeclaration));
    assertEquals("1", display1.getArguments().get(0).getCode());
    assertTrue(displayDeclaration.getNextEOG().contains(displayDeclaration.getDefaultParameters().get(1)));
    assertTrue(displayDeclaration.getNextEOG().contains(displayDeclaration.getDefaultParameters().get(0)));
    assertTrue(displayDeclaration.getDefaultParameters().get(0).getNextEOG().contains(displayDeclaration.getDefaultParameters().get(1)));
    for (Node node : displayDeclaration.getNextEOG()) {
        assertTrue(node.equals(displayDeclaration.getDefaultParameters().get(0)) || node.equals(displayDeclaration.getDefaultParameters().get(1)) || displayDeclaration.getDefaultParameters().get(1).getNextEOG().contains(node));
    }
    CallExpression display = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display();");
    });
    assertEquals(2, display.getInvokes().size());
    assertTrue(display.getInvokes().contains(displayDeclaration));
    assertEquals(0, display.getArguments().size());
    CallExpression displayCount$ = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display(count, '$');");
    });
    assertEquals(2, display.getInvokes().size());
    assertTrue(display.getInvokes().contains(displayDeclaration));
    assertEquals("count", displayCount$.getArguments().get(0).getName());
    assertEquals("'$'", displayCount$.getArguments().get(1).getCode());
    CallExpression display10 = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display(10.0);");
    });
    assertEquals(2, display10.getInvokes().size());
    assertTrue(display.getInvokes().contains(displayDeclaration));
    assertEquals(1, display10.getArguments().size());
    assertTrue(display10.getArguments().get(0) instanceof CastExpression);
    assertEquals("10.0", ((CastExpression) display10.getArguments().get(0)).getExpression().getCode());
    assertEquals("int", ((CastExpression) display10.getArguments().get(0)).getCastType().getName());
}
Also used : Node(de.fraunhofer.aisec.cpg.graph.Node) CastExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)4 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)4 CastExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression)4 Test (org.junit.jupiter.api.Test)4 Node (de.fraunhofer.aisec.cpg.graph.Node)1