summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Ridge2013-07-18 01:37:08 (EDT)
committer Sergey Prigogin2013-07-18 13:18:19 (EDT)
commit82d19e7096df1445426a8124b90f8c2307b52d57 (patch)
tree91640ab840c4370f23e979eec7f4f606741cace0
parentfd53d6c37cd6f025f98682059edf49879dc269a9 (diff)
downloadorg.eclipse.cdt-82d19e7096df1445426a8124b90f8c2307b52d57.zip
org.eclipse.cdt-82d19e7096df1445426a8124b90f8c2307b52d57.tar.gz
org.eclipse.cdt-82d19e7096df1445426a8124b90f8c2307b52d57.tar.bz2
Bug 363884 - NPE using 'Add break statement' quick fixrefs/changes/40/14640/2
Change-Id: I240b196d04b5f032bfaa9666db595b788b7a1d02 Signed-off-by: Nathan Ridge <zeratul976@hotmail.com> Reviewed-on: https://git.eclipse.org/r/14640 Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com> IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com> Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java18
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixTest.java18
2 files changed, 29 insertions, 7 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java
index 815d411..72738a2 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java
@@ -61,10 +61,6 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
while (beforeBreakNode != null) {
if (beforeBreakNode.getParent() instanceof IASTCompoundStatement
&& beforeBreakNode.getParent().getParent() instanceof IASTSwitchStatement) {
- if (beforeBreakNode instanceof IASTCompoundStatement) {
- IASTStatement[] statements = ((IASTCompoundStatement) beforeBreakNode).getStatements();
- return statements[statements.length - 1]; // return last one
- }
return (IASTStatement) beforeBreakNode;
}
beforeBreakNode = beforeBreakNode.getParent();
@@ -81,8 +77,16 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
IASTStatement beforeBreak = getStmtBeforeBreak(marker, ast);
if (beforeBreak != null && beforeBreak.getParent() instanceof IASTCompoundStatement) {
- IASTCompoundStatement enclosingStatement = (IASTCompoundStatement) beforeBreak.getParent();
- IASTStatement after = getAfterStatement(beforeBreak);
+ IASTCompoundStatement enclosingStatement;
+ IASTStatement after;
+ if (beforeBreak instanceof IASTCompoundStatement) {
+ // Case body is enclosed in braces. Add 'break' as last statement inside braces.
+ enclosingStatement = (IASTCompoundStatement) beforeBreak;
+ after = null;
+ } else {
+ enclosingStatement = (IASTCompoundStatement) beforeBreak.getParent();
+ after = getStatementAfter(beforeBreak);
+ }
ASTRewrite r = ASTRewrite.create(enclosingStatement.getTranslationUnit());
IASTBreakStatement breakStatement = ast.getASTNodeFactory().newBreakStatement();
r.insertBefore(enclosingStatement, after, breakStatement, null);
@@ -96,7 +100,7 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
}
}
- private IASTStatement getAfterStatement(IASTStatement beforeBreak) {
+ private IASTStatement getStatementAfter(IASTStatement beforeBreak) {
IASTCompoundStatement enclosingStatement = (IASTCompoundStatement) beforeBreak.getParent();
IASTStatement after = null;
IASTStatement[] statements = enclosingStatement.getStatements();
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixTest.java
index 4982bb2..3fb5f59 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixTest.java
@@ -49,4 +49,22 @@ public class CaseBreakQuickFixTest extends QuickFixTestCase {
String result = runQuickFixOneFile();
assertContainedIn("hello();\t\tbreak;", result);
}
+
+ // int main() {
+ // int a;
+ // switch(a)
+ // {
+ // case 0:
+ // {
+ // }
+ // default:
+ // break;
+ // }
+ // return 0;
+ // }
+ public void testNPE_bug363884() throws Exception {
+ loadcode(getAboveComment());
+ String result = runQuickFixOneFile();
+ assertContainedIn("break;\t}\t\t\tdefault:", result);
+ }
}