{"id":23470806,"url":"https://github.com/sonaropencommunity/cxx-custom-checks-example-plugin","last_synced_at":"2025-10-26T02:09:53.825Z","repository":{"id":52235986,"uuid":"53788033","full_name":"SonarOpenCommunity/cxx-custom-checks-example-plugin","owner":"SonarOpenCommunity","description":"Sample for CXX custom rule: This plugin adds a custom rule to the cxx plugin of SonarQube.","archived":false,"fork":false,"pushed_at":"2024-07-09T22:18:00.000Z","size":56,"stargazers_count":6,"open_issues_count":6,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T17:29:36.855Z","etag":null,"topics":["custom","cxx","plugin","rule"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SonarOpenCommunity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-03-13T14:01:48.000Z","updated_at":"2024-03-07T15:30:34.000Z","dependencies_parsed_at":"2024-03-13T13:50:05.712Z","dependency_job_id":"a3e593ee-349a-4d5a-9abe-28e550c5c806","html_url":"https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SonarOpenCommunity/cxx-custom-checks-example-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SonarOpenCommunity%2Fcxx-custom-checks-example-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SonarOpenCommunity%2Fcxx-custom-checks-example-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SonarOpenCommunity%2Fcxx-custom-checks-example-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SonarOpenCommunity%2Fcxx-custom-checks-example-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SonarOpenCommunity","download_url":"https://codeload.github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SonarOpenCommunity%2Fcxx-custom-checks-example-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281047743,"owners_count":26435124,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-26T02:00:06.575Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom","cxx","plugin","rule"],"created_at":"2024-12-24T16:14:49.527Z","updated_at":"2025-10-26T02:09:53.806Z","avatar_url":"https://github.com/SonarOpenCommunity.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CXX Custom Rules\n\nThe _cxx plugin_ makes it possible to add custom rules written in Java. In general, there are three ways to add coding rules to SonarQube:\n* Writing a [SonarQube plugin](https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/) in Java that uses [SonarQube APIs](https://javadocs.sonarsource.org/9.9.0.229/index.html) to add new rules\n* Adding [XPath rules](https://docs.sonarsource.com/sonarqube/latest/extension-guide/adding-coding-rules/#adding-coding-rules-using-xpath) directly through the SonarQube web interface. The _cxx plugin_ provides an own [[CXX XPath|CXX-Custom-XPath-Rules]] extension.\n* Importing [generic issue reports](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/importing-external-issues/generic-issue-import-format/) generated by an independently run tool\n\nThe _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).\n\nImporting 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).\n\n# Writing a SonarQube plugin in Java that uses SonarQube APIs to add new rules\n\nWriting custom rules for CXX is a six-step process:\n\n* Create a new _SonarQube_ custom rules plugin (use https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin as template).\n  * As a first step, the [pom.xml](https://github.com/SonarOpenCommunity/cxx-custom-checks-example-plugin/blob/master/pom.xml) file must be adapted. The version numbers must match the versions of the _cxx plugin_ ([cxx plugin pom.xml](https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/pom.xml)) for which custom rules are written.\n  * Under [Advanced build properties](https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/#advanced-build-properties) you will find further instructions for setting the metadata.\n  * [sonar-plugin-api](https://github.com/SonarSource/sonar-plugin-api?tab=readme-ov-file#sonar-plugin-api) describes changes to the API.\n* Put a dependency on the API of the [cxx plugin](https://github.com/SonarOpenCommunity/sonar-cxx). 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.\n* Create as many custom rules as required. The rules must be derived from [CustomCxxRulesDefinition](https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/CustomCxxRulesDefinition.java).\n  * the HTML description(s) must be created in `/org/sonar/l10n/cxx/rules/{repositoryKey}`\n* Generate the _SonarQube_ custom rules plugin (jar file).\n* Place this jar file in the `SONARQUBE_HOME/extensions/plugins` directory.\n* Restart _SonarQube Server_.\n\nThe description [Plugin Basics](https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/) is a good starting point for writing your own extensions. In addition, [Adding Coding Rules](https://docs.sonarsource.com/sonarqube/latest/extension-guide/adding-coding-rules/) gives further useful hints.\n\nThe existing _CXX rules_ can be used as a template for the new rules:\u003cbr\u003e\nhttps://github.com/SonarOpenCommunity/sonar-cxx/tree/master/cxx-checks/src/main/java/org/sonar/cxx/checks\n\n**C++ sample to verify:**\n\n```C++\nusing namespace std;\n\nvoid foo()\n{\n}\n```\n**Resulting AST:**\n\n![grafik](https://github.com/SonarOpenCommunity/sonar-cxx/assets/6077367/88c41af8-6f0d-4725-802f-81af48b1b2c8)\n\n**Custom Rule Plugin sample:**\n\n```Java\npublic final class MyCustomRulesPlugin implements Plugin {\n\n  @Override\n  public void define(Context context) {\n    context.addExtension(\n      MyCustomRulesDefinition.class\n    );\n  }\n}\n\npublic class MyCustomRulesDefinition extends CustomCxxRulesDefinition {\n\n  @Override\n  public String repositoryName() {\n    return \"Custom CXX\";\n  }\n\n  @Override\n  public String repositoryKey() {\n    // The html descriptions for the rules of repository must be stored in the path '/org/sonar/l10n/cxx/rules/mycxx'.\n    // If the return value of 'repositoryKey' is changed, the storage location in 'resources' must also be adjusted.\n    return \"mycxx\";\n  }\n\n  @SuppressWarnings(\"rawtypes\")\n  @Override\n  public Class[] checkClasses() {\n    return new Class[]{\n      UsingNamespaceCheck.class\n    };\n  }\n}\n\n// In case you are adding a .html description in resources, the .html file name should match the rule key.\n// In this sample the name must be 'UsingNamespace.html'.\n@Rule(\n  key = \"UsingNamespace\",\n   priority = Priority.BLOCKER,\n   name = \"Using namespace directives are not allowed\",\n   tags = {Tag.CONVENTION}\n// second possibility to add a rule description:\n//,description = \"Using namespace directives are not allowed.\"\n)\n@SqaleConstantRemediation(\"5min\")\n@ActivatedByDefault\npublic class UsingNamespaceCheck extends SquidCheck\u003cGrammar\u003e {\n\n  @Override\n  public void init() {\n    subscribeTo(CxxGrammarImpl.usingDirective);\n  }\n\n  @Override\n  public void visitNode(AstNode node) {\n    getContext().createLineViolation(this, \"Using namespace are not allowed.\", node);\n  }\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonaropencommunity%2Fcxx-custom-checks-example-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonaropencommunity%2Fcxx-custom-checks-example-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonaropencommunity%2Fcxx-custom-checks-example-plugin/lists"}