Skip to content

cxx-custom-checks-example-plugin can not to report issue information #2642

@smileQiny

Description

@smileQiny

hi guwirth:
I'm trying to update the example of xx-custom-checks-example-plugin for Sonar 9.9 TLS. After writing the plugin, it successfully loads under Sonar, and the rules are triggered during scanning. However, there are no results reported. Can you provide me with some troubleshooting tips?
CXX Vesrion: C++ (Community) / 2.1.1-SNAPSHOT
Sonar version:9.9

The main process of writing the plugin is as follows:

  1. Writing rules (using sonar-cxx/tree/master/cxx-checks),eg:
@Rule(
        key = LengthOfLineCheck.RULE_KEY,
        name = "TYY.2.7.1 test_line",
        priority = Priority.MINOR,
        description = "dec")
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class LengthOfLineCheck extends SquidCheck<Grammar> {
    public static final String RULE_KEY = "LengthOfLine";
    private static final int MAXIMUM_DEFAULT_LINE_LENGTH = 80;
    private static final int TAB_DEFAULT_WIDTH = 4;

    @RuleProperty(
            key = "maximumLineLength",
            description = "The maximum authorized line length",
            defaultValue = "" + MAXIMUM_DEFAULT_LINE_LENGTH)
    public long maxLineLength = MAXIMUM_DEFAULT_LINE_LENGTH;

    @RuleProperty(
            key = "tabWidth",
            description = "Number of spaces in a 'tab' character",
            defaultValue = "" + TAB_DEFAULT_WIDTH)
    public int tabWidth = TAB_DEFAULT_WIDTH;

    @Override
    public void visitFile(@Nullable AstNode astNodeInfo) {
        System.out.printf("%s", "[qy dbg] tabWidth  visitFile\r\n"); // 输出 "hello world"
        int char_count = 0;
        for (String inputLine : getContext().getInputFileLines()) {
            ++char_count;
            long charsLength = inputLine.chars().filter(c -> c == '\t').count();
            charsLength = inputLine.length() + charsLength * (tabWidth - 1);
            System.out.printf("[qy dbg] tabWidth visitFile: char_count %d, charsLength %d, maxLineLength %d.\n", char_count, charsLength, maxLineLength);
            if (charsLength > maxLineLength) {
                System.out.printf("[qy dbg] tabWidth visitFile  =======> report failed.\n");
                getContext().createLineViolation(
                        this,
                        "Split this {0} characters long line (which is greater than {1} authorized).",
                        char_count, charsLength, maxLineLength);
            }
        }
    }
}
  1. Registering rules, eg:
public class CTyunOSCustomCxxRulesDefinition extends CustomCxxRulesDefinition {
  private static final Language LANGUAGE = new CxxLanguage(new MapSettings().asConfig());

  @Override
  public Language getLanguage() {
    return LANGUAGE;
  }

  @Override
  public String repositoryName() {
    return "CXX Repository";
  }

  @Override
  public String repositoryKey() {
    return "cxx-rules";
  }

  @SuppressWarnings("rawtypes")
  @Override
  public Class[] checkClasses() {
    return new Class[] {
            LengthOfLineCheck.class,
    };
  }
  @Override
  public void define(RulesDefinition.Context context) {
    NewRepository repo = context.createRepository(repositoryKey(), getLanguage().getKey())
            .setName(repositoryName());
    new AnnotationBasedRulesDefinition(repo, getLanguage().getKey()).addRuleClasses(false,
            Arrays.asList(checkClasses()));
    repo.done();
  }

}   
  1. Writing the plugin, and registering the plugin.
public class CTyunOSCustomCxxRulesPlugin implements Plugin {

  @Override
  public void define(Context context) {
    System.out.printf("%s", "[qy dbg] CustomCxxRulesPlugin  Plugin"); 
    context.addExtension(
            CTyunOSCustomCxxRulesDefinition.class
    );
  }
}

the log : Please see the attached files for the scan and loading logs.
MobaXterm_WSL-SonarServer.txt
MobaXterm_WSL-OpenEuler23.03_20240227_213752.txt
The validation examples used are the test cases from sonar-cxx. Custom rules are not logging any information, while native rules are logging properly.
Please take some time from your busy schedule to provide some guidance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions