mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +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.IControlFlowGraph;
|
||||
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.IASTDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||
|
@ -61,7 +58,8 @@ public class ReturnChecker extends AbstractAstFunctionChecker implements
|
|||
if (stmt instanceof IASTReturnStatement) {
|
||||
IASTReturnStatement ret = (IASTReturnStatement) stmt;
|
||||
if (!isVoid(func)) {
|
||||
if (checkImplicitReturn(RET_NO_VALUE_ID) ||isExplicitReturn(func)) {
|
||||
if (checkImplicitReturn(RET_NO_VALUE_ID)
|
||||
|| isExplicitReturn(func)) {
|
||||
if (ret.getReturnValue() == null)
|
||||
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
|
||||
*/
|
||||
protected boolean checkImplicitReturn(String id) {
|
||||
|
@ -166,11 +164,8 @@ public class ReturnChecker extends AbstractAstFunctionChecker implements
|
|||
public void initParameters(IProblemWorkingCopy problem) {
|
||||
if (problem.getId().equals(RET_NO_VALUE_ID)
|
||||
|| problem.getId().equals(RET_NORET_ID)) {
|
||||
HashParameterInfo info1 = new HashParameterInfo();
|
||||
info1.setElement(new SingleParameterInfo(PARAM_IMPLICIT,
|
||||
CheckersMessages.ReturnChecker_Param0, ParameterType.TYPE_BOOLEAN));
|
||||
problem.setParameterInfo(info1);
|
||||
problem.setParameter(PARAM_IMPLICIT, Boolean.FALSE);
|
||||
addParam(problem, PARAM_IMPLICIT,
|
||||
CheckersMessages.ReturnChecker_Param0, Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
|||
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
|
||||
* 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 | &
|
||||
|
|
|
@ -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.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.IASTDeclSpecifier;
|
||||
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
|
||||
*/
|
||||
public abstract class AbstractAstFunctionChecker extends
|
||||
AbstractIndexAstChecker implements ICheckerWithParameters{
|
||||
AbstractIndexAstChecker implements ICheckerWithParameters {
|
||||
public void processAst(IASTTranslationUnit ast) {
|
||||
// traverse the ast using the visitor pattern.
|
||||
ast.accept(new ASTVisitor() {
|
||||
|
@ -61,4 +65,19 @@ public abstract class AbstractAstFunctionChecker extends
|
|||
public void initParameters(IProblemWorkingCopy problem) {
|
||||
// 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,
|
||||
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.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;
|
||||
x-friends:="org.eclipse.cdt.codan.core,
|
||||
org.eclipse.cdt.codan.core.cxx,
|
||||
|
|
|
@ -10,7 +10,10 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.param;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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_FILE("file"), //$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 ParameterType(String literal) {
|
||||
|
@ -54,6 +57,27 @@ public interface IProblemParameterInfo {
|
|||
public String toString() {
|
||||
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();
|
||||
|
|
|
@ -22,10 +22,10 @@ import java.util.Iterator;
|
|||
* "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>();
|
||||
|
||||
public HashParameterInfo() {
|
||||
public MapParameterInfo() {
|
||||
super(""); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class HashParameterInfo extends SingleParameterInfo {
|
|||
* @param label
|
||||
* - label for this group of parameters
|
||||
*/
|
||||
public HashParameterInfo(String label) {
|
||||
public MapParameterInfo(String label) {
|
||||
super(label);
|
||||
}
|
||||
|
||||
|
@ -43,13 +43,13 @@ public class HashParameterInfo extends SingleParameterInfo {
|
|||
* @param label
|
||||
* - label for this group of parameters
|
||||
*/
|
||||
public HashParameterInfo(String key, String label) {
|
||||
public MapParameterInfo(String key, String label) {
|
||||
super(key, label);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterType getType() {
|
||||
return ParameterType.TYPE_HASH;
|
||||
return ParameterType.TYPE_MAP;
|
||||
}
|
||||
|
||||
@Override
|
|
@ -79,7 +79,7 @@ public class ParametersComposite extends Composite {
|
|||
addField(fe);
|
||||
break;
|
||||
}
|
||||
case TYPE_HASH: {
|
||||
case TYPE_MAP: {
|
||||
Iterator<IProblemParameterInfo> iterator = info
|
||||
.getIterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
@ -121,7 +121,7 @@ public class ParametersComposite extends Composite {
|
|||
return;
|
||||
String key = info.getKey();
|
||||
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();
|
||||
while (iterator.hasNext()) {
|
||||
IProblemParameterInfo info1 = iterator.next();
|
||||
|
@ -145,7 +145,7 @@ public class ParametersComposite extends Composite {
|
|||
return;
|
||||
String key = info.getKey();
|
||||
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();
|
||||
while (iterator.hasNext()) {
|
||||
IProblemParameterInfo info1 = iterator.next();
|
||||
|
|
Loading…
Add table
Reference in a new issue