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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations