Search in sources :

Example 16 with Node

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

the class SubgraphWalker method activateTypes.

public static void activateTypes(Node node, ScopeManager scopeManager) {
    AtomicInteger num = new AtomicInteger();
    Map<HasType, Set<Type>> typeCache = TypeManager.getInstance().getTypeCache();
    IterativeGraphWalker walker = new IterativeGraphWalker();
    walker.registerOnNodeVisit(scopeManager::enterScopeIfExists);
    walker.registerOnScopeExit(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();
        }
    });
    walker.iterate(node);
    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 : java.util(java.util) NonNull(org.checkerframework.checker.nullness.qual.NonNull) AnnotationFormatError(java.lang.annotation.AnnotationFormatError) LoggerFactory(org.slf4j.LoggerFactory) Relationship(org.neo4j.ogm.annotation.Relationship) 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) SubGraph(de.fraunhofer.aisec.cpg.graph.SubGraph) BiConsumer(java.util.function.BiConsumer) Node(de.fraunhofer.aisec.cpg.graph.Node) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LanguageFrontend(de.fraunhofer.aisec.cpg.frontends.LanguageFrontend) Logger(org.slf4j.Logger) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Predicate(java.util.function.Predicate) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) HasType(de.fraunhofer.aisec.cpg.graph.HasType) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) ScopeManager(de.fraunhofer.aisec.cpg.passes.scopes.ScopeManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 17 with Node

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

the class Strategy method AST_FORWARD.

/**
 * Traverse AST in forward direction.
 *
 * @param x
 * @return
 */
@NonNull
public static Iterator<Node> AST_FORWARD(@NonNull Node x) {
    // Set for duplicate elimination
    HashSet<Node> children = new HashSet<>();
    Class<?> classType = x.getClass();
    for (Field field : getAllFields(classType)) {
        SubGraph subGraph = field.getAnnotation(SubGraph.class);
        if (subGraph != null && Arrays.asList(subGraph.value()).contains("AST")) {
            try {
                // disable access mechanisms
                field.setAccessible(true);
                Object obj = field.get(x);
                // restore old state
                field.setAccessible(false);
                // skip, if null
                if (obj == null) {
                    continue;
                }
                obj = handlePropertyEdges(field, obj);
                if (obj instanceof Node) {
                    children.add((Node) obj);
                } else if (obj instanceof Collection) {
                    Collection<? extends Node> astChildren = (Collection<? extends Node>) obj;
                    astChildren.removeIf(Objects::isNull);
                    children.addAll(astChildren);
                }
            } catch (IllegalAccessException ex) {
            // Nothing to do here
            }
        }
    }
    return children.iterator();
}
Also used : Field(java.lang.reflect.Field) Node(de.fraunhofer.aisec.cpg.graph.Node) SubGraph(de.fraunhofer.aisec.cpg.graph.SubGraph) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

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