From abb0b06e2f60cdf3867a3ad8d87b8dd1d0f468fd Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Sat, 11 Jan 2020 17:44:39 +0100 Subject: [PATCH] Bug 559007 - Added new option to C-style cast checker Add a new option to check in macro expansion. User, if want, can disable this warning if it's in a macro expansion since the macro used can be out of control of the user, for example a system macro like FD_SET could be used. Change-Id: I0efaaedd09087de87d9340d81ddbd7f2dc1e1abb Signed-off-by: Marco Stornelli --- .../internal/checkers/CStyleCastChecker.java | 23 ++++++++++++++++++- .../internal/checkers/CheckersMessages.java | 3 +-- .../checkers/CheckersMessages.properties | 1 + .../checkers/CStyleCastCheckerTest.java | 10 ++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) 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); + } }