Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorMarco Stornelli2019-05-28 16:55:45 +0000
committerMarco Stornelli2019-05-30 05:09:45 +0000
commit1c147d87ceaa7c8d277295e3e4c59ec4aebb858f (patch)
tree680cbd29951ab8f04be6f9fa134ddfd3e168614f /codan
parent353315f84ae85704e444482236615415589fc2ac (diff)
downloadorg.eclipse.cdt-1c147d87ceaa7c8d277295e3e4c59ec4aebb858f.tar.gz
org.eclipse.cdt-1c147d87ceaa7c8d277295e3e4c59ec4aebb858f.tar.xz
org.eclipse.cdt-1c147d87ceaa7c8d277295e3e4c59ec4aebb858f.zip
Bug 545699 - Added quickfix for C style cast
Change-Id: Icc1152c013363c5c1b8c2606e3ad0ceb2aa18aa5 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml16
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/AbstractQuickFixCppCast.java64
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastConst.java28
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastDynamic.java28
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastReinterpret.java28
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastStatic.java28
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java4
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties4
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/AutomatedIntegrationSuite.java2
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCStyleCastTest.java105
10 files changed, 307 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
index cf753037cb..e4921ba9af 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
@@ -12,6 +12,22 @@
problemId="com.baldapps.artemis.checkers.MissCaseProblem">
</resolution>
<resolution
+ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastStatic"
+ problemId="org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem">
+ </resolution>
+ <resolution
+ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastDynamic"
+ problemId="org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem">
+ </resolution>
+ <resolution
+ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastConst"
+ problemId="org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem">
+ </resolution>
+ <resolution
+ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastReinterpret"
+ problemId="org.eclipse.cdt.codan.internal.checkers.CStyleCastProblem">
+ </resolution>
+ <resolution
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixForFixit"
messagePattern=".*">
</resolution>
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/AbstractQuickFixCppCast.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/AbstractQuickFixCppCast.java
new file mode 100644
index 0000000000..9ebe85067e
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/AbstractQuickFixCppCast.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * 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.ui.quickfix;
+
+import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
+import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
+import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.core.dom.ast.INodeFactory;
+import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.cdt.core.index.IIndex;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ltk.core.refactoring.Change;
+
+public abstract class AbstractQuickFixCppCast extends AbstractAstRewriteQuickFix {
+
+ protected abstract int getCastType();
+
+ @Override
+ public void modifyAST(IIndex index, IMarker marker) {
+ IASTTranslationUnit ast;
+ try {
+ ITranslationUnit tu = getTranslationUnitViaEditor(marker);
+ ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ return;
+ }
+ IASTNode astNode = null;
+ if (isCodanProblem(marker)) {
+ astNode = getASTNodeFromMarker(marker, ast);
+ }
+ if (astNode == null || !(astNode instanceof IASTCastExpression)) {
+ return;
+ }
+ ASTRewrite r = ASTRewrite.create(ast);
+ INodeFactory factory = ast.getASTNodeFactory();
+ IASTCastExpression oldcast = (IASTCastExpression) astNode;
+ IASTCastExpression newcast = factory.newCastExpression(getCastType(),
+ oldcast.getTypeId().copy(CopyStyle.withLocations), oldcast.getOperand().copy(CopyStyle.withLocations));
+ r.replace(oldcast, newcast, null);
+ Change c = r.rewriteAST();
+ try {
+ c.perform(new NullProgressMonitor());
+ marker.delete();
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ }
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastConst.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastConst.java
new file mode 100644
index 0000000000..7fa914a863
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastConst.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * 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.ui.quickfix;
+
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
+
+public class QuickFixCppCastConst extends AbstractQuickFixCppCast {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixCppCast_const_cast;
+ }
+
+ @Override
+ protected int getCastType() {
+ return ICPPASTCastExpression.op_const_cast;
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastDynamic.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastDynamic.java
new file mode 100644
index 0000000000..92dc395b21
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastDynamic.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * 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.ui.quickfix;
+
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
+
+public class QuickFixCppCastDynamic extends AbstractQuickFixCppCast {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixCppCast_dynamic_cast;
+ }
+
+ @Override
+ protected int getCastType() {
+ return ICPPASTCastExpression.op_dynamic_cast;
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastReinterpret.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastReinterpret.java
new file mode 100644
index 0000000000..b2e99d33b8
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastReinterpret.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * 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.ui.quickfix;
+
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
+
+public class QuickFixCppCastReinterpret extends AbstractQuickFixCppCast {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixCppCast_reinterpret_cast;
+ }
+
+ @Override
+ protected int getCastType() {
+ return ICPPASTCastExpression.op_reinterpret_cast;
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastStatic.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastStatic.java
new file mode 100644
index 0000000000..8305752499
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCppCastStatic.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * 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.ui.quickfix;
+
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
+
+public class QuickFixCppCastStatic extends AbstractQuickFixCppCast {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixCppCast_static_cast;
+ }
+
+ @Override
+ protected int getCastType() {
+ return ICPPASTCastExpression.op_static_cast;
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
index 444a565aa6..5cdecfd4f6 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
@@ -31,6 +31,10 @@ public class QuickFixMessages extends NLS {
public static String QuickFixSuppressProblem_Label;
public static String QuickFixAddDefaultSwitch_add_default_to_switch;
public static String QuickFixAddCaseSwitch_add_cases_to_switch;
+ public static String QuickFixCppCast_const_cast;
+ public static String QuickFixCppCast_dynamic_cast;
+ public static String QuickFixCppCast_reinterpret_cast;
+ public static String QuickFixCppCast_static_cast;
static {
NLS.initializeMessages(QuickFixMessages.class.getName(), QuickFixMessages.class);
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
index 9c5486487c..bfab4d5178 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
@@ -26,3 +26,7 @@ QuickFixForFixit_apply_fixit=Apply compiler recommended fix-it
QuickFixSuppressProblem_Label=Suppress problem "%s"
QuickFixAddDefaultSwitch_add_default_to_switch=Add default to switch
QuickFixAddCaseSwitch_add_cases_to_switch=Add missing cases to switch
+QuickFixCppCast_static_cast=Convert to static cast
+QuickFixCppCast_reinterpret_cast=Convert to reinterpret cast
+QuickFixCppCast_const_cast=Convert to const cast
+QuickFixCppCast_dynamic_cast=Convert to dynamic cast
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 52107633ac..40120ed32d 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
@@ -45,6 +45,7 @@ import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuick
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CreateLocalVariableQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddCaseTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddDefaultTest;
+import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCStyleCastTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixSuppressProblemTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
@@ -107,6 +108,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(QuickFixSuppressProblemTest.class);
suite.addTestSuite(QuickFixAddDefaultTest.class);
suite.addTestSuite(QuickFixAddCaseTest.class);
+ suite.addTestSuite(QuickFixCStyleCastTest.class);
return suite;
}
}
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCStyleCastTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCStyleCastTest.java
new file mode 100644
index 0000000000..e8f81503e7
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCStyleCastTest.java
@@ -0,0 +1,105 @@
+/*******************************************************************************
+ * 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
+ *
+ * Contributors:
+ * Marco Stornelli - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import org.eclipse.cdt.codan.internal.checkers.CStyleCastChecker;
+import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
+
+public class QuickFixCStyleCastTest extends QuickFixTestCase {
+ @Override
+ protected AbstractCodanCMarkerResolution createQuickFix() {
+ return null;
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ enableProblems(CStyleCastChecker.ERR_ID);
+ }
+
+ @Override
+ public boolean isCpp() {
+ return true;
+ }
+
+ //void func() {
+ // int a;
+ // double b = (double) a;
+ //}
+ public void testStaticCast() throws Exception {
+ setQuickFix(new QuickFixCppCastStatic());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("double b = static_cast<double>(a);", result);
+ }
+
+ //typedef int MyInt;
+ //void func() {
+ // MyInt a;
+ // double b = (MyInt) a;
+ //}
+ public void testStaticCastTypedef() throws Exception {
+ setQuickFix(new QuickFixCppCastStatic());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("double b = static_cast<MyInt>(a);", result);
+ }
+
+ //using MyInt = int;
+ //void func() {
+ // MyInt a;
+ // double b = (MyInt) a;
+ //}
+ public void testStaticCastTypeAlias() throws Exception {
+ setQuickFix(new QuickFixCppCastStatic());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("double b = static_cast<MyInt>(a);", result);
+ }
+
+ //void func() {
+ // int *a;
+ // double b = (double) a;
+ //}
+ public void testReinterpretCast() throws Exception {
+ setQuickFix(new QuickFixCppCastReinterpret());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("double b = reinterpret_cast<double>(a);", result);
+ }
+
+ //void func() {
+ // const int a;
+ // int b = (int) a;
+ //}
+ public void testConstCast() throws Exception {
+ setQuickFix(new QuickFixCppCastConst());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("int b = const_cast<int>(a);", result);
+ }
+
+ //class Base {}
+ //class Child1: public Base {}
+ //void func() {
+ // Base* b = new Child1();
+ // Child1 *c = (Child1*) b;
+ //}
+ public void testDynamicCast() throws Exception {
+ setQuickFix(new QuickFixCppCastDynamic());
+ loadcode(getAboveComment(), true);
+ String result = runQuickFixOneFile();
+ assertContainedIn("Child1 *c = dynamic_cast<Child1*>(b);", result);
+ }
+}

Back to the top