1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Bug 356040 - Invalid "Unused declaration of variable" warning.

This commit is contained in:
Sergey Prigogin 2011-08-28 20:35:50 -07:00
parent c0b9f79b30
commit 17f96f9eb3
2 changed files with 29 additions and 17 deletions

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Andrew Gvozdev - initial API and implementation * Andrew Gvozdev - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers; package org.eclipse.cdt.codan.internal.checkers;
@ -90,11 +91,11 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
} }
private boolean isAnyCandidate() { private boolean isAnyCandidate() {
return externFunctionDeclarations.size() > 0 || return !externFunctionDeclarations.isEmpty() ||
staticFunctionDeclarations.size() > 0 || !staticFunctionDeclarations.isEmpty() ||
staticFunctionDefinitions.size() > 0 || !staticFunctionDefinitions.isEmpty() ||
externVariableDeclarations.size() > 0 || !externVariableDeclarations.isEmpty() ||
staticVariableDeclarations.size() > 0; !staticVariableDeclarations.isEmpty();
} }
public void processAst(IASTTranslationUnit ast) { public void processAst(IASTTranslationUnit ast) {
@ -138,16 +139,15 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
} }
} else if (binding instanceof IVariable) { } else if (binding instanceof IVariable) {
if (storageClass == IASTDeclSpecifier.sc_extern) { if (storageClass == IASTDeclSpecifier.sc_extern) {
IASTInitializer initializer = decl.getInitializer(); // Initializer makes "extern" declaration to become definition do not count these
// initializer makes "extern" declaration to become definition do not count these if (decl.getInitializer() == null) {
if (initializer==null) {
externVariableDeclarations.put(binding, decl); externVariableDeclarations.put(binding, decl);
} }
} else if (storageClass == IASTDeclSpecifier.sc_static) { } else if (storageClass == IASTDeclSpecifier.sc_static) {
IType type = ((IVariable) binding).getType(); IType type = ((IVariable) binding).getType();
// account for class constructor and avoid possible false positive // Account for class constructor and avoid possible false positive
if (!(type instanceof ICPPClassType) && !(type instanceof IProblemType)) { if (!(type instanceof ICPPClassType) && !(type instanceof IProblemType)) {
// check if initializer disqualifies it // Check if initializer disqualifies it
IASTInitializer initializer = decl.getInitializer(); IASTInitializer initializer = decl.getInitializer();
IASTInitializerClause clause = null; IASTInitializerClause clause = null;
if (initializer instanceof IASTEqualsInitializer) { if (initializer instanceof IASTEqualsInitializer) {
@ -156,7 +156,7 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
} else if (initializer instanceof ICPPASTConstructorInitializer) { } else if (initializer instanceof ICPPASTConstructorInitializer) {
ICPPASTConstructorInitializer constructorInitializer = (ICPPASTConstructorInitializer) initializer; ICPPASTConstructorInitializer constructorInitializer = (ICPPASTConstructorInitializer) initializer;
IASTInitializerClause[] args = constructorInitializer.getArguments(); IASTInitializerClause[] args = constructorInitializer.getArguments();
if (args.length==1) if (args.length == 1)
clause = args[0]; clause = args[0];
} }
if (clause instanceof IASTLiteralExpression) { if (clause instanceof IASTLiteralExpression) {
@ -232,7 +232,12 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
staticFunctionDefinitions.remove(binding); staticFunctionDefinitions.remove(binding);
} }
if (!(parentNode instanceof IASTDeclarator)) { if (parentNode instanceof IASTDeclarator) {
// Initializer makes "extern" declaration to become definition.
if (((IASTDeclarator) parentNode).getInitializer() != null) {
externVariableDeclarations.remove(binding);
}
} else {
externVariableDeclarations.remove(binding); externVariableDeclarations.remove(binding);
staticVariableDeclarations.remove(binding); staticVariableDeclarations.remove(binding);
} }

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Andrew Gvozdev - initial API and implementation * Andrew Gvozdev - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers; package org.eclipse.cdt.codan.core.internal.checkers;
@ -17,7 +18,6 @@ import org.eclipse.cdt.codan.internal.checkers.UnusedSymbolInFileScopeChecker;
/** /**
* Test for {@see UnusedSymbolInFileScopeChecker} class * Test for {@see UnusedSymbolInFileScopeChecker} class
*
*/ */
public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase { public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase {
@Override @Override
@ -222,8 +222,15 @@ public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase {
checkNoErrors(); checkNoErrors();
} }
// extern int test_var=0; // not quite legal but some compilers allow that // extern const int test_var=0; // not quite legal but some compilers allow that
public void testExternVariable_Definition() throws IOException { public void testExternVariable_Definition1() throws IOException {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// extern const int test_var;
// const int test_var = 0;
public void testExternVariable_Definition2() throws IOException {
loadCodeAndRun(getAboveComment()); loadCodeAndRun(getAboveComment());
checkNoErrors(); checkNoErrors();
} }