mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-21 07:55:24 +02:00
added statement has no effect checker
This commit is contained in:
parent
59cf9d5fad
commit
a2e8d0f91a
2 changed files with 99 additions and 0 deletions
|
@ -14,6 +14,19 @@
|
||||||
id="org.eclipse.cdt.codan.checkers.sample.AssignmentInConditionProblem"
|
id="org.eclipse.cdt.codan.checkers.sample.AssignmentInConditionProblem"
|
||||||
name="Assignment in condition">
|
name="Assignment in condition">
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
|
|
||||||
|
</checker>
|
||||||
|
<checker
|
||||||
|
class="org.eclipse.cdt.codan.checkers.sample.StatementHasNoEffectChecker"
|
||||||
|
id="org.eclipse.cdt.codan.checkers.sample.StatementHasNoEffectChecker"
|
||||||
|
name="StatementHasNoEffectChecker">
|
||||||
|
<problem
|
||||||
|
category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"
|
||||||
|
defaultSeverity="Warning"
|
||||||
|
id="org.eclipse.cdt.codan.checkers.sample.StatementHasNoEffectProblem"
|
||||||
|
name="Statement has no effect">
|
||||||
|
</problem>
|
||||||
</checker>
|
</checker>
|
||||||
</extension>
|
</extension>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2009 Alena Laskavaia
|
||||||
|
* 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:
|
||||||
|
* Alena Laskavaia - initial API and implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.cdt.codan.checkers.sample;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checker that detects statements without effect such as
|
||||||
|
*
|
||||||
|
* a+b;
|
||||||
|
*
|
||||||
|
* or
|
||||||
|
*
|
||||||
|
* +b;
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
||||||
|
private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.StatementHasNoEffectProblem";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processAst(IASTTranslationUnit ast) {
|
||||||
|
// traverse the ast using the visitor pattern.
|
||||||
|
ast.accept(new CheckStmpVisitor());
|
||||||
|
}
|
||||||
|
|
||||||
|
class CheckStmpVisitor extends ASTVisitor {
|
||||||
|
CheckStmpVisitor() {
|
||||||
|
shouldVisitStatements = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int visit(IASTStatement stmt) {
|
||||||
|
if (stmt instanceof IASTExpressionStatement) {
|
||||||
|
if (hasNoEffect(((IASTExpressionStatement) stmt)
|
||||||
|
.getExpression())) {
|
||||||
|
reportProblem(ER_ID, getFile(), stmt.getFileLocation()
|
||||||
|
.getStartingLineNumber(), "Statement has no effect");
|
||||||
|
}
|
||||||
|
return PROCESS_SKIP;
|
||||||
|
}
|
||||||
|
return PROCESS_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We consider has not effect binary statements without assignment and
|
||||||
|
* unary statement which is not dec and inc
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean hasNoEffect(IASTExpression e) {
|
||||||
|
if (e instanceof IASTBinaryExpression) {
|
||||||
|
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
||||||
|
return binExpr.getOperator() != IASTBinaryExpression.op_assign;
|
||||||
|
}
|
||||||
|
if (e instanceof IASTUnaryExpression) {
|
||||||
|
IASTUnaryExpression unaryExpr = (IASTUnaryExpression) e;
|
||||||
|
int operator = unaryExpr.getOperator();
|
||||||
|
switch (operator) {
|
||||||
|
case IASTUnaryExpression.op_postFixDecr:
|
||||||
|
case IASTUnaryExpression.op_prefixDecr:
|
||||||
|
case IASTUnaryExpression.op_postFixIncr:
|
||||||
|
case IASTUnaryExpression.op_prefixIncr:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue