Search in sources :

Example 1 with ForEachStatement

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

the class CXXLanguageFrontendTest method testForEach.

/*@Test
  void testFF() throws Exception {
    File file = new File("src/test/resources/hqxvlc.c");
    TranslationUnitDeclaration tu =
        TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);

    assertNotNull(tu);

    var nodes = SubgraphWalker.flattenAST(tu);
    var types = new HashMap<String, Integer>();
    var codes = new HashMap<String, Set<Integer>>();
    for (var n : nodes) {
      var count = types.computeIfAbsent(n.getClass().getSimpleName(), key -> 0);
      types.put(n.getClass().getSimpleName(), count + 1);

      var code = codes.computeIfAbsent(n.getClass().getSimpleName(), key -> new HashSet<>());
      code.add(n.hashCode());
    }

    for (var t : types.keySet()) {
      System.out.println(t + ": " + types.get(t) + " | unique hash codes: " + codes.get(t).size());
    }
  }*/
@Test
void testForEach() throws Exception {
    File file = new File("src/test/resources/components/foreachstmt.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    @NonNull Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration decl = main.iterator().next();
    VariableDeclaration ls = decl.getVariableDeclarationByName("ls").orElse(null);
    assertNotNull(ls);
    assertEquals(TypeParser.createFrom("std::vector<int>", true), ls.getType());
    assertEquals("ls", ls.getName());
    ForEachStatement forEachStatement = decl.getBodyStatementAs(1, ForEachStatement.class);
    assertNotNull(forEachStatement);
    // should loop over ls
    assertEquals(ls, ((DeclaredReferenceExpression) forEachStatement.getIterable()).getRefersTo());
    // should declare auto i (so far no concrete type inferrable)
    Statement stmt = forEachStatement.getVariable();
    assertNotNull(stmt);
    assertTrue(stmt instanceof DeclarationStatement);
    assertTrue(((DeclarationStatement) stmt).isSingleDeclaration());
    VariableDeclaration i = (VariableDeclaration) ((DeclarationStatement) stmt).getSingleDeclaration();
    assertNotNull(i);
    assertEquals("i", i.getName());
    assertEquals(UnknownType.getUnknownType(), i.getType());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) 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) NonNull(org.checkerframework.checker.nullness.qual.NonNull) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) ForEachStatement(de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement) DeclarationStatement(de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement) 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 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 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Test (org.junit.jupiter.api.Test)1