Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java
  • Loading branch information
Stefan Weiser committed Apr 26, 2017
2 parents 6f709b0 + 5188274 commit be979a6
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

import org.sonar.api.batch.sensor.SensorContext;

/**
Expand Down Expand Up @@ -68,18 +70,18 @@ static class Warning {
public final String id;
public final String msg;

Warning(String filename, String line, String id, String msg) {
Warning(@Nullable String filename, @Nullable String line, @Nullable String id, @Nullable String msg) {
this.filename = getValueOrDefault(filename, "");
this.line = getValueOrDefault(line, "");
this.id = getValueOrDefault(id, "");
this.msg = getValueOrDefault(msg, "");
}

private static String getValueOrDefault(String value, String defaultValue) {
private static String getValueOrDefault(@Nullable String value, String defaultValue) {
return isNotNullOrEmpty(value) ? value : defaultValue;
}

private static boolean isNotNullOrEmpty(String str) {
private static boolean isNotNullOrEmpty(@Nullable String str) {
return str != null && !str.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected void processReport(final SensorContext context, File report)
}

private boolean isInputValid(CompilerParser.Warning warning) {
return warning != null && !warning.toString().isEmpty();
return !warning.toString().isEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.in.SMHierarchicCursor;
Expand Down Expand Up @@ -230,7 +231,7 @@ public String toString() {
return getClass().getSimpleName();
}

private String ensureRefPathIsCorrect(String refPath) {
private String ensureRefPathIsCorrect(@Nullable String refPath) {
if (refPath == null || refPath.isEmpty() || refPath.endsWith(File.separator)) {
return refPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* {@inheritDoc}
*/
public class CppcheckParserV1 implements CppcheckParser {
public static final Logger LOG = Loggers.get(CppcheckParserV1.class);
private static final Logger LOG = Loggers.get(CppcheckParserV1.class);
private final CxxCppCheckSensor sensor;

public CppcheckParserV1(CxxCppCheckSensor sensor) {
Expand Down Expand Up @@ -72,24 +72,26 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
file = file.replace('\\','/');
}

if ("*".equals(file)) { // findings on project level
if ("*".equals(file)) {
// findings on project level
file = null;
line = null;
}

if (isInputValid(file, line, id, msg)) {
if (isInputValid(id, msg)) {
sensor.saveUniqueViolation(context, CxxCppCheckRuleRepository.KEY, file, line, id, msg);
} else {
LOG.warn("Skipping invalid violation: '{}'", msg);
}
}
} catch (RuntimeException e) { //NOSONAR
throw new XMLStreamException(e.getMessage()); //NOSONAR
} catch (RuntimeException e) {
LOG.debug("processReport failed {}", e);
throw new XMLStreamException();
}
}

