From 488ef11fd4c9d39c702564cdee7ec9e6b6d53c2e Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 26 Oct 2015 16:12:23 +0100 Subject: [PATCH] dfa: support (i)++ (IDEA-146805) --- .../dataFlow/ControlFlowAnalyzer.java | 49 +++++++++---------- .../fixture/IncrementParenthesized.java | 7 +++ .../DataFlowInspectionTest.java | 1 + 3 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/IncrementParenthesized.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java index 1a6e9214f603..209d0e91634c 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java @@ -1217,7 +1217,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { } } - private void generateBoxingUnboxingInstructionFor(PsiExpression expression, PsiType expectedType) { + private void generateBoxingUnboxingInstructionFor(@NotNull PsiExpression expression, PsiType expectedType) { if (expectedType == PsiType.VOID) return; PsiType exprType = expression.getType(); @@ -1617,23 +1617,18 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { @Override public void visitPostfixExpression(PsiPostfixExpression expression) { startElement(expression); - PsiExpression operand = expression.getOperand(); - operand.accept(this); - generateBoxingUnboxingInstructionFor(operand, PsiType.INT); + PsiExpression operand = PsiUtil.skipParenthesizedExprDown(expression.getOperand()); + if (operand != null) { + operand.accept(this); + generateBoxingUnboxingInstructionFor(operand, PsiType.INT); + } else { + pushUnknown(); + } addInstruction(new PopInstruction()); pushUnknown(); - if (operand instanceof PsiReferenceExpression) { - PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)expression.getOperand()); - if (psiVariable != null) { - DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false); - addInstruction(new FlushVariableInstruction(dfaVariable)); - if (psiVariable instanceof PsiField) { - addInstruction(new FlushVariableInstruction(null)); - } - } - } + flushIncrementedValue(operand); finishElement(expression); } @@ -1643,7 +1638,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { DfaValue dfaValue = myFactory.createValue(expression); if (dfaValue == null) { - PsiExpression operand = expression.getOperand(); + PsiExpression operand = PsiUtil.skipParenthesizedExprDown(expression.getOperand()); if (operand == null) { pushUnknown(); @@ -1660,16 +1655,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { addInstruction(new PopInstruction()); pushUnknown(); - if (operand instanceof PsiReferenceExpression) { - PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)operand); - if (psiVariable != null) { - DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false); - addInstruction(new FlushVariableInstruction(dfaVariable)); - if (psiVariable instanceof PsiField) { - addInstruction(new FlushVariableInstruction(null)); - } - } - } + flushIncrementedValue(operand); } } } @@ -1680,6 +1666,19 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { finishElement(expression); } + private void flushIncrementedValue(@Nullable PsiExpression operand) { + if (operand instanceof PsiReferenceExpression) { + PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)operand); + if (psiVariable != null) { + DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false); + addInstruction(new FlushVariableInstruction(dfaVariable)); + if (psiVariable instanceof PsiField) { + addInstruction(new FlushVariableInstruction(null)); + } + } + } + } + @Override public void visitReferenceExpression(PsiReferenceExpression expression) { startElement(expression); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/IncrementParenthesized.java b/java/java-tests/testData/inspection/dataFlow/fixture/IncrementParenthesized.java new file mode 100644 index 000000000000..b5871d3fd07b --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/IncrementParenthesized.java @@ -0,0 +1,7 @@ +class Test { + public static void main(String[] args) { + for (int i = 0; i < 30; (i)++) { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index fdcb1a6d2c10..0d56a084a577 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -245,6 +245,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testDoubleCCEWarning() { doTest(); } public void testLongCircuitOperations() { doTest(); } public void testUnconditionalForLoop() { doTest(); } + public void testIncrementParenthesized() { doTest(); } public void testAnonymousMethodIndependence() { doTest(); } public void testAnonymousFieldIndependence() { doTest(); } public void testNoConfusionWithAnonymousConstantInitializer() { doTest(); }