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

fix CppcheckParserV2; allow multiple NewIssueLocations #1436

Closed
wants to merge 6 commits 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 @@ -20,12 +20,18 @@
package org.sonar.cxx.sensors.cppcheck;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.xml.stream.XMLStreamException;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.sensors.utils.CxxReportIssue;
import org.sonar.cxx.sensors.utils.CxxReportLocation;;
import org.sonar.cxx.sensors.utils.EmptyReportException;
import org.sonar.cxx.sensors.utils.StaxParser;

Expand Down Expand Up @@ -70,18 +76,18 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
parsed = true;
SMInputCursor errorCursor = errorsCursor.childElementCursor("error");
while (errorCursor.getNext() != null) {
String id = errorCursor.getAttrValue("id");
String msg = createMsg(
errorCursor.getAttrValue("inconclusive"),
errorCursor.getAttrValue("msg")
);
String file = null;
String line = null;
String id = Objects.requireNonNull(errorCursor.getAttrValue("id"),
"Missing mandatory attribute /results/errors/error[@id]");
String msg = Objects.requireNonNull(errorCursor.getAttrValue("msg"),
"Missing mandatory attribute /results/errors/error[@msg]");
msg = createMsg(errorCursor.getAttrValue("inconclusive"), errorCursor.getAttrValue("msg"));

List<CxxReportLocation> locations = new ArrayList<>();
SMInputCursor locationCursor = errorCursor.childElementCursor("location");
if (locationCursor.getNext() != null) {
file = locationCursor.getAttrValue("file");
line = locationCursor.getAttrValue("line");
while (locationCursor.getNext() != null) {
String file = locationCursor.getAttrValue("file");
String line = locationCursor.getAttrValue("line");
String info = locationCursor.getAttrValue("info");

if (file != null) {
file = file.replace('\\', '/');
Expand All @@ -91,11 +97,36 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
// findings on project level
file = null;
line = null;
info = null;
}

if (locations.isEmpty()) {
CxxReportLocation primaryLocation = new CxxReportLocation(file, line, msg);
locations.add(primaryLocation);

// add the same file:line second time if there is additional
// information about the flow/analysis
if (info != null && !msg.equals(info)) {
CxxReportLocation primaryLocationWithMoreInfo = new CxxReportLocation(file, line, info);
locations.add(primaryLocationWithMoreInfo);
}
} else if (info != null) {
CxxReportLocation secondaryLocation = new CxxReportLocation(file, line, info);
locations.add(secondaryLocation);
}
}

if (isInputValid(id, msg)) {
sensor.saveUniqueViolation(context, CxxCppCheckRuleRepository.KEY, file, line, id, msg);
if (locations.isEmpty()) {
// no <location> tags: issue raised on the whole
// module/project
CxxReportLocation moduleLocation = new CxxReportLocation(null, null, msg);
CxxReportIssue moduleIssue = new CxxReportIssue(CxxCppCheckRuleRepository.KEY, id, moduleLocation);
sensor.saveUniqueViolation(context, moduleIssue);
} else {
CxxReportIssue issue = new CxxReportIssue(CxxCppCheckRuleRepository.KEY, id, locations);
sensor.saveUniqueViolation(context, issue);
}
} else {
LOG.warn("Skipping invalid violation: '{}'", msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.sensors.utils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Issue with one or multiple locations
*/
public class CxxReportIssue {
final String ruleRepoKey;
final String ruleId;
final List<CxxReportLocation> locations;

public CxxReportIssue(String ruleRepoKey, String ruleId, CxxReportLocation primaryLocation) {
this(ruleRepoKey, ruleId, Collections.singletonList(primaryLocation));
}

public CxxReportIssue(String ruleRepoKey, String ruleId, List<CxxReportLocation> locations) {
super();
this.ruleRepoKey = ruleRepoKey;
this.ruleId = ruleId;
this.locations = locations;
}

public String getRuleRepoKey() {
return ruleRepoKey;
}

public String getRuleId() {
return ruleId;
}

public List<CxxReportLocation> getLocations() {
return Collections.unmodifiableList(locations);
}

@Override
public String toString() {
String locationsToString = locations.stream().map(Object::toString).collect(Collectors.joining(", "));
return "CxxReportIssue [ruleRepoKey=" + ruleRepoKey + ", ruleId=" + ruleId + ", locations=" + locationsToString
+ "]";
}

@Override
public int hashCode() {
return Objects.hash(locations, ruleId, ruleRepoKey);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CxxReportIssue other = (CxxReportIssue) obj;
return Objects.equals(locations, other.locations) && Objects.equals(ruleId, other.ruleId)
&& Objects.equals(ruleRepoKey, other.ruleRepoKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.sensors.utils;

import java.util.Objects;

/**
* Each issues in SonarQube might have multiple locations; Encapsulate its
* properties in this structure
*/
public class CxxReportLocation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unit test for CxxReportLocation missing

final String file;
final String line;
final String info;

public CxxReportLocation(String file, String line, String info) {
super();
this.file = file;
this.line = line;
this.info = info;
}

public String getFile() {
return file;
}

public String getLine() {
return line;
}

public String getInfo() {
return info;
}

@Override
public String toString() {
return "CxxReportLocation [file=" + file + ", line=" + line + ", info=" + info + "]";
}

@Override
public int hashCode() {
return Objects.hash(file, info, line);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CxxReportLocation other = (CxxReportLocation) obj;
return Objects.equals(file, other.file) && Objects.equals(info, other.info) && Objects.equals(line, other.line);
}
}
Loading