From 39e579aa0a2fa7eb8cd35e0371379b0cdf988f47 Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Wed, 6 Apr 2011 08:11:52 +0000 Subject: [PATCH] Bug 341089: Codan does not handle missing functions in macros correctly https://bugs.eclipse.org/bugs/show_bug.cgi?id=341089 --- .../checkers/ProblemBindingChecker.java | 4 +- .../cxx/model/AbstractIndexAstChecker.java | 39 ++++++++++++------- .../checkers/ProblemBindingCheckerTest.java | 12 ++++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java index 01c1041c03e..87adf7bf554 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java @@ -200,7 +200,7 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker { private void handleFunctionProblem(IASTName name, IProblemBinding problemBinding, String contextFlagsString) throws DOMException { 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 { String candidatesString = getCandidatesString(problemBinding); 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; if (isMethod) { 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 { String candidatesString = getCandidatesString(problemBinding); reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString); diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java index cf995147bb1..b1874505c51 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java @@ -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.IProblem; 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.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.IASTTranslationUnit; import org.eclipse.cdt.core.index.IIndex; @@ -92,10 +96,6 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem if (loc!=null) reportProblem(problem, loc, args); } - /** - * @param astNode - * @return - */ @SuppressWarnings("restriction") protected IProblemLocation getProblemLocation(IASTNode astNode) { IASTFileLocation astLocation = astNode.getFileLocation(); @@ -108,14 +108,29 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem Activator.log("Cannot resolve location: " + location); //$NON-NLS-1$ return null; } - IProblemLocation loc; + return getProblemLocation(astNode, astLocation, astFile); + } + + private IProblemLocation getProblemLocation(IASTNode astNode, IASTFileLocation astLocation, IFile astFile) { int line = astLocation.getStartingLineNumber(); - if (line == astLocation.getEndingLineNumber()) - loc = getRuntime().getProblemLocationFactory().createProblemLocation(astFile, astLocation.getNodeOffset(), + IProblemLocationFactory locFactory = getRuntime().getProblemLocationFactory(); + 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); - else - loc = getRuntime().getProblemLocationFactory().createProblemLocation(astFile, line); - return loc; + } + return locFactory.createProblemLocation(astFile, line); + } + + private boolean hasMacroLocation(IASTNode astNode) { + return astNode.getNodeLocations().length == 1 && astNode.getNodeLocations()[0] instanceof IASTMacroExpansionLocation; } @Override @@ -143,10 +158,6 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem } } - /** - * @return - * - */ protected ICodanCommentMap getCommentMap() { if (commentmap == null) { try { diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java index ef4cc85d37a..924fc446545 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java @@ -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.internal.checkers.ProblemBindingChecker; +import org.eclipse.core.resources.IMarker; public class ProblemBindingCheckerTest extends CheckerTestCase { @Override @@ -74,4 +75,15 @@ public class ProblemBindingCheckerTest extends CheckerTestCase { checkErrorLine(12, ProblemBindingChecker.ERR_ID_FieldResolutionProblem); 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$ + } }