Skip to content

Commit

Permalink
Revert "- Removing metric definitions from this project"
Browse files Browse the repository at this point in the history
This reverts commit 008fd89.
  • Loading branch information
ericlemes committed Jun 8, 2018
1 parent 008fd89 commit ac0bb4d
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 53 deletions.
5 changes: 0 additions & 5 deletions cxx-sensors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,5 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonar.opencommunity.metrics</groupId>
<artifactId>sonar-opencommunity-metrics</artifactId>
<version>0.9.9</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +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.opencommunity.metrics.OpenCommunityMetrics;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
Expand Down Expand Up @@ -139,13 +138,13 @@ private void publishComplexFunctionMetricsForFile(InputFile inputFile, SourceFil
}

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS)
.forMetric(FunctionComplexityMetrics.COMPLEX_FUNCTIONS)
.on(inputFile)
.withValue((int)c.countOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_PERC)
.forMetric(FunctionComplexityMetrics.PERC_COMPLEX_FUNCTIONS)
.on(inputFile)
.withValue(calculatePercentual((int)c.countOverThreshold, (int)c.countBelowThreshold))
.save();
Expand All @@ -161,46 +160,47 @@ private void publishLocInComplexFunctionMetricsForFile(InputFile inputFile, Sour
}

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_LOC)
.forMetric(FunctionComplexityMetrics.LOC_IN_COMPLEX_FUNCTIONS)
.on(inputFile)
.withValue(locCount.countOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_LOC_PERC)
.forMetric(FunctionComplexityMetrics.PERC_LOC_IN_COMPLEX_FUNCTIONS)
.on(inputFile)
.withValue(calculatePercentual((int)locCount.countOverThreshold, (int)locCount.countBelowThreshold))
.save();
}

@Override
public void publishMeasureForProject(InputModule module, SensorContext context) {

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

private void publishComplexFunctionMetrics(InputModule module, SensorContext context){
context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS)
.forMetric(FunctionComplexityMetrics.COMPLEX_FUNCTIONS)
.on(module)
.withValue(functionsOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_PERC)
.forMetric(FunctionComplexityMetrics.PERC_COMPLEX_FUNCTIONS)
.on(module)
.withValue(calculatePercentual(functionsOverThreshold, functionsBelowThreshold))
.save();
}

private void publishLinesOfCodeInComplexFunctionMetrics(InputModule module, SensorContext context){
context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_LOC)
.forMetric(FunctionComplexityMetrics.LOC_IN_COMPLEX_FUNCTIONS)
.on(module)
.withValue(linesOfCodeOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.COMPLEX_FUNCTIONS_LOC_PERC)
.forMetric(FunctionComplexityMetrics.PERC_LOC_IN_COMPLEX_FUNCTIONS)
.on(module)
.withValue(calculatePercentual(linesOfCodeOverThreshold, linesOfCodeBelowThreshold))
.save();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.sensors.functioncomplexity;

import static java.util.Arrays.asList;
import java.util.List;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;

public class FunctionComplexityMetrics implements Metrics {

public static final Metric<Integer> COMPLEX_FUNCTIONS = new Metric.Builder("complex_functions", "Complex Functions", Metric.ValueType.INT)
.setDescription("Number of functions with high cyclomatic complexity")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_COMPLEXITY)
.create();

public static final Metric<Double> PERC_COMPLEX_FUNCTIONS = new Metric.Builder("perc_complex_functions", "% of Complex Functions", Metric.ValueType.PERCENT)
.setDescription("% of functions with high cyclomatic complexity")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_COMPLEXITY)
.create();

public static final Metric<Integer> LOC_IN_COMPLEX_FUNCTIONS = new Metric.Builder("loc_in_complex_functions", "LoC in Complex Functions", Metric.ValueType.INT)
.setDescription("Number of lines of code in functions with high cyclomatic complexity")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_COMPLEXITY)
.create();

public static final Metric<Double> PERC_LOC_IN_COMPLEX_FUNCTIONS = new Metric.Builder("perc_loc_in_complex_functions", "% of LoC in Complex Functions", Metric.ValueType.PERCENT)
.setDescription("% of lines of code in functions with high cyclomatic complexity")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_COMPLEXITY)
.create();

