Skip to content

cache TooManyLinesOfCodeInFunctionCheck.getNumberOfLine #1503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.api.CppPunctuator;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.api.SourceFunction;
import org.sonar.squidbridge.checks.SquidCheck;

@Rule(key = "TooManyLinesOfCodeInFunction",
Expand All @@ -56,7 +58,8 @@ public void init() {

@Override
public void leaveNode(AstNode node) {
int lineCount = getNumberOfLine(node);
SourceFunction sourceFunction = (SourceFunction) getContext().peekSourceCode();
int lineCount = sourceFunction.getInt(CxxMetric.LINES_OF_CODE_IN_FUNCTION_BODY);
if (lineCount > max) {
getContext().createLineViolation(this,
"The number of code lines in this function is {0,number,integer} which is greater than "
Expand All @@ -65,31 +68,6 @@ public void leaveNode(AstNode node) {
}
}

/**
* aggregates lines for child nodes
*
* @param node with child nodes
* @return number of lines
*/
public static int getNumberOfLine(AstNode node) {
List<AstNode> allChilds = node.getDescendants(CxxGrammarImpl.statement, CppPunctuator.CURLBR_LEFT,
CppPunctuator.CURLBR_RIGHT);
int lines = 1;
int firstLine = node.getTokenLine();
if (allChilds != null && !allChilds.isEmpty()) {
int previousLine = firstLine;
int currentLine = 0;
for (AstNode child : allChilds) {
currentLine = child.getTokenLine();
if (currentLine != previousLine) {
lines++;
previousLine = currentLine;
}
}
}
return lines;
}

public void setMax(int max) {
this.max = max;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.api.CxxMetric;
import static org.sonar.cxx.checks.TooManyLinesOfCodeInFunctionCheck.getNumberOfLine;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.sensors.squid.SquidSensor;
import org.sonar.squidbridge.SquidAstVisitor;
Expand Down Expand Up @@ -73,16 +72,16 @@ public SquidAstVisitor<Grammar> getVisitor() {

@Override
public void init() {
subscribeTo(CxxGrammarImpl.functionDefinition);
subscribeTo(CxxGrammarImpl.functionBody);
}

@Override
public void leaveNode(AstNode node) {
SourceFunction sourceFunction = (SourceFunction) getContext().peekSourceCode();
SourceFile sourceFile = (SourceFile)sourceFunction.getAncestor(SourceFile.class);
SourceFile sourceFile = sourceFunction.getAncestor(SourceFile.class);

int complexity = ChecksHelper.getRecursiveMeasureInt(sourceFunction, CxxMetric.COMPLEXITY);
int lineCount = getNumberOfLine(node);
int lineCount = sourceFunction.getInt(CxxMetric.LINES_OF_CODE_IN_FUNCTION_BODY);

incrementFunctionByThresholdForAllFiles(complexity, lineCount);
incrementFunctionByThresholdForFile(sourceFile, complexity, lineCount);
Expand Down Expand Up @@ -137,13 +136,13 @@ private void publishComplexFunctionMetricsForFile(InputFile inputFile, SourceFil
context.<Integer>newMeasure()
.forMetric(FunctionComplexityMetrics.COMPLEX_FUNCTIONS)
.on(inputFile)
.withValue((int)c.countOverThreshold)
.withValue(c.countOverThreshold)
.save();

context.<Double>newMeasure()
.forMetric(FunctionComplexityMetrics.COMPLEX_FUNCTIONS_PERC)
.on(inputFile)
.withValue(calculatePercentual((int)c.countOverThreshold, (int)c.countBelowThreshold))
.withValue(calculatePercentual(c.countOverThreshold, c.countBelowThreshold))
.save();
}

Expand All @@ -165,7 +164,7 @@ private void publishLocInComplexFunctionMetricsForFile(InputFile inputFile, Sour
context.<Double>newMeasure()
.forMetric(FunctionComplexityMetrics.COMPLEX_FUNCTIONS_LOC_PERC)
.on(inputFile)
.withValue(calculatePercentual((int)locCount.countOverThreshold, (int)locCount.countBelowThreshold))
.withValue(calculatePercentual(locCount.countOverThreshold, locCount.countBelowThreshold))
.save();
}

Expand Down Expand Up @@ -206,7 +205,7 @@ private void publishLinesOfCodeInComplexFunctionMetrics(InputModule module, Sens
private double calculatePercentual(int overThreshold, int belowThreshold){
double total = (double)overThreshold + (double)belowThreshold;
if (total > 0) {
return ((float)overThreshold * 100.0) / ((float)overThreshold + (float)belowThreshold);
return (overThreshold * 100.0) / ((double)overThreshold + (double)belowThreshold);
}
else {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;
import static org.sonar.cxx.checks.TooManyLinesOfCodeInFunctionCheck.getNumberOfLine;
import org.sonar.cxx.api.CxxMetric;

import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.sensors.functioncomplexity.FunctionCount;
import org.sonar.cxx.sensors.squid.SquidSensor;
Expand Down Expand Up @@ -71,9 +72,9 @@ public void init() {
@Override
public void leaveNode(AstNode node) {
SourceFunction sourceFunction = (SourceFunction) getContext().peekSourceCode();
SourceFile sourceFile = (SourceFile)sourceFunction.getAncestor(SourceFile.class);
SourceFile sourceFile = sourceFunction.getAncestor(SourceFile.class);

int lineCount = getNumberOfLine(node);
int lineCount = sourceFunction.getInt(CxxMetric.LINES_OF_CODE_IN_FUNCTION_BODY);

incrementFunctionByThresholdForProject(lineCount);
incrementFunctionByThresholdForFile(sourceFile, lineCount);
Expand Down Expand Up @@ -166,7 +167,7 @@ private void publishLocInBigFunctionForProject(InputModule module, SensorContext
private double calculatePercentual(int overThreshold, int belowThreshold){
double total = (double)overThreshold + (double)belowThreshold;
if (total > 0) {
return ((float)overThreshold * 100.0) / ((float)overThreshold + (float)belowThreshold);
return (overThreshold * 100.0) / ((double)overThreshold + (double)belowThreshold);
}
else {
return 0;
Expand All @@ -184,7 +185,7 @@ private void publishBigFunctionMetrics(InputFile inputFile, SourceFile squidFile
context.<Integer>newMeasure()
.forMetric(FunctionSizeMetrics.BIG_FUNCTIONS)
.on(inputFile)
.withValue((int)c.countOverThreshold)
.withValue(c.countOverThreshold)
.save();

context.<Double>newMeasure()
Expand Down
2 changes: 2 additions & 0 deletions cxx-squid/src/main/java/org/sonar/cxx/CxxAstScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor;
import org.sonar.cxx.visitors.CxxCognitiveComplexityVisitor;
import org.sonar.cxx.visitors.CxxFileVisitor;
import org.sonar.cxx.visitors.CxxLinesOfCodeInFunctionBodyVisitor;
import org.sonar.cxx.visitors.CxxLinesOfCodeVisitor;
import org.sonar.cxx.visitors.CxxParseErrorLoggerVisitor;
import org.sonar.cxx.visitors.CxxPublicApiVisitor;
Expand Down Expand Up @@ -199,6 +200,7 @@ public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode)
/* Metrics */
builder.withSquidAstVisitor(new LinesVisitor<>(CxxMetric.LINES));
builder.withSquidAstVisitor(new CxxLinesOfCodeVisitor<>(CxxMetric.LINES_OF_CODE));
builder.withSquidAstVisitor(new CxxLinesOfCodeInFunctionBodyVisitor<>());
builder.withSquidAstVisitor(new CxxPublicApiVisitor<>(CxxMetric.PUBLIC_API,
CxxMetric.PUBLIC_UNDOCUMENTED_API)
.withHeaderFileSuffixes(conf.getHeaderFileSuffixes()));
Expand Down
1 change: 1 addition & 0 deletions cxx-squid/src/main/java/org/sonar/cxx/api/CxxMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum CxxMetric implements MetricDef {
FILES,
LINES,
LINES_OF_CODE,
LINES_OF_CODE_IN_FUNCTION_BODY,
STATEMENTS,
FUNCTIONS,
CLASSES,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2018 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.visitors;

import java.util.List;

import org.sonar.cxx.api.CppPunctuator;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.squidbridge.SquidAstVisitor;

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AstVisitor;
import com.sonar.sslr.api.Grammar;

/**
* Visitor that computes the NCLOCs in function body, leading and trailing {} do not count
*
* @param <GRAMMAR>
*/
public class CxxLinesOfCodeInFunctionBodyVisitor<GRAMMAR extends Grammar> extends SquidAstVisitor<GRAMMAR>
implements AstVisitor {

@Override
public void init() {
subscribeTo(CxxGrammarImpl.functionBody);
}

@Override
public void visitNode(AstNode node) {
List<AstNode> allChilds = node.getDescendants(CxxGrammarImpl.statement, CppPunctuator.CURLBR_LEFT,
CppPunctuator.CURLBR_RIGHT);
int lines = 1;
int firstLine = node.getTokenLine();
if (allChilds != null && !allChilds.isEmpty()) {
int previousLine = firstLine;
for (AstNode child : allChilds) {
int currentLine = child.getTokenLine();
if (currentLine != previousLine) {
lines++;
previousLine = currentLine;
}
}
}
getContext().peekSourceCode().add(CxxMetric.LINES_OF_CODE_IN_FUNCTION_BODY, lines);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CxxMetricTest {
@Test
public void test() {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(CxxMetric.values()).hasSize(11);
softly.assertThat(CxxMetric.values()).hasSize(12);

for (CxxMetric metric : CxxMetric.values()) {
softly.assertThat(metric.getName()).isEqualTo(metric.name());
Expand Down