diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml index cf753037cb3..e4921ba9afa 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml @@ -11,6 +11,22 @@ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddCaseSwitch" problemId="com.baldapps.artemis.checkers.MissCaseProblem"> + + + + + + + + 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 00000000000..9ebe85067e4 --- /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 00000000000..7fa914a863b --- /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 00000000000..92dc395b217 --- /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 00000000000..b2e99d33b85 --- /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 00000000000..83057524991 --- /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 444a565aa6a..5cdecfd4f61 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 9c5486487c4..bfab4d5178b 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 52107633ac7..40120ed32de 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 00000000000..e8f81503e7a --- /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(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(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(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(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(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(b);", result); + } +}