1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Streamlined check enablement logic.

This commit is contained in:
Sergey Prigogin 2012-04-28 20:20:47 -07:00
parent c554c4e707
commit 00a6c5069f
4 changed files with 55 additions and 101 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -39,8 +39,8 @@ public abstract class AbstractChecker implements IChecker {
@Deprecated @Deprecated
@Override @Override
public boolean enabledInContext(IResource res) { public boolean enabledInContext(IResource resource) {
return res.getType() == IResource.FILE; return false;
} }
/** /**
@ -204,17 +204,15 @@ public abstract class AbstractChecker implements IChecker {
*/ */
@Override @Override
public void before(IResource resource) { public void before(IResource resource) {
IProblemReporter problemReporter = CodanRuntime.getInstance().getProblemReporter(); IProblemReporter reporter = CodanRuntime.getInstance().getProblemReporter();
this.problemReporter = problemReporter; problemReporter = reporter;
if (problemReporter instanceof IProblemReporterSessionPersistent) { if (reporter instanceof IProblemReporterSessionPersistent) {
// create session problem reporter // Create session problem reporter
this.problemReporter = ((IProblemReporterSessionPersistent) problemReporter).createReporter(resource, this); problemReporter = ((IProblemReporterSessionPersistent) reporter).createReporter(resource, this);
((IProblemReporterSessionPersistent) this.problemReporter).start(); ((IProblemReporterSessionPersistent) problemReporter).start();
} else if (problemReporter instanceof IProblemReporterPersistent) { } else if (reporter instanceof IProblemReporterPersistent) {
// delete markers if checker can possibly run on this // Delete markers.
// resource this way if checker is not enabled markers would be ((IProblemReporterPersistent) reporter).deleteProblems(resource, this);
// deleted too
((IProblemReporterPersistent) problemReporter).deleteProblems(resource, this);
} }
} }
@ -224,7 +222,7 @@ public abstract class AbstractChecker implements IChecker {
@Override @Override
public void after(IResource resource) { public void after(IResource resource) {
if (problemReporter instanceof IProblemReporterSessionPersistent) { if (problemReporter instanceof IProblemReporterSessionPersistent) {
// Delete general markers // Delete general markers.
((IProblemReporterSessionPersistent) problemReporter).done(); ((IProblemReporterSessionPersistent) problemReporter).done();
} }
problemReporter = null; problemReporter = null;

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -29,6 +29,14 @@ import org.eclipse.core.runtime.OperationCanceledException;
* Extend {@link AbstractChecker} class instead. * Extend {@link AbstractChecker} class instead.
*/ */
public interface IChecker { public interface IChecker {
/**
* Called before processing a resource.
*
* @param resource the resource that is about to be processed.
* @since 2.0
*/
void before(IResource resource);
/** /**
* Main method that checker should implement that actually detects errors * Main method that checker should implement that actually detects errors
* *
@ -44,15 +52,7 @@ public interface IChecker {
throws OperationCanceledException; throws OperationCanceledException;
/** /**
* Called before processing a resource. * Called after processing a resource.
*
* @param resource the resource that is about to be processed.
* @since 2.0
*/
void before(IResource resource);
/**
* Called before processing a resource.
* *
* @param resource the resource that has been processed. * @param resource the resource that has been processed.
* @since 2.0 * @since 2.0
@ -66,14 +66,10 @@ public interface IChecker {
IProblemReporter getProblemReporter(); IProblemReporter getProblemReporter();
/** /**
* Implement this method to trim down type of resource you are interested
* in, usually it will be c/c++ files only. This method should be
* independent from current user preferences.
*
* @param resource the resource to run on. * @param resource the resource to run on.
* @return true if checker should be run on this resource. * @return true if checker should be run on this resource.
* @deprecated Replaced by {@code CheckersRegistry.isCheckerEnabled((IChecker IResource, CheckerLaunchMode)} * @deprecated Ignored since 2.0. Replaced by
* and {@code ICheckerEnablementVerifier.isCheckerEnabled(IChecker IResource, CheckerLaunchMode)}. * {@code CheckersRegistry.isCheckerEnabled((IChecker, IResource, CheckerLaunchMode)}
*/ */
@Deprecated @Deprecated
boolean enabledInContext(IResource resource); boolean enabledInContext(IResource resource);
@ -86,8 +82,7 @@ public interface IChecker {
* Checker should return false if check is non-trivial and takes a long * Checker should return false if check is non-trivial and takes a long
* time. * time.
* *
* @return true if need to be run in editor as user types, and false * @return true if need to be run in editor as user types, and false otherwise
* otherwise
*/ */
boolean runInEditor(); boolean runInEditor();
} }

View file

@ -22,6 +22,7 @@ import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.PreferenceConstants; import org.eclipse.cdt.codan.core.PreferenceConstants;
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences; import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode; import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.core.model.Checkers;
import org.eclipse.cdt.codan.core.model.CodanSeverity; import org.eclipse.cdt.codan.core.model.CodanSeverity;
import org.eclipse.cdt.codan.core.model.IChecker; import org.eclipse.cdt.codan.core.model.IChecker;
import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences; import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences;
@ -446,31 +447,6 @@ public class CheckersRegistry implements Iterable<IChecker>, ICheckersRegistry {
return prof; return prof;
} }
/**
* Tests if a checker is enabled (needs to be run) or not. Checker is
* enabled
* if at least one problem it reports is enabled.
*
* @param checker
* @param resource
* @return <code>true</code> if the checker is enabled
*/
public boolean isCheckerEnabled(IChecker checker, IResource resource) {
IProblemProfile resourceProfile = getResourceProfile(resource);
Collection<IProblem> refProblems = getRefProblems(checker);
for (Iterator<IProblem> iterator = refProblems.iterator(); iterator.hasNext();) {
IProblem p = iterator.next();
// we need to check problem enablement in particular profile
IProblem problem = resourceProfile.findProblem(p.getId());
if (problem == null)
throw new IllegalArgumentException("Id is not registered"); //$NON-NLS-1$
if (problem.isEnabled())
return true;
}
// no problem is enabled for this checker, skip the checker
return false;
}
/** /**
* Tests if a checker needs to run in a specific launch mode. * Tests if a checker needs to run in a specific launch mode.
* *
@ -483,29 +459,35 @@ public class CheckersRegistry implements Iterable<IChecker>, ICheckersRegistry {
if (resource.getType() != IResource.FILE) { if (resource.getType() != IResource.FILE) {
return false; return false;
} }
if (mode == CheckerLaunchMode.RUN_AS_YOU_TYPE && !Checkers.canCheckerRunAsYouType(checker)) {
return false;
}
for (ICheckerEnablementVerifier verifier : checkerEnablementVerifiers) { for (ICheckerEnablementVerifier verifier : checkerEnablementVerifiers) {
if (!verifier.isCheckerEnabled(checker, resource, mode)) { if (!verifier.isCheckerEnabled(checker, resource, mode)) {
return false; return false;
} }
} }
IProblemProfile resourceProfile = getResourceProfile(resource); IProblemProfile resourceProfile = getResourceProfile(resource);
Collection<IProblem> refProblems = getRefProblems(checker); Collection<IProblem> refProblems = getRefProblems(checker);
boolean enabled = false;
for (Iterator<IProblem> iterator = refProblems.iterator(); iterator.hasNext();) { for (Iterator<IProblem> iterator = refProblems.iterator(); iterator.hasNext();) {
IProblem p = iterator.next(); IProblem p = iterator.next();
// we need to check problem enablement in particular profile // We need to check problem enablement in a particular profile.
IProblem problem = resourceProfile.findProblem(p.getId()); IProblem problem = resourceProfile.findProblem(p.getId());
if (problem == null) if (problem == null)
throw new IllegalArgumentException("Id is not registered"); //$NON-NLS-1$ throw new IllegalArgumentException(p.getId() + "is not registered"); //$NON-NLS-1$
if (!problem.isEnabled())
return false;
if (checker instanceof AbstractCheckerWithProblemPreferences) { if (checker instanceof AbstractCheckerWithProblemPreferences) {
LaunchModeProblemPreference pref = ((AbstractCheckerWithProblemPreferences) checker).getLaunchModePreference(problem); LaunchModeProblemPreference pref =
((AbstractCheckerWithProblemPreferences) checker).getLaunchModePreference(problem);
if (pref.isRunningInMode(mode)) { if (pref.isRunningInMode(mode)) {
enabled = true; return true;
break;
} }
} }
} }
return enabled; return false;
} }
/** /**

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2011 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -16,7 +16,6 @@ import java.util.Map;
import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.Messages; import org.eclipse.cdt.codan.core.Messages;
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode; import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.core.model.Checkers;
import org.eclipse.cdt.codan.core.model.IChecker; import org.eclipse.cdt.codan.core.model.IChecker;
import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext; import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext;
import org.eclipse.cdt.codan.core.model.ICodanBuilder; import org.eclipse.cdt.codan.core.model.ICodanBuilder;
@ -44,9 +43,6 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
private class CodanDeltaVisitor implements IResourceDeltaVisitor { private class CodanDeltaVisitor implements IResourceDeltaVisitor {
private IProgressMonitor monitor; private IProgressMonitor monitor;
/**
* @param monitor
*/
public CodanDeltaVisitor(IProgressMonitor monitor) { public CodanDeltaVisitor(IProgressMonitor monitor) {
this.monitor = monitor; this.monitor = monitor;
} }
@ -56,28 +52,22 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
IResource resource = delta.getResource(); IResource resource = delta.getResource();
switch (delta.getKind()) { switch (delta.getKind()) {
case IResourceDelta.ADDED: case IResourceDelta.ADDED:
// handle added resource // Handle added resource
processResourceDelta(resource, monitor); processResourceDelta(resource, monitor);
break; break;
case IResourceDelta.REMOVED: case IResourceDelta.REMOVED:
// handle removed resource // Handle removed resource
break; break;
case IResourceDelta.CHANGED: case IResourceDelta.CHANGED:
// handle changed resource // Handle changed resource
processResourceDelta(resource, monitor); processResourceDelta(resource, monitor);
break; break;
} }
// return true to continue visiting children. // Return true to continue visiting children.
return true; return true;
} }
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.core.internal.events.InternalBuilder#build(int,
* java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
*/
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
@ -116,7 +106,8 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
processResource(resource, monitor, CheckerLaunchMode.RUN_ON_INC_BUILD); processResource(resource, monitor, CheckerLaunchMode.RUN_ON_INC_BUILD);
} }
protected void processResource(IResource resource, IProgressMonitor monitor, Object model, CheckerLaunchMode checkerLaunchMode) { protected void processResource(IResource resource, IProgressMonitor monitor, Object model,
CheckerLaunchMode checkerLaunchMode) {
CheckersRegistry chegistry = CheckersRegistry.getInstance(); CheckersRegistry chegistry = CheckersRegistry.getInstance();
int checkers = chegistry.getCheckersSize(); int checkers = chegistry.getCheckersSize();
int memsize = 0; int memsize = 0;
@ -139,13 +130,10 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
try { try {
if (monitor.isCanceled()) if (monitor.isCanceled())
return; return;
if (doesCheckerSupportLaunchMode(checker, checkerLaunchMode) if (chegistry.isCheckerEnabled(checker, resource, checkerLaunchMode)) {
&& chegistry.isCheckerEnabled(checker, resource, checkerLaunchMode)) {
synchronized (checker) { synchronized (checker) {
try { try {
checker.before(resource); checker.before(resource);
if (chegistry.isCheckerEnabled(checker, resource)) {
try {
CheckersTimeStats.getInstance().checkerStart(checker.getClass().getName()); CheckersTimeStats.getInstance().checkerStart(checker.getClass().getName());
if (checkerLaunchMode == CheckerLaunchMode.RUN_AS_YOU_TYPE) { if (checkerLaunchMode == CheckerLaunchMode.RUN_AS_YOU_TYPE) {
((IRunnableInEditorChecker) checker).processModel(model, context); ((IRunnableInEditorChecker) checker).processModel(model, context);
@ -154,9 +142,6 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
} }
} finally { } finally {
CheckersTimeStats.getInstance().checkerStop(checker.getClass().getName()); CheckersTimeStats.getInstance().checkerStop(checker.getClass().getName());
}
}
} finally {
checker.after(resource); checker.after(resource);
} }
} }
@ -192,18 +177,12 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
} }
} }
private boolean doesCheckerSupportLaunchMode(IChecker checker, CheckerLaunchMode mode) {
if (mode == CheckerLaunchMode.RUN_AS_YOU_TYPE)
return Checkers.canCheckerRunAsYouType(checker);
return true;
}
protected void fullBuild(final IProgressMonitor monitor) throws CoreException { protected void fullBuild(final IProgressMonitor monitor) throws CoreException {
processResource(getProject(), monitor); processResource(getProject(), monitor);
} }
protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException { protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException {
// the visitor does the work. // The visitor does the work.
delta.accept(new CodanDeltaVisitor(monitor)); delta.accept(new CodanDeltaVisitor(monitor));
} }