Giter Club home page Giter Club logo

cxx-custom-checks-example-plugin's Introduction

CXX Custom Rules

The cxx plugin makes it possible to add custom rules written in Java. In general, there are three ways to add coding rules to SonarQube:

The Java API will be more fully-featured than what's available for XPath, and is generally preferable. However, this comes with the overhead of maintaining a SonarQube plugin (including keeping it up-to-date as APIs change, upgrading the plugin after releasing a new version).

Importing generic issue reports is a good solution when there's a very specific need for a subset of projects on your SonarQube instance. They are the most flexible option but lack some features (such as being able to control their execution by inclusion in a quality profile).

Writing a SonarQube plugin in Java that uses SonarQube APIs to add new rules

Writing custom rules for CXX is a six-step process:

  • Create a new SonarQube custom rules plugin (use https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin as template).
  • Put a dependency on the API of the cxx plugin. The cxx plugin must be built locally with Maven so that it is available in the local Maven Repository and can be used as a dependency in the custom plugin.
  • Create as many custom rules as required. The rules must be derived from CustomCxxRulesDefinition.
    • the HTML description(s) must be created in /org/sonar/l10n/cxx/rules/{repositoryKey}
  • Generate the SonarQube custom rules plugin (jar file).
  • Place this jar file in the SONARQUBE_HOME/extensions/plugins directory.
  • Restart SonarQube Server.

The description Plugin Basics is a good starting point for writing your own extensions. In addition, Adding Coding Rules gives further useful hints.

The existing CXX rules can be used as a template for the new rules:
https://github.com/SonarOpenCommunity/sonar-cxx/tree/master/cxx-checks/src/main/java/org/sonar/cxx/checks

C++ sample to verify:

using namespace std;

void foo()
{
}

Resulting AST:

grafik

Custom Rule Plugin sample:

public final class MyCustomRulesPlugin implements Plugin {

  @Override
  public void define(Context context) {
    context.addExtension(
      MyCustomRulesDefinition.class
    );
  }
}

public class MyCustomRulesDefinition extends CustomCxxRulesDefinition {

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

  @Override
  public String repositoryKey() {
    // The html descriptions for the rules of repository must be stored in the path '/org/sonar/l10n/cxx/rules/mycxx'.
    // If the return value of 'repositoryKey' is changed, the storage location in 'resources' must also be adjusted.
    return "mycxx";
  }

  @SuppressWarnings("rawtypes")
  @Override
  public Class[] checkClasses() {
    return new Class[]{
      UsingNamespaceCheck.class
    };
  }
}

// In case you are adding a .html description in resources, the .html file name should match the rule key.
// In this sample the name must be 'UsingNamespace.html'.
@Rule(
  key = "UsingNamespace",
   priority = Priority.BLOCKER,
   name = "Using namespace directives are not allowed",
   tags = {Tag.CONVENTION}
// second possibility to add a rule description:
//,description = "Using namespace directives are not allowed."
)
@SqaleConstantRemediation("5min")
@ActivatedByDefault
public class UsingNamespaceCheck extends SquidCheck<Grammar> {

  @Override
  public void init() {
    subscribeTo(CxxGrammarImpl.usingDirective);
  }

  @Override
  public void visitNode(AstNode node) {
    getContext().createLineViolation(this, "Using namespace are not allowed.", node);
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.