private boolean isInputValid(String file, String line, String id, String msg) {
return id != null && !id.isEmpty() && msg != null && !msg.isEmpty();
private boolean isInputValid(String id, String msg) {
return !id.isEmpty() && msg != null && !msg.isEmpty();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* {@inheritDoc}
*/
public class CppcheckParserV2 implements CppcheckParser {
public static final Logger LOG = Loggers.get(CppcheckParserV2.class);
private static final Logger LOG = Loggers.get(CppcheckParserV2.class);
private final CxxCppCheckSensor sensor;

public CppcheckParserV2(CxxCppCheckSensor sensor) {
Expand Down Expand Up @@ -88,13 +88,14 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
file = file.replace('\\','/');
}

if ("*".equals(file)) { // findings on project level
if ("*".equals(file)) {
// findings on project level
file = null;
line = null;
}
}

if (isInputValid(file, line, id, msg)) {
if (isInputValid(id, msg)) {
sensor.saveUniqueViolation(context, CxxCppCheckRuleRepository.KEY, file, line, id, msg);
} else {
LOG.warn("Skipping invalid violation: '{}'", msg);
Expand All @@ -103,6 +104,7 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
}
}
} catch (RuntimeException e) {
LOG.debug("processReport failed {}", e);
throw new XMLStreamException();
}

Expand All @@ -112,16 +114,14 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
}

private String createMsg(String inconclusive, String msg) {
if (msg != null && !msg.isEmpty()) {
if ("true".equals(inconclusive)) {
return "[inconclusive] " + msg;
}
if (!msg.isEmpty() && ("true".equals(inconclusive))) {
return "[inconclusive] " + msg;
}
return msg;
}

private boolean isInputValid(String file, String line, String id, String msg) {
return id != null && !id.isEmpty() && msg != null && !msg.isEmpty();
private boolean isInputValid(String id, String msg) {
return !id.isEmpty() && msg != null && !msg.isEmpty();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.in.SMHierarchicCursor;
Expand All @@ -46,7 +47,7 @@
* @author Bert
*/
public class CxxPCLintSensor extends CxxReportSensor {
public static final Logger LOG = Loggers.get(CxxPCLintSensor.class);
private static final Logger LOG = Loggers.get(CxxPCLintSensor.class);
public static final String REPORT_PATH_KEY = "pclint.reportPath";
public static final String KEY = "PC-Lint";

Expand Down Expand Up @@ -112,7 +113,7 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
}
}

private boolean isInputValid(String file, String line, String id, String msg) {
private boolean isInputValid(@Nullable String file, @Nullable String line, @Nullable String id, @Nullable String msg) {
try {
if (file == null || file.isEmpty() || (Integer.parseInt(line) == 0)) {
// issue for project or file level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.measures.Metric;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.parser.CxxParser;
import org.sonar.cxx.sensors.compiler.CxxCompilerSensor;
Expand All @@ -78,6 +80,7 @@
*/
public class CxxSquidSensor implements Sensor {

private static final Logger LOG = Loggers.get(CxxSquidSensor.class);
public static final String SOURCE_FILE_SUFFIXES_KEY = "suffixes.sources";
public static final String HEADER_FILE_SUFFIXES_KEY = "suffixes.headers";
public static final String DEFINES_KEY = "defines";
Expand All @@ -94,7 +97,7 @@ public class CxxSquidSensor implements Sensor {

private static final Number[] LIMITS_COMPLEXITY_METHODS = {1, 2, 4, 6, 8, 10, 12, 20, 30};
private static final Number[] LIMITS_COMPLEXITY_FILES = {0, 5, 10, 20, 30, 60, 90};
public static String KEY = "Squid";
public static final String KEY = "Squid";

private final FileLinesContextFactory fileLinesContextFactory;
private final CxxChecks checks;
Expand Down Expand Up @@ -202,12 +205,8 @@ private CxxConfiguration createConfiguration(FileSystem fs) {
if (cxxConf.getJsonCompilationDatabaseFile() != null) {
try {
new JsonCompilationDatabase(cxxConf, new File(cxxConf.getJsonCompilationDatabaseFile()));
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
LOG.debug("Cannot access Json DB File: {}", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

import javax.annotation.Nullable;

import org.apache.commons.io.FilenameUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.sonar.api.batch.sensor.Sensor;
Expand All @@ -43,7 +46,7 @@
* common logic such as finding the reports and saving issues in SonarQube
*/
public abstract class CxxReportSensor implements Sensor {
public static final Logger LOG = Loggers.get(CxxReportSensor.class);
private static final Logger LOG = Loggers.get(CxxReportSensor.class);
private final Set<String> notFoundFiles = new HashSet<>();
private final Set<String> uniqueIssues = new HashSet<>();
private int violationsCount;
Expand Down Expand Up @@ -197,9 +200,8 @@ public static List<File> getReports(CxxLanguage language,
* @param ruleId
* @param msg
*/
public void saveUniqueViolation(SensorContext sensorContext, String ruleRepoKey, String file,
String line, String ruleId, String msg) {

public void saveUniqueViolation(SensorContext sensorContext, String ruleRepoKey,
@Nullable String file, @Nullable String line, String ruleId, String msg) {
if (uniqueIssues.add(file + line + ruleId + msg)) { // StringBuilder is slower
saveViolation(sensorContext, ruleRepoKey, file, line, ruleId, msg);
}
Expand All @@ -212,9 +214,8 @@ public void saveUniqueViolation(SensorContext sensorContext, String ruleRepoKey,
* according parameters ('file' = null for project level, 'line' = null for
* file-level)
*/
private void saveViolation(SensorContext sensorContext, String ruleRepoKey, String filename, String line,
String ruleId, String msg) {

private void saveViolation(SensorContext sensorContext, String ruleRepoKey,
@Nullable String filename, @Nullable String line, String ruleId, String msg) {
// handles file="" situation -- file level
if ((filename != null) && (!filename.isEmpty())) {
String root = sensorContext.fileSystem().baseDir().getAbsolutePath();
Expand All @@ -238,7 +239,7 @@ private void saveViolation(SensorContext sensorContext, String ruleRepoKey, Stri
newIssue.at(location);
newIssue.save();
violationsCount++;
} catch (Exception ex) { //NOSONAR
} catch (Exception ex) {
LOG.error("Could not add the issue '{}', skipping issue", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}
Expand All @@ -257,14 +258,14 @@ private void saveViolation(SensorContext sensorContext, String ruleRepoKey, Stri
newIssue.at(location);
newIssue.save();
violationsCount++;
} catch (Exception ex) { //NOSONAR
} catch (Exception ex) {
LOG.error("Could not add the issue '{}' for rule '{}:{}', skipping issue", ex.getMessage(), ruleRepoKey, ruleId);
CxxUtils.validateRecovery(ex, this.language);
}
}
}

private int getLineAsInt(String line, int maxLine) {
private int getLineAsInt(@Nullable String line, int maxLine) {
int lineNr = 0;
if (line != null) {
try {
Expand All @@ -284,9 +285,9 @@ private int getLineAsInt(String line, int maxLine) {
}

protected void processReport(final SensorContext context, File report)
throws Exception { //NOSONAR
throws Exception {
}

abstract protected String reportPathKey();
abstract protected String getSensorKey();
protected abstract String reportPathKey();
protected abstract String getSensorKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
* {@inheritDoc}
*/
public class CxxValgrindSensor extends CxxReportSensor {
public static final Logger LOG = Loggers.get(CxxValgrindSensor.class);
private static final Logger LOG = Loggers.get(CxxValgrindSensor.class);
public static final String REPORT_PATH_KEY = "valgrind.reportPath";
public static String KEY = "Valgrind";
public static final String KEY = "Valgrind";

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package org.sonar.cxx.sensors.valgrind;

import java.io.File;

import javax.annotation.Nullable;

import org.apache.commons.io.FilenameUtils;

import org.apache.commons.lang.builder.HashCodeBuilder;
Expand All @@ -38,10 +41,11 @@ class ValgrindFrame {
private String line = "";

/**
* Constucts a stack frame with given attributes. Its perfectly valid if some
* of them are empty or dont carry meaningfull information.
* Constructs a stack frame with given attributes. Its perfectly valid if some
* of them are empty or don't carry meaningful information.
*/
public ValgrindFrame(String ip, String obj, String fn, String dir, String file, String line) {
public ValgrindFrame(@Nullable String ip, @Nullable String obj, @Nullable String fn, @Nullable String dir,
@Nullable String file, @Nullable String line) {
if (ip != null) {
this.ip = ip;
}
Expand All @@ -67,7 +71,7 @@ public String toString() {
StringBuilder builder = new StringBuilder().append(ip).append(": ").append(fn);
if (isLocationKnown()) {
builder.append(" (")
.append("".equals(file) ? ("in " + obj) : (file + getLineStr()))
.append("".equals(file) ? ("in " + obj) : (file + getLineStr())) //NOSONAR
.append(')');
}

Expand Down Expand Up @@ -114,6 +118,6 @@ private boolean isLocationKnown() {
}

private String getLineStr() {
return "".equals(line) ? "" : ":" + line;
return "".equals(line) ? "" : ":" + line; //NOSONAR
}
}
Loading

0 comments on commit be979a6

Please sign in to comment.