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

bug 343429: [checker] Checker to pinpoint unused static functions in a file

Added exception for CVS ident
This commit is contained in:
Andrew Gvozdev 2011-04-29 11:17:42 +00:00
parent bfab63bae9
commit fe52e11b25
4 changed files with 60 additions and 11 deletions

View file

@ -30,6 +30,9 @@ public class CheckersMessages extends NLS {
public static String SuspiciousSemicolonChecker_ParamElse;
public static String ProblemBindingChecker_Candidates;
public static String UnusedSymbolInFileScopeChecker_CharacterSequence;
public static String UnusedSymbolInFileScopeChecker_Exceptions;
static {
NLS.initializeMessages(CheckersMessages.class.getName(), CheckersMessages.class);
}

View file

@ -21,4 +21,6 @@ StatementHasNoEffectChecker_ParameterMacro=Report problem in statements that com
SuggestedParenthesisChecker_SuggestParanthesesAroundNot=Suggest parenthesis around not operator
SuspiciousSemicolonChecker_ParamElse=Do not report an error if 'else' exists
ProblemBindingChecker_Candidates=Candidates are:
ProblemBindingChecker_Candidates=Candidates are:
UnusedSymbolInFileScopeChecker_CharacterSequence=Ident character sequence:
UnusedSymbolInFileScopeChecker_Exceptions=Exceptions (ident character sequence)

View file

@ -17,13 +17,19 @@ import java.util.Map;
import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
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.codan.core.param.ListProblemPreference;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
@ -44,23 +50,31 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
public static final String ER_UNUSED_VARIABLE_DECLARATION_ID = "org.eclipse.cdt.codan.internal.checkers.UnusedVariableDeclarationProblem"; //$NON-NLS-1$
public static final String ER_UNUSED_FUNCTION_DECLARATION_ID = "org.eclipse.cdt.codan.internal.checkers.UnusedFunctionDeclarationProblem"; //$NON-NLS-1$
public static final String ER_UNUSED_STATIC_FUNCTION_ID = "org.eclipse.cdt.codan.internal.checkers.UnusedStaticFunctionProblem"; //$NON-NLS-1$
public static final String PARAM_EXCEPT_ARG_LIST = "exceptions"; //$NON-NLS-1$
private Map<IBinding, IASTDeclarator> externFunctionDeclarations = new HashMap<IBinding, IASTDeclarator>();
private Map<IBinding, IASTDeclarator> staticFunctionDeclarations = new HashMap<IBinding, IASTDeclarator>();
private Map<IBinding, IASTDeclarator> staticFunctionDefinitions = new HashMap<IBinding, IASTDeclarator>();
private Map<IBinding, IASTDeclarator> externVariableDeclarations = new HashMap<IBinding, IASTDeclarator>();
private Map<IBinding, IASTDeclarator> staticVariableDeclarations = new HashMap<IBinding, IASTDeclarator>();
private IProblemWorkingCopy unusedVariableProblem = null;
/*
* (non-Javadoc)
*
* @see
* org.eclipse.cdt.codan.core.model.ICheckerWithPreferences#initParameters
* (org.eclipse.cdt.codan.core.model.IProblemWorkingCopy)
*/
@Override
public boolean runInEditor() {
return true;
}
@Override
public void initPreferences(IProblemWorkingCopy problem) {
super.initPreferences(problem);
if (problem.getId().equals(ER_UNUSED_VARIABLE_DECLARATION_ID)) {
unusedVariableProblem = problem;
ListProblemPreference pref = addListPreference(problem, PARAM_EXCEPT_ARG_LIST,
CheckersMessages.UnusedSymbolInFileScopeChecker_Exceptions,
CheckersMessages.UnusedSymbolInFileScopeChecker_CharacterSequence);
pref.addChildValue("@(#)"); //$NON-NLS-1$
pref.addChildValue("$Id"); //$NON-NLS-1$
}
}
private void clearCandidates() {
@ -125,6 +139,18 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
IType type = ((IVariable) binding).getType();
// account for class constructor and avoid possible false positive
if (!(type instanceof ICPPClassType) && !(type instanceof IProblemType)) {
IASTInitializer initializer = decl.getInitializer();
if (initializer instanceof IASTEqualsInitializer) {
IASTEqualsInitializer equalsInitializer = (IASTEqualsInitializer) initializer;
IASTInitializerClause clause = equalsInitializer.getInitializerClause();
if (clause instanceof IASTLiteralExpression) {
IASTLiteralExpression literalExpression = (IASTLiteralExpression) clause;
String literal = literalExpression.toString();
if (isFilteredOut(literal, unusedVariableProblem, PARAM_EXCEPT_ARG_LIST))
continue;
}
}
staticVariableDeclarations.put(binding, decl);
}
}
@ -248,8 +274,14 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
clearCandidates(); // release memory
}
@Override
public boolean runInEditor() {
return true;
public boolean isFilteredOut(String arg, IProblem problem, String exceptionListParamId) {
Object[] arr = (Object[]) getPreference(problem, exceptionListParamId);
for (int i = 0; i < arr.length; i++) {
String str = (String) arr[i];
if (arg.contains(str))
return true;
}
return false;
}
}

View file

@ -244,4 +244,16 @@ public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase {
checkNoErrors();
}
// static char* test_var="$Id: file.c,v 1.1 2000/01/01 11:11:11 agvozdev Exp $";
public void testExternVariable_Declaration_cvs_ident() throws IOException {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// static char* test_var="@(#) $Header: /src/file.c,v 1.1 2000/01/01 11:11:11 agvozdev Exp $";
public void testExternVariable_Declaration_sccs_ident() throws IOException {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
}