### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java,v retrieving revision 1.45 diff -u -r1.45 Statement.java --- compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java 7 Mar 2009 00:58:58 -0000 1.45 +++ compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java 15 Jul 2009 15:05:04 -0000 @@ -17,6 +17,37 @@ public abstract class Statement extends ASTNode { + /** + * Answers true if the if is identified as a known coding pattern which + * should be tolerated by dead code analysis. + * e.g. if (DEBUG) print(); // no complaint + * Only invoked when overall condition is known to be optimizeable into false. + */ + protected static boolean isKnowDeadCodePattern(Expression expression) { + // if (!DEBUG) print(); - tolerated + if (expression instanceof UnaryExpression) { + expression = ((UnaryExpression) expression).expression; + } + // if (DEBUG) print(); - tolerated + if (expression instanceof Reference) return true; + +// if (expression instanceof BinaryExpression) { +// BinaryExpression binary = (BinaryExpression) expression; +// switch ((binary.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT/* operator */) { +// case OperatorIds.AND_AND : +// case OperatorIds.OR_OR : +// break; +// default: +// // if (DEBUG_LEVEL > 0) print(); - tolerated +// if ((binary.left instanceof Reference) && binary.right.constant != Constant.NotAConstant) +// return true; +// // if (0 < DEBUG_LEVEL) print(); - tolerated +// if ((binary.right instanceof Reference) && binary.left.constant != Constant.NotAConstant) +// return true; +// } +// } + return false; + } public abstract FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo); public static final int NOT_COMPLAINED = 0; Index: compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java,v retrieving revision 1.64 diff -u -r1.64 IfStatement.java --- compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java 7 Mar 2009 01:08:07 -0000 1.64 +++ compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java 15 Jul 2009 15:05:04 -0000 @@ -205,37 +205,7 @@ codeStream.recordPositionsFrom(pc, this.sourceStart); } -/** - * Answers true if the if is identified as a known coding pattern which - * should be tolerated by dead code analysis. - * e.g. if (DEBUG) print(); // no complaint - * Only invoked when overall condition is known to be optimizeable into false. - */ -public static boolean isKnowDeadCodePattern(Expression expression) { - // if (!DEBUG) print(); - tolerated - if (expression instanceof UnaryExpression) { - expression = ((UnaryExpression) expression).expression; - } - // if (DEBUG) print(); - tolerated - if (expression instanceof Reference) return true; -// if (expression instanceof BinaryExpression) { -// BinaryExpression binary = (BinaryExpression) expression; -// switch ((binary.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT/* operator */) { -// case OperatorIds.AND_AND : -// case OperatorIds.OR_OR : -// break; -// default: -// // if (DEBUG_LEVEL > 0) print(); - tolerated -// if ((binary.left instanceof Reference) && binary.right.constant != Constant.NotAConstant) -// return true; -// // if (0 < DEBUG_LEVEL) print(); - tolerated -// if ((binary.right instanceof Reference) && binary.left.constant != Constant.NotAConstant) -// return true; -// } -// } - return false; -} public StringBuffer printStatement(int indent, StringBuffer output) { printIndent(indent, output).append("if ("); //$NON-NLS-1$ Index: compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java,v retrieving revision 1.94 diff -u -r1.94 ConditionalExpression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 7 Mar 2009 01:08:07 -0000 1.94 +++ compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 15 Jul 2009 15:05:04 -0000 @@ -41,7 +41,8 @@ } public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, - FlowInfo flowInfo) { + FlowInfo flowInfo) { + int initialComplaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) != 0 ? Statement.COMPLAINED_FAKE_REACHABLE : Statement.NOT_COMPLAINED; Constant cst = this.condition.optimizedBooleanConstant(); boolean isConditionOptimizedTrue = cst != Constant.NotAConstant && cst.booleanValue() == true; boolean isConditionOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false; @@ -53,9 +54,11 @@ FlowInfo trueFlowInfo = flowInfo.initsWhenTrue().copy(); if (isConditionOptimizedFalse) { if ((mode & FlowInfo.UNREACHABLE) == 0) { - currentScope.problemReporter().fakeReachable(this.valueIfTrue); trueFlowInfo.setReachMode(FlowInfo.UNREACHABLE); } + if (!isKnowDeadCodePattern(this.condition) || currentScope.compilerOptions().reportDeadCodeInTrivialIfStatement) { + this.valueIfTrue.complainIfUnreachable(trueFlowInfo, currentScope, initialComplaintLevel); + } } this.trueInitStateIndex = currentScope.methodScope().recordInitializationStates(trueFlowInfo); trueFlowInfo = this.valueIfTrue.analyseCode(currentScope, flowContext, trueFlowInfo); @@ -64,9 +67,11 @@ FlowInfo falseFlowInfo = flowInfo.initsWhenFalse().copy(); if (isConditionOptimizedTrue) { if ((mode & FlowInfo.UNREACHABLE) == 0) { - currentScope.problemReporter().fakeReachable(this.valueIfFalse); falseFlowInfo.setReachMode(FlowInfo.UNREACHABLE); } + if (!isKnowDeadCodePattern(this.condition) || currentScope.compilerOptions().reportDeadCodeInTrivialIfStatement) { + this.valueIfFalse.complainIfUnreachable(falseFlowInfo, currentScope, initialComplaintLevel); + } } this.falseInitStateIndex = currentScope.methodScope().recordInitializationStates(falseFlowInfo); falseFlowInfo = this.valueIfFalse.analyseCode(currentScope, flowContext, falseFlowInfo);