Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gvozdev2011-04-29 15:25:26 -0400
committerAndrew Gvozdev2011-04-29 15:25:26 -0400
commit91ab5f5876a57e33baa6ea0f5358d8ba75962c82 (patch)
tree6fbe27c828668a5a713a063d530b5ef6c1a685a4 /codan/org.eclipse.cdt.codan.checkers
parent5efd41bb09f8510cec9a023bcc24306234b22e70 (diff)
downloadorg.eclipse.cdt-91ab5f5876a57e33baa6ea0f5358d8ba75962c82.tar.gz
org.eclipse.cdt-91ab5f5876a57e33baa6ea0f5358d8ba75962c82.tar.xz
org.eclipse.cdt-91ab5f5876a57e33baa6ea0f5358d8ba75962c82.zip
bug 343429: [checker] Checker to pinpoint unused static functions in a file
A few false positives corrected
Diffstat (limited to 'codan/org.eclipse.cdt.codan.checkers')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UnusedSymbolInFileScopeChecker.java54
1 files changed, 45 insertions, 9 deletions
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 6759b2631b..8ef51bdcf2 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
@@ -12,8 +12,10 @@ package org.eclipse.cdt.codan.internal.checkers;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
@@ -36,9 +38,11 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
+import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IProblemType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IVariable;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
@@ -134,23 +138,34 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
}
} else if (binding instanceof IVariable) {
if (storageClass == IASTDeclSpecifier.sc_extern) {
- externVariableDeclarations.put(binding, decl);
+ IASTInitializer initializer = decl.getInitializer();
+ // initializer makes "extern" declaration to become definition do not count these
+ if (initializer==null) {
+ externVariableDeclarations.put(binding, decl);
+ }
} else if (storageClass == IASTDeclSpecifier.sc_static) {
IType type = ((IVariable) binding).getType();
// account for class constructor and avoid possible false positive
if (!(type instanceof ICPPClassType) && !(type instanceof IProblemType)) {
+ // check if initializer disqualifies it
IASTInitializer initializer = decl.getInitializer();
+ IASTInitializerClause clause = null;
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;
- }
+ clause = equalsInitializer.getInitializerClause();
+ } else if (initializer instanceof ICPPASTConstructorInitializer) {
+ ICPPASTConstructorInitializer constructorInitializer = (ICPPASTConstructorInitializer) initializer;
+ IASTInitializerClause[] args = constructorInitializer.getArguments();
+ if (args.length==1)
+ clause = args[0];
+ }
+ 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);
}
}
@@ -200,6 +215,16 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
if (binding instanceof ICPPMethod)
return PROCESS_CONTINUE;
+ if (binding instanceof IProblemBinding) {
+ // avoid false positives related to unresolved names
+ String plainName = name.toString();
+ filterOutByPlainName(externFunctionDeclarations, plainName);
+ filterOutByPlainName(staticFunctionDeclarations, plainName);
+ filterOutByPlainName(staticFunctionDefinitions, plainName);
+ filterOutByPlainName(externVariableDeclarations, plainName);
+ filterOutByPlainName(staticVariableDeclarations, plainName);
+ }
+
IASTNode parentNode = name.getParent();
if (!(parentNode instanceof IASTFunctionDefinition || parentNode instanceof IASTFunctionDeclarator)) {
@@ -218,6 +243,17 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
return PROCESS_CONTINUE;
}
+ private void filterOutByPlainName(Map<IBinding, IASTDeclarator> declarators, String id) {
+ Iterator<Entry<IBinding, IASTDeclarator>> iter = declarators.entrySet().iterator();
+ while (iter.hasNext()) {
+ Entry<IBinding, IASTDeclarator> entry = iter.next();
+ IASTDeclarator decl = entry.getValue();
+ IASTName astName = getAstName(decl);
+ if (id.equals(astName.toString()))
+ iter.remove();
+ }
+ }
+
});
} catch (Exception e) {
CodanCheckersActivator.log(e);

Back to the top