Search in sources :

Example 21 with HasType

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

the class ArraySubscriptionExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    setType(getSubscriptType(src.getPropagationType()), root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : HasType(de.fraunhofer.aisec.cpg.graph.HasType) Type(de.fraunhofer.aisec.cpg.graph.types.Type)

Example 22 with HasType

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

the class BinaryOperator method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    if (this.operatorCode.equals("=")) {
        setType(src.getPropagationType(), root);
    } else {
        if (this.lhs != null && "java.lang.String".equals(this.lhs.getType().toString()) || this.rhs != null && "java.lang.String".equals(this.rhs.getType().toString())) {
            getPossibleSubTypes().clear();
            setType(TypeParser.createFrom("java.lang.String", true), root);
        }
    }
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 23 with HasType

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

the class ConditionalExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    List<Type> types = new ArrayList<>();
    if (thenExpr != null && thenExpr.getPropagationType() != null) {
        types.add(thenExpr.getPropagationType());
    }
    if (elseExpr != null && elseExpr.getPropagationType() != null) {
        types.add(elseExpr.getPropagationType());
    }
    Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
    subTypes.remove(oldType);
    subTypes.addAll(types);
    Type alternative = !types.isEmpty() ? types.get(0) : UnknownType.getUnknownType();
    setType(TypeManager.getInstance().getCommonType(types).orElse(alternative), root);
    setPossibleSubTypes(subTypes, root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : HasType(de.fraunhofer.aisec.cpg.graph.HasType) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type)

Example 24 with HasType

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

the class FieldDeclaration method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
        return;
    }
    Type previous = this.type;
    Type newType;
    if (src == initializer && initializer instanceof InitializerListExpression) {
        // can be ignored once we have a type
        if (isArray) {
            newType = src.getType();
        } else if (!TypeManager.getInstance().isUnknown(this.type)) {
            return;
        } else {
            newType = src.getType().dereference();
        }
    } else {
        newType = src.getPropagationType();
    }
    setType(newType, root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) InitializerListExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)

Example 25 with HasType

use of de.fraunhofer.aisec.cpg.graph.HasType 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)

Aggregations

HasType (de.fraunhofer.aisec.cpg.graph.HasType)26 Type (de.fraunhofer.aisec.cpg.graph.types.Type)23 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)7 Node (de.fraunhofer.aisec.cpg.graph.Node)5 TypeManager (de.fraunhofer.aisec.cpg.graph.TypeManager)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)4 InitializerListExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)4 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)4 TypeListener (de.fraunhofer.aisec.cpg.graph.HasType.TypeListener)3 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)3 FunctionPointerType (de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)3 ReferenceType (de.fraunhofer.aisec.cpg.graph.types.ReferenceType)3 Predicate (java.util.function.Predicate)3 Relationship (org.neo4j.ogm.annotation.Relationship)3 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)2 PropertyEdge.unwrap (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap)2 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)2