1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 09:46:02 +02:00

bug 284830: [code analysis] Codan report markers on wrong files.

Based on patch from Francois Rigault.
This commit is contained in:
Andrew Gvozdev 2009-07-31 20:39:13 +00:00
parent 482d0431f3
commit 85230a764d
4 changed files with 26 additions and 15 deletions

View file

@ -35,9 +35,7 @@ public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
public int visit(IASTExpression expression) { public int visit(IASTExpression expression) {
if (isAssignmentExpression(expression) if (isAssignmentExpression(expression)
&& isUsedAsCondition(expression)) { && isUsedAsCondition(expression)) {
reportProblem(ER_ID, getFile(), expression.getFileLocation() reportProblem(ER_ID, expression, "Possible assignment in condition");
.getStartingLineNumber(),
"Possible assignment in condition");
} }
return PROCESS_CONTINUE; return PROCESS_CONTINUE;
} }

View file

@ -17,15 +17,14 @@ import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
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.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.core.resources.IFile;
/** /**
* Checker to find that class has virtual method and non virtual destructor * Checker to find that class has virtual method and non virtual destructor
@ -59,20 +58,17 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
String mess; String mess;
String clazz = className.toString(); String clazz = className.toString();
String method = virMethodName.getName(); String method = virMethodName.getName();
int line = 1; IASTNode ast = decl;
IFile file = getFile();
if (destName != null) { if (destName != null) {
if (destName instanceof ICPPInternalBinding) { if (destName instanceof ICPPInternalBinding) {
ICPPInternalBinding bin = (ICPPInternalBinding) destName; ICPPInternalBinding bin = (ICPPInternalBinding) destName;
IASTFileLocation fileLocation = bin ast = bin.getDeclarations()[0];
.getDeclarations()[0].getFileLocation();
line = fileLocation.getStartingLineNumber();
} }
mess = MessageFormat mess = MessageFormat
.format( .format(
"Class ''{0}'' has virtual method ''{1}'' but non-virtual destructor ''{2}''", "Class ''{0}'' has virtual method ''{1}'' but non-virtual destructor ''{2}''",
clazz, method, destName.getName()); clazz, method, destName.getName());
reportProblem(ER_ID, file, line, mess); reportProblem(ER_ID, ast, mess);
} }
} }
} catch (DOMException e) { } catch (DOMException e) {

View file

@ -52,8 +52,7 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
if (stmt instanceof IASTExpressionStatement) { if (stmt instanceof IASTExpressionStatement) {
if (hasNoEffect(((IASTExpressionStatement) stmt) if (hasNoEffect(((IASTExpressionStatement) stmt)
.getExpression())) { .getExpression())) {
reportProblem(ER_ID, getFile(), stmt.getFileLocation() reportProblem(ER_ID, stmt, "Statement has no effect");
.getStartingLineNumber(), "Statement has no effect");
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }

View file

@ -12,6 +12,8 @@ package org.eclipse.cdt.codan.core.model;
import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
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.CoreModel;
@ -19,7 +21,10 @@ 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.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/** /**
* @author Alena * @author Alena
@ -29,12 +34,11 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
ICAstChecker { ICAstChecker {
private IFile file; private IFile file;
public IFile getFile() { protected IFile getFile() {
return file; return file;
} }
void processFile(IFile file) throws CoreException, InterruptedException { void processFile(IFile file) throws CoreException, InterruptedException {
this.file = file;
// create translation unit and access index // create translation unit and access index
ICElement model = CoreModel.getDefault().create(file); ICElement model = CoreModel.getDefault().create(file);
if (!(model instanceof ITranslationUnit)) if (!(model instanceof ITranslationUnit))
@ -50,8 +54,10 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
IASTTranslationUnit ast = tu.getAST(index, IASTTranslationUnit ast = tu.getAST(index,
ITranslationUnit.AST_SKIP_INDEXED_HEADERS); ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
// traverse the ast using the visitor pattern. // traverse the ast using the visitor pattern.
this.file = file;
processAst(ast); processAst(ast);
} finally { } finally {
this.file = null;
index.releaseReadLock(); index.releaseReadLock();
} }
} }
@ -70,4 +76,16 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
} }
return true; return true;
} }
public void reportProblem(String id, IASTNode astNode, String message) {
IASTFileLocation astLocation = astNode.getFileLocation();
IFile astFile = file;
if (astFile == null) {
IPath location = new Path(astLocation.getFileName());
astFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(location);
}
CodanRuntime.getInstance().getProblemReporter().reportProblem(id,
astFile, astLocation.getStartingLineNumber(), message);
}
} }