Search in sources :

Example 6 with Node

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

the class CallResolverTest method testScopedFunctionResolutionAfterRedeclaration.

void testScopedFunctionResolutionAfterRedeclaration(List<TranslationUnitDeclaration> result, List<CallExpression> calls) {
    CallExpression fn = TestUtils.findByUniquePredicate(calls, c -> c.getLocation().getRegion().getStartLine() == 13);
    Literal<?> literal7 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(7));
    assertEquals(1, fn.getInvokes().size());
    assertFalse(fn.getInvokes().get(0).isImplicit());
    assertEquals(2, fn.getInvokes().get(0).getLocation().getRegion().getStartLine());
    assertEquals(1, fn.getArguments().size());
    assertEquals(6, ((Literal) fn.getArguments().get(0)).getValue());
    assertTrue(fn.getInvokes().get(0).getNextEOG().contains(literal7));
    for (Node node : fn.getInvokes().get(0).getNextEOG()) {
        assertTrue(node.equals(literal7) || literal7.getNextEOG().contains(node));
    }
}
Also used : Literal(de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal) Node(de.fraunhofer.aisec.cpg.graph.Node) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)

Example 7 with Node

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

the class CallResolverTest method testDefaultArgumentsInDefinition.

@Test
void testDefaultArgumentsInDefinition() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultargs", "defaultInDefinition.cpp").toFile()), topLevel, true);
    List<CallExpression> calls = TestUtils.subnodesOfType(result, CallExpression.class);
    List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    FunctionDeclaration displayFunction = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("display") && !f.isImplicit());
    Literal<?> literalStar = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals('*'));
    Literal<?> literal3 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(3));
    // Check defaults edge of ParamVariableDeclaration
    assertTrue(displayFunction.getDefaultParameters().get(0) instanceof Literal);
    assertTrue(displayFunction.getDefaultParameters().get(1) instanceof Literal);
    assertEquals('*', ((Literal) displayFunction.getDefaultParameters().get(0)).getValue());
    assertEquals(3, ((Literal) displayFunction.getDefaultParameters().get(1)).getValue());
    // Check call display();
    CallExpression display = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display();");
    });
    assertEquals(1, display.getInvokes().size());
    assertEquals(displayFunction, display.getInvokes().get(0));
    assertEquals(0, display.getArguments().size());
    assertTrue(displayFunction.getNextEOG().contains(literalStar));
    assertTrue(displayFunction.getNextEOG().contains(literal3));
    assertTrue(literalStar.getNextEOG().contains(literal3));
    for (Node node : displayFunction.getNextEOG()) {
        assertTrue(node.equals(literal3) || node.equals(literalStar) || literal3.getNextEOG().contains(node));
    }
    // Check call display('#');
    CallExpression displayHash = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display('#');");
    });
    assertEquals(1, displayHash.getInvokes().size());
    assertEquals(displayFunction, displayHash.getInvokes().get(0));
    assertEquals(1, displayHash.getArguments().size());
    assertTrue(displayHash.getArguments().get(0) instanceof Literal);
    assertEquals('#', ((Literal) displayHash.getArguments().get(0)).getValue());
    // Check call display('#');
    CallExpression display$Count = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("display('$', count);");
    });
    assertEquals(1, display$Count.getInvokes().size());
    assertEquals(displayFunction, display$Count.getInvokes().get(0));
    assertTrue(display$Count.getArguments().get(0) instanceof Literal);
    assertEquals('$', ((Literal) display$Count.getArguments().get(0)).getValue());
    assertEquals("count", display$Count.getArguments().get(1).getName());
}
Also used : Literal(de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal) Node(de.fraunhofer.aisec.cpg.graph.Node) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 8 with Node

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

the class CallResolverTest method testScopedFunctionResolutionRedeclaration.

void testScopedFunctionResolutionRedeclaration(List<TranslationUnitDeclaration> result, List<CallExpression> calls) {
    CallExpression fm1 = TestUtils.findByUniquePredicate(calls, c -> c.getLocation().getRegion().getStartLine() == 8);
    assertEquals(1, fm1.getInvokes().size());
    assertEquals(1, fm1.getArguments().size());
    assertEquals(8, ((Literal) fm1.getArguments().get(0)).getValue());
    CallExpression fm2 = TestUtils.findByUniquePredicate(calls, c -> c.getLocation().getRegion().getStartLine() == 10);
    Literal<?> literal5 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(5));
    assertEquals(1, fm2.getInvokes().size());
    assertEquals(9, fm2.getInvokes().get(0).getLocation().getRegion().getStartLine());
    assertEquals(1, fm2.getArguments().size());
    assertEquals(4, ((Literal) fm2.getArguments().get(0)).getValue());
    assertTrue(fm2.getInvokes().get(0).getNextEOG().contains(literal5));
    for (Node node : fm2.getInvokes().get(0).getNextEOG()) {
        assertTrue(node.equals(literal5) || literal5.getNextEOG().contains(node));
    }
}
Also used : Literal(de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal) Node(de.fraunhofer.aisec.cpg.graph.Node) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)

Example 9 with Node

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

the class CallResolverTest method testPartialDefaultArguments.

@Test
void testPartialDefaultArguments() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultargs", "partialDefaults.cpp").toFile()), topLevel, true);
    List<CallExpression> calls = TestUtils.subnodesOfType(result, CallExpression.class);
    List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    FunctionDeclaration addFunction = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("add") && !f.isInferred());
    FunctionDeclaration addFunctionInferred = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("add") && f.isInferred());
    // Check call add();
    CallExpression add = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("add();");
    });
    assertEquals(1, add.getInvokes().size());
    assertEquals(addFunctionInferred, add.getInvokes().get(0));
    // Check call add(1, 2);
    CallExpression add12 = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("add(1,2);");
    });
    assertEquals(1, add12.getInvokes().size());
    assertEquals(addFunction, add12.getInvokes().get(0));
    assertEquals(2, add12.getArguments().size());
    assertEquals("1", add12.getArguments().get(0).getCode());
    assertEquals("2", add12.getArguments().get(1).getCode());
    assertTrue(addFunction.getNextEOG().contains(addFunction.getDefaultParameters().get(2)));
    assertTrue(addFunction.getNextEOG().contains(addFunction.getDefaultParameters().get(3)));
    assertTrue(addFunction.getDefaultParameters().get(2).getNextEOG().contains(addFunction.getDefaultParameters().get(3)));
    for (Node node : addFunction.getNextEOG()) {
        assertTrue(node.equals(addFunction.getDefaultParameters().get(2)) || node.equals(addFunction.getDefaultParameters().get(3)) || addFunction.getDefaultParameters().get(3).getNextEOG().contains(node));
    }
    // Check call add(1, 2, 5, 6);
    CallExpression add1256 = TestUtils.findByUniquePredicate(calls, c -> {
        assert c.getCode() != null;
        return c.getCode().equals("add(1,2,5,6);");
    });
    assertEquals(1, add1256.getInvokes().size());
    assertEquals(addFunction, add1256.getInvokes().get(0));
    assertEquals(4, add1256.getArguments().size());
    assertEquals("1", add1256.getArguments().get(0).getCode());
    assertEquals("2", add1256.getArguments().get(1).getCode());
    assertEquals("5", add1256.getArguments().get(2).getCode());
    assertEquals("6", add1256.getArguments().get(3).getCode());
}
Also used : Node(de.fraunhofer.aisec.cpg.graph.Node) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 10 with Node

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

the class CXXLanguageFrontendTest method testSwitch.

@Test
void testSwitch() throws Exception {
    File file = new File("src/test/resources/cfg/switch.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    List<Node> graphNodes = SubgraphWalker.flattenAST(declaration);
    graphNodes.sort(new NodeComparator());
    assertTrue(graphNodes.size() != 0);
    List<SwitchStatement> switchStatements = Util.filterCast(graphNodes, SwitchStatement.class);
    assertTrue(switchStatements.size() == 3);
    SwitchStatement switchStatement = switchStatements.get(0);
    assertTrue(((CompoundStatement) switchStatement.getStatement()).getStatements().size() == 11);
    List<CaseStatement> caseStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), CaseStatement.class);
    assertTrue(caseStatements.size() == 4);
    List<DefaultStatement> defaultStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), DefaultStatement.class);
    assertTrue(defaultStatements.size() == 1);
}
Also used : SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) Node(de.fraunhofer.aisec.cpg.graph.Node) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

Node (de.fraunhofer.aisec.cpg.graph.Node)17 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)11 Test (org.junit.jupiter.api.Test)11 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)8 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)7 Literal (de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal)6 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)5 File (java.io.File)5 TranslationConfiguration (de.fraunhofer.aisec.cpg.TranslationConfiguration)3 TranslationManager (de.fraunhofer.aisec.cpg.TranslationManager)3 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)3 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)3 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)3 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)3 Collectors (java.util.stream.Collectors)3 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)2 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)2 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)2