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

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

Closed
smileQiny opened this issue Feb 27, 2024 · 8 comments
Closed
Milestone

Comments

@smileQiny
Copy link

smileQiny commented Feb 27, 2024

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.

@smileQiny smileQiny changed the title 【request】 cxx-custom-checks-example-plugin can not to report issue information Feb 27, 2024
@guwirth
Copy link
Collaborator

guwirth commented Feb 27, 2024

Hello @smileQiny,

think you know the entry point in the wiki:
https://github.com/SonarOpenCommunity/sonar-cxx/wiki/CXX-Custom-Rules

An up to date description from SonarSource for plugin development is here:
https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/

As first step I would update the pom.xml file to latest versions:
https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin/blob/master/pom.xml

This is for sure something you have to update:

old:

    <dependency>
      <groupId>org.sonarsource.sonarqube</groupId>
      <artifactId>sonar-plugin-api</artifactId>
      <version>${sonar.version}</version>
      <scope>provided</scope>
    </dependency>

new:

      <dependency>
        <groupId>org.sonarsource.api.plugin</groupId>
        <artifactId>sonar-plugin-api</artifactId>
        <version>${sonar.plugin.api.version}</version>
      </dependency>

Logging:
https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/#logging

Than I would have a look to running sensors of the cxx plugin to see what is different. Think an easy one is the Cppcheck sensor:
https://github.com/SonarOpenCommunity/sonar-cxx/tree/master/cxx-sensors/src/main/java/org/sonar/cxx/sensors/cppcheck

An additional point is, that you always have to activate rules in a quality profile to use them:
https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Manage-Quality-Profiles

Hope this helps!

Regards,

@guwirth
Copy link
Collaborator

guwirth commented Feb 27, 2024

@smileQiny
Copy link
Author

smileQiny commented Feb 28, 2024

Hi,guwirth:
Thank you very much for your reply.
I still have a slight question during debugging.
Based on the old cxx-custom-checks-example-plugin and the official python_custom_rules_demo examples for reference, I understand that I only need to write a plugin for a rule and register it. Regarding the corresponding sensor, it will continue to use CxxSquidSensor. I wonder if my understanding is correct?

Because when I tried to verify, I changed the repo key from "custom-cxx" to "cxx", and the rule I wrote was displayed in the sonar rules web , and the repo display name was "Sonar CXX", not "Custom CXX". In this case, select the rule to activate, perform a sonar-scanner scan, and the check results of the custom defined rule can be displayed in the Sonar web.

@smileQiny
Copy link
Author

smileQiny commented Feb 28, 2024

Hi,guwirth:
I understand why this problem occurred.
in org/sonar/plugins/cxx/CxxSquidSensor.java file:In the saveViolations function, when creating a new issue, the source is fixed as "cxx".
image

@smileQiny
Copy link
Author

please check pr: #2644

@guwirth
Copy link
Collaborator

guwirth commented Feb 29, 2024

Hello @smileQiny,

Based on the old cxx-custom-checks-example-plugin and the official python_custom_rules_demo examples for reference, I understand that I only need to write a plugin for a rule and register it. Regarding the corresponding sensor, it will continue to use CxxSquidSensor. I wonder if my understanding is correct?

It depends what you wanna do.

  1. One use case is to extend an existing rule repository
  2. Second use case is to create an own independent rule repository
  3. In both cases you always have to define for which language(s) the rules are

https://javadocs.sonarsource.org/8.9.0.43852/org/sonar/api/server/rule/RulesDefinition.html
https://javadocs.sonarsource.org/8.9.0.43852/org/sonar/api/server/rule/RulesDefinition.Context.html

One thing is to define the rules in the rule repository, the second to read a report, parse the report and create issues with a matching rule ID.

  • For this step you can use an existing sensor (report format) - case 1. above
  • or write an own new sensor for your own report format.

https://javadocs.sonarsource.org/8.9.0.43852/org/sonar/api/batch/sensor/Sensor.html
https://javadocs.sonarsource.org/8.9.0.43852/org/sonar/api/batch/sensor/SensorContext.html

Hope this helps!

Regards,

@smileQiny
Copy link
Author

smileQiny commented Mar 2, 2024

Hi,guwirth:
Thank you for taking the time out of your busy schedule to answer my questions.
My purpose is to extend the rules based on the current sonar-cxx.
If you have time, please review a PR(SonarOpenCommunity/cxx-custom-checks-example-plugin#23) under this repo (https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin). The person in charge of this repo seems to have given up maintaining it. I understand that the existence of the repo is still very meaningful, so I would like to update its content.

@guwirth guwirth added this to the 2.1.2 milestone Mar 7, 2024
@guwirth
Copy link
Collaborator

guwirth commented Mar 7, 2024

@guwirth guwirth closed this as completed Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants