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 6a3130babb7..fbcd2f4e9da 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 @@ -11,17 +11,38 @@ package org.eclipse.cdt.codan.internal.checkers; import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; +import org.eclipse.cdt.codan.core.model.IProblem; +import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTCastExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; +import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; public class CStyleCastChecker extends AbstractIndexAstChecker { public static final String ERR_ID = "org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem"; //$NON-NLS-1$ + public static final String PARAM_MACRO = "checkMacro"; //$NON-NLS-1$ + private boolean checkMacro = true; + + @Override + public void initPreferences(IProblemWorkingCopy problem) { + super.initPreferences(problem); + addPreference(problem, PARAM_MACRO, CheckersMessages.Copyright_regex, true); + } + + private boolean enclosedInMacroExpansion(IASTExpression statement) { + if (!checkMacro) + return false; + IASTNodeLocation[] locations = statement.getNodeLocations(); + return locations.length == 1 && locations[0] instanceof IASTMacroExpansionLocation; + } @Override public void processAst(IASTTranslationUnit ast) { + final IProblem pt = getProblemById(ERR_ID, getFile()); + checkMacro = (boolean) getPreference(pt, PARAM_MACRO); if (ast.getLinkage().getLinkageID() == ILinkage.CPP_LINKAGE_ID) { ast.accept(new ASTVisitor() { { @@ -30,7 +51,7 @@ public class CStyleCastChecker extends AbstractIndexAstChecker { @Override public int visit(IASTExpression expression) { - if (expression instanceof IASTCastExpression) { + if (expression instanceof IASTCastExpression && !enclosedInMacroExpansion(expression)) { if (((IASTCastExpression) expression).getOperator() == IASTCastExpression.op_cast) reportProblem(ERR_ID, expression); } diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java index acfb71f970e..deb2b298ca2 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java @@ -43,10 +43,9 @@ public class CheckersMessages extends NLS { public static String UnusedSymbolInFileScopeChecker_CharacterSequence; public static String UnusedSymbolInFileScopeChecker_Exceptions; public static String SymbolShadowingChecker_CheckFunctionParameters; - public static String Copyright_regex; - public static String ShallowCopyChecker_OnlyNew; + public static String CStyleCastCheck_checkInMacro; static { NLS.initializeMessages(CheckersMessages.class.getName(), CheckersMessages.class); diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.properties b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.properties index ad9bfe7ac3f..63a42ad201d 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.properties +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.properties @@ -23,6 +23,7 @@ ReturnChecker_Param0=Also check functions with implicit return value GenericParameter_ParameterExceptions=Exceptions (value of the problem argument) GenericParameter_ParameterExceptionsItem=Value of the argument StatementHasNoEffectChecker_ParameterMacro=Report problem in statements that come from macro expansion +CStyleCastCheck_checkInMacro=Report problem in expressions that come from macro expansion SuggestedParenthesisChecker_SuggestParanthesesAroundNot=Suggest parenthesis around 'not' operator SuspiciousSemicolonChecker_ParamAfterElse=Report an error if semicolon is right after 'else' statement SuspiciousSemicolonChecker_ParamElse=Do not report an error after 'if' when 'else' exists 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 e4102e047ed..3adf4e7d079 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 @@ -47,4 +47,14 @@ public class CStyleCastCheckerTest extends CheckerTestCase { loadCodeAndRun(getAboveComment()); checkNoErrorsOfKind(ERR_ID); } + + //#define CAST(X) (void)X + //void bar() { + // int b; + // CAST(b); + //}; + public void testInMacro() throws Exception { + loadCodeAndRun(getAboveComment()); + checkNoErrorsOfKind(ERR_ID); + } }