mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 14:12:10 +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:
parent
7a0634e8ab
commit
abb0b06e2f
4 changed files with 34 additions and 3 deletions
|
@ -11,17 +11,38 @@
|
||||||
package org.eclipse.cdt.codan.internal.checkers;
|
package org.eclipse.cdt.codan.internal.checkers;
|
||||||
|
|
||||||
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
|
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.ILinkage;
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
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;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
|
||||||
public class CStyleCastChecker extends AbstractIndexAstChecker {
|
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 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
|
@Override
|
||||||
public void processAst(IASTTranslationUnit ast) {
|
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) {
|
if (ast.getLinkage().getLinkageID() == ILinkage.CPP_LINKAGE_ID) {
|
||||||
ast.accept(new ASTVisitor() {
|
ast.accept(new ASTVisitor() {
|
||||||
{
|
{
|
||||||
|
@ -30,7 +51,7 @@ public class CStyleCastChecker extends AbstractIndexAstChecker {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTExpression expression) {
|
public int visit(IASTExpression expression) {
|
||||||
if (expression instanceof IASTCastExpression) {
|
if (expression instanceof IASTCastExpression && !enclosedInMacroExpansion(expression)) {
|
||||||
if (((IASTCastExpression) expression).getOperator() == IASTCastExpression.op_cast)
|
if (((IASTCastExpression) expression).getOperator() == IASTCastExpression.op_cast)
|
||||||
reportProblem(ERR_ID, expression);
|
reportProblem(ERR_ID, expression);
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,10 +43,9 @@ public class CheckersMessages extends NLS {
|
||||||
public static String UnusedSymbolInFileScopeChecker_CharacterSequence;
|
public static String UnusedSymbolInFileScopeChecker_CharacterSequence;
|
||||||
public static String UnusedSymbolInFileScopeChecker_Exceptions;
|
public static String UnusedSymbolInFileScopeChecker_Exceptions;
|
||||||
public static String SymbolShadowingChecker_CheckFunctionParameters;
|
public static String SymbolShadowingChecker_CheckFunctionParameters;
|
||||||
|
|
||||||
public static String Copyright_regex;
|
public static String Copyright_regex;
|
||||||
|
|
||||||
public static String ShallowCopyChecker_OnlyNew;
|
public static String ShallowCopyChecker_OnlyNew;
|
||||||
|
public static String CStyleCastCheck_checkInMacro;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
NLS.initializeMessages(CheckersMessages.class.getName(), CheckersMessages.class);
|
NLS.initializeMessages(CheckersMessages.class.getName(), CheckersMessages.class);
|
||||||
|
|
|
@ -23,6 +23,7 @@ ReturnChecker_Param0=Also check functions with implicit return value
|
||||||
GenericParameter_ParameterExceptions=Exceptions (value of the problem argument)
|
GenericParameter_ParameterExceptions=Exceptions (value of the problem argument)
|
||||||
GenericParameter_ParameterExceptionsItem=Value of the argument
|
GenericParameter_ParameterExceptionsItem=Value of the argument
|
||||||
StatementHasNoEffectChecker_ParameterMacro=Report problem in statements that come from macro expansion
|
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
|
SuggestedParenthesisChecker_SuggestParanthesesAroundNot=Suggest parenthesis around 'not' operator
|
||||||
SuspiciousSemicolonChecker_ParamAfterElse=Report an error if semicolon is right after 'else' statement
|
SuspiciousSemicolonChecker_ParamAfterElse=Report an error if semicolon is right after 'else' statement
|
||||||
SuspiciousSemicolonChecker_ParamElse=Do not report an error after 'if' when 'else' exists
|
SuspiciousSemicolonChecker_ParamElse=Do not report an error after 'if' when 'else' exists
|
||||||
|
|
|
@ -47,4 +47,14 @@ public class CStyleCastCheckerTest extends CheckerTestCase {
|
||||||
loadCodeAndRun(getAboveComment());
|
loadCodeAndRun(getAboveComment());
|
||||||
checkNoErrorsOfKind(ERR_ID);
|
checkNoErrorsOfKind(ERR_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define CAST(X) (void)X
|
||||||
|
//void bar() {
|
||||||
|
// int b;
|
||||||
|
// CAST(b);
|
||||||
|
//};
|
||||||
|
public void testInMacro() throws Exception {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkNoErrorsOfKind(ERR_ID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue