Skip to content

Commit

Permalink
Removing the Scanner side file dumps.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlemes committed Jan 31, 2018
1 parent dafcc7f commit d9f2a2f
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import org.sonar.cxx.parser.CxxGrammarImpl;
import static org.sonar.cxx.sensors.clangtidy.CxxClangTidySensor.REPORT_PATH_KEY;
import org.sonar.cxx.sensors.squid.SquidSensor;
import org.sonar.cxx.sensors.utils.FileStreamFactory;
import org.sonar.cxx.sensors.utils.StreamFactory;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
Expand All @@ -50,8 +48,7 @@ public class CxxFunctionComplexitySquidSensor extends SquidAstVisitor<Grammar> i

private static final Logger LOG = Loggers.get(CxxFunctionComplexitySquidSensor.class);

public static final String FUNCTION_COMPLEXITY_THRESHOLD_KEY = "funccomplexity.threshold";
public static final String FUNCTION_COMPLEXITY_FILE_NAME_KEY = "funccomplexity.filename";
public static final String FUNCTION_COMPLEXITY_THRESHOLD_KEY = "funccomplexity.threshold";

private int cyclomaticComplexityThreshold;

Expand All @@ -67,28 +64,12 @@ public class CxxFunctionComplexitySquidSensor extends SquidAstVisitor<Grammar> i

private Hashtable<SourceFile, FunctionCount> locInComplexFunctionsPerFile = new Hashtable<>();

private String fileName;

private StreamFactory streamFactory;

private TreeSet<FunctionScore> rankedList = new TreeSet<FunctionScore>(new FunctionScoreComparator());
public SortedSet<FunctionScore> getRankedList(){
return this.rankedList;
}
private String fileName;

public CxxFunctionComplexitySquidSensor(CxxLanguage language){
this.cyclomaticComplexityThreshold = language.getIntegerOption(FUNCTION_COMPLEXITY_THRESHOLD_KEY).orElse(10);
LOG.debug("Cyclomatic complexity threshold: " + this.cyclomaticComplexityThreshold);

this.fileName = language.getStringOption(FUNCTION_COMPLEXITY_FILE_NAME_KEY).orElse("");
LOG.debug("File name to dump CC data: " + this.fileName);

this.streamFactory = new FileStreamFactory();
}

public void setFileStreamFactory(StreamFactory factory){
this.streamFactory = factory;
}
LOG.debug("Cyclomatic complexity threshold: " + this.cyclomaticComplexityThreshold);
}

@Override
public SquidAstVisitor<Grammar> getVisitor() {
Expand All @@ -109,37 +90,8 @@ public void leaveNode(AstNode node) {
int lineCount = getNumberOfLine(node);

incrementFunctionByThresholdForAllFiles(complexity, lineCount);
incrementFunctionByThresholdForFile(sourceFile, complexity, lineCount);
appendRankedList(sourceFunction, complexity);
}

private void appendRankedList(SourceFunction sourceFunction, int complexity){
if (fileName.equals(""))
return;

FunctionScore score = new FunctionScore(complexity, getContext().getFile().getName(), sourceFunction.getKey());
this.rankedList.add(score);
}

private void writeScore(OutputStream stream, FunctionScore score) throws IOException{
stream.write((score.getComponentName() + "\t" + score.getFunctionId() + "\t" + score.getScore() + System.lineSeparator()).getBytes());
}

private void dumpRankedList(){
if (fileName.equals(""))
return;

try {
OutputStream stream = streamFactory.createOutputFileStream(this.fileName);
for(FunctionScore score : rankedList)
writeScore(stream, score);
stream.flush();
stream.close();
}
catch (Exception e){
LOG.error("Couldn't write ranked list to " + fileName + ". Exception text: " + e.getMessage());
}
}
incrementFunctionByThresholdForFile(sourceFile, complexity, lineCount);
}

private void incrementFunctionByThresholdForAllFiles(int complexity, int lineCount){
if (complexity > this.cyclomaticComplexityThreshold){
Expand Down Expand Up @@ -217,9 +169,7 @@ private void publishLocInComplexFunctionMetricsForFile(InputFile inputFile, Sour
@Override
public void publishMeasureForProject(InputModule module, SensorContext context) {
publishComplexFunctionMetrics(module, context);
publishLinesOfCodeInComplexFunctionMetrics(module, context);

dumpRankedList();
publishLinesOfCodeInComplexFunctionMetrics(module, context);
}

private void publishComplexFunctionMetrics(InputModule module, SensorContext context){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import org.sonar.cxx.sensors.functioncomplexity.FunctionScore;
import org.sonar.cxx.sensors.functioncomplexity.FunctionScoreComparator;
import org.sonar.cxx.sensors.squid.SquidSensor;
import org.sonar.cxx.sensors.utils.FileStreamFactory;
import org.sonar.cxx.sensors.utils.StreamFactory;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
Expand All @@ -50,7 +48,6 @@ public class CxxFunctionSizeSquidSensor extends SquidAstVisitor<Grammar> impleme
private static final Logger LOG = Loggers.get(CxxFunctionSizeSquidSensor.class);

public static final String FUNCTION_SIZE_THRESHOLD_KEY = "funcsize.threshold";
public static final String FUNCTION_SIZE_FILE_NAME_KEY = "funcsize.filename";

private int functionsBelowThreshold;

Expand All @@ -64,29 +61,11 @@ public class CxxFunctionSizeSquidSensor extends SquidAstVisitor<Grammar> impleme

private Hashtable<SourceFile, FunctionCount> bigFunctionsPerFile = new Hashtable<>();

private Hashtable<SourceFile, FunctionCount> locInBigFunctionsPerFile = new Hashtable<>();

private String fileName;

private StreamFactory streamFactory;

private TreeSet<FunctionScore> rankedList = new TreeSet<FunctionScore>(new FunctionScoreComparator());
public SortedSet<FunctionScore> getRankedList(){
return this.rankedList;
}

public void setFileStreamFactory(StreamFactory factory){
this.streamFactory = factory;
}
private Hashtable<SourceFile, FunctionCount> locInBigFunctionsPerFile = new Hashtable<>();

public CxxFunctionSizeSquidSensor(CxxLanguage language){
this.sizeThreshold = language.getIntegerOption(FUNCTION_SIZE_THRESHOLD_KEY).orElse(20);
LOG.debug("Function size threshold: " + this.sizeThreshold);

this.fileName = language.getStringOption(FUNCTION_SIZE_FILE_NAME_KEY).orElse("");
LOG.debug("File name to dump function size data: " + this.fileName);

this.streamFactory = new FileStreamFactory();
}

@Override
Expand All @@ -102,38 +81,9 @@ public void leaveNode(AstNode node) {
int lineCount = getNumberOfLine(node);

incrementFunctionByThresholdForProject(lineCount);
incrementFunctionByThresholdForFile(sourceFile, lineCount);
appendRankedList(sourceFunction, lineCount);
incrementFunctionByThresholdForFile(sourceFile, lineCount);
}

private void appendRankedList(SourceFunction sourceFunction, int complexity){
if (fileName.equals(""))
return;

FunctionScore score = new FunctionScore(complexity, getContext().getFile().getName(), sourceFunction.getKey());
this.rankedList.add(score);
}

private void writeScore(OutputStream stream, FunctionScore score) throws IOException{
stream.write((score.getComponentName() + "\t" + score.getFunctionId() + "\t" + score.getScore() + System.lineSeparator()).getBytes());
}

private void dumpRankedList(){
if (fileName.equals(""))
return;

try {
OutputStream stream = streamFactory.createOutputFileStream(this.fileName);
for(FunctionScore score : rankedList)
writeScore(stream, score);
stream.flush();
stream.close();
}
catch (Exception e){
LOG.error("Couldn't write ranked list to " + fileName + ". Exception text: " + e.getMessage());
}
}

private void incrementFunctionByThresholdForFile(SourceFile sourceFile, int lineCount){
if (!bigFunctionsPerFile.containsKey(sourceFile))
bigFunctionsPerFile.put(sourceFile, new FunctionCount());
Expand Down Expand Up @@ -180,7 +130,6 @@ public void publishMeasureForFile(InputFile inputFile, SourceFile squidFile, Sen
public void publishMeasureForProject(InputModule module, SensorContext context) {
publishBigFunctionCountForProject(module, context);
publishLocInBigFunctionForProject(module, context);
dumpRankedList();
}

private void publishBigFunctionCountForProject(InputModule module, SensorContext context){
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.sonar.cxx.CxxAstScanner;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.sensors.squid.CxxSquidSensor;
import org.sonar.cxx.sensors.utils.StreamFactory;
import org.sonar.cxx.sensors.utils.TestUtils;
import org.sonar.squidbridge.api.SourceFile;

Expand Down Expand Up @@ -123,25 +122,5 @@ public void testPublishMeasuresForFile() throws IOException {
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionComplexityMetrics.LOC_IN_COMPLEX_FUNCTIONS)).isEqualTo(44);
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionComplexityMetrics.PERC_COMPLEX_FUNCTIONS)).isEqualTo(40.0);
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionComplexityMetrics.PERC_LOC_IN_COMPLEX_FUNCTIONS)).isEqualTo(80);
}

@Test
public void testSaveRankedListToFile() throws IOException{
DefaultInputFile inputFile = getInputFile();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamFactory streamFactory = mock(StreamFactory.class);
when(streamFactory.createOutputFileStream("complex_functions.txt")).thenReturn(outputStream);

when(language.getStringOption(CxxFunctionComplexitySquidSensor.FUNCTION_COMPLEXITY_FILE_NAME_KEY)).thenReturn(Optional.of("complex_functions.txt"));

this.sensor = new CxxFunctionComplexitySquidSensor(this.language);
SourceFile squidFile = CxxAstScanner.scanSingleFile(inputFile, sensorContext, TestUtils.mockCxxLanguage(), sensor.getVisitor());
sensor.setFileStreamFactory(streamFactory);
sensor.publishMeasureForProject(sensorContext.module(), sensorContext);

String fileData = new String(outputStream.toByteArray(), "UTF-8");
assertThat(fileData).isNotEmpty();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.cxx.CxxAstScanner;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.sensors.functioncomplexity.CxxFunctionComplexitySquidSensor;
import org.sonar.cxx.sensors.functioncomplexity.FunctionComplexityMetrics;
import org.sonar.cxx.sensors.utils.StreamFactory;
import org.sonar.cxx.sensors.utils.TestUtils;
import org.sonar.squidbridge.api.SourceFile;

Expand Down Expand Up @@ -121,25 +118,5 @@ public void testPublishMeasuresForFile() throws IOException {
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionSizeMetrics.LOC_IN_BIG_FUNCTIONS)).isEqualTo(44);
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionSizeMetrics.PERC_BIG_FUNCTIONS)).isEqualTo(40.0);
assertThat(getMeasureValue(sensorContext, inputFile.key(), FunctionSizeMetrics.PERC_LOC_IN_BIG_FUNCTIONS)).isEqualTo(80);
}

@Test
public void testSaveRankedListToFile() throws IOException{
DefaultInputFile inputFile = getInputFile();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamFactory streamFactory = mock(StreamFactory.class);
when(streamFactory.createOutputFileStream("big_functions.txt")).thenReturn(outputStream);

when(language.getStringOption(CxxFunctionSizeSquidSensor.FUNCTION_SIZE_FILE_NAME_KEY)).thenReturn(Optional.of("big_functions.txt"));

this.sensor = new CxxFunctionSizeSquidSensor(this.language);
SourceFile squidFile = CxxAstScanner.scanSingleFile(inputFile, sensorContext, TestUtils.mockCxxLanguage(), sensor.getVisitor());
sensor.setFileStreamFactory(streamFactory);
sensor.publishMeasureForProject(sensorContext.module(), sensorContext);

String fileData = new String(outputStream.toByteArray(), "UTF-8");
assertThat(fileData).isNotEmpty();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void define(Context context) {
l.addAll(codeAnalysisProperties());
l.addAll(testingAndCoverageProperties());
l.addAll(compilerWarningsProperties());
l.addAll(duplicationsProperties());
l.addAll(duplicationsProperties());

context.addExtensions(l);
}
Expand Down

0 comments on commit d9f2a2f

Please sign in to comment.