Skip to content

Commit

Permalink
Merge pull request #1121 from Bertk/fixes2
Browse files Browse the repository at this point in the history
getElements - log IOException & replace tabs with whitespace ...
  • Loading branch information
guwirth authored Apr 22, 2017
2 parents 56a3ebb + ad41aa5 commit 3a8d79a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
package org.sonar.cxx.sensors.drmemory;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorDescriptor;
Expand All @@ -46,7 +43,7 @@
* @author asylvestre
*/
public class CxxDrMemorySensor extends CxxReportSensor {
public static final Logger LOG = Loggers.get(CxxDrMemorySensor.class);
private static final Logger LOG = Loggers.get(CxxDrMemorySensor.class);
public static final String REPORT_PATH_KEY = "drmemory.reportPath";
public static final String KEY = "DrMemory";
public static final String DEFAULT_CHARSET_DEF = StandardCharsets.UTF_8.name();
Expand All @@ -58,6 +55,9 @@ public CxxDrMemorySensor(CxxLanguage language) {
super(language);
}

/**
* {@inheritDoc}
*/
public String defaultCharset() {
return DEFAULT_CHARSET_DEF;
}
Expand All @@ -66,7 +66,7 @@ public String defaultCharset() {
public void describe(SensorDescriptor descriptor) {
descriptor.onlyOnLanguage(this.language.getKey()).name(language.getName() + " DrMemorySensor");
}

@Override
protected String reportPathKey() {
return REPORT_PATH_KEY;
Expand All @@ -76,21 +76,21 @@ protected String reportPathKey() {
protected void processReport(final SensorContext context, File report) {
LOG.debug("Parsing 'Dr Memory' format");

for (DrMemoryError error : DrMemoryParser.parse(report, defaultCharset())) {
if (error.stackTrace.isEmpty()) {
for (DrMemoryError error : DrMemoryParser.parse(report, defaultCharset())) {
if (error.stackTrace.isEmpty()) {
saveUniqueViolation(context, CxxDrMemoryRuleRepository.KEY,
null, null,
error.type.getId(), error.message);
}
for (Location errorLocation : error.stackTrace) {
if (isFileInAnalysis(context, errorLocation)) {
saveUniqueViolation(context, CxxDrMemoryRuleRepository.KEY,
null, null,
errorLocation.file, errorLocation.line.toString(),
error.type.getId(), error.message);
}
for (Location errorLocation : error.stackTrace) {
if (isFileInAnalysis(context, errorLocation)) {
saveUniqueViolation(context, CxxDrMemoryRuleRepository.KEY,
errorLocation.file, errorLocation.line.toString(),
error.type.getId(), error.message);
break;
}
break;
}
}
}
}

private boolean isFileInAnalysis(SensorContext context, Location errorLocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,144 +22,145 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.sensors.drmemory.DrMemoryParser.DrMemoryError.Location;

public class DrMemoryParser {

public enum DrMemoryErrorType {
UNADRESSABLE_ACCESS("UnadressableAccess", "UNADDRESSABLE ACCESS"),
UNINITIALIZE_READ("UninitializedRead", "UNINITIALIZED READ"),
INVALID_HEAP_ARGUMENT("InvalidHeapArgument", "INVALID HEAP ARGUMENT"),
GDI_USAGE_ERROR("GdiUsageError", "GDI Usage Error"),
HANDLE_LEAK("HandleLeak", "HANDLE LEAK"),
WARNING("DrMemoryWarning", "WARNING"),
POSSIBLE_LEAK("PossibleMemoryLeak", "POSSIBLE LEAK"),
LEAK("MemoryLeak", "LEAK"),
UNRECOGNIZED("Dr Memory unrecognized error", "");

private String id;
private String title;

DrMemoryErrorType(String id, String title) {
this.id = id;
this.title = title;
}

public String getId() {
return id;
}

public String getTitle() {
return title;
}
}

public static class DrMemoryError {

public static class Location {
public String file = "";
public Integer line;
}

public DrMemoryErrorType type = DrMemoryErrorType.UNRECOGNIZED;
public class DrMemoryParser {
private static final Logger LOG = Loggers.get(DrMemoryParser.class);
public static final Pattern rx_message_finder = Pattern.compile("^Error #\\d+:(.*)");
public static final Pattern rx_file_finder = Pattern.compile("^.*\\[(.*):(\\d+)\\]$");
public static final int TOP_COUNT = 4;

public enum DrMemoryErrorType {
UNADRESSABLE_ACCESS("UnadressableAccess", "UNADDRESSABLE ACCESS"),
UNINITIALIZE_READ("UninitializedRead", "UNINITIALIZED READ"),
INVALID_HEAP_ARGUMENT("InvalidHeapArgument", "INVALID HEAP ARGUMENT"),
GDI_USAGE_ERROR("GdiUsageError", "GDI Usage Error"),
HANDLE_LEAK("HandleLeak", "HANDLE LEAK"),
WARNING("DrMemoryWarning", "WARNING"),
POSSIBLE_LEAK("PossibleMemoryLeak", "POSSIBLE LEAK"),
LEAK("MemoryLeak", "LEAK"),
UNRECOGNIZED("Dr Memory unrecognized error", "");

private String id;
private String title;

DrMemoryErrorType(String id, String title) {
this.id = id;
this.title = title;
}

public String getId() {
return id;
}

public String getTitle() {
return title;
}
}

public static class DrMemoryError {

public static class Location {
public String file = "";
public Integer line;
}

public DrMemoryErrorType type = DrMemoryErrorType.UNRECOGNIZED;
public List<Location> stackTrace = new ArrayList<>();
public String message = "";
}

private DrMemoryParser() {
}
public String message = "";
}

public static final Pattern rx_message_finder = Pattern.compile( "^Error #\\d+:(.*)");
public static final Pattern rx_file_finder = Pattern.compile( "^.*\\[(.*):(\\d+)\\]$");
private DrMemoryParser() {
}

public static final int __TOP_COUNT = 4;

public static List<DrMemoryError> parse(File file, String charset) {

List<DrMemoryError> result = new ArrayList<>();

List<String> elements = getElements(file, charset);
for (String element : elements) {
Matcher m = rx_message_finder.matcher( element );
if( m.find() ) {
DrMemoryError error = new DrMemoryError();
error.type = extractErrorType(m.group(1));

for (String element : elements) {
Matcher m = rx_message_finder.matcher(element);

if (m.find()) {
DrMemoryError error = new DrMemoryError();
error.type = extractErrorType(m.group(1));
String[] elementSplitted = element.split("\\r?\\n");
error.message = elementSplitted[0];
for (String elementPart : elementSplitted) {
Matcher locationMatcher = rx_file_finder.matcher( elementPart );
if (locationMatcher.find()) {
Location location = new Location();
location.file = locationMatcher.group( 1 );
location.line = Integer.valueOf(locationMatcher.group( 2 ));
error.stackTrace.add(location);
}
}
result.add(error);

}
}

return result;
}


private static DrMemoryErrorType extractErrorType(String title) {
String cleanedTitle = clean(title);
for (DrMemoryErrorType drMemoryErrorType : DrMemoryErrorType.values()) {
if (cleanedTitle.startsWith(drMemoryErrorType.getTitle())) {
return drMemoryErrorType;
}
}
return DrMemoryErrorType.UNRECOGNIZED;
}


private static String clean(String title) {
return title.trim();
}

public static List<String> getElements(File file, String charset){
error.message = elementSplitted[0];
for (String elementPart : elementSplitted) {
Matcher locationMatcher = rx_file_finder.matcher(elementPart);
if (locationMatcher.find()) {
Location location = new Location();
location.file = locationMatcher.group(1);
location.line = Integer.valueOf(locationMatcher.group(2));
error.stackTrace.add(location);
}
}
result.add(error);

}
}

return result;
}

private static DrMemoryErrorType extractErrorType(String title) {
String cleanedTitle = clean(title);
for (DrMemoryErrorType drMemoryErrorType : DrMemoryErrorType.values()) {
if (cleanedTitle.startsWith(drMemoryErrorType.getTitle())) {
return drMemoryErrorType;
}
}
return DrMemoryErrorType.UNRECOGNIZED;
}

private static String clean(String title) {
return title.trim();
}

public static List<String> getElements(File file, String charset) {

List<String> list = new ArrayList<>();
try (FileInputStream input = new FileInputStream(file)) {
BufferedReader br = new BufferedReader(new InputStreamReader(input, charset));
StringBuilder sb = new StringBuilder();
String line;
int cnt = 0;
while( ( line = br.readLine() ) != null ) {
if( cnt > ( __TOP_COUNT ) ) {
if( line.matches( "^\\s*$" ) ) {
list.add( sb.toString() );
sb.setLength( 0 );
} else {
StringBuilder sb = new StringBuilder();
String line;
int cnt = 0;

while ((line = br.readLine()) != null) {
if (cnt > (TOP_COUNT)) {
if (line.matches("^\\s*$")) {
list.add(sb.toString());
sb.setLength(0);
} else {
sb.append(line);
sb.append('\n');
}
}
cnt++;
}
if( sb.length() > 0 ) {
list.add( sb.toString() );
}
br.close();
}
}
cnt++;
}

if (sb.length() > 0) {
list.add(sb.toString());
}

br.close();
input.close();
} catch (IOException e) { //NOSONAR
} catch (IOException e) {
String msg = new StringBuilder().append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'").toString();
LOG.error(msg);
}
return list;
}
return list;
}
}

0 comments on commit 3a8d79a

Please sign in to comment.