Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlena Laskavaia2009-11-20 19:31:38 +0000
committerAlena Laskavaia2009-11-20 19:31:38 +0000
commitf1483c3294d50b563ddffb124eee580ce9dec472 (patch)
treeae7537401afde97898d19c09f5ff93c8deb1e5a6 /codan/org.eclipse.cdt.codan.checkers
parent0c6de59669f50124ea5b708e7c6abbd26fceaf67 (diff)
downloadorg.eclipse.cdt-f1483c3294d50b563ddffb124eee580ce9dec472.tar.gz
org.eclipse.cdt-f1483c3294d50b563ddffb124eee580ce9dec472.tar.xz
org.eclipse.cdt-f1483c3294d50b563ddffb124eee580ce9dec472.zip
[281187] - added checker to find uses of reference in declarator of catch exception
Diffstat (limited to 'codan/org.eclipse.cdt.codan.checkers')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/plugin.xml13
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java82
2 files changed, 95 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml
index e2dac3e0d3a..dde64b6e931 100644
--- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml
@@ -40,5 +40,18 @@
name="Class has a virtual method and non-virtual destructor">
</problem>
</checker>
+
+ <checker
+ class="org.eclipse.cdt.codan.checkers.sample.CatchUsesReference"
+ id="org.eclipse.cdt.codan.checkers.sample.CatchUsesReference"
+ name="CatchUsesReferenceChecker">
+ <problem
+ category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"
+ defaultSeverity="Warning"
+ id="org.eclipse.cdt.codan.checkers.sample.CatchUsesReference"
+ name="Catch uses reference to exception">
+ </problem>
+ </checker>
+
</extension>
</plugin>
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java
new file mode 100644
index 00000000000..d1ad9aae1a5
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Alena Laskavaia
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Alena Laskavaia - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.cdt.codan.checkers.sample;
+
+import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker;
+import org.eclipse.cdt.core.dom.ast.ASTVisitor;
+import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
+import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+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.ICPPASTTryBlockStatement;
+
+/**
+ * @author Alena
+ *
+ */
+public class CatchUsesReference extends AbstractIndexAstChecker {
+ private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.CatchUsesReference";
+
+ public void processAst(IASTTranslationUnit ast) {
+ // traverse the ast using the visitor pattern.
+ ast.accept(new OnCatch());
+ }
+
+ class OnCatch extends ASTVisitor {
+ OnCatch() {
+ shouldVisitStatements = true;
+ }
+ public int visit(IASTStatement stmt) {
+ if (stmt instanceof ICPPASTTryBlockStatement) {
+ ICPPASTTryBlockStatement tblock = (ICPPASTTryBlockStatement) stmt;
+ ICPPASTCatchHandler[] catchHandlers = tblock.getCatchHandlers();
+ for (int i = 0; i < catchHandlers.length; i++) {
+ ICPPASTCatchHandler catchHandler = catchHandlers[i];
+ if (usesReference(catchHandler)) {
+ reportProblem(ER_ID, catchHandler, "Catch clause uses reference in declaration of exception");
+ }
+ }
+
+ return PROCESS_SKIP;
+ }
+ return PROCESS_CONTINUE;
+ }
+ /**
+ * @param catchHandler
+ * @return
+ */
+ private boolean usesReference(ICPPASTCatchHandler catchHandler) {
+ IASTDeclaration declaration = catchHandler.getDeclaration();
+ if (declaration instanceof IASTSimpleDeclaration) {
+ IASTDeclarator[] declarators = ((IASTSimpleDeclaration) declaration).getDeclarators();
+ for (int i = 0; i < declarators.length; i++) {
+ IASTDeclarator d = declarators[i];
+ IASTPointerOperator[] pointerOperators = d.getPointerOperators();
+ for (int j = 0; j < pointerOperators.length; j++) {
+ IASTPointerOperator po = pointerOperators[j];
+ if (po instanceof ICPPASTReferenceOperator) {
+ return true;
+ }
+
+ }
+ }
+ }
+ return false;
+ }
+ }
+
+
+}

Back to the top