Skip to content

Commit

Permalink
Support case-insensitive file systems in all sensors #155:
Browse files Browse the repository at this point in the history
Normalize paths before passing them to Sonar. Do it in (hopefully) all
relevant places:
- CxxReportSensor (feeding violations)
- CoberturaParser (feeding coverage data)
- BullseyeParser (feeding coverage data)
  • Loading branch information
Waleri Enns committed Apr 28, 2014
1 parent 7c82d40 commit 10bd87a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ private void recTreeWalk(String refPath, SMInputCursor folder, List<String> path
}
CoverageMeasuresBuilder fileMeasuresBuilderIn = CoverageMeasuresBuilder.create();
fileWalk(child, fileMeasuresBuilderIn);
coverageData.put(refPath + fileName, fileMeasuresBuilderIn);

String normalPath = CxxUtils.normalizePath(refPath + fileName);
if(normalPath != null){
coverageData.put(refPath + fileName, fileMeasuresBuilderIn);

This comment has been minimized.

Copy link
@guwirth

guwirth Apr 28, 2014

Collaborator

Should be coverageData.put(normalPath, ... ) ?

This comment has been minimized.

Copy link
@wenns

wenns Apr 30, 2014

Contributor

f*ck, yes

}

} else {
recTreeWalk(refPath, child, path, coverageData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ private void collectFileMeasures(SMInputCursor clazz, Map<String, CoverageMeasur
throws XMLStreamException
{
while (clazz.getNext() != null) {
String fileName = clazz.getAttrValue("filename");
CoverageMeasuresBuilder builder = coverageData.get(fileName);
if (builder == null) {
builder = CoverageMeasuresBuilder.create();
coverageData.put(fileName, builder);
String normalPath = CxxUtils.normalizePath(clazz.getAttrValue("filename"));
if(normalPath != null){
CoverageMeasuresBuilder builder = coverageData.get(normalPath);
if (builder == null) {
builder = CoverageMeasuresBuilder.create();
coverageData.put(normalPath, builder);
}
collectFileData(clazz, builder);
}
collectFileData(clazz, builder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,29 @@ public boolean saveViolation(Project project, SensorContext context, String rule
Violation violation = null;
// handles file="" situation
if ((file != null) && (file.length() > 0)){
org.sonar.api.resources.File resource =
org.sonar.api.resources.File.fromIOFile(new File(file), project);
if (context.getResource(resource) != null) {
// file level violation
violation = Violation.create(rule, resource);

// considering the line information for file level violations only
if (line != null){
try{
int linenr = Integer.parseInt(line);
linenr = linenr == 0 ? 1 : linenr;
violation.setLineId(linenr);
} catch(java.lang.NumberFormatException nfe){
CxxUtils.LOG.warn("Skipping invalid line number: {}", line);
String normalPath = CxxUtils.normalizePath(file);
if(normalPath != null){
org.sonar.api.resources.File resource =
org.sonar.api.resources.File.fromIOFile(new File(normalPath), project);
if (context.getResource(resource) != null) {
// file level violation
violation = Violation.create(rule, resource);

// considering the line information for file level violations only
if (line != null){
try{
int linenr = Integer.parseInt(line);
linenr = linenr == 0 ? 1 : linenr;
violation.setLineId(linenr);
} catch(java.lang.NumberFormatException nfe){
CxxUtils.LOG.warn("Skipping invalid line number: {}", line);
}
}
} else {
if (notFoundFiles.add(normalPath)) {
// issue this warning only once per file
CxxUtils.LOG.warn("Cannot find the file '{}', skipping violations", normalPath);
}
}
} else {
if (notFoundFiles.add(file)) {
// issue this warning only once per file
CxxUtils.LOG.warn("Cannot find the file '{}', skipping violations", file);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public static String fileToAbsolutePath(File file) {
}
return file.getAbsolutePath();
}

/**
* Normalize the given path to pass it to sonar. Return null if normalization has failed.
*/
public static String normalizePath(String filename) {
try {
return new File(filename).getCanonicalPath();
} catch (java.io.IOException e) {
LOG.error("path normalizing of '{}' failed: '{}'", filename, e.toString());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<error file="src/lib/component2.cc" line="46" id="deallocDealloc" severity="error" msg="Deallocating a deallocated pointer: ip"/>
<error file="src/lib/component2.cc" line="46" id="doubleFree" severity="error" msg="Memory pointed to by 'ip' is freed twice."/>
<error file="src/lib/component2.cc" line="31" id="uninitvar" severity="error" msg="Uninitialized variable: a"/>
<error file="src/lib/component1.cc" line="24" id="unusedFunction" severity="style" msg="The function 'do_valgrind_errors' is never used."/>
<!-- alternative notation for the file path. -->
<error file="./src/lib/component1.cc" line="24" id="unusedFunction" severity="style" msg="The function 'do_valgrind_errors' is never used."/>
<!-- Violation for a file which does not exist. Should be be saved. -->
<error file="src/lib/component_XXX.cc" line="24" id="unusedFunction" severity="style" msg="The function 'do_valgrind_errors' is never used."/>
<error id="missingInclude" severity="style" msg="Cppcheck cannot find all the include files. Cppcheck can check the code without the include files found. But the results will probably be more accurate if all the include files are found. Please check your project's include directories and add all of them as include directories for Cppcheck. To see what files Cppcheck cannot find use --check-config."/>
</results>

0 comments on commit 10bd87a

Please sign in to comment.