-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
see CXX issue #102
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,22 +19,20 @@ | |
*/ | ||
package org.sonar.plugins.cxx.compiler; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.sonar.api.batch.SensorContext; | ||
import org.sonar.api.config.Settings; | ||
import org.sonar.api.profiles.RulesProfile; | ||
import org.sonar.api.resources.Project; | ||
import org.sonar.api.rules.RuleFinder; | ||
import org.sonar.api.scan.filesystem.ModuleFileSystem; | ||
import org.sonar.plugins.cxx.utils.CxxReportSensor; | ||
import org.sonar.plugins.cxx.utils.CxxUtils; | ||
import org.sonar.api.scan.filesystem.ModuleFileSystem; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* compiler for C++ with advanced analysis features (e.g. for VC 2008 team edition or 2010/2012/2013 premium edition) | ||
|
@@ -75,8 +73,9 @@ private void addCompilerParser(CompilerParser parser) { | |
private CompilerParser getCompilerParser() { | ||
String parserKey = getStringProperty(PARSER_KEY_DEF, DEFAULT_PARSER_DEF); | ||
CompilerParser parser = parsers.get(parserKey); | ||
if (parser == null) | ||
parser = parsers.get(DEFAULT_PARSER_DEF); | ||
if (parser == null) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Bertk
Author
Owner
|
||
parser = parsers.get(DEFAULT_PARSER_DEF); | ||
} | ||
return parser; | ||
} | ||
|
||
|
@@ -107,31 +106,30 @@ protected String defaultReportPath() { | |
* @return Value of the property if set and not empty, else default value. | ||
*/ | ||
public String getParserStringProperty(String name, String def) { | ||
String s = getStringProperty(name, ""); | ||
if (StringUtils.isEmpty(s)) | ||
return def; | ||
return s; | ||
String s = getStringProperty(name, ""); | ||
if (StringUtils.isEmpty(s)) { | ||
return def; | ||
} | ||
return s; | ||
} | ||
|
||
@Override | ||
protected void processReport(final Project project, final SensorContext context, File report) | ||
throws javax.xml.stream.XMLStreamException | ||
{ | ||
throws javax.xml.stream.XMLStreamException { | ||
int countViolations = 0; | ||
final CompilerParser parser = getCompilerParser(); | ||
final String reportCharset = getParserStringProperty(REPORT_CHARSET_DEF, parser.defaultCharset()); | ||
final String reportRegEx = getParserStringProperty(REPORT_REGEX_DEF, parser.defaultRegexp()); | ||
final List<CompilerParser.Warning> warnings = new LinkedList<CompilerParser.Warning>(); | ||
|
||
// Iterate through the lines of the input file | ||
CxxUtils.LOG.info("Scanner '" + parser.key() + "' initialized with report '{}'" + ", CharSet= '" + reportCharset + "'", report); | ||
CxxUtils.LOG.info("Scanner '" + parser.key() + "' initialized with report '" + report + "', CharSet= '" + reportCharset + "'" ); | ||
This comment has been minimized.
Sorry, something went wrong.
wenns
|
||
try { | ||
parser.ParseReport(report, reportCharset, reportRegEx, warnings); | ||
for(CompilerParser.Warning w : warnings) { | ||
// get filename from file system - e.g. VC writes case insensitive file name to html | ||
String filename = getCaseSensitiveFileName(w.filename, fs.sourceDirs()); | ||
if (isInputValid(filename, w.line, w.id, w.msg)) { | ||
if (saveUniqueViolation(project, context, parser.rulesRepositoryKey(), filename, w.line, w.id, w.msg)) { | ||
if (isInputValid(w.filename, w.line, w.id, w.msg)) { | ||
if (saveUniqueViolation(project, context, parser.rulesRepositoryKey(), w.filename, w.line, w.id, w.msg)) { | ||
countViolations++; | ||
} | ||
} else { | ||
|
@@ -150,40 +148,4 @@ private boolean isInputValid(String file, String line, String id, String msg) { | |
return !StringUtils.isEmpty(file) && !StringUtils.isEmpty(line) | ||
&& !StringUtils.isEmpty(id) && !StringUtils.isEmpty(msg); | ||
} | ||
|
||
/** | ||
* Supports full path and relative path in report.xml file. | ||
*/ | ||
private String getCaseSensitiveFileName(String file, List<java.io.File> sourceDirs) { | ||
// check whether the report file uses absolute path | ||
File targetfile = new java.io.File(file); | ||
if (targetfile.exists()) { | ||
file = getRealFileName(targetfile); | ||
} else { | ||
Iterator<java.io.File> iterator = sourceDirs.iterator(); | ||
while (iterator.hasNext()) { | ||
targetfile = new java.io.File(iterator.next().getPath() + java.io.File.separatorChar + file); | ||
if (targetfile.exists()) { | ||
file = getRealFileName(targetfile); | ||
break; | ||
} | ||
} | ||
} | ||
return file; | ||
} | ||
|
||
/** | ||
* Find the case sensitive file name - tools might use different naming schema | ||
* e.g. VC HTML or build log report uses case insensitive file name (lower case on windows) | ||
*/ | ||
private String getRealFileName( File filename){ | ||
try { | ||
return filename.getCanonicalFile().getAbsolutePath(); | ||
} catch (java.io.IOException e) { | ||
CxxUtils.LOG.error("SaveViolation GetRealFileName failed '{}'", e.toString()); | ||
} | ||
return filename.getName(); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ public void analyse(Project project, SensorContext context) { | |
List<File> reports = getReports(conf, fs.baseDir().getPath(), | ||
reportPathKey(), defaultReportPath()); | ||
for (File report : reports) { | ||
CxxUtils.LOG.info("Processing report '{}'", report); | ||
CxxUtils.LOG.info("Processing report '" + report + "'"); | ||
try{ | ||
processReport(project, context, report); | ||
} | ||
|
@@ -106,10 +106,11 @@ public String toString() { | |
} | ||
|
||
public String getStringProperty(String name, String def) { | ||
String value = conf.getString(name); | ||
if (value == null) | ||
value = def; | ||
return value; | ||
String value = conf.getString(name); | ||
if (value == null) { | ||
value = def; | ||
} | ||
return value; | ||
} | ||
|
||
protected List<File> getReports(Settings conf, | ||
|
@@ -158,7 +159,7 @@ public boolean saveUniqueViolation(Project project, SensorContext context, Strin | |
* Project or file-level violations can be saved by passing null for the according parameters | ||
* ('file' = 'line' = null for project level, 'line' = null for file-level) | ||
*/ | ||
public boolean saveViolation(Project project, SensorContext context, String ruleRepoKey, | ||
public boolean saveViolation(Project module, SensorContext context, String ruleRepoKey, | ||
String file, String line, String ruleId, String msg) { | ||
boolean added = false; | ||
RuleQuery ruleQuery = RuleQuery.create() | ||
|
@@ -171,11 +172,19 @@ public boolean saveViolation(Project project, SensorContext context, String rule | |
if ((file != null) && (file.length() > 0)){ | ||
String normalPath = CxxUtils.normalizePath(file); | ||
if(normalPath != null){ | ||
org.sonar.api.resources.File resource = | ||
org.sonar.api.resources.File.fromIOFile(new File(normalPath), project); | ||
if (context.getResource(resource) != null) { | ||
org.sonar.api.resources.File sonarFile = | ||
This comment has been minimized.
Sorry, something went wrong.
wenns
|
||
org.sonar.api.resources.File.fromIOFile(new File(normalPath), module); | ||
if (sonarFile == null) { | ||
// support SQ<4.2 | ||
sonarFile = org.sonar.api.resources.File.fromIOFile(new File(normalPath), module.getFileSystem().getTestDirs()); | ||
} | ||
if (sonarFile == null){ | ||
normalPath = CxxUtils.getCaseSensitiveFileName(file, fs); | ||
sonarFile = org.sonar.api.resources.File.fromIOFile(new File(normalPath), module); | ||
} | ||
if (context.getResource(sonarFile ) != null) { | ||
// file level violation | ||
violation = Violation.create(rule, resource); | ||
violation = Violation.create(rule, sonarFile); | ||
|
||
// considering the line information for file level violations only | ||
if (line != null){ | ||
|
@@ -184,7 +193,7 @@ public boolean saveViolation(Project project, SensorContext context, String rule | |
linenr = linenr == 0 ? 1 : linenr; | ||
violation.setLineId(linenr); | ||
} catch(java.lang.NumberFormatException nfe){ | ||
CxxUtils.LOG.warn("Skipping invalid line number: {}", line); | ||
CxxUtils.LOG.warn("Skipping invalid line number: " + line); | ||
} | ||
} | ||
} else { | ||
|
@@ -196,7 +205,7 @@ public boolean saveViolation(Project project, SensorContext context, String rule | |
} | ||
} else { | ||
// project level violation | ||
violation = Violation.create(rule, project); | ||
violation = Violation.create(rule, module); | ||
} | ||
|
||
if (violation != null){ | ||
|
May sound picky, but I dont like mixing style changes with logic changes. I dont reject style chnanges per se, but please do them in a separate commit. This comment applies to half of the changes in this commit.