1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.problem;
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
15 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
16 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
17 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
18 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
21 * Compiler error handler, responsible to determine whether
22 * a problem is actually a warning or an error; also will
23 * decide whether the compilation task can be processed further or not.
25 * Behavior : will request its current policy if need to stop on
26 * first error, and if should proceed (persist) with problems.
29 public class ProblemHandler implements ProblemSeverities {
31 final public IErrorHandlingPolicy policy;
32 public final IProblemFactory problemFactory;
33 public final CompilerOptions options;
35 * Problem handler can be supplied with a policy to specify
36 * its behavior in error handling. Also see static methods for
40 public ProblemHandler(IErrorHandlingPolicy policy, CompilerOptions options, IProblemFactory problemFactory) {
42 this.problemFactory = problemFactory;
43 this.options = options;
46 * Given the current configuration, answers which category the problem
48 * Error | Warning | Ignore
50 public int computeSeverity(int problemId){
52 return Error; // by default all problems are errors
54 public IProblem createProblem(
57 String[] problemArguments,
59 int problemStartPosition,
60 int problemEndPosition,
62 ReferenceContext referenceContext,
63 CompilationResult unitResult) {
65 return problemFactory.createProblem(
76 String[] problemArguments,
78 int problemStartPosition,
79 int problemEndPosition,
80 ReferenceContext referenceContext,
81 CompilationResult unitResult) {
83 if (severity == Ignore)
86 // if no reference context, we need to abort from the current compilation process
87 if (referenceContext == null) {
88 if ((severity & Error) != 0) { // non reportable error is fatal
89 throw new AbortCompilation(problemId, problemArguments);
91 return; // ignore non reportable warning
97 unitResult.getFileName(),
101 problemStartPosition,
103 problemStartPosition >= 0
104 ? searchLineNumber(unitResult.lineSeparatorPositions, problemStartPosition)
108 if (problem == null) return; // problem couldn't be created, ignore
110 switch (severity & Error) {
112 this.record(problem, unitResult, referenceContext);
113 referenceContext.tagAsHavingErrors();
118 (policy.stopOnFirstError() ? AbortCompilation : severity & Abort)) != 0) {
120 referenceContext.abort(abortLevel);
124 this.record(problem, unitResult, referenceContext);
129 * Standard problem handling API, the actual severity (warning/error/ignore) is deducted
130 * from the problem ID and the current compiler options.
134 String[] problemArguments,
135 int problemStartPosition,
136 int problemEndPosition,
137 ReferenceContext referenceContext,
138 CompilationResult unitResult) {
143 this.computeSeverity(problemId), // severity inferred using the ID
144 problemStartPosition,
149 public void record(IProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
150 unitResult.record(problem, referenceContext);
153 * Search the line number corresponding to a specific position
155 * @param methodBinding org.eclipse.jdt.internal.compiler.nameloopkup.SyntheticAccessMethodBinding
157 public static final int searchLineNumber(int[] startLineIndexes, int position) {
158 if (startLineIndexes == null)
160 int length = startLineIndexes.length;
163 int g = 0, d = length - 1;
167 if (position < startLineIndexes[m]) {
169 } else if (position > startLineIndexes[m]) {
175 if (position < startLineIndexes[m]) {