Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #204

Merged
merged 5 commits into from
Oct 25, 2016
Merged

Dev #204

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.cflint</groupId>
<artifactId>CFLint</artifactId>
<version>0.7.3</version>
<version>0.8.0</version>

<name>CFLint</name>
<description>
Expand Down
194 changes: 91 additions & 103 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@

public class CFLint implements IErrorReporter {

private static final String FILE_ERROR = "FILE_ERROR";
private static final String PARSE_ERROR = "PARSE_ERROR";
public static final String PLUGIN_ERROR = "PLUGIN_ERROR:";
CFMLParser cfmlParser = new CFMLParser();
static final String FILE_ERROR = "FILE_ERROR";
static final String PARSE_ERROR = "PARSE_ERROR";
static final String PLUGIN_ERROR = "PLUGIN_ERROR:";

static final String RESOURCE_BUNDLE_NAME = "com.cflint.cflint";

CFMLParser cfmlParser = new CFMLParser();
StackHandler handler = new StackHandler();
boolean inFunction = false;
BugList bugs;
List<CFLintScanner> extensions = new ArrayList<CFLintScanner>();
List<String> allowedExtensions = new ArrayList<String>();
Expand All @@ -86,14 +87,11 @@ public class CFLint implements IErrorReporter {
boolean progressUsesThread = true;

// constants
final String resourceBundleName = "com.cflint.cflint";

private String currentFile;
List<ScanProgressListener> scanProgressListeners = new ArrayList<ScanProgressListener>();
List<CFLintExceptionListener> exceptionListeners = new ArrayList<CFLintExceptionListener>();

ConfigRuntime configuration;
private final Stack<Element> currentElement = new Stack<Element>();

public CFLint(final CFLintConfig configFile) throws IOException {
final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
Expand All @@ -106,7 +104,7 @@ public CFLint(final CFLintConfig configFile) throws IOException {
if (exceptionListeners.isEmpty()) {
addExceptionListener(new DefaultCFLintExceptionListener(bugs));
}
allowedExtensions = AllowedExtensionsLoader.init(resourceBundleName);
allowedExtensions = AllowedExtensionsLoader.init(RESOURCE_BUNDLE_NAME);
cfmlParser.setErrorReporter(this);
}

Expand Down Expand Up @@ -134,7 +132,7 @@ public CFLint(final ConfigRuntime configuration, final CFLintScanner... bugsScan
if (exceptionListeners.isEmpty()) {
addExceptionListener(new DefaultCFLintExceptionListener(bugs));
}
allowedExtensions = AllowedExtensionsLoader.init(resourceBundleName);
allowedExtensions = AllowedExtensionsLoader.init(RESOURCE_BUNDLE_NAME);
cfmlParser.setErrorReporter(this);
}

Expand Down Expand Up @@ -220,8 +218,7 @@ public void processStack(final List<Element> elements, final String space, final

private void process(final Element elem, final String space, final Context context)
throws ParseException, IOException {
currentElement.push(elem);

currentElement=elem;
if (elem.getName().equalsIgnoreCase("cfcomponent")) {
final Context componentContext = context.subContext(elem);
componentContext.setInComponent(true);
Expand All @@ -232,102 +229,96 @@ private void process(final Element elem, final String space, final Context conte
} else if (elem.getName().equalsIgnoreCase("cffunction")) {
final Context functionContext = context.subContext(elem);
functionContext.setFunctionName(elem.getAttributeValue("name"));
inFunction = true;
registerRuleOverrides(functionContext);
handler.push("function");
doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
}

try {
if (elem.getName().equalsIgnoreCase("cfset") || elem.getName().equalsIgnoreCase("cfif")
|| elem.getName().equalsIgnoreCase("cfelseif") || elem.getName().equalsIgnoreCase("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);
try {
final CFExpression expression = cfmlParser.parseCFExpression(cfscript, this);
if (elem.getName().equalsIgnoreCase("cfset") || elem.getName().equalsIgnoreCase("cfif")
|| elem.getName().equalsIgnoreCase("cfelseif") || elem.getName().equalsIgnoreCase("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);
try {
final CFExpression expression = cfmlParser.parseCFExpression(cfscript, this);

if (expression == null) {
throw new NullPointerException("expression is null, parsing error");
}
process(expression, elem, context);
} catch (final Exception npe) {
printException(npe, elem);
if (expression == null) {
throw new NullPointerException("expression is null, parsing error");
}
process(expression, elem, context);
} catch (final Exception npe) {
printException(npe, elem);
}
processStack(elem.getChildElements(), space + " ", context);
}
processStack(elem.getChildElements(), space + " ", context);

} else if (elem.getName().equalsIgnoreCase("cfargument")) {
scanElement(elem, context);
final String name = elem.getAttributeValue("name");
if (name != null) {
handler.addArgument(name);
}
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfscript")) {
scanElement(elem, context);
final String cfscript = elem.getContent().toString();
final CFScriptStatement scriptStatement = cfmlParser.parseScript(cfscript);

Context subcontext = context.subContext(elem);
process(scriptStatement, subcontext);
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cffunction")) {
final Context functionContext = context.subContext(elem);
functionContext.setFunctionName(elem.getAttributeValue("name"));
registerRuleOverrides(functionContext);
scanElement(elem, functionContext);
processStack(elem.getChildElements(), space + " ", functionContext);
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);
} else if (elem.getName().equalsIgnoreCase("cfargument")) {
scanElement(elem, context);
final String name = elem.getAttributeValue("name");
if (name != null) {
handler.addArgument(name);
}
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfscript")) {
scanElement(elem, context);
final String cfscript = elem.getContent().toString();
final CFScriptStatement scriptStatement = cfmlParser.parseScript(cfscript);

Context subcontext = context.subContext(elem);
process(scriptStatement, subcontext);
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cffunction")) {
final Context functionContext = context.subContext(elem);
functionContext.setFunctionName(elem.getAttributeValue("name"));
registerRuleOverrides(functionContext);
scanElement(elem, functionContext);
processStack(elem.getChildElements(), space + " ", functionContext);
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);
}
inFunction = false;
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfcomponent")) {
final Context componentContext = context.subContext(elem);
componentContext.setInComponent(true);
componentContext.setComponentName(elem.getAttributeValue("displayname"));
registerRuleOverrides(componentContext);
scanElement(elem, componentContext);
}
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfcomponent")) {
final Context componentContext = context.subContext(elem);
componentContext.setInComponent(true);
componentContext.setComponentName(elem.getAttributeValue("displayname"));
registerRuleOverrides(componentContext);
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);
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);
}
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfquery")) {
scanElement(elem, context);
final List<Element> list = elem.getAllElements();
processStack(list.subList(1, list.size()), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfqueryparam")) {
scanElement(elem, context);
if (elem.getAttributeValue("value") != null) {
}
} else {
scanElement(elem, context);
processStack(elem.getChildElements(), space + " ", context);
}
} finally {
currentElement.pop();
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfquery")) {
scanElement(elem, context);
final List<Element> list = elem.getAllElements();
processStack(list.subList(1, list.size()), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfqueryparam")) {
scanElement(elem, context);
if (elem.getAttributeValue("value") != null) {
}
} else {
scanElement(elem, context);
processStack(elem.getChildElements(), space + " ", context);
}
}

Expand Down Expand Up @@ -438,7 +429,6 @@ private void process(final CFScriptStatement expression, Context context) {
final Context functionContext = context.subContext(null);
functionContext.setFunctionInfo(function);
registerRuleOverrides(functionContext, function.getToken());
inFunction = true;
handler.push("function");
for (final CFFunctionParameter param : function.getFormals()) {
handler.addArgument(param.getName());
Expand All @@ -459,7 +449,6 @@ private void process(final CFScriptStatement expression, Context context) {
printException(e);
}
}
inFunction = false;
handler.pop();
} else {
scanExpression(expression, context, elem);
Expand Down Expand Up @@ -834,7 +823,6 @@ protected void fireFinishedProcessing(final String srcidentifier) {
for (final ScanProgressListener p : scanProgressListeners) {
p.finishedProcessing(srcidentifier);
}
currentFile = null;
}

protected void fireClose() {
Expand Down Expand Up @@ -870,6 +858,9 @@ public void setProgressUsesThread(final boolean progressUsesThread) {
this.progressUsesThread = progressUsesThread;
}

String currentFile = null;
Element currentElement=null;

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, int line,
int charPositionInLine, final String msg, final org.antlr.v4.runtime.RecognitionException e) {
Expand All @@ -881,13 +872,12 @@ public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendin
expression = expression.substring(1, 40) + "...";
}
}
if (!currentElement.isEmpty()) {
final Element elem = currentElement.peek();
if (currentElement != null) {
if (line == 1) {
line = elem.getSource().getRow(elem.getBegin());
charPositionInLine = charPositionInLine + elem.getSource().getColumn(elem.getBegin());
line = currentElement.getSource().getRow(currentElement.getBegin());
charPositionInLine = charPositionInLine + currentElement.getSource().getColumn(currentElement.getBegin());
} else {
line = elem.getSource().getRow(elem.getBegin()) + line - 1;
line = currentElement.getSource().getRow(currentElement.getBegin()) + line - 1;
}
}
if (recognizer instanceof Parser && ((Parser) recognizer).isExpectedToken(CFSCRIPTParser.SEMICOLON)) {
Expand Down Expand Up @@ -917,12 +907,10 @@ public void reportContextSensitivity(final Parser recognizer, final DFA dfa, fin

@Override
public void reportError(final org.antlr.v4.runtime.RecognitionException re) {
final String file = currentFile == null ? "" : currentFile + "\r\n";
}

@Override
public void reportError(final String[] tokenNames, final org.antlr.v4.runtime.RecognitionException re) {
final String file = currentFile == null ? "" : currentFile + "\r\n";
}

@Override
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/cflint/main/CFLintMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,15 @@ private void execute() throws IOException, TransformerException, JAXBException,
private void mergeConfigFileInFilter(CFLintFilter filter)
{
CFLintConfig cfg = loadConfig(configfile);
if(cfg != null){
for(PluginMessage message : cfg.getIncludes())
{
filter.includeCode(message.getCode());
}
for(PluginMessage message : cfg.getExcludes())
{
filter.excludeCode(message.getCode());
{
filter.includeCode(message.getCode());
}
for(PluginMessage message : cfg.getExcludes())
{
filter.excludeCode(message.getCode());
}
}
}

Expand Down