1
0
Fork 0
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:
Marco Stornelli 2019-05-29 19:03:21 +02:00
parent 1c147d87ce
commit 90358f5374
6 changed files with 118 additions and 24 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
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
Require-Bundle: org.eclipse.core.resources,
org.eclipse.core.runtime,

View file

@ -5,11 +5,11 @@
point="org.eclipse.cdt.codan.ui.codanMarkerResolution">
<resolution
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
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
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCppCastStatic"

View file

@ -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.IASTName;
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.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -90,25 +91,29 @@ public class QuickFixAddCaseSwitch extends AbstractAstRewriteQuickFix {
IASTBreakStatement breakStatement = factory.newBreakStatement();
IASTNode[] children = astNode.getChildren();
IASTCompoundStatement compound = null;
IASTNullStatement nullStatement = null;
for (int i = 0; i < children.length; ++i) {
if (children[i] instanceof IASTCompoundStatement) {
compound = (IASTCompoundStatement) children[i];
break;
}
} else if (children[i] instanceof IASTNullStatement)
nullStatement = (IASTNullStatement) children[i];
}
if (compound == null)
if (compound == null && nullStatement != null) {
compound = factory.newCompoundStatement();
for (IASTCaseStatement caseStatement : caseStatements)
compound.addStatement(caseStatement);
compound.addStatement(breakStatement);
r.replace(nullStatement, compound, null);
} else if (compound != null) {
for (IASTCaseStatement caseStatement : caseStatements)
r.insertBefore(compound, null, caseStatement, null);
r.insertBefore(compound, null, breakStatement, null);
} else
return;
for (IASTCaseStatement caseStatement : caseStatements)
r.insertBefore(compound, null, caseStatement, null);
r.insertBefore(compound, null, breakStatement, null);
Change c = r.rewriteAST();
try {
c.perform(new NullProgressMonitor());
} catch (CoreException e) {
CheckersUiActivator.log(e);
return;
}
try {
marker.delete();
} catch (CoreException e) {
CheckersUiActivator.log(e);
@ -122,13 +127,13 @@ public class QuickFixAddCaseSwitch extends AbstractAstRewriteQuickFix {
Map<String, Number> enumValues = new HashMap<>();
if (type instanceof IEnumeration) {
IEnumerator[] enums = ((IEnumeration) type).getEnumerators();
String prefix = "";
String prefix = ""; //$NON-NLS-1$
if (type instanceof ICPPEnumeration) {
String[] qualName = CPPVisitor.getQualifiedName((IEnumeration) type);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < qualName.length - 1; ++i) {
builder.append(qualName[i]);
builder.append("::");
builder.append("::"); //$NON-NLS-1$
}
prefix = builder.toString();
}

View file

@ -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.IASTDefaultStatement;
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.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
@ -58,24 +59,27 @@ public class QuickFixAddDefaultSwitch extends AbstractAstRewriteQuickFix {
IASTBreakStatement breakStatement = factory.newBreakStatement();
IASTNode[] children = astNode.getChildren();
IASTCompoundStatement compound = null;
IASTNullStatement nullStatement = null;
for (int i = 0; i < children.length; ++i) {
if (children[i] instanceof IASTCompoundStatement) {
compound = (IASTCompoundStatement) children[i];
break;
}
} else if (children[i] instanceof IASTNullStatement)
nullStatement = (IASTNullStatement) children[i];
}
if (compound == null)
if (compound == null && nullStatement != null) {
compound = factory.newCompoundStatement();
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, breakStatement, null);
} else
return;
r.insertBefore(compound, null, defStatement, null);
r.insertBefore(compound, null, breakStatement, null);
Change c = r.rewriteAST();
try {
c.perform(new NullProgressMonitor());
} catch (CoreException e) {
CheckersUiActivator.log(e);
return;
}
try {
marker.delete();
} catch (CoreException e) {
CheckersUiActivator.log(e);

View file

@ -50,4 +50,54 @@ public class QuickFixAddCaseTest extends QuickFixTestCase {
assertContainedIn("PEAR:", 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$
}
}

View file

@ -47,4 +47,39 @@ public class QuickFixAddDefaultTest extends QuickFixTestCase {
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$
}
}