use of com.cflint.plugins.CFLintScanner 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);
}
}
}
use of com.cflint.plugins.CFLintScanner in project CFLint by cflint.
the class CFLint method process.
/**
* @param expression
* CF expression
* @param elem
* Jericho HTML element
* @param oldcontext
* The previous context
*/
private void process(final CFExpression expression, final Element elem, final 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);
}
}
// Handle a few expression types in a special fashion.
if (expression instanceof CFVarDeclExpression) {
handler.addVariable(((CFVarDeclExpression) expression).getName());
}
// CFIdentifier should not decompose
if (expression.getClass().equals(CFIdentifier.class)) {
final String name = ((CFIdentifier) expression).getName();
handler.checkVariable(name);
}
if (expression instanceof CFStructElementExpression) {
final Context assignmentContext = context.subContext(elem);
// assignmentContext.setInAssignmentExpression(true);
handler.push(CF.STRUCT);
process(((CFStructElementExpression) expression).getKey(), elem, assignmentContext);
handler.pop();
process(((CFStructElementExpression) expression).getValue(), elem, context);
} else if (expression instanceof CFVarDeclExpression) {
final Context assignmentContext = context.subContext(elem);
assignmentContext.setInAssignmentExpression(true);
process(((CFVarDeclExpression) expression).getVar(), elem, assignmentContext);
// Right hand side is handled below. Left hand side gets a
// special context.
process(((CFVarDeclExpression) expression).getInit(), elem, context);
// Only process function call expressions
} else 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));
}
final Context subContext = context.subContext(context.getElement());
subContext.setInAssignmentExpression(false);
for (final CFExpression expr : fullVarExpression.getExpressions()) {
if (expr instanceof CFFunctionExpression) {
process(expr, elem, subContext);
}
if (expr instanceof CFMember) {
process(((CFMember) expr).getExpression(), elem, subContext);
}
if (expr instanceof CFArrayExpression) {
final CFArrayExpression aryExpr = (CFArrayExpression) expr;
if (!aryExpr.getElements().isEmpty()) {
process(aryExpr.getElements().get(0), elem, subContext);
}
}
}
} else {
// Loop into all relevant nested (child) expressions.
for (final CFExpression child : expression.decomposeExpression()) {
process(child, elem, context.subContextInAssignment(false));
}
}
}
}
use of com.cflint.plugins.CFLintScanner 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)) {
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));
}
}
// 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 (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()) {
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(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)) {
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) {
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) {
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) {
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();
}
use of com.cflint.plugins.CFLintScanner 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);
}
}
}
use of com.cflint.plugins.CFLintScanner in project CFLint by cflint.
the class CFLint method reportRule.
public void reportRule(final Element elem, final Object currentExpression, final Context context, final CFLintScanner pluginParm, final ContextMessage msg) {
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");
}
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"));
}
bldr.setRuleParameters(ruleInfo.getParameters());
if (configuration.includes(ruleInfo.getMessageByCode(msgcode)) && !configuration.excludes(ruleInfo.getMessageByCode(msgcode))) {
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());
bugInfo.setColumn(msg.getOffset() - lineOffsets[msg.getLine() - 1]);
} else {
bugInfo.setOffset(lineOffsets != null ? lineOffsets[msg.getLine() - 1] : 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());
bug.setColumn(msg.getOffset() - lineOffsets[msg.getLine() - 1]);
} else {
bug.setOffset(lineOffsets != null ? lineOffsets[msg.getLine() - 1] : 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);
}
}
}
}
Aggregations