Sonar - Cobol -Plugin Development

We are trying to add more custom rules through Plugin in our Sonar Profile for analysis of Cobol programs.

As a first step, to understand the AST, I started using SSLR tool kit. I observed that no tokens are used to represent the COPY statements. Is there any specific reason for this? I have a requirement to collect the copybooks used and do some validation on them. Since no tokens are representing COPY statements, should I go for function calls like “getCopyBookOriginalLine”.

Thanks,
Ramapriya

Hello @Ramapriya ,

Sorry for the late answer.

I observed that no tokens are used to represent the COPY statements. Is there any specific reason for this?

COPY statements are pre-processed to be directly replaced with the content of the targeted copybook.

If you want to collect COPY statements, your check could implement CobolPreprocessorEvent interface, which exposes the following method:

void onCopyPreprocessorEvent(CopyPreprocessorEventContext context)

CopyPreprocessorEventContext contains the information about copybooks.

Hope that helps,
Andrea G.

@Andrea_Guarino Thanks a lot for explaining. This helped me a lot. Thanks Again!

@Andrea_Guarino Trying to test the implementation by incorporating this function in our script. Not able to find any associated Javadoc. Included the package “com.sonarsource.cobol.api.pp.CobolPreprocessorEvent” in the import statement and also, implemented the CobolPreprocessorEvent Interface. But not sure, what is the Argument type to be passed to the method “void onCopyPreprocessorEvent(CopyPreprocessorEventContext context)”. I’m not sure how to resolve the type context. Please help me by providing more information regarding this method or link to the Javadoc regarding the same.

Team, please help me with information on "onCopyPreprocessorEvent(CopyPreprocessorEventContext context)”.

Thank in Advance.

Hi,

You should indeed implement CopyPreprocessorEvent and override the following method:
onCopyPreprocessorEvent(CopyPreprocessorEventContext context)

When you override that method, you can call the tokens method on the context parameter (of type CopyPreprocessorEventContext). It will give you the tokens of the COPY statement.