修复:c++插件部署后在扫描有问题的被测件但扫描结果显示无问题的情况

wuhaoyang
Guo XIn 10 months ago
parent 1390585ba5
commit b8d407c798
  1. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxLanguage.java
  2. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxPlugin.java
  3. 48
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxSonarWayProfile.java
  4. 976
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxSquidSensor.java
  5. 9
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/SubscriptionAstVisitor.java
  6. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java
  7. 4
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/CxxLanguageTest.java
  8. 24
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/CxxPluginTest.java

@ -40,7 +40,7 @@ public class CxxLanguage extends AbstractLanguage {
/**
* Default cxx files knows suffixes
*/
public static final String DEFAULT_FILE_SUFFIXES = "-";
public static final String DEFAULT_FILE_SUFFIXES = ".cxx,.cpp,.cc,.c,.hxx,.hpp,.hh,.h";
/**
* Settings of the plugin.

@ -64,9 +64,9 @@ public final class CxxPlugin implements Plugin {
// plugin elements
l.add(CxxLanguage.class);
l.add(CxxSonarWayProfile.class);
l.add(SecurityDesignWayProfile.class);
l.add(CxxRuleRepository.class);
l.add(SecurityDesignRuleRepository.class);
l.add(SecurityDesignWayProfile.class);
// reusable elements
l.addAll(getSensorsImpl());

@ -8,6 +8,7 @@ package com.keyware.sonar.cxx;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.keyware.sonar.cxx.rules.SecurityDesignRuleRepository;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.cxx.checks.CheckList;
import org.sonarsource.api.sonarlint.SonarLintSide;
@ -23,34 +24,33 @@ import java.util.List;
@SonarLintSide
public class CxxSonarWayProfile implements BuiltInQualityProfilesDefinition {
private static String readResource(URL resource) {
try {
return Resources.toString(resource, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Failed to read: " + resource, e);
private static String readResource(URL resource) {
try {
return Resources.toString(resource, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Failed to read: " + resource, e);
}
}
}
static Profile readProfile() {
URL resource = CxxSonarWayProfile.class.getResource("/org/sonar/l10n/cxx/rules/cxx/Sonar_way_profile.json");
return new Gson().fromJson(readResource(resource), Profile.class);
}
@Override
public void define(Context context) {
var sonarWay = context.createBuiltInQualityProfile("Sonar way", CxxLanguage.KEY);
Profile jsonProfile = readProfile();
jsonProfile.ruleKeys.forEach((key) -> {
sonarWay.activateRule(CheckList.REPOSITORY_KEY, key);
});
static Profile readProfile() {
URL resource = CxxSonarWayProfile.class.getResource("/org/sonar/l10n/cxx/rules/cxx/Sonar_way_profile.json");
return new Gson().fromJson(readResource(resource), Profile.class);
}
sonarWay.done();
}
@Override
public void define(Context context) {
var sonarWay = context.createBuiltInQualityProfile("Sonar way", CxxLanguage.KEY);
Profile jsonProfile = readProfile();
jsonProfile.ruleKeys.forEach((key) -> {
sonarWay.activateRule(CheckList.REPOSITORY_KEY, key);
});
sonarWay.done();
}
static class Profile {
static class Profile {
public String name;
public List<String> ruleKeys;
}
public String name;
public List<String> ruleKeys;
}
}

@ -9,6 +9,7 @@ package com.keyware.sonar.cxx;
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.AstNodeType;
import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.cxx.squidbridge.SquidAstVisitor;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import java.util.List;
@ -19,13 +20,15 @@ import java.util.List;
* @author GuoXin
* @date 2024/1/13
*/
public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> {
public abstract class SubscriptionAstVisitor extends SquidAstVisitor<Grammar> {
private SquidCheck<Grammar> checker;
/**
* 构造函数需要传入初代访问器
*
* @param checker 初代规则检查器
*/
public SubscriptionAstVisitor(SquidCheck<Grammar> checker) {
this.checker = checker;
setContext(checker.getContext());
visitNodeTypes().forEach(this::subscribeTo);
}
@ -95,7 +98,7 @@ public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> {
* @param messageParameters 可选消息参数请参阅 java.text.MessageFormat
*/
protected void reportIssue(String message, Object... messageParameters) {
getContext().createFileViolation(this, message, messageParameters);
getContext().createFileViolation(checker, message, messageParameters);
}
/**
@ -106,7 +109,7 @@ public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> {
* @param messageParameters 可选消息参数请参阅 java.text.MessageFormat
*/
protected void reportIssue(AstNode node, String message, Object... messageParameters) {
getContext().createLineViolation(this, message, node, messageParameters);
getContext().createLineViolation(checker, message, node, messageParameters);
}
}

@ -25,7 +25,7 @@ public class SecurityDesignRuleRepository implements RulesDefinition {
public final static String REPOSITORY_NAME = "C++信息安全性设计准则";
// 规则检查器的集合,当有新的规则开发完毕后,需要添加到下面的集合中
private final List<Class> RULE_CHECKERS = Arrays.asList(ABCVarNameChecker.class);
public final static List<Class> RULE_CHECKERS = Arrays.asList(ABCVarNameChecker.class);
@Override
public void define(Context context) {

@ -32,7 +32,7 @@ class CxxLanguageTest {
@Test
void shouldReturnDefaultFileSuffixes1() {
var cxx = new CxxLanguage(settings.asConfig());
String[] expected = {"disabled"};
String[] expected = {".cxx", ".cpp", ".cc", ".c", ".hxx", ".hpp", ".hh", ".h"};
assertThat(cxx.getFileSuffixes()).contains(expected);
}
@ -40,7 +40,7 @@ class CxxLanguageTest {
void shouldReturnDefaultFileSuffixes2() {
settings.setProperty(CxxLanguage.FILE_SUFFIXES_KEY, "");
var cxx = new CxxLanguage(settings.asConfig());
String[] expected = {"disabled"};
String[] expected = {".cxx", ".cpp", ".cc", ".c", ".hxx", ".hpp", ".hh", ".h"};
assertThat(cxx.getFileSuffixes()).contains(expected);
}

@ -18,17 +18,17 @@ import static org.assertj.core.api.Assertions.assertThat;
class CxxPluginTest {
@Test
void testGetExtensions() throws Exception {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(
Version.create(8, 6),
SonarQubeSide.SCANNER,
SonarEdition.COMMUNITY
);
var context = new Plugin.Context(runtime);
var plugin = new CxxPlugin();
plugin.define(context);
assertThat(context.getExtensions()).hasSize(84);
}
@Test
void testGetExtensions() throws Exception {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(
Version.create(8, 6),
SonarQubeSide.SCANNER,
SonarEdition.COMMUNITY
);
var context = new Plugin.Context(runtime);
var plugin = new CxxPlugin();
plugin.define(context);
assertThat(context.getExtensions()).hasSize(85);
}
}

Loading…
Cancel
Save