1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

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 <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2020-01-11 17:44:39 +01:00
parent 7a0634e8ab
commit abb0b06e2f
4 changed files with 34 additions and 3 deletions

View file

@ -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);
}

View file

@ -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);

View file

@ -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

View file

@ -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);
}
}