@Override
public List<Metric> getMetrics() {
return asList(COMPLEX_FUNCTIONS, PERC_COMPLEX_FUNCTIONS, LOC_IN_COMPLEX_FUNCTIONS, PERC_LOC_IN_COMPLEX_FUNCTIONS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import org.sonar.cxx.CxxLanguage;
import static org.sonar.cxx.checks.TooManyLinesOfCodeInFunctionCheck.getNumberOfLine;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.sensors.functioncomplexity.FunctionComplexityMetrics;
import org.sonar.cxx.sensors.functioncomplexity.FunctionCount;
import org.sonar.cxx.sensors.functioncomplexity.FunctionScore;
import org.sonar.cxx.sensors.functioncomplexity.FunctionScoreComparator;
import org.sonar.cxx.sensors.squid.SquidSensor;
import org.sonar.opencommunity.metrics.OpenCommunityMetrics;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
Expand Down Expand Up @@ -128,32 +128,39 @@ public void publishMeasureForFile(InputFile inputFile, SourceFile squidFile, Sen

@Override
public void publishMeasureForProject(InputModule module, SensorContext context) {

publishBigFunctionCountForProject(module, context);
publishLocInBigFunctionForProject(module, context);
}

private void publishBigFunctionCountForProject(InputModule module, SensorContext context){
context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS)
.forMetric(FunctionSizeMetrics.BIG_FUNCTIONS)
.on(module)
.withValue(functionsOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_PERC)
.forMetric(FunctionSizeMetrics.PERC_BIG_FUNCTIONS)
.on(module)
.withValue(calculatePercentual(functionsOverThreshold, functionsBelowThreshold))
.save();
}

private void publishLocInBigFunctionForProject(InputModule module, SensorContext context){
private void publishLocInBigFunctionForProject(InputModule module, SensorContext context){
context.<Integer>newMeasure()
.forMetric(FunctionSizeMetrics.LOC_IN_FUNCTIONS)
.on(module)
.withValue(locOverThreshold + locBelowThreshold)
.save();

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_LOC)
.forMetric(FunctionSizeMetrics.LOC_IN_BIG_FUNCTIONS)
.on(module)
.withValue(locOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_LOC_PERC)
.forMetric(FunctionSizeMetrics.PERC_LOC_IN_BIG_FUNCTIONS)
.on(module)
.withValue(calculatePercentual(locOverThreshold, locBelowThreshold))
.save();
Expand All @@ -176,13 +183,13 @@ private void publishBigFunctionMetrics(InputFile inputFile, SourceFile squidFile
}

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS)
.forMetric(FunctionSizeMetrics.BIG_FUNCTIONS)
.on(inputFile)
.withValue((int)c.countOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_PERC)
.forMetric(FunctionSizeMetrics.PERC_BIG_FUNCTIONS)
.on(inputFile)
.withValue(calculatePercentual(c.countOverThreshold, c.countBelowThreshold))
.save();
Expand All @@ -197,19 +204,19 @@ private void publishLocInBigFunctionMetrics(InputFile inputFile, SourceFile squi
}

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.LOC_IN_FUNCTIONS)
.forMetric(FunctionSizeMetrics.LOC_IN_FUNCTIONS)
.on(inputFile)
.withValue(c.countOverThreshold + c.countBelowThreshold)
.save();

context.<Integer>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_LOC)
.forMetric(FunctionSizeMetrics.LOC_IN_BIG_FUNCTIONS)
.on(inputFile)
.withValue(c.countOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(OpenCommunityMetrics.BIG_FUNCTIONS_LOC_PERC)
.forMetric(FunctionSizeMetrics.PERC_LOC_IN_BIG_FUNCTIONS)
.on(inputFile)
.withValue(calculatePercentual(c.countOverThreshold, c.countBelowThreshold))
.save();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.cxx.sensors.functionsize;

import static java.util.Arrays.asList;
import java.util.List;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;

public class FunctionSizeMetrics implements Metrics {

public static final Metric<Integer> BIG_FUNCTIONS = new Metric.Builder("big_functions", "Big Functions", Metric.ValueType.INT)
.setDescription("Number of functions with too many lines")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_SIZE)
.create();

public static final Metric<Integer> LOC_IN_BIG_FUNCTIONS = new Metric.Builder("loc_in_big_functions", "LoC in Big Functions", Metric.ValueType.INT)
.setDescription("Number of lines of code in functions with too many lines")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_SIZE)
.create();

public static final Metric<Double> PERC_BIG_FUNCTIONS = new Metric.Builder("perc_big_functions", "% of Big Functions", Metric.ValueType.PERCENT)
.setDescription("% of functions with too many lines")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_SIZE)
.create();

public static final Metric<Double> PERC_LOC_IN_BIG_FUNCTIONS = new Metric.Builder("perc_loc_in_big_functions", "% of LoC in Big Functions", Metric.ValueType.PERCENT)
.setDescription("% of lines of code in functions with too many lines")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_SIZE)
.create();

public static final Metric<Integer> LOC_IN_FUNCTIONS = new Metric.Builder("loc_in_functions", "LoC in Functions", Metric.ValueType.INT)
.setDescription("Number of lines of code in function bodies")
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(false)
.setDomain(CoreMetrics.DOMAIN_SIZE)
.create();

@Override
public List<Metric> getMetrics() {
return asList(BIG_FUNCTIONS, PERC_BIG_FUNCTIONS, LOC_IN_BIG_FUNCTIONS, PERC_LOC_IN_BIG_FUNCTIONS, LOC_IN_FUNCTIONS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ public CxxSquidSensor(CxxLanguage language,
registerSquidSensors();
}

protected void registerSquidSensors(){
this.squidSensors.add(new CxxFunctionComplexitySquidSensor(this.language));
this.squidSensors.add(new CxxFunctionSizeSquidSensor(this.language));
protected void registerSquidSensors(){
if (this.language.getKey() == "c++"){
this.squidSensors.add(new CxxFunctionComplexitySquidSensor(this.language));
this.squidSensors.add(new CxxFunctionSizeSquidSensor(this.language));
}
}

@Override
Expand Down
Loading

0 comments on commit ac0bb4d

Please sign in to comment.