1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-07 00:05:53 +02:00

- optimized process of non-editor files

This commit is contained in:
Alena Laskavaia 2010-04-18 19:10:11 +00:00
parent 3e9949a4eb
commit e4694d3d57
3 changed files with 89 additions and 24 deletions

View file

@ -15,14 +15,10 @@ import org.eclipse.cdt.codan.core.cxx.Activator;
import org.eclipse.cdt.codan.core.model.AbstractChecker; import org.eclipse.cdt.codan.core.model.AbstractChecker;
import org.eclipse.cdt.codan.core.model.IProblemLocation; import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker; import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
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.internal.core.resources.ResourceLookup; import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
@ -44,24 +40,19 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
protected IFile getFile() { protected IFile getFile() {
return file; return file;
} }
protected IProject getProject() { protected IProject getProject() {
return file==null?null:file.getProject(); return file == null ? null : file.getProject();
} }
void processFile(IFile file) throws CoreException, InterruptedException { void processFile(IFile file) throws CoreException, InterruptedException {
// create translation unit and access index IASTTranslationUnit ast = CxxModelsCache.getInstance().getAst(file);
ICElement model = CoreModel.getDefault().create(file); if (ast == null)
if (!(model instanceof ITranslationUnit)) return;
return; // not a C/C++ file
ITranslationUnit tu = (ITranslationUnit) model;
IIndex index = CCorePlugin.getIndexManager().getIndex(tu.getCProject());
// lock the index for read access // lock the index for read access
IIndex index = CxxModelsCache.getInstance().getIndex(file);
index.acquireReadLock(); index.acquireReadLock();
try { try {
// create index based ast
IASTTranslationUnit ast = tu.getAST(index,
ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
if (ast==null) return;//
// traverse the ast using the visitor pattern. // traverse the ast using the visitor pattern.
this.file = file; this.file = file;
processAst(ast); processAst(ast);
@ -90,18 +81,18 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
public void reportProblem(String id, IASTNode astNode, Object... args) { public void reportProblem(String id, IASTNode astNode, Object... args) {
IASTFileLocation astLocation = astNode.getFileLocation(); IASTFileLocation astLocation = astNode.getFileLocation();
IPath location = new Path(astLocation.getFileName()); IPath location = new Path(astLocation.getFileName());
IFile astFile = ResourceLookup.selectFileForLocation(location, getProject()); IFile astFile = ResourceLookup.selectFileForLocation(location,
getProject());
if (astFile == null) { if (astFile == null) {
astFile = file; astFile = file;
} }
if (astFile == null) { if (astFile == null) {
Activator.log("Cannot resolve location: "+location); //$NON-NLS-1$ Activator.log("Cannot resolve location: " + location); //$NON-NLS-1$
return; return;
} }
IProblemLocation loc; IProblemLocation loc;
int line = astLocation.getStartingLineNumber(); int line = astLocation.getStartingLineNumber();
if (line == astLocation if (line == astLocation.getEndingLineNumber())
.getEndingLineNumber())
loc = getRuntime().getProblemLocationFactory() loc = getRuntime().getProblemLocationFactory()
.createProblemLocation( .createProblemLocation(
astFile, astFile,
@ -110,8 +101,7 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
+ astLocation.getNodeLength(), line); + astLocation.getNodeLength(), line);
else else
loc = getRuntime().getProblemLocationFactory() loc = getRuntime().getProblemLocationFactory()
.createProblemLocation(astFile, .createProblemLocation(astFile, line);
line);
getProblemReporter().reportProblem(id, loc, args); getProblemReporter().reportProblem(id, loc, args);
} }
@ -128,11 +118,12 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
* (java.lang.Object) * (java.lang.Object)
*/ */
@SuppressWarnings("restriction") @SuppressWarnings("restriction")
public void processModel(Object model) { public synchronized void processModel(Object model) {
if (model instanceof IASTTranslationUnit) { if (model instanceof IASTTranslationUnit) {
IASTTranslationUnit ast = (IASTTranslationUnit) model; IASTTranslationUnit ast = (IASTTranslationUnit) model;
IPath location = new Path(ast.getFilePath()); IPath location = new Path(ast.getFilePath());
IFile astFile = ResourceLookup.selectFileForLocation(location, getProject()); IFile astFile = ResourceLookup.selectFileForLocation(location,
getProject());
file = astFile; file = astFile;
processAst(ast); processAst(ast);
} }

View file

@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2009 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
* 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.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
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.runtime.CoreException;
/**
* Cache data models for resource so checkers can share it
*/
public class CxxModelsCache {
private IFile file;
private IASTTranslationUnit ast;
private ITranslationUnit tu;
private IIndex index;
private static CxxModelsCache instance = new CxxModelsCache();
public static CxxModelsCache getInstance() {
return instance;
}
public synchronized IASTTranslationUnit getAst(IFile file)
throws CoreException, InterruptedException {
if (file.equals(this.file)) {
return ast;
}
// create translation unit and access index
ICElement celement = CoreModel.getDefault().create(file);
if (!(celement instanceof ITranslationUnit))
return null; // not a C/C++ file
this.file = file;
System.err.println("Making ast for "+file);
tu = (ITranslationUnit) celement;
index = CCorePlugin.getIndexManager().getIndex(tu.getCProject());
// lock the index for read access
index.acquireReadLock();
try {
// create index based ast
ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
if (ast == null)
return null;//
return ast;
} finally {
index.releaseReadLock();
}
}
public synchronized IIndex getIndex(IFile file)
throws CoreException, InterruptedException {
if (file.equals(this.file)) {
return index;
}
getAst(file); // to init variables
return index;
}
}

View file

@ -115,6 +115,8 @@ public class CodanBuilder extends IncrementalProjectBuilder implements
CheckersRegisry chegistry = CheckersRegisry.getInstance(); CheckersRegisry chegistry = CheckersRegisry.getInstance();
for (IChecker checker : chegistry) { for (IChecker checker : chegistry) {
try { try {
if (monitor.isCanceled())
return;
if (chegistry.isCheckerEnabled(checker, resource) if (chegistry.isCheckerEnabled(checker, resource)
&& checker.enabledInContext(resource)) { && checker.enabledInContext(resource)) {
checker.processResource(resource); checker.processResource(resource);