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:
parent
482d0431f3
commit
85230a764d
4 changed files with 26 additions and 15 deletions
|
@ -35,9 +35,7 @@ public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
|
|||
public int visit(IASTExpression expression) {
|
||||
if (isAssignmentExpression(expression)
|
||||
&& isUsedAsCondition(expression)) {
|
||||
reportProblem(ER_ID, getFile(), expression.getFileLocation()
|
||||
.getStartingLineNumber(),
|
||||
"Possible assignment in condition");
|
||||
reportProblem(ER_ID, expression, "Possible assignment in condition");
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
}
|
||||
|
|
|
@ -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.DOMException;
|
||||
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.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
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.ICPPClassType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
||||
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
|
||||
|
@ -59,20 +58,17 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
|
|||
String mess;
|
||||
String clazz = className.toString();
|
||||
String method = virMethodName.getName();
|
||||
int line = 1;
|
||||
IFile file = getFile();
|
||||
IASTNode ast = decl;
|
||||
if (destName != null) {
|
||||
if (destName instanceof ICPPInternalBinding) {
|
||||
ICPPInternalBinding bin = (ICPPInternalBinding) destName;
|
||||
IASTFileLocation fileLocation = bin
|
||||
.getDeclarations()[0].getFileLocation();
|
||||
line = fileLocation.getStartingLineNumber();
|
||||
ast = bin.getDeclarations()[0];
|
||||
}
|
||||
mess = MessageFormat
|
||||
.format(
|
||||
"Class ''{0}'' has virtual method ''{1}'' but non-virtual destructor ''{2}''",
|
||||
clazz, method, destName.getName());
|
||||
reportProblem(ER_ID, file, line, mess);
|
||||
reportProblem(ER_ID, ast, mess);
|
||||
}
|
||||
}
|
||||
} catch (DOMException e) {
|
||||
|
|
|
@ -52,8 +52,7 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
|||
if (stmt instanceof IASTExpressionStatement) {
|
||||
if (hasNoEffect(((IASTExpressionStatement) stmt)
|
||||
.getExpression())) {
|
||||
reportProblem(ER_ID, getFile(), stmt.getFileLocation()
|
||||
.getStartingLineNumber(), "Statement has no effect");
|
||||
reportProblem(ER_ID, stmt, "Statement has no effect");
|
||||
}
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ package org.eclipse.cdt.codan.core.model;
|
|||
|
||||
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
||||
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.index.IIndex;
|
||||
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.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
|
||||
/**
|
||||
* @author Alena
|
||||
|
@ -29,12 +34,11 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
|
|||
ICAstChecker {
|
||||
private IFile file;
|
||||
|
||||
public IFile getFile() {
|
||||
protected IFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
void processFile(IFile file) throws CoreException, InterruptedException {
|
||||
this.file = file;
|
||||
// create translation unit and access index
|
||||
ICElement model = CoreModel.getDefault().create(file);
|
||||
if (!(model instanceof ITranslationUnit))
|
||||
|
@ -50,8 +54,10 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
|
|||
IASTTranslationUnit ast = tu.getAST(index,
|
||||
ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
||||
// traverse the ast using the visitor pattern.
|
||||
this.file = file;
|
||||
processAst(ast);
|
||||
} finally {
|
||||
this.file = null;
|
||||
index.releaseReadLock();
|
||||
}
|
||||
}
|
||||
|
@ -70,4 +76,16 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements
|
|||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue