mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-05 08:46:02 +02:00
Refactored adding parameter and renamed hash to map
This commit is contained in:
parent
e76f05df90
commit
459a3c2b6e
7 changed files with 64 additions and 22 deletions
|
@ -20,9 +20,6 @@ import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||||
import org.eclipse.cdt.codan.core.model.cfg.ICfgData;
|
import org.eclipse.cdt.codan.core.model.cfg.ICfgData;
|
||||||
import org.eclipse.cdt.codan.core.model.cfg.IControlFlowGraph;
|
import org.eclipse.cdt.codan.core.model.cfg.IControlFlowGraph;
|
||||||
import org.eclipse.cdt.codan.core.model.cfg.IExitNode;
|
import org.eclipse.cdt.codan.core.model.cfg.IExitNode;
|
||||||
import org.eclipse.cdt.codan.core.param.HashParameterInfo;
|
|
||||||
import org.eclipse.cdt.codan.core.param.IProblemParameterInfo.ParameterType;
|
|
||||||
import org.eclipse.cdt.codan.core.param.SingleParameterInfo;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||||
|
@ -61,7 +58,8 @@ public class ReturnChecker extends AbstractAstFunctionChecker implements
|
||||||
if (stmt instanceof IASTReturnStatement) {
|
if (stmt instanceof IASTReturnStatement) {
|
||||||
IASTReturnStatement ret = (IASTReturnStatement) stmt;
|
IASTReturnStatement ret = (IASTReturnStatement) stmt;
|
||||||
if (!isVoid(func)) {
|
if (!isVoid(func)) {
|
||||||
if (checkImplicitReturn(RET_NO_VALUE_ID) ||isExplicitReturn(func)) {
|
if (checkImplicitReturn(RET_NO_VALUE_ID)
|
||||||
|
|| isExplicitReturn(func)) {
|
||||||
if (ret.getReturnValue() == null)
|
if (ret.getReturnValue() == null)
|
||||||
reportProblem(RET_NO_VALUE_ID, ret);
|
reportProblem(RET_NO_VALUE_ID, ret);
|
||||||
}
|
}
|
||||||
|
@ -97,7 +95,7 @@ public class ReturnChecker extends AbstractAstFunctionChecker implements
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param if - problem id
|
* @param if - problem id
|
||||||
* @return true if need to check inside functions with implicit return
|
* @return true if need to check inside functions with implicit return
|
||||||
*/
|
*/
|
||||||
protected boolean checkImplicitReturn(String id) {
|
protected boolean checkImplicitReturn(String id) {
|
||||||
|
@ -166,11 +164,8 @@ public class ReturnChecker extends AbstractAstFunctionChecker implements
|
||||||
public void initParameters(IProblemWorkingCopy problem) {
|
public void initParameters(IProblemWorkingCopy problem) {
|
||||||
if (problem.getId().equals(RET_NO_VALUE_ID)
|
if (problem.getId().equals(RET_NO_VALUE_ID)
|
||||||
|| problem.getId().equals(RET_NORET_ID)) {
|
|| problem.getId().equals(RET_NORET_ID)) {
|
||||||
HashParameterInfo info1 = new HashParameterInfo();
|
addParam(problem, PARAM_IMPLICIT,
|
||||||
info1.setElement(new SingleParameterInfo(PARAM_IMPLICIT,
|
CheckersMessages.ReturnChecker_Param0, Boolean.FALSE);
|
||||||
CheckersMessages.ReturnChecker_Param0, ParameterType.TYPE_BOOLEAN));
|
|
||||||
problem.setParameterInfo(info1);
|
|
||||||
problem.setParameter(PARAM_IMPLICIT, Boolean.FALSE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This checker finds a problems that cause by lack of understanding operator
|
* This checker finds a problems which are caused by lack of understanding operator
|
||||||
* precedence in C. In any case it is better to surround expressions in
|
* precedence in C. In any case it is better to surround expressions in
|
||||||
* parenthesis to improve readability. Example: ! x>0 && x<10 (this would be
|
* parenthesis to improve readability. Example: ! x>0 && x<10 (this would be
|
||||||
* (!x)>0 && x<10 in C) We only look for &&, || and ! operators (and binary | &
|
* (!x)>0 && x<10 in C) We only look for &&, || and ! operators (and binary | &
|
||||||
|
|
|
@ -12,6 +12,10 @@ package org.eclipse.cdt.codan.core.cxx.model;
|
||||||
|
|
||||||
import org.eclipse.cdt.codan.core.model.ICheckerWithParameters;
|
import org.eclipse.cdt.codan.core.model.ICheckerWithParameters;
|
||||||
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||||
|
import org.eclipse.cdt.codan.core.param.MapParameterInfo;
|
||||||
|
import org.eclipse.cdt.codan.core.param.IProblemParameterInfo;
|
||||||
|
import org.eclipse.cdt.codan.core.param.IProblemParameterInfo.ParameterType;
|
||||||
|
import org.eclipse.cdt.codan.core.param.SingleParameterInfo;
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||||
|
@ -24,7 +28,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
|
||||||
* Abstract class for checkers that do all the work on function definition level
|
* Abstract class for checkers that do all the work on function definition level
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractAstFunctionChecker extends
|
public abstract class AbstractAstFunctionChecker extends
|
||||||
AbstractIndexAstChecker implements ICheckerWithParameters{
|
AbstractIndexAstChecker implements ICheckerWithParameters {
|
||||||
public void processAst(IASTTranslationUnit ast) {
|
public void processAst(IASTTranslationUnit ast) {
|
||||||
// traverse the ast using the visitor pattern.
|
// traverse the ast using the visitor pattern.
|
||||||
ast.accept(new ASTVisitor() {
|
ast.accept(new ASTVisitor() {
|
||||||
|
@ -61,4 +65,19 @@ public abstract class AbstractAstFunctionChecker extends
|
||||||
public void initParameters(IProblemWorkingCopy problem) {
|
public void initParameters(IProblemWorkingCopy problem) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IProblemParameterInfo addParam(IProblemWorkingCopy problem,
|
||||||
|
String key, String label, Object defaultValue) {
|
||||||
|
MapParameterInfo map = (MapParameterInfo) problem.getParameterInfo();
|
||||||
|
if (map == null) {
|
||||||
|
map = new MapParameterInfo();
|
||||||
|
problem.setParameterInfo(map);
|
||||||
|
}
|
||||||
|
SingleParameterInfo info = new SingleParameterInfo(key,
|
||||||
|
label,
|
||||||
|
ParameterType.typeOf(defaultValue));
|
||||||
|
map.setElement(info);
|
||||||
|
problem.setParameter(key, defaultValue);
|
||||||
|
return info;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,11 @@ Bundle-RequiredExecutionEnvironment: J2SE-1.5
|
||||||
Export-Package: org.eclipse.cdt.codan.core,
|
Export-Package: org.eclipse.cdt.codan.core,
|
||||||
org.eclipse.cdt.codan.core.model,
|
org.eclipse.cdt.codan.core.model,
|
||||||
org.eclipse.cdt.codan.core.model.cfg;x-friends:="org.eclipse.cdt.codan.core.cxx,org.eclipse.cdt.codan.checkers",
|
org.eclipse.cdt.codan.core.model.cfg;x-friends:="org.eclipse.cdt.codan.core.cxx,org.eclipse.cdt.codan.checkers",
|
||||||
org.eclipse.cdt.codan.core.param;x-friends:="org.eclipse.cdt.codan.checkers,org.eclipse.cdt.codan.checkers.ui,org.eclipse.cdt.codan.ui",
|
org.eclipse.cdt.codan.core.param;
|
||||||
|
x-friends:="org.eclipse.cdt.codan.checkers,
|
||||||
|
org.eclipse.cdt.codan.checkers.ui,
|
||||||
|
org.eclipse.cdt.codan.ui,
|
||||||
|
org.eclipse.cdt.codan.core.cxx",
|
||||||
org.eclipse.cdt.codan.internal.core;
|
org.eclipse.cdt.codan.internal.core;
|
||||||
x-friends:="org.eclipse.cdt.codan.core,
|
x-friends:="org.eclipse.cdt.codan.core,
|
||||||
org.eclipse.cdt.codan.core.cxx,
|
org.eclipse.cdt.codan.core.cxx,
|
||||||
|
|
|
@ -10,7 +10,10 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.codan.core.param;
|
package org.eclipse.cdt.codan.core.param;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Problem parameter usually key=value settings that allows to alter checker
|
* Problem parameter usually key=value settings that allows to alter checker
|
||||||
|
@ -33,7 +36,7 @@ public interface IProblemParameterInfo {
|
||||||
TYPE_BOOLEAN("boolean"), //$NON-NLS-1$
|
TYPE_BOOLEAN("boolean"), //$NON-NLS-1$
|
||||||
TYPE_FILE("file"), //$NON-NLS-1$
|
TYPE_FILE("file"), //$NON-NLS-1$
|
||||||
TYPE_LIST("list"), //$NON-NLS-1$
|
TYPE_LIST("list"), //$NON-NLS-1$
|
||||||
TYPE_HASH("hash"); //$NON-NLS-1$
|
TYPE_MAP("map"); //$NON-NLS-1$
|
||||||
private String literal;
|
private String literal;
|
||||||
|
|
||||||
private ParameterType(String literal) {
|
private ParameterType(String literal) {
|
||||||
|
@ -54,6 +57,27 @@ public interface IProblemParameterInfo {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return literal;
|
return literal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value
|
||||||
|
* @return parameter type corresponding to the value java type
|
||||||
|
*/
|
||||||
|
public static ParameterType typeOf(Object value) {
|
||||||
|
if (value instanceof Boolean)
|
||||||
|
return TYPE_BOOLEAN;
|
||||||
|
if (value instanceof String)
|
||||||
|
return TYPE_STRING;
|
||||||
|
if (value instanceof Integer)
|
||||||
|
return TYPE_INTEGER;
|
||||||
|
if (value instanceof File)
|
||||||
|
return TYPE_FILE;
|
||||||
|
if (value instanceof List)
|
||||||
|
return TYPE_LIST;
|
||||||
|
if (value instanceof Map)
|
||||||
|
return TYPE_MAP;
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Cannot determine type for " + value.getClass()); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String getKey();
|
String getKey();
|
||||||
|
|
|
@ -22,10 +22,10 @@ import java.util.Iterator;
|
||||||
* "check setters". In this case you use this type.
|
* "check setters". In this case you use this type.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HashParameterInfo extends SingleParameterInfo {
|
public class MapParameterInfo extends SingleParameterInfo {
|
||||||
protected HashMap<String, IProblemParameterInfo> hash = new HashMap<String, IProblemParameterInfo>();
|
protected HashMap<String, IProblemParameterInfo> hash = new HashMap<String, IProblemParameterInfo>();
|
||||||
|
|
||||||
public HashParameterInfo() {
|
public MapParameterInfo() {
|
||||||
super(""); //$NON-NLS-1$
|
super(""); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class HashParameterInfo extends SingleParameterInfo {
|
||||||
* @param label
|
* @param label
|
||||||
* - label for this group of parameters
|
* - label for this group of parameters
|
||||||
*/
|
*/
|
||||||
public HashParameterInfo(String label) {
|
public MapParameterInfo(String label) {
|
||||||
super(label);
|
super(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,13 +43,13 @@ public class HashParameterInfo extends SingleParameterInfo {
|
||||||
* @param label
|
* @param label
|
||||||
* - label for this group of parameters
|
* - label for this group of parameters
|
||||||
*/
|
*/
|
||||||
public HashParameterInfo(String key, String label) {
|
public MapParameterInfo(String key, String label) {
|
||||||
super(key, label);
|
super(key, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ParameterType getType() {
|
public ParameterType getType() {
|
||||||
return ParameterType.TYPE_HASH;
|
return ParameterType.TYPE_MAP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
|
@ -79,7 +79,7 @@ public class ParametersComposite extends Composite {
|
||||||
addField(fe);
|
addField(fe);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_HASH: {
|
case TYPE_MAP: {
|
||||||
Iterator<IProblemParameterInfo> iterator = info
|
Iterator<IProblemParameterInfo> iterator = info
|
||||||
.getIterator();
|
.getIterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
|
@ -121,7 +121,7 @@ public class ParametersComposite extends Composite {
|
||||||
return;
|
return;
|
||||||
String key = info.getKey();
|
String key = info.getKey();
|
||||||
Object parameter = problem.getParameter(key);
|
Object parameter = problem.getParameter(key);
|
||||||
if (info.getType() == ParameterType.TYPE_HASH && parameter == null) {
|
if (info.getType() == ParameterType.TYPE_MAP && parameter == null) {
|
||||||
Iterator<IProblemParameterInfo> iterator = info.getIterator();
|
Iterator<IProblemParameterInfo> iterator = info.getIterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
IProblemParameterInfo info1 = iterator.next();
|
IProblemParameterInfo info1 = iterator.next();
|
||||||
|
@ -145,7 +145,7 @@ public class ParametersComposite extends Composite {
|
||||||
return;
|
return;
|
||||||
String key = info.getKey();
|
String key = info.getKey();
|
||||||
Object parameter = problem.getParameter(key);
|
Object parameter = problem.getParameter(key);
|
||||||
if (info.getType() == ParameterType.TYPE_HASH && parameter == null) {
|
if (info.getType() == ParameterType.TYPE_MAP && parameter == null) {
|
||||||
Iterator<IProblemParameterInfo> iterator = info.getIterator();
|
Iterator<IProblemParameterInfo> iterator = info.getIterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
IProblemParameterInfo info1 = iterator.next();
|
IProblemParameterInfo info1 = iterator.next();
|
||||||
|
|
Loading…
Add table
Reference in a new issue