use of com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage in project CFLint by cflint.
the class CFLintCLI method main.
public static void main(final String[] args) throws Exception {
final Options commandOptions = new Options();
final Options helpOptions = new Options();
// documented
Option optionMARKDOWN = new Option(Settings.MARKDOWN, false, "generate MarkDown of all supported rules");
Option optionRULES = new Option(Settings.RULES, false, "list of all supported rules");
Option optionCONFIG = new Option(Settings.CONFIG, false, "list of rules in config file");
Option optionINCLUDE_RULE = new Option(Settings.INCLUDE_RULE, true, "specify rules to include");
Option optionEXCLUDE_RULE = new Option(Settings.EXCLUDE_RULE, true, "specify rules to exclude");
Option optionFOLDER = new Option(Settings.FOLDER, true, "folder(s) to scan");
Option optionFILE = new Option(Settings.FILE, true, "file(s) to scan");
Option optionFILTER_FILE = new Option(Settings.FILTER_FILE, true, "filter file");
Option optionV = new Option(Settings.V, false, "verbose output during linting");
Option optionVERSION = new Option(Settings.VERSION, false, "show the version number");
Option optionVERBOSE = new Option(Settings.VERBOSE, false, "verbose output during linting");
Option optionSTRICT_INCLUDE = new Option(Settings.STRICT_INCLUDE, false, "Check every include and try to parse it");
Option optionLOGERROR = new Option(Settings.LOGERROR, false, "log parsing errors as bugs");
Option optionE = new Option(Settings.E, false, "log parsing errors as bugs");
Option optionQUIET = new Option(Settings.QUIET, false, "quiet mode surpresses most linting error and commentary output");
Option optionQ = new Option(Settings.Q, false, "quiet mode surpresses most linting error and commentary output");
Option optionDEBUG = new Option(Settings.DEBUG, false, "debug-level output during linting");
Option optionD = new Option(Settings.D, false, "debug-level output during linting");
Option optionHELP = new Option(Settings.HELP, false, DISPLAY_THIS_HELP);
Option optionQUESTION_MARK = new Option(Settings.QUESTION_MARK, false, DISPLAY_THIS_HELP);
Option optionH = new Option(Settings.H, false, DISPLAY_THIS_HELP);
Option optionXML = new Option(Settings.XML, false, "output in xml format");
Option optionXMLFILE = new Option(Settings.XMLFILE, true, "specify the output xml file (default: cflint-results.xml)");
Option optionXMLSTYLE = new Option(Settings.XMLSTYLE, true, "cflint,findbugs");
Option optionHTML = new Option(Settings.HTML, false, "output in html format (default)");
Option optionHTMLFILE = new Option(Settings.HTMLFILE, true, "specify the output html file (default: cflint-results.html)");
// fancy,fancy-hist,summary
Option optionHTMLSTYLE = new Option(Settings.HTMLSTYLE, true, "default,plain");
Option optionJSON = new Option(Settings.JSON, false, "output in json format");
Option optionJSONFILE = new Option(Settings.JSONFILE, true, "specify the output json file (default: cflint-results.json)");
Option optionTEXT = new Option(Settings.TEXT, false, "output in plain text");
Option optionTEXTFILE = new Option(Settings.TEXTFILE, true, "specify the output text file (default: cflint-results.txt)");
Option optionEXTENSIONS = new Option(Settings.EXTENSIONS, true, "specify the extensions of the CF source files (default: .cfm,.cfc)");
Option optionSTDIN = new Option(Settings.STDIN, true, "use stdin for file input (default: source.cfc)");
Option optionSTDOUT = new Option(Settings.STDOUT, false, "output to stdout only");
Option optionLIST_RULE_GROUPS = new Option(Settings.LIST_RULE_GROUPS, false, "list rule groups");
Option optionRULE_GROUPS = new Option(Settings.RULE_GROUPS, true, "rule groups");
// undocumented
Option optionCONFIGFILE = new Option(Settings.CONFIGFILE, true, "specify the location of the config file");
// supported options
commandOptions.addOption(optionMARKDOWN).addOption(optionRULES).addOption(optionCONFIG).addOption(optionINCLUDE_RULE).addOption(optionEXCLUDE_RULE).addOption(optionFOLDER).addOption(optionFILE).addOption(optionFILTER_FILE).addOption(optionV).addOption(optionVERSION).addOption(optionVERBOSE).addOption(optionSTRICT_INCLUDE).addOption(optionLOGERROR).addOption(optionE).addOption(optionQUIET).addOption(optionQ).addOption(optionHELP).addOption(optionQUESTION_MARK).addOption(optionH).addOption(optionXML).addOption(optionXMLFILE).addOption(optionXMLSTYLE).addOption(optionHTML).addOption(optionHTMLFILE).addOption(optionHTMLSTYLE).addOption(optionJSON).addOption(optionJSONFILE).addOption(optionTEXT).addOption(optionTEXTFILE).addOption(optionEXTENSIONS).addOption(optionSTDIN).addOption(optionSTDOUT).addOption(optionLIST_RULE_GROUPS).addOption(optionRULE_GROUPS).addOption(optionCONFIGFILE).addOption(optionDEBUG).addOption(optionD);
// documented options for HelpFormatter
helpOptions.addOption(optionMARKDOWN).addOption(optionRULES).addOption(optionCONFIG).addOption(optionINCLUDE_RULE).addOption(optionEXCLUDE_RULE).addOption(optionFOLDER).addOption(optionFILE).addOption(optionFILTER_FILE).addOption(optionV).addOption(optionVERSION).addOption(optionVERBOSE).addOption(optionSTRICT_INCLUDE).addOption(optionLOGERROR).addOption(optionE).addOption(optionQUIET).addOption(optionQ).addOption(optionHELP).addOption(optionQUESTION_MARK).addOption(optionH).addOption(optionXML).addOption(optionXMLFILE).addOption(optionXMLSTYLE).addOption(optionHTML).addOption(optionHTMLFILE).addOption(optionHTMLSTYLE).addOption(optionJSON).addOption(optionJSONFILE).addOption(optionTEXT).addOption(optionTEXTFILE).addOption(optionEXTENSIONS).addOption(optionSTDIN).addOption(optionSTDOUT).addOption(optionLIST_RULE_GROUPS).addOption(optionRULE_GROUPS).addOption(optionDEBUG).addOption(optionD);
final CommandLineParser parser = new GnuParser();
final CommandLine cmd = parser.parse(commandOptions, args);
final CFLintCLI main = new CFLintCLI();
if (cmd.hasOption(Settings.H) || cmd.hasOption(Settings.HELP) || cmd.hasOption(Settings.QUESTION_MARK)) {
final HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(CFLINT_USAGE, helpOptions);
return;
}
if (cmd.hasOption(Settings.VERSION)) {
System.out.println("CFLint " + Version.getVersion());
System.out.println("CFParser " + cfml.parsing.Version.getVersion());
return;
}
main.strictInclude = cmd.hasOption(Settings.STRICT_INCLUDE);
if (cmd.hasOption(Settings.RULES) || cmd.hasOption(Settings.CONFIG)) {
final ConfigBuilder configBuilder = new ConfigBuilder(cmd.hasOption(Settings.RULES) ? ConfigUtils.loadDefaultPluginInfo() : new CFLintPluginInfo());
if (cmd.hasOption(Settings.CONFIGFILE)) {
final String configfile = cmd.getOptionValue(Settings.CONFIGFILE);
try {
configBuilder.addCustomConfig(configfile);
} catch (final Exception e) {
System.err.println("Unable to load config file " + configfile + ". " + e.getMessage());
}
}
final Map<String, String> descriptions = ConfigUtils.loadDescriptions();
System.out.println("Supported rules:");
for (final PluginInfoRule rule : configBuilder.build().getRules()) {
System.out.println(" " + rule.getName());
for (final PluginMessage message : rule.getMessages()) {
System.out.println(" " + message.getCode() + " - " + descriptions.get(message.getCode()));
}
}
return;
}
final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
final ConfigBuilder configBuilder = new ConfigBuilder(pluginInfo);
if (cmd.hasOption(Settings.MARKDOWN)) {
final FileWriter out = new FileWriter("RULES.MD");
CFLintDoc.generateRuleMarkDown(pluginInfo, new PrintWriter(out));
System.out.println("Rules written to RULES.MD");
out.close();
return;
}
if (cmd.hasOption(Settings.CONFIGFILE)) {
final String configfile = cmd.getOptionValue(Settings.CONFIGFILE);
try {
configBuilder.addCustomConfig(configfile);
} catch (final Exception e) {
System.err.println("Unable to load config file " + configfile + ". " + e.getMessage());
}
}
if (cmd.hasOption(Settings.ENVIRONMENT)) {
main.environmentName = cmd.getOptionValue(Settings.ENVIRONMENT);
}
if (cmd.hasOption(Settings.INCLUDE_RULE)) {
configBuilder.include(Arrays.asList(cmd.getOptionValue(Settings.INCLUDE_RULE).split(",")));
}
if (cmd.hasOption(Settings.EXCLUDE_RULE)) {
configBuilder.exclude(Arrays.asList(cmd.getOptionValue(Settings.EXCLUDE_RULE).split(",")));
}
if (cmd.hasOption(Settings.LIST_RULE_GROUPS)) {
CFLintDoc.generateRuleGroup(pluginInfo, new PrintWriter(System.out));
return;
}
// groups are specified
if (cmd.hasOption(Settings.RULE_GROUPS)) {
final String rulegroups = cmd.getOptionValue(Settings.RULE_GROUPS);
configBuilder.ruleGroups(rulegroups);
}
main.quiet = (cmd.hasOption(Settings.Q) || cmd.hasOption(Settings.QUIET));
main.verbose = (cmd.hasOption(Settings.V) || cmd.hasOption(Settings.VERBOSE));
if (main.verbose) {
System.out.println("Verbose is enabled");
}
main.debug = (cmd.hasOption(Settings.D) || cmd.hasOption(Settings.DEBUG));
if (main.debug) {
System.out.println("Debug is enabled");
// Setting verbose = true and quiet = false in debug mode, regardless of settings actually being passed in.
main.verbose = true;
main.quiet = false;
}
main.logerror = (cmd.hasOption(Settings.E) || cmd.hasOption(Settings.LOGERROR));
main.xmlOutput = cmd.hasOption(Settings.XML) || cmd.hasOption(Settings.XMLSTYLE) || cmd.hasOption(Settings.XMLFILE);
main.textOutput = cmd.hasOption(Settings.TEXT) || cmd.hasOption(Settings.TEXTFILE);
main.jsonOutput = cmd.hasOption(Settings.JSON) || cmd.hasOption(Settings.JSONFILE);
// If an output is specified, htmlOutput is not defaulted to true.
if (main.xmlOutput || main.textOutput || main.jsonOutput) {
main.htmlOutput = cmd.hasOption(Settings.HTML) || cmd.hasOption(Settings.HTMLSTYLE) || cmd.hasOption(Settings.HTMLFILE);
}
if (main.verbose) {
System.out.println("XML Output " + main.xmlOutput);
System.out.println("Text Output " + main.textOutput);
System.out.println("JSON Output " + main.jsonOutput);
System.out.println("HTML Output " + main.htmlOutput);
}
if (cmd.hasOption(Settings.FOLDER)) {
main.folder.addAll(Arrays.asList(cmd.getOptionValue(Settings.FOLDER).split(",")));
}
if (cmd.hasOption(Settings.FILE)) {
main.folder.addAll(Arrays.asList(cmd.getOptionValue(Settings.FILE).split(",")));
}
if (cmd.hasOption(Settings.HTMLSTYLE)) {
main.htmlStyle = cmd.getOptionValue(Settings.HTMLSTYLE);
if (!main.htmlStyle.endsWith(".xsl") && !main.htmlStyle.endsWith(".xslt")) {
main.htmlStyle = main.htmlStyle + ".xsl";
}
}
if (cmd.hasOption(Settings.XMLSTYLE)) {
main.xmlstyle = cmd.getOptionValue(Settings.XMLSTYLE);
}
if (cmd.hasOption(Settings.FILTER_FILE)) {
main.filterFile = cmd.getOptionValue(Settings.FILTER_FILE);
}
if (cmd.hasOption(Settings.XMLFILE)) {
main.xmlOutFile = cmd.getOptionValue(Settings.XMLFILE);
}
if (cmd.hasOption(Settings.JSONFILE)) {
main.jsonOutFile = cmd.getOptionValue(Settings.JSONFILE);
}
if (cmd.hasOption(Settings.HTMLFILE)) {
main.htmlOutFile = cmd.getOptionValue(Settings.HTMLFILE);
}
if (cmd.hasOption(Settings.TEXTFILE)) {
main.textOutFile = cmd.getOptionValue(Settings.TEXTFILE);
}
if (cmd.hasOption(Settings.JSONFILE)) {
main.jsonOutFile = cmd.getOptionValue(Settings.JSONFILE);
}
if (cmd.hasOption(Settings.EXTENSIONS)) {
main.extensions = cmd.getOptionValue(Settings.EXTENSIONS);
}
main.stdIn = cmd.hasOption(Settings.STDIN);
if (main.stdIn) {
final String stdInOptionValue = cmd.getOptionValue(Settings.STDIN);
if (stdInOptionValue != null) {
main.stdInFile = stdInOptionValue;
}
}
main.stdOut = cmd.hasOption(Settings.STDOUT);
if (main.isValid()) {
main.execute(configBuilder.build());
} else {
final HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(CFLINT_USAGE, helpOptions);
}
}
use of com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage 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.config.CFLintPluginInfo.PluginInfoRule.PluginMessage in project CFLint by cflint.
the class ConfigBuilder method include.
/**
* Include these rule codes
*
* @param codes list of rule codes to include
* @return this ConfigBuilder
*/
public ConfigBuilder include(final List<String> codes) {
if (cmdLineConfig == null) {
cmdLineConfig = new CFLintConfig();
}
codes.forEach(code -> {
cmdLineConfig.addInclude(new PluginMessage(code));
cmdLineConfig.setInheritParent(false);
});
return this;
}
use of com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage in project CFLint by cflint.
the class CFLintDoc method generateRuleMarkDown.
public static void generateRuleMarkDown(final CFLintPluginInfo pluginInfo, final PrintWriter print) {
final Map<String, String> descriptions = ConfigUtils.loadDescriptions();
final List<String> diminishParms = Arrays.asList("UnusedLocalVarChecker", "CFXTagChecker", "FunctionXChecker");
print.println("List of built-in rules and rule groups");
print.println("======================================");
print.println("## Rule Parameters ");
for (PluginInfoRule ruleInfo : pluginInfo.getRules()) {
// Do not highlight specific parameters.
if (!diminishParms.contains(ruleInfo.getClassName())) {
for (PluginParameter p : ruleInfo.getParameters()) {
print.println("<br>" + ruleInfo.getName() + "." + p.getName() + " = *" + p.getValue() + "*");
}
}
}
print.println("## Built-in rules");
for (PluginInfoRule ruleInfo : pluginInfo.getRules()) {
print.println("* " + ruleInfo.getName());
final String className = ruleInfo.getClassName() == null ? ruleInfo.getName() : ruleInfo.getClassName();
final String fullClassName = className.contains(".") ? className : "com.cflint.plugins.core." + className;
// print.println("**Class:** "+fullClassName);
if (!ruleInfo.getParameters().isEmpty()) {
print.println(" * Parameters");
for (PluginParameter p : ruleInfo.getParameters()) {
print.println(" * " + p.getName() + " = *" + p.getValue() + "*");
}
}
int counter = 1;
for (PluginMessage msg : ruleInfo.getMessages()) {
final String desc = descriptions.get(msg.getCode()) != null ? descriptions.get(msg.getCode()).replace(">", ">").replace("<", "<") : "";
print.println(" * " + msg.getCode() + " - " + desc + " *" + msg.getSeverity() + "*");
print.println(" * " + cleanUpMessage(msg, ruleInfo));
}
}
print.println("## Rule Groups");
for (final RuleGroup ruleGroup : pluginInfo.getRuleGroups()) {
print.println("### " + ruleGroup.getName());
for (final PluginMessage msg : ruleGroup.getMessages()) {
print.println(" * " + msg.getCode() + " *" + msg.getSeverity() + "*");
}
}
}
use of com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage in project CFLint by cflint.
the class CFLintDoc method generateRuleGroup.
public static void generateRuleGroup(final CFLintPluginInfo pluginInfo, final PrintWriter print) {
final Map<String, PluginMessage> allCodes = new LinkedHashMap<>();
for (final PluginInfoRule rule : pluginInfo.getRules()) {
for (final PluginMessage msg : rule.getMessages()) {
allCodes.put(msg.getCode(), msg);
}
}
for (final RuleGroup ruleGroup : pluginInfo.getRuleGroups()) {
print.println("Rule Group : " + ruleGroup.getName());
for (final PluginMessage msg : ruleGroup.getMessages()) {
print.println("\t" + msg.getCode() + " : " + msg.getSeverity());
allCodes.remove(msg.getCode());
}
}
if (!allCodes.isEmpty()) {
print.println("Rule Group : UNASSIGNED");
for (final PluginMessage msg : allCodes.values()) {
print.println("\t" + msg.getCode() + " : " + msg.getSeverity());
}
}
}
Aggregations