From dd528c19b6c4ec9245c076e46ae4ea23c0914abb Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Wed, 4 Mar 2015 15:16:47 -0500 Subject: [PATCH] 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 --- .../cxx/model/AbstractCElementChecker.java | 58 +++++++++++++++++++ .../core/cxx/model/AbstractCIndexChecker.java | 55 +++++------------- 2 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCElementChecker.java diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCElementChecker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCElementChecker.java new file mode 100644 index 00000000000..b4bfbeb9943 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCElementChecker.java @@ -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; + } +} diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCIndexChecker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCIndexChecker.java index f5f2ab3094d..2aa8016e94a 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCIndexChecker.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractCIndexChecker.java @@ -11,14 +11,10 @@ package org.eclipse.cdt.codan.core.cxx.model; 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.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.core.resources.IFile; -import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; /** @@ -26,7 +22,7 @@ import org.eclipse.core.runtime.CoreException; * * Clients may extend this class. */ -public abstract class AbstractCIndexChecker extends AbstractCheckerWithProblemPreferences implements ICIndexChecker { +public abstract class AbstractCIndexChecker extends AbstractCElementChecker implements ICIndexChecker { private IFile file; protected IIndex index; @@ -34,43 +30,24 @@ public abstract class AbstractCIndexChecker extends AbstractCheckerWithProblemPr return file; } - void processFile(IFile file) throws CoreException, InterruptedException { - // create translation unit and access index - ICElement model = CoreModel.getDefault().create(file); - if (!(model instanceof ITranslationUnit)) - return; // not a C/C++ file - ITranslationUnit tu = (ITranslationUnit) model; - index = CCorePlugin.getIndexManager().getIndex(tu.getCProject()); - // lock the index for read access - index.acquireReadLock(); + @Override + protected void processTranslationUnitUnlocked(ITranslationUnit tu) { try { - // traverse the translation unit using the visitor pattern. - this.file = file; - processUnit(tu); - } finally { - this.file = null; - index.releaseReadLock(); - } - } - - @Override - public synchronized boolean processResource(IResource resource) { - if (resource instanceof IFile) { - IFile file = (IFile) resource; + index = CCorePlugin.getIndexManager().getIndex(tu.getCProject()); + // lock the index for read access + index.acquireReadLock(); try { - processFile(file); - } catch (CoreException e) { - CodanCorePlugin.log(e); - } catch (InterruptedException e) { - // ignore + // traverse the translation unit using the visitor pattern. + this.file = tu.getFile(); + processUnit(tu); + } finally { + this.file = null; + index.releaseReadLock(); } - return false; + } catch (CoreException e) { + CodanCorePlugin.log(e); + } catch (InterruptedException e) { + // ignore } - return true; - } - - @Override - public boolean runInEditor() { - return false; } }