From fb1eb3faedaca8bafbfec1df9771f505d0ce791a Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Thu, 27 May 2010 01:21:57 +0000 Subject: [PATCH] Added parameters for Catch by Reference checker --- .../internal/checkers/CatchByReference.java | 40 ++++++++++++++++++- .../internal/checkers/CheckersMessages.java | 1 + .../internal/checkers/messages.properties | 1 + .../checkers/CatchByReferenceTest.java | 25 +++++++++++- .../cdt/codan/core/test/CheckerTestCase.java | 9 ++++- 5 files changed, 73 insertions(+), 3 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CatchByReference.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CatchByReference.java index a96d3218f46..3b9eafd905a 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CatchByReference.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CatchByReference.java @@ -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); + } } diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java index 9e1c4a77ce3..089934f6330 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CheckersMessages.java @@ -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; diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/messages.properties b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/messages.properties index f6944378a40..15fdcd98fbb 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/messages.properties +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/messages.properties @@ -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) diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CatchByReferenceTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CatchByReferenceTest.java index b8148746065..d60c1828c49 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CatchByReferenceTest.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CatchByReferenceTest.java @@ -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 { @@ -53,7 +57,7 @@ public class CatchByReferenceTest extends CheckerTestCase { loadCodeAndRun(getAboveComment()); checkNoErrors(); } - + // 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() { diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java index 3282ea3c39e..41daecee476 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java @@ -35,7 +35,7 @@ public class CheckerTestCase extends CodanTestCase { /** * @param expectedLine - * - line + * - line * @return */ public IMarker checkErrorLine(File file, int expectedLine) { @@ -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