mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
codan: added AbstractCElementChecker class
this allows to run on translation unit but without index locking useful for externl tools Change-Id: If0788eee7d322aa4cafb7985e0c8f742c31369ae Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
This commit is contained in:
parent
6bd7355e47
commit
dd528c19b6
2 changed files with 74 additions and 39 deletions
|
@ -0,0 +1,58 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2015 QNX Software Systems
|
||||||
|
* 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
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Alena Laskavaia - initial API and implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.cdt.codan.core.cxx.model;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
|
||||||
|
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
|
||||||
|
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||||
|
import org.eclipse.cdt.core.model.CoreModel;
|
||||||
|
import org.eclipse.cdt.core.model.ICElement;
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
import org.eclipse.core.resources.IResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of IChecker that works with translation unit without locking
|
||||||
|
* the index
|
||||||
|
*
|
||||||
|
* Clients may extend this class.
|
||||||
|
* @since 3.3
|
||||||
|
*/
|
||||||
|
public abstract class AbstractCElementChecker extends AbstractCheckerWithProblemPreferences implements ICIndexChecker {
|
||||||
|
@Override
|
||||||
|
public synchronized boolean processResource(IResource resource) {
|
||||||
|
ICElement model = CoreModel.getDefault().create(resource);
|
||||||
|
if (!(model instanceof ITranslationUnit))
|
||||||
|
return true; // not a C/C++ file
|
||||||
|
ITranslationUnit tu = (ITranslationUnit) model;
|
||||||
|
processTranslationUnitUnlocked(tu);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void processTranslationUnitUnlocked(ITranslationUnit tu) {
|
||||||
|
processUnit(tu);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initPreferences(IProblemWorkingCopy problem) {
|
||||||
|
getTopLevelPreference(problem); // initialize
|
||||||
|
getLaunchModePreference(problem).enableInLaunchModes(
|
||||||
|
CheckerLaunchMode.RUN_ON_FILE_OPEN,
|
||||||
|
CheckerLaunchMode.RUN_ON_FILE_SAVE,
|
||||||
|
CheckerLaunchMode.RUN_ON_DEMAND,
|
||||||
|
CheckerLaunchMode.RUN_ON_FULL_BUILD,
|
||||||
|
CheckerLaunchMode.RUN_ON_INC_BUILD);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean runInEditor() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,14 +11,10 @@
|
||||||
package org.eclipse.cdt.codan.core.cxx.model;
|
package org.eclipse.cdt.codan.core.cxx.model;
|
||||||
|
|
||||||
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
||||||
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
import org.eclipse.cdt.core.model.CoreModel;
|
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IResource;
|
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,7 +22,7 @@ import org.eclipse.core.runtime.CoreException;
|
||||||
*
|
*
|
||||||
* Clients may extend this class.
|
* Clients may extend this class.
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractCIndexChecker extends AbstractCheckerWithProblemPreferences implements ICIndexChecker {
|
public abstract class AbstractCIndexChecker extends AbstractCElementChecker implements ICIndexChecker {
|
||||||
private IFile file;
|
private IFile file;
|
||||||
protected IIndex index;
|
protected IIndex index;
|
||||||
|
|
||||||
|
@ -34,43 +30,24 @@ public abstract class AbstractCIndexChecker extends AbstractCheckerWithProblemPr
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void processFile(IFile file) throws CoreException, InterruptedException {
|
@Override
|
||||||
// create translation unit and access index
|
protected void processTranslationUnitUnlocked(ITranslationUnit tu) {
|
||||||
ICElement model = CoreModel.getDefault().create(file);
|
try {
|
||||||
if (!(model instanceof ITranslationUnit))
|
|
||||||
return; // not a C/C++ file
|
|
||||||
ITranslationUnit tu = (ITranslationUnit) model;
|
|
||||||
index = CCorePlugin.getIndexManager().getIndex(tu.getCProject());
|
index = CCorePlugin.getIndexManager().getIndex(tu.getCProject());
|
||||||
// lock the index for read access
|
// lock the index for read access
|
||||||
index.acquireReadLock();
|
index.acquireReadLock();
|
||||||
try {
|
try {
|
||||||
// traverse the translation unit using the visitor pattern.
|
// traverse the translation unit using the visitor pattern.
|
||||||
this.file = file;
|
this.file = tu.getFile();
|
||||||
processUnit(tu);
|
processUnit(tu);
|
||||||
} finally {
|
} finally {
|
||||||
this.file = null;
|
this.file = null;
|
||||||
index.releaseReadLock();
|
index.releaseReadLock();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized boolean processResource(IResource resource) {
|
|
||||||
if (resource instanceof IFile) {
|
|
||||||
IFile file = (IFile) resource;
|
|
||||||
try {
|
|
||||||
processFile(file);
|
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
CodanCorePlugin.log(e);
|
CodanCorePlugin.log(e);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean runInEditor() {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue