Search in sources :

Example 1 with MemberCallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression in project cpg by Fraunhofer-AISEC.

the class VariableUsageResolver method resolveLocalVarUsage.

protected void resolveLocalVarUsage(RecordDeclaration currentClass, Node parent, Node current) {
    if (lang == null) {
        Util.errorWithFileLocation(current, log, "Could not resolve local variable usage: language frontend is null");
        return;
    }
    if (current instanceof DeclaredReferenceExpression && !(current instanceof MemberExpression)) {
        DeclaredReferenceExpression ref = (DeclaredReferenceExpression) current;
        if (parent instanceof MemberCallExpression && current == ((MemberCallExpression) parent).getMember() && !(ref.getType() instanceof FunctionPointerType)) {
            // function pointer call
            return;
        }
        // only consider resolving, if the language frontend did not specify a resolution
        Optional<? extends Declaration> refersTo = ref.getRefersTo() == null ? Optional.ofNullable(lang.getScopeManager().resolveReference(ref)) : Optional.of(ref.getRefersTo());
        Type recordDeclType = null;
        if (currentClass != null) {
            recordDeclType = TypeParser.createFrom(currentClass.getName(), true);
        }
        if (ref.getType() instanceof FunctionPointerType && refersTo.isEmpty()) {
            refersTo = resolveFunctionPtr(recordDeclType, ref);
        }
        // only add new nodes for non-static unknown
        if (refersTo.isEmpty() && !(((DeclaredReferenceExpression) current).isStaticAccess()) && recordDeclType != null && recordMap.containsKey(recordDeclType)) {
            // Maybe we are referring to a field instead of a local var
            if (current.getName().contains(lang.getNamespaceDelimiter())) {
                List<String> path = Arrays.asList(current.getName().split(Pattern.quote(lang.getNamespaceDelimiter())));
                recordDeclType = TypeParser.createFrom(String.join(lang.getNamespaceDelimiter(), path.subList(0, path.size() - 1)), true);
            }
            ValueDeclaration field = resolveMember(recordDeclType, (DeclaredReferenceExpression) current);
            if (field != null) {
                refersTo = Optional.of(field);
            }
        }
        // just enables CXX static fields
        if (refersTo.isEmpty() && current.getName().contains(lang.getNamespaceDelimiter())) {
            var path = Arrays.asList(current.getName().split(Pattern.quote(lang.getNamespaceDelimiter())));
            recordDeclType = TypeParser.createFrom(String.join(lang.getNamespaceDelimiter(), path.subList(0, path.size() - 1)), true);
            var field = resolveMember(recordDeclType, (DeclaredReferenceExpression) current);
            if (field != null) {
                refersTo = Optional.of(field);
            }
        }
        if (refersTo.isPresent()) {
            ref.setRefersTo(refersTo.get());
        } else {
            warnWithFileLocation(current, log, "Did not find a declaration for {}", ref.getName());
        }
    }
}
Also used : MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) MemberCallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression)

Aggregations

DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)1 MemberCallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression)1 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)1