mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Bug 545699 - Added checker for C-style casts in C++
Change-Id: I38076599b354608a4b806f7b1d2ca1f6acc50a44 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
ef2468b390
commit
60a4cccbe3
4 changed files with 112 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -454,5 +454,20 @@
|
|||
name="%problem.name.UsingInHeaderProblem">
|
||||
</problem>
|
||||
</checker>
|
||||
<checker
|
||||
class="org.eclipse.cdt.codan.internal.checkers.CStyleCastChecker"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.CStyleCastChecker"
|
||||
name="%checker.name.CStyleCastChecker">
|
||||
<problem
|
||||
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
|
||||
defaultEnabled="false"
|
||||
defaultSeverity="Warning"
|
||||
description="%problem.description.CStyleCastProblem"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem"
|
||||
markerType="org.eclipse.cdt.codan.core.codanProblem"
|
||||
messagePattern="%problem.messagePattern.CStyleCastProblem"
|
||||
name="%problem.name.CStyleCastProblem">
|
||||
</problem>
|
||||
</checker>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<int*>(malloc(10));
|
||||
//};
|
||||
public void testUsingInNamespace() throws Exception {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkNoErrorsOfKind(ERR_ID);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue