解决C++示例规则无法被SonarQube识别的问题

wuhaoyang
Guo XIn 11 months ago
parent 1534e32307
commit 102558ec39
  1. 4
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxPlugin.java
  2. 110
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/CxxSecurityDesignRulesRepository.java
  3. 33
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java
  4. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ABCVarNameChecker.java

@ -6,7 +6,7 @@
*/
package com.keyware.sonar.cxx;
import com.keyware.sonar.cxx.rules.CxxSecurityDesignRulesRepository;
import com.keyware.sonar.cxx.rules.SecurityDesignRuleRepository;
import org.sonar.api.Plugin;
import org.sonar.cxx.AggregateMeasureComputer;
import org.sonar.cxx.DensityMeasureComputer;
@ -64,7 +64,7 @@ public final class CxxPlugin implements Plugin {
l.add(CxxLanguage.class);
l.add(CxxSonarWayProfile.class);
l.add(CxxRuleRepository.class);
l.add(CxxSecurityDesignRulesRepository.class);
l.add(SecurityDesignRuleRepository.class);
// reusable elements
l.addAll(getSensorsImpl());

@ -1,110 +0,0 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules;
import com.keyware.sonar.cxx.CustomCxxRulesDefinition;
import com.keyware.sonar.cxx.CxxLanguage;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.resources.Language;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
/**
* TODO CxxSecurityDesignRulesRepository
*
* @author GuoXin
* @date 2024/1/6
*/
public class CxxSecurityDesignRulesRepository extends CustomCxxRulesDefinition {
// 不要修改这个值,因为路径在 CheckVerifier 中是硬编码的
private static final String RESOURCE_BASE_PATH = "org/sonar/l10n/java/rules/java";
public static final String REPOSITORY_KEY = "keyware-cxx-security-design";
public static final String REPOSITORY_NAME = "C++ 安全性设计准则";
// 添加需要视为模板规则的规则的规则键
private static final Set<String> RULE_TEMPLATES_KEY = Collections.emptySet();
private final SonarRuntime runtime;
public CxxSecurityDesignRulesRepository(SonarRuntime runtime) {
this.runtime = runtime;
}
@Override
public void define(RulesDefinition.Context context) {
RulesDefinition.NewRepository repository = context.createRepository(REPOSITORY_KEY, "cxx").setName(REPOSITORY_NAME);
setTemplates(repository);
repository.done();
}
@Override
public Language getLanguage() {
return new CxxLanguage(null) {
public String[] getFileSuffixes() {
return new String[]{".cxx", ".cpp", ".cc", ".c", ".hxx", ".hpp", ".hh", ".h"};
}
};
}
@Override
public String repositoryName() {
return REPOSITORY_NAME;
}
@Override
public String repositoryKey() {
return REPOSITORY_KEY;
}
@Override
public Class[] checkClasses() {
return new Class[0];
}
private static void setTemplates(RulesDefinition.NewRepository repository) {
RULE_TEMPLATES_KEY.stream()
.map(repository::rule)
.filter(Objects::nonNull)
.forEach(rule -> rule.setTemplate(true));
}
public static class MockedSonarRuntime implements SonarRuntime {
@Override
public Version getApiVersion() {
return Version.create(9, 9);
}
@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}
@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}
@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
}
}

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules;
import com.keyware.sonar.cxx.CxxLanguage;
import com.keyware.sonar.cxx.rules.checkers.ABCVarNameChecker;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.cxx.squidbridge.annotations.AnnotationBasedRulesDefinition;
import java.util.Arrays;
import java.util.List;
/**
* TODO SecurityDesignRuleRepository
*
* @author GuoXin
* @date 2024/1/9
*/
public class SecurityDesignRuleRepository implements RulesDefinition {
private final String REPOSITORY_NAME = "C++信息安全性设计准则";
private final List<Class> RULES_DEFINITIONS = Arrays.asList(ABCVarNameChecker.class);
@Override
public void define(Context context) {
var repository = context.createRepository("cxx-security-design-rules", CxxLanguage.KEY).
setName(REPOSITORY_NAME);
new AnnotationBasedRulesDefinition(repository, CxxLanguage.KEY).addRuleClasses(false, RULES_DEFINITIONS);
repository.done();
}
}

@ -25,7 +25,7 @@ import static javax.swing.Action.DEFAULT;
* @author GuoXin
* @date 2024/1/6
*/
@Rule(key = "ABCVarNameChecker", name = "变量名称不可以是ABC", description = "变量名称不可以是ABC", priority = Priority.INFO, tags = {"28Suo"})
@Rule(key = "ABCVarNameChecker", name = "变量名称不可以是ABC", description = "变量名称不可以是ABC", priority = Priority.INFO, tags = {"28suo"})
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class ABCVarNameChecker extends SquidCheck<Grammar> {

Loading…
Cancel
Save