Search in sources :

Example 1 with CFLiteral

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

the class CFLint method process.

private void process(final CFScriptStatement expression, final Context context) {
    if (expression == null) {
        return;
    }
    if (expression != null && expression.getToken() != null) {
        final List<Object> checkItem = Arrays.asList(expression, expression.getToken());
        if (processed.contains(checkItem)) {
            if (!quiet) {
                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);
            registerRuleOverrides(context, (CFExpressionStatement) expression);
            process(((CFExpressionStatement) expression).getExpression(), elem, context);
        } else if (expression instanceof CFPropertyStatement) {
            try {
                final CFPropertyStatement propertyStatement = (CFPropertyStatement) expression;
                CFExpression value = propertyStatement.getPropertyName();
                if (value == null) {
                    for (final Entry<CFIdentifier, CFExpression> entry : propertyStatement.getAttributes().entrySet()) {
                        if (CF.NAME.equalsIgnoreCase(entry.getKey().getName())) {
                            value = entry.getValue();
                        }
                    }
                }
                if (value != null) {
                    final String name = value.Decompile(0);
                    handler.addVariable(name.substring(1, name.length() - 1));
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
            scanExpression(expression, context, elem);
        } else if (expression instanceof CFCompDeclStatement) {
            final CFCompDeclStatement compDeclStatement = (CFCompDeclStatement) expression;
            final Context componentContext = context.subContext(null);
            componentContext.setInComponent(true);
            componentContext.setContextType(ContextType.COMPONENT);
            for (final Entry<CFExpression, CFExpression> entry : compDeclStatement.getAttributes().entrySet()) {
                if (entry.getKey() != null && entry.getKey().Decompile(0).equalsIgnoreCase(CF.NAME)) {
                    componentContext.setComponentName(entry.getValue().Decompile(0));
                }
            }
            // 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 (final CFScriptStatement subscript : compDeclStatement.getBody().decomposeScript()) {
                    if (subscript instanceof CFPropertyStatement) {
                        process(subscript, componentContext);
                    }
                }
                for (final 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);
                    final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, context.startLine());
                    reportRule(currentElement, null, context, null, cm);
                }
            }
        } 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 CFWhileStatement) {
            scanExpression(expression, context, elem);
            process(((CFWhileStatement) expression).getCond(), elem, context);
            process(((CFWhileStatement) expression).getBody(), context);
        } else if (expression instanceof CFForInStatement) {
            scanExpression(expression, context, elem);
            final Context forInitContext = context.subContext(elem);
            forInitContext.setInAssignmentExpression(true);
            process(((CFForInStatement) expression).getVariable(), elem, forInitContext);
            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 (final CFCase _case : cfswitch.getCases()) {
                process(_case, context);
            }
        } else if (expression instanceof CFCase) {
            scanExpression(expression, context, elem);
            final CFCase cfcase = (CFCase) expression;
            for (final 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 (final CFCatchStatement stmt : cftry.getCatchStatements()) {
                handler.push(CF.CFCATCH);
                if (stmt.getVariable() != null && stmt.getVariable().getName() != null) {
                    handler.addVariable(stmt.getVariable().getName());
                }
                process(stmt.getCatchBody(), context);
                handler.pop();
            }
            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(CF.FUNCTION);
            for (final CFFunctionParameter param : function.getFormals()) {
                handler.addArgument(param.getName());
            }
            doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
            scanExpression(expression, functionContext, elem);
            final 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);
                    final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, context.startLine());
                    reportRule(currentElement, null, context, null, cm);
                }
            }
            handler.pop();
        } else if (expression instanceof CFIncludeStatement) {
            scanExpression(expression, context, elem);
            final CFExpression includeExpr = ((CFIncludeStatement) expression).getTemplate();
            if (includeExpr instanceof CFStringExpression) {
                final List<CFExpression> subExpressions = ((CFStringExpression) includeExpr).getSubExpressions();
                if (subExpressions.size() == 1 && subExpressions.get(0) instanceof CFLiteral) {
                    final String path = ((CFLiteral) subExpressions.get(0)).getVal();
                    final File include = new File(new File(context.getFilename()).getParentFile(), path);
                    if (include.exists() || strictInclude) {
                        try {
                            if (includeFileStack.contains(include)) {
                                if (!quiet) {
                                    System.err.println("Terminated a recursive call to include file " + include);
                                }
                            } else {
                                includeFileStack.push(include);
                                process(FileUtil.loadFile(include), context.getFilename());
                                includeFileStack.pop();
                            }
                        } catch (final CFLintScanException ex) {
                            if (!quiet) {
                                System.err.println("Invalid include file " + context.getFilename());
                            }
                            final int line = context.startLine();
                            final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, line);
                            reportRule(currentElement, "Invalid include file " + expression.getClass(), context, null, cm);
                        }
                    }
                } else if (strictInclude) {
                    if (!quiet) {
                        System.err.println("Unable to resolve template value " + context.getFilename());
                    }
                    final int line = context.startLine();
                    final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, line);
                    reportRule(currentElement, "Unable to resolve template value " + expression.getClass(), context, null, cm);
                }
            }
        } else {
            scanExpression(expression, context, elem);
            for (CFScriptStatement childExpression : expression.decomposeScript()) {
                process(childExpression, context);
            }
        }
    } catch (final StackOverflowError soe) {
        if (!quiet) {
            System.err.println("Stack overflow in " + context.getFilename());
        }
        final int line = context.startLine();
        final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, line);
        reportRule(currentElement, "Stack overflow on " + expression.getClass(), context, null, cm);
    }
    // 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 : CFWhileStatement(cfml.parsing.cfscript.script.CFWhileStatement) CFFuncDeclStatement(cfml.parsing.cfscript.script.CFFuncDeclStatement) CFReturnStatement(cfml.parsing.cfscript.script.CFReturnStatement) CFLintScanException(com.cflint.exception.CFLintScanException) 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) Entry(java.util.Map.Entry) CFLiteral(cfml.parsing.cfscript.CFLiteral) List(java.util.List) ArrayList(java.util.ArrayList) CFIncludeStatement(cfml.parsing.cfscript.script.CFIncludeStatement) Context(com.cflint.plugins.Context) CFCompoundStatement(cfml.parsing.cfscript.script.CFCompoundStatement) CFLintScanner(com.cflint.plugins.CFLintScanner) CFCompDeclStatement(cfml.parsing.cfscript.script.CFCompDeclStatement) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFIfStatement(cfml.parsing.cfscript.script.CFIfStatement) CFTryCatchStatement(cfml.parsing.cfscript.script.CFTryCatchStatement) CFForInStatement(cfml.parsing.cfscript.script.CFForInStatement) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFStringExpression(cfml.parsing.cfscript.CFStringExpression) 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) File(java.io.File) CFLintStructureListener(com.cflint.plugins.CFLintStructureListener)

Example 2 with CFLiteral

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

the class LiteralChecker method expression.

// May want to consider resetting literal map on new components but this way
// it detects duplicated literals across files which is useful
@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
    final String repeatThreshold = context.getConfiguration().getParameter(this, "maximum");
    final String maxWarnings = context.getConfiguration().getParameter(this, "maxWarnings");
    final String warningScope = context.getConfiguration().getParameter(this, "warningScope");
    if (repeatThreshold != null) {
        threshold = Integer.parseInt(repeatThreshold);
    }
    if (maxWarnings != null) {
        warningThreshold = Integer.parseInt(maxWarnings);
    }
    if (expression instanceof CFLiteral) {
        final CFLiteral literal = (CFLiteral) expression;
        final String name = literal.Decompile(0).replace("'", "");
        if (isCommon(name)) {
            return;
        }
        final int lineNo = literal.getLine() + context.startLine() - 1;
        final int offset = literal.getOffset() + context.offset();
        if ((warningScope == null || "global".equals(warningScope)) && !context.isInFunction()) {
            literalCount(name, lineNo, offset, globalLiterals, true, context, bugs, expression);
        } else if ("local".equals(warningScope) && context.isInFunction()) {
            literalCount(name, lineNo, offset, functionListerals, false, context, bugs, expression);
        }
    }
}
Also used : CFLiteral(cfml.parsing.cfscript.CFLiteral)

Aggregations

CFLiteral (cfml.parsing.cfscript.CFLiteral)2 CFExpression (cfml.parsing.cfscript.CFExpression)1 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)1 CFStringExpression (cfml.parsing.cfscript.CFStringExpression)1 CFCase (cfml.parsing.cfscript.script.CFCase)1 CFCatchStatement (cfml.parsing.cfscript.script.CFCatchStatement)1 CFCompDeclStatement (cfml.parsing.cfscript.script.CFCompDeclStatement)1 CFCompoundStatement (cfml.parsing.cfscript.script.CFCompoundStatement)1 CFExpressionStatement (cfml.parsing.cfscript.script.CFExpressionStatement)1 CFForInStatement (cfml.parsing.cfscript.script.CFForInStatement)1 CFForStatement (cfml.parsing.cfscript.script.CFForStatement)1 CFFuncDeclStatement (cfml.parsing.cfscript.script.CFFuncDeclStatement)1 CFFunctionParameter (cfml.parsing.cfscript.script.CFFunctionParameter)1 CFIfStatement (cfml.parsing.cfscript.script.CFIfStatement)1 CFIncludeStatement (cfml.parsing.cfscript.script.CFIncludeStatement)1 CFPropertyStatement (cfml.parsing.cfscript.script.CFPropertyStatement)1 CFReturnStatement (cfml.parsing.cfscript.script.CFReturnStatement)1 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)1 CFSwitchStatement (cfml.parsing.cfscript.script.CFSwitchStatement)1 CFTryCatchStatement (cfml.parsing.cfscript.script.CFTryCatchStatement)1