Search in sources :

Example 6 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, final 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);
        }
    }
}
Also used : ContextMessage(com.cflint.plugins.Context.ContextMessage) CFLintScanner(com.cflint.plugins.CFLintScanner) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException)

Example 7 with ContextMessage

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

the class PackageCaseChecker method startComponent.

@Override
public void startComponent(final Context context, final BugList bugs) {
    final String key = context.getComponentName().toLowerCase();
    if (!componentRegister.containsKey(key)) {
        componentRegister.put(key, new ArrayList<String>());
    }
    componentRegister.get(key).add(normalize(context.getFilename()));
    // if an expression already referenced this component, check it here:
    boolean matched = false;
    if (expressionCheckRegister.containsKey(key)) {
        List<PackageCaseCheckerEntry> clonedList = new ArrayList<>();
        clonedList.addAll(expressionCheckRegister.get(key));
        for (final PackageCaseCheckerEntry expressionEntry : clonedList) {
            if (checkComponentRegister(expressionEntry.context, expressionEntry.componentPath, expressionEntry.componentName)) {
                matched = true;
                for (ContextMessage message : expressionEntry.context.getMessages()) {
                    cflintRef.reportRule(expressionEntry.context.getElement(), null, expressionEntry.context, this, message);
                }
                expressionEntry.context.getMessages().clear();
            }
        }
    }
    // If component matched, remove the remembered expression.  (Efficiency)
    if (matched) {
        expressionCheckRegister.remove(key);
    }
}
Also used : ContextMessage(com.cflint.plugins.Context.ContextMessage) ArrayList(java.util.ArrayList)

Example 8 with ContextMessage

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

the class CFLint method process.

private void process(final Element elem, final String space, final Context context) throws CFLintScanException {
    if (skipToPosition > elem.getBegin()) {
        return;
    } else {
        skipToPosition = 0;
    }
    try {
        currentElement = elem;
        if (elem.getName().equalsIgnoreCase(CF.CFCOMPONENT)) {
            final Context componentContext = context.subContext(elem);
            componentContext.setInComponent(true);
            // elem.getAttributeValue(CF.DISPLAYNAME)
            componentContext.setComponentName(context.getFilename().replaceAll(".[cC][fF][cC]", "").replaceAll("^.*[/\\\\]", ""));
            componentContext.setContextType(ContextType.COMPONENT);
            handler.push(CF.COMPONENT);
            doStructureStart(elem, componentContext, CFCompDeclStatement.class);
        } else if (elem.getName().equalsIgnoreCase(CF.CFFUNCTION)) {
            final Context functionContext = context.subContext(elem);
            functionContext.setFunctionName(elem.getAttributeValue(CF.NAME));
            functionContext.setContextType(ContextType.FUNCTION);
            handler.push(CF.FUNCTION);
            doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
        } else if (elem.getName().equalsIgnoreCase(CF.CFLOOP) && elem.getAttributeValue(CF.QUERY) != null) {
            // Give a cfloop for query its own context and set the column
            // names as variables if they are available
            final Context loopContext = context.subContext(elem);
            loopContext.setContextType(ContextType.QUERY_LOOP);
            handler.push(CF.CFLOOP);
            final String qryName = elem.getAttributeValue(CF.QUERY);
            handler.addVariables(handler.getQueryColumns(qryName));
            doStructureStart(elem, loopContext, CFFuncDeclStatement.class);
        }
        if (elem.getName().equalsIgnoreCase(CF.CFSET) || elem.getName().equalsIgnoreCase(CF.CFIF) || elem.getName().equalsIgnoreCase(CF.CFELSEIF) || elem.getName().equalsIgnoreCase(CF.CFRETURN)) {
            scanElement(elem, context);
            final Pattern p = Pattern.compile("<\\w+\\s(.*[^/])/?>", Pattern.MULTILINE | Pattern.DOTALL);
            final String expr = elem.getFirstStartTag().toString();
            final Matcher m = p.matcher(expr);
            if (m.matches()) {
                final String cfscript = m.group(1).trim();
                if (!cfscript.isEmpty()) {
                    try {
                        final CFExpression expression = cfmlParser.parseCFMLExpression(cfscript, this);
                        if (expression != null) {
                            process(expression, elem, context);
                        }
                    } catch (final Exception npe) {
                        printException(npe, elem);
                        final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, context.startLine(), elem.getBegin());
                        reportRule(currentElement, null, context, null, cm);
                    }
                }
            }
            processStack(elem.getChildElements(), space + " ", context);
        } else if (elem.getName().equalsIgnoreCase(CF.CFARGUMENT) || elem.getName().equalsIgnoreCase(CF.CFDOCUMENTSECTION)) {
            scanElement(elem, context);
            final String name = elem.getAttributeValue(CF.NAME);
            if (name != null) {
                handler.addArgument(name);
            }
            processStack(elem.getChildElements(), space + " ", context);
        } else if (elem.getName().equalsIgnoreCase(CF.CFSCRIPT)) {
            scanElement(elem, context);
            String cfscript = elem.getContent().toString();
            if (elem.getEndTag() == null) {
                // Hack to fetch the entire cfscript text, if cfscript is a
                // word in the content somewhere, and causes
                // the jericho parser to fail
                EndTag nextTag = elem.getSource().getNextEndTag(elem.getBegin());
                while (nextTag != null && !nextTag.getName().equalsIgnoreCase(elem.getName())) {
                    nextTag = elem.getSource().getNextEndTag(nextTag.getEnd());
                }
                if (nextTag.getName().equalsIgnoreCase(elem.getName())) {
                    cfscript = elem.getSource().subSequence(elem.getStartTag().getEnd(), nextTag.getBegin()).toString();
                    skipToPosition = nextTag.getEnd();
                }
            }
            CFScriptStatement scriptStatement;
            try {
                scriptStatement = cfmlParser.parseScript(cfscript);
            } catch (ParseException e) {
                throw new CFLintScanException(e);
            } catch (IOException e) {
                throw new CFLintScanException(e);
            }
            final Context subcontext = context.subContext(elem, scriptStatement == null ? null : scriptStatement.getTokens());
            process(scriptStatement, subcontext);
            processStack(elem.getChildElements(), space + " ", context);
        } else if (elem.getName().equalsIgnoreCase(CF.CFFUNCTION)) {
            final Context functionContext = context.subContext(elem);
            functionContext.setFunctionName(elem.getAttributeValue(CF.NAME));
            functionContext.setContextType(ContextType.FUNCTION);
            scanElement(elem, functionContext);
            processStack(elem.getChildElements(), space + " ", functionContext);
            // Process any messages added by downstream parsing.
            for (final ContextMessage message : functionContext.getMessages()) {
                reportRule(elem, null, functionContext, message.getSource(), message);
            }
            functionContext.getMessages().clear();
            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 (elem.getName().equalsIgnoreCase(CF.CFCOMPONENT)) {
            final Context componentContext = context.subContext(elem);
            componentContext.setInComponent(true);
            // elem.getAttributeValue(CF.DISPLAYNAME)
            componentContext.setComponentName(context.getFilename().replaceAll(".[cC][fF][cC]", "").replaceAll("^.*[/\\\\]", ""));
            componentContext.setContextType(ContextType.COMPONENT);
            scanElement(elem, componentContext);
            processStack(elem.getChildElements(), space + " ", componentContext);
            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);
                }
            }
            handler.pop();
        } else if (elem.getName().equalsIgnoreCase(CF.CFQUERY)) {
            scanElement(elem, context);
            for (final Entry<String, CFExpression> expression : unpackTagExpressions(elem).entrySet()) {
                if (expression != null) {
                    process(expression.getValue(), elem, context);
                }
            }
            final List<Element> list = elem.getAllElements();
            processStack(list.subList(1, list.size()), space + " ", context);
            // Save any columns from the cfquery
            final String qryName = elem.getAttributeValue(CF.NAME);
            if (qryName != null && qryName.trim().length() > 0) {
                final String qryText = elem.getTextExtractor().toString().toUpperCase();
                final Matcher m = Pattern.compile(".*SELECT\\s(\\w+(\\s*,\\s*\\w+)+)\\s+FROM\\s+.*").matcher(qryText);
                final List<String> cols = new ArrayList<>();
                if (m.matches()) {
                    cols.addAll(Arrays.asList(m.group(1).trim().split("\\s*,\\s*")));
                    handler.addQueryColumnSet(qryName, cols);
                }
            }
        } else if (elem.getName().equalsIgnoreCase(CF.CFQUERYPARAM)) {
            scanElement(elem, context);
            for (final Entry<String, CFExpression> expression : unpackTagExpressions(elem).entrySet()) {
                if (expression != null) {
                    process(expression.getValue(), elem, context);
                }
            }
        } else if (elem.getName().equalsIgnoreCase(CF.CFINCLUDE)) {
            scanElement(elem, context);
            final String path = elem.getAttributeValue(CF.TEMPLATE);
            final File include = new File(new File(context.getFilename()).getParentFile(), path);
            if (strictInclude || include.exists()) {
                if (includeFileStack.contains(include)) {
                    System.err.println("Terminated a recursive call to include file " + include);
                } else {
                    includeFileStack.push(include);
                    process(FileUtil.loadFile(include), context.getFilename());
                    includeFileStack.pop();
                }
            }
        } else if (elem.getName().equalsIgnoreCase(CF.CFLOOP) && elem.getAttributeValue(CF.QUERY) != null) {
            scanElement(elem, context);
            processStack(elem.getChildElements(), space + " ", context);
            handler.pop();
        } else {
            scanElement(elem, context);
            for (final Entry<String, CFExpression> expression : unpackTagExpressions(elem).entrySet()) {
                if (expression != null) {
                    process(expression.getValue(), elem, tagInfo.isAssignmentAttribute(elem, expression.getKey()) ? context.subContextInAssignment() : context);
                }
            }
            processStack(elem.getChildElements(), space + " ", context);
        }
        // Process any messages added by downstream parsing.
        for (final ContextMessage message : context.getMessages()) {
            reportRule(elem, null, context, message.getSource(), message);
        }
        context.getMessages().clear();
    } catch (final NullPointerException npe) {
        printException(npe);
        final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, context.startLine());
        reportRule(currentElement, null, context, null, cm);
    }
}
Also used : Context(com.cflint.plugins.Context) CFFuncDeclStatement(cfml.parsing.cfscript.script.CFFuncDeclStatement) Pattern(java.util.regex.Pattern) EndTag(net.htmlparser.jericho.EndTag) Matcher(java.util.regex.Matcher) CFLintScanException(com.cflint.exception.CFLintScanException) CFLintScanner(com.cflint.plugins.CFLintScanner) IOException(java.io.IOException) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFExpression(cfml.parsing.cfscript.CFExpression) Entry(java.util.Map.Entry) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFScriptStatement(cfml.parsing.cfscript.script.CFScriptStatement) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(cfml.parsing.reporting.ParseException) File(java.io.File) CFLintStructureListener(com.cflint.plugins.CFLintStructureListener)

Example 9 with ContextMessage

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

the class CFLint method process.

public void process(final String src, final String filename) throws CFLintScanException {
    try {
        fireStartedProcessing(filename);
        lineOffsets = null;
        if (src == null || src.trim().length() == 0) {
            final Context context = new Context(filename, null, null, false, handler, configuration);
            reportRule(null, null, context, null, new ContextMessage(AVOID_EMPTY_FILES, null));
        } else {
            lineOffsets = getLineOffsets(src.split("\n"));
            final CFMLSource cfmlSource = new CFMLSource(src.contains("<!---") ? CommentReformatting.wrap(src) : src);
            final ParserTag firstTag = getFirstTagQuietly(cfmlSource);
            final List<Element> elements = new ArrayList<>();
            if (firstTag != null) {
                elements.addAll(cfmlSource.getChildElements());
            }
            if (isComponentOrInterfaceScript(src, elements)) {
                // Check if pure cfscript
                final CFScriptStatement scriptStatement = cfmlParser.parseScript(src);
                final Context context = new Context(filename, null, null, false, handler, scriptStatement == null ? null : scriptStatement.getTokens(), configuration);
                process(scriptStatement, context);
            } else {
                processStack(elements, " ", filename, null);
            }
        }
        fireFinishedProcessing(filename);
    } catch (final Exception e) {
        throw new CFLintScanException(e);
    }
}
Also used : Context(com.cflint.plugins.Context) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFMLSource(cfml.parsing.CFMLSource) CFScriptStatement(cfml.parsing.cfscript.script.CFScriptStatement) CFLintScanException(com.cflint.exception.CFLintScanException) ParserTag(cfml.parsing.ParserTag) Element(net.htmlparser.jericho.Element) ArrayList(java.util.ArrayList) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException)

Example 10 with ContextMessage

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

the class CFLint method syntaxError.

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, int line, int charPositionInLine, final String msg, final org.antlr.v4.runtime.RecognitionException re) {
    String expression = null;
    int offset = charPositionInLine;
    int startLine = 0;
    int startOffset = 0;
    final Context context = new Context(currentFile, currentElement, null, true, null, null);
    if (currentElement != null) {
        startOffset = context.offset();
        if (context.startLine() != 1) {
            startLine = currentElement.getSource().getRow(startOffset) - 1;
        }
    }
    if (offendingSymbol instanceof Token && re != null) {
        // grab the first non-comment previous token, which is actually the cause of the syntax error theoretically
        CommonTokenStream tokenStream = (CommonTokenStream) recognizer.getInputStream();
        Token previousToken = tokenStream.get(re.getOffendingToken().getTokenIndex() - 1);
        if (previousToken != null) {
            while (previousToken.getChannel() == Token.HIDDEN_CHANNEL && tokenStream.get(previousToken.getTokenIndex() - 1) != null) {
                previousToken = tokenStream.get(previousToken.getTokenIndex() - 1);
            }
            line = previousToken.getLine();
            offset = previousToken.getStopIndex();
            expression = previousToken.getText();
        } else {
            expression = re.getOffendingToken().getText();
        }
        if (expression.length() > 50) {
            expression = expression.substring(1, 40) + "...";
        }
    }
    offset += startOffset;
    line += startLine;
    if (recognizer instanceof Parser && ((Parser) recognizer).isExpectedToken(CFSCRIPTParser.SEMICOLON)) {
        final ContextMessage cm = new ContextMessage(MISSING_SEMI, expression, null, line, offset);
        reportRule(currentElement, null, context, null, cm);
    } else {
        final ContextMessage cm = new ContextMessage(PARSE_ERROR, expression, null, line, offset);
        reportRule(currentElement, null, context, null, cm);
    }
}
Also used : Context(com.cflint.plugins.Context) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ContextMessage(com.cflint.plugins.Context.ContextMessage) Token(org.antlr.v4.runtime.Token) CFMLParser(cfml.parsing.CFMLParser) Parser(org.antlr.v4.runtime.Parser) CFSCRIPTParser(cfml.CFSCRIPTParser)

Aggregations

ContextMessage (com.cflint.plugins.Context.ContextMessage)10 ParseException (cfml.parsing.reporting.ParseException)7 CFLintScanException (com.cflint.exception.CFLintScanException)7 IOException (java.io.IOException)7 Context (com.cflint.plugins.Context)6 CFLintScanner (com.cflint.plugins.CFLintScanner)5 ArrayList (java.util.ArrayList)4 CFExpression (cfml.parsing.cfscript.CFExpression)3 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)3 CFLintStructureListener (com.cflint.plugins.CFLintStructureListener)3 CFMLParser (cfml.parsing.CFMLParser)2 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)2 CFFuncDeclStatement (cfml.parsing.cfscript.script.CFFuncDeclStatement)2 CFSCRIPTParser (cfml.CFSCRIPTParser)1 CFMLSource (cfml.parsing.CFMLSource)1 ParserTag (cfml.parsing.ParserTag)1 CFArrayExpression (cfml.parsing.cfscript.CFArrayExpression)1 CFAssignmentExpression (cfml.parsing.cfscript.CFAssignmentExpression)1 CFFullVarExpression (cfml.parsing.cfscript.CFFullVarExpression)1 CFFunctionExpression (cfml.parsing.cfscript.CFFunctionExpression)1