mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Bug 545699 - Added quickfix for C style cast
Change-Id: Icc1152c013363c5c1b8c2606e3ad0ceb2aa18aa5 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
353315f84a
commit
1c147d87ce
10 changed files with 307 additions and 0 deletions
|
@ -11,6 +11,22 @@
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddCaseSwitch"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddCaseSwitch"
|
||||||
problemId="com.baldapps.artemis.checkers.MissCaseProblem">
|
problemId="com.baldapps.artemis.checkers.MissCaseProblem">
|
||||||
</resolution>
|
</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
|
<resolution
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixForFixit"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixForFixit"
|
||||||
messagePattern=".*">
|
messagePattern=".*">
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -31,6 +31,10 @@ public class QuickFixMessages extends NLS {
|
||||||
public static String QuickFixSuppressProblem_Label;
|
public static String QuickFixSuppressProblem_Label;
|
||||||
public static String QuickFixAddDefaultSwitch_add_default_to_switch;
|
public static String QuickFixAddDefaultSwitch_add_default_to_switch;
|
||||||
public static String QuickFixAddCaseSwitch_add_cases_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 {
|
static {
|
||||||
NLS.initializeMessages(QuickFixMessages.class.getName(), QuickFixMessages.class);
|
NLS.initializeMessages(QuickFixMessages.class.getName(), QuickFixMessages.class);
|
||||||
|
|
|
@ -26,3 +26,7 @@ QuickFixForFixit_apply_fixit=Apply compiler recommended fix-it
|
||||||
QuickFixSuppressProblem_Label=Suppress problem "%s"
|
QuickFixSuppressProblem_Label=Suppress problem "%s"
|
||||||
QuickFixAddDefaultSwitch_add_default_to_switch=Add default to switch
|
QuickFixAddDefaultSwitch_add_default_to_switch=Add default to switch
|
||||||
QuickFixAddCaseSwitch_add_cases_to_switch=Add missing cases 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
|
||||||
|
|
|
@ -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.CreateLocalVariableQuickFixTest;
|
||||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddCaseTest;
|
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.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.QuickFixSuppressProblemTest;
|
||||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
|
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(QuickFixSuppressProblemTest.class);
|
||||||
suite.addTestSuite(QuickFixAddDefaultTest.class);
|
suite.addTestSuite(QuickFixAddDefaultTest.class);
|
||||||
suite.addTestSuite(QuickFixAddCaseTest.class);
|
suite.addTestSuite(QuickFixAddCaseTest.class);
|
||||||
|
suite.addTestSuite(QuickFixCStyleCastTest.class);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue