1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

Added parameters for Catch by Reference checker

This commit is contained in:
Alena Laskavaia 2010-05-27 01:21:57 +00:00
parent 670709454b
commit fb1eb3faed
5 changed files with 73 additions and 3 deletions

View file

@ -13,6 +13,7 @@ package org.eclipse.cdt.codan.internal.checkers;
import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.CxxAstUtils;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@ -26,6 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
@ -40,6 +42,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
*/
public class CatchByReference extends AbstractIndexAstChecker {
public static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.CatchByReference"; //$NON-NLS-1$
public static final String PARAM_EXCEPT_ARG_LIST = "exceptions"; //$NON-NLS-1$
public static final String PARAM_UNKNOWN_TYPE = "unknown"; //$NON-NLS-1$
public void processAst(IASTTranslationUnit ast) {
// traverse the ast using the visitor pattern.
@ -75,7 +79,12 @@ public class CatchByReference extends AbstractIndexAstChecker {
|| typeName instanceof IPointerType
|| typeName == null)
continue;
reportProblem(ER_ID, decl, decl.getRawSignature());
if (typeName instanceof IProblemBinding && !shouldReportForUnknownType())
continue;
String arg = spec.getRawSignature();
if (!isFilteredArg(arg)) {
reportProblem(ER_ID, decl, arg);
}
}
}
}
@ -117,4 +126,33 @@ public class CatchByReference extends AbstractIndexAstChecker {
return false;
}
}
@Override
public void initPreferences(IProblemWorkingCopy problem) {
super.initPreferences(problem);
addPreference(problem, PARAM_UNKNOWN_TYPE,
CheckersMessages.CatchByReference_ReportForUnknownType, Boolean.FALSE);
addListPreference(problem, PARAM_EXCEPT_ARG_LIST,
CheckersMessages.GenericParameter_ParameterExceptions,
CheckersMessages.GenericParameter_ParameterExceptionsItem);
}
public boolean isFilteredArg(String arg) {
Object[] arr = (Object[]) getPreference(
getProblemById(ER_ID, getFile()), PARAM_EXCEPT_ARG_LIST);
for (int i = 0; i < arr.length; i++) {
String str = (String) arr[i];
if (arg.equals(str))
return true;
}
return false;
}
/**
* @return
*/
public boolean shouldReportForUnknownType() {
return (Boolean) getPreference(getProblemById(ER_ID, getFile()),
PARAM_UNKNOWN_TYPE);
}
}

View file

@ -17,6 +17,7 @@ import org.eclipse.osgi.util.NLS;
*/
public class CheckersMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.codan.internal.checkers.messages"; //$NON-NLS-1$
public static String CatchByReference_ReportForUnknownType;
public static String NamingConventionFunctionChecker_LabelNamePattern;
public static String ReturnChecker_Param0;
public static String GenericParameter_ParameterExceptions;

View file

@ -1,3 +1,4 @@
CatchByReference_ReportForUnknownType=Report a problem if type cannot be resolved
NamingConventionFunctionChecker_LabelNamePattern=Name Pattern
ReturnChecker_Param0=Also check functions with implicit return value
GenericParameter_ParameterExceptions=Exceptions (value of the problem argument)

View file

@ -11,12 +11,14 @@
package org.eclipse.cdt.codan.core.internal.checkers;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
import org.eclipse.cdt.codan.internal.checkers.CatchByReference;
/**
* Test for {@see CatchByReference} class
*
*/
public class CatchByReferenceTest extends CheckerTestCase {
@Override
public boolean isCpp() {
return true;
}
@ -31,6 +33,7 @@ public class CatchByReferenceTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// class C {};
// void main() {
// try {
@ -42,6 +45,7 @@ public class CatchByReferenceTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkErrorLine(5);
}
// class C {};
// void main() {
// try {
@ -65,6 +69,7 @@ public class CatchByReferenceTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// typedef int A;
// void main() {
// try {
@ -76,6 +81,7 @@ public class CatchByReferenceTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// typedef int A;
// typedef A B;
// void main() {
@ -88,6 +94,7 @@ public class CatchByReferenceTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// void main() {
// try {
// foo();
@ -95,9 +102,25 @@ public class CatchByReferenceTest extends CheckerTestCase {
// }
// }
public void test_class_unknown() {
setPreferenceValue(CatchByReference.ER_ID,
CatchByReference.PARAM_UNKNOWN_TYPE, false);
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// void main() {
// try {
// foo();
// } catch (C e) {
// }
// }
public void test_class_unknown_on() {
setPreferenceValue(CatchByReference.ER_ID,
CatchByReference.PARAM_UNKNOWN_TYPE, true);
loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
// class C {};
// typedef C B;
// void main() {

View file

@ -135,6 +135,13 @@ public class CheckerTestCase extends CodanTestCase {
return pref;
}
protected IProblemPreference setPreferenceValue(String problemId,
String paramId, Object value) {
IProblemPreference param = getPreference(problemId, paramId);
param.setValue(value);
return param;
}
/**
* @param string
* @param m