1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 341089: Codan does not handle missing functions in macros correctly

https://bugs.eclipse.org/bugs/show_bug.cgi?id=341089
This commit is contained in:
Emanuel Graf 2011-04-06 08:11:52 +00:00
parent 616d9b7ce9
commit 39e579aa0a
3 changed files with 39 additions and 16 deletions

View file

@ -200,7 +200,7 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker {
private void handleFunctionProblem(IASTName name, IProblemBinding problemBinding, String contextFlagsString) throws DOMException { private void handleFunctionProblem(IASTName name, IProblemBinding problemBinding, String contextFlagsString) throws DOMException {
if (problemBinding.getCandidateBindings().length == 0) { if (problemBinding.getCandidateBindings().length == 0) {
reportProblem(ERR_ID_FunctionResolutionProblem, name.getLastName(), name.getRawSignature(), contextFlagsString); reportProblem(ERR_ID_FunctionResolutionProblem, name.getLastName(), new String(name.getSimpleID()), contextFlagsString);
} else { } else {
String candidatesString = getCandidatesString(problemBinding); String candidatesString = getCandidatesString(problemBinding);
reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString); reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString);
@ -218,7 +218,7 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker {
boolean isMethod = parentParentNode instanceof IASTFunctionCallExpression && parentParentNode.getChildren()[0] == parentNode; boolean isMethod = parentParentNode instanceof IASTFunctionCallExpression && parentParentNode.getChildren()[0] == parentNode;
if (isMethod) { if (isMethod) {
if (problemBinding.getCandidateBindings().length == 0) { if (problemBinding.getCandidateBindings().length == 0) {
reportProblem(ERR_ID_MethodResolutionProblem, name.getLastName(), name.getRawSignature(), contextFlagsString); reportProblem(ERR_ID_MethodResolutionProblem, name.getLastName(), new String(name.getSimpleID()), contextFlagsString);
} else { } else {
String candidatesString = getCandidatesString(problemBinding); String candidatesString = getCandidatesString(problemBinding);
reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString); reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString);

View file

@ -15,8 +15,12 @@ import org.eclipse.cdt.codan.core.cxx.Activator;
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences; import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemLocation; import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemLocationFactory;
import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker; import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
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;
@ -92,10 +96,6 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
if (loc!=null) reportProblem(problem, loc, args); if (loc!=null) reportProblem(problem, loc, args);
} }
/**
* @param astNode
* @return
*/
@SuppressWarnings("restriction") @SuppressWarnings("restriction")
protected IProblemLocation getProblemLocation(IASTNode astNode) { protected IProblemLocation getProblemLocation(IASTNode astNode) {
IASTFileLocation astLocation = astNode.getFileLocation(); IASTFileLocation astLocation = astNode.getFileLocation();
@ -108,14 +108,29 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
Activator.log("Cannot resolve location: " + location); //$NON-NLS-1$ Activator.log("Cannot resolve location: " + location); //$NON-NLS-1$
return null; return null;
} }
IProblemLocation loc; return getProblemLocation(astNode, astLocation, astFile);
}
private IProblemLocation getProblemLocation(IASTNode astNode, IASTFileLocation astLocation, IFile astFile) {
int line = astLocation.getStartingLineNumber(); int line = astLocation.getStartingLineNumber();
if (line == astLocation.getEndingLineNumber()) IProblemLocationFactory locFactory = getRuntime().getProblemLocationFactory();
loc = getRuntime().getProblemLocationFactory().createProblemLocation(astFile, astLocation.getNodeOffset(), if (hasMacroLocation(astNode) && astNode instanceof IASTName) {
IASTImageLocation imageLocation = ((IASTName) astNode).getImageLocation();
if (imageLocation != null) {
int start = imageLocation.getNodeOffset();
int end = start + imageLocation.getNodeLength();
return locFactory.createProblemLocation(astFile, start, end, line);
}
}
if (line == astLocation.getEndingLineNumber()) {
return locFactory.createProblemLocation(astFile, astLocation.getNodeOffset(),
astLocation.getNodeOffset() + astLocation.getNodeLength(), line); astLocation.getNodeOffset() + astLocation.getNodeLength(), line);
else }
loc = getRuntime().getProblemLocationFactory().createProblemLocation(astFile, line); return locFactory.createProblemLocation(astFile, line);
return loc; }
private boolean hasMacroLocation(IASTNode astNode) {
return astNode.getNodeLocations().length == 1 && astNode.getNodeLocations()[0] instanceof IASTMacroExpansionLocation;
} }
@Override @Override
@ -143,10 +158,6 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
} }
} }
/**
* @return
*
*/
protected ICodanCommentMap getCommentMap() { protected ICodanCommentMap getCommentMap() {
if (commentmap == null) { if (commentmap == null) {
try { try {

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.codan.core.internal.checkers;
import org.eclipse.cdt.codan.core.test.CheckerTestCase; import org.eclipse.cdt.codan.core.test.CheckerTestCase;
import org.eclipse.cdt.codan.internal.checkers.ProblemBindingChecker; import org.eclipse.cdt.codan.internal.checkers.ProblemBindingChecker;
import org.eclipse.core.resources.IMarker;
public class ProblemBindingCheckerTest extends CheckerTestCase { public class ProblemBindingCheckerTest extends CheckerTestCase {
@Override @Override
@ -74,4 +75,15 @@ public class ProblemBindingCheckerTest extends CheckerTestCase {
checkErrorLine(12, ProblemBindingChecker.ERR_ID_FieldResolutionProblem); checkErrorLine(12, ProblemBindingChecker.ERR_ID_FieldResolutionProblem);
checkErrorLine(13, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); checkErrorLine(13, ProblemBindingChecker.ERR_ID_MethodResolutionProblem);
} }
// #define MACRO(code) code
// int main() {
// MACRO(foo());
// return 0;
// }
public void testBug341089DontUnderlineWholeMacro() {
loadCodeAndRun(getAboveComment());
IMarker marker = checkErrorLine(3, ProblemBindingChecker.ERR_ID_FunctionResolutionProblem);
assertFalse(marker.getAttribute(IMarker.MESSAGE, "").contains("MACRO")); //$NON-NLS-1$//$NON-NLS-2$
}
} }