Search in sources :

Example 1 with ContextMessage

use of com.cflint.plugins.Context.ContextMessage in project CFLint by cflint.

the class CFLint method scanElement.

protected void scanElement(final Element elem, final Context context) {
    for (final CFLintScanner plugin : extensions) {
        try {
            plugin.element(elem, context, bugs);
            for (final ContextMessage message : context.getMessages()) {
                reportRule(elem, null, context, plugin, message);
            }
            context.getMessages().clear();
        } catch (final Exception e) {
            printException(e);
            reportRule(elem, null, context, plugin, PLUGIN_ERROR);
            fireCFLintException(e, PLUGIN_ERROR, context.getFilename(), null, null, null, null);
        }
    }
}
Also used : ContextMessage(com.cflint.plugins.Context.ContextMessage) CFLintScanner(com.cflint.plugins.CFLintScanner) RecognitionException(org.antlr.runtime.RecognitionException) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException)

Example 2 with ContextMessage

use of com.cflint.plugins.Context.ContextMessage in project CFLint by cflint.

the class CFLint method process.

private void process(final CFScriptStatement expression, Context context) {
    if (expression == null) {
        return;
    }
    if (expression != null && expression.getToken() != null) {
        List<Object> checkItem = Arrays.asList(expression, expression.getToken());
        if (processed.contains(checkItem)) {
            System.err.println("Attempt to process expression twice aborted.  This may be a parsing bug in " + context.getFilename() + " : " + (expression.getToken() != null ? expression.getToken().getLine() : ""));
            return;
        }
        processed.add(checkItem);
    }
    final Element elem = context.getElement();
    try {
        if (expression instanceof CFCompoundStatement) {
            scanExpression(expression, context, elem);
            for (final CFScriptStatement statement : ((CFCompoundStatement) expression).getStatements()) {
                process(statement, context);
            }
        } else if (expression instanceof CFExpressionStatement) {
            scanExpression(expression, context, elem);
            process(((CFExpressionStatement) expression).getExpression(), elem, context);
        } else if (expression instanceof CFPropertyStatement) {
            try {
                //TODO fix this to use getPropertyName() when it is available and not null.
                Field field = CFPropertyStatement.class.getDeclaredField("propertyName");
                field.setAccessible(true);
                CFExpression value = (CFExpression) field.get(expression);
                if (value == null) {
                    for (Entry<CFIdentifier, CFExpression> entry : ((CFPropertyStatement) expression).getAttributes().entrySet()) {
                        if ("name".equals(entry.getKey().getName())) {
                            value = entry.getValue();
                        }
                    }
                }
                String name = value.Decompile(0);
                handler.addVariable(name.substring(1, name.length() - 1));
            } catch (Exception e) {
                e.printStackTrace();
            }
            scanExpression(expression, context, elem);
        //                for(CFExpression expr: ((CFPropertyStatement) expression).decomposeExpression()){
        //                    process(expr, elem, context);
        //                }
        } else if (expression instanceof CFCompDeclStatement) {
            CFCompDeclStatement compDeclStatement = (CFCompDeclStatement) expression;
            final Context componentContext = context.subContext(null);
            componentContext.setInComponent(true);
            componentContext.setContextType(ContextType.Component);
            for (Entry<CFExpression, CFExpression> entry : compDeclStatement.getAttributes().entrySet()) {
                if (entry.getKey() != null && entry.getKey().Decompile(0).equalsIgnoreCase("name")) {
                    componentContext.setComponentName(entry.getValue().Decompile(0));
                }
            }
            // componentContext.setComponentName(compDeclStatement.get); //
            // TODO
            // Register any overrides from multi-line comments.
            registerRuleOverrides(componentContext, expression.getToken());
            // do startComponent notifications
            doStructureStart(elem, componentContext, expression.getClass());
            scanExpression(compDeclStatement, componentContext, elem);
            // process the component declaration
            if (compDeclStatement.getBody() instanceof CFCompoundStatement) {
                //Process property expressions first
                for (CFScriptStatement subscript : compDeclStatement.getBody().decomposeScript()) {
                    if (subscript instanceof CFPropertyStatement) {
                        process(subscript, componentContext);
                    }
                }
                for (CFScriptStatement subscript : compDeclStatement.getBody().decomposeScript()) {
                    if (!(subscript instanceof CFPropertyStatement)) {
                        process(subscript, componentContext);
                    }
                }
            } else {
                process(compDeclStatement.getBody(), componentContext);
            }
            // do endComponent notifications
            for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
                try {
                    structurePlugin.endComponent(componentContext, bugs);
                    for (final ContextMessage message : componentContext.getMessages()) {
                        reportRule(elem, null, componentContext, (CFLintScanner) structurePlugin, message);
                    }
                    componentContext.getMessages().clear();
                } catch (final Exception e) {
                    printException(e);
                    fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
                }
            }
        } else if (expression instanceof CFForStatement) {
            scanExpression(expression, context, elem);
            process(((CFForStatement) expression).getInit(), elem, context);
            process(((CFForStatement) expression).getCond(), elem, context);
            process(((CFForStatement) expression).getNext(), elem, context);
            process(((CFForStatement) expression).getBody(), context);
        } else if (expression instanceof CFForInStatement) {
            scanExpression(expression, context, elem);
            process(((CFForInStatement) expression).getVariable(), elem, context);
            process(((CFForInStatement) expression).getStructure(), elem, context);
            process(((CFForInStatement) expression).getBody(), context);
        } else if (expression instanceof CFIfStatement) {
            scanExpression(expression, context, elem);
            final CFIfStatement cfif = (CFIfStatement) expression;
            process(cfif.getCond(), elem, context);
            process(cfif.getThenStatement(), context);
            process(cfif.getElseStatement(), context);
        } else if (expression instanceof CFSwitchStatement) {
            scanExpression(expression, context, elem);
            final CFSwitchStatement cfswitch = (CFSwitchStatement) expression;
            process(cfswitch.getVariable(), elem, context);
            for (CFCase _case : cfswitch.getCases()) {
                process(_case, context);
            }
        } else if (expression instanceof CFCase) {
            scanExpression(expression, context, elem);
            final CFCase cfcase = (CFCase) expression;
            for (CFScriptStatement cfstatement : cfcase.getStatements()) {
                process(cfstatement, context);
            }
        } else if (expression instanceof CFTryCatchStatement) {
            scanExpression(expression, context, elem);
            final CFTryCatchStatement cftry = (CFTryCatchStatement) expression;
            process(cftry.getBody(), context);
            for (CFCatchStatement stmt : cftry.getCatchStatements()) {
                process(stmt.getCatchBody(), context);
            }
            process(cftry.getFinallyStatement(), context);
        } else if (expression instanceof CFReturnStatement) {
            scanExpression(expression, context, elem);
            final CFReturnStatement cfreturn = (CFReturnStatement) expression;
            final CFExpression subExpression = cfreturn.getExpression();
            process(subExpression, elem, context);
        } else if (expression instanceof CFFuncDeclStatement) {
            final CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
            final Context functionContext = context.subContext(null);
            functionContext.setContextType(ContextType.Function);
            functionContext.setFunctionInfo(function);
            registerRuleOverrides(functionContext, function.getToken());
            handler.push("function");
            for (final CFFunctionParameter param : function.getFormals()) {
                handler.addArgument(param.getName());
            }
            doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
            scanExpression(expression, functionContext, elem);
            Context functionBodyContext = functionContext.subContext(null);
            process(function.getBody(), functionBodyContext);
            for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
                try {
                    structurePlugin.endFunction(functionContext, bugs);
                    for (final ContextMessage message : functionContext.getMessages()) {
                        reportRule(elem, null, functionContext, (CFLintScanner) structurePlugin, message);
                    }
                    functionContext.getMessages().clear();
                } catch (final Exception e) {
                    printException(e);
                    fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
                }
            }
            handler.pop();
        } else {
            scanExpression(expression, context, elem);
        }
    } catch (final StackOverflowError soe) {
        System.err.println("Stack overflow in " + context.getFilename());
        final int line = context.startLine();
        fireCFLintException(soe, PARSE_ERROR, context.getFilename(), line, 1, "", "Stack overflow on " + expression.getClass());
    }
    // Process any messages added by downstream parsing.
    for (final ContextMessage message : context.getMessages()) {
        reportRule(elem, null, context, message.getSource(), message);
    }
    context.getMessages().clear();
}
Also used : CFFuncDeclStatement(cfml.parsing.cfscript.script.CFFuncDeclStatement) CFReturnStatement(cfml.parsing.cfscript.script.CFReturnStatement) Element(net.htmlparser.jericho.Element) CFFunctionParameter(cfml.parsing.cfscript.script.CFFunctionParameter) CFPropertyStatement(cfml.parsing.cfscript.script.CFPropertyStatement) CFIdentifier(cfml.parsing.cfscript.CFIdentifier) CFExpression(cfml.parsing.cfscript.CFExpression) CFForStatement(cfml.parsing.cfscript.script.CFForStatement) Field(java.lang.reflect.Field) Entry(java.util.Map.Entry) Context(com.cflint.plugins.Context) CFCompoundStatement(cfml.parsing.cfscript.script.CFCompoundStatement) CFLintScanner(com.cflint.plugins.CFLintScanner) CFCompDeclStatement(cfml.parsing.cfscript.script.CFCompDeclStatement) RecognitionException(org.antlr.runtime.RecognitionException) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFIfStatement(cfml.parsing.cfscript.script.CFIfStatement) CFTryCatchStatement(cfml.parsing.cfscript.script.CFTryCatchStatement) CFForInStatement(cfml.parsing.cfscript.script.CFForInStatement) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFScriptStatement(cfml.parsing.cfscript.script.CFScriptStatement) CFSwitchStatement(cfml.parsing.cfscript.script.CFSwitchStatement) CFCatchStatement(cfml.parsing.cfscript.script.CFCatchStatement) CFCase(cfml.parsing.cfscript.script.CFCase) CFExpressionStatement(cfml.parsing.cfscript.script.CFExpressionStatement) CFLintStructureListener(com.cflint.plugins.CFLintStructureListener)

Example 3 with ContextMessage

use of com.cflint.plugins.Context.ContextMessage in project CFLint by cflint.

the class CFLint method reportRule.

protected void reportRule(final Element elem, final Object expression, final Context context, final CFLintScanner plugin, final String msg) {
    final String[] exceptionmsg = (msg != null ? msg : "").split(":");
    final String msgcode = exceptionmsg[0].trim();
    final String nameVar = exceptionmsg.length > 1 ? exceptionmsg[1].trim() : null;
    reportRule(elem, expression, context, plugin, new ContextMessage(msgcode, nameVar));
}
Also used : ContextMessage(com.cflint.plugins.Context.ContextMessage)

Example 4 with ContextMessage

use of com.cflint.plugins.Context.ContextMessage 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 5 with ContextMessage

use of com.cflint.plugins.Context.ContextMessage in project CFLint by cflint.

the class CFLint method scanExpression.

protected void scanExpression(final CFScriptStatement expression, Context context, final Element elem) {
    if (expression == null)
        return;
    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);
        }
    }
}
Also used : ContextMessage(com.cflint.plugins.Context.ContextMessage) CFLintScanner(com.cflint.plugins.CFLintScanner) RecognitionException(org.antlr.runtime.RecognitionException) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException)

Aggregations

ContextMessage (com.cflint.plugins.Context.ContextMessage)6 ParseException (cfml.parsing.reporting.ParseException)5 CFLintScanner (com.cflint.plugins.CFLintScanner)5 IOException (java.io.IOException)5 RecognitionException (org.antlr.runtime.RecognitionException)5 CFExpression (cfml.parsing.cfscript.CFExpression)3 Context (com.cflint.plugins.Context)3 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)2 CFFuncDeclStatement (cfml.parsing.cfscript.script.CFFuncDeclStatement)2 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)2 CFLintStructureListener (com.cflint.plugins.CFLintStructureListener)2 CFAssignmentExpression (cfml.parsing.cfscript.CFAssignmentExpression)1 CFFullVarExpression (cfml.parsing.cfscript.CFFullVarExpression)1 CFFunctionExpression (cfml.parsing.cfscript.CFFunctionExpression)1 CFMember (cfml.parsing.cfscript.CFMember)1 CFTernaryExpression (cfml.parsing.cfscript.CFTernaryExpression)1 CFVarDeclExpression (cfml.parsing.cfscript.CFVarDeclExpression)1 CFCase (cfml.parsing.cfscript.script.CFCase)1 CFCatchStatement (cfml.parsing.cfscript.script.CFCatchStatement)1 CFCompDeclStatement (cfml.parsing.cfscript.script.CFCompDeclStatement)1