Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gvozdev2011-04-29 11:17:42 +0000
committerAndrew Gvozdev2011-04-29 11:17:42 +0000
commitfe52e11b255ef0e09fc928b69adba8104492375e (patch)
tree31213481789c89deb435028ac4013c1690e745f2
parentbfab63bae94a5717cbe644927f52afda8cddac1c (diff)
downloadorg.eclipse.cdt-fe52e11b255ef0e09fc928b69adba8104492375e.tar.gz
org.eclipse.cdt-fe52e11b255ef0e09fc928b69adba8104492375e.tar.xz
org.eclipse.cdt-fe52e11b255ef0e09fc928b69adba8104492375e.zip
bug 343429: [checker] Checker to pinpoint unused static functions in a filev201104290807
Added exception for CVS ident
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java3
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.properties4
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java52
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java12
4 files changed, 60 insertions, 11 deletions
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 255aaf2d0db..32397ef13ca 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
@@ -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);
}
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 74fc7c6647c..3296e70fa2c 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
@@ -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: \ No newline at end of file
+ProblemBindingChecker_Candidates=Candidates are:
+UnusedSymbolInFileScopeChecker_CharacterSequence=Ident character sequence:
+UnusedSymbolInFileScopeChecker_Exceptions=Exceptions (ident character sequence)
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java
index 841b8d45b06..6759b2631b3 100644
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java
@@ -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;
}
+
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java
index c737a95dda6..5975ea70d74 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java
@@ -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();
+ }
+
}

Back to the top