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.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.CxxAstUtils; import org.eclipse.cdt.codan.core.cxx.CxxAstUtils;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; 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.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;
@ -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.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IPointerType; 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.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator; 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 class CatchByReference extends AbstractIndexAstChecker {
public static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.CatchByReference"; //$NON-NLS-1$ 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) { public void processAst(IASTTranslationUnit ast) {
// traverse the ast using the visitor pattern. // traverse the ast using the visitor pattern.
@ -75,7 +79,12 @@ public class CatchByReference extends AbstractIndexAstChecker {
|| typeName instanceof IPointerType || typeName instanceof IPointerType
|| typeName == null) || typeName == null)
continue; 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; 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 { public class CheckersMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.codan.internal.checkers.messages"; //$NON-NLS-1$ 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 NamingConventionFunctionChecker_LabelNamePattern;
public static String ReturnChecker_Param0; public static String ReturnChecker_Param0;
public static String GenericParameter_ParameterExceptions; 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 NamingConventionFunctionChecker_LabelNamePattern=Name Pattern
ReturnChecker_Param0=Also check functions with implicit return value ReturnChecker_Param0=Also check functions with implicit return value
GenericParameter_ParameterExceptions=Exceptions (value of the problem argument) GenericParameter_ParameterExceptions=Exceptions (value of the problem argument)

View file

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

View file

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