use of com.cflint.plugins.Context in project CFLint by cflint.
the class CFLint method reportRule.
public void reportRule(Element elem, Object currentExpression, final Context context, final CFLintScanner pluginParm, final ContextMessage msg) {
// If the message carries a context, use it for generating error info like line number
if (msg.getOriginalContext() != null && !context.equals(msg.getOriginalContext())) {
reportRule(msg.getOriginalContext().getElement(), currentExpression, msg.getOriginalContext(), pluginParm, msg);
return;
}
final Context pseudoCfmlParent = context.getParent(ContextType.PSEUDO_CFML);
if (pseudoCfmlParent != null && ContextType.PSEUDO_CFML.equals(pseudoCfmlParent.getContextType())) {
elem = pseudoCfmlParent.getElement();
currentExpression = pseudoCfmlParent.getPseudoCfmlExpression();
}
final Object expression = msg.getCfExpression() != null ? msg.getCfExpression() : currentExpression;
// If we are processing includes, do NOT report any errors
if (!includeFileStack.isEmpty()) {
return;
}
final String msgcode = msg.getMessageCode();
final String nameVar = msg.getVariable();
final CFLintScanner plugin = msg.getSource() == null ? pluginParm : msg.getSource();
if (checkForDisabled(elem, msgcode)) {
return;
}
if (configuration == null) {
throw new NullPointerException("Configuration is null");
}
final PluginInfoRule ruleInfo;
if (MISSING_SEMI.equals(msgcode)) {
ruleInfo = new PluginInfoRule();
final PluginMessage msgInfo = new PluginMessage(MISSING_SEMI);
msgInfo.setMessageText("End of statement(;) expected after ${variable}");
msgInfo.setSeverity(Levels.ERROR);
ruleInfo.getMessages().add(msgInfo);
} else if (PLUGIN_ERROR.equals(msgcode)) {
ruleInfo = new PluginInfoRule();
final PluginMessage msgInfo = new PluginMessage(PLUGIN_ERROR);
msgInfo.setMessageText("Error in plugin: ${variable}");
msgInfo.setSeverity(Levels.ERROR);
ruleInfo.getMessages().add(msgInfo);
} else if (AVOID_EMPTY_FILES.equals(msgcode)) {
ruleInfo = new PluginInfoRule();
final PluginMessage msgInfo = new PluginMessage(AVOID_EMPTY_FILES);
msgInfo.setMessageText("CF file is empty: ${file}");
msgInfo.setSeverity(Levels.WARNING);
ruleInfo.getMessages().add(msgInfo);
} else if (PARSE_ERROR.equals(msgcode)) {
ruleInfo = new CFLintPluginInfo.PluginInfoRule();
final CFLintPluginInfo.PluginInfoRule.PluginMessage msgInfo = new CFLintPluginInfo.PluginInfoRule.PluginMessage(PARSE_ERROR);
msgInfo.setMessageText("Unable to parse");
msgInfo.setSeverity(Levels.ERROR);
ruleInfo.getMessages().add(msgInfo);
} else {
if (plugin == null) {
throw new NullPointerException("Plugin not set. Plugin should be using addMessage(messageCode,variable,source) to report messages in parent contexts");
}
ruleInfo = configuration.getRuleForPlugin(plugin);
}
if (ruleInfo == null) {
throw new NullPointerException("Rule not found for " + plugin.getClass().getSimpleName());
}
final PluginMessage msgInfo = ruleInfo.getMessageByCode(msgcode);
if (msgInfo == null) {
throw new NullPointerException("Message definition not found for [" + msgcode + "] in " + plugin.getClass().getSimpleName());
}
final BugInfoBuilder bldr = new BugInfo.BugInfoBuilder().setMessageCode(msgcode).setVariable(nameVar).setFunction(context.getFunctionName()).setFilename(context.getFilename()).setComponent(context.getComponentName());
bldr.setSeverity(msgInfo.getSeverity());
bldr.setMessage(msgInfo.getMessageText());
if (expression instanceof CFStatement) {
bldr.setExpression(((CFStatement) expression).Decompile(0));
} else if (expression instanceof CFScriptStatement) {
bldr.setExpression(((CFScriptStatement) expression).Decompile(0));
} else if (elem != null) {
bldr.setExpression(elem.toString().replaceAll("\r\n", "\n"));
}
// Rebuild the parameter list so that custom configurations are picked up
final List<PluginParameter> parameters = new ArrayList<>();
for (PluginParameter x : ruleInfo.getParameters()) {
parameters.add(new PluginParameter(x.getName(), context.getConfiguration().getParameter(plugin, x.getName())));
}
bldr.setRuleParameters(parameters);
if (configuration.includes(ruleInfo.getMessageByCode(msgcode)) && !configuration.excludes(ruleInfo.getMessageByCode(msgcode))) {
// A bit of a hack to fix the offset issue
// This could be handled better at the source where line and offset are calc'd.
int idxOffSet = 1;
try {
if (lineOffsets != null && msg.getLine() != null && msg.getOffset() != null && msg.getOffset() >= lineOffsets[msg.getLine()]) {
idxOffSet = 0;
}
} catch (ArrayIndexOutOfBoundsException e) {
}
if (expression instanceof CFExpression) {
final BugInfo bugInfo = bldr.build((CFExpression) expression, elem);
final Token token = ((CFExpression) expression).getToken();
if (!suppressed(bugInfo, token, context)) {
bugs.add(bugInfo);
}
// If the context expression is attached, use the context line and column
if (msg.getCfExpression() != null && msg.getCfExpression() != currentExpression) {
if (msg.getLine() != null) {
bugInfo.setLine(msg.getLine());
if (msg.getOffset() != null) {
bugInfo.setOffset(msg.getOffset());
try {
bugInfo.setColumn(msg.getOffset() - lineOffsets[msg.getLine() - idxOffSet]);
} catch (ArrayIndexOutOfBoundsException aie) {
bugInfo.setColumn(0);
}
} else {
bugInfo.setOffset(lineOffsets != null ? lineOffsets[msg.getLine() - idxOffSet] : 0);
bugInfo.setColumn(0);
}
}
}
} else {
final BugInfo bug = bldr.build((CFScriptStatement) expression, elem);
if (msg.getLine() != null) {
bug.setLine(msg.getLine());
if (msg.getOffset() != null) {
bug.setOffset(msg.getOffset());
try {
bug.setColumn(msg.getOffset() - lineOffsets[msg.getLine() - idxOffSet]);
} catch (ArrayIndexOutOfBoundsException aie) {
bug.setColumn(0);
}
} else {
bug.setOffset(lineOffsets != null ? lineOffsets[msg.getLine() - idxOffSet] : 0);
bug.setColumn(0);
}
bug.setLength(msg.getVariable() != null ? msg.getVariable().length() : 0);
}
if (!suppressed(bug, expression == null ? null : ((CFScriptStatement) expression).getToken(), context)) {
bugs.add(bug);
}
}
}
}
use of com.cflint.plugins.Context 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);
}
}
use of com.cflint.plugins.Context in project CFLint by cflint.
the class CFLint method processStack.
public void processStack(final List<Element> elements, final String space, final Context context) throws CFLintScanException {
Element commentElement = null;
for (final Element elem : elements) {
if (elem.getName().equals(CF.COMMENT)) {
commentElement = elem;
} else {
final Context subContext = context.subContext(elem);
if (commentElement != null) {
applyRuleOverrides(subContext, commentElement);
commentElement = null;
}
process(elem, space, subContext);
}
}
}
use of com.cflint.plugins.Context 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);
}
}
use of com.cflint.plugins.Context 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();
}
}
final CFScriptStatement scriptStatement;
try {
scriptStatement = cfmlParser.parseScript(cfscript);
} catch (Exception 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)) {
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();
}
}
} else if (elem.getName().equalsIgnoreCase(CF.CFLOOP) && elem.getAttributeValue(CF.QUERY) != null) {
scanElement(elem, context);
processStack(elem.getChildElements(), space + " ", context);
handler.pop();
} else if (elem.getName().equalsIgnoreCase(CF.CFCATCH)) {
scanElement(elem, context);
handler.push(CF.CFCATCH);
if (elem.getAttributeValue("name") != null) {
handler.addVariable(elem.getAttributeValue("name"));
}
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);
}
}
Aggregations