Search code examples
eclipse-plugineclipse-cdt

Deactivate all Codan Checkers programmatically


How can I deactivate all Codan Checkers for my content type or inside my editor programmatically?

I know I can turn off the Checkers in Window -> Preferences -> C/C++ -> Code Analysis. But I need to do that programmatically.

One way to achieve that is to modify the methods runInEditor() and processResource() in org.eclipse.cdt.codan.internal.core.CodanRunner.

public static void runInEditor(Object model, IResource resource, IProgressMonitor monitor) {
    if (resource != null && !resource.toString().endsWith("blub)) {
        processResource(resource, model, CheckerLaunchMode.RUN_AS_YOU_TYPE, monitor);
    }
}

public static void processResource(IResource resource, CheckerLaunchMode checkerLaunchMode, IProgressMonitor monitor) {
    if (resource != null && !resource.toString().endsWith("blub")) {
        processResource(resource, null, checkerLaunchMode, monitor);
    }
}

For the Unresolved Inclusion warning I can overwrite CPreprocessor and return do nothing in the overriden handleProblem() method.

Is there a way to suppress the Codan Checkers without modifying CDT code?


Solution

  • You should be able to do this using the org.eclipse.cdt.codan.core.checkerEnablement extension point.

    I can't find generated documentation for it, but you can see the schema for it here.

    The extension point allows you to specify a class inheriting from org.eclipse.cdt.codan.internal.core.ICheckerEnablementVerifier and provide a method boolean isCheckerEnabled(IChecker, IResource, CheckerLaunchMode) which determines if the given checker can run on the given resource in the given launch mode. If any enablement verifier returns false, the checker is not run.

    Register your own implementation of ICheckerEnablementVerifier in your Plugin's plugin.xml:

    <extension
        point="org.eclipse.cdt.codan.core.checkerEnablement">
      <verifier class="path.to.MyCheckerEnablementVerifier" />
    </extension>
    

    The following implementation checks the content type:

    public class MyCheckerEnablementVerifier implements ICheckerEnablementVerifier {
    
        @Override
        public boolean isCheckerEnabled(IChecker checker, IResource resource, CheckerLaunchMode mode) {
            IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
            IContentType contentType = contentTypeManager.findContentTypeFor(resource.getLocation().toOSString());
            if (contentType.getId().equals("contenttypeid")) {
                return false;
            } else {
                return true;
            }
        }
    }