Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-05-29 17:03:21 +0000
committerMarco Stornelli2019-05-30 15:41:05 +0000
commit90358f53746e2227005c82d5412a8fca1c83b9cd (patch)
tree3384cc849e05adcaf1243dfef50abda2d756c248
parent1c147d87ceaa7c8d277295e3e4c59ec4aebb858f (diff)
downloadorg.eclipse.cdt-90358f53746e2227005c82d5412a8fca1c83b9cd.tar.gz
org.eclipse.cdt-90358f53746e2227005c82d5412a8fca1c83b9cd.tar.xz
org.eclipse.cdt-90358f53746e2227005c82d5412a8fca1c83b9cd.zip
Bug 355174 - Improved tests and code for quickfix
Change-Id: I032040e6c4d3c9342ebdbf72b3ba75114810a31f Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF2
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml4
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java29
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java22
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java50
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java35
6 files changed, 118 insertions, 24 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
index ca272a0877e..9724352eaf2 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
@@ -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,
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
index e4921ba9afa..f795a728f95 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
@@ -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"
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java
index 5f02f1fc152..55ad0c8a72a 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java
@@ -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();
}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java
index 468dc3eed2b..219dbe67a90 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java
@@ -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);
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java
index d20b41f9ede..2da277dc137 100644
--- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java
@@ -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$
+ }
}
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java
index 45a0732bf38..29fb17ddabe 100644
--- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java
@@ -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$
+ }
}

Back to the top