Properly analyze CopyBooks in a CobolCheck for Custom Checks

Hello Community,

I am currently working on custom cobol checks - some new ones and some build on top of existing ones with exemptions - and one of them is focused on copybooks which has me struggling quite a bit. The rule I want to implement is “cobol:S1683 - Copybooks should not contain keywords relating to the nature or structure of a program”. So far I implemented the onCopyPreprocessorEvent() method from the interface which gives me the copybooks, but now I don’t know how to actually go through it; I put a lot of prints in the methods and in the onCopyPreprocessorEvent() the only tokens I got from the context are the “COPY COPYBOOK.” and nothing more. I then thought I needed visitNode() too but it appears that that method is never hit.

Here is a snippet from my implementation:

@Override
public void onCopyPreprocessorEvent(CopyPreprocessorEventContext    copyPreprocessorEventContext) {
	System.out.println(“PreprocessorEvent”);
	System.out.println(copyPreprocessorEventContext.copybookName());
	if (isNotExempted(copyPreprocessorEventContext)) {
		List tokens = copyPreprocessorEventContext.tokens();
		System.out.println("Tokens: " + tokens);

		// Analyze tokens to find keywords related to program structure
		for (Token token : tokens) {
			String tokenValue = token.getValue().toUpperCase();
			System.out.println("token: " + token);
			System.out.println("TokenValue: " + tokenValue);

			// Check if token is a structural keyword
			if (STRUCTURAL_KEYWORDS.contains(tokenValue)) {
				reportIssue("Keyword related to program structure found in copybook: " + tokenValue).on(token);
			}
		}
	}

}

@Override
public void visitNode(AstNode node) {
	System.out.println(“VisitNode”);
}

And my unit test is properly set up (I think) but nothing is marked / processed as noncompliant. My unit test:

@Before
public void init() {
    config = new CobolConfiguration();
    check = new NoStrcuturalDefinitionInCopybookCheck();

    config.addCopyExtension("cpy");
    config.addCopyExtension("cbl");
    config.addLibDirectory(new File("src/test/resources/checks/COPY/"));
}

@Test
public void testNoStructuralDefinitionInCopybook() {
    check.setExemptedVariables("EXEMPTION");

    config.setPreprocessorsActivated(true);
    config.setPreprocessorListeners(Collections.singletonList(check));

    CobolCheckVerifier.verify(
          new File("src/test/resources/checks/SRC/NoStructuralDefinitionInCopybookCheck/NoStructuralDefinitionInCopybook.cbl"),
          config,
          check);
}

Edit: Versions:

  • SonarQube Enterprise Edition v2025.1.4

  • Sonarsource version 11.1.0.2693

  • Sonarcobol version 5.11.0.10062

Thanks for any help!

Hi,

Can you confirm you’re working against a current version of SonarQube?

 
Thx,
Ann

Sorry, I didn’t specify my version:

  • SonarQube Enterprise Edition v2025.1.4
  • Sonarsource version 11.1.0.2693
  • Sonarcobol version 5.11.0.10062

So far I haven’t been able to test my custom rules in SonarQube itself, only in unit tests.

Thanks,

Tim