use of de.fraunhofer.aisec.cpg.processing.strategy.Strategy in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testAllEogNodeVisitor.
/**
* Visits all nodes along EOG.
*/
@Test
void testAllEogNodeVisitor() {
List<Node> nodeList = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
MethodDeclaration method = recordDeclaration.getMethods().stream().filter(m -> m.getName().equals("method")).collect(Collectors.toList()).get(0);
/* TODO A better way to get the "first" statement in a method body is needed.
This is currently the only (fragile, ugly and unsafe) way to get to the first "real" statement in a method body.
getNextEOG() and getNextCFG() return empty lists.
*/
Statement firstStmt = ((CompoundStatement) method.getBody()).getStatements().get(0);
firstStmt.accept(Strategy::EOG_FORWARD, new IVisitor<Node>() {
public void visit(Node n) {
System.out.println(n);
nodeList.add(n);
}
});
assertEquals(22, nodeList.size());
}
use of de.fraunhofer.aisec.cpg.processing.strategy.Strategy in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testReturnStmtVisitor.
/**
* Visits only ReturnStatement nodes.
*/
@Test
void testReturnStmtVisitor() {
List<ReturnStatement> returnStmts = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
recordDeclaration.accept(Strategy::AST_FORWARD, new IVisitor<Node>() {
public void visit(ReturnStatement r) {
returnStmts.add(r);
}
});
assertEquals(2, returnStmts.size());
}
use of de.fraunhofer.aisec.cpg.processing.strategy.Strategy in project cpg by Fraunhofer-AISEC.
the class SubgraphWalker method activateTypes.
public static void activateTypes(Node node) {
AtomicInteger num = new AtomicInteger();
Map<HasType, Set<Type>> typeCache = TypeManager.getInstance().getTypeCache();
node.accept(Strategy::AST_FORWARD, new IVisitor<>() {
@Override
public void visit(Node n) {
if (n instanceof HasType) {
HasType typeNode = (HasType) n;
typeCache.getOrDefault(typeNode, Collections.emptySet()).forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
((HasType) n).setType(t);
});
typeCache.remove((HasType) n);
num.getAndIncrement();
}
}
});
LOGGER.debug("Activated {} nodes for {}", num, node.getName());
// For some nodes it may happen that they are not reachable via AST, but we still need to set
// their type to the requested value
typeCache.forEach((n, types) -> types.forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
n.setType(t);
}));
}
use of de.fraunhofer.aisec.cpg.processing.strategy.Strategy in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testAllAstNodeVisitor.
/**
* Visits all nodes along AST.
*/
@Test
void testAllAstNodeVisitor() {
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
List<Node> nodeList = new ArrayList<>();
recordDeclaration.accept(Strategy::AST_FORWARD, new IVisitor<Node>() {
public void visit(Node r) {
System.out.println(r);
nodeList.add(r);
}
});
assertEquals(35, nodeList.size());
}
Aggregations