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());
}
Aggregations