mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 355174 - Improved tests and code for quickfix
Change-Id: I032040e6c4d3c9342ebdbf72b3ba75114810a31f Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
1c147d87ce
commit
90358f5374
6 changed files with 118 additions and 24 deletions
|
@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||||
Bundle-ManifestVersion: 2
|
Bundle-ManifestVersion: 2
|
||||||
Bundle-Name: %Bundle-Name
|
Bundle-Name: %Bundle-Name
|
||||||
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers.ui;singleton:=true
|
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers.ui;singleton:=true
|
||||||
Bundle-Version: 3.2.3.qualifier
|
Bundle-Version: 3.2.100.qualifier
|
||||||
Bundle-Activator: org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator
|
Bundle-Activator: org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator
|
||||||
Require-Bundle: org.eclipse.core.resources,
|
Require-Bundle: org.eclipse.core.resources,
|
||||||
org.eclipse.core.runtime,
|
org.eclipse.core.runtime,
|
||||||
|
|
|
@ -5,11 +5,11 @@
|
||||||
point="org.eclipse.cdt.codan.ui.codanMarkerResolution">
|
point="org.eclipse.cdt.codan.ui.codanMarkerResolution">
|
||||||
<resolution
|
<resolution
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddDefaultSwitch"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAddDefaultSwitch"
|
||||||
problemId="com.baldapps.artemis.checkers.MissDefaultProblem">
|
problemId="org.eclipse.cdt.codan.internal.checkers.MissDefaultProblem">
|
||||||
</resolution>
|
</resolution>
|
||||||
<resolution
|
<resolution
|
||||||
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="org.eclipse.cdt.codan.internal.checkers.MissCaseProblem">
|
||||||
</resolution>
|
</resolution>
|
||||||
<resolution
|
<resolution
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastStatic"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastStatic"
|
||||||
|
|
|
@ -31,6 +31,7 @@ import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
@ -90,25 +91,29 @@ public class QuickFixAddCaseSwitch extends AbstractAstRewriteQuickFix {
|
||||||
IASTBreakStatement breakStatement = factory.newBreakStatement();
|
IASTBreakStatement breakStatement = factory.newBreakStatement();
|
||||||
IASTNode[] children = astNode.getChildren();
|
IASTNode[] children = astNode.getChildren();
|
||||||
IASTCompoundStatement compound = null;
|
IASTCompoundStatement compound = null;
|
||||||
|
IASTNullStatement nullStatement = null;
|
||||||
for (int i = 0; i < children.length; ++i) {
|
for (int i = 0; i < children.length; ++i) {
|
||||||
if (children[i] instanceof IASTCompoundStatement) {
|
if (children[i] instanceof IASTCompoundStatement) {
|
||||||
compound = (IASTCompoundStatement) children[i];
|
compound = (IASTCompoundStatement) children[i];
|
||||||
break;
|
break;
|
||||||
|
} else if (children[i] instanceof IASTNullStatement)
|
||||||
|
nullStatement = (IASTNullStatement) children[i];
|
||||||
}
|
}
|
||||||
}
|
if (compound == null && nullStatement != null) {
|
||||||
if (compound == null)
|
compound = factory.newCompoundStatement();
|
||||||
return;
|
for (IASTCaseStatement caseStatement : caseStatements)
|
||||||
|
compound.addStatement(caseStatement);
|
||||||
|
compound.addStatement(breakStatement);
|
||||||
|
r.replace(nullStatement, compound, null);
|
||||||
|
} else if (compound != null) {
|
||||||
for (IASTCaseStatement caseStatement : caseStatements)
|
for (IASTCaseStatement caseStatement : caseStatements)
|
||||||
r.insertBefore(compound, null, caseStatement, null);
|
r.insertBefore(compound, null, caseStatement, null);
|
||||||
r.insertBefore(compound, null, breakStatement, null);
|
r.insertBefore(compound, null, breakStatement, null);
|
||||||
|
} else
|
||||||
|
return;
|
||||||
Change c = r.rewriteAST();
|
Change c = r.rewriteAST();
|
||||||
try {
|
try {
|
||||||
c.perform(new NullProgressMonitor());
|
c.perform(new NullProgressMonitor());
|
||||||
} catch (CoreException e) {
|
|
||||||
CheckersUiActivator.log(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
marker.delete();
|
marker.delete();
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
CheckersUiActivator.log(e);
|
CheckersUiActivator.log(e);
|
||||||
|
@ -122,13 +127,13 @@ public class QuickFixAddCaseSwitch extends AbstractAstRewriteQuickFix {
|
||||||
Map<String, Number> enumValues = new HashMap<>();
|
Map<String, Number> enumValues = new HashMap<>();
|
||||||
if (type instanceof IEnumeration) {
|
if (type instanceof IEnumeration) {
|
||||||
IEnumerator[] enums = ((IEnumeration) type).getEnumerators();
|
IEnumerator[] enums = ((IEnumeration) type).getEnumerators();
|
||||||
String prefix = "";
|
String prefix = ""; //$NON-NLS-1$
|
||||||
if (type instanceof ICPPEnumeration) {
|
if (type instanceof ICPPEnumeration) {
|
||||||
String[] qualName = CPPVisitor.getQualifiedName((IEnumeration) type);
|
String[] qualName = CPPVisitor.getQualifiedName((IEnumeration) type);
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (int i = 0; i < qualName.length - 1; ++i) {
|
for (int i = 0; i < qualName.length - 1; ++i) {
|
||||||
builder.append(qualName[i]);
|
builder.append(qualName[i]);
|
||||||
builder.append("::");
|
builder.append("::"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
prefix = builder.toString();
|
prefix = builder.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.INodeFactory;
|
import org.eclipse.cdt.core.dom.ast.INodeFactory;
|
||||||
|
@ -58,24 +59,27 @@ public class QuickFixAddDefaultSwitch extends AbstractAstRewriteQuickFix {
|
||||||
IASTBreakStatement breakStatement = factory.newBreakStatement();
|
IASTBreakStatement breakStatement = factory.newBreakStatement();
|
||||||
IASTNode[] children = astNode.getChildren();
|
IASTNode[] children = astNode.getChildren();
|
||||||
IASTCompoundStatement compound = null;
|
IASTCompoundStatement compound = null;
|
||||||
|
IASTNullStatement nullStatement = null;
|
||||||
for (int i = 0; i < children.length; ++i) {
|
for (int i = 0; i < children.length; ++i) {
|
||||||
if (children[i] instanceof IASTCompoundStatement) {
|
if (children[i] instanceof IASTCompoundStatement) {
|
||||||
compound = (IASTCompoundStatement) children[i];
|
compound = (IASTCompoundStatement) children[i];
|
||||||
break;
|
break;
|
||||||
|
} else if (children[i] instanceof IASTNullStatement)
|
||||||
|
nullStatement = (IASTNullStatement) children[i];
|
||||||
}
|
}
|
||||||
}
|
if (compound == null && nullStatement != null) {
|
||||||
if (compound == null)
|
compound = factory.newCompoundStatement();
|
||||||
return;
|
compound.addStatement(defStatement);
|
||||||
|
compound.addStatement(breakStatement);
|
||||||
|
r.replace(nullStatement, compound, null);
|
||||||
|
} else if (compound != null) {
|
||||||
r.insertBefore(compound, null, defStatement, null);
|
r.insertBefore(compound, null, defStatement, null);
|
||||||
r.insertBefore(compound, null, breakStatement, null);
|
r.insertBefore(compound, null, breakStatement, null);
|
||||||
|
} else
|
||||||
|
return;
|
||||||
Change c = r.rewriteAST();
|
Change c = r.rewriteAST();
|
||||||
try {
|
try {
|
||||||
c.perform(new NullProgressMonitor());
|
c.perform(new NullProgressMonitor());
|
||||||
} catch (CoreException e) {
|
|
||||||
CheckersUiActivator.log(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
marker.delete();
|
marker.delete();
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
CheckersUiActivator.log(e);
|
CheckersUiActivator.log(e);
|
||||||
|
|
|
@ -50,4 +50,54 @@ public class QuickFixAddCaseTest extends QuickFixTestCase {
|
||||||
assertContainedIn("PEAR:", result); //$NON-NLS-1$
|
assertContainedIn("PEAR:", result); //$NON-NLS-1$
|
||||||
assertContainedIn("BANANA:", result); //$NON-NLS-1$
|
assertContainedIn("BANANA:", result); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//enum FRUIT {
|
||||||
|
// APPLE, PEAR, BANANA
|
||||||
|
//};
|
||||||
|
//void func() {
|
||||||
|
//FRUIT f = APPLE;
|
||||||
|
//switch (f)
|
||||||
|
//{
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
public void testAddCase2() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("PEAR:", result); //$NON-NLS-1$
|
||||||
|
assertContainedIn("BANANA:", result); //$NON-NLS-1$
|
||||||
|
assertContainedIn("APPLE:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
//enum FRUIT {
|
||||||
|
// APPLE, PEAR, BANANA
|
||||||
|
//};
|
||||||
|
//void func() {
|
||||||
|
//FRUIT f = APPLE;
|
||||||
|
//switch (f)
|
||||||
|
// ;
|
||||||
|
//}
|
||||||
|
public void testAddCase3() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("PEAR:", result); //$NON-NLS-1$
|
||||||
|
assertContainedIn("BANANA:", result); //$NON-NLS-1$
|
||||||
|
assertContainedIn("APPLE:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
//enum FRUIT {
|
||||||
|
// APPLE, PEAR, BANANA
|
||||||
|
//};
|
||||||
|
//void func() {
|
||||||
|
//FRUIT f = APPLE;
|
||||||
|
//switch (f)
|
||||||
|
// case APPLE:
|
||||||
|
// break;
|
||||||
|
//}
|
||||||
|
public void testAddCase4() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("PEAR:", result); //$NON-NLS-1$
|
||||||
|
assertContainedIn("BANANA:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,4 +47,39 @@ public class QuickFixAddDefaultTest extends QuickFixTestCase {
|
||||||
assertContainedIn("default:", result); //$NON-NLS-1$
|
assertContainedIn("default:", result); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//void func() {
|
||||||
|
//int f = 0;
|
||||||
|
//switch (f)
|
||||||
|
//{
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
public void testAddCase2() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("default:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
//void func() {
|
||||||
|
//int f = 0;
|
||||||
|
//switch (f)
|
||||||
|
// ;
|
||||||
|
//}
|
||||||
|
public void testAddCase3() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("default:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
//void func() {
|
||||||
|
//int f = 0;
|
||||||
|
//switch (f)
|
||||||
|
// case 0:
|
||||||
|
// break;
|
||||||
|
//}
|
||||||
|
public void testAddCase4() throws Exception {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("default:", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue