mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Streamlined check enablement logic.
This commit is contained in:
parent
c554c4e707
commit
00a6c5069f
4 changed files with 55 additions and 101 deletions
|
@ -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
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -39,8 +39,8 @@ public abstract class AbstractChecker implements IChecker {
|
|||
|
||||
@Deprecated
|
||||
@Override
|
||||
public boolean enabledInContext(IResource res) {
|
||||
return res.getType() == IResource.FILE;
|
||||
public boolean enabledInContext(IResource resource) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -204,17 +204,15 @@ public abstract class AbstractChecker implements IChecker {
|
|||
*/
|
||||
@Override
|
||||
public void before(IResource resource) {
|
||||
IProblemReporter problemReporter = CodanRuntime.getInstance().getProblemReporter();
|
||||
this.problemReporter = problemReporter;
|
||||
if (problemReporter instanceof IProblemReporterSessionPersistent) {
|
||||
// create session problem reporter
|
||||
this.problemReporter = ((IProblemReporterSessionPersistent) problemReporter).createReporter(resource, this);
|
||||
((IProblemReporterSessionPersistent) this.problemReporter).start();
|
||||
} else if (problemReporter instanceof IProblemReporterPersistent) {
|
||||
// delete markers if checker can possibly run on this
|
||||
// resource this way if checker is not enabled markers would be
|
||||
// deleted too
|
||||
((IProblemReporterPersistent) problemReporter).deleteProblems(resource, this);
|
||||
IProblemReporter reporter = CodanRuntime.getInstance().getProblemReporter();
|
||||
problemReporter = reporter;
|
||||
if (reporter instanceof IProblemReporterSessionPersistent) {
|
||||
// Create session problem reporter
|
||||
problemReporter = ((IProblemReporterSessionPersistent) reporter).createReporter(resource, this);
|
||||
((IProblemReporterSessionPersistent) problemReporter).start();
|
||||
} else if (reporter instanceof IProblemReporterPersistent) {
|
||||
// Delete markers.
|
||||
((IProblemReporterPersistent) reporter).deleteProblems(resource, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,7 +222,7 @@ public abstract class AbstractChecker implements IChecker {
|
|||
@Override
|
||||
public void after(IResource resource) {
|
||||
if (problemReporter instanceof IProblemReporterSessionPersistent) {
|
||||
// Delete general markers
|
||||
// Delete general markers.
|
||||
((IProblemReporterSessionPersistent) problemReporter).done();
|
||||
}
|
||||
problemReporter = null;
|
||||
|
|
|
@ -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
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -29,6 +29,14 @@ import org.eclipse.core.runtime.OperationCanceledException;
|
|||
* Extend {@link AbstractChecker} class instead.
|
||||
*/
|
||||
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
|
||||
*
|
||||
|
@ -44,15 +52,7 @@ public interface IChecker {
|
|||
throws OperationCanceledException;
|
||||
|
||||
/**
|
||||
* Called before 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.
|
||||
* Called after processing a resource.
|
||||
*
|
||||
* @param resource the resource that has been processed.
|
||||
* @since 2.0
|
||||
|
@ -66,14 +66,10 @@ public interface IChecker {
|
|||
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.
|
||||
* @return true if checker should be run on this resource.
|
||||
* @deprecated Replaced by {@code CheckersRegistry.isCheckerEnabled((IChecker IResource, CheckerLaunchMode)}
|
||||
* and {@code ICheckerEnablementVerifier.isCheckerEnabled(IChecker IResource, CheckerLaunchMode)}.
|
||||
* @deprecated Ignored since 2.0. Replaced by
|
||||
* {@code CheckersRegistry.isCheckerEnabled((IChecker, IResource, CheckerLaunchMode)}
|
||||
*/
|
||||
@Deprecated
|
||||
boolean enabledInContext(IResource resource);
|
||||
|
@ -86,8 +82,7 @@ public interface IChecker {
|
|||
* Checker should return false if check is non-trivial and takes a long
|
||||
* time.
|
||||
*
|
||||
* @return true if need to be run in editor as user types, and false
|
||||
* otherwise
|
||||
* @return true if need to be run in editor as user types, and false otherwise
|
||||
*/
|
||||
boolean runInEditor();
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
|||
import org.eclipse.cdt.codan.core.PreferenceConstants;
|
||||
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
|
||||
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.IChecker;
|
||||
import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences;
|
||||
|
@ -446,31 +447,6 @@ public class CheckersRegistry implements Iterable<IChecker>, ICheckersRegistry {
|
|||
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.
|
||||
*
|
||||
|
@ -483,29 +459,35 @@ public class CheckersRegistry implements Iterable<IChecker>, ICheckersRegistry {
|
|||
if (resource.getType() != IResource.FILE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mode == CheckerLaunchMode.RUN_AS_YOU_TYPE && !Checkers.canCheckerRunAsYouType(checker)) {
|
||||
return false;
|
||||
}
|
||||
for (ICheckerEnablementVerifier verifier : checkerEnablementVerifiers) {
|
||||
if (!verifier.isCheckerEnabled(checker, resource, mode)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
IProblemProfile resourceProfile = getResourceProfile(resource);
|
||||
Collection<IProblem> refProblems = getRefProblems(checker);
|
||||
boolean enabled = false;
|
||||
for (Iterator<IProblem> iterator = refProblems.iterator(); iterator.hasNext();) {
|
||||
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());
|
||||
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) {
|
||||
LaunchModeProblemPreference pref = ((AbstractCheckerWithProblemPreferences) checker).getLaunchModePreference(problem);
|
||||
LaunchModeProblemPreference pref =
|
||||
((AbstractCheckerWithProblemPreferences) checker).getLaunchModePreference(problem);
|
||||
if (pref.isRunningInMode(mode)) {
|
||||
enabled = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return enabled;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* 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.Messages;
|
||||
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.ICheckerInvocationContext;
|
||||
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 IProgressMonitor monitor;
|
||||
|
||||
/**
|
||||
* @param monitor
|
||||
*/
|
||||
public CodanDeltaVisitor(IProgressMonitor monitor) {
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
@ -56,28 +52,22 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
|
|||
IResource resource = delta.getResource();
|
||||
switch (delta.getKind()) {
|
||||
case IResourceDelta.ADDED:
|
||||
// handle added resource
|
||||
// Handle added resource
|
||||
processResourceDelta(resource, monitor);
|
||||
break;
|
||||
case IResourceDelta.REMOVED:
|
||||
// handle removed resource
|
||||
// Handle removed resource
|
||||
break;
|
||||
case IResourceDelta.CHANGED:
|
||||
// handle changed resource
|
||||
// Handle changed resource
|
||||
processResourceDelta(resource, monitor);
|
||||
break;
|
||||
}
|
||||
// return true to continue visiting children.
|
||||
// Return true to continue visiting children.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.core.internal.events.InternalBuilder#build(int,
|
||||
* java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
int checkers = chegistry.getCheckersSize();
|
||||
int memsize = 0;
|
||||
|
@ -139,13 +130,10 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
|
|||
try {
|
||||
if (monitor.isCanceled())
|
||||
return;
|
||||
if (doesCheckerSupportLaunchMode(checker, checkerLaunchMode)
|
||||
&& chegistry.isCheckerEnabled(checker, resource, checkerLaunchMode)) {
|
||||
if (chegistry.isCheckerEnabled(checker, resource, checkerLaunchMode)) {
|
||||
synchronized (checker) {
|
||||
try {
|
||||
checker.before(resource);
|
||||
if (chegistry.isCheckerEnabled(checker, resource)) {
|
||||
try {
|
||||
CheckersTimeStats.getInstance().checkerStart(checker.getClass().getName());
|
||||
if (checkerLaunchMode == CheckerLaunchMode.RUN_AS_YOU_TYPE) {
|
||||
((IRunnableInEditorChecker) checker).processModel(model, context);
|
||||
|
@ -154,9 +142,6 @@ public class CodanBuilder extends IncrementalProjectBuilder implements ICodanBui
|
|||
}
|
||||
} finally {
|
||||
CheckersTimeStats.getInstance().checkerStop(checker.getClass().getName());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
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 {
|
||||
processResource(getProject(), monitor);
|
||||
}
|
||||
|
||||
protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException {
|
||||
// the visitor does the work.
|
||||
// The visitor does the work.
|
||||
delta.accept(new CodanDeltaVisitor(monitor));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue