Search in sources :

Example 1 with Strategy

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with Strategy

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with Strategy

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);
    }));
}
Also used : LanguageFrontend(de.fraunhofer.aisec.cpg.frontends.LanguageFrontend) java.util(java.util) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Logger(org.slf4j.Logger) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) AnnotationFormatError(java.lang.annotation.AnnotationFormatError) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) Relationship(org.neo4j.ogm.annotation.Relationship) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) IVisitor(de.fraunhofer.aisec.cpg.processing.IVisitor) de.fraunhofer.aisec.cpg.graph.declarations(de.fraunhofer.aisec.cpg.graph.declarations) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) NotNull(org.jetbrains.annotations.NotNull) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Nullable(org.checkerframework.checker.nullness.qual.Nullable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy)

Example 4 with Strategy

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

Strategy (de.fraunhofer.aisec.cpg.processing.strategy.Strategy)4 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)3 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)3 Test (org.junit.jupiter.api.Test)3 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)2 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)2 LanguageFrontend (de.fraunhofer.aisec.cpg.frontends.LanguageFrontend)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 de.fraunhofer.aisec.cpg.graph.declarations (de.fraunhofer.aisec.cpg.graph.declarations)1 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)1 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)1 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)1 Type (de.fraunhofer.aisec.cpg.graph.types.Type)1 IVisitor (de.fraunhofer.aisec.cpg.processing.IVisitor)1 AnnotationFormatError (java.lang.annotation.AnnotationFormatError)1 Field (java.lang.reflect.Field)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1