Search in sources :

Example 1 with CFVarDeclExpression

use of cfml.parsing.cfscript.CFVarDeclExpression in project CFLint by cflint.

the class CFLint method process.

private void process(final CFExpression expression, final Element elem, Context oldcontext) {
    if (expression != null) {
        final Context context = oldcontext.subContext(elem);
        for (final CFLintScanner plugin : extensions) {
            try {
                plugin.expression(expression, context, bugs);
                for (final ContextMessage message : context.getMessages()) {
                    reportRule(elem, expression, context, plugin, message);
                }
                context.getMessages().clear();
            } catch (final Exception e) {
                printException(e);
                reportRule(elem, expression, context, plugin, PLUGIN_ERROR);
                fireCFLintException(e, PLUGIN_ERROR, context.getFilename(), null, null, null, null);
            }
        }
        // Handle a few expression types in a special fashion.
        if (expression instanceof CFVarDeclExpression) {
            handler.addVariable(((CFVarDeclExpression) expression).getName());
        }
        //CFIdentifier should not decompose
        if (expression instanceof CFIdentifier) {
            final String name = ((CFIdentifier) expression).getName();
            handler.checkVariable(name);
        }
        if (expression instanceof CFAssignmentExpression && !(expression instanceof CFTernaryExpression)) {
            final Context assignmentContext = context.subContext(elem);
            assignmentContext.setInAssignmentExpression(true);
            process(((CFAssignmentExpression) expression).getLeft(), elem, assignmentContext);
            // Right hand side is handled below. Left hand side gets a
            // special context.
            process(((CFAssignmentExpression) expression).getRight(), elem, context);
        //Only process function call expressions
        } else if (expression instanceof CFFullVarExpression) {
            final CFFullVarExpression fullVarExpression = (CFFullVarExpression) expression;
            if (context.isInAssignmentExpression() && new CFScopes().isScoped(fullVarExpression, "local") && fullVarExpression.getExpressions().size() > 1) {
                handler.addVariable(fullVarExpression.getExpressions().get(1).Decompile(0));
            }
            for (final CFExpression expr : fullVarExpression.getExpressions()) {
                if (expr instanceof CFFunctionExpression) {
                    process(expr, elem, context);
                }
                if (expr instanceof CFMember) {
                    process(((CFMember) expr).getExpression(), elem, context);
                }
            }
        } else {
            // Loop into all relevant nested (child) expressions.
            for (CFExpression child : expression.decomposeExpression()) {
                process(child, elem, context);
            }
        }
    }
}
Also used : Context(com.cflint.plugins.Context) CFFunctionExpression(cfml.parsing.cfscript.CFFunctionExpression) CFLintScanner(com.cflint.plugins.CFLintScanner) CFAssignmentExpression(cfml.parsing.cfscript.CFAssignmentExpression) CFIdentifier(cfml.parsing.cfscript.CFIdentifier) RecognitionException(org.antlr.runtime.RecognitionException) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFScopes(com.cflint.plugins.core.CFScopes) CFExpression(cfml.parsing.cfscript.CFExpression) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFMember(cfml.parsing.cfscript.CFMember) CFVarDeclExpression(cfml.parsing.cfscript.CFVarDeclExpression) CFTernaryExpression(cfml.parsing.cfscript.CFTernaryExpression) CFFullVarExpression(cfml.parsing.cfscript.CFFullVarExpression)

Example 2 with CFVarDeclExpression

use of cfml.parsing.cfscript.CFVarDeclExpression in project CFLint by cflint.

the class UnusedLocalVarChecker method expression.

