diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java index fbcd2f4e9da..54ccfc8b593 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java @@ -29,12 +29,10 @@ public class CStyleCastChecker extends AbstractIndexAstChecker { @Override public void initPreferences(IProblemWorkingCopy problem) { super.initPreferences(problem); - addPreference(problem, PARAM_MACRO, CheckersMessages.Copyright_regex, true); + addPreference(problem, PARAM_MACRO, CheckersMessages.CStyleCastCheck_checkInMacro, true); } private boolean enclosedInMacroExpansion(IASTExpression statement) { - if (!checkMacro) - return false; IASTNodeLocation[] locations = statement.getNodeLocations(); return locations.length == 1 && locations[0] instanceof IASTMacroExpansionLocation; } @@ -51,7 +49,8 @@ public class CStyleCastChecker extends AbstractIndexAstChecker { @Override public int visit(IASTExpression expression) { - if (expression instanceof IASTCastExpression && !enclosedInMacroExpansion(expression)) { + if (expression instanceof IASTCastExpression + && (checkMacro || !enclosedInMacroExpansion(expression))) { if (((IASTCastExpression) expression).getOperator() == IASTCastExpression.op_cast) reportProblem(ERR_ID, expression); } diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java index 3adf4e7d079..ceef770005d 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.codan.core.internal.checkers; +import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.codan.core.tests.CheckerTestCase; import org.eclipse.cdt.codan.internal.checkers.CStyleCastChecker; import org.eclipse.cdt.codan.internal.checkers.UsingInHeaderChecker; @@ -27,6 +28,11 @@ public class CStyleCastCheckerTest extends CheckerTestCase { enableProblems(ERR_ID); } + private void setCheckInMacro(boolean val) { + IProblemPreference pref = getPreference(CStyleCastChecker.ERR_ID, CStyleCastChecker.PARAM_MACRO); + pref.setValue(val); + } + @Override public boolean isCpp() { return true; @@ -54,6 +60,17 @@ public class CStyleCastCheckerTest extends CheckerTestCase { // CAST(b); //}; public void testInMacro() throws Exception { + loadCodeAndRun(getAboveComment()); + checkErrorLine(4, ERR_ID); + } + + //#define CAST(X) (void)X + //void bar() { + // int b; + // CAST(b); + //}; + public void testInMacroOptionOff() throws Exception { + setCheckInMacro(false); loadCodeAndRun(getAboveComment()); checkNoErrorsOfKind(ERR_ID); }