Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorMarco Stornelli2019-03-30 08:59:12 +0000
committerMarco Stornelli2019-05-08 04:50:59 +0000
commit838a12d8f4cca53657cb3e62fe2dfff2e4d50c6c (patch)
treef927a2f56c62341c48cf51e9b4ceacc1cfe4a297 /codan
parent8b88c9bffea13b448e5149e4edb52245fcc916d6 (diff)
downloadorg.eclipse.cdt-838a12d8f4cca53657cb3e62fe2dfff2e4d50c6c.tar.gz
org.eclipse.cdt-838a12d8f4cca53657cb3e62fe2dfff2e4d50c6c.tar.xz
org.eclipse.cdt-838a12d8f4cca53657cb3e62fe2dfff2e4d50c6c.zip
Bug 545952 - Added checker to check for goto usage
Change-Id: I5f7f157c5c208e686627bb90b001879953d83e70 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties5
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/plugin.xml15
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/GotoStatementChecker.java38
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/GotoStatementCheckerTest.java54
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java2
5 files changed, 114 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties
index 42b53d2084d..5f4b76794e0 100644
--- a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties
+++ b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties
@@ -144,3 +144,8 @@ checker.name.CStyleCastChecker = C-Style cast
problem.name.CStyleCastProblem = C-Style cast instead of C++ cast
problem.messagePattern.CStyleCastProblem = C++ style casts express the intent of the programmer more clearly and they can be checked by compiler
problem.description.CStyleCastProblem = This rule will flag C-style cast expressions in C++ files
+
+checker.name.GotoStatementChecker = Goto statement in source files checker
+problem.name.GotoStatementProblem = Goto statement used
+problem.messagePattern.GotoStatementProblem = Code that uses goto statements is harder to understand than alternative constructions
+problem.description.GotoStatementProblem = This rule will flag goto statements in source files
diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml
index 126ac3a5485..dc150898d69 100644
--- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml
@@ -467,6 +467,21 @@
markerType="org.eclipse.cdt.codan.core.codanProblem"
messagePattern="%problem.messagePattern.CStyleCastProblem"
name="%problem.name.CStyleCastProblem">
+ </problem>
+ </checker>
+ <checker
+ class="org.eclipse.cdt.codan.internal.checkers.GotoStatementChecker"
+ id="org.eclipse.cdt.codan.internal.checkers.GotoStatementChecker"
+ name="%checker.name.GotoStatementChecker">
+ <problem
+ category="org.eclipse.cdt.codan.core.categories.CodeStyle"
+ defaultEnabled="false"
+ defaultSeverity="Warning"
+ description="%problem.description.GotoStatementProblem"
+ id="org.eclipse.cdt.codan.internal.checkers.GotoStatementProblem"
+ markerType="org.eclipse.cdt.codan.core.codanProblem"
+ messagePattern="%problem.messagePattern.GotoStatementProblem"
+ name="%problem.name.GotoStatementProblem">
</problem>
</checker>
</extension>
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/GotoStatementChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/GotoStatementChecker.java
new file mode 100644
index 00000000000..87be65bc6ce
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/GotoStatementChecker.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Marco Stornelli
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers;
+
+import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
+import org.eclipse.cdt.core.dom.ast.ASTVisitor;
+import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
+import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+
+public class GotoStatementChecker extends AbstractIndexAstChecker {
+ public static final String ERR_ID = "org.eclipse.cdt.codan.internal.checkers.GotoStatementProblem"; //$NON-NLS-1$
+
+ @Override
+ public void processAst(IASTTranslationUnit ast) {
+ ast.accept(new ASTVisitor() {
+ {
+ shouldVisitStatements = true;
+ }
+
+ @Override
+ public int visit(IASTStatement statement) {
+ if (statement instanceof IASTGotoStatement) {
+ reportProblem(ERR_ID, statement);
+ }
+ return PROCESS_CONTINUE;
+ }
+ });
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/GotoStatementCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/GotoStatementCheckerTest.java
new file mode 100644
index 00000000000..3ab408939c6
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/GotoStatementCheckerTest.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Marco Stornelli
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.cdt.codan.core.internal.checkers;
+
+import org.eclipse.cdt.codan.core.tests.CheckerTestCase;
+import org.eclipse.cdt.codan.internal.checkers.GotoStatementChecker;
+
+/**
+ * Test for {@link GotoStatementChecker} class
+ */
+public class GotoStatementCheckerTest extends CheckerTestCase {
+
+ public static final String ERR_ID = GotoStatementChecker.ERR_ID;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ enableProblems(ERR_ID);
+ }
+
+ @Override
+ public boolean isCpp() {
+ return true;
+ }
+
+ //void foo() {
+ //while(1) {
+ // goto label;
+ //}
+ //label:
+ //return 1;
+ //}
+ public void testWithGoto() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkErrorLine(3, ERR_ID);
+ }
+
+ //void foo() {
+ //while(1) {
+ // return 1;
+ //}
+ public void testWithoutGoto() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrorsOfKind(ERR_ID);
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java
index 11258e2ef4d..2deb3a5030d 100644
--- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java
@@ -25,6 +25,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerLineTests;
import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerNestedTests;
import org.eclipse.cdt.codan.core.internal.checkers.DecltypeAutoCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.FormatStringCheckerTest;
+import org.eclipse.cdt.codan.core.internal.checkers.GotoStatementCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.NonVirtualDestructorCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.ProblemBindingCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest;
@@ -84,6 +85,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(UnusedSymbolInFileScopeCheckerTest.class);
suite.addTestSuite(CommentCheckerLineTests.class);
suite.addTestSuite(CommentCheckerNestedTests.class);
+ suite.addTestSuite(GotoStatementCheckerTest.class);
// framework
suite.addTest(CodanFastTestSuite.suite());
// quick fixes

Back to the top