Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-03-23 06:00:27 -0400
committerDoug Schaefer2019-04-15 10:28:39 -0400
commit60a4cccbe39f00bf22aa97eaa0882e5cd10b1dbc (patch)
treefbdc0edd75dc57f9a2dc6ebb126396bd9e1c2ddf
parentef2468b390ffbd3950607116d09db504f014ea86 (diff)
downloadorg.eclipse.cdt-60a4cccbe39f00bf22aa97eaa0882e5cd10b1dbc.tar.gz
org.eclipse.cdt-60a4cccbe39f00bf22aa97eaa0882e5cd10b1dbc.tar.xz
org.eclipse.cdt-60a4cccbe39f00bf22aa97eaa0882e5cd10b1dbc.zip
Bug 545699 - Added checker for C-style casts in C++
Change-Id: I38076599b354608a4b806f7b1d2ca1f6acc50a44 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
-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/CStyleCastChecker.java42
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CStyleCastCheckerTest.java50
4 files changed, 112 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 068e6f05cf..42b53d2084 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 93c074f67b..126ac3a548 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">
</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>
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 0000000000..6a3130babb
--- /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 0000000000..e4102e047e
--- /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<int*>(malloc(10));
+ //};
+ public void testUsingInNamespace() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrorsOfKind(ERR_ID);
+ }
+}

Back to the top