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

Bug 545952 - Added checker to check for goto usage

Change-Id: I5f7f157c5c208e686627bb90b001879953d83e70
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-03-30 09:59:12 +01:00
parent 8b88c9bffe
commit 838a12d8f4
5 changed files with 114 additions and 0 deletions

View file

@ -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

View file

@ -469,5 +469,20 @@
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>
</plugin>

View file

@ -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;
}
});
}
}

View file

@ -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);
}
}

View file

@ -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