1
/*******************************************************************************
2
* Copyright (c) 2006, 2009 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* QNX - initial API and implementation, @author Doug Schaefer
10
*******************************************************************************/
12
package org.eclipse.cdt.core.errorparsers;
15
import java.io.IOException;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
19
import org.eclipse.cdt.core.ErrorParserManager;
20
import org.eclipse.cdt.core.IMarkerGenerator;
21
import org.eclipse.cdt.utils.CygPath;
22
import org.eclipse.core.resources.IResource;
23
import org.eclipse.core.runtime.IPath;
24
import org.eclipse.core.runtime.Path;
27
* Error Pattern - used by Error Parser to convert build output to problem markers
30
* Clients may extend this class.
32
public class ErrorPattern {
33
private final Pattern pattern;
34
private final int groupFileName;
35
private final int groupLineNum;
36
private final int groupDesc;
37
private final int groupVarName;
38
private final int severity;
40
private static boolean isCygwin = true;
43
* Full Pattern Constructor. Note that a group equal -1 means that
44
* the parameter is missing in the error message.
46
* @param pattern - regular expression describing the message
47
* @param groupFileName - matcher group of file name
48
* @param groupLineNum - matcher group of line number
49
* @param groupDesc - matcher group of description
50
* @param groupVarName - matcher group of variable name
51
* @param severity - severity, one of
52
* <br>{@link IMarkerGenerator#SEVERITY_INFO},
53
* <br>{@link IMarkerGenerator#SEVERITY_WARNING},
54
* <br>{@link IMarkerGenerator#SEVERITY_ERROR_RESOURCE},
55
* <br>{@link IMarkerGenerator#SEVERITY_ERROR_BUILD}
57
public ErrorPattern(String pattern,
63
this.pattern = Pattern.compile(pattern);
64
this.groupFileName = groupFileName;
65
this.groupLineNum = groupLineNum;
66
this.groupDesc = groupDesc;
67
this.groupVarName = groupVarName;
68
this.severity = severity;
72
* Pattern for errors not associated file a file
73
* (e.g. make and linker errors).
75
* @param pattern - regular expression describing the message
76
* @param groupDesc - matcher group of description
77
* @param severity - severity, one of
78
* <br>{@link IMarkerGenerator#SEVERITY_INFO},
79
* <br>{@link IMarkerGenerator#SEVERITY_WARNING},
80
* <br>{@link IMarkerGenerator#SEVERITY_ERROR_RESOURCE},
81
* <br>{@link IMarkerGenerator#SEVERITY_ERROR_BUILD}
83
public ErrorPattern(String pattern, int groupDesc, int severity) {
84
this(pattern, 0, 0, groupDesc, 0, severity);
88
* Pattern for errors that should be skipped.
90
* @param pattern - error pattern.
92
public ErrorPattern(String pattern) {
93
this(pattern, 0, 0, 0, 0, -1);
97
* @param input - input line.
98
* @return matcher to interpret the input line.
100
public Matcher getMatcher(CharSequence input) {
101
return pattern.matcher(input);
105
* @param matcher - matcher to parse the input line.
106
* @return parsed file name or {@code null}.
108
public String getFileName(Matcher matcher) {
109
return groupFileName != 0 ? matcher.group(groupFileName) : null;
113
* @param matcher - matcher to parse the input line.
114
* @return parsed line number or {@code 0}.
116
public int getLineNum(Matcher matcher) {
118
return groupLineNum != 0
119
? Integer.valueOf(matcher.group(groupLineNum)).intValue()
121
} catch (NumberFormatException e) {
127
* @param matcher - matcher to parse the input line.
128
* @return parsed description or {@code null}.
130
public String getDesc(Matcher matcher) {
131
return groupDesc != 0 ? matcher.group(groupDesc) : null;
135
* @param matcher - matcher to parse the input line.
136
* @return parsed variable name or {@code null}.
138
public String getVarName(Matcher matcher) {
139
return groupVarName != 0 ? matcher.group(groupVarName) : null;
143
* @param matcher - matcher to parse the input line.
144
* @return severity of the problem.
146
public int getSeverity(Matcher matcher) {
151
* Parse a line of build output and register error/warning for
154
* @param line - one line of output.
155
* @param eoParser - {@link ErrorParserManager}.
156
* @return {@code true} if error/warning/info problem was found.
158
public boolean processLine(String line, ErrorParserManager eoParser) {
159
Matcher matcher = getMatcher(line);
163
return recordError(matcher, eoParser);
167
* Register the error in {@link ErrorParserManager}.
169
* @param matcher - matcher to parse the input line.
170
* @param eoParser - {@link ErrorParserManager}.
171
* @return {@code true} indicating that error was found.
173
protected boolean recordError(Matcher matcher, ErrorParserManager eoParser) {
174
int severity = getSeverity(matcher);
179
String fileName = getFileName(matcher);
180
int lineNum = getLineNum(matcher);
181
String desc = getDesc(matcher);
182
String varName = getVarName(matcher);
183
IPath externalPath = null ;
185
IResource file = null;
186
if (fileName != null) {
187
file = eoParser.findFileName(fileName);
190
// If the file is not found in the workspace we attach the problem to the project
191
// and add the external path to the file.
192
file = eoParser.getProject();
193
externalPath = getLocation(fileName);
197
eoParser.generateExternalMarker(file, lineNum, desc, severity, varName, externalPath);
202
* If the file designated by filename exists, return the IPath representation of the filename
203
* If it does not exist, try cygpath translation
205
* @param filename - file name
206
* @return location (outside of the workspace).
208
protected IPath getLocation(String filename) {
209
IPath path = new Path(filename);
210
File file = path.toFile() ;
211
if (!file.exists() && isCygwin && path.isAbsolute()) {
212
CygPath cygpath = null ;
214
cygpath = new CygPath("cygpath"); //$NON-NLS-1$
215
String cygfilename = cygpath.getFileName(filename);
216
IPath convertedPath = new Path(cygfilename);
217
file = convertedPath.toFile() ;
219
path = convertedPath;
221
} catch (UnsupportedOperationException e) {
223
} catch (IOException e) {