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 068e6f05cf7..42b53d2084d 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 @@ -139,3 +139,8 @@ checker.name.UsingInHeaderChecker = Using directive in header files checker problem.name.UsingInHeaderProblem = Using directive in header problem.messagePattern.UsingInHeaderProblem = Using directive in header files can cause name clashes in other files problem.description.UsingInHeaderProblem = This rule will flag 'using' directive in header files + +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 diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml index 93c074f67b4..126ac3a5485 100644 --- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml @@ -454,5 +454,20 @@ name="%problem.name.UsingInHeaderProblem"> + + + + diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java new file mode 100644 index 00000000000..6a3130babb7 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CStyleCastChecker.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * 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.ILinkage; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCastExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; + +public class CStyleCastChecker extends AbstractIndexAstChecker { + public static final String ERR_ID = "org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem"; //$NON-NLS-1$ + + @Override + public void processAst(IASTTranslationUnit ast) { + if (ast.getLinkage().getLinkageID() == ILinkage.CPP_LINKAGE_ID) { + ast.accept(new ASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTCastExpression) { + if (((IASTCastExpression) expression).getOperator() == IASTCastExpression.op_cast) + reportProblem(ERR_ID, expression); + } + return PROCESS_CONTINUE; + } + }); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java new file mode 100644 index 00000000000..e4102e047ed --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * 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.CStyleCastChecker; +import org.eclipse.cdt.codan.internal.checkers.UsingInHeaderChecker; + +/** + * Test for {@link UsingInHeaderChecker} class + */ +public class CStyleCastCheckerTest extends CheckerTestCase { + + public static final String ERR_ID = CStyleCastChecker.ERR_ID; + + @Override + public void setUp() throws Exception { + super.setUp(); + enableProblems(ERR_ID); + } + + @Override + public boolean isCpp() { + return true; + } + + //void bar() { + // int* p = (int*) malloc(10); + //}; + public void testUsingInGlobalNamespace() throws Exception { + loadCodeAndRun(getAboveComment()); + checkErrorLine(2, ERR_ID); + } + + //void bar() { + // int* p = static_cast(malloc(10)); + //}; + public void testUsingInNamespace() throws Exception { + loadCodeAndRun(getAboveComment()); + checkNoErrorsOfKind(ERR_ID); + } +}