//protected Map<String, Integer> variableLineNo = new HashMap<String, Integer>();
@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
    if (expression instanceof CFFullVarExpression) {
        final CFFullVarExpression fullVarExpression = (CFFullVarExpression) expression;
        final CFExpression variable = fullVarExpression.getExpressions().get(0);
        if (variable instanceof CFIdentifier) {
            final String name = ((CFIdentifier) variable).getName();
            if (!scopes.isCFScoped(name)) {
                localVariables.put(name.toLowerCase(), new VarInfo(name, true));
            } else if (scopes.isLocalScoped(name) && fullVarExpression.getExpressions().size() > 1) {
                final CFExpression variable2 = fullVarExpression.getExpressions().get(1);
                if (variable2 instanceof CFIdentifier) {
                    final String namepart = ((CFIdentifier) variable2).getName();
                    localVariables.put(namepart.toLowerCase(), new VarInfo(namepart, true));
                }
            }
        }
        for (CFExpression subexpr : ((CFFullVarExpression) expression).getExpressions()) {
            if (subexpr instanceof CFMember) {
                CFMember memberExpr = (CFMember) subexpr;
                if (memberExpr.getExpression() != null) {
                    expression(memberExpr.getExpression(), context, bugs);
                }
            }
        }
    } else if (expression instanceof CFVarDeclExpression) {
        final String name = ((CFVarDeclExpression) expression).getName();
        final int lineNo = expression.getLine() + context.startLine() - 1;
        addLocalVariable(name, lineNo);
    } else if (expression instanceof CFIdentifier) {
        final String name = ((CFIdentifier) expression).getName();
        if (name != null) {
            localVariables.put(name.toLowerCase(), new VarInfo(name, true));
        }
    }
}
Also used : CFMember(cfml.parsing.cfscript.CFMember) CFVarDeclExpression(cfml.parsing.cfscript.CFVarDeclExpression) CFIdentifier(cfml.parsing.cfscript.CFIdentifier) CFFullVarExpression(cfml.parsing.cfscript.CFFullVarExpression) CFExpression(cfml.parsing.cfscript.CFExpression)

Example 3 with CFVarDeclExpression

use of cfml.parsing.cfscript.CFVarDeclExpression in project CFLint by cflint.

the class VariableNameChecker method expression.

@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
    if (expression instanceof CFVarDeclExpression) {
        final CFVarDeclExpression cfVarDeclExpression = (CFVarDeclExpression) expression;
        final int lineNo = expression.getLine() + context.startLine() - 1;
        final String varName = cfVarDeclExpression.getName();
        checkNameForBugs(context, varName, varName, context.getFilename(), context.getFunctionName(), lineNo, bugs);
    } else if (expression instanceof CFFullVarExpression) {
        final CFFullVarExpression cfFullVarExpression = (CFFullVarExpression) expression;
        for (final CFExpression subexpression : cfFullVarExpression.getExpressions()) {
            if (subexpression instanceof CFIdentifier) {
                final String varName = ((CFIdentifier) subexpression).getName();
                final int lineNo = ((CFIdentifier) subexpression).getLine() + context.startLine() - 1;
                checkNameForBugs(context, cfFullVarExpression.Decompile(0), varName, context.getFilename(), context.getFunctionName(), lineNo, bugs);
            }
        }
    } else if (expression instanceof CFIdentifier) {
        final String varName = ((CFIdentifier) expression).getName();
        final int lineNo = ((CFIdentifier) expression).getLine() + context.startLine() - 1;
        checkNameForBugs(context, varName, varName, context.getFilename(), context.getFunctionName(), lineNo, bugs);
    }
}
Also used : CFVarDeclExpression(cfml.parsing.cfscript.CFVarDeclExpression) CFIdentifier(cfml.parsing.cfscript.CFIdentifier) CFFullVarExpression(cfml.parsing.cfscript.CFFullVarExpression) CFExpression(cfml.parsing.cfscript.CFExpression)

Aggregations

CFExpression (cfml.parsing.cfscript.CFExpression)3 CFFullVarExpression (cfml.parsing.cfscript.CFFullVarExpression)3 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)3 CFVarDeclExpression (cfml.parsing.cfscript.CFVarDeclExpression)3 CFMember (cfml.parsing.cfscript.CFMember)2 CFAssignmentExpression (cfml.parsing.cfscript.CFAssignmentExpression)1 CFFunctionExpression (cfml.parsing.cfscript.CFFunctionExpression)1 CFTernaryExpression (cfml.parsing.cfscript.CFTernaryExpression)1 ParseException (cfml.parsing.reporting.ParseException)1 CFLintScanner (com.cflint.plugins.CFLintScanner)1 Context (com.cflint.plugins.Context)1 ContextMessage (com.cflint.plugins.Context.ContextMessage)1 CFScopes (com.cflint.plugins.core.CFScopes)1 IOException (java.io.IOException)1 RecognitionException (org.antlr.runtime.RecognitionException)1