From 3023e9fc88249d9c6fa7cbb92ec801bc97ed390f Mon Sep 17 00:00:00 2001 From: Sylvain Kuchen Date: Wed, 29 Jan 2025 15:03:52 +0100 Subject: [PATCH 1/3] Build only Cobol --- build.sh | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/build.sh b/build.sh index 1bdfb6f..734035e 100755 --- a/build.sh +++ b/build.sh @@ -13,22 +13,9 @@ RunMaven() { fi } -cd rpg-custom-rules -wget https://binaries.sonarsource.com/CommercialDistribution/sonar-rpg-plugin/sonar-rpg-plugin-3.9.0.5001.jar -mv sonar-rpg-plugin-3.9.0.5001.jar ./lib/sonar-rpg-plugin-3.9.0.5001.jar -# We explicitly call validate here, to install the sonar-rpg-plugin jar ahead of packaging -mvn validate -RunMaven -cd .. - cd cobol-custom-rules wget https://binaries.sonarsource.com/CommercialDistribution/sonar-cobol-plugin/sonar-cobol-plugin-4.3.0.3019.jar mv sonar-cobol-plugin-4.3.0.3019.jar ./lib/sonar-cobol-plugin-4.3.0.3019.jar RunMaven cd .. -cd jcl-custom-rules -wget https://binaries.sonarsource.com/CommercialDistribution/sonar-jcl-plugin/sonar-jcl-plugin-1.2.0.1148.jar -mv sonar-jcl-plugin-1.2.0.1148.jar ./lib/sonar-jcl-plugin-1.2.0.1148.jar -RunMaven -cd .. From cff02ffc1a22d81fee53f4c0c2d6c410fc442bc2 Mon Sep 17 00:00:00 2001 From: Sylvain Kuchen Date: Wed, 29 Jan 2025 15:04:42 +0100 Subject: [PATCH 2/3] Use recent plugin version --- build.sh | 4 ++-- cobol-custom-rules/pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index 734035e..fbd3c6f 100755 --- a/build.sh +++ b/build.sh @@ -14,8 +14,8 @@ RunMaven() { } cd cobol-custom-rules -wget https://binaries.sonarsource.com/CommercialDistribution/sonar-cobol-plugin/sonar-cobol-plugin-4.3.0.3019.jar -mv sonar-cobol-plugin-4.3.0.3019.jar ./lib/sonar-cobol-plugin-4.3.0.3019.jar +wget https://binaries.sonarsource.com/CommercialDistribution/sonar-cobol-plugin/sonar-cobol-plugin-5.7.0.8061.jar +mv sonar-cobol-plugin-5.7.0.8061.jar ./lib/sonar-cobol-plugin-5.7.0.8061.jar RunMaven cd .. diff --git a/cobol-custom-rules/pom.xml b/cobol-custom-rules/pom.xml index 3e5b8fb..42184d3 100644 --- a/cobol-custom-rules/pom.xml +++ b/cobol-custom-rules/pom.xml @@ -19,7 +19,7 @@ 1.2.0 6.7.4 - 4.3.0.3019 + 5.7.0.8061 From 7cad18bc4d749a0fac2957bf1d5d1cf2c1bf406e Mon Sep 17 00:00:00 2001 From: Sylvain Kuchen Date: Wed, 29 Jan 2025 15:05:43 +0100 Subject: [PATCH 3/3] Implement custom rule --- cobol-custom-rules/pom.xml | 2 +- .../sample/CobolCustomCheckRepository.java | 5 +++- .../sample/checks/ForbiddenCopyRule.java | 23 ++++++++++++++++ .../l10n/cobol/rules/cobol/ForbiddenCopy.html | 9 +++++++ .../sample/checks/ForbiddenCopyRuleTest.java | 26 +++++++++++++++++++ .../resources/checks/SRC/ForbiddenCopy.cbl | 4 +++ 6 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRule.java create mode 100644 cobol-custom-rules/src/main/resources/org/sonar/l10n/cobol/rules/cobol/ForbiddenCopy.html create mode 100644 cobol-custom-rules/src/test/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRuleTest.java create mode 100644 cobol-custom-rules/src/test/resources/checks/SRC/ForbiddenCopy.cbl diff --git a/cobol-custom-rules/pom.xml b/cobol-custom-rules/pom.xml index 42184d3..9f8aee7 100644 --- a/cobol-custom-rules/pom.xml +++ b/cobol-custom-rules/pom.xml @@ -67,7 +67,7 @@ 1.21.0.505 true - cobol-custom + cobolcustom COBOL Custom Rules com.mycompany.cobol.sample.CobolCustomRulesPlugin true diff --git a/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/CobolCustomCheckRepository.java b/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/CobolCustomCheckRepository.java index a1317bd..f5b2470 100644 --- a/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/CobolCustomCheckRepository.java +++ b/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/CobolCustomCheckRepository.java @@ -19,6 +19,7 @@ */ package com.mycompany.cobol.sample; +import com.mycompany.cobol.sample.checks.ForbiddenCopyRule; import com.mycompany.cobol.sample.checks.ForbiddenCallRule; import com.mycompany.cobol.sample.checks.IssueOnEachFileRule; import com.mycompany.cobol.sample.checks.TrivialEvaluateRule; @@ -42,7 +43,7 @@ public class CobolCustomCheckRepository implements CobolCheckRepository, RulesDe // Change key and name to reflect your company rule repository. // Don't use "cobol" key, it's the core repository. - private static final String REPOSITORY_KEY = "my-custom-cobol"; + private static final String REPOSITORY_KEY = "customcobol"; private static final String REPOSITORY_NAME = "My Custom Cobol Analyzer"; // Must be "cobol" @@ -56,6 +57,7 @@ public String getRepositoryKey() { @Override public Collection getCheckClassesOrObjects() { return Arrays.asList( + ForbiddenCopyRule.class, ForbiddenCallRule.class, IssueOnEachFileRule.class, TrivialEvaluateRule.class); @@ -79,6 +81,7 @@ public void define(Context context) { // Optionally define remediation costs Map remediationCosts = new HashMap<>(); + remediationCosts.put("ForbiddenCopy", "5min"); remediationCosts.put("ForbiddenCall", "5min"); remediationCosts.put("IssueOnEachFile", "5min"); remediationCosts.put("TrivialEvaluate", "5min"); diff --git a/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRule.java b/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRule.java new file mode 100644 index 0000000..1354d31 --- /dev/null +++ b/cobol-custom-rules/src/main/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRule.java @@ -0,0 +1,23 @@ +package com.mycompany.cobol.sample.checks; + +import com.sonarsource.cobol.api.ast.CobolCheck; +import com.sonarsource.cobol.api.pp.CopyPreprocessorEvent; +import com.sonarsource.cobol.api.pp.CopyPreprocessorEventContext; +import org.sonar.check.Priority; +import org.sonar.check.Rule; + +@Rule(key = "ForbiddenCopy", + name = "COPY statements should not be used", + priority = Priority.CRITICAL, + tags = {"bug"}) +public class ForbiddenCopyRule extends CobolCheck implements CopyPreprocessorEvent { + + private static final String MESSAGE = "Don't use COPY!"; + + @Override + public void onCopyPreprocessorEvent(CopyPreprocessorEventContext context) { + reportIssue(MESSAGE).on(context.tokens().get(0)); + } + +} + diff --git a/cobol-custom-rules/src/main/resources/org/sonar/l10n/cobol/rules/cobol/ForbiddenCopy.html b/cobol-custom-rules/src/main/resources/org/sonar/l10n/cobol/rules/cobol/ForbiddenCopy.html new file mode 100644 index 0000000..0d8675b --- /dev/null +++ b/cobol-custom-rules/src/main/resources/org/sonar/l10n/cobol/rules/cobol/ForbiddenCopy.html @@ -0,0 +1,9 @@ +

COPY should not be used

+

Noncompliant Code Example

+
+TO DO 
+
+

Compliant Solution

+
+TO DO 
+
diff --git a/cobol-custom-rules/src/test/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRuleTest.java b/cobol-custom-rules/src/test/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRuleTest.java new file mode 100644 index 0000000..95d2231 --- /dev/null +++ b/cobol-custom-rules/src/test/java/com/mycompany/cobol/sample/checks/ForbiddenCopyRuleTest.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2009-2016 SonarSource SA + * All rights reserved + * mailto:contact AT sonarsource DOT com + */ +package com.mycompany.cobol.sample.checks; + +import com.sonarsource.cobol.squid.CobolConfiguration; +import com.sonarsource.cobol.testing.checks.CobolCheckVerifier; +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Test; + +public class ForbiddenCopyRuleTest { + + @Test + public void testVisitNode() { + ForbiddenCopyRule check = new ForbiddenCopyRule(); + CobolConfiguration conf = CobolCheckVerifier.defaultConfig(); + conf.setPreprocessorListeners(Collections.singletonList(check)); + CobolCheckVerifier.verify( + new File("src/test/resources/checks/SRC/ForbiddenCopy.cbl"), conf, check); + } + +} diff --git a/cobol-custom-rules/src/test/resources/checks/SRC/ForbiddenCopy.cbl b/cobol-custom-rules/src/test/resources/checks/SRC/ForbiddenCopy.cbl new file mode 100644 index 0000000..41aa553 --- /dev/null +++ b/cobol-custom-rules/src/test/resources/checks/SRC/ForbiddenCopy.cbl @@ -0,0 +1,4 @@ + IDENTIFICATION DIVISION. + PROCEDURE DIVISION. + COPY MyCopyBook. *> Noncompliant + DISPLAY "FORBIDDEN42".