View | Details | Raw Unified | Return to bug 282768 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java (+31 lines)
Lines 17-22 Link Here
17
17
18
public abstract class Statement extends ASTNode {
18
public abstract class Statement extends ASTNode {
19
19
20
	/**
21
	 * Answers true if the if is identified as a known coding pattern which
22
	 * should be tolerated by dead code analysis.
23
	 * e.g. if (DEBUG) print(); // no complaint
24
	 * Only invoked when overall condition is known to be optimizeable into false.
25
	 */
26
	protected static boolean isKnowDeadCodePattern(Expression expression) {
27
		// if (!DEBUG) print(); - tolerated
28
		if (expression instanceof UnaryExpression) {
29
			expression = ((UnaryExpression) expression).expression;
30
		}
31
		// if (DEBUG) print(); - tolerated
32
		if (expression instanceof Reference) return true;
33
34
//		if (expression instanceof BinaryExpression) {
35
//			BinaryExpression binary = (BinaryExpression) expression;
36
//			switch ((binary.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT/* operator */) {
37
//				case OperatorIds.AND_AND :
38
//				case OperatorIds.OR_OR :
39
//					break;
40
//				default: 
41
//					// if (DEBUG_LEVEL > 0) print(); - tolerated
42
//					if ((binary.left instanceof Reference) && binary.right.constant != Constant.NotAConstant)
43
//						return true;
44
//					// if (0 < DEBUG_LEVEL) print(); - tolerated
45
//					if ((binary.right instanceof Reference) && binary.left.constant != Constant.NotAConstant)
46
//						return true;
47
//			}
48
//		}
49
		return false;
50
	}
20
public abstract FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo);
51
public abstract FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo);
21
52
22
	public static final int NOT_COMPLAINED = 0;
53
	public static final int NOT_COMPLAINED = 0;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java (-30 lines)
Lines 205-241 Link Here
205
	codeStream.recordPositionsFrom(pc, this.sourceStart);
205
	codeStream.recordPositionsFrom(pc, this.sourceStart);
206
}
206
}
207
207
208
/**
209
 * Answers true if the if is identified as a known coding pattern which
210
 * should be tolerated by dead code analysis.
211
 * e.g. if (DEBUG) print(); // no complaint
212
 * Only invoked when overall condition is known to be optimizeable into false.
213
 */
214
public static boolean isKnowDeadCodePattern(Expression expression) {
215
	// if (!DEBUG) print(); - tolerated
216
	if (expression instanceof UnaryExpression) {
217
		expression = ((UnaryExpression) expression).expression;
218
	}
219
	// if (DEBUG) print(); - tolerated
220
	if (expression instanceof Reference) return true;
221
208
222
//	if (expression instanceof BinaryExpression) {
223
//		BinaryExpression binary = (BinaryExpression) expression;
224
//		switch ((binary.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT/* operator */) {
225
//			case OperatorIds.AND_AND :
226
//			case OperatorIds.OR_OR :
227
//				break;
228
//			default: 
229
//				// if (DEBUG_LEVEL > 0) print(); - tolerated
230
//				if ((binary.left instanceof Reference) && binary.right.constant != Constant.NotAConstant)
231
//					return true;
232
//				// if (0 < DEBUG_LEVEL) print(); - tolerated
233
//				if ((binary.right instanceof Reference) && binary.left.constant != Constant.NotAConstant)
234
//					return true;
235
//		}
236
//	}
237
	return false;
238
}
239
209
240
public StringBuffer printStatement(int indent, StringBuffer output) {
210
public StringBuffer printStatement(int indent, StringBuffer output) {
241
	printIndent(indent, output).append("if ("); //$NON-NLS-1$
211
	printIndent(indent, output).append("if ("); //$NON-NLS-1$
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java (-3 / +8 lines)
Lines 41-47 Link Here
41
	}
41
	}
42
42
43
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext,
43
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext,
44
		FlowInfo flowInfo) {
44
			FlowInfo flowInfo) {
45
		int initialComplaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) != 0 ? Statement.COMPLAINED_FAKE_REACHABLE : Statement.NOT_COMPLAINED;
45
		Constant cst = this.condition.optimizedBooleanConstant();
46
		Constant cst = this.condition.optimizedBooleanConstant();
46
		boolean isConditionOptimizedTrue = cst != Constant.NotAConstant && cst.booleanValue() == true;
47
		boolean isConditionOptimizedTrue = cst != Constant.NotAConstant && cst.booleanValue() == true;
47
		boolean isConditionOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;
48
		boolean isConditionOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;
Lines 53-61 Link Here
53
		FlowInfo trueFlowInfo = flowInfo.initsWhenTrue().copy();
54
		FlowInfo trueFlowInfo = flowInfo.initsWhenTrue().copy();
54
		if (isConditionOptimizedFalse) {
55
		if (isConditionOptimizedFalse) {
55
			if ((mode & FlowInfo.UNREACHABLE) == 0) {
56
			if ((mode & FlowInfo.UNREACHABLE) == 0) {
56
				currentScope.problemReporter().fakeReachable(this.valueIfTrue);
57
				trueFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
57
				trueFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
58
			}
58
			}
59
			if (!isKnowDeadCodePattern(this.condition) || currentScope.compilerOptions().reportDeadCodeInTrivialIfStatement) {
60
				this.valueIfTrue.complainIfUnreachable(trueFlowInfo, currentScope, initialComplaintLevel);
61
			}
59
		}
62
		}
60
		this.trueInitStateIndex = currentScope.methodScope().recordInitializationStates(trueFlowInfo);
63
		this.trueInitStateIndex = currentScope.methodScope().recordInitializationStates(trueFlowInfo);
61
		trueFlowInfo = this.valueIfTrue.analyseCode(currentScope, flowContext, trueFlowInfo);
64
		trueFlowInfo = this.valueIfTrue.analyseCode(currentScope, flowContext, trueFlowInfo);
Lines 64-72 Link Here
64
		FlowInfo falseFlowInfo = flowInfo.initsWhenFalse().copy();
67
		FlowInfo falseFlowInfo = flowInfo.initsWhenFalse().copy();
65
		if (isConditionOptimizedTrue) {
68
		if (isConditionOptimizedTrue) {
66
			if ((mode & FlowInfo.UNREACHABLE) == 0) {
69
			if ((mode & FlowInfo.UNREACHABLE) == 0) {
67
				currentScope.problemReporter().fakeReachable(this.valueIfFalse);
68
				falseFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
70
				falseFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
69
			}
71
			}
72
			if (!isKnowDeadCodePattern(this.condition) || currentScope.compilerOptions().reportDeadCodeInTrivialIfStatement) {
73
				this.valueIfFalse.complainIfUnreachable(falseFlowInfo, currentScope, initialComplaintLevel);
74
			}
70
		}
75
		}
71
		this.falseInitStateIndex = currentScope.methodScope().recordInitializationStates(falseFlowInfo);
76
		this.falseInitStateIndex = currentScope.methodScope().recordInitializationStates(falseFlowInfo);
72
		falseFlowInfo = this.valueIfFalse.analyseCode(currentScope, flowContext, falseFlowInfo);
77
		falseFlowInfo = this.valueIfFalse.analyseCode(currentScope, flowContext, falseFlowInfo);

Return to bug 282768