1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Bug 322119 - [checker] Suspicious semicolon (i.e. ";" after "if")

This commit is contained in:
Alena Laskavaia 2010-10-18 01:18:49 +00:00
parent 1ee803fcc8
commit ea00500725
5 changed files with 233 additions and 3 deletions

View file

@ -52,4 +52,8 @@ problem.description.AssignmentToItself = Finds expression where left and right s
checker.name.ReturnStyle = Return with parenthesis
problem.name.ReturnStyle = Return with parenthesis
problem.messagePattern.ReturnStyle = Return statement has invalid style. Return value should be surrounded by parenthesis
problem.description.ReturnStyle = Checks for return statements that do no return the value in parenthesis. For example 'return 0;'
problem.description.ReturnStyle = Checks for return statements that do no return the value in parenthesis. For example 'return 0;'
checker.name.SuspiciousSemicolon = Suspicious semicolon
problem.name.SuspiciousSemicolon = Suspicious semicolon
problem.messagePattern.SuspiciousSemicolon = Suspicious semicolon
problem.description.SuspiciousSemicolon = A semicolon is used as a null statement in a condition. For example, 'if(expression);'

View file

@ -278,7 +278,7 @@
name="%problem.name.AssignmentToItself">
</problem>
</checker>
<checker
<checker
class="org.eclipse.cdt.codan.internal.checkers.ReturnStyleChecker"
id="org.eclipse.cdt.codan.internal.checkers.ReturnStyle"
name="%checker.name.ReturnStyle">
@ -291,6 +291,20 @@
messagePattern="%problem.messagePattern.ReturnStyle"
name="%problem.name.ReturnStyle">
</problem>
</checker>
</checker>
<checker
class="org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonChecker"
id="org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolon"
name="%checker.name.SuspiciousSemicolon">
<problem
category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"
defaultEnabled="true"
defaultSeverity="Warning"
description="%problem.description.SuspiciousSemicolon"
id="org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonProblem"
messagePattern="%problem.messagePattern.SuspiciousSemicolon"
name="%problem.name.SuspiciousSemicolon">
</problem>
</checker>
</extension>
</plugin>

View file

@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2010 Marc-Andre Laperle and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc-Andre Laperle - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
public class SuspiciousSemicolonChecker extends AbstractIndexAstChecker {
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonProblem"; //$NON-NLS-1$
public void processAst(IASTTranslationUnit ast) {
ast.accept(new ASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTIfStatement) {
IASTStatement thenStmt = ((IASTIfStatement) statement)
.getThenClause();
if (thenStmt instanceof IASTNullStatement
&& noMacroInvolved(thenStmt)) {
reportProblem(ER_ID, thenStmt, (Object) null);
}
}
return PROCESS_CONTINUE;
}
});
}
protected boolean noMacroInvolved(IASTStatement node) {
IASTNodeSelector nodeSelector = node.getTranslationUnit()
.getNodeSelector(node.getTranslationUnit().getFilePath());
IASTFileLocation fileLocation = node.getFileLocation();
IASTPreprocessorMacroExpansion macro = nodeSelector
.findEnclosingMacroExpansion(fileLocation.getNodeOffset() - 1,
1);
return macro == null;
}
}

View file

@ -0,0 +1,153 @@
/*******************************************************************************
* Copyright (c) 2010 Marc-Andre Laperle and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc-Andre Laperle - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
public class SuspiciousSemicolonCheckerTest extends CheckerTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
enableProblems("org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonProblem"); //$NON-NLS-1$
}
// void foo() {
// if(0);
// }
public void testIf1() {
loadCodeAndRun(getAboveComment());
checkErrorLine(2);
}
// void foo() {
// if(0);
// {
// }
// }
public void testIf2() {
loadCodeAndRun(getAboveComment());
checkErrorLine(2);
}
// void foo() {
// if(0)
// foo();
// }
public void testIf3() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// void foo() {
// if(0)
// ;
// }
public void testIf4() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// void foo() {
// if(0);{
// }
// }
public void testIf5() {
loadCodeAndRun(getAboveComment());
checkErrorLine(2);
}
// void foo() {
// if(0) {};
// }
public void testIf6() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// void foo() {
// if(0
// );
// }
// }
public void testIf7() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// void foo() {
// if(0)
// ;
// else if(0);
// }
public void testElseIf1() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
checkErrorLine(4);
}
// void foo() {
// if(0)
// ;
// else if(0);
// {
//
// }
// }
public void testElseIf2() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
checkErrorLine(4);
}
// void foo() {
// if(0)
// ;
// else if(0);{
// }
// }
public void testElseIf3() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
checkErrorLine(4);
}
// void foo() {
// if(0)
// ;
// else if(0){};
// }
public void testElseIf4() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// void foo() {
// if(0)
// ;
// else if(0
// );
// }
public void testElseIf5() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
checkErrorLine(5);
}
// #define OP
// void foo() {
// if(0)
// OP;
// }
public void testMacro() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
}

View file

@ -21,6 +21,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.ReturnStyleCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
public class AutomatedIntegrationSuite extends TestSuite {
@ -49,6 +50,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(AssignmentInConditionCheckerTest.class);
suite.addTestSuite(AssignmentToItselfCheckerTest.class);
suite.addTestSuite(ReturnStyleCheckerTest.class);
suite.addTestSuite(SuspiciousSemicolonCheckerTest.class);
// framework
suite.addTest(CodanFastTestSuite.suite());
// quick fixes