Skip to content
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

limit report line number #611

Closed
wants to merge 1 commit into from
Closed
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 @@ -52,6 +52,7 @@ public abstract class CxxReportSensor implements Sensor {
private ResourcePerspectives perspectives;
private Set<String> notFoundFiles = new HashSet<String>();
private Set<String> uniqueIssues = new HashSet<String>();
private HashMap<String, Integer> maxLineFiles = new HashMap<String, Integer>();
private final Metric metric;
private int violationsCount;

Expand Down Expand Up @@ -239,6 +240,21 @@ private boolean saveViolation(Project project, SensorContext context, String rul
= org.sonar.api.resources.File.fromIOFile(new File(normalPath), project);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromIOFile is deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html. So we should use something like:

InputFile file= fs.inputFile(fs.predicates().is(new File(filename)));
int maxLine = file.lines();

InputFile has method lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance needs to be measured...

On Thu, 3 Sep 2015 at 14:52 Günter Wirth [email protected] wrote:

In
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java
#611 (comment):

@@ -239,6 +240,21 @@ private boolean saveViolation(Project project, SensorContext context, String rul
= org.sonar.api.resources.File.fromIOFile(new File(normalPath), project);

fromIOFile is deprecated, see
http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html. So we
should use something like:

InputFile file= fs.inputFile(fs.predicates().is(new File(filename)));int maxLine = file.lines();

InputFile has method lines.


Reply to this email directly or view it on GitHub
https://github.com/wenns/sonar-cxx/pull/611/files#r38637121.

if (context.getResource(file) != null) {
lineNr = getLineAsInt(line);
int maxLine = lineNr;
if (maxLineFiles.containsKey(normalPath)){
maxLine = maxLineFiles.get(normalPath);
}
else {
maxLine = CxxUtils.countLines(normalPath);
if (maxLine > 0)
maxLineFiles.put(normalPath, maxLine);
}
if (lineNr > maxLine) {
if(CxxUtils.LOG.isWarnEnabled()) {
CxxUtils.LOG.warn("Correct line number ("+ lineNr +") from report for '"+ normalPath +"' to max. line (" + maxLine +")");
}
lineNr = maxLine;
}
resource = file;
add = true;
} else {
Expand Down Expand Up @@ -303,3 +319,4 @@ protected String reportPathKey() {
return "";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
package org.sonar.plugins.cxx.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,7 +53,7 @@ public static String fileToAbsolutePath(File file) {
}
return file.getAbsolutePath();
}

/**
* Normalize the given path to pass it to sonar. Return null if normalization
* has failed.
Expand Down Expand Up @@ -78,8 +82,32 @@ public static String normalizePathFull(String filename, String baseDir) {
return filePath;
}


/**
* @return returns true for Reactor Projects
*/
public static boolean isReactorProject(Project project) {
return project.isRoot() && !project.getModules().isEmpty();
}

/**
* @return returns number of Lines of a File
*/
public static int countLines(String normalPath) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no core metric we can use to get the line number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a line number in the size section of the metrics.
http://docs.sonarqube.org/display/SONAR/Metric+definitions#Metricdefinitions-Size
I am not sure whether the line metric is available when the report is consumed.

LineNumberReader reader;
int cnt = 0;
try {
reader = new LineNumberReader(new FileReader(normalPath));
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {}
cnt = reader.getLineNumber() + 1;
reader.close();
} catch (FileNotFoundException e) {
LOG.error("countLines of '{}' failed: '{}'", normalPath, e.toString());
} catch (IOException e1) {
LOG.error("countLines of '{}' failed: '{}'", normalPath, e1.toString());
}
return cnt;
}
}