Skip to content

Commit

Permalink
Merge pull request #695 from guwirth/bugfix/693-multi-module
Browse files Browse the repository at this point in the history
improved multi module support
  • Loading branch information
guwirth committed Jan 1, 2016
2 parents df8317d + 07f3d49 commit 7089088
Show file tree
Hide file tree
Showing 27 changed files with 127 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ script:

after_failure:
- cat $SONARHOME/logs/sonar.log
- find . -name _cpp-multimodule-project-2_.log | xargs cat
- find . -name "*.log" | xargs cat
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
sonar.host.url=http://localhost:9000

sonar.modules=cli,lib,package1,package2
cli.sonar.projectBaseDir=cli
cli.sonar.sources=.

cli.sonar.projectBaseDir=/home/travis/build/SonarOpenCommunity/sonar-cxx/integration-tests/testdata/cpp-multimodule-project/
cli.sonar.sources=cli
cli.sonar.projectName=cli
cli.sonar.cxx.includeDirectories=../../googletest_project/tests/gtest-1.6.0/include/gtest
lib.sonar.projectBaseDir=lib
lib.sonar.sources=.

lib.sonar.projectBaseDir=/home/travis/build/SonarOpenCommunity/sonar-cxx/integration-tests/testdata/cpp-multimodule-project/
lib.sonar.sources=lib
lib.sonar.projectName=lib
package1.sonar.projectBaseDir=package1
package1.sonar.sources=.

package1.sonar.projectBaseDir=/home/travis/build/SonarOpenCommunity/sonar-cxx/integration-tests/testdata/cpp-multimodule-project/
package1.sonar.sources=package1
package1.sonar.projectName=package1
package2.sonar.projectBaseDir=package2
package2.sonar.sources=.

package2.sonar.projectBaseDir=/home/travis/build/SonarOpenCommunity/sonar-cxx/integration-tests/testdata/cpp-multimodule-project/
package2.sonar.sources=package2
package2.sonar.projectName=package2

