Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / problem / ProblemHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.problem;
12
13 import org.eclipse.jdt.core.compiler.IProblem;
14 import org.eclipse.jdt.internal.compiler.CompilationResult;
15 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
16 import org.eclipse.jdt.internal.compiler.IProblemFactory;
17 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
18 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
19
20 /*
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.
24  *
25  * Behavior : will request its current policy if need to stop on
26  *      first error, and if should proceed (persist) with problems.
27  */
28
29 public class ProblemHandler implements ProblemSeverities {
30
31         public final static String[] NoArgument = new String[0];
32         
33         final public IErrorHandlingPolicy policy;
34         public final IProblemFactory problemFactory;
35         public final CompilerOptions options;
36 /*
37  * Problem handler can be supplied with a policy to specify
38  * its behavior in error handling. Also see static methods for
39  * built-in policies.
40  *
41  */
42 public ProblemHandler(IErrorHandlingPolicy policy, CompilerOptions options, IProblemFactory problemFactory) {
43         this.policy = policy;
44         this.problemFactory = problemFactory;
45         this.options = options;
46 }
47 /*
48  * Given the current configuration, answers which category the problem
49  * falls into:
50  *              Error | Warning | Ignore
51  */
52 public int computeSeverity(int problemId){
53         
54         return Error; // by default all problems are errors
55 }
56 public IProblem createProblem(
57         char[] fileName, 
58         int problemId, 
59         String[] problemArguments, 
60         String[] messageArguments,
61         int severity, 
62         int problemStartPosition, 
63         int problemEndPosition, 
64         int lineNumber) {
65
66         return this.problemFactory.createProblem(
67                 fileName, 
68                 problemId, 
69                 problemArguments, 
70                 messageArguments,
71                 severity, 
72                 problemStartPosition, 
73                 problemEndPosition, 
74                 lineNumber); 
75 }
76 public void handle(
77         int problemId, 
78         String[] problemArguments, 
79         String[] messageArguments,
80         int severity, 
81         int problemStartPosition, 
82         int problemEndPosition, 
83         ReferenceContext referenceContext, 
84         CompilationResult unitResult) {
85
86         if (severity == Ignore)
87                 return;
88
89         // if no reference context, we need to abort from the current compilation process
90         if (referenceContext == null) {
91                 if ((severity & Error) != 0) { // non reportable error is fatal
92                         IProblem problem = this.createProblem(null,     problemId,      problemArguments, messageArguments, severity, 0, 0, 0);                 
93                         throw new AbortCompilation(null, problem);
94                 } else {
95                         return; // ignore non reportable warning
96                 }
97         }
98
99         IProblem problem = 
100                 this.createProblem(
101                         unitResult.getFileName(), 
102                         problemId, 
103                         problemArguments, 
104                         messageArguments,
105                         severity, 
106                         problemStartPosition, 
107                         problemEndPosition, 
108                         problemStartPosition >= 0
109                                 ? searchLineNumber(unitResult.lineSeparatorPositions, problemStartPosition)
110                                 : 0);
111         if (problem == null) return; // problem couldn't be created, ignore
112         
113         switch (severity & Error) {
114                 case Error :
115                         this.record(problem, unitResult, referenceContext);
116                         referenceContext.tagAsHavingErrors();
117
118                         // should abort ?
119                         int abortLevel;
120                         if ((abortLevel = 
121                                 (this.policy.stopOnFirstError() ? AbortCompilation : severity & Abort)) != 0) {
122
123                                 referenceContext.abort(abortLevel, problem);
124                         }
125                         break;
126                 case Warning :
127                         this.record(problem, unitResult, referenceContext);
128                         break;
129         }
130 }
131 /**
132  * Standard problem handling API, the actual severity (warning/error/ignore) is deducted
133  * from the problem ID and the current compiler options.
134  */
135 public void handle(
136         int problemId, 
137         String[] problemArguments, 
138         String[] messageArguments,
139         int problemStartPosition, 
140         int problemEndPosition, 
141         ReferenceContext referenceContext, 
142         CompilationResult unitResult) {
143
144         this.handle(
145                 problemId,
146                 problemArguments,
147                 messageArguments,
148                 this.computeSeverity(problemId), // severity inferred using the ID
149                 problemStartPosition,
150                 problemEndPosition,
151                 referenceContext,
152                 unitResult);
153 }
154 public void record(IProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
155         unitResult.record(problem, referenceContext);
156 }
157 /**
158  * Search the line number corresponding to a specific position
159  */
160 public static final int searchLineNumber(int[] startLineIndexes, int position) {
161         if (startLineIndexes == null)
162                 return 1;
163         int length = startLineIndexes.length;
164         if (length == 0)
165                 return 1;
166         int g = 0, d = length - 1;
167         int m = 0;
168         while (g <= d) {
169                 m = (g + d) /2;
170                 if (position < startLineIndexes[m]) {
171                         d = m-1;
172                 } else if (position > startLineIndexes[m]) {
173                         g = m+1;
174                 } else {
175                         return m + 1;
176                 }
177         }
178         if (position < startLineIndexes[m]) {
179                 return m+1;
180         }
181         return m+2;
182 }
183 }