From 9ba50a946241a94f0d870b70e0c148038f5df3a0 Mon Sep 17 00:00:00 2001 From: Alisson Linhares de Carvalho Date: Tue, 16 Feb 2016 13:43:50 -0200 Subject: [PATCH] Bug 486610 - Fixed a highlight problem in reportProblem method Every time we report a problem in a declaration/expression that occupies more than one line, the codan marks the entire line as a problem - including everything before the node. This patch solves this inconvenient behavior. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=486610 for a complete explanation. Change-Id: I742cbaac8a1392676695d574355597b0cfc87385 Signed-off-by: Alisson Linhares de Carvalho --- .../core/cxx/model/AbstractIndexAstChecker.java | 10 +++++----- .../checkers/AssignmentToItselfCheckerTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) 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 44287f7ea50..92cbfed1b00 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 @@ -197,11 +197,11 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem return locFactory.createProblemLocation(getFile(), start, end, line); } } - if (line == astLocation.getEndingLineNumber()) { - return locFactory.createProblemLocation(getFile(), astLocation.getNodeOffset(), - astLocation.getNodeOffset() + astLocation.getNodeLength(), line); - } - return locFactory.createProblemLocation(getFile(), line); + // If the raw signature has more than one line, we highlight only the code + // related to the problem. + int start = astLocation.getNodeOffset(); + int end = start + astLocation.getNodeLength(); + return locFactory.createProblemLocation(getFile(), start, end, line); } protected static boolean enclosedInMacroExpansion(IASTNode node) { diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java index e3c5968d7fc..18c39ada406 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.codan.core.internal.checkers; import org.eclipse.cdt.codan.core.test.CheckerTestCase; +import org.eclipse.core.resources.IMarker; /** * Test for {@see AssignmentToItselfChecker} class @@ -63,4 +64,18 @@ public class AssignmentToItselfCheckerTest extends CheckerTestCase { loadCodeAndRun(getAboveComment()); checkNoErrors(); } + + // void foo() { + // int x = 0; x + // = x; + // } + public void testMarkerOffset_Bug486610() throws Exception { + String code = getAboveComment(); + loadCodeAndRun(code); + IMarker marker = checkErrorLine(2); + int start = marker.getAttribute(IMarker.CHAR_START, -1); + int end = marker.getAttribute(IMarker.CHAR_END, -1); + // The offset should start at the beginning of the expression "x = x" + assertEquals("x\n = x", code.substring(start, end)); + } }