sonar.projectKey=cpp-multimodule-project
Expand Down Expand Up @@ -41,7 +45,7 @@ sonar.cxx.xunit.reportPath=sonarcpp/reports-xunit/*.xml
sonar.cxx.coverage.reportPath=sonarcpp/bullseye-coverage-results/bullseyecoverage-result-0.xml
sonar.cxx.coverage.itReportPath=sonarcpp/bullseye-coverage-results/bullseyecoverage-result-1.xml
sonar.cxx.coverage.overallReportPath=sonarcpp/bullseye-coverage-results/bullseyecoverage-result-2.xml

sonar.cxx.defines=__cdecl, \n\
__declspec(a), \n\
__pragma(a), \n\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.sonar.plugins.cxx.utils.CxxMetrics;
import org.sonar.plugins.cxx.utils.CxxReportSensor;
import org.sonar.plugins.cxx.utils.CxxUtils;
import org.sonar.api.batch.bootstrap.ProjectReactor;

/**
* compiler for C++ with advanced analysis features (e.g. for VC 2008 team edition or 2010/2012/2013 premium edition)
Expand All @@ -56,8 +55,8 @@ public class CxxCompilerSensor extends CxxReportSensor {
/**
* {@inheritDoc}
*/
public CxxCompilerSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile, ProjectReactor reactor) {
super(perspectives, settings, fs, reactor, CxxMetrics.COMPILER);
public CxxCompilerSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile) {
super(perspectives, settings, fs, CxxMetrics.COMPILER);
this.profile = profile;

addCompilerParser(new CxxCompilerVcParser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javax.xml.stream.XMLStreamException;

import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.InputFile;
Expand Down Expand Up @@ -61,15 +60,12 @@ private enum CoverageType {
public static final String FORCE_ZERO_COVERAGE_KEY = "sonar.cxx.coverage.forceZeroCoverage";

private List<CoverageParser> parsers = new LinkedList<>();
private final ProjectReactor reactor;

/**
* {@inheritDoc}
*/
public CxxCoverageSensor(Settings settings, FileSystem fs, ProjectReactor reactor) {
super(settings, fs, reactor);

this.reactor = reactor;
public CxxCoverageSensor(Settings settings, FileSystem fs) {
super(settings, fs);
final String baseDir = fs.baseDir().getAbsolutePath();
parsers.add(new CoberturaParser(baseDir));
parsers.add(new BullseyeParser(baseDir));
Expand Down Expand Up @@ -97,30 +93,21 @@ public void analyse(Project project, SensorContext context) {

if (settings.hasKey(REPORT_PATH_KEY)) {
CxxUtils.LOG.debug("Parsing coverage reports");
List<File> reports = getReports(settings,
reactor.getRoot().getBaseDir().getAbsolutePath(),
fs.baseDir().getPath(),
REPORT_PATH_KEY);
List<File> reports = getReports(settings, fs.baseDir(), REPORT_PATH_KEY);
coverageMeasures = processReports(project, context, reports);
saveMeasures(context, coverageMeasures, CoverageType.UT_COVERAGE);
}

if (settings.hasKey(IT_REPORT_PATH_KEY)) {
CxxUtils.LOG.debug("Parsing integration test coverage reports");
List<File> itReports = getReports(settings,
reactor.getRoot().getBaseDir().getAbsolutePath(),
fs.baseDir().getPath(),
IT_REPORT_PATH_KEY);
List<File> itReports = getReports(settings, fs.baseDir(), IT_REPORT_PATH_KEY);
itCoverageMeasures = processReports(project, context, itReports);
saveMeasures(context, itCoverageMeasures, CoverageType.IT_COVERAGE);
}

if (settings.hasKey(OVERALL_REPORT_PATH_KEY)) {
CxxUtils.LOG.debug("Parsing overall test coverage reports");
List<File> overallReports = getReports(settings,
reactor.getRoot().getBaseDir().getAbsolutePath(),
fs.baseDir().getPath(),
OVERALL_REPORT_PATH_KEY);
List<File> overallReports = getReports(settings, fs.baseDir(), OVERALL_REPORT_PATH_KEY);
overallCoverageMeasures = processReports(project, context, overallReports);
saveMeasures(context, overallCoverageMeasures, CoverageType.OVERALL_COVERAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
Expand All @@ -52,8 +51,8 @@ public class CxxCppCheckSensor extends CxxReportSensor {
* {@inheritDoc}
*/
public CxxCppCheckSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs,
RulesProfile profile, ProjectReactor reactor) {
super(perspectives, settings, fs, reactor, CxxMetrics.CPPCHECK);
RulesProfile profile) {
super(perspectives, settings, fs, CxxMetrics.CPPCHECK);
this.profile = profile;
parsers.add(new CppcheckParserV2(this));
parsers.add(new CppcheckParserV1(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.sonar.api.utils.StaxParser;
import org.sonar.plugins.cxx.utils.CxxMetrics;
import org.sonar.plugins.cxx.utils.CxxReportSensor;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.plugins.cxx.utils.CxxUtils;

/**
Expand All @@ -50,8 +49,8 @@ public class CxxExternalRulesSensor extends CxxReportSensor {
/**
* {@inheritDoc}
*/
public CxxExternalRulesSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile, ProjectReactor reactor) {
super(perspectives, settings, fs, reactor, CxxMetrics.EXTERNAL);
public CxxExternalRulesSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile) {
super(perspectives, settings, fs, CxxMetrics.EXTERNAL);
this.profile = profile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.sonar.plugins.cxx.utils.CxxReportSensor;
import org.sonar.plugins.cxx.utils.CxxUtils;
import org.sonar.plugins.cxx.utils.EmptyReportException;
import org.sonar.api.batch.bootstrap.ProjectReactor;

/**
* PC-lint is an equivalent to pmd but for C++ The first version of the tool was
Expand All @@ -57,8 +56,8 @@ public class CxxPCLintSensor extends CxxReportSensor {
/**
* {@inheritDoc}
*/
public CxxPCLintSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile, ProjectReactor reactor) {
super(perspectives, settings, fs, reactor, CxxMetrics.PCLINT);
public CxxPCLintSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile) {
super(perspectives, settings, fs, CxxMetrics.PCLINT);
this.profile = profile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.sonar.plugins.cxx.utils.CxxMetrics;
import org.sonar.plugins.cxx.utils.CxxReportSensor;
import org.sonar.plugins.cxx.utils.CxxUtils;
import org.sonar.api.batch.bootstrap.ProjectReactor;

/**
* {@inheritDoc}
Expand All @@ -46,8 +45,8 @@ public final class CxxRatsSensor extends CxxReportSensor {
/**
* {@inheritDoc}
*/
public CxxRatsSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile, ProjectReactor reactor) {
super(perspectives, settings, fs, reactor, CxxMetrics.RATS);
public CxxRatsSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, RulesProfile profile) {
super(perspectives, settings, fs, CxxMetrics.RATS);
this.profile = profile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private CxxConfiguration createConfiguration(FileSystem fs, Settings settings) {

String filePaths = settings.getString(CxxCompilerSensor.REPORT_PATH_KEY);
if (filePaths != null && !"".equals(filePaths)) {
List<File> reports = CxxReportSensor.getReports(settings, fs.baseDir().getPath(), "", CxxCompilerSensor.REPORT_PATH_KEY);
List<File> reports = CxxReportSensor.getReports(settings, fs.baseDir(), CxxCompilerSensor.REPORT_PATH_KEY);
cxxConf.setCompilationPropertiesWithBuildLog(reports,
settings.getString(CxxCompilerSensor.PARSER_KEY_DEF),
settings.getString(CxxCompilerSensor.REPORT_CHARSET_DEF));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issue;
import org.sonar.api.measures.Measure;
Expand All @@ -51,24 +49,22 @@
public abstract class CxxReportSensor implements Sensor {

private ResourcePerspectives perspectives;
private Set<String> notFoundFiles = new HashSet<String>();
private Set<String> uniqueIssues = new HashSet<String>();
private Set<String> notFoundFiles = new HashSet<>();
private Set<String> uniqueIssues = new HashSet<>();
private final Metric metric;
private int violationsCount;

protected FileSystem fs;
protected CxxSettings settings;
private final ProjectReactor reactor;

/**
* Use this constructor if you dont have to save violations aka issues
*
* @param settings the Settings object used to access the configuration properties
* @param fs file system access layer
* @param reactor
*/
protected CxxReportSensor(Settings settings, FileSystem fs, ProjectReactor reactor) {
this(null, settings, fs, reactor, null);
protected CxxReportSensor(Settings settings, FileSystem fs) {
this(null, settings, fs, null);
}

/**
Expand All @@ -79,19 +75,18 @@ protected CxxReportSensor(Settings settings, FileSystem fs, ProjectReactor react
* @param fs file system access layer
* @param metric this metrics will be used to save a measure of the overall
* issue count. Pass 'null' to skip this.
* @param reactor
*/
protected CxxReportSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, ProjectReactor reactor, Metric metric) {
protected CxxReportSensor(ResourcePerspectives perspectives, Settings settings, FileSystem fs, Metric metric) {
this.settings = new CxxSettings(settings);
this.fs = fs;
this.metric = metric;
this.reactor = reactor;
this.perspectives = perspectives;
}

/**
* {@inheritDoc}
*/
@Override
public boolean shouldExecuteOnProject(Project project) {
return fs.hasFiles(fs.predicates().hasLanguage(CxxLanguage.KEY))
&& settings.hasKey(reportPathKey());
Expand All @@ -100,40 +95,36 @@ public boolean shouldExecuteOnProject(Project project) {
/**
* {@inheritDoc}
*/
@Override
public void analyse(Project project, SensorContext context) {
if (!CxxUtils.isReactorProject(project)) {
try {
List<File> reports = getReports(settings,
reactor.getRoot().getBaseDir().getCanonicalPath(),
fs.baseDir().getPath(),
reportPathKey());
violationsCount = 0;
try {
List<File> reports = getReports(settings, fs.baseDir(), reportPathKey());
violationsCount = 0;

for (File report : reports) {
CxxUtils.LOG.info("Processing report '{}'", report);
try {
int prevViolationsCount = violationsCount;
processReport(project, context, report);
CxxUtils.LOG.info("{} processed = {}", metric == null ? "Issues" : metric.getName(),
violationsCount - prevViolationsCount);
} catch (EmptyReportException e) {
CxxUtils.LOG.warn("The report '{}' seems to be empty, ignoring.", report);
}
for (File report : reports) {
CxxUtils.LOG.info("Processing report '{}'", report);
try {
int prevViolationsCount = violationsCount;
processReport(project, context, report);
CxxUtils.LOG.info("{} processed = {}", metric == null ? "Issues" : metric.getName(),
violationsCount - prevViolationsCount);
} catch (EmptyReportException e) {
CxxUtils.LOG.warn("The report '{}' seems to be empty, ignoring.", report);
}
}

if (metric != null) {
Measure measure = new Measure(metric);
measure.setIntValue(violationsCount);
context.saveMeasure(measure);
}
} catch (Exception e) {
String msg = new StringBuilder()
.append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'")
.toString();
throw new IllegalStateException(msg, e);
if (metric != null) {
Measure measure = new Measure(metric);
measure.setIntValue(violationsCount);
context.saveMeasure(measure);
}
} catch (Exception e) {
String msg = new StringBuilder()
.append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'")
.toString();
throw new IllegalStateException(msg, e);
}
}

Expand All @@ -150,12 +141,11 @@ protected String getStringProperty(String name, String def) {
}

public static List<File> getReports(Settings settings,
String reactorBaseDir,
String moduleBaseDir,
File moduleBaseDir,
String reportPathPropertyKey) {

String reportPath = settings.getString(reportPathPropertyKey);
List<File> reports = new ArrayList<File>();
List<File> reports = new ArrayList<>();
if (reportPath != null && !reportPath.isEmpty()) {
reportPath = FilenameUtils.normalize(reportPath);

Expand All @@ -164,10 +154,7 @@ public static List<File> getReports(Settings settings,
reports.add(singleFile);
} else {
CxxUtils.LOG.debug("Using pattern '{}' to find reports", reportPath);
CxxUtils.GetReportForBaseDirAndPattern(reactorBaseDir, reportPath, reports);
if (reports.isEmpty() && !moduleBaseDir.isEmpty()) {
CxxUtils.GetReportForBaseDirAndPattern(moduleBaseDir, reportPath, reports);
}
CxxUtils.GetReportForBaseDirAndPattern(moduleBaseDir.getPath(), reportPath, reports);
if (reports.isEmpty()) {
CxxUtils.LOG.warn("Cannot find a report for '{}={}'", reportPathPropertyKey, reportPath);
}
Expand Down Expand Up @@ -204,7 +191,7 @@ private void saveViolation(Project project, SensorContext context, String ruleRe
int lineNr = 0;
// handles file="" situation -- file level
if ((filename != null) && (filename.length() > 0)) {
String root = reactor.getRoot().getBaseDir().getAbsolutePath();
String root = fs.baseDir().getAbsolutePath();
String normalPath = CxxUtils.normalizePathFull(filename, root);
if (normalPath != null && !notFoundFiles.contains(normalPath)) {
InputFile inputFile = fs.inputFile(fs.predicates().is(new File(normalPath)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.tools.ant.DirectoryScanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -146,9 +144,6 @@ public static String normalizePathFull(String filename, String baseDir) {
}
return filePath;
}

public static boolean isReactorProject(Project project) {
return project.isRoot() && !project.getModules().isEmpty();
}

}

Loading

0 comments on commit 7089088

Please sign in